diff options
Diffstat (limited to 'src/a_targ.c')
| -rw-r--r-- | src/a_targ.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/a_targ.c b/src/a_targ.c index 1513bc6..f6e060c 100644 --- a/src/a_targ.c +++ b/src/a_targ.c @@ -67,6 +67,87 @@ parsetriple(TargTriple *trg, const char *str) return 1; } +static const char *const ospredefs[] = { + [OSlinux] = "__linux\0__linux__\0linux\0unix\0__unix\0__unix__\0__ELF__\0", + [OSopenbsd] = "__OpenBSD__\0unix\0__unix\0__unix__\0__ELF__\0" +}, *archpredefs[] = { + [ISx86_64] = "__x86_64__\0__x86_64\0__amd64__\0__amd64\0", + [ISaarch64] = "__aarch64__\0__aarch64\0", +}; + +/* https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html */ +static void +putpredefmacros(void) +{ + for (const char *s = ospredefs[target.os]; s && *s; s = s + strlen(s)+1) + cpp0define(s, NULL); + for (const char *s = archpredefs[target.arch]; s && *s; s = s + strlen(s)+1) + cpp0define(s, NULL); + if (ccopt.pie) + cpp0define("__pie__", NULL), cpp0define("__PIE__", NULL); + if (ccopt.pic) + cpp0define("__pic__", NULL), cpp0define("__PIC__", NULL); + + if (targ_primsizes[TYPTR] == 4) + cpp0define("_ILP32", NULL), cpp0define("__ILP32__", NULL); + else if (targ_primsizes[TYLONG] == 4) + cpp0define("_LLP64", NULL), cpp0define("__LLP64__", NULL); + else { + assert(targ_primsizes[TYINT] == 4); + cpp0define("_LP64", NULL), cpp0define("__LP64__", NULL); + } + + if (!targ_charsigned) cpp0define("__CHAR_UNSIGNED__", NULL); + /* __SIZE_TYPE__ & co. */ + int i64t = targ_primsizes[TYLONG] == 8 ? TYLONG : TYVLONG; +#define DEFTYP(T,tt) cpp0define("__"T"_TYPE__", primtypenames[tt]); + DEFTYP("SIZE", targ_sizetype); + DEFTYP("PTRDIFF", targ_ptrdifftype); + DEFTYP("WCHAR", targ_wchartype); + DEFTYP("WINT", targ_wchartype - isunsignedt(targ_wchartype)); + DEFTYP("INTMAX", i64t); + DEFTYP("UINTMAX", i64t+1); + DEFTYP("SIG_ATOMIC", TYINT); + DEFTYP("INT8", TYSCHAR); + DEFTYP("INT16", TYSHORT); + DEFTYP("INT32", TYINT); + DEFTYP("INT64", i64t); + DEFTYP("UINT8", TYUCHAR); + DEFTYP("UINT16", TYUSHORT); + DEFTYP("UINT32", TYUINT); + DEFTYP("UINT64", i64t+1); + DEFTYP("INT_LEAST8", TYSCHAR); + DEFTYP("INT_LEAST16", TYSHORT); + DEFTYP("INT_LEAST32", TYINT); + DEFTYP("INT_LEAST64", i64t); + DEFTYP("UINT_LEAST8", TYUCHAR); + DEFTYP("UINT_LEAST16", TYUSHORT); + DEFTYP("UINT_LEAST32", TYUINT); + DEFTYP("UINT_LEAST64", i64t+1); + DEFTYP("INTPTR", targ_64bit ? i64t : TYINT); + DEFTYP("UINTPTR", targ_64bit ? i64t : TYINT); +#undef DEFTYP + static const char atoi[][3] = {[1]="1",[2]="2",[4]="4",[8]="8",[16]="16"}; +#define TYPSIZ(T,n) cpp0define("__SIZEOF_"T"__", atoi[targ_primsizes[n]]) + TYPSIZ("INT", TYINT); + TYPSIZ("LONG", TYLONG); + TYPSIZ("LONG_LONG", TYVLONG); + TYPSIZ("SHORT", TYSHORT); + TYPSIZ("POINTER", TYPTR); + TYPSIZ("FLOAT", TYFLOAT); + TYPSIZ("DOUBLE", TYDOUBLE); + TYPSIZ("LONG_DOUBLE", TYLDOUBLE); + TYPSIZ("SIZE_T", targ_sizetype); + TYPSIZ("WCHAR_T", targ_wchartype); + TYPSIZ("WINT_T", targ_wchartype); + TYPSIZ("PTRDIFF_T", targ_ptrdifftype); + + cpp0define("__ORDER_LITTLE_ENDIAN__", "1234"); + cpp0define("__ORDER_BIG_ENDIAN__", "4321"); + cpp0define("__BYTE_ORDER__", + targ_bigendian ? "__ORDER_BIG_ENDIAN__" : "__ORDER_LITTLE_ENDIAN__"); +} + bool targ_init(const char *starg, const TargTriple *dfault) { @@ -120,5 +201,7 @@ targ_init(const char *starg, const TargTriple *dfault) mctarg = t->mctarg; targ_arch = ISx86_64; + putpredefmacros(); + return 1; } |