ldump.c 6.7 KB

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