lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi.

First of all I would like to apologise for not searching the archive properly before posting my question about cross compiling. Having now done my homework I would like to answer my own question:

The question:
Is it possible to cross compile Lua code on a 32 bit platform to be run on a 16 bit platform?

The answer:
The luac man page says no, but the authors of Lua say yes... but only if you are prepared to beat lua with a big stick. Those might not have been their exact words but you get the idea. The changes can either be made in ldump.c on the source machine or in lundump.c on the target machine. Seeing as I want to share bytecode between PocketPC (32 bit) and PalmOS (16 bit) and had already done quite a lot of persuasion on the PalmOS port I decided to edit lundump.c in the PalmOS port of Lua (coming soon to a source forge project near you soon). LHF says several helpful things in his post here:

   http://lua-users.org/lists/lua-l/2003-05/msg00357.html

In summary you have to update LoadInt and LoadLines in addition to making sure that the size of lua_Number and Instruction match on target and source platforms. In addition to this you may also have to edit LoadHeader to trick it into accepting your 16 bit int where it was expecting a 32 bit int. For the sake of completeness I have included the edited functions below and hope this information is useful to someone. There appear to be a number of people interested in getting a proper PalmOS Lua port off the ground so hopefully we will take some of the PalmOS specific issues off this list to a more appropriate forum.

Many thanks for your patience.

-haemish

----------8<----------8<----------8<----------8<----------
static void LoadHeader (LoadState* S)
{
int version;
lua_Number x,tx=TEST_NUMBER;
LoadSignature(S);
version=LoadByte(S);
if (version>VERSION)
 luaG_runerror(S->L,"%s too new: "
   "read version %d.%d; expected at most %d.%d",
   S->name,V(version),V(VERSION));
if (version<VERSION0)                /* check last major change */
 luaG_runerror(S->L,"%s too old: "
   "read version %d.%d; expected at least %d.%d",
   S->name,V(version),V(VERSION0));
S->swap=(luaU_endianness()!=LoadByte(S));    /* need to swap bytes? */
/* JFRG
TESTSIZE(sizeof(int),"int");
*/
TESTSIZE(4,"int");
TESTSIZE(sizeof(size_t), "size_t");
TESTSIZE(sizeof(Instruction), "Instruction");
TESTSIZE(SIZE_OP, "OP");
TESTSIZE(SIZE_A, "A");
TESTSIZE(SIZE_B, "B");
TESTSIZE(SIZE_C, "C");
TESTSIZE(sizeof(lua_Number), "number");
x=LoadNumber(S);
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
 luaG_runerror(S->L,"unknown number format in %s",S->name);
}

static void LoadLines (LoadState* S, Proto* f)
{
/* JFRG
int size=LoadInt(S);
f->lineinfo=luaM_newvector(S->L,size,int);
f->sizelineinfo=size;
LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo));
*/
int i, size=LoadInt(S);
f->lineinfo=luaM_newvector(S->L,size,int);
f->sizelineinfo=size;
for (i=0; i<size; i++)
{
    f->lineinfo[i] = LoadInt(S);
}
}

static int LoadInt (LoadState* S)
{
/* JFRG
int x;
LoadBlock(S,&x,sizeof(x));
if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name);
return x;
*/
long x;
LoadBlock(S,&x,sizeof(x));
if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name);
return (int)x;
}