Add OP_CLOSURE to jit

This commit is contained in:
James Roseborough
2008-05-21 00:33:00 +00:00
parent 8d055862e0
commit 3582b97d8b
2 changed files with 80 additions and 36 deletions

View File

@@ -1,8 +1,10 @@
package org.luaj.jit; package org.luaj.jit;
import org.luaj.vm.LClosure; import org.luaj.vm.LClosure;
import org.luaj.vm.LNil;
import org.luaj.vm.LPrototype; import org.luaj.vm.LPrototype;
import org.luaj.vm.LTable; import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm.LuaState; import org.luaj.vm.LuaState;
abstract abstract
@@ -39,4 +41,5 @@ public class JitPrototype extends LPrototype {
} }
public abstract void jitCall( LuaState vm, LTable env, JitClosure jcl ); public abstract void jitCall( LuaState vm, LTable env, JitClosure jcl );
} }

View File

@@ -41,7 +41,10 @@ public class LuaJit extends Lua implements LuaCompiler {
Platform.setInstance(new J2sePlatform()); Platform.setInstance(new J2sePlatform());
LuaC.install(); LuaC.install();
String program = "print 'starting'\nfor i=1,10 do\n\tprint 'hello, world'\nend"; String program = "print 'starting'\n" +
"for i=1,10 do\n" +
" print 'hello, world'\n" +
"end";
InputStream is = new ByteArrayInputStream(program.getBytes()); InputStream is = new ByteArrayInputStream(program.getBytes());
LPrototype p = LuaC.compile(is, "program"); LPrototype p = LuaC.compile(is, "program");
test( p ); test( p );
@@ -172,6 +175,15 @@ public class LuaJit extends Lua implements LuaCompiler {
} }
for (; is<ns; is++ ) for (; is<ns; is++ )
ps.println( "\t\tLValue s"+is+" = LNil.NIL;" ); ps.println( "\t\tLValue s"+is+" = LNil.NIL;" );
ps.println("\t\tLClosure newcl;");
ps.println();
// save var args
if ( p.is_vararg ) {
ps.println( "\t\tint ncopy, ntotal;" );
ps.println( "\t\tint nvarargs = vm.top - vm.base;" );
ps.println( "\t\tbase = base + nvarargs;" );
}
ps.println(); ps.println();
@@ -531,6 +543,9 @@ public class LuaJit extends Lua implements LuaCompiler {
//return; //return;
// number of return vals to return // number of return vals to return
if ( p.is_vararg )
ps.println( "\t\tbase -= nvarargs;" );
b = LuaState.GETARG_B(i); b = LuaState.GETARG_B(i);
if (b > 0) { if (b > 0) {
for ( int j=1; j<b; j++ ) for ( int j=1; j<b; j++ )
@@ -626,45 +641,71 @@ public class LuaJit extends Lua implements LuaCompiler {
closeUpVals( base + a ); // close upvals higher in the stack than position a closeUpVals( base + a ); // close upvals higher in the stack than position a
continue; continue;
} }
*/
case LuaState.OP_CLOSURE: { case LuaState.OP_CLOSURE: {
//b = LuaState.GETARG_Bx(i);
//proto = cl.p.p[b];
//newClosure = proto.newClosure(cl.env);
//for (int j = 0; j < newClosure.upVals.length; j++, ci.pc++) {
// i = code[ci.pc];
// o = LuaState.GET_OPCODE(i);
// b = LuaState.GETARG_B(i);
// if (o == LuaState.OP_GETUPVAL) {
// newClosure.upVals[j] = cl.upVals[b];
// } else if (o == LuaState.OP_MOVE) {
// newClosure.upVals[j] = findUpVal( base + b );
// } else {
// throw new java.lang.IllegalArgumentException(
// "bad opcode: " + o);
// }
//}
//this.stack[base + a] = newClosure;
//continue;
b = LuaState.GETARG_Bx(i); b = LuaState.GETARG_Bx(i);
proto = cl.p.p[b]; ps.println("\t\ts"+a+" = newcl = p.p["+b+"].newClosure(env);");
newClosure = proto.newClosure(cl.env); for (int j = 0, nj=p.p[b].nups; j < nj; j++, pc++) {
for (int j = 0; j < newClosure.upVals.length; j++, ci.pc++) { i = code[pc];
i = code[ci.pc];
o = LuaState.GET_OPCODE(i); o = LuaState.GET_OPCODE(i);
b = LuaState.GETARG_B(i); b = LuaState.GETARG_B(i);
if (o == LuaState.OP_GETUPVAL) { if (o == LuaState.OP_GETUPVAL) {
newClosure.upVals[j] = cl.upVals[b]; ps.println("\t\tnewcl.upVals[j] = newcl.upVals["+b+"];");
} else if (o == LuaState.OP_MOVE) { } else if (o == LuaState.OP_MOVE) {
newClosure.upVals[j] = findUpVal( base + b ); ps.println("\t\tnewcl.upVals[j] = vm.findUpVal(base+"+b+");");
} else { } else {
throw new java.lang.IllegalArgumentException( throw new java.lang.IllegalArgumentException("bad opcode: " + o);
"bad opcode: " + o);
} }
} }
this.stack[base + a] = newClosure; break;
continue;
} }
case LuaState.OP_VARARG: { case LuaState.OP_VARARG: {
// figure out how many args to copy //// figure out how many args to copy
//b = LuaState.GETARG_B(i) - 1;
//nvarargs = this.stack[base - 1];
//n = nvarargs.toJavaInt();
//if (b == LuaState.LUA_MULTRET) {
// b = n; // use entire varargs supplied
//}
//
//// copy args up to call stack area
//checkstack(a+b);
//for (int j = 0; j < b; j++)
// this.stack[base + a + j] = (j < n ? this.stack[base
// - n + j - 1]
// : LNil.NIL);
//top = base + a + b;
//continue;
b = LuaState.GETARG_B(i) - 1; b = LuaState.GETARG_B(i) - 1;
nvarargs = this.stack[base - 1];
n = nvarargs.toJavaInt();
if ( b == LuaState.LUA_MULTRET ) { if ( b == LuaState.LUA_MULTRET ) {
b = n; // use entire varargs supplied ps.println( "\t\tncopy = ntotal = nvarargs;" );
} else {
ps.println( "\t\tncopy = Math.min(ntotal="+b+",nvarargs);" );
} }
ps.println( "\t\tSystem.arraycopy(vm.stack,base-nvarargs,vm.stack,base+"+a+",ncopy);" );
// copy args up to call stack area ps.println( "\t\tfor (int j = ncopy; j < ntotal; j++)" );
checkstack(a+b); ps.println( "\t\t\tvm.stack[base+j] = LNil.NIL;" );
for (int j = 0; j < b; j++) ps.println( "\t\tvm.top = base+ntotal+"+(a)+";" );
this.stack[base + a + j] = (j < n ? this.stack[base break;
- n + j - 1]
: LNil.NIL);
top = base + a + b;
continue;
} }
*/
} }
} }
ps.print( ps.print(