diff --git a/src/main/java/soot/AbstractUnit.java b/src/main/java/soot/AbstractUnit.java index 2bd4acb15ec..4015361df22 100644 --- a/src/main/java/soot/AbstractUnit.java +++ b/src/main/java/soot/AbstractUnit.java @@ -39,7 +39,11 @@ public abstract class AbstractUnit extends AbstractHost implements Unit { * List of UnitBoxes pointing to this Unit. */ protected List boxesPointingToThis = null; - + + /** Field to map the BCI of the bytecode offset of the instruction corresponding this unit. + * Any value other than -1 is valid. */ + protected int bci = -1; + /** * Returns a deep clone of this object. */ @@ -145,4 +149,23 @@ public void redirectJumpsToThisTo(Unit newLocation) { } } } + + /** + * @return the bytecode offset of the bytecode instruction corresponding to the unit. + * Any value other than -1 is valid. + * */ + @Override + public int getBytecodeOffset() { + return this.bci; + } + + /** + * Set the bytecode offset of the bytecode instruction corresponding to the unit. + * Any value other than -1 is valid. + * */ + @Override + public void setBytecodeOffset(int bci) { + this.bci = bci; + } + } diff --git a/src/main/java/soot/Unit.java b/src/main/java/soot/Unit.java index 68bfc6ede76..08f87f186ab 100644 --- a/src/main/java/soot/Unit.java +++ b/src/main/java/soot/Unit.java @@ -93,4 +93,10 @@ public interface Unit extends Switchable, Host, Serializable, Context { /** This is an overrideable method to be used by all stmt of Jimple currently to perform the respective analysis. */ public BiFuncFlowSet performAnalysis(BiFuncFlowSet inset,Operator operator); + + /** API to retrieve the bytecode offset of the instruction stored from the classfile. */ + public int getBytecodeOffset(); + + /** API to store the bytecode offset of the instruction from the classfile into the unit. */ + public void setBytecodeOffset(int bco); } diff --git a/src/main/java/soot/asm/AsmMethodSource.java b/src/main/java/soot/asm/AsmMethodSource.java index 2c347d93393..59a572c47ba 100644 --- a/src/main/java/soot/asm/AsmMethodSource.java +++ b/src/main/java/soot/asm/AsmMethodSource.java @@ -291,6 +291,7 @@ import soot.jimple.ThrowStmt; import soot.jimple.UnopExpr; import soot.options.Options; +import soot.tagkit.BytecodeOffsetTag; import soot.tagkit.LineNumberTag; import soot.tagkit.Tag; import soot.util.Chain; @@ -538,7 +539,20 @@ void setUnit(AbstractInsnNode insn, Unit u) { throw new RuntimeException("Line tag mismatch"); } } - + + /** Fix for correctly storing the bytecode offset of the unit from the bytecode instruction. */ + if (Options.v().keep_offset() && null !=insn) { + Tag bcoTag = u.getTag(BytecodeOffsetTag.NAME); + if (bcoTag == null && insn.getBytecodeOffset() != -1) { + bcoTag = new BytecodeOffsetTag(insn.getBytecodeOffset()); + u.addTag(bcoTag); + } + } + + if(null != insn && insn.getBytecodeOffset() != -1) { + u.setBytecodeOffset(insn.getBytecodeOffset()); + } + Unit o = units.put(insn, u); if (o != null) { throw new AssertionError(insn.getOpcode() + " already has a unit, " + o); diff --git a/src/main/java/soot/asm/UnitContainer.java b/src/main/java/soot/asm/UnitContainer.java index db385368eed..4245d700758 100644 --- a/src/main/java/soot/asm/UnitContainer.java +++ b/src/main/java/soot/asm/UnitContainer.java @@ -169,5 +169,17 @@ public BiFuncOutset performAnalysis(BiFuncInset inset,Operator operato // TO-DO : Implementations for other IR's also. throw new UnsupportedOperationException(); } + + @Override + public int getBytecodeOffset() { + // TO-DO : Implementations for other IR's also. + throw new UnsupportedOperationException(); + } + + @Override + public void setBytecodeOffset(int bco) { + // TO-DO : Implementations for other IR's also. + throw new UnsupportedOperationException(); + } } \ No newline at end of file