ldump.c 6.9 KB

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