Improve bytecode generation.

This commit is contained in:
James Roseborough
2010-08-10 05:52:33 +00:00
parent e3f32988d6
commit afa2d5fd09
3 changed files with 18 additions and 18 deletions

View File

@@ -317,6 +317,7 @@ public class JavaGen {
builder.dup(); builder.dup();
builder.dup(); builder.dup();
builder.storeLocal(pc, a); builder.storeLocal(pc, a);
// builder.createUpvalues(pc, a+3, 1);
builder.storeLocal(pc, a+3); builder.storeLocal(pc, a+3);
builder.loadLocal(pc, a+1); // limit builder.loadLocal(pc, a+1); // limit
builder.loadLocal(pc, a+2); // step builder.loadLocal(pc, a+2); // step
@@ -418,13 +419,6 @@ public class JavaGen {
// let builder process branch instructions // let builder process branch instructions
builder.onEndOfLuaInstruction( pc0 ); builder.onEndOfLuaInstruction( pc0 );
// for-loops have upvalue created at top of loop
switch ( o ) {
case Lua.OP_FORPREP:
builder.convertToUpvalue(pc+1, a+3);
break;
}
} }
} }

View File

@@ -244,11 +244,12 @@ public class ProtoInfo {
case Lua.OP_FORLOOP: /* A sBx R(A)+=R(A+2); 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) }*/ if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
a = Lua.GETARG_A( ins ); a = Lua.GETARG_A( ins );
v[a][pc].isreferenced = true;
v[a+2][pc].isreferenced = true; v[a+2][pc].isreferenced = true;
v[a][pc] = new VarInfo(a,pc); v[a][pc] = new VarInfo(a,pc);
v[a+3][pc] = new VarInfo(a+1,pc);
v[a][pc].isreferenced = true; v[a][pc].isreferenced = true;
v[a+1][pc].isreferenced = true; v[a+1][pc].isreferenced = true;
v[a+3][pc] = new VarInfo(a+3,pc);
break; break;
case Lua.OP_LOADNIL: /* A B R(A) := ... := R(B) := nil */ case Lua.OP_LOADNIL: /* A B R(A) := ... := R(B) := nil */
@@ -444,16 +445,9 @@ public class ProtoInfo {
} }
public boolean isUpvalueRefer(int pc, int slot) { public boolean isUpvalueRefer(int pc, int slot) {
// special case for opcodes where overlap between refer and assign // special case when both refer and assign in same instruction
if ( pc >= 0 ) { if ( pc >= 0 && vars[slot][pc] != null && vars[slot][pc].pc == pc )
switch ( Lua.GET_OPCODE(prototype.code[pc]) ) { pc -= 1;
case Lua.OP_CLOSURE:
case Lua.OP_CALL:
case Lua.OP_TFORLOOP:
pc -= 1;
break;
}
}
VarInfo v = pc<0? params[slot]: vars[slot][pc]; VarInfo v = pc<0? params[slot]: vars[slot][pc];
// return v.upvalue != null && v.upvalue.rw; // return v.upvalue != null && v.upvalue.rw;
return v != null && v.upvalue != null; return v != null && v.upvalue != null;

View File

@@ -388,6 +388,18 @@ public class FragmentsTest extends TestSuite {
"end\n"); "end\n");
} }
public void testNumericForUpvalues2() {
runFragment( LuaValue.valueOf("222 222"),
"local t = {}\n"+
"local template = [[123 456]]\n"+
"for i = 1,2 do\n"+
" t[i] = template:gsub('%d', function(s)\n"+
" return i\n"+
" end)\n"+
"end\n" +
"return t[2]\n");
}
public void testReturnUpvalue() { public void testReturnUpvalue() {
runFragment( LuaValue.varargsOf(new LuaValue[] { LuaValue.ONE, LuaValue.valueOf(5), }), runFragment( LuaValue.varargsOf(new LuaValue[] { LuaValue.ONE, LuaValue.valueOf(5), }),
"local a = 1\n"+ "local a = 1\n"+