dce: Don't force unreachable case label to be live
[tinycc.git] / conftest.c
blobad7275cf67c490bb71e5248c67d3655a94addc54
1 /* ----------------------------------------------------------------------- */
2 #if C2STR
4 /* with -D C2STR: convert tccdefs.h to C-strings */
6 #include <stdio.h>
7 #include <string.h>
9 /* replace native host macros by compile-time versions */
10 const char *platform_macros[] = {
11 "__i386__", "TCC_TARGET_I386",
12 "__x86_64__", "TCC_TARGET_X86_64",
13 "_WIN32", "TCC_TARGET_PE",
14 "__arm__", "TCC_TARGET_ARM",
15 "__ARM_EABI__", "TCC_ARM_EABI",
16 "__aarch64__", "TCC_TARGET_ARM64",
17 "__riscv", "TCC_TARGET_RISCV64",
18 "__APPLE__", "TCC_TARGET_MACHO",
19 "__FreeBSD__", "TARGETOS_FreeBSD",
20 "__FreeBSD_kernel__", "TARGETOS_FreeBSD_kernel",
21 "__OpenBSD__", "TARGETOS_OpenBSD",
22 "__NetBSD__", "TARGETOS_NetBSD",
23 "__linux__", "TARGETOS_Linux",
24 "__ANDROID__", "TARGETOS_ANDROID",
26 "__SIZEOF_POINTER__", "PTR_SIZE",
27 "__SIZEOF_LONG__", "LONG_SIZE",
31 int isid(int c)
33 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
34 || (c >= '0' && c <= '9') || c == '_';
37 int isspc(int c)
39 return (unsigned char)c <= ' ' && c != 0;
42 int main(int argc, char **argv)
44 char l[1000], l2[1000], *p, *q, *p0;
45 FILE *fp, *op;
46 int c, e, f, s, cmt, cmt_n;
47 const char *r;
49 if (argc < 3)
50 return 1;
52 fp = fopen(argv[1], "rb");
53 op = fopen(argv[2], "wb");
54 if (!fp || !op) {
55 fprintf(stderr, "c2str: file error\n");
56 return 1;
59 cmt = cmt_n = 0;
60 for (;;) {
61 p = l;
62 append:
63 if (fgets(p, sizeof l - (p - l), fp)) {
64 p = strchr(p, 0);
65 while (p > l && isspc(p[-1]))
66 --p;
67 *p = 0;
68 } else if (p == l)
69 break;
71 /* check for continuation */
72 if (p > l && p[-1] == '\\') {
73 p[-1] = ' ';
74 goto append;
77 /* count & skip leading spaces */
78 p = l, q = l2, f = 0;
79 while (*p && isspc(*p))
80 ++p, ++f;
82 /* handle comments */
83 if (p[0] == '/' && cmt == 0) {
84 if (p[1] == '*')
85 cmt = 2;
86 if (p[1] == '/')
87 cmt = 1;
89 if (cmt) {
90 fprintf(op, "%s", l);
91 if (++cmt_n == 1)
92 fprintf(op, " (converted, do not edit this file)");
93 fprintf(op, "\n");
94 if (cmt == 1)
95 cmt = 0;
96 if (cmt == 2) {
97 p = strchr(l, 0);
98 if (p >= l + 2 && p[-1] == '/' && p[-2] == '*')
99 cmt = 0;
101 continue;
104 if (f < 4) {
105 do {
106 /* replace machine/os macros by compile-time counterparts */
107 for (e = f = 0; (r = platform_macros[f]); f += 2) {
108 c = strlen(r);
109 /* remove 'defined' */
110 //e = memcmp(p, "defined ", 8) ? 0 : 8;
111 if (0 == memcmp(p + e, r, c)) {
112 p += e + c;
113 q = strchr(strcpy(q, platform_macros[f + 1]), 0);
114 break;
118 if (r)
119 continue;
120 } while (!!(*q++ = *p++));
121 /* output as is */
122 fprintf(op, "%s\n", l2);
123 continue;
125 } else {
126 s = e = f = 0, p0 = p;
127 for (;;) {
128 c = *p++;
130 if (isspc(c)) {
131 s = 1;
132 continue;
134 if (c == '/' && (p[0] == '/' || p[0] == '*'))
135 c = 0; /* trailing comment detected */
136 else if (s && q > l2
137 && ((isid(q[-1]) && isid(c))
138 // keep space after macro name
139 || (q >= l2 + 2
140 && l2[0] == '#'
141 && l2[1] == 'd'
142 && f < 2 && !e
144 *q++ = ' ', ++f;
145 s = 0;
147 if (c == '(')
148 ++e;
149 if (c == ')')
150 --e;
151 if (c == '\\' || c == '\"')
152 *q++ = '\\';
153 *q++ = c;
154 if (c == 0)
155 break;
156 p0 = p;
158 /* output with quotes */
159 fprintf(op, " \"%s\\n\"%s\n", l2, p0);
163 fclose(fp);
164 fclose(op);
165 return 0;
168 /* ----------------------------------------------------------------------- */
169 #elif 1
171 /* get some information from the host compiler for configure */
173 #include <stdio.h>
175 #if defined(_WIN32)
176 #include <fcntl.h>
177 #endif
179 /* Define architecture */
180 #if defined(__i386__) || defined _M_IX86
181 # define TRIPLET_ARCH "i386"
182 #elif defined(__x86_64__) || defined _M_AMD64
183 # define TRIPLET_ARCH "x86_64"
184 #elif defined(__arm__)
185 # define TRIPLET_ARCH "arm"
186 #elif defined(__aarch64__)
187 # define TRIPLET_ARCH "aarch64"
188 #elif defined(__riscv) && defined(__LP64__)
189 # define TRIPLET_ARCH "riscv64"
190 #else
191 # define TRIPLET_ARCH "unknown"
192 #endif
194 /* Define OS */
195 #if defined (__linux__)
196 # define TRIPLET_OS "linux"
197 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
198 # define TRIPLET_OS "kfreebsd"
199 #elif defined(__NetBSD__)
200 # define TRIPLET_OS "netbsd"
201 #elif defined(__OpenBSD__)
202 # define TRIPLET_OS "openbsd"
203 #elif defined(_WIN32)
204 # define TRIPLET_OS "win32"
205 #elif defined(__APPLE__)
206 # define TRIPLET_OS "darwin"
207 #elif !defined (__GNU__)
208 # define TRIPLET_OS "unknown"
209 #endif
211 #if defined __ANDROID__
212 # define ABI_PREFIX "android"
213 #else
214 # define ABI_PREFIX "gnu"
215 #endif
217 /* Define calling convention and ABI */
218 #if defined (__ARM_EABI__)
219 # if defined (__ARM_PCS_VFP)
220 # define TRIPLET_ABI ABI_PREFIX"eabihf"
221 # else
222 # define TRIPLET_ABI ABI_PREFIX"eabi"
223 # endif
224 #else
225 # define TRIPLET_ABI ABI_PREFIX
226 #endif
228 #if defined _WIN32
229 # define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS
230 #elif defined __GNU__
231 # define TRIPLET TRIPLET_ARCH "-" TRIPLET_ABI
232 #else
233 # define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS "-" TRIPLET_ABI
234 #endif
236 #if defined(_WIN32)
237 int _CRT_glob = 0;
238 #endif
240 int main(int argc, char *argv[])
242 #if defined(_WIN32)
243 _setmode(_fileno(stdout), _O_BINARY); /* don't translate \n to \r\n */
244 #endif
245 switch(argc == 2 ? argv[1][0] : 0) {
246 case 'b'://igendian
248 volatile unsigned foo = 0x01234567;
249 puts(*(unsigned char*)&foo == 0x67 ? "no" : "yes");
250 break;
252 #if defined(__clang__)
253 case 'm'://inor
254 printf("%d\n", __clang_minor__);
255 break;
256 case 'v'://ersion
257 printf("%d\n", __clang_major__);
258 break;
259 #elif defined(__TINYC__)
260 case 'v'://ersion
261 puts("0");
262 break;
263 case 'm'://inor
264 printf("%d\n", __TINYC__);
265 break;
266 #elif defined(_MSC_VER)
267 case 'v'://ersion
268 puts("0");
269 break;
270 case 'm'://inor
271 printf("%d\n", _MSC_VER);
272 break;
273 #elif defined(__GNUC__) && defined(__GNUC_MINOR__)
274 /* GNU comes last as other compilers may add 'GNU' compatibility */
275 case 'm'://inor
276 printf("%d\n", __GNUC_MINOR__);
277 break;
278 case 'v'://ersion
279 printf("%d\n", __GNUC__);
280 break;
281 #else
282 case 'm'://inor
283 case 'v'://ersion
284 puts("0");
285 break;
286 #endif
287 case 't'://riplet
288 puts(TRIPLET);
289 break;
290 case 'c'://ompiler
291 #if defined(__clang__)
292 puts("clang");
293 #elif defined(__TINYC__)
294 puts("tcc");
295 #elif defined(_MSC_VER)
296 puts("msvc");
297 #elif defined(__GNUC__)
298 puts("gcc");
299 #else
300 puts("unknown");
301 #endif
302 break;
303 default:
304 break;
306 return 0;
309 /* ----------------------------------------------------------------------- */
310 #endif