KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > BranchInstruction


1 package alt.jiapi.reflect;
2
3 /**
4  * BranchInstruction is represents an Instruction, that has
5  * a jump target to another Instruction.
6  *
7  * @author Mika Riekkinen
8  */

9 public class BranchInstruction extends Instruction {
10     private Instruction target;
11
12     public BranchInstruction(byte[] bytes) {
13         super(bytes);
14     }
15
16     public void setTarget(Instruction target) {
17         this.target = target;
18     }
19
20     public byte[] getBytes() {
21         // We should calculate new offset for target here
22
return super.getBytes();
23     }
24
25     public int getTargetOffset() {
26         byte[] bytes = super.getBytes();
27         int offset;
28
29         if (bytes.length == 3) {
30             offset = bytes[1] << 8;
31             offset |= bytes[2] & 0xff;
32         }
33         else {
34             offset = (((int)bytes[1]) << 24) | (((int)bytes[2]) << 16) |
35                 (((int)bytes[3]) << 8) | bytes[4];
36         }
37
38         return offset;
39     }
40     
41     void setTargetOffset(int offset) {
42         byte[] bytes = super.getBytes();
43         if (bytes.length == 3) {
44             bytes[1] = (byte)(offset >> 8);
45             bytes[2] = (byte)(offset & 0xff);
46         }
47         else {
48             bytes[1] = (byte)(offset >> 24);
49             bytes[2] = (byte)(offset >> 16);
50             bytes[3] = (byte)(offset >> 8);
51             bytes[4] = (byte)(offset & 0xff);
52         }
53
54     }
55
56     /**
57      * Get the target of this branch.
58      *
59      * @return target Instruction, that this branch points to
60      */

61     public Instruction getTarget() {
62         return target;
63     }
64
65     public String JavaDoc toString() {
66         return super.toString() + ", offset=" + getTargetOffset() + " (" + getTarget() + ")";
67     }
68 }
69
Popular Tags