First cut at updating compiler and runtime to handle lua 5.2 bytecodes. Able to compile and execute "hello, world" lua script.

This commit is contained in:
James Roseborough
2012-08-23 04:40:40 +00:00
parent 659a20a999
commit f7e17c588e
19 changed files with 792 additions and 557 deletions

View File

@@ -196,7 +196,7 @@ public class JavaBuilder {
main = new InstructionList();
// create the fields
for ( int i=0; i<p.nups; i++ ) {
for ( int i=0; i<p.upvalues.length; i++ ) {
boolean isrw = pi.isReadWriteUpvalue( pi.upvals[i] );
Type uptype = isrw? (Type) TYPE_LOCALUPVALUE: (Type) TYPE_LUAVALUE;
FieldGen fg = new FieldGen(0, uptype, upvalueName(i), cp);

View File

@@ -122,20 +122,6 @@ public class JavaGen {
builder.storeLocal( pc, a );
break;
case Lua.OP_GETGLOBAL: /* A Bx R(A):= Gbl[Kst(Bx)] */
builder.loadEnv();
builder.loadConstant( p.k[bx] );
builder.getTable();
builder.storeLocal( pc, a );
break;
case Lua.OP_SETGLOBAL: /* A Bx Gbl[Kst(Bx)]:= R(A) */
builder.loadEnv();
builder.loadConstant( p.k[bx] );
builder.loadLocal( pc, a );
builder.setTable();
break;
case Lua.OP_LOADNIL: /* A B R(A):= ...:= R(B):= nil */
builder.loadNil();
for ( ; a<=b; a++ ) {
@@ -145,6 +131,13 @@ public class JavaGen {
}
break;
case Lua.OP_GETTABUP: /* A B C R(A) := UpValue[B][RK(C)] */
builder.loadUpvalue( b );
loadLocalOrConstant( p, builder, pc, c );
builder.getTable();
builder.storeLocal( pc, a );
break;
case Lua.OP_GETTABLE: /* A B C R(A):= R(B)[RK(C)] */
builder.loadLocal( pc, b );
loadLocalOrConstant( p, builder, pc, c );
@@ -152,6 +145,13 @@ public class JavaGen {
builder.storeLocal( pc, a );
break;
case Lua.OP_SETTABUP: /* A B C UpValue[A][RK(B)] := RK(C) */
builder.loadUpvalue( a );
loadLocalOrConstant( p, builder, pc, b );
loadLocalOrConstant( p, builder, pc, c );
builder.setTable();
break;
case Lua.OP_SETTABLE: /* A B C R(A)[RK(B)]:= RK(C) */
builder.loadLocal( pc, a );
loadLocalOrConstant( p, builder, pc, b );
@@ -342,6 +342,9 @@ public class JavaGen {
builder.addBranch(pc, JavaBuilder.BRANCH_IFNE, pc+1+sbx);
break;
case Lua.OP_TFORCALL: /* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
throw new RuntimeException("Unimplemented OP_TFORCALL");
case Lua.OP_TFORLOOP: /*
* A C R(A+3), ... ,R(A+2+C):= R(A)(R(A+1),
* R(A+2)): if R(A+3) ~= nil then R(A+2)=R(A+3)
@@ -395,14 +398,11 @@ public class JavaGen {
}
break;
case Lua.OP_CLOSE: /* A close all variables in the stack up to (>=) R(A)*/
break;
case Lua.OP_CLOSURE: /* A Bx R(A):= closure(KPROTO[Bx], R(A), ... ,R(A+n)) */
{
Prototype newp = p.p[bx];
String protoname = closureName(classname, bx);
int nup = newp.nups;
int nup = newp.upvalues.length;
builder.closureCreate( protoname );
if ( nup > 0 )
builder.dup();

View File

@@ -164,7 +164,7 @@ public class ProtoInfo {
case Lua.OP_LOADK:/* A Bx R(A) := Kst(Bx) */
case Lua.OP_LOADBOOL:/* A B C R(A) := (Bool)B; if (C) pc++ */
case Lua.OP_GETUPVAL: /* A B R(A) := UpValue[B] */
case Lua.OP_GETGLOBAL: /* A Bx R(A) := Gbl[Kst(Bx)] */
case Lua.OP_GETTABUP: /* A B C R(A) := UpValue[B][RK(C)] */
case Lua.OP_NEWTABLE: /* A B C R(A) := {} (size = B,C) */
a = Lua.GETARG_A( ins );
v[a][pc] = new VarInfo(a,pc);
@@ -203,6 +203,13 @@ public class ProtoInfo {
if (!Lua.ISK(b)) v[b][pc].isreferenced = true;
if (!Lua.ISK(c)) v[c][pc].isreferenced = true;
break;
case Lua.OP_SETTABUP: /* A B C UpValue[A][RK(B)] := RK(C) */
b = Lua.GETARG_B( ins );
c = Lua.GETARG_C( ins );
if (!Lua.ISK(b)) v[b][pc].isreferenced = true;
if (!Lua.ISK(c)) v[c][pc].isreferenced = true;
break;
case Lua.OP_CONCAT: /* A B C R(A) := R(B).. ... ..R(C) */
a = Lua.GETARG_A( ins );
@@ -295,6 +302,7 @@ public class ProtoInfo {
v[a+i][pc].isreferenced = true;
break;
case Lua.OP_TFORCALL: /* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
case Lua.OP_TFORLOOP: /* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */
a = Lua.GETARG_A( ins );
@@ -311,7 +319,7 @@ public class ProtoInfo {
case Lua.OP_CLOSURE: /* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */
a = Lua.GETARG_A( ins );
b = Lua.GETARG_Bx( ins );
nups = prototype.p[b].nups;
nups = prototype.p[b].upvalues.length;
for ( int k=1; k<=nups; ++k ) {
int i = prototype.code[pc+k];
if ( (i&4) == 0 ) {
@@ -324,12 +332,7 @@ public class ProtoInfo {
propogateVars( v, pc, pc+k );
pc += nups;
break;
case Lua.OP_CLOSE: /* A close all variables in the stack up to (>=) R(A)*/
a = Lua.GETARG_A( ins );
for ( ; a<m; a++ )
v[a][pc] = VarInfo.INVALID;
break;
case Lua.OP_SETLIST: /* A B C R(A)[(C-1)*FPF+i]:= R(A+i), 1 <= i <= B */
a = Lua.GETARG_A( ins );
b = Lua.GETARG_B( ins );
@@ -338,7 +341,6 @@ public class ProtoInfo {
v[a+i][pc].isreferenced = true;
break;
case Lua.OP_SETGLOBAL: /* A Bx Gbl[Kst(Bx)]:= R(A) */
case Lua.OP_SETUPVAL: /* A B UpValue[B]:= R(A) */
case Lua.OP_TEST: /* A C if not (R(A) <=> C) then pc++ */
a = Lua.GETARG_A( ins );
@@ -402,9 +404,9 @@ public class ProtoInfo {
if ( Lua.GET_OPCODE(code[pc]) == Lua.OP_CLOSURE ) {
int bx = Lua.GETARG_Bx(code[pc]);
Prototype newp = prototype.p[bx];
UpvalInfo[] newu = newp.nups>0? new UpvalInfo[newp.nups]: null;
UpvalInfo[] newu = new UpvalInfo[newp.upvalues.length];
String newname = name + "$" + bx;
for ( int j=0; j<newp.nups; ++j ) {
for ( int j=0; j<newp.upvalues.length; ++j ) {
int i = code[++pc];
int b = Lua.GETARG_B(i);
newu[j] = (i&4) != 0? upvals[b]: findOpenUp(pc,b);