Skip to content

Commit 5a88619

Browse files
author
thk123
committed
Split up gen_nondet_init function
Broke up the code handling initilising a pointer into a standalone function.
1 parent e0b4515 commit 5a88619

File tree

1 file changed

+81
-47
lines changed

1 file changed

+81
-47
lines changed

src/java_bytecode/java_object_factory.cpp

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ class java_object_factoryt
110110
bool create_dynamic_objects,
111111
bool override=false,
112112
const typet &override_type=empty_typet());
113+
114+
private:
115+
void gen_nondet_pointer_init(
116+
code_blockt &assignments,
117+
const exprt &expr,
118+
const irep_idt &class_identifier,
119+
bool create_dynamic_objects,
120+
const pointer_typet &pointer_type);
113121
};
114122

115123
/// \param assignments: The code block to add code to
@@ -250,6 +258,74 @@ void java_object_factoryt::gen_pointer_target_init(
250258
}
251259
}
252260

261+
/// Initialises a primitive or object tree rooted at `expr`, of type pointer. It
262+
/// allocates child objects as necessary and nondet-initialising their members,
263+
/// \param assignments - the code block we are building with
264+
/// initilisation code
265+
/// \param expr: lvalue expression to initialise
266+
/// \param class_identifier - the name of the class so we can identify
267+
/// special cases where a null pointer is not applicable.
268+
/// \param create_dynamic_objects: if true, use malloc to allocate objects;
269+
/// otherwise generate fresh static symbols.
270+
/// \param pointer_type - The type of the pointer we are initalising
271+
void java_object_factoryt::gen_nondet_pointer_init(
272+
code_blockt &assignments,
273+
const exprt &expr,
274+
const irep_idt &class_identifier,
275+
bool create_dynamic_objects,
276+
const pointer_typet &pointer_type)
277+
{
278+
const typet &subtype=ns.follow(pointer_type.subtype());
279+
280+
if(subtype.id()==ID_struct)
281+
{
282+
const struct_typet &struct_type=to_struct_type(subtype);
283+
const irep_idt struct_tag=struct_type.get_tag();
284+
// set to null if found in recursion set and not a sub-type
285+
if(recursion_set.find(struct_tag)!=recursion_set.end() &&
286+
struct_tag==class_identifier)
287+
{
288+
assignments.copy_to_operands(
289+
get_null_assignment(expr, pointer_type));
290+
return;
291+
}
292+
}
293+
294+
code_blockt non_null_inst;
295+
gen_pointer_target_init(
296+
non_null_inst,
297+
expr,
298+
subtype,
299+
create_dynamic_objects);
300+
301+
if(assume_non_null)
302+
{
303+
// Add the following code to assignments:
304+
// <expr> = <aoe>;
305+
assignments.append(non_null_inst);
306+
}
307+
else
308+
{
309+
// if(NONDET(_Bool)
310+
// {
311+
// <expr> = <null pointer>
312+
// }
313+
// else
314+
// {
315+
// <code from recursive call to gen_nondet_init() with
316+
// tmp$<temporary_counter>>
317+
// }
318+
auto set_null_inst=get_null_assignment(expr, pointer_type);
319+
320+
code_ifthenelset null_check;
321+
null_check.cond()=side_effect_expr_nondett(bool_typet());
322+
null_check.then_case()=set_null_inst;
323+
null_check.else_case()=non_null_inst;
324+
325+
assignments.add(null_check);
326+
}
327+
}
328+
253329
/// Creates a nondet for expr, including calling itself recursively to make
254330
/// appropriate symbols to point to if expr is a pointer or struct
255331
/// \param expr: The expression which we are generating a non-determinate value
@@ -277,54 +353,12 @@ void java_object_factoryt::gen_nondet_init(
277353
{
278354
// dereferenced type
279355
const pointer_typet &pointer_type=to_pointer_type(type);
280-
const typet &subtype=ns.follow(pointer_type.subtype());
281-
282-
if(subtype.id()==ID_struct)
283-
{
284-
const struct_typet &struct_type=to_struct_type(subtype);
285-
const irep_idt struct_tag=struct_type.get_tag();
286-
// set to null if found in recursion set and not a sub-type
287-
if(recursion_set.find(struct_tag)!=recursion_set.end() &&
288-
struct_tag==class_identifier)
289-
{
290-
assignments.copy_to_operands(
291-
get_null_assignment(expr, pointer_type));
292-
return;
293-
}
294-
}
295-
296-
code_blockt non_null_inst;
297-
gen_pointer_target_init(
298-
non_null_inst,
356+
gen_nondet_pointer_init(
357+
assignments,
299358
expr,
300-
subtype,
301-
create_dynamic_objects);
302-
303-
if(assume_non_null)
304-
{
305-
// Add the following code to assignments:
306-
// <expr> = <aoe>;
307-
assignments.append(non_null_inst);
308-
}
309-
else
310-
{
311-
// Add the following code to assignments:
312-
// IF !NONDET(_Bool) THEN GOTO <label1>
313-
// <expr> = <null pointer>
314-
// GOTO <label2>
315-
// <label1>: <expr> = &tmp$<temporary_counter>;
316-
// <code from recursive call to gen_nondet_init() with
317-
// tmp$<temporary_counter>>
318-
// And the next line is labelled label2
319-
auto set_null_inst=get_null_assignment(expr, pointer_type);
320-
321-
code_ifthenelset null_check;
322-
null_check.cond()=side_effect_expr_nondett(bool_typet());
323-
null_check.then_case()=set_null_inst;
324-
null_check.else_case()=non_null_inst;
325-
326-
assignments.add(null_check);
327-
}
359+
class_identifier,
360+
create_dynamic_objects,
361+
pointer_type);
328362
}
329363
else if(type.id()==ID_struct)
330364
{

0 commit comments

Comments
 (0)