ldump.c 6.1 KB

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