ldump.c 5.7 KB

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