Skip to content

Commit 651246e

Browse files
Extract convert_if_cmp function
1 parent fc95df1 commit 651246e

File tree

2 files changed

+72
-32
lines changed

2 files changed

+72
-32
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,26 +1575,8 @@ codet java_bytecode_convert_methodt::convert_instructions(
15751575
mp_integer number;
15761576
bool ret=to_integer(to_constant_expr(arg0), number);
15771577
INVARIANT(!ret, "if_?cmp?? argument should be an integer");
1578-
1579-
code_ifthenelset code_branch;
1580-
const irep_idt cmp_op=get_if_cmp_operator(statement);
1581-
1582-
binary_relation_exprt condition(op[0], cmp_op, op[1]);
1583-
1584-
exprt &lhs(condition.lhs());
1585-
exprt &rhs(condition.rhs());
1586-
const typet &lhs_type(lhs.type());
1587-
if(lhs_type!=rhs.type())
1588-
rhs=typecast_exprt(rhs, lhs_type);
1589-
1590-
code_branch.cond()=condition;
1591-
code_branch.cond().add_source_location()=i_it->source_location;
1592-
code_branch.then_case()=code_gotot(label(integer2string(number)));
1593-
code_branch.then_case().add_source_location()=
1594-
address_map.at(integer2unsigned(number)).source->source_location;
1595-
code_branch.add_source_location()=i_it->source_location;
1596-
1597-
c=code_branch;
1578+
c = convert_if_cmp(
1579+
address_map, statement, op, number, i_it->source_location);
15981580
}
15991581
else if(statement==patternt("if??"))
16001582
{
@@ -2593,6 +2575,34 @@ codet java_bytecode_convert_methodt::convert_instructions(
25932575
return code;
25942576
}
25952577

2578+
codet java_bytecode_convert_methodt::convert_if_cmp(
2579+
const java_bytecode_convert_methodt::address_mapt &address_map,
2580+
const irep_idt &statement,
2581+
const exprt::operandst &op,
2582+
const mp_integer &number,
2583+
const source_locationt &location) const
2584+
{
2585+
code_ifthenelset code_branch;
2586+
const irep_idt cmp_op = get_if_cmp_operator(statement);
2587+
2588+
binary_relation_exprt condition(op[0], cmp_op, op[1]);
2589+
2590+
exprt &lhs(condition.lhs());
2591+
exprt &rhs(condition.rhs());
2592+
const typet &lhs_type(lhs.type());
2593+
if(lhs_type != rhs.type())
2594+
rhs = typecast_exprt(rhs, lhs_type);
2595+
2596+
code_branch.cond() = condition;
2597+
code_branch.cond().add_source_location() = location;
2598+
code_branch.then_case() = code_gotot(label(integer2string(number)));
2599+
code_branch.then_case().add_source_location() =
2600+
address_map.at(integer2unsigned(number)).source->source_location;
2601+
code_branch.add_source_location() = location;
2602+
2603+
return code_branch;
2604+
}
2605+
25962606
code_blockt java_bytecode_convert_methodt::convert_ret(
25972607
const std::vector<unsigned int> &jsr_ret_targets,
25982608
const exprt &arg0,

jbmc/src/java_bytecode/java_bytecode_convert_method_class.h

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ class java_bytecode_convert_methodt:public messaget
110110
size_t length;
111111
bool is_parameter;
112112
std::vector<holet> holes;
113-
variablet() : symbol_expr(), start_pc(0), length(0), is_parameter(false) {}
113+
114+
variablet() : symbol_expr(), start_pc(0), length(0), is_parameter(false)
115+
{
116+
}
114117
};
115118

116119
protected:
@@ -124,8 +127,8 @@ class java_bytecode_convert_methodt:public messaget
124127

125128
enum instruction_sizet
126129
{
127-
INST_INDEX=2,
128-
INST_INDEX_CONST=3
130+
INST_INDEX = 2,
131+
INST_INDEX_CONST = 3
129132
};
130133

131134
// return corresponding reference of variable
@@ -161,22 +164,25 @@ class java_bytecode_convert_methodt:public messaget
161164
exprt::operandst pop(std::size_t n);
162165

163166
void pop_residue(std::size_t n);
167+
164168
void push(const exprt::operandst &o);
165169

166170
/// Returns true iff the slot index of the local variable of a method (coming
167171
/// from the LVT) is a parameter of that method. Assumes that
168172
/// `slots_for_parameters` is initialized upon call.
169173
bool is_parameter(const local_variablet &v)
170174
{
171-
return v.index<slots_for_parameters;
175+
return v.index < slots_for_parameters;
172176
}
173177

174178
struct converted_instructiont
175179
{
176180
converted_instructiont(
177181
const instructionst::const_iterator &it,
178-
const codet &_code):source(it), code(_code), done(false)
179-
{}
182+
const codet &_code)
183+
: source(it), code(_code), done(false)
184+
{
185+
}
180186

181187
instructionst::const_iterator source;
182188
std::list<unsigned> successors;
@@ -211,9 +217,19 @@ class java_bytecode_convert_methodt:public messaget
211217
bool leaf;
212218
std::vector<unsigned> branch_addresses;
213219
std::vector<block_tree_nodet> branch;
214-
block_tree_nodet():leaf(false) {}
215-
explicit block_tree_nodet(bool l):leaf(l) {}
216-
static block_tree_nodet get_leaf() { return block_tree_nodet(true); }
220+
221+
block_tree_nodet() : leaf(false)
222+
{
223+
}
224+
225+
explicit block_tree_nodet(bool l) : leaf(l)
226+
{
227+
}
228+
229+
static block_tree_nodet get_leaf()
230+
{
231+
return block_tree_nodet(true);
232+
}
217233
};
218234

219235
static void replace_goto_target(
@@ -235,7 +251,7 @@ class java_bytecode_convert_methodt:public messaget
235251
unsigned address_limit,
236252
unsigned next_block_start_address,
237253
const address_mapt &amap,
238-
bool allow_merge=true);
254+
bool allow_merge = true);
239255

240256
optionalt<symbolt> get_lambda_method_symbol(
241257
const java_class_typet::java_lambda_method_handlest &lambda_method_handles,
@@ -261,13 +277,21 @@ class java_bytecode_convert_methodt:public messaget
261277
irep_idt get_static_field(
262278
const irep_idt &class_identifier, const irep_idt &component_name) const;
263279

264-
enum class bytecode_write_typet { VARIABLE, ARRAY_REF, STATIC_FIELD, FIELD};
280+
enum class bytecode_write_typet
281+
{
282+
VARIABLE,
283+
ARRAY_REF,
284+
STATIC_FIELD,
285+
FIELD
286+
};
287+
265288
void save_stack_entries(
266289
const std::string &,
267290
const typet &,
268291
code_blockt &,
269292
const bytecode_write_typet,
270293
const irep_idt &);
294+
271295
void create_stack_tmp_var(
272296
const std::string &,
273297
const typet &,
@@ -311,6 +335,12 @@ class java_bytecode_convert_methodt:public messaget
311335
const exprt &arg0,
312336
const source_locationt &location,
313337
unsigned address);
314-
};
338+
339+
codet convert_if_cmp(
340+
const java_bytecode_convert_methodt::address_mapt &address_map,
341+
const irep_idt &statement,
342+
const exprt::operandst &op,
343+
const mp_integer &number,
344+
const source_locationt &location) const;
315345

316346
#endif

0 commit comments

Comments
 (0)