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