Example usage for org.objectweb.asm Opcodes ALOAD

List of usage examples for org.objectweb.asm Opcodes ALOAD

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ALOAD.

Prototype

int ALOAD

To view the source code for org.objectweb.asm Opcodes ALOAD.

Click Source Link

Usage

From source file:com.offbynull.coroutines.instrumenter.SynchronizationGenerators.java

License:Open Source License

/**
 * Generates instruction to enter a monitor (top item on the stack) and store it in the {@link LockState} object sitting in the
 * lockstate variable./* w w w. j  a  v a2  s .com*/
 * @param markerType debug marker type
 * @param lockVars variables for lock/synchpoint functionality
 * @return instructions to enter a monitor and store it in the {@link LockState} object
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if lock variables aren't set (the method doesn't contain any monitorenter/monitorexit instructions)
 */
public static InsnList enterMonitorAndStore(MarkerType markerType, LockVariables lockVars) {
    Validate.notNull(markerType);
    Validate.notNull(lockVars);

    Variable lockStateVar = lockVars.getLockStateVar();
    Validate.isTrue(lockStateVar != null);

    Type clsType = Type.getType(LOCKSTATE_ENTER_METHOD.getDeclaringClass());
    Type methodType = Type.getType(LOCKSTATE_ENTER_METHOD);
    String clsInternalName = clsType.getInternalName();
    String methodDesc = methodType.getDescriptor();
    String methodName = LOCKSTATE_ENTER_METHOD.getName();

    // NOTE: This adds to the lock state AFTER locking.
    return merge(debugMarker(markerType, "Entering monitor and storing"),
            // [obj]
            new InsnNode(Opcodes.DUP), // [obj, obj]
            new InsnNode(Opcodes.MONITORENTER), // [obj]
            new VarInsnNode(Opcodes.ALOAD, lockStateVar.getIndex()), // [obj, lockState]
            new InsnNode(Opcodes.SWAP), // [lockState, obj]
            new MethodInsnNode(Opcodes.INVOKEVIRTUAL, // []
                    clsInternalName, methodName, methodDesc, false));
}

From source file:com.offbynull.coroutines.instrumenter.SynchronizationGenerators.java

License:Open Source License

/**
 * Generates instruction to exit a monitor (top item on the stack) and remove it from the {@link LockState} object sitting in the
 * lockstate variable./*from   w ww.j  a  v  a2  s  . c  o  m*/
 * @param markerType debug marker type
 * @param lockVars variables for lock/synchpoint functionality
 * @return instructions to exit a monitor and remove it from the {@link LockState} object
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if lock variables aren't set (the method doesn't contain any monitorenter/monitorexit instructions)
 */
public static InsnList exitMonitorAndDelete(MarkerType markerType, LockVariables lockVars) {
    Validate.notNull(markerType);
    Validate.notNull(lockVars);

    Variable lockStateVar = lockVars.getLockStateVar();
    Validate.isTrue(lockStateVar != null);

    Type clsType = Type.getType(LOCKSTATE_EXIT_METHOD.getDeclaringClass());
    Type methodType = Type.getType(LOCKSTATE_EXIT_METHOD);
    String clsInternalName = clsType.getInternalName();
    String methodDesc = methodType.getDescriptor();
    String methodName = LOCKSTATE_EXIT_METHOD.getName();

    // NOTE: This removes the lock AFTER unlocking.
    return merge(debugMarker(markerType, "Exiting monitor and unstoring"),
            // [obj]
            new InsnNode(Opcodes.DUP), // [obj, obj]
            new InsnNode(Opcodes.MONITOREXIT), // [obj]
            new VarInsnNode(Opcodes.ALOAD, lockStateVar.getIndex()), // [obj, lockState]
            new InsnNode(Opcodes.SWAP), // [lockState, obj]
            new MethodInsnNode(Opcodes.INVOKEVIRTUAL, // []
                    clsInternalName, methodName, methodDesc, false));
}

From source file:com.sixrr.metrics.profile.instanceHolder.MetricInstanceHolder.java

License:Apache License

private void loadMetricsFromProviders() {
    for (MetricProvider provider : MetricProvider.EXTENSION_POINT_NAME.getExtensions()) {
        final List<Class<? extends Metric>> classesForProvider = provider.getMetricClasses();

        for (Class<? extends Metric> aClass : classesForProvider) {
            try {
                myMetricInstances.put(aClass.getName(), aClass.newInstance());
            } catch (InstantiationException e) {
                MetricInstanceHolder.LOGGER.error(e);
            } catch (IllegalAccessException e) {
                MetricInstanceHolder.LOGGER.error(e);
            }/*from  w ww  .jav  a  2s  .  c  o  m*/
        }
    }

    for (Metric metric : Metric.EP_NAME.getExtensions()) {
        myMetricInstances.put(metric.getClass().getName(), metric);
    }

    // add some magic
    // in Profiles it stored by ClassName. Line Of code is can be handle without new metrics, need only extends  LinesOfCodeProjectMetric with
    // new name
    for (LineOfCodeFileTypeProviderEP ep : LineOfCodeFileTypeProviderEP.EP_NAME.getExtensions()) {
        FileType fileTypeByFileName = FileTypeRegistry.getInstance().findFileTypeByName(ep.fileType);

        if (fileTypeByFileName == null) {
            LOGGER.error("File type is unknown: " + ep.fileType);
            fileTypeByFileName = PlainTextFileType.INSTANCE;
        }
        String lineOfCodeClass = Type.getInternalName(LinesOfCodeProjectMetric.class);
        String className = lineOfCodeClass + "$" + ep.fileType;

        ClassWriter writer = new ClassWriter(Opcodes.F_FULL);
        writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, className, null, lineOfCodeClass, null);

        String desc = Type.getConstructorDescriptor(LinesOfCodeProjectMetric.class.getConstructors()[0]);
        MethodVisitor methodVisitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", desc, null, null);

        methodVisitor.visitMaxs(2, 2);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, lineOfCodeClass, "<init>", desc);
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitEnd();
        writer.visitEnd();

        Class<?> aClass = defineClass(className.replace("/", "."), writer.toByteArray());

        try {
            myMetricInstances.put(aClass.getName(),
                    (Metric) aClass.getConstructors()[0].newInstance(fileTypeByFileName));
        } catch (InstantiationException e) {
            LOGGER.error(e);
        } catch (IllegalAccessException e) {
            LOGGER.error(e);
        } catch (InvocationTargetException e) {
            LOGGER.error(e);
        }
    }
}

From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.DefUseChains.java

License:Open Source License

private static void removeUnboxedValueInsns(AbstractInterpretationValue val, ByteCodeMethodVisitor bcmv) {
    for (Insn i : val.getDefs())
        removeInsn(bcmv, i, val, "RemovingBoxedValueDefinition");

    for (Insn i : val.getUses()) {
        if (i.isBoxingMethod()) {
            removeInsn(bcmv, i, val, "RemoveBoxingMethod");
        } else if (i.isUnBoxingMethod()) {
            removeInsn(bcmv, i, val, "UnboxingMethod");
        } else if (i.isCheckCast()) {
            removeInsn(bcmv, i, val, "CheckCast"); // FIXME CHF
        } else if (i instanceof VarInsn) {
            VarInsn vi = (VarInsn) i;/*  w  ww  . j  a  va  2s . c  o m*/
            if (vi.opcode == Opcodes.ASTORE) {
                int j = bcmv.insns.indexOf(i);
                removeInsn(bcmv, i, val, "astoreconversion" + val.getType());
                if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ32;"))
                    bcmv.insns.add(j, new VarInsn("ISTORE", Opcodes.ISTORE, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ64;"))
                    bcmv.insns.add(j, new VarInsn("LSTORE", Opcodes.LSTORE, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR32;"))
                    bcmv.insns.add(j, new VarInsn("FSTORE", Opcodes.FSTORE, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR64;"))
                    bcmv.insns.add(j, new VarInsn("DSTORE", Opcodes.DSTORE, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FBoolean;"))
                    bcmv.insns.add(j, new VarInsn("ISTORE", Opcodes.ISTORE, val.getValueNumber(), vi.index));
                else
                    bcmv.insns.add(j, new VarInsn("ASTORE", Opcodes.ASTORE, val.getValueNumber(), vi.index));
            } else if (vi.opcode == Opcodes.ALOAD) {
                int j = bcmv.insns.indexOf(i);
                removeInsn(bcmv, i, val, "Aloadconversion" + val.getType());
                if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ32;"))
                    bcmv.insns.add(j, new VarInsn("ILOAD", Opcodes.ILOAD, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ64;"))
                    bcmv.insns.add(j, new VarInsn("LLOAD", Opcodes.LLOAD, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR32;"))
                    bcmv.insns.add(j, new VarInsn("FLOAD", Opcodes.FLOAD, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR64;"))
                    bcmv.insns.add(j, new VarInsn("DLOAD", Opcodes.DLOAD, val.getValueNumber(), vi.index));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FBoolean;"))
                    bcmv.insns.add(j, new VarInsn("ILOAD", Opcodes.ILOAD, val.getValueNumber(), vi.index));
                else
                    bcmv.insns.add(j, new VarInsn("ALOAD", Opcodes.ALOAD, val.getValueNumber(), vi.index));
            }
        } else if (i instanceof SingleInsn) {
            SingleInsn si = (SingleInsn) i;
            if (si.opcode == Opcodes.ARETURN) {
                int j = bcmv.insns.indexOf(i);
                if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ32;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FZZ32", "make",
                            "(I)Lcom/sun/fortress/compiler/runtimeValues/FZZ32;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FZZ64;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FZZ64", "make",
                            "(J)Lcom/sun/fortress/compiler/runtimeValues/FZZ64;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR32;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FRR32", "make",
                            "(F)Lcom/sun/fortress/compiler/runtimeValues/FRR32;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FRR64;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FRR64", "make",
                            "(D)Lcom/sun/fortress/compiler/runtimeValues/FRR64;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FVoid;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FVoid", "make",
                            "()Lcom/sun/fortress/compiler/runtimeValues/FVoid;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FBoolean;"))
                    bcmv.insns.add(j, new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                            "com/sun/fortress/compiler/runtimeValues/FBoolean", "make",
                            "(Z)Lcom/sun/fortress/compiler/runtimeValues/FBoolean;", "ReboxingReturnValue"));
                else if (val.getType().equals("Lcom/sun/fortress/compiler/runtimeValues/FJavaString;"))
                    bcmv.insns.add(j,
                            new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC,
                                    "com/sun/fortress/compiler/runtimeValues/FJavaString", "make",
                                    "(java.lang.String)Lcom/sun/fortress/compiler/runtimeValues/FJavaString;",
                                    "ReboxingReturnValue"));
                else
                    throw new RuntimeException("Don't recognize var type " + val.getType());
            }
        }
    }
}

From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.Inlining.java

License:Open Source License

public static List<Insn> convertInsns(MethodInsn mi, List<Insn> insns, int[] args, int _index, Label end) {
    List<Insn> result = new ArrayList<Insn>();
    HashMap labels = new HashMap();
    int index = _index;
    for (Insn i : insns) {
        if (i.isExpanded()) {
            MethodInsn expanded = (MethodInsn) i;
            // This use of end should be OK because all returns should have been removed when inlined before.
            // What could go wrong?
            result.addAll(convertInsns(expanded, expanded.inlineExpansionInsns, args, _index, end));
        } else if (i instanceof SingleInsn) {
            SingleInsn si = (SingleInsn) i;
            switch (si.opcode) {
            case Opcodes.IRETURN:
            case Opcodes.LRETURN:
            case Opcodes.FRETURN:
            case Opcodes.DRETURN:
            case Opcodes.ARETURN:
            case Opcodes.RETURN:
                result.add(new JumpInsn("RETURN->GOTO", Opcodes.GOTO, end, newIndex(mi, index++)));
                break;
            default:
                result.add(i.copy(newIndex(mi, index++)));
            }//from  w  ww.  ja  v a 2s  .  c om
        } else if (i instanceof VarInsn) {
            VarInsn vi = (VarInsn) i;
            switch (vi.opcode) {
            case Opcodes.ILOAD:
            case Opcodes.LLOAD:
            case Opcodes.FLOAD:
            case Opcodes.DLOAD:
            case Opcodes.ALOAD:
            case Opcodes.ISTORE:
            case Opcodes.LSTORE:
            case Opcodes.FSTORE:
            case Opcodes.DSTORE:
            case Opcodes.ASTORE:
                VarInsn newVarInsn = new VarInsn(vi.name, vi.opcode, args[vi.var], newIndex(mi, index++));
                result.add(newVarInsn);
                break;
            default:
                result.add(i.copy(newIndex(mi, index++)));
            }
        } else if (i instanceof VisitMaxs) {
        } else if (i instanceof VisitEnd) {
        } else if (i instanceof VisitCode) {
        } else if (i instanceof VisitFrame) {
        } else if (i instanceof LabelInsn) {
            LabelInsn li = (LabelInsn) i;
            if (labels.containsKey(li.label))
                result.add(new LabelInsn(li.name, (Label) labels.get(li.label), newIndex(mi, index++)));
            else {
                Label l = new Label();
                labels.put(li.label, l);
                result.add(new LabelInsn(li.name, l, newIndex(mi, index++)));
            }
        } else if (i instanceof JumpInsn) {
            JumpInsn ji = (JumpInsn) i;
            if (labels.containsKey(ji.label))
                result.add(
                        new JumpInsn(ji.name, ji.opcode, (Label) labels.get(ji.label), newIndex(mi, index++)));
            else {
                Label l = new Label();
                labels.put(ji.label, l);
                result.add(new JumpInsn(ji.name, ji.opcode, l, newIndex(mi, index++)));
            }
        } else if (i instanceof VisitLineNumberInsn) {
            VisitLineNumberInsn vlni = (VisitLineNumberInsn) i;
            if (labels.containsKey(vlni.start))
                result.add(new VisitLineNumberInsn(vlni.name, vlni.line, (Label) labels.get(vlni.start),
                        newIndex(mi, index++)));
            else {
                Label l = new Label();
                labels.put(vlni.start, l);
                result.add(new VisitLineNumberInsn(vlni.name, vlni.line, l, newIndex(mi, index++)));
            }
        } else if (i instanceof LocalVariableInsn) {
            LocalVariableInsn lvi = (LocalVariableInsn) i;
            if (labels.containsKey(lvi.start) && labels.containsKey(lvi.end)) {
                result.add(new LocalVariableInsn(lvi.name, lvi._name, lvi.desc, lvi.sig,
                        (Label) labels.get(lvi.start), (Label) labels.get(lvi.end), args[lvi._index],
                        newIndex(mi, index++)));
            } else
                throw new RuntimeException("NYI");
        } else if (i instanceof TryCatchBlock) {
            TryCatchBlock tcb = (TryCatchBlock) i;
            if (labels.containsKey(tcb.start) && labels.containsKey(tcb.end)
                    && labels.containsKey(tcb.handler)) {
                result.add(
                        new TryCatchBlock(tcb.name, (Label) labels.get(tcb.start), (Label) labels.get(tcb.end),
                                (Label) labels.get(tcb.handler), tcb.type, newIndex(mi, index++)));
            } else if (!labels.containsKey(tcb.start) && !labels.containsKey(tcb.end)
                    && !labels.containsKey(tcb.handler)) {
                Label s = new Label();
                Label e = new Label();
                Label h = new Label();
                labels.put(tcb.start, s);
                labels.put(tcb.end, e);
                labels.put(tcb.handler, h);
                result.add(new TryCatchBlock(tcb.name, s, e, h, tcb.type, newIndex(mi, index++)));
            } else
                throw new RuntimeException("NYI");
            // Need to add TableSwitch, LookupSwitch
        } else {
            result.add(i.copy(newIndex(mi, index++)));
        }
    }
    return result;
}

From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.RemoveLiteralCoercions.java

License:Open Source License

public static Substitution removeFloatLiterals2(ByteCodeMethodVisitor bcmv) {
    String floatLiteral = "com/sun/fortress/compiler/runtimeValues/FFloatLiteral";
    String RR64 = "com/sun/fortress/compiler/runtimeValues/FRR64";
    ArrayList<Insn> matches = new ArrayList<Insn>();
    matches.add(new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC, floatLiteral, "make",
            "(D)L" + floatLiteral + ";", "targetedForRemoval"));
    matches.add(new VarInsn("ASTORE", Opcodes.ASTORE, 0, "targetedForRemoval"));
    matches.add(new LabelInsn("LabelInsn", new Label(), "targetedForRemoval"));
    matches.add(new VisitLineNumberInsn("visitlinenumber", 0, new Label(), "targetedForRemoval"));
    matches.add(new VarInsn("ALOAD", Opcodes.ALOAD, 0, "targetedForRemoval"));
    matches.add(new LabelInsn("LabelInsn", new Label(), "targetedForRemoval"));
    matches.add(new VisitLineNumberInsn("visitlinenumber", 0, new Label(), "targetedForRemoval"));
    matches.add(new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC, "fortress/CompilerBuiltin", "coerce_RR64",
            "(Lfortress/CompilerBuiltin$FloatLiteral;)L" + RR64 + ";", "targetedForRemoval"));
    ArrayList<Insn> replacements = new ArrayList<Insn>();
    replacements.add(new MethodInsn("INVOKESTATIC", Opcodes.INVOKESTATIC, RR64, "make", "(D)L" + RR64 + ";",
            "ReplacementInsn"));
    return new Substitution(matches, replacements);
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

/**
 * Generate the default constructor for this class.
 * This constructors calls the method setToplevel().
 * If this environment is not a top-level environment, then a default
 * constructor does not need to be created.  (ASM will generate a
 * default constructor)./* w  w  w . j  ava 2s  .  c o  m*/
 */
private static void writeMethodInit(ClassWriter cw, String className) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getType(BaseEnv.class).getInternalName(), "<init>", "()V");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, "setTopLevel", "()V");
    mv.visitInsn(Opcodes.RETURN);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

/**
 * Implementing "static reflection" for the method getFooRaw so the
 * interpreter uses a switch instruction for ***GetRaw
 * based on the hash values of String names in this namespace.
 *//*from  w  w  w.  j  a  v a 2  s  .  c  o  m*/
private static void writeMethodGetRaw(ClassWriter cw, String className, String methodName,
        EnvironmentClass environmentClass, EnvSymbolNames symbolNames) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName,
            "(Ljava/lang/String;)" + environmentClass.descriptor(), null, null);
    mv.visitCode();

    Label beginFunction = new Label();
    mv.visitLabel(beginFunction);

    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I");
    mv.visitVarInsn(Opcodes.ISTORE, 2);
    Label beginLoop = new Label();
    mv.visitLabel(beginLoop);

    Relation<String, Integer> hashCodeRelation = symbolNames.makeHashCodeRelation(environmentClass);
    ArrayList<Integer> sortedCodes = new ArrayList<Integer>(hashCodeRelation.secondSet());
    Collections.sort(sortedCodes);
    Label returnNull = new Label();

    getRawHelper(mv, className, hashCodeRelation, environmentClass, sortedCodes, returnNull);

    mv.visitLabel(returnNull);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitInsn(Opcodes.ARETURN);

    Label endFunction = new Label();
    mv.visitLabel(endFunction);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, beginFunction, endFunction, 0);
    mv.visitLocalVariable("queryString", "Ljava/lang/String;", null, beginFunction, endFunction, 1);
    mv.visitLocalVariable("queryHashCode", "I", null, beginLoop, endFunction, 2);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(2, 3);
    mv.visitEnd();
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

private static void getRawBaseCase(MethodVisitor mv, String className,
        Relation<String, Integer> hashCodeRelation, EnvironmentClass environmentClass, int code,
        Label returnNull) {// w  w w  . j a  v a 2  s. c o  m

    PredicateSet<String> strings = hashCodeRelation.matchSecond(code);
    for (String testString : strings) {
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn(testString);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
        Label afterReturn = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, afterReturn);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        String idString = testString + environmentClass.namespace();
        mv.visitFieldInsn(Opcodes.GETFIELD, className, Naming.mangleIdentifier(idString),
                environmentClass.descriptor());
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitLabel(afterReturn);
    }
    mv.visitJumpInsn(Opcodes.GOTO, returnNull);
}

From source file:com.sun.fortress.compiler.environments.TopLevelEnvGen.java

License:Open Source License

/**
 * Implementing "static reflection" for the method putRaw so the
 * interpreter uses a switch instruction for ***PutRaw
 * based on the hash values of String names in this namespace.
 *///from  w w  w  .j  a  v a 2s . c o m
private static void writeMethodPutRaw(ClassWriter cw, String className, String methodName,
        EnvironmentClass environmentClass, EnvSymbolNames symbolNames) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName,
            "(" + STRING_DESCRIPTOR + environmentClass.descriptor() + ")V", null, null);
    mv.visitCode();

    Label beginFunction = new Label();
    mv.visitLabel(beginFunction);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STRING_INTERNALNAME, "hashCode", "()I");
    mv.visitVarInsn(Opcodes.ISTORE, 3);
    Label beginLoop = new Label();
    mv.visitLabel(beginLoop);

    Relation<String, Integer> hashCodeRelation = symbolNames.makeHashCodeRelation(environmentClass);
    ArrayList<Integer> sortedCodes = new ArrayList<Integer>(hashCodeRelation.secondSet());
    Collections.sort(sortedCodes);
    Label notFound = new Label();
    putRawHelper(mv, className, environmentClass, hashCodeRelation, sortedCodes, notFound);
    mv.visitLabel(notFound);
    mv.visitInsn(Opcodes.RETURN);
    Label endFunction = new Label();
    mv.visitLabel(endFunction);
    mv.visitLocalVariable("this", Naming.internalToDesc(className), null, beginFunction, endFunction, 0);
    mv.visitLocalVariable("queryString", STRING_DESCRIPTOR, null, beginFunction, endFunction, 1);
    mv.visitLocalVariable("value", environmentClass.descriptor(), null, beginFunction, endFunction, 2);
    mv.visitLocalVariable("queryHashCode", "I", null, beginLoop, endFunction, 3);
    // See comment above on ClassWriter.COMPUTE_FRAMES
    mv.visitMaxs(2, 4);
    mv.visitEnd();
}