Search in sources :

Example 46 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 47 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 48 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 49 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)

Example 50 with Type

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

the class ExpressionStmtCompiler method writeSysCall.

@SuppressWarnings("unchecked")
void writeSysCall(Class clazz, int INVOKE_TYPE, String method, Class returnClazz, Class... paramClasses) {
    if (INVOKE_TYPE != INVOKESPECIAL && clazz != null) {
        if (compiler.getScope().isDebugMode()) {
            if (!methodExists(clazz, method, paramClasses)) {
                throw new NoSuchMethodException(clazz, method, paramClasses);
            }
        }
    }
    Type[] args = new Type[paramClasses.length];
    if (INVOKE_TYPE == INVOKEVIRTUAL || INVOKE_TYPE == INVOKEINTERFACE)
        // this
        stackPop();
    for (int i = 0; i < args.length; i++) {
        args[i] = Type.getType(paramClasses[i]);
        stackPop();
    }
    String owner = clazz == null ? this.method.clazz.node.name : Type.getInternalName(clazz);
    if (clazz == null && this.method.clazz.entity.isTrait())
        throw new CriticalException("[Compiler Error] Cannot use current classname in Trait");
    code.add(new MethodInsnNode(INVOKE_TYPE, owner, method, Type.getMethodDescriptor(Type.getType(returnClazz), args), clazz != null && clazz.isInterface()));
    if (returnClazz != void.class) {
        stackPush(null, StackItem.Type.valueOf(returnClazz));
    }
}
Also used : Type(org.objectweb.asm.Type) CriticalException(php.runtime.exceptions.CriticalException)

Aggregations

Type (org.objectweb.asm.Type)185 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)39 MethodVisitor (org.objectweb.asm.MethodVisitor)34 Method (org.objectweb.asm.commons.Method)28 Label (org.objectweb.asm.Label)27 ClassWriter (org.objectweb.asm.ClassWriter)16 Method (java.lang.reflect.Method)13 ArrayList (java.util.ArrayList)12 ClassReader (org.objectweb.asm.ClassReader)10 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)9 ClassVisitor (org.objectweb.asm.ClassVisitor)9 ExprString (lucee.transformer.expression.ExprString)8 ModelType (org.gradle.model.internal.type.ModelType)7 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)6 IOException (java.io.IOException)6 MethodType (java.lang.invoke.MethodType)6 HashMap (java.util.HashMap)6 List (java.util.List)6 LitString (lucee.transformer.expression.literal.LitString)6 PropertyAccessorType (org.gradle.internal.reflect.PropertyAccessorType)6