KickJava   Java API By Example, From Geeks To Geeks.

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


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

109     void emitAdd(CodeBuilder cb) { cb.emitDADD(); }
110
111     void emitNumericOp(CodeBuilder cb, String JavaDoc op) {
112         if (op == "-") cb.emitDSUB();
113         else if (op == "%") cb.emitDREM();
114         else if (op == "/") cb.emitDDIV();
115         else if (op == "*") cb.emitDMUL();
116     }
117     void emitNeg(CodeBuilder cb) { cb.emitDNEG(); }
118
119     public void emitLoad(CodeBuilder cb, int loc) { cb.emitDLOAD(loc); }
120     public void emitStore(CodeBuilder cb, int loc) { cb.emitDSTORE(loc); }
121     public void emitReturn(CodeBuilder cb) { cb.emitDRETURN(); }
122
123     void emitCast(CodeBuilder cb, Type castTo) {
124         ((NumericType) castTo).emitCastFromDouble(cb);
125     }
126     void emitCastFromInt(CodeBuilder cb) { cb.emitI2D(); }
127     void emitCastFromFloat(CodeBuilder cb) { cb.emitF2D(); }
128     void emitCastFromLong(CodeBuilder cb) { cb.emitL2D(); }
129     void emitCastFromDouble(CodeBuilder cb) { }
130
131     void emitPop(CodeBuilder cb) { cb.emitPOP2(); }
132     void emitDup(CodeBuilder cb) { cb.emitDUP2(); }
133     void emitDupX1(CodeBuilder cb) { cb.emitDUP2_X1(); }
134     void emitDupX2(CodeBuilder cb) { cb.emitDUP2_X2(); }
135
136     void emitZero(CodeBuilder cb) { cb.emitDoubleConstant(0.0); }
137     void emitOne(CodeBuilder cb) { cb.emitDoubleConstant(1.0); }
138     void emitMinusOne(CodeBuilder cb) { cb.emitDoubleConstant(-1.0); }
139
140     void emitEqualityCompare(CodeBuilder cb, String JavaDoc op, Label t, Label f) {
141         cb.emitDCMPL(); // -1 if either are NaN
142
if (op == "==") cb.emitIFEQ(t, f);
143         else if (op == "!=") cb.emitIFNE(t, f);
144     }
145
146     void emitNumericCompare(CodeBuilder cb, String JavaDoc op, Label t, Label f) {
147         if (op == "<") { cb.emitDCMPG(); cb.emitIFLT(t, f); }
148         else if (op == "<=") { cb.emitDCMPG(); cb.emitIFLE(t, f); }
149         else if (op == ">=") { cb.emitDCMPL(); cb.emitIFGE(t, f); }
150         else if (op == ">") { cb.emitDCMPL(); cb.emitIFGT(t, f); }
151     }
152
153     void emitAload(CodeBuilder cb) { cb.emitDALOAD(); }
154     void emitAstore(CodeBuilder cb) { cb.emitDASTORE(); }
155
156 }
157
158
159
Popular Tags