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