Skip to content

Commit cd416bc

Browse files
author
thk123
committed
Call generate_start_function only when regenerating the start function
Rather than calling the final method, call a refactored out method that is just responsible for generating the _start body.
1 parent ee5fb93 commit cd416bc

File tree

6 files changed

+66
-8
lines changed

6 files changed

+66
-8
lines changed

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include <util/config.h>
1919
#include <util/cprover_prefix.h>
2020
#include <util/prefix.h>
21+
#include <util/symbol.h>
2122

2223
#include <util/c_types.h>
2324
#include <ansi-c/string_constant.h>
@@ -190,6 +191,22 @@ bool ansi_c_entry_point(
190191
if(static_lifetime_init(symbol_table, symbol.location, message_handler))
191192
return true;
192193

194+
return generate_ansi_c_start_function(symbol, symbol_table, message_handler);
195+
}
196+
197+
198+
/// Generate a _start function for a specific function
199+
/// \param entry_function_symbol: The symbol for the function that should be
200+
/// used as the entry point
201+
/// \param symbol_table: The symbol table for the program. The new _start
202+
/// function symbol will be added to this table
203+
/// \return Returns false if the _start method was generated correctly
204+
bool generate_ansi_c_start_function(
205+
const symbolt &symbol,
206+
symbol_tablet &symbol_table,
207+
message_handlert &message_handler)
208+
{
209+
PRECONDITION(!symbol.value.is_nil());
193210
code_blockt init_code;
194211

195212
// build call to initialization function
@@ -237,7 +254,7 @@ bool ansi_c_entry_point(
237254
const code_typet::parameterst &parameters=
238255
to_code_type(symbol.type).parameters();
239256

240-
if(symbol.name==standard_main)
257+
if(symbol.name==ID_main)
241258
{
242259
if(parameters.empty())
243260
{

src/ansi-c/ansi_c_entry_point.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ bool ansi_c_entry_point(
1818
const std::string &standard_main,
1919
message_handlert &message_handler);
2020

21+
bool generate_ansi_c_start_function(
22+
const symbolt &symbol,
23+
symbol_tablet &symbol_table,
24+
message_handlert &message_handler);
25+
2126
#endif // CPROVER_ANSI_C_ANSI_C_ENTRY_POINT_H

src/ansi-c/ansi_c_language.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ void ansi_c_languaget::modules_provided(std::set<std::string> &modules)
3636
modules.insert(get_base_name(parse_path, true));
3737
}
3838

39+
/// Generate a _start function for a specific function
40+
/// \param entry_function_symbol: The symbol for the function that should be
41+
/// used as the entry point
42+
/// \param symbol_table: The symbol table for the program. The new _start
43+
/// function symbol will be added to this table
44+
/// \return Returns false if the _start method was generated correctly
45+
bool ansi_c_languaget::generate_start_function(
46+
const symbolt &entry_function_symbol,
47+
symbol_tablet &symbol_table)
48+
{
49+
return generate_ansi_c_start_function(
50+
entry_function_symbol,
51+
symbol_table,
52+
*message_handler);
53+
}
54+
3955
/// ANSI-C preprocessing
4056
bool ansi_c_languaget::preprocess(
4157
std::istream &instream,

src/ansi-c/ansi_c_language.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class ansi_c_languaget:public languaget
7373

7474
void modules_provided(std::set<std::string> &modules) override;
7575

76+
virtual bool generate_start_function(
77+
const class symbolt &entry_function_symbol,
78+
class symbol_tablet &symbol_table) override;
79+
7680
protected:
7781
ansi_c_parse_treet parse_tree;
7882
std::string parse_path;

src/util/language.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ void languaget::generate_opaque_parameter_symbols(
194194
}
195195
}
196196

197+
/// Generate a entry function for a specific function. Should be overriden in
198+
/// derived languagets
199+
/// \param entry_function_symbol: The symbol for the function that should be
200+
/// used as the entry point
201+
/// \param symbol_table: The symbol table for the program. The new _start
202+
/// function symbol will be added to this table
203+
/// \return Returns false if the entry method was generated correctly
204+
bool languaget::generate_start_function(
205+
const symbolt &entry_function_symbol,
206+
symbol_tablet &symbol_table)
207+
{
208+
// Implement in derived languagets
209+
assert(0);
210+
return true;
211+
}
212+
197213
/// To replace an existing _start function with a new one that calls a specified
198214
/// function
199215
/// \param required_entry_function: a code symbol inside the symbol table which
@@ -205,18 +221,14 @@ void languaget::generate_opaque_parameter_symbols(
205221
/// \return Returns false if the new start function is created successfully,
206222
/// true otherwise.
207223
bool languaget::regenerate_start_function(
208-
const symbolt &required_entry_function,
224+
const symbolt &entry_function_symbol,
209225
symbol_tablet &symbol_table,
210226
goto_functionst &goto_functions)
211227
{
212228
// Remove the existing _start function so we can create a new one
213229
symbol_table.remove(goto_functionst::entry_point());
214-
config.main=required_entry_function.name.c_str();
215230

216-
// TODO(tkiley): calling final is not really correct (in fact for example,
217-
// opaque function stubs get generated here). Instead the final method should
218-
// call this to generate the function in the first place.
219-
bool return_code=final(symbol_table);
231+
bool return_code=generate_start_function(entry_function_symbol, symbol_table);
220232

221233
// Remove the function from the goto_functions so is copied back in
222234
// from the symbol table

src/util/language.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,12 @@ class languaget:public messaget
119119

120120
void set_should_generate_opaque_method_stubs(bool should_generate_stubs);
121121

122+
virtual bool generate_start_function(
123+
const class symbolt &entry_function_symbol,
124+
class symbol_tablet &symbol_table);
125+
122126
bool regenerate_start_function(
123-
const class symbolt &required_entry_function,
127+
const class symbolt &entry_function_symbol,
124128
symbol_tablet &symbol_table,
125129
class goto_functionst &goto_functions);
126130

0 commit comments

Comments
 (0)