KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > IntishType


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.ast;
26
27 import org.aspectj.compiler.base.*;
28 import java.io.IOException JavaDoc;
29
30 import org.aspectj.compiler.base.bcg.CodeBuilder;
31 import org.aspectj.compiler.base.bcg.Label;
32
33 public abstract class IntishType extends IntegralType {
34
35     public Expr getNullExpr() {
36         return new IntLiteralExpr(getAST().getSourceLocation(), this, "0", 0);
37     }
38
39     public IntishType(JavaCompiler compiler) {
40         super(compiler);
41     }
42
43     // folding
44
private LiteralExpr makeLit(int i) {
45         return getAST().makeLiteral(i);
46     }
47     private LiteralExpr makeBooleanLit(boolean b) {
48         return getAST().makeLiteral(b);
49     }
50
51     LiteralExpr foldPlusOp(LiteralExpr rand) {
52         return makeLit(+ rand.getIntValue());
53     }
54     LiteralExpr foldMinusOp(LiteralExpr rand) {
55         return makeLit(- rand.getIntValue());
56     }
57     LiteralExpr foldBitNotOp(LiteralExpr rand) {
58         return makeLit(~ rand.getIntValue());
59     }
60
61     LiteralExpr foldAddOp(LiteralExpr rand1, LiteralExpr rand2) {
62         return makeLit(rand1.getIntValue() + rand2.getIntValue());
63     }
64
65     LiteralExpr foldNumericOp(String JavaDoc op, LiteralExpr lit1, LiteralExpr lit2) {
66         final AST ast = getAST();
67         int a = lit1.getIntValue();
68         int b = lit2.getIntValue();
69         if (op == "-") return ast.makeLiteral(a - b);
70         else if (op == "%") return ast.makeLiteral(a % b);
71         else if (op == "/") return ast.makeLiteral(a / b);
72         else if (op == "*") return ast.makeLiteral(a * b);
73         else throw new RuntimeException JavaDoc("bad op " + op);
74     }
75
76     LiteralExpr foldBitwiseOp(String JavaDoc op, LiteralExpr lit1, LiteralExpr lit2) {
77         final AST ast = getAST();
78         int a = lit1.getIntValue();
79         int b = lit2.getIntValue();
80         if (op == "&") return ast.makeLiteral(a & b);
81         else if (op == "|") return ast.makeLiteral(a | b);
82         else if (op == "^") return ast.makeLiteral(a ^ b);
83         else throw new RuntimeException JavaDoc("bad op " + op);
84     }
85
86     LiteralExpr foldShiftOp(String JavaDoc op, LiteralExpr lit1, LiteralExpr lit2) {
87         final AST ast = getAST();
88         int a = lit1.getIntValue();
89         int b = lit2.getIntValue();
90         if (op == "<<") return ast.makeLiteral(a << b);
91         else if (op == ">>") return ast.makeLiteral(a >> b);
92         else if (op == ">>>") return ast.makeLiteral(a >>> b);
93         else throw new RuntimeException JavaDoc("bad op " + op);
94     }
95
96     LiteralExpr foldEqualityTestOp(String JavaDoc op, LiteralExpr lit1, LiteralExpr lit2) {
97         final AST ast = getAST();
98         int a = lit1.getIntValue();
99         int b = lit2.getIntValue();
100         if (op == "==") return ast.makeLiteral(a == b);
101         else if (op == "!=") return ast.makeLiteral(a != b);
102         else throw new RuntimeException JavaDoc("bad op " + op);
103     }
104
105     LiteralExpr foldNumericTestOp(String JavaDoc op, LiteralExpr lit1, LiteralExpr lit2) {
106         final AST ast = getAST();
107         int a = lit1.getIntValue();
108         int b = lit2.getIntValue();
109         if (op == "<") return ast.makeLiteral(a < b);
110         else if (op == "<=") return ast.makeLiteral(a <= b);
111         else if (op == ">=") return ast.makeLiteral(a >= b);
112         else if (op == ">") return ast.makeLiteral(a > b);
113         else throw new RuntimeException JavaDoc("bad op " + op);
114     }
115
116     // code gen
117
final void emitAdd(CodeBuilder cb) { cb.emitIADD(); }
118
119     final void emitNumericOp(CodeBuilder cb, String JavaDoc op) {
120         if (op == "-") cb.emitISUB();
121         else if (op == "%") cb.emitIREM();
122         else if (op == "/") cb.emitIDIV();
123         else if (op == "*") cb.emitIMUL();
124     }
125     final void emitBitwiseOp(CodeBuilder cb, String JavaDoc op) {
126         if (op == "&") cb.emitIAND();
127         else if (op == "|") cb.emitIOR();
128         else if (op == "^") cb.emitIXOR();
129     }
130     final void emitShiftOp(CodeBuilder cb, String JavaDoc op) {
131         if (op == "<<") cb.emitISHL();
132         else if (op == ">>") cb.emitISHR();
133         else if (op == ">>>") cb.emitIUSHR();
134     }
135     final void emitEqualityCompare(CodeBuilder cb, String JavaDoc op, Label t, Label f) {
136         if (op == "==") cb.emitIF_ICMPEQ(t, f);
137         else if (op == "!=") cb.emitIF_ICMPNE(t, f);
138     }
139     final void emitNumericCompare(CodeBuilder cb, String JavaDoc op, Label t, Label f) {
140         if (op == "<") cb.emitIF_ICMPLT(t, f);
141         else if (op == "<=") cb.emitIF_ICMPLE(t, f);
142         else if (op == ">=") cb.emitIF_ICMPGE(t, f);
143         else if (op == ">") cb.emitIF_ICMPGT(t, f);
144     }
145
146     final void emitNeg(CodeBuilder cb) { cb.emitINEG(); }
147
148     void emitCast(CodeBuilder cb, Type castTo) {
149         if (castTo != this) {
150             ((NumericType) castTo).emitCastFromInt(cb);
151         }
152     }
153
154     final public void emitLoad(CodeBuilder cb, int loc) { cb.emitILOAD(loc); }
155     final public void emitStore(CodeBuilder cb, int loc) { cb.emitISTORE(loc); }
156     final public void emitReturn(CodeBuilder cb) { cb.emitIRETURN(); }
157
158     final void emitZero(CodeBuilder cb) { cb.emitIntConstant(0); }
159     final void emitOne(CodeBuilder cb) { cb.emitIntConstant(1); }
160     final void emitMinusOne(CodeBuilder cb) { cb.emitIntConstant(-1); }
161
162
163     // ------------------------------
164
// "fast" opcode handling
165

166     boolean hasFastEqualityTestOp() { return true; }
167     void emitFastEqualityTestOp(CodeBuilder cb, EqualityTestOpExpr e,
168                                 Label t, Label f) {
169         Expr rand1 = e.getRand1();
170         Expr rand2 = e.getRand2();
171         String JavaDoc op = e.getOp();
172         if (rand1.isConstantZero()) {
173             rand2.cgValue(cb, this);
174             emitEqualityCompare0(cb, op, t, f);
175         } else if (rand2.isConstantZero()) {
176             rand1.cgValue(cb, this);
177             emitEqualityCompare0(cb, op, t, f);
178         } else {
179             rand1.cgValue(cb, this);
180             rand2.cgValue(cb, this);
181             emitEqualityCompare(cb, op, t, f);
182         }
183     }
184     private void emitEqualityCompare0(CodeBuilder cb, String JavaDoc op, Label t, Label f) {
185         if (op == "==") cb.emitIFEQ(t, f);
186         else if (op == "!=") cb.emitIFNE(t, f);
187     }
188
189     boolean hasFastNumericTestOp() { return true; }
190     void emitFastNumericTestOp(CodeBuilder cb, NumericTestOpExpr e,
191                                 Label t, Label f) {
192         Expr rand1 = e.getRand1();
193         Expr rand2 = e.getRand2();
194         String JavaDoc op = e.getOp();
195         if (rand1.isConstantZero()) {
196             rand2.cgValue(cb, this);
197             emit0Compare(cb, op, t, f);
198         } else if (rand2.isConstantZero()) {
199             rand1.cgValue(cb, this);
200             emitCompare0(cb, op, t, f);
201         } else {
202             rand1.cgValue(cb, this);
203             rand2.cgValue(cb, this);
204             emitNumericCompare(cb, op, t, f);
205         }
206     }
207     private void emit0Compare(CodeBuilder cb, String JavaDoc op, Label t, Label f) {
208         if (op == "<") cb.emitIFGT(t, f);
209         else if (op == "<=") cb.emitIFGE(t, f);
210         else if (op == ">=") cb.emitIFLE(t, f);
211         else if (op == ">") cb.emitIFLT(t, f);
212     }
213     private void emitCompare0(CodeBuilder cb, String JavaDoc op, Label t, Label f) {
214         if (op == "<") cb.emitIFLT(t, f);
215         else if (op == "<=") cb.emitIFLE(t, f);
216         else if (op == ">=") cb.emitIFGE(t, f);
217         else if (op == ">") cb.emitIFGT(t, f);
218     }
219 }
220
Popular Tags