moved target_machine defines to the <target>-gen.c files.
Unified · Split
| @@ -221,8 +221,8 @@ endif · 220 unmodified lines | ||
| 221 | 221 | endif |
| 222 | 222 | |
| 223 | 223 | # convert "include/tccdefs.h" to "tccdefs_.h" |
| 224 | −%_.h : include/%.h tests/misc/c2str.c | |
| 225 | − $S$(CC) -o c2str.exe $(filter %.c,$^) && ./c2str.exe $< $@ | |
| 224 | +%_.h : include/%.h conftest.c | |
| 225 | + $S$(CC) -DC2STR $(filter %.c,$^) -o c2str.exe && ./c2str.exe $< $@ | |
| 226 | 226 | |
| 227 | 227 | # target specific object rule |
| 228 | 228 | $(X)%.o : %.c $(LIBTCC_INC) |
| @@ -141,6 +141,21 @@ enum { · 140 unmodified lines | ||
| 141 | 141 | #define USING_GLOBALS |
| 142 | 142 | #include "tcc.h" |
| 143 | 143 | |
| 144 | +ST_DATA const char *target_machine_defs = | |
| 145 | + "__arm__\0" | |
| 146 | + "__arm\0" | |
| 147 | + "arm\0" | |
| 148 | + "__arm_elf__\0" | |
| 149 | + "__arm_elf\0" | |
| 150 | + "arm_elf\0" | |
| 151 | + "__ARM_ARCH_4__\0" | |
| 152 | + "__ARMEL__\0" | |
| 153 | + "__APCS_32__\0" | |
| 154 | +#if defined TCC_ARM_EABI | |
| 155 | + "__ARM_EABI__\0" | |
| 156 | +#endif | |
| 157 | + ; | |
| 158 | + | |
| 144 | 159 | enum float_abi float_abi; |
| 145 | 160 | |
| 146 | 161 | ST_DATA const int reg_classes[NB_REGS] = { |
| @@ -50,6 +50,10 @@ · 49 unmodified lines | ||
| 50 | 50 | #include "tcc.h" |
| 51 | 51 | #include <assert.h> |
| 52 | 52 | |
| 53 | +ST_DATA const char *target_machine_defs = | |
| 54 | + "__aarch64__\0" | |
| 55 | + ; | |
| 56 | + | |
| 53 | 57 | ST_DATA const int reg_classes[NB_REGS] = { |
| 54 | 58 | RC_INT | RC_R(0), |
| 55 | 59 | RC_INT | RC_R(1), |
| @@ -114,8 +114,12 @@ enum { · 113 unmodified lines | ||
| 114 | 114 | #define USING_GLOBALS |
| 115 | 115 | #include "tcc.h" |
| 116 | 116 | |
| 117 | +ST_DATA const char *target_machine_defs = | |
| 118 | + "__C67__\0" | |
| 119 | + ; | |
| 120 | + | |
| 117 | 121 | ST_DATA const int reg_classes[NB_REGS] = { |
| 118 | − /* eax */ RC_INT | RC_FLOAT | RC_EAX, | |
| 122 | + /* eax */ RC_INT | RC_FLOAT | RC_EAX, | |
| 119 | 123 | // only allow even regs for floats (allow for doubles) |
| 120 | 124 | /* ecx */ RC_INT | RC_ECX, |
| 121 | 125 | /* edx */ RC_INT | RC_INT_BSIDE | RC_FLOAT | RC_EDX, |
| @@ -1,3 +1,173 @@ | ||
| 1 | +/* ----------------------------------------------------------------------- */ | |
| 2 | +#if C2STR | |
| 3 | + | |
| 4 | +/* with -D C2STR: convert tccdefs.h to C-strings */ | |
| 5 | + | |
| 6 | +#include <stdio.h> | |
| 7 | +#include <string.h> | |
| 8 | + | |
| 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 | + "__SIZEOF_POINTER__", "PTR_SIZE", | |
| 25 | + "__SIZEOF_LONG__", "LONG_SIZE", | |
| 26 | + 0 | |
| 27 | +}; | |
| 28 | + | |
| 29 | +int isid(int c) | |
| 30 | +{ | |
| 31 | + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') | |
| 32 | + || (c >= '0' && c <= '9') || c == '_'; | |
| 33 | +} | |
| 34 | + | |
| 35 | +int isspc(int c) | |
| 36 | +{ | |
| 37 | + return (unsigned char)c <= ' ' && c != 0; | |
| 38 | +} | |
| 39 | + | |
| 40 | +int main(int argc, char **argv) | |
| 41 | +{ | |
| 42 | + char l[1000], l2[1000], *p, *q, *p0; | |
| 43 | + FILE *fp, *op; | |
| 44 | + int c, e, f, s, cmt, cmt_n; | |
| 45 | + const char *r; | |
| 46 | + | |
| 47 | + if (argc < 3) | |
| 48 | + return 1; | |
| 49 | + | |
| 50 | + fp = fopen(argv[1], "rb"); | |
| 51 | + op = fopen(argv[2], "wb"); | |
| 52 | + if (!fp || !op) { | |
| 53 | + fprintf(stderr, "c2str: file error\n"); | |
| 54 | + return 1; | |
| 55 | + } | |
| 56 | + | |
| 57 | + cmt = cmt_n = 0; | |
| 58 | + for (;;) { | |
| 59 | + p = l; | |
| 60 | + append: | |
| 61 | + if (fgets(p, sizeof l - (p - l), fp)) { | |
| 62 | + p = strchr(p, 0); | |
| 63 | + while (p > l && isspc(p[-1])) | |
| 64 | + --p; | |
| 65 | + *p = 0; | |
| 66 | + } else if (p == l) | |
| 67 | + break; | |
| 68 | + | |
| 69 | + /* check for continuation */ | |
| 70 | + if (p > l && p[-1] == '\\') { | |
| 71 | + p[-1] = ' '; | |
| 72 | + goto append; | |
| 73 | + } | |
| 74 | + | |
| 75 | + /* count & skip leading spaces */ | |
| 76 | + p = l, q = l2, f = 0; | |
| 77 | + while (*p && isspc(*p)) | |
| 78 | + ++p, ++f; | |
| 79 | + | |
| 80 | + /* handle comments */ | |
| 81 | + if (p[0] == '/' && cmt == 0) { | |
| 82 | + if (p[1] == '*') | |
| 83 | + cmt = 2; | |
| 84 | + if (p[1] == '/') | |
| 85 | + cmt = 1; | |
| 86 | + } | |
| 87 | + if (cmt) { | |
| 88 | + fprintf(op, "%s", l); | |
| 89 | + if (++cmt_n == 1) | |
| 90 | + fprintf(op, " (converted, do not edit this file)"); | |
| 91 | + fprintf(op, "\n"); | |
| 92 | + if (cmt == 1) | |
| 93 | + cmt = 0; | |
| 94 | + if (cmt == 2) { | |
| 95 | + p = strchr(l, 0); | |
| 96 | + if (p >= l + 2 && p[-1] == '/' && p[-2] == '*') | |
| 97 | + cmt = 0; | |
| 98 | + } | |
| 99 | + continue; | |
| 100 | + } | |
| 101 | + | |
| 102 | + if (f < 4) { | |
| 103 | + do { | |
| 104 | + /* replace machine/os macros by compile-time counterparts */ | |
| 105 | + for (e = f = 0; (r = platform_macros[f]); f += 2) { | |
| 106 | + c = strlen(r); | |
| 107 | + /* remove 'defined' */ | |
| 108 | + //e = memcmp(p, "defined ", 8) ? 0 : 8; | |
| 109 | + if (0 == memcmp(p + e, r, c)) { | |
| 110 | + p += e + c; | |
| 111 | + q = strchr(strcpy(q, platform_macros[f + 1]), 0); | |
| 112 | + break; | |
| 113 | + } | |
| 114 | + | |
| 115 | + } | |
| 116 | + if (r) | |
| 117 | + continue; | |
| 118 | + } while (!!(*q++ = *p++)); | |
| 119 | + /* output as is */ | |
| 120 | + fprintf(op, "%s\n", l2); | |
| 121 | + continue; | |
| 122 | + | |
| 123 | + } else { | |
| 124 | + s = e = f = 0, p0 = p; | |
| 125 | + for (;;) { | |
| 126 | + c = *p++; | |
| 127 | + | |
| 128 | + if (isspc(c)) { | |
| 129 | + s = 1; | |
| 130 | + continue; | |
| 131 | + } | |
| 132 | + if (c == '/' && (p[0] == '/' || p[0] == '*')) | |
| 133 | + c = 0; /* trailing comment detected */ | |
| 134 | + else if (s && q > l2 | |
| 135 | + && ((isid(q[-1]) && isid(c)) | |
| 136 | + // keep space after macro name | |
| 137 | + || (q >= l2 + 2 | |
| 138 | + && l2[0] == '#' | |
| 139 | + && l2[1] == 'd' | |
| 140 | + && f < 2 && !e | |
| 141 | + ))) | |
| 142 | + *q++ = ' ', ++f; | |
| 143 | + s = 0; | |
| 144 | + | |
| 145 | + if (c == '(') | |
| 146 | + ++e; | |
| 147 | + if (c == ')') | |
| 148 | + --e; | |
| 149 | + if (c == '\\' || c == '\"') | |
| 150 | + *q++ = '\\'; | |
| 151 | + *q++ = c; | |
| 152 | + if (c == 0) | |
| 153 | + break; | |
| 154 | + p0 = p; | |
| 155 | + } | |
| 156 | + /* output with quotes */ | |
| 157 | + fprintf(op, " \"%s\\n\"%s\n", l2, p0); | |
| 158 | + } | |
| 159 | + } | |
| 160 | + | |
| 161 | + fclose(fp); | |
| 162 | + fclose(op); | |
| 163 | + return 0; | |
| 164 | +} | |
| 165 | + | |
| 166 | +/* ----------------------------------------------------------------------- */ | |
| 167 | +#elif 1 | |
| 168 | + | |
| 169 | +/* get some information from the host compiler for configure */ | |
| 170 | + | |
| 1 | 171 | #include <stdio.h> |
| 2 | 172 | |
| 3 | 173 | #if defined(_WIN32) |
| @@ -123,3 +293,6 @@ int main(int argc, char *argv[]) · 119 unmodified lines | ||
| 123 | 293 | } |
| 124 | 294 | return 0; |
| 125 | 295 | } |
| 296 | + | |
| 297 | +/* ----------------------------------------------------------------------- */ | |
| 298 | +#endif | |
| @@ -81,6 +81,11 @@ enum { · 80 unmodified lines | ||
| 81 | 81 | #define USING_GLOBALS |
| 82 | 82 | #include "tcc.h" |
| 83 | 83 | |
| 84 | +ST_DATA const char *target_machine_defs = | |
| 85 | + "__i386__\0" | |
| 86 | + "__i386\0" | |
| 87 | + ; | |
| 88 | + | |
| 84 | 89 | /* define to 1/0 to [not] have EBX as 4th register */ |
| 85 | 90 | #define USE_EBX 0 |
| 86 | 91 | |
| @@ -17,25 +17,11 @@ typedef union { long long __ll; long double __ld; } max_align_t; · 16 unmodified lines | ||
| 17 | 17 | typedef signed char int8_t; |
| 18 | 18 | typedef signed short int int16_t; |
| 19 | 19 | typedef signed int int32_t; |
| 20 | −#if defined(__LP64__) \ | |
| 21 | − && !defined(__APPLE__) \ | |
| 22 | − && !defined(__OpenBSD__) \ | |
| 23 | − && !(defined(__NetBSD__) && defined(__aarch64__)) | |
| 24 | −typedef signed long int int64_t; | |
| 25 | −#else | |
| 26 | −typedef signed long long int int64_t; | |
| 27 | −#endif | |
| 20 | +typedef signed __INT64_TYPE__ int64_t; | |
| 28 | 21 | typedef unsigned char uint8_t; |
| 29 | 22 | typedef unsigned short int uint16_t; |
| 30 | 23 | typedef unsigned int uint32_t; |
| 31 | −#if defined(__LP64__) \ | |
| 32 | − && !defined(__APPLE__) \ | |
| 33 | − && !defined(__OpenBSD__) \ | |
| 34 | − && !(defined(__NetBSD__) && defined(__aarch64__)) | |
| 35 | −typedef unsigned long int uint64_t; | |
| 36 | −#else | |
| 37 | −typedef unsigned long long int uint64_t; | |
| 38 | −#endif | |
| 24 | +typedef unsigned __INT64_TYPE__ uint64_t; | |
| 39 | 25 | #endif |
| 40 | 26 | |
| 41 | 27 | #ifndef NULL |
| @@ -1,27 +1,43 @@ | ||
| 1 | 1 | /* tccdefs.h |
| 2 | − * | |
| 3 | − * By using native platf�rm macros this file may be included at runtime | |
| 4 | − * just as is. | |
| 5 | − * | |
| 6 | − * If converted to C-strings and included in tccpp.c, these are trahslated | |
| 7 | − * to tcc target macros accordingly. | |
| 8 | − */ | |
| 2 | + | |
| 3 | + Nothing is defined before this file except target machine, target os | |
| 4 | + and the few things related to option settings in tccpp.c:tcc_predefs(). | |
| 5 | + | |
| 6 | + This file is either included at runtime as is, or converted and | |
| 7 | + included as C-strings at compile-time (depending on CONFIG_TCC_PREDEFS). | |
| 8 | + | |
| 9 | + Note that line indent matters: | |
| 10 | + | |
| 11 | + - in lines starting at column 1, platform macros are replaced by | |
| 12 | + corresponding TCC target compile-time macros. See conftest.c for | |
| 13 | + the list of platform macros supported in lines starting at column 1. | |
| 14 | + | |
| 15 | + - only lines indented >= 4 are actually included into the executable, | |
| 16 | + check tccdefs_.h. | |
| 17 | +*/ | |
| 9 | 18 | |
| 10 | 19 | #if __SIZEOF_POINTER__ == 4 |
| 11 | 20 | /* 32bit systems. */ |
| 12 | 21 | #define __SIZE_TYPE__ unsigned int |
| 13 | 22 | #define __PTRDIFF_TYPE__ int |
| 14 | 23 | #define __ILP32__ 1 |
| 24 | + #define __INT64_TYPE__ long long | |
| 15 | 25 | #elif __SIZEOF_LONG__ == 4 |
| 16 | 26 | /* 64bit Windows. */ |
| 17 | 27 | #define __SIZE_TYPE__ unsigned long long |
| 18 | 28 | #define __PTRDIFF_TYPE__ long long |
| 19 | 29 | #define __LLP64__ 1 |
| 30 | + #define __INT64_TYPE__ long long | |
| 20 | 31 | #else |
| 21 | 32 | /* Other 64bit systems. */ |
| 22 | 33 | #define __SIZE_TYPE__ unsigned long |
| 23 | 34 | #define __PTRDIFF_TYPE__ long |
| 24 | 35 | #define __LP64__ 1 |
| 36 | +# if defined __linux__ | |
| 37 | + #define __INT64_TYPE__ long | |
| 38 | +# else /* APPLE, BSD */ | |
| 39 | + #define __INT64_TYPE__ long long | |
| 40 | +# endif | |
| 25 | 41 | #endif |
| 26 | 42 | #define __SIZEOF_INT__ 4 |
| 27 | 43 | #define __INT_MAX__ 0x7fffffff |
| @@ -58,15 +74,10 @@ · 30 unmodified lines | ||
| 58 | 74 | #endif |
| 59 | 75 | |
| 60 | 76 | #if defined _WIN32 |
| 61 | − //#define _WIN32 1 | |
| 62 | −# if __SIZEOF_POINTER__ == 8 | |
| 63 | − #define _WIN64 1 | |
| 64 | −# endif | |
| 65 | 77 | #define __declspec(x) __attribute__((x)) |
| 66 | 78 | #define __cdecl |
| 67 | 79 | |
| 68 | 80 | #elif defined __FreeBSD__ |
| 69 | − //#define __FreeBSD__ 12 | |
| 70 | 81 | #define __GNUC__ 9 |
| 71 | 82 | #define __GNUC_MINOR__ 3 |
| 72 | 83 | #define __GNUC_PATCHLEVEL__ 0 |
| @@ -78,27 +89,22 @@ · 5 unmodified lines | ||
| 78 | 89 | # endif |
| 79 | 90 | |
| 80 | 91 | #elif defined __FreeBSD_kernel__ |
| 81 | − //#define __FreeBSD_kernel__ 1 | |
| 82 | 92 | |
| 83 | 93 | #elif defined __NetBSD__ |
| 84 | − //#define __NetBSD__ 1 | |
| 85 | 94 | #define __GNUC__ 4 |
| 86 | 95 | #define __GNUC_MINOR__ 1 |
| 87 | 96 | #define __GNUC_PATCHLEVEL__ 0 |
| 88 | 97 | #define _Pragma(x) |
| 89 | 98 | #define __ELF__ 1 |
| 90 | − #if defined(__aarch64__) && defined(__TINYC__) && !defined(_LOCORE) | |
| 91 | − /* avoids usage of __asm which is not yet supported by tcc */ | |
| 92 | − #define _LOCORE | |
| 93 | − #endif | |
| 99 | +#if defined __aarch64__ | |
| 100 | + #define _LOCORE /* avoids usage of __asm */ | |
| 101 | +#endif | |
| 94 | 102 | |
| 95 | 103 | #elif defined __OpenBSD__ |
| 96 | − //#define __OpenBSD__ 1 | |
| 97 | 104 | #define __GNUC__ 4 |
| 98 | 105 | #define _ANSI_LIBRARY 1 |
| 99 | 106 | |
| 100 | 107 | #elif defined __APPLE__ |
| 101 | − //#define __APPLE__ 1 | |
| 102 | 108 | /* emulate APPLE-GCC to make libc's headerfiles compile: */ |
| 103 | 109 | #define __GNUC__ 4 /* darwin emits warning on GCC<4 */ |
| 104 | 110 | #define __APPLE_CC__ 1 /* for <TargetConditionals.h> */ |
| @@ -108,8 +114,8 @@ · 3 unmodified lines | ||
| 108 | 114 | #define _FORTIFY_SOURCE 0 |
| 109 | 115 | |
| 110 | 116 | #else |
| 111 | − //#define __linux__ 1 | |
| 112 | − //#define __linux 1 | |
| 117 | + /* Linux */ | |
| 118 | + | |
| 113 | 119 | #endif |
| 114 | 120 | |
| 115 | 121 | #if !defined _WIN32 |
| @@ -36,6 +36,17 @@ · 35 unmodified lines | ||
| 36 | 36 | #include "tcc.h" |
| 37 | 37 | #include <assert.h> |
| 38 | 38 | |
| 39 | +ST_DATA const char *target_machine_defs = | |
| 40 | + "__riscv\0" | |
| 41 | + "__riscv_xlen 64\0" | |
| 42 | + "__riscv_flen 64\0" | |
| 43 | + "__riscv_div\0" | |
| 44 | + "__riscv_mul\0" | |
| 45 | + "__riscv_fdiv\0" | |
| 46 | + "__riscv_fsqrt\0" | |
| 47 | + "__riscv_float_abi_double\0" | |
| 48 | + ; | |
| 49 | + | |
| 39 | 50 | #define XLEN 8 |
| 40 | 51 | |
| 41 | 52 | #define TREG_RA 17 |
| @@ -211,6 +211,11 @@ extern long double strtold (const char *__nptr, char **__endptr); · 210 unmodified lines | ||
| 211 | 211 | # define ELF_OBJ_ONLY /* create elf .o but native executables */ |
| 212 | 212 | #endif |
| 213 | 213 | |
| 214 | +/* No ten-byte long doubles on window except in cross-compilers made by GCC */ | |
| 215 | +#if defined TCC_TARGET_PE || (defined _WIN32 && !defined __GNUC__) | |
| 216 | +# define TCC_USING_DOUBLE_FOR_LDOUBLE 1 | |
| 217 | +#endif | |
| 218 | + | |
| 214 | 219 | /* ------------ path configuration ------------ */ |
| 215 | 220 | |
| 216 | 221 | #ifndef CONFIG_SYSROOT |
| @@ -1620,7 +1625,7 @@ ST_FUNC void relocate_plt(TCCState *s1); · 1403 unmodified lines | ||
| 1620 | 1625 | ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val); |
| 1621 | 1626 | |
| 1622 | 1627 | /* ------------ xxx-gen.c ------------ */ |
| 1623 | − | |
| 1628 | +ST_DATA const char *target_machine_defs; | |
| 1624 | 1629 | ST_DATA const int reg_classes[NB_REGS]; |
| 1625 | 1630 | |
| 1626 | 1631 | ST_FUNC void gsym_addr(int t, int a); |
| @@ -160,7 +160,11 @@ static const struct { · 159 unmodified lines | ||
| 160 | 160 | { VT_BYTE | VT_DEFSIGN | VT_UNSIGNED, "unsigned char:t13=r13;0;255;" }, |
| 161 | 161 | { VT_FLOAT, "float:t14=r1;4;0;" }, |
| 162 | 162 | { VT_DOUBLE, "double:t15=r1;8;0;" }, |
| 163 | +#ifdef TCC_USING_DOUBLE_FOR_LDOUBLE | |
| 164 | + { VT_DOUBLE | VT_LONG, "long double:t16=r1;8;0;" }, | |
| 165 | +#else | |
| 163 | 166 | { VT_LDOUBLE, "long double:t16=r1;16;0;" }, |
| 167 | +#endif | |
| 164 | 168 | { -1, "_Float32:t17=r1;4;0;" }, |
| 165 | 169 | { -1, "_Float64:t18=r1;8;0;" }, |
| 166 | 170 | { -1, "_Float128:t19=r1;16;0;" }, |
| @@ -5061,7 +5065,7 @@ the_end: · 4894 unmodified lines | ||
| 5061 | 5065 | bt = t & (VT_BTYPE|VT_LONG); |
| 5062 | 5066 | if (bt == VT_LONG) |
| 5063 | 5067 | t |= LONG_SIZE == 8 ? VT_LLONG : VT_INT; |
| 5064 | −#if defined TCC_TARGET_PE || (defined _WIN32 && defined _MSC_VER) | |
| 5068 | +#ifdef TCC_USING_DOUBLE_FOR_LDOUBLE | |
| 5065 | 5069 | if (bt == VT_LDOUBLE) |
| 5066 | 5070 | t = (t & ~(VT_BTYPE|VT_LONG)) | (VT_DOUBLE|VT_LONG); |
| 5067 | 5071 | #endif |
| @@ -1840,8 +1840,6 @@ ST_FUNC void preprocess(int is_bof) · 1839 unmodified lines | ||
| 1840 | 1840 | |
| 1841 | 1841 | if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE) |
| 1842 | 1842 | tcc_error("#include recursion too deep"); |
| 1843 | − /* push current file on stack */ | |
| 1844 | − *s1->include_stack_ptr++ = file; | |
| 1845 | 1843 | i = tok == TOK_INCLUDE_NEXT ? file->include_next_index + 1 : 0; |
| 1846 | 1844 | n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths; |
| 1847 | 1845 | for (; i < n; ++i) { |
| @@ -1884,7 +1882,8 @@ ST_FUNC void preprocess(int is_bof) · 36 unmodified lines | ||
| 1884 | 1882 | |
| 1885 | 1883 | if (tcc_open(s1, buf1) < 0) |
| 1886 | 1884 | continue; |
| 1887 | − | |
| 1885 | + /* push previous file on stack */ | |
| 1886 | + *s1->include_stack_ptr++ = file->prev; | |
| 1888 | 1887 | file->include_next_index = i; |
| 1889 | 1888 | #ifdef INC_DEBUG |
| 1890 | 1889 | printf("%s: including %s\n", file->prev->filename, file->filename); |
| @@ -1907,7 +1906,6 @@ ST_FUNC void preprocess(int is_bof) · 16 unmodified lines | ||
| 1907 | 1906 | } |
| 1908 | 1907 | tcc_error("include file '%s' not found", buf); |
| 1909 | 1908 | include_done: |
| 1910 | − --s1->include_stack_ptr; | |
| 1911 | 1909 | break; |
| 1912 | 1910 | case TOK_IFNDEF: |
| 1913 | 1911 | c = 1; |
| @@ -3625,97 +3623,77 @@ ST_INLN void unget_tok(int last_tok) · 1711 unmodified lines | ||
| 3625 | 3623 | tok = last_tok; |
| 3626 | 3624 | } |
| 3627 | 3625 | |
| 3628 | −static void tcc_predefs(TCCState *s1, CString *cs, int is_asm) | |
| 3629 | −{ | |
| 3630 | − int a,b,c; | |
| 3626 | +/* ------------------------------------------------------------------------- */ | |
| 3627 | +/* init preprocessor */ | |
| 3631 | 3628 | |
| 3632 | − sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c); | |
| 3633 | − cstr_printf(cs, "#define __TINYC__ %d\n", a*10000 + b*100 + c); | |
| 3634 | − cstr_cat(cs, | |
| 3635 | − /* target machine */ | |
| 3636 | −#if defined TCC_TARGET_I386 | |
| 3637 | − "#define __i386__ 1\n" | |
| 3638 | − "#define __i386 1\n" | |
| 3639 | −#elif defined TCC_TARGET_X86_64 | |
| 3640 | − "#define __x86_64__ 1\n" | |
| 3641 | − "#define __amd64__ 1\n" | |
| 3642 | −#elif defined TCC_TARGET_ARM | |
| 3643 | − "#define __ARM_ARCH_4__ 1\n" | |
| 3644 | − "#define __arm_elf__ 1\n" | |
| 3645 | − "#define __arm_elf 1\n" | |
| 3646 | − "#define arm_elf 1\n" | |
| 3647 | − "#define __arm__ 1\n" | |
| 3648 | − "#define __arm 1\n" | |
| 3649 | − "#define arm 1\n" | |
| 3650 | − "#define __APCS_32__ 1\n" | |
| 3651 | − "#define __ARMEL__ 1\n" | |
| 3652 | −# if defined TCC_ARM_EABI | |
| 3653 | − "#define __ARM_EABI__ 1\n" | |
| 3654 | −# endif | |
| 3655 | −#elif defined TCC_TARGET_ARM64 | |
| 3656 | − "#define __aarch64__ 1\n" | |
| 3657 | −#elif defined TCC_TARGET_C67 | |
| 3658 | − "#define __C67__ 1\n" | |
| 3659 | −#elif defined TCC_TARGET_RISCV64 | |
| 3660 | − "#define __riscv 1\n" | |
| 3661 | − "#define __riscv_xlen 64\n" | |
| 3662 | − "#define __riscv_flen 64\n" | |
| 3663 | − "#define __riscv_div 1\n" | |
| 3664 | − "#define __riscv_mul 1\n" | |
| 3665 | − "#define __riscv_fdiv 1\n" | |
| 3666 | − "#define __riscv_fsqrt 1\n" | |
| 3667 | − "#define __riscv_float_abi_double 1\n" | |
| 3668 | −#endif | |
| 3669 | − , -1); | |
| 3670 | −#ifdef TCC_TARGET_ARM | |
| 3671 | − if (s1->float_abi == ARM_HARD_FLOAT) | |
| 3672 | − cstr_printf(cs, "#define __ARM_PCS_VFP 1\n"); | |
| 3673 | −#endif | |
| 3674 | − cstr_cat(cs, | |
| 3675 | − /* target platform */ | |
| 3629 | +static const char *target_os_defs = | |
| 3676 | 3630 | #ifdef TCC_TARGET_PE |
| 3677 | − "#define _WIN32 1\n" | |
| 3631 | + "_WIN32\0" | |
| 3632 | +# if PTR_SIZE == 8 | |
| 3633 | + "_WIN64\0" | |
| 3634 | +# endif | |
| 3678 | 3635 | #else |
| 3679 | − "#define __unix__ 1\n" | |
| 3680 | − "#define __unix 1\n" | |
| 3681 | 3636 | # if defined TCC_TARGET_MACHO |
| 3682 | − "#define __APPLE__ 1\n" | |
| 3637 | + "__APPLE__\0" | |
| 3683 | 3638 | # elif TARGETOS_FreeBSD |
| 3684 | − "#define __FreeBSD__ 12\n" | |
| 3639 | + "__FreeBSD__ 12\0" | |
| 3685 | 3640 | # elif TARGETOS_FreeBSD_kernel |
| 3686 | − "#define __FreeBSD_kernel__ 1\n" | |
| 3641 | + "__FreeBSD_kernel__\0" | |
| 3687 | 3642 | # elif TARGETOS_NetBSD |
| 3688 | − "#define __NetBSD__ 1\n" | |
| 3643 | + "__NetBSD__\0" | |
| 3689 | 3644 | # elif TARGETOS_OpenBSD |
| 3690 | − "#define __OpenBSD__ 1\n" | |
| 3645 | + "__OpenBSD__\0" | |
| 3691 | 3646 | # else |
| 3692 | − "#define __linux__ 1\n" | |
| 3693 | − "#define __linux 1\n" | |
| 3647 | + "__linux__\0" | |
| 3648 | + "__linux\0" | |
| 3694 | 3649 | # endif |
| 3650 | + "__unix__\0" | |
| 3651 | + "__unix\0" | |
| 3652 | +#endif | |
| 3653 | + ; | |
| 3654 | + | |
| 3655 | +static void putdef(CString *cs, const char *p) | |
| 3656 | +{ | |
| 3657 | + cstr_printf(cs, "#define %s%s\n", p, &" 1"[!!strchr(p, ' ')*2]); | |
| 3658 | +} | |
| 3659 | + | |
| 3660 | +static void tcc_predefs(TCCState *s1, CString *cs, int is_asm) | |
| 3661 | +{ | |
| 3662 | + int a, b, c; | |
| 3663 | + const char *defs[] = { target_machine_defs, target_os_defs, NULL }; | |
| 3664 | + const char *p; | |
| 3665 | + | |
| 3666 | + sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c); | |
| 3667 | + cstr_printf(cs, "#define __TINYC__ %d\n", a*10000 + b*100 + c); | |
| 3668 | + for (a = 0; defs[a]; ++a) | |
| 3669 | + for (p = defs[a]; *p; p = strchr(p, 0) + 1) | |
| 3670 | + putdef(cs, p); | |
| 3671 | +#ifdef TCC_TARGET_ARM | |
| 3672 | + if (s1->float_abi == ARM_HARD_FLOAT) | |
| 3673 | + putdef(cs, "__ARM_PCS_VFP"); | |
| 3695 | 3674 | #endif |
| 3696 | − , -1); | |
| 3697 | 3675 | if (is_asm) |
| 3698 | − cstr_printf(cs, "#define __ASSEMBLER__ 1\n"); | |
| 3676 | + putdef(cs, "__ASSEMBLER__"); | |
| 3699 | 3677 | if (s1->output_type == TCC_OUTPUT_PREPROCESS) |
| 3700 | − cstr_printf(cs, "#define __TCC_PP__ 1\n"); | |
| 3678 | + putdef(cs, "__TCC_PP__"); | |
| 3701 | 3679 | if (s1->output_type == TCC_OUTPUT_MEMORY) |
| 3702 | − cstr_printf(cs, "#define __TCC_RUN__ 1\n"); | |
| 3680 | + putdef(cs, "__TCC_RUN__"); | |
| 3703 | 3681 | if (s1->char_is_unsigned) |
| 3704 | − cstr_printf(cs, "#define __CHAR_UNSIGNED__ 1\n"); | |
| 3682 | + putdef(cs, "__CHAR_UNSIGNED__"); | |
| 3705 | 3683 | if (s1->optimize > 0) |
| 3706 | − cstr_printf(cs, "#define __OPTIMIZE__ 1\n"); | |
| 3684 | + putdef(cs, "__OPTIMIZE__"); | |
| 3707 | 3685 | if (s1->option_pthread) |
| 3708 | − cstr_printf(cs, "#define _REENTRANT 1\n"); | |
| 3686 | + putdef(cs, "_REENTRANT"); | |
| 3709 | 3687 | if (s1->leading_underscore) |
| 3710 | − cstr_printf(cs, "#define __leading_underscore 1\n"); | |
| 3688 | + putdef(cs, "__leading_underscore"); | |
| 3711 | 3689 | #ifdef CONFIG_TCC_BCHECK |
| 3712 | 3690 | if (s1->do_bounds_check) |
| 3713 | − cstr_printf(cs, "#define __BOUNDS_CHECKING_ON 1\n"); | |
| 3691 | + putdef(cs, "__BOUNDS_CHECKING_ON"); | |
| 3714 | 3692 | #endif |
| 3715 | 3693 | cstr_printf(cs, "#define __SIZEOF_POINTER__ %d\n", PTR_SIZE); |
| 3716 | 3694 | cstr_printf(cs, "#define __SIZEOF_LONG__ %d\n", LONG_SIZE); |
| 3717 | 3695 | if (!is_asm) { |
| 3718 | − cstr_printf(cs, "#define __STDC__ 1\n"); | |
| 3696 | + putdef(cs, "__STDC__"); | |
| 3719 | 3697 | cstr_printf(cs, "#define __STDC_VERSION__ %dL\n", s1->cversion); |
| 3720 | 3698 | cstr_cat(cs, |
| 3721 | 3699 | /* load more predefs and __builtins */ |
| @@ -269,7 +269,7 @@ int main(int argc, char **argv) · 268 unmodified lines | ||
| 269 | 269 | printf("\n (%u ms)\n", getclock_ms() - t); |
| 270 | 270 | #endif |
| 271 | 271 | #if 1 |
| 272 | − printf("running tcc.c in threads to run fib\n"), fflush(stdout); | |
| 272 | + printf("running tcc.c in threads to run fib\n "), fflush(stdout); | |
| 273 | 273 | t = getclock_ms(); |
| 274 | 274 | for (n = 0; n < M; ++n) |
| 275 | 275 | create_thread(thread_test_complex, n); |
| @@ -1,158 +0,0 @@ | ||
| 1 | −#include <stdio.h> | |
| 2 | −#include <string.h> | |
| 3 | − | |
| 4 | −/* replace native host macros by compile-time versions */ | |
| 5 | −const char *platform_macros[] = { | |
| 6 | − "__i386__", "TCC_TARGET_I386", | |
| 7 | − "__x86_64__", "TCC_TARGET_X86_64", | |
| 8 | − "_WIN32", "TCC_TARGET_PE", | |
| 9 | − "__arm__", "TCC_TARGET_ARM", | |
| 10 | − "__ARM_EABI__", "TCC_ARM_EABI", | |
| 11 | − "__aarch64__", "TCC_TARGET_ARM64", | |
| 12 | − "__riscv", "TCC_TARGET_RISCV64", | |
| 13 | − "__APPLE__", "TCC_TARGET_MACHO", | |
| 14 | − "__FreeBSD__", "TARGETOS_FreeBSD", | |
| 15 | − "__FreeBSD_kernel__", "TARGETOS_FreeBSD_kernel", | |
| 16 | − "__OpenBSD__", "TARGETOS_OpenBSD", | |
| 17 | − "__NetBSD__", "TARGETOS_NetBSD", | |
| 18 | − "__linux__", "TARGETOS_Linux", | |
| 19 | − "__SIZEOF_POINTER__", "PTR_SIZE", | |
| 20 | − "__SIZEOF_LONG__", "LONG_SIZE", | |
| 21 | − 0 | |
| 22 | −}; | |
| 23 | − | |
| 24 | −int isid(int c) | |
| 25 | −{ | |
| 26 | − return (c >= 'a' && c <= 'z') | |
| 27 | − || (c >= 'A' && c <= 'Z') | |
| 28 | − || (c >= '0' && c <= '9') | |
| 29 | − || c == '_'; | |
| 30 | −} | |
| 31 | − | |
| 32 | −int isspc(int c) | |
| 33 | −{ | |
| 34 | − return c == ' ' || c == '\t'; | |
| 35 | −} | |
| 36 | − | |
| 37 | −int main(int argc, char **argv) | |
| 38 | −{ | |
| 39 | − char l[1000], l2[1000], *p, *q, *p0; | |
| 40 | − FILE *fp, *op; | |
| 41 | − int c, e, f, cmt, cmt_n; | |
| 42 | − const char *r; | |
| 43 | − | |
| 44 | − if (argc < 3) | |
| 45 | − return 1; | |
| 46 | − | |
| 47 | − fp = fopen(argv[1], "rb"); | |
| 48 | − op = fopen(argv[2], "wb"); | |
| 49 | − if (!fp || !op) { | |
| 50 | − fprintf(stderr, "c2str: file error\n"); | |
| 51 | − return 1; | |
| 52 | − } | |
| 53 | − | |
| 54 | − cmt = cmt_n = 0; | |
| 55 | − for (;;) { | |
| 56 | − p = l; | |
| 57 | − append: | |
| 58 | − if (fgets(p, sizeof l - (p - l), fp)) { | |
| 59 | − p = strchr(p, 0); | |
| 60 | − while (p > l && p[-1] <= ' ') | |
| 61 | − --p; | |
| 62 | − *p = 0; | |
| 63 | − } else if (p == l) | |
| 64 | − break; | |
| 65 | − | |
| 66 | − /* check for continuation */ | |
| 67 | − if (p > l && p[-1] == '\\') { | |
| 68 | − p[-1] = ' '; | |
| 69 | − goto append; | |
| 70 | − } | |
| 71 | − | |
| 72 | − p = l, q = l2, f = 0; | |
| 73 | − /* count & skip leading spaces */ | |
| 74 | − while (*p && isspc(*p)) | |
| 75 | − ++p, ++f; | |
| 76 | − | |
| 77 | − /* handle comments */ | |
| 78 | − if (p[0] == '/' && cmt == 0) { | |
| 79 | − if (p[1] == '*') | |
| 80 | − cmt = 2; | |
| 81 | − if (p[1] == '/') | |
| 82 | − cmt = 1; | |
| 83 | − } | |
| 84 | − if (cmt) { | |
| 85 | − fprintf(op, "%s", l); | |
| 86 | − if (++cmt_n == 1) | |
| 87 | − fprintf(op, " (converted, do not edit this file)"); | |
| 88 | − fprintf(op, "\n"); | |
| 89 | − if (cmt == 1) | |
| 90 | − cmt = 0; | |
| 91 | − if (cmt == 2) { | |
| 92 | − p = strchr(l, 0); | |
| 93 | − if (p >= l + 2 && p[-1] == '/' && p[-2] == '*') | |
| 94 | − cmt = 0; | |
| 95 | − } | |
| 96 | − continue; | |
| 97 | − } | |
| 98 | − | |
| 99 | − if (f < 4) { | |
| 100 | − /* less than 4 leading spaces : no quotes but replace macros | |
| 101 | − by compile-time versions */ | |
| 102 | − do { | |
| 103 | − for (f = 0; (r = platform_macros[f]); f += 2) { | |
| 104 | − c = strlen(r); | |
| 105 | − e = 0; | |
| 106 | − /*if (memcmp(p, "defined ", 8)) | |
| 107 | − e = 8;*/ | |
| 108 | − if (0 == memcmp(p + e, r, c)) { | |
| 109 | − p += e + c; | |
| 110 | − q = strchr(strcpy(q, platform_macros[f + 1]), 0); | |
| 111 | − break; | |
| 112 | − } | |
| 113 | − | |
| 114 | − } | |
| 115 | − if (r) | |
| 116 | − continue; | |
| 117 | − } while (!!(*q++ = *p++)); | |
| 118 | − fprintf(op, "%s\n", l2); | |
| 119 | − continue; | |
| 120 | − | |
| 121 | − } else { | |
| 122 | − /* output line as C string */ | |
| 123 | − e = f = 0, p0 = p; | |
| 124 | − for (;;) { | |
| 125 | − c = *p++; | |
| 126 | − | |
| 127 | − if (c == '/' && (p[0] == '/' || p[0] == '*')) | |
| 128 | − c = 0; /* trailing comment detected */ | |
| 129 | − | |
| 130 | − if (isspc(c)) { | |
| 131 | − /* remove spaces if possible */ | |
| 132 | − if (q == l2 || isspc(q[-1])) | |
| 133 | − continue; | |
| 134 | − /* keep space after macro identifier */ | |
| 135 | − if ((f >= 2 || e) && (!isid(q[-1]) || !isid(*p))) | |
| 136 | − continue; | |
| 137 | − if (f == 1) | |
| 138 | − f = 2; | |
| 139 | − } | |
| 140 | − if (c == '(') | |
| 141 | − ++e; | |
| 142 | − if (c == ')') | |
| 143 | − --e, f += e == 0; | |
| 144 | − if (c == '\\' || c == '\"') | |
| 145 | − *q++ = '\\'; | |
| 146 | − *q++ = c; | |
| 147 | − if (c == 0) | |
| 148 | − break; | |
| 149 | − p0 = p; | |
| 150 | − } | |
| 151 | − fprintf(op, " \"%s\\n\"%s\n", l2, p0); | |
| 152 | − } | |
| 153 | − } | |
| 154 | − | |
| 155 | − fclose(fp); | |
| 156 | − fclose(op); | |
| 157 | − return 0; | |
| 158 | −} | |
| @@ -80,7 +80,6 @@ · 79 unmodified lines | ||
| 80 | 80 | #define __stdcall __attribute__((__stdcall__)) |
| 81 | 81 | #define _X86_ 1 |
| 82 | 82 | #define _M_IX86 300 /* Visual Studio */ |
| 83 | −#define WIN32 1 | |
| 84 | 83 | #define _USE_32BIT_TIME_T |
| 85 | 84 | #endif |
| 86 | 85 | |
| @@ -160,5 +159,6 @@ typedef struct localeinfo_struct _locale_tstruct,*_locale_t; · 73 unmodified lines | ||
| 160 | 159 | #define __MINGW_EXTENSION |
| 161 | 160 | #define WINAPI_FAMILY_PARTITION(X) 1 |
| 162 | 161 | #define MINGW_HAS_SECURE_API |
| 162 | +#define WIN32 1 | |
| 163 | 163 | |
| 164 | 164 | #endif /* __MINGW_H */ |
| @@ -14,7 +14,7 @@ extern "C" { · 13 unmodified lines | ||
| 14 | 14 | |
| 15 | 15 | #ifndef _CRT_ERRNO_DEFINED |
| 16 | 16 | #define _CRT_ERRNO_DEFINED |
| 17 | − _CRTIMP extern int *__cdecl _errno(void); | |
| 17 | + _CRTIMP int *__cdecl _errno(void); | |
| 18 | 18 | #define errno (*_errno()) |
| 19 | 19 | |
| 20 | 20 | errno_t __cdecl _set_errno(int _Value); |
| @@ -274,7 +274,7 @@ extern "C" { · 273 unmodified lines | ||
| 274 | 274 | : __signbitl (x)) |
| 275 | 275 | */ |
| 276 | 276 | #define signbit(x) \ |
| 277 | − _Generic(x, float: __signbitf, double: __signbit, long double: signbitl)(x) | |
| 277 | + _Generic(x, float: __signbitf, double: __signbit, long double: __signbitl)(x) | |
| 278 | 278 | |
| 279 | 279 | extern double __cdecl exp2(double); |
| 280 | 280 | extern float __cdecl exp2f(float); |
| @@ -136,7 +136,7 @@ extern "C" { · 135 unmodified lines | ||
| 136 | 136 | |
| 137 | 137 | #ifndef _CRT_ERRNO_DEFINED |
| 138 | 138 | #define _CRT_ERRNO_DEFINED |
| 139 | − _CRTIMP extern int *__cdecl _errno(void); | |
| 139 | + _CRTIMP int *__cdecl _errno(void); | |
| 140 | 140 | #define errno (*_errno()) |
| 141 | 141 | errno_t __cdecl _set_errno(int _Value); |
| 142 | 142 | errno_t __cdecl _get_errno(int *_Value); |
| @@ -467,7 +467,7 @@ __CRT_INLINE long double __cdecl hypotl(long double x, long double y) { return h · 466 unmodified lines | ||
| 467 | 467 | __CRT_INLINE long double __cdecl logl(long double x) { return log(x); } |
| 468 | 468 | __CRT_INLINE long double __cdecl logbl(long double x) { return logb(x); } |
| 469 | 469 | __CRT_INLINE long double __cdecl log10l(long double x) { return log10(x); } |
| 470 | −__CRT_INLINE long double __cdecl modfl(long double x, long double* y) { return modf(x, y); } | |
| 470 | +__CRT_INLINE long double __cdecl modfl(long double x, long double* y) { double y1 = *y; x = modf(x, &y1); *y = y1; return x; } | |
| 471 | 471 | __CRT_INLINE long double __cdecl powl(long double x, long double y) { return pow(x, y); } |
| 472 | 472 | __CRT_INLINE long double __cdecl sinhl(long double x) { return sinh(x); } |
| 473 | 473 | __CRT_INLINE long double __cdecl sinl(long double x) { return sin(x); } |
| @@ -112,6 +112,11 @@ enum { · 111 unmodified lines | ||
| 112 | 112 | #include "tcc.h" |
| 113 | 113 | #include <assert.h> |
| 114 | 114 | |
| 115 | +ST_DATA const char *target_machine_defs = | |
| 116 | + "__x86_64__\0" | |
| 117 | + "__amd64__\0" | |
| 118 | + ; | |
| 119 | + | |
| 115 | 120 | ST_DATA const int reg_classes[NB_REGS] = { |
| 116 | 121 | /* eax */ RC_INT | RC_RAX, |
| 117 | 122 | /* ecx */ RC_INT | RC_RCX, |