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