Search in sources :

Example 21 with Type

use of org.objectweb.asm.Type in project semantic-versioning by jeluard.

the class StreamDiffHandler method addMethodNodes.

/**
     * Add the method nodes for the method descriptor.
     * This writes out an <arguments> node containing the 
     * argument types for the method, followed by a <return> node
     * containing the return type.
     *
     * @param desc The descriptor for the method to write out.
     * @throws IOException when there is an underlying IOException.
     */
protected void addMethodNodes(String desc) throws IOException {
    Type[] args = Type.getArgumentTypes(desc);
    Type ret = Type.getReturnType(desc);
    out.write("<arguments>");
    for (int i = 0; i < args.length; i++) addTypeNode(args[i]);
    out.write("</arguments>");
    out.write("<return>");
    addTypeNode(ret);
    out.write("</return>");
}
Also used : Type(org.objectweb.asm.Type)

Example 22 with Type

use of org.objectweb.asm.Type in project semantic-versioning by jeluard.

the class DOMDiffHandler method addMethodNodes.

/**
     * Add the method nodes for the method descriptor.
     * This writes out an &lt;arguments&gt; node containing the 
     * argument types for the method, followed by a &lt;return&gt; node
     * containing the return type.
     *
     * @param desc The descriptor for the method to write out.
     */
protected void addMethodNodes(String desc) {
    Type[] args = Type.getArgumentTypes(desc);
    Type ret = Type.getReturnType(desc);
    Node currentNode = this.currentNode;
    Element tmp = doc.createElementNS(XML_URI, "arguments");
    currentNode.appendChild(tmp);
    this.currentNode = tmp;
    for (int i = 0; i < args.length; i++) addTypeNode(args[i]);
    tmp = doc.createElementNS(XML_URI, "return");
    currentNode.appendChild(tmp);
    this.currentNode = tmp;
    addTypeNode(ret);
    this.currentNode = currentNode;
}
Also used : Type(org.objectweb.asm.Type)

Example 23 with Type

use of org.objectweb.asm.Type in project enumerable by hraberg.

the class ExpressionInterpreter method newOperation.

public Value newOperation(final AbstractInsnNode insn) throws AnalyzerException {
    switch(insn.getOpcode()) {
        case ACONST_NULL:
            return new ExpressionValue(createClassOrInterfaceType(Object.class.getName()), new NullLiteralExpr());
        case ICONST_M1:
            return new ExpressionValue(PRIMITIVE_INT, new UnaryExpr(new IntegerLiteralExpr("1"), UnaryExpr.Operator.negative));
        case ICONST_0:
            return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("0"));
        case ICONST_1:
            return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("1"));
        case ICONST_2:
            return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("2"));
        case ICONST_3:
            return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("3"));
        case ICONST_4:
            return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("4"));
        case ICONST_5:
            return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("5"));
        case LCONST_0:
            return new ExpressionValue(PRIMITIVE_LONG, new LongLiteralExpr("0L"));
        case LCONST_1:
            return new ExpressionValue(PRIMITIVE_LONG, new LongLiteralExpr("1L"));
        case FCONST_0:
            return new ExpressionValue(PRIMITIVE_FLOAT, new DoubleLiteralExpr("0.0f"));
        case FCONST_1:
            return new ExpressionValue(PRIMITIVE_FLOAT, new DoubleLiteralExpr("1.0f"));
        case FCONST_2:
            return new ExpressionValue(PRIMITIVE_FLOAT, new DoubleLiteralExpr("2.0f"));
        case DCONST_0:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new DoubleLiteralExpr("0.0"));
        case DCONST_1:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new DoubleLiteralExpr("1.0"));
        case BIPUSH:
        case SIPUSH:
            int operand = ((IntInsnNode) insn).operand;
            if (operand < 0)
                return new ExpressionValue(PRIMITIVE_INT, new UnaryExpr(new IntegerLiteralExpr("" + Math.abs(operand)), UnaryExpr.Operator.negative));
            return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("" + operand));
        case LDC:
            Object cst = ((LdcInsnNode) insn).cst;
            if (cst instanceof Number) {
                ExpressionValue value = null;
                if (cst instanceof Integer) {
                    value = new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr(cst.toString()));
                } else if (cst instanceof Float) {
                    value = new ExpressionValue(PRIMITIVE_FLOAT, new DoubleLiteralExpr(cst.toString() + "f"));
                } else if (cst instanceof Long) {
                    value = new ExpressionValue(PRIMITIVE_LONG, new LongLiteralExpr(cst.toString() + "L"));
                } else if (cst instanceof Double) {
                    value = new ExpressionValue(PRIMITIVE_DOUBLE, new DoubleLiteralExpr(cst.toString()));
                }
                if (((Number) cst).intValue() < 0) {
                    StringLiteralExpr expr = (StringLiteralExpr) value.expression;
                    expr.setValue(expr.getValue().substring("-".length()));
                    value.expression = new UnaryExpr(expr, UnaryExpr.Operator.negative);
                }
                return value;
            } else if (cst instanceof Type) {
                ClassExpr classExpr = new ClassExpr(new ReferenceType(createClassOrInterfaceType(((Type) cst).getClassName())));
                return new ExpressionValue(createClassOrInterfaceType(Class.class.getName()), classExpr);
            } else {
                return new ExpressionValue(createClassOrInterfaceType(String.class.getName()), new StringLiteralExpr(cst.toString()));
            }
        case JSR:
            throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
        case GETSTATIC:
            FieldInsnNode fieldNode = (FieldInsnNode) insn;
            ExpressionValue getField = (ExpressionValue) newValue(getType(fieldNode.desc));
            getField.expression = new FieldAccessExpr(new NameExpr(removeJavaLang(getObjectType(fieldNode.owner).getClassName())), fieldNode.name);
            return getField;
        case NEW:
            return newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
        default:
            throw new Error("Internal error.");
    }
}
Also used : IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) ReferenceType(japa.parser.ast.type.ReferenceType) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) Type(org.objectweb.asm.Type) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) ReferenceType(japa.parser.ast.type.ReferenceType) PrimitiveType(japa.parser.ast.type.PrimitiveType)

Example 24 with Type

use of org.objectweb.asm.Type in project jphp by jphp-compiler.

the class ClassStmtCompiler method writeDefaultConstructors.

protected void writeDefaultConstructors() {
    if (!isSystem && !isClosure() && entity.getParent() != null && entity.getParent().getNativeClass() != null && !BaseObject.class.isAssignableFrom(entity.getParent().getNativeClass())) {
        for (Constructor el : entity.getParent().getNativeClass().getConstructors()) {
            Class<?>[] parameterTypes = el.getParameterTypes();
            if (parameterTypes.length == 2 && parameterTypes[0] == Environment.class && parameterTypes[1] == ClassEntity.class) {
                continue;
            }
            MethodNode constructor = new MethodNodeImpl();
            constructor.name = Constants.INIT_METHOD;
            constructor.access = el.getModifiers();
            constructor.exceptions = new ArrayList();
            MethodStmtCompiler methodCompiler = new MethodStmtCompiler(this, constructor);
            ExpressionStmtCompiler expressionCompiler = new ExpressionStmtCompiler(methodCompiler, null);
            LabelNode l0 = writeLabel(constructor, statement.getMeta().getStartLine());
            methodCompiler.addLocalVariable("~this", l0);
            Type[] argumentTypes = new Type[parameterTypes.length];
            int i = 0;
            for (Class type : parameterTypes) {
                argumentTypes[i++] = Type.getType(type);
                methodCompiler.addLocalVariable("arg" + i, l0, type);
            }
            constructor.desc = Type.getMethodDescriptor(Type.getType(void.class), argumentTypes);
            methodCompiler.writeHeader();
            expressionCompiler.writeVarLoad("~this");
            for (i = 0; i < argumentTypes.length; i++) {
                expressionCompiler.writeVarLoad("arg" + (i + 1));
            }
            constructor.instructions.add(new MethodInsnNode(INVOKESPECIAL, node.superName, Constants.INIT_METHOD, constructor.desc, false));
            methodCompiler.writeFooter();
            constructor.instructions.add(new InsnNode(RETURN));
            node.methods.add(constructor);
        }
    }
}
Also used : Constructor(java.lang.reflect.Constructor) MethodNodeImpl(org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl) ErrorType(php.runtime.exceptions.support.ErrorType) Type(org.objectweb.asm.Type)

Example 25 with Type

use of org.objectweb.asm.Type in project jphp by jphp-compiler.

the class ExpressionStmtCompiler method writeUnaryOperator.

Memory writeUnaryOperator(OperatorExprToken operator, boolean returnValue, boolean writeOpcode) {
    if (stackEmpty(true))
        unexpectedToken(operator);
    StackItem o = stackPop();
    ValueExprToken L = o.getToken();
    Memory mem = tryWritePush(o, false, false, true);
    StackItem.Type type = tryGetType(o);
    if (mem != null) {
        Memory result = CompilerUtils.calcUnary(getCompiler().getEnvironment(), operator.toTraceInfo(getCompiler().getContext()), mem, operator);
        if (operator instanceof ValueIfElseToken) {
            ValueIfElseToken valueIfElseToken = (ValueIfElseToken) operator;
            ExprStmtToken ret = valueIfElseToken.getValue();
            if (mem.toBoolean()) {
                if (ret == null)
                    result = mem;
                else
                    result = writeExpression(ret, true, true, false);
            } else {
                result = writeExpression(valueIfElseToken.getAlternative(), true, true, false);
            }
        } else if (operator instanceof ArrayGetExprToken && !(operator instanceof ArrayGetRefExprToken)) {
        // TODO: check!!!
        /*Memory array = mem;
                ArrayGetExprToken arrayGet = (ArrayGetExprToken)operator;
                for(ExprStmtToken expr : arrayGet.getParameters()){
                    Memory key = writeExpression(expr, true, true, false);
                    if (key == null)
                        break;
                    result = array = array.valueOfIndex(key).toImmutable();
                }*/
        }
        if (result != null) {
            stackPush(result);
            setStackPeekAsImmutable();
            return result;
        }
    }
    if (!writeOpcode)
        return null;
    writeLineNumber(operator);
    String name = operator.getCode();
    Class operatorResult = operator.getResultClass();
    LocalVariable variable = null;
    if (L instanceof VariableExprToken) {
        variable = method.getLocalVariable(((VariableExprToken) L).getName());
        if (operator instanceof ArrayPushExprToken || operator instanceof ArrayGetRefExprToken)
            variable.setValue(null);
    }
    if (operator instanceof IncExprToken || operator instanceof DecExprToken) {
        if (variable == null || variable.isReference()) {
            if (operator.getAssociation() == Association.LEFT && returnValue) {
                writePush(o);
                if (stackPeek().type.isConstant())
                    unexpectedToken(operator);
                writePushDup();
                writePopImmutable();
                code.add(new InsnNode(SWAP));
                writePushDup();
            } else {
                writePush(o);
                if (stackPeek().type.isConstant())
                    unexpectedToken(operator);
                writePushDup();
            }
            writeSysDynamicCall(Memory.class, name, operatorResult);
            writeSysDynamicCall(Memory.class, "assign", Memory.class, operatorResult);
            if (!returnValue || operator.getAssociation() == Association.LEFT) {
                writePopAll(1);
            }
        } else {
            writePush(o);
            if (stackPeek().type.isConstant())
                unexpectedToken(operator);
            if (operator.getAssociation() == Association.LEFT && returnValue) {
                writeVarLoad(variable);
            }
            writeSysDynamicCall(Memory.class, name, operatorResult);
            // TODO for constant values
            variable.setValue(null);
            if (operator.getAssociation() == Association.RIGHT)
                writeVarStore(variable, returnValue);
            else {
                writeVarStore(variable, false);
            }
        }
    } else if (operator instanceof AmpersandRefToken) {
        writePush(o, false, false);
        setStackPeekAsImmutable();
        Token token = o.getToken();
        if (token instanceof VariableExprToken) {
            LocalVariable local = method.getLocalVariable(((VariableExprToken) token).getName());
            local.setValue(null);
        }
    } else if (operator instanceof SilentToken) {
        writePushEnv();
        writeSysDynamicCall(Environment.class, "__pushSilent", void.class);
        writePush(o);
        writePushEnv();
        writeSysDynamicCall(Environment.class, "__popSilent", void.class);
    } else if (operator instanceof ValueIfElseToken) {
        writePush(o);
        ValueIfElseToken valueIfElseToken = (ValueIfElseToken) operator;
        LabelNode end = new LabelNode();
        LabelNode elseL = new LabelNode();
        if (valueIfElseToken.getValue() == null) {
            StackItem.Type dup = stackPeek().type;
            writePushDup();
            writePopBoolean();
            code.add(new JumpInsnNode(Opcodes.IFEQ, elseL));
            stackPop();
            writePopBoxing();
            stackPop();
            code.add(new JumpInsnNode(Opcodes.GOTO, end));
            code.add(elseL);
            // remove duplicate of condition value , IMPORTANT!!!
            makePop(dup);
            writeExpression(valueIfElseToken.getAlternative(), true, false);
            writePopBoxing();
            code.add(end);
        } else {
            writePopBoolean();
            code.add(new JumpInsnNode(Opcodes.IFEQ, elseL));
            stackPop();
            writeExpression(valueIfElseToken.getValue(), true, false);
            writePopBoxing();
            stackPop();
            // goto end
            code.add(new JumpInsnNode(Opcodes.GOTO, end));
            // else
            code.add(elseL);
            writeExpression(valueIfElseToken.getAlternative(), true, false);
            writePopBoxing();
            code.add(end);
        }
        setStackPeekAsImmutable(false);
    } else if (operator instanceof ArrayGetExprToken) {
        stackPush(o);
        writeArrayGet((ArrayGetExprToken) operator, returnValue);
    } else if (operator instanceof CallOperatorToken) {
        stackPush(o);
        CallOperatorToken call = (CallOperatorToken) operator;
        writePushParameters(call.getParameters());
        writePushEnv();
        writePushTraceInfo(operator);
        writeSysStaticCall(InvokeHelper.class, "callAny", Memory.class, Memory.class, Memory[].class, Environment.class, TraceInfo.class);
        if (!returnValue)
            writePopAll(1);
    } else {
        writePush(o);
        writePopBoxing();
        if (operator.isEnvironmentNeeded() && operator.isTraceNeeded()) {
            writePushEnv();
            writePushTraceInfo(operator);
            writeSysDynamicCall(Memory.class, name, operatorResult, Environment.class, TraceInfo.class);
        } else if (operator.isEnvironmentNeeded()) {
            writePushEnv();
            writeSysDynamicCall(Memory.class, name, operatorResult, Environment.class);
        } else if (operator.isTraceNeeded()) {
            writePushTraceInfo(operator);
            writeSysDynamicCall(Memory.class, name, operatorResult, TraceInfo.class);
        } else
            writeSysDynamicCall(Memory.class, name, operatorResult);
        if (!returnValue) {
            writePopAll(1);
        }
    }
    return null;
}
Also used : UndefinedMemory(php.runtime.memory.helper.UndefinedMemory) Memory(php.runtime.Memory) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) OperatorExprToken(org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) ClassExprToken(org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken) OpenEchoTagToken(org.develnext.jphp.core.tokenizer.token.OpenEchoTagToken) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken) ObjectInvokeHelper(php.runtime.invoke.ObjectInvokeHelper) InvokeHelper(php.runtime.invoke.InvokeHelper) TraceInfo(php.runtime.env.TraceInfo) StackItem(org.develnext.jphp.core.compiler.common.misc.StackItem) Type(org.objectweb.asm.Type) Environment(php.runtime.env.Environment) CompileClass(php.runtime.ext.support.compile.CompileClass)

Aggregations

Type (org.objectweb.asm.Type)151 MethodVisitor (org.objectweb.asm.MethodVisitor)33 Label (org.objectweb.asm.Label)21 Method (org.objectweb.asm.commons.Method)16 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)14 ClassWriter (org.objectweb.asm.ClassWriter)13 ArrayList (java.util.ArrayList)11 ClassReader (org.objectweb.asm.ClassReader)10 Method (java.lang.reflect.Method)9 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)9 ClassVisitor (org.objectweb.asm.ClassVisitor)9 PropertyAccessorType (org.gradle.internal.reflect.PropertyAccessorType)7 ModelType (org.gradle.model.internal.type.ModelType)7 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)6 MethodType (java.lang.invoke.MethodType)6 List (java.util.List)6 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)5 InterceptorType (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorType)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4