From 8926a9434875f6e6536ffe4c841ecdbbbaad6d43 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Thu, 22 Apr 2010 14:49:01 +0000 Subject: [PATCH] Use bitfields for slots --- src/core/org/luaj/vm2/lib/LibFunction.java | 7 +- src/jse/org/luaj/vm2/luajc/JavaBuilder.java | 2 +- src/jse/org/luaj/vm2/luajc/JavaGen.java | 16 +- src/jse/org/luaj/vm2/luajc/Slots.java | 291 ++++++++++---------- 4 files changed, 166 insertions(+), 150 deletions(-) diff --git a/src/core/org/luaj/vm2/lib/LibFunction.java b/src/core/org/luaj/vm2/lib/LibFunction.java index aeb5050c..a55020fc 100644 --- a/src/core/org/luaj/vm2/lib/LibFunction.java +++ b/src/core/org/luaj/vm2/lib/LibFunction.java @@ -292,6 +292,11 @@ abstract public class LibFunction extends LuaFunction { // allocate storage for upvalue, initialize with nil protected static LuaValue[] newupn() { - return new LuaValue[] { valueOf(777) }; + return new LuaValue[] { NIL }; + } + + // allocate storage for upvalue, initialize with value + protected static LuaValue[] newupl(LuaValue v) { + return new LuaValue[] { v }; } } diff --git a/src/jse/org/luaj/vm2/luajc/JavaBuilder.java b/src/jse/org/luaj/vm2/luajc/JavaBuilder.java index e72e953f..bf5aec8c 100644 --- a/src/jse/org/luaj/vm2/luajc/JavaBuilder.java +++ b/src/jse/org/luaj/vm2/luajc/JavaBuilder.java @@ -320,7 +320,7 @@ public class JavaBuilder { } public void storeLocal(int pc, int slot) { - boolean isupval = slots.isUpvalue(pc, slot); + boolean isupval = slots.isUpvalueAssign(pc, slot); int index = findSlotIndex( slot, isupval ); if (isupval) { boolean isupcreate = slots.isUpvalueCreate(pc, slot); diff --git a/src/jse/org/luaj/vm2/luajc/JavaGen.java b/src/jse/org/luaj/vm2/luajc/JavaGen.java index 586fd12e..db29cc5b 100644 --- a/src/jse/org/luaj/vm2/luajc/JavaGen.java +++ b/src/jse/org/luaj/vm2/luajc/JavaGen.java @@ -58,6 +58,7 @@ public class JavaGen { int vresultbase = -1; for ( int pc=0, n=p.code.length; pc 0 ) sb.append( "\n" ); - byte[] s = slots[i]; - for ( int j=s.length; --j>=0; ) { - if ( s[j] == 0 ) - s[j] = ' '; - } - sb.append( i>0 && branchdest[i]? "D": " " ); - sb.append( new String(s) ); - } - return sb.toString(); + return toString(null); } private void markassignments( Prototype p ) { // mark initial assignments and references int j=0; for ( ; j C) then pc++ */ - s[a] = REFER; + s[a] |= BIT_REFER; branchdest[index+2] = true; break; case Lua.OP_TESTSET: /* A B C if (R(B) <=> C) then R(A):= R(B) else pc++ */ - s[a] = REFER; - s[b] = REFER; + s[a] |= BIT_REFER; + s[b] |= BIT_REFER; branchdest[index+2] = true; break; case Lua.OP_CALL: /* A B C R(A), ... ,R(A+C-2):= R(A)(R(A+1), ... ,R(A+B-1)) */ - while ( a < c-1 || a < b ) - s[a++] = (byte) (a=) R(A)*/ while ( aassign) - slots[index--][j] = UPVAL_USE; + promoteUpvalue( slots[index--], j ); } - private void promoteUpvalueAfter(byte[] s, int index, int j) { + private void promoteUpvalueAfter(int index, int j) { int end = nextUndefined(index,j); int access = lastAccessBefore(end,index,j); while ( index<=access ) - slots[index++][j] = UPVAL_USE; + promoteUpvalue( slots[index++], j ); + } + + private void promoteUpvalue(byte[] s, int slot) { + if ( (s[slot] & BIT_REFER) != 0 ) + s[slot] |= BIT_UP_REFER; + if ( (s[slot] & BIT_ASSIGN) != 0 ) + s[slot] |= BIT_UP_ASSIGN; } private int prevUndefined(int index, int j) { - while ( index>0 && slots[index][j] != INVALID ) + while ( index>0 && ((slots[index][j] & BIT_INVALID) == 0) ) --index; return index; } private int firstAssignAfter(int index, int limit, int j) { for ( ; indexlimit; --index ) { - switch (slots[index][j]) { - case ASSIGN: - case REFER_ASSIGN: - case REFER: + for ( --index; index>limit; --index ) { + if ( (slots[index][j] & (BIT_ASSIGN|BIT_REFER)) != 0 ) return index; - case UPVAL_CREATE: - case UPVAL_USE: - throw new IllegalStateException("overlapping upvalues"); - } } return index; } + // ------------- pretty-print slot info -------------- + + public static void printSlots(Prototype p) { + Slots s = new Slots(p); + System.out.println("slots for "+p.source+":\n"+s.toString(p) ); + for ( int i=0; i0 ) ps.append( '\n' ); + ps.append( s[i] ); + if ( p != null && i>0 && i<=p.code.length ) + Print.printOpCode(ps, p, i-1); + } + ps.close(); + return baos.toString(); + } }