Skip to content

Commit 043f02f

Browse files
committed
Treat all anonymous locals as void*
The existing code tried to infer local type from the assignment, but (a) the information wasn't recorded in the variable table leading to later misuse, and (b) I'm not convinced it's safe to infer types for locals this way because a pattern like if(x) a = new A(); else a = new B(); (where A and B are unrelated) can use the same local slot for both classes. In this case we'd want to give it java.lang.Object* type. In view of all this it is surely simplest to just treat locals as generic, like the bytecode does, then cast the void* to needed types at use sites.
1 parent 05652d2 commit 043f02f

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -731,17 +731,21 @@ codet java_bytecode_convert_methodt::convert_instructions(
731731

732732
// do some type adjustment for the arguments,
733733
// as Java promotes arguments
734+
// Also cast pointers since intermediate locals
735+
// can be void*.
734736

735737
for(std::size_t i=0; i<parameters.size(); i++)
736738
{
737739
const typet &type=parameters[i].type();
738740
if(type==java_boolean_type() ||
739741
type==java_char_type() ||
740742
type==java_byte_type() ||
741-
type==java_short_type())
743+
type==java_short_type() ||
744+
type.id()==ID_pointer)
742745
{
743746
assert(i<call.arguments().size());
744-
call.arguments()[i].make_typecast(type);
747+
if(type!=call.arguments()[i].type())
748+
call.arguments()[i].make_typecast(type);
745749
}
746750
}
747751

@@ -831,12 +835,11 @@ codet java_bytecode_convert_methodt::convert_instructions(
831835

832836
exprt var=variable(arg0, statement[0], i_it->address, INST_INDEX);
833837

834-
const bool is_array('a' == statement[0]);
838+
exprt toassign=op[0];
839+
if('a'==statement[0] && toassign.type()!=var.type())
840+
toassign=typecast_exprt(toassign, var.type());
835841

836-
if(is_array)
837-
var.type()=op[0].type();
838-
839-
c=code_assignt(var, op[0]);
842+
c=code_assignt(var, toassign);
840843
}
841844
else if(statement==patternt("?aload"))
842845
{
@@ -997,7 +1000,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
9971000
irep_idt number=to_constant_expr(arg0).get_value();
9981001
assert(op.size()==1 && results.empty());
9991002
code_ifthenelset code_branch;
1000-
const typecast_exprt lhs(op[0], pointer_typet());
1003+
const typecast_exprt lhs(op[0], pointer_typet(empty_typet()));
10011004
const exprt rhs(gen_zero(lhs.type()));
10021005
code_branch.cond()=binary_relation_exprt(lhs, ID_notequal, rhs);
10031006
code_branch.then_case()=code_gotot(label(number));

0 commit comments

Comments
 (0)