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 {
|
|
171 printf("ty = %d, %d\n", type.ty, t.ty);
|
|
172 if (type.ty == Tenum) {
|
|
173 printf("test1\n");
|
|
174 }
|
72
|
175 ///type.print();
|
|
176 assert(0);
|
|
177 }
|
|
178 break;
|
73
|
179
|
72
|
180 }
|
|
181 break;
|
|
182 }
|
0
|
183 return value;
|
|
184 }
|
|
185
|
72
|
186 override real toReal()
|
0
|
187 {
|
72
|
188 Type t;
|
|
189
|
|
190 toInteger();
|
|
191 t = type.toBasetype();
|
|
192 if (t.ty == Tuns64)
|
|
193 return cast(real)cast(ulong)value;
|
|
194 else
|
0
|
195 return cast(real)cast(long)value;
|
|
196 }
|
|
197
|
72
|
198 override real toImaginary()
|
0
|
199 {
|
|
200 assert(false);
|
|
201 }
|
|
202
|
72
|
203 override Complex!(real) toComplex()
|
0
|
204 {
|
|
205 assert(false);
|
|
206 }
|
|
207
|
72
|
208 override int isConst()
|
0
|
209 {
|
|
210 return 1;
|
|
211 }
|
|
212
|
72
|
213 override bool isBool(bool result)
|
0
|
214 {
|
|
215 return result ? value != 0 : value == 0;
|
|
216 }
|
|
217
|
72
|
218 override MATCH implicitConvTo(Type t)
|
0
|
219 {
|
72
|
220 static if (false) {
|
|
221 printf("IntegerExp.implicitConvTo(this=%s, type=%s, t=%s)\n",
|
|
222 toChars(), type.toChars(), t.toChars());
|
|
223 }
|
|
224
|
|
225 MATCH m = type.implicitConvTo(t);
|
|
226 if (m >= MATCH.MATCHconst)
|
|
227 return m;
|
|
228
|
|
229 TY ty = type.toBasetype().ty;
|
|
230 TY toty = t.toBasetype().ty;
|
|
231
|
|
232 if (m == MATCH.MATCHnomatch && t.ty == TY.Tenum)
|
|
233 goto Lno;
|
|
234
|
|
235 switch (ty)
|
|
236 {
|
|
237 case TY.Tbit:
|
|
238 case TY.Tbool:
|
|
239 value &= 1;
|
|
240 ty = TY.Tint32;
|
|
241 break;
|
|
242
|
|
243 case TY.Tint8:
|
|
244 value = cast(byte)value;
|
|
245 ty = TY.Tint32;
|
|
246 break;
|
|
247
|
|
248 case TY.Tchar:
|
|
249 case TY.Tuns8:
|
|
250 value &= 0xFF;
|
|
251 ty = TY.Tint32;
|
|
252 break;
|
|
253
|
|
254 case TY.Tint16:
|
|
255 value = cast(short)value;
|
|
256 ty = TY.Tint32;
|
|
257 break;
|
|
258
|
|
259 case TY.Tuns16:
|
|
260 case TY.Twchar:
|
|
261 value &= 0xFFFF;
|
|
262 ty = TY.Tint32;
|
|
263 break;
|
|
264
|
|
265 case TY.Tint32:
|
|
266 value = cast(int)value;
|
|
267 break;
|
|
268
|
|
269 case TY.Tuns32:
|
|
270 case TY.Tdchar:
|
|
271 value &= 0xFFFFFFFF;
|
|
272 ty = TY.Tuns32;
|
|
273 break;
|
|
274
|
|
275 default:
|
|
276 break;
|
|
277 }
|
|
278
|
|
279 // Only allow conversion if no change in value
|
|
280 switch (toty)
|
|
281 {
|
|
282 case TY.Tbit:
|
|
283 case TY.Tbool:
|
|
284 if ((value & 1) != value)
|
|
285 goto Lno;
|
|
286 goto Lyes;
|
|
287
|
|
288 case TY.Tint8:
|
|
289 if (cast(byte)value != value)
|
|
290 goto Lno;
|
|
291 goto Lyes;
|
|
292
|
|
293 case TY.Tchar:
|
|
294 case TY.Tuns8:
|
|
295 //printf("value = %llu %llu\n", (dinteger_t)(unsigned char)value, value);
|
|
296 if (cast(ubyte)value != value)
|
|
297 goto Lno;
|
|
298 goto Lyes;
|
|
299
|
|
300 case TY.Tint16:
|
|
301 if (cast(short)value != value)
|
|
302 goto Lno;
|
|
303 goto Lyes;
|
|
304
|
|
305 case TY.Tuns16:
|
|
306 if (cast(ushort)value != value)
|
|
307 goto Lno;
|
|
308 goto Lyes;
|
|
309
|
|
310 case TY.Tint32:
|
|
311 if (ty == TY.Tuns32) {
|
|
312 ;
|
|
313 }
|
|
314 else if (cast(int)value != value) {
|
|
315 goto Lno;
|
|
316 }
|
|
317 goto Lyes;
|
|
318
|
|
319 case TY.Tuns32:
|
|
320 if (ty == TY.Tint32) {
|
|
321 } else if (cast(uint)value != value) {
|
|
322 goto Lno;
|
|
323 }
|
|
324 goto Lyes;
|
|
325
|
|
326 case TY.Tdchar:
|
|
327 if (value > 0x10FFFF) {
|
|
328 goto Lno;
|
|
329 }
|
|
330 goto Lyes;
|
|
331
|
|
332 case TY.Twchar:
|
|
333 if (cast(ushort)value != value) {
|
|
334 goto Lno;
|
|
335 }
|
|
336 goto Lyes;
|
|
337
|
|
338 case TY.Tfloat32:
|
|
339 {
|
|
340 /*volatile*/ float f; ///
|
|
341 if (type.isunsigned()) {
|
|
342 f = cast(float)value;
|
|
343 if (f != value) {
|
|
344 goto Lno;
|
|
345 }
|
|
346 } else {
|
|
347 f = cast(float)cast(long)value;
|
|
348 if (f != cast(long)value) {
|
|
349 goto Lno;
|
|
350 }
|
|
351 }
|
|
352 goto Lyes;
|
|
353 }
|
|
354
|
|
355 case TY.Tfloat64:
|
|
356 {
|
|
357 /*volatile*/ double f; ///
|
|
358 if (type.isunsigned()) {
|
|
359 f = cast(double)value;
|
|
360 if (f != value)
|
|
361 goto Lno;
|
|
362 } else {
|
|
363 f = cast(double)cast(long)value;
|
|
364 if (f != cast(long)value)
|
|
365 goto Lno;
|
|
366 }
|
|
367 goto Lyes;
|
|
368 }
|
|
369
|
|
370 case TY.Tfloat80:
|
|
371 {
|
|
372 /*volatile*/ real f; ///
|
|
373 if (type.isunsigned()) {
|
|
374 f = cast(real)value;
|
|
375 if (f != value)
|
|
376 goto Lno;
|
|
377 } else {
|
|
378 f = cast(real)cast(long)value;
|
|
379 if (f != cast(long)value)
|
|
380 goto Lno;
|
|
381 }
|
|
382 goto Lyes;
|
|
383 }
|
|
384
|
|
385 case TY.Tpointer:
|
|
386 //printf("type = %s\n", type.toBasetype().toChars());
|
|
387 //printf("t = %s\n", t.toBasetype().toChars());
|
|
388 if (ty == TY.Tpointer && type.toBasetype().nextOf().ty == t.toBasetype().nextOf().ty)
|
|
389 {
|
|
390 /* Allow things like:
|
|
391 * const char* P = cast(char *)3;
|
|
392 * char* q = P;
|
|
393 */
|
|
394 goto Lyes;
|
|
395 }
|
|
396 break;
|
|
397
|
|
398 default:
|
|
399 break; ///
|
|
400 }
|
|
401
|
|
402 return Expression.implicitConvTo(t);
|
|
403
|
|
404 Lyes:
|
|
405 //printf("MATCHconvert\n");
|
|
406 return MATCH.MATCHconvert;
|
|
407
|
|
408 Lno:
|
|
409 //printf("MATCHnomatch\n");
|
0
|
410 return MATCH.MATCHnomatch;
|
|
411 }
|
|
412
|
72
|
413 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
414 {
|
72
|
415 long v = toInteger();
|
|
416
|
|
417 if (type)
|
|
418 {
|
|
419 Type t = type;
|
|
420
|
|
421 L1:
|
|
422 switch (t.ty)
|
|
423 {
|
|
424 case TY.Tenum:
|
|
425 {
|
|
426 TypeEnum te = cast(TypeEnum)t;
|
|
427 buf.printf("cast(%s)", te.sym.toChars());
|
|
428 t = te.sym.memtype;
|
|
429 goto L1;
|
|
430 }
|
|
431
|
|
432 case TY.Ttypedef:
|
|
433 {
|
|
434 TypeTypedef tt = cast(TypeTypedef)t;
|
|
435 buf.printf("cast(%s)", tt.sym.toChars());
|
|
436 t = tt.sym.basetype;
|
|
437 goto L1;
|
|
438 }
|
|
439
|
|
440 case TY.Twchar: // BUG: need to cast(wchar)
|
|
441 case TY.Tdchar: // BUG: need to cast(dchar)
|
|
442 if (cast(ulong)v > 0xFF)
|
|
443 {
|
|
444 buf.printf("'\\U%08x'", v);
|
|
445 break;
|
|
446 }
|
|
447 case TY.Tchar:
|
|
448 if (v == '\'')
|
|
449 buf.writestring("'\\''");
|
|
450 else if (isprint(cast(int)v) && v != '\\')
|
|
451 buf.printf("'%c'", cast(int)v);
|
|
452 else
|
|
453 buf.printf("'\\x%02x'", cast(int)v);
|
|
454 break;
|
|
455
|
|
456 case TY.Tint8:
|
|
457 buf.writestring("cast(byte)");
|
|
458 goto L2;
|
|
459
|
|
460 case TY.Tint16:
|
|
461 buf.writestring("cast(short)");
|
|
462 goto L2;
|
|
463
|
|
464 case TY.Tint32:
|
|
465 L2:
|
|
466 buf.printf("%d", cast(int)v);
|
|
467 break;
|
|
468
|
|
469 case TY.Tuns8:
|
|
470 buf.writestring("cast(ubyte)");
|
|
471 goto L3;
|
|
472
|
|
473 case TY.Tuns16:
|
|
474 buf.writestring("cast(ushort)");
|
|
475 goto L3;
|
|
476
|
|
477 case TY.Tuns32:
|
|
478 L3:
|
|
479 buf.printf("%du", cast(uint)v);
|
|
480 break;
|
|
481
|
|
482 case TY.Tint64:
|
|
483 //buf.printf("%jdL", v);
|
|
484 buf.printf("%sL", v);
|
|
485 break;
|
|
486
|
|
487 case TY.Tuns64:
|
|
488 L4:
|
|
489 //buf.printf("%juLU", v);
|
|
490 buf.printf("%sLU", v);
|
|
491 break;
|
|
492
|
|
493 case TY.Tbit:
|
|
494 case TY.Tbool:
|
|
495 buf.writestring(v ? "true" : "false");
|
|
496 break;
|
|
497
|
|
498 case TY.Tpointer:
|
|
499 buf.writestring("cast(");
|
|
500 buf.writestring(t.toChars());
|
|
501 buf.writeByte(')');
|
|
502 if (PTRSIZE == 4)
|
|
503 goto L3;
|
|
504 else if (PTRSIZE == 8)
|
|
505 goto L4;
|
|
506 else
|
|
507 assert(0);
|
|
508
|
|
509 default:
|
|
510 /* This can happen if errors, such as
|
|
511 * the type is painted on like in fromConstInitializer().
|
|
512 */
|
|
513 if (!global.errors)
|
|
514 {
|
|
515 debug {
|
|
516 writef("%s\n", t.toChars());
|
|
517 }
|
|
518 assert(0);
|
|
519 }
|
|
520 break;
|
|
521 }
|
|
522 }
|
|
523 else if (v & 0x8000000000000000L)
|
|
524 buf.printf("0x%jx", v);
|
|
525 else
|
0
|
526 buf.printf("%jd", v);
|
|
527 }
|
|
528
|
72
|
529 override void toMangleBuffer(OutBuffer buf)
|
0
|
530 {
|
72
|
531 if (cast(long)value < 0)
|
|
532 buf.printf("N%d", -value);
|
|
533 else
|
|
534 buf.printf("%d", value);
|
0
|
535 }
|
|
536
|
72
|
537 override Expression toLvalue(Scope sc, Expression e)
|
0
|
538 {
|
72
|
539 if (!e)
|
|
540 e = this;
|
|
541 else if (!loc.filename)
|
|
542 loc = e.loc;
|
|
543 e.error("constant %s is not an lvalue", e.toChars());
|
0
|
544 return this;
|
|
545 }
|
|
546
|
72
|
547 override elem* toElem(IRState* irs)
|
0
|
548 {
|
72
|
549 elem* e = el_long(type.totym(), value);
|
|
550 el_setLoc(e,loc);
|
0
|
551 return e;
|
|
552 }
|
|
553
|
72
|
554 override dt_t** toDt(dt_t** pdt)
|
0
|
555 {
|
72
|
556 //printf("IntegerExp.toDt() %d\n", op);
|
|
557 uint sz = cast(uint)type.size();
|
|
558 if (value == 0)
|
|
559 pdt = dtnzeros(pdt, sz);
|
|
560 else
|
|
561 pdt = dtnbytes(pdt, sz, cast(char*)&value);
|
|
562
|
0
|
563 return pdt;
|
|
564 }
|
|
565 }
|
|
566
|