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 
19 
21 
22 static void create_initialize(symbol_tablet &symbol_table)
23 {
24  symbolt initialize;
25  initialize.name = INITIALIZE_FUNCTION;
26  initialize.base_name = INITIALIZE_FUNCTION;
27  initialize.mode="jsil";
28 
29  initialize.type = code_typet({}, empty_typet());
30 
31  code_blockt init_code;
32 
33  namespacet ns(symbol_table);
34 
35  symbol_exprt rounding_mode=
36  ns.lookup(CPROVER_PREFIX "rounding_mode").symbol_expr();
37 
38  code_assignt a(rounding_mode, from_integer(0, rounding_mode.type()));
39  init_code.add(a);
40 
41  initialize.value=init_code;
42 
43  if(symbol_table.add(initialize))
44  throw "failed to add " INITIALIZE_FUNCTION;
45 }
46 
48  symbol_tablet &symbol_table,
49  message_handlert &message_handler)
50 {
51  // check if main is already there
52  if(symbol_table.symbols.find(goto_functionst::entry_point())!=
53  symbol_table.symbols.end())
54  return false; // silently ignore
55 
56  irep_idt main_symbol;
57 
58  // find main symbol
59  if(config.main!="")
60  {
61  std::list<irep_idt> matches;
62 
64  {
65  // look it up
66  symbol_tablet::symbolst::const_iterator s_it=
67  symbol_table.symbols.find(it->second);
68 
69  if(s_it==symbol_table.symbols.end())
70  continue;
71 
72  if(s_it->second.type.id()==ID_code)
73  matches.push_back(it->second);
74  }
75 
76  if(matches.empty())
77  {
78  messaget message(message_handler);
79  message.error() << "main symbol `" << config.main
80  << "' not found" << messaget::eom;
81  return true; // give up
82  }
83 
84  if(matches.size()>=2)
85  {
86  messaget message(message_handler);
87  message.error() << "main symbol `" << config.main
88  << "' is ambiguous" << messaget::eom;
89  return true;
90  }
91 
92  main_symbol=matches.front();
93  }
94  else
95  main_symbol=ID_main;
96 
97  // look it up
98  symbol_tablet::symbolst::const_iterator s_it=
99  symbol_table.symbols.find(main_symbol);
100 
101  if(s_it==symbol_table.symbols.end())
102  {
103  messaget message(message_handler);
104  message.error() << "main symbol `" << id2string(main_symbol)
105  << "' not in symbol table" << messaget::eom;
106  return true; // give up, no main
107  }
108 
109  const symbolt &symbol=s_it->second;
110 
111  // check if it has a body
112  if(symbol.value.is_nil())
113  {
114  messaget message(message_handler);
115  message.error() << "main symbol `" << main_symbol
116  << "' has no body" << messaget::eom;
117  return false; // give up
118  }
119 
120  create_initialize(symbol_table);
121 
122  code_blockt init_code;
123 
124  // build call to initialization function
125 
126  {
127  symbol_tablet::symbolst::const_iterator init_it=
128  symbol_table.symbols.find(INITIALIZE_FUNCTION);
129 
130  if(init_it==symbol_table.symbols.end())
131  throw "failed to find " INITIALIZE_FUNCTION " symbol";
132 
133  code_function_callt call_init(init_it->second.symbol_expr());
134  call_init.add_source_location()=symbol.location;
135  init_code.add(call_init);
136  }
137 
138  // build call to main function
139 
140  code_function_callt call_main(symbol.symbol_expr());
141  call_main.add_source_location()=symbol.location;
142  call_main.function().add_source_location()=symbol.location;
143 
144  init_code.add(call_main);
145 
146  // add "main"
147  symbolt new_symbol;
148 
149  new_symbol.name=goto_functionst::entry_point();
150  new_symbol.type = code_typet({}, empty_typet());
151  new_symbol.value.swap(init_code);
152 
153  if(!symbol_table.insert(std::move(new_symbol)).second)
154  {
155  messaget message;
156  message.set_message_handler(message_handler);
157  message.error() << "failed to move main symbol" << messaget::eom;
158  return true;
159  }
160 
161  return false;
162 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:144
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:35
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:150
symbol_tablet
The symbol table.
Definition: symbol_table.h:19
arith_tools.h
symbol_table_baset::symbol_base_map
const symbol_base_mapt & symbol_base_map
Definition: symbol_table_base.h:28
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
configt::main
std::string main
Definition: config.h:172
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
messaget::eom
static eomt eom
Definition: message.h:284
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:143
jsil_entry_point.h
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:93
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:68
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:166
code_function_callt
codet representation of a function call statement.
Definition: std_code.h:1036
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
messaget::error
mstreamt & error() const
Definition: message.h:386
empty_typet
The empty type.
Definition: std_types.h:48
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
INITIALIZE_FUNCTION
#define INITIALIZE_FUNCTION
Definition: static_lifetime_init.h:23
messaget::set_message_handler
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:169
symbol_tablet::insert
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Definition: symbol_table.cpp:17
irept::swap
void swap(irept &irep)
Definition: irep.h:303
code_typet
Base type of functions.
Definition: std_types.h:751
irept::is_nil
bool is_nil() const
Definition: irep.h:172
message_handlert
Definition: message.h:24
code_blockt::add
void add(const codet &code)
Definition: std_code.h:189
config
configt config
Definition: config.cpp:24
symbol_table_baset::add
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Definition: symbol_table_base.cpp:18
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
jsil_entry_point
bool jsil_entry_point(symbol_tablet &symbol_table, message_handlert &message_handler)
Definition: jsil_entry_point.cpp:47
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:27
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:123
symbol_table_baset::symbols
const symbolst & symbols
Definition: symbol_table_base.h:27
forall_symbol_base_map
#define forall_symbol_base_map(it, expr, base_name)
Definition: symbol_table.h:11
CPROVER_PREFIX
#define CPROVER_PREFIX
Definition: cprover_prefix.h:14
goto_functions.h
config.h
goto_functionst::entry_point
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
Definition: goto_functions.h:101
static_lifetime_init.h
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:233
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:256
message.h
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
code_function_callt::function
exprt & function()
Definition: std_code.h:1099
create_initialize
static void create_initialize(symbol_tablet &symbol_table)
Definition: jsil_entry_point.cpp:22