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