72
|
1 module dmd.RealExp;
|
|
2
|
114
|
3 import dmd.common;
|
129
|
4 import dmd.Complex;
|
72
|
5 import dmd.Expression;
|
|
6 import dmd.backend.elem;
|
|
7 import dmd.InterState;
|
|
8 import dmd.Type;
|
|
9 import dmd.OutBuffer;
|
|
10 import dmd.Loc;
|
|
11 import dmd.TOK;
|
|
12 import dmd.Scope;
|
0
|
13 import dmd.IRState;
|
72
|
14 import dmd.Type;
|
0
|
15 import dmd.HdrGenState;
|
|
16 import dmd.Port;
|
|
17 import dmd.TY;
|
72
|
18
|
129
|
19 import dmd.expression.Util;
|
|
20
|
0
|
21 import dmd.backend.dt_t;
|
|
22 import dmd.backend.Util;
|
|
23 import dmd.backend.TYM;
|
|
24 import dmd.backend.mTY;
|
|
25
|
73
|
26 import std.stdio;
|
148
|
27 import std.string;
|
73
|
28
|
0
|
29 class RealExp : Expression
|
|
30 {
|
|
31 real value;
|
|
32
|
|
33 this(Loc loc, real value, Type type)
|
|
34 {
|
|
35 super(loc, TOK.TOKfloat64, RealExp.sizeof);
|
|
36 //printf("RealExp.RealExp(%Lg)\n", value);
|
|
37 this.value = value;
|
|
38 this.type = type;
|
|
39 }
|
|
40
|
72
|
41 override bool equals(Object o)
|
0
|
42 {
|
129
|
43 if (this is o)
|
|
44 return true;
|
|
45
|
|
46 Expression e = cast(Expression)o;
|
|
47 if (e.op == TOKfloat64) {
|
|
48 RealExp ne = cast(RealExp)e;
|
|
49 if (type.toHeadMutable().equals(ne.type.toHeadMutable())) {
|
|
50 if (RealEquals(value, ne.value)) {
|
|
51 return true;
|
|
52 }
|
|
53 }
|
|
54 }
|
|
55
|
148
|
56 return false;
|
0
|
57 }
|
|
58
|
72
|
59 override Expression semantic(Scope sc)
|
0
|
60 {
|
|
61 if (!type)
|
|
62 type = Type.tfloat64;
|
|
63 else
|
|
64 type = type.semantic(loc, sc);
|
|
65 return this;
|
|
66 }
|
|
67
|
72
|
68 override Expression interpret(InterState istate)
|
0
|
69 {
|
148
|
70 version(LOG)
|
|
71 writef("RealExp::interpret() %s\n", toChars());
|
|
72
|
|
73 return this;
|
0
|
74 }
|
|
75
|
72
|
76 override string toChars()
|
0
|
77 {
|
148
|
78 return format(type.isimaginary() ? "%gi" : "%g", value);
|
0
|
79 }
|
|
80
|
72
|
81 override ulong toInteger()
|
0
|
82 {
|
148
|
83 version(IN_GCC)
|
|
84 return toReal().toInt();
|
|
85 else
|
|
86 return cast(ulong) toReal();
|
0
|
87 }
|
|
88
|
72
|
89 override ulong toUInteger()
|
0
|
90 {
|
148
|
91 version(IN_GCC)
|
|
92 return cast(ulong) toReal().toInt();
|
|
93 else
|
|
94 return cast(ulong) toReal();
|
0
|
95 }
|
|
96
|
72
|
97 override real toReal()
|
0
|
98 {
|
|
99 return type.isreal() ? value : 0;
|
|
100 }
|
|
101
|
72
|
102 override real toImaginary()
|
0
|
103 {
|
|
104 return type.isreal() ? 0 : value;
|
|
105 }
|
|
106
|
72
|
107 override Complex!(real) toComplex()
|
0
|
108 {
|
|
109 return Complex!(real)(toReal(), toImaginary());
|
|
110 }
|
|
111
|
72
|
112 override Expression castTo(Scope sc, Type t)
|
0
|
113 {
|
|
114 Expression e = this;
|
|
115 if (type != t)
|
|
116 {
|
|
117 if ((type.isreal() && t.isreal()) ||
|
|
118 (type.isimaginary() && t.isimaginary())
|
|
119 )
|
|
120 {
|
|
121 e = copy();
|
|
122 e.type = t;
|
|
123 }
|
|
124 else
|
|
125 e = Expression.castTo(sc, t);
|
|
126 }
|
|
127 return e;
|
|
128 }
|
|
129
|
72
|
130 override int isConst()
|
0
|
131 {
|
|
132 return 1;
|
|
133 }
|
|
134
|
72
|
135 override bool isBool(bool result)
|
0
|
136 {
|
123
|
137 return result ? (value != 0) : (value == 0);
|
0
|
138 }
|
|
139
|
72
|
140 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
141 {
|
148
|
142 floatToBuffer(buf, type, value);
|
0
|
143 }
|
|
144
|
72
|
145 override void toMangleBuffer(OutBuffer buf)
|
0
|
146 {
|
129
|
147 buf.writeByte('e');
|
|
148 realToMangleBuffer(buf, value);
|
0
|
149 }
|
|
150
|
72
|
151 override elem* toElem(IRState* irs)
|
0
|
152 {
|
|
153 eve c;
|
|
154 tym_t ty;
|
|
155
|
|
156 //printf("RealExp.toElem(%p) %s\n", this, toChars());
|
|
157 ///memset(&c, 0, sizeof(c));
|
|
158 ty = type.toBasetype().totym();
|
|
159 switch (tybasic(ty))
|
|
160 {
|
|
161 case TYfloat:
|
|
162 case TYifloat:
|
|
163 c.Vfloat = value;
|
|
164 if (Port.isSignallingNan(value)) {
|
|
165 std.stdio.writeln("signalling float");
|
|
166 (cast(uint*)&c.Vfloat)[0] &= 0xFFBFFFFFL;
|
|
167 }
|
|
168 break;
|
|
169
|
|
170 case TYdouble:
|
|
171 case TYidouble:
|
|
172 c.Vdouble = value; // unfortunately, this converts SNAN to QNAN
|
|
173 if (Port.isSignallingNan(value)) {
|
|
174 std.stdio.writeln("signalling double");
|
|
175 // Put SNAN back
|
|
176 (cast(uint*)&c.Vdouble)[1] &= 0xFFF7FFFFL;
|
|
177 }
|
|
178 break;
|
|
179
|
|
180 case TYldouble:
|
|
181 case TYildouble:
|
|
182 c.Vldouble = value;
|
|
183 break;
|
|
184
|
|
185 default:
|
|
186 print();
|
|
187 ///type.print();
|
|
188 ///type.toBasetype().print();
|
148
|
189 printf("ty = %d, tym = %lx\n", type.ty, ty);
|
0
|
190 assert(0);
|
|
191 }
|
|
192 return el_const(ty, &c);
|
|
193 }
|
|
194
|
|
195 static private char[6] zeropad;
|
|
196
|
72
|
197 override dt_t** toDt(dt_t** pdt)
|
0
|
198 {
|
|
199 float fvalue;
|
|
200 double dvalue;
|
|
201 real evalue;
|
|
202
|
|
203 //printf("RealExp.toDt(%Lg)\n", value);
|
|
204 switch (type.toBasetype().ty)
|
|
205 {
|
|
206 case Tfloat32:
|
|
207 case Timaginary32:
|
|
208 fvalue = value;
|
|
209 pdt = dtnbytes(pdt,4,cast(char*)&fvalue);
|
|
210 break;
|
|
211
|
|
212 case Tfloat64:
|
|
213 case Timaginary64:
|
|
214 dvalue = value;
|
|
215 pdt = dtnbytes(pdt,8,cast(char*)&dvalue);
|
|
216 break;
|
|
217
|
|
218 case Tfloat80:
|
|
219 case Timaginary80:
|
|
220 evalue = value;
|
|
221 pdt = dtnbytes(pdt,REALSIZE - REALPAD,cast(char*)&evalue);
|
|
222 pdt = dtnbytes(pdt,REALPAD,zeropad.ptr);
|
|
223 assert(REALPAD <= zeropad.sizeof);
|
|
224 break;
|
|
225
|
|
226 default:
|
|
227 writef("%s\n", toChars());
|
|
228 ///type.print();
|
|
229 assert(0);
|
|
230 }
|
|
231 return pdt;
|
|
232 }
|
148
|
233 } |