001 /*
002 * This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 * This file is licensed to You under the Eclipse Public License (EPL);
005 * You may not use this file except in compliance with the License. You
006 * may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 * See the COPYRIGHT.txt file distributed with this work for information
011 * regarding copyright ownership.
012 */
013 package org.jikesrvm.osr;
014
015 import org.jikesrvm.VM;
016
017 /**
018 * Utility class used by BytecodeTraverser.
019 */
020 class TypeStack {
021 private final byte[] stack;
022 private int top;
023 private final byte defv;
024
025 public TypeStack(int depth, byte defv) {
026 byte[] stk = new byte[depth];
027 for (int i = 0; i < depth; i++) {
028 stk[i] = defv;
029 }
030
031 this.stack = stk;
032 this.top = 0;
033 this.defv = defv;
034 }
035
036 public TypeStack(TypeStack other) {
037
038 int ssize = other.stack.length;
039 this.stack = new byte[ssize];
040 System.arraycopy(other.stack, 0, this.stack, 0, ssize);
041 this.top = other.top;
042 this.defv = other.defv;
043 }
044
045 public void push(byte v) {
046 if (top == stack.length) {
047 VM.sysWrite("TypeStack.push(B) : overflow!\n");
048 }
049 stack[top++] = v;
050 }
051
052 public byte pop() {
053 if (top <= 0) {
054 VM.sysWrite("TypeStack.pop() : underflow!\n");
055 }
056 top--;
057 byte v = stack[top];
058 stack[top] = defv;
059
060 return v;
061 }
062
063 public void pop(int n) {
064 int newtop = top - n;
065
066 if (newtop < 0) {
067 VM.sysWrite("TypeStack.pop(I) : underflow!\n");
068 }
069
070 for (int i = top - 1; i >= newtop; i--) {
071 stack[i] = defv;
072 }
073
074 top = newtop;
075 }
076
077 public byte peek() {
078 return stack[top - 1];
079 }
080
081 public byte[] snapshot() {
082 return stack;
083 }
084
085 public void clear() {
086 top = 0;
087 for (int i = 0, n = stack.length; i < n; i++) {
088 stack[i] = defv;
089 }
090 }
091
092 public int depth() {
093 return top;
094 }
095 }