target.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "symbol.h"
  4. #include "target.h"
  5. #include "machine.h"
  6. struct symbol *ptrdiff_ctype;
  7. struct symbol *intptr_ctype;
  8. struct symbol *uintptr_ctype;
  9. struct symbol *size_t_ctype = &ulong_ctype;
  10. struct symbol *ssize_t_ctype = &long_ctype;
  11. struct symbol *intmax_ctype = &long_ctype;
  12. struct symbol *uintmax_ctype = &ulong_ctype;
  13. struct symbol *int64_ctype = &long_ctype;
  14. struct symbol *uint64_ctype = &ulong_ctype;
  15. struct symbol *int32_ctype = &int_ctype;
  16. struct symbol *uint32_ctype = &uint_ctype;
  17. struct symbol *wchar_ctype = &int_ctype;
  18. struct symbol *wint_ctype = &uint_ctype;
  19. struct symbol *least8_ctype = &schar_ctype;
  20. struct symbol *uleast8_ctype = &uchar_ctype;
  21. struct symbol *least16_ctype = &short_ctype;
  22. struct symbol *uleast16_ctype = &ushort_ctype;
  23. struct symbol *least32_ctype = &int_ctype;
  24. struct symbol *uleast32_ctype = &uint_ctype;
  25. struct symbol *least64_ctype = &llong_ctype;
  26. struct symbol *uleast64_ctype = &ullong_ctype;
  27. struct symbol *fast8_ctype = &schar_ctype;
  28. struct symbol *ufast8_ctype = &uchar_ctype;
  29. struct symbol *fast16_ctype = &long_ctype;
  30. struct symbol *ufast16_ctype = &ulong_ctype;
  31. struct symbol *fast32_ctype = &long_ctype;
  32. struct symbol *ufast32_ctype = &ulong_ctype;
  33. struct symbol *fast64_ctype = &long_ctype;
  34. struct symbol *ufast64_ctype = &ulong_ctype;
  35. struct symbol *sig_atomic_ctype = &int_ctype;
  36. /*
  37. * For "__attribute__((aligned))"
  38. */
  39. int max_alignment = 16;
  40. /*
  41. * Integer data types
  42. */
  43. int bits_in_bool = 1;
  44. int bits_in_char = 8;
  45. int bits_in_short = 16;
  46. int bits_in_int = 32;
  47. int bits_in_long = 64;
  48. int bits_in_longlong = 64;
  49. int bits_in_longlonglong = 128;
  50. int max_int_alignment = 8;
  51. /*
  52. * Floating point data types
  53. */
  54. int bits_in_float = 32;
  55. int bits_in_double = 64;
  56. int bits_in_longdouble = 128;
  57. int max_fp_alignment = 16;
  58. /*
  59. * Pointer data type
  60. */
  61. int bits_in_pointer = 64;
  62. int pointer_alignment = 8;
  63. /*
  64. * Enum data types
  65. */
  66. int bits_in_enum = 32;
  67. int enum_alignment = 4;
  68. static const struct target *targets[] = {
  69. [MACH_ALPHA] = &target_alpha,
  70. [MACH_ARM] = &target_arm,
  71. [MACH_ARM64] = &target_arm64,
  72. [MACH_BFIN] = &target_bfin,
  73. [MACH_H8300] = &target_h8300,
  74. [MACH_I386] = &target_i386,
  75. [MACH_M68K] = &target_m68k,
  76. [MACH_MICROBLAZE] = &target_microblaze,
  77. [MACH_MIPS32] = &target_mips32,
  78. [MACH_MIPS64] = &target_mips64,
  79. [MACH_NDS32] = &target_nds32,
  80. [MACH_NIOS2] = &target_nios2,
  81. [MACH_OPENRISC] = &target_openrisc,
  82. [MACH_PPC32] = &target_ppc32,
  83. [MACH_PPC64] = &target_ppc64,
  84. [MACH_RISCV32] = &target_riscv32,
  85. [MACH_RISCV64] = &target_riscv64,
  86. [MACH_S390] = &target_s390,
  87. [MACH_S390X] = &target_s390x,
  88. [MACH_SH] = &target_sh,
  89. [MACH_SPARC32] = &target_sparc32,
  90. [MACH_SPARC64] = &target_sparc64,
  91. [MACH_X86_64] = &target_x86_64,
  92. [MACH_XTENSA] = &target_xtensa,
  93. [MACH_UNKNOWN] = &target_default,
  94. };
  95. const struct target *arch_target = &target_default;
  96. enum machine target_parse(const char *name)
  97. {
  98. static const struct arch {
  99. const char *name;
  100. enum machine mach;
  101. char bits;
  102. } archs[] = {
  103. { "alpha", MACH_ALPHA, 64, },
  104. { "aarch64", MACH_ARM64, 64, },
  105. { "arm64", MACH_ARM64, 64, },
  106. { "arm", MACH_ARM, 32, },
  107. { "bfin", MACH_BFIN, 32, },
  108. { "h8300", MACH_H8300, 32, },
  109. { "i386", MACH_I386, 32, },
  110. { "m68k", MACH_M68K, 32, },
  111. { "microblaze", MACH_MICROBLAZE,32, },
  112. { "mips", MACH_MIPS32, 0, },
  113. { "nds32", MACH_NDS32, 32, },
  114. { "nios2", MACH_NIOS2, 32, },
  115. { "openrisc", MACH_OPENRISC, 32, },
  116. { "powerpc", MACH_PPC32, 0, },
  117. { "ppc", MACH_PPC32, 0, },
  118. { "riscv", MACH_RISCV32, 0, },
  119. { "s390x", MACH_S390X, 64, },
  120. { "s390", MACH_S390, 32, },
  121. { "sparc", MACH_SPARC32, 0, },
  122. { "x86_64", MACH_X86_64, 64, },
  123. { "x86-64", MACH_X86_64, 64, },
  124. { "sh", MACH_SH, 32, },
  125. { "xtensa", MACH_XTENSA, 32, },
  126. { NULL },
  127. };
  128. const struct arch *p;
  129. for (p = &archs[0]; p->name; p++) {
  130. size_t len = strlen(p->name);
  131. if (strncmp(p->name, name, len) == 0) {
  132. enum machine mach = p->mach;
  133. const char *suf = name + len;
  134. int bits = p->bits;
  135. if (bits == 0) {
  136. if (!strcmp(suf, "") || !strcmp(suf, "32")) {
  137. ;
  138. } else if (!strcmp(suf, "64")) {
  139. mach += 1;
  140. } else {
  141. die("invalid architecture: %s", name);
  142. }
  143. } else {
  144. if (strcmp(suf, ""))
  145. die("invalid architecture: %s", name);
  146. }
  147. return mach;
  148. }
  149. }
  150. return MACH_UNKNOWN;
  151. }
  152. void target_os(const char *name)
  153. {
  154. static const struct os {
  155. const char *name;
  156. int os;
  157. } oses[] = {
  158. { "cygwin", OS_CYGWIN },
  159. { "darwin", OS_DARWIN },
  160. { "freebsd", OS_FREEBSD },
  161. { "linux", OS_LINUX },
  162. { "native", OS_NATIVE, },
  163. { "netbsd", OS_NETBSD },
  164. { "none", OS_NONE },
  165. { "openbsd", OS_OPENBSD },
  166. { "sunos", OS_SUNOS },
  167. { "unix", OS_UNIX },
  168. { NULL },
  169. }, *p;
  170. for (p = &oses[0]; p->name; p++) {
  171. if (!strcmp(p->name, name)) {
  172. arch_os = p->os;
  173. return;
  174. }
  175. }
  176. die("invalid os: %s", name);
  177. }
  178. void target_config(enum machine mach)
  179. {
  180. const struct target *target = targets[mach];
  181. arch_target = target;
  182. arch_m64 = target->bitness;
  183. arch_big_endian = target->big_endian;
  184. funsigned_char = target->unsigned_char;
  185. }
  186. void target_init(void)
  187. {
  188. const struct target *target = arch_target;
  189. switch (arch_m64) {
  190. case ARCH_X32:
  191. if (target->target_x32bit)
  192. target = target->target_x32bit;
  193. goto case_32bit;
  194. case ARCH_LP32:
  195. max_int_alignment = 4;
  196. if (target->target_32bit)
  197. target = target->target_32bit;
  198. /* fallthrough */
  199. case_32bit:
  200. bits_in_long = 32;
  201. bits_in_pointer = 32;
  202. pointer_alignment = 4;
  203. size_t_ctype = &uint_ctype;
  204. ssize_t_ctype = &int_ctype;
  205. int64_ctype = &llong_ctype;
  206. uint64_ctype = &ullong_ctype;
  207. intmax_ctype = &llong_ctype;
  208. uintmax_ctype = &ullong_ctype;
  209. fast64_ctype = &llong_ctype;
  210. ufast64_ctype = &ullong_ctype;
  211. break;
  212. case ARCH_LLP64:
  213. bits_in_long = 32;
  214. size_t_ctype = &ullong_ctype;
  215. ssize_t_ctype = &llong_ctype;
  216. int64_ctype = &llong_ctype;
  217. uint64_ctype = &ullong_ctype;
  218. intmax_ctype = &llong_ctype;
  219. uintmax_ctype = &ullong_ctype;
  220. /* fallthrough */
  221. case ARCH_LP64:
  222. if (target->target_64bit)
  223. target = target->target_64bit;
  224. break;
  225. }
  226. arch_target = target;
  227. if (fpie > fpic)
  228. fpic = fpie;
  229. if (target->wchar)
  230. wchar_ctype = target->wchar;
  231. if (target->wint)
  232. wint_ctype = target->wint;
  233. if (target->bits_in_longdouble)
  234. bits_in_longdouble = target->bits_in_longdouble;
  235. if (target->max_fp_alignment)
  236. max_fp_alignment = target->max_fp_alignment;
  237. if (target->init)
  238. target->init(target);
  239. if (arch_msize_long || target->size_t_long) {
  240. size_t_ctype = &ulong_ctype;
  241. ssize_t_ctype = &long_ctype;
  242. }
  243. if (fshort_wchar)
  244. wchar_ctype = &ushort_ctype;
  245. }