View Javadoc

1   /*
2    * $Id: RoutineContext.java,v 1.6 2005/11/03 00:52:47 weiju Exp $
3    * 
4    * Created on 03.10.2005
5    * Copyright 2005 by Wei-ju Wu
6    *
7    * This file is part of The Z-machine Preservation Project (ZMPP).
8    *
9    * ZMPP is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   *
14   * ZMPP is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with ZMPP; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22   */
23  package org.zmpp.vm;
24  
25  /***
26   * This class holds information about a subroutine.
27   * 
28   * @author Wei-ju Wu
29   * @version 1.0
30   */
31  public class RoutineContext {
32  
33    /***
34     * The start address of the routine's code.
35     */
36    private int startAddress;
37    
38    /***
39     * The local variables.
40     */
41    private short[] locals;
42    
43    /***
44     * The return address.
45     */
46    private int returnAddress;
47    
48    /***
49     * The return variable number to store the return value to.
50     */
51    private int returnVarNum;
52    
53    /***
54     * The stack pointer at invocation time.
55     */
56    private int invocationStackPointer;
57    
58    /***
59     * The number of arguments.
60     */
61    private int numArgs;
62    
63    /***
64     * Constructor.
65     * 
66     * @param startAddress the routine's start address
67     * @param numLocalVariables the number of local variables
68     */
69    public RoutineContext(int startAddress, int numLocalVariables) {
70      
71      this.startAddress = startAddress;
72      locals = new short[numLocalVariables];
73    }
74    
75    /***
76     * Sets the number of arguments.
77     * 
78     * @param numArgs the number of arguments
79     */
80    public void setNumArguments(int numArgs) {
81      
82      this.numArgs = numArgs;
83    }
84    
85    /***
86     * Returns the number of arguments.
87     * 
88     * @return the number of arguments
89     */
90    public int getNumArguments() {
91      
92      return numArgs;
93    }
94    
95    /***
96     * Returns the number of local variables.
97     * 
98     * @return the number of local variables
99     */
100   public int getNumLocalVariables() {
101     
102     return (locals != null) ? locals.length : 0;
103   }
104   
105   /***
106    * Sets a value to the specified local variable.
107    *  
108    * @param localNum the local variable number, starting with 0
109    * @param value the value
110    */
111   public void setLocalVariable(int localNum, short value) {
112     
113     locals[localNum] = value;
114   }
115 
116   /***
117    * Retrieves the value of the specified local variable.
118    * 
119    * @param localNum the local variable number, starting at 0
120    * @return the value of the specified variable
121    */
122   public short getLocalVariable(int localNum) {
123     
124     return locals[localNum];
125   }
126   
127   /***
128    * Returns this routine's start address.
129    * 
130    * @return the start address
131    */
132   public int getStartAddress() {
133     
134     return startAddress;
135   }
136   
137   /***
138    * Returns the routine's return address.
139    * 
140    * @return the routine's return address
141    */
142   public int getReturnAddress() {
143     
144     return returnAddress;
145   }
146   
147   /***
148    * Sets the return address.
149    * 
150    * @param address the return address
151    */
152   public void setReturnAddress(int address) {
153     
154     this.returnAddress = address;
155   }
156   
157   /***
158    * Returns the routine's return variable number.
159    * 
160    * @return the return variable number
161    */
162   public int getReturnVariable() {
163     
164     return returnVarNum;
165   }
166   
167   /***
168    * Sets the routine's return variable number.
169    * 
170    * @param varnum the return variable number
171    */
172   public void setReturnVariable(int varnum) {
173     
174     returnVarNum = varnum;
175   }
176   
177   /***
178    * Returns the stack pointer at invocation time.
179    * 
180    * @return the stack pointer at invocation time
181    */
182   public int getInvocationStackPointer() {
183     
184     return invocationStackPointer;
185   }
186   
187   /***
188    * Sets the stack pointer at invocation time.
189    * 
190    * @param stackpointer the stack pointer at invocation time.
191    */
192   public void setInvocationStackPointer(int stackpointer) {
193     
194     invocationStackPointer = stackpointer;
195   }
196 }