vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java
branchregisters
changeset 307 eaf4e8387065
parent 281 f2352e0b713e
child 310 ec7d8bc17725
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java	Wed Dec 12 11:04:02 2012 +0100
     1.3 @@ -0,0 +1,194 @@
     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 +public final class StackMapper {
    1.26 +    private final TypeArray stackTypeIndexPairs;
    1.27 +    private int[] typeCounters;
    1.28 +    private int[] typeMaxCounters;
    1.29 +
    1.30 +    public StackMapper() {
    1.31 +        stackTypeIndexPairs = new TypeArray();
    1.32 +        typeCounters = new int[VarType.LAST + 1];
    1.33 +        typeMaxCounters = new int[VarType.LAST + 1];
    1.34 +    }
    1.35 +
    1.36 +    public void clear() {
    1.37 +        for (int type = 0; type <= VarType.LAST; ++type) {
    1.38 +            typeCounters[type] = 0;
    1.39 +        }
    1.40 +        stackTypeIndexPairs.clear();
    1.41 +    }
    1.42 +
    1.43 +    public void syncWithFrameStack(final TypeArray frameStack) {
    1.44 +        clear();
    1.45 +
    1.46 +        final int size = frameStack.getSize();
    1.47 +        for (int i = 0; i < size; ++i) {
    1.48 +            pushTypeImpl(VarType.fromStackMapType(frameStack.get(i)));
    1.49 +        }
    1.50 +    }
    1.51 +
    1.52 +    public Variable pushI() {
    1.53 +        return pushT(VarType.INTEGER);
    1.54 +    }
    1.55 +
    1.56 +    public Variable pushL() {
    1.57 +        return pushT(VarType.LONG);
    1.58 +    }
    1.59 +
    1.60 +    public Variable pushF() {
    1.61 +        return pushT(VarType.FLOAT);
    1.62 +    }
    1.63 +
    1.64 +    public Variable pushD() {
    1.65 +        return pushT(VarType.DOUBLE);
    1.66 +    }
    1.67 +
    1.68 +    public Variable pushA() {
    1.69 +        return pushT(VarType.REFERENCE);
    1.70 +    }
    1.71 +
    1.72 +    public Variable pushT(final int type) {
    1.73 +        return getVariable(pushTypeImpl(type));
    1.74 +    }
    1.75 +
    1.76 +    public Variable popI() {
    1.77 +        return popT(VarType.INTEGER);
    1.78 +    }
    1.79 +
    1.80 +    public Variable popL() {
    1.81 +        return popT(VarType.LONG);
    1.82 +    }
    1.83 +
    1.84 +    public Variable popF() {
    1.85 +        return popT(VarType.FLOAT);
    1.86 +    }
    1.87 +
    1.88 +    public Variable popD() {
    1.89 +        return popT(VarType.DOUBLE);
    1.90 +    }
    1.91 +
    1.92 +    public Variable popA() {
    1.93 +        return popT(VarType.REFERENCE);
    1.94 +    }
    1.95 +
    1.96 +    public Variable popT(final int type) {
    1.97 +        final Variable variable = getT(0, type);
    1.98 +        popImpl(1);
    1.99 +        return variable;
   1.100 +    }
   1.101 +
   1.102 +    public Variable pop() {
   1.103 +        final Variable variable = get(0);
   1.104 +        popImpl(1);
   1.105 +        return variable;
   1.106 +    }
   1.107 +
   1.108 +    public void pop(final int count) {
   1.109 +        final int stackSize = stackTypeIndexPairs.getSize();
   1.110 +        if (count > stackSize) {
   1.111 +            throw new IllegalStateException("Stack underflow");
   1.112 +        }
   1.113 +        popImpl(count);
   1.114 +    }
   1.115 +
   1.116 +    public Variable getI(final int indexFromTop) {
   1.117 +        return getT(indexFromTop, VarType.INTEGER);
   1.118 +    }
   1.119 +
   1.120 +    public Variable getL(final int indexFromTop) {
   1.121 +        return getT(indexFromTop, VarType.LONG);
   1.122 +    }
   1.123 +
   1.124 +    public Variable getF(final int indexFromTop) {
   1.125 +        return getT(indexFromTop, VarType.FLOAT);
   1.126 +    }
   1.127 +
   1.128 +    public Variable getD(final int indexFromTop) {
   1.129 +        return getT(indexFromTop, VarType.DOUBLE);
   1.130 +    }
   1.131 +
   1.132 +    public Variable getA(final int indexFromTop) {
   1.133 +        return getT(indexFromTop, VarType.REFERENCE);
   1.134 +    }
   1.135 +
   1.136 +    public Variable getT(final int indexFromTop, final int type) {
   1.137 +        final int stackSize = stackTypeIndexPairs.getSize();
   1.138 +        if (indexFromTop >= stackSize) {
   1.139 +            throw new IllegalStateException("Stack underflow");
   1.140 +        }
   1.141 +        final int stackValue =
   1.142 +                stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
   1.143 +        if ((stackValue & 0xff) != type) {
   1.144 +            throw new IllegalStateException("Type mismatch");
   1.145 +        }
   1.146 +
   1.147 +        return getVariable(stackValue);
   1.148 +    }
   1.149 +
   1.150 +    public Variable get(final int indexFromTop) {
   1.151 +        final int stackSize = stackTypeIndexPairs.getSize();
   1.152 +        if (indexFromTop >= stackSize) {
   1.153 +            throw new IllegalStateException("Stack underflow");
   1.154 +        }
   1.155 +        final int stackValue =
   1.156 +                stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
   1.157 +
   1.158 +        return getVariable(stackValue);
   1.159 +    }
   1.160 +
   1.161 +    private int pushTypeImpl(final int type) {
   1.162 +        final int count = typeCounters[type];
   1.163 +        final int value = (count << 8) | (type & 0xff);
   1.164 +        incCounter(type);
   1.165 +        stackTypeIndexPairs.add(value);
   1.166 +
   1.167 +        return value;
   1.168 +    }
   1.169 +
   1.170 +    private void popImpl(final int count) {
   1.171 +        final int stackSize = stackTypeIndexPairs.getSize();
   1.172 +        for (int i = stackSize - count; i < stackSize; ++i) {
   1.173 +            final int value = stackTypeIndexPairs.get(i);
   1.174 +            decCounter(value & 0xff);
   1.175 +        }
   1.176 +
   1.177 +        stackTypeIndexPairs.setSize(stackSize - count);
   1.178 +    }
   1.179 +
   1.180 +    private void incCounter(final int type) {
   1.181 +        final int newValue = ++typeCounters[type];
   1.182 +        if (typeMaxCounters[type] < newValue) {
   1.183 +            typeMaxCounters[type] = newValue;
   1.184 +        }
   1.185 +    }
   1.186 +
   1.187 +    private void decCounter(final int type) {
   1.188 +        --typeCounters[type];
   1.189 +    }
   1.190 +
   1.191 +    public Variable getVariable(final int typeAndIndex) {
   1.192 +        final int type = typeAndIndex & 0xff;
   1.193 +        final int index = typeAndIndex >> 8;
   1.194 +
   1.195 +        return Variable.getStackVariable(type, index);
   1.196 +    }
   1.197 +}