return_to_param: delete debug code
[smatch.git] / predefine.c
blobf898cdfa39b81b78846407754e1e5ef91ebdf59b
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("__ORDER_LITTLE_ENDIAN__", 1, "1234");
183 predefine("__ORDER_BIG_ENDIAN__", 1, "4321");
184 predefine("__ORDER_PDP_ENDIAN__", 1, "3412");
185 if (arch_big_endian) {
186 predefine("__BIG_ENDIAN__", 1, "1");
187 predefine("__BYTE_ORDER__", 1, "__ORDER_BIG_ENDIAN__");
188 } else {
189 predefine("__LITTLE_ENDIAN__", 1, "1");
190 predefine("__BYTE_ORDER__", 1, "__ORDER_LITTLE_ENDIAN__");
193 if (optimize_level)
194 predefine("__OPTIMIZE__", 0, "1");
195 if (optimize_size)
196 predefine("__OPTIMIZE_SIZE__", 0, "1");
198 predefine("__PRAGMA_REDEFINE_EXTNAME", 1, "1");
200 // Temporary hacks
201 predefine("__extension__", 0, NULL);
202 predefine("__pragma__", 0, NULL);
204 switch (arch_m64) {
205 case ARCH_LP32:
206 break;
207 case ARCH_X32:
208 predefine("__ILP32__", 1, "1");
209 predefine("_ILP32", 1, "1");
210 break;
211 case ARCH_LP64:
212 predefine("__LP64__", 1, "1");
213 predefine("_LP64", 1, "1");
214 break;
215 case ARCH_LLP64:
216 predefine("__LLP64__", 1, "1");
217 break;
220 if (fpic) {
221 predefine("__pic__", 0, "%d", fpic);
222 predefine("__PIC__", 0, "%d", fpic);
224 if (fpie) {
225 predefine("__pie__", 0, "%d", fpie);
226 predefine("__PIE__", 0, "%d", fpie);
229 if (arch_target->predefine)
230 arch_target->predefine(arch_target);
232 if (arch_os >= OS_UNIX && arch_os != OS_DARWIN) {
233 predefine("__unix__", 1, "1");
234 predefine("__unix", 1, "1");
235 predefine_nostd("unix");
238 switch (arch_os) {
239 case OS_CYGWIN:
240 predefine("__CYGWIN__", 1, "1");
241 if (arch_m64 == ARCH_LP32)
242 predefine("__CYGWIN32__", 1, "1");
243 add_pre_buffer("#define __cdecl __attribute__((__cdecl__))\n");
244 add_pre_buffer("#define __declspec(x) __attribute__((x))\n");
245 add_pre_buffer("#define __fastcall __attribute__((__fastcall__))\n");
246 add_pre_buffer("#define __stdcall __attribute__((__stdcall__))\n");
247 add_pre_buffer("#define __thiscall __attribute__((__thiscall__))\n");
248 add_pre_buffer("#define _cdecl __attribute__((__cdecl__))\n");
249 add_pre_buffer("#define _fastcall __attribute__((__fastcall__))\n");
250 add_pre_buffer("#define _stdcall __attribute__((__stdcall__))\n");
251 add_pre_buffer("#define _thiscall __attribute__((__thiscall__))\n");
252 break;
253 case OS_DARWIN:
254 predefine("__APPLE__", 1, "1");
255 predefine("__APPLE_CC__", 1, "1");
256 predefine("__MACH__", 1, "1");
257 break;
258 case OS_FREEBSD:
259 predefine("__FreeBSD__", 1, "1");
260 break;
261 case OS_LINUX:
262 predefine("__linux__", 1, "1");
263 predefine("__linux", 1, "1");
264 break;
265 case OS_NETBSD:
266 predefine("__NetBSD__", 1, "1");
267 break;
268 case OS_OPENBSD:
269 predefine("__OpenBSD__", 1, "1");
270 break;
271 case OS_SUNOS:
272 predefine("__sun__", 1, "1");
273 predefine("__sun", 1, "1");
274 predefine_nostd("sun");
275 predefine("__svr4__", 1, "1");
276 break;