ldump.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. ** $Id: ldump.c $
  3. ** save precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define ldump_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <limits.h>
  10. #include <stddef.h>
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "lgc.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. #include "ltable.h"
  17. #include "lundump.h"
  18. typedef struct {
  19. lua_State *L;
  20. lua_Writer writer;
  21. void *data;
  22. size_t offset; /* current position relative to beginning of dump */
  23. int strip;
  24. int status;
  25. Table *h; /* table to track saved strings */
  26. lua_Unsigned nstr; /* counter for counting saved strings */
  27. } DumpState;
  28. /*
  29. ** All high-level dumps go through dumpVector; you can change it to
  30. ** change the endianness of the result
  31. */
  32. #define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
  33. #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
  34. /*
  35. ** Dump the block of memory pointed by 'b' with given 'size'.
  36. ** 'b' should not be NULL, except for the last call signaling the end
  37. ** of the dump.
  38. */
  39. static void dumpBlock (DumpState *D, const void *b, size_t size) {
  40. if (D->status == 0) { /* do not write anything after an error */
  41. lua_unlock(D->L);
  42. D->status = (*D->writer)(D->L, b, size, D->data);
  43. lua_lock(D->L);
  44. D->offset += size;
  45. }
  46. }
  47. /*
  48. ** Dump enough zeros to ensure that current position is a multiple of
  49. ** 'align'.
  50. */
  51. static void dumpAlign (DumpState *D, unsigned align) {
  52. unsigned padding = align - cast_uint(D->offset % align);
  53. if (padding < align) { /* padding == align means no padding */
  54. static lua_Integer paddingContent = 0;
  55. lua_assert(align <= sizeof(lua_Integer));
  56. dumpBlock(D, &paddingContent, padding);
  57. }
  58. lua_assert(D->offset % align == 0);
  59. }
  60. #define dumpVar(D,x) dumpVector(D,&x,1)
  61. static void dumpByte (DumpState *D, int y) {
  62. lu_byte x = (lu_byte)y;
  63. dumpVar(D, x);
  64. }
  65. /*
  66. ** size for 'dumpVarint' buffer: each byte can store up to 7 bits.
  67. ** (The "+6" rounds up the division.)
  68. */
  69. #define DIBS ((l_numbits(lua_Unsigned) + 6) / 7)
  70. /*
  71. ** Dumps an unsigned integer using the MSB Varint encoding
  72. */
  73. static void dumpVarint (DumpState *D, lua_Unsigned x) {
  74. lu_byte buff[DIBS];
  75. unsigned n = 1;
  76. buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */
  77. while ((x >>= 7) != 0) /* fill other bytes in reverse order */
  78. buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80);
  79. dumpVector(D, buff + DIBS - n, n);
  80. }
  81. static void dumpSize (DumpState *D, size_t sz) {
  82. dumpVarint(D, cast(lua_Unsigned, sz));
  83. }
  84. static void dumpInt (DumpState *D, int x) {
  85. lua_assert(x >= 0);
  86. dumpVarint(D, cast_uint(x));
  87. }
  88. static void dumpNumber (DumpState *D, lua_Number x) {
  89. dumpVar(D, x);
  90. }
  91. /*
  92. ** Signed integers are coded to keep small values small. (Coding -1 as
  93. ** 0xfff...fff would use too many bytes to save a quite common value.)
  94. ** A non-negative x is coded as 2x; a negative x is coded as -2x - 1.
  95. ** (0 => 0; -1 => 1; 1 => 2; -2 => 3; 2 => 4; ...)
  96. */
  97. static void dumpInteger (DumpState *D, lua_Integer x) {
  98. lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x)
  99. : (2u * ~l_castS2U(x)) + 1;
  100. dumpVarint(D, cx);
  101. }
  102. /*
  103. ** Dump a String. First dump its "size": size==0 means NULL;
  104. ** size==1 is followed by an index and means "reuse saved string with
  105. ** that index"; size>=2 is followed by the string contents with real
  106. ** size==size-2 and means that string, which will be saved with
  107. ** the next available index.
  108. */
  109. static void dumpString (DumpState *D, TString *ts) {
  110. if (ts == NULL)
  111. dumpSize(D, 0);
  112. else {
  113. TValue idx;
  114. int tag = luaH_getstr(D->h, ts, &idx);
  115. if (!tagisempty(tag)) { /* string already saved? */
  116. dumpVarint(D, 1); /* reuse a saved string */
  117. dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */
  118. }
  119. else { /* must write and save the string */
  120. TValue key, value; /* to save the string in the hash */
  121. size_t size;
  122. const char *s = getlstr(ts, size);
  123. dumpSize(D, size + 2);
  124. dumpVector(D, s, size + 1); /* include ending '\0' */
  125. D->nstr++; /* one more saved string */
  126. setsvalue(D->L, &key, ts); /* the string is the key */
  127. setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */
  128. luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */
  129. /* integer value does not need barrier */
  130. }
  131. }
  132. }
  133. static void dumpCode (DumpState *D, const Proto *f) {
  134. dumpInt(D, f->sizecode);
  135. dumpAlign(D, sizeof(f->code[0]));
  136. lua_assert(f->code != NULL);
  137. dumpVector(D, f->code, cast_uint(f->sizecode));
  138. }
  139. static void dumpFunction (DumpState *D, const Proto *f);
  140. static void dumpConstants (DumpState *D, const Proto *f) {
  141. int i;
  142. int n = f->sizek;
  143. dumpInt(D, n);
  144. for (i = 0; i < n; i++) {
  145. const TValue *o = &f->k[i];
  146. int tt = ttypetag(o);
  147. dumpByte(D, tt);
  148. switch (tt) {
  149. case LUA_VNUMFLT:
  150. dumpNumber(D, fltvalue(o));
  151. break;
  152. case LUA_VNUMINT:
  153. dumpInteger(D, ivalue(o));
  154. break;
  155. case LUA_VSHRSTR:
  156. case LUA_VLNGSTR:
  157. dumpString(D, tsvalue(o));
  158. break;
  159. default:
  160. lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
  161. }
  162. }
  163. }
  164. static void dumpProtos (DumpState *D, const Proto *f) {
  165. int i;
  166. int n = f->sizep;
  167. dumpInt(D, n);
  168. for (i = 0; i < n; i++)
  169. dumpFunction(D, f->p[i]);
  170. }
  171. static void dumpUpvalues (DumpState *D, const Proto *f) {
  172. int i, n = f->sizeupvalues;
  173. dumpInt(D, n);
  174. for (i = 0; i < n; i++) {
  175. dumpByte(D, f->upvalues[i].instack);
  176. dumpByte(D, f->upvalues[i].idx);
  177. dumpByte(D, f->upvalues[i].kind);
  178. }
  179. }
  180. static void dumpDebug (DumpState *D, const Proto *f) {
  181. int i, n;
  182. n = (D->strip) ? 0 : f->sizelineinfo;
  183. dumpInt(D, n);
  184. if (f->lineinfo != NULL)
  185. dumpVector(D, f->lineinfo, cast_uint(n));
  186. n = (D->strip) ? 0 : f->sizeabslineinfo;
  187. dumpInt(D, n);
  188. if (n > 0) {
  189. /* 'abslineinfo' is an array of structures of int's */
  190. dumpAlign(D, sizeof(int));
  191. dumpVector(D, f->abslineinfo, cast_uint(n));
  192. }
  193. n = (D->strip) ? 0 : f->sizelocvars;
  194. dumpInt(D, n);
  195. for (i = 0; i < n; i++) {
  196. dumpString(D, f->locvars[i].varname);
  197. dumpInt(D, f->locvars[i].startpc);
  198. dumpInt(D, f->locvars[i].endpc);
  199. }
  200. n = (D->strip) ? 0 : f->sizeupvalues;
  201. dumpInt(D, n);
  202. for (i = 0; i < n; i++)
  203. dumpString(D, f->upvalues[i].name);
  204. }
  205. static void dumpFunction (DumpState *D, const Proto *f) {
  206. dumpInt(D, f->linedefined);
  207. dumpInt(D, f->lastlinedefined);
  208. dumpByte(D, f->numparams);
  209. dumpByte(D, f->flag);
  210. dumpByte(D, f->maxstacksize);
  211. dumpCode(D, f);
  212. dumpConstants(D, f);
  213. dumpUpvalues(D, f);
  214. dumpProtos(D, f);
  215. dumpString(D, D->strip ? NULL : f->source);
  216. dumpDebug(D, f);
  217. }
  218. #define dumpNumInfo(D, tvar, value) \
  219. { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); }
  220. static void dumpHeader (DumpState *D) {
  221. dumpLiteral(D, LUA_SIGNATURE);
  222. dumpByte(D, LUAC_VERSION);
  223. dumpByte(D, LUAC_FORMAT);
  224. dumpLiteral(D, LUAC_DATA);
  225. dumpNumInfo(D, int, LUAC_INT);
  226. dumpNumInfo(D, Instruction, LUAC_INST);
  227. dumpNumInfo(D, lua_Integer, LUAC_INT);
  228. dumpNumInfo(D, lua_Number, LUAC_NUM);
  229. }
  230. /*
  231. ** dump Lua function as precompiled chunk
  232. */
  233. int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data,
  234. int strip) {
  235. DumpState D;
  236. D.h = luaH_new(L); /* aux. table to keep strings already dumped */
  237. sethvalue2s(L, L->top.p, D.h); /* anchor it */
  238. L->top.p++;
  239. D.L = L;
  240. D.writer = w;
  241. D.offset = 0;
  242. D.data = data;
  243. D.strip = strip;
  244. D.status = 0;
  245. D.nstr = 0;
  246. dumpHeader(&D);
  247. dumpByte(&D, f->sizeupvalues);
  248. dumpFunction(&D, f);
  249. dumpBlock(&D, NULL, 0); /* signal end of dump */
  250. return D.status;
  251. }