List of usage examples for org.objectweb.asm Opcodes AASTORE
int AASTORE
To view the source code for org.objectweb.asm Opcodes AASTORE.
Click Source Link
From source file:com.offbynull.coroutines.instrumenter.OperandStackStateGenerators.java
License:Open Source License
/** * Generates instructions to save a certain number of items from the top of the operand stack. * <p>/*from www . j a v a2 s. c o m*/ * The instructions generated here expect the operand stack to be fully loaded. The stack items specified by {@code frame} must actually * all be on the operand stack. * <p> * REMEMBER: The items aren't returned to the operand stack after they've been saved (they have been popped off the stack). If you want * them back on the operand stack, reload using * {@code loadOperandStack(markerType, storageVars, frame, frame.getStackSize() - count, frame.getStackSize() - count, count)}. * @param markerType debug marker type * @param storageVars variables to store operand stack in to * @param frame execution frame at the instruction where the operand stack is to be saved * @param count number of items to store from the stack * @return instructions to save the operand stack to the storage variables * @throws NullPointerException if any argument is {@code null} * @throws IllegalArgumentException if {@code size} is larger than the number of items in the stack at {@code frame} (or is negative), * or if {@code count} is larger than {@code top} (or is negative) */ public static InsnList saveOperandStack(MarkerType markerType, StorageVariables storageVars, Frame<BasicValue> frame, int count) { Validate.notNull(markerType); Validate.notNull(storageVars); Validate.notNull(frame); Validate.isTrue(count >= 0); Validate.isTrue(count <= frame.getStackSize()); Variable intsVar = storageVars.getIntStorageVar(); Variable floatsVar = storageVars.getFloatStorageVar(); Variable longsVar = storageVars.getLongStorageVar(); Variable doublesVar = storageVars.getDoubleStorageVar(); Variable objectsVar = storageVars.getObjectStorageVar(); StorageSizes storageSizes = computeSizes(frame, frame.getStackSize() - count, count); int intsCounter = storageSizes.getIntsSize() - 1; int floatsCounter = storageSizes.getFloatsSize() - 1; int longsCounter = storageSizes.getLongsSize() - 1; int doublesCounter = storageSizes.getDoublesSize() - 1; int objectsCounter = storageSizes.getObjectsSize() - 1; InsnList ret = new InsnList(); // Create stack storage arrays and save them ret.add(merge(debugMarker(markerType, "Saving operand stack (" + count + " items)"), mergeIf(storageSizes.getIntsSize() > 0, () -> new Object[] { debugMarker(markerType, "Generating ints container (" + storageSizes.getIntsSize() + ")"), new LdcInsnNode(storageSizes.getIntsSize()), new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_INT), new VarInsnNode(Opcodes.ASTORE, intsVar.getIndex()) }), mergeIf(storageSizes.getFloatsSize() > 0, () -> new Object[] { debugMarker(markerType, "Generating floats container (" + storageSizes.getFloatsSize() + ")"), new LdcInsnNode(storageSizes.getFloatsSize()), new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_FLOAT), new VarInsnNode(Opcodes.ASTORE, floatsVar.getIndex()) }), mergeIf(storageSizes.getLongsSize() > 0, () -> new Object[] { debugMarker(markerType, "Generating longs container (" + storageSizes.getLongsSize() + ")"), new LdcInsnNode(storageSizes.getLongsSize()), new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_LONG), new VarInsnNode(Opcodes.ASTORE, longsVar.getIndex()) }), mergeIf(storageSizes.getDoublesSize() > 0, () -> new Object[] { debugMarker(markerType, "Generating doubles container (" + storageSizes.getDoublesSize() + ")"), new LdcInsnNode(storageSizes.getDoublesSize()), new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_DOUBLE), new VarInsnNode(Opcodes.ASTORE, doublesVar.getIndex()) }), mergeIf(storageSizes.getObjectsSize() > 0, () -> new Object[] { debugMarker(markerType, "Generating objects container (" + storageSizes.getObjectsSize() + ")"), new LdcInsnNode(storageSizes.getObjectsSize()), new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"), new VarInsnNode(Opcodes.ASTORE, objectsVar.getIndex()) }))); // Save the stack int start = frame.getStackSize() - 1; int end = frame.getStackSize() - count; for (int i = start; i >= end; i--) { BasicValue basicValue = frame.getStack(i); Type type = basicValue.getType(); // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise' // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this // point in the code so we can avoid saving it (but we still need to do a POP to get rid of it). When we load it back up, we can // simply push a null in to that slot, thereby keeping the same 'Lnull;' type. if ("Lnull;".equals(type.getDescriptor())) { ret.add(debugMarker(markerType, "Skipping null value at " + i)); ret.add(new InsnNode(Opcodes.POP)); continue; } // Convert the item to an object (if not already an object) and stores it in local vars table. Item removed from stack. switch (type.getSort()) { case Type.BOOLEAN: case Type.BYTE: case Type.SHORT: case Type.CHAR: case Type.INT: ret.add(debugMarker(markerType, "Popping/storing int at " + i + " to storage index " + intsCounter)); ret.add(new VarInsnNode(Opcodes.ALOAD, intsVar.getIndex())); // [val, int[]] ret.add(new InsnNode(Opcodes.SWAP)); // [int[], val] ret.add(new LdcInsnNode(intsCounter)); // [int[], val, idx] ret.add(new InsnNode(Opcodes.SWAP)); // [int[], idx, val] ret.add(new InsnNode(Opcodes.IASTORE)); // [] intsCounter--; break; case Type.FLOAT: ret.add(debugMarker(markerType, "Popping/storing float at " + i + " to storage index " + floatsCounter)); ret.add(new VarInsnNode(Opcodes.ALOAD, floatsVar.getIndex())); // [val, float[]] ret.add(new InsnNode(Opcodes.SWAP)); // [float[], val] ret.add(new LdcInsnNode(floatsCounter)); // [float[], val, idx] ret.add(new InsnNode(Opcodes.SWAP)); // [float[], idx, val] ret.add(new InsnNode(Opcodes.FASTORE)); // [] floatsCounter--; break; case Type.LONG: ret.add(debugMarker(markerType, "Popping/storing long at " + i + " to storage index " + longsCounter)); ret.add(new VarInsnNode(Opcodes.ALOAD, longsVar.getIndex())); // [val_PART1, val_PART2, long[]] ret.add(new LdcInsnNode(longsCounter)); // [val_PART1, val_PART2, long[], idx] ret.add(new InsnNode(Opcodes.DUP2_X2)); // [long[], idx, val_PART1, val_PART2, long[], idx] ret.add(new InsnNode(Opcodes.POP2)); // [long[], idx, val_PART1, val_PART2] ret.add(new InsnNode(Opcodes.LASTORE)); // [] longsCounter--; break; case Type.DOUBLE: ret.add(debugMarker(markerType, "Popping/storing double at " + i + " to storage index " + doublesCounter)); ret.add(new VarInsnNode(Opcodes.ALOAD, doublesVar.getIndex())); // [val_PART1, val_PART2, double[]] ret.add(new LdcInsnNode(doublesCounter)); // [val_PART1, val_PART2, double[], idx] ret.add(new InsnNode(Opcodes.DUP2_X2)); // [double[], idx, val_PART1, val_PART2, double[], idx] ret.add(new InsnNode(Opcodes.POP2)); // [double[], idx, val_PART1, val_PART2] ret.add(new InsnNode(Opcodes.DASTORE)); // [] doublesCounter--; break; case Type.ARRAY: case Type.OBJECT: ret.add(debugMarker(markerType, "Popping/storing object at " + i + " to storage index " + objectsCounter)); ret.add(new VarInsnNode(Opcodes.ALOAD, objectsVar.getIndex())); // [val, object[]] ret.add(new InsnNode(Opcodes.SWAP)); // [object[], val] ret.add(new LdcInsnNode(objectsCounter)); // [object[], val, idx] ret.add(new InsnNode(Opcodes.SWAP)); // [object[], idx, val] ret.add(new InsnNode(Opcodes.AASTORE)); // [] objectsCounter--; break; case Type.METHOD: case Type.VOID: default: throw new IllegalArgumentException(); } } // At this point, the storage array will contain the saved operand stack AND THE STACK WILL HAVE count ITEMS POPPED OFF OF IT. // // Reload using... // --------------- // ret.add(debugMarker(markerType, "Reloading stack items")); // InsnList reloadInsnList = loadOperandStack(markerType, storageVars, frame, // frame.getStackSize() - count, // frame.getStackSize() - count, // count); // ret.add(reloadInsnList); return ret; }
From source file:com.offbynull.coroutines.instrumenter.PackStateGenerators.java
License:Open Source License
public static InsnList packStorageArrays(MarkerType markerType, Frame<BasicValue> frame, Variable containerVar, StorageVariables localsStorageVars, StorageVariables operandStackStorageVars) { Validate.notNull(markerType);/* w w w . j ava 2 s. co m*/ Validate.notNull(frame); Validate.notNull(containerVar); Validate.notNull(localsStorageVars); Validate.notNull(operandStackStorageVars); Variable localsIntsVar = localsStorageVars.getIntStorageVar(); Variable localsFloatsVar = localsStorageVars.getFloatStorageVar(); Variable localsLongsVar = localsStorageVars.getLongStorageVar(); Variable localsDoublesVar = localsStorageVars.getDoubleStorageVar(); Variable localsObjectsVar = localsStorageVars.getObjectStorageVar(); Variable stackIntsVar = operandStackStorageVars.getIntStorageVar(); Variable stackFloatsVar = operandStackStorageVars.getFloatStorageVar(); Variable stackLongsVar = operandStackStorageVars.getLongStorageVar(); Variable stackDoublesVar = operandStackStorageVars.getDoubleStorageVar(); Variable stackObjectsVar = operandStackStorageVars.getObjectStorageVar(); StorageSizes stackSizes = OperandStackStateGenerators.computeSizes(frame, 0, frame.getStackSize()); StorageSizes localsSizes = LocalsStateGenerators.computeSizes(frame); // Why are we using size > 0 vs checking to see if var != null? // // REMEMBER THAT the analyzer will determine the variable slots to create for storage array based on its scan of EVERY // continuation/suspend point in the method. Imagine the method that we're instrumenting is this... // // public void example(Continuation c, String arg1) { // String var1 = "hi"; // c.suspend(); // // System.out.println(var1); // int var2 = 5; // c.suspend(); // // System.out.println(var1 + var2); // } // // There are two continuation/suspend points. The analyzer determines that method will need to assign variable slots for // localsObjectsVar+localsIntsVar. All the other locals vars will be null. // // If we ended up using var != null instead of size > 0, things would mess up on the first suspend(). The only variable initialized // at the first suspend is var1. As such, LocalStateGenerator ONLY CREATES AN ARRAY FOR localsObjectsVar. It doesn't touch // localsIntsVar because, at the first suspend(), var2 is UNINITALIZED. Nothing has been set to that variable slot. // // // The same thing applies to the operand stack. It doesn't make sense to create arrays for operand stack types that don't exist yet // at a continuation point, even though they may exist at other continuation points furhter down // Storage arrays in to locals container return merge( debugMarker(markerType, "Packing storage arrays for locals and operand stack in to an Object[]"), new LdcInsnNode(10), new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"), new VarInsnNode(Opcodes.ASTORE, containerVar.getIndex()), mergeIf(localsSizes.getIntsSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting locals ints in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(0), // [Object[], 0] new VarInsnNode(Opcodes.ALOAD, localsIntsVar.getIndex()), // [Object[], 0, val] new InsnNode(Opcodes.AASTORE), // [] }), mergeIf(localsSizes.getFloatsSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting locals floats in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(1), // [Object[], 1] new VarInsnNode(Opcodes.ALOAD, localsFloatsVar.getIndex()), // [Object[], 1, val] new InsnNode(Opcodes.AASTORE), // [] }), mergeIf(localsSizes.getLongsSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting locals longs in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(2), // [Object[], 2] new VarInsnNode(Opcodes.ALOAD, localsLongsVar.getIndex()), // [Object[], 2, val] new InsnNode(Opcodes.AASTORE), // [] }), mergeIf(localsSizes.getDoublesSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting locals doubles in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(3), // [Object[], 3] new VarInsnNode(Opcodes.ALOAD, localsDoublesVar.getIndex()), // [Object[], 3, val] new InsnNode(Opcodes.AASTORE), // [] }), mergeIf(localsSizes.getObjectsSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting locals objects in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(4), // [Object[], 4] new VarInsnNode(Opcodes.ALOAD, localsObjectsVar.getIndex()), // [Object[], 4, val] new InsnNode(Opcodes.AASTORE), // [] }), mergeIf(stackSizes.getIntsSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting stack ints in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(5), // [Object[], 5] new VarInsnNode(Opcodes.ALOAD, stackIntsVar.getIndex()), // [Object[], 5, val] new InsnNode(Opcodes.AASTORE), // [] }), mergeIf(stackSizes.getFloatsSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting stack floats in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(6), // [Object[], 6] new VarInsnNode(Opcodes.ALOAD, stackFloatsVar.getIndex()), // [Object[], 6, val] new InsnNode(Opcodes.AASTORE), // [] }), mergeIf(stackSizes.getLongsSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting stack longs in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(7), // [Object[], 7] new VarInsnNode(Opcodes.ALOAD, stackLongsVar.getIndex()), // [Object[], 7, val] new InsnNode(Opcodes.AASTORE), // [] }), mergeIf(stackSizes.getDoublesSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting stack doubles in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(8), // [Object[], 8] new VarInsnNode(Opcodes.ALOAD, stackDoublesVar.getIndex()), // [Object[], 8, val] new InsnNode(Opcodes.AASTORE), // [] }), mergeIf(stackSizes.getObjectsSize() > 0, () -> new Object[] { debugMarker(markerType, "Putting stack objects in to container"), new VarInsnNode(Opcodes.ALOAD, containerVar.getIndex()), // [Object[]] new LdcInsnNode(9), // [Object[], 9] new VarInsnNode(Opcodes.ALOAD, stackObjectsVar.getIndex()), // [Object[], 9, val] new InsnNode(Opcodes.AASTORE), // [] })); }
From source file:com.sun.fortress.compiler.OverloadSet.java
License:Open Source License
public void generateCall(MethodVisitor mv, int firstArgIndex, int one_if_method_closure) { if (!splitDone) { throw new CompilerError("Must split overload set before generating call(s)"); }//w ww . ja v a 2 s. c o m int l = specificDispatchOrder.length; TaggedFunctionName[] functionsToCall = new TaggedFunctionName[l]; for (int i = 0; i < l; i++) { functionsToCall[i] = getFunctionToCall(specificDispatchOrder[i]); } // create type structures for parameter types. TypeStructure[][] type_structures = new TypeStructure[l][]; MultiMap[] spmaps = new MultiMap[l]; TypeStructure[] return_type_structures = new TypeStructure[l]; for (int i = 0; i < l; i++) { TaggedFunctionName f = functionsToCall[i]; Functional eff = f.getF(); List<Param> parameters = f.getParameters(); MultiMap<String, TypeStructure> spmap = new MultiMap<String, TypeStructure>(); spmaps[i] = spmap; List<StaticParam> staticParams = staticParametersOf(f.getF()); Type rt = oa.getRangeType(eff); return_type_structures[i] = makeTypeStructure(rt, null, 1, 0, staticParams, eff); // skip parameters -- no 'this' for ordinary functions if (parameters.size() == 1 && oa.getDomainType(eff) instanceof TupleType) { TupleType tt = (TupleType) oa.getDomainType(eff); List<Type> tl = tt.getElements(); int storeAtIndex = tl.size(); // DRC back this out + firstArgIndex; // little dubious here, not sure we are getting the // right type structures for generic methods. what about 'self' TypeStructure[] f_type_structures = new TypeStructure[tl.size()]; type_structures[i] = f_type_structures; for (int j = 0; j < tl.size(); j++) { Type t = STypesUtil.insertStaticParams(tl.get(j), tt.getInfo().getStaticParams()); TypeStructure type_structure = makeTypeStructure(t, spmap, 1, storeAtIndex, staticParams, eff); f_type_structures[j] = type_structure; storeAtIndex = type_structure.successorIndex; } } else { int storeAtIndex = parameters.size(); // DRC back this out + firstArgIndex; TypeStructure[] f_type_structures = new TypeStructure[parameters.size()]; type_structures[i] = f_type_structures; for (int j = 0; j < parameters.size(); j++) { if (j != selfIndex()) { Type t = oa.getParamType(eff, j); TypeStructure type_structure = makeTypeStructure(t, spmap, 1, storeAtIndex, staticParams, eff); f_type_structures[j] = type_structure; storeAtIndex = type_structure.successorIndex; } } } } for (int i = 0; i < l; i++) { TaggedFunctionName f = functionsToCall[i]; TypeStructure[] f_type_structures = type_structures[i]; Label lookahead = null; boolean infer = false; List<StaticParam> staticParams = staticParametersOf(f.getF()); boolean last_case = i == l - 1; /* Trust the static checker; no need to verify * applicability of the last one. * Also, static parameters will be provided by static checker for the last one */ // Will need lookahead for the next one. lookahead = new Label(); // if this was a generic method that needs inference, we need to include the receiver argument // in the inference even if the firstArgIndex is 1 so that we can include it in inference // and dispatch //KBN-WIP is there a cleaner way to do this? int offset = (f_type_structures.length == specificDispatchOrder[i].getParameters().size()) ? firstArgIndex : 0; for (int j = 0; j < f_type_structures.length; j++) { if (j != selfIndex()) { //inference needed if the type structure contains generics TODO: do generics not appearing in the parameters make sense? probably not, but might need to deal with them. if (f_type_structures[j].containsTypeVariables) infer = true; } } if (infer || !last_case) for (int j = 0; j < f_type_structures.length; j++) { // Load actual parameter if (j != selfIndex()) { mv.visitVarInsn(Opcodes.ALOAD, j + offset); f_type_structures[j].emitInstanceOf(mv, lookahead, true); } } //Runtime inference for some cases if (infer) { @SuppressWarnings("unchecked") MultiMap<String, TypeStructure> staticTss = spmaps[i]; int localCount = f_type_structures[f_type_structures.length - 1].successorIndex; //counter for use storing stuff such as lower bounds //create type structures for lower bounds Map<StaticParam, TypeStructure> lowerBounds = new HashMap<StaticParam, TypeStructure>(); for (StaticParam sp : staticParams) lowerBounds.put(sp, makeParamTypeStructure(sp, localCount++, TypeStructure.COVARIANT)); //gather different types of bounds into Multimaps for use later MultiMap<StaticParam, StaticParam> relativeLowerBounds = new MultiMap<StaticParam, StaticParam>(); //form X :> Y MultiMap<StaticParam, Type> genericUpperBounds = new MultiMap<StaticParam, Type>(); //form X <: GenericStem[\ ... \] where Y appears in ... MultiMap<StaticParam, Type> concreteUpperBounds = new MultiMap<StaticParam, Type>(); //form X <: T where T contains no type variables for (int outer = 0; outer < staticParams.size(); outer++) { StaticParam outerSP = staticParams.get(outer); for (BaseType bt : outerSP.getExtendsClause()) { if (bt instanceof VarType) { // outerSP <: bt so outerSP will provide a lower bound on BT String varName = ((VarType) bt).getName().getText(); boolean found = false; for (int inner = 0; inner < outer && !found; inner++) { StaticParam innerSP = staticParams.get(inner); if (varName.equals(innerSP.getName().getText())) { relativeLowerBounds.putItem(innerSP, outerSP); // outerSP provides a lower bound on innerSP found = true; } } if (!found) throw new CompilerError( "Bad Scoping of static parameters found during runtime inference codegen:" + varName + " not declared before used in a bound"); } else if (bt instanceof AnyType) { //figure out if concrete or generic //do nothing - no need to add meaningless upper bound } else if (bt instanceof NamedType) { if (isGeneric(bt)) genericUpperBounds.putItem(outerSP, bt); else concreteUpperBounds.putItem(outerSP, bt); } } } //infer and load RTTIs for (int j = 0; j < staticParams.size(); j++) { StaticParam sp = staticParams.get(staticParams.size() - 1 - j); //reverse order due to left to right scoping Set<TypeStructure> instances = staticTss.get(sp.getName().getText()); //sort static parameters by their variance and put into //arrays using their local variable number List<Integer> invariantInstances = new ArrayList<Integer>(); List<Integer> covariantInstances = new ArrayList<Integer>(); List<Integer> contravariantInstances = new ArrayList<Integer>(); if (instances != null) for (TypeStructure ts : instances) { switch (ts.variance) { case TypeStructure.INVARIANT: invariantInstances.add(ts.localIndex); break; case TypeStructure.CONTRAVARIANT: contravariantInstances.add(ts.localIndex); break; case TypeStructure.COVARIANT: covariantInstances.add(ts.localIndex); break; default: throw new CompilerError("Unexpected Variance on TypeStructure during " + "generic instantiation analysis for overload dispatch"); } } // if any invariant instances, we must use that RTTI and check that //1) any other invariant instances are the same type (each subtypes the other) //2) any covariant instances are subtypes of the invariant instance //3) any contravariant instances are supertypes of the invariant instance if (invariantInstances.size() > 0) { //a valid instantiation must use the runtime type //of all invariant instances (which must all be the same) //thus, wlog, we can use the first invariant instance int RTTItoUse = invariantInstances.get(0); //1) for each other invariant instance, they must be the same //which we test by checking that each subtypes the other for (int k = 1; k < invariantInstances.size(); k++) { int RTTIcompare = invariantInstances.get(k); //RTTItoUse.runtimeSupertypeOf(RTTIcompare) mv.visitVarInsn(Opcodes.ALOAD, RTTItoUse); mv.visitVarInsn(Opcodes.ALOAD, RTTIcompare); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME, Naming.RTTI_SUBTYPE_METHOD_SIG); mv.visitJumpInsn(Opcodes.IFEQ, lookahead); //if false fail //RTTIcompare.runtimeSupertypeOf(RTTItoUse) mv.visitVarInsn(Opcodes.ALOAD, RTTIcompare); mv.visitVarInsn(Opcodes.ALOAD, RTTItoUse); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME, Naming.RTTI_SUBTYPE_METHOD_SIG); mv.visitJumpInsn(Opcodes.IFEQ, lookahead); //if false fail } //2) for each covariant instance, the runtime type (RTTIcompare) must be a // subtype of the instantiated type (RTTItoUse) for (int RTTIcompare : covariantInstances) { //RTTItoUse.runtimeSupertypeOf(RTTIcompare) mv.visitVarInsn(Opcodes.ALOAD, RTTItoUse); mv.visitVarInsn(Opcodes.ALOAD, RTTIcompare); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME, Naming.RTTI_SUBTYPE_METHOD_SIG); mv.visitJumpInsn(Opcodes.IFEQ, lookahead); //if false fail } //3) for each contravariant instance, the instantiated type (RTTItoUse) must be a // subtype of the runtime type (RTTIcompare) for (int RTTIcompare : contravariantInstances) { //RTTIcompare.runtimeSupertypeOf(RTTItoUse) mv.visitVarInsn(Opcodes.ALOAD, RTTIcompare); mv.visitVarInsn(Opcodes.ALOAD, RTTItoUse); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME, Naming.RTTI_SUBTYPE_METHOD_SIG); mv.visitJumpInsn(Opcodes.IFEQ, lookahead); //if false fail } //check lower bounds given by other variables Set<StaticParam> relativeLB = relativeLowerBounds.get(sp); if (relativeLB != null) for (StaticParam lb : relativeLB) { //RTTItoUse.runtimeSupertypeOf(otherLB) int otherOffset = lowerBounds.get(lb).localIndex; mv.visitVarInsn(Opcodes.ALOAD, RTTItoUse); mv.visitVarInsn(Opcodes.ALOAD, otherOffset); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME, Naming.RTTI_SUBTYPE_METHOD_SIG); mv.visitJumpInsn(Opcodes.IFEQ, lookahead); //if false fail } //verify meets upper bounds Set<Type> concreteUB = concreteUpperBounds.get(sp); if (concreteUB != null) for (Type cub : concreteUB) { //transform into RTTI generateRTTIfromStaticType(mv, cub); mv.visitVarInsn(Opcodes.ALOAD, RTTItoUse); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME, Naming.RTTI_SUBTYPE_METHOD_SIG); mv.visitJumpInsn(Opcodes.IFEQ, lookahead); //if false fail } //generate more bounds for generic upper bounds Set<Type> genericUB = genericUpperBounds.get(sp); if (genericUB != null) for (Type gub : genericUB) { TypeStructure newTS = makeTypeStructure(gub, staticTss, TypeStructure.COVARIANT, localCount, staticParams, null); localCount = newTS.successorIndex; mv.visitVarInsn(Opcodes.ALOAD, RTTItoUse); newTS.emitInstanceOf(mv, lookahead, false); //fail if RTTItoUse doesn't have this structure } //checks out, so store the RTTI we will use into the lower bound for this parameter mv.visitVarInsn(Opcodes.ALOAD, RTTItoUse); int index = lowerBounds.get(sp).localIndex; mv.visitVarInsn(Opcodes.ASTORE, index); } else if (contravariantInstances.size() == 0) { //we can do inference for covariant-only occurrences boolean started = false; if (covariantInstances.size() > 0) { started = true; mv.visitVarInsn(Opcodes.ALOAD, covariantInstances.get(0)); for (int k = 1; k < covariantInstances.size(); k++) { mv.visitVarInsn(Opcodes.ALOAD, covariantInstances.get(k)); //TODO: allow unions joinStackNoUnion(mv, lookahead); //fails if cannot join w/o union } } //incorporate lower bounds Set<StaticParam> relativeLB = relativeLowerBounds.get(sp); if (relativeLB != null) for (StaticParam lb : relativeLB) { mv.visitVarInsn(Opcodes.ALOAD, lowerBounds.get(lb).localIndex); if (started) { //join it in //TODO: allow unions joinStackNoUnion(mv, lookahead); } else { //start with this lower bound started = true; } } if (started) { //verify meets upper bounds Set<Type> concreteUB = concreteUpperBounds.get(sp); if (concreteUB != null) for (Type cub : concreteUB) { Label cleanup = new Label(); Label next = new Label(); mv.visitInsn(Opcodes.DUP); generateRTTIfromStaticType(mv, cub); //transform concrete bound into RTTI mv.visitInsn(Opcodes.SWAP); // LB <: CUB mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Naming.RTTI_CONTAINER_TYPE, Naming.RTTI_SUBTYPE_METHOD_NAME, Naming.RTTI_SUBTYPE_METHOD_SIG); mv.visitJumpInsn(Opcodes.IFEQ, cleanup); mv.visitJumpInsn(Opcodes.GOTO, next); mv.visitLabel(cleanup); mv.visitInsn(Opcodes.POP); mv.visitJumpInsn(Opcodes.GOTO, lookahead); mv.visitLabel(next); } //checks out, so store to lower bound of sp int index = lowerBounds.get(sp).localIndex; mv.visitVarInsn(Opcodes.ASTORE, index); //generate more bounds for generic upper bounds Set<Type> genericUB = genericUpperBounds.get(sp); if (genericUB != null) for (Type gub : genericUB) { TypeStructure newTS = makeTypeStructure(gub, staticTss, TypeStructure.COVARIANT, localCount, staticParams, null); localCount = newTS.successorIndex; mv.visitVarInsn(Opcodes.ALOAD, index); newTS.emitInstanceOf(mv, lookahead, false); //fail if candidate doesn't have this structure } } else { //Bottom is ok - no need to check upper bounds //or generate lower bounds mv.visitFieldInsn(Opcodes.GETSTATIC, Naming.RT_VALUES_PKG + "VoidRTTI", Naming.RTTI_SINGLETON, Naming.RTTI_CONTAINER_DESC); int index = lowerBounds.get(sp).localIndex; mv.visitVarInsn(Opcodes.ASTORE, index); } } else { //otherwise, we might need to do inference which is not implemented yet throw new CompilerError("non-invariant inference with contravariance not implemented"); } } //load instance cache table to avoid classloader when possible String tableName = this.generateClosureTableName(specificDispatchOrder[i]); //use original function for table name String tableOwner = this.generateClosureTableOwner(f); mv.visitFieldInsn(Opcodes.GETSTATIC, tableOwner, tableName, Naming.CACHE_TABLE_DESC); //load template class name String arrow = this.instanceArrowSchema(f); //NamingCzar.makeArrowDescriptor(f.getParameters(), f.getReturnType(),f.tagA); String functionName = this.functionName(f); String templateClass = Naming.genericFunctionPkgClass(Naming.dotToSep(f.tagA.getText()), functionName, Naming.LEFT_OXFORD + Naming.RIGHT_OXFORD, arrow); if (otherOverloadKeys.contains(templateClass)) { templateClass = Naming.genericFunctionPkgClass(Naming.dotToSep(f.tagA.getText()), NamingCzar.mangleAwayFromOverload(functionName), Naming.LEFT_OXFORD + Naming.RIGHT_OXFORD, arrow); //templateClass = NamingCzar.mangleAwayFromOverload(templateClass); } mv.visitLdcInsn(templateClass); String ic_sig; if (staticParams.size() > 6) { //use an array //load the function: RThelpers.loadClosureClass:(BAlongTree,String,RTTI[]) String paramList = Naming.CACHE_TABLE_DESC + NamingCzar.descString + Naming.RTTI_CONTAINER_ARRAY_DESC; ic_sig = Naming.makeMethodDesc(paramList, Naming.internalToDesc(NamingCzar.internalObject)); mv.visitLdcInsn(staticParams.size()); mv.visitTypeInsn(Opcodes.ANEWARRAY, Naming.RTTI_CONTAINER_TYPE); //dup array enough times to store RTTIs into it //know need at least 6 more mv.visitInsn(Opcodes.DUP); //first one to get arrays as top two elts on stack for (int numDups = staticParams.size() - 1; numDups > 0; numDups = numDups / 2) mv.visitInsn(Opcodes.DUP2); if (staticParams.size() % 2 == 0) mv.visitInsn(Opcodes.DUP); //if even, started halving with an odd number, so needs one last //store parameters into array for (int k = 0; k < staticParams.size(); k++) { int index = lowerBounds.get(staticParams.get(k)).localIndex; mv.visitLdcInsn(k); //index is the static param number mv.visitVarInsn(Opcodes.ALOAD, index); mv.visitInsn(Opcodes.AASTORE); } //array left on stack } else { //load the function: RTHelpers.loadClosureClass:(BAlongTree,(String,RTTI)^n)Object ic_sig = InstantiatingClassloader.jvmSignatureForOnePlusNTypes( Naming.CACHE_TABLE_TYPE + ";L" + NamingCzar.internalString, staticParams.size(), Naming.RTTI_CONTAINER_TYPE, Naming.internalToDesc(NamingCzar.internalObject)); //load parameter RTTIs for (int k = 0; k < staticParams.size(); k++) { int index = lowerBounds.get(staticParams.get(k)).localIndex; mv.visitVarInsn(Opcodes.ALOAD, index); } } mv.visitMethodInsn(Opcodes.INVOKESTATIC, Naming.RT_HELPERS, "loadClosureClass", ic_sig); //cast to object arrow int numParams = f.getParameters().size(); String objectAbstractArrow = NamingCzar.objectAbstractArrowTypeForNParams(numParams); InstantiatingClassloader.generalizedCastTo(mv, objectAbstractArrow); //if a method parameters converted //loadThisForMethods(mv); //load parameters for (int j = 0; j < f_type_structures.length; j++) { // Load actual parameter if (j != selfIndex()) { mv.visitVarInsn(Opcodes.ALOAD, j); // DRC back this out+ one_if_method_closure); // + firstArgIndex); KBN if a method, parameters already converted //no cast needed here - done by apply method } } //call apply method String objectArrow = NamingCzar.objectArrowTypeForNParams(numParams); String applySig = InstantiatingClassloader.jvmSignatureForNTypes(numParams, NamingCzar.internalObject, Naming.internalToDesc(NamingCzar.internalObject)); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, objectArrow, Naming.APPLY_METHOD, applySig); //cast to correct return type Type f_return = f.getReturnType(); if (f_return instanceof BottomType) { CodeGen.castToBottom(mv); } else { String returnType = NamingCzar.makeBoxedTypeName(f_return, f.tagA); InstantiatingClassloader.generalizedCastTo(mv, returnType); } } else { //no inferences needed loadThisForMethods(mv); for (int j = 0; j < f_type_structures.length; j++) { // Load actual parameter if (j != selfIndex()) { mv.visitVarInsn(Opcodes.ALOAD, j + firstArgIndex); InstantiatingClassloader.generalizedCastTo(mv, f_type_structures[j].fullname); } } String sig = jvmSignatureFor(f); invokeParticularMethod(mv, f, sig); Type f_return = f.getReturnType(); if (f_return instanceof BottomType) { CodeGen.castToBottom(mv); } } mv.visitInsn(Opcodes.ARETURN); if (lookahead != null) mv.visitLabel(lookahead); } }
From source file:com.sun.fortress.runtimeSystem.InstantiatingClassloader.java
License:Open Source License
public static void pushArgsIntoArray(MethodVisitor mv, int first_arg, int n_args, int array_offset) { for (int arg = 0; arg < n_args; arg++) { mv.visitVarInsn(Opcodes.ALOAD, array_offset); mv.visitLdcInsn(arg); //index is the static param number mv.visitVarInsn(Opcodes.ALOAD, arg + first_arg); mv.visitInsn(Opcodes.AASTORE); }// w ww. jav a 2s. c o m mv.visitVarInsn(ALOAD, array_offset); }
From source file:com.sun.fortress.runtimeSystem.InstantiatingClassloader.java
License:Open Source License
public static void pushArgsIntoArray(MethodVisitor mv, int first_arg, int n_args, int array_offset, List<Boolean> nulls) { int nulls_pushed = 0; for (int arg = 0; arg < n_args; arg++) { mv.visitVarInsn(Opcodes.ALOAD, array_offset); mv.visitLdcInsn(arg); //index is the static param number if (nulls.get(arg)) { mv.visitInsn(Opcodes.ACONST_NULL); nulls_pushed++;//w ww. j a v a2 s. c o m } else { mv.visitVarInsn(Opcodes.ALOAD, arg + first_arg - nulls_pushed); } mv.visitInsn(Opcodes.AASTORE); } mv.visitVarInsn(ALOAD, array_offset); }
From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java
License:Creative Commons License
private static void transformArmorTexture(MethodNode method) { InsnList needle = new InsnList(); needle.add(new VarInsnNode(Opcodes.ALOAD, 0)); needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_HORSE_FUNC110241CB, false)); needle.add(new VarInsnNode(Opcodes.ISTORE, 3)); AbstractInsnNode node = ASMHelper.findLastNodeFromNeedle(method.instructions, needle); InsnList newInstr = new InsnList(); LabelNode l17 = new LabelNode(); newInstr.add(l17);/*from w ww.ja va2 s . co m*/ newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0)); newInstr.add( ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_GET_CUSTOM_ARMOR_ITEM, false)); newInstr.add(new VarInsnNode(Opcodes.ASTORE, 4)); LabelNode l18 = new LabelNode(); newInstr.add(l18); newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4)); newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false)); newInstr.add(new TypeInsnNode(Opcodes.INSTANCEOF, ASMNames.CL_ITEM_HORSE_ARMOR)); LabelNode l19 = new LabelNode(); newInstr.add(new JumpInsnNode(Opcodes.IFEQ, l19)); LabelNode l20 = new LabelNode(); newInstr.add(l20); newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0)); newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_FIELD110280BR)); newInstr.add(new InsnNode(Opcodes.ICONST_2)); newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4)); newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_ITEM, false)); newInstr.add(new TypeInsnNode(Opcodes.CHECKCAST, ASMNames.CL_ITEM_HORSE_ARMOR)); newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0)); newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4)); newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_GET_ARMOR_TEXTURE, false)); newInstr.add(new InsnNode(Opcodes.AASTORE)); LabelNode l21 = new LabelNode(); newInstr.add(l21); newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0)); newInstr.add(new TypeInsnNode(Opcodes.NEW, ASMNames.CL_STRING_BUILDER)); newInstr.add(new InsnNode(Opcodes.DUP)); newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0)); newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_FIELD110286BQ)); newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESTATIC, ASMNames.MD_STRING_VALUE_OF, false)); newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_STRINGBUILDER_INIT, false)); newInstr.add(new LdcInsnNode("cst-")); newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_STRINGBUILDER_APPEND, false)); newInstr.add(new VarInsnNode(Opcodes.ALOAD, 4)); newInstr.add( ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ITEMSTACK_GET_UNLOC_NAME, false)); newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_STRINGBUILDER_APPEND, false)); newInstr.add( ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_STRINGBUILDER_TO_STRING, false)); newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.PUTFIELD, ASMNames.FD_HORSE_FIELD110286BQ)); newInstr.add(new InsnNode(Opcodes.RETURN)); newInstr.add(l19); method.instructions.insert(node, newInstr); }
From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java
License:Open Source License
/** * Initializes a classField of type Array in the default Constructor. * The array is initialized with the given Numbers. * //from ww w .jav a 2 s.c om * @param values * the values * @return the abstract optimizer test * @see AbstractOptimizerTest#initArrayInConstructor(String, int) */ public ClassNodeBuilder initArrayWith(final Number... values) { if (isInterface) { return this; } final Type elementType = Type.getType(lastField.desc).getElementType(); if (elementType.getDescriptor().startsWith("L")) { initArrayInternal(Opcodes.AASTORE, (Object[]) values); return this; } final Type type = toPrimitive(elementType); initArrayInternal(type.getOpcode(Opcodes.IASTORE), (Object[]) values); return this; }
From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java
License:Open Source License
/** * Inits the multi array with.//from w w w .jav a 2s. c om * * @param value * the value * @param indexes * the indexes * @return the class node builder */ public ClassNodeBuilder initMultiArrayWith(final Object value, final int... indexes) { if (isInterface) { return this; } final InsnList list = new InsnList(); list.add(new VarInsnNode(Opcodes.ALOAD, 0)); list.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, lastField.name, lastField.desc)); for (int i = 0; i < indexes.length - 1; ++i) { list.add(NodeHelper.getInsnNodeFor(indexes[i])); list.add(new InsnNode(Opcodes.AALOAD)); } list.add(NodeHelper.getInsnNodeFor(indexes[indexes.length - 1])); list.add(NodeHelper.getInsnNodeFor(value)); final Type elementType = Type.getType(lastField.desc).getElementType(); if (elementType.getDescriptor().startsWith("L")) { list.add(new InsnNode(Opcodes.AASTORE)); } else { list.add(new InsnNode(toPrimitive(Type.getType(value.getClass())).getOpcode(Opcodes.IASTORE))); } addToConstructor(list); return this; }
From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java
License:Open Source License
/** * Initializes a classField of type Array in the default lastConstructor. * The array is initialized with the given Strings. * /*w ww .j a v a 2 s .c om*/ * @param values * the values * @return the abstract optimizer test * @see AbstractOptimizerTest#initArrayInConstructor(String, int) */ public ClassNodeBuilder initArrayWith(final String... values) { if (isInterface) { return this; } initArrayInternal(Opcodes.AASTORE, (Object[]) values); return this; }
From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java
License:Open Source License
/** * Stores the given value of type Number or String in the array created before at index indexes... * /*from w ww . j av a2 s .co m*/ * @param value * the value * @param indexes * the indexes * @return the class node builder */ public ClassNodeBuilder withValue(final Object value, final int... indexes) { if (isInterface) { return this; } addInsn(new VarInsnNode(Opcodes.ALOAD, lastMethodVarIndex)); for (int i = 0; i < indexes.length - 1; ++i) { addInsn(NodeHelper.getInsnNodeFor(indexes[i])); addInsn(new InsnNode(Opcodes.AALOAD)); } addInsn(NodeHelper.getInsnNodeFor(indexes[indexes.length - 1])); addInsn(NodeHelper.getInsnNodeFor(value)); if (lastVarElementType.getDescriptor().startsWith("L")) { addInsn(new InsnNode(Opcodes.AASTORE)); } else { addInsn(new InsnNode(toPrimitive(Type.getType(value.getClass())).getOpcode(Opcodes.IASTORE))); } return this; }