lubomir@221
|
1 |
/**
|
lubomir@221
|
2 |
* Back 2 Browser Bytecode Translator
|
lubomir@221
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
lubomir@221
|
4 |
*
|
lubomir@221
|
5 |
* This program is free software: you can redistribute it and/or modify
|
lubomir@221
|
6 |
* it under the terms of the GNU General Public License as published by
|
lubomir@221
|
7 |
* the Free Software Foundation, version 2 of the License.
|
lubomir@221
|
8 |
*
|
lubomir@221
|
9 |
* This program is distributed in the hope that it will be useful,
|
lubomir@221
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
lubomir@221
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
lubomir@221
|
12 |
* GNU General Public License for more details.
|
lubomir@221
|
13 |
*
|
lubomir@221
|
14 |
* You should have received a copy of the GNU General Public License
|
lubomir@221
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
lubomir@221
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
lubomir@221
|
17 |
*/
|
lubomir@221
|
18 |
package org.apidesign.vm4brwsr;
|
lubomir@221
|
19 |
|
lubomir@281
|
20 |
import org.apidesign.javap.TypeArray;
|
lubomir@281
|
21 |
|
lubomir@310
|
22 |
final class StackMapper {
|
lubomir@281
|
23 |
private final TypeArray stackTypeIndexPairs;
|
lubomir@281
|
24 |
private int[] typeCounters;
|
lubomir@281
|
25 |
private int[] typeMaxCounters;
|
lubomir@221
|
26 |
|
lubomir@307
|
27 |
public StackMapper() {
|
lubomir@281
|
28 |
stackTypeIndexPairs = new TypeArray();
|
lubomir@307
|
29 |
typeCounters = new int[VarType.LAST + 1];
|
lubomir@307
|
30 |
typeMaxCounters = new int[VarType.LAST + 1];
|
lubomir@221
|
31 |
}
|
lubomir@221
|
32 |
|
lubomir@281
|
33 |
public void clear() {
|
lubomir@307
|
34 |
for (int type = 0; type <= VarType.LAST; ++type) {
|
lubomir@281
|
35 |
typeCounters[type] = 0;
|
lubomir@281
|
36 |
}
|
lubomir@281
|
37 |
stackTypeIndexPairs.clear();
|
lubomir@281
|
38 |
}
|
lubomir@281
|
39 |
|
lubomir@281
|
40 |
public void syncWithFrameStack(final TypeArray frameStack) {
|
lubomir@281
|
41 |
clear();
|
lubomir@281
|
42 |
|
lubomir@281
|
43 |
final int size = frameStack.getSize();
|
lubomir@281
|
44 |
for (int i = 0; i < size; ++i) {
|
lubomir@307
|
45 |
pushTypeImpl(VarType.fromStackMapType(frameStack.get(i)));
|
lubomir@221
|
46 |
}
|
lubomir@221
|
47 |
}
|
lubomir@221
|
48 |
|
lubomir@281
|
49 |
public Variable pushI() {
|
lubomir@307
|
50 |
return pushT(VarType.INTEGER);
|
lubomir@281
|
51 |
}
|
lubomir@281
|
52 |
|
lubomir@281
|
53 |
public Variable pushL() {
|
lubomir@307
|
54 |
return pushT(VarType.LONG);
|
lubomir@281
|
55 |
}
|
lubomir@281
|
56 |
|
lubomir@281
|
57 |
public Variable pushF() {
|
lubomir@307
|
58 |
return pushT(VarType.FLOAT);
|
lubomir@281
|
59 |
}
|
lubomir@281
|
60 |
|
lubomir@281
|
61 |
public Variable pushD() {
|
lubomir@307
|
62 |
return pushT(VarType.DOUBLE);
|
lubomir@281
|
63 |
}
|
lubomir@281
|
64 |
|
lubomir@281
|
65 |
public Variable pushA() {
|
lubomir@307
|
66 |
return pushT(VarType.REFERENCE);
|
lubomir@281
|
67 |
}
|
lubomir@281
|
68 |
|
lubomir@281
|
69 |
public Variable pushT(final int type) {
|
lubomir@281
|
70 |
return getVariable(pushTypeImpl(type));
|
lubomir@281
|
71 |
}
|
lubomir@281
|
72 |
|
lubomir@281
|
73 |
public Variable popI() {
|
lubomir@307
|
74 |
return popT(VarType.INTEGER);
|
lubomir@281
|
75 |
}
|
lubomir@281
|
76 |
|
lubomir@281
|
77 |
public Variable popL() {
|
lubomir@307
|
78 |
return popT(VarType.LONG);
|
lubomir@281
|
79 |
}
|
lubomir@281
|
80 |
|
lubomir@281
|
81 |
public Variable popF() {
|
lubomir@307
|
82 |
return popT(VarType.FLOAT);
|
lubomir@281
|
83 |
}
|
lubomir@281
|
84 |
|
lubomir@281
|
85 |
public Variable popD() {
|
lubomir@307
|
86 |
return popT(VarType.DOUBLE);
|
lubomir@281
|
87 |
}
|
lubomir@281
|
88 |
|
lubomir@281
|
89 |
public Variable popA() {
|
lubomir@307
|
90 |
return popT(VarType.REFERENCE);
|
lubomir@281
|
91 |
}
|
lubomir@281
|
92 |
|
lubomir@281
|
93 |
public Variable popT(final int type) {
|
lubomir@281
|
94 |
final Variable variable = getT(0, type);
|
lubomir@281
|
95 |
popImpl(1);
|
lubomir@281
|
96 |
return variable;
|
lubomir@281
|
97 |
}
|
lubomir@281
|
98 |
|
lubomir@281
|
99 |
public Variable pop() {
|
lubomir@281
|
100 |
final Variable variable = get(0);
|
lubomir@281
|
101 |
popImpl(1);
|
lubomir@281
|
102 |
return variable;
|
lubomir@281
|
103 |
}
|
lubomir@281
|
104 |
|
lubomir@281
|
105 |
public void pop(final int count) {
|
lubomir@281
|
106 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
107 |
if (count > stackSize) {
|
lubomir@281
|
108 |
throw new IllegalStateException("Stack underflow");
|
lubomir@281
|
109 |
}
|
lubomir@281
|
110 |
popImpl(count);
|
lubomir@281
|
111 |
}
|
lubomir@281
|
112 |
|
lubomir@281
|
113 |
public Variable getI(final int indexFromTop) {
|
lubomir@307
|
114 |
return getT(indexFromTop, VarType.INTEGER);
|
lubomir@281
|
115 |
}
|
lubomir@281
|
116 |
|
lubomir@281
|
117 |
public Variable getL(final int indexFromTop) {
|
lubomir@307
|
118 |
return getT(indexFromTop, VarType.LONG);
|
lubomir@281
|
119 |
}
|
lubomir@281
|
120 |
|
lubomir@281
|
121 |
public Variable getF(final int indexFromTop) {
|
lubomir@307
|
122 |
return getT(indexFromTop, VarType.FLOAT);
|
lubomir@281
|
123 |
}
|
lubomir@281
|
124 |
|
lubomir@281
|
125 |
public Variable getD(final int indexFromTop) {
|
lubomir@307
|
126 |
return getT(indexFromTop, VarType.DOUBLE);
|
lubomir@281
|
127 |
}
|
lubomir@281
|
128 |
|
lubomir@281
|
129 |
public Variable getA(final int indexFromTop) {
|
lubomir@307
|
130 |
return getT(indexFromTop, VarType.REFERENCE);
|
lubomir@281
|
131 |
}
|
lubomir@281
|
132 |
|
lubomir@281
|
133 |
public Variable getT(final int indexFromTop, final int type) {
|
lubomir@281
|
134 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
135 |
if (indexFromTop >= stackSize) {
|
lubomir@281
|
136 |
throw new IllegalStateException("Stack underflow");
|
lubomir@281
|
137 |
}
|
lubomir@281
|
138 |
final int stackValue =
|
lubomir@281
|
139 |
stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
|
lubomir@281
|
140 |
if ((stackValue & 0xff) != type) {
|
lubomir@281
|
141 |
throw new IllegalStateException("Type mismatch");
|
lubomir@281
|
142 |
}
|
lubomir@281
|
143 |
|
lubomir@281
|
144 |
return getVariable(stackValue);
|
lubomir@281
|
145 |
}
|
lubomir@281
|
146 |
|
lubomir@281
|
147 |
public Variable get(final int indexFromTop) {
|
lubomir@281
|
148 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
149 |
if (indexFromTop >= stackSize) {
|
lubomir@281
|
150 |
throw new IllegalStateException("Stack underflow");
|
lubomir@281
|
151 |
}
|
lubomir@281
|
152 |
final int stackValue =
|
lubomir@281
|
153 |
stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
|
lubomir@281
|
154 |
|
lubomir@281
|
155 |
return getVariable(stackValue);
|
lubomir@281
|
156 |
}
|
lubomir@281
|
157 |
|
lubomir@281
|
158 |
private int pushTypeImpl(final int type) {
|
lubomir@281
|
159 |
final int count = typeCounters[type];
|
lubomir@281
|
160 |
final int value = (count << 8) | (type & 0xff);
|
lubomir@281
|
161 |
incCounter(type);
|
lubomir@281
|
162 |
stackTypeIndexPairs.add(value);
|
lubomir@281
|
163 |
|
lubomir@281
|
164 |
return value;
|
lubomir@281
|
165 |
}
|
lubomir@281
|
166 |
|
lubomir@281
|
167 |
private void popImpl(final int count) {
|
lubomir@281
|
168 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
169 |
for (int i = stackSize - count; i < stackSize; ++i) {
|
lubomir@281
|
170 |
final int value = stackTypeIndexPairs.get(i);
|
lubomir@281
|
171 |
decCounter(value & 0xff);
|
lubomir@281
|
172 |
}
|
lubomir@281
|
173 |
|
lubomir@281
|
174 |
stackTypeIndexPairs.setSize(stackSize - count);
|
lubomir@281
|
175 |
}
|
lubomir@281
|
176 |
|
lubomir@281
|
177 |
private void incCounter(final int type) {
|
lubomir@281
|
178 |
final int newValue = ++typeCounters[type];
|
lubomir@281
|
179 |
if (typeMaxCounters[type] < newValue) {
|
lubomir@281
|
180 |
typeMaxCounters[type] = newValue;
|
lubomir@221
|
181 |
}
|
lubomir@221
|
182 |
}
|
lubomir@221
|
183 |
|
lubomir@281
|
184 |
private void decCounter(final int type) {
|
lubomir@281
|
185 |
--typeCounters[type];
|
lubomir@221
|
186 |
}
|
lubomir@221
|
187 |
|
lubomir@281
|
188 |
public Variable getVariable(final int typeAndIndex) {
|
lubomir@281
|
189 |
final int type = typeAndIndex & 0xff;
|
lubomir@281
|
190 |
final int index = typeAndIndex >> 8;
|
lubomir@221
|
191 |
|
lubomir@281
|
192 |
return Variable.getStackVariable(type, index);
|
lubomir@221
|
193 |
}
|
lubomir@221
|
194 |
}
|