use of soot.UnitBox in project soot by Sable.
the class AsmMethodSource method emitUnits.
private void emitUnits() {
AbstractInsnNode insn = instructions.getFirst();
ArrayDeque<LabelNode> labls = new ArrayDeque<LabelNode>();
while (insn != null) {
// Save the label to assign it to the next real unit
if (insn instanceof LabelNode)
labls.add((LabelNode) insn);
// Get the unit associated with the current instruction
Unit u = units.get(insn);
if (u == null) {
insn = insn.getNext();
continue;
}
emitUnits(u);
// If this is an exception handler, register the starting unit for it
{
IdentityStmt caughtEx = null;
if (u instanceof IdentityStmt)
caughtEx = (IdentityStmt) u;
else if (u instanceof UnitContainer)
caughtEx = getIdentityRefFromContrainer((UnitContainer) u);
if (insn instanceof LabelNode && caughtEx != null && caughtEx.getRightOp() instanceof CaughtExceptionRef) {
// We directly place this label
Collection<UnitBox> traps = trapHandlers.get((LabelNode) insn);
for (UnitBox ub : traps) ub.setUnit(caughtEx);
}
}
// Register this unit for all targets of the labels ending up at it
while (!labls.isEmpty()) {
LabelNode ln = labls.poll();
Collection<UnitBox> boxes = labels.get(ln);
if (boxes != null) {
for (UnitBox box : boxes) {
box.setUnit(u instanceof UnitContainer ? ((UnitContainer) u).getFirstUnit() : u);
}
}
}
insn = insn.getNext();
}
// Emit the inline exception handlers
for (LabelNode ln : this.inlineExceptionHandlers.keySet()) {
Unit handler = this.inlineExceptionHandlers.get(ln);
emitUnits(handler);
Collection<UnitBox> traps = trapHandlers.get(ln);
for (UnitBox ub : traps) ub.setUnit(handler);
// We need to jump to the original implementation
Unit targetUnit = units.get(ln);
GotoStmt gotoImpl = Jimple.v().newGotoStmt(targetUnit);
body.getUnits().add(gotoImpl);
}
/* set remaining labels & boxes to last unit of chain */
if (labls.isEmpty())
return;
Unit end = Jimple.v().newNopStmt();
body.getUnits().add(end);
while (!labls.isEmpty()) {
LabelNode ln = labls.poll();
Collection<UnitBox> boxes = labels.get(ln);
if (boxes != null) {
for (UnitBox box : boxes) box.setUnit(end);
}
}
}
use of soot.UnitBox in project soot by Sable.
the class AsmMethodSource method convertJumpInsn.
private void convertJumpInsn(JumpInsnNode insn) {
int op = insn.getOpcode();
if (op == GOTO) {
if (!units.containsKey(insn)) {
UnitBox box = Jimple.v().newStmtBox(null);
labels.put(insn.label, box);
setUnit(insn, Jimple.v().newGotoStmt(box));
}
return;
}
/* must be ifX insn */
StackFrame frame = getFrame(insn);
if (!units.containsKey(insn)) {
Operand val = popImmediate();
Value v = val.stackOrValue();
ConditionExpr cond;
if (op >= IF_ICMPEQ && op <= IF_ACMPNE) {
Operand val1 = popImmediate();
Value v1 = val1.stackOrValue();
if (op == IF_ICMPEQ)
cond = Jimple.v().newEqExpr(v1, v);
else if (op == IF_ICMPNE)
cond = Jimple.v().newNeExpr(v1, v);
else if (op == IF_ICMPLT)
cond = Jimple.v().newLtExpr(v1, v);
else if (op == IF_ICMPGE)
cond = Jimple.v().newGeExpr(v1, v);
else if (op == IF_ICMPGT)
cond = Jimple.v().newGtExpr(v1, v);
else if (op == IF_ICMPLE)
cond = Jimple.v().newLeExpr(v1, v);
else if (op == IF_ACMPEQ)
cond = Jimple.v().newEqExpr(v1, v);
else if (op == IF_ACMPNE)
cond = Jimple.v().newNeExpr(v1, v);
else
throw new AssertionError("Unknown if op: " + op);
val1.addBox(cond.getOp1Box());
val.addBox(cond.getOp2Box());
frame.boxes(cond.getOp2Box(), cond.getOp1Box());
frame.in(val, val1);
} else {
if (op == IFEQ)
cond = Jimple.v().newEqExpr(v, IntConstant.v(0));
else if (op == IFNE)
cond = Jimple.v().newNeExpr(v, IntConstant.v(0));
else if (op == IFLT)
cond = Jimple.v().newLtExpr(v, IntConstant.v(0));
else if (op == IFGE)
cond = Jimple.v().newGeExpr(v, IntConstant.v(0));
else if (op == IFGT)
cond = Jimple.v().newGtExpr(v, IntConstant.v(0));
else if (op == IFLE)
cond = Jimple.v().newLeExpr(v, IntConstant.v(0));
else if (op == IFNULL)
cond = Jimple.v().newEqExpr(v, NullConstant.v());
else if (op == IFNONNULL)
cond = Jimple.v().newNeExpr(v, NullConstant.v());
else
throw new AssertionError("Unknown if op: " + op);
val.addBox(cond.getOp1Box());
frame.boxes(cond.getOp1Box());
frame.in(val);
}
UnitBox box = Jimple.v().newStmtBox(null);
labels.put(insn.label, box);
setUnit(insn, Jimple.v().newIfStmt(cond, box));
} else {
if (op >= IF_ICMPEQ && op <= IF_ACMPNE)
frame.mergeIn(pop(), pop());
else
frame.mergeIn(pop());
}
}
use of soot.UnitBox in project soot by Sable.
the class AsmMethodSource method convertLookupSwitchInsn.
private void convertLookupSwitchInsn(LookupSwitchInsnNode 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);
}
List<IntConstant> keys = new ArrayList<IntConstant>(insn.keys.size());
for (Integer i : insn.keys) keys.add(IntConstant.v(i));
LookupSwitchStmt lss = Jimple.v().newLookupSwitchStmt(key.stackOrValue(), keys, targets, dflt);
key.addBox(lss.getKeyBox());
frame.in(key);
frame.boxes(lss.getKeyBox());
setUnit(insn, lss);
}
use of soot.UnitBox in project soot by Sable.
the class Walker method outACatchClause.
/*
* catch_clause = catch [name]:class_name from [from_label]:label_name to
* [to_label]:label_name with [with_label]:label_name semicolon;
*/
// public void caseACatchClause(ACatchClause node){}
public void outACatchClause(ACatchClause node) {
String exceptionName;
UnitBox withUnit, fromUnit, toUnit;
withUnit = Jimple.v().newStmtBox(null);
addBoxToPatch((String) mProductions.removeLast(), withUnit);
toUnit = Jimple.v().newStmtBox(null);
addBoxToPatch((String) mProductions.removeLast(), toUnit);
fromUnit = Jimple.v().newStmtBox(null);
addBoxToPatch((String) mProductions.removeLast(), fromUnit);
exceptionName = (String) mProductions.removeLast();
Trap trap = Jimple.v().newTrap(mResolver.makeClassRef(exceptionName), fromUnit, toUnit, withUnit);
mProductions.addLast(trap);
}
use of soot.UnitBox in project soot by Sable.
the class Walker method outALookupswitchStatement.
public void outALookupswitchStatement(ALookupswitchStatement node) {
List<IntConstant> lookupValues = new ArrayList<IntConstant>();
List<UnitBox> targets = new ArrayList<UnitBox>();
UnitBox defaultTarget = null;
if (node.getCaseStmt() != null) {
int size = node.getCaseStmt().size();
for (int i = 0; i < size; i++) {
Object valueTargetPair = mProductions.removeLast();
if (valueTargetPair instanceof UnitBox) {
if (defaultTarget != null)
throw new RuntimeException("error: can't ;have more than 1 default stmt");
defaultTarget = (UnitBox) valueTargetPair;
} else {
Object[] pair = (Object[]) valueTargetPair;
lookupValues.add(0, (IntConstant) pair[0]);
targets.add(0, (UnitBox) pair[1]);
}
}
} else {
throw new RuntimeException("error: switch stmt has no case stmts");
}
Value key = (Value) mProductions.removeLast();
Unit switchStmt = Jimple.v().newLookupSwitchStmt(key, lookupValues, targets, defaultTarget);
mProductions.addLast(switchStmt);
}
Aggregations