CBMC
builtin_factory.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "builtin_factory.h"
10 
11 #include <util/config.h>
12 #include <util/prefix.h>
13 #include <util/string_utils.h>
14 #include <util/symbol_table.h>
15 
17 #include "ansi_c_parser.h"
18 #include "ansi_c_typecheck.h"
19 
20 #include <sstream>
21 
22 static bool find_pattern(
23  const std::string &pattern,
24  const char *header_file,
25  std::ostream &out)
26 {
27  std::istringstream hdr(header_file);
28  std::string line;
29  while(std::getline(hdr, line))
30  {
31  line = strip_string(line);
32  if(has_prefix(line, "//") || line.find(pattern) == std::string::npos)
33  continue;
34 
35  out << line;
36  return true;
37  }
38 
39  return false;
40 }
41 
42 static bool convert(
43  const irep_idt &identifier,
44  const std::ostringstream &s,
45  symbol_table_baset &symbol_table,
46  message_handlert &message_handler)
47 {
48  std::istringstream in(s.str());
49 
50  ansi_c_parsert ansi_c_parser{message_handler};
51  ansi_c_parser.set_file(ID_built_in);
52  ansi_c_parser.in=&in;
53  ansi_c_parser.for_has_scope=config.ansi_c.for_has_scope;
54  ansi_c_parser.ts_18661_3_Floatn_types = config.ansi_c.ts_18661_3_Floatn_types;
55  ansi_c_parser.float16_type = config.ansi_c.float16_type;
56  ansi_c_parser.bf16_type = config.ansi_c.bf16_type;
57  ansi_c_parser.cpp98=false; // it's not C++
58  ansi_c_parser.cpp11=false; // it's not C++
59  ansi_c_parser.mode=config.ansi_c.mode;
60 
61  ansi_c_scanner_init(ansi_c_parser);
62 
63  if(ansi_c_parser.parse())
64  return true;
65 
66  symbol_tablet new_symbol_table;
67 
68  // this is recursive -- builtin_factory is called
69  // from the typechecker
71  ansi_c_parser.parse_tree,
72  new_symbol_table,
73  "", // module
74  message_handler))
75  {
76  return true;
77  }
78 
79  // we should now have a new symbol
80  symbol_tablet::symbolst::const_iterator s_it=
81  new_symbol_table.symbols.find(identifier);
82 
83  if(s_it==new_symbol_table.symbols.end())
84  {
85  messaget message(message_handler);
86  message.error() << "failed to produce built-in symbol '" << identifier
87  << '\'' << messaget::eom;
88  return true;
89  }
90 
91  // copy the new symbol
92  symbol_table.add(s_it->second);
93 
94  return false;
95 }
96 
101  const irep_idt &identifier,
102  bool support_float16_type,
103  symbol_table_baset &symbol_table,
104  message_handlert &mh)
105 {
106  // we search for "space" "identifier" "("
107  const std::string pattern=' '+id2string(identifier)+'(';
108 
109  std::ostringstream s;
110 
111  std::string code;
112  ansi_c_internal_additions(code, support_float16_type);
113  s << code;
114 
115  // our own extensions
116  if(find_pattern(pattern, cprover_builtin_headers, s))
117  return convert(identifier, s, symbol_table, mh);
118 
119  // this is Visual C/C++ only
121  {
122  if(find_pattern(pattern, windows_builtin_headers, s))
123  return convert(identifier, s, symbol_table, mh);
124  }
125 
126  // ARM stuff
128  {
129  if(find_pattern(pattern, arm_builtin_headers, s))
130  return convert(identifier, s, symbol_table, mh);
131  }
132 
133  // CW stuff
135  {
136  if(find_pattern(pattern, cw_builtin_headers, s))
137  return convert(identifier, s, symbol_table, mh);
138  }
139 
140  // GCC junk stuff, also for CLANG and ARM
141  if(
145  {
147  return convert(identifier, s, symbol_table, mh);
148 
149  if(find_pattern(pattern, gcc_builtin_headers_math, s))
150  return convert(identifier, s, symbol_table, mh);
151 
153  return convert(identifier, s, symbol_table, mh);
154 
155  if(find_pattern(pattern, gcc_builtin_headers_omp, s))
156  return convert(identifier, s, symbol_table, mh);
157 
158  if(find_pattern(pattern, gcc_builtin_headers_tm, s))
159  return convert(identifier, s, symbol_table, mh);
160 
161  if(find_pattern(pattern, gcc_builtin_headers_ubsan, s))
162  return convert(identifier, s, symbol_table, mh);
163 
164  if(find_pattern(pattern, clang_builtin_headers, s))
165  return convert(identifier, s, symbol_table, mh);
166 
167  if(config.ansi_c.arch=="i386" ||
168  config.ansi_c.arch=="x86_64" ||
169  config.ansi_c.arch=="x32")
170  {
171  if(find_pattern(pattern, gcc_builtin_headers_ia32, s))
172  return convert(identifier, s, symbol_table, mh);
173 
175  return convert(identifier, s, symbol_table, mh);
176 
178  return convert(identifier, s, symbol_table, mh);
179 
181  return convert(identifier, s, symbol_table, mh);
182 
184  return convert(identifier, s, symbol_table, mh);
185 
187  return convert(identifier, s, symbol_table, mh);
188  }
189  else if(config.ansi_c.arch=="arm64" ||
190  config.ansi_c.arch=="armel" ||
191  config.ansi_c.arch=="armhf" ||
192  config.ansi_c.arch=="arm")
193  {
194  if(find_pattern(pattern, gcc_builtin_headers_arm, s))
195  return convert(identifier, s, symbol_table, mh);
196  }
197  else if(config.ansi_c.arch=="mips64el" ||
198  config.ansi_c.arch=="mipsn32el" ||
199  config.ansi_c.arch=="mipsel" ||
200  config.ansi_c.arch=="mips64" ||
201  config.ansi_c.arch=="mipsn32" ||
202  config.ansi_c.arch=="mips")
203  {
204  if(find_pattern(pattern, gcc_builtin_headers_mips, s))
205  return convert(identifier, s, symbol_table, mh);
206  }
207  else if(config.ansi_c.arch=="powerpc" ||
208  config.ansi_c.arch=="ppc64" ||
209  config.ansi_c.arch=="ppc64le")
210  {
211  if(find_pattern(pattern, gcc_builtin_headers_power, s))
212  return convert(identifier, s, symbol_table, mh);
213  }
214  }
215 
216  return true;
217 }
const char cprover_builtin_headers[]
const char gcc_builtin_headers_ia32[]
const char gcc_builtin_headers_ia32_2[]
const char gcc_builtin_headers_ia32_5[]
const char gcc_builtin_headers_ubsan[]
void ansi_c_internal_additions(std::string &code, bool support_float16_type)
const char windows_builtin_headers[]
const char gcc_builtin_headers_mem_string[]
const char gcc_builtin_headers_ia32_4[]
const char gcc_builtin_headers_generic[]
const char gcc_builtin_headers_tm[]
const char cw_builtin_headers[]
const char gcc_builtin_headers_math[]
const char gcc_builtin_headers_arm[]
const char gcc_builtin_headers_ia32_6[]
const char gcc_builtin_headers_mips[]
const char clang_builtin_headers[]
const char arm_builtin_headers[]
const char gcc_builtin_headers_omp[]
const char gcc_builtin_headers_ia32_3[]
const char gcc_builtin_headers_power[]
void ansi_c_scanner_init(ansi_c_parsert &)
bool ansi_c_typecheck(ansi_c_parse_treet &ansi_c_parse_tree, symbol_table_baset &symbol_table, const std::string &module, message_handlert &message_handler)
ANSI-C Language Type Checking.
configt config
Definition: config.cpp:25
static bool convert(const irep_idt &identifier, const std::ostringstream &s, symbol_table_baset &symbol_table, message_handlert &message_handler)
bool builtin_factory(const irep_idt &identifier, bool support_float16_type, symbol_table_baset &symbol_table, message_handlert &mh)
Check whether given identifier is a compiler built-in.
static bool find_pattern(const std::string &pattern, const char *header_file, std::ostream &out)
struct configt::ansi_ct ansi_c
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
mstreamt & error() const
Definition: message.h:399
static eomt eom
Definition: message.h:297
The symbol table base class interface.
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
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
const std::string & id2string(const irep_idt &d)
Definition: irep.h:40
std::string strip_string(const std::string &s)
Remove all whitespace characters from either end of a string.
bool for_has_scope
Definition: config.h:151
bool float16_type
Definition: config.h:154
bool bf16_type
Definition: config.h:155
bool ts_18661_3_Floatn_types
Definition: config.h:152
irep_idt arch
Definition: config.h:221
flavourt mode
Definition: config.h:253
Author: Diffblue Ltd.