ldump.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. ** $Id: ldump.c,v 1.4 2003/02/11 23:52:12 lhf Exp lhf $
  3. ** save bytecodes
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stddef.h>
  7. #define ldump_c
  8. #include "lua.h"
  9. #include "lobject.h"
  10. #include "lopcodes.h"
  11. #include "lstate.h"
  12. #include "lundump.h"
  13. #define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D)
  14. #define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D)
  15. typedef struct {
  16. lua_State* L;
  17. lua_Chunkwriter write;
  18. void* data;
  19. int strip;
  20. } DumpState;
  21. static void DumpBlock(const void* b, size_t size, DumpState* D)
  22. {
  23. lua_unlock(D->L);
  24. (*D->write)(D->L,b,size,D->data);
  25. lua_lock(D->L);
  26. }
  27. static void DumpByte(int y, DumpState* D)
  28. {
  29. char x=(char)y;
  30. DumpBlock(&x,sizeof(x),D);
  31. }
  32. static void DumpInt(int x, DumpState* D)
  33. {
  34. DumpBlock(&x,sizeof(x),D);
  35. }
  36. static void DumpSize(size_t x, DumpState* D)
  37. {
  38. DumpBlock(&x,sizeof(x),D);
  39. }
  40. static void DumpNumber(lua_Number x, DumpState* D)
  41. {
  42. DumpBlock(&x,sizeof(x),D);
  43. }
  44. static void DumpString(TString* s, DumpState* D)
  45. {
  46. if (s==NULL || getstr(s)==NULL)
  47. DumpSize(0,D);
  48. else
  49. {
  50. size_t size=s->tsv.len+1; /* include trailing '\0' */
  51. DumpSize(size,D);
  52. DumpBlock(getstr(s),size,D);
  53. }
  54. }
  55. static void DumpCode(const Proto* f, DumpState* D)
  56. {
  57. DumpInt(f->sizecode,D);
  58. DumpVector(f->code,f->sizecode,sizeof(*f->code),D);
  59. }
  60. static void DumpLocals(const Proto* f, DumpState* D)
  61. {
  62. int i,n=f->sizelocvars;
  63. DumpInt(n,D);
  64. for (i=0; i<n; i++)
  65. {
  66. DumpString(f->locvars[i].varname,D);
  67. DumpInt(f->locvars[i].startpc,D);
  68. DumpInt(f->locvars[i].endpc,D);
  69. }
  70. }
  71. static void DumpLines(const Proto* f, DumpState* D)
  72. {
  73. DumpInt(f->sizelineinfo,D);
  74. DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D);
  75. }
  76. static void DumpUpvalues(const Proto* f, DumpState* D)
  77. {
  78. int i,n=f->sizeupvalues;
  79. DumpInt(n,D);
  80. for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
  81. }
  82. static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
  83. static void DumpConstants(const Proto* f, DumpState* D)
  84. {
  85. int i,n;
  86. DumpInt(n=f->sizek,D);
  87. for (i=0; i<n; i++)
  88. {
  89. const TObject* o=&f->k[i];
  90. DumpByte(ttype(o),D);
  91. switch (ttype(o))
  92. {
  93. case LUA_TNUMBER:
  94. DumpNumber(nvalue(o),D);
  95. break;
  96. case LUA_TSTRING:
  97. DumpString(tsvalue(o),D);
  98. break;
  99. case LUA_TNIL:
  100. break;
  101. default:
  102. lua_assert(0); /* cannot happen */
  103. break;
  104. }
  105. }
  106. DumpInt(n=f->sizep,D);
  107. for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
  108. }
  109. static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
  110. {
  111. DumpString((f->source==p) ? NULL : f->source,D);
  112. DumpInt(f->lineDefined,D);
  113. DumpByte(f->nups,D);
  114. DumpByte(f->numparams,D);
  115. DumpByte(f->is_vararg,D);
  116. DumpByte(f->maxstacksize,D);
  117. if (D->strip) DumpInt(0,D); else DumpLines(f,D);
  118. if (D->strip) DumpInt(0,D); else DumpLocals(f,D);
  119. if (D->strip) DumpInt(0,D); else DumpUpvalues(f,D);
  120. DumpConstants(f,D);
  121. DumpCode(f,D);
  122. }
  123. static void DumpHeader(DumpState* D)
  124. {
  125. DumpLiteral(LUA_SIGNATURE,D);
  126. DumpByte(VERSION,D);
  127. DumpByte(luaU_endianness(),D);
  128. DumpByte(sizeof(int),D);
  129. DumpByte(sizeof(size_t),D);
  130. DumpByte(sizeof(Instruction),D);
  131. DumpByte(sizeof(lua_Number),D);
  132. DumpNumber(TEST_NUMBER,D);
  133. }
  134. /*
  135. ** dump function as precompiled chunk
  136. */
  137. int luaU_dump (lua_State* L, const Proto* Main, lua_Chunkwriter w, void* data, int strip)
  138. {
  139. DumpState D;
  140. D.L=L;
  141. D.write=w;
  142. D.data=data;
  143. D.strip=strip;
  144. DumpHeader(&D);
  145. DumpFunction(Main,NULL,&D);
  146. return 1;
  147. }