1.1 --- a/javap/src/main/java/org/apidesign/javap/TypeArray.java Fri Dec 14 11:15:37 2012 +0100
1.2 +++ b/javap/src/main/java/org/apidesign/javap/TypeArray.java Fri Dec 14 15:06:53 2012 +0100
1.3 @@ -1,186 +1,186 @@
1.4 -/*
1.5 - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -
1.29 -package org.apidesign.javap;
1.30 -
1.31 -import static org.apidesign.javap.RuntimeConstants.ITEM_Bogus;
1.32 -import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
1.33 -import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
1.34 -import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
1.35 -import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
1.36 -import static org.apidesign.javap.RuntimeConstants.ITEM_Null;
1.37 -import static org.apidesign.javap.RuntimeConstants.ITEM_InitObject;
1.38 -import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
1.39 -import static org.apidesign.javap.RuntimeConstants.ITEM_NewObject;
1.40 -
1.41 -public final class TypeArray {
1.42 - private static final int CAPACITY_INCREMENT = 16;
1.43 -
1.44 - private int[] types;
1.45 - private int size;
1.46 -
1.47 - public TypeArray() {
1.48 - }
1.49 -
1.50 - public TypeArray(final TypeArray initialTypes) {
1.51 - setAll(initialTypes);
1.52 - }
1.53 -
1.54 - public void add(final int newType) {
1.55 - ensureCapacity(size + 1);
1.56 - types[size++] = newType;
1.57 - }
1.58 -
1.59 - public void addAll(final TypeArray newTypes) {
1.60 - addAll(newTypes.types, 0, newTypes.size);
1.61 - }
1.62 -
1.63 - public void addAll(final int[] newTypes) {
1.64 - addAll(newTypes, 0, newTypes.length);
1.65 - }
1.66 -
1.67 - public void addAll(final int[] newTypes,
1.68 - final int offset,
1.69 - final int count) {
1.70 - if (count > 0) {
1.71 - ensureCapacity(size + count);
1.72 - arraycopy(newTypes, offset, types, size, count);
1.73 - size += count;
1.74 - }
1.75 - }
1.76 -
1.77 - public void set(final int index, final int newType) {
1.78 - types[index] = newType;
1.79 - }
1.80 -
1.81 - public void setAll(final TypeArray newTypes) {
1.82 - setAll(newTypes.types, 0, newTypes.size);
1.83 - }
1.84 -
1.85 - public void setAll(final int[] newTypes) {
1.86 - setAll(newTypes, 0, newTypes.length);
1.87 - }
1.88 -
1.89 - public void setAll(final int[] newTypes,
1.90 - final int offset,
1.91 - final int count) {
1.92 - if (count > 0) {
1.93 - ensureCapacity(count);
1.94 - arraycopy(newTypes, offset, types, 0, count);
1.95 - size = count;
1.96 - } else {
1.97 - clear();
1.98 - }
1.99 - }
1.100 -
1.101 - public void setSize(final int newSize) {
1.102 - if (size != newSize) {
1.103 - ensureCapacity(newSize);
1.104 -
1.105 - for (int i = size; i < newSize; ++i) {
1.106 - types[i] = 0;
1.107 - }
1.108 - size = newSize;
1.109 - }
1.110 - }
1.111 -
1.112 - public void clear() {
1.113 - size = 0;
1.114 - }
1.115 -
1.116 - public int getSize() {
1.117 - return size;
1.118 - }
1.119 -
1.120 - public int get(final int index) {
1.121 - return types[index];
1.122 - }
1.123 -
1.124 - public static String typeString(final int type) {
1.125 - switch (type & 0xff) {
1.126 - case ITEM_Bogus:
1.127 - return "_top_";
1.128 - case ITEM_Integer:
1.129 - return "_int_";
1.130 - case ITEM_Float:
1.131 - return "_float_";
1.132 - case ITEM_Double:
1.133 - return "_double_";
1.134 - case ITEM_Long:
1.135 - return "_long_";
1.136 - case ITEM_Null:
1.137 - return "_null_";
1.138 - case ITEM_InitObject: // UninitializedThis
1.139 - return "_init_";
1.140 - case ITEM_Object:
1.141 - return "_object_";
1.142 - case ITEM_NewObject: // Uninitialized
1.143 - return "_new_";
1.144 - default:
1.145 - throw new IllegalArgumentException("Unknown type");
1.146 - }
1.147 - }
1.148 -
1.149 - @Override
1.150 - public String toString() {
1.151 - final StringBuilder sb = new StringBuilder("[");
1.152 - if (size > 0) {
1.153 - sb.append(typeString(types[0]));
1.154 - for (int i = 1; i < size; ++i) {
1.155 - sb.append(", ");
1.156 - sb.append(typeString(types[i]));
1.157 - }
1.158 - }
1.159 -
1.160 - return sb.append(']').toString();
1.161 - }
1.162 -
1.163 - private void ensureCapacity(final int minCapacity) {
1.164 - if ((minCapacity == 0)
1.165 - || (types != null) && (minCapacity <= types.length)) {
1.166 - return;
1.167 - }
1.168 -
1.169 - final int newCapacity =
1.170 - ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
1.171 - * CAPACITY_INCREMENT;
1.172 - final int[] newTypes = new int[newCapacity];
1.173 -
1.174 - if (size > 0) {
1.175 - arraycopy(types, 0, newTypes, 0, size);
1.176 - }
1.177 -
1.178 - types = newTypes;
1.179 - }
1.180 -
1.181 - // no System.arraycopy
1.182 - private void arraycopy(final int[] src, final int srcPos,
1.183 - final int[] dest, final int destPos,
1.184 - final int length) {
1.185 - for (int i = 0; i < length; ++i) {
1.186 - dest[destPos + i] = src[srcPos + i];
1.187 - }
1.188 - }
1.189 -}
1.190 +/*
1.191 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
1.192 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.193 + *
1.194 + * This code is free software; you can redistribute it and/or modify it
1.195 + * under the terms of the GNU General Public License version 2 only, as
1.196 + * published by the Free Software Foundation. Oracle designates this
1.197 + * particular file as subject to the "Classpath" exception as provided
1.198 + * by Oracle in the LICENSE file that accompanied this code.
1.199 + *
1.200 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.201 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.202 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.203 + * version 2 for more details (a copy is included in the LICENSE file that
1.204 + * accompanied this code).
1.205 + *
1.206 + * You should have received a copy of the GNU General Public License version
1.207 + * 2 along with this work; if not, write to the Free Software Foundation,
1.208 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.209 + *
1.210 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.211 + * or visit www.oracle.com if you need additional information or have any
1.212 + * questions.
1.213 + */
1.214 +
1.215 +package org.apidesign.javap;
1.216 +
1.217 +import static org.apidesign.javap.RuntimeConstants.ITEM_Bogus;
1.218 +import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
1.219 +import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
1.220 +import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
1.221 +import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
1.222 +import static org.apidesign.javap.RuntimeConstants.ITEM_Null;
1.223 +import static org.apidesign.javap.RuntimeConstants.ITEM_InitObject;
1.224 +import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
1.225 +import static org.apidesign.javap.RuntimeConstants.ITEM_NewObject;
1.226 +
1.227 +public final class TypeArray {
1.228 + private static final int CAPACITY_INCREMENT = 16;
1.229 +
1.230 + private int[] types;
1.231 + private int size;
1.232 +
1.233 + public TypeArray() {
1.234 + }
1.235 +
1.236 + public TypeArray(final TypeArray initialTypes) {
1.237 + setAll(initialTypes);
1.238 + }
1.239 +
1.240 + public void add(final int newType) {
1.241 + ensureCapacity(size + 1);
1.242 + types[size++] = newType;
1.243 + }
1.244 +
1.245 + public void addAll(final TypeArray newTypes) {
1.246 + addAll(newTypes.types, 0, newTypes.size);
1.247 + }
1.248 +
1.249 + public void addAll(final int[] newTypes) {
1.250 + addAll(newTypes, 0, newTypes.length);
1.251 + }
1.252 +
1.253 + public void addAll(final int[] newTypes,
1.254 + final int offset,
1.255 + final int count) {
1.256 + if (count > 0) {
1.257 + ensureCapacity(size + count);
1.258 + arraycopy(newTypes, offset, types, size, count);
1.259 + size += count;
1.260 + }
1.261 + }
1.262 +
1.263 + public void set(final int index, final int newType) {
1.264 + types[index] = newType;
1.265 + }
1.266 +
1.267 + public void setAll(final TypeArray newTypes) {
1.268 + setAll(newTypes.types, 0, newTypes.size);
1.269 + }
1.270 +
1.271 + public void setAll(final int[] newTypes) {
1.272 + setAll(newTypes, 0, newTypes.length);
1.273 + }
1.274 +
1.275 + public void setAll(final int[] newTypes,
1.276 + final int offset,
1.277 + final int count) {
1.278 + if (count > 0) {
1.279 + ensureCapacity(count);
1.280 + arraycopy(newTypes, offset, types, 0, count);
1.281 + size = count;
1.282 + } else {
1.283 + clear();
1.284 + }
1.285 + }
1.286 +
1.287 + public void setSize(final int newSize) {
1.288 + if (size != newSize) {
1.289 + ensureCapacity(newSize);
1.290 +
1.291 + for (int i = size; i < newSize; ++i) {
1.292 + types[i] = 0;
1.293 + }
1.294 + size = newSize;
1.295 + }
1.296 + }
1.297 +
1.298 + public void clear() {
1.299 + size = 0;
1.300 + }
1.301 +
1.302 + public int getSize() {
1.303 + return size;
1.304 + }
1.305 +
1.306 + public int get(final int index) {
1.307 + return types[index];
1.308 + }
1.309 +
1.310 + public static String typeString(final int type) {
1.311 + switch (type & 0xff) {
1.312 + case ITEM_Bogus:
1.313 + return "_top_";
1.314 + case ITEM_Integer:
1.315 + return "_int_";
1.316 + case ITEM_Float:
1.317 + return "_float_";
1.318 + case ITEM_Double:
1.319 + return "_double_";
1.320 + case ITEM_Long:
1.321 + return "_long_";
1.322 + case ITEM_Null:
1.323 + return "_null_";
1.324 + case ITEM_InitObject: // UninitializedThis
1.325 + return "_init_";
1.326 + case ITEM_Object:
1.327 + return "_object_";
1.328 + case ITEM_NewObject: // Uninitialized
1.329 + return "_new_";
1.330 + default:
1.331 + throw new IllegalArgumentException("Unknown type");
1.332 + }
1.333 + }
1.334 +
1.335 + @Override
1.336 + public String toString() {
1.337 + final StringBuilder sb = new StringBuilder("[");
1.338 + if (size > 0) {
1.339 + sb.append(typeString(types[0]));
1.340 + for (int i = 1; i < size; ++i) {
1.341 + sb.append(", ");
1.342 + sb.append(typeString(types[i]));
1.343 + }
1.344 + }
1.345 +
1.346 + return sb.append(']').toString();
1.347 + }
1.348 +
1.349 + private void ensureCapacity(final int minCapacity) {
1.350 + if ((minCapacity == 0)
1.351 + || (types != null) && (minCapacity <= types.length)) {
1.352 + return;
1.353 + }
1.354 +
1.355 + final int newCapacity =
1.356 + ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
1.357 + * CAPACITY_INCREMENT;
1.358 + final int[] newTypes = new int[newCapacity];
1.359 +
1.360 + if (size > 0) {
1.361 + arraycopy(types, 0, newTypes, 0, size);
1.362 + }
1.363 +
1.364 + types = newTypes;
1.365 + }
1.366 +
1.367 + // no System.arraycopy
1.368 + private void arraycopy(final int[] src, final int srcPos,
1.369 + final int[] dest, final int destPos,
1.370 + final int length) {
1.371 + for (int i = 0; i < length; ++i) {
1.372 + dest[destPos + i] = src[srcPos + i];
1.373 + }
1.374 + }
1.375 +}
2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/LocalsMapper.java Fri Dec 14 11:15:37 2012 +0100
2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/LocalsMapper.java Fri Dec 14 15:06:53 2012 +0100
2.3 @@ -1,141 +1,141 @@
2.4 -/**
2.5 - * Back 2 Browser Bytecode Translator
2.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.7 - *
2.8 - * This program is free software: you can redistribute it and/or modify
2.9 - * it under the terms of the GNU General Public License as published by
2.10 - * the Free Software Foundation, version 2 of the License.
2.11 - *
2.12 - * This program is distributed in the hope that it will be useful,
2.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 - * GNU General Public License for more details.
2.16 - *
2.17 - * You should have received a copy of the GNU General Public License
2.18 - * along with this program. Look for COPYING file in the top folder.
2.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
2.20 - */
2.21 -package org.apidesign.vm4brwsr;
2.22 -
2.23 -import java.io.IOException;
2.24 -import org.apidesign.javap.RuntimeConstants;
2.25 -import org.apidesign.javap.TypeArray;
2.26 -
2.27 -final class LocalsMapper {
2.28 - private final TypeArray argTypeRecords;
2.29 - private final TypeArray localTypeRecords;
2.30 -
2.31 - public LocalsMapper(final TypeArray stackMapArgs) {
2.32 - final TypeArray initTypeRecords = new TypeArray();
2.33 - updateRecords(initTypeRecords, stackMapArgs);
2.34 -
2.35 - argTypeRecords = initTypeRecords;
2.36 - localTypeRecords = new TypeArray(initTypeRecords);
2.37 - }
2.38 -
2.39 - public void outputArguments(final Appendable out) throws IOException {
2.40 - final int argRecordCount = argTypeRecords.getSize();
2.41 - if (argRecordCount > 0) {
2.42 - Variable variable = getVariable(argTypeRecords, 0);
2.43 - out.append(variable);
2.44 -
2.45 - int i = variable.isCategory2() ? 2 : 1;
2.46 - while (i < argRecordCount) {
2.47 - variable = getVariable(argTypeRecords, i);
2.48 - out.append(", ");
2.49 - out.append(variable);
2.50 - i += variable.isCategory2() ? 2 : 1;
2.51 - }
2.52 - }
2.53 - }
2.54 -
2.55 - public void syncWithFrameLocals(final TypeArray frameLocals) {
2.56 - updateRecords(localTypeRecords, frameLocals);
2.57 - }
2.58 -
2.59 - public Variable setI(final int index) {
2.60 - return setT(index, VarType.INTEGER);
2.61 - }
2.62 -
2.63 - public Variable setL(final int index) {
2.64 - return setT(index, VarType.LONG);
2.65 - }
2.66 -
2.67 - public Variable setF(final int index) {
2.68 - return setT(index, VarType.FLOAT);
2.69 - }
2.70 -
2.71 - public Variable setD(final int index) {
2.72 - return setT(index, VarType.DOUBLE);
2.73 - }
2.74 -
2.75 - public Variable setA(final int index) {
2.76 - return setT(index, VarType.REFERENCE);
2.77 - }
2.78 -
2.79 - public Variable setT(final int index, final int type) {
2.80 - updateRecord(localTypeRecords, index, type);
2.81 - return Variable.getLocalVariable(type, index);
2.82 - }
2.83 -
2.84 - public Variable getI(final int index) {
2.85 - return getT(index, VarType.INTEGER);
2.86 - }
2.87 -
2.88 - public Variable getL(final int index) {
2.89 - return getT(index, VarType.LONG);
2.90 - }
2.91 -
2.92 - public Variable getF(final int index) {
2.93 - return getT(index, VarType.FLOAT);
2.94 - }
2.95 -
2.96 - public Variable getD(final int index) {
2.97 - return getT(index, VarType.DOUBLE);
2.98 - }
2.99 -
2.100 - public Variable getA(final int index) {
2.101 - return getT(index, VarType.REFERENCE);
2.102 - }
2.103 -
2.104 - public Variable getT(final int index, final int type) {
2.105 - final int oldRecordValue = localTypeRecords.get(index);
2.106 - if ((oldRecordValue & 0xff) != type) {
2.107 - throw new IllegalStateException("Type mismatch");
2.108 - }
2.109 -
2.110 - return Variable.getLocalVariable(type, index);
2.111 - }
2.112 -
2.113 - private static void updateRecords(final TypeArray typeRecords,
2.114 - final TypeArray stackMapTypes) {
2.115 - final int srcSize = stackMapTypes.getSize();
2.116 - for (int i = 0, dstIndex = 0; i < srcSize; ++i) {
2.117 - final int smType = stackMapTypes.get(i);
2.118 - if (smType == RuntimeConstants.ITEM_Bogus) {
2.119 - ++dstIndex;
2.120 - continue;
2.121 - }
2.122 - final int varType = VarType.fromStackMapType(smType);
2.123 - updateRecord(typeRecords, dstIndex, varType);
2.124 - dstIndex += VarType.isCategory2(varType) ? 2 : 1;
2.125 - }
2.126 - }
2.127 -
2.128 - private static void updateRecord(final TypeArray typeRecords,
2.129 - final int index, final int type) {
2.130 - if (typeRecords.getSize() < (index + 1)) {
2.131 - typeRecords.setSize(index + 1);
2.132 - }
2.133 -
2.134 - final int oldRecordValue = typeRecords.get(index);
2.135 - final int usedTypesMask =
2.136 - (oldRecordValue >> 8) | (1 << type);
2.137 - typeRecords.set(index, (usedTypesMask << 8) | type);
2.138 - }
2.139 -
2.140 - private static Variable getVariable(final TypeArray typeRecords,
2.141 - final int index) {
2.142 - return Variable.getLocalVariable(typeRecords.get(index) & 0xff, index);
2.143 - }
2.144 -}
2.145 +/**
2.146 + * Back 2 Browser Bytecode Translator
2.147 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.148 + *
2.149 + * This program is free software: you can redistribute it and/or modify
2.150 + * it under the terms of the GNU General Public License as published by
2.151 + * the Free Software Foundation, version 2 of the License.
2.152 + *
2.153 + * This program is distributed in the hope that it will be useful,
2.154 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.155 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.156 + * GNU General Public License for more details.
2.157 + *
2.158 + * You should have received a copy of the GNU General Public License
2.159 + * along with this program. Look for COPYING file in the top folder.
2.160 + * If not, see http://opensource.org/licenses/GPL-2.0.
2.161 + */
2.162 +package org.apidesign.vm4brwsr;
2.163 +
2.164 +import java.io.IOException;
2.165 +import org.apidesign.javap.RuntimeConstants;
2.166 +import org.apidesign.javap.TypeArray;
2.167 +
2.168 +final class LocalsMapper {
2.169 + private final TypeArray argTypeRecords;
2.170 + private final TypeArray localTypeRecords;
2.171 +
2.172 + public LocalsMapper(final TypeArray stackMapArgs) {
2.173 + final TypeArray initTypeRecords = new TypeArray();
2.174 + updateRecords(initTypeRecords, stackMapArgs);
2.175 +
2.176 + argTypeRecords = initTypeRecords;
2.177 + localTypeRecords = new TypeArray(initTypeRecords);
2.178 + }
2.179 +
2.180 + public void outputArguments(final Appendable out) throws IOException {
2.181 + final int argRecordCount = argTypeRecords.getSize();
2.182 + if (argRecordCount > 0) {
2.183 + Variable variable = getVariable(argTypeRecords, 0);
2.184 + out.append(variable);
2.185 +
2.186 + int i = variable.isCategory2() ? 2 : 1;
2.187 + while (i < argRecordCount) {
2.188 + variable = getVariable(argTypeRecords, i);
2.189 + out.append(", ");
2.190 + out.append(variable);
2.191 + i += variable.isCategory2() ? 2 : 1;
2.192 + }
2.193 + }
2.194 + }
2.195 +
2.196 + public void syncWithFrameLocals(final TypeArray frameLocals) {
2.197 + updateRecords(localTypeRecords, frameLocals);
2.198 + }
2.199 +
2.200 + public Variable setI(final int index) {
2.201 + return setT(index, VarType.INTEGER);
2.202 + }
2.203 +
2.204 + public Variable setL(final int index) {
2.205 + return setT(index, VarType.LONG);
2.206 + }
2.207 +
2.208 + public Variable setF(final int index) {
2.209 + return setT(index, VarType.FLOAT);
2.210 + }
2.211 +
2.212 + public Variable setD(final int index) {
2.213 + return setT(index, VarType.DOUBLE);
2.214 + }
2.215 +
2.216 + public Variable setA(final int index) {
2.217 + return setT(index, VarType.REFERENCE);
2.218 + }
2.219 +
2.220 + public Variable setT(final int index, final int type) {
2.221 + updateRecord(localTypeRecords, index, type);
2.222 + return Variable.getLocalVariable(type, index);
2.223 + }
2.224 +
2.225 + public Variable getI(final int index) {
2.226 + return getT(index, VarType.INTEGER);
2.227 + }
2.228 +
2.229 + public Variable getL(final int index) {
2.230 + return getT(index, VarType.LONG);
2.231 + }
2.232 +
2.233 + public Variable getF(final int index) {
2.234 + return getT(index, VarType.FLOAT);
2.235 + }
2.236 +
2.237 + public Variable getD(final int index) {
2.238 + return getT(index, VarType.DOUBLE);
2.239 + }
2.240 +
2.241 + public Variable getA(final int index) {
2.242 + return getT(index, VarType.REFERENCE);
2.243 + }
2.244 +
2.245 + public Variable getT(final int index, final int type) {
2.246 + final int oldRecordValue = localTypeRecords.get(index);
2.247 + if ((oldRecordValue & 0xff) != type) {
2.248 + throw new IllegalStateException("Type mismatch");
2.249 + }
2.250 +
2.251 + return Variable.getLocalVariable(type, index);
2.252 + }
2.253 +
2.254 + private static void updateRecords(final TypeArray typeRecords,
2.255 + final TypeArray stackMapTypes) {
2.256 + final int srcSize = stackMapTypes.getSize();
2.257 + for (int i = 0, dstIndex = 0; i < srcSize; ++i) {
2.258 + final int smType = stackMapTypes.get(i);
2.259 + if (smType == RuntimeConstants.ITEM_Bogus) {
2.260 + ++dstIndex;
2.261 + continue;
2.262 + }
2.263 + final int varType = VarType.fromStackMapType(smType);
2.264 + updateRecord(typeRecords, dstIndex, varType);
2.265 + dstIndex += VarType.isCategory2(varType) ? 2 : 1;
2.266 + }
2.267 + }
2.268 +
2.269 + private static void updateRecord(final TypeArray typeRecords,
2.270 + final int index, final int type) {
2.271 + if (typeRecords.getSize() < (index + 1)) {
2.272 + typeRecords.setSize(index + 1);
2.273 + }
2.274 +
2.275 + final int oldRecordValue = typeRecords.get(index);
2.276 + final int usedTypesMask =
2.277 + (oldRecordValue >> 8) | (1 << type);
2.278 + typeRecords.set(index, (usedTypesMask << 8) | type);
2.279 + }
2.280 +
2.281 + private static Variable getVariable(final TypeArray typeRecords,
2.282 + final int index) {
2.283 + return Variable.getLocalVariable(typeRecords.get(index) & 0xff, index);
2.284 + }
2.285 +}
3.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/VarType.java Fri Dec 14 11:15:37 2012 +0100
3.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VarType.java Fri Dec 14 15:06:53 2012 +0100
3.3 @@ -1,110 +1,110 @@
3.4 -/**
3.5 - * Back 2 Browser Bytecode Translator
3.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.7 - *
3.8 - * This program is free software: you can redistribute it and/or modify
3.9 - * it under the terms of the GNU General Public License as published by
3.10 - * the Free Software Foundation, version 2 of the License.
3.11 - *
3.12 - * This program is distributed in the hope that it will be useful,
3.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.15 - * GNU General Public License for more details.
3.16 - *
3.17 - * You should have received a copy of the GNU General Public License
3.18 - * along with this program. Look for COPYING file in the top folder.
3.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
3.20 - */
3.21 -package org.apidesign.vm4brwsr;
3.22 -
3.23 -import org.apidesign.javap.RuntimeConstants;
3.24 -
3.25 -final class VarType {
3.26 - public static final int INTEGER = 0;
3.27 - public static final int LONG = 1;
3.28 - public static final int FLOAT = 2;
3.29 - public static final int DOUBLE = 3;
3.30 - public static final int REFERENCE = 4;
3.31 -
3.32 - public static final int LAST = REFERENCE;
3.33 -
3.34 - private VarType() {
3.35 - }
3.36 -
3.37 - public static boolean isCategory2(final int varType) {
3.38 - return (varType == LONG) || (varType == DOUBLE);
3.39 - }
3.40 -
3.41 - public static int fromStackMapType(final int smType) {
3.42 - switch (smType & 0xff) {
3.43 - case RuntimeConstants.ITEM_Integer:
3.44 - return VarType.INTEGER;
3.45 - case RuntimeConstants.ITEM_Float:
3.46 - return VarType.FLOAT;
3.47 - case RuntimeConstants.ITEM_Double:
3.48 - return VarType.DOUBLE;
3.49 - case RuntimeConstants.ITEM_Long:
3.50 - return VarType.LONG;
3.51 - case RuntimeConstants.ITEM_Object:
3.52 - return VarType.REFERENCE;
3.53 -
3.54 - case RuntimeConstants.ITEM_Bogus:
3.55 - case RuntimeConstants.ITEM_Null:
3.56 - case RuntimeConstants.ITEM_InitObject:
3.57 - case RuntimeConstants.ITEM_NewObject:
3.58 - /* unclear how to handle for now */
3.59 - default:
3.60 - throw new IllegalStateException("Unhandled stack map type");
3.61 - }
3.62 - }
3.63 -
3.64 - public static int fromConstantType(final byte constantTag) {
3.65 - switch (constantTag) {
3.66 - case RuntimeConstants.CONSTANT_INTEGER:
3.67 - return VarType.INTEGER;
3.68 - case RuntimeConstants.CONSTANT_FLOAT:
3.69 - return VarType.FLOAT;
3.70 - case RuntimeConstants.CONSTANT_LONG:
3.71 - return VarType.LONG;
3.72 - case RuntimeConstants.CONSTANT_DOUBLE:
3.73 - return VarType.DOUBLE;
3.74 -
3.75 - case RuntimeConstants.CONSTANT_CLASS:
3.76 - case RuntimeConstants.CONSTANT_UTF8:
3.77 - case RuntimeConstants.CONSTANT_UNICODE:
3.78 - case RuntimeConstants.CONSTANT_STRING:
3.79 - return VarType.REFERENCE;
3.80 -
3.81 - case RuntimeConstants.CONSTANT_FIELD:
3.82 - case RuntimeConstants.CONSTANT_METHOD:
3.83 - case RuntimeConstants.CONSTANT_INTERFACEMETHOD:
3.84 - case RuntimeConstants.CONSTANT_NAMEANDTYPE:
3.85 - /* unclear how to handle for now */
3.86 - default:
3.87 - throw new IllegalStateException("Unhandled constant tag");
3.88 - }
3.89 - }
3.90 -
3.91 - public static int fromFieldType(final char fieldType) {
3.92 - switch (fieldType) {
3.93 - case 'B':
3.94 - case 'C':
3.95 - case 'S':
3.96 - case 'Z':
3.97 - case 'I':
3.98 - return VarType.INTEGER;
3.99 - case 'J':
3.100 - return VarType.LONG;
3.101 - case 'F':
3.102 - return VarType.FLOAT;
3.103 - case 'D':
3.104 - return VarType.DOUBLE;
3.105 - case 'L':
3.106 - case '[':
3.107 - return VarType.REFERENCE;
3.108 -
3.109 - default:
3.110 - throw new IllegalStateException("Unhandled field type");
3.111 - }
3.112 - }
3.113 -}
3.114 +/**
3.115 + * Back 2 Browser Bytecode Translator
3.116 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.117 + *
3.118 + * This program is free software: you can redistribute it and/or modify
3.119 + * it under the terms of the GNU General Public License as published by
3.120 + * the Free Software Foundation, version 2 of the License.
3.121 + *
3.122 + * This program is distributed in the hope that it will be useful,
3.123 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.124 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.125 + * GNU General Public License for more details.
3.126 + *
3.127 + * You should have received a copy of the GNU General Public License
3.128 + * along with this program. Look for COPYING file in the top folder.
3.129 + * If not, see http://opensource.org/licenses/GPL-2.0.
3.130 + */
3.131 +package org.apidesign.vm4brwsr;
3.132 +
3.133 +import org.apidesign.javap.RuntimeConstants;
3.134 +
3.135 +final class VarType {
3.136 + public static final int INTEGER = 0;
3.137 + public static final int LONG = 1;
3.138 + public static final int FLOAT = 2;
3.139 + public static final int DOUBLE = 3;
3.140 + public static final int REFERENCE = 4;
3.141 +
3.142 + public static final int LAST = REFERENCE;
3.143 +
3.144 + private VarType() {
3.145 + }
3.146 +
3.147 + public static boolean isCategory2(final int varType) {
3.148 + return (varType == LONG) || (varType == DOUBLE);
3.149 + }
3.150 +
3.151 + public static int fromStackMapType(final int smType) {
3.152 + switch (smType & 0xff) {
3.153 + case RuntimeConstants.ITEM_Integer:
3.154 + return VarType.INTEGER;
3.155 + case RuntimeConstants.ITEM_Float:
3.156 + return VarType.FLOAT;
3.157 + case RuntimeConstants.ITEM_Double:
3.158 + return VarType.DOUBLE;
3.159 + case RuntimeConstants.ITEM_Long:
3.160 + return VarType.LONG;
3.161 + case RuntimeConstants.ITEM_Object:
3.162 + return VarType.REFERENCE;
3.163 +
3.164 + case RuntimeConstants.ITEM_Bogus:
3.165 + case RuntimeConstants.ITEM_Null:
3.166 + case RuntimeConstants.ITEM_InitObject:
3.167 + case RuntimeConstants.ITEM_NewObject:
3.168 + /* unclear how to handle for now */
3.169 + default:
3.170 + throw new IllegalStateException("Unhandled stack map type");
3.171 + }
3.172 + }
3.173 +
3.174 + public static int fromConstantType(final byte constantTag) {
3.175 + switch (constantTag) {
3.176 + case RuntimeConstants.CONSTANT_INTEGER:
3.177 + return VarType.INTEGER;
3.178 + case RuntimeConstants.CONSTANT_FLOAT:
3.179 + return VarType.FLOAT;
3.180 + case RuntimeConstants.CONSTANT_LONG:
3.181 + return VarType.LONG;
3.182 + case RuntimeConstants.CONSTANT_DOUBLE:
3.183 + return VarType.DOUBLE;
3.184 +
3.185 + case RuntimeConstants.CONSTANT_CLASS:
3.186 + case RuntimeConstants.CONSTANT_UTF8:
3.187 + case RuntimeConstants.CONSTANT_UNICODE:
3.188 + case RuntimeConstants.CONSTANT_STRING:
3.189 + return VarType.REFERENCE;
3.190 +
3.191 + case RuntimeConstants.CONSTANT_FIELD:
3.192 + case RuntimeConstants.CONSTANT_METHOD:
3.193 + case RuntimeConstants.CONSTANT_INTERFACEMETHOD:
3.194 + case RuntimeConstants.CONSTANT_NAMEANDTYPE:
3.195 + /* unclear how to handle for now */
3.196 + default:
3.197 + throw new IllegalStateException("Unhandled constant tag");
3.198 + }
3.199 + }
3.200 +
3.201 + public static int fromFieldType(final char fieldType) {
3.202 + switch (fieldType) {
3.203 + case 'B':
3.204 + case 'C':
3.205 + case 'S':
3.206 + case 'Z':
3.207 + case 'I':
3.208 + return VarType.INTEGER;
3.209 + case 'J':
3.210 + return VarType.LONG;
3.211 + case 'F':
3.212 + return VarType.FLOAT;
3.213 + case 'D':
3.214 + return VarType.DOUBLE;
3.215 + case 'L':
3.216 + case '[':
3.217 + return VarType.REFERENCE;
3.218 +
3.219 + default:
3.220 + throw new IllegalStateException("Unhandled field type");
3.221 + }
3.222 + }
3.223 +}
4.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/Variable.java Fri Dec 14 11:15:37 2012 +0100
4.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Variable.java Fri Dec 14 15:06:53 2012 +0100
4.3 @@ -1,100 +1,100 @@
4.4 -/**
4.5 - * Back 2 Browser Bytecode Translator
4.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.7 - *
4.8 - * This program is free software: you can redistribute it and/or modify
4.9 - * it under the terms of the GNU General Public License as published by
4.10 - * the Free Software Foundation, version 2 of the License.
4.11 - *
4.12 - * This program is distributed in the hope that it will be useful,
4.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.15 - * GNU General Public License for more details.
4.16 - *
4.17 - * You should have received a copy of the GNU General Public License
4.18 - * along with this program. Look for COPYING file in the top folder.
4.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
4.20 - */
4.21 -package org.apidesign.vm4brwsr;
4.22 -
4.23 -final class Variable implements CharSequence {
4.24 - private static final String STACK_VAR_PREFIX = "st";
4.25 - private static final String LOCAL_VAR_PREFIX = "lc";
4.26 -
4.27 - private final String name;
4.28 - private final int type;
4.29 - private final int index;
4.30 -
4.31 - private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' };
4.32 -
4.33 - private Variable(final String prefix, final int type, final int index) {
4.34 - this.name = prefix + TYPE_IDS[type] + index;
4.35 - this.type = type;
4.36 - this.index = index;
4.37 - }
4.38 -
4.39 - public static Variable getStackVariable(
4.40 - final int type, final int index) {
4.41 - // TODO: precreate frequently used variables
4.42 - return new Variable(STACK_VAR_PREFIX, type, index);
4.43 - }
4.44 -
4.45 - public static Variable getLocalVariable(
4.46 - final int type, final int index) {
4.47 - // TODO: precreate frequently used variables
4.48 - return new Variable(LOCAL_VAR_PREFIX, type, index);
4.49 - }
4.50 -
4.51 - public String getName() {
4.52 - return name;
4.53 - }
4.54 -
4.55 - public int getType() {
4.56 - return type;
4.57 - }
4.58 -
4.59 - public int getIndex() {
4.60 - return index;
4.61 - }
4.62 -
4.63 - public boolean isCategory2() {
4.64 - return VarType.isCategory2(type);
4.65 - }
4.66 -
4.67 - @Override
4.68 - public int length() {
4.69 - return name.length();
4.70 - }
4.71 -
4.72 - @Override
4.73 - public char charAt(final int index) {
4.74 - return name.charAt(index);
4.75 - }
4.76 -
4.77 - @Override
4.78 - public CharSequence subSequence(final int start, final int end) {
4.79 - return name.subSequence(start, end);
4.80 - }
4.81 -
4.82 - @Override
4.83 - public int hashCode() {
4.84 - return name.hashCode();
4.85 - }
4.86 -
4.87 - @Override
4.88 - public boolean equals(final Object other) {
4.89 - if (this == other) {
4.90 - return true;
4.91 - }
4.92 - if (!(other instanceof Variable)) {
4.93 - return false;
4.94 - }
4.95 -
4.96 - return name.equals(((Variable) other).name);
4.97 - }
4.98 -
4.99 - @Override
4.100 - public String toString() {
4.101 - return name;
4.102 - }
4.103 -}
4.104 +/**
4.105 + * Back 2 Browser Bytecode Translator
4.106 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.107 + *
4.108 + * This program is free software: you can redistribute it and/or modify
4.109 + * it under the terms of the GNU General Public License as published by
4.110 + * the Free Software Foundation, version 2 of the License.
4.111 + *
4.112 + * This program is distributed in the hope that it will be useful,
4.113 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.114 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.115 + * GNU General Public License for more details.
4.116 + *
4.117 + * You should have received a copy of the GNU General Public License
4.118 + * along with this program. Look for COPYING file in the top folder.
4.119 + * If not, see http://opensource.org/licenses/GPL-2.0.
4.120 + */
4.121 +package org.apidesign.vm4brwsr;
4.122 +
4.123 +final class Variable implements CharSequence {
4.124 + private static final String STACK_VAR_PREFIX = "st";
4.125 + private static final String LOCAL_VAR_PREFIX = "lc";
4.126 +
4.127 + private final String name;
4.128 + private final int type;
4.129 + private final int index;
4.130 +
4.131 + private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' };
4.132 +
4.133 + private Variable(final String prefix, final int type, final int index) {
4.134 + this.name = prefix + TYPE_IDS[type] + index;
4.135 + this.type = type;
4.136 + this.index = index;
4.137 + }
4.138 +
4.139 + public static Variable getStackVariable(
4.140 + final int type, final int index) {
4.141 + // TODO: precreate frequently used variables
4.142 + return new Variable(STACK_VAR_PREFIX, type, index);
4.143 + }
4.144 +
4.145 + public static Variable getLocalVariable(
4.146 + final int type, final int index) {
4.147 + // TODO: precreate frequently used variables
4.148 + return new Variable(LOCAL_VAR_PREFIX, type, index);
4.149 + }
4.150 +
4.151 + public String getName() {
4.152 + return name;
4.153 + }
4.154 +
4.155 + public int getType() {
4.156 + return type;
4.157 + }
4.158 +
4.159 + public int getIndex() {
4.160 + return index;
4.161 + }
4.162 +
4.163 + public boolean isCategory2() {
4.164 + return VarType.isCategory2(type);
4.165 + }
4.166 +
4.167 + @Override
4.168 + public int length() {
4.169 + return name.length();
4.170 + }
4.171 +
4.172 + @Override
4.173 + public char charAt(final int index) {
4.174 + return name.charAt(index);
4.175 + }
4.176 +
4.177 + @Override
4.178 + public CharSequence subSequence(final int start, final int end) {
4.179 + return name.subSequence(start, end);
4.180 + }
4.181 +
4.182 + @Override
4.183 + public int hashCode() {
4.184 + return name.hashCode();
4.185 + }
4.186 +
4.187 + @Override
4.188 + public boolean equals(final Object other) {
4.189 + if (this == other) {
4.190 + return true;
4.191 + }
4.192 + if (!(other instanceof Variable)) {
4.193 + return false;
4.194 + }
4.195 +
4.196 + return name.equals(((Variable) other).name);
4.197 + }
4.198 +
4.199 + @Override
4.200 + public String toString() {
4.201 + return name;
4.202 + }
4.203 +}