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 |
|
jaroslav@1453
|
20 |
import java.io.IOException;
|
jaroslav@810
|
21 |
import org.apidesign.vm4brwsr.ByteCodeParser.TypeArray;
|
lubomir@281
|
22 |
|
lubomir@310
|
23 |
final class StackMapper {
|
lubomir@281
|
24 |
private final TypeArray stackTypeIndexPairs;
|
jaroslav@1455
|
25 |
private final StringArray stackValues;
|
lubomir@221
|
26 |
|
lubomir@307
|
27 |
public StackMapper() {
|
lubomir@281
|
28 |
stackTypeIndexPairs = new TypeArray();
|
jaroslav@1455
|
29 |
stackValues = new StringArray();
|
lubomir@221
|
30 |
}
|
lubomir@221
|
31 |
|
lubomir@281
|
32 |
public void clear() {
|
lubomir@281
|
33 |
stackTypeIndexPairs.clear();
|
jaroslav@1455
|
34 |
stackValues.clear();
|
lubomir@281
|
35 |
}
|
lubomir@281
|
36 |
|
lubomir@281
|
37 |
public void syncWithFrameStack(final TypeArray frameStack) {
|
lubomir@281
|
38 |
clear();
|
lubomir@281
|
39 |
|
lubomir@281
|
40 |
final int size = frameStack.getSize();
|
lubomir@281
|
41 |
for (int i = 0; i < size; ++i) {
|
lubomir@307
|
42 |
pushTypeImpl(VarType.fromStackMapType(frameStack.get(i)));
|
lubomir@221
|
43 |
}
|
lubomir@221
|
44 |
}
|
lubomir@221
|
45 |
|
lubomir@281
|
46 |
public Variable pushI() {
|
lubomir@307
|
47 |
return pushT(VarType.INTEGER);
|
lubomir@281
|
48 |
}
|
lubomir@281
|
49 |
|
lubomir@281
|
50 |
public Variable pushL() {
|
lubomir@307
|
51 |
return pushT(VarType.LONG);
|
lubomir@281
|
52 |
}
|
lubomir@281
|
53 |
|
lubomir@281
|
54 |
public Variable pushF() {
|
lubomir@307
|
55 |
return pushT(VarType.FLOAT);
|
lubomir@281
|
56 |
}
|
lubomir@281
|
57 |
|
lubomir@281
|
58 |
public Variable pushD() {
|
lubomir@307
|
59 |
return pushT(VarType.DOUBLE);
|
lubomir@281
|
60 |
}
|
lubomir@281
|
61 |
|
lubomir@281
|
62 |
public Variable pushA() {
|
lubomir@307
|
63 |
return pushT(VarType.REFERENCE);
|
lubomir@281
|
64 |
}
|
lubomir@281
|
65 |
|
lubomir@281
|
66 |
public Variable pushT(final int type) {
|
lubomir@281
|
67 |
return getVariable(pushTypeImpl(type));
|
lubomir@281
|
68 |
}
|
lubomir@281
|
69 |
|
jaroslav@1453
|
70 |
void assign(Appendable out, int varType, CharSequence s) throws IOException {
|
jaroslav@1455
|
71 |
pushTypeAndValue(varType, s);
|
jaroslav@1455
|
72 |
flush(out);
|
jaroslav@1455
|
73 |
}
|
jaroslav@1455
|
74 |
|
jaroslav@1455
|
75 |
void flush(Appendable out) throws IOException {
|
jaroslav@1455
|
76 |
int count = stackTypeIndexPairs.getSize();
|
jaroslav@1455
|
77 |
for (int i = 0; i < count; i++) {
|
jaroslav@1455
|
78 |
String val = stackValues.getAndClear(i);
|
jaroslav@1455
|
79 |
if (val == null) {
|
jaroslav@1455
|
80 |
continue;
|
jaroslav@1455
|
81 |
}
|
jaroslav@1455
|
82 |
CharSequence var = getVariable(stackTypeIndexPairs.get(i));
|
jaroslav@1455
|
83 |
ByteCodeToJavaScript.emitImpl(out, "var @1 = @2;", var, val);
|
jaroslav@1455
|
84 |
}
|
jaroslav@1453
|
85 |
}
|
jaroslav@1453
|
86 |
|
lubomir@281
|
87 |
public Variable popI() {
|
lubomir@307
|
88 |
return popT(VarType.INTEGER);
|
lubomir@281
|
89 |
}
|
lubomir@281
|
90 |
|
lubomir@281
|
91 |
public Variable popL() {
|
lubomir@307
|
92 |
return popT(VarType.LONG);
|
lubomir@281
|
93 |
}
|
lubomir@281
|
94 |
|
lubomir@281
|
95 |
public Variable popF() {
|
lubomir@307
|
96 |
return popT(VarType.FLOAT);
|
lubomir@281
|
97 |
}
|
lubomir@281
|
98 |
|
lubomir@281
|
99 |
public Variable popD() {
|
lubomir@307
|
100 |
return popT(VarType.DOUBLE);
|
lubomir@281
|
101 |
}
|
lubomir@281
|
102 |
|
lubomir@281
|
103 |
public Variable popA() {
|
lubomir@307
|
104 |
return popT(VarType.REFERENCE);
|
lubomir@281
|
105 |
}
|
lubomir@281
|
106 |
|
lubomir@281
|
107 |
public Variable popT(final int type) {
|
lubomir@281
|
108 |
final Variable variable = getT(0, type);
|
lubomir@281
|
109 |
popImpl(1);
|
lubomir@281
|
110 |
return variable;
|
lubomir@281
|
111 |
}
|
lubomir@281
|
112 |
|
lubomir@281
|
113 |
public Variable pop() {
|
lubomir@281
|
114 |
final Variable variable = get(0);
|
lubomir@281
|
115 |
popImpl(1);
|
lubomir@281
|
116 |
return variable;
|
lubomir@281
|
117 |
}
|
lubomir@281
|
118 |
|
lubomir@281
|
119 |
public void pop(final int count) {
|
lubomir@281
|
120 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
121 |
if (count > stackSize) {
|
lubomir@281
|
122 |
throw new IllegalStateException("Stack underflow");
|
lubomir@281
|
123 |
}
|
lubomir@281
|
124 |
popImpl(count);
|
lubomir@281
|
125 |
}
|
lubomir@281
|
126 |
|
lubomir@281
|
127 |
public Variable getI(final int indexFromTop) {
|
lubomir@307
|
128 |
return getT(indexFromTop, VarType.INTEGER);
|
lubomir@281
|
129 |
}
|
lubomir@281
|
130 |
|
lubomir@281
|
131 |
public Variable getL(final int indexFromTop) {
|
lubomir@307
|
132 |
return getT(indexFromTop, VarType.LONG);
|
lubomir@281
|
133 |
}
|
lubomir@281
|
134 |
|
lubomir@281
|
135 |
public Variable getF(final int indexFromTop) {
|
lubomir@307
|
136 |
return getT(indexFromTop, VarType.FLOAT);
|
lubomir@281
|
137 |
}
|
lubomir@281
|
138 |
|
lubomir@281
|
139 |
public Variable getD(final int indexFromTop) {
|
lubomir@307
|
140 |
return getT(indexFromTop, VarType.DOUBLE);
|
lubomir@281
|
141 |
}
|
lubomir@281
|
142 |
|
lubomir@281
|
143 |
public Variable getA(final int indexFromTop) {
|
lubomir@307
|
144 |
return getT(indexFromTop, VarType.REFERENCE);
|
lubomir@281
|
145 |
}
|
lubomir@281
|
146 |
|
lubomir@281
|
147 |
public Variable getT(final int indexFromTop, final int type) {
|
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 |
if ((stackValue & 0xff) != type) {
|
lubomir@281
|
155 |
throw new IllegalStateException("Type mismatch");
|
lubomir@281
|
156 |
}
|
lubomir@281
|
157 |
|
lubomir@281
|
158 |
return getVariable(stackValue);
|
lubomir@281
|
159 |
}
|
lubomir@281
|
160 |
|
lubomir@281
|
161 |
public Variable get(final int indexFromTop) {
|
lubomir@281
|
162 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
163 |
if (indexFromTop >= stackSize) {
|
lubomir@281
|
164 |
throw new IllegalStateException("Stack underflow");
|
lubomir@281
|
165 |
}
|
lubomir@281
|
166 |
final int stackValue =
|
lubomir@281
|
167 |
stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
|
lubomir@281
|
168 |
|
lubomir@281
|
169 |
return getVariable(stackValue);
|
lubomir@281
|
170 |
}
|
lubomir@281
|
171 |
|
lubomir@281
|
172 |
private int pushTypeImpl(final int type) {
|
jaroslav@1454
|
173 |
final int count = stackTypeIndexPairs.getSize();
|
lubomir@281
|
174 |
final int value = (count << 8) | (type & 0xff);
|
lubomir@281
|
175 |
stackTypeIndexPairs.add(value);
|
jaroslav@1455
|
176 |
|
jaroslav@1455
|
177 |
addStackValue(count, null);
|
jaroslav@1455
|
178 |
return value;
|
jaroslav@1455
|
179 |
}
|
lubomir@281
|
180 |
|
jaroslav@1455
|
181 |
private void pushTypeAndValue(final int type, CharSequence v) {
|
jaroslav@1455
|
182 |
final int count = stackTypeIndexPairs.getSize();
|
jaroslav@1455
|
183 |
final int value = (count << 8) | (type & 0xff);
|
jaroslav@1455
|
184 |
stackTypeIndexPairs.add(value);
|
jaroslav@1455
|
185 |
final String val = v.toString();
|
jaroslav@1455
|
186 |
addStackValue(count, val);
|
jaroslav@1455
|
187 |
}
|
jaroslav@1455
|
188 |
|
jaroslav@1455
|
189 |
private void addStackValue(int at, final String val) {
|
jaroslav@1455
|
190 |
final String[] arr = stackValues.toArray();
|
jaroslav@1455
|
191 |
if (arr.length > at) {
|
jaroslav@1455
|
192 |
arr[at] = val;
|
jaroslav@1455
|
193 |
} else {
|
jaroslav@1455
|
194 |
stackValues.add(val);
|
jaroslav@1455
|
195 |
}
|
lubomir@281
|
196 |
}
|
lubomir@281
|
197 |
|
lubomir@281
|
198 |
private void popImpl(final int count) {
|
lubomir@281
|
199 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
200 |
stackTypeIndexPairs.setSize(stackSize - count);
|
lubomir@281
|
201 |
}
|
lubomir@281
|
202 |
|
lubomir@281
|
203 |
public Variable getVariable(final int typeAndIndex) {
|
lubomir@281
|
204 |
final int type = typeAndIndex & 0xff;
|
lubomir@281
|
205 |
final int index = typeAndIndex >> 8;
|
lubomir@221
|
206 |
|
lubomir@281
|
207 |
return Variable.getStackVariable(type, index);
|
lubomir@221
|
208 |
}
|
lubomir@221
|
209 |
}
|