ldump.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. ** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
  3. ** save precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define ldump_c
  7. #define LUA_CORE
  8. #include "lua.h"
  9. #include <string.h>
  10. #include "lobject.h"
  11. #include "lstate.h"
  12. #include "lundump.h"
  13. typedef struct {
  14. lua_State* L;
  15. lua_Writer writer;
  16. void* data;
  17. int strip;
  18. int status;
  19. DumpTargetInfo target;
  20. size_t wrote;
  21. } DumpState;
  22. #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
  23. #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
  24. static void DumpBlock(const void* b, size_t size, DumpState* D)
  25. {
  26. if (D->status==0)
  27. {
  28. lua_unlock(D->L);
  29. D->status=(*D->writer)(D->L,b,size,D->data);
  30. D->wrote+=size;
  31. lua_lock(D->L);
  32. }
  33. }
  34. static void DumpChar(int y, DumpState* D)
  35. {
  36. char x=(char)y;
  37. DumpVar(x,D);
  38. }
  39. static void Align4(DumpState *D)
  40. {
  41. while(D->wrote&3 && D->status==0)
  42. DumpChar(0,D);
  43. }
  44. static void MaybeByteSwap(char *number, size_t numbersize, DumpState *D)
  45. {
  46. int x=1;
  47. int platform_little_endian = *(char*)&x;
  48. if (platform_little_endian != D->target.little_endian)
  49. {
  50. unsigned long i;
  51. for (i=0; i<numbersize/2; i++)
  52. {
  53. char temp = number[i];
  54. number[i] = number[numbersize-1-i];
  55. number[numbersize-1-i] = temp;
  56. }
  57. }
  58. }
  59. static void DumpIntWithSize(int x, int sizeof_int, DumpState* D)
  60. {
  61. /* dump signed integer */
  62. switch(sizeof_int) {
  63. case 1: {
  64. if (x>0x7F || x<(-0x80)) D->status=LUA_ERR_CC_INTOVERFLOW;
  65. DumpChar(x,D);
  66. } break;
  67. case 2: {
  68. if (x>0x7FFF || x<(-0x8000)) D->status=LUA_ERR_CC_INTOVERFLOW;
  69. int16_t y=(int16_t)x;
  70. MaybeByteSwap((char*)&y,2,D);
  71. DumpVar(y,D);
  72. } break;
  73. case 4: {
  74. /* Need to reduce bounds by 1 to avoid messing 32-bit compilers up */
  75. if (x>0x7FFFFFFE || x<(-0x7FFFFFFF)) D->status=LUA_ERR_CC_INTOVERFLOW;
  76. int32_t y=(int32_t)x;
  77. MaybeByteSwap((char*)&y,4,D);
  78. DumpVar(y,D);
  79. } break;
  80. default: lua_assert(0);
  81. }
  82. }
  83. static void DumpInt(int x, DumpState* D)
  84. {
  85. DumpIntWithSize(x,D->target.sizeof_int,D);
  86. }
  87. static void DumpSize(uint32_t x, DumpState* D)
  88. {
  89. /* dump unsigned integer */
  90. switch(D->target.sizeof_strsize_t) {
  91. case 1: {
  92. if (x>0xFF) D->status=LUA_ERR_CC_INTOVERFLOW;
  93. DumpChar(x,D);
  94. } break;
  95. case 2: {
  96. if (x>0xFFFF) D->status=LUA_ERR_CC_INTOVERFLOW;
  97. uint16_t y=(uint16_t)x;
  98. MaybeByteSwap((char*)&y,2,D);
  99. DumpVar(y,D);
  100. } break;
  101. case 4: {
  102. /* Reduce bounds to avoid messing 32-bit compilers up */
  103. if (x>0xFFFFFFFE) D->status=LUA_ERR_CC_INTOVERFLOW;
  104. uint32_t y=x;
  105. MaybeByteSwap((char*)&y,4,D);
  106. DumpVar(y,D);
  107. } break;
  108. default: lua_assert(0);
  109. }
  110. }
  111. static void DumpNumber(lua_Number x, DumpState* D)
  112. {
  113. #if defined( LUA_NUMBER_INTEGRAL ) && !defined( LUA_CROSS_COMPILER )
  114. DumpIntWithSize(x,D->target.sizeof_lua_Number,D);
  115. #else // #if defined( LUA_NUMBER_INTEGRAL ) && !defined( LUA_CROSS_COMPILER )
  116. if (D->target.lua_Number_integral)
  117. {
  118. if (((float)(int)x)!=x) D->status=LUA_ERR_CC_NOTINTEGER;
  119. DumpIntWithSize(x,D->target.sizeof_lua_Number,D);
  120. }
  121. else
  122. {
  123. switch(D->target.sizeof_lua_Number)
  124. {
  125. /* do we need bounds checking? */
  126. case 4: {
  127. float y=x;
  128. MaybeByteSwap((char*)&y,4,D);
  129. DumpVar(y,D);
  130. } break;
  131. case 8: {
  132. double y=x;
  133. // ARM FPA mode: keep endianness, but swap high and low parts of the
  134. // memory representation. This is the default compilation mode for ARM
  135. // targets with non-EABI gcc
  136. if(D->target.is_arm_fpa)
  137. {
  138. char *pnum=(char*)&y, temp[4];
  139. memcpy(temp,pnum,4);
  140. memcpy(pnum,pnum+4,4);
  141. memcpy(pnum+4,temp,4);
  142. }
  143. MaybeByteSwap((char*)&y,8,D);
  144. DumpVar(y,D);
  145. } break;
  146. default: lua_assert(0);
  147. }
  148. }
  149. #endif // #if defined( LUA_NUMBER_INTEGRAL ) && !defined( LUA_CROSS_COMPILER )
  150. }
  151. static void DumpCode(const Proto *f, DumpState* D)
  152. {
  153. DumpInt(f->sizecode,D);
  154. char buf[10];
  155. int i;
  156. Align4(D);
  157. for (i=0; i<f->sizecode; i++)
  158. {
  159. memcpy(buf,&f->code[i],sizeof(Instruction));
  160. MaybeByteSwap(buf,sizeof(Instruction),D);
  161. DumpBlock(buf,sizeof(Instruction),D);
  162. }
  163. }
  164. static void DumpString(const TString* s, DumpState* D)
  165. {
  166. if (s==NULL || getstr(s)==NULL)
  167. {
  168. strsize_t size=0;
  169. DumpSize(size,D);
  170. }
  171. else
  172. {
  173. strsize_t size=( strsize_t )s->tsv.len+1; /* include trailing '\0' */
  174. DumpSize(size,D);
  175. DumpBlock(getstr(s),size,D);
  176. }
  177. }
  178. static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
  179. static void DumpConstants(const Proto* f, DumpState* D)
  180. {
  181. int i,n=f->sizek;
  182. DumpInt(n,D);
  183. for (i=0; i<n; i++)
  184. {
  185. const TValue* o=&f->k[i];
  186. DumpChar(ttype(o),D);
  187. switch (ttype(o))
  188. {
  189. case LUA_TNIL:
  190. break;
  191. case LUA_TBOOLEAN:
  192. DumpChar(bvalue(o),D);
  193. break;
  194. case LUA_TNUMBER:
  195. DumpNumber(nvalue(o),D);
  196. break;
  197. case LUA_TSTRING:
  198. DumpString(rawtsvalue(o),D);
  199. break;
  200. default:
  201. lua_assert(0); /* cannot happen */
  202. break;
  203. }
  204. }
  205. n=f->sizep;
  206. DumpInt(n,D);
  207. for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
  208. }
  209. static void DumpDebug(const Proto* f, DumpState* D)
  210. {
  211. int i,n;
  212. n = (D->strip || f->packedlineinfo == NULL) ? 0: strlen(cast(char *,f->packedlineinfo))+1;
  213. DumpInt(n,D);
  214. Align4(D);
  215. if (n)
  216. {
  217. DumpBlock(f->packedlineinfo, n, D);
  218. }
  219. n= (D->strip) ? 0 : f->sizelocvars;
  220. DumpInt(n,D);
  221. for (i=0; i<n; i++)
  222. {
  223. DumpString(f->locvars[i].varname,D);
  224. DumpInt(f->locvars[i].startpc,D);
  225. DumpInt(f->locvars[i].endpc,D);
  226. }
  227. n= (D->strip) ? 0 : f->sizeupvalues;
  228. DumpInt(n,D);
  229. for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
  230. }
  231. static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
  232. {
  233. DumpString((f->source==p || D->strip) ? NULL : f->source,D);
  234. DumpInt(f->linedefined,D);
  235. DumpInt(f->lastlinedefined,D);
  236. DumpChar(f->nups,D);
  237. DumpChar(f->numparams,D);
  238. DumpChar(f->is_vararg,D);
  239. DumpChar(f->maxstacksize,D);
  240. DumpCode(f,D);
  241. DumpConstants(f,D);
  242. DumpDebug(f,D);
  243. }
  244. static void DumpHeader(DumpState* D)
  245. {
  246. char buf[LUAC_HEADERSIZE];
  247. char *h=buf;
  248. /* This code must be kept in sync wiht luaU_header */
  249. memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
  250. h+=sizeof(LUA_SIGNATURE)-1;
  251. *h++=(char)LUAC_VERSION;
  252. *h++=(char)LUAC_FORMAT;
  253. *h++=(char)D->target.little_endian;
  254. *h++=(char)D->target.sizeof_int;
  255. *h++=(char)D->target.sizeof_strsize_t;
  256. *h++=(char)sizeof(Instruction);
  257. *h++=(char)D->target.sizeof_lua_Number;
  258. *h++=(char)D->target.lua_Number_integral;
  259. DumpBlock(buf,LUAC_HEADERSIZE,D);
  260. }
  261. /*
  262. ** dump Lua function as precompiled chunk with specified target
  263. */
  264. int luaU_dump_crosscompile (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip, DumpTargetInfo target)
  265. {
  266. DumpState D;
  267. D.L=L;
  268. D.writer=w;
  269. D.data=data;
  270. D.strip=strip;
  271. D.status=0;
  272. D.target=target;
  273. D.wrote=0;
  274. DumpHeader(&D);
  275. DumpFunction(f,NULL,&D);
  276. return D.status;
  277. }
  278. /*
  279. ** dump Lua function as precompiled chunk with local machine as target
  280. */
  281. int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
  282. {
  283. DumpTargetInfo target;
  284. int test=1;
  285. target.little_endian=*(char*)&test;
  286. target.sizeof_int=sizeof(int);
  287. target.sizeof_strsize_t=sizeof(strsize_t);
  288. target.sizeof_lua_Number=sizeof(lua_Number);
  289. target.lua_Number_integral=(((lua_Number)0.5)==0);
  290. target.is_arm_fpa=0;
  291. return luaU_dump_crosscompile(L,f,w,data,strip,target);
  292. }