rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Feb 2014 12:08:31 +0100
branchReducedStack
changeset 1467 5538c1eb03be
parent 1466 39d26d3686d9
child 1543 bafa63ac44a2
permissions -rw-r--r--
Correctly encode the index of the type using bit shift
     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     }
    73 
    74     void replace(Appendable out, int varType, String format, CharSequence... arr) 
    75     throws IOException {
    76         StringBuilder sb = new StringBuilder();
    77         ByteCodeToJavaScript.emitImpl(sb, format, arr);
    78         String[] values = stackValues.toArray();
    79         final int last = stackTypeIndexPairs.getSize() - 1;
    80         values[last] = sb.toString();
    81         final int value = (last << 8) | (varType & 0xff);
    82         stackTypeIndexPairs.set(last, value);
    83     }
    84     
    85     void flush(Appendable out) throws IOException {
    86         int count = stackTypeIndexPairs.getSize();
    87         for (int i = 0; i < count; i++) {
    88             String val = stackValues.getAndClear(i, true);
    89             if (val == null) {
    90                 continue;
    91             }
    92             CharSequence var = getVariable(stackTypeIndexPairs.get(i));
    93             ByteCodeToJavaScript.emitImpl(out, "var @1 = @2;", var, val);
    94         }
    95     }
    96     
    97     public CharSequence popI() {
    98         return popT(VarType.INTEGER);
    99     }
   100 
   101     public CharSequence popL() {
   102         return popT(VarType.LONG);
   103     }
   104 
   105     public CharSequence popF() {
   106         return popT(VarType.FLOAT);
   107     }
   108 
   109     public CharSequence popD() {
   110         return popT(VarType.DOUBLE);
   111     }
   112 
   113     public CharSequence popA() {
   114         return popT(VarType.REFERENCE);
   115     }
   116 
   117     public CharSequence popT(final int type) {
   118         final CharSequence variable = getT(0, type);
   119         popImpl(1);
   120         return variable;
   121     }
   122 
   123     public CharSequence popValue() {
   124         final CharSequence variable = getT(0, -1);
   125         popImpl(1);
   126         return variable;
   127     }
   128     public Variable pop(Appendable out) throws IOException {
   129         flush(out);
   130         final Variable variable = get(0);
   131         popImpl(1);
   132         return variable;
   133     }
   134 
   135     public void pop(final int count) {
   136         final int stackSize = stackTypeIndexPairs.getSize();
   137         if (count > stackSize) {
   138             throw new IllegalStateException("Stack underflow");
   139         }
   140         popImpl(count);
   141     }
   142 
   143     public CharSequence getI(final int indexFromTop) {
   144         return getT(indexFromTop, VarType.INTEGER);
   145     }
   146 
   147     public CharSequence getL(final int indexFromTop) {
   148         return getT(indexFromTop, VarType.LONG);
   149     }
   150 
   151     public CharSequence getF(final int indexFromTop) {
   152         return getT(indexFromTop, VarType.FLOAT);
   153     }
   154 
   155     public CharSequence getD(final int indexFromTop) {
   156         return getT(indexFromTop, VarType.DOUBLE);
   157     }
   158 
   159     public CharSequence getA(final int indexFromTop) {
   160         return getT(indexFromTop, VarType.REFERENCE);
   161     }
   162 
   163     public CharSequence getT(final int indexFromTop, final int type) {
   164         return getT(indexFromTop, type, true);
   165     }
   166     public CharSequence getT(final int indexFromTop, final int type, boolean clear) {
   167         final int stackSize = stackTypeIndexPairs.getSize();
   168         if (indexFromTop >= stackSize) {
   169             throw new IllegalStateException("Stack underflow");
   170         }
   171         final int stackValue =
   172                 stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
   173         if (type != -1 && (stackValue & 0xff) != type) {
   174             throw new IllegalStateException("Type mismatch");
   175         }
   176         String value =
   177             stackValues.getAndClear(stackSize - indexFromTop - 1, clear);
   178         if (value != null) {
   179             return value;
   180         }
   181         return getVariable(stackValue);
   182     }
   183 
   184     public Variable get(final int indexFromTop) {
   185         final int stackSize = stackTypeIndexPairs.getSize();
   186         if (indexFromTop >= stackSize) {
   187             throw new IllegalStateException("Stack underflow");
   188         }
   189         final int stackValue =
   190                 stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
   191 
   192         return getVariable(stackValue);
   193     }
   194 
   195     private int pushTypeImpl(final int type) {
   196         final int count = stackTypeIndexPairs.getSize();
   197         final int value = (count << 8) | (type & 0xff);
   198         stackTypeIndexPairs.add(value);
   199         
   200         addStackValue(count, null);
   201         return value;
   202     }
   203 
   204     private void pushTypeAndValue(final int type, CharSequence v) {
   205         final int count = stackTypeIndexPairs.getSize();
   206         final int value = (count << 8) | (type & 0xff);
   207         stackTypeIndexPairs.add(value);
   208         final String val = v.toString();
   209         addStackValue(count, val);
   210     }
   211 
   212     private void addStackValue(int at, final String val) {
   213         final String[] arr = stackValues.toArray();
   214         if (arr.length > at) {
   215             arr[at] = val;
   216         } else {
   217             stackValues.add(val);
   218         }
   219     }
   220 
   221     private void popImpl(final int count) {
   222         final int stackSize = stackTypeIndexPairs.getSize();
   223         stackTypeIndexPairs.setSize(stackSize - count);
   224     }
   225 
   226     public Variable getVariable(final int typeAndIndex) {
   227         final int type = typeAndIndex & 0xff;
   228         final int index = typeAndIndex >> 8;
   229 
   230         return Variable.getStackVariable(type, index);
   231     }
   232 }