vm/src/main/java/org/apidesign/vm4brwsr/StackToVariableMapper.java
branchregisters
changeset 221 3ee23267706c
child 281 f2352e0b713e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/StackToVariableMapper.java	Thu Nov 29 20:19:00 2012 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.vm4brwsr;
    1.22 +
    1.23 +public final class StackToVariableMapper {
    1.24 +    private static final String VAR_NAME_PREFIX = "stack";
    1.25 +
    1.26 +    private int stackSize;
    1.27 +    private StringBuilder varNameBuilder;
    1.28 +
    1.29 +    private int maxStackSize;
    1.30 +
    1.31 +    public StackToVariableMapper() {
    1.32 +        varNameBuilder = new StringBuilder(VAR_NAME_PREFIX);
    1.33 +    }
    1.34 +
    1.35 +    public void reset(final int newStackSize) {
    1.36 +        stackSize = newStackSize;
    1.37 +        if (maxStackSize < stackSize) {
    1.38 +            maxStackSize = stackSize;
    1.39 +        }
    1.40 +    }
    1.41 +
    1.42 +    public void push(final int numOfElements) {
    1.43 +        stackSize += numOfElements;
    1.44 +        if (maxStackSize < stackSize) {
    1.45 +            maxStackSize = stackSize;
    1.46 +        }
    1.47 +    }
    1.48 +
    1.49 +    public String push() {
    1.50 +        push(1);
    1.51 +        return get(0);
    1.52 +    }
    1.53 +
    1.54 +    public void pop(final int numOfElements) {
    1.55 +        if (numOfElements > stackSize) {
    1.56 +            throw new IllegalStateException("Stack underflow");
    1.57 +        }
    1.58 +        stackSize -= numOfElements;
    1.59 +    }
    1.60 +
    1.61 +    public String pop() {
    1.62 +        final String variableName = get(0);
    1.63 +        pop(1);
    1.64 +        return variableName;
    1.65 +    }
    1.66 +
    1.67 +    public String get(final int indexFromTop) {
    1.68 +        if (indexFromTop >= stackSize) {
    1.69 +            throw new IllegalStateException("Stack underflow");
    1.70 +        }
    1.71 +
    1.72 +        return constructVariableName(stackSize - indexFromTop - 1);
    1.73 +    }
    1.74 +
    1.75 +    public String top() {
    1.76 +        return get(0);
    1.77 +    }
    1.78 +
    1.79 +    public String bottom() {
    1.80 +        if (stackSize == 0) {
    1.81 +            throw new IllegalStateException("Stack underflow");
    1.82 +        }
    1.83 +
    1.84 +        return constructVariableName(0);
    1.85 +    }
    1.86 +
    1.87 +    public int getMaxStackSize() {
    1.88 +        return maxStackSize;
    1.89 +    }
    1.90 +
    1.91 +    public String constructVariableName(final int index) {
    1.92 +        varNameBuilder.setLength(VAR_NAME_PREFIX.length());
    1.93 +        varNameBuilder.append(index);
    1.94 +        return varNameBuilder.toString();
    1.95 +    }
    1.96 +}