Skip to content

Commit 518efa4

Browse files
Conversion between string and float moved into a new file
We remove the add_axioms_from_float that was defined in string_constraint_generator_valueof.cpp and use the new one from string_constraint_generator_float.cpp instead. We add string_constraint_generator_float.cpp to Makefile We also define an helper method for concatenation of char which can be used in float conversion. Improved documentation in string_constraint_generator_valueof and in string_constraint_generator_concat.
1 parent 5f28dc6 commit 518efa4

6 files changed

+61
-154
lines changed

src/solvers/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ SRC = $(BOOLEFORCE_SRC) \
165165
refinement/string_constraint_generator_constants.cpp \
166166
refinement/string_constraint_generator_indexof.cpp \
167167
refinement/string_constraint_generator_insert.cpp \
168+
refinement/string_constraint_generator_float.cpp \
168169
refinement/string_constraint_generator_main.cpp \
169170
refinement/string_constraint_generator_testing.cpp \
170171
refinement/string_constraint_generator_transformation.cpp \

src/solvers/refinement/string_constraint_generator.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class string_constraint_generatort
155155
string_exprt add_axioms_for_concat_long(const function_application_exprt &f);
156156
string_exprt add_axioms_for_concat_bool(const function_application_exprt &f);
157157
string_exprt add_axioms_for_concat_char(const function_application_exprt &f);
158+
string_exprt add_axioms_for_concat_char(
159+
const string_exprt &string_expr, const exprt &char_expr);
158160
string_exprt add_axioms_for_concat_double(
159161
const function_application_exprt &f);
160162
string_exprt add_axioms_for_concat_float(const function_application_exprt &f);
@@ -236,9 +238,14 @@ class string_constraint_generatort
236238
// the start for negative number
237239
string_exprt add_axioms_from_float(const function_application_exprt &f);
238240
string_exprt add_axioms_from_float(
239-
const exprt &f,
240-
const refined_string_typet &ref_type,
241-
bool double_precision);
241+
const exprt &f, const refined_string_typet &ref_type);
242+
243+
string_exprt add_axioms_for_fractional_part(
244+
const exprt &i, size_t max_size, const refined_string_typet &ref_type);
245+
string_exprt add_axioms_from_float_scientific_notation(
246+
const exprt &f, const refined_string_typet &ref_type);
247+
string_exprt add_axioms_from_float_scientific_notation(
248+
const function_application_exprt &f);
242249

243250
// Add axioms corresponding to the String.valueOf(D) java function
244251
// TODO: the specifications is only partial

src/solvers/refinement/string_constraint_generator_concat.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,22 @@ string_exprt string_constraint_generatort::add_axioms_for_concat_char(
148148
const function_application_exprt &f)
149149
{
150150
string_exprt s1=get_string_expr(args(f, 2)[0]);
151-
const refined_string_typet &ref_type=to_refined_string_type(s1.type());
152-
string_exprt s2=add_axioms_from_char(args(f, 2)[1], ref_type);
153-
return add_axioms_for_concat(s1, s2);
151+
return add_axioms_for_concat_char(s1, args(f, 2)[1]);
152+
}
153+
154+
/// Add axioms corresponding adding the character char at the end of
155+
/// string_expr.
156+
/// \param string_expr: a string expression
157+
/// \param char' a character expression
158+
/// \return a new string expression
159+
160+
string_exprt string_constraint_generatort::add_axioms_for_concat_char(
161+
const string_exprt &string_expr, const exprt &char_expr)
162+
{
163+
const refined_string_typet &ref_type=
164+
to_refined_string_type(string_expr.type());
165+
string_exprt s2=add_axioms_from_char(char_expr, ref_type);
166+
return add_axioms_for_concat(string_expr, s2);
154167
}
155168

156169
/// Add axioms corresponding to the StringBuilder.append(D) java function

src/solvers/refinement/string_constraint_generator_insert.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ string_exprt string_constraint_generatort::add_axioms_for_insert_double(
119119
{
120120
string_exprt s1=get_string_expr(args(f, 3)[0]);
121121
const refined_string_typet &ref_type=to_refined_string_type(s1.type());
122-
string_exprt s2=add_axioms_from_float(args(f, 3)[2], ref_type, true);
122+
string_exprt s2=add_axioms_from_float(args(f, 3)[2], ref_type);
123123
return add_axioms_for_insert(s1, s2, args(f, 3)[1]);
124124
}
125125

@@ -133,7 +133,7 @@ string_exprt string_constraint_generatort::add_axioms_for_insert_float(
133133
{
134134
string_exprt s1=get_string_expr(args(f, 3)[0]);
135135
const refined_string_typet &ref_type=to_refined_string_type(s1.type());
136-
string_exprt s2=add_axioms_from_float(args(f, 3)[2], ref_type, false);
136+
string_exprt s2=add_axioms_from_float(args(f, 3)[2], ref_type);
137137
return add_axioms_for_insert(s1, s2, args(f, 3)[1]);
138138
}
139139

src/solvers/refinement/string_constraint_generator_main.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ exprt string_constraint_generatort::add_axioms_for_function_application(
454454
res=add_axioms_from_int_hex(expr);
455455
else if(id==ID_cprover_string_of_float_func)
456456
res=add_axioms_from_float(expr);
457+
else if(id==ID_cprover_string_of_float_scientific_notation_func)
458+
res=add_axioms_from_float_scientific_notation(expr);
457459
else if(id==ID_cprover_string_of_double_func)
458460
res=add_axioms_from_double(expr);
459461
else if(id==ID_cprover_string_of_long_func)

src/solvers/refinement/string_constraint_generator_valueof.cpp

+30-146
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ Author: Romain Brenguier, [email protected]
1212
/// types, in particular int, long, float, double, char, bool
1313

1414
#include <solvers/refinement/string_constraint_generator.h>
15-
#include <solvers/floatbv/float_bv.h>
1615

17-
/// add axioms corresponding to the String.valueOf(I) java function
18-
/// \par parameters: function application with one integer argument
16+
/// Add axioms corresponding to the String.valueOf(I) java function.
17+
/// \param expr: function application with one integer argument
1918
/// \return a new string expression
2019
string_exprt string_constraint_generatort::add_axioms_from_int(
2120
const function_application_exprt &expr)
@@ -24,8 +23,8 @@ string_exprt string_constraint_generatort::add_axioms_from_int(
2423
return add_axioms_from_int(args(expr, 1)[0], MAX_INTEGER_LENGTH, ref_type);
2524
}
2625

27-
/// add axioms corresponding to the String.valueOf(J) java function
28-
/// \par parameters: function application with one long argument
26+
/// Add axioms corresponding to the String.valueOf(J) java function.
27+
/// \param expr: function application with one long argument
2928
/// \return a new string expression
3029
string_exprt string_constraint_generatort::add_axioms_from_long(
3130
const function_application_exprt &expr)
@@ -34,125 +33,8 @@ string_exprt string_constraint_generatort::add_axioms_from_long(
3433
return add_axioms_from_int(args(expr, 1)[0], MAX_LONG_LENGTH, ref_type);
3534
}
3635

37-
/// add axioms corresponding to the String.valueOf(F) java function
38-
/// \par parameters: function application with one float argument
39-
/// \return a new string expression
40-
string_exprt string_constraint_generatort::add_axioms_from_float(
41-
const function_application_exprt &f)
42-
{
43-
const refined_string_typet &ref_type=to_refined_string_type(f.type());
44-
return add_axioms_from_float(args(f, 1)[0], ref_type, false);
45-
}
46-
47-
/// add axioms corresponding to the String.valueOf(D) java function
48-
/// \par parameters: function application with one double argument
49-
/// \return a new string expression
50-
string_exprt string_constraint_generatort::add_axioms_from_double(
51-
const function_application_exprt &f)
52-
{
53-
const refined_string_typet &ref_type=to_refined_string_type(f.type());
54-
return add_axioms_from_float(args(f, 1)[0], ref_type, true);
55-
}
56-
57-
/// add axioms corresponding to the String.valueOf(F) java function Warning: we
58-
/// currently only have partial specification
59-
/// \par parameters: float expression and Boolean signaling that the argument
60-
/// has
61-
/// double precision
62-
/// \return a new string expression
63-
string_exprt string_constraint_generatort::add_axioms_from_float(
64-
const exprt &f, const refined_string_typet &ref_type, bool double_precision)
65-
{
66-
string_exprt res=fresh_string(ref_type);
67-
const typet &index_type=ref_type.get_index_type();
68-
const typet &char_type=ref_type.get_char_type();
69-
const exprt &index24=from_integer(24, index_type);
70-
axioms.push_back(res.axiom_for_is_shorter_than(index24));
71-
72-
string_exprt magnitude=fresh_string(ref_type);
73-
string_exprt sign_string=fresh_string(ref_type);
74-
string_exprt nan_string=add_axioms_for_constant("NaN", ref_type);
75-
76-
// We add the axioms:
77-
// a1 : If the argument is NaN, the result length is that of "NaN".
78-
// a2 : If the argument is NaN, the result content is the string "NaN".
79-
// a3 : f<0 => |sign_string|=1
80-
// a4 : f>=0 => |sign_string|=0
81-
// a5 : f<0 => sign_string[0]='-'
82-
// a6 : f infinite => |magnitude|=|"Infinity"|
83-
// a7 : forall i<|"Infinity"|, f infinite => magnitude[i]="Infinity"[i]
84-
// a8 : f=0 => |magnitude|=|"0.0"|
85-
// a9 : forall i<|"0.0"|, f=0 => f[i]="0.0"[i]
86-
87-
ieee_float_spect fspec=
88-
double_precision?ieee_float_spect::double_precision()
89-
:ieee_float_spect::single_precision();
90-
91-
exprt isnan=float_bvt().isnan(f, fspec);
92-
implies_exprt a1(isnan, magnitude.axiom_for_has_same_length_as(nan_string));
93-
axioms.push_back(a1);
94-
95-
symbol_exprt qvar=fresh_univ_index("QA_equal_nan", index_type);
96-
string_constraintt a2(
97-
qvar,
98-
nan_string.length(),
99-
isnan,
100-
equal_exprt(magnitude[qvar], nan_string[qvar]));
101-
axioms.push_back(a2);
102-
103-
// If the argument is not NaN, the result is a string that represents
104-
// the sign and magnitude (absolute value) of the argument.
105-
// If the sign is negative, the first character of the result is '-';
106-
// if the sign is positive, no sign character appears in the result.
107-
108-
bitvector_typet bv_type=to_bitvector_type(f.type());
109-
unsigned width=bv_type.get_width();
110-
exprt isneg=extractbit_exprt(f, width-1);
111-
112-
implies_exprt a3(isneg, sign_string.axiom_for_has_length(1));
113-
axioms.push_back(a3);
114-
115-
implies_exprt a4(not_exprt(isneg), sign_string.axiom_for_has_length(0));
116-
axioms.push_back(a4);
117-
118-
implies_exprt a5(
119-
isneg, equal_exprt(sign_string[0], constant_char('-', char_type)));
120-
axioms.push_back(a5);
121-
122-
// If m is infinity, it is represented by the characters "Infinity";
123-
// thus, positive infinity produces the result "Infinity" and negative
124-
// infinity produces the result "-Infinity".
125-
126-
string_exprt infinity_string=add_axioms_for_constant("Infinity", ref_type);
127-
exprt isinf=float_bvt().isinf(f, fspec);
128-
implies_exprt a6(
129-
isinf, magnitude.axiom_for_has_same_length_as(infinity_string));
130-
axioms.push_back(a6);
131-
132-
symbol_exprt qvar_inf=fresh_univ_index("QA_equal_infinity", index_type);
133-
equal_exprt meq(magnitude[qvar_inf], infinity_string[qvar_inf]);
134-
string_constraintt a7(qvar_inf, infinity_string.length(), isinf, meq);
135-
axioms.push_back(a7);
136-
137-
// If m is zero, it is represented by the characters "0.0"; thus, negative
138-
// zero produces the result "-0.0" and positive zero produces "0.0".
139-
140-
string_exprt zero_string=add_axioms_for_constant("0.0", ref_type);
141-
exprt iszero=float_bvt().is_zero(f, fspec);
142-
implies_exprt a8(iszero, magnitude.axiom_for_has_same_length_as(zero_string));
143-
axioms.push_back(a8);
144-
145-
symbol_exprt qvar_zero=fresh_univ_index("QA_equal_zero", index_type);
146-
equal_exprt eq_zero(magnitude[qvar_zero], zero_string[qvar_zero]);
147-
string_constraintt a9(qvar_zero, zero_string.length(), iszero, eq_zero);
148-
axioms.push_back(a9);
149-
150-
return add_axioms_for_concat(sign_string, magnitude);
151-
}
152-
153-
154-
/// add axioms corresponding to the String.valueOf(Z) java function
155-
/// \par parameters: function application with on Boolean argument
36+
/// Add axioms corresponding to the String.valueOf(Z) java function.
37+
/// \param f: function application with a Boolean argument
15638
/// \return a new string expression
15739
string_exprt string_constraint_generatort::add_axioms_from_bool(
15840
const function_application_exprt &f)
@@ -161,10 +43,10 @@ string_exprt string_constraint_generatort::add_axioms_from_bool(
16143
return add_axioms_from_bool(args(f, 1)[0], ref_type);
16244
}
16345

164-
165-
/// add axioms stating that the returned string equals "true" when the Boolean
166-
/// expression is true and "false" when it is false
167-
/// \par parameters: Boolean expression
46+
/// Add axioms stating that the returned string equals "true" when the Boolean
47+
/// expression is true and "false" when it is false.
48+
/// \param b: Boolean expression
49+
/// \param ref_type: type of refined string expressions
16850
/// \return a new string expression
16951
string_exprt string_constraint_generatort::add_axioms_from_bool(
17052
const exprt &b, const refined_string_typet &ref_type)
@@ -329,8 +211,8 @@ string_exprt string_constraint_generatort::add_axioms_from_int(
329211
return res;
330212
}
331213

332-
/// returns the value represented by the character
333-
/// \par parameters: a character expression in the following set:
214+
/// Returns the integer value represented by the character.
215+
/// \param chr: a character expression in the following set:
334216
/// 0123456789abcdef
335217
/// \return an integer expression
336218
exprt string_constraint_generatort::int_of_hex_char(const exprt &chr) const
@@ -344,9 +226,10 @@ exprt string_constraint_generatort::int_of_hex_char(const exprt &chr) const
344226
minus_exprt(chr, zero_char));
345227
}
346228

347-
/// add axioms stating that the returned string corresponds to the integer
348-
/// argument written in hexadecimal
349-
/// \par parameters: one integer argument
229+
/// Add axioms stating that the returned string corresponds to the integer
230+
/// argument written in hexadecimal.
231+
/// \param i: an integer argument
232+
/// \param ref_type: type of refined string expressions
350233
/// \return a new string expression
351234
string_exprt string_constraint_generatort::add_axioms_from_int_hex(
352235
const exprt &i, const refined_string_typet &ref_type)
@@ -402,7 +285,7 @@ string_exprt string_constraint_generatort::add_axioms_from_int_hex(
402285
}
403286

404287
/// add axioms corresponding to the Integer.toHexString(I) java function
405-
/// \par parameters: function application with integer argument
288+
/// \param f: function application with an integer argument
406289
/// \return a new string expression
407290
string_exprt string_constraint_generatort::add_axioms_from_int_hex(
408291
const function_application_exprt &f)
@@ -411,8 +294,8 @@ string_exprt string_constraint_generatort::add_axioms_from_int_hex(
411294
return add_axioms_from_int_hex(args(f, 1)[0], ref_type);
412295
}
413296

414-
/// add axioms corresponding to the String.valueOf(C) java function
415-
/// \par parameters: function application one char argument
297+
/// Add axioms corresponding to the String.valueOf(C) java function.
298+
/// \param f: function application one char argument
416299
/// \return a new string expression
417300
string_exprt string_constraint_generatort::add_axioms_from_char(
418301
const function_application_exprt &f)
@@ -421,9 +304,10 @@ string_exprt string_constraint_generatort::add_axioms_from_char(
421304
return add_axioms_from_char(args(f, 1)[0], ref_type);
422305
}
423306

424-
/// add axioms stating that the returned string has length 1 and the character
425-
/// it contains correspond to the input expression
426-
/// \par parameters: one expression of type char
307+
/// Add axioms stating that the returned string has length 1 and the character
308+
/// it contains correspond to the input expression.
309+
/// \param c: one expression of type char
310+
/// \param ref_type: type of refined string expressions
427311
/// \return a new string expression
428312
string_exprt string_constraint_generatort::add_axioms_from_char(
429313
const exprt &c, const refined_string_typet &ref_type)
@@ -434,9 +318,9 @@ string_exprt string_constraint_generatort::add_axioms_from_char(
434318
return res;
435319
}
436320

437-
/// add axioms corresponding to the String.valueOf([C) and String.valueOf([CII)
438-
/// functions
439-
/// \par parameters: function application with one or three arguments
321+
/// Add axioms corresponding to the String.valueOf([C) and String.valueOf([CII)
322+
/// functions.
323+
/// \param f: function application with one or three arguments
440324
/// \return a new string expression
441325
string_exprt string_constraint_generatort::add_axioms_for_value_of(
442326
const function_application_exprt &f)
@@ -471,9 +355,9 @@ string_exprt string_constraint_generatort::add_axioms_for_value_of(
471355
}
472356
}
473357

474-
/// add axioms making the return value true if the given string is a correct
475-
/// number
476-
/// \par parameters: function application with one string expression
358+
/// Add axioms making the return value true if the given string is a correct
359+
/// number.
360+
/// \param f: function application with one string expression
477361
/// \return an boolean expression
478362
exprt string_constraint_generatort::add_axioms_for_correct_number_format(
479363
const string_exprt &str, std::size_t max_size)
@@ -527,7 +411,7 @@ exprt string_constraint_generatort::add_axioms_for_correct_number_format(
527411
}
528412

529413
/// add axioms corresponding to the Integer.parseInt java function
530-
/// \par parameters: function application with one string expression
414+
/// \param f: function application with one string expression
531415
/// \return an integer expression
532416
exprt string_constraint_generatort::add_axioms_for_parse_int(
533417
const function_application_exprt &f)

0 commit comments

Comments
 (0)