Skip to content

Commit 6e0d4e3

Browse files
committed
Don't cast field assignments on the LHS
Previously it was possible to generate code such as `(void*)someobject->somefield = some_void_typed_local`. This fixes Java method conversion to always generate the more intuitively correct `someobject->somefield = (A*)some_void_typed_local` (i.e. always cast on the RHS), making it clearer what types are involved.
1 parent 05652d2 commit 6e0d4e3

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/java_bytecode/java_bytecode_convert_method.cpp

+18-7
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,18 @@ class java_bytecode_convert_methodt:public messaget
119119
}
120120

121121
// JVM local variables
122-
const exprt variable(const exprt &arg, char type_char, size_t address, instruction_sizet inst_size)
122+
enum variable_cast_argumentt
123+
{
124+
CAST_AS_NEEDED,
125+
NO_CAST
126+
};
127+
128+
const exprt variable(
129+
const exprt &arg,
130+
char type_char,
131+
size_t address,
132+
instruction_sizet inst_size,
133+
variable_cast_argumentt do_cast)
123134
{
124135
irep_idt number=to_constant_expr(arg).get_value();
125136

@@ -145,7 +156,7 @@ class java_bytecode_convert_methodt:public messaget
145156
else
146157
{
147158
exprt result=var.symbol_expr;
148-
if(t!=result.type()) result=typecast_exprt(result, t);
159+
if(do_cast==CAST_AS_NEEDED && t!=result.type()) result=typecast_exprt(result, t);
149160
return result;
150161
}
151162
}
@@ -829,7 +840,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
829840
// store value into some local variable
830841
assert(op.size()==1 && results.empty());
831842

832-
exprt var=variable(arg0, statement[0], i_it->address, INST_INDEX);
843+
exprt var=variable(arg0, statement[0], i_it->address, INST_INDEX, NO_CAST);
833844

834845
const bool is_array('a' == statement[0]);
835846

@@ -861,7 +872,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
861872
else if(statement==patternt("?load"))
862873
{
863874
// load a value from a local variable
864-
results[0]=variable(arg0, statement[0], i_it->address, INST_INDEX);
875+
results[0]=variable(arg0, statement[0], i_it->address, INST_INDEX, CAST_AS_NEEDED);
865876
}
866877
else if(statement=="ldc" || statement=="ldc_w" ||
867878
statement=="ldc2" || statement=="ldc2_w")
@@ -1023,10 +1034,10 @@ codet java_bytecode_convert_methodt::convert_instructions(
10231034
else if(statement=="iinc")
10241035
{
10251036
code_assignt code_assign;
1026-
code_assign.lhs()=variable(arg0, 'i', i_it->address, INST_INDEX_CONST);
1037+
code_assign.lhs()=variable(arg0, 'i', i_it->address, INST_INDEX_CONST, NO_CAST);
10271038
code_assign.rhs()=plus_exprt(
1028-
variable(arg0, 'i', i_it->address, INST_INDEX_CONST),
1029-
typecast_exprt(arg1, java_int_type()));
1039+
variable(arg0, 'i', i_it->address, INST_INDEX_CONST, CAST_AS_NEEDED),
1040+
typecast_exprt(arg1, java_int_type()));
10301041
c=code_assign;
10311042
}
10321043
else if(statement==patternt("?xor"))

0 commit comments

Comments
 (0)