Improve bytecode generation.

This commit is contained in:
James Roseborough
2010-08-12 00:49:15 +00:00
parent 267e89adef
commit 386e89aedf
5 changed files with 446 additions and 410 deletions

View File

@@ -3,7 +3,6 @@
*/ */
package org.luaj.vm2.luajc; package org.luaj.vm2.luajc;
import java.util.Hashtable;
import java.util.Vector; import java.util.Vector;
import org.luaj.vm2.Lua; import org.luaj.vm2.Lua;
@@ -13,6 +12,7 @@ public class BasicBlock {
int pc0,pc1; // range of program counter values for the block int pc0,pc1; // range of program counter values for the block
BasicBlock[] prev; // previous basic blocks (0-n of these) BasicBlock[] prev; // previous basic blocks (0-n of these)
BasicBlock[] next; // next basic blocks (0, 1, or 2 of these) BasicBlock[] next; // next basic blocks (0, 1, or 2 of these)
boolean islive; // true if this block is used
public BasicBlock(Prototype p, int pc0) { public BasicBlock(Prototype p, int pc0) {
this.pc0 = this.pc1 = pc0; this.pc0 = this.pc1 = pc0;
@@ -150,36 +150,31 @@ public class BasicBlock {
visitor.visitBranch( i, i+1 ); visitor.visitBranch( i, i+1 );
} }
} }
public static BasicBlock[] sortDepthFirst(BasicBlock[] blocks) { public static BasicBlock[] findLiveBlocks(BasicBlock[] blocks) {
Vector list = new Vector(); // add reachable blocks
Hashtable seen = new Hashtable(); Vector next = new Vector ();
Vector next = new Vector();
BasicBlock b0,b1,b[];
next.addElement( blocks[0] ); next.addElement( blocks[0] );
seen.put( blocks[0], Boolean.TRUE ); while ( ! next.isEmpty() ) {
while ( (b = toBasicBlockArray(next)) != null ) { BasicBlock b = (BasicBlock) next.elementAt(0);
next.clear(); next.removeElementAt(0);
for ( int i=0; i<b.length; i++ ) { if ( ! b.islive ) {
list.addElement( b0 = b[i] ); b.islive = true;
for ( int k=0, n=b0.next!=null? b0.next.length: 0; k<n; k++ ) { for ( int i=0, n=b.next!=null? b.next.length: 0; i<n; i++ )
if ( seen.get( b1 = b0.next[k] ) == null ) { if ( ! b.next[i].islive )
seen.put( b1, Boolean.TRUE ); next.addElement( b.next[i] );
next.addElement( b1 );
}
}
} }
} }
return toBasicBlockArray(list);
} // create list in natural order
Vector list = new Vector();
private static BasicBlock[] toBasicBlockArray(Vector list) { for ( int i=0; i<blocks.length; i=blocks[i].pc1+1 )
if ( list.size() <= 0 ) if ( blocks[i].islive )
return null; list.addElement(blocks[i]);
// convert to array
BasicBlock[] array = new BasicBlock[list.size()]; BasicBlock[] array = new BasicBlock[list.size()];
list.copyInto(array); list.copyInto(array);
return array; return array;
} }
} }

View File

@@ -66,393 +66,375 @@ public class JavaGen {
Prototype p = pi.prototype; Prototype p = pi.prototype;
int vresultbase = -1; int vresultbase = -1;
for ( int pc=0, n=p.code.length; pc<n; pc++ ) { for ( int bi=0; bi<pi.blocklist.length; bi++ ) {
BasicBlock b0 = pi.blocklist[bi];
// skip dead blocks
BasicBlock b0 = pi.blocks[pc]; // convert upvalues that are phi-variables
if ( b0.pc0>0 && b0.prev == null ) { for ( int slot=0; slot<p.maxstacksize; slot++ ) {
pc = b0.pc1; int pc = b0.pc0;
continue; boolean c = pi.isUpvalueCreate(pc, slot);
if ( c && pi.vars[slot][pc].isPhiVar() )
builder.convertToUpvalue(pc, slot);
} }
int pc0 = pc; // closure changes pc for ( int pc=b0.pc0; pc<=b0.pc1; pc++ ) {
int ins = p.code[pc];
final int o = Lua.GET_OPCODE(ins);
int a = Lua.GETARG_A(ins);
int b = Lua.GETARG_B(ins);
int bx = Lua.GETARG_Bx(ins);
int sbx = Lua.GETARG_sBx(ins);
int c = Lua.GETARG_C(ins);
switch ( o ) {
case Lua.OP_GETUPVAL: /* A B R(A):= UpValue[B] */
builder.loadUpvalue( b );
builder.storeLocal( pc, a );
break;
case Lua.OP_SETUPVAL: /* A B UpValue[B]:= R(A) */
builder.storeUpvalue( pc, b, a );
break;
case Lua.OP_NEWTABLE: /* A B C R(A):= {} (size = B,C) */
builder.newTable( b, c );
builder.storeLocal( pc, a );
break;
case Lua.OP_MOVE:/* A B R(A):= R(B) */
builder.loadLocal( pc, b );
builder.storeLocal( pc, a );
break;
case Lua.OP_UNM: /* A B R(A):= -R(B) */ int pc0 = pc; // closure changes pc
case Lua.OP_NOT: /* A B R(A):= not R(B) */ int ins = p.code[pc];
case Lua.OP_LEN: /* A B R(A):= length of R(B) */ final int o = Lua.GET_OPCODE(ins);
builder.loadLocal( pc, b ); int a = Lua.GETARG_A(ins);
builder.unaryop( o ); int b = Lua.GETARG_B(ins);
builder.storeLocal( pc, a ); int bx = Lua.GETARG_Bx(ins);
break; int sbx = Lua.GETARG_sBx(ins);
int c = Lua.GETARG_C(ins);
case Lua.OP_LOADK:/* A Bx R(A):= Kst(Bx) */
builder.loadConstant( p.k[bx] ); switch ( o ) {
builder.storeLocal( pc, a ); case Lua.OP_GETUPVAL: /* A B R(A):= UpValue[B] */
break; builder.loadUpvalue( b );
builder.storeLocal( pc, a );
case Lua.OP_GETGLOBAL: /* A Bx R(A):= Gbl[Kst(Bx)] */ break;
builder.loadEnv();
builder.loadConstant( p.k[bx] ); case Lua.OP_SETUPVAL: /* A B UpValue[B]:= R(A) */
builder.getTable(); builder.storeUpvalue( pc, b, a );
builder.storeLocal( pc, a ); break;
break;
case Lua.OP_NEWTABLE: /* A B C R(A):= {} (size = B,C) */
case Lua.OP_SETGLOBAL: /* A Bx Gbl[Kst(Bx)]:= R(A) */ builder.newTable( b, c );
builder.loadEnv(); builder.storeLocal( pc, a );
builder.loadConstant( p.k[bx] ); break;
builder.loadLocal( pc, a );
builder.setTable(); case Lua.OP_MOVE:/* A B R(A):= R(B) */
break; builder.loadLocal( pc, b );
case Lua.OP_LOADNIL: /* A B R(A):= ...:= R(B):= nil */
builder.loadNil();
for ( ; a<=b; a++ ) {
if ( a < b )
builder.dup();
builder.storeLocal( pc, a ); 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 );
builder.getTable();
builder.storeLocal( pc, a );
break;
case Lua.OP_SETTABLE: /* A B C R(A)[RK(B)]:= RK(C) */
builder.loadLocal( pc, a );
loadLocalOrConstant( p, builder, pc, b );
loadLocalOrConstant( p, builder, pc, c );
builder.setTable();
break;
case Lua.OP_ADD: /* A B C R(A):= RK(B) + RK(C) */
case Lua.OP_SUB: /* A B C R(A):= RK(B) - RK(C) */
case Lua.OP_MUL: /* A B C R(A):= RK(B) * RK(C) */
case Lua.OP_DIV: /* A B C R(A):= RK(B) / RK(C) */
case Lua.OP_MOD: /* A B C R(A):= RK(B) % RK(C) */
case Lua.OP_POW: /* A B C R(A):= RK(B) ^ RK(C) */
loadLocalOrConstant( p, builder, pc, b );
loadLocalOrConstant( p, builder, pc, c );
builder.binaryop( o );
builder.storeLocal( pc, a );
break;
case Lua.OP_SELF: /* A B C R(A+1):= R(B): R(A):= R(B)[RK(C)] */
builder.loadLocal(pc,b);
builder.dup();
builder.storeLocal(pc, a+1);
loadLocalOrConstant( p, builder, pc, c );
builder.getTable();
builder.storeLocal(pc, a);
break;
case Lua.OP_CONCAT: /* A B C R(A):= R(B).. ... ..R(C) */
builder.newBuffer();
while ( b<=c ) {
builder.loadLocal(pc, b++);
builder.appendBuffer();
}
builder.tostring();
builder.storeLocal(pc, a);
break;
case Lua.OP_LOADBOOL:/* A B C R(A):= (Bool)B: if (C) pc++ */
builder.loadBoolean( b!=0 );
builder.storeLocal( pc, a );
if ( c!=0 )
builder.addBranch(pc, JavaBuilder.BRANCH_GOTO, pc+2);
break;
case Lua.OP_JMP: /* sBx pc+=sBx */
builder.addBranch(pc, JavaBuilder.BRANCH_GOTO, pc+1+sbx);
break;
case Lua.OP_EQ: /* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
case Lua.OP_LT: /* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
case Lua.OP_LE: /* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
loadLocalOrConstant( p, builder, pc, b );
loadLocalOrConstant( p, builder, pc, c );
builder.compareop(o);
builder.addBranch(pc, (a!=0? JavaBuilder.BRANCH_IFEQ: JavaBuilder.BRANCH_IFNE), pc+2);
break;
case Lua.OP_TEST: /* A C if not (R(A) <=> C) then pc++ */
builder.loadLocal( pc, a );
builder.toBoolean();
builder.addBranch(pc, (c!=0? JavaBuilder.BRANCH_IFEQ: JavaBuilder.BRANCH_IFNE), pc+2);
break;
case Lua.OP_TESTSET: /* A B C if (R(B) <=> C) then R(A):= R(B) else pc++ */
builder.loadLocal( pc, b );
builder.toBoolean();
builder.addBranch(pc, (c!=0? JavaBuilder.BRANCH_IFEQ: JavaBuilder.BRANCH_IFNE), pc+2);
builder.loadLocal( pc, b );
builder.storeLocal( pc, a );
break;
case Lua.OP_CALL: /* A B C R(A), ... ,R(A+C-2):= R(A)(R(A+1), ... ,R(A+B-1)) */
// load function
builder.loadLocal(pc, a);
// load args
int narg = b - 1;
switch ( narg ) {
case 0: case 1: case 2: case 3:
for ( int i=1; i<b; i++ )
builder.loadLocal(pc, a+i);
break; break;
default: // fixed arg count > 3
builder.newVarargs( pc, a+1, b-1 ); case Lua.OP_UNM: /* A B R(A):= -R(B) */
narg = -1; case Lua.OP_NOT: /* A B R(A):= not R(B) */
case Lua.OP_LEN: /* A B R(A):= length of R(B) */
builder.loadLocal( pc, b );
builder.unaryop( o );
builder.storeLocal( pc, a );
break; break;
case -1: // prev vararg result
loadVarargResults( builder, pc, a+1, vresultbase ); case Lua.OP_LOADK:/* A Bx R(A):= Kst(Bx) */
narg = -1; builder.loadConstant( p.k[bx] );
builder.storeLocal( pc, a );
break; break;
}
case Lua.OP_GETGLOBAL: /* A Bx R(A):= Gbl[Kst(Bx)] */
// call or invoke builder.loadEnv();
boolean useinvoke = narg<0 || c<1 || c>2; builder.loadConstant( p.k[bx] );
if ( useinvoke ) builder.getTable();
builder.invoke(narg); builder.storeLocal( pc, a );
else
builder.call(narg);
// handle results
switch ( c ) {
case 1:
builder.pop();
break; break;
case 2:
if ( useinvoke ) case Lua.OP_SETGLOBAL: /* A Bx Gbl[Kst(Bx)]:= R(A) */
builder.arg( 1 ); 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++ ) {
if ( a < b )
builder.dup();
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 );
builder.getTable();
builder.storeLocal( pc, a );
break;
case Lua.OP_SETTABLE: /* A B C R(A)[RK(B)]:= RK(C) */
builder.loadLocal( pc, a );
loadLocalOrConstant( p, builder, pc, b );
loadLocalOrConstant( p, builder, pc, c );
builder.setTable();
break;
case Lua.OP_ADD: /* A B C R(A):= RK(B) + RK(C) */
case Lua.OP_SUB: /* A B C R(A):= RK(B) - RK(C) */
case Lua.OP_MUL: /* A B C R(A):= RK(B) * RK(C) */
case Lua.OP_DIV: /* A B C R(A):= RK(B) / RK(C) */
case Lua.OP_MOD: /* A B C R(A):= RK(B) % RK(C) */
case Lua.OP_POW: /* A B C R(A):= RK(B) ^ RK(C) */
loadLocalOrConstant( p, builder, pc, b );
loadLocalOrConstant( p, builder, pc, c );
builder.binaryop( o );
builder.storeLocal( pc, a );
break;
case Lua.OP_SELF: /* A B C R(A+1):= R(B): R(A):= R(B)[RK(C)] */
builder.loadLocal(pc,b);
builder.dup();
builder.storeLocal(pc, a+1);
loadLocalOrConstant( p, builder, pc, c );
builder.getTable();
builder.storeLocal(pc, a); builder.storeLocal(pc, a);
break; break;
default: // fixed result count - unpack args
for ( int i=1; i<c; i++ ) { case Lua.OP_CONCAT: /* A B C R(A):= R(B).. ... ..R(C) */
if ( i+1 < c ) builder.newBuffer();
builder.dup(); while ( b<=c ) {
builder.arg( i ); builder.loadLocal(pc, b++);
builder.storeLocal(pc, a+i-1); builder.appendBuffer();
}
builder.tostring();
builder.storeLocal(pc, a);
break;
case Lua.OP_LOADBOOL:/* A B C R(A):= (Bool)B: if (C) pc++ */
builder.loadBoolean( b!=0 );
builder.storeLocal( pc, a );
if ( c!=0 )
builder.addBranch(pc, JavaBuilder.BRANCH_GOTO, pc+2);
break;
case Lua.OP_JMP: /* sBx pc+=sBx */
builder.addBranch(pc, JavaBuilder.BRANCH_GOTO, pc+1+sbx);
break;
case Lua.OP_EQ: /* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
case Lua.OP_LT: /* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
case Lua.OP_LE: /* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
loadLocalOrConstant( p, builder, pc, b );
loadLocalOrConstant( p, builder, pc, c );
builder.compareop(o);
builder.addBranch(pc, (a!=0? JavaBuilder.BRANCH_IFEQ: JavaBuilder.BRANCH_IFNE), pc+2);
break;
case Lua.OP_TEST: /* A C if not (R(A) <=> C) then pc++ */
builder.loadLocal( pc, a );
builder.toBoolean();
builder.addBranch(pc, (c!=0? JavaBuilder.BRANCH_IFEQ: JavaBuilder.BRANCH_IFNE), pc+2);
break;
case Lua.OP_TESTSET: /* A B C if (R(B) <=> C) then R(A):= R(B) else pc++ */
builder.loadLocal( pc, b );
builder.toBoolean();
builder.addBranch(pc, (c!=0? JavaBuilder.BRANCH_IFEQ: JavaBuilder.BRANCH_IFNE), pc+2);
builder.loadLocal( pc, b );
builder.storeLocal( pc, a );
break;
case Lua.OP_CALL: /* A B C R(A), ... ,R(A+C-2):= R(A)(R(A+1), ... ,R(A+B-1)) */
// load function
builder.loadLocal(pc, a);
// load args
int narg = b - 1;
switch ( narg ) {
case 0: case 1: case 2: case 3:
for ( int i=1; i<b; i++ )
builder.loadLocal(pc, a+i);
break;
default: // fixed arg count > 3
builder.newVarargs( pc, a+1, b-1 );
narg = -1;
break;
case -1: // prev vararg result
loadVarargResults( builder, pc, a+1, vresultbase );
narg = -1;
break;
}
// call or invoke
boolean useinvoke = narg<0 || c<1 || c>2;
if ( useinvoke )
builder.invoke(narg);
else
builder.call(narg);
// handle results
switch ( c ) {
case 1:
builder.pop();
break;
case 2:
if ( useinvoke )
builder.arg( 1 );
builder.storeLocal(pc, a);
break;
default: // fixed result count - unpack args
for ( int i=1; i<c; i++ ) {
if ( i+1 < c )
builder.dup();
builder.arg( i );
builder.storeLocal(pc, a+i-1);
}
break;
case 0: // vararg result
vresultbase = a;
builder.storeVarresult();
break;
} }
break; break;
case 0: // vararg result
vresultbase = a; case Lua.OP_TAILCALL: /* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
builder.storeVarresult();
break; // load function
} builder.loadLocal(pc, a);
break;
// load args
case Lua.OP_TAILCALL: /* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
// load function
builder.loadLocal(pc, a);
// load args
switch ( b ) {
case 1:
builder.loadNone();
break;
case 2:
builder.loadLocal(pc, a+1);
break;
default: // fixed arg count > 1
builder.newVarargs( pc, a+1, b-1 );
break;
case 0: // prev vararg result
loadVarargResults( builder, pc, a+1, vresultbase );
break;
}
builder.newTailcallVarargs();
builder.areturn();
break;
case Lua.OP_RETURN: /* A B return R(A), ... ,R(A+B-2) (see note) */
if ( c == 1 ) {
builder.loadNone();
} else {
switch ( b ) { switch ( b ) {
case 0: loadVarargResults( builder, pc, a, vresultbase ); break; case 1:
case 1: builder.loadNone(); break; builder.loadNone();
case 2: builder.loadLocal(pc, a); break; break;
default: builder.newVarargs(pc, a, b-1); break; case 2:
builder.loadLocal(pc, a+1);
break;
default: // fixed arg count > 1
builder.newVarargs( pc, a+1, b-1 );
break;
case 0: // prev vararg result
loadVarargResults( builder, pc, a+1, vresultbase );
break;
} }
} builder.newTailcallVarargs();
builder.areturn(); builder.areturn();
break; break;
case Lua.OP_FORPREP: /* A sBx R(A)-=R(A+2): pc+=sBx */ case Lua.OP_RETURN: /* A B return R(A), ... ,R(A+B-2) (see note) */
builder.loadLocal(pc, a); if ( c == 1 ) {
builder.loadLocal(pc, a+2); builder.loadNone();
builder.binaryop( Lua.OP_SUB );
builder.storeLocal(pc, a);
builder.addBranch(pc, JavaBuilder.BRANCH_GOTO, pc+1+sbx);
break;
case Lua.OP_FORLOOP: /* A sBx R(A)+=R(A+2): if R(A) <?= R(A+1) then { pc+=sBx: R(A+3)=R(A) }*/
builder.loadLocal(pc, a);
builder.loadLocal(pc, a+2);
builder.binaryop( Lua.OP_ADD );
builder.dup();
builder.dup();
builder.storeLocal(pc, a);
builder.storeLocal(pc, a+3);
builder.loadLocal(pc, a+1); // limit
builder.loadLocal(pc, a+2); // step
builder.testForLoop();
builder.addBranch(pc, JavaBuilder.BRANCH_IFNE, pc+1+sbx);
break;
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++
*/
// v = stack[a].invoke(varargsOf(stack[a+1],stack[a+2]));
// if ( (o=v.arg1()).isnil() )
// ++pc;
builder.loadLocal(pc, a);
builder.loadLocal(pc, a+1);
builder.loadLocal(pc, a+2);
builder.invoke(2); // varresult on stack
builder.dup();
builder.storeVarresult();
builder.arg( 1 );
builder.isNil();
builder.addBranch(pc, JavaBuilder.BRANCH_IFNE, pc+2);
// a[2] = a[3] = v[1], leave varargs on stack
builder.createUpvalues(pc, a+3, c);
builder.loadVarresult();
if (c>=2)
builder.dup();
builder.arg( 1 );
builder.dup();
builder.storeLocal(pc, a+2);
builder.storeLocal(pc, a+3);
// v[2]..v[c], use varargs from stack
for ( int j=2; j<=c; j++ ) {
if ( j<c )
builder.dup();
builder.arg( j );
builder.storeLocal(pc, a+2+j);
}
break;
case Lua.OP_SETLIST: /* A B C R(A)[(C-1)*FPF+i]:= R(A+i), 1 <= i <= B */
int index0 = (c-1)*Lua.LFIELDS_PER_FLUSH + 1;
builder.loadLocal( pc, a );
if ( b == 0 ) {
int nstack = vresultbase - (a+1);
if ( nstack > 0 ) {
builder.setlistStack( pc, a+1, index0, nstack );
index0 += nstack;
}
builder.setlistVarargs( index0, vresultbase );
} else {
builder.setlistStack( pc, a+1, index0, b );
builder.pop();
}
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)) */
{
/*
String protoname = closureName(classname, bx);
int nups = p.p[bx].nups;
for ( int k=1; k<=nups; ++k ) {
builder.dup();
int i = p.code[pc+k];
b = Lua.GETARG_B(i);
if ( (i&4) == 0 ) {
builder.closureInitUpvalueFromUpvalue( protoname, k-1, b );
} else { } else {
builder.closureInitUpvalueFromLocal( protoname, k-1, pc, b ); switch ( b ) {
} case 0: loadVarargResults( builder, pc, a, vresultbase ); break;
} case 1: builder.loadNone(); break;
v[a][pc] = new VarInfo(a,pc); case 2: builder.loadLocal(pc, a); break;
for ( int k=1; k<=nups; ++k ) { default: builder.newVarargs(pc, a, b-1); break;
for ( int j=0; j<m; j++ )
v[j][pc+k] = v[j][pc];
}
pc += nups;
/*/
Prototype newp = p.p[bx];
String protoname = closureName(classname, bx);
int nup = newp.nups;
builder.closureCreate( protoname );
if ( nup > 0 )
builder.dup();
builder.storeLocal( pc, a );
if ( nup > 0 ) {
for ( int up=0; up<nup; ++up ) {
if ( up+1 < nup )
builder.dup();
ins = p.code[pc+up+1];
b = Lua.GETARG_B(ins);
if ( (ins&4) != 0 ) {
builder.closureInitUpvalueFromUpvalue( protoname, up, b );
} else {
builder.closureInitUpvalueFromLocal( protoname, up, pc, b );
} }
} }
pc += nup; builder.areturn();
} break;
//*/
break; case Lua.OP_FORPREP: /* A sBx R(A)-=R(A+2): pc+=sBx */
} builder.loadLocal(pc, a);
case Lua.OP_VARARG: /* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ builder.loadLocal(pc, a+2);
if ( b == 0 ) { builder.binaryop( Lua.OP_SUB );
builder.loadVarargs(); builder.storeLocal(pc, a);
builder.storeVarresult(); builder.addBranch(pc, JavaBuilder.BRANCH_GOTO, pc+1+sbx);
vresultbase = a; break;
} else {
for ( int i=1; i<b; ++a, ++i ) { case Lua.OP_FORLOOP: /* A sBx R(A)+=R(A+2): if R(A) <?= R(A+1) then { pc+=sBx: R(A+3)=R(A) }*/
builder.loadVarargs( i ); builder.loadLocal(pc, a);
builder.storeLocal(pc, a); builder.loadLocal(pc, a+2);
builder.binaryop( Lua.OP_ADD );
builder.dup();
builder.dup();
builder.storeLocal(pc, a);
builder.storeLocal(pc, a+3);
builder.loadLocal(pc, a+1); // limit
builder.loadLocal(pc, a+2); // step
builder.testForLoop();
builder.addBranch(pc, JavaBuilder.BRANCH_IFNE, pc+1+sbx);
break;
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++
*/
// v = stack[a].invoke(varargsOf(stack[a+1],stack[a+2]));
// if ( (o=v.arg1()).isnil() )
// ++pc;
builder.loadLocal(pc, a);
builder.loadLocal(pc, a+1);
builder.loadLocal(pc, a+2);
builder.invoke(2); // varresult on stack
builder.dup();
builder.storeVarresult();
builder.arg( 1 );
builder.isNil();
builder.addBranch(pc, JavaBuilder.BRANCH_IFNE, pc+2);
// a[2] = a[3] = v[1], leave varargs on stack
builder.createUpvalues(pc, a+3, c);
builder.loadVarresult();
if (c>=2)
builder.dup();
builder.arg( 1 );
builder.dup();
builder.storeLocal(pc, a+2);
builder.storeLocal(pc, a+3);
// v[2]..v[c], use varargs from stack
for ( int j=2; j<=c; j++ ) {
if ( j<c )
builder.dup();
builder.arg( j );
builder.storeLocal(pc, a+2+j);
} }
break;
case Lua.OP_SETLIST: /* A B C R(A)[(C-1)*FPF+i]:= R(A+i), 1 <= i <= B */
int index0 = (c-1)*Lua.LFIELDS_PER_FLUSH + 1;
builder.loadLocal( pc, a );
if ( b == 0 ) {
int nstack = vresultbase - (a+1);
if ( nstack > 0 ) {
builder.setlistStack( pc, a+1, index0, nstack );
index0 += nstack;
}
builder.setlistVarargs( index0, vresultbase );
} else {
builder.setlistStack( pc, a+1, index0, b );
builder.pop();
}
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;
builder.closureCreate( protoname );
if ( nup > 0 )
builder.dup();
builder.storeLocal( pc, a );
if ( nup > 0 ) {
for ( int up=0; up<nup; ++up ) {
if ( up+1 < nup )
builder.dup();
ins = p.code[pc+up+1];
b = Lua.GETARG_B(ins);
if ( (ins&4) != 0 ) {
builder.closureInitUpvalueFromUpvalue( protoname, up, b );
} else {
builder.closureInitUpvalueFromLocal( protoname, up, pc, b );
}
}
pc += nup;
}
break;
}
case Lua.OP_VARARG: /* A B R(A), R(A+1), ..., R(A+B-1) = vararg */
if ( b == 0 ) {
builder.loadVarargs();
builder.storeVarresult();
vresultbase = a;
} else {
for ( int i=1; i<b; ++a, ++i ) {
builder.loadVarargs( i );
builder.storeLocal(pc, a);
}
}
break;
} }
break;
// let builder process branch instructions
builder.onEndOfLuaInstruction( pc0 );
} }
// let builder process branch instructions
builder.onEndOfLuaInstruction( pc0 );
} }
} }

View File

@@ -21,7 +21,6 @@ public class ProtoInfo {
public final VarInfo[][] vars; // Each variable public final VarInfo[][] vars; // Each variable
public final UpvalInfo[] upvals; // from outer scope public final UpvalInfo[] upvals; // from outer scope
public final UpvalInfo[][] openups; // per slot, upvalues allocated by this prototype public final UpvalInfo[][] openups; // per slot, upvalues allocated by this prototype
public ProtoInfo(Prototype p, String name) { public ProtoInfo(Prototype p, String name) {
this(p,name,null); this(p,name,null);
@@ -35,8 +34,7 @@ public class ProtoInfo {
// find basic blocks // find basic blocks
this.blocks = BasicBlock.findBasicBlocks(p); this.blocks = BasicBlock.findBasicBlocks(p);
this.blocklist = BasicBlock.sortDepthFirst(blocks); this.blocklist = BasicBlock.findLiveBlocks(blocks);
// params are inputs to first block // params are inputs to first block
this.params = new VarInfo[p.maxstacksize]; this.params = new VarInfo[p.maxstacksize];
@@ -65,8 +63,9 @@ public class ProtoInfo {
sb.append( " up["+i+"]: "+upvals[i]+"\n" ); sb.append( " up["+i+"]: "+upvals[i]+"\n" );
// basic blocks // basic blocks
for ( int pc0=blocks[0].pc0; pc0<prototype.code.length; pc0=blocks[pc0].pc1+1 ) { for ( int i=0; i<blocklist.length; i++ ) {
BasicBlock b = blocks[pc0]; BasicBlock b = blocklist[i];
int pc0 = b.pc0;
sb.append( " block "+b.toString() ); sb.append( " block "+b.toString() );
appendOpenUps( sb, -1 ); appendOpenUps( sb, -1 );
@@ -143,19 +142,18 @@ public class ProtoInfo {
if ( v[slot][bp.pc1] == VarInfo.INVALID ) if ( v[slot][bp.pc1] == VarInfo.INVALID )
var = VarInfo.INVALID; var = VarInfo.INVALID;
} }
if ( var == null )
var = VarInfo.PHI(this, slot, b0.pc0);
} }
if ( var == null )
var = VarInfo.PHI(this, slot, b0.pc0);
v[slot][b0.pc0] = var; v[slot][b0.pc0] = var;
} }
// process instructions for this basic block // process instructions for this basic block
for ( int pc=b0.pc0; pc<=b0.pc1; pc++ ) { for ( int pc=b0.pc0; pc<=b0.pc1; pc++ ) {
// propogate previous for all but block boundaries // propogate previous values except at block boundaries
if ( pc>b0.pc0 ) if ( pc > b0.pc0 )
for ( int j=0; j<m; j++ ) propogateVars( v, pc-1, pc );
v[j][pc] = v[j][pc-1];
int a,b,c,nups; int a,b,c,nups;
int ins = prototype.code[pc]; int ins = prototype.code[pc];
@@ -322,10 +320,8 @@ public class ProtoInfo {
} }
} }
v[a][pc] = new VarInfo(a,pc); v[a][pc] = new VarInfo(a,pc);
for ( int k=1; k<=nups; ++k ) { for ( int k=1; k<=nups; k++ )
for ( int j=0; j<m; j++ ) propogateVars( v, pc, pc+k );
v[j][pc+k] = v[j][pc];
}
pc += nups; pc += nups;
break; break;
case Lua.OP_CLOSE: /* A close all variables in the stack up to (>=) R(A)*/ case Lua.OP_CLOSE: /* A close all variables in the stack up to (>=) R(A)*/
@@ -369,11 +365,16 @@ public class ProtoInfo {
return v; return v;
} }
private static void propogateVars(VarInfo[][] v, int pcfrom, int pcto) {
for ( int j=0, m=v.length; j<m; j++ )
v[j][pcto] = v[j][pcfrom];
}
private void replaceTrivialPhiVariables() { private void replaceTrivialPhiVariables() {
for ( int i=0; i<blocklist.length; i++ ) { for ( int i=0; i<blocklist.length; i++ ) {
BasicBlock b0 = blocklist[i]; BasicBlock b0 = blocklist[i];
for ( int slot=0; slot<prototype.maxstacksize; slot++ ) { for ( int slot=0; slot<prototype.maxstacksize; slot++ ) {
VarInfo vold = vars[slot][b0.pc1]; VarInfo vold = vars[slot][b0.pc0];
VarInfo vnew = vold.resolvePhiVariableValues(); VarInfo vnew = vold.resolvePhiVariableValues();
if ( vnew != null ) if ( vnew != null )
substituteVariable( slot, vold, vnew ); substituteVariable( slot, vold, vnew );
@@ -443,7 +444,7 @@ public class ProtoInfo {
public boolean isUpvalueRefer(int pc, int slot) { public boolean isUpvalueRefer(int pc, int slot) {
// special case when both refer and assign in same instruction // special case when both refer and assign in same instruction
if ( pc >= 0 && vars[slot][pc] != null && vars[slot][pc].pc == pc ) if ( pc > 0 && vars[slot][pc] != null && vars[slot][pc].pc == pc && vars[slot][pc-1] != null )
pc -= 1; pc -= 1;
VarInfo v = pc<0? params[slot]: vars[slot][pc]; VarInfo v = pc<0? params[slot]: vars[slot][pc];
return v != null && v.upvalue != null && v.upvalue.rw; return v != null && v.upvalue != null && v.upvalue.rw;

View File

@@ -63,6 +63,10 @@ public class VarInfo {
vars.add(this); vars.add(this);
} }
public boolean isPhiVar() {
return false;
}
private static final class PhiVarInfo extends VarInfo { private static final class PhiVarInfo extends VarInfo {
private final ProtoInfo pi; private final ProtoInfo pi;
VarInfo[] values; VarInfo[] values;
@@ -72,6 +76,10 @@ public class VarInfo {
this.pi = pi; this.pi = pi;
} }
public boolean isPhiVar() {
return true;
}
public String toString() { public String toString() {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
sb.append( super.toString() ); sb.append( super.toString() );
@@ -108,6 +116,8 @@ public class VarInfo {
protected void collectUniqueValues(Set visitedBlocks, Set vars) { protected void collectUniqueValues(Set visitedBlocks, Set vars) {
BasicBlock b = pi.blocks[pc]; BasicBlock b = pi.blocks[pc];
if ( pc == 0 )
vars.add(pi.params[slot]);
for (int i = 0, n = b.prev != null ? b.prev.length : 0; i < n; i++) { for (int i = 0, n = b.prev != null ? b.prev.length : 0; i < n; i++) {
BasicBlock bp = b.prev[i]; BasicBlock bp = b.prev[i];
if (!visitedBlocks.contains(bp)) { if (!visitedBlocks.contains(bp)) {

View File

@@ -501,5 +501,53 @@ public class FragmentsTest extends TestSuite {
" return c,b,a\n" + " return c,b,a\n" +
"end\n" ); "end\n" );
} }
public void testPhiUpvalue() {
runFragment( LuaValue.valueOf(6),
"local a = foo or 0\n"+
"local function b(c)\n"+
" if c > a then a = c end\n" +
" return a\n"+
"end\n" +
"b(6)\n" +
"return a\n" );
}
public void testAssignReferUpvalues() {
runFragment( LuaValue.valueOf(123),
"local entity = 234\n" +
"local function c()\n" +
" return entity\n" +
"end\n" +
"entity = (a == b) and 123\n" +
"if entity then\n" +
" return entity\n" +
"end\n" );
}
public void testSimpleRepeatUntil() {
runFragment( LuaValue.valueOf(5),
"local a\n"+
"local w\n"+
"repeat\n"+
" a = w\n"+
"until not a\n" +
"return 5\n" );
}
public void testTwoLoops() {
runFragment( LuaValue.valueOf("b"),
"local env = {}\n" +
"for a,b in pairs(_G) do\n" +
" c = function()\n" +
" return b\n" +
" end\n" +
"end\n" +
"local e = env\n" +
"local f = {a='b'}\n" +
"for k,v in pairs(f) do\n" +
" return env[k] or v\n" +
"end\n");
}
} }
} }