Example usage for org.objectweb.asm Opcodes DUP

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

Introduction

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

Prototype

int DUP

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

Click Source Link

Usage

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * Generates _asActions() method as following:
 * /*from   ww  w .  j ava  2 s. co  m*/
 * if(name.equals("upload"))
 *    return upload();
 * else if(name.equals("save"))
 *    return save();
 * else if(name.equals("main"))
 *    return main();
 * else if(name.equals("redir"))
 *    return redir();
 * else
 *    throw new RuntimeException("no action named "+name);
 */
private void createActionMethod() {
    StringBuilder sb = new StringBuilder();
    sb.append("(").append(Type.getType(String.class)).append(")").append(Type.getType(Result.class));
    String desc = sb.toString();

    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asAction", desc, null, null);

    Label start = new Label();
    Label next = new Label();
    Label variable = new Label();
    boolean first = true;

    for (String name : reflector.getActions().keySet()) {
        Label l0 = null;
        Label l1 = new Label();

        if (first) {
            l0 = new Label();
            first = false;
        } else {
            l0 = next;
            next = new Label();
        }

        mv.visitLabel(l0);
        if (!first) {
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        }
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        mv.visitJumpInsn(Opcodes.IFEQ, next);

        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, reflector.getClazzInternalName(), name,
                "()" + Type.getType(Result.class), false);
        mv.visitInsn(Opcodes.ARETURN);
    }

    mv.visitLabel(next);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/NoSuchMethodException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("no @Action named ");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;",
            false);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/NoSuchMethodException", "<init>",
            "(Ljava/lang/String;)V", false);
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(variable);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, start, next, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, start, next, 1);

    mv.visitMaxs(5, 2);
    mv.visitEnd();
}

From source file:io.syncframework.optimizer.OControllerStaticMethodVisitor.java

License:Apache License

public void visitInsn(int opcode) {
    if (opcode != Opcodes.RETURN) {
        mv.visitInsn(opcode);//from w  w  w .  ja  va  2s.  com
        return;
    }

    Label start = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label interceptorsLabel = new Label();

    mv.visitCode();
    mv.visitTryCatchBlock(start, l1, l2, "java/lang/Throwable");

    /*
     * _asActions = new HashMap<String,Boolean>();
     */
    {
        mv.visitLabel(start);
        mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asActions", "Ljava/util/Map;");
    }
    /*
     * _asActions.put("main", true);
     * _asActions.put("action1", true);
     */
    for (String name : reflector.getActions().keySet()) {
        Label l = new Label();
        mv.visitLabel(l);
        mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asActions", "Ljava/util/Map;");
        mv.visitLdcInsn(name);
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;",
                false);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitInsn(Opcodes.POP);
    }

    /*
     * _asInterceptors = new HashMap<String,Class<?>[]>()
     */
    {
        Label l = new Label();
        mv.visitLabel(l);
        mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asInterceptors",
                "Ljava/util/Map;");
    }

    /*
     * List<Class<?>> l = new ArrayList<Class<?>>();
     */
    mv.visitLabel(interceptorsLabel);
    mv.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
    mv.visitVarInsn(Opcodes.ASTORE, 0);

    for (String name : reflector.getActions().keySet()) {
        Class<?> interceptors[] = reflector.getInterceptors().get(name);
        if (interceptors == null || interceptors.length == 0)
            continue;

        /*
         * l.clear();
         */
        Label l01 = new Label();
        mv.visitLabel(l01);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "clear", "()V", true);

        for (Class<?> interceptor : interceptors) {
            /*
             * l.add(LoginInterceptor.class);
             */
            Label l02 = new Label();
            mv.visitLabel(l02);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitLdcInsn(Type.getType(interceptor));
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
        }

        /*
         * _asInterceptors.put("upload", l.toArray(new Class[0]));
         */
        Label l03 = new Label();
        mv.visitLabel(l03);
        mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asInterceptors",
                "Ljava/util/Map;");
        mv.visitLdcInsn(name);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ICONST_0);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Class");
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "toArray",
                "([Ljava/lang/Object;)[Ljava/lang/Object;", true);
        mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Class;");
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitInsn(Opcodes.POP);
    }

    /* 
     * _asParameters = new HashMap<String,Class<?>>() 
     */
    {
        Label l = new Label();
        mv.visitLabel(l);
        mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asParameters",
                "Ljava/util/Map;");
    }
    /*
     * _asParameters.put("name", Type.class);
     */
    for (String name : reflector.getParameters().keySet()) {
        Label l = new Label();
        mv.visitLabel(l);
        mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asParameters",
                "Ljava/util/Map;");
        mv.visitLdcInsn(name);
        mv.visitLdcInsn(Type.getType(reflector.getParameters().get(name)));
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitInsn(Opcodes.POP);
    }

    /* 
     * _asConverters = new HashMap<String,Class<?>>() 
     */
    {
        Label l = new Label();
        mv.visitLabel(l);
        mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asConverters",
                "Ljava/util/Map;");
    }
    /*
     * _asConverters.put("name", Type.class);
     */
    for (String name : reflector.getParameters().keySet()) {
        if (reflector.getConverters().get(name) != null) {
            Class<?> converter = reflector.getConverters().get(name);
            Label l = new Label();
            mv.visitLabel(l);
            mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asConverters",
                    "Ljava/util/Map;");
            mv.visitLdcInsn(name);
            mv.visitLdcInsn(Type.getType(converter));
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                    "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
            mv.visitInsn(Opcodes.POP);
        }
    }

    /*
     * }
     * catch(Throwable t) {
     *    throw t;
     * }
     */
    Label throwableStart = new Label();
    Label throwableEnd = new Label();

    mv.visitLabel(l1);
    mv.visitJumpInsn(Opcodes.GOTO, throwableEnd);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });
    mv.visitVarInsn(Opcodes.ASTORE, 0);
    mv.visitLabel(throwableStart);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(throwableEnd);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitLocalVariable("l", "Ljava/util/List;", null, interceptorsLabel, l1, 0);
    mv.visitLocalVariable("t", "Ljava/lang/Throwable;", null, throwableStart, throwableEnd, 0);
}

From source file:io.syncframework.optimizer.OInterceptorStaticMethodVisitor.java

License:Apache License

public void visitInsn(int opcode) {
    if (opcode != Opcodes.RETURN) {
        mv.visitInsn(opcode);//from ww w  .  j a  v  a 2 s  .  co m
        return;
    }

    Label start = new Label();
    Label l1 = new Label();
    Label l2 = new Label();

    mv.visitCode();
    mv.visitTryCatchBlock(start, l1, l2, "java/lang/Throwable");

    /* 
     * _asParameters = new HashMap<String,Class<?>>() 
     */
    {
        mv.visitLabel(start);
        mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asParameters",
                "Ljava/util/Map;");
    }
    /*
     * _asParameters.put("name", Type.class);
     */
    for (String name : reflector.getParameters().keySet()) {
        Label l = new Label();
        mv.visitLabel(l);
        mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asParameters",
                "Ljava/util/Map;");
        mv.visitLdcInsn(name);
        mv.visitLdcInsn(Type.getType(reflector.getParameters().get(name)));
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitInsn(Opcodes.POP);
    }

    /* 
     * _asConverters = new HashMap<String,Class<?>>() 
     */
    {
        Label l = new Label();
        mv.visitLabel(l);
        mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asConverters",
                "Ljava/util/Map;");
    }
    /*
     * _asConverters.put("name", Type.class);
     */
    for (String name : reflector.getParameters().keySet()) {
        if (reflector.getConverters().get(name) != null) {
            Class<?> converter = reflector.getConverters().get(name);
            Label l = new Label();
            mv.visitLabel(l);
            mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asConverters",
                    "Ljava/util/Map;");
            mv.visitLdcInsn(name);
            mv.visitLdcInsn(Type.getType(converter));
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                    "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
            mv.visitInsn(Opcodes.POP);
        }
    }

    /*
     * }
     * catch(Throwable t) {
     *    throw t;
     * }
     */
    Label throwableStart = new Label();
    Label throwableEnd = new Label();

    mv.visitLabel(l1);
    mv.visitJumpInsn(Opcodes.GOTO, throwableEnd);

    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });
    mv.visitVarInsn(Opcodes.ASTORE, 0);

    mv.visitLabel(throwableStart);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(throwableEnd);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitLocalVariable("t", "Ljava/lang/Throwable;", null, throwableStart, throwableEnd, 0);
}

From source file:jerl.bcm.inj.impl.MethodExceptionHandlerPrintStackTrace.java

License:Apache License

@Override
public void inject(MethodVisitor mv) {
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V");
}

From source file:jpcsp.Allegrex.compiler.CompilerContext.java

License:Open Source License

private void visitContinueToAddress(int returnAddress, boolean returnOnUnknownAddress) {
    //      if (x != returnAddress) {
    //          RuntimeContext.jump(x, returnAddress);
    //      }/*from w  w w  .j a v a  2 s  . co m*/
    Label continueLabel = new Label();
    Label isReturnAddress = new Label();

    mv.visitInsn(Opcodes.DUP);
    loadImm(returnAddress);
    visitJump(Opcodes.IF_ICMPEQ, isReturnAddress);

    if (returnOnUnknownAddress) {
        visitJump();
    } else {
        loadImm(returnAddress);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName, "jump", "(II)V");
        mv.visitJumpInsn(Opcodes.GOTO, continueLabel);
    }

    mv.visitLabel(isReturnAddress);
    mv.visitInsn(Opcodes.POP);
    mv.visitLabel(continueLabel);
}

From source file:jpcsp.Allegrex.compiler.CompilerContext.java

License:Open Source License

private void visitContinueToAddressInRegister(int reg) {
    //      if (x != cpu.reg) {
    //          RuntimeContext.jump(x, cpu.reg);
    //      }/* w w w .ja v  a  2  s  .  c  om*/
    Label continueLabel = new Label();
    Label isReturnAddress = new Label();

    mv.visitInsn(Opcodes.DUP);
    loadRegister(reg);
    visitJump(Opcodes.IF_ICMPEQ, isReturnAddress);

    loadRegister(reg);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName, "jump", "(II)V");
    mv.visitJumpInsn(Opcodes.GOTO, continueLabel);

    mv.visitLabel(isReturnAddress);
    mv.visitInsn(Opcodes.POP);
    mv.visitLabel(continueLabel);
}

From source file:jpcsp.Allegrex.compiler.CompilerContext.java

License:Open Source License

/**
 * Generate the required Java code to load one parameter for
 * the syscall function from the CPU registers.
 *
 * The following code is generated based on the parameter type:
 * Processor: parameterValue = RuntimeContext.processor
 * int:       parameterValue = cpu.gpr[paramIndex++]
 * float:     parameterValue = cpu.fpr[paramFloatIndex++]
 * long:      parameterValue = (cpu.gpr[paramIndex++] & 0xFFFFFFFFL) + ((long) cpu.gpr[paramIndex++]) << 32)
 * boolean:   parameterValue = cpu.gpr[paramIndex++]
 * TPointer,/*ww  w.j a va2  s  .com*/
 * TPointer8,
 * TPointer16,
 * TPointer32,
 * TPointer64,
 * TErrorPointer32:
 *            if (checkMemoryAccess()) {
 *                if (canBeNullParam && address == 0) {
 *                    goto addressGood;
 *                }
 *                if (RuntimeContext.checkMemoryPointer(address)) {
 *                    goto addressGood;
 *                }
 *                cpu.gpr[_v0] = SceKernelErrors.ERROR_INVALID_POINTER;
 *                pop all the parameters already prepared on the stack;
 *                goto afterSyscall;
 *                addressGood:
 *            }
 *            <parameterType> pointer = new <parameterType>(address);
 *            if (parameterType == TErrorPointer32.class) {
 *                parameterReader.setHasErrorPointer(true);
 *                localVar[LOCAL_ERROR_POINTER] = pointer;
 *            }
 *            parameterValue = pointer
 * HLEUidClass defined in annotation:
 *            <parameterType> uidObject = HLEUidObjectMapping.getObject("<parameterType>", uid);
 *            if (uidObject == null) {
 *                cpu.gpr[_v0] = errorValueOnNotFound;
 *                pop all the parameters already prepared on the stack;
 *                goto afterSyscall;
 *            }
 *            parameterValue = uidObject
 *
 * And then common for all the types:
 *            try {
 *                parameterValue = <module>.<methodToCheck>(parameterValue);
 *            } catch (SceKernelErrorException e) {
 *                goto catchSceKernelErrorException;
 *            }
 *            push parameterValue on stack
 *
 * @param parameterReader               the current parameter state
 * @param func                          the syscall function
 * @param parameterType                 the type of the parameter
 * @param afterSyscallLabel             the Label pointing after the call to the syscall function
 * @param catchSceKernelErrorException  the Label pointing to the SceKernelErrorException catch handler
 */
private void loadParameter(CompilerParameterReader parameterReader, HLEModuleFunction func,
        Class<?> parameterType, Annotation[] parameterAnnotations, Label afterSyscallLabel,
        Label catchSceKernelErrorException) {
    if (parameterType == Processor.class) {
        loadProcessor();
        parameterReader.incrementCurrentStackSize();
    } else if (parameterType == CpuState.class) {
        loadCpu();
        parameterReader.incrementCurrentStackSize();
    } else if (parameterType == int.class) {
        parameterReader.loadNextInt();
        parameterReader.incrementCurrentStackSize();
    } else if (parameterType == float.class) {
        parameterReader.loadNextFloat();
        parameterReader.incrementCurrentStackSize();
    } else if (parameterType == long.class) {
        parameterReader.loadNextLong();
        parameterReader.incrementCurrentStackSize(2);
    } else if (parameterType == boolean.class) {
        parameterReader.loadNextInt();
        parameterReader.incrementCurrentStackSize();
    } else if (parameterType == String.class) {
        parameterReader.loadNextInt();

        int maxLength = 16 * 1024;
        for (Annotation parameterAnnotation : parameterAnnotations) {
            if (parameterAnnotation instanceof StringInfo) {
                StringInfo stringInfo = ((StringInfo) parameterAnnotation);
                maxLength = stringInfo.maxLength();
                break;
            }
        }
        loadImm(maxLength);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName, "readStringNZ",
                "(II)" + Type.getDescriptor(String.class));
        parameterReader.incrementCurrentStackSize();
    } else if (parameterType == PspString.class) {
        parameterReader.loadNextInt();

        int maxLength = 16 * 1024;
        boolean canBeNull = false;
        for (Annotation parameterAnnotation : parameterAnnotations) {
            if (parameterAnnotation instanceof StringInfo) {
                StringInfo stringInfo = ((StringInfo) parameterAnnotation);
                maxLength = stringInfo.maxLength();
            }
            if (parameterAnnotation instanceof CanBeNull) {
                canBeNull = true;
            }
        }
        loadImm(maxLength);
        loadImm(canBeNull);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName, "readPspStringNZ",
                "(IIZ)" + Type.getDescriptor(PspString.class));
        parameterReader.incrementCurrentStackSize();
    } else if (parameterType == TPointer.class || parameterType == TPointer8.class
            || parameterType == TPointer16.class || parameterType == TPointer32.class
            || parameterType == TPointer64.class || parameterType == TErrorPointer32.class) {
        // if (checkMemoryAccess()) {
        //     if (canBeNullParam && address == 0) {
        //         goto addressGood;
        //     }
        //     if (RuntimeContext.checkMemoryPointer(address)) {
        //         goto addressGood;
        //     }
        //     cpu.gpr[_v0] = SceKernelErrors.ERROR_INVALID_POINTER;
        //     pop all the parameters already prepared on the stack;
        //     goto afterSyscall;
        //     addressGood:
        // }
        // <parameterType> pointer = new <parameterType>(address);
        // if (parameterType == TErrorPointer32.class) {
        //     parameterReader.setHasErrorPointer(true);
        //     localVar[LOCAL_ERROR_POINTER] = pointer;
        // }
        mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(parameterType));
        mv.visitInsn(Opcodes.DUP);
        loadMemory();
        parameterReader.loadNextInt();

        boolean canBeNull = false;
        for (Annotation parameterAnnotation : parameterAnnotations) {
            if (parameterAnnotation instanceof CanBeNull) {
                canBeNull = true;
                break;
            }
        }

        if (checkMemoryAccess() && afterSyscallLabel != null) {
            Label addressGood = new Label();
            if (canBeNull) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitJumpInsn(Opcodes.IFEQ, addressGood);
            }
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName, "checkMemoryPointer", "(I)Z");
            mv.visitJumpInsn(Opcodes.IFNE, addressGood);
            storeRegister(_v0, SceKernelErrors.ERROR_INVALID_POINTER);
            parameterReader.popAllStack(4);
            mv.visitJumpInsn(Opcodes.GOTO, afterSyscallLabel);
            mv.visitLabel(addressGood);
        }
        if (parameterType == TPointer8.class || parameterType == TPointer16.class
                || parameterType == TPointer32.class || parameterType == TPointer64.class) {
            loadImm(canBeNull);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(parameterType), "<init>",
                    "(" + memoryDescriptor + "IZ)V");
        } else {
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(parameterType), "<init>",
                    "(" + memoryDescriptor + "I)V");
        }
        if (parameterType == TErrorPointer32.class) {
            parameterReader.setHasErrorPointer(true);
            mv.visitInsn(Opcodes.DUP);
            mv.visitVarInsn(Opcodes.ASTORE, LOCAL_ERROR_POINTER);
        }
        parameterReader.incrementCurrentStackSize();
    } else if (pspAbstractMemoryMappedStructure.class.isAssignableFrom(parameterType)) {
        parameterReader.loadNextInt();

        boolean canBeNull = false;
        for (Annotation parameterAnnotation : parameterAnnotations) {
            if (parameterAnnotation instanceof CanBeNull) {
                canBeNull = true;
                break;
            }
        }

        if (checkMemoryAccess() && afterSyscallLabel != null) {
            Label addressGood = new Label();
            if (canBeNull) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitJumpInsn(Opcodes.IFEQ, addressGood);
            }
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName, "checkMemoryPointer", "(I)Z");
            mv.visitJumpInsn(Opcodes.IFNE, addressGood);
            storeRegister(_v0, SceKernelErrors.ERROR_INVALID_POINTER);
            parameterReader.popAllStack(1);
            mv.visitJumpInsn(Opcodes.GOTO, afterSyscallLabel);
            mv.visitLabel(addressGood);
        }

        mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(parameterType));
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(parameterType), "<init>", "()V");
        mv.visitInsn(Opcodes.DUP_X1);
        mv.visitInsn(Opcodes.SWAP);
        loadMemory();
        mv.visitInsn(Opcodes.SWAP);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(parameterType), "read",
                "(" + memoryDescriptor + "I)V");
        parameterReader.incrementCurrentStackSize();
    } else {
        HLEUidClass hleUidClass = parameterType.getAnnotation(HLEUidClass.class);
        if (hleUidClass != null) {
            int errorValueOnNotFound = hleUidClass.errorValueOnNotFound();

            // <parameterType> uidObject = HLEUidObjectMapping.getObject("<parameterType>", uid);
            // if (uidObject == null) {
            //     cpu.gpr[_v0] = errorValueOnNotFound;
            //     pop all the parameters already prepared on the stack;
            //     goto afterSyscall;
            // }
            mv.visitLdcInsn(parameterType.getName());
            // Load the UID
            parameterReader.loadNextInt();

            // Load the UID Object
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HLEUidObjectMapping.class),
                    "getObject",
                    "(" + Type.getDescriptor(String.class) + "I)" + Type.getDescriptor(Object.class));
            if (afterSyscallLabel != null) {
                Label foundUid = new Label();
                mv.visitInsn(Opcodes.DUP);
                mv.visitJumpInsn(Opcodes.IFNONNULL, foundUid);
                storeRegister(_v0, errorValueOnNotFound);
                parameterReader.popAllStack(1);
                mv.visitJumpInsn(Opcodes.GOTO, afterSyscallLabel);
                mv.visitLabel(foundUid);
            }
            mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(parameterType));
            parameterReader.incrementCurrentStackSize();
        } else {
            log.error(String.format("Unsupported sycall parameter type '%s'", parameterType.getName()));
            Emulator.PauseEmuWithStatus(Emulator.EMU_STATUS_UNIMPLEMENTED);
        }
    }

    Method methodToCheck = null;
    if (afterSyscallLabel != null) {
        for (Annotation parameterAnnotation : parameterAnnotations) {
            if (parameterAnnotation instanceof CheckArgument) {
                CheckArgument checkArgument = (CheckArgument) parameterAnnotation;
                try {
                    methodToCheck = func.getHLEModule().getClass().getMethod(checkArgument.value(),
                            parameterType);
                } catch (Exception e) {
                    log.error(String.format("CheckArgument method '%s' not found in %s", checkArgument.value(),
                            func.getModuleName()), e);
                }
                break;
            }
        }
    }

    if (methodToCheck != null) {
        // try {
        //     parameterValue = <module>.<methodToCheck>(parameterValue);
        // } catch (SceKernelErrorException e) {
        //     goto catchSceKernelErrorException;
        // }
        loadModule(func.getModuleName());
        mv.visitInsn(Opcodes.SWAP);

        Label tryStart = new Label();
        Label tryEnd = new Label();
        mv.visitTryCatchBlock(tryStart, tryEnd, catchSceKernelErrorException,
                Type.getInternalName(SceKernelErrorException.class));

        mv.visitLabel(tryStart);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(methodToCheck.getDeclaringClass()),
                methodToCheck.getName(),
                "(" + Type.getDescriptor(parameterType) + ")" + Type.getDescriptor(parameterType));
        mv.visitLabel(tryEnd);
    }

    parameterReader.incrementCurrentParameterIndex();
}

From source file:jpcsp.Allegrex.compiler.CompilerContext.java

License:Open Source License

private void logSyscall(HLEModuleFunction func, String logPrefix, String logCheckFunction, String logFunction) {
    // Modules.getLogger(func.getModuleName()).warn("Unimplemented...");
    loadModuleLoggger(func);//w w w  .  j  a  v  a2 s .  c  o  m
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), logCheckFunction, "()Z");
    Label loggingDisabled = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, loggingDisabled);

    loadModuleLoggger(func);

    StringBuilder formatString = new StringBuilder();
    if (logPrefix != null) {
        formatString.append(logPrefix);
    }
    formatString.append(func.getFunctionName());
    ParameterInfo[] parameters = new ClassAnalyzer().getParameters(func.getFunctionName(),
            func.getHLEModuleMethod().getDeclaringClass());
    if (parameters != null) {
        // Log message:
        //    String.format(
        //       "Unimplemented <function name>
        //                 <parameterIntegerName>=0x%X,
        //                 <parameterBooleanName>=%b,
        //                 <parameterLongName>=0x%X,
        //                 <parameterFloatName>=%f,
        //                 <parameterOtherTypeName>=%s",
        //       new Object[] {
        //                 new Integer(parameterValueInteger),
        //                 new Boolean(parameterValueBoolean),
        //                 new Long(parameterValueLong),
        //                 new Float(parameterValueFloat),
        //                 parameterValueOtherTypes
        //       })
        loadImm(parameters.length);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(Object.class));
        CompilerParameterReader parameterReader = new CompilerParameterReader(this);
        Annotation[][] paramsAnotations = func.getHLEModuleMethod().getParameterAnnotations();
        int objectArrayIndex = 0;
        for (int paramIndex = 0; paramIndex < parameters.length; paramIndex++) {
            ParameterInfo parameter = parameters[paramIndex];
            Class<?> parameterType = parameter.type;
            CompilerTypeInformation typeInformation = compilerTypeManager
                    .getCompilerTypeInformation(parameterType);

            mv.visitInsn(Opcodes.DUP);
            loadImm(objectArrayIndex);

            formatString.append(paramIndex > 0 ? ", " : " ");
            formatString.append(parameter.name);
            formatString.append("=");
            formatString.append(typeInformation.formatString);

            if (typeInformation.boxingTypeInternalName != null) {
                mv.visitTypeInsn(Opcodes.NEW, typeInformation.boxingTypeInternalName);
                mv.visitInsn(Opcodes.DUP);
            }

            loadParameter(parameterReader, func, parameterType, paramsAnotations[paramIndex], null, null);

            if (typeInformation.boxingTypeInternalName != null) {
                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, typeInformation.boxingTypeInternalName, "<init>",
                        typeInformation.boxingMethodDescriptor);
            }
            mv.visitInsn(Opcodes.AASTORE);

            objectArrayIndex++;
        }
        mv.visitLdcInsn(formatString.toString());
        mv.visitInsn(Opcodes.SWAP);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "format",
                "(" + Type.getDescriptor(String.class) + "[" + Type.getDescriptor(Object.class) + ")"
                        + Type.getDescriptor(String.class));
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), logFunction,
                "(" + Type.getDescriptor(Object.class) + ")V");

        parameterReader = new CompilerParameterReader(this);
        for (int paramIndex = 0; paramIndex < parameters.length; paramIndex++) {
            ParameterInfo parameter = parameters[paramIndex];
            Class<?> parameterType = parameter.type;

            LengthInfo lengthInfo = BufferInfo.defaultLengthInfo;
            int length = BufferInfo.defaultLength;
            Usage usage = BufferInfo.defaultUsage;
            for (Annotation parameterAnnotation : paramsAnotations[paramIndex]) {
                if (parameterAnnotation instanceof BufferInfo) {
                    BufferInfo bufferInfo = (BufferInfo) parameterAnnotation;
                    lengthInfo = bufferInfo.lengthInfo();
                    length = bufferInfo.length();
                    usage = bufferInfo.usage();
                }
            }

            boolean parameterRead = false;
            if ((usage == Usage.in || usage == Usage.inout)
                    && (lengthInfo != LengthInfo.unknown || parameterType == TPointer16.class
                            || parameterType == TPointer32.class || parameterType == TPointer64.class)) {
                loadModuleLoggger(func);
                loadImm(1);
                mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(Object.class));
                mv.visitInsn(Opcodes.DUP);
                loadImm(0);

                Label done = new Label();
                Label addressNull = new Label();
                parameterReader.loadNextInt();
                parameterRead = true;
                mv.visitInsn(Opcodes.DUP);
                mv.visitJumpInsn(Opcodes.IFEQ, addressNull);

                String format = String.format("%s[%s]:%%s", parameter.name, usage);
                boolean useMemoryDump = true;

                switch (lengthInfo) {
                case fixedLength:
                    loadImm(length);
                    break;
                case nextNextParameter:
                    parameterReader.skipNextInt();
                    paramIndex++;
                    parameterReader.loadNextInt();
                    paramIndex++;
                    break;
                case nextParameter:
                    parameterReader.loadNextInt();
                    paramIndex++;
                    break;
                case previousParameter:
                    // Go back to the address parameter
                    parameterReader.rewindPreviousInt();
                    // Go back to the previous parameter
                    parameterReader.rewindPreviousInt();
                    // Load the length from the previous parameter
                    parameterReader.loadNextInt();
                    // Skip again the address parameter
                    // to come back to the above situation
                    parameterReader.skipNextInt();
                    break;
                case variableLength:
                    mv.visitInsn(Opcodes.DUP);
                    loadMemory();
                    mv.visitInsn(Opcodes.SWAP);
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read32", "(I)I");
                    break;
                case unknown:
                    useMemoryDump = false;
                    format = String.format("%s[%s]: 0x%%X", parameter.name, usage);
                    loadMemory();
                    mv.visitInsn(Opcodes.SWAP);
                    if (parameterType == TPointer64.class) {
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read64", "(I)J");
                        mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Long.class));
                        mv.visitInsn(Opcodes.DUP);
                        mv.visitInsn(Opcodes.DUP2_X2);
                        mv.visitInsn(Opcodes.POP2);
                        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Long.class), "<init>",
                                "(J)V");
                    } else if (parameterType == TPointer16.class) {
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read16", "(I)I");
                        mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Integer.class));
                        mv.visitInsn(Opcodes.DUP_X1);
                        mv.visitInsn(Opcodes.SWAP);
                        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Integer.class), "<init>",
                                "(I)V");
                    } else {
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read32", "(I)I");
                        mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Integer.class));
                        mv.visitInsn(Opcodes.DUP_X1);
                        mv.visitInsn(Opcodes.SWAP);
                        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Integer.class), "<init>",
                                "(I)V");
                    }
                    break;
                default:
                    log.error(String.format("Unimplemented lengthInfo=%s", lengthInfo));
                    break;
                }

                if (useMemoryDump) {
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Utilities.class),
                            "getMemoryDump", "(II)" + Type.getDescriptor(String.class));
                }
                mv.visitInsn(Opcodes.AASTORE);

                mv.visitLdcInsn(format);
                mv.visitInsn(Opcodes.SWAP);
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "format",
                        "(" + Type.getDescriptor(String.class) + "[" + Type.getDescriptor(Object.class) + ")"
                                + Type.getDescriptor(String.class));
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), logFunction,
                        "(" + Type.getDescriptor(Object.class) + ")V");
                mv.visitJumpInsn(Opcodes.GOTO, done);

                mv.visitLabel(addressNull);
                mv.visitInsn(Opcodes.POP);
                mv.visitInsn(Opcodes.POP2);
                mv.visitInsn(Opcodes.POP2);
                mv.visitLabel(done);
            }

            if (!parameterRead) {
                if (parameterType == long.class) {
                    parameterReader.skipNextLong();
                } else if (parameterType == float.class) {
                    parameterReader.skipNextFloat();
                } else {
                    parameterReader.skipNextInt();
                }
            }
        }
    } else {
        mv.visitLdcInsn(formatString.toString());
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), logFunction,
                "(" + Type.getDescriptor(Object.class) + ")V");
    }

    mv.visitLabel(loggingDisabled);
}

From source file:jpcsp.Allegrex.compiler.CompilerContext.java

License:Open Source License

private void logSyscallEnd(HLEModuleFunction func, boolean isErrorCode) {
    String loggingLevel = getLoggingLevel(func);
    if (loggingLevel == null) {
        return;//from  ww w .j a  va2s  .  c om
    }
    String logCheckFunction = getLogCheckFunction(loggingLevel);

    // if (Modules.getLogger(func.getModuleName()).isDebugEnabled()) {
    //     Modules.getLogger(func.getModuleName()).debug(String.format("<function name> returning 0x%X", new Object[1] { new Integer(returnValue) }));
    // }
    loadModuleLoggger(func);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), logCheckFunction, "()Z");
    Label notDebug = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, notDebug);

    boolean isReturningVoid = func.getHLEModuleMethod().getReturnType() == void.class;

    mv.visitInsn(Opcodes.DUP);
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Integer.class));
    mv.visitInsn(Opcodes.DUP_X1);
    mv.visitInsn(Opcodes.SWAP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Integer.class), "<init>", "(I)V");
    loadImm(1);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(Object.class));
    mv.visitInsn(Opcodes.DUP_X1);
    mv.visitInsn(Opcodes.SWAP);
    loadImm(0);
    mv.visitInsn(Opcodes.SWAP);
    mv.visitInsn(Opcodes.AASTORE);
    String prefix = func.isUnimplemented() && !codeBlock.isHLEFunction() ? "Unimplemented " : "";
    mv.visitLdcInsn(String.format("%s%s returning %s%s", prefix, func.getFunctionName(),
            isErrorCode ? "errorCode " : "", isReturningVoid ? "void" : "0x%X"));
    mv.visitInsn(Opcodes.SWAP);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "format",
            "(" + Type.getDescriptor(String.class) + "[" + Type.getDescriptor(Object.class) + ")"
                    + Type.getDescriptor(String.class));
    loadModuleLoggger(func);
    mv.visitInsn(Opcodes.SWAP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), loggingLevel,
            "(" + Type.getDescriptor(Object.class) + ")V");

    if (!isErrorCode) {
        ParameterInfo[] parameters = new ClassAnalyzer().getParameters(func.getFunctionName(),
                func.getHLEModuleMethod().getDeclaringClass());
        if (parameters != null) {
            CompilerParameterReader parameterReader;
            if (parametersSavedToLocals) {
                parameterReader = new CompilerLocalVarParameterReader(this, LOCAL_FIRST_SAVED_PARAMETER);
            } else {
                parameterReader = new CompilerParameterReader(this);
            }

            Annotation[][] paramsAnotations = func.getHLEModuleMethod().getParameterAnnotations();
            for (int paramIndex = 0; paramIndex < parameters.length; paramIndex++) {
                ParameterInfo parameter = parameters[paramIndex];
                Class<?> parameterType = parameter.type;

                LengthInfo lengthInfo = BufferInfo.defaultLengthInfo;
                int length = BufferInfo.defaultLength;
                Usage usage = BufferInfo.defaultUsage;
                boolean debugMemory = false;
                for (Annotation parameterAnnotation : paramsAnotations[paramIndex]) {
                    if (parameterAnnotation instanceof BufferInfo) {
                        BufferInfo bufferInfo = (BufferInfo) parameterAnnotation;
                        lengthInfo = bufferInfo.lengthInfo();
                        length = bufferInfo.length();
                        usage = bufferInfo.usage();
                    } else if (parameterAnnotation instanceof DebugMemory) {
                        debugMemory = true;
                    }
                }

                boolean parameterRead = false;
                if ((usage == Usage.out || usage == Usage.inout)
                        && (lengthInfo != LengthInfo.unknown || parameterType == TPointer16.class
                                || parameterType == TPointer32.class || parameterType == TPointer64.class)) {
                    loadModuleLoggger(func);
                    loadImm(1);
                    mv.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(Object.class));
                    mv.visitInsn(Opcodes.DUP);
                    loadImm(0);

                    Label done = new Label();
                    Label addressNull = new Label();
                    parameterReader.loadNextInt();
                    parameterRead = true;
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitJumpInsn(Opcodes.IFEQ, addressNull);

                    String format = String.format("%s[%s]:%%s", parameter.name, usage);
                    boolean useMemoryDump = true;

                    switch (lengthInfo) {
                    case fixedLength:
                        loadImm(length);
                        break;
                    case nextNextParameter:
                        parameterReader.skipNextInt();
                        paramIndex++;
                        parameterReader.loadNextInt();
                        paramIndex++;
                        break;
                    case nextParameter:
                        parameterReader.loadNextInt();
                        paramIndex++;
                        break;
                    case previousParameter:
                        // Go back to the address parameter
                        parameterReader.rewindPreviousInt();
                        // Go back to the previous parameter
                        parameterReader.rewindPreviousInt();
                        // Load the length from the previous parameter
                        parameterReader.loadNextInt();
                        // Skip again the address parameter
                        // to come back to the above situation
                        parameterReader.skipNextInt();
                        break;
                    case variableLength:
                        mv.visitInsn(Opcodes.DUP);
                        loadMemory();
                        mv.visitInsn(Opcodes.SWAP);
                        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read32", "(I)I");
                        break;
                    case returnValue:
                        loadRegister(_v0);
                        break;
                    case unknown:
                        useMemoryDump = false;
                        format = String.format("%s[%s]: 0x%%X", parameter.name, usage);
                        loadMemory();
                        mv.visitInsn(Opcodes.SWAP);
                        if (parameterType == TPointer64.class) {
                            if (debugMemory) {
                                mv.visitInsn(Opcodes.DUP);
                                loadImm(8);
                                mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName,
                                        "debugMemory", "(II)V");
                            }
                            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read64", "(I)J");
                            mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Long.class));
                            mv.visitInsn(Opcodes.DUP);
                            mv.visitInsn(Opcodes.DUP2_X2);
                            mv.visitInsn(Opcodes.POP2);
                            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Long.class),
                                    "<init>", "(J)V");
                        } else if (parameterType == TPointer16.class) {
                            if (debugMemory) {
                                mv.visitInsn(Opcodes.DUP);
                                loadImm(2);
                                mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName,
                                        "debugMemory", "(II)V");
                            }
                            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read16", "(I)I");
                            mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Integer.class));
                            mv.visitInsn(Opcodes.DUP_X1);
                            mv.visitInsn(Opcodes.SWAP);
                            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Integer.class),
                                    "<init>", "(I)V");
                        } else {
                            if (debugMemory) {
                                mv.visitInsn(Opcodes.DUP);
                                loadImm(4);
                                mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName,
                                        "debugMemory", "(II)V");
                            }
                            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, memoryInternalName, "read32", "(I)I");
                            mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(Integer.class));
                            mv.visitInsn(Opcodes.DUP_X1);
                            mv.visitInsn(Opcodes.SWAP);
                            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Integer.class),
                                    "<init>", "(I)V");
                        }
                        break;
                    default:
                        log.error(String.format("Unimplemented lengthInfo=%s", lengthInfo));
                        break;
                    }

                    if (useMemoryDump) {
                        if (debugMemory) {
                            mv.visitInsn(Opcodes.DUP2);
                            mv.visitMethodInsn(Opcodes.INVOKESTATIC, runtimeContextInternalName, "debugMemory",
                                    "(II)V");
                        }
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Utilities.class),
                                "getMemoryDump", "(II)" + Type.getDescriptor(String.class));
                    }
                    mv.visitInsn(Opcodes.AASTORE);

                    mv.visitLdcInsn(format);
                    mv.visitInsn(Opcodes.SWAP);
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(String.class), "format",
                            "(" + Type.getDescriptor(String.class) + "[" + Type.getDescriptor(Object.class)
                                    + ")" + Type.getDescriptor(String.class));
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Logger.class), loggingLevel,
                            "(" + Type.getDescriptor(Object.class) + ")V");
                    mv.visitJumpInsn(Opcodes.GOTO, done);

                    mv.visitLabel(addressNull);
                    mv.visitInsn(Opcodes.POP);
                    mv.visitInsn(Opcodes.POP2);
                    mv.visitInsn(Opcodes.POP2);
                    mv.visitLabel(done);
                }

                if (!parameterRead) {
                    if (parameterType == long.class) {
                        parameterReader.skipNextLong();
                    } else if (parameterType == float.class) {
                        parameterReader.skipNextFloat();
                    } else {
                        parameterReader.skipNextInt();
                    }
                }
            }
        }
    }

    mv.visitLabel(notDebug);
}

From source file:jpcsp.Allegrex.compiler.CompilerContext.java

License:Open Source License

private void flushInstructionCount(boolean local, boolean last) {
    if (enableIntructionCounting) {
        if (local) {
            if (currentInstructionCount > 0) {
                mv.visitIincInsn(LOCAL_INSTRUCTION_COUNT, currentInstructionCount);
            }//  ww w.  jav  a2  s.  co m
        } else {
            mv.visitFieldInsn(Opcodes.GETSTATIC, runtimeContextInternalName, "currentThread",
                    sceKernalThreadInfoDescriptor);
            mv.visitInsn(Opcodes.DUP);
            mv.visitFieldInsn(Opcodes.GETFIELD, sceKernalThreadInfoInternalName, "runClocks", "J");
            loadLocalVar(LOCAL_INSTRUCTION_COUNT);
            if (currentInstructionCount > 0) {
                loadImm(currentInstructionCount);
                mv.visitInsn(Opcodes.IADD);
            }
            if (Profiler.isProfilerEnabled()) {
                mv.visitInsn(Opcodes.DUP);
                loadImm(getCodeBlock().getStartAddress());
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, profilerInternalName, "addInstructionCount", "(II)V");
            }
            mv.visitInsn(Opcodes.I2L);
            mv.visitInsn(Opcodes.LADD);
            mv.visitFieldInsn(Opcodes.PUTFIELD, sceKernalThreadInfoInternalName, "runClocks", "J");
            if (!last) {
                mv.visitInsn(Opcodes.ICONST_0);
                storeLocalVar(LOCAL_INSTRUCTION_COUNT);
            }
        }
        currentInstructionCount = 0;
    }
}