Search in sources :

Example 1 with BafLocal

use of soot.baf.internal.BafLocal in project soot by Sable.

the class BafASMBackend method generateMethodBody.

/*
	 * (non-Javadoc)
	 * 
	 * @see soot.AbstractASMBackend#generateMethodBody(org.objectweb.asm.
	 * MethodVisitor, soot.SootMethod)
	 */
@Override
protected void generateMethodBody(MethodVisitor mv, SootMethod method) {
    BafBody body = getBafBody(method);
    Chain<Unit> instructions = body.getUnits();
    /*
		 * Create a label for each instruction that is the target of some branch
		 */
    for (UnitBox box : body.getUnitBoxes(true)) {
        Unit u = box.getUnit();
        if (!branchTargetLabels.containsKey(u)) {
            branchTargetLabels.put(u, new Label());
        }
    }
    Label startLabel = null;
    if (Options.v().write_local_annotations()) {
        startLabel = new Label();
        mv.visitLabel(startLabel);
    }
    /*
		 * Handle all TRY-CATCH-blocks
		 */
    for (Trap trap : body.getTraps()) {
        // Check if the try-block contains any statement
        if (trap.getBeginUnit() != trap.getEndUnit()) {
            Label start = branchTargetLabels.get(trap.getBeginUnit());
            Label end = branchTargetLabels.get(trap.getEndUnit());
            Label handler = branchTargetLabels.get(trap.getHandlerUnit());
            String type = slashify(trap.getException().getName());
            mv.visitTryCatchBlock(start, end, handler, type);
        }
    }
    /*
		 * Handle local variable slots for the "this"-local and the parameters
		 */
    int localCount = 0;
    int[] paramSlots = new int[method.getParameterCount()];
    Set<Local> assignedLocals = new HashSet<Local>();
    /*
		 * For non-static methods the first parameters and zero-slot is the
		 * "this"-local
		 */
    if (!method.isStatic()) {
        ++localCount;
    }
    for (int i = 0; i < method.getParameterCount(); ++i) {
        paramSlots[i] = localCount;
        localCount += sizeOfType(method.getParameterType(i));
    }
    for (Unit u : instructions) {
        if (u instanceof IdentityInst && ((IdentityInst) u).getLeftOp() instanceof Local) {
            Local l = (Local) ((IdentityInst) u).getLeftOp();
            IdentityRef identity = (IdentityRef) ((IdentityInst) u).getRightOp();
            int slot = 0;
            if (identity instanceof ThisRef) {
                if (method.isStatic())
                    throw new RuntimeException("Attempting to use 'this' in static method");
            } else if (identity instanceof ParameterRef)
                slot = paramSlots[((ParameterRef) identity).getIndex()];
            else {
                // Exception ref. Skip over this
                continue;
            }
            localToSlot.put(l, slot);
            assignedLocals.add(l);
        }
    }
    for (Local local : body.getLocals()) {
        if (assignedLocals.add(local)) {
            localToSlot.put(local, localCount);
            localCount += sizeOfType(local.getType());
        }
    }
    // Generate the code
    for (Unit u : instructions) {
        if (branchTargetLabels.containsKey(u)) {
            mv.visitLabel(branchTargetLabels.get(u));
        }
        if (u.hasTag("LineNumberTag")) {
            LineNumberTag lnt = (LineNumberTag) u.getTag("LineNumberTag");
            Label l;
            if (branchTargetLabels.containsKey(u)) {
                l = branchTargetLabels.get(u);
            } else {
                l = new Label();
                mv.visitLabel(l);
            }
            mv.visitLineNumber(lnt.getLineNumber(), l);
        }
        generateInstruction(mv, (Inst) u);
    }
    // Generate the local annotations
    if (Options.v().write_local_annotations()) {
        Label endLabel = new Label();
        mv.visitLabel(endLabel);
        for (Local local : body.getLocals()) {
            Integer slot = localToSlot.get(local);
            if (slot != null) {
                BafLocal l = (BafLocal) local;
                if (l.getOriginalLocal() != null) {
                    Local jimpleLocal = l.getOriginalLocal();
                    if (jimpleLocal != null)
                        mv.visitLocalVariable(jimpleLocal.getName(), toTypeDesc(jimpleLocal.getType()), null, startLabel, endLabel, slot);
                }
            }
        }
    }
}
Also used : UnitBox(soot.UnitBox) Label(org.objectweb.asm.Label) BafLocal(soot.baf.internal.BafLocal) Local(soot.Local) Trap(soot.Trap) Unit(soot.Unit) ParameterRef(soot.jimple.ParameterRef) ThisRef(soot.jimple.ThisRef) LineNumberTag(soot.tagkit.LineNumberTag) IdentityRef(soot.jimple.IdentityRef) HashSet(java.util.HashSet) BafLocal(soot.baf.internal.BafLocal)

Aggregations

HashSet (java.util.HashSet)1 Label (org.objectweb.asm.Label)1 Local (soot.Local)1 Trap (soot.Trap)1 Unit (soot.Unit)1 UnitBox (soot.UnitBox)1 BafLocal (soot.baf.internal.BafLocal)1 IdentityRef (soot.jimple.IdentityRef)1 ParameterRef (soot.jimple.ParameterRef)1 ThisRef (soot.jimple.ThisRef)1 LineNumberTag (soot.tagkit.LineNumberTag)1