KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > compiler > Label


1 // Copyright (c) Corporation for National Research Initiatives
2

3 package org.python.compiler;
4
5 import java.io.*;
6 import java.util.Vector JavaDoc;
7
8 class Label {
9     int position;
10     int[] offsets, positions, sizes;
11     int noffsets;
12     Code code;
13     int stack;
14
15     public Label(Code code) {
16         this.code = code;
17         position = -1;
18         noffsets = 0;
19         offsets = new int[4];
20         positions = new int[4];
21         sizes = new int[4];
22         stack = -1;
23     }
24
25     public void fix(byte[] data) throws IOException {
26         ByteArrayOutputStream array = new ByteArrayOutputStream();
27         DataOutputStream stream = new DataOutputStream(array);
28
29         if (noffsets > 0 && position == -1)
30             throw new InternalError JavaDoc("position never set for label");
31
32         for (int i=0; i<noffsets; i++) {
33             //System.out.println("o: "+offsets[i]+", "+position+", "+
34
// positions[i]);
35
int off = position-offsets[i];
36             int p = positions[i];
37             if (sizes[i] == 2) {
38                 stream.writeShort(off);
39             } else {
40                 stream.writeInt(off);
41             }
42
43             System.arraycopy(array.toByteArray(), 0, data, p, sizes[i]);
44             array.reset();
45             //data[p] = (byte)(off >>> 8);
46
//data[p+1] = (byte)(off & 0xff00);
47
}
48     }
49
50
51     public void setStack(int stack) {
52         if (this.stack == -1) {
53             this.stack = stack;
54         } else {
55             if (this.stack != stack) {
56                 throw new InternalError JavaDoc("stack sizes don't agree: "+
57                                         this.stack+", "+stack);
58             }
59         }
60     }
61
62     public int getPosition() {
63         if (position == -1)
64             throw new InternalError JavaDoc("position never set for label");
65         return position;
66     }
67
68     public void setPosition() {
69         position = code.size();
70         //code.addLabel(this);
71
}
72
73     public void setBranch(int offset, int size) throws IOException {
74         if (noffsets >= offsets.length) {
75             int[] new_offsets = new int[offsets.length*2];
76             System.arraycopy(offsets, 0, new_offsets, 0, noffsets);
77             offsets = new_offsets;
78
79             int[] new_positions = new int[positions.length*2];
80             System.arraycopy(positions, 0, new_positions, 0, noffsets);
81             positions = new_positions;
82
83             int[] new_sizes = new int[sizes.length*2];
84             System.arraycopy(sizes, 0, new_sizes, 0, noffsets);
85             sizes = new_sizes;
86         }
87         positions[noffsets] = code.size();
88         offsets[noffsets] = offset;
89         sizes[noffsets] = size;
90         noffsets = noffsets+1;
91         if (size == 2) {
92             code.code.writeShort(0);
93         } else {
94             code.code.writeInt(0);
95         }
96     }
97 }
98
Popular Tags