72
|
1 module dmd.IntegerExp;
|
|
2
|
114
|
3 import dmd.common;
|
72
|
4 import dmd.Expression;
|
|
5 import dmd.backend.elem;
|
|
6 import dmd.TY;
|
|
7 import dmd.TypeEnum;
|
|
8 import dmd.TypeTypedef;
|
|
9 import dmd.Global;
|
|
10 import dmd.InterState;
|
|
11 import dmd.MATCH;
|
|
12 import dmd.Type;
|
|
13 import dmd.OutBuffer;
|
|
14 import dmd.Loc;
|
|
15 import dmd.Scope;
|
|
16 import dmd.IntRange;
|
|
17 import dmd.IRState;
|
|
18 import dmd.HdrGenState;
|
|
19 import dmd.TOK;
|
|
20 import dmd.Complex;
|
|
21
|
|
22 import dmd.backend.dt_t;
|
|
23 import dmd.backend.Util;
|
|
24
|
|
25 import core.stdc.ctype : isprint;
|
73
|
26 import std.stdio;
|
72
|
27
|
0
|
28 class IntegerExp : Expression
|
|
29 {
|
|
30 ulong value;
|
|
31
|
|
32 this(Loc loc, ulong value, Type type)
|
|
33 {
|
72
|
34 super(loc, TOK.TOKint64, IntegerExp.sizeof);
|
|
35
|
|
36 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type.toChars() : "");
|
|
37 if (type && !type.isscalar())
|
|
38 {
|
|
39 //printf("%s, loc = %d\n", toChars(), loc.linnum);
|
|
40 error("integral constant must be scalar type, not %s", type.toChars());
|
|
41 type = Type.terror;
|
|
42 }
|
|
43 this.type = type;
|
0
|
44 this.value = value;
|
|
45 }
|
|
46
|
|
47 this(ulong value)
|
72
|
48 {
|
0
|
49 super(Loc(0), TOK.TOKint64, IntegerExp.sizeof);
|
72
|
50 this.type = Type.tint32;
|
0
|
51 this.value = value;
|
|
52 }
|
|
53
|
72
|
54 override bool equals(Object o)
|
0
|
55 {
|
72
|
56 IntegerExp ne;
|
|
57
|
|
58 if (this == o ||
|
|
59 ((cast(Expression)o).op == TOKint64 &&
|
|
60 ((ne = cast(IntegerExp)o), type.toHeadMutable().equals(ne.type.toHeadMutable())) &&
|
|
61 value == ne.value)
|
|
62 )
|
|
63 return 1;
|
|
64
|
0
|
65 return 0;
|
|
66 }
|
|
67
|
72
|
68 override Expression semantic(Scope sc)
|
0
|
69 {
|
72
|
70 if (!type)
|
|
71 {
|
|
72 // Determine what the type of this number is
|
|
73 ulong number = value;
|
|
74
|
|
75 if (number & 0x8000000000000000)
|
|
76 type = Type.tuns64;
|
|
77 else if (number & 0xFFFFFFFF80000000)
|
|
78 type = Type.tint64;
|
|
79 else
|
|
80 type = Type.tint32;
|
|
81 }
|
|
82 else
|
|
83 { if (!type.deco)
|
|
84 type = type.semantic(loc, sc);
|
|
85 }
|
0
|
86 return this;
|
|
87 }
|
|
88
|
72
|
89 override Expression interpret(InterState istate)
|
0
|
90 {
|
72
|
91 version (LOG) {
|
|
92 printf("IntegerExp.interpret() %s\n", toChars());
|
|
93 }
|
63
|
94 return this;
|
0
|
95 }
|
|
96
|
72
|
97 override string toChars()
|
0
|
98 {
|
72
|
99 static if (true) {
|
|
100 return Expression.toChars();
|
|
101 } else {
|
|
102 static char buffer[value.sizeof * 3 + 1];
|
|
103 int len = sprintf(buffer.ptr, "%jd", value);
|
|
104 return buffer[0..len].idup;
|
0
|
105 }
|
|
106 }
|
|
107
|
72
|
108 override void dump(int indent)
|
0
|
109 {
|
|
110 assert(false);
|
|
111 }
|
|
112
|
72
|
113 override IntRange getIntRange()
|
0
|
114 {
|
72
|
115 IntRange ir;
|
|
116 ir.imin = value & type.sizemask();
|
|
117 ir.imax = ir.imin;
|
53
|
118 return ir;
|
0
|
119 }
|
|
120
|
72
|
121 override ulong toInteger()
|
0
|
122 {
|
72
|
123 Type t;
|
|
124
|
|
125 t = type;
|
|
126 while (t)
|
|
127 {
|
|
128 switch (t.ty)
|
|
129 {
|
|
130 case TY.Tbit:
|
|
131 case TY.Tbool: value = (value != 0); break;
|
|
132 case TY.Tint8: value = cast(byte) value; break;
|
|
133 case TY.Tchar:
|
|
134 case TY.Tuns8: value = cast(ubyte) value; break;
|
|
135 case TY.Tint16: value = cast(short) value; break;
|
|
136 case TY.Twchar:
|
|
137 case TY.Tuns16: value = cast(ushort)value; break;
|
|
138 case TY.Tint32: value = cast(int) value; break;
|
|
139 case TY.Tdchar:
|
|
140 case TY.Tuns32: value = cast(uint) value; break;
|
|
141 case TY.Tint64: value = cast(long) value; break;
|
|
142 case TY.Tuns64: value = cast(ulong) value; break;
|
|
143 case TY.Tpointer:
|
|
144 if (PTRSIZE == 4)
|
|
145 value = cast(uint) value;
|
|
146 else if (PTRSIZE == 8)
|
|
147 value = cast(ulong) value;
|
|
148 else
|
|
149 assert(0);
|
|
150 break;
|
|
151
|
|
152 case TY.Tenum:
|
|
153 {
|
|
154 TypeEnum te = cast(TypeEnum)t;
|
|
155 t = te.sym.memtype;
|
|
156 continue;
|
|
157 }
|
|
158
|
|
159 case TY.Ttypedef:
|
|
160 {
|
|
161 TypeTypedef tt = cast(TypeTypedef)t;
|
|
162 t = tt.sym.basetype;
|
|
163 continue;
|
|
164 }
|
|
165
|
|
166 default:
|
|
167 /* This can happen if errors, such as
|
|
168 * the type is painted on like in fromConstInitializer().
|
|
169 */
|
|
170 if (!global.errors)
|
73
|
171 {
|
109
|
172 writef("%s %p\n", type.toChars(), type);
|
72
|
173 assert(0);
|
|
174 }
|
|
175 break;
|
73
|
176
|
72
|
177 }
|
|
178 break;
|
|
179 }
|
0
|
180 return value;
|
|
181 }
|
|
182
|
72
|
183 override real toReal()
|
0
|
184 {
|
72
|
185 Type t;
|
|
186
|
|
187 toInteger();
|
|
188 t = type.toBasetype();
|
|
189 if (t.ty == Tuns64)
|
|
190 return cast(real)cast(ulong)value;
|
|
191 else
|
0
|
192 return cast(real)cast(long)value;
|
|
193 }
|
|
194
|
72
|
195 override real toImaginary()
|
0
|
196 {
|
|
197 assert(false);
|
|
198 }
|
|
199
|
72
|
200 override Complex!(real) toComplex()
|
0
|
201 {
|
|
202 assert(false);
|
|
203 }
|
|
204
|
72
|
205 override int isConst()
|
0
|
206 {
|
|
207 return 1;
|
|
208 }
|
|
209
|
72
|
210 override bool isBool(bool result)
|
0
|
211 {
|
135
|
212 int r = toInteger() != 0;
|
|
213 return cast(bool)(result ? r : !r);
|
0
|
214 }
|
|
215
|
72
|
216 override MATCH implicitConvTo(Type t)
|
0
|
217 {
|
72
|
218 static if (false) {
|
|
219 printf("IntegerExp.implicitConvTo(this=%s, type=%s, t=%s)\n",
|
|
220 toChars(), type.toChars(), t.toChars());
|
|
221 }
|
|
222
|
|
223 MATCH m = type.implicitConvTo(t);
|
|
224 if (m >= MATCH.MATCHconst)
|
|
225 return m;
|
|
226
|
|
227 TY ty = type.toBasetype().ty;
|
|
228 TY toty = t.toBasetype().ty;
|
|
229
|
|
230 if (m == MATCH.MATCHnomatch && t.ty == TY.Tenum)
|
|
231 goto Lno;
|
|
232
|
|
233 switch (ty)
|
|
234 {
|
|
235 case TY.Tbit:
|
|
236 case TY.Tbool:
|
|
237 value &= 1;
|
|
238 ty = TY.Tint32;
|
|
239 break;
|
|
240
|
|
241 case TY.Tint8:
|
|
242 value = cast(byte)value;
|
|
243 ty = TY.Tint32;
|
|
244 break;
|
|
245
|
|
246 case TY.Tchar:
|
|
247 case TY.Tuns8:
|
|
248 value &= 0xFF;
|
|
249 ty = TY.Tint32;
|
|
250 break;
|
|
251
|
|
252 case TY.Tint16:
|
|
253 value = cast(short)value;
|
|
254 ty = TY.Tint32;
|
|
255 break;
|
|
256
|
|
257 case TY.Tuns16:
|
|
258 case TY.Twchar:
|
|
259 value &= 0xFFFF;
|
|
260 ty = TY.Tint32;
|
|
261 break;
|
|
262
|
|
263 case TY.Tint32:
|
|
264 value = cast(int)value;
|
|
265 break;
|
|
266
|
|
267 case TY.Tuns32:
|
|
268 case TY.Tdchar:
|
|
269 value &= 0xFFFFFFFF;
|
|
270 ty = TY.Tuns32;
|
|
271 break;
|
|
272
|
|
273 default:
|
|
274 break;
|
|
275 }
|
|
276
|
|
277 // Only allow conversion if no change in value
|
|
278 switch (toty)
|
|
279 {
|
|
280 case TY.Tbit:
|
|
281 case TY.Tbool:
|
|
282 if ((value & 1) != value)
|
|
283 goto Lno;
|
|
284 goto Lyes;
|
|
285
|
|
286 case TY.Tint8:
|
|
287 if (cast(byte)value != value)
|
|
288 goto Lno;
|
|
289 goto Lyes;
|
|
290
|
|
291 case TY.Tchar:
|
|
292 case TY.Tuns8:
|
|
293 //printf("value = %llu %llu\n", (dinteger_t)(unsigned char)value, value);
|
|
294 if (cast(ubyte)value != value)
|
|
295 goto Lno;
|
|
296 goto Lyes;
|
|
297
|
|
298 case TY.Tint16:
|
|
299 if (cast(short)value != value)
|
|
300 goto Lno;
|
|
301 goto Lyes;
|
|
302
|
|
303 case TY.Tuns16:
|
|
304 if (cast(ushort)value != value)
|
|
305 goto Lno;
|
|
306 goto Lyes;
|
|
307
|
|
308 case TY.Tint32:
|
|
309 if (ty == TY.Tuns32) {
|
|
310 ;
|
|
311 }
|
|
312 else if (cast(int)value != value) {
|
|
313 goto Lno;
|
|
314 }
|
|
315 goto Lyes;
|
|
316
|
|
317 case TY.Tuns32:
|
|
318 if (ty == TY.Tint32) {
|
|
319 } else if (cast(uint)value != value) {
|
|
320 goto Lno;
|
|
321 }
|
|
322 goto Lyes;
|
|
323
|
|
324 case TY.Tdchar:
|
|
325 if (value > 0x10FFFF) {
|
|
326 goto Lno;
|
|
327 }
|
|
328 goto Lyes;
|
|
329
|
|
330 case TY.Twchar:
|
|
331 if (cast(ushort)value != value) {
|
|
332 goto Lno;
|
|
333 }
|
|
334 goto Lyes;
|
|
335
|
|
336 case TY.Tfloat32:
|
|
337 {
|
|
338 /*volatile*/ float f; ///
|
|
339 if (type.isunsigned()) {
|
|
340 f = cast(float)value;
|
|
341 if (f != value) {
|
|
342 goto Lno;
|
|
343 }
|
|
344 } else {
|
|
345 f = cast(float)cast(long)value;
|
|
346 if (f != cast(long)value) {
|
|
347 goto Lno;
|
|
348 }
|
|
349 }
|
|
350 goto Lyes;
|
|
351 }
|
|
352
|
|
353 case TY.Tfloat64:
|
|
354 {
|
|
355 /*volatile*/ double f; ///
|
|
356 if (type.isunsigned()) {
|
|
357 f = cast(double)value;
|
|
358 if (f != value)
|
|
359 goto Lno;
|
|
360 } else {
|
|
361 f = cast(double)cast(long)value;
|
|
362 if (f != cast(long)value)
|
|
363 goto Lno;
|
|
364 }
|
|
365 goto Lyes;
|
|
366 }
|
|
367
|
|
368 case TY.Tfloat80:
|
|
369 {
|
|
370 /*volatile*/ real f; ///
|
|
371 if (type.isunsigned()) {
|
|
372 f = cast(real)value;
|
|
373 if (f != value)
|
|
374 goto Lno;
|
|
375 } else {
|
|
376 f = cast(real)cast(long)value;
|
|
377 if (f != cast(long)value)
|
|
378 goto Lno;
|
|
379 }
|
|
380 goto Lyes;
|
|
381 }
|
|
382
|
|
383 case TY.Tpointer:
|
|
384 //printf("type = %s\n", type.toBasetype().toChars());
|
|
385 //printf("t = %s\n", t.toBasetype().toChars());
|
|
386 if (ty == TY.Tpointer && type.toBasetype().nextOf().ty == t.toBasetype().nextOf().ty)
|
|
387 {
|
|
388 /* Allow things like:
|
|
389 * const char* P = cast(char *)3;
|
|
390 * char* q = P;
|
|
391 */
|
|
392 goto Lyes;
|
|
393 }
|
|
394 break;
|
|
395
|
|
396 default:
|
|
397 break; ///
|
|
398 }
|
|
399
|
|
400 return Expression.implicitConvTo(t);
|
|
401
|
|
402 Lyes:
|
|
403 //printf("MATCHconvert\n");
|
|
404 return MATCH.MATCHconvert;
|
|
405
|
|
406 Lno:
|
|
407 //printf("MATCHnomatch\n");
|
0
|
408 return MATCH.MATCHnomatch;
|
|
409 }
|
|
410
|
72
|
411 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
412 {
|
72
|
413 long v = toInteger();
|
|
414
|
|
415 if (type)
|
|
416 {
|
|
417 Type t = type;
|
|
418
|
|
419 L1:
|
|
420 switch (t.ty)
|
|
421 {
|
|
422 case TY.Tenum:
|
|
423 {
|
|
424 TypeEnum te = cast(TypeEnum)t;
|
|
425 buf.printf("cast(%s)", te.sym.toChars());
|
|
426 t = te.sym.memtype;
|
|
427 goto L1;
|
|
428 }
|
|
429
|
|
430 case TY.Ttypedef:
|
|
431 {
|
|
432 TypeTypedef tt = cast(TypeTypedef)t;
|
|
433 buf.printf("cast(%s)", tt.sym.toChars());
|
|
434 t = tt.sym.basetype;
|
|
435 goto L1;
|
|
436 }
|
|
437
|
|
438 case TY.Twchar: // BUG: need to cast(wchar)
|
|
439 case TY.Tdchar: // BUG: need to cast(dchar)
|
|
440 if (cast(ulong)v > 0xFF)
|
|
441 {
|
|
442 buf.printf("'\\U%08x'", v);
|
|
443 break;
|
|
444 }
|
|
445 case TY.Tchar:
|
|
446 if (v == '\'')
|
|
447 buf.writestring("'\\''");
|
|
448 else if (isprint(cast(int)v) && v != '\\')
|
|
449 buf.printf("'%c'", cast(int)v);
|
|
450 else
|
|
451 buf.printf("'\\x%02x'", cast(int)v);
|
|
452 break;
|
|
453
|
|
454 case TY.Tint8:
|
|
455 buf.writestring("cast(byte)");
|
|
456 goto L2;
|
|
457
|
|
458 case TY.Tint16:
|
|
459 buf.writestring("cast(short)");
|
|
460 goto L2;
|
|
461
|
|
462 case TY.Tint32:
|
|
463 L2:
|
|
464 buf.printf("%d", cast(int)v);
|
|
465 break;
|
|
466
|
|
467 case TY.Tuns8:
|
|
468 buf.writestring("cast(ubyte)");
|
|
469 goto L3;
|
|
470
|
|
471 case TY.Tuns16:
|
|
472 buf.writestring("cast(ushort)");
|
|
473 goto L3;
|
|
474
|
|
475 case TY.Tuns32:
|
|
476 L3:
|
|
477 buf.printf("%du", cast(uint)v);
|
|
478 break;
|
|
479
|
|
480 case TY.Tint64:
|
|
481 //buf.printf("%jdL", v);
|
|
482 buf.printf("%sL", v);
|
|
483 break;
|
|
484
|
|
485 case TY.Tuns64:
|
|
486 L4:
|
|
487 //buf.printf("%juLU", v);
|
|
488 buf.printf("%sLU", v);
|
|
489 break;
|
|
490
|
|
491 case TY.Tbit:
|
|
492 case TY.Tbool:
|
|
493 buf.writestring(v ? "true" : "false");
|
|
494 break;
|
|
495
|
|
496 case TY.Tpointer:
|
|
497 buf.writestring("cast(");
|
|
498 buf.writestring(t.toChars());
|
|
499 buf.writeByte(')');
|
|
500 if (PTRSIZE == 4)
|
|
501 goto L3;
|
|
502 else if (PTRSIZE == 8)
|
|
503 goto L4;
|
|
504 else
|
|
505 assert(0);
|
|
506
|
|
507 default:
|
|
508 /* This can happen if errors, such as
|
|
509 * the type is painted on like in fromConstInitializer().
|
|
510 */
|
|
511 if (!global.errors)
|
|
512 {
|
|
513 debug {
|
|
514 writef("%s\n", t.toChars());
|
|
515 }
|
|
516 assert(0);
|
|
517 }
|
|
518 break;
|
|
519 }
|
|
520 }
|
|
521 else if (v & 0x8000000000000000L)
|
|
522 buf.printf("0x%jx", v);
|
|
523 else
|
0
|
524 buf.printf("%jd", v);
|
|
525 }
|
|
526
|
72
|
527 override void toMangleBuffer(OutBuffer buf)
|
0
|
528 {
|
72
|
529 if (cast(long)value < 0)
|
|
530 buf.printf("N%d", -value);
|
|
531 else
|
|
532 buf.printf("%d", value);
|
0
|
533 }
|
|
534
|
72
|
535 override Expression toLvalue(Scope sc, Expression e)
|
0
|
536 {
|
72
|
537 if (!e)
|
|
538 e = this;
|
|
539 else if (!loc.filename)
|
|
540 loc = e.loc;
|
|
541 e.error("constant %s is not an lvalue", e.toChars());
|
0
|
542 return this;
|
|
543 }
|
|
544
|
72
|
545 override elem* toElem(IRState* irs)
|
0
|
546 {
|
72
|
547 elem* e = el_long(type.totym(), value);
|
|
548 el_setLoc(e,loc);
|
0
|
549 return e;
|
|
550 }
|
|
551
|
72
|
552 override dt_t** toDt(dt_t** pdt)
|
0
|
553 {
|
72
|
554 //printf("IntegerExp.toDt() %d\n", op);
|
|
555 uint sz = cast(uint)type.size();
|
|
556 if (value == 0)
|
|
557 pdt = dtnzeros(pdt, sz);
|
|
558 else
|
|
559 pdt = dtnbytes(pdt, sz, cast(char*)&value);
|
|
560
|
0
|
561 return pdt;
|
|
562 }
|
|
563 }
|
|
564
|