db/smdb.py: add a try catch block printing the return states
[smatch.git] / predefine.c
blob98e38a04579dc5b7e053618add096f8361eccf5c
1 // SPDX-License-Identifier: MIT
2 // Copyright (C) 2017-2020 Luc Van Oostenryck.
4 #include <stdio.h>
6 #include "lib.h"
7 #include "machine.h"
8 #include "symbol.h"
10 #define PTYPE_SIZEOF (1U << 0)
11 #define PTYPE_T (1U << 1)
12 #define PTYPE_MAX (1U << 2)
13 #define PTYPE_MIN (1U << 3)
14 #define PTYPE_WIDTH (1U << 4)
15 #define PTYPE_TYPE (1U << 5)
16 #define PTYPE_ALL (PTYPE_MAX|PTYPE_SIZEOF|PTYPE_WIDTH)
17 #define PTYPE_ALL_T (PTYPE_MAX|PTYPE_SIZEOF|PTYPE_WIDTH|PTYPE_T)
20 static void predefined_sizeof(const char *name, const char *suffix, unsigned bits)
22 char buf[32];
24 snprintf(buf, sizeof(buf), "__SIZEOF_%s%s__", name, suffix);
25 predefine(buf, 1, "%d", bits/8);
28 static void predefined_width(const char *name, struct symbol *type)
30 char buf[32];
32 snprintf(buf, sizeof(buf), "__%s_WIDTH__", name);
33 predefine(buf, 1, "%d", type->bit_size);
36 static void predefined_max(const char *name, struct symbol *type)
38 const char *suffix = builtin_type_suffix(type);
39 unsigned bits = type->bit_size - is_signed_type(type);
40 unsigned long long max = bits_mask(bits);
41 char buf[32];
43 snprintf(buf, sizeof(buf), "__%s_MAX__", name);
44 predefine(buf, 1, "%#llx%s", max, suffix);
47 static void predefined_min(const char *name, struct symbol *type)
49 const char *suffix = builtin_type_suffix(type);
50 char buf[32];
52 snprintf(buf, sizeof(buf), "__%s_MIN__", name);
54 if (is_signed_type(type))
55 add_pre_buffer("#weak_define %s (-__%s_MAX__ - 1)\n", buf, name);
56 else
57 predefine(buf, 1, "0%s", suffix);
60 static void predefined_type(const char *name, struct symbol *type)
62 const char *typename = builtin_typename(type);
63 add_pre_buffer("#weak_define __%s_TYPE__ %s\n", name, typename);
66 static void predefined_ctype(const char *name, struct symbol *type, int flags)
68 unsigned bits = type->bit_size;
70 if (flags & PTYPE_SIZEOF) {
71 const char *suffix = (flags & PTYPE_T) ? "_T" : "";
72 predefined_sizeof(name, suffix, bits);
74 if (flags & PTYPE_MAX)
75 predefined_max(name, type);
76 if (flags & PTYPE_MIN)
77 predefined_min(name, type);
78 if (flags & PTYPE_TYPE)
79 predefined_type(name, type);
80 if (flags & PTYPE_WIDTH)
81 predefined_width(name, type);
84 void predefined_macros(void)
86 predefine("__CHECKER__", 0, "1");
87 predefine("__GNUC__", 1, "%d", gcc_major);
88 predefine("__GNUC_MINOR__", 1, "%d", gcc_minor);
89 predefine("__GNUC_PATCHLEVEL__", 1, "%d", gcc_patchlevel);
91 predefine("__STDC__", 1, "1");
92 predefine("__STDC_HOSTED__", 0, fhosted ? "1" : "0");
93 switch (standard) {
94 default:
95 break;
97 case STANDARD_C94:
98 predefine("__STDC_VERSION__", 1, "199409L");
99 break;
101 case STANDARD_C99:
102 case STANDARD_GNU99:
103 predefine("__STDC_VERSION__", 1, "199901L");
104 break;
106 case STANDARD_C11:
107 case STANDARD_GNU11:
108 predefine("__STDC_VERSION__", 1, "201112L");
109 break;
110 case STANDARD_C17:
111 case STANDARD_GNU17:
112 predefine("__STDC_VERSION__", 1, "201710L");
113 break;
115 if (!(standard & STANDARD_GNU) && (standard != STANDARD_NONE))
116 predefine("__STRICT_ANSI__", 1, "1");
117 if (standard >= STANDARD_C11) {
118 predefine("__STDC_NO_ATOMICS__", 1, "1");
119 predefine("__STDC_NO_COMPLEX__", 1, "1");
120 predefine("__STDC_NO_THREADS__", 1, "1");
123 predefine("__CHAR_BIT__", 1, "%d", bits_in_char);
124 if (funsigned_char)
125 predefine("__CHAR_UNSIGNED__", 1, "1");
127 predefined_ctype("SHORT", &short_ctype, PTYPE_SIZEOF);
128 predefined_ctype("SHRT", &short_ctype, PTYPE_MAX|PTYPE_WIDTH);
129 predefined_ctype("SCHAR", &schar_ctype, PTYPE_MAX|PTYPE_WIDTH);
130 predefined_ctype("WCHAR", wchar_ctype, PTYPE_ALL_T|PTYPE_MIN|PTYPE_TYPE);
131 predefined_ctype("WINT", wint_ctype, PTYPE_ALL_T|PTYPE_MIN|PTYPE_TYPE);
132 predefined_ctype("CHAR16", &ushort_ctype, PTYPE_TYPE);
133 predefined_ctype("CHAR32", uint32_ctype, PTYPE_TYPE);
135 predefined_ctype("INT", &int_ctype, PTYPE_ALL);
136 predefined_ctype("LONG", &long_ctype, PTYPE_ALL);
137 predefined_ctype("LONG_LONG", &llong_ctype, PTYPE_ALL);
139 predefined_ctype("INT8", &schar_ctype, PTYPE_MAX|PTYPE_TYPE);
140 predefined_ctype("UINT8", &uchar_ctype, PTYPE_MAX|PTYPE_TYPE);
141 predefined_ctype("INT16", &short_ctype, PTYPE_MAX|PTYPE_TYPE);
142 predefined_ctype("UINT16", &ushort_ctype, PTYPE_MAX|PTYPE_TYPE);
143 predefined_ctype("INT32", int32_ctype, PTYPE_MAX|PTYPE_TYPE);
144 predefined_ctype("UINT32", uint32_ctype, PTYPE_MAX|PTYPE_TYPE);
145 predefined_ctype("INT64", int64_ctype, PTYPE_MAX|PTYPE_TYPE);
146 predefined_ctype("UINT64", uint64_ctype, PTYPE_MAX|PTYPE_TYPE);
148 predefined_ctype("INT_LEAST8", &schar_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
149 predefined_ctype("UINT_LEAST8", &uchar_ctype, PTYPE_MAX|PTYPE_TYPE);
150 predefined_ctype("INT_LEAST16", &short_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
151 predefined_ctype("UINT_LEAST16",&ushort_ctype, PTYPE_MAX|PTYPE_TYPE);
152 predefined_ctype("INT_LEAST32", int32_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
153 predefined_ctype("UINT_LEAST32", uint32_ctype, PTYPE_MAX|PTYPE_TYPE);
154 predefined_ctype("INT_LEAST64", int64_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
155 predefined_ctype("UINT_LEAST64", uint64_ctype, PTYPE_MAX|PTYPE_TYPE);
157 predefined_ctype("INT_FAST8", fast8_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
158 predefined_ctype("UINT_FAST8", ufast8_ctype, PTYPE_MAX|PTYPE_TYPE);
159 predefined_ctype("INT_FAST16", fast16_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
160 predefined_ctype("UINT_FAST16",ufast16_ctype, PTYPE_MAX|PTYPE_TYPE);
161 predefined_ctype("INT_FAST32", fast32_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
162 predefined_ctype("UINT_FAST32",ufast32_ctype, PTYPE_MAX|PTYPE_TYPE);
163 predefined_ctype("INT_FAST64", fast64_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
164 predefined_ctype("UINT_FAST64",ufast64_ctype, PTYPE_MAX|PTYPE_TYPE);
166 predefined_ctype("INTMAX", intmax_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
167 predefined_ctype("UINTMAX", uintmax_ctype, PTYPE_MAX|PTYPE_TYPE);
168 predefined_ctype("INTPTR", intptr_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
169 predefined_ctype("UINTPTR", uintptr_ctype, PTYPE_MAX|PTYPE_TYPE);
170 predefined_ctype("PTRDIFF", ptrdiff_ctype, PTYPE_ALL_T|PTYPE_TYPE);
171 predefined_ctype("SIZE", size_t_ctype, PTYPE_ALL_T|PTYPE_TYPE);
172 predefined_ctype("POINTER", &ptr_ctype, PTYPE_SIZEOF);
173 predefined_ctype("SIG_ATOMIC", sig_atomic_ctype, PTYPE_MAX|PTYPE_MIN|PTYPE_TYPE|PTYPE_WIDTH);
175 predefined_sizeof("FLOAT", "", bits_in_float);
176 predefined_sizeof("DOUBLE", "", bits_in_double);
177 predefined_sizeof("LONG_DOUBLE", "", bits_in_longdouble);
179 if (arch_target->has_int128)
180 predefined_sizeof("INT128", "", 128);
182 predefine("__ATOMIC_RELAXED", 0, "0");
183 predefine("__ATOMIC_CONSUME", 0, "1");
184 predefine("__ATOMIC_ACQUIRE", 0, "3");
185 predefine("__ATOMIC_RELEASE", 0, "4");
186 predefine("__ATOMIC_ACQ_REL", 0, "7");
187 predefine("__ATOMIC_SEQ_CST", 0, "8");
189 predefine("__ORDER_LITTLE_ENDIAN__", 1, "1234");
190 predefine("__ORDER_BIG_ENDIAN__", 1, "4321");
191 predefine("__ORDER_PDP_ENDIAN__", 1, "3412");
192 if (arch_big_endian) {
193 predefine("__BIG_ENDIAN__", 1, "1");
194 predefine("__BYTE_ORDER__", 1, "__ORDER_BIG_ENDIAN__");
195 } else {
196 predefine("__LITTLE_ENDIAN__", 1, "1");
197 predefine("__BYTE_ORDER__", 1, "__ORDER_LITTLE_ENDIAN__");
200 if (optimize_level)
201 predefine("__OPTIMIZE__", 0, "1");
202 if (optimize_size)
203 predefine("__OPTIMIZE_SIZE__", 0, "1");
205 predefine("__PRAGMA_REDEFINE_EXTNAME", 1, "1");
207 // Temporary hacks
208 predefine("__extension__", 0, NULL);
209 predefine("__pragma__", 0, NULL);
211 switch (arch_m64) {
212 case ARCH_LP32:
213 break;
214 case ARCH_X32:
215 predefine("__ILP32__", 1, "1");
216 predefine("_ILP32", 1, "1");
217 break;
218 case ARCH_LP64:
219 predefine("__LP64__", 1, "1");
220 predefine("_LP64", 1, "1");
221 break;
222 case ARCH_LLP64:
223 predefine("__LLP64__", 1, "1");
224 break;
227 if (fpic) {
228 predefine("__pic__", 0, "%d", fpic);
229 predefine("__PIC__", 0, "%d", fpic);
231 if (fpie) {
232 predefine("__pie__", 0, "%d", fpie);
233 predefine("__PIE__", 0, "%d", fpie);
236 if (arch_target->predefine)
237 arch_target->predefine(arch_target);
239 if (arch_os >= OS_UNIX && arch_os != OS_DARWIN) {
240 predefine("__unix__", 1, "1");
241 predefine("__unix", 1, "1");
242 predefine_nostd("unix");
245 switch (arch_os) {
246 case OS_CYGWIN:
247 predefine("__CYGWIN__", 1, "1");
248 if (arch_m64 == ARCH_LP32)
249 predefine("__CYGWIN32__", 1, "1");
250 add_pre_buffer("#define __cdecl __attribute__((__cdecl__))\n");
251 add_pre_buffer("#define __declspec(x) __attribute__((x))\n");
252 add_pre_buffer("#define __fastcall __attribute__((__fastcall__))\n");
253 add_pre_buffer("#define __stdcall __attribute__((__stdcall__))\n");
254 add_pre_buffer("#define __thiscall __attribute__((__thiscall__))\n");
255 add_pre_buffer("#define _cdecl __attribute__((__cdecl__))\n");
256 add_pre_buffer("#define _fastcall __attribute__((__fastcall__))\n");
257 add_pre_buffer("#define _stdcall __attribute__((__stdcall__))\n");
258 add_pre_buffer("#define _thiscall __attribute__((__thiscall__))\n");
259 break;
260 case OS_DARWIN:
261 predefine("__APPLE__", 1, "1");
262 predefine("__APPLE_CC__", 1, "1");
263 predefine("__MACH__", 1, "1");
264 break;
265 case OS_FREEBSD:
266 predefine("__FreeBSD__", 1, "1");
267 break;
268 case OS_LINUX:
269 predefine("__linux__", 1, "1");
270 predefine("__linux", 1, "1");
271 break;
272 case OS_NETBSD:
273 predefine("__NetBSD__", 1, "1");
274 break;
275 case OS_OPENBSD:
276 predefine("__OpenBSD__", 1, "1");
277 break;
278 case OS_SUNOS:
279 predefine("__sun__", 1, "1");
280 predefine("__sun", 1, "1");
281 predefine_nostd("sun");
282 predefine("__svr4__", 1, "1");
283 break;