2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import org.apidesign.vm4brwsr.ByteCodeParser.TypeArray;
23 final class StackMapper {
24 private final TypeArray stackTypeIndexPairs;
25 private final StringArray stackValues;
27 public StackMapper() {
28 stackTypeIndexPairs = new TypeArray();
29 stackValues = new StringArray();
33 stackTypeIndexPairs.clear();
37 public void syncWithFrameStack(final TypeArray frameStack) {
40 final int size = frameStack.getSize();
41 for (int i = 0; i < size; ++i) {
42 pushTypeImpl(VarType.fromStackMapType(frameStack.get(i)));
46 public Variable pushI() {
47 return pushT(VarType.INTEGER);
50 public Variable pushL() {
51 return pushT(VarType.LONG);
54 public Variable pushF() {
55 return pushT(VarType.FLOAT);
58 public Variable pushD() {
59 return pushT(VarType.DOUBLE);
62 public Variable pushA() {
63 return pushT(VarType.REFERENCE);
66 public Variable pushT(final int type) {
67 return getVariable(pushTypeImpl(type));
70 void assign(Appendable out, int varType, CharSequence s) throws IOException {
71 pushTypeAndValue(varType, s);
74 void replace(Appendable out, int varType, String format, CharSequence... arr)
76 StringBuilder sb = new StringBuilder();
77 ByteCodeToJavaScript.emitImpl(sb, format, arr);
78 String[] values = stackValues.toArray();
79 final int last = stackTypeIndexPairs.getSize() - 1;
80 values[last] = sb.toString();
81 stackTypeIndexPairs.set(last, varType);
84 void flush(Appendable out) throws IOException {
85 int count = stackTypeIndexPairs.getSize();
86 for (int i = 0; i < count; i++) {
87 String val = stackValues.getAndClear(i, true);
91 CharSequence var = getVariable(stackTypeIndexPairs.get(i));
92 ByteCodeToJavaScript.emitImpl(out, "var @1 = @2;", var, val);
96 public CharSequence popI() {
97 return popT(VarType.INTEGER);
100 public CharSequence popL() {
101 return popT(VarType.LONG);
104 public CharSequence popF() {
105 return popT(VarType.FLOAT);
108 public CharSequence popD() {
109 return popT(VarType.DOUBLE);
112 public CharSequence popA() {
113 return popT(VarType.REFERENCE);
116 public CharSequence popT(final int type) {
117 final CharSequence variable = getT(0, type);
122 public CharSequence popValue() {
123 final CharSequence variable = getT(0, -1);
127 public Variable pop(Appendable out) throws IOException {
129 final Variable variable = get(0);
134 public void pop(final int count) {
135 final int stackSize = stackTypeIndexPairs.getSize();
136 if (count > stackSize) {
137 throw new IllegalStateException("Stack underflow");
142 public CharSequence getI(final int indexFromTop) {
143 return getT(indexFromTop, VarType.INTEGER);
146 public CharSequence getL(final int indexFromTop) {
147 return getT(indexFromTop, VarType.LONG);
150 public CharSequence getF(final int indexFromTop) {
151 return getT(indexFromTop, VarType.FLOAT);
154 public CharSequence getD(final int indexFromTop) {
155 return getT(indexFromTop, VarType.DOUBLE);
158 public CharSequence getA(final int indexFromTop) {
159 return getT(indexFromTop, VarType.REFERENCE);
162 public CharSequence getT(final int indexFromTop, final int type) {
163 return getT(indexFromTop, type, true);
165 public CharSequence getT(final int indexFromTop, final int type, boolean clear) {
166 final int stackSize = stackTypeIndexPairs.getSize();
167 if (indexFromTop >= stackSize) {
168 throw new IllegalStateException("Stack underflow");
170 final int stackValue =
171 stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
172 if (type != -1 && (stackValue & 0xff) != type) {
173 throw new IllegalStateException("Type mismatch");
176 stackValues.getAndClear(stackSize - indexFromTop - 1, clear);
180 return getVariable(stackValue);
183 public Variable get(final int indexFromTop) {
184 final int stackSize = stackTypeIndexPairs.getSize();
185 if (indexFromTop >= stackSize) {
186 throw new IllegalStateException("Stack underflow");
188 final int stackValue =
189 stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
191 return getVariable(stackValue);
194 private int pushTypeImpl(final int type) {
195 final int count = stackTypeIndexPairs.getSize();
196 final int value = (count << 8) | (type & 0xff);
197 stackTypeIndexPairs.add(value);
199 addStackValue(count, null);
203 private void pushTypeAndValue(final int type, CharSequence v) {
204 final int count = stackTypeIndexPairs.getSize();
205 final int value = (count << 8) | (type & 0xff);
206 stackTypeIndexPairs.add(value);
207 final String val = v.toString();
208 addStackValue(count, val);
211 private void addStackValue(int at, final String val) {
212 final String[] arr = stackValues.toArray();
213 if (arr.length > at) {
216 stackValues.add(val);
220 private void popImpl(final int count) {
221 final int stackSize = stackTypeIndexPairs.getSize();
222 stackTypeIndexPairs.setSize(stackSize - count);
225 public Variable getVariable(final int typeAndIndex) {
226 final int type = typeAndIndex & 0xff;
227 final int index = typeAndIndex >> 8;
229 return Variable.getStackVariable(type, index);