Search in sources :

Example 16 with LocalVariable

use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.

the class ExpressionStmtCompiler method writeVariableAssign.

void writeVariableAssign(VariableExprToken variable, StackItem R, AssignExprToken operator, boolean returnValue) {
    LocalVariable local = method.getLocalVariable(variable.getName());
    checkAssignableVar(variable);
    Memory value = R.getMemory();
    writeLineNumber(variable);
    if (local.isReference()) {
        String name = "assign";
        if (operator.isAsReference())
            name = "assignRef";
        if (!R.isKnown()) {
            stackPush(R);
            writePopBoxing();
            writePopImmutable();
            writePushVariable(variable);
            writeSysStaticCall(Memory.class, name + "Right", Memory.class, stackPeek().type.toClass(), Memory.class);
        } else {
            writePushVariable(variable);
            Memory tmp = tryWritePush(R);
            if (tmp != null) {
                value = tmp;
                writePushMemory(value);
            }
            writePopBoxing();
            writePopImmutable();
            writeSysDynamicCall(Memory.class, name, Memory.class, stackPeek().type.toClass());
        }
        if (!returnValue)
            writePopAll(1);
    } else {
        Memory result = tryWritePush(R);
        if (result != null) {
            writePushMemory(result);
            value = result;
        }
        writePopBoxing();
        writeVarStore(local, returnValue, true);
    }
    /*if (!methodStatement.variable(variable).isPassed())
            local.setValue(value);

        if (methodStatement.isDynamicLocal())   */
    local.setValue(null);
}
Also used : UndefinedMemory(php.runtime.memory.helper.UndefinedMemory) Memory(php.runtime.Memory) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)

Example 17 with LocalVariable

use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.

the class TryCatchCompiler method write.

@Override
public void write(TryStmtToken token) {
    if (token.getBody() == null || token.getBody().getInstructions().isEmpty()) {
        if (token.getFinally() != null)
            expr.write(BodyStmtToken.class, token.getFinally());
        return;
    }
    expr.writeDefineVariables(token.getLocal());
    LabelNode tryStart = expr.writeLabel(node, token.getMeta().getStartLine());
    LabelNode tryEnd = new LabelNode();
    LabelNode catchStart = new LabelNode();
    LabelNode catchEnd = new LabelNode();
    LabelNode returnLabel = new LabelNode();
    method.node.tryCatchBlocks.add(0, new TryCatchBlockNode(tryStart, tryEnd, catchStart, Type.getInternalName(BaseBaseException.class)));
    if (token.getFinally() != null) {
        method.getTryStack().push(new MethodStmtCompiler.TryCatchItem(token, returnLabel));
    }
    expr.write(BodyStmtToken.class, token.getBody());
    if (token.getFinally() != null) {
        method.getTryStack().pop();
    }
    add(tryEnd);
    add(new JumpInsnNode(GOTO, catchEnd));
    add(catchStart);
    LocalVariable exception = method.addLocalVariable("~catch~" + method.nextStatementIndex(BaseException.class), catchStart, BaseBaseException.class);
    exception.setEndLabel(catchEnd);
    expr.makeVarStore(exception);
    LabelNode nextCatch = null;
    int i = 0, size = token.getCatches().size();
    LocalVariable local = null;
    LabelNode catchFail = new LabelNode();
    for (CatchStmtToken _catch : token.getCatches()) {
        if (nextCatch != null) {
            add(nextCatch);
        }
        if (i == size - 1) {
            nextCatch = catchFail;
        } else {
            nextCatch = new LabelNode();
        }
        local = method.getLocalVariable(_catch.getVariable().getName());
        expr.writePushEnv();
        expr.writeVarLoad(exception);
        expr.writePushConstString(_catch.getException().toName());
        expr.writePushConstString(_catch.getException().toName().toLowerCase());
        expr.writeSysDynamicCall(Environment.class, "__throwCatch", Memory.class, BaseBaseException.class, String.class, String.class);
        expr.writeVarAssign(local, _catch.getVariable(), true, false);
        expr.writePopBoolean();
        add(new JumpInsnNode(IFEQ, nextCatch));
        expr.stackPop();
        expr.write(BodyStmtToken.class, _catch.getBody());
        add(new JumpInsnNode(GOTO, catchEnd));
        i++;
    }
    add(catchFail);
    if (token.getFinally() != null) {
        expr.write(BodyStmtToken.class, token.getFinally());
    }
    /*if (method.getTryStack().empty()) {
            expr.writePushEnv();
            expr.writeVarLoad(exception);
            expr.writeSysDynamicCall(Environment.class, "__throwFailedCatch", void.class, BaseException.class);
        } else {*/
    expr.makeVarLoad(exception);
    add(new InsnNode(ATHROW));
    //}
    add(catchEnd);
    if (token.getFinally() != null) {
        LabelNode skip = new LabelNode();
        add(new JumpInsnNode(GOTO, skip));
        // finally for return
        add(returnLabel);
        expr.write(BodyStmtToken.class, token.getFinally());
        if (method.getTryStack().empty()) {
            // all finally blocks are done
            LocalVariable retVar = method.getOrAddLocalVariable("~result~", null, Memory.class);
            expr.writeVarLoad(retVar);
            add(new InsnNode(ARETURN));
            expr.stackPop();
        } else {
            // goto next finally block
            add(new JumpInsnNode(GOTO, method.getTryStack().peek().getReturnLabel()));
        }
        add(skip);
        // other finally
        expr.write(BodyStmtToken.class, token.getFinally());
    }
    expr.writeUndefineVariables(token.getLocal());
    method.prevStatementIndex(BaseBaseException.class);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) MethodStmtCompiler(org.develnext.jphp.core.compiler.jvm.statement.MethodStmtCompiler) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) CatchStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CatchStmtToken) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)

Example 18 with LocalVariable

use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.

the class ClosureValueCompiler method writePushUses.

protected void writePushUses(Collection<ArgumentStmtToken> parameters) {
    if (parameters.isEmpty()) {
        add(new InsnNode(ACONST_NULL));
        expr.stackPush(Memory.Type.REFERENCE);
        return;
    }
    expr.writePushSmallInt(parameters.size());
    add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Memory.class)));
    expr.stackPop();
    expr.stackPush(Memory.Type.REFERENCE);
    int i = 0;
    for (ArgumentStmtToken param : parameters) {
        expr.writePushDup();
        expr.writePushSmallInt(i);
        LocalVariable local = method.getLocalVariable(param.getName().getName());
        if (local == null)
            expr.writePushNull();
        else
            expr.writeVarLoad(local);
        if (!param.isReference())
            expr.writePopBoxing(true);
        add(new InsnNode(AASTORE));
        expr.stackPop();
        expr.stackPop();
        expr.stackPop();
        i++;
    }
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) ArgumentStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ArgumentStmtToken) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode)

Example 19 with LocalVariable

use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.

the class ExpressionStmtCompiler method writePushEnv.

public void writePushEnv() {
    method.entity.setUsesStackTrace(true);
    LocalVariable variable = method.getLocalVariable("~env");
    if (variable == null) {
        if (!methodStatement.isStatic())
            writePushEnvFromSelf();
        else
            throw new RuntimeException("Cannot find `~env` variable");
        return;
    }
    stackPush(Memory.Type.REFERENCE);
    makeVarLoad(variable);
}
Also used : LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)

Example 20 with LocalVariable

use of org.develnext.jphp.core.compiler.jvm.misc.LocalVariable in project jphp by jphp-compiler.

the class MethodStmtCompiler method addLocalVariable.

public LocalVariable addLocalVariable(String variable, LabelNode label, Class clazz) {
    LocalVariable result;
    localVariables.put(variable, result = new LocalVariable(variable, localVariables.size(), label, clazz));
    return result;
}
Also used : LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)

Aggregations

LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)23 Memory (php.runtime.Memory)11 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)6 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)5 LabelNode (org.objectweb.asm.tree.LabelNode)5 UndefinedMemory (php.runtime.memory.helper.UndefinedMemory)5 VariableExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken)4 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)4 Environment (php.runtime.env.Environment)4 Token (org.develnext.jphp.core.tokenizer.token.Token)3 InsnNode (org.objectweb.asm.tree.InsnNode)3 StackItem (org.develnext.jphp.core.compiler.common.misc.StackItem)2 DynamicAccessExprToken (org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken)2 GetVarExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.GetVarExprToken)2 ListExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken)2 BodyStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken)2 LineNumberNode (org.objectweb.asm.tree.LineNumberNode)2 CompileClass (php.runtime.ext.support.compile.CompileClass)2 IObject (php.runtime.lang.IObject)2 MethodNodeImpl (org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl)1