Java Code Examples for org.luaj.vm2.Lua#GET_OPCODE

The following examples show how to use org.luaj.vm2.Lua#GET_OPCODE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtoInfo.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
private void findUpvalues() {
	int[] code = prototype.code;
	int n = code.length;
	
	// propogate to inner prototypes
	String[] names = findInnerprotoNames();
	for ( int pc=0; pc<n; pc++ ) {
		if ( Lua.GET_OPCODE(code[pc]) == Lua.OP_CLOSURE ) {
			int bx = Lua.GETARG_Bx(code[pc]);
			Prototype newp = prototype.p[bx];
			UpvalInfo[] newu = new UpvalInfo[newp.upvalues.length];
			String newname = name + "$" + names[bx];
			for ( int j=0; j<newp.upvalues.length; ++j ) {
				Upvaldesc u = newp.upvalues[j];
				newu[j] = u.instack? findOpenUp(pc,u.idx) : upvals[u.idx];
			}
			subprotos[bx] = new ProtoInfo(newp, newname, newu);
		}
	}
	
	// mark all upvalues that are written locally as read/write
	for ( int pc=0; pc<n; pc++ ) {
		if ( Lua.GET_OPCODE(code[pc]) == Lua.OP_SETUPVAL )
			upvals[Lua.GETARG_B(code[pc])].rw = true;
	}
}
 
Example 2
Source File: ProtoInfo.java    From luaj with MIT License 6 votes vote down vote up
private void findUpvalues() {
	int[] code = prototype.code;
	int n = code.length;
	
	// propogate to inner prototypes
	String[] names = findInnerprotoNames();
	for ( int pc=0; pc<n; pc++ ) {
		if ( Lua.GET_OPCODE(code[pc]) == Lua.OP_CLOSURE ) {
			int bx = Lua.GETARG_Bx(code[pc]);
			Prototype newp = prototype.p[bx];
			UpvalInfo[] newu = new UpvalInfo[newp.upvalues.length];
			String newname = name + "$" + names[bx];
			for ( int j=0; j<newp.upvalues.length; ++j ) {
				Upvaldesc u = newp.upvalues[j];
				newu[j] = u.instack? findOpenUp(pc,u.idx) : upvals[u.idx];
			}
			subprotos[bx] = new ProtoInfo(newp, newname, newu);
		}
	}
	
	// mark all upvalues that are written locally as read/write
	for ( int pc=0; pc<n; pc++ ) {
		if ( Lua.GET_OPCODE(code[pc]) == Lua.OP_SETUPVAL )
			upvals[Lua.GETARG_B(code[pc])].rw = true;
	}
}
 
Example 3
Source File: DebugLib.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
static NameWhat getfuncname(DebugLib.CallFrame frame) {
	if (!frame.f.isclosure())
		return new NameWhat(frame.f.classnamestub(), "Java");
	Prototype p = frame.f.checkclosure().p;
	int pc = frame.pc;
	int i = p.code[pc]; /* calling instruction */
	LuaString tm;
	switch (Lua.GET_OPCODE(i)) {
		case Lua.OP_CALL:
		case Lua.OP_TAILCALL: /* get function name */
			return getobjname(p, pc, Lua.GETARG_A(i));
		case Lua.OP_TFORCALL: /* for iterator */
	    	return new NameWhat("(for iterator)", "(for iterator");
	    /* all other instructions can call only through metamethods */
	    case Lua.OP_SELF:
	    case Lua.OP_GETTABUP:
	    case Lua.OP_GETTABLE: tm = LuaValue.INDEX; break;
	    case Lua.OP_SETTABUP:
	    case Lua.OP_SETTABLE: tm = LuaValue.NEWINDEX; break;
	    case Lua.OP_EQ: tm = LuaValue.EQ; break;
	    case Lua.OP_ADD: tm = LuaValue.ADD; break;
	    case Lua.OP_SUB: tm = LuaValue.SUB; break;
	    case Lua.OP_MUL: tm = LuaValue.MUL; break;
	    case Lua.OP_DIV: tm = LuaValue.DIV; break;
	    case Lua.OP_MOD: tm = LuaValue.MOD; break;
	    case Lua.OP_POW: tm = LuaValue.POW; break;
	    case Lua.OP_UNM: tm = LuaValue.UNM; break;
	    case Lua.OP_LEN: tm = LuaValue.LEN; break;
	    case Lua.OP_LT: tm = LuaValue.LT; break;
	    case Lua.OP_LE: tm = LuaValue.LE; break;
	    case Lua.OP_CONCAT: tm = LuaValue.CONCAT; break;
	    default:
	      return null;  /* else no useful name can be found */
	}
	return new NameWhat( tm.tojstring(), "metamethod" );
}
 
Example 4
Source File: BasicBlock.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public static void visitBranches( Prototype p, BranchVisitor visitor ) {
	int sbx,j,c;
	int[] code = p.code;
	int n = code.length;
	for ( int i=0; i<n; i++ ) {
		int ins = code[i];
		switch ( Lua.GET_OPCODE( ins ) ) {
		case Lua.OP_LOADBOOL:
			if ( 0 == Lua.GETARG_C(ins) )
				break;
			if ( Lua.GET_OPCODE(code[i+1]) == Lua.OP_JMP  )
				throw new IllegalArgumentException("OP_LOADBOOL followed by jump at "+i);
			visitor.visitBranch( i, i+2 );
			continue;
		case Lua.OP_EQ:
		case Lua.OP_LT:
		case Lua.OP_LE:
		case Lua.OP_TEST: 
		case Lua.OP_TESTSET:
			if ( Lua.GET_OPCODE(code[i+1]) != Lua.OP_JMP  )
				throw new IllegalArgumentException("test not followed by jump at "+i); 
			sbx = Lua.GETARG_sBx(code[i+1]);
			++i;
			j = i + sbx + 1;
			visitor.visitBranch( i, j );
			visitor.visitBranch( i, i+1 ); 				
			continue;
		case Lua.OP_TFORLOOP:
		case Lua.OP_FORLOOP:
			sbx = Lua.GETARG_sBx(ins);
			j = i + sbx + 1;
			visitor.visitBranch( i, j );
			visitor.visitBranch( i, i+1 ); 				
			continue;
		case Lua.OP_JMP:
		case Lua.OP_FORPREP:
			sbx = Lua.GETARG_sBx(ins);
			j = i + sbx + 1;
			visitor.visitBranch( i, j );
			continue;
		case Lua.OP_TAILCALL:
		case Lua.OP_RETURN:
			visitor.visitReturn( i );
			continue;
		}
		if ( i+1<n && visitor.isbeg[i+1] )
			visitor.visitBranch( i, i+1 );
	}
}
 
Example 5
Source File: UpvalInfo.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
private boolean isLoopVariable(VarInfo var) {
	if ( var.pc >= 0 ) {
		switch ( Lua.GET_OPCODE(pi.prototype.code[var.pc]) ) {
		case Lua.OP_TFORLOOP:
		case Lua.OP_FORLOOP:
			return true;
		}
	}
	return false;
}
 
Example 6
Source File: DebugLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
static NameWhat getfuncname(DebugLib.CallFrame frame) {
	if (!frame.f.isclosure())
		return new NameWhat(frame.f.classnamestub(), "Java");
	Prototype p = frame.f.checkclosure().p;
	int pc = frame.pc;
	int i = p.code[pc]; /* calling instruction */
	LuaString tm;
	switch (Lua.GET_OPCODE(i)) {
		case Lua.OP_CALL:
		case Lua.OP_TAILCALL: /* get function name */
			return getobjname(p, pc, Lua.GETARG_A(i));
		case Lua.OP_TFORCALL: /* for iterator */
	    	return new NameWhat("(for iterator)", "(for iterator");
	    /* all other instructions can call only through metamethods */
	    case Lua.OP_SELF:
	    case Lua.OP_GETTABUP:
	    case Lua.OP_GETTABLE: tm = LuaValue.INDEX; break;
	    case Lua.OP_SETTABUP:
	    case Lua.OP_SETTABLE: tm = LuaValue.NEWINDEX; break;
	    case Lua.OP_EQ: tm = LuaValue.EQ; break;
	    case Lua.OP_ADD: tm = LuaValue.ADD; break;
	    case Lua.OP_SUB: tm = LuaValue.SUB; break;
	    case Lua.OP_MUL: tm = LuaValue.MUL; break;
	    case Lua.OP_DIV: tm = LuaValue.DIV; break;
	    case Lua.OP_MOD: tm = LuaValue.MOD; break;
	    case Lua.OP_POW: tm = LuaValue.POW; break;
	    case Lua.OP_UNM: tm = LuaValue.UNM; break;
	    case Lua.OP_LEN: tm = LuaValue.LEN; break;
	    case Lua.OP_LT: tm = LuaValue.LT; break;
	    case Lua.OP_LE: tm = LuaValue.LE; break;
	    case Lua.OP_CONCAT: tm = LuaValue.CONCAT; break;
	    default:
	      return null;  /* else no useful name can be found */
	}
	return new NameWhat( tm.tojstring(), "metamethod" );
}
 
Example 7
Source File: JavaBuilder.java    From luaj with MIT License 5 votes vote down vote up
private void resolveBranches() {
	int nc = p.code.length; 
	for (int pc = 0; pc < nc; pc++) {
		if (branches[pc] != null) {
			int t=targets[pc];
			while ( t<branchDestHandles.length && branchDestHandles[t] == null )
				t++;
			if ( t>= branchDestHandles.length )
				 throw new IllegalArgumentException("no target at or after "+targets[pc]+" op="+Lua.GET_OPCODE(p.code[targets[pc]]));
			branches[pc].setTarget(branchDestHandles[t]);
		}
	}
}
 
Example 8
Source File: UpvalInfo.java    From luaj with MIT License 5 votes vote down vote up
private boolean isLoopVariable(VarInfo var) {
	if ( var.pc >= 0 ) {
		switch ( Lua.GET_OPCODE(pi.prototype.code[var.pc]) ) {
		case Lua.OP_TFORLOOP:
		case Lua.OP_FORLOOP:
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: DebugLib.java    From luaj with MIT License 5 votes vote down vote up
static NameWhat getfuncname(DebugLib.CallFrame frame) {
	if (!frame.f.isclosure())
		return new NameWhat(frame.f.classnamestub(), "Java");
	Prototype p = frame.f.checkclosure().p;
	int pc = frame.pc;
	int i = p.code[pc]; /* calling instruction */
	LuaString tm;
	switch (Lua.GET_OPCODE(i)) {
		case Lua.OP_CALL:
		case Lua.OP_TAILCALL: /* get function name */
			return getobjname(p, pc, Lua.GETARG_A(i));
		case Lua.OP_TFORCALL: /* for iterator */
	    	return new NameWhat("(for iterator)", "(for iterator");
	    /* all other instructions can call only through metamethods */
	    case Lua.OP_SELF:
	    case Lua.OP_GETTABUP:
	    case Lua.OP_GETTABLE: tm = LuaValue.INDEX; break;
	    case Lua.OP_SETTABUP:
	    case Lua.OP_SETTABLE: tm = LuaValue.NEWINDEX; break;
	    case Lua.OP_EQ: tm = LuaValue.EQ; break;
	    case Lua.OP_ADD: tm = LuaValue.ADD; break;
	    case Lua.OP_SUB: tm = LuaValue.SUB; break;
	    case Lua.OP_MUL: tm = LuaValue.MUL; break;
	    case Lua.OP_DIV: tm = LuaValue.DIV; break;
	    case Lua.OP_MOD: tm = LuaValue.MOD; break;
	    case Lua.OP_POW: tm = LuaValue.POW; break;
	    case Lua.OP_UNM: tm = LuaValue.UNM; break;
	    case Lua.OP_LEN: tm = LuaValue.LEN; break;
	    case Lua.OP_LT: tm = LuaValue.LT; break;
	    case Lua.OP_LE: tm = LuaValue.LE; break;
	    case Lua.OP_CONCAT: tm = LuaValue.CONCAT; break;
	    default:
	      return null;  /* else no useful name can be found */
	}
	return new NameWhat( tm.tojstring(), "metamethod" );
}
 
Example 10
Source File: DebugLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
static NameWhat getfuncname(DebugLib.CallFrame frame) {
    if (!frame.f.isclosure())
        return new NameWhat(frame.f.classnamestub(), "Java");
    Prototype p = frame.f.checkclosure().p;
    int pc = frame.pc;
    int i = p.code[pc]; /* calling instruction */
    LuaString tm;
    switch (Lua.GET_OPCODE(i)) {
        case Lua.OP_CALL:
        case Lua.OP_TAILCALL: /* get function name */
            return getobjname(p, pc, Lua.GETARG_A(i));
        case Lua.OP_TFORCALL: /* for iterator */
            return new NameWhat("(for iterator)", "(for iterator");
        /* all other instructions can call only through metamethods */
        case Lua.OP_SELF:
        case Lua.OP_GETTABUP:
        case Lua.OP_GETTABLE:
            tm = LuaValue.INDEX;
            break;
        case Lua.OP_SETTABUP:
        case Lua.OP_SETTABLE:
            tm = LuaValue.NEWINDEX;
            break;
        case Lua.OP_EQ:
            tm = LuaValue.EQ;
            break;
        case Lua.OP_ADD:
            tm = LuaValue.ADD;
            break;
        case Lua.OP_SUB:
            tm = LuaValue.SUB;
            break;
        case Lua.OP_MUL:
            tm = LuaValue.MUL;
            break;
        case Lua.OP_DIV:
            tm = LuaValue.DIV;
            break;
        case Lua.OP_MOD:
            tm = LuaValue.MOD;
            break;
        case Lua.OP_POW:
            tm = LuaValue.POW;
            break;
        case Lua.OP_UNM:
            tm = LuaValue.UNM;
            break;
        case Lua.OP_LEN:
            tm = LuaValue.LEN;
            break;
        case Lua.OP_LT:
            tm = LuaValue.LT;
            break;
        case Lua.OP_LE:
            tm = LuaValue.LE;
            break;
        case Lua.OP_CONCAT:
            tm = LuaValue.CONCAT;
            break;
        default:
            return null;  /* else no useful name can be found */
    }
    return new NameWhat(tm.tojstring(), "metamethod");
}
 
Example 11
Source File: JavaBuilder.java    From luaj with MIT License 4 votes vote down vote up
public JavaBuilder(ProtoInfo pi, String classname, String filename) {
	this.pi = pi;
	this.p = pi.prototype;
	this.classname = classname;
	
	// what class to inherit from
	superclassType = p.numparams;
	if ( p.is_vararg != 0 || superclassType >= SUPERTYPE_VARARGS )
		superclassType = SUPERTYPE_VARARGS;
	for ( int i=0, n=p.code.length; i<n; i++ ) {
		int inst = p.code[i];
		int o = Lua.GET_OPCODE(inst);
		if ( (o == Lua.OP_TAILCALL) ||
		     ((o == Lua.OP_RETURN) && (Lua.GETARG_B(inst) < 1 || Lua.GETARG_B(inst) > 2)) ) {
			superclassType = SUPERTYPE_VARARGS;
			break;
		}
	}
	
	// create class generator
	cg = new ClassGen(classname, SUPER_NAME_N[superclassType], filename,
			Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
	cp = cg.getConstantPool(); // cg creates constant pool

	// main instruction lists
	factory = new InstructionFactory(cg);
	init = new InstructionList();
	main = new InstructionList();

	// create the fields
	for ( int i=0; i<p.upvalues.length; i++ ) {
		boolean isrw = pi.isReadWriteUpvalue( pi.upvals[i] ); 
		Type uptype = isrw? (Type) TYPE_LOCALUPVALUE: (Type) TYPE_LUAVALUE;
		FieldGen fg = new FieldGen(0, uptype, upvalueName(i), cp);
		cg.addField(fg.getField());
	}
	
	// create the method
	mg = new MethodGen( Constants.ACC_PUBLIC | Constants.ACC_FINAL, // access flags
			RETURN_TYPE_N[superclassType], // return type
			ARG_TYPES_N[superclassType], // argument types
			ARG_NAMES_N[superclassType], // arg names
			METH_NAME_N[superclassType], 
			STR_LUAVALUE, // method, defining class
			main, cp);
	
	// initialize the values in the slots
	initializeSlots();	

	// initialize branching
	int nc = p.code.length;
	targets = new int[nc];
	branches = new BranchInstruction[nc];
	branchDestHandles = new InstructionHandle[nc];
	lastInstrHandles = new InstructionHandle[nc];
}
 
Example 12
Source File: BasicBlock.java    From luaj with MIT License 4 votes vote down vote up
public static void visitBranches( Prototype p, BranchVisitor visitor ) {
	int sbx,j,c;
	int[] code = p.code;
	int n = code.length;
	for ( int i=0; i<n; i++ ) {
		int ins = code[i];
		switch ( Lua.GET_OPCODE( ins ) ) {
		case Lua.OP_LOADBOOL:
			if ( 0 == Lua.GETARG_C(ins) )
				break;
			if ( Lua.GET_OPCODE(code[i+1]) == Lua.OP_JMP  )
				throw new IllegalArgumentException("OP_LOADBOOL followed by jump at "+i);
			visitor.visitBranch( i, i+2 );
			continue;
		case Lua.OP_EQ:
		case Lua.OP_LT:
		case Lua.OP_LE:
		case Lua.OP_TEST: 
		case Lua.OP_TESTSET:
			if ( Lua.GET_OPCODE(code[i+1]) != Lua.OP_JMP  )
				throw new IllegalArgumentException("test not followed by jump at "+i); 
			sbx = Lua.GETARG_sBx(code[i+1]);
			++i;
			j = i + sbx + 1;
			visitor.visitBranch( i, j );
			visitor.visitBranch( i, i+1 ); 				
			continue;
		case Lua.OP_TFORLOOP:
		case Lua.OP_FORLOOP:
			sbx = Lua.GETARG_sBx(ins);
			j = i + sbx + 1;
			visitor.visitBranch( i, j );
			visitor.visitBranch( i, i+1 ); 				
			continue;
		case Lua.OP_JMP:
		case Lua.OP_FORPREP:
			sbx = Lua.GETARG_sBx(ins);
			j = i + sbx + 1;
			visitor.visitBranch( i, j );
			continue;
		case Lua.OP_TAILCALL:
		case Lua.OP_RETURN:
			visitor.visitReturn( i );
			continue;
		}
		if ( i+1<n && visitor.isbeg[i+1] )
			visitor.visitBranch( i, i+1 );
	}
}