src/host/buildvm.c - luajit-2.0-src

Global variables defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** LuaJIT VM builder.
  3. ** Copyright (C) 2005-2015 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. #include "lj_ir.h"
  21. #include "lj_ircall.h"
  22. #include "lj_frame.h"
  23. #include "lj_dispatch.h"
  24. #if LJ_HASFFI
  25. #include "lj_ctype.h"
  26. #include "lj_ccall.h"
  27. #endif
  28. #include "luajit.h"

  29. #if defined(_WIN32)
  30. #include <fcntl.h>
  31. #include <io.h>
  32. #endif

  33. /* ------------------------------------------------------------------------ */

  34. /* DynASM glue definitions. */
  35. #define Dst                ctx
  36. #define Dst_DECL        BuildCtx *ctx
  37. #define Dst_REF                (ctx->D)
  38. #define DASM_CHECKS        1

  39. #include "../dynasm/dasm_proto.h"

  40. /* Glue macros for DynASM. */
  41. static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type);

  42. #define DASM_EXTERN(ctx, addr, idx, type) \
  43.   collect_reloc(ctx, addr, idx, type)

  44. /* ------------------------------------------------------------------------ */

  45. /* Avoid trouble if cross-compiling for an x86 target. Speed doesn't matter. */
  46. #define DASM_ALIGNED_WRITES        1

  47. /* Embed architecture-specific DynASM encoder. */
  48. #if LJ_TARGET_X86ORX64
  49. #include "../dynasm/dasm_x86.h"
  50. #elif LJ_TARGET_ARM
  51. #include "../dynasm/dasm_arm.h"
  52. #elif LJ_TARGET_ARM64
  53. #include "../dynasm/dasm_arm64.h"
  54. #elif LJ_TARGET_PPC
  55. #include "../dynasm/dasm_ppc.h"
  56. #elif LJ_TARGET_MIPS
  57. #include "../dynasm/dasm_mips.h"
  58. #else
  59. #error "No support for this architecture (yet)"
  60. #endif

  61. /* Embed generated architecture-specific backend. */
  62. #include "buildvm_arch.h"

  63. /* ------------------------------------------------------------------------ */

  64. void owrite(BuildCtx *ctx, const void *ptr, size_t sz)
  65. {
  66.   if (fwrite(ptr, 1, sz, ctx->fp) != sz) {
  67.     fprintf(stderr, "Error: cannot write to output file: %s\n",
  68.             strerror(errno));
  69.     exit(1);
  70.   }
  71. }

  72. /* ------------------------------------------------------------------------ */

  73. /* Emit code as raw bytes. Only used for DynASM debugging. */
  74. static void emit_raw(BuildCtx *ctx)
  75. {
  76.   owrite(ctx, ctx->code, ctx->codesz);
  77. }

  78. /* -- Build machine code -------------------------------------------------- */

  79. static const char *sym_decorate(BuildCtx *ctx,
  80.                                 const char *prefix, const char *suffix)
  81. {
  82.   char name[256];
  83.   char *p;
  84. #if LJ_64
  85.   const char *symprefix = ctx->mode == BUILD_machasm ? "_" : "";
  86. #elif LJ_TARGET_XBOX360
  87.   const char *symprefix = "";
  88. #else
  89.   const char *symprefix = ctx->mode != BUILD_elfasm ? "_" : "";
  90. #endif
  91.   sprintf(name, "%s%s%s", symprefix, prefix, suffix);
  92.   p = strchr(name, '@');
  93.   if (p) {
  94. #if LJ_TARGET_X86ORX64
  95.     if (!LJ_64 && (ctx->mode == BUILD_coffasm || ctx->mode == BUILD_peobj))
  96.       name[0] = '@';
  97.     else
  98.       *p = '\0';
  99. #elif LJ_TARGET_PPC && !LJ_TARGET_CONSOLE
  100.     /* Keep @plt etc. */
  101. #else
  102.     *p = '\0';
  103. #endif
  104.   }
  105.   p = (char *)malloc(strlen(name)+1);  /* MSVC doesn't like strdup. */
  106.   strcpy(p, name);
  107.   return p;
  108. }

  109. #define NRELOCSYM        (sizeof(extnames)/sizeof(extnames[0])-1)

  110. static int relocmap[NRELOCSYM];

  111. /* Collect external relocations. */
  112. static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type)
  113. {
  114.   if (ctx->nreloc >= BUILD_MAX_RELOC) {
  115.     fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n");
  116.     exit(1);
  117.   }
  118.   if (relocmap[idx] < 0) {
  119.     relocmap[idx] = ctx->nrelocsym;
  120.     ctx->relocsym[ctx->nrelocsym] = sym_decorate(ctx, "", extnames[idx]);
  121.     ctx->nrelocsym++;
  122.   }
  123.   ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code);
  124.   ctx->reloc[ctx->nreloc].sym = relocmap[idx];
  125.   ctx->reloc[ctx->nreloc].type = type;
  126.   ctx->nreloc++;
  127. #if LJ_TARGET_XBOX360
  128.   return (int)(ctx->code - addr) + 4/* Encode symbol offset of .text. */
  129. #else
  130.   return 0/* Encode symbol offset of 0. */
  131. #endif
  132. }

  133. /* Naive insertion sort. Performance doesn't matter here. */
  134. static void sym_insert(BuildCtx *ctx, int32_t ofs,
  135.                        const char *prefix, const char *suffix)
  136. {
  137.   ptrdiff_t i = ctx->nsym++;
  138.   while (i > 0) {
  139.     if (ctx->sym[i-1].ofs <= ofs)
  140.       break;
  141.     ctx->sym[i] = ctx->sym[i-1];
  142.     i--;
  143.   }
  144.   ctx->sym[i].ofs = ofs;
  145.   ctx->sym[i].name = sym_decorate(ctx, prefix, suffix);
  146. }

  147. /* Build the machine code. */
  148. static int build_code(BuildCtx *ctx)
  149. {
  150.   int status;
  151.   int i;

  152.   /* Initialize DynASM structures. */
  153.   ctx->nglob = GLOB__MAX;
  154.   ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *));
  155.   memset(ctx->glob, 0, ctx->nglob*sizeof(void *));
  156.   ctx->nreloc = 0;

  157.   ctx->globnames = globnames;
  158.   ctx->extnames = extnames;
  159.   ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *));
  160.   ctx->nrelocsym = 0;
  161.   for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1;

  162.   ctx->dasm_ident = DASM_IDENT;
  163.   ctx->dasm_arch = DASM_ARCH;

  164.   dasm_init(Dst, DASM_MAXSECTION);
  165.   dasm_setupglobal(Dst, ctx->glob, ctx->nglob);
  166.   dasm_setup(Dst, build_actionlist);

  167.   /* Call arch-specific backend to emit the code. */
  168.   ctx->npc = build_backend(ctx);

  169.   /* Finalize the code. */
  170.   (void)dasm_checkstep(Dst, -1);
  171.   if ((status = dasm_link(Dst, &ctx->codesz))) return status;
  172.   ctx->code = (uint8_t *)malloc(ctx->codesz);
  173.   if ((status = dasm_encode(Dst, (void *)ctx->code))) return status;

  174.   /* Allocate symbol table and bytecode offsets. */
  175.   ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin");
  176.   ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym));
  177.   ctx->nsym = 0;
  178.   ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t));

  179.   /* Collect the opcodes (PC labels). */
  180.   for (i = 0; i < ctx->npc; i++) {
  181.     int32_t ofs = dasm_getpclabel(Dst, i);
  182.     if (ofs < 0) return 0x22000000|i;
  183.     ctx->bc_ofs[i] = ofs;
  184.     if ((LJ_HASJIT ||
  185.          !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP ||
  186.            i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) &&
  187.         (LJ_HASFFI || i != BC_KCDATA))
  188.       sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]);
  189.   }

  190.   /* Collect the globals (named labels). */
  191.   for (i = 0; i < ctx->nglob; i++) {
  192.     const char *gl = globnames[i];
  193.     int len = (int)strlen(gl);
  194.     if (!ctx->glob[i]) {
  195.       fprintf(stderr, "Error: undefined global %s\n", gl);
  196.       exit(2);
  197.     }
  198.     /* Skip the _Z symbols. */
  199.     if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z'))
  200.       sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code),
  201.                  LABEL_PREFIX, globnames[i]);
  202.   }

  203.   /* Close the address range. */
  204.   sym_insert(ctx, (int32_t)ctx->codesz, "", "");
  205.   ctx->nsym--;

  206.   dasm_free(Dst);

  207.   return 0;
  208. }

  209. /* -- Generate VM enums --------------------------------------------------- */

  210. const char *const bc_names[] = {
  211. #define BCNAME(name, ma, mb, mc, mt)       #name,
  212. BCDEF(BCNAME)
  213. #undef BCNAME
  214.   NULL
  215. };

  216. const char *const ir_names[] = {
  217. #define IRNAME(name, m, m1, m2)        #name,
  218. IRDEF(IRNAME)
  219. #undef IRNAME
  220.   NULL
  221. };

  222. const char *const irt_names[] = {
  223. #define IRTNAME(name, size)        #name,
  224. IRTDEF(IRTNAME)
  225. #undef IRTNAME
  226.   NULL
  227. };

  228. const char *const irfpm_names[] = {
  229. #define FPMNAME(name)                #name,
  230. IRFPMDEF(FPMNAME)
  231. #undef FPMNAME
  232.   NULL
  233. };

  234. const char *const irfield_names[] = {
  235. #define FLNAME(name, ofs)        #name,
  236. IRFLDEF(FLNAME)
  237. #undef FLNAME
  238.   NULL
  239. };

  240. const char *const ircall_names[] = {
  241. #define IRCALLNAME(cond, name, nargs, kind, type, flags)        #name,
  242. IRCALLDEF(IRCALLNAME)
  243. #undef IRCALLNAME
  244.   NULL
  245. };

  246. static const char *const trace_errors[] = {
  247. #define TREDEF(name, msg)        msg,
  248. #include "lj_traceerr.h"
  249.   NULL
  250. };

  251. static const char *lower(char *buf, const char *s)
  252. {
  253.   char *p = buf;
  254.   while (*s) {
  255.     *p++ = (*s >= 'A' && *s <= 'Z') ? *s+0x20 : *s;
  256.     s++;
  257.   }
  258.   *p = '\0';
  259.   return buf;
  260. }

  261. /* Emit C source code for bytecode-related definitions. */
  262. static void emit_bcdef(BuildCtx *ctx)
  263. {
  264.   int i;
  265.   fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n");
  266.   fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_ofs[] = {\n");
  267.   for (i = 0; i < ctx->npc; i++) {
  268.     if (i != 0)
  269.       fprintf(ctx->fp, ",\n");
  270.     fprintf(ctx->fp, "%d", ctx->bc_ofs[i]);
  271.   }
  272. }

  273. /* Emit VM definitions as Lua code for debug modules. */
  274. static void emit_vmdef(BuildCtx *ctx)
  275. {
  276.   char buf[80];
  277.   int i;
  278.   fprintf(ctx->fp, "-- This is a generated file. DO NOT EDIT!\n\n");
  279.   fprintf(ctx->fp, "return {\n\n");

  280.   fprintf(ctx->fp, "bcnames = \"");
  281.   for (i = 0; bc_names[i]; i++) fprintf(ctx->fp, "%-6s", bc_names[i]);
  282.   fprintf(ctx->fp, "\",\n\n");

  283.   fprintf(ctx->fp, "irnames = \"");
  284.   for (i = 0; ir_names[i]; i++) fprintf(ctx->fp, "%-6s", ir_names[i]);
  285.   fprintf(ctx->fp, "\",\n\n");

  286.   fprintf(ctx->fp, "irfpm = { [0]=");
  287.   for (i = 0; irfpm_names[i]; i++)
  288.     fprintf(ctx->fp, "\"%s\", ", lower(buf, irfpm_names[i]));
  289.   fprintf(ctx->fp, "},\n\n");

  290.   fprintf(ctx->fp, "irfield = { [0]=");
  291.   for (i = 0; irfield_names[i]; i++) {
  292.     char *p;
  293.     lower(buf, irfield_names[i]);
  294.     p = strchr(buf, '_');
  295.     if (p) *p = '.';
  296.     fprintf(ctx->fp, "\"%s\", ", buf);
  297.   }
  298.   fprintf(ctx->fp, "},\n\n");

  299.   fprintf(ctx->fp, "ircall = {\n[0]=");
  300.   for (i = 0; ircall_names[i]; i++)
  301.     fprintf(ctx->fp, "\"%s\",\n", ircall_names[i]);
  302.   fprintf(ctx->fp, "},\n\n");

  303.   fprintf(ctx->fp, "traceerr = {\n[0]=");
  304.   for (i = 0; trace_errors[i]; i++)
  305.     fprintf(ctx->fp, "\"%s\",\n", trace_errors[i]);
  306.   fprintf(ctx->fp, "},\n\n");
  307. }

  308. /* -- Argument parsing ---------------------------------------------------- */

  309. /* Build mode names. */
  310. static const char *const modenames[] = {
  311. #define BUILDNAME(name)                #name,
  312. BUILDDEF(BUILDNAME)
  313. #undef BUILDNAME
  314.   NULL
  315. };

  316. /* Print usage information and exit. */
  317. static void usage(void)
  318. {
  319.   int i;
  320.   fprintf(stderr, LUAJIT_VERSION " VM builder.\n");
  321.   fprintf(stderr, LUAJIT_COPYRIGHT ", " LUAJIT_URL "\n");
  322.   fprintf(stderr, "Target architecture: " LJ_ARCH_NAME "\n\n");
  323.   fprintf(stderr, "Usage: buildvm -m mode [-o outfile] [infiles...]\n\n");
  324.   fprintf(stderr, "Available modes:\n");
  325.   for (i = 0; i < BUILD__MAX; i++)
  326.     fprintf(stderr, %s\n", modenames[i]);
  327.   exit(1);
  328. }

  329. /* Parse the output mode name. */
  330. static BuildMode parsemode(const char *mode)
  331. {
  332.   int i;
  333.   for (i = 0; modenames[i]; i++)
  334.     if (!strcmp(mode, modenames[i]))
  335.       return (BuildMode)i;
  336.   usage();
  337.   return (BuildMode)-1;
  338. }

  339. /* Parse arguments. */
  340. static void parseargs(BuildCtx *ctx, char **argv)
  341. {
  342.   const char *a;
  343.   int i;
  344.   ctx->mode = (BuildMode)-1;
  345.   ctx->outname = "-";
  346.   for (i = 1; (a = argv[i]) != NULL; i++) {
  347.     if (a[0] != '-')
  348.       break;
  349.     switch (a[1]) {
  350.     case '-':
  351.       if (a[2]) goto err;
  352.       i++;
  353.       goto ok;
  354.     case '\0':
  355.       goto ok;
  356.     case 'm':
  357.       i++;
  358.       if (a[2] || argv[i] == NULL) goto err;
  359.       ctx->mode = parsemode(argv[i]);
  360.       break;
  361.     case 'o':
  362.       i++;
  363.       if (a[2] || argv[i] == NULL) goto err;
  364.       ctx->outname = argv[i];
  365.       break;
  366.     default: err:
  367.       usage();
  368.       break;
  369.     }
  370.   }
  371. ok:
  372.   ctx->args = argv+i;
  373.   if (ctx->mode == (BuildMode)-1) goto err;
  374. }

  375. int main(int argc, char **argv)
  376. {
  377.   BuildCtx ctx_;
  378.   BuildCtx *ctx = &ctx_;
  379.   int status, binmode;

  380.   if (sizeof(void *) != 4*LJ_32+8*LJ_64) {
  381.     fprintf(stderr,"Error: pointer size mismatch in cross-build.\n");
  382.     fprintf(stderr,"Try: make HOST_CC=\"gcc -m32\" CROSS=...\n\n");
  383.     return 1;
  384.   }

  385.   UNUSED(argc);
  386.   parseargs(ctx, argv);

  387.   if ((status = build_code(ctx))) {
  388.     fprintf(stderr,"Error: DASM error %08x\n", status);
  389.     return 1;
  390.   }

  391.   switch (ctx->mode) {
  392.   case BUILD_peobj:
  393.   case BUILD_raw:
  394.     binmode = 1;
  395.     break;
  396.   default:
  397.     binmode = 0;
  398.     break;
  399.   }

  400.   if (ctx->outname[0] == '-' && ctx->outname[1] == '\0') {
  401.     ctx->fp = stdout;
  402. #if defined(_WIN32)
  403.     if (binmode)
  404.       _setmode(_fileno(stdout), _O_BINARY);  /* Yuck. */
  405. #endif
  406.   } else if (!(ctx->fp = fopen(ctx->outname, binmode ? "wb" : "w"))) {
  407.     fprintf(stderr, "Error: cannot open output file '%s': %s\n",
  408.             ctx->outname, strerror(errno));
  409.     exit(1);
  410.   }

  411.   switch (ctx->mode) {
  412.   case BUILD_elfasm:
  413.   case BUILD_coffasm:
  414.   case BUILD_machasm:
  415.     emit_asm(ctx);
  416.     emit_asm_debug(ctx);
  417.     break;
  418.   case BUILD_peobj:
  419.     emit_peobj(ctx);
  420.     break;
  421.   case BUILD_raw:
  422.     emit_raw(ctx);
  423.     break;
  424.   case BUILD_bcdef:
  425.     emit_bcdef(ctx);
  426.     emit_lib(ctx);
  427.     break;
  428.   case BUILD_vmdef:
  429.     emit_vmdef(ctx);
  430.     emit_lib(ctx);
  431.     fprintf(ctx->fp, "}\n\n");
  432.     break;
  433.   case BUILD_ffdef:
  434.   case BUILD_libdef:
  435.   case BUILD_recdef:
  436.     emit_lib(ctx);
  437.     break;
  438.   case BUILD_folddef:
  439.     emit_fold(ctx);
  440.     break;
  441.   default:
  442.     break;
  443.   }

  444.   fflush(ctx->fp);
  445.   if (ferror(ctx->fp)) {
  446.     fprintf(stderr, "Error: cannot write to output file: %s\n",
  447.             strerror(errno));
  448.     exit(1);
  449.   }
  450.   fclose(ctx->fp);

  451.   return 0;
  452. }