KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > baf > internal > BPrimitiveCastInst


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrick Lam, Patrick Pominville and Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30 package soot.baf.internal;
31
32 import soot.*;
33 import soot.baf.*;
34 import soot.util.*;
35 import java.util.*;
36
37 public class BPrimitiveCastInst extends AbstractInst
38                             implements PrimitiveCastInst
39 {
40     Type fromType;
41     
42     protected Type toType;
43
44     public int getInCount()
45     {
46         return 1;
47     }
48
49     public int getInMachineCount()
50     {
51         return JasminClass.sizeOfType(fromType);
52     }
53     
54     public int getOutCount()
55     {
56         return 1;
57     }
58
59     public int getOutMachineCount()
60     {
61         return JasminClass.sizeOfType(toType);
62     }
63
64     
65     public BPrimitiveCastInst(Type fromType, Type toType)
66     {
67         
68         if( fromType instanceof NullType )
69             throw new RuntimeException JavaDoc("invalid fromType " + fromType);
70         this.fromType = fromType;
71         this.toType = toType;
72     }
73
74     
75     public Object JavaDoc clone()
76     {
77         return new BPrimitiveCastInst(getFromType(), toType);
78     }
79
80
81
82     // after changing the types, use getName to check validity
83
public Type getFromType() { return fromType; }
84     public void setFromType(Type t) { fromType = t;}
85     
86     public Type getToType() { return toType; }
87     public void setToType(Type t) { toType = t;}
88
89     final public String JavaDoc getName()
90     {
91         TypeSwitch sw;
92
93         fromType.apply(sw = new TypeSwitch()
94         {
95             public void defaultCase(Type ty)
96                 {
97                     throw new RuntimeException JavaDoc("invalid fromType " + fromType);
98                 }
99
100             public void caseDoubleType(DoubleType ty)
101                 {
102                     if(toType.equals(IntType.v()))
103                         setResult("d2i");
104                     else if(toType.equals(LongType.v()))
105                         setResult("d2l");
106                     else if(toType.equals(FloatType.v()))
107                         setResult("d2f");
108                     else
109                         throw new RuntimeException JavaDoc
110                             ("invalid toType from double: " + toType);
111                 }
112
113             public void caseFloatType(FloatType ty)
114                 {
115                     if(toType.equals(IntType.v()))
116                         setResult("f2i");
117                     else if(toType.equals(LongType.v()))
118                         setResult("f2l");
119                     else if(toType.equals(DoubleType.v()))
120                         setResult("f2d");
121                     else
122                         throw new RuntimeException JavaDoc
123                             ("invalid toType from float: " + toType);
124                 }
125
126             public void caseIntType(IntType ty)
127                 {
128                     emitIntToTypeCast();
129                 }
130
131             public void caseBooleanType(BooleanType ty)
132                 {
133                     emitIntToTypeCast();
134                 }
135
136             public void caseByteType(ByteType ty)
137                 {
138                     emitIntToTypeCast();
139                 }
140
141             public void caseCharType(CharType ty)
142                 {
143                     emitIntToTypeCast();
144                 }
145             
146             public void caseShortType(ShortType ty)
147                 {
148                     emitIntToTypeCast();
149                 }
150
151             private void emitIntToTypeCast()
152                 {
153                     if(toType.equals(ByteType.v()))
154                         setResult("i2b");
155                     else if(toType.equals(CharType.v()))
156                         setResult("i2c");
157                     else if(toType.equals(ShortType.v()))
158                         setResult("i2s");
159                     else if(toType.equals(FloatType.v()))
160                         setResult("i2f");
161                     else if(toType.equals(LongType.v()))
162                         setResult("i2l");
163                     else if(toType.equals(DoubleType.v()))
164                         setResult("i2d");
165                     else if(toType.equals(IntType.v()))
166                         setResult(""); // this shouldn't happen?
167
else if(toType.equals(BooleanType.v()))
168             setResult("");
169                     else
170                         throw new RuntimeException JavaDoc
171                             ("invalid toType from int: " + toType);
172                 }
173
174             public void caseLongType(LongType ty)
175                 {
176                     if(toType.equals(IntType.v()))
177                         setResult("l2i");
178                     else if(toType.equals(FloatType.v()))
179                         setResult("l2f");
180                     else if(toType.equals(DoubleType.v()))
181                         setResult("l2d");
182                     else
183                         throw new RuntimeException JavaDoc
184                               ("invalid toType from long: " + toType);
185                             
186                 }
187         });
188         return (String JavaDoc)sw.getResult();
189     }
190
191     /* override toString with our own, *not* including types */
192     public String JavaDoc toString()
193     {
194         return getName() +
195             getParameters();
196     }
197
198     public void apply(Switch sw)
199     {
200         ((InstSwitch) sw).casePrimitiveCastInst(this);
201     }
202 }
203
204
Popular Tags