CBMC
Loading...
Searching...
No Matches
statement_list_entry_point.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Statement List Language Entry Point
4
5Author: Matthias Weiss, matthias.weiss@diffblue.com
6
7\*******************************************************************/
8
11
13
14#include <util/c_types.h>
15#include <util/config.h>
16#include <util/message.h>
17#include <util/pointer_expr.h>
18#include <util/std_code.h>
20
23
25
28#define DB_ENTRY_POINT_POSTFIX "_entry_point"
29
31#define CPROVER_HIDE CPROVER_PREFIX "HIDE"
32
42 const symbol_table_baset &symbol_table,
43 message_handlert &message_handler,
45{
46 bool found = false;
47
48 for(const std::pair<const irep_idt, symbolt> &pair : symbol_table)
49 {
50 if(pair.first == main_symbol_name && pair.second.type.id() == ID_code)
51 {
52 if(found)
53 {
54 messaget message(message_handler);
55 message.error() << "main symbol " << messaget::quote_begin
57 << " is ambiguous" << messaget::eom;
58 return true;
59 }
60 else
61 found = true;
62 }
63 }
64
65 if(found)
66 return false;
67 else
68 {
69 messaget message(message_handler);
70 message.error() << "main symbol " << messaget::quote_begin
71 << main_symbol_name << messaget::quote_end << " not found"
73 return true;
74 }
75}
76
85 const symbol_table_baset &symbol_table,
87{
88 symbolt init = symbol_table.lookup_ref(INITIALIZE_FUNCTION);
90 call_init.add_source_location() = main_symbol_location;
92}
93
101 symbol_table_baset &symbol_table,
103{
104 const code_typet &function_type = to_code_type(main_function_block.type);
105 PRECONDITION(1u == function_type.parameters().size());
107 function_type.parameters().front();
112 instance_data_block.is_static_lifetime = true;
113 symbol_table.add(instance_data_block);
115
118 call_main.add_source_location() = main_function_block.location;
120}
121
124static void
126{
127 symbolt init{
129
130 code_blockt dest;
132
133 for(const std::pair<const irep_idt, symbolt> &pair : symbol_table)
134 {
135 const symbolt &symbol = pair.second;
136 if(symbol.is_static_lifetime && symbol.value.is_not_nil())
137 dest.add(code_assignt{pair.second.symbol_expr(), pair.second.value});
138 }
139 init.value = std::move(dest);
140 symbol_table.add(init);
141}
142
146{
148 rounding_mode.is_thread_local = true;
149 rounding_mode.is_static_lifetime = true;
153 rounding_mode.value = rounding_val;
154 symbol_table.add(rounding_mode);
155}
156
163 const symbolt &main,
164 symbol_table_baset &symbol_table,
165 message_handlert &message_handler)
166{
167 PRECONDITION(!main.value.is_nil());
170
171 add_initialize_call(start_function_body, symbol_table, main.location);
172 // TODO: Support calls to STL functions.
173 // Since STL function calls do not involve a data block, pass all arguments
174 // as normal parameters.
176
177 // Add the start symbol.
182
183 if(!symbol_table.insert(std::move(start_symbol)).second)
184 {
185 messaget message(message_handler);
186 message.error() << "failed to insert start symbol" << messaget::eom;
187 return true;
188 }
189
190 return false;
191}
192
194 symbol_table_baset &symbol_table,
195 message_handlert &message_handler)
196{
197 // Check if the entry point is already present and return if it is.
198 if(
199 symbol_table.symbols.find(goto_functionst::entry_point()) !=
200 symbol_table.symbols.end())
201 return false;
202
204
205 // Find main symbol given by the user.
206 if(config.main.has_value())
207 {
209 symbol_table, message_handler, config.main.value()))
210 return true;
211 main_symbol_name = config.main.value();
212 }
213 // Fallback: Search for block with TIA main standard name.
214 // TODO: Support the standard entry point of STL (organisation blocks).
215 // This also requires to expand the grammar and typecheck.
216 else
217 {
219 symbol_table, message_handler, ID_statement_list_main_function))
220 return true;
222 }
223
224 const symbolt &main = symbol_table.lookup_ref(main_symbol_name);
225
226 // Check if the symbol has a body.
227 if(main.value.is_nil())
228 {
229 messaget message(message_handler);
230 message.error() << "main symbol " << messaget::quote_begin
231 << main_symbol_name << messaget::quote_end << " has no body"
232 << messaget::eom;
233 return true;
234 }
235
236 generate_rounding_mode(symbol_table);
239 main, symbol_table, message_handler);
240}
irep_idt rounding_mode_identifier()
Return the identifier of the program symbol used to store the current rounding mode.
Symbolic Execution.
configt config
Definition config.cpp:25
signedbv_typet signed_int_type()
Definition c_types.cpp:22
Operator to return the address of an object.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
A goto_instruction_codet representing an assignment in the program.
A codet representing sequential composition of program statements.
Definition std_code.h:130
void add(const codet &code)
Definition std_code.h:168
goto_instruction_codet representation of a function call statement.
codet representation of a label for branch targets.
Definition std_code.h:959
A codet representing a skip statement.
Definition std_code.h:322
Base type of functions.
Definition std_types.h:582
const parameterst & parameters() const
Definition std_types.h:698
std::optional< std::string > main
Definition config.h:390
A constant literal expression.
Definition std_expr.h:3007
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
The empty type.
Definition std_types.h:50
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
bool is_not_nil() const
Definition irep.h:372
Class that provides messages with a built-in verbosity 'level'.
Definition message.h:154
static const commandt quote_begin
Start quoted text.
Definition message.h:389
mstreamt & error() const
Definition message.h:401
static const commandt quote_end
End quoted text.
Definition message.h:393
static eomt eom
Definition message.h:289
The symbol table base class interface.
virtual std::pair< symbolt &, bool > insert(symbolt symbol)=0
Move or copy a new symbol to the symbol table.
const symbolst & symbols
Read-only field, used to look up symbols given their names.
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Symbol table entry.
Definition symbol.h:28
bool is_static_lifetime
Definition symbol.h:70
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition symbol.cpp:121
exprt value
Initial value of symbol.
Definition symbol.h:34
int main()
Definition example.cpp:18
Goto Programs with Functions.
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
API to expression classes for Pointers.
#define PRECONDITION(CONDITION)
Definition invariant.h:463
static void add_initialize_call(code_blockt &function_body, const symbol_table_baset &symbol_table, const source_locationt &main_symbol_location)
Creates a call to __CPROVER_initialize and adds it to the start function's body.
static bool is_main_symbol_invalid(const symbol_table_baset &symbol_table, message_handlert &message_handler, const irep_idt &main_symbol_name)
Searches for symbols with the given name (which is considered to be the name of the main symbol) and ...
bool statement_list_entry_point(symbol_table_baset &symbol_table, message_handlert &message_handler)
Creates a new entry point for the Statement List language.
static void add_main_function_block_call(code_blockt &function_body, symbol_table_baset &symbol_table, const symbolt &main_function_block)
Creates a call to the main function block and adds it to the start function's body.
#define CPROVER_HIDE
Name of the CPROVER-specific hide label.
#define DB_ENTRY_POINT_POSTFIX
Postfix for the artificial data block that is created when calling a main symbol that is a function b...
static void generate_rounding_mode(symbol_table_baset &symbol_table)
Creates __CPROVER_rounding_mode and adds it to the symbol table.
static void generate_statement_list_init_function(symbol_table_baset &symbol_table)
Creates __CPROVER_initialize and adds it to the symbol table.
bool generate_statement_list_start_function(const symbolt &main, symbol_table_baset &symbol_table, message_handlert &message_handler)
Creates a start function and adds it to the symbol table.
Statement List Language Entry Point.
#define INITIALIZE_FUNCTION
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition std_types.h:787
Author: Diffblue Ltd.
const type_with_subtypet & to_type_with_subtype(const typet &type)
Definition type.h:208