KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > util > IntType


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: IntType.java,v 1.8 2004/02/16 22:26:44 minchau Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
21
22 import com.sun.org.apache.bcel.internal.generic.BranchHandle;
23 import com.sun.org.apache.bcel.internal.generic.BranchInstruction;
24 import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
25 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
26 import com.sun.org.apache.bcel.internal.generic.GOTO;
27 import com.sun.org.apache.bcel.internal.generic.IFEQ;
28 import com.sun.org.apache.bcel.internal.generic.IFGE;
29 import com.sun.org.apache.bcel.internal.generic.IFGT;
30 import com.sun.org.apache.bcel.internal.generic.IFLE;
31 import com.sun.org.apache.bcel.internal.generic.IFLT;
32 import com.sun.org.apache.bcel.internal.generic.IF_ICMPGE;
33 import com.sun.org.apache.bcel.internal.generic.IF_ICMPGT;
34 import com.sun.org.apache.bcel.internal.generic.IF_ICMPLE;
35 import com.sun.org.apache.bcel.internal.generic.IF_ICMPLT;
36 import com.sun.org.apache.bcel.internal.generic.ILOAD;
37 import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
38 import com.sun.org.apache.bcel.internal.generic.INVOKESTATIC;
39 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
40 import com.sun.org.apache.bcel.internal.generic.ISTORE;
41 import com.sun.org.apache.bcel.internal.generic.Instruction;
42 import com.sun.org.apache.bcel.internal.generic.InstructionConstants;
43 import com.sun.org.apache.bcel.internal.generic.InstructionList;
44 import com.sun.org.apache.bcel.internal.generic.NEW;
45 import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
46 import com.sun.org.apache.xalan.internal.xsltc.compiler.FlowList;
47
48 /**
49  * @author Jacek Ambroziak
50  * @author Santiago Pericas-Geertsen
51  */

52 public final class IntType extends NumberType {
53     protected IntType() {}
54
55     public String JavaDoc toString() {
56     return "int";
57     }
58
59     public boolean identicalTo(Type other) {
60     return this == other;
61     }
62
63     public String JavaDoc toSignature() {
64     return "I";
65     }
66
67     public com.sun.org.apache.bcel.internal.generic.Type toJCType() {
68     return com.sun.org.apache.bcel.internal.generic.Type.INT;
69     }
70
71     /**
72      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#distanceTo
73      */

74     public int distanceTo(Type type) {
75     if (type == this) {
76         return 0;
77     }
78     else if (type == Type.Real) {
79         return 1;
80     }
81     else
82         return Integer.MAX_VALUE;
83     }
84     
85     /**
86      * Translates an integer into an object of internal type <code>type</code>.
87      *
88      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
89      */

90     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
91                 final Type type) {
92     if (type == Type.Real) {
93         translateTo(classGen, methodGen, (RealType) type);
94     }
95     else if (type == Type.String) {
96         translateTo(classGen, methodGen, (StringType) type);
97     }
98     else if (type == Type.Boolean) {
99         translateTo(classGen, methodGen, (BooleanType) type);
100     }
101     else if (type == Type.Reference) {
102         translateTo(classGen, methodGen, (ReferenceType) type);
103     }
104     else {
105         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
106                     toString(), type.toString());
107         classGen.getParser().reportError(Constants.FATAL, err);
108     }
109     }
110
111     /**
112      * Expects an integer on the stack and pushes a real.
113      *
114      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
115      */

116     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
117                 RealType type) {
118     methodGen.getInstructionList().append(I2D);
119     }
120
121     /**
122      * Expects an integer on the stack and pushes its string value by calling
123      * <code>Integer.toString(int i)</code>.
124      *
125      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
126      */

127     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
128                 StringType type) {
129     final ConstantPoolGen cpg = classGen.getConstantPool();
130     final InstructionList il = methodGen.getInstructionList();
131     il.append(new INVOKESTATIC(cpg.addMethodref(INTEGER_CLASS,
132                             "toString",
133                             "(I)" + STRING_SIG)));
134     }
135
136     /**
137      * Expects an integer on the stack and pushes a 0 if its value is 0 and
138      * a 1 otherwise.
139      *
140      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
141      */

142     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
143                 BooleanType type) {
144     final InstructionList il = methodGen.getInstructionList();
145     final BranchHandle falsec = il.append(new IFEQ(null));
146     il.append(ICONST_1);
147     final BranchHandle truec = il.append(new GOTO(null));
148     falsec.setTarget(il.append(ICONST_0));
149     truec.setTarget(il.append(NOP));
150     }
151
152     /**
153      * Expects an integer on the stack and translates it to a non-synthesized
154      * boolean. It does not push a 0 or a 1 but instead returns branchhandle
155      * list to be appended to the false list.
156      *
157      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateToDesynthesized
158      */

159     public FlowList translateToDesynthesized(ClassGenerator classGen,
160                          MethodGenerator methodGen,
161                          BooleanType type) {
162     final InstructionList il = methodGen.getInstructionList();
163     return new FlowList(il.append(new IFEQ(null)));
164     }
165
166     /**
167      * Expects an integer on the stack and pushes a boxed integer.
168      * Boxed integers are represented by an instance of
169      * <code>java.lang.Integer</code>.
170      *
171      * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
172      */

173     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
174                 ReferenceType type) {
175     final ConstantPoolGen cpg = classGen.getConstantPool();
176     final InstructionList il = methodGen.getInstructionList();
177     il.append(new NEW(cpg.addClass(INTEGER_CLASS)));
178     il.append(DUP_X1);
179     il.append(SWAP);
180     il.append(new INVOKESPECIAL(cpg.addMethodref(INTEGER_CLASS,
181                              "<init>", "(I)V")));
182     }
183
184     /**
185      * Translates an integer into the Java type denoted by <code>clazz</code>.
186      * Expects an integer on the stack and pushes a number of the appropriate
187      * type after coercion.
188      */

189     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
190                 Class JavaDoc clazz) {
191     final InstructionList il = methodGen.getInstructionList();
192     if (clazz == Character.TYPE) {
193         il.append(I2C);
194     }
195     else if (clazz == Byte.TYPE) {
196         il.append(I2B);
197     }
198     else if (clazz == Short.TYPE) {
199         il.append(I2S);
200     }
201     else if (clazz == Integer.TYPE) {
202         il.append(NOP);
203     }
204     else if (clazz == Long.TYPE) {
205         il.append(I2L);
206     }
207     else if (clazz == Float.TYPE) {
208         il.append(I2F);
209     }
210     else if (clazz == Double.TYPE) {
211         il.append(I2D);
212     }
213          // Is Double <: clazz? I.e. clazz in { Double, Number, Object }
214
else if (clazz.isAssignableFrom(java.lang.Double JavaDoc.class)) {
215            il.append(I2D);
216            Type.Real.translateTo(classGen, methodGen, Type.Reference);
217         }
218     else {
219         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
220                     toString(), clazz.getName());
221         classGen.getParser().reportError(Constants.FATAL, err);
222     }
223     }
224
225     /**
226      * Translates an object of this type to its boxed representation.
227      */

228     public void translateBox(ClassGenerator classGen,
229                  MethodGenerator methodGen) {
230     translateTo(classGen, methodGen, Type.Reference);
231     }
232
233     /**
234      * Translates an object of this type to its unboxed representation.
235      */

236     public void translateUnBox(ClassGenerator classGen,
237                    MethodGenerator methodGen) {
238     final ConstantPoolGen cpg = classGen.getConstantPool();
239     final InstructionList il = methodGen.getInstructionList();
240     il.append(new CHECKCAST(cpg.addClass(INTEGER_CLASS)));
241     final int index = cpg.addMethodref(INTEGER_CLASS,
242                        INT_VALUE,
243                        INT_VALUE_SIG);
244     il.append(new INVOKEVIRTUAL(index));
245     }
246
247     public Instruction ADD() {
248     return InstructionConstants.IADD;
249     }
250
251     public Instruction SUB() {
252     return InstructionConstants.ISUB;
253     }
254
255     public Instruction MUL() {
256     return InstructionConstants.IMUL;
257     }
258
259     public Instruction DIV() {
260     return InstructionConstants.IDIV;
261     }
262
263     public Instruction REM() {
264     return InstructionConstants.IREM;
265     }
266
267     public Instruction NEG() {
268     return InstructionConstants.INEG;
269     }
270
271     public Instruction LOAD(int slot) {
272     return new ILOAD(slot);
273     }
274     
275     public Instruction STORE(int slot) {
276     return new ISTORE(slot);
277     }
278
279     public BranchInstruction GT(boolean tozero) {
280     return tozero ? (BranchInstruction) new IFGT(null) :
281         (BranchInstruction) new IF_ICMPGT(null);
282     }
283
284     public BranchInstruction GE(boolean tozero) {
285     return tozero ? (BranchInstruction) new IFGE(null) :
286         (BranchInstruction) new IF_ICMPGE(null);
287     }
288
289     public BranchInstruction LT(boolean tozero) {
290     return tozero ? (BranchInstruction) new IFLT(null) :
291         (BranchInstruction) new IF_ICMPLT(null);
292     }
293
294     public BranchInstruction LE(boolean tozero) {
295     return tozero ? (BranchInstruction) new IFLE(null) :
296         (BranchInstruction) new IF_ICMPLE(null);
297     }
298 }
299
Popular Tags