1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/LocalsMapper.java Fri Mar 22 16:59:47 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,142 +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 java.io.IOException;
1.24 -import org.apidesign.javap.RuntimeConstants;
1.25 -import org.apidesign.javap.TypeArray;
1.26 -
1.27 -final class LocalsMapper {
1.28 - private final TypeArray argTypeRecords;
1.29 - private final TypeArray localTypeRecords;
1.30 -
1.31 - public LocalsMapper(final TypeArray stackMapArgs) {
1.32 - final TypeArray initTypeRecords = new TypeArray();
1.33 - updateRecords(initTypeRecords, stackMapArgs);
1.34 -
1.35 - argTypeRecords = initTypeRecords;
1.36 - localTypeRecords = new TypeArray(initTypeRecords);
1.37 - }
1.38 -
1.39 - public void outputArguments(final Appendable out, boolean isStatic) throws IOException {
1.40 - final int argRecordCount = argTypeRecords.getSize();
1.41 - int first = isStatic ? 0 : 1;
1.42 - if (argRecordCount > first) {
1.43 - Variable variable = getVariable(argTypeRecords, first);
1.44 - out.append(variable);
1.45 -
1.46 - int i = first + (variable.isCategory2() ? 2 : 1);
1.47 - while (i < argRecordCount) {
1.48 - variable = getVariable(argTypeRecords, i);
1.49 - out.append(", ");
1.50 - out.append(variable);
1.51 - i += variable.isCategory2() ? 2 : 1;
1.52 - }
1.53 - }
1.54 - }
1.55 -
1.56 - public void syncWithFrameLocals(final TypeArray frameLocals) {
1.57 - updateRecords(localTypeRecords, frameLocals);
1.58 - }
1.59 -
1.60 - public Variable setI(final int index) {
1.61 - return setT(index, VarType.INTEGER);
1.62 - }
1.63 -
1.64 - public Variable setL(final int index) {
1.65 - return setT(index, VarType.LONG);
1.66 - }
1.67 -
1.68 - public Variable setF(final int index) {
1.69 - return setT(index, VarType.FLOAT);
1.70 - }
1.71 -
1.72 - public Variable setD(final int index) {
1.73 - return setT(index, VarType.DOUBLE);
1.74 - }
1.75 -
1.76 - public Variable setA(final int index) {
1.77 - return setT(index, VarType.REFERENCE);
1.78 - }
1.79 -
1.80 - public Variable setT(final int index, final int type) {
1.81 - updateRecord(localTypeRecords, index, type);
1.82 - return Variable.getLocalVariable(type, index);
1.83 - }
1.84 -
1.85 - public Variable getI(final int index) {
1.86 - return getT(index, VarType.INTEGER);
1.87 - }
1.88 -
1.89 - public Variable getL(final int index) {
1.90 - return getT(index, VarType.LONG);
1.91 - }
1.92 -
1.93 - public Variable getF(final int index) {
1.94 - return getT(index, VarType.FLOAT);
1.95 - }
1.96 -
1.97 - public Variable getD(final int index) {
1.98 - return getT(index, VarType.DOUBLE);
1.99 - }
1.100 -
1.101 - public Variable getA(final int index) {
1.102 - return getT(index, VarType.REFERENCE);
1.103 - }
1.104 -
1.105 - public Variable getT(final int index, final int type) {
1.106 - final int oldRecordValue = localTypeRecords.get(index);
1.107 - if ((oldRecordValue & 0xff) != type) {
1.108 - throw new IllegalStateException("Type mismatch");
1.109 - }
1.110 -
1.111 - return Variable.getLocalVariable(type, index);
1.112 - }
1.113 -
1.114 - private static void updateRecords(final TypeArray typeRecords,
1.115 - final TypeArray stackMapTypes) {
1.116 - final int srcSize = stackMapTypes.getSize();
1.117 - for (int i = 0, dstIndex = 0; i < srcSize; ++i) {
1.118 - final int smType = stackMapTypes.get(i);
1.119 - if (smType == RuntimeConstants.ITEM_Bogus) {
1.120 - ++dstIndex;
1.121 - continue;
1.122 - }
1.123 - final int varType = VarType.fromStackMapType(smType);
1.124 - updateRecord(typeRecords, dstIndex, varType);
1.125 - dstIndex += VarType.isCategory2(varType) ? 2 : 1;
1.126 - }
1.127 - }
1.128 -
1.129 - private static void updateRecord(final TypeArray typeRecords,
1.130 - final int index, final int type) {
1.131 - if (typeRecords.getSize() < (index + 1)) {
1.132 - typeRecords.setSize(index + 1);
1.133 - }
1.134 -
1.135 - final int oldRecordValue = typeRecords.get(index);
1.136 - final int usedTypesMask =
1.137 - (oldRecordValue >> 8) | (1 << type);
1.138 - typeRecords.set(index, (usedTypesMask << 8) | type);
1.139 - }
1.140 -
1.141 - private static Variable getVariable(final TypeArray typeRecords,
1.142 - final int index) {
1.143 - return Variable.getLocalVariable(typeRecords.get(index) & 0xff, index);
1.144 - }
1.145 -}