vm/src/main/java/org/apidesign/vm4brwsr/StackToVariableMapper.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Thu, 29 Nov 2012 20:19:00 +0100
branchregisters
changeset 221 3ee23267706c
child 281 f2352e0b713e
permissions -rw-r--r--
Register based VM
     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 public final class StackToVariableMapper {
    21     private static final String VAR_NAME_PREFIX = "stack";
    22 
    23     private int stackSize;
    24     private StringBuilder varNameBuilder;
    25 
    26     private int maxStackSize;
    27 
    28     public StackToVariableMapper() {
    29         varNameBuilder = new StringBuilder(VAR_NAME_PREFIX);
    30     }
    31 
    32     public void reset(final int newStackSize) {
    33         stackSize = newStackSize;
    34         if (maxStackSize < stackSize) {
    35             maxStackSize = stackSize;
    36         }
    37     }
    38 
    39     public void push(final int numOfElements) {
    40         stackSize += numOfElements;
    41         if (maxStackSize < stackSize) {
    42             maxStackSize = stackSize;
    43         }
    44     }
    45 
    46     public String push() {
    47         push(1);
    48         return get(0);
    49     }
    50 
    51     public void pop(final int numOfElements) {
    52         if (numOfElements > stackSize) {
    53             throw new IllegalStateException("Stack underflow");
    54         }
    55         stackSize -= numOfElements;
    56     }
    57 
    58     public String pop() {
    59         final String variableName = get(0);
    60         pop(1);
    61         return variableName;
    62     }
    63 
    64     public String get(final int indexFromTop) {
    65         if (indexFromTop >= stackSize) {
    66             throw new IllegalStateException("Stack underflow");
    67         }
    68 
    69         return constructVariableName(stackSize - indexFromTop - 1);
    70     }
    71 
    72     public String top() {
    73         return get(0);
    74     }
    75 
    76     public String bottom() {
    77         if (stackSize == 0) {
    78             throw new IllegalStateException("Stack underflow");
    79         }
    80 
    81         return constructVariableName(0);
    82     }
    83 
    84     public int getMaxStackSize() {
    85         return maxStackSize;
    86     }
    87 
    88     public String constructVariableName(final int index) {
    89         varNameBuilder.setLength(VAR_NAME_PREFIX.length());
    90         varNameBuilder.append(index);
    91         return varNameBuilder.toString();
    92     }
    93 }