buildvm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. ** LuaJIT VM builder.
  3. ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** This is a tool to build the hand-tuned assembler code required for
  6. ** LuaJIT's bytecode interpreter. It supports a variety of output formats
  7. ** to feed different toolchains (see usage() below).
  8. **
  9. ** This tool is not particularly optimized because it's only used while
  10. ** _building_ LuaJIT. There's no point in distributing or installing it.
  11. ** Only the object code generated by this tool is linked into LuaJIT.
  12. **
  13. ** Caveat: some memory is not free'd, error handling is lazy.
  14. ** It's a one-shot tool -- any effort fixing this would be wasted.
  15. */
  16. #include "buildvm.h"
  17. #include "lj_obj.h"
  18. #include "lj_gc.h"
  19. #include "lj_bc.h"
  20. #if LJ_HASJIT
  21. #include "lj_ir.h"
  22. #include "lj_ircall.h"
  23. #endif
  24. #include "lj_frame.h"
  25. #include "lj_dispatch.h"
  26. #if LJ_HASFFI
  27. #include "lj_ctype.h"
  28. #include "lj_ccall.h"
  29. #endif
  30. #include "luajit.h"
  31. #if defined(_WIN32)
  32. #include <fcntl.h>
  33. #include <io.h>
  34. #endif
  35. /* ------------------------------------------------------------------------ */
  36. /* DynASM glue definitions. */
  37. #define Dst ctx
  38. #define Dst_DECL BuildCtx *ctx
  39. #define Dst_REF (ctx->D)
  40. #define DASM_CHECKS 1
  41. #include "../dynasm/dasm_proto.h"
  42. /* Glue macros for DynASM. */
  43. static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type);
  44. #define DASM_EXTERN(ctx, addr, idx, type) \
  45. collect_reloc(ctx, addr, idx, type)
  46. /* ------------------------------------------------------------------------ */
  47. /* Avoid trouble if cross-compiling for an x86 target. Speed doesn't matter. */
  48. #define DASM_ALIGNED_WRITES 1
  49. /* Embed architecture-specific DynASM encoder. */
  50. #if LJ_TARGET_X86ORX64
  51. #include "../dynasm/dasm_x86.h"
  52. #elif LJ_TARGET_ARM
  53. #include "../dynasm/dasm_arm.h"
  54. #elif LJ_TARGET_ARM64
  55. #include "../dynasm/dasm_arm64.h"
  56. #elif LJ_TARGET_PPC
  57. #include "../dynasm/dasm_ppc.h"
  58. #elif LJ_TARGET_MIPS
  59. #include "../dynasm/dasm_mips.h"
  60. #else
  61. #error "No support for this architecture (yet)"
  62. #endif
  63. /* Embed generated architecture-specific backend. */
  64. #include "buildvm_arch.h"
  65. /* ------------------------------------------------------------------------ */
  66. void owrite(BuildCtx *ctx, const void *ptr, size_t sz)
  67. {
  68. if (fwrite(ptr, 1, sz, ctx->fp) != sz) {
  69. fprintf(stderr, "Error: cannot write to output file: %s\n",
  70. strerror(errno));
  71. exit(1);
  72. }
  73. }
  74. /* ------------------------------------------------------------------------ */
  75. /* Emit code as raw bytes. Only used for DynASM debugging. */
  76. static void emit_raw(BuildCtx *ctx)
  77. {
  78. owrite(ctx, ctx->code, ctx->codesz);
  79. }
  80. /* -- Build machine code -------------------------------------------------- */
  81. static const char *sym_decorate(BuildCtx *ctx,
  82. const char *prefix, const char *suffix)
  83. {
  84. char name[256];
  85. char *p;
  86. #if LJ_64
  87. const char *symprefix = ctx->mode == BUILD_machasm ? "_" : "";
  88. #elif LJ_TARGET_XBOX360
  89. const char *symprefix = "";
  90. #else
  91. const char *symprefix = ctx->mode != BUILD_elfasm ? "_" : "";
  92. #endif
  93. sprintf(name, "%s%s%s", symprefix, prefix, suffix);
  94. p = strchr(name, '@');
  95. if (p) {
  96. #if LJ_TARGET_X86ORX64
  97. if (!LJ_64 && (ctx->mode == BUILD_coffasm || ctx->mode == BUILD_peobj))
  98. name[0] = name[1] == 'R' ? '_' : '@'; /* Just for _RtlUnwind@16. */
  99. else
  100. *p = '\0';
  101. #elif LJ_TARGET_PPC && !LJ_TARGET_CONSOLE
  102. /* Keep @plt etc. */
  103. #else
  104. *p = '\0';
  105. #endif
  106. }
  107. p = (char *)malloc(strlen(name)+1); /* MSVC doesn't like strdup. */
  108. strcpy(p, name);
  109. return p;
  110. }
  111. #define NRELOCSYM (sizeof(extnames)/sizeof(extnames[0])-1)
  112. static int relocmap[NRELOCSYM];
  113. /* Collect external relocations. */
  114. static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type)
  115. {
  116. if (ctx->nreloc >= BUILD_MAX_RELOC) {
  117. fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n");
  118. exit(1);
  119. }
  120. if (relocmap[idx] < 0) {
  121. relocmap[idx] = ctx->nrelocsym;
  122. ctx->relocsym[ctx->nrelocsym] = sym_decorate(ctx, "", extnames[idx]);
  123. ctx->nrelocsym++;
  124. }
  125. ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code);
  126. ctx->reloc[ctx->nreloc].sym = relocmap[idx];
  127. ctx->reloc[ctx->nreloc].type = type;
  128. ctx->nreloc++;
  129. #if LJ_TARGET_XBOX360
  130. return (int)(ctx->code - addr) + 4; /* Encode symbol offset of .text. */
  131. #else
  132. return 0; /* Encode symbol offset of 0. */
  133. #endif
  134. }
  135. /* Naive insertion sort. Performance doesn't matter here. */
  136. static void sym_insert(BuildCtx *ctx, int32_t ofs,
  137. const char *prefix, const char *suffix)
  138. {
  139. ptrdiff_t i = ctx->nsym++;
  140. while (i > 0) {
  141. if (ctx->sym[i-1].ofs <= ofs)
  142. break;
  143. ctx->sym[i] = ctx->sym[i-1];
  144. i--;
  145. }
  146. ctx->sym[i].ofs = ofs;
  147. ctx->sym[i].name = sym_decorate(ctx, prefix, suffix);
  148. }
  149. /* Build the machine code. */
  150. static int build_code(BuildCtx *ctx)
  151. {
  152. int status;
  153. int i;
  154. /* Initialize DynASM structures. */
  155. ctx->nglob = GLOB__MAX;
  156. ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *));
  157. memset(ctx->glob, 0, ctx->nglob*sizeof(void *));
  158. ctx->nreloc = 0;
  159. ctx->globnames = globnames;
  160. ctx->extnames = extnames;
  161. ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *));
  162. ctx->nrelocsym = 0;
  163. for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1;
  164. ctx->dasm_ident = DASM_IDENT;
  165. ctx->dasm_arch = DASM_ARCH;
  166. dasm_init(Dst, DASM_MAXSECTION);
  167. dasm_setupglobal(Dst, ctx->glob, ctx->nglob);
  168. dasm_setup(Dst, build_actionlist);
  169. /* Call arch-specific backend to emit the code. */
  170. ctx->npc = build_backend(ctx);
  171. /* Finalize the code. */
  172. (void)dasm_checkstep(Dst, -1);
  173. if ((status = dasm_link(Dst, &ctx->codesz))) return status;
  174. ctx->code = (uint8_t *)malloc(ctx->codesz);
  175. if ((status = dasm_encode(Dst, (void *)ctx->code))) return status;
  176. /* Allocate symbol table and bytecode offsets. */
  177. ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin");
  178. ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym));
  179. ctx->nsym = 0;
  180. ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t));
  181. /* Collect the opcodes (PC labels). */
  182. for (i = 0; i < ctx->npc; i++) {
  183. int32_t ofs = dasm_getpclabel(Dst, i);
  184. if (ofs < 0) return 0x22000000|i;
  185. ctx->bc_ofs[i] = ofs;
  186. if ((LJ_HASJIT ||
  187. !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP ||
  188. i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) &&
  189. (LJ_HASFFI || i != BC_KCDATA))
  190. sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]);
  191. }
  192. /* Collect the globals (named labels). */
  193. for (i = 0; i < ctx->nglob; i++) {
  194. const char *gl = globnames[i];
  195. int len = (int)strlen(gl);
  196. if (!ctx->glob[i]) {
  197. fprintf(stderr, "Error: undefined global %s\n", gl);
  198. exit(2);
  199. }
  200. /* Skip the _Z symbols. */
  201. if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z'))
  202. sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code),
  203. LABEL_PREFIX, globnames[i]);
  204. }
  205. /* Close the address range. */
  206. sym_insert(ctx, (int32_t)ctx->codesz, "", "");
  207. ctx->nsym--;
  208. dasm_free(Dst);
  209. return 0;
  210. }
  211. /* -- Generate VM enums --------------------------------------------------- */
  212. const char *const bc_names[] = {
  213. #define BCNAME(name, ma, mb, mc, mt) #name,
  214. BCDEF(BCNAME)
  215. #undef BCNAME
  216. NULL
  217. };
  218. #if LJ_HASJIT
  219. const char *const ir_names[] = {
  220. #define IRNAME(name, m, m1, m2) #name,
  221. IRDEF(IRNAME)
  222. #undef IRNAME
  223. NULL
  224. };
  225. const char *const irt_names[] = {
  226. #define IRTNAME(name, size) #name,
  227. IRTDEF(IRTNAME)
  228. #undef IRTNAME
  229. NULL
  230. };
  231. const char *const irfpm_names[] = {
  232. #define FPMNAME(name) #name,
  233. IRFPMDEF(FPMNAME)
  234. #undef FPMNAME
  235. NULL
  236. };
  237. const char *const irfield_names[] = {
  238. #define FLNAME(name, ofs) #name,
  239. IRFLDEF(FLNAME)
  240. #undef FLNAME
  241. NULL
  242. };
  243. const char *const ircall_names[] = {
  244. #define IRCALLNAME(cond, name, nargs, kind, type, flags) #name,
  245. IRCALLDEF(IRCALLNAME)
  246. #undef IRCALLNAME
  247. NULL
  248. };
  249. static const char *const trace_errors[] = {
  250. #define TREDEF(name, msg) msg,
  251. #include "lj_traceerr.h"
  252. NULL
  253. };
  254. #endif
  255. #if LJ_HASJIT
  256. static const char *lower(char *buf, const char *s)
  257. {
  258. char *p = buf;
  259. while (*s) {
  260. *p++ = (*s >= 'A' && *s <= 'Z') ? *s+0x20 : *s;
  261. s++;
  262. }
  263. *p = '\0';
  264. return buf;
  265. }
  266. #endif
  267. /* Emit C source code for bytecode-related definitions. */
  268. static void emit_bcdef(BuildCtx *ctx)
  269. {
  270. int i;
  271. fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n");
  272. fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_ofs[] = {\n");
  273. for (i = 0; i < ctx->npc; i++) {
  274. if (i != 0)
  275. fprintf(ctx->fp, ",\n");
  276. fprintf(ctx->fp, "%d", ctx->bc_ofs[i]);
  277. }
  278. }
  279. /* Emit VM definitions as Lua code for debug modules. */
  280. static void emit_vmdef(BuildCtx *ctx)
  281. {
  282. #if LJ_HASJIT
  283. char buf[80];
  284. #endif
  285. int i;
  286. fprintf(ctx->fp, "-- This is a generated file. DO NOT EDIT!\n\n");
  287. fprintf(ctx->fp, "assert(require(\"jit\").version == \"%s\", \"LuaJIT core/library version mismatch\")\n\n", LUAJIT_VERSION);
  288. fprintf(ctx->fp, "return {\n\n");
  289. fprintf(ctx->fp, "bcnames = \"");
  290. for (i = 0; bc_names[i]; i++) fprintf(ctx->fp, "%-6s", bc_names[i]);
  291. fprintf(ctx->fp, "\",\n\n");
  292. #if LJ_HASJIT
  293. fprintf(ctx->fp, "irnames = \"");
  294. for (i = 0; ir_names[i]; i++) fprintf(ctx->fp, "%-6s", ir_names[i]);
  295. fprintf(ctx->fp, "\",\n\n");
  296. fprintf(ctx->fp, "irfpm = { [0]=");
  297. for (i = 0; irfpm_names[i]; i++)
  298. fprintf(ctx->fp, "\"%s\", ", lower(buf, irfpm_names[i]));
  299. fprintf(ctx->fp, "},\n\n");
  300. fprintf(ctx->fp, "irfield = { [0]=");
  301. for (i = 0; irfield_names[i]; i++) {
  302. char *p;
  303. lower(buf, irfield_names[i]);
  304. p = strchr(buf, '_');
  305. if (p) *p = '.';
  306. fprintf(ctx->fp, "\"%s\", ", buf);
  307. }
  308. fprintf(ctx->fp, "},\n\n");
  309. fprintf(ctx->fp, "ircall = {\n[0]=");
  310. for (i = 0; ircall_names[i]; i++)
  311. fprintf(ctx->fp, "\"%s\",\n", ircall_names[i]);
  312. fprintf(ctx->fp, "},\n\n");
  313. fprintf(ctx->fp, "traceerr = {\n[0]=");
  314. for (i = 0; trace_errors[i]; i++)
  315. fprintf(ctx->fp, "\"%s\",\n", trace_errors[i]);
  316. fprintf(ctx->fp, "},\n\n");
  317. #endif
  318. }
  319. /* -- Argument parsing ---------------------------------------------------- */
  320. /* Build mode names. */
  321. static const char *const modenames[] = {
  322. #define BUILDNAME(name) #name,
  323. BUILDDEF(BUILDNAME)
  324. #undef BUILDNAME
  325. NULL
  326. };
  327. /* Print usage information and exit. */
  328. static void usage(void)
  329. {
  330. int i;
  331. fprintf(stderr, LUAJIT_VERSION " VM builder.\n");
  332. fprintf(stderr, LUAJIT_COPYRIGHT ", " LUAJIT_URL "\n");
  333. fprintf(stderr, "Target architecture: " LJ_ARCH_NAME "\n\n");
  334. fprintf(stderr, "Usage: buildvm -m mode [-o outfile] [infiles...]\n\n");
  335. fprintf(stderr, "Available modes:\n");
  336. for (i = 0; i < BUILD__MAX; i++)
  337. fprintf(stderr, " %s\n", modenames[i]);
  338. exit(1);
  339. }
  340. /* Parse the output mode name. */
  341. static BuildMode parsemode(const char *mode)
  342. {
  343. int i;
  344. for (i = 0; modenames[i]; i++)
  345. if (!strcmp(mode, modenames[i]))
  346. return (BuildMode)i;
  347. usage();
  348. return (BuildMode)-1;
  349. }
  350. /* Parse arguments. */
  351. static void parseargs(BuildCtx *ctx, char **argv)
  352. {
  353. const char *a;
  354. int i;
  355. ctx->mode = (BuildMode)-1;
  356. ctx->outname = "-";
  357. for (i = 1; (a = argv[i]) != NULL; i++) {
  358. if (a[0] != '-')
  359. break;
  360. switch (a[1]) {
  361. case '-':
  362. if (a[2]) goto err;
  363. i++;
  364. goto ok;
  365. case '\0':
  366. goto ok;
  367. case 'm':
  368. i++;
  369. if (a[2] || argv[i] == NULL) goto err;
  370. ctx->mode = parsemode(argv[i]);
  371. break;
  372. case 'o':
  373. i++;
  374. if (a[2] || argv[i] == NULL) goto err;
  375. ctx->outname = argv[i];
  376. break;
  377. default: err:
  378. usage();
  379. break;
  380. }
  381. }
  382. ok:
  383. ctx->args = argv+i;
  384. if (ctx->mode == (BuildMode)-1) goto err;
  385. }
  386. int main(int argc, char **argv)
  387. {
  388. BuildCtx ctx_;
  389. BuildCtx *ctx = &ctx_;
  390. int status, binmode;
  391. if (sizeof(void *) != 4*LJ_32+8*LJ_64) {
  392. fprintf(stderr,"Error: pointer size mismatch in cross-build.\n");
  393. fprintf(stderr,"Try: make HOST_CC=\"gcc -m32\" CROSS=...\n\n");
  394. return 1;
  395. }
  396. UNUSED(argc);
  397. parseargs(ctx, argv);
  398. if ((status = build_code(ctx))) {
  399. fprintf(stderr,"Error: DASM error %08x\n", status);
  400. return 1;
  401. }
  402. switch (ctx->mode) {
  403. case BUILD_peobj:
  404. case BUILD_raw:
  405. binmode = 1;
  406. break;
  407. default:
  408. binmode = 0;
  409. break;
  410. }
  411. if (ctx->outname[0] == '-' && ctx->outname[1] == '\0') {
  412. ctx->fp = stdout;
  413. #if defined(_WIN32)
  414. if (binmode)
  415. _setmode(_fileno(stdout), _O_BINARY); /* Yuck. */
  416. #endif
  417. } else if (!(ctx->fp = fopen(ctx->outname, binmode ? "wb" : "w"))) {
  418. fprintf(stderr, "Error: cannot open output file '%s': %s\n",
  419. ctx->outname, strerror(errno));
  420. exit(1);
  421. }
  422. switch (ctx->mode) {
  423. case BUILD_elfasm:
  424. case BUILD_coffasm:
  425. case BUILD_machasm:
  426. emit_asm(ctx);
  427. emit_asm_debug(ctx);
  428. break;
  429. case BUILD_peobj:
  430. emit_peobj(ctx);
  431. break;
  432. case BUILD_raw:
  433. emit_raw(ctx);
  434. break;
  435. case BUILD_bcdef:
  436. emit_bcdef(ctx);
  437. emit_lib(ctx);
  438. break;
  439. case BUILD_vmdef:
  440. emit_vmdef(ctx);
  441. emit_lib(ctx);
  442. fprintf(ctx->fp, "}\n\n");
  443. break;
  444. case BUILD_ffdef:
  445. case BUILD_libdef:
  446. case BUILD_recdef:
  447. emit_lib(ctx);
  448. break;
  449. case BUILD_folddef:
  450. emit_fold(ctx);
  451. break;
  452. default:
  453. break;
  454. }
  455. fflush(ctx->fp);
  456. if (ferror(ctx->fp)) {
  457. fprintf(stderr, "Error: cannot write to output file: %s\n",
  458. strerror(errno));
  459. exit(1);
  460. }
  461. fclose(ctx->fp);
  462. return 0;
  463. }