Jut compile logic for for loop.

This commit is contained in:
James Roseborough
2008-05-17 23:28:15 +00:00
parent ebdbe60849
commit c5c322fee1
2 changed files with 100 additions and 65 deletions

View File

@@ -80,7 +80,7 @@ public class LuaState extends Lua {
private static final int LUA_MINCALLS = 10; private static final int LUA_MINCALLS = 10;
public int base = 0; public int base = 0;
protected int top = 0; public int top = 0;
protected int nresults = -1; protected int nresults = -1;
public LValue[] stack = new LValue[LUA_MINSTACK]; public LValue[] stack = new LValue[LUA_MINSTACK];
public int cc = -1; public int cc = -1;

View File

@@ -69,7 +69,8 @@ public class LuaJit extends Lua {
// write the file // write the file
String name = filename(); String name = filename();
File source = new File(name+JavaFileObject.Kind.SOURCE.extension); new File("jit").mkdirs();
File source = new File("jit/"+name+JavaFileObject.Kind.SOURCE.extension);
PrintStream ps = new PrintStream(new FileOutputStream(source)); PrintStream ps = new PrintStream(new FileOutputStream(source));
writeSource(ps, name, p); writeSource(ps, name, p);
ps.close(); ps.close();
@@ -83,7 +84,6 @@ public class LuaJit extends Lua {
Iterable<? extends JavaFileObject> compilationUnits = fm.getJavaFileObjects(source); Iterable<? extends JavaFileObject> compilationUnits = fm.getJavaFileObjects(source);
CompilationTask task = compiler.getTask(null, fm, null, null, null, compilationUnits); CompilationTask task = compiler.getTask(null, fm, null, null, null, compilationUnits);
boolean success = task.call(); boolean success = task.call();
System.out.println("Success: " + success);
// instantiate, config and return // instantiate, config and return
if (success) { if (success) {
@@ -177,7 +177,7 @@ public class LuaJit extends Lua {
// top = base + a; // top = base + a;
// table.luaGetTable(this, table, key); // table.luaGetTable(this, table, key);
// pw.println("\t\tvm.top = base+"+a+";"); // pw.println("\t\tvm.top = base+"+a+";");
ps.println("\t\tvm.settop("+a+");"); ps.println("\t\tvm.top = "+a+";");
ps.println("\t\tenv.luaGetTable(vm, env, p.k["+b+"]);"); ps.println("\t\tenv.luaGetTable(vm, env, p.k["+b+"]);");
continue; continue;
} }
@@ -294,36 +294,54 @@ public class LuaJit extends Lua {
this.stack[base + a] = rkb; this.stack[base + a] = rkb;
continue; continue;
} }
*/
case LuaState.OP_CALL: { case LuaState.OP_CALL: {
// ra is base of new call frame // ra is base of new call frame
this.base += a; // this.base += a;
// ps.println("\t\tvm.base = base+"+a+";");
// number of args // number of args
b = LuaState.GETARG_B(i); b = LuaState.GETARG_B(i);
if (b != 0) // else use previous instruction set top
top = base + b; // adjust top before the call
if (b != 0) { // else use previous instruction set top
// top = base + b;
ps.println("\t\tvm.top = "+(a+b)+";");
}
// number of return values we need // number of return values we need
c = LuaState.GETARG_C(i); c = LuaState.GETARG_C(i);
// make or set up the call // make the call
this.nresults = c - 1; ps.println("\t\tvm.call("+(b-1)+","+(c-1)+");");
if (this.stack[base].luaStackCall(this)) //
return; //
// // make or set up the call
// adjustTop only for case when call was completed // // this.nresults = c - 1;
// and number of args > 0. If call completed but // int nresults = c - 1;
// c == 0, leave top to point to end of results // ps.println("\t\tvm.nresults = "+nresults+";");
if (c > 0) // // if (this.stack[base].luaStackCall(this))
adjustTop(base + c - 1); // // return;
// ps.println("\t\tif ( vm.stack[base].luaStackCall(vm) ) {");
// restore base // ps.println("\t\t\tvm.execute();");
base = ci.base; // ps.println("\t\t}");
//
continue; // // adjustTop only for case when call was completed
// // and number of args > 0. If call completed but
// // c == 0, leave top to point to end of results
// if (c > 0) {
// // adjustTop(base + c - 1);
// ps.println( "\t\tvm.settop("+nresults+");");
// }
//
// // restore base
// // base = ci.base;
//
// // continue;
break;
} }
/*
case LuaState.OP_TAILCALL: { case LuaState.OP_TAILCALL: {
closeUpVals(base); closeUpVals(base);
@@ -387,52 +405,69 @@ public class LuaJit extends Lua {
// force a reload of the calling context // force a reload of the calling context
return; return;
} }
case LuaState.OP_FORLOOP: { */
i0 = this.stack[base + a];
step = this.stack[base + a + 2];
idx = step.luaBinOpUnknown(Lua.OP_ADD, i0);
limit = this.stack[base + a + 1];
back = step.luaBinCmpInteger(Lua.OP_LT, 0);
body = (back ? idx.luaBinCmpUnknown(Lua.OP_LE, limit) : limit
.luaBinCmpUnknown(Lua.OP_LE, idx));
if (body) {
this.stack[base + a] = idx;
this.stack[base + a + 3] = idx;
top = base + a + 3 + 1;
ci.pc += LuaState.GETARG_sBx(i);
}
continue;
}
case LuaState.OP_FORPREP: { case LuaState.OP_FORPREP: {
init = this.stack[base + a]; //init = this.stack[base + a];
step = this.stack[base + a + 2]; //step = this.stack[base + a + 2];
this.stack[base + a] = step.luaBinOpUnknown(Lua.OP_SUB, init); //this.stack[base + a] = step.luaBinOpUnknown(Lua.OP_SUB, init);
//b = LuaState.GETARG_sBx(i);
//ci.pc += b;
//continue;
// do the test at the top, not the bottom of the loop
b = LuaState.GETARG_sBx(i); b = LuaState.GETARG_sBx(i);
ci.pc += b;
continue; // set up the loop variables
ps.println( "\t\tdouble index = vm.stack[base+"+a+"].toJavaDouble();");
ps.println( "\t\tdouble limit = vm.stack[base+"+(a+1)+"].toJavaDouble();");
ps.println( "\t\tdouble step = vm.stack[base+"+(a+2)+"].toJavaDouble();");
ps.println( "\t\tboolean back = step < 0;");
ps.println( "\t\tfor ( ; back? (index>=limit): (index<=limit); index+=step ) {");
ps.println( "\t\tvm.stack[base+"+(a+3)+"] = LDouble.valueOf(index);");
break;
}
case LuaState.OP_FORLOOP: {
ps.println( "\t\t}");
//i0 = this.stack[base + a];
//step = this.stack[base + a + 2];
//idx = step.luaBinOpUnknown(Lua.OP_ADD, i0);
//limit = this.stack[base + a + 1];
//back = step.luaBinCmpInteger(Lua.OP_LT, 0);
//body = (back ? idx.luaBinCmpUnknown(Lua.OP_LE, limit) : limit
// .luaBinCmpUnknown(Lua.OP_LE, idx));
//if (body) {
// this.stack[base + a] = idx;
// this.stack[base + a + 3] = idx;
// top = base + a + 3 + 1;
// ci.pc += LuaState.GETARG_sBx(i);
//}
//continue;
break;
} }
case LuaState.OP_TFORLOOP: { case LuaState.OP_TFORLOOP: {
cb = base + a + 3; // call base ps.println( "\t\t}");
base = cb; //cb = base + a + 3; // call base
adjustTop( cb + 3 ); //base = cb;
System.arraycopy(this.stack, cb-3, this.stack, cb, 3); //adjustTop( cb + 3 );
//System.arraycopy(this.stack, cb-3, this.stack, cb, 3);
// call the iterator //
c = LuaState.GETARG_C(i); //// call the iterator
this.nresults = c; //c = LuaState.GETARG_C(i);
if (this.stack[cb].luaStackCall(this)) //this.nresults = c;
execute(); //if (this.stack[cb].luaStackCall(this))
base = ci.base; // execute();
adjustTop( cb + c ); //base = ci.base;
//adjustTop( cb + c );
// test for continuation //
if (!this.stack[cb].isNil() ) { // continue? //// test for continuation
this.stack[cb-1] = this.stack[cb]; // save control variable //if (!this.stack[cb].isNil() ) { // continue?
} else { // this.stack[cb-1] = this.stack[cb]; // save control variable
ci.pc++; // skip over jump //} else {
} // ci.pc++; // skip over jump
continue; //}
//continue;
} }
/*
case LuaState.OP_SETLIST: { case LuaState.OP_SETLIST: {
b = LuaState.GETARG_B(i); b = LuaState.GETARG_B(i);
c = LuaState.GETARG_C(i); c = LuaState.GETARG_C(i);