rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 15 Feb 2014 14:36:43 +0100
branchReducedStack
changeset 1453 e046cfcb8f00
parent 810 9eb750594b15
child 1454 ca83b2adebb5
permissions -rw-r--r--
Centralizing the stack pushes into single assign method
lubomir@221
     1
/**
lubomir@221
     2
 * Back 2 Browser Bytecode Translator
lubomir@221
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
lubomir@221
     4
 *
lubomir@221
     5
 * This program is free software: you can redistribute it and/or modify
lubomir@221
     6
 * it under the terms of the GNU General Public License as published by
lubomir@221
     7
 * the Free Software Foundation, version 2 of the License.
lubomir@221
     8
 *
lubomir@221
     9
 * This program is distributed in the hope that it will be useful,
lubomir@221
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
lubomir@221
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
lubomir@221
    12
 * GNU General Public License for more details.
lubomir@221
    13
 *
lubomir@221
    14
 * You should have received a copy of the GNU General Public License
lubomir@221
    15
 * along with this program. Look for COPYING file in the top folder.
lubomir@221
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
lubomir@221
    17
 */
lubomir@221
    18
package org.apidesign.vm4brwsr;
lubomir@221
    19
jaroslav@1453
    20
import java.io.IOException;
jaroslav@810
    21
import org.apidesign.vm4brwsr.ByteCodeParser.TypeArray;
lubomir@281
    22
lubomir@310
    23
final class StackMapper {
lubomir@281
    24
    private final TypeArray stackTypeIndexPairs;
lubomir@281
    25
    private int[] typeCounters;
lubomir@281
    26
    private int[] typeMaxCounters;
lubomir@221
    27
lubomir@307
    28
    public StackMapper() {
lubomir@281
    29
        stackTypeIndexPairs = new TypeArray();
lubomir@307
    30
        typeCounters = new int[VarType.LAST + 1];
lubomir@307
    31
        typeMaxCounters = new int[VarType.LAST + 1];
lubomir@221
    32
    }
lubomir@221
    33
lubomir@281
    34
    public void clear() {
lubomir@307
    35
        for (int type = 0; type <= VarType.LAST; ++type) {
lubomir@281
    36
            typeCounters[type] = 0;
lubomir@281
    37
        }
lubomir@281
    38
        stackTypeIndexPairs.clear();
lubomir@281
    39
    }
lubomir@281
    40
lubomir@281
    41
    public void syncWithFrameStack(final TypeArray frameStack) {
lubomir@281
    42
        clear();
lubomir@281
    43
lubomir@281
    44
        final int size = frameStack.getSize();
lubomir@281
    45
        for (int i = 0; i < size; ++i) {
lubomir@307
    46
            pushTypeImpl(VarType.fromStackMapType(frameStack.get(i)));
lubomir@221
    47
        }
lubomir@221
    48
    }
lubomir@221
    49
lubomir@281
    50
    public Variable pushI() {
lubomir@307
    51
        return pushT(VarType.INTEGER);
lubomir@281
    52
    }
lubomir@281
    53
lubomir@281
    54
    public Variable pushL() {
lubomir@307
    55
        return pushT(VarType.LONG);
lubomir@281
    56
    }
lubomir@281
    57
lubomir@281
    58
    public Variable pushF() {
lubomir@307
    59
        return pushT(VarType.FLOAT);
lubomir@281
    60
    }
lubomir@281
    61
lubomir@281
    62
    public Variable pushD() {
lubomir@307
    63
        return pushT(VarType.DOUBLE);
lubomir@281
    64
    }
lubomir@281
    65
lubomir@281
    66
    public Variable pushA() {
lubomir@307
    67
        return pushT(VarType.REFERENCE);
lubomir@281
    68
    }
lubomir@281
    69
lubomir@281
    70
    public Variable pushT(final int type) {
lubomir@281
    71
        return getVariable(pushTypeImpl(type));
lubomir@281
    72
    }
lubomir@281
    73
jaroslav@1453
    74
    void assign(Appendable out, int varType, CharSequence s) throws IOException {
jaroslav@1453
    75
        ByteCodeToJavaScript.emit(out, "var @1 = @2;", pushT(varType), s);
jaroslav@1453
    76
    }
jaroslav@1453
    77
    
lubomir@281
    78
    public Variable popI() {
lubomir@307
    79
        return popT(VarType.INTEGER);
lubomir@281
    80
    }
lubomir@281
    81
lubomir@281
    82
    public Variable popL() {
lubomir@307
    83
        return popT(VarType.LONG);
lubomir@281
    84
    }
lubomir@281
    85
lubomir@281
    86
    public Variable popF() {
lubomir@307
    87
        return popT(VarType.FLOAT);
lubomir@281
    88
    }
lubomir@281
    89
lubomir@281
    90
    public Variable popD() {
lubomir@307
    91
        return popT(VarType.DOUBLE);
lubomir@281
    92
    }
lubomir@281
    93
lubomir@281
    94
    public Variable popA() {
lubomir@307
    95
        return popT(VarType.REFERENCE);
lubomir@281
    96
    }
lubomir@281
    97
lubomir@281
    98
    public Variable popT(final int type) {
lubomir@281
    99
        final Variable variable = getT(0, type);
lubomir@281
   100
        popImpl(1);
lubomir@281
   101
        return variable;
lubomir@281
   102
    }
lubomir@281
   103
lubomir@281
   104
    public Variable pop() {
lubomir@281
   105
        final Variable variable = get(0);
lubomir@281
   106
        popImpl(1);
lubomir@281
   107
        return variable;
lubomir@281
   108
    }
lubomir@281
   109
lubomir@281
   110
    public void pop(final int count) {
lubomir@281
   111
        final int stackSize = stackTypeIndexPairs.getSize();
lubomir@281
   112
        if (count > stackSize) {
lubomir@281
   113
            throw new IllegalStateException("Stack underflow");
lubomir@281
   114
        }
lubomir@281
   115
        popImpl(count);
lubomir@281
   116
    }
lubomir@281
   117
lubomir@281
   118
    public Variable getI(final int indexFromTop) {
lubomir@307
   119
        return getT(indexFromTop, VarType.INTEGER);
lubomir@281
   120
    }
lubomir@281
   121
lubomir@281
   122
    public Variable getL(final int indexFromTop) {
lubomir@307
   123
        return getT(indexFromTop, VarType.LONG);
lubomir@281
   124
    }
lubomir@281
   125
lubomir@281
   126
    public Variable getF(final int indexFromTop) {
lubomir@307
   127
        return getT(indexFromTop, VarType.FLOAT);
lubomir@281
   128
    }
lubomir@281
   129
lubomir@281
   130
    public Variable getD(final int indexFromTop) {
lubomir@307
   131
        return getT(indexFromTop, VarType.DOUBLE);
lubomir@281
   132
    }
lubomir@281
   133
lubomir@281
   134
    public Variable getA(final int indexFromTop) {
lubomir@307
   135
        return getT(indexFromTop, VarType.REFERENCE);
lubomir@281
   136
    }
lubomir@281
   137
lubomir@281
   138
    public Variable getT(final int indexFromTop, final int type) {
lubomir@281
   139
        final int stackSize = stackTypeIndexPairs.getSize();
lubomir@281
   140
        if (indexFromTop >= stackSize) {
lubomir@281
   141
            throw new IllegalStateException("Stack underflow");
lubomir@281
   142
        }
lubomir@281
   143
        final int stackValue =
lubomir@281
   144
                stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
lubomir@281
   145
        if ((stackValue & 0xff) != type) {
lubomir@281
   146
            throw new IllegalStateException("Type mismatch");
lubomir@281
   147
        }
lubomir@281
   148
lubomir@281
   149
        return getVariable(stackValue);
lubomir@281
   150
    }
lubomir@281
   151
lubomir@281
   152
    public Variable get(final int indexFromTop) {
lubomir@281
   153
        final int stackSize = stackTypeIndexPairs.getSize();
lubomir@281
   154
        if (indexFromTop >= stackSize) {
lubomir@281
   155
            throw new IllegalStateException("Stack underflow");
lubomir@281
   156
        }
lubomir@281
   157
        final int stackValue =
lubomir@281
   158
                stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
lubomir@281
   159
lubomir@281
   160
        return getVariable(stackValue);
lubomir@281
   161
    }
lubomir@281
   162
lubomir@281
   163
    private int pushTypeImpl(final int type) {
lubomir@281
   164
        final int count = typeCounters[type];
lubomir@281
   165
        final int value = (count << 8) | (type & 0xff);
lubomir@281
   166
        incCounter(type);
lubomir@281
   167
        stackTypeIndexPairs.add(value);
lubomir@281
   168
lubomir@281
   169
        return value;
lubomir@281
   170
    }
lubomir@281
   171
lubomir@281
   172
    private void popImpl(final int count) {
lubomir@281
   173
        final int stackSize = stackTypeIndexPairs.getSize();
lubomir@281
   174
        for (int i = stackSize - count; i < stackSize; ++i) {
lubomir@281
   175
            final int value = stackTypeIndexPairs.get(i);
lubomir@281
   176
            decCounter(value & 0xff);
lubomir@281
   177
        }
lubomir@281
   178
lubomir@281
   179
        stackTypeIndexPairs.setSize(stackSize - count);
lubomir@281
   180
    }
lubomir@281
   181
lubomir@281
   182
    private void incCounter(final int type) {
lubomir@281
   183
        final int newValue = ++typeCounters[type];
lubomir@281
   184
        if (typeMaxCounters[type] < newValue) {
lubomir@281
   185
            typeMaxCounters[type] = newValue;
lubomir@221
   186
        }
lubomir@221
   187
    }
lubomir@221
   188
lubomir@281
   189
    private void decCounter(final int type) {
lubomir@281
   190
        --typeCounters[type];
lubomir@221
   191
    }
lubomir@221
   192
lubomir@281
   193
    public Variable getVariable(final int typeAndIndex) {
lubomir@281
   194
        final int type = typeAndIndex & 0xff;
lubomir@281
   195
        final int index = typeAndIndex >> 8;
lubomir@221
   196
lubomir@281
   197
        return Variable.getStackVariable(type, index);
lubomir@221
   198
    }
lubomir@221
   199
}