View Javadoc

1   /*
2    * $Id: Operand.java,v 1.3 2005/10/15 19:29:52 weiju Exp $
3    * 
4    * Created on 24.09.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 is the definition of an instruction's operand. Each operand has
27   * an operand type, and a value which is to be interpreted according to
28   * the type.
29   * 
30   * @author Wei-ju Wu
31   * @version 1.0
32   */
33  public class Operand {
34  
35    /***
36     * Type number for a large constant.
37     */
38    public static final byte TYPENUM_LARGE_CONSTANT = 0x00;
39    
40    /***
41     * Type number for a small constant.
42     */
43    public static final byte TYPENUM_SMALL_CONSTANT = 0x01;
44    
45    /***
46     * Type number for a variable.
47     */
48    public static final byte TYPENUM_VARIABLE       = 0x02;
49    
50    /***
51     * Type number for omitted.
52     */
53    public static final byte TYPENUM_OMITTED        = 0x03;
54    
55    /***
56     * The available operand types.
57     */
58    public enum OperandType { SMALL_CONSTANT, LARGE_CONSTANT, VARIABLE, OMITTED }
59    
60    /***
61     * This operand's type.
62     */
63    private OperandType type;
64    
65    /***
66     * This operand's value.
67     */
68    private short value;
69  
70    /***
71     * Constructor.
72     * 
73     * @param typenum the type number, must be < 4
74     * @param value the operand value
75     */
76    public Operand(int typenum, short value) {
77      
78      type = getOperandType(typenum);
79      this.value = value;
80    }
81  
82    /***
83     * Determines the operand type from a two-bit value.
84     * 
85     * @param typenum the type number
86     * @return the operand type
87     */
88    private static OperandType getOperandType(int typenum) {
89      
90      switch (typenum) {
91      
92      case 0x00:
93        return OperandType.LARGE_CONSTANT;
94      case 0x01:
95        return OperandType.SMALL_CONSTANT;
96      case 0x02:
97        return OperandType.VARIABLE;
98      default:
99        return OperandType.OMITTED; // In fact, such a value should never exist..
100     }
101   }
102   
103   /***
104    * Returns this operand's type.
105    * 
106    * @return the operand type
107    */
108   public OperandType getType() { return type; }
109   
110   /***
111    * The operand value.
112    * 
113    * @return the value
114    */
115   public short getValue() { return value; }
116 }