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.compilers.opt.bc2ir; 014 015 import org.jikesrvm.VM; 016 import org.jikesrvm.compilers.opt.ir.operand.Operand; 017 018 /** 019 * Simulated Operand Stack 020 */ 021 final class OperandStack { 022 023 private final Operand[] stack; 024 private int top; 025 026 OperandStack(int size) { 027 stack = new Operand[size]; 028 top = 0; 029 } 030 031 OperandStack copy() { 032 OperandStack newss = new OperandStack(stack.length); 033 newss.top = top; 034 for (int i = 0; i < top; i++) { 035 // deep copy of stack 036 newss.stack[i] = stack[i].copy(); 037 } 038 return newss; 039 } 040 041 void clear() { 042 top = 0; 043 } 044 045 void push(Operand val) { 046 // if (VM.VerifyAssertions) VM._assert(val.instruction == null); 047 stack[top++] = val; 048 } 049 050 Operand pop() { 051 return stack[--top]; 052 } 053 054 Operand peek(int depth) { 055 return stack[top - depth - 1]; 056 } 057 058 Operand peekAt(int pos) { 059 return stack[pos]; 060 } 061 062 void pop2() { 063 pop(); 064 pop(); 065 } 066 067 void swap() { 068 Operand v1 = pop(); 069 Operand v2 = pop(); 070 push(v1); 071 push(v2); 072 } 073 074 boolean isEmpty() { 075 return (top == 0); 076 } 077 078 int getSize() { 079 return top; 080 } 081 082 int getCapacity() { 083 return stack.length; 084 } 085 086 Operand getFromTop(int n) { 087 return stack[top - n - 1]; 088 } 089 090 void replaceFromTop(int n, Operand op) { 091 if (VM.VerifyAssertions) VM._assert(op.instruction == null); 092 stack[top - n - 1] = op; 093 } 094 }