Search in sources :

Example 1 with LineNumberNode

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

the class WhileCompiler method write.

@Override
public void write(WhileStmtToken token) {
    expr.writeDefineVariables(token.getLocal());
    LabelNode start = expr.writeLabel(node, token.getMeta().getStartLine());
    LabelNode end = new LabelNode();
    expr.writeConditional(token.getCondition(), end);
    method.pushJump(end, start);
    expr.write(BodyStmtToken.class, token.getBody());
    method.popJump();
    add(new JumpInsnNode(GOTO, start));
    add(end);
    add(new LineNumberNode(token.getMeta().getEndLine(), end));
    expr.writeUndefineVariables(token.getLocal());
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LineNumberNode(org.objectweb.asm.tree.LineNumberNode)

Example 2 with LineNumberNode

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

the class IfElseCompiler method writeBody.

private void writeBody(IfStmtToken token) {
    LabelNode end = new LabelNode();
    LabelNode elseL = new LabelNode();
    expr.writePopBoolean();
    add(new JumpInsnNode(IFEQ, token.getElseBody() != null ? elseL : end));
    expr.stackPop();
    if (token.getBody() != null) {
        expr.write(token.getBody());
    }
    if (token.getElseBody() != null) {
        add(new JumpInsnNode(GOTO, end));
        add(elseL);
        expr.write(token.getElseBody());
    }
    add(end);
    add(new LineNumberNode(token.getMeta().getEndLine(), end));
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LineNumberNode(org.objectweb.asm.tree.LineNumberNode)

Example 3 with LineNumberNode

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

the class SwitchCompiler method write.

@Override
public void write(SwitchStmtToken token) {
    expr.writeDefineVariables(token.getLocal());
    LabelNode l = new LabelNode();
    LabelNode end = new LabelNode();
    add(l);
    LocalVariable switchValue = method.addLocalVariable("~switch~" + method.nextStatementIndex(Memory.class), l, Memory.class);
    switchValue.setEndLabel(end);
    LabelNode[][] jumps = new LabelNode[token.getCases().size() + 1][2];
    int i = 0;
    for (CaseStmtToken one : token.getCases()) {
        // checkLabel, bodyLabel
        jumps[i] = new LabelNode[] { new LabelNode(), new LabelNode() };
        if (i == jumps.length - 1)
            jumps[i] = new LabelNode[] { end, end };
        i++;
    }
    jumps[jumps.length - 1] = new LabelNode[] { end, end };
    method.pushJump(end, end);
    expr.writeExpression(token.getValue(), true, false);
    expr.writePopBoxing();
    expr.writeVarStore(switchValue, false, false);
    i = 0;
    for (CaseStmtToken one : token.getCases()) {
        // conditional
        add(jumps[i][0]);
        if (one.getConditional() != null) {
            expr.writeVarLoad(switchValue);
            expr.writeExpression(one.getConditional(), true, false);
            expr.writeSysDynamicCall(Memory.class, "equal", Boolean.TYPE, expr.stackPeek().type.toClass());
            add(new JumpInsnNode(IFEQ, jumps[i + 1][0]));
            expr.stackPop();
        }
        // if is done...
        add(new JumpInsnNode(GOTO, jumps[i][1]));
        i++;
    }
    i = 0;
    for (CaseStmtToken one : token.getCases()) {
        add(jumps[i][1]);
        expr.writeDefineVariables(one.getLocals());
        expr.write(BodyStmtToken.class, one.getBody());
        i++;
        expr.writeUndefineVariables(one.getLocals());
    }
    method.popJump();
    add(end);
    add(new LineNumberNode(token.getMeta().getEndLine(), end));
    method.prevStatementIndex(Memory.class);
    expr.writeUndefineVariables(token.getLocal());
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) CaseStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.CaseStmtToken) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LineNumberNode(org.objectweb.asm.tree.LineNumberNode)

Example 4 with LineNumberNode

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

the class DoCompiler method write.

@Override
public void write(DoStmtToken token) {
    expr.writeDefineVariables(token.getLocal());
    LabelNode start = expr.writeLabel(node, token.getMeta().getStartLine());
    LabelNode end = new LabelNode();
    method.pushJump(end, start);
    expr.write(token.getBody());
    method.popJump();
    expr.writeConditional(token.getCondition(), end);
    add(new JumpInsnNode(GOTO, start));
    add(end);
    add(new LineNumberNode(token.getMeta().getEndLine(), end));
    expr.writeUndefineVariables(token.getLocal());
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LineNumberNode(org.objectweb.asm.tree.LineNumberNode)

Example 5 with LineNumberNode

use of org.objectweb.asm.tree.LineNumberNode in project MineCamera by PorPit.

the class Transformer method replaceLabelBefore.

public void replaceLabelBefore(InsnList instructions, AbstractInsnNode node, @Nullable ArrayList<AbstractInsnNode> replaceInstructions, int labels, int labelsBefore, boolean keepFirstLabel, boolean deleteFrame) {
    ListIterator<AbstractInsnNode> iterator = instructions.iterator();
    LabelNode searchedLabel = null;
    ArrayList<LabelNode> foundLabels = new ArrayList<LabelNode>();
    while (iterator.hasNext()) {
        AbstractInsnNode insn = iterator.next();
        if (insn instanceof LabelNode)
            foundLabels.add((LabelNode) insn);
        if (areNodesEqual(insn, node)) {
            int index = foundLabels.size() - 1;
            index -= labelsBefore;
            if (index >= 0)
                searchedLabel = foundLabels.get(index);
            labels += labelsBefore;
            break;
        }
    }
    if (searchedLabel != null) {
        boolean found = false;
        int labelCounter = 0;
        iterator = instructions.iterator();
        while (iterator.hasNext()) {
            AbstractInsnNode insn = iterator.next();
            if (found && insn instanceof LabelNode) {
                labelCounter++;
                if (labelCounter >= labels) {
                    if (replaceInstructions != null)
                        for (int i = 0; i < replaceInstructions.size(); i++) instructions.insertBefore(insn, replaceInstructions.get(i));
                    break;
                }
            }
            if (insn == searchedLabel) {
                found = true;
                if (keepFirstLabel)
                    continue;
            }
            if (found)
                if (deleteFrame || !(insn instanceof FrameNode))
                    instructions.remove(insn);
        }
    } else if (node instanceof LineNumberNode)
        System.out.println("COULD NOT FIND NODE line=" + ((LineNumberNode) node).line);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) FrameNode(org.objectweb.asm.tree.FrameNode) ArrayList(java.util.ArrayList) LineNumberNode(org.objectweb.asm.tree.LineNumberNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Aggregations

LineNumberNode (org.objectweb.asm.tree.LineNumberNode)17 LabelNode (org.objectweb.asm.tree.LabelNode)10 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)8 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)6 FrameNode (org.objectweb.asm.tree.FrameNode)5 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)3 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)2 InsnList (org.objectweb.asm.tree.InsnList)2 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)2 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)2 MethodNode (org.objectweb.asm.tree.MethodNode)2 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)2 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 VariableExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken)1 CaseStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.CaseStmtToken)1 ExprStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ExprStmtToken)1 ControlDependenceGraph (org.evosuite.graphs.cdg.ControlDependenceGraph)1