1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/StackToVariableMapper.java Fri Dec 07 19:16:21 2012 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,230 +0,0 @@
1.4 -/**
1.5 - * Back 2 Browser Bytecode Translator
1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, version 2 of the License.
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program. Look for COPYING file in the top folder.
1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 - */
1.21 -package org.apidesign.vm4brwsr;
1.22 -
1.23 -import org.apidesign.javap.TypeArray;
1.24 -
1.25 -import static org.apidesign.javap.RuntimeConstants.ITEM_Bogus;
1.26 -import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
1.27 -import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
1.28 -import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
1.29 -import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
1.30 -import static org.apidesign.javap.RuntimeConstants.ITEM_Null;
1.31 -import static org.apidesign.javap.RuntimeConstants.ITEM_InitObject;
1.32 -import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
1.33 -import static org.apidesign.javap.RuntimeConstants.ITEM_NewObject;
1.34 -
1.35 -public final class StackToVariableMapper {
1.36 - private final TypeArray stackTypeIndexPairs;
1.37 - private int[] typeCounters;
1.38 - private int[] typeMaxCounters;
1.39 -
1.40 - public StackToVariableMapper() {
1.41 - stackTypeIndexPairs = new TypeArray();
1.42 - typeCounters = new int[Variable.LAST_TYPE + 1];
1.43 - typeMaxCounters = new int[Variable.LAST_TYPE + 1];
1.44 - }
1.45 -
1.46 - public void clear() {
1.47 - for (int type = 0; type <= Variable.LAST_TYPE; ++type) {
1.48 - typeCounters[type] = 0;
1.49 - }
1.50 - stackTypeIndexPairs.clear();
1.51 - }
1.52 -
1.53 - public void syncWithFrameStack(final TypeArray frameStack) {
1.54 - clear();
1.55 -
1.56 - final int size = frameStack.getSize();
1.57 - for (int i = 0; i < size; ++i) {
1.58 - final int frameStackValue = frameStack.get(i);
1.59 - switch (frameStackValue & 0xff) {
1.60 - case ITEM_Integer:
1.61 - pushTypeImpl(Variable.TYPE_INT);
1.62 - break;
1.63 - case ITEM_Float:
1.64 - pushTypeImpl(Variable.TYPE_FLOAT);
1.65 - break;
1.66 - case ITEM_Double:
1.67 - pushTypeImpl(Variable.TYPE_DOUBLE);
1.68 - break;
1.69 - case ITEM_Long:
1.70 - pushTypeImpl(Variable.TYPE_LONG);
1.71 - break;
1.72 - case ITEM_Object:
1.73 - pushTypeImpl(Variable.TYPE_REF);
1.74 - break;
1.75 -
1.76 - case ITEM_Bogus:
1.77 - case ITEM_Null:
1.78 - case ITEM_InitObject:
1.79 - case ITEM_NewObject:
1.80 - /* unclear how to handle for now */
1.81 - default:
1.82 - throw new IllegalStateException(
1.83 - "Unhandled frame stack type");
1.84 - }
1.85 - }
1.86 - }
1.87 -
1.88 - public Variable pushI() {
1.89 - return pushT(Variable.TYPE_INT);
1.90 - }
1.91 -
1.92 - public Variable pushL() {
1.93 - return pushT(Variable.TYPE_LONG);
1.94 - }
1.95 -
1.96 - public Variable pushF() {
1.97 - return pushT(Variable.TYPE_FLOAT);
1.98 - }
1.99 -
1.100 - public Variable pushD() {
1.101 - return pushT(Variable.TYPE_DOUBLE);
1.102 - }
1.103 -
1.104 - public Variable pushA() {
1.105 - return pushT(Variable.TYPE_REF);
1.106 - }
1.107 -
1.108 - public Variable pushT(final int type) {
1.109 - return getVariable(pushTypeImpl(type));
1.110 - }
1.111 -
1.112 - public Variable popI() {
1.113 - return popT(Variable.TYPE_INT);
1.114 - }
1.115 -
1.116 - public Variable popL() {
1.117 - return popT(Variable.TYPE_LONG);
1.118 - }
1.119 -
1.120 - public Variable popF() {
1.121 - return popT(Variable.TYPE_FLOAT);
1.122 - }
1.123 -
1.124 - public Variable popD() {
1.125 - return popT(Variable.TYPE_DOUBLE);
1.126 - }
1.127 -
1.128 - public Variable popA() {
1.129 - return popT(Variable.TYPE_REF);
1.130 - }
1.131 -
1.132 - public Variable popT(final int type) {
1.133 - final Variable variable = getT(0, type);
1.134 - popImpl(1);
1.135 - return variable;
1.136 - }
1.137 -
1.138 - public Variable pop() {
1.139 - final Variable variable = get(0);
1.140 - popImpl(1);
1.141 - return variable;
1.142 - }
1.143 -
1.144 - public void pop(final int count) {
1.145 - final int stackSize = stackTypeIndexPairs.getSize();
1.146 - if (count > stackSize) {
1.147 - throw new IllegalStateException("Stack underflow");
1.148 - }
1.149 - popImpl(count);
1.150 - }
1.151 -
1.152 - public Variable getI(final int indexFromTop) {
1.153 - return getT(indexFromTop, Variable.TYPE_INT);
1.154 - }
1.155 -
1.156 - public Variable getL(final int indexFromTop) {
1.157 - return getT(indexFromTop, Variable.TYPE_LONG);
1.158 - }
1.159 -
1.160 - public Variable getF(final int indexFromTop) {
1.161 - return getT(indexFromTop, Variable.TYPE_FLOAT);
1.162 - }
1.163 -
1.164 - public Variable getD(final int indexFromTop) {
1.165 - return getT(indexFromTop, Variable.TYPE_DOUBLE);
1.166 - }
1.167 -
1.168 - public Variable getA(final int indexFromTop) {
1.169 - return getT(indexFromTop, Variable.TYPE_REF);
1.170 - }
1.171 -
1.172 - public Variable getT(final int indexFromTop, final int type) {
1.173 - final int stackSize = stackTypeIndexPairs.getSize();
1.174 - if (indexFromTop >= stackSize) {
1.175 - throw new IllegalStateException("Stack underflow");
1.176 - }
1.177 - final int stackValue =
1.178 - stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
1.179 - if ((stackValue & 0xff) != type) {
1.180 - throw new IllegalStateException("Type mismatch");
1.181 - }
1.182 -
1.183 - return getVariable(stackValue);
1.184 - }
1.185 -
1.186 - public Variable get(final int indexFromTop) {
1.187 - final int stackSize = stackTypeIndexPairs.getSize();
1.188 - if (indexFromTop >= stackSize) {
1.189 - throw new IllegalStateException("Stack underflow");
1.190 - }
1.191 - final int stackValue =
1.192 - stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
1.193 -
1.194 - return getVariable(stackValue);
1.195 - }
1.196 -
1.197 - private int pushTypeImpl(final int type) {
1.198 - final int count = typeCounters[type];
1.199 - final int value = (count << 8) | (type & 0xff);
1.200 - incCounter(type);
1.201 - stackTypeIndexPairs.add(value);
1.202 -
1.203 - return value;
1.204 - }
1.205 -
1.206 - private void popImpl(final int count) {
1.207 - final int stackSize = stackTypeIndexPairs.getSize();
1.208 - for (int i = stackSize - count; i < stackSize; ++i) {
1.209 - final int value = stackTypeIndexPairs.get(i);
1.210 - decCounter(value & 0xff);
1.211 - }
1.212 -
1.213 - stackTypeIndexPairs.setSize(stackSize - count);
1.214 - }
1.215 -
1.216 - private void incCounter(final int type) {
1.217 - final int newValue = ++typeCounters[type];
1.218 - if (typeMaxCounters[type] < newValue) {
1.219 - typeMaxCounters[type] = newValue;
1.220 - }
1.221 - }
1.222 -
1.223 - private void decCounter(final int type) {
1.224 - --typeCounters[type];
1.225 - }
1.226 -
1.227 - public Variable getVariable(final int typeAndIndex) {
1.228 - final int type = typeAndIndex & 0xff;
1.229 - final int index = typeAndIndex >> 8;
1.230 -
1.231 - return Variable.getStackVariable(type, index);
1.232 - }
1.233 -}