riscv: jalr: implement pseudo and parse like GAS
[tinycc/nsauzede.git] / conftest.c
bloba4450d5143ddc884ddb7a9b367e149e0b2e60345
1 /* ----------------------------------------------------------------------- */
2 /* with -D C2STR: convert tccdefs.h to C-strings */
4 #if C2STR
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 /* get some information from the host compiler for configure */
171 #elif 1
173 #include <stdio.h>
175 #if defined(_WIN32)
176 #include <fcntl.h>
177 #include <io.h>
178 int _CRT_glob = 0;
179 #endif
181 /* Define architecture */
182 #if defined(__i386__) || defined _M_IX86
183 # define TRIPLET_ARCH "i386"
184 #elif defined(__x86_64__) || defined _M_AMD64
185 # define TRIPLET_ARCH "x86_64"
186 #elif defined(__arm__)
187 # define TRIPLET_ARCH "arm"
188 #elif defined(__aarch64__)
189 # define TRIPLET_ARCH "aarch64"
190 #elif defined(__riscv) && defined(__LP64__)
191 # define TRIPLET_ARCH "riscv64"
192 #else
193 # define TRIPLET_ARCH "unknown"
194 #endif
196 /* Define OS */
197 #if defined (__linux__)
198 # define TRIPLET_OS "linux"
199 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
200 # define TRIPLET_OS "kfreebsd"
201 #elif defined(__NetBSD__)
202 # define TRIPLET_OS "netbsd"
203 #elif defined(__OpenBSD__)
204 # define TRIPLET_OS "openbsd"
205 #elif defined(_WIN32)
206 # define TRIPLET_OS "win32"
207 #elif defined(__APPLE__)
208 # define TRIPLET_OS "darwin"
209 #elif !defined (__GNU__)
210 # define TRIPLET_OS "unknown"
211 #endif
213 #if defined __ANDROID__
214 # define ABI_PREFIX "android"
215 #else
216 # define ABI_PREFIX "gnu"
217 #endif
219 /* Define calling convention and ABI */
220 #if defined (__ARM_EABI__)
221 # if defined (__ARM_PCS_VFP)
222 # define TRIPLET_ABI ABI_PREFIX"eabihf"
223 # else
224 # define TRIPLET_ABI ABI_PREFIX"eabi"
225 # endif
226 #else
227 # define TRIPLET_ABI ABI_PREFIX
228 #endif
230 #if defined _WIN32
231 # define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS
232 #elif defined __GNU__
233 # define TRIPLET TRIPLET_ARCH "-" TRIPLET_ABI
234 #else
235 # define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS "-" TRIPLET_ABI
236 #endif
238 int main(int argc, char *argv[])
240 #if defined(_WIN32)
241 _setmode(_fileno(stdout), _O_BINARY); /* don't translate \n to \r\n */
242 #endif
243 switch(argc == 2 ? argv[1][0] : 0) {
244 case 'b'://igendian
246 volatile unsigned foo = 0x01234567;
247 puts(*(unsigned char*)&foo == 0x67 ? "no" : "yes");
248 break;
250 #if defined(__clang__)
251 case 'm'://inor
252 printf("%d\n", __clang_minor__);
253 break;
254 case 'v'://ersion
255 printf("%d\n", __clang_major__);
256 break;
257 #elif defined(__TINYC__)
258 case 'v'://ersion
259 puts("0");
260 break;
261 case 'm'://inor
262 printf("%d\n", __TINYC__);
263 break;
264 #elif defined(_MSC_VER)
265 case 'v'://ersion
266 puts("0");
267 break;
268 case 'm'://inor
269 printf("%d\n", _MSC_VER);
270 break;
271 #elif defined(__GNUC__) && defined(__GNUC_MINOR__)
272 /* GNU comes last as other compilers may add 'GNU' compatibility */
273 case 'm'://inor
274 printf("%d\n", __GNUC_MINOR__);
275 break;
276 case 'v'://ersion
277 printf("%d\n", __GNUC__);
278 break;
279 #else
280 case 'm'://inor
281 case 'v'://ersion
282 puts("0");
283 break;
284 #endif
285 case 't'://riplet
286 puts(TRIPLET);
287 break;
288 case 'c'://ompiler
289 #if defined(__clang__)
290 puts("clang");
291 #elif defined(__TINYC__)
292 puts("tcc");
293 #elif defined(_MSC_VER)
294 puts("msvc");
295 #elif defined(__GNUC__)
296 puts("gcc");
297 #else
298 puts("unknown");
299 #endif
300 break;
301 default:
302 break;
304 return 0;
307 /* ----------------------------------------------------------------------- */
308 #endif