Search in sources :

Example 66 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project evosuite by EvoSuite.

the class RawControlFlowGraph method addSwitchBranchEdge.

private ControlFlowEdge addSwitchBranchEdge(BytecodeInstruction src, BytecodeInstruction target, boolean isExceptionEdge) {
    if (!target.isLabel())
        throw new IllegalStateException("expect control flow edges from switch statements to always target labelNodes");
    LabelNode label = (LabelNode) target.getASMNode();
    List<Branch> switchCaseBranches = BranchPool.getInstance(classLoader).getBranchForLabel(label);
    if (switchCaseBranches == null) {
        logger.debug("not a switch case label: " + label.toString() + " " + target.toString());
        return internalAddEdge(src, target, new ControlFlowEdge(isExceptionEdge));
    }
    for (Branch switchCaseBranch : switchCaseBranches) {
        // TODO n^2
        Set<ControlFlowEdge> soFar = incomingEdgesOf(target);
        boolean handled = false;
        for (ControlFlowEdge old : soFar) if (switchCaseBranch.equals(old.getBranchInstruction()))
            handled = true;
        if (handled)
            continue;
        /*
			 * previous try to add fake intermediate nodes for each empty case
			 * block to help the CDG - unsuccessful:
			 * if(switchCaseBranches.size()>1) { // // e = new
			 * ControlFlowEdge(isExceptionEdge); //
			 * e.setBranchInstruction(switchCaseBranch); //
			 * e.setBranchExpressionValue(true); // BytecodeInstruction
			 * fakeInstruction =
			 * BytecodeInstructionPool.createFakeInstruction(className
			 * ,methodName); // addVertex(fakeInstruction); //
			 * internalAddEdge(src,fakeInstruction,e); // // e = new
			 * ControlFlowEdge(isExceptionEdge); //
			 * e.setBranchInstruction(switchCaseBranch); //
			 * e.setBranchExpressionValue(true); // // e =
			 * internalAddEdge(fakeInstruction,target,e); // } else {
			 */
        ControlDependency cd = new ControlDependency(switchCaseBranch, true);
        ControlFlowEdge e = new ControlFlowEdge(cd, isExceptionEdge);
        e = internalAddEdge(src, target, e);
    }
    return new ControlFlowEdge(isExceptionEdge);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) Branch(org.evosuite.coverage.branch.Branch)

Example 67 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project soot by Sable.

the class AsmMethodSource method emitTraps.

private void emitTraps() {
    Chain<Trap> traps = body.getTraps();
    SootClass throwable = Scene.v().getSootClass("java.lang.Throwable");
    Map<LabelNode, Iterator<UnitBox>> handlers = new HashMap<LabelNode, Iterator<UnitBox>>(tryCatchBlocks.size());
    for (TryCatchBlockNode tc : tryCatchBlocks) {
        UnitBox start = Jimple.v().newStmtBox(null);
        UnitBox end = Jimple.v().newStmtBox(null);
        Iterator<UnitBox> hitr = handlers.get(tc.handler);
        if (hitr == null) {
            hitr = trapHandlers.get(tc.handler).iterator();
            handlers.put(tc.handler, hitr);
        }
        UnitBox handler = hitr.next();
        SootClass cls = tc.type == null ? throwable : Scene.v().getSootClass(AsmUtil.toQualifiedName(tc.type));
        Trap trap = Jimple.v().newTrap(cls, start, end, handler);
        traps.add(trap);
        labels.put(tc.start, start);
        labels.put(tc.end, end);
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) UnitBox(soot.UnitBox) HashMap(java.util.HashMap) Iterator(java.util.Iterator) Trap(soot.Trap) SootClass(soot.SootClass)

Example 68 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project soot by Sable.

the class AsmMethodSource method convertTableSwitchInsn.

private void convertTableSwitchInsn(TableSwitchInsnNode insn) {
    StackFrame frame = getFrame(insn);
    if (units.containsKey(insn)) {
        frame.mergeIn(pop());
        return;
    }
    Operand key = popImmediate();
    UnitBox dflt = Jimple.v().newStmtBox(null);
    List<UnitBox> targets = new ArrayList<UnitBox>(insn.labels.size());
    labels.put(insn.dflt, dflt);
    for (LabelNode ln : insn.labels) {
        UnitBox box = Jimple.v().newStmtBox(null);
        targets.add(box);
        labels.put(ln, box);
    }
    TableSwitchStmt tss = Jimple.v().newTableSwitchStmt(key.stackOrValue(), insn.min, insn.max, targets, dflt);
    key.addBox(tss.getKeyBox());
    frame.in(key);
    frame.boxes(tss.getKeyBox());
    setUnit(insn, tss);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TableSwitchStmt(soot.jimple.TableSwitchStmt) UnitBox(soot.UnitBox) ArrayList(java.util.ArrayList)

Example 69 with LabelNode

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

the class ForeachCompiler method write.

@Override
public void write(ForeachStmtToken token) {
    expr.writeDefineVariables(token.getLocal());
    LabelNode start = new LabelNode();
    LabelNode end = new LabelNode();
    LabelNode l = new LabelNode();
    add(l);
    expr.writePushEnv();
    expr.writePushTraceInfo(token);
    expr.writeExpression(token.getIterator(), true, false, true);
    expr.writePopBoxing();
    expr.writePushConstBoolean(token.isValueReference());
    expr.writePushConstBoolean(token.isKeyReference());
    expr.writeSysDynamicCall(Environment.class, "__getIterator", ForeachIterator.class, TraceInfo.class, Memory.class, Boolean.TYPE, Boolean.TYPE);
    String name = "~foreach~" + method.nextStatementIndex(ForeachIterator.class);
    LocalVariable foreachVariable = method.getLocalVariable(name);
    if (foreachVariable == null)
        foreachVariable = method.addLocalVariable(name, l, ForeachIterator.class);
    /*LocalVariable foreachVariable = method.addLocalVariable(
                "~foreach~" + method.nextStatementIndex(ForeachIterator.class), l, ForeachIterator.class
        );*/
    foreachVariable.setEndLabel(end);
    expr.writeVarStore(foreachVariable, false, false);
    method.pushJump(end, start);
    add(start);
    expr.writeVarLoad(foreachVariable);
    expr.writeSysDynamicCall(ForeachIterator.class, "next", Boolean.TYPE);
    add(new JumpInsnNode(IFEQ, end));
    expr.stackPop();
    // $key
    if (token.getKey() != null) {
        LocalVariable key = method.getLocalVariable(token.getKey().getName());
        expr.checkAssignableVar(token.getKey());
        expr.writeVarLoad(foreachVariable);
        expr.writeSysDynamicCall(ForeachIterator.class, "getMemoryKey", Memory.class);
        if (token.isKeyReference()) {
            throw new FatalException("Key element cannot be a reference", token.getKey().toTraceInfo(compiler.getContext()));
        // writeVarStore(key, false, false);
        } else
            expr.writeVarAssign(key, null, false, false);
    }
    // $var
    // LocalVariable variable = method.getLocalVariable(token.getValue().getName());
    Token last = token.getValue().getLast();
    VariableExprToken var = null;
    if (last instanceof DynamicAccessExprToken) {
        DynamicAccessExprToken setter = (DynamicAccessExprToken) last;
        ExprStmtToken value = new ExprStmtToken(this.env, this.compiler.getContext(), token.getValue().getTokens());
        value.getTokens().remove(value.getTokens().size() - 1);
        value.updateAsmExpr(this.env, this.compiler.getContext());
        expr.writeExpression(value, true, false);
        expr.writeVarLoad(foreachVariable);
        expr.writeSysDynamicCall(ForeachIterator.class, "getValue", Memory.class);
        if (!token.isValueReference())
            expr.writePopImmutable();
        expr.writeDynamicAccessInfo(setter, false);
        expr.writeGetStatic("$CALL_PROP_CACHE", PropertyCallCache.class);
        expr.writePushConstInt(method.clazz.getAndIncCallPropCount());
        expr.writeSysStaticCall(ObjectInvokeHelper.class, "assignProperty", Memory.class, Memory.class, Memory.class, String.class, Environment.class, TraceInfo.class, PropertyCallCache.class, int.class);
    } else {
        if (token.getValue().getSingle() instanceof VariableExprToken)
            expr.checkAssignableVar(var = (VariableExprToken) token.getValue().getSingle());
        expr.writeVarLoad(foreachVariable);
        expr.writeSysDynamicCall(ForeachIterator.class, "getValue", Memory.class);
        if (!token.isValueReference())
            expr.writePopImmutable();
        ExprStmtToken value = token.getValue();
        if (value.isSingle() && value.getSingle() instanceof ListExprToken) {
            ListCompiler listCompiler = (ListCompiler) expr.getCompiler(ListExprToken.class);
            listCompiler.write((ListExprToken) value.getSingle(), false, false);
        } else {
            expr.writeExpression(value, true, false);
            if (expr.stackPeek().immutable)
                expr.unexpectedToken(value.getLast());
            expr.writeSysStaticCall(Memory.class, token.isValueReference() ? "assignRefRight" : "assignRight", Memory.class, Memory.class, Memory.class);
        }
    }
    expr.writePopAll(1);
    /*
        if (token.isValueReference())
            writeVarStore(variable, false, false);
        else
            writeVarAssign(variable, false, true); */
    // body
    expr.write(BodyStmtToken.class, token.getBody());
    add(new JumpInsnNode(GOTO, start));
    add(end);
    /*if (compiler.getLangMode() == LangMode.JPHP){
            if (token.isValueReference() && var != null){
                expr.writeVarLoad(var.getName());
                expr.writeSysDynamicCall(Memory.class, "unset", void.class);
            }
        }*/
    method.popJump();
    expr.writeUndefineVariables(token.getLocal());
    method.prevStatementIndex(ForeachIterator.class);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ForeachIterator(php.runtime.lang.ForeachIterator) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) FatalException(php.runtime.exceptions.FatalException) ListCompiler(org.develnext.jphp.core.compiler.jvm.statement.expr.value.ListCompiler) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) DynamicAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken) Token(org.develnext.jphp.core.tokenizer.token.Token) BodyStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.BodyStmtToken) ForeachStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ForeachStmtToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) ExprStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken) ListExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken) DynamicAccessExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.DynamicAccessExprToken) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) ListExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.ListExprToken)

Example 70 with LabelNode

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

the class StaticDefinitionCompiler method write.

@Override
public void write(StaticStmtToken token) {
    LocalVariable local = method.getLocalVariable(token.getVariable().getName());
    assert local != null;
    LabelNode end = new LabelNode();
    boolean isClosure = method.clazz.isClosure();
    if (isClosure)
        expr.writeVarLoad("~this");
    else
        expr.writePushEnv();
    // writePushConstString(name);
    writePushNameForStaticVariable(local);
    expr.writeSysDynamicCall(isClosure ? null : Environment.class, "getStatic", Memory.class, String.class);
    expr.writePushDup();
    add(new JumpInsnNode(IFNONNULL, end));
    expr.stackPop();
    expr.writePopAll(1);
    if (isClosure)
        expr.writeVarLoad("~this");
    else
        expr.writePushEnv();
    writePushNameForStaticVariable(local);
    if (token.getInitValue() != null) {
        expr.writeExpression(token.getInitValue(), true, false, true);
    } else {
        expr.writePushNull();
    }
    expr.writePopBoxing(true);
    expr.writeSysDynamicCall(isClosure ? null : Environment.class, "getOrCreateStatic", Memory.class, String.class, Memory.class);
    add(end);
    expr.writeVarStore(local, false, false);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) Environment(php.runtime.env.Environment) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode)

Aggregations

LabelNode (org.objectweb.asm.tree.LabelNode)89 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)37 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)32 Label (org.objectweb.asm.Label)28 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)23 InsnList (org.objectweb.asm.tree.InsnList)22 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)21 InsnNode (org.objectweb.asm.tree.InsnNode)20 MethodNode (org.objectweb.asm.tree.MethodNode)19 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)17 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)16 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)15 ClassNode (org.objectweb.asm.tree.ClassNode)14 ClassReader (org.objectweb.asm.ClassReader)12 LineNumberNode (org.objectweb.asm.tree.LineNumberNode)10 Type (org.objectweb.asm.Type)8 LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)8 LinkedList (java.util.LinkedList)6 ArrayList (java.util.ArrayList)5 LookupSwitchInsnNode (org.objectweb.asm.tree.LookupSwitchInsnNode)5