forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.d
846 lines (741 loc) · 25.6 KB
/
builtin.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/**
* Implement CTFE for intrinsic (builtin) functions.
*
* Currently includes functions from `std.math`, `core.math` and `core.bitop`.
*
* Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/builtin.d, _builtin.d)
* Documentation: https://dlang.org/phobos/dmd_builtin.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/builtin.d
*/
module dmd.builtin;
import dmd.arraytypes;
import dmd.astenums;
import dmd.dmangle;
import dmd.errors;
import dmd.expression;
import dmd.func;
import dmd.globals;
import dmd.location;
import dmd.mtype;
import dmd.root.ctfloat;
import dmd.tokens;
import dmd.id;
static import core.bitop;
/**********************************
* Determine if function is a builtin one that we can
* evaluate at compile time.
*/
public extern (C++) BUILTIN isBuiltin(FuncDeclaration fd)
{
if (fd.builtin == BUILTIN.unknown)
{
fd.builtin = determine_builtin(fd);
}
return fd.builtin;
}
/**************************************
* Evaluate builtin function.
* Return result; NULL if cannot evaluate it.
*/
public extern (C++) Expression eval_builtin(const ref Loc loc, FuncDeclaration fd, Expressions* arguments)
{
if (fd.builtin == BUILTIN.unimp)
return null;
switch (fd.builtin)
{
foreach(e; __traits(allMembers, BUILTIN))
{
static if (e == "unknown")
case BUILTIN.unknown: assert(false);
else static if (IN_LLVM && e.length > 5 && e[0..5] == "llvm_")
mixin("case BUILTIN."~e~": return eval_llvm"~e[5..$]~"(loc, fd, (*arguments)[]);");
else
mixin("case BUILTIN."~e~": return eval_"~e~"(loc, fd, (*arguments)[]);");
}
default: assert(0);
}
}
private:
/**
* Handler for evaluating builtins during CTFE.
*
* Params:
* loc = The call location, for error reporting.
* fd = The callee declaration, e.g. to disambiguate between different overloads
* in a single handler (LDC).
* arguments = The function call arguments.
* Returns:
* An Expression containing the return value of the call.
*/
BUILTIN determine_builtin(FuncDeclaration func)
{
auto fd = func.toAliasFunc();
if (fd.isDeprecated())
return BUILTIN.unimp;
version (IN_LLVM)
{
import dmd.root.string : toDString;
import gen.dpragma : LDCPragma;
if (func.llvmInternal == LDCPragma.LLVMintrinsic)
{
const(char)[] name = func.mangleOverride;
if (name.length < 7 || name[0..5] != "llvm.")
return BUILTIN.unimp;
// find next "." after "llvm."
size_t end = 0;
foreach (i; 6 .. name.length)
{
if (name[i] == '.')
{
end = i;
break;
}
}
if (end == 0)
return BUILTIN.unimp;
name = name[5 .. end]; // e.g., "llvm.sin.f32" => "sin"
switch (name)
{
foreach (e; __traits(allMembers, BUILTIN))
{
static if (e.length > 5 && e[0..5] == "llvm_")
mixin(`case "`~e[5..$]~`": return BUILTIN.`~e~";");
}
default: return BUILTIN.unimp;
}
}
}
auto m = fd.getModule();
if (!m || !m.md)
return BUILTIN.unimp;
const md = m.md;
// Look for core.math, core.bitop, std.math, and std.math.<package>
const id2 = (md.packages.length == 2) ? md.packages[1] : md.id;
if (id2 != Id.math && id2 != Id.bitop && id2 != Id.builtinsModuleName)
return BUILTIN.unimp;
if (md.packages.length != 1 && !(md.packages.length == 2 && id2 == Id.math))
return BUILTIN.unimp;
const id1 = md.packages[0];
if (id1 != Id.core && id1 != Id.std)
return BUILTIN.unimp;
const id3 = fd.ident;
if (id1 == Id.core && id2 == Id.bitop)
{
if (id3 == Id.bsf) return BUILTIN.bsf;
if (id3 == Id.bsr) return BUILTIN.bsr;
if (id3 == Id.bswap) return BUILTIN.bswap;
if (id3 == Id._popcnt) return BUILTIN.popcnt;
return BUILTIN.unimp;
}
if (id1 == Id.core && id2 == Id.builtinsModuleName)
{
if (id3 == Id.ctfeWrite) return BUILTIN.ctfeWrite;
return BUILTIN.unimp;
}
// Math
if (id3 == Id.sin) return BUILTIN.sin;
if (id3 == Id.cos) return BUILTIN.cos;
if (id3 == Id.tan) return BUILTIN.tan;
if (id3 == Id.atan2) return BUILTIN.unimp; // N.B unimplmeneted
if (id3 == Id._sqrt) return BUILTIN.sqrt;
if (id3 == Id.fabs) return BUILTIN.fabs;
if (id3 == Id.exp) return BUILTIN.exp;
if (id3 == Id.expm1) return BUILTIN.expm1;
if (id3 == Id.exp2) return BUILTIN.exp2;
version (IN_LLVM)
{
// Our implementations in CTFloat fall back to a generic version in case
// host compiler's druntime doesn't provide core.math.yl2x[p1] (GDC,
// non-x86 hosts). Not providing yl2x[p1] for CTFE would significantly
// limit CTFE-ability of std.math for x86 targets.
if (id3 == Id.yl2x) return BUILTIN.yl2x;
if (id3 == Id.yl2xp1) return BUILTIN.yl2xp1;
}
else
{
if (id3 == Id.yl2x) return CTFloat.yl2x_supported ? BUILTIN.yl2x : BUILTIN.unimp;
if (id3 == Id.yl2xp1) return CTFloat.yl2xp1_supported ? BUILTIN.yl2xp1 : BUILTIN.unimp;
}
if (id3 == Id.log) return BUILTIN.log;
if (id3 == Id.log2) return BUILTIN.log2;
if (id3 == Id.log10) return BUILTIN.log10;
if (id3 == Id.ldexp) return BUILTIN.ldexp;
if (id3 == Id.round) return BUILTIN.round;
if (id3 == Id.floor) return BUILTIN.floor;
if (id3 == Id.ceil) return BUILTIN.ceil;
if (id3 == Id.trunc) return BUILTIN.trunc;
if (id3 == Id.fmin) return BUILTIN.fmin;
if (id3 == Id.fmax) return BUILTIN.fmax;
if (id3 == Id.fma) return BUILTIN.fma;
if (id3 == Id.copysign) return BUILTIN.copysign;
if (id3 == Id.isnan) return BUILTIN.isnan;
if (id3 == Id.isInfinity) return BUILTIN.isinfinity;
if (id3 == Id.isfinite) return BUILTIN.isfinite;
// Only match pow(fp,fp) where fp is a floating point type
if (id3 == Id._pow)
{
if ((*fd.parameters)[0].type.isfloating() &&
(*fd.parameters)[1].type.isfloating())
return BUILTIN.pow;
return BUILTIN.unimp;
}
if (id3 != Id.toPrec)
return BUILTIN.unimp;
const(char)* me = mangleExact(fd);
final switch (me["_D4core4math__T6toPrecHT".length])
{
case 'd': return BUILTIN.toPrecDouble;
case 'e': return BUILTIN.toPrecReal;
case 'f': return BUILTIN.toPrecFloat;
}
}
Expression eval_unimp(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
return null;
}
Expression eval_ctfeWrite(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
import core.stdc.stdio: fprintf, stderr;
import dmd.expression: CTFEExp;
import dmd.ctfeexpr: resolveSlice;
Expression e = arguments[0];
const se = resolveSlice(e).toStringExp();
assert(se);
const slice = se.peekString();
fprintf(stderr, "%.*s", cast(int)slice.length, slice.ptr);
return CTFEExp.voidexp;
}
Expression eval_sin(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.sin(arg0.toReal()), arg0.type);
}
Expression eval_cos(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.cos(arg0.toReal()), arg0.type);
}
Expression eval_tan(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.tan(arg0.toReal()), arg0.type);
}
Expression eval_sqrt(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.sqrt(arg0.toReal()), arg0.type);
}
Expression eval_fabs(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.fabs(arg0.toReal()), arg0.type);
}
Expression eval_ldexp(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.int64);
return new RealExp(loc, CTFloat.ldexp(arg0.toReal(), cast(int) arg1.toInteger()), arg0.type);
}
Expression eval_log(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.log(arg0.toReal()), arg0.type);
}
Expression eval_log2(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.log2(arg0.toReal()), arg0.type);
}
Expression eval_log10(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.log10(arg0.toReal()), arg0.type);
}
Expression eval_exp(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.exp(arg0.toReal()), arg0.type);
}
Expression eval_expm1(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.expm1(arg0.toReal()), arg0.type);
}
Expression eval_exp2(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.exp2(arg0.toReal()), arg0.type);
}
Expression eval_round(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.round(arg0.toReal()), arg0.type);
}
Expression eval_floor(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.floor(arg0.toReal()), arg0.type);
}
Expression eval_ceil(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.ceil(arg0.toReal()), arg0.type);
}
Expression eval_trunc(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.trunc(arg0.toReal()), arg0.type);
}
Expression eval_copysign(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
return new RealExp(loc, CTFloat.copysign(arg0.toReal(), arg1.toReal()), arg0.type);
}
Expression eval_pow(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
return new RealExp(loc, CTFloat.pow(arg0.toReal(), arg1.toReal()), arg0.type);
}
Expression eval_fmin(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
return new RealExp(loc, CTFloat.fmin(arg0.toReal(), arg1.toReal()), arg0.type);
}
Expression eval_fmax(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
return new RealExp(loc, CTFloat.fmax(arg0.toReal(), arg1.toReal()), arg0.type);
}
Expression eval_fma(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
Expression arg2 = arguments[2];
assert(arg2.op == EXP.float64);
return new RealExp(loc, CTFloat.fma(arg0.toReal(), arg1.toReal(), arg2.toReal()), arg0.type);
}
Expression eval_isnan(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return IntegerExp.createBool(CTFloat.isNaN(arg0.toReal()));
}
Expression eval_isinfinity(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return IntegerExp.createBool(CTFloat.isInfinity(arg0.toReal()));
}
Expression eval_isfinite(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
const value = !CTFloat.isNaN(arg0.toReal()) && !CTFloat.isInfinity(arg0.toReal());
return IntegerExp.createBool(value);
}
Expression eval_bsf(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.int64);
uinteger_t n = arg0.toInteger();
if (n == 0)
error(loc, "`bsf(0)` is undefined");
return new IntegerExp(loc, core.bitop.bsf(n), Type.tint32);
}
Expression eval_bsr(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.int64);
uinteger_t n = arg0.toInteger();
if (n == 0)
error(loc, "`bsr(0)` is undefined");
return new IntegerExp(loc, core.bitop.bsr(n), Type.tint32);
}
Expression eval_bswap(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.int64);
uinteger_t n = arg0.toInteger();
TY ty = arg0.type.toBasetype().ty;
if (ty == Tint64 || ty == Tuns64)
return new IntegerExp(loc, core.bitop.bswap(cast(ulong) n), arg0.type);
else
return new IntegerExp(loc, core.bitop.bswap(cast(uint) n), arg0.type);
}
Expression eval_popcnt(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.int64);
uinteger_t n = arg0.toInteger();
return new IntegerExp(loc, core.bitop.popcnt(n), Type.tint32);
}
Expression eval_yl2x(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
const x = arg0.toReal();
const y = arg1.toReal();
real_t result = CTFloat.zero;
CTFloat.yl2x(&x, &y, &result);
return new RealExp(loc, result, arg0.type);
}
Expression eval_yl2xp1(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
const x = arg0.toReal();
const y = arg1.toReal();
real_t result = CTFloat.zero;
CTFloat.yl2xp1(&x, &y, &result);
return new RealExp(loc, result, arg0.type);
}
Expression eval_toPrecFloat(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
float f = cast(real)arg0.toReal();
return new RealExp(loc, real_t(f), Type.tfloat32);
}
Expression eval_toPrecDouble(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
double d = cast(real)arg0.toReal();
return new RealExp(loc, real_t(d), Type.tfloat64);
}
Expression eval_toPrecReal(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Expression arg0 = arguments[0];
return new RealExp(loc, arg0.toReal(), Type.tfloat80);
}
// These built-ins are reserved for GDC and LDC.
Expression eval_gcc(Loc, FuncDeclaration, Expression[])
{
assert(0);
}
Expression eval_llvm(Loc, FuncDeclaration, Expression[])
{
assert(0);
}
version (IN_LLVM)
{
private Type getTypeOfOverloadedIntrinsic(FuncDeclaration fd)
{
import dmd.dtemplate : TemplateInstance;
// Depending on the state of the code generation we have to look at
// the template instance or the function declaration.
assert(fd.parent && "function declaration requires parent");
TemplateInstance tinst = fd.parent.isTemplateInstance();
if (tinst)
{
// See DtoOverloadedIntrinsicName
assert(tinst.tdtypes.length == 1);
return cast(Type) tinst.tdtypes[0];
}
else
{
assert(fd.type.ty == Tfunction);
TypeFunction tf = cast(TypeFunction) fd.type;
assert(tf.parameterList.length >= 1);
return (*tf.parameterList.parameters)[0].type;
}
}
private int getBitsizeOfType(Loc loc, Type type)
{
switch (type.toBasetype().ty)
{
case Tint64:
case Tuns64: return 64;
case Tint32:
case Tuns32: return 32;
case Tint16:
case Tuns16: return 16;
case Tint128:
case Tuns128:
error(loc, "cent/ucent not supported");
break;
default:
error(loc, "unsupported type");
break;
}
return 32; // in case of error
}
Expression eval_llvmsin(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.sin(arg0.toReal()), type);
}
Expression eval_llvmcos(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.cos(arg0.toReal()), type);
}
Expression eval_llvmsqrt(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.sqrt(arg0.toReal()), type);
}
Expression eval_llvmexp(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.exp(arg0.toReal()), type);
}
Expression eval_llvmexp2(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.exp2(arg0.toReal()), type);
}
Expression eval_llvmlog(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.log(arg0.toReal()), type);
}
Expression eval_llvmlog2(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.log2(arg0.toReal()), type);
}
Expression eval_llvmlog10(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.log10(arg0.toReal()), type);
}
Expression eval_llvmfabs(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.fabs(arg0.toReal()), type);
}
Expression eval_llvmminnum(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
return new RealExp(loc, CTFloat.fmin(arg0.toReal(), arg1.toReal()), type);
}
Expression eval_llvmmaxnum(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
return new RealExp(loc, CTFloat.fmax(arg0.toReal(), arg1.toReal()), type);
}
Expression eval_llvmfloor(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.floor(arg0.toReal()), type);
}
Expression eval_llvmceil(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.ceil(arg0.toReal()), type);
}
Expression eval_llvmtrunc(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.trunc(arg0.toReal()), type);
}
Expression eval_llvmrint(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.rint(arg0.toReal()), type);
}
Expression eval_llvmnearbyint(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.nearbyint(arg0.toReal()), type);
}
Expression eval_llvmround(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
return new RealExp(loc, CTFloat.round(arg0.toReal()), type);
}
Expression eval_llvmfma(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
Expression arg2 = arguments[2];
assert(arg2.op == EXP.float64);
return new RealExp(loc, CTFloat.fma(arg0.toReal(), arg1.toReal(), arg2.toReal()), type);
}
Expression eval_llvmcopysign(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.float64);
Expression arg1 = arguments[1];
assert(arg1.op == EXP.float64);
return new RealExp(loc, CTFloat.copysign(arg0.toReal(), arg1.toReal()), type);
}
Expression eval_llvmbswap(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.int64);
uinteger_t n = arg0.toInteger();
enum ulong BYTEMASK = 0x00FF00FF00FF00FF;
enum ulong SHORTMASK = 0x0000FFFF0000FFFF;
enum ulong INTMASK = 0x00000000FFFFFFFF;
switch (type.toBasetype().ty)
{
case Tint64:
case Tuns64:
// swap high and low uints
n = ((n >> 32) & INTMASK) | ((n & INTMASK) << 32);
goto case Tuns32;
case Tint32:
case Tuns32:
// swap adjacent ushorts
n = ((n >> 16) & SHORTMASK) | ((n & SHORTMASK) << 16);
goto case Tuns16;
case Tint16:
case Tuns16:
// swap adjacent ubytes
n = ((n >> 8 ) & BYTEMASK) | ((n & BYTEMASK) << 8 );
break;
case Tint128:
case Tuns128:
error(loc, "cent/ucent not supported");
break;
default:
error(loc, "unsupported type");
break;
}
return new IntegerExp(loc, n, type);
}
Expression eval_llvmcttz(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.int64);
uinteger_t x = arg0.toInteger();
int n = getBitsizeOfType(loc, type);
if (x == 0)
{
if (arguments[1].toInteger())
error(loc, "llvm.cttz.i#(0) is undefined");
}
else
{
int c = n >> 1;
n -= 1;
const uinteger_t mask = (uinteger_t(1L) << n) | (uinteger_t(1L) << n)-1;
do {
uinteger_t y = (x << c) & mask;
if (y != 0) { n -= c; x = y; }
c = c >> 1;
} while (c != 0);
}
return new IntegerExp(loc, n, type);
}
Expression eval_llvmctlz(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.int64);
uinteger_t x = arg0.toInteger();
if (x == 0 && arguments[1].toInteger())
error(loc, "llvm.ctlz.i#(0) is undefined");
int n = getBitsizeOfType(loc, type);
int c = n >> 1;
do {
uinteger_t y = x >> c; if (y != 0) { n -= c; x = y; }
c = c >> 1;
} while (c != 0);
return new IntegerExp(loc, n - x, type);
}
Expression eval_llvmctpop(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
// FIXME Does not work for cent/ucent
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.int64);
uinteger_t n = arg0.toInteger();
int cnt = 0;
while (n)
{
cnt += (n & 1);
n >>= 1;
}
return new IntegerExp(loc, cnt, type);
}
Expression eval_llvmexpect(Loc loc, FuncDeclaration fd, Expression[] arguments)
{
Type type = getTypeOfOverloadedIntrinsic(fd);
Expression arg0 = arguments[0];
assert(arg0.op == EXP.int64);
return new IntegerExp(loc, arg0.toInteger(), type);
}
} // IN_LLVM