src/lj_bcwrite.c - luajit-2.0-src

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** Bytecode writer.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. #define lj_bcwrite_c
  6. #define LUA_CORE

  7. #include "lj_obj.h"
  8. #include "lj_gc.h"
  9. #include "lj_buf.h"
  10. #include "lj_bc.h"
  11. #if LJ_HASFFI
  12. #include "lj_ctype.h"
  13. #endif
  14. #if LJ_HASJIT
  15. #include "lj_dispatch.h"
  16. #include "lj_jit.h"
  17. #endif
  18. #include "lj_strfmt.h"
  19. #include "lj_bcdump.h"
  20. #include "lj_vm.h"

  21. /* Context for bytecode writer. */
  22. typedef struct BCWriteCtx {
  23.   SBuf sb;                        /* Output buffer. */
  24.   GCproto *pt;                        /* Root prototype. */
  25.   lua_Writer wfunc;                /* Writer callback. */
  26.   void *wdata;                        /* Writer callback data. */
  27.   int strip;                        /* Strip debug info. */
  28.   int status;                        /* Status from writer callback. */
  29. } BCWriteCtx;

  30. /* -- Bytecode writer ----------------------------------------------------- */

  31. /* Write a single constant key/value of a template table. */
  32. static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)
  33. {
  34.   char *p = lj_buf_more(&ctx->sb, 1+10);
  35.   if (tvisstr(o)) {
  36.     const GCstr *str = strV(o);
  37.     MSize len = str->len;
  38.     p = lj_buf_more(&ctx->sb, 5+len);
  39.     p = lj_strfmt_wuleb128(p, BCDUMP_KTAB_STR+len);
  40.     p = lj_buf_wmem(p, strdata(str), len);
  41.   } else if (tvisint(o)) {
  42.     *p++ = BCDUMP_KTAB_INT;
  43.     p = lj_strfmt_wuleb128(p, intV(o));
  44.   } else if (tvisnum(o)) {
  45.     if (!LJ_DUALNUM && narrow) {  /* Narrow number constants to integers. */
  46.       lua_Number num = numV(o);
  47.       int32_t k = lj_num2int(num);
  48.       if (num == (lua_Number)k) {  /* -0 is never a constant. */
  49.         *p++ = BCDUMP_KTAB_INT;
  50.         p = lj_strfmt_wuleb128(p, k);
  51.         setsbufP(&ctx->sb, p);
  52.         return;
  53.       }
  54.     }
  55.     *p++ = BCDUMP_KTAB_NUM;
  56.     p = lj_strfmt_wuleb128(p, o->u32.lo);
  57.     p = lj_strfmt_wuleb128(p, o->u32.hi);
  58.   } else {
  59.     lua_assert(tvispri(o));
  60.     *p++ = BCDUMP_KTAB_NIL+~itype(o);
  61.   }
  62.   setsbufP(&ctx->sb, p);
  63. }

  64. /* Write a template table. */
  65. static void bcwrite_ktab(BCWriteCtx *ctx, char *p, const GCtab *t)
  66. {
  67.   MSize narray = 0, nhash = 0;
  68.   if (t->asize > 0) {  /* Determine max. length of array part. */
  69.     ptrdiff_t i;
  70.     TValue *array = tvref(t->array);
  71.     for (i = (ptrdiff_t)t->asize-1; i >= 0; i--)
  72.       if (!tvisnil(&array[i]))
  73.         break;
  74.     narray = (MSize)(i+1);
  75.   }
  76.   if (t->hmask > 0) {  /* Count number of used hash slots. */
  77.     MSize i, hmask = t->hmask;
  78.     Node *node = noderef(t->node);
  79.     for (i = 0; i <= hmask; i++)
  80.       nhash += !tvisnil(&node[i].val);
  81.   }
  82.   /* Write number of array slots and hash slots. */
  83.   p = lj_strfmt_wuleb128(p, narray);
  84.   p = lj_strfmt_wuleb128(p, nhash);
  85.   setsbufP(&ctx->sb, p);
  86.   if (narray) {  /* Write array entries (may contain nil). */
  87.     MSize i;
  88.     TValue *o = tvref(t->array);
  89.     for (i = 0; i < narray; i++, o++)
  90.       bcwrite_ktabk(ctx, o, 1);
  91.   }
  92.   if (nhash) {  /* Write hash entries. */
  93.     MSize i = nhash;
  94.     Node *node = noderef(t->node) + t->hmask;
  95.     for (;; node--)
  96.       if (!tvisnil(&node->val)) {
  97.         bcwrite_ktabk(ctx, &node->key, 0);
  98.         bcwrite_ktabk(ctx, &node->val, 1);
  99.         if (--i == 0) break;
  100.       }
  101.   }
  102. }

  103. /* Write GC constants of a prototype. */
  104. static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
  105. {
  106.   MSize i, sizekgc = pt->sizekgc;
  107.   GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc;
  108.   for (i = 0; i < sizekgc; i++, kr++) {
  109.     GCobj *o = gcref(*kr);
  110.     MSize tp, need = 1;
  111.     char *p;
  112.     /* Determine constant type and needed size. */
  113.     if (o->gch.gct == ~LJ_TSTR) {
  114.       tp = BCDUMP_KGC_STR + gco2str(o)->len;
  115.       need = 5+gco2str(o)->len;
  116.     } else if (o->gch.gct == ~LJ_TPROTO) {
  117.       lua_assert((pt->flags & PROTO_CHILD));
  118.       tp = BCDUMP_KGC_CHILD;
  119. #if LJ_HASFFI
  120.     } else if (o->gch.gct == ~LJ_TCDATA) {
  121.       CTypeID id = gco2cd(o)->ctypeid;
  122.       need = 1+4*5;
  123.       if (id == CTID_INT64) {
  124.         tp = BCDUMP_KGC_I64;
  125.       } else if (id == CTID_UINT64) {
  126.         tp = BCDUMP_KGC_U64;
  127.       } else {
  128.         lua_assert(id == CTID_COMPLEX_DOUBLE);
  129.         tp = BCDUMP_KGC_COMPLEX;
  130.       }
  131. #endif
  132.     } else {
  133.       lua_assert(o->gch.gct == ~LJ_TTAB);
  134.       tp = BCDUMP_KGC_TAB;
  135.       need = 1+2*5;
  136.     }
  137.     /* Write constant type. */
  138.     p = lj_buf_more(&ctx->sb, need);
  139.     p = lj_strfmt_wuleb128(p, tp);
  140.     /* Write constant data (if any). */
  141.     if (tp >= BCDUMP_KGC_STR) {
  142.       p = lj_buf_wmem(p, strdata(gco2str(o)), gco2str(o)->len);
  143.     } else if (tp == BCDUMP_KGC_TAB) {
  144.       bcwrite_ktab(ctx, p, gco2tab(o));
  145.       continue;
  146. #if LJ_HASFFI
  147.     } else if (tp != BCDUMP_KGC_CHILD) {
  148.       cTValue *q = (TValue *)cdataptr(gco2cd(o));
  149.       p = lj_strfmt_wuleb128(p, q[0].u32.lo);
  150.       p = lj_strfmt_wuleb128(p, q[0].u32.hi);
  151.       if (tp == BCDUMP_KGC_COMPLEX) {
  152.         p = lj_strfmt_wuleb128(p, q[1].u32.lo);
  153.         p = lj_strfmt_wuleb128(p, q[1].u32.hi);
  154.       }
  155. #endif
  156.     }
  157.     setsbufP(&ctx->sb, p);
  158.   }
  159. }

  160. /* Write number constants of a prototype. */
  161. static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
  162. {
  163.   MSize i, sizekn = pt->sizekn;
  164.   cTValue *o = mref(pt->k, TValue);
  165.   char *p = lj_buf_more(&ctx->sb, 10*sizekn);
  166.   for (i = 0; i < sizekn; i++, o++) {
  167.     int32_t k;
  168.     if (tvisint(o)) {
  169.       k = intV(o);
  170.       goto save_int;
  171.     } else {
  172.       /* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */
  173.       if (!LJ_DUALNUM) {  /* Narrow number constants to integers. */
  174.         lua_Number num = numV(o);
  175.         k = lj_num2int(num);
  176.         if (num == (lua_Number)k) {  /* -0 is never a constant. */
  177.         save_int:
  178.           p = lj_strfmt_wuleb128(p, 2*(uint32_t)k | ((uint32_t)k&0x80000000u));
  179.           if (k < 0)
  180.             p[-1] = (p[-1] & 7) | ((k>>27) & 0x18);
  181.           continue;
  182.         }
  183.       }
  184.       p = lj_strfmt_wuleb128(p, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));
  185.       if (o->u32.lo >= 0x80000000u)
  186.         p[-1] = (p[-1] & 7) | ((o->u32.lo>>27) & 0x18);
  187.       p = lj_strfmt_wuleb128(p, o->u32.hi);
  188.     }
  189.   }
  190.   setsbufP(&ctx->sb, p);
  191. }

  192. /* Write bytecode instructions. */
  193. static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt)
  194. {
  195.   MSize nbc = pt->sizebc-1/* Omit the [JI]FUNC* header. */
  196. #if LJ_HASJIT
  197.   uint8_t *q = (uint8_t *)p;
  198. #endif
  199.   p = lj_buf_wmem(p, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));
  200.   UNUSED(ctx);
  201. #if LJ_HASJIT
  202.   /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
  203.   if ((pt->flags & PROTO_ILOOP) || pt->trace) {
  204.     jit_State *J = L2J(sbufL(&ctx->sb));
  205.     MSize i;
  206.     for (i = 0; i < nbc; i++, q += sizeof(BCIns)) {
  207.       BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)];
  208.       if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||
  209.           op == BC_JFORI) {
  210.         q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);
  211.       } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
  212.         BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8);
  213.         BCIns ins = traceref(J, rd)->startins;
  214.         q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL);
  215.         q[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins);
  216.         q[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins);
  217.       }
  218.     }
  219.   }
  220. #endif
  221.   return p;
  222. }

  223. /* Write prototype. */
  224. static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
  225. {
  226.   MSize sizedbg = 0;
  227.   char *p;

  228.   /* Recursively write children of prototype. */
  229.   if ((pt->flags & PROTO_CHILD)) {
  230.     ptrdiff_t i, n = pt->sizekgc;
  231.     GCRef *kr = mref(pt->k, GCRef) - 1;
  232.     for (i = 0; i < n; i++, kr--) {
  233.       GCobj *o = gcref(*kr);
  234.       if (o->gch.gct == ~LJ_TPROTO)
  235.         bcwrite_proto(ctx, gco2pt(o));
  236.     }
  237.   }

  238.   /* Start writing the prototype info to a buffer. */
  239.   p = lj_buf_need(&ctx->sb,
  240.                   5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
  241.   p += 5/* Leave room for final size. */

  242.   /* Write prototype header. */
  243.   *p++ = (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI));
  244.   *p++ = pt->numparams;
  245.   *p++ = pt->framesize;
  246.   *p++ = pt->sizeuv;
  247.   p = lj_strfmt_wuleb128(p, pt->sizekgc);
  248.   p = lj_strfmt_wuleb128(p, pt->sizekn);
  249.   p = lj_strfmt_wuleb128(p, pt->sizebc-1);
  250.   if (!ctx->strip) {
  251.     if (proto_lineinfo(pt))
  252.       sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);
  253.     p = lj_strfmt_wuleb128(p, sizedbg);
  254.     if (sizedbg) {
  255.       p = lj_strfmt_wuleb128(p, pt->firstline);
  256.       p = lj_strfmt_wuleb128(p, pt->numline);
  257.     }
  258.   }

  259.   /* Write bytecode instructions and upvalue refs. */
  260.   p = bcwrite_bytecode(ctx, p, pt);
  261.   p = lj_buf_wmem(p, proto_uv(pt), pt->sizeuv*2);
  262.   setsbufP(&ctx->sb, p);

  263.   /* Write constants. */
  264.   bcwrite_kgc(ctx, pt);
  265.   bcwrite_knum(ctx, pt);

  266.   /* Write debug info, if not stripped. */
  267.   if (sizedbg) {
  268.     p = lj_buf_more(&ctx->sb, sizedbg);
  269.     p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg);
  270.     setsbufP(&ctx->sb, p);
  271.   }

  272.   /* Pass buffer to writer function. */
  273.   if (ctx->status == 0) {
  274.     MSize n = sbuflen(&ctx->sb) - 5;
  275.     MSize nn = (lj_fls(n)+8)*9 >> 6;
  276.     char *q = sbufB(&ctx->sb) + (5 - nn);
  277.     p = lj_strfmt_wuleb128(q, n);  /* Fill in final size. */
  278.     lua_assert(p == sbufB(&ctx->sb) + 5);
  279.     ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata);
  280.   }
  281. }

  282. /* Write header of bytecode dump. */
  283. static void bcwrite_header(BCWriteCtx *ctx)
  284. {
  285.   GCstr *chunkname = proto_chunkname(ctx->pt);
  286.   const char *name = strdata(chunkname);
  287.   MSize len = chunkname->len;
  288.   char *p = lj_buf_need(&ctx->sb, 5+5+len);
  289.   *p++ = BCDUMP_HEAD1;
  290.   *p++ = BCDUMP_HEAD2;
  291.   *p++ = BCDUMP_HEAD3;
  292.   *p++ = BCDUMP_VERSION;
  293.   *p++ = (ctx->strip ? BCDUMP_F_STRIP : 0) +
  294.          LJ_BE*BCDUMP_F_BE +
  295.          ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0) +
  296.          LJ_FR2*BCDUMP_F_FR2;
  297.   if (!ctx->strip) {
  298.     p = lj_strfmt_wuleb128(p, len);
  299.     p = lj_buf_wmem(p, name, len);
  300.   }
  301.   ctx->status = ctx->wfunc(sbufL(&ctx->sb), sbufB(&ctx->sb),
  302.                            (MSize)(p - sbufB(&ctx->sb)), ctx->wdata);
  303. }

  304. /* Write footer of bytecode dump. */
  305. static void bcwrite_footer(BCWriteCtx *ctx)
  306. {
  307.   if (ctx->status == 0) {
  308.     uint8_t zero = 0;
  309.     ctx->status = ctx->wfunc(sbufL(&ctx->sb), &zero, 1, ctx->wdata);
  310.   }
  311. }

  312. /* Protected callback for bytecode writer. */
  313. static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)
  314. {
  315.   BCWriteCtx *ctx = (BCWriteCtx *)ud;
  316.   UNUSED(L); UNUSED(dummy);
  317.   lj_buf_need(&ctx->sb, 1024);  /* Avoids resize for most prototypes. */
  318.   bcwrite_header(ctx);
  319.   bcwrite_proto(ctx, ctx->pt);
  320.   bcwrite_footer(ctx);
  321.   return NULL;
  322. }

  323. /* Write bytecode for a prototype. */
  324. int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data,
  325.               int strip)
  326. {
  327.   BCWriteCtx ctx;
  328.   int status;
  329.   ctx.pt = pt;
  330.   ctx.wfunc = writer;
  331.   ctx.wdata = data;
  332.   ctx.strip = strip;
  333.   ctx.status = 0;
  334.   lj_buf_init(L, &ctx.sb);
  335.   status = lj_vm_cpcall(L, NULL, &ctx, cpwriter);
  336.   if (status == 0) status = ctx.status;
  337.   lj_buf_free(G(sbufL(&ctx.sb)), &ctx.sb);
  338.   return status;
  339. }