lubomir@221: /** lubomir@221: * Back 2 Browser Bytecode Translator lubomir@221: * Copyright (C) 2012 Jaroslav Tulach lubomir@221: * lubomir@221: * This program is free software: you can redistribute it and/or modify lubomir@221: * it under the terms of the GNU General Public License as published by lubomir@221: * the Free Software Foundation, version 2 of the License. lubomir@221: * lubomir@221: * This program is distributed in the hope that it will be useful, lubomir@221: * but WITHOUT ANY WARRANTY; without even the implied warranty of lubomir@221: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the lubomir@221: * GNU General Public License for more details. lubomir@221: * lubomir@221: * You should have received a copy of the GNU General Public License lubomir@221: * along with this program. Look for COPYING file in the top folder. lubomir@221: * If not, see http://opensource.org/licenses/GPL-2.0. lubomir@221: */ lubomir@221: package org.apidesign.vm4brwsr; lubomir@221: lubomir@221: public final class StackToVariableMapper { lubomir@221: private static final String VAR_NAME_PREFIX = "stack"; lubomir@221: lubomir@221: private int stackSize; lubomir@221: private StringBuilder varNameBuilder; lubomir@221: lubomir@221: private int maxStackSize; lubomir@221: lubomir@221: public StackToVariableMapper() { lubomir@221: varNameBuilder = new StringBuilder(VAR_NAME_PREFIX); lubomir@221: } lubomir@221: lubomir@221: public void reset(final int newStackSize) { lubomir@221: stackSize = newStackSize; lubomir@221: if (maxStackSize < stackSize) { lubomir@221: maxStackSize = stackSize; lubomir@221: } lubomir@221: } lubomir@221: lubomir@221: public void push(final int numOfElements) { lubomir@221: stackSize += numOfElements; lubomir@221: if (maxStackSize < stackSize) { lubomir@221: maxStackSize = stackSize; lubomir@221: } lubomir@221: } lubomir@221: lubomir@221: public String push() { lubomir@221: push(1); lubomir@221: return get(0); lubomir@221: } lubomir@221: lubomir@221: public void pop(final int numOfElements) { lubomir@221: if (numOfElements > stackSize) { lubomir@221: throw new IllegalStateException("Stack underflow"); lubomir@221: } lubomir@221: stackSize -= numOfElements; lubomir@221: } lubomir@221: lubomir@221: public String pop() { lubomir@221: final String variableName = get(0); lubomir@221: pop(1); lubomir@221: return variableName; lubomir@221: } lubomir@221: lubomir@221: public String get(final int indexFromTop) { lubomir@221: if (indexFromTop >= stackSize) { lubomir@221: throw new IllegalStateException("Stack underflow"); lubomir@221: } lubomir@221: lubomir@221: return constructVariableName(stackSize - indexFromTop - 1); lubomir@221: } lubomir@221: lubomir@221: public String top() { lubomir@221: return get(0); lubomir@221: } lubomir@221: lubomir@221: public String bottom() { lubomir@221: if (stackSize == 0) { lubomir@221: throw new IllegalStateException("Stack underflow"); lubomir@221: } lubomir@221: lubomir@221: return constructVariableName(0); lubomir@221: } lubomir@221: lubomir@221: public int getMaxStackSize() { lubomir@221: return maxStackSize; lubomir@221: } lubomir@221: lubomir@221: public String constructVariableName(final int index) { lubomir@221: varNameBuilder.setLength(VAR_NAME_PREFIX.length()); lubomir@221: varNameBuilder.append(index); lubomir@221: return varNameBuilder.toString(); lubomir@221: } lubomir@221: }