CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
builtin_factory.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: 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
22static 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
42static 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.__float128_is_keyword = config.ansi_c.__float128_is_keyword;
56 ansi_c_parser.float16_type = config.ansi_c.float16_type;
57 ansi_c_parser.bf16_type = config.ansi_c.bf16_type;
58 ansi_c_parser.fp16_type = config.ansi_c.fp16_type;
59 ansi_c_parser.cpp98=false; // it's not C++
60 ansi_c_parser.cpp11=false; // it's not C++
61 ansi_c_parser.c17 = false; // we do C11 for now
62 ansi_c_parser.c23 = false; // we do C11 for now
63 ansi_c_parser.mode=config.ansi_c.mode;
64
65 ansi_c_scanner_init(ansi_c_parser);
66
67 if(ansi_c_parser.parse())
68 return true;
69
70 symbol_tablet new_symbol_table;
71
72 // this is recursive -- builtin_factory is called
73 // from the typechecker
75 ansi_c_parser.parse_tree,
76 new_symbol_table,
77 "", // module
78 message_handler))
79 {
80 return true;
81 }
82
83 // we should now have a new symbol
84 symbol_tablet::symbolst::const_iterator s_it=
85 new_symbol_table.symbols.find(identifier);
86
87 if(s_it==new_symbol_table.symbols.end())
88 {
89 messaget message(message_handler);
90 message.error() << "failed to produce built-in symbol '" << identifier
91 << '\'' << messaget::eom;
92 return true;
93 }
94
95 // copy the new symbol
96 symbol_table.add(s_it->second);
97
98 return false;
99}
100
105 const irep_idt &identifier,
106 bool support_float16_type,
107 symbol_table_baset &symbol_table,
109{
110 // we search for "space" "identifier" "("
111 const std::string pattern=' '+id2string(identifier)+'(';
112
113 std::ostringstream s;
114
115 std::string code;
116 ansi_c_internal_additions(code, support_float16_type);
117 s << code;
118
119 // our own extensions
121 return convert(identifier, s, symbol_table, mh);
122
123 // this is Visual C/C++ only
125 {
127 return convert(identifier, s, symbol_table, mh);
128 }
129
130 // ARM stuff
132 {
134 return convert(identifier, s, symbol_table, mh);
135 }
136
137 // CW stuff
139 {
141 return convert(identifier, s, symbol_table, mh);
142 }
143
144 // GCC junk stuff, also for CLANG and ARM
145 if(
149 {
151 return convert(identifier, s, symbol_table, mh);
152
154 return convert(identifier, s, symbol_table, mh);
155
157 return convert(identifier, s, symbol_table, mh);
158
160 return convert(identifier, s, symbol_table, mh);
161
163 return convert(identifier, s, symbol_table, mh);
164
166 return convert(identifier, s, symbol_table, mh);
167
169 return convert(identifier, s, symbol_table, mh);
170
171 if(config.ansi_c.arch=="i386" ||
172 config.ansi_c.arch=="x86_64" ||
173 config.ansi_c.arch=="x32")
174 {
176 return convert(identifier, s, symbol_table, mh);
177
179 return convert(identifier, s, symbol_table, mh);
180
182 return convert(identifier, s, symbol_table, mh);
183
185 return convert(identifier, s, symbol_table, mh);
186
188 return convert(identifier, s, symbol_table, mh);
189
191 return convert(identifier, s, symbol_table, mh);
192
194 return convert(identifier, s, symbol_table, mh);
195
197 return convert(identifier, s, symbol_table, mh);
198
200 return convert(identifier, s, symbol_table, mh);
201 }
202 else if(config.ansi_c.arch=="arm64" ||
203 config.ansi_c.arch=="armel" ||
204 config.ansi_c.arch=="armhf" ||
205 config.ansi_c.arch=="arm")
206 {
208 return convert(identifier, s, symbol_table, mh);
209 }
210 else if(config.ansi_c.arch=="mips64el" ||
211 config.ansi_c.arch=="mipsn32el" ||
212 config.ansi_c.arch=="mipsel" ||
213 config.ansi_c.arch=="mips64" ||
214 config.ansi_c.arch=="mipsn32" ||
215 config.ansi_c.arch=="mips")
216 {
218 return convert(identifier, s, symbol_table, mh);
219 }
220 else if(config.ansi_c.arch=="powerpc" ||
221 config.ansi_c.arch=="ppc64" ||
222 config.ansi_c.arch=="ppc64le")
223 {
225 return convert(identifier, s, symbol_table, mh);
226 }
227 }
228
229 return true;
230}
const char gcc_builtin_headers_ia32_7[]
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_ia32_8[]
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 gcc_builtin_headers_ia32_9[]
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)
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
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:154
mstreamt & error() const
Definition message.h:391
static eomt eom
Definition message.h:289
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.
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:44
std::string strip_string(const std::string &s)
Remove all whitespace characters from either end of a string.
Author: Diffblue Ltd.