ldump.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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":
  104. ** size==0 is followed by an index and means "reuse saved string with
  105. ** that index"; index==0 means NULL.
  106. ** size>=1 is followed by the string contents with real size==size-1 and
  107. ** means that string, which will be saved with the next available index.
  108. ** The real size does not include the ending '\0' (which is not dumped),
  109. ** so adding 1 to it cannot overflow a size_t.
  110. */
  111. static void dumpString (DumpState *D, TString *ts) {
  112. if (ts == NULL) {
  113. dumpVarint(D, 0); /* will "reuse" NULL */
  114. dumpVarint(D, 0); /* special index for NULL */
  115. }
  116. else {
  117. TValue idx;
  118. int tag = luaH_getstr(D->h, ts, &idx);
  119. if (!tagisempty(tag)) { /* string already saved? */
  120. dumpVarint(D, 0); /* reuse a saved string */
  121. dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */
  122. }
  123. else { /* must write and save the string */
  124. TValue key, value; /* to save the string in the hash */
  125. size_t size;
  126. const char *s = getlstr(ts, size);
  127. dumpSize(D, size + 1);
  128. dumpVector(D, s, size + 1); /* include ending '\0' */
  129. D->nstr++; /* one more saved string */
  130. setsvalue(D->L, &key, ts); /* the string is the key */
  131. setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */
  132. luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */
  133. /* integer value does not need barrier */
  134. }
  135. }
  136. }
  137. static void dumpCode (DumpState *D, const Proto *f) {
  138. dumpInt(D, f->sizecode);
  139. dumpAlign(D, sizeof(f->code[0]));
  140. lua_assert(f->code != NULL);
  141. dumpVector(D, f->code, cast_uint(f->sizecode));
  142. }
  143. static void dumpFunction (DumpState *D, const Proto *f);
  144. static void dumpConstants (DumpState *D, const Proto *f) {
  145. int i;
  146. int n = f->sizek;
  147. dumpInt(D, n);
  148. for (i = 0; i < n; i++) {
  149. const TValue *o = &f->k[i];
  150. int tt = ttypetag(o);
  151. dumpByte(D, tt);
  152. switch (tt) {
  153. case LUA_VNUMFLT:
  154. dumpNumber(D, fltvalue(o));
  155. break;
  156. case LUA_VNUMINT:
  157. dumpInteger(D, ivalue(o));
  158. break;
  159. case LUA_VSHRSTR:
  160. case LUA_VLNGSTR:
  161. dumpString(D, tsvalue(o));
  162. break;
  163. default:
  164. lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
  165. }
  166. }
  167. }
  168. static void dumpProtos (DumpState *D, const Proto *f) {
  169. int i;
  170. int n = f->sizep;
  171. dumpInt(D, n);
  172. for (i = 0; i < n; i++)
  173. dumpFunction(D, f->p[i]);
  174. }
  175. static void dumpUpvalues (DumpState *D, const Proto *f) {
  176. int i, n = f->sizeupvalues;
  177. dumpInt(D, n);
  178. for (i = 0; i < n; i++) {
  179. dumpByte(D, f->upvalues[i].instack);
  180. dumpByte(D, f->upvalues[i].idx);
  181. dumpByte(D, f->upvalues[i].kind);
  182. }
  183. }
  184. static void dumpDebug (DumpState *D, const Proto *f) {
  185. int i, n;
  186. n = (D->strip) ? 0 : f->sizelineinfo;
  187. dumpInt(D, n);
  188. if (f->lineinfo != NULL)
  189. dumpVector(D, f->lineinfo, cast_uint(n));
  190. n = (D->strip) ? 0 : f->sizeabslineinfo;
  191. dumpInt(D, n);
  192. if (n > 0) {
  193. /* 'abslineinfo' is an array of structures of int's */
  194. dumpAlign(D, sizeof(int));
  195. dumpVector(D, f->abslineinfo, cast_uint(n));
  196. }
  197. n = (D->strip) ? 0 : f->sizelocvars;
  198. dumpInt(D, n);
  199. for (i = 0; i < n; i++) {
  200. dumpString(D, f->locvars[i].varname);
  201. dumpInt(D, f->locvars[i].startpc);
  202. dumpInt(D, f->locvars[i].endpc);
  203. }
  204. n = (D->strip) ? 0 : f->sizeupvalues;
  205. dumpInt(D, n);
  206. for (i = 0; i < n; i++)
  207. dumpString(D, f->upvalues[i].name);
  208. }
  209. static void dumpFunction (DumpState *D, const Proto *f) {
  210. dumpInt(D, f->linedefined);
  211. dumpInt(D, f->lastlinedefined);
  212. dumpByte(D, f->numparams);
  213. dumpByte(D, f->flag);
  214. dumpByte(D, f->maxstacksize);
  215. dumpCode(D, f);
  216. dumpConstants(D, f);
  217. dumpUpvalues(D, f);
  218. dumpProtos(D, f);
  219. dumpString(D, D->strip ? NULL : f->source);
  220. dumpDebug(D, f);
  221. }
  222. #define dumpNumInfo(D, tvar, value) \
  223. { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); }
  224. static void dumpHeader (DumpState *D) {
  225. dumpLiteral(D, LUA_SIGNATURE);
  226. dumpByte(D, LUAC_VERSION);
  227. dumpByte(D, LUAC_FORMAT);
  228. dumpLiteral(D, LUAC_DATA);
  229. dumpNumInfo(D, int, LUAC_INT);
  230. dumpNumInfo(D, Instruction, LUAC_INST);
  231. dumpNumInfo(D, lua_Integer, LUAC_INT);
  232. dumpNumInfo(D, lua_Number, LUAC_NUM);
  233. }
  234. /*
  235. ** dump Lua function as precompiled chunk
  236. */
  237. int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data,
  238. int strip) {
  239. DumpState D;
  240. D.h = luaH_new(L); /* aux. table to keep strings already dumped */
  241. sethvalue2s(L, L->top.p, D.h); /* anchor it */
  242. L->top.p++;
  243. D.L = L;
  244. D.writer = w;
  245. D.offset = 0;
  246. D.data = data;
  247. D.strip = strip;
  248. D.status = 0;
  249. D.nstr = 0;
  250. dumpHeader(&D);
  251. dumpByte(&D, f->sizeupvalues);
  252. dumpFunction(&D, f);
  253. dumpBlock(&D, NULL, 0); /* signal end of dump */
  254. return D.status;
  255. }