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;
|
lubomir@221
|
25 |
|
lubomir@307
|
26 |
public StackMapper() {
|
lubomir@281
|
27 |
stackTypeIndexPairs = new TypeArray();
|
lubomir@221
|
28 |
}
|
lubomir@221
|
29 |
|
lubomir@281
|
30 |
public void clear() {
|
lubomir@281
|
31 |
stackTypeIndexPairs.clear();
|
lubomir@281
|
32 |
}
|
lubomir@281
|
33 |
|
lubomir@281
|
34 |
public void syncWithFrameStack(final TypeArray frameStack) {
|
lubomir@281
|
35 |
clear();
|
lubomir@281
|
36 |
|
lubomir@281
|
37 |
final int size = frameStack.getSize();
|
lubomir@281
|
38 |
for (int i = 0; i < size; ++i) {
|
lubomir@307
|
39 |
pushTypeImpl(VarType.fromStackMapType(frameStack.get(i)));
|
lubomir@221
|
40 |
}
|
lubomir@221
|
41 |
}
|
lubomir@221
|
42 |
|
lubomir@281
|
43 |
public Variable pushI() {
|
lubomir@307
|
44 |
return pushT(VarType.INTEGER);
|
lubomir@281
|
45 |
}
|
lubomir@281
|
46 |
|
lubomir@281
|
47 |
public Variable pushL() {
|
lubomir@307
|
48 |
return pushT(VarType.LONG);
|
lubomir@281
|
49 |
}
|
lubomir@281
|
50 |
|
lubomir@281
|
51 |
public Variable pushF() {
|
lubomir@307
|
52 |
return pushT(VarType.FLOAT);
|
lubomir@281
|
53 |
}
|
lubomir@281
|
54 |
|
lubomir@281
|
55 |
public Variable pushD() {
|
lubomir@307
|
56 |
return pushT(VarType.DOUBLE);
|
lubomir@281
|
57 |
}
|
lubomir@281
|
58 |
|
lubomir@281
|
59 |
public Variable pushA() {
|
lubomir@307
|
60 |
return pushT(VarType.REFERENCE);
|
lubomir@281
|
61 |
}
|
lubomir@281
|
62 |
|
lubomir@281
|
63 |
public Variable pushT(final int type) {
|
lubomir@281
|
64 |
return getVariable(pushTypeImpl(type));
|
lubomir@281
|
65 |
}
|
lubomir@281
|
66 |
|
jaroslav@1453
|
67 |
void assign(Appendable out, int varType, CharSequence s) throws IOException {
|
jaroslav@1453
|
68 |
ByteCodeToJavaScript.emit(out, "var @1 = @2;", pushT(varType), s);
|
jaroslav@1453
|
69 |
}
|
jaroslav@1453
|
70 |
|
lubomir@281
|
71 |
public Variable popI() {
|
lubomir@307
|
72 |
return popT(VarType.INTEGER);
|
lubomir@281
|
73 |
}
|
lubomir@281
|
74 |
|
lubomir@281
|
75 |
public Variable popL() {
|
lubomir@307
|
76 |
return popT(VarType.LONG);
|
lubomir@281
|
77 |
}
|
lubomir@281
|
78 |
|
lubomir@281
|
79 |
public Variable popF() {
|
lubomir@307
|
80 |
return popT(VarType.FLOAT);
|
lubomir@281
|
81 |
}
|
lubomir@281
|
82 |
|
lubomir@281
|
83 |
public Variable popD() {
|
lubomir@307
|
84 |
return popT(VarType.DOUBLE);
|
lubomir@281
|
85 |
}
|
lubomir@281
|
86 |
|
lubomir@281
|
87 |
public Variable popA() {
|
lubomir@307
|
88 |
return popT(VarType.REFERENCE);
|
lubomir@281
|
89 |
}
|
lubomir@281
|
90 |
|
lubomir@281
|
91 |
public Variable popT(final int type) {
|
lubomir@281
|
92 |
final Variable variable = getT(0, type);
|
lubomir@281
|
93 |
popImpl(1);
|
lubomir@281
|
94 |
return variable;
|
lubomir@281
|
95 |
}
|
lubomir@281
|
96 |
|
lubomir@281
|
97 |
public Variable pop() {
|
lubomir@281
|
98 |
final Variable variable = get(0);
|
lubomir@281
|
99 |
popImpl(1);
|
lubomir@281
|
100 |
return variable;
|
lubomir@281
|
101 |
}
|
lubomir@281
|
102 |
|
lubomir@281
|
103 |
public void pop(final int count) {
|
lubomir@281
|
104 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
105 |
if (count > stackSize) {
|
lubomir@281
|
106 |
throw new IllegalStateException("Stack underflow");
|
lubomir@281
|
107 |
}
|
lubomir@281
|
108 |
popImpl(count);
|
lubomir@281
|
109 |
}
|
lubomir@281
|
110 |
|
lubomir@281
|
111 |
public Variable getI(final int indexFromTop) {
|
lubomir@307
|
112 |
return getT(indexFromTop, VarType.INTEGER);
|
lubomir@281
|
113 |
}
|
lubomir@281
|
114 |
|
lubomir@281
|
115 |
public Variable getL(final int indexFromTop) {
|
lubomir@307
|
116 |
return getT(indexFromTop, VarType.LONG);
|
lubomir@281
|
117 |
}
|
lubomir@281
|
118 |
|
lubomir@281
|
119 |
public Variable getF(final int indexFromTop) {
|
lubomir@307
|
120 |
return getT(indexFromTop, VarType.FLOAT);
|
lubomir@281
|
121 |
}
|
lubomir@281
|
122 |
|
lubomir@281
|
123 |
public Variable getD(final int indexFromTop) {
|
lubomir@307
|
124 |
return getT(indexFromTop, VarType.DOUBLE);
|
lubomir@281
|
125 |
}
|
lubomir@281
|
126 |
|
lubomir@281
|
127 |
public Variable getA(final int indexFromTop) {
|
lubomir@307
|
128 |
return getT(indexFromTop, VarType.REFERENCE);
|
lubomir@281
|
129 |
}
|
lubomir@281
|
130 |
|
lubomir@281
|
131 |
public Variable getT(final int indexFromTop, final int type) {
|
lubomir@281
|
132 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
133 |
if (indexFromTop >= stackSize) {
|
lubomir@281
|
134 |
throw new IllegalStateException("Stack underflow");
|
lubomir@281
|
135 |
}
|
lubomir@281
|
136 |
final int stackValue =
|
lubomir@281
|
137 |
stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
|
lubomir@281
|
138 |
if ((stackValue & 0xff) != type) {
|
lubomir@281
|
139 |
throw new IllegalStateException("Type mismatch");
|
lubomir@281
|
140 |
}
|
lubomir@281
|
141 |
|
lubomir@281
|
142 |
return getVariable(stackValue);
|
lubomir@281
|
143 |
}
|
lubomir@281
|
144 |
|
lubomir@281
|
145 |
public Variable get(final int indexFromTop) {
|
lubomir@281
|
146 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
147 |
if (indexFromTop >= stackSize) {
|
lubomir@281
|
148 |
throw new IllegalStateException("Stack underflow");
|
lubomir@281
|
149 |
}
|
lubomir@281
|
150 |
final int stackValue =
|
lubomir@281
|
151 |
stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
|
lubomir@281
|
152 |
|
lubomir@281
|
153 |
return getVariable(stackValue);
|
lubomir@281
|
154 |
}
|
lubomir@281
|
155 |
|
lubomir@281
|
156 |
private int pushTypeImpl(final int type) {
|
jaroslav@1454
|
157 |
final int count = stackTypeIndexPairs.getSize();
|
lubomir@281
|
158 |
final int value = (count << 8) | (type & 0xff);
|
lubomir@281
|
159 |
stackTypeIndexPairs.add(value);
|
lubomir@281
|
160 |
|
lubomir@281
|
161 |
return value;
|
lubomir@281
|
162 |
}
|
lubomir@281
|
163 |
|
lubomir@281
|
164 |
private void popImpl(final int count) {
|
lubomir@281
|
165 |
final int stackSize = stackTypeIndexPairs.getSize();
|
lubomir@281
|
166 |
for (int i = stackSize - count; i < stackSize; ++i) {
|
lubomir@281
|
167 |
final int value = stackTypeIndexPairs.get(i);
|
lubomir@281
|
168 |
}
|
lubomir@281
|
169 |
|
lubomir@281
|
170 |
stackTypeIndexPairs.setSize(stackSize - count);
|
lubomir@281
|
171 |
}
|
lubomir@281
|
172 |
|
lubomir@281
|
173 |
public Variable getVariable(final int typeAndIndex) {
|
lubomir@281
|
174 |
final int type = typeAndIndex & 0xff;
|
lubomir@281
|
175 |
final int index = typeAndIndex >> 8;
|
lubomir@221
|
176 |
|
lubomir@281
|
177 |
return Variable.getStackVariable(type, index);
|
lubomir@221
|
178 |
}
|
lubomir@221
|
179 |
}
|