ldump.c 7.2 KB

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