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      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      TypeStack(TypeStack other) {
037        int ssize = other.stack.length;
038        this.stack = new byte[ssize];
039        System.arraycopy(other.stack, 0, this.stack, 0, ssize);
040        this.top = other.top;
041        this.defv = other.defv;
042      }
043    
044      void push(byte v) {
045        if (top == stack.length) {
046          VM.sysWrite("TypeStack.push(B) : overflow!\n");
047        }
048        stack[top++] = v;
049      }
050    
051      byte pop() {
052        if (top <= 0) {
053          VM.sysWrite("TypeStack.pop() : underflow!\n");
054        }
055        top--;
056        byte v = stack[top];
057        stack[top] = defv;
058    
059        return v;
060      }
061    
062      void pop(int n) {
063        int newtop = top - n;
064    
065        if (newtop < 0) {
066          VM.sysWrite("TypeStack.pop(I) : underflow!\n");
067        }
068    
069        for (int i = top - 1; i >= newtop; i--) {
070          stack[i] = defv;
071        }
072    
073        top = newtop;
074      }
075    
076      byte peek() {
077        return stack[top - 1];
078      }
079    
080      byte[] snapshot() {
081        return stack;
082      }
083    
084      void clear() {
085        top = 0;
086        for (int i = 0, n = stack.length; i < n; i++) {
087          stack[i] = defv;
088        }
089      }
090    
091      int depth() {
092        return top;
093      }
094    }