KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > classfile > InsnIntOp


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.classfile;
26
27 import java.io.PrintStream JavaDoc;
28
29 /**
30  * An instruction which requires a integral constant as an immediate operand
31  */

32
33 public class InsnIntOp extends Insn {
34   /* The operand */
35   private int operandValue;
36
37   /* public accessors */
38
39   public int nStackArgs() {
40     return VMOp.ops[opcode()].nStackArgs();
41   }
42
43   public int nStackResults() {
44     return VMOp.ops[opcode()].nStackResults();
45   }
46
47   public String JavaDoc argTypes() {
48     return VMOp.ops[opcode()].argTypes();
49   }
50
51   public String JavaDoc resultTypes() {
52     return VMOp.ops[opcode()].resultTypes();
53   }
54
55   public boolean branches() {
56     return opcode() == opc_ret;
57   }
58
59   public int value() {
60     return operandValue;
61   }
62
63   /* package local methods */
64
65   static String JavaDoc primType(int primIndex) {
66     switch (primIndex) {
67     case T_BOOLEAN:
68         return "boolean";//NOI18N
69
case T_CHAR:
70         return "char";//NOI18N
71
case T_FLOAT:
72         return "float";//NOI18N
73
case T_DOUBLE:
74         return "double";//NOI18N
75
case T_BYTE:
76         return "byte";//NOI18N
77
case T_SHORT:
78         return "short";//NOI18N
79
case T_INT:
80         return "int";//NOI18N
81
case T_LONG:
82         return "long";//NOI18N
83
default:
84         throw new InsnError ("Invalid primitive type(" + primIndex + ")");//NOI18N
85
}
86   }
87
88   void print (PrintStream JavaDoc out, int indent) {
89     ClassPrint.spaces(out, indent);
90     if (opcode() == opc_newarray)
91         out.println(offset() + " opc_newarray " + primType(operandValue));//NOI18N
92
else
93         out.println(offset() + " " + opName(opcode()) + " " + operandValue);//NOI18N
94
}
95
96   int store(byte[] buf, int index) {
97     if (size() == 4) {
98       /* prefix with an opc_wide */
99       buf[index++] = (byte) opc_wide;
100     }
101
102     buf[index++] = (byte) opcode();
103     if (size() > 2)
104       buf[index++] = (byte)(operandValue >> 8);
105     buf[index++] = (byte)(operandValue & 0xff);
106     return index;
107   }
108
109
110   /* return the size of the instruction in bytes */
111
112   int size() {
113     switch(opcode()) {
114     case opc_bipush:
115     case opc_newarray:
116       /* These are always 1 byte constants */
117       return 2;
118
119     case opc_sipush: /* a short constant */
120       /* This is always a 2 byte constant */
121       return 3;
122
123     case opc_iload:
124     case opc_lload:
125     case opc_fload:
126     case opc_dload:
127     case opc_aload:
128     case opc_istore:
129     case opc_lstore:
130     case opc_fstore:
131     case opc_dstore:
132     case opc_astore:
133     case opc_ret:
134       /* These can be one or two byte constants specifying a local var.
135        * If a two byte constant, the constant is prefixed by a wide
136        * instruction */

137       if (operandValue < 256)
138     return 2;
139       else
140     return 4;
141
142     default:
143         throw new InsnError ("invalid instruction " + opName(opcode()) +//NOI18N
144
" with an integer operand");//NOI18N
145
}
146   }
147
148
149   InsnIntOp (int theOpcode, int theOperand, int pc) {
150     super(theOpcode, pc);
151
152     operandValue = theOperand;
153   }
154
155
156   InsnIntOp (int theOpcode, int theOperand) {
157     super(theOpcode, NO_OFFSET);
158
159     operandValue = theOperand;
160     switch(theOpcode) {
161     case opc_bipush:
162     case opc_newarray:
163       /* These are always 1 byte constants */
164
165     case opc_sipush: /* a short constant */
166       /* This is always a 2 byte constant */
167
168     case opc_dload:
169     case opc_lload:
170     case opc_iload:
171     case opc_fload:
172     case opc_aload:
173     case opc_istore:
174     case opc_lstore:
175     case opc_fstore:
176     case opc_dstore:
177     case opc_astore:
178     case opc_ret:
179       /* These can be one or two byte constants specifying a local var */
180       break;
181
182     default:
183         throw new InsnError ("attempt to create an " + opName(theOpcode) +//NOI18N
184
" with an integer operand");//NOI18N
185
}
186   }
187 }
188
Popular Tags