Improve bytecode generation.

This commit is contained in:
James Roseborough
2010-04-24 17:18:52 +00:00
parent 612b75d201
commit c1f67181fa
4 changed files with 28 additions and 12 deletions

View File

@@ -574,16 +574,26 @@ public class JavaBuilder {
private Map<LuaValue,String> constants = new HashMap<LuaValue,String>();
public void loadConstant(LuaValue value) {
String name = constants.get(value);
if ( name == null ) {
name = value.type() == LuaValue.TNUMBER?
value.isinttype()?
createLuaIntegerField(value.checkint()):
createLuaDoubleField(value.checkdouble()):
createLuaStringField(value.checkstring());
constants.put(value, name);
switch ( value.type() ) {
case LuaValue.TNIL:
loadNil();
break;
case LuaValue.TNUMBER:
case LuaValue.TSTRING:
String name = constants.get(value);
if ( name == null ) {
name = value.type() == LuaValue.TNUMBER?
value.isinttype()?
createLuaIntegerField(value.checkint()):
createLuaDoubleField(value.checkdouble()):
createLuaStringField(value.checkstring());
constants.put(value, name);
}
append(factory.createGetStatic(classname, name, TYPE_LUAVALUE));
break;
default:
throw new IllegalArgumentException("bad constant type: "+value.type());
}
append(factory.createGetStatic(classname, name, TYPE_LUAVALUE));
}
private String createLuaIntegerField(int value) {

View File

@@ -172,7 +172,8 @@ public class JavaGen {
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 ) branchdest[index+2] = true;
if ( c!=0 )
builder.addBranch(pc, JavaBuilder.BRANCH_GOTO, pc+2);
break;
case Lua.OP_JMP: /* sBx pc+=sBx */

View File

@@ -383,7 +383,7 @@ public class Slots {
private int prevUndefined(int index, int j) {
for ( ; index>=0; --index )
if ( ((slots[index][j] & BIT_INVALID) != 0) )
return 0;
return index;
return index;
}

View File

@@ -333,5 +333,10 @@ public class FragmentsTest extends TestCase {
"end\n" +
"return foo()\n" );
}
public void testTestSimpleBinops() {
runFragment( LuaValue.varargsOf(new LuaValue[] {
LuaValue.FALSE, LuaValue.FALSE, LuaValue.TRUE, LuaValue.TRUE, LuaValue.FALSE }),
"local a,b,c = 2,-2.5,0\n" +
"return (a==c), (b==c), (a==a), (a>c), (b>0)\n" );
}
}