cprover
jsil_entry_point.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Jsil Language
4 
5 Author: Michael Tautschnig, tautschn@amazon.com
6 
7 \*******************************************************************/
8 
11 
12 #include "jsil_entry_point.h"
13 
14 #include <util/arith_tools.h>
15 #include <util/config.h>
16 #include <util/message.h>
17 #include <util/range.h>
18 #include <util/symbol_table.h>
19 
22 
24 
25 static void create_initialize(symbol_tablet &symbol_table)
26 {
27  symbolt initialize;
28  initialize.name = INITIALIZE_FUNCTION;
29  initialize.base_name = INITIALIZE_FUNCTION;
30  initialize.mode="jsil";
31 
32  initialize.type = code_typet({}, empty_typet());
33 
34  code_blockt init_code;
35 
36  namespacet ns(symbol_table);
37 
38  symbol_exprt rounding_mode =
39  ns.lookup(rounding_mode_identifier()).symbol_expr();
40 
41  code_assignt a(rounding_mode, from_integer(0, rounding_mode.type()));
42  init_code.add(a);
43 
44  initialize.value=init_code;
45 
46  if(symbol_table.add(initialize))
47  throw "failed to add " INITIALIZE_FUNCTION;
48 }
49 
51  symbol_tablet &symbol_table,
52  message_handlert &message_handler)
53 {
54  // check if main is already there
55  if(symbol_table.symbols.find(goto_functionst::entry_point())!=
56  symbol_table.symbols.end())
57  return false; // silently ignore
58 
59  irep_idt main_symbol;
60 
61  // find main symbol, if any is given
62  if(config.main.has_value())
63  {
64  std::list<irep_idt> matches;
65 
66  for(const auto &symbol_name_entry :
67  equal_range(symbol_table.symbol_base_map, config.main.value()))
68  {
69  // look it up
70  symbol_tablet::symbolst::const_iterator s_it =
71  symbol_table.symbols.find(symbol_name_entry.second);
72 
73  if(s_it==symbol_table.symbols.end())
74  continue;
75 
76  if(s_it->second.type.id()==ID_code)
77  matches.push_back(symbol_name_entry.second);
78  }
79 
80  if(matches.empty())
81  {
82  messaget message(message_handler);
83  message.error() << "main symbol '" << config.main.value() << "' not found"
84  << messaget::eom;
85  return true; // give up
86  }
87 
88  if(matches.size()>=2)
89  {
90  messaget message(message_handler);
91  message.error() << "main symbol '" << config.main.value()
92  << "' is ambiguous" << messaget::eom;
93  return true;
94  }
95 
96  main_symbol=matches.front();
97  }
98  else
99  main_symbol=ID_main;
100 
101  // look it up
102  symbol_tablet::symbolst::const_iterator s_it=
103  symbol_table.symbols.find(main_symbol);
104 
105  if(s_it==symbol_table.symbols.end())
106  {
107  messaget message(message_handler);
108  message.error() << "main symbol '" << id2string(main_symbol)
109  << "' not in symbol table" << messaget::eom;
110  return true; // give up, no main
111  }
112 
113  const symbolt &symbol=s_it->second;
114 
115  // check if it has a body
116  if(symbol.value.is_nil())
117  {
118  messaget message(message_handler);
119  message.error() << "main symbol '" << main_symbol << "' has no body"
120  << messaget::eom;
121  return false; // give up
122  }
123 
124  create_initialize(symbol_table);
125 
126  code_blockt init_code;
127 
128  // build call to initialization function
129 
130  {
131  symbol_tablet::symbolst::const_iterator init_it=
132  symbol_table.symbols.find(INITIALIZE_FUNCTION);
133 
134  if(init_it==symbol_table.symbols.end())
135  throw "failed to find " INITIALIZE_FUNCTION " symbol";
136 
137  code_function_callt call_init(init_it->second.symbol_expr());
138  call_init.add_source_location()=symbol.location;
139  init_code.add(call_init);
140  }
141 
142  // build call to main function
143 
144  code_function_callt call_main(symbol.symbol_expr());
145  call_main.add_source_location()=symbol.location;
146  call_main.function().add_source_location()=symbol.location;
147 
148  init_code.add(call_main);
149 
150  // add "main"
151  symbolt new_symbol;
152 
153  new_symbol.name=goto_functionst::entry_point();
154  new_symbol.type = code_typet({}, empty_typet());
155  new_symbol.value.swap(init_code);
156 
157  if(!symbol_table.insert(std::move(new_symbol)).second)
158  {
159  messaget message;
160  message.set_message_handler(message_handler);
161  message.error() << "failed to move main symbol" << messaget::eom;
162  return true;
163  }
164 
165  return false;
166 }
irep_idt rounding_mode_identifier()
Return the identifier of the program symbol used to store the current rounding mode.
Symbolic Execution.
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:99
A codet representing an assignment in the program.
Definition: std_code.h:293
A codet representing sequential composition of program statements.
Definition: std_code.h:168
void add(const codet &code)
Definition: std_code.h:206
codet representation of a function call statement.
Definition: std_code.h:1213
exprt & function()
Definition: std_code.h:1248
Base type of functions.
Definition: std_types.h:539
optionalt< std::string > main
Definition: config.h:260
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
The empty type.
Definition: std_types.h:51
source_locationt & add_source_location()
Definition: expr.h:235
typet & type()
Return the type of the expression.
Definition: expr.h:82
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
void swap(irept &irep)
Definition: irep.h:453
bool is_nil() const
Definition: irep.h:387
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
mstreamt & error() const
Definition: message.h:399
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:179
static eomt eom
Definition: message.h:297
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
Expression to hold a symbol (variable)
Definition: std_expr.h:80
const symbol_base_mapt & symbol_base_map
Read-only field, used to look up symbol names given their base names.
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.
The symbol table.
Definition: symbol_table.h:14
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Symbol table entry.
Definition: symbol.h:28
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
typet type
Type of symbol.
Definition: symbol.h:31
irep_idt name
The unique identifier.
Definition: symbol.h:40
exprt value
Initial value of symbol.
Definition: symbol.h:34
irep_idt mode
Language mode.
Definition: symbol.h:49
configt config
Definition: config.cpp:25
Goto Programs with Functions.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
bool jsil_entry_point(symbol_tablet &symbol_table, message_handlert &message_handler)
static void create_initialize(symbol_tablet &symbol_table)
Jsil Language.
Ranges: pair of begin and end iterators, which can be initialized from containers,...
ranget< typename multimapt::const_iterator > equal_range(const multimapt &multimap, const typename multimapt::key_type &key)
Utility function to make equal_range method of multimap easier to use by returning a ranget object.
Definition: range.h:541
#define INITIALIZE_FUNCTION
Author: Diffblue Ltd.