1 package serp.bytecode;
2
3
4
5
6
7
8
9
10
11
12 class InstructionPtrStrategy implements InstructionPtr {
13
14 private InstructionPtr _pointer;
15
16
17
18 private Instruction _target = null;
19 private int _byteIndex = -1;
20
21 public InstructionPtrStrategy(InstructionPtr pointer) {
22 _pointer = pointer;
23 }
24
25 public InstructionPtrStrategy(InstructionPtr pointer, Instruction target) {
26 this(pointer);
27 setTargetInstruction(target);
28 }
29
30
31
32
33
34
35
36 public void setByteIndex(int index) {
37 if (index < -1)
38 throw new IllegalArgumentException(String.valueOf(index));
39 _byteIndex = index;
40 _target = null;
41 }
42
43
44
45
46
47
48 public void setTargetInstruction(Instruction ins) {
49 if (ins.getCode() != getCode())
50 throw new IllegalArgumentException("Instruction pointers and "
51 + "targets must be part of the same code block.");
52 _target = ins;
53 _byteIndex = -1;
54 }
55
56
57
58
59
60 public Instruction getTargetInstruction() {
61 if (_target != null)
62 return _target;
63 return getCode().getInstruction(_byteIndex);
64 }
65
66
67
68
69
70 public int getByteIndex() {
71 if (_target == null)
72 return _byteIndex;
73 return _target.getByteIndex();
74 }
75
76
77
78
79
80
81 public void updateTargets() {
82 if (_target == null)
83 _target = getCode().getInstruction(_byteIndex);
84 }
85
86 public void replaceTarget(Instruction oldTarget, Instruction newTarget) {
87 if (getTargetInstruction() == oldTarget)
88 setTargetInstruction(newTarget);
89 }
90
91 public Code getCode() {
92 return _pointer.getCode();
93 }
94 }