ldump.c 7.0 KB

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