Source code: com/chaoswg/xtc4y/classdesc/code/instructions/BinaryInstruction.java
1 //$Header: /cvsroot/xtc4y/xtc4y/src/com/chaoswg/xtc4y/classdesc/code/instructions/BinaryInstruction.java,v 1.2 2003/08/26 12:35:25 toggm Exp $
2 /******************************************************************************
3 * XTC4y - eXtreme Testing Collection 4 you *
4 * -------------------------------------------------------------------------- *
5 * URL: http://www.chaoswg.com/xtc4y *
6 * Author: Mike Toggweiler (2.dog@gmx.ch) *
7 * *
8 * Last Updated: $Date: 2003/08/26 12:35:25 $, by $Author: toggm $ *
9 * Version: $Revision: 1.2 $ *
10 * -------------------------------------------------------------------------- *
11 * COPYRIGHT: (c) 2003 by Mike Toggweiler *
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 *****************************************************************************/
18 package com.chaoswg.xtc4y.classdesc.code.instructions;
19
20 import java.io.DataInputStream;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import com.chaoswg.xtc4y.classdesc.ConstantPool;
25
26 /**
27 * A binary instruction consists of a command and one one byte operand,
28 * therefore its size is 2 bytes
29 * @author Mike Toggweiler
30 **/
31 public class BinaryInstruction extends Instruction {
32 private byte operand;
33
34 /**
35 * Opcodes of byte binary instructions
36 **/
37
38 /**
39 * push a byte onto the stack
40 **/
41 public static final byte BIPUSH = (byte)16;
42 /**
43 * Load a local int variable by index
44 **/
45 public static final byte ILOAD = (byte)21;
46 /**
47 * Load a local long variable by index
48 **/
49 public static final byte LLOAD = (byte)22;
50 /**
51 * Load a local float variable by index
52 **/
53 public static final byte FLOAD = (byte)23;
54 /**
55 * Load a local double variable by index
56 **/
57 public static final byte DLOAD = (byte)24;
58 /**
59 * Load a local object variable by index
60 **/
61 public static final byte ALOAD = (byte)25;
62 /**
63 * store a local int variable at index
64 **/
65 public static final byte ISTORE = (byte)54;
66 /**
67 * store a local long variable at index
68 **/
69 public static final byte LSTORE = (byte)55;
70 /**
71 * store a local float variable at index
72 **/
73 public static final byte FSTORE = (byte)56;
74 /**
75 * store a local double variable at index
76 **/
77 public static final byte DSTORE = (byte)57;
78 /**
79 * store a local object variable at index
80 **/
81 public static final byte ASTORE = (byte)58;
82 /**
83 * Return from a subroutine call reading the return address from the loca
84 * variable
85 **/
86 public static final byte RET = (byte)169;
87
88 /**
89 * Creates an BinaryInstruction and initializes it from a
90 * DataInputStream. the operand is read from the stream and
91 * provided to extending classes for a further handling of it
92 * @param the opcode of the command
93 * @param dis the DataInputStream to read from
94 * @param cp the constant pool to resolve indices
95 **/
96 protected BinaryInstruction(byte command, DataInputStream dis,
97 ConstantPool cp) throws IOException {
98 super(command);
99 handleOperand((byte)dis.readUnsignedByte(), cp);
100 }
101
102 /**
103 * Create a new binary instruction with a command and the byte value
104 * as operand
105 * @param command
106 **/
107 public BinaryInstruction(byte command) {
108 super(command);
109 }
110
111 /**
112 * Handle operand. By default, the operands value is stored. Another
113 * behaviour may be implemented in an extending class
114 * @param operand to handle
115 * @param cp the constant pool to resolve indices
116 **/
117 protected void handleOperand(byte operand, ConstantPool cp) {
118 this.operand = operand;
119 }
120
121 /**
122 * Write the instruction onto the DataOutputStream, using the
123 * constant pool to register variables. .
124 * @param dos the DataOutputStream to write on
125 * @param cp The constant pool to register variables
126 **/
127 public final void write(DataOutputStream dos, ConstantPool cp)
128 throws IOException {
129 super.write(dos, cp);
130 dos.writeByte(getOperand(cp));
131 }
132
133 /**
134 * @param cp The constant pool to register variables
135 * @return the byte value of the operand, by default, the previously
136 * stored operands value will be returned. Another behiour may be
137 * implemented in an extending class.
138 **/
139 protected byte getOperand(ConstantPool cp) {
140 return operand;
141 }
142
143 /**
144 * @return the number of bytes used in this command
145 **/
146 public final int getNumberOfUsedBytes() {
147 return 2;
148 }
149
150 /**
151 * Write code in a readable way
152 **/
153 public String toString() {
154 return super.toString() + " " + operand;
155 }
156 }