lj_bcwrite.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. ** Bytecode writer.
  3. ** Copyright (C) 2005-2014 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_str.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_bcdump.h"
  19. #include "lj_vm.h"
  20. /* Context for bytecode writer. */
  21. typedef struct BCWriteCtx {
  22. SBuf sb; /* Output buffer. */
  23. lua_State *L; /* Lua state. */
  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. /* -- Output buffer handling ---------------------------------------------- */
  31. /* Resize buffer if needed. */
  32. static LJ_NOINLINE void bcwrite_resize(BCWriteCtx *ctx, MSize len)
  33. {
  34. MSize sz = ctx->sb.sz * 2;
  35. while (ctx->sb.n + len > sz) sz = sz * 2;
  36. lj_str_resizebuf(ctx->L, &ctx->sb, sz);
  37. }
  38. /* Need a certain amount of buffer space. */
  39. static LJ_AINLINE void bcwrite_need(BCWriteCtx *ctx, MSize len)
  40. {
  41. if (LJ_UNLIKELY(ctx->sb.n + len > ctx->sb.sz))
  42. bcwrite_resize(ctx, len);
  43. }
  44. /* Add memory block to buffer. */
  45. static void bcwrite_block(BCWriteCtx *ctx, const void *p, MSize len)
  46. {
  47. uint8_t *q = (uint8_t *)(ctx->sb.buf + ctx->sb.n);
  48. MSize i;
  49. ctx->sb.n += len;
  50. for (i = 0; i < len; i++) q[i] = ((uint8_t *)p)[i];
  51. }
  52. /* Add byte to buffer. */
  53. static LJ_AINLINE void bcwrite_byte(BCWriteCtx *ctx, uint8_t b)
  54. {
  55. ctx->sb.buf[ctx->sb.n++] = b;
  56. }
  57. /* Add ULEB128 value to buffer. */
  58. static void bcwrite_uleb128(BCWriteCtx *ctx, uint32_t v)
  59. {
  60. MSize n = ctx->sb.n;
  61. uint8_t *p = (uint8_t *)ctx->sb.buf;
  62. for (; v >= 0x80; v >>= 7)
  63. p[n++] = (uint8_t)((v & 0x7f) | 0x80);
  64. p[n++] = (uint8_t)v;
  65. ctx->sb.n = n;
  66. }
  67. /* -- Bytecode writer ----------------------------------------------------- */
  68. /* Write a single constant key/value of a template table. */
  69. static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)
  70. {
  71. bcwrite_need(ctx, 1+10);
  72. if (tvisstr(o)) {
  73. const GCstr *str = strV(o);
  74. MSize len = str->len;
  75. bcwrite_need(ctx, 5+len);
  76. bcwrite_uleb128(ctx, BCDUMP_KTAB_STR+len);
  77. bcwrite_block(ctx, strdata(str), len);
  78. } else if (tvisint(o)) {
  79. bcwrite_byte(ctx, BCDUMP_KTAB_INT);
  80. bcwrite_uleb128(ctx, intV(o));
  81. } else if (tvisnum(o)) {
  82. if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */
  83. lua_Number num = numV(o);
  84. int32_t k = lj_num2int(num);
  85. if (num == (lua_Number)k) { /* -0 is never a constant. */
  86. bcwrite_byte(ctx, BCDUMP_KTAB_INT);
  87. bcwrite_uleb128(ctx, k);
  88. return;
  89. }
  90. }
  91. bcwrite_byte(ctx, BCDUMP_KTAB_NUM);
  92. bcwrite_uleb128(ctx, o->u32.lo);
  93. bcwrite_uleb128(ctx, o->u32.hi);
  94. } else {
  95. lua_assert(tvispri(o));
  96. bcwrite_byte(ctx, BCDUMP_KTAB_NIL+~itype(o));
  97. }
  98. }
  99. /* Write a template table. */
  100. static void bcwrite_ktab(BCWriteCtx *ctx, const GCtab *t)
  101. {
  102. MSize narray = 0, nhash = 0;
  103. if (t->asize > 0) { /* Determine max. length of array part. */
  104. ptrdiff_t i;
  105. TValue *array = tvref(t->array);
  106. for (i = (ptrdiff_t)t->asize-1; i >= 0; i--)
  107. if (!tvisnil(&array[i]))
  108. break;
  109. narray = (MSize)(i+1);
  110. }
  111. if (t->hmask > 0) { /* Count number of used hash slots. */
  112. MSize i, hmask = t->hmask;
  113. Node *node = noderef(t->node);
  114. for (i = 0; i <= hmask; i++)
  115. nhash += !tvisnil(&node[i].val);
  116. }
  117. /* Write number of array slots and hash slots. */
  118. bcwrite_uleb128(ctx, narray);
  119. bcwrite_uleb128(ctx, nhash);
  120. if (narray) { /* Write array entries (may contain nil). */
  121. MSize i;
  122. TValue *o = tvref(t->array);
  123. for (i = 0; i < narray; i++, o++)
  124. bcwrite_ktabk(ctx, o, 1);
  125. }
  126. if (nhash) { /* Write hash entries. */
  127. MSize i = nhash;
  128. Node *node = noderef(t->node) + t->hmask;
  129. for (;; node--)
  130. if (!tvisnil(&node->val)) {
  131. bcwrite_ktabk(ctx, &node->key, 0);
  132. bcwrite_ktabk(ctx, &node->val, 1);
  133. if (--i == 0) break;
  134. }
  135. }
  136. }
  137. /* Write GC constants of a prototype. */
  138. static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
  139. {
  140. MSize i, sizekgc = pt->sizekgc;
  141. GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc;
  142. for (i = 0; i < sizekgc; i++, kr++) {
  143. GCobj *o = gcref(*kr);
  144. MSize tp, need = 1;
  145. /* Determine constant type and needed size. */
  146. if (o->gch.gct == ~LJ_TSTR) {
  147. tp = BCDUMP_KGC_STR + gco2str(o)->len;
  148. need = 5+gco2str(o)->len;
  149. } else if (o->gch.gct == ~LJ_TPROTO) {
  150. lua_assert((pt->flags & PROTO_CHILD));
  151. tp = BCDUMP_KGC_CHILD;
  152. #if LJ_HASFFI
  153. } else if (o->gch.gct == ~LJ_TCDATA) {
  154. CTypeID id = gco2cd(o)->ctypeid;
  155. need = 1+4*5;
  156. if (id == CTID_INT64) {
  157. tp = BCDUMP_KGC_I64;
  158. } else if (id == CTID_UINT64) {
  159. tp = BCDUMP_KGC_U64;
  160. } else {
  161. lua_assert(id == CTID_COMPLEX_DOUBLE);
  162. tp = BCDUMP_KGC_COMPLEX;
  163. }
  164. #endif
  165. } else {
  166. lua_assert(o->gch.gct == ~LJ_TTAB);
  167. tp = BCDUMP_KGC_TAB;
  168. need = 1+2*5;
  169. }
  170. /* Write constant type. */
  171. bcwrite_need(ctx, need);
  172. bcwrite_uleb128(ctx, tp);
  173. /* Write constant data (if any). */
  174. if (tp >= BCDUMP_KGC_STR) {
  175. bcwrite_block(ctx, strdata(gco2str(o)), gco2str(o)->len);
  176. } else if (tp == BCDUMP_KGC_TAB) {
  177. bcwrite_ktab(ctx, gco2tab(o));
  178. #if LJ_HASFFI
  179. } else if (tp != BCDUMP_KGC_CHILD) {
  180. cTValue *p = (TValue *)cdataptr(gco2cd(o));
  181. bcwrite_uleb128(ctx, p[0].u32.lo);
  182. bcwrite_uleb128(ctx, p[0].u32.hi);
  183. if (tp == BCDUMP_KGC_COMPLEX) {
  184. bcwrite_uleb128(ctx, p[1].u32.lo);
  185. bcwrite_uleb128(ctx, p[1].u32.hi);
  186. }
  187. #endif
  188. }
  189. }
  190. }
  191. /* Write number constants of a prototype. */
  192. static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
  193. {
  194. MSize i, sizekn = pt->sizekn;
  195. cTValue *o = mref(pt->k, TValue);
  196. bcwrite_need(ctx, 10*sizekn);
  197. for (i = 0; i < sizekn; i++, o++) {
  198. int32_t k;
  199. if (tvisint(o)) {
  200. k = intV(o);
  201. goto save_int;
  202. } else {
  203. /* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */
  204. if (!LJ_DUALNUM) { /* Narrow number constants to integers. */
  205. lua_Number num = numV(o);
  206. k = lj_num2int(num);
  207. if (num == (lua_Number)k) { /* -0 is never a constant. */
  208. save_int:
  209. bcwrite_uleb128(ctx, 2*(uint32_t)k | ((uint32_t)k & 0x80000000u));
  210. if (k < 0) {
  211. char *p = &ctx->sb.buf[ctx->sb.n-1];
  212. *p = (*p & 7) | ((k>>27) & 0x18);
  213. }
  214. continue;
  215. }
  216. }
  217. bcwrite_uleb128(ctx, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));
  218. if (o->u32.lo >= 0x80000000u) {
  219. char *p = &ctx->sb.buf[ctx->sb.n-1];
  220. *p = (*p & 7) | ((o->u32.lo>>27) & 0x18);
  221. }
  222. bcwrite_uleb128(ctx, o->u32.hi);
  223. }
  224. }
  225. }
  226. /* Write bytecode instructions. */
  227. static void bcwrite_bytecode(BCWriteCtx *ctx, GCproto *pt)
  228. {
  229. MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */
  230. #if LJ_HASJIT
  231. uint8_t *p = (uint8_t *)&ctx->sb.buf[ctx->sb.n];
  232. #endif
  233. bcwrite_block(ctx, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));
  234. #if LJ_HASJIT
  235. /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
  236. if ((pt->flags & PROTO_ILOOP) || pt->trace) {
  237. jit_State *J = L2J(ctx->L);
  238. MSize i;
  239. for (i = 0; i < nbc; i++, p += sizeof(BCIns)) {
  240. BCOp op = (BCOp)p[LJ_ENDIAN_SELECT(0, 3)];
  241. if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||
  242. op == BC_JFORI) {
  243. p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);
  244. } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
  245. BCReg rd = p[LJ_ENDIAN_SELECT(2, 1)] + (p[LJ_ENDIAN_SELECT(3, 0)] << 8);
  246. BCIns ins = traceref(J, rd)->startins;
  247. p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL);
  248. p[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins);
  249. p[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins);
  250. }
  251. }
  252. }
  253. #endif
  254. }
  255. /* Write prototype. */
  256. static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
  257. {
  258. MSize sizedbg = 0;
  259. /* Recursively write children of prototype. */
  260. if ((pt->flags & PROTO_CHILD)) {
  261. ptrdiff_t i, n = pt->sizekgc;
  262. GCRef *kr = mref(pt->k, GCRef) - 1;
  263. for (i = 0; i < n; i++, kr--) {
  264. GCobj *o = gcref(*kr);
  265. if (o->gch.gct == ~LJ_TPROTO)
  266. bcwrite_proto(ctx, gco2pt(o));
  267. }
  268. }
  269. /* Start writing the prototype info to a buffer. */
  270. lj_str_resetbuf(&ctx->sb);
  271. ctx->sb.n = 5; /* Leave room for final size. */
  272. bcwrite_need(ctx, 4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
  273. /* Write prototype header. */
  274. bcwrite_byte(ctx, (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI)));
  275. bcwrite_byte(ctx, pt->numparams);
  276. bcwrite_byte(ctx, pt->framesize);
  277. bcwrite_byte(ctx, pt->sizeuv);
  278. bcwrite_uleb128(ctx, pt->sizekgc);
  279. bcwrite_uleb128(ctx, pt->sizekn);
  280. bcwrite_uleb128(ctx, pt->sizebc-1);
  281. if (!ctx->strip) {
  282. if (proto_lineinfo(pt))
  283. sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);
  284. bcwrite_uleb128(ctx, sizedbg);
  285. if (sizedbg) {
  286. bcwrite_uleb128(ctx, pt->firstline);
  287. bcwrite_uleb128(ctx, pt->numline);
  288. }
  289. }
  290. /* Write bytecode instructions and upvalue refs. */
  291. bcwrite_bytecode(ctx, pt);
  292. bcwrite_block(ctx, proto_uv(pt), pt->sizeuv*2);
  293. /* Write constants. */
  294. bcwrite_kgc(ctx, pt);
  295. bcwrite_knum(ctx, pt);
  296. /* Write debug info, if not stripped. */
  297. if (sizedbg) {
  298. bcwrite_need(ctx, sizedbg);
  299. bcwrite_block(ctx, proto_lineinfo(pt), sizedbg);
  300. }
  301. /* Pass buffer to writer function. */
  302. if (ctx->status == 0) {
  303. MSize n = ctx->sb.n - 5;
  304. MSize nn = (lj_fls(n)+8)*9 >> 6;
  305. ctx->sb.n = 5 - nn;
  306. bcwrite_uleb128(ctx, n); /* Fill in final size. */
  307. lua_assert(ctx->sb.n == 5);
  308. ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf+5-nn, nn+n, ctx->wdata);
  309. }
  310. }
  311. /* Write header of bytecode dump. */
  312. static void bcwrite_header(BCWriteCtx *ctx)
  313. {
  314. GCstr *chunkname = proto_chunkname(ctx->pt);
  315. const char *name = strdata(chunkname);
  316. MSize len = chunkname->len;
  317. lj_str_resetbuf(&ctx->sb);
  318. bcwrite_need(ctx, 5+5+len);
  319. bcwrite_byte(ctx, BCDUMP_HEAD1);
  320. bcwrite_byte(ctx, BCDUMP_HEAD2);
  321. bcwrite_byte(ctx, BCDUMP_HEAD3);
  322. bcwrite_byte(ctx, BCDUMP_VERSION);
  323. bcwrite_byte(ctx, (ctx->strip ? BCDUMP_F_STRIP : 0) +
  324. (LJ_BE ? BCDUMP_F_BE : 0) +
  325. ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0));
  326. if (!ctx->strip) {
  327. bcwrite_uleb128(ctx, len);
  328. bcwrite_block(ctx, name, len);
  329. }
  330. ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf, ctx->sb.n, ctx->wdata);
  331. }
  332. /* Write footer of bytecode dump. */
  333. static void bcwrite_footer(BCWriteCtx *ctx)
  334. {
  335. if (ctx->status == 0) {
  336. uint8_t zero = 0;
  337. ctx->status = ctx->wfunc(ctx->L, &zero, 1, ctx->wdata);
  338. }
  339. }
  340. /* Protected callback for bytecode writer. */
  341. static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)
  342. {
  343. BCWriteCtx *ctx = (BCWriteCtx *)ud;
  344. UNUSED(dummy);
  345. lj_str_resizebuf(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */
  346. bcwrite_header(ctx);
  347. bcwrite_proto(ctx, ctx->pt);
  348. bcwrite_footer(ctx);
  349. return NULL;
  350. }
  351. /* Write bytecode for a prototype. */
  352. int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data,
  353. int strip)
  354. {
  355. BCWriteCtx ctx;
  356. int status;
  357. ctx.L = L;
  358. ctx.pt = pt;
  359. ctx.wfunc = writer;
  360. ctx.wdata = data;
  361. ctx.strip = strip;
  362. ctx.status = 0;
  363. lj_str_initbuf(&ctx.sb);
  364. status = lj_vm_cpcall(L, NULL, &ctx, cpwriter);
  365. if (status == 0) status = ctx.status;
  366. lj_str_freebuf(G(ctx.L), &ctx.sb);
  367. return status;
  368. }