vm/src/main/java/org/apidesign/vm4brwsr/LocalsMapper.java
branchregisters
changeset 319 83f638b13242
parent 310 ec7d8bc17725
child 442 b107ed66f2e7
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/LocalsMapper.java	Wed Dec 12 14:07:53 2012 +0100
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/LocalsMapper.java	Fri Dec 14 15:06:53 2012 +0100
     1.3 @@ -1,141 +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 -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 -}
   1.145 +/**
   1.146 + * Back 2 Browser Bytecode Translator
   1.147 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
   1.148 + *
   1.149 + * This program is free software: you can redistribute it and/or modify
   1.150 + * it under the terms of the GNU General Public License as published by
   1.151 + * the Free Software Foundation, version 2 of the License.
   1.152 + *
   1.153 + * This program is distributed in the hope that it will be useful,
   1.154 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   1.155 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   1.156 + * GNU General Public License for more details.
   1.157 + *
   1.158 + * You should have received a copy of the GNU General Public License
   1.159 + * along with this program. Look for COPYING file in the top folder.
   1.160 + * If not, see http://opensource.org/licenses/GPL-2.0.
   1.161 + */
   1.162 +package org.apidesign.vm4brwsr;
   1.163 +
   1.164 +import java.io.IOException;
   1.165 +import org.apidesign.javap.RuntimeConstants;
   1.166 +import org.apidesign.javap.TypeArray;
   1.167 +
   1.168 +final class LocalsMapper {
   1.169 +    private final TypeArray argTypeRecords;
   1.170 +    private final TypeArray localTypeRecords;
   1.171 +
   1.172 +    public LocalsMapper(final TypeArray stackMapArgs) {
   1.173 +        final TypeArray initTypeRecords = new TypeArray();
   1.174 +        updateRecords(initTypeRecords, stackMapArgs);
   1.175 +
   1.176 +        argTypeRecords = initTypeRecords;
   1.177 +        localTypeRecords = new TypeArray(initTypeRecords);
   1.178 +    }
   1.179 +
   1.180 +    public void outputArguments(final Appendable out) throws IOException {
   1.181 +        final int argRecordCount = argTypeRecords.getSize();
   1.182 +        if (argRecordCount > 0) {
   1.183 +            Variable variable = getVariable(argTypeRecords, 0);
   1.184 +            out.append(variable);
   1.185 +
   1.186 +            int i = variable.isCategory2() ? 2 : 1;
   1.187 +            while (i < argRecordCount) {
   1.188 +                variable = getVariable(argTypeRecords, i);
   1.189 +                out.append(", ");
   1.190 +                out.append(variable);
   1.191 +                i += variable.isCategory2() ? 2 : 1;
   1.192 +            }
   1.193 +        }
   1.194 +    }
   1.195 +
   1.196 +    public void syncWithFrameLocals(final TypeArray frameLocals) {
   1.197 +        updateRecords(localTypeRecords, frameLocals);
   1.198 +    }
   1.199 +
   1.200 +    public Variable setI(final int index) {
   1.201 +        return setT(index, VarType.INTEGER);
   1.202 +    }
   1.203 +
   1.204 +    public Variable setL(final int index) {
   1.205 +        return setT(index, VarType.LONG);
   1.206 +    }
   1.207 +
   1.208 +    public Variable setF(final int index) {
   1.209 +        return setT(index, VarType.FLOAT);
   1.210 +    }
   1.211 +
   1.212 +    public Variable setD(final int index) {
   1.213 +        return setT(index, VarType.DOUBLE);
   1.214 +    }
   1.215 +
   1.216 +    public Variable setA(final int index) {
   1.217 +        return setT(index, VarType.REFERENCE);
   1.218 +    }
   1.219 +
   1.220 +    public Variable setT(final int index, final int type) {
   1.221 +        updateRecord(localTypeRecords, index, type);
   1.222 +        return Variable.getLocalVariable(type, index);
   1.223 +    }
   1.224 +
   1.225 +    public Variable getI(final int index) {
   1.226 +        return getT(index, VarType.INTEGER);
   1.227 +    }
   1.228 +
   1.229 +    public Variable getL(final int index) {
   1.230 +        return getT(index, VarType.LONG);
   1.231 +    }
   1.232 +
   1.233 +    public Variable getF(final int index) {
   1.234 +        return getT(index, VarType.FLOAT);
   1.235 +    }
   1.236 +
   1.237 +    public Variable getD(final int index) {
   1.238 +        return getT(index, VarType.DOUBLE);
   1.239 +    }
   1.240 +
   1.241 +    public Variable getA(final int index) {
   1.242 +        return getT(index, VarType.REFERENCE);
   1.243 +    }
   1.244 +
   1.245 +    public Variable getT(final int index, final int type) {
   1.246 +        final int oldRecordValue = localTypeRecords.get(index);
   1.247 +        if ((oldRecordValue & 0xff) != type) {
   1.248 +            throw new IllegalStateException("Type mismatch");
   1.249 +        }
   1.250 +
   1.251 +        return Variable.getLocalVariable(type, index);
   1.252 +    }
   1.253 +
   1.254 +    private static void updateRecords(final TypeArray typeRecords,
   1.255 +                                      final TypeArray stackMapTypes) {
   1.256 +        final int srcSize = stackMapTypes.getSize();
   1.257 +        for (int i = 0, dstIndex = 0; i < srcSize; ++i) {
   1.258 +            final int smType = stackMapTypes.get(i);
   1.259 +            if (smType == RuntimeConstants.ITEM_Bogus) {
   1.260 +                ++dstIndex;
   1.261 +                continue;
   1.262 +            }
   1.263 +            final int varType = VarType.fromStackMapType(smType);
   1.264 +            updateRecord(typeRecords, dstIndex, varType);
   1.265 +            dstIndex += VarType.isCategory2(varType) ? 2 : 1;
   1.266 +        }
   1.267 +    }
   1.268 +
   1.269 +    private static void updateRecord(final TypeArray typeRecords,
   1.270 +                                     final int index, final int type) {
   1.271 +        if (typeRecords.getSize() < (index + 1)) {
   1.272 +            typeRecords.setSize(index + 1);
   1.273 +        }
   1.274 +
   1.275 +        final int oldRecordValue = typeRecords.get(index);
   1.276 +        final int usedTypesMask =
   1.277 +                (oldRecordValue >> 8) | (1 << type);
   1.278 +        typeRecords.set(index, (usedTypesMask << 8) | type);
   1.279 +    }
   1.280 +
   1.281 +    private static Variable getVariable(final TypeArray typeRecords,
   1.282 +                                        final int index) {
   1.283 +        return Variable.getLocalVariable(typeRecords.get(index) & 0xff, index);
   1.284 +    }
   1.285 +}