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