KickJava   Java API By Example, From Geeks To Geeks.

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


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 final class FloatType extends RealType {
34
35     protected boolean isAssignableFromOtherType(Type other) {
36         return (other instanceof NumericType) && ! (other instanceof DoubleType);
37     }
38
39     public String JavaDoc getName() { return "float"; }
40     public int getTypeIndex() { return Type.FLOAT; }
41     public Type getRefType() { return getTypeManager().getType("java.lang", "Float"); }
42     public Expr getNullExpr() { return new FloatLiteralExpr(getAST().getSourceLocation(), 0); }
43
44     public FloatType(JavaCompiler compiler) {
45         super(compiler);
46     }
47
48     // vm internals
49
public String JavaDoc getDescriptor() { return "F"; }
50
51     // folding
52
private LiteralExpr makeLit(float f) {
53         return new FloatLiteralExpr(getAST().getSourceLocation(), f);
54     }
55     private LiteralExpr makeBooleanLit(boolean b) {
56         return getAST().makeLiteral(b);
57     }
58
59     LiteralExpr foldCast(LiteralExpr rand) {
60         return makeLit(rand.getFloatValue());
61     }
62
63     strictfp LiteralExpr foldPlusOp(LiteralExpr rand) {
64         return makeLit(+ rand.getFloatValue());
65     }
66     strictfp LiteralExpr foldMinusOp(LiteralExpr rand) {
67         return makeLit(- rand.getFloatValue());
68     }
69
70     strictfp LiteralExpr foldAddOp(LiteralExpr rand1, LiteralExpr rand2) {
71         return makeLit(rand1.getFloatValue() + rand2.getFloatValue());
72     }
73
74     strictfp LiteralExpr foldNumericOp(String JavaDoc op,
75                                        LiteralExpr lit1, LiteralExpr lit2) {
76         final AST ast = getAST();
77         float a = lit1.getFloatValue();
78         float b = lit2.getFloatValue();
79         if (op == "-") return ast.makeLiteral(a - b);
80         else 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 numeric op " + op);
84     }
85
86     LiteralExpr foldEqualityTestOp(String JavaDoc op, LiteralExpr lit1, LiteralExpr lit2) {
87         final AST ast = getAST();
88         float a = lit1.getFloatValue();
89         float b = lit2.getFloatValue();
90         if (op == "==") return ast.makeLiteral(a == b);
91         else if (op == "!=") return ast.makeLiteral(a != b);
92         else throw new RuntimeException JavaDoc("bad op " + op);
93     }
94
95     LiteralExpr foldNumericTestOp(String JavaDoc op, LiteralExpr lit1, LiteralExpr lit2) {
96         final AST ast = getAST();
97         float a = lit1.getFloatValue();
98         float b = lit2.getFloatValue();
99         if (op == "<") return ast.makeLiteral(a < b);
100         else if (op == "<=") return ast.makeLiteral(a <= b);
101         else if (op == ">=") return ast.makeLiteral(a >= b);
102         else if (op == ">") return ast.makeLiteral(a > b);
103         else throw new RuntimeException JavaDoc("bad op " + op);
104     }
105
106     // code gen
107

108     void emitAdd(CodeBuilder cb) { cb.emitFADD(); }
109
110     void emitNumericOp(CodeBuilder cb, String JavaDoc op) {
111         if (op == "-") cb.emitFSUB();
112         else if (op == "%") cb.emitFREM();
113         else if (op == "/") cb.emitFDIV();
114         else if (op == "*") cb.emitFMUL();
115     }
116
117     void emitNeg(CodeBuilder cb) { cb.emitFNEG(); }
118
119     public void emitLoad(CodeBuilder cb, int loc) { cb.emitFLOAD(loc); }
120     public void emitStore(CodeBuilder cb, int loc) { cb.emitFSTORE(loc); }
121     public void emitReturn(CodeBuilder cb) { cb.emitFRETURN(); }
122
123     void emitCast(CodeBuilder cb, Type castTo) {
124         ((NumericType) castTo).emitCastFromFloat(cb);
125     }
126     void emitCastFromInt(CodeBuilder cb) { cb.emitI2F(); }
127     void emitCastFromFloat(CodeBuilder cb) { }
128     void emitCastFromLong(CodeBuilder cb) { cb.emitL2F(); }
129     void emitCastFromDouble(CodeBuilder cb) { cb.emitD2F(); }
130
131     void emitZero(CodeBuilder cb) { cb.emitFloatConstant(0.0f); }
132     void emitOne(CodeBuilder cb) { cb.emitFloatConstant(1.0f); }
133     void emitMinusOne(CodeBuilder cb) { cb.emitFloatConstant(-1.0f); }
134
135     void emitEqualityCompare(CodeBuilder cb, String JavaDoc op, Label t, Label f) {
136         cb.emitFCMPL(); // -1 if either are NaN
137
if (op == "==") cb.emitIFEQ(t, f);
138         else if (op == "!=") cb.emitIFNE(t, f);
139     }
140
141     void emitNumericCompare(CodeBuilder cb, String JavaDoc op, Label t, Label f) {
142         if (op == "<") { cb.emitFCMPG(); cb.emitIFLT(t, f); }
143         else if (op == "<=") { cb.emitFCMPG(); cb.emitIFLE(t, f); }
144         else if (op == ">=") { cb.emitFCMPL(); cb.emitIFGE(t, f); }
145         else if (op == ">") { cb.emitFCMPL(); cb.emitIFGT(t, f); }
146     }
147
148     void emitAload(CodeBuilder cb) { cb.emitFALOAD(); }
149     void emitAstore(CodeBuilder cb) { cb.emitFASTORE(); }
150
151 }
152
Popular Tags