Java Code Examples for org.apache.bcel.generic.ASTORE

The following examples show how to use org.apache.bcel.generic.ASTORE. These examples are extracted from open source projects. 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 Project: luaj   Source File: JavaBuilder.java    License: MIT License 6 votes vote down vote up
public void storeLocal(int pc, int slot) {
	boolean isupval = pi.isUpvalueAssign(pc, slot);
	int index = findSlotIndex( slot, isupval );
	if (isupval) {
		boolean isupcreate = pi.isUpvalueCreate(pc, slot);
		if ( isupcreate ) {
			append(factory.createInvoke(classname, "newupe", TYPE_LOCALUPVALUE, ARG_TYPES_NONE, Constants.INVOKESTATIC));
			append(InstructionConstants.DUP);
			append(new ASTORE(index));
		} else {
			append(new ALOAD(index));
		}
		append(InstructionConstants.SWAP);
		append(new PUSH(cp, 0));
		append(InstructionConstants.SWAP);
		append(InstructionConstants.AASTORE);
	} else {
		append(new ASTORE(index));
	}
}
 
Example 2
Source Project: commons-bcel   Source File: Subroutines.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Adds a new JSR or JSR_W that has this subroutine as its target.
 */
public void addEnteringJsrInstruction(final InstructionHandle jsrInst) {
    if ( (jsrInst == null) || (! (jsrInst.getInstruction() instanceof JsrInstruction))) {
        throw new AssertionViolatedException("Expecting JsrInstruction InstructionHandle.");
    }
    if (localVariable == UNSET) {
        throw new AssertionViolatedException("Set the localVariable first!");
    }
    // Something is wrong when an ASTORE is targeted that does not operate on the same local variable than the rest of the
    // JsrInstruction-targets and the RET.
    // (We don't know out leader here so we cannot check if we're really targeted!)
    if (localVariable != ((ASTORE) (((JsrInstruction) jsrInst.getInstruction()).getTarget().getInstruction())).getIndex()) {
        throw new AssertionViolatedException("Setting a wrong JsrInstruction.");
    }
    theJSRs.add(jsrInst);
}
 
Example 3
Source Project: luaj   Source File: JavaBuilder.java    License: MIT License 5 votes vote down vote up
public void initializeSlots() {
	int slot = 0;
	createUpvalues(-1, 0, p.maxstacksize);
	if ( superclassType == SUPERTYPE_VARARGS ) {
		for ( slot=0; slot<p.numparams; slot++ ) {
			if ( pi.isInitialValueUsed(slot) ) {
				append(new ALOAD(1));
				append(new PUSH(cp, slot+1));
				append(factory.createInvoke(STR_VARARGS, "arg", TYPE_LUAVALUE, ARG_TYPES_INT, Constants.INVOKEVIRTUAL));
				storeLocal(-1, slot);
			}
		}
		append(new ALOAD(1));
		append(new PUSH(cp, 1 + p.numparams));
		append(factory.createInvoke(STR_VARARGS, "subargs", TYPE_VARARGS, ARG_TYPES_INT, Constants.INVOKEVIRTUAL));
		append(new ASTORE(1));
	} else {
		// fixed arg function between 0 and 3 arguments
		for ( slot=0; slot<p.numparams; slot++ ) {
			this.plainSlotVars.put( Integer.valueOf(slot), Integer.valueOf(1+slot) );
			if ( pi.isUpvalueCreate(-1, slot) ) {
				append(new ALOAD(1+slot));
				storeLocal(-1, slot);
			}
		}
	}
	
	// nil parameters 
	// TODO: remove this for lua 5.2, not needed
	for ( ; slot<p.maxstacksize; slot++ ) {
		if ( pi.isInitialValueUsed(slot) ) {
			loadNil();
			storeLocal(-1, slot);
		}
	}		
}
 
Example 4
Source Project: luaj   Source File: JavaBuilder.java    License: MIT License 5 votes vote down vote up
public void createUpvalues(int pc, int firstslot, int numslots) {
	for ( int i=0; i<numslots; i++ ) {
		int slot = firstslot + i;
		boolean isupcreate = pi.isUpvalueCreate(pc, slot);
		if ( isupcreate ) {
			int index = findSlotIndex( slot, true );
			append(factory.createInvoke(classname, "newupn", TYPE_LOCALUPVALUE, ARG_TYPES_NONE, Constants.INVOKESTATIC));
			append(new ASTORE(index));
		}
	}
}
 
Example 5
Source Project: luaj   Source File: JavaBuilder.java    License: MIT License 5 votes vote down vote up
public void convertToUpvalue(int pc, int slot) {
	boolean isupassign = pi.isUpvalueAssign(pc, slot);
	if ( isupassign ) {
		int index = findSlotIndex( slot, false );
		append(new ALOAD(index));
		append(factory.createInvoke(classname, "newupl", TYPE_LOCALUPVALUE,  ARG_TYPES_LUAVALUE, Constants.INVOKESTATIC));
		int upindex = findSlotIndex( slot, true );
		append(new ASTORE(upindex));
	}
}
 
Example 6
Source Project: commons-bcel   Source File: Pass3aVerifier.java    License: Apache License 2.0 5 votes vote down vote up
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */
@Override
public void visitASTORE(final ASTORE o) {
    final int idx = o.getIndex();
    if (idx < 0) {
        constraintViolated(o, "Index '"+idx+"' must be non-negative.");
    }
    else{
        final int maxminus1 =  max_locals()-1;
        if (idx > maxminus1) {
            constraintViolated(o, "Index '"+idx+"' must not be greater than max_locals-1 '"+maxminus1+"'.");
        }
    }
}
 
Example 7
Source Project: luaj   Source File: JavaBuilder.java    License: MIT License 4 votes vote down vote up
public void storeVarresult() {
	append(new ASTORE(getVarresultIndex()));
}
 
Example 8
Source Project: commons-bcel   Source File: Pass3aVerifier.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * These are the checks for the satisfaction of constraints which are described in the
 * Java Virtual Machine Specification, Second Edition as Static Constraints on
 * the operands of instructions of Java Virtual Machine Code (chapter 4.8.1).
 * BCEL parses the code array to create an InstructionList and therefore has to check
 * some of these constraints. Additional checks are also implemented here.
 *
 * @throws StaticCodeConstraintException if the verification fails.
 */
private void pass3StaticInstructionOperandsChecks() {
    try {
    // When building up the InstructionList, BCEL has already done all those checks
    // mentioned in The Java Virtual Machine Specification, Second Edition, as
    // "static constraints on the operands of instructions in the code array".
    // TODO: see the do_verify() comments. Maybe we should really work on the
    //       byte array first to give more comprehensive messages.
    // TODO: Review Exception API, possibly build in some "offending instruction" thing
    //       when we're ready to insulate the offending instruction by doing the
    //       above thing.

    // TODO: Implement as much as possible here. BCEL does _not_ check everything.

    final ConstantPoolGen cpg = new ConstantPoolGen(Repository.lookupClass(myOwner.getClassName()).getConstantPool());
    final InstOperandConstraintVisitor v = new InstOperandConstraintVisitor(cpg);

    // Checks for the things BCEL does _not_ handle itself.
    InstructionHandle ih = instructionList.getStart();
    while (ih != null) {
        final Instruction i = ih.getInstruction();

        // An "own" constraint, due to JustIce's new definition of what "subroutine" means.
        if (i instanceof JsrInstruction) {
            final InstructionHandle target = ((JsrInstruction) i).getTarget();
            if (target == instructionList.getStart()) {
                throw new StaticCodeInstructionOperandConstraintException(
                    "Due to JustIce's clear definition of subroutines, no JSR or JSR_W may have a top-level instruction"+
                    " (such as the very first instruction, which is targeted by instruction '"+ih+"' as its target.");
            }
            if (!(target.getInstruction() instanceof ASTORE)) {
                throw new StaticCodeInstructionOperandConstraintException(
                    "Due to JustIce's clear definition of subroutines, no JSR or JSR_W may target anything else"+
                    " than an ASTORE instruction. Instruction '"+ih+"' targets '"+target+"'.");
            }
        }

        // vmspec2, page 134-137
        ih.accept(v);

        ih = ih.getNext();
    }

    } catch (final ClassNotFoundException e) {
    // FIXME: maybe not the best way to handle this
    throw new AssertionViolatedException("Missing class: " + e, e);
    }
}