ldump.c 3.3 KB

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