CBMC
Loading...
Searching...
No Matches
cpp_internal_additions.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
10
11#include <ostream>
12
13#include <util/c_types.h>
14#include <util/config.h>
15
17
19
20std::string c2cpp(const std::string &s)
21{
22 std::string result;
23
24 result.reserve(s.size());
25
26 for(std::size_t i=0; i<s.size(); i++)
27 {
28 char ch=s[i];
29
30 if(ch=='_' && std::string(s, i, 5)=="_Bool")
31 {
32 result.append("bool");
33 i+=4;
34 continue;
35 }
36
37 result+=ch;
38 }
39
40 return result;
41}
42
43void cpp_internal_additions(std::ostream &out)
44{
45 out << "#line 1 \"<built-in-additions>\"" << '\n';
46
47 // __CPROVER namespace
48 out << "namespace __CPROVER { }" << '\n';
49
50 // types
51 out << "typedef __typeof__(sizeof(int)) __CPROVER::size_t;" << '\n';
52 out << "typedef __CPROVER::size_t " CPROVER_PREFIX "size_t;" << '\n';
53 out << "typedef "
54 << c_type_as_string(signed_size_type().get(ID_C_c_type))
55 << " __CPROVER::ssize_t;" << '\n';
56 out << "typedef __CPROVER::ssize_t " CPROVER_PREFIX "ssize_t;" << '\n';
57
58 // new and delete are in the root namespace!
59 out << "void operator delete(void *);" << '\n';
60 out << "void *operator new(__CPROVER::size_t);" << '\n';
61
62 out << "extern \"C\" {" << '\n';
63
64 // CPROVER extensions
65 out << "const unsigned __CPROVER::constant_infinity_uint;" << '\n';
66 out << "typedef void " CPROVER_PREFIX "integer;" << '\n';
67 out << "typedef void " CPROVER_PREFIX "rational;" << '\n';
68
69 // malloc
70 out << "const void *" CPROVER_PREFIX "deallocated = 0;" << '\n';
71 out << "const void *" CPROVER_PREFIX "dead_object = 0;" << '\n';
72 out << "const void *" CPROVER_PREFIX "memory_leak = 0;" << '\n';
73 out << "void *" CPROVER_PREFIX "allocate("
74 << CPROVER_PREFIX "size_t size, " CPROVER_PREFIX "bool zero);" << '\n';
75
76 // auxiliaries for new/delete
77 out << "void *__new(__CPROVER::size_t);" << '\n';
78 out << "void *__new_array(__CPROVER::size_t, __CPROVER::size_t);" << '\n';
79 out << "void *__placement_new(__CPROVER::size_t, void *);" << '\n';
80 out << "void *__placement_new_array("
81 << "__CPROVER::size_t, __CPROVER::size_t, void *);" << '\n';
82 out << "void __delete(void *);" << '\n';
83 out << "void __delete_array(void *);" << '\n';
84
85 // float
86 // TODO: should be thread_local
87 out << "int " << configt::rounding_mode_identifier() << " = "
88 << std::to_string(config.ansi_c.rounding_mode) << ';' << '\n';
89
90 // pipes, write, read, close
91 out << "struct " CPROVER_PREFIX "pipet {\n"
92 << " bool widowed;\n"
93 << " char data[4];\n"
94 << " short next_avail;\n"
95 << " short next_unread;\n"
96 << "};\n";
97
98 // This function needs to be declared, or otherwise can't be called
99 // by the entry-point construction.
100 out << "void " INITIALIZE_FUNCTION "();" << '\n';
101
102 // GCC junk stuff, also for CLANG and ARM
103 if(
107 {
109
110 if(
111 config.ansi_c.arch == "i386" || config.ansi_c.arch == "x86_64" ||
112 config.ansi_c.arch == "x32" || config.ansi_c.arch == "ia64" ||
113 config.ansi_c.arch == "powerpc" || config.ansi_c.arch == "ppc64")
114 {
115 // https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html
116 // For clang, __float128 is a keyword.
117 // For gcc, this is a typedef and not a keyword.
118 // C++ doesn't have _Float128.
120 out << "typedef " CPROVER_PREFIX "Float128 __float128;" << '\n';
121 }
122 else if(config.ansi_c.arch == "hppa")
123 {
124 // https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html
125 // For clang, __float128 is a keyword.
126 // For gcc, this is a typedef and not a keyword.
127 // C++ doesn't have _Float128.
129 out << "typedef long double __float128;" << '\n';
130 }
131 else if(config.ansi_c.arch == "ppc64le")
132 {
133 // https://patchwork.ozlabs.org/patch/792295/
135 out << "typedef " CPROVER_PREFIX "Float128 __ieee128;\n";
136 }
137
138 if(
139 config.ansi_c.arch == "i386" || config.ansi_c.arch == "x86_64" ||
140 config.ansi_c.arch == "x32" || config.ansi_c.arch == "ia64")
141 {
142 // clang doesn't do __float80
143 // Note that __float80 is a typedef, and not a keyword,
144 // and that C++ doesn't have _Float64x.
146 out << "typedef " CPROVER_PREFIX "Float80 __float80;" << '\n';
147 }
148
149 // On 64-bit systems, gcc has typedefs
150 // __int128_t und __uint128_t -- but not on 32 bit!
151 if(config.ansi_c.long_int_width >= 64)
152 {
153 out << "typedef signed __int128 __int128_t;" << '\n';
154 out << "typedef unsigned __int128 __uint128_t;" << '\n';
155 }
156
157 if(
158 config.ansi_c.arch == "arm64" &&
160 {
161 out << "typedef struct __va_list {";
162 out << "void *__stack;";
163 out << "void *__gr_top;";
164 out << "void *__vr_top;";
165 out << "int __gr_offs;";
166 out << "int __vr_offs;";
167 out << " } __builtin_va_list;" << '\n';
168 }
169 else
170 {
171 out << "typedef void ** __builtin_va_list;" << '\n';
172 }
173 }
174
175 // this is Visual C/C++ only
177 {
178 out << "int __noop(...);" << '\n';
179 out << "int __assume(int);" << '\n';
180 }
181
182 // ARM stuff
183 if(config.ansi_c.mode == configt::ansi_ct::flavourt::ARM)
185
186 // CW stuff
189
190 // string symbols to identify the architecture we have compiled for
191 std::string architecture_strings;
192 ansi_c_architecture_strings(architecture_strings);
193 out << c2cpp(architecture_strings);
194
195 out << '}' << '\n'; // end extern "C"
196
197 // Microsoft stuff
199 {
200 // type_info infrastructure -- the standard wants this to be in the
201 // std:: namespace, but MS has it in the root namespace
202 out << "class type_info;" << '\n';
203
204 // this is the return type of __uuidof(...),
205 // in the root namespace
206 out << "struct _GUID;" << '\n';
207
208 // MS ATL-related stuff
209 out << "namespace ATL; " << '\n';
210 out << "void ATL::AtlThrowImpl(long);" << '\n';
211 out << "void __stdcall ATL::AtlThrowLastWin32();" << '\n';
212 }
213
214 out << std::flush;
215}
const char gcc_builtin_headers_types[]
const char cw_builtin_headers[]
const char arm_builtin_headers[]
void ansi_c_architecture_strings(std::string &code)
configt config
Definition config.cpp:25
std::string c_type_as_string(const irep_idt &c_type)
Definition c_types.cpp:251
signedbv_typet signed_size_type()
Definition c_types.cpp:66
static irep_idt rounding_mode_identifier()
Return the identifier of the program symbol used to store the current rounding mode.
Definition config.cpp:1706
std::string c2cpp(const std::string &s)
void cpp_internal_additions(std::ostream &out)
#define CPROVER_PREFIX
#define INITIALIZE_FUNCTION