Skip to content

Commit ff81171

Browse files
authored
Merge pull request #983 from romainbrenguier/pull-request/override-in-final-step
Overwrite String functions in final step of bytecode conversion
2 parents ac71afe + 65f2e36 commit ff81171

5 files changed

+51
-11
lines changed

src/java_bytecode/java_bytecode_convert_class.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ class java_bytecode_convert_classt:public messaget
4545
{
4646
add_array_types();
4747

48-
if(parse_tree.loading_successful)
48+
bool loading_success=parse_tree.loading_successful;
49+
if(loading_success)
4950
convert(parse_tree.parsed_class);
50-
else if(string_preprocess.is_known_string_type(
51-
parse_tree.parsed_class.name))
51+
52+
if(string_preprocess.is_known_string_type(parse_tree.parsed_class.name))
5253
string_preprocess.add_string_type(
5354
parse_tree.parsed_class.name, symbol_table);
54-
else
55+
else if(!loading_success)
5556
generate_class_stub(parse_tree.parsed_class.name);
5657
}
5758

src/java_bytecode/java_bytecode_convert_method.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -1550,17 +1550,11 @@ codet java_bytecode_convert_methodt::convert_instructions(
15501550
symbol.type=arg0.type();
15511551
symbol.value.make_nil();
15521552
symbol.mode=ID_java;
1553-
15541553
assign_parameter_names(
15551554
to_code_type(symbol.type),
15561555
symbol.name,
15571556
symbol_table);
15581557

1559-
// The string refinement module may provide a definition for this
1560-
// function.
1561-
symbol.value=string_preprocess.code_for_function(
1562-
id, to_code_type(symbol.type), loc, symbol_table);
1563-
15641558
symbol_table.add(symbol);
15651559
}
15661560

src/java_bytecode/java_bytecode_language.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,43 @@ void java_bytecode_languaget::convert_lazy_method(
799799

800800
/*******************************************************************\
801801
802+
Function: java_bytecode_languaget::replace_string_methods
803+
804+
Inputs:
805+
context - a symbol table
806+
807+
Purpose: Replace methods of the String library that are in the symbol table
808+
by code generated by string_preprocess.
809+
810+
\*******************************************************************/
811+
812+
void java_bytecode_languaget::replace_string_methods(
813+
symbol_tablet &context)
814+
{
815+
// Symbols that have code type are potentialy to be replaced
816+
std::list<symbolt> code_symbols;
817+
forall_symbols(symbol, context.symbols)
818+
{
819+
if(symbol->second.type.id()==ID_code)
820+
code_symbols.push_back(symbol->second);
821+
}
822+
823+
for(const auto &symbol : code_symbols)
824+
{
825+
const irep_idt &id=symbol.name;
826+
exprt generated_code=string_preprocess.code_for_function(
827+
id, to_code_type(symbol.type), symbol.location, context);
828+
if(generated_code.is_not_nil())
829+
{
830+
// Replace body of the function by code generated by string preprocess
831+
symbolt &symbol=context.lookup(id);
832+
symbol.value=generated_code;
833+
}
834+
}
835+
}
836+
837+
/*******************************************************************\
838+
802839
Function: java_bytecode_languaget::final
803840
804841
Inputs:
@@ -816,6 +853,9 @@ bool java_bytecode_languaget::final(symbol_tablet &symbol_table)
816853
*/
817854
java_internal_additions(symbol_table);
818855

856+
// Replace code of String methods calls by code we generate
857+
replace_string_methods(symbol_table);
858+
819859
main_function_resultt res=
820860
get_main_symbol(symbol_table, main_class, get_message_handler());
821861
if(res.stop_convert)

src/java_bytecode/java_bytecode_language.h

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class java_bytecode_languaget:public languaget
4949
symbol_tablet &context,
5050
const std::string &module) override;
5151

52+
void replace_string_methods(symbol_tablet &context);
53+
5254
virtual bool final(
5355
symbol_tablet &context) override;
5456

src/java_bytecode/java_string_library_preprocess.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,10 @@ void java_string_library_preprocesst::add_string_type(
319319
string_symbol.type=string_type;
320320
string_symbol.is_type=true;
321321

322-
symbol_table.add(string_symbol);
322+
// Overwrite any pre-existing symbol in the table, e.g. created by
323+
// a loaded model.
324+
symbol_table.remove(string_symbol.name);
325+
assert(!symbol_table.add(string_symbol));
323326
}
324327

325328
/*******************************************************************\

0 commit comments

Comments
 (0)