Skip to content

Commit 86b503d

Browse files
committed
Use allocate_objectst in the java object factory
1 parent 460db7b commit 86b503d

File tree

2 files changed

+7
-211
lines changed

2 files changed

+7
-211
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 6 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ class java_object_factoryt
9494
pointer_type_selector(pointer_type_selector)
9595
{}
9696

97-
exprt allocate_object(
98-
code_blockt &assignments,
99-
const exprt &,
100-
const typet &,
101-
allocation_typet alloc_type);
102-
10397
void gen_nondet_array_init(
10498
code_blockt &assignments,
10599
const exprt &expr,
@@ -168,182 +162,6 @@ class java_object_factoryt
168162
const irep_idt &method_name);
169163
};
170164

171-
/// Generates code for allocating a dynamic object. This is used in
172-
/// allocate_object() and also in the library preprocessing for allocating
173-
/// strings.
174-
/// \param target_expr: expression to which the necessary memory will be
175-
/// allocated, its type should be pointer to `allocate_type`
176-
/// \param allocate_type: type of the object allocated
177-
/// \param symbol_table: symbol table
178-
/// \param loc: location in the source
179-
/// \param function_id: function ID to associate with auxiliary variables
180-
/// \param output_code: code block to which the necessary code is added
181-
/// \param symbols_created: created symbols to be declared by the caller
182-
/// \param cast_needed: Boolean flags saying where we need to cast the malloc
183-
/// site
184-
/// \return Expression representing the malloc site allocated.
185-
exprt allocate_dynamic_object(
186-
const exprt &target_expr,
187-
const typet &allocate_type,
188-
symbol_table_baset &symbol_table,
189-
const source_locationt &loc,
190-
const irep_idt &function_id,
191-
code_blockt &output_code,
192-
std::vector<const symbolt *> &symbols_created,
193-
bool cast_needed)
194-
{
195-
// build size expression
196-
exprt object_size=size_of_expr(allocate_type, namespacet(symbol_table));
197-
198-
if(allocate_type.id()!=ID_empty)
199-
{
200-
INVARIANT(!object_size.is_nil(), "Size of Java objects should be known");
201-
// malloc expression
202-
side_effect_exprt malloc_expr(
203-
ID_allocate, pointer_type(allocate_type), loc);
204-
malloc_expr.copy_to_operands(object_size);
205-
malloc_expr.copy_to_operands(false_exprt());
206-
// create a symbol for the malloc expression so we can initialize
207-
// without having to do it potentially through a double-deref, which
208-
// breaks the to-SSA phase.
209-
symbolt &malloc_sym = get_fresh_aux_symbol(
210-
pointer_type(allocate_type),
211-
id2string(function_id),
212-
"malloc_site",
213-
loc,
214-
ID_java,
215-
symbol_table);
216-
symbols_created.push_back(&malloc_sym);
217-
code_assignt assign(malloc_sym.symbol_expr(), malloc_expr);
218-
assign.add_source_location()=loc;
219-
output_code.add(assign);
220-
exprt malloc_symbol_expr=malloc_sym.symbol_expr();
221-
if(cast_needed)
222-
malloc_symbol_expr=typecast_exprt(malloc_symbol_expr, target_expr.type());
223-
code_assignt code(target_expr, malloc_symbol_expr);
224-
code.add_source_location()=loc;
225-
output_code.add(code);
226-
return malloc_sym.symbol_expr();
227-
}
228-
else
229-
{
230-
// make null
231-
null_pointer_exprt null_pointer_expr(to_pointer_type(target_expr.type()));
232-
code_assignt code(target_expr, null_pointer_expr);
233-
code.add_source_location()=loc;
234-
output_code.add(code);
235-
return exprt();
236-
}
237-
}
238-
239-
/// Generates code for allocating a dynamic object. This is a static version of
240-
/// allocate_dynamic_object that can be called from outside java_object_factory
241-
/// and which takes care of creating the associated declarations.
242-
/// \param target_expr: expression to which the necessary memory will be
243-
/// allocated
244-
/// \param symbol_table: symbol table
245-
/// \param loc: location in the source
246-
/// \param function_id: function ID to associate with auxiliary variables
247-
/// \param output_code: code block to which the necessary code is added
248-
/// \return the dynamic object created
249-
exprt allocate_dynamic_object_with_decl(
250-
const exprt &target_expr,
251-
symbol_table_baset &symbol_table,
252-
const source_locationt &loc,
253-
const irep_idt &function_id,
254-
code_blockt &output_code)
255-
{
256-
std::vector<const symbolt *> symbols_created;
257-
code_blockt tmp_block;
258-
const typet &allocate_type=target_expr.type().subtype();
259-
const exprt dynamic_object = allocate_dynamic_object(
260-
target_expr,
261-
allocate_type,
262-
symbol_table,
263-
loc,
264-
function_id,
265-
tmp_block,
266-
symbols_created,
267-
false);
268-
269-
// Add the following code to output_code for each symbol that's been created:
270-
// <type> <identifier>;
271-
for(const symbolt * const symbol_ptr : symbols_created)
272-
{
273-
code_declt decl(symbol_ptr->symbol_expr());
274-
decl.add_source_location()=loc;
275-
output_code.add(decl);
276-
}
277-
278-
for(const auto &code : tmp_block.statements())
279-
output_code.add(code);
280-
281-
return dynamic_object;
282-
}
283-
284-
/// Installs a new symbol in the symbol table, pushing the corresponding symbolt
285-
/// object to the field `symbols_created` and emits to \p assignments a new
286-
/// assignment of the form `<target_expr> := address-of(new_object)`. The
287-
/// \p allocate_type may differ from `target_expr.type()`, e.g. for target_expr
288-
/// having type int* and allocate_type being an int[10].
289-
///
290-
/// \param assignments: The code block to add code to.
291-
/// \param target_expr: The expression which we are allocating a symbol for.
292-
/// \param allocate_type:
293-
/// \param alloc_type: Allocation type (global, local or dynamic)
294-
/// \return An address_of_exprt of the newly allocated object.
295-
exprt java_object_factoryt::allocate_object(
296-
code_blockt &assignments,
297-
const exprt &target_expr,
298-
const typet &allocate_type,
299-
allocation_typet alloc_type)
300-
{
301-
const typet &allocate_type_resolved=ns.follow(allocate_type);
302-
const typet &target_type=ns.follow(target_expr.type().subtype());
303-
bool cast_needed=allocate_type_resolved!=target_type;
304-
switch(alloc_type)
305-
{
306-
case allocation_typet::LOCAL:
307-
case allocation_typet::GLOBAL:
308-
{
309-
symbolt &aux_symbol = get_fresh_aux_symbol(
310-
allocate_type,
311-
id2string(object_factory_parameters.function_id),
312-
"tmp_object_factory",
313-
loc,
314-
ID_java,
315-
symbol_table);
316-
if(alloc_type==allocation_typet::GLOBAL)
317-
aux_symbol.is_static_lifetime=true;
318-
symbols_created.push_back(&aux_symbol);
319-
320-
exprt object=aux_symbol.symbol_expr();
321-
exprt aoe=address_of_exprt(object);
322-
if(cast_needed)
323-
aoe=typecast_exprt(aoe, target_expr.type());
324-
code_assignt code(target_expr, aoe);
325-
code.add_source_location()=loc;
326-
assignments.add(code);
327-
return aoe;
328-
}
329-
case allocation_typet::DYNAMIC:
330-
{
331-
return allocate_dynamic_object(
332-
target_expr,
333-
allocate_type,
334-
symbol_table,
335-
loc,
336-
object_factory_parameters.function_id,
337-
assignments,
338-
symbols_created,
339-
cast_needed);
340-
}
341-
default:
342-
UNREACHABLE;
343-
return exprt();
344-
} // End switch
345-
}
346-
347165
/// Returns a codet that assigns \p expr, of type \p ptr_type, a NULL value.
348166
code_assignt java_object_factoryt::get_null_assignment(
349167
const exprt &expr,
@@ -433,7 +251,12 @@ void java_object_factoryt::gen_pointer_target_init(
433251
exprt target;
434252
if(update_in_place == update_in_placet::NO_UPDATE_IN_PLACE)
435253
{
436-
target = allocate_object(assignments, expr, target_type, alloc_type);
254+
allocate_objectst allocate_objects(
255+
ID_java, loc, object_factory_parameters.function_id, symbol_table);
256+
257+
target = allocate_objects.allocate_object(
258+
assignments, expr, target_type, alloc_type, symbols_created);
259+
437260
INVARIANT(
438261
target.type().id() == ID_pointer, "Pointer-typed expression expected");
439262
}

jbmc/src/java_bytecode/java_object_factory.h

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,11 @@ Author: Daniel Kroening, [email protected]
7474
#include "java_bytecode_language.h"
7575
#include "select_pointer_type.h"
7676

77+
#include <util/allocate_objects.h>
7778
#include <util/message.h>
7879
#include <util/std_code.h>
7980
#include <util/symbol_table.h>
8081

81-
/// Selects the kind of allocation used by the object factories
82-
enum class allocation_typet
83-
{
84-
/// Allocate global objects
85-
GLOBAL,
86-
/// Allocate local stacked objects
87-
LOCAL,
88-
/// Allocate dynamic objects (using MALLOC)
89-
DYNAMIC
90-
};
91-
9282
exprt object_factory(
9383
const typet &type,
9484
const irep_idt base_name,
@@ -136,21 +126,4 @@ void gen_nondet_init(
136126
const java_object_factory_parameterst &object_factory_parameters,
137127
update_in_placet update_in_place);
138128

139-
exprt allocate_dynamic_object(
140-
const exprt &target_expr,
141-
const typet &allocate_type,
142-
symbol_table_baset &symbol_table,
143-
const source_locationt &loc,
144-
const irep_idt &function_id,
145-
code_blockt &output_code,
146-
std::vector<const symbolt *> &symbols_created,
147-
bool cast_needed = false);
148-
149-
exprt allocate_dynamic_object_with_decl(
150-
const exprt &target_expr,
151-
symbol_table_baset &symbol_table,
152-
const source_locationt &loc,
153-
const irep_idt &function_id,
154-
code_blockt &output_code);
155-
156129
#endif // CPROVER_JAVA_BYTECODE_JAVA_OBJECT_FACTORY_H

0 commit comments

Comments
 (0)