Make the compiled bytecode more portable by requiring that the generic int sizes be 32 bits. In addition, swap 32 bit ints and 64 bit doubles as needed.

This commit is contained in:
Steve Checkoway
2007-04-21 20:08:12 +00:00
parent a87cc6a80a
commit 1e99c68e8b
7 changed files with 53 additions and 18 deletions
+1 -1
View File
@@ -786,7 +786,7 @@ static int luaK_code (FuncState *fs, Instruction i, int line) {
MAX_INT, "code size overflow");
f->code[fs->pc] = i;
/* save corresponding line information */
luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, uint32_t,
MAX_INT, "code size overflow");
f->lineinfo[fs->pc] = line;
return fs->pc++;
+1 -1
View File
@@ -180,7 +180,7 @@ static void collectvalidlines (lua_State *L, Closure *f) {
}
else {
Table *t = luaH_new(L, 0, 0);
int *lineinfo = f->l.p->lineinfo;
uint32_t *lineinfo = f->l.p->lineinfo;
int i;
for (i=0; i<f->l.p->sizelineinfo; i++)
setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
+1 -1
View File
@@ -13,7 +13,7 @@
#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0)
#define getline(f,pc) (((f)->lineinfo) ? (int)(f)->lineinfo[pc] : 0)
#define resethookcount(L) (L->hookcount = L->basehookcount)
+5 -6
View File
@@ -42,7 +42,7 @@ static void DumpChar(int y, DumpState* D)
DumpVar(x,D);
}
static void DumpInt(int x, DumpState* D)
static void DumpInt(uint32_t x, DumpState* D)
{
DumpVar(x,D);
}
@@ -62,13 +62,12 @@ static void DumpString(const TString* s, DumpState* D)
{
if (s==NULL || getstr(s)==NULL)
{
size_t size=0;
DumpVar(size,D);
DumpInt(0,D);
}
else
{
size_t size=s->tsv.len+1; /* include trailing '\0' */
DumpVar(size,D);
lu_int32 size=s->tsv.len+1; /* include trailing '\0' */
DumpInt(size,D);
DumpBlock(getstr(s),size,D);
}
}
@@ -112,7 +111,7 @@ static void DumpDebug(const Proto* f, DumpState* D)
{
int i,n;
n= (D->strip) ? 0 : f->sizelineinfo;
DumpVector(f->lineinfo,n,sizeof(int),D);
DumpVector(f->lineinfo,n,sizeof(uint32_t),D);
n= (D->strip) ? 0 : f->sizelocvars;
DumpInt(n,D);
for (i=0; i<n; i++)
+1 -1
View File
@@ -233,7 +233,7 @@ typedef struct Proto {
TValue *k; /* constants used by the function */
Instruction *code;
struct Proto **p; /* functions defined inside the function */
int *lineinfo; /* map from opcodes to source lines */
uint32_t *lineinfo; /* map from opcodes to source lines */
struct LocVar *locvars; /* information about local variables */
TString **upvalues; /* upvalue names */
TString *source;
+42 -6
View File
@@ -25,6 +25,7 @@ typedef struct {
ZIO* Z;
Mbuffer* b;
const char* name;
int flip;
} LoadState;
#ifdef LUAC_TRUST_BINARIES
@@ -44,6 +45,9 @@ static void error(LoadState* S, const char* why)
#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
/* XXX: Use optimized versions if they exist. */
#define Swap32(n) ( ((n)>>24) | (((n)>>8) & 0x0000FF00) | (((n)<<8) & 0x00FF0000) | ((n)<<24) )
static void LoadBlock(LoadState* S, void* b, size_t size)
{
size_t r=luaZ_read(S->Z,b,size);
@@ -59,8 +63,10 @@ static int LoadChar(LoadState* S)
static int LoadInt(LoadState* S)
{
int x;
uint32_t x;
LoadVar(S,x);
if (S->flip)
x = Swap32(x);
IF (x<0, "bad integer");
return x;
}
@@ -69,13 +75,26 @@ static lua_Number LoadNumber(LoadState* S)
{
lua_Number x;
LoadVar(S,x);
return x;
if (!S->flip)
return x;
#ifdef LUA_NUMBER_DOUBLE
union { double d; struct { uint32_t l; uint32_t h; } i; } u;
u.d = x;
uint32_t temp = Swap32(u.i.l);
u.i.l = Swap32(u.i.h);
u.i.h = temp;
return u.d;
#else
union { float f; uint32_t i } u;
u.f = x;
u.i = Swap32(u.i);
return u.f;
#endif
}
static TString* LoadString(LoadState* S)
{
size_t size;
LoadVar(S,size);
int size=LoadInt(S);
if (size==0)
return NULL;
else
@@ -92,6 +111,11 @@ static void LoadCode(LoadState* S, Proto* f)
f->code=luaM_newvector(S->L,n,Instruction);
f->sizecode=n;
LoadVector(S,f->code,n,sizeof(Instruction));
if (!S->flip)
return;
int i;
for (i=0; i<n; i++)
f->code[i] = Swap32(f->code[i]);
}
static Proto* LoadFunction(LoadState* S, TString* p);
@@ -137,9 +161,12 @@ static void LoadDebug(LoadState* S, Proto* f)
{
int i,n;
n=LoadInt(S);
f->lineinfo=luaM_newvector(S->L,n,int);
f->lineinfo=luaM_newvector(S->L,n,uint32_t);
f->sizelineinfo=n;
LoadVector(S,f->lineinfo,n,sizeof(int));
LoadVector(S,f->lineinfo,n,sizeof(uint32_t));
if (S->flip)
for (i=0; i<n; i++)
f->lineinfo[i] = Swap32(f->lineinfo[i]);
n=LoadInt(S);
f->locvars=luaM_newvector(S->L,n,LocVar);
f->sizelocvars=n;
@@ -176,12 +203,20 @@ static Proto* LoadFunction(LoadState* S, TString* p)
return f;
}
/* LUA_SIGNATURE = sizeof(LUA_SIGnATURE)-1
* LUAC_VERSION = 1
* LUAC_FORMAT = 1
* endian = 1 */
#define ENDIAN_OFFSET (sizeof(LUA_SIGNATURE)-1+2)
static void LoadHeader(LoadState* S)
{
char h[LUAC_HEADERSIZE];
char s[LUAC_HEADERSIZE];
luaU_header(h);
LoadBlock(S,s,LUAC_HEADERSIZE);
S->flip = h[ENDIAN_OFFSET] != s[ENDIAN_OFFSET];
s[ENDIAN_OFFSET] = h[ENDIAN_OFFSET];
IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
}
@@ -200,6 +235,7 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
S.L=L;
S.Z=Z;
S.b=buff;
S.flip=0;
LoadHeader(&S);
return LoadFunction(&S,luaS_newliteral(L,"=?"));
}
+2 -2
View File
@@ -27,8 +27,8 @@ LUAI_FUNC void luaU_print (const Proto* f, int full);
/* for header of binary files -- this is Lua 5.1 */
#define LUAC_VERSION 0x51
/* for header of binary files -- this is the official format */
#define LUAC_FORMAT 0
/* for header of binary files -- this is StepMania's own format */
#define LUAC_FORMAT 'S'
/* size of header of binary files */
#define LUAC_HEADERSIZE 12