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
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
lubomir@221
    20
public final class StackToVariableMapper {
lubomir@221
    21
    private static final String VAR_NAME_PREFIX = "stack";
lubomir@221
    22
lubomir@221
    23
    private int stackSize;
lubomir@221
    24
    private StringBuilder varNameBuilder;
lubomir@221
    25
lubomir@221
    26
    private int maxStackSize;
lubomir@221
    27
lubomir@221
    28
    public StackToVariableMapper() {
lubomir@221
    29
        varNameBuilder = new StringBuilder(VAR_NAME_PREFIX);
lubomir@221
    30
    }
lubomir@221
    31
lubomir@221
    32
    public void reset(final int newStackSize) {
lubomir@221
    33
        stackSize = newStackSize;
lubomir@221
    34
        if (maxStackSize < stackSize) {
lubomir@221
    35
            maxStackSize = stackSize;
lubomir@221
    36
        }
lubomir@221
    37
    }
lubomir@221
    38
lubomir@221
    39
    public void push(final int numOfElements) {
lubomir@221
    40
        stackSize += numOfElements;
lubomir@221
    41
        if (maxStackSize < stackSize) {
lubomir@221
    42
            maxStackSize = stackSize;
lubomir@221
    43
        }
lubomir@221
    44
    }
lubomir@221
    45
lubomir@221
    46
    public String push() {
lubomir@221
    47
        push(1);
lubomir@221
    48
        return get(0);
lubomir@221
    49
    }
lubomir@221
    50
lubomir@221
    51
    public void pop(final int numOfElements) {
lubomir@221
    52
        if (numOfElements > stackSize) {
lubomir@221
    53
            throw new IllegalStateException("Stack underflow");
lubomir@221
    54
        }
lubomir@221
    55
        stackSize -= numOfElements;
lubomir@221
    56
    }
lubomir@221
    57
lubomir@221
    58
    public String pop() {
lubomir@221
    59
        final String variableName = get(0);
lubomir@221
    60
        pop(1);
lubomir@221
    61
        return variableName;
lubomir@221
    62
    }
lubomir@221
    63
lubomir@221
    64
    public String get(final int indexFromTop) {
lubomir@221
    65
        if (indexFromTop >= stackSize) {
lubomir@221
    66
            throw new IllegalStateException("Stack underflow");
lubomir@221
    67
        }
lubomir@221
    68
lubomir@221
    69
        return constructVariableName(stackSize - indexFromTop - 1);
lubomir@221
    70
    }
lubomir@221
    71
lubomir@221
    72
    public String top() {
lubomir@221
    73
        return get(0);
lubomir@221
    74
    }
lubomir@221
    75
lubomir@221
    76
    public String bottom() {
lubomir@221
    77
        if (stackSize == 0) {
lubomir@221
    78
            throw new IllegalStateException("Stack underflow");
lubomir@221
    79
        }
lubomir@221
    80
lubomir@221
    81
        return constructVariableName(0);
lubomir@221
    82
    }
lubomir@221
    83
lubomir@221
    84
    public int getMaxStackSize() {
lubomir@221
    85
        return maxStackSize;
lubomir@221
    86
    }
lubomir@221
    87
lubomir@221
    88
    public String constructVariableName(final int index) {
lubomir@221
    89
        varNameBuilder.setLength(VAR_NAME_PREFIX.length());
lubomir@221
    90
        varNameBuilder.append(index);
lubomir@221
    91
        return varNameBuilder.toString();
lubomir@221
    92
    }
lubomir@221
    93
}