-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patheval.c
More file actions
1758 lines (1583 loc) · 58.6 KB
/
eval.c
File metadata and controls
1758 lines (1583 loc) · 58.6 KB
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "sunder.h"
static bool
integer_is_out_of_range(struct type const* type, struct bigint const* res);
static uint8_t* //sbuf
sized_primitive_value_to_new_bytes(struct value const* value);
static struct value*
eval_rvalue_symbol(struct expr const* expr);
static struct value*
eval_rvalue_value(struct expr const* expr);
static struct value*
eval_rvalue_bytes(struct expr const* expr);
static struct value*
eval_rvalue_array_list(struct expr const* expr);
static struct value*
eval_rvalue_slice_list(struct expr const* expr);
static struct value*
eval_rvalue_slice(struct expr const* expr);
static struct value*
eval_rvalue_init(struct expr const* expr);
static struct value*
eval_rvalue_cast(struct expr const* expr);
static struct value*
eval_rvalue_access_index(struct expr const* expr);
static struct value*
eval_rvalue_access_slice(struct expr const* expr);
static struct value*
eval_rvalue_access_member_variable(struct expr const* expr);
static struct value*
eval_rvalue_sizeof(struct expr const* expr);
static struct value*
eval_rvalue_alignof(struct expr const* expr);
static struct value*
eval_rvalue_unary(struct expr const* expr);
static struct value*
eval_rvalue_binary(struct expr const* expr);
static struct value*
eval_lvalue_symbol(struct expr const* expr);
static struct value*
eval_lvalue_bytes(struct expr const* expr);
static struct value*
eval_lvalue_access_index(struct expr const* expr);
static struct value*
eval_lvalue_access_member_variable(struct expr const* expr);
static struct value*
eval_lvalue_unary(struct expr const* expr);
static bool
integer_is_out_of_range(struct type const* type, struct bigint const* res)
{
assert(type != NULL);
assert(type_is_integer(type));
assert(res != NULL);
if (type->kind == TYPE_INTEGER) {
assert(type->data.integer.min == NULL);
assert(type->data.integer.max == NULL);
// Arbitrary precision integers do not have a defined min or max value.
return false;
}
return bigint_cmp(res, type->data.integer.min) < 0
|| bigint_cmp(res, type->data.integer.max) > 0;
}
static uint8_t*
sized_primitive_value_to_new_bytes(struct value const* value)
{
assert(value != NULL);
assert(value->type->size != SIZEOF_UNSIZED);
assert(!type_is_compound(value->type));
sbuf(uint8_t) bytes = NULL;
assert(value->type->size <= SIZE_MAX);
sbuf_resize(bytes, (size_t)value->type->size);
safe_memset(bytes, 0x00, (size_t)value->type->size);
switch (value->type->kind) {
case TYPE_ANY: {
UNREACHABLE();
}
case TYPE_VOID: {
assert(sbuf_count(bytes) == 0);
return bytes;
}
case TYPE_BOOL: {
assert(sbuf_count(bytes) == 1);
bytes[0] = value->data.boolean;
return bytes;
}
case TYPE_BYTE: {
assert(sbuf_count(bytes) == 1);
bytes[0] = value->data.byte;
return bytes;
}
case TYPE_U8: /* fallthrough */
case TYPE_S8: /* fallthrough */
case TYPE_U16: /* fallthrough */
case TYPE_S16: /* fallthrough */
case TYPE_U32: /* fallthrough */
case TYPE_S32: /* fallthrough */
case TYPE_U64: /* fallthrough */
case TYPE_S64: /* fallthrough */
case TYPE_USIZE: /* fallthrough */
case TYPE_SSIZE: /* fallthrough */
case TYPE_ENUM: {
// Convert the bigint into a bit array.
size_t const bit_count = (size_t)value->type->size * 8u;
struct bitarr* const bits = bitarr_new(bit_count);
if (bigint_to_bitarr(bits, value->data.integer)) {
// Internal compiler error. Integer is out of range.
UNREACHABLE();
}
// Convert the bit array into a byte array via bit shifting and masking.
for (size_t i = 0; i < bit_count; ++i) {
uint8_t const mask = (uint8_t)(bitarr_get(bits, i) << (i % 8u));
bytes[i / 8u] |= mask;
}
bitarr_del(bits);
return bytes;
}
case TYPE_INTEGER: {
// Arbitrary precision integers have no meaningful byte representation.
UNREACHABLE();
}
case TYPE_F32: {
union {
float f32;
uint8_t bytes[sizeof(float)];
} u;
u.f32 = value->data.f32;
assert(sbuf_count(bytes) == ARRAY_COUNT(u.bytes));
for (size_t i = 0; i < sbuf_count(bytes); ++i) {
bytes[i] = u.bytes[i];
}
return bytes;
}
case TYPE_F64: {
union {
double f64;
uint8_t bytes[sizeof(double)];
} u;
u.f64 = value->data.f64;
assert(sbuf_count(bytes) == ARRAY_COUNT(u.bytes));
for (size_t i = 0; i < sbuf_count(bytes); ++i) {
bytes[i] = u.bytes[i];
}
return bytes;
}
case TYPE_REAL: {
// Arbitrary precision reals have no meaningful byte representation.
UNREACHABLE();
}
case TYPE_FUNCTION: /* fallthrough */
case TYPE_POINTER: /* fallthrough */
case TYPE_ARRAY: /* fallthrough */
case TYPE_SLICE: /* fallthrough */
case TYPE_STRUCT: /* fallthrough */
case TYPE_UNION: /* fallthrough */
case TYPE_EXTERN: {
UNREACHABLE();
}
}
UNREACHABLE();
return NULL;
}
struct value*
eval_rvalue(struct expr const* expr)
{
assert(expr != NULL);
switch (expr->kind) {
case EXPR_SYMBOL: {
return eval_rvalue_symbol(expr);
}
case EXPR_VALUE: {
return eval_rvalue_value(expr);
}
case EXPR_BYTES: {
return eval_rvalue_bytes(expr);
}
case EXPR_ARRAY_LIST: {
return eval_rvalue_array_list(expr);
}
case EXPR_SLICE_LIST: {
return eval_rvalue_slice_list(expr);
}
case EXPR_SLICE: {
return eval_rvalue_slice(expr);
}
case EXPR_INIT: {
return eval_rvalue_init(expr);
}
case EXPR_CAST: {
return eval_rvalue_cast(expr);
}
case EXPR_CALL: {
fatal(expr->location, "constant expression contains function call");
}
case EXPR_ACCESS_INDEX: {
return eval_rvalue_access_index(expr);
}
case EXPR_ACCESS_SLICE: {
return eval_rvalue_access_slice(expr);
}
case EXPR_ACCESS_MEMBER_VARIABLE: {
return eval_rvalue_access_member_variable(expr);
}
case EXPR_SIZEOF: {
return eval_rvalue_sizeof(expr);
}
case EXPR_ALIGNOF: {
return eval_rvalue_alignof(expr);
}
case EXPR_UNARY: {
return eval_rvalue_unary(expr);
}
case EXPR_BINARY: {
return eval_rvalue_binary(expr);
}
}
UNREACHABLE();
return NULL;
}
static struct value*
eval_rvalue_symbol(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_SYMBOL);
struct symbol const* const symbol = expr->data.symbol;
enum symbol_kind const kind = symbol->kind;
if (kind == SYMBOL_CONSTANT || kind == SYMBOL_FUNCTION) {
return value_clone(symbol_xget_value(expr->location, symbol));
}
fatal(expr->location, "identifier `%s` is not a constant", symbol->name);
return NULL;
}
static struct value*
eval_rvalue_value(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_VALUE);
return value_clone(expr->data.value);
}
static struct value*
eval_rvalue_bytes(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_BYTES);
struct value* const start = value_new_pointer(
type_unique_pointer(context()->builtin.byte),
*symbol_xget_address(expr->data.bytes.array_symbol));
struct value* const count = value_new_integer(
context()->builtin.usize, bigint_new_umax(expr->data.bytes.count));
return value_new_slice(expr->type, start, count);
}
static struct value*
eval_rvalue_array_list(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_ARRAY_LIST);
sbuf(struct expr const* const) elements = expr->data.array_list.elements;
sbuf(struct value*) evaled_elements = NULL;
for (size_t i = 0; i < sbuf_count(elements); ++i) {
sbuf_push(evaled_elements, eval_rvalue(elements[i]));
}
struct value* evaled_ellipsis = NULL;
if (expr->data.array_list.ellipsis != NULL) {
evaled_ellipsis = eval_rvalue(expr->data.array_list.ellipsis);
}
return value_new_array(expr->type, evaled_elements, evaled_ellipsis);
}
static struct value*
eval_rvalue_slice_list(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_SLICE_LIST);
assert(expr->type->kind == TYPE_SLICE);
struct address const* const address =
symbol_xget_address(expr->data.slice_list.array_symbol);
assert(address->kind == ADDRESS_STATIC);
struct value* const pointer = value_new_pointer(
type_unique_pointer(expr->type->data.slice.base), *address);
sbuf(struct expr const* const) const elements =
expr->data.slice_list.elements;
struct value* const count = value_new_integer(
context()->builtin.usize, bigint_new_umax(sbuf_count(elements)));
return value_new_slice(expr->type, pointer, count);
}
static struct value*
eval_rvalue_slice(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_SLICE);
struct value* const start = eval_rvalue(expr->data.slice.start);
struct value* const count = eval_rvalue(expr->data.slice.count);
return value_new_slice(expr->type, start, count);
}
static struct value*
eval_rvalue_init_struct(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_INIT);
assert(expr->type->kind == TYPE_STRUCT);
struct value* const value = value_new_struct(expr->type);
sbuf(struct member_variable) const defs =
expr->type->data.struct_.member_variables;
(void)defs;
sbuf(struct member_variable_initializer const) const initializers =
expr->data.init.initializers;
assert(sbuf_count(defs) == sbuf_count(initializers));
for (size_t i = 0; i < sbuf_count(initializers); ++i) {
if (initializers[i].expr == NULL) {
continue;
}
value_set_member(
value,
initializers[i].variable->name,
eval_rvalue(initializers[i].expr));
}
return value;
}
static struct value*
eval_rvalue_init_union(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_INIT);
assert(expr->type->kind == TYPE_UNION);
sbuf(struct member_variable const) const member_variables =
expr->type->data.union_.member_variables;
sbuf(struct member_variable_initializer const) const initializers =
expr->data.init.initializers;
struct value* const value = value_new_union(expr->type);
if (sbuf_count(member_variables) == 0) {
assert(sbuf_count(initializers) == 0);
return value;
}
assert(sbuf_count(initializers) == 1);
value_set_member(
value,
initializers[0].variable->name,
eval_rvalue(initializers[0].expr));
assert(value->data.union_.member_variable != NULL);
assert(value->data.union_.member_value != NULL);
return value;
}
static struct value*
eval_rvalue_init(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_INIT);
assert(expr->type->kind == TYPE_STRUCT || expr->type->kind == TYPE_UNION);
if (expr->type->kind == TYPE_STRUCT) {
return eval_rvalue_init_struct(expr);
}
if (expr->type->kind == TYPE_UNION) {
return eval_rvalue_init_union(expr);
}
UNREACHABLE();
return NULL;
}
static struct value*
eval_rvalue_cast(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_CAST);
struct value* const from = eval_rvalue(expr->data.cast.expr);
// Check if the value casted from is already the correct type. Also allows
// us to assume `from->type->kind != expr->type->kind` from this point
// forward. Illegal casts of the same type kind (e.g. cast []u16 to []u32)
// should have produced a fatal error during the resolve phase.
assert(from->type->kind != TYPE_ANY);
assert(from->type->kind != TYPE_VOID);
assert(from->type->kind != TYPE_ARRAY);
assert(from->type->kind != TYPE_SLICE);
assert(from->type->kind != TYPE_STRUCT);
if (from->type->kind == expr->type->kind) {
// Setting the type of `from` to the type of the `expr` to cover cases
// such as function-to-function conversions where the type kind is the
// same, but the actual type is different.
from->type = expr->type;
return from;
}
// The representation of a non-absolute address is chosen by the
// assembler/linker and has no meaningful representation at compile-time.
if (from->type->kind == TYPE_POINTER) {
fatal(
expr->location,
"constant expression contains cast from pointer type `%s` to non-pointer type `%s`",
from->type->name,
expr->type->name);
}
// Casting from a compile-time known usize value to absolute address.
if (expr->type->kind == TYPE_POINTER) {
switch (from->type->kind) {
case TYPE_USIZE: {
uintmax_t absolute = 0;
if (bigint_to_umax(&absolute, from->data.integer)) {
UNREACHABLE();
}
struct value* const result =
value_new_pointer(expr->type, address_init_absolute(absolute));
value_del(from);
return result;
}
case TYPE_FUNCTION: {
struct value* const result =
value_new_pointer(expr->type, *from->data.function->address);
value_del(from);
return result;
}
case TYPE_ANY: /* fallthrough */
case TYPE_VOID: /* fallthrough */
case TYPE_BOOL: /* fallthrough */
case TYPE_BYTE: /* fallthrough */
case TYPE_U8: /* fallthrough */
case TYPE_S8: /* fallthrough */
case TYPE_U16: /* fallthrough */
case TYPE_S16: /* fallthrough */
case TYPE_U32: /* fallthrough */
case TYPE_S32: /* fallthrough */
case TYPE_U64: /* fallthrough */
case TYPE_S64: /* fallthrough */
case TYPE_SSIZE: /* fallthrough */
case TYPE_INTEGER: /* fallthrough */
case TYPE_F32: /* fallthrough */
case TYPE_F64: /* fallthrough */
case TYPE_REAL: /* fallthrough */
case TYPE_POINTER: /* fallthrough */
case TYPE_ARRAY: /* fallthrough */
case TYPE_SLICE: /* fallthrough */
case TYPE_STRUCT: /* fallthrough */
case TYPE_UNION: /* fallthrough */
case TYPE_ENUM: /* fallthrough */
case TYPE_EXTERN: {
UNREACHABLE();
}
}
}
// Special cases when casting from unsized integers. Check to make sure
// that the value of the integer expression can fit into the range of the
// casted-to type for byte and sized integer types. Unsized integers may
// only appear in integer constant expressions, so evaluating the rhs
// expression should always produce a constant value.
if (expr->type->kind == TYPE_BOOL && from->type->kind == TYPE_INTEGER) {
struct value* const result =
value_new_boolean(bigint_cmp(from->data.integer, BIGINT_ZERO) != 0);
value_del(from);
return result;
}
if (expr->type->kind == TYPE_BYTE && from->type->kind == TYPE_INTEGER) {
struct bigint const* const min = context()->u8_min;
struct bigint const* const max = context()->u8_max;
assert(min != NULL);
assert(max != NULL);
if (bigint_cmp(from->data.integer, min) < 0) {
fatal(
expr->location,
"out-of-range conversion from `%s` to `%s` (%s < %s)",
from->type->name,
expr->type->name,
bigint_to_new_cstr(from->data.integer),
bigint_to_new_cstr(min));
}
if (bigint_cmp(from->data.integer, max) > 0) {
fatal(
expr->location,
"out-of-range conversion from `%s` to `%s` (%s > %s)",
from->type->name,
expr->type->name,
bigint_to_new_cstr(from->data.integer),
bigint_to_new_cstr(max));
}
uint8_t byte = 0;
int const out_of_range = bigint_to_u8(&byte, from->data.integer);
if (out_of_range) {
UNREACHABLE();
}
struct value* const result = value_new_byte(byte);
value_del(from);
return result;
}
if (type_is_integer(expr->type) && from->type->kind == TYPE_INTEGER) {
assert(expr->type->data.integer.min != NULL);
assert(expr->type->data.integer.max != NULL);
struct bigint const* const min = expr->type->data.integer.min;
struct bigint const* const max = expr->type->data.integer.max;
if (bigint_cmp(from->data.integer, min) < 0) {
fatal(
expr->location,
"out-of-range conversion from `%s` to `%s` (%s < %s)",
from->type->name,
expr->type->name,
bigint_to_new_cstr(from->data.integer),
bigint_to_new_cstr(min));
}
if (bigint_cmp(from->data.integer, max) > 0) {
fatal(
expr->location,
"out-of-range conversion from `%s` to `%s` (%s > %s)",
from->type->name,
expr->type->name,
bigint_to_new_cstr(from->data.integer),
bigint_to_new_cstr(max));
}
struct value* const result =
value_new_integer(expr->type, bigint_new(from->data.integer));
value_del(from);
return result;
}
if (expr->type->kind == TYPE_ENUM && from->type->kind == TYPE_INTEGER) {
struct type const* const expr_actual_type =
expr->type->data.enum_.underlying_type;
assert(expr_actual_type->data.integer.min != NULL);
assert(expr_actual_type->data.integer.max != NULL);
struct bigint const* const min = expr_actual_type->data.integer.min;
struct bigint const* const max = expr_actual_type->data.integer.max;
assert(min != NULL);
assert(max != NULL);
if (bigint_cmp(from->data.integer, min) < 0) {
fatal(
expr->location,
"out-of-range conversion from `%s` to `%s` (%s < %s)",
from->type->name,
expr->type->name,
bigint_to_new_cstr(from->data.integer),
bigint_to_new_cstr(min));
}
if (bigint_cmp(from->data.integer, max) > 0) {
fatal(
expr->location,
"out-of-range conversion from `%s` to `%s` (%s > %s)",
from->type->name,
expr->type->name,
bigint_to_new_cstr(from->data.integer),
bigint_to_new_cstr(max));
}
struct value* result =
value_new_integer(expr_actual_type, bigint_new(from->data.integer));
// Convert from the underlying type to enum.
result->type = expr->type;
value_del(from);
return result;
}
// Cases casting between integer and IEEE-754 floating point types.
if (type_is_ieee754(expr->type) && type_is_integer(from->type)) {
assert(expr->type->kind == TYPE_F32 || expr->type->kind == TYPE_F64);
struct bigint const* const integer = from->data.integer;
if (from->type->kind == TYPE_INTEGER) {
struct bigint const* const min = expr->type->kind == TYPE_F64
? context()->f64_integer_min
: context()->f32_integer_min;
struct bigint const* const max = expr->type->kind == TYPE_F64
? context()->f64_integer_max
: context()->f32_integer_max;
bool const integer_ge_min = bigint_cmp(integer, min) >= 0;
bool const integer_le_max = bigint_cmp(integer, max) <= 0;
bool const is_representable = integer_ge_min && integer_le_max;
if (!is_representable) {
fatal(
expr->location,
"constant expression contains cast from integer type `%s` to floating point type `%s` with unrepresentable value %s",
from->type->name,
expr->type->name,
bigint_to_new_cstr(integer));
}
}
intmax_t smax = 0;
if (bigint_to_smax(&smax, integer)) {
UNREACHABLE();
}
struct value* const result = expr->type->kind == TYPE_F64
? value_new_f64((double)smax)
: value_new_f32((float)smax);
value_del(from);
return result;
}
if (type_is_integer(expr->type) && type_is_ieee754(from->type)) {
assert(expr->type->kind != TYPE_INTEGER);
assert(from->type->kind == TYPE_F32 || from->type->kind == TYPE_F64);
double const from_as_double = from->type->kind == TYPE_F64
? from->data.f64
: (double)from->data.f32;
bool const is_finite_f32 =
(from->type->kind == TYPE_F32 && isfinite(from->data.f32));
bool const is_finite_f64 =
(from->type->kind == TYPE_F64 && isfinite(from->data.f64));
bool const is_finite = is_finite_f32 || is_finite_f64;
if (!is_finite) {
fatal(
expr->location,
"constant expression contains cast from floating point type `%s` to integer type `%s` with unrepresentable value %f",
from->type->name,
expr->type->name,
from_as_double);
}
assert(expr->type->data.integer.min != NULL);
assert(expr->type->data.integer.max != NULL);
if (type_is_uinteger(expr->type)) {
uintmax_t min_as_umax = 0;
uintmax_t max_as_umax = 0;
if (bigint_to_umax(&min_as_umax, expr->type->data.integer.min)) {
UNREACHABLE();
}
if (bigint_to_umax(&max_as_umax, expr->type->data.integer.max)) {
UNREACHABLE();
}
uintmax_t from_as_umax = (uintmax_t)from_as_double;
if (from_as_umax < min_as_umax || max_as_umax < from_as_umax) {
fatal(expr->location, "operation produces out-of-range result");
}
struct bigint* const integer = bigint_new_umax(from_as_umax);
struct value* const result = value_new_integer(expr->type, integer);
value_del(from);
return result;
}
if (type_is_sinteger(expr->type)) {
intmax_t min_as_smax = 0;
intmax_t max_as_smax = 0;
if (bigint_to_smax(&min_as_smax, expr->type->data.integer.min)) {
UNREACHABLE();
}
if (bigint_to_smax(&max_as_smax, expr->type->data.integer.max)) {
UNREACHABLE();
}
intmax_t from_as_smax = (intmax_t)from_as_double;
if (from_as_smax < min_as_smax || max_as_smax < from_as_smax) {
fatal(expr->location, "operation produces out-of-range result");
}
struct bigint* const integer = bigint_new_smax(from_as_smax);
struct value* const result = value_new_integer(expr->type, integer);
value_del(from);
return result;
}
UNREACHABLE();
}
// Cases casting between sized IEEE-754 floating point types.
if (expr->type->kind == TYPE_F32 && from->type->kind == TYPE_F64) {
struct value* const result = value_new_f32((float)from->data.f64);
value_del(from);
return result;
}
if (expr->type->kind == TYPE_F64 && from->type->kind == TYPE_F32) {
struct value* const result = value_new_f64((double)from->data.f32);
value_del(from);
return result;
}
// Special cases when casting from unsized IEEE-754 values.
if (expr->type->kind == TYPE_F32 && from->type->kind == TYPE_REAL) {
struct value* const result = value_new_f32((float)from->data.real);
value_del(from);
return result;
}
if (expr->type->kind == TYPE_F64 && from->type->kind == TYPE_REAL) {
struct value* const result = value_new_f64(from->data.real);
value_del(from);
return result;
}
// Cases casting from sized types with a defined byte representation.
assert(from->type->size != SIZEOF_UNSIZED);
sbuf(uint8_t) bytes = sized_primitive_value_to_new_bytes(from);
struct value* res = NULL;
switch (expr->type->kind) {
case TYPE_BOOL: {
bool boolean = false;
for (size_t i = 0; i < sbuf_count(bytes); ++i) {
boolean |= bytes[i] != 0;
}
res = value_new_boolean(boolean);
break;
}
case TYPE_BYTE: {
assert(sbuf_count(bytes) >= 1);
res = value_new_byte(bytes[0]);
break;
}
case TYPE_U8: /* fallthrough */
case TYPE_S8: /* fallthrough */
case TYPE_U16: /* fallthrough */
case TYPE_S16: /* fallthrough */
case TYPE_U32: /* fallthrough */
case TYPE_S32: /* fallthrough */
case TYPE_U64: /* fallthrough */
case TYPE_S64: /* fallthrough */
case TYPE_USIZE: /* fallthrough */
case TYPE_SSIZE: /* fallthrough */
case TYPE_ENUM: {
struct type const* const from_actual_type =
from->type->kind == TYPE_ENUM
? from->type->data.enum_.underlying_type
: from->type;
struct type const* const expr_actual_type =
expr->type->kind == TYPE_ENUM
? expr->type->data.enum_.underlying_type
: expr->type;
// Zero-extension or sign-extension bit.
size_t bytes_count = sbuf_count(bytes);
int const extend = type_is_sinteger(from_actual_type)
&& (bytes[bytes_count - 1] & 0x80);
size_t const bit_count = (size_t)expr_actual_type->size * 8u;
struct bitarr* const bits = bitarr_new(bit_count);
for (size_t i = 0; i < bit_count; ++i) {
if (i >= (bytes_count * 8u)) {
bitarr_set(bits, i, extend);
continue;
}
unsigned const byte = bytes[i / 8u];
unsigned const mask = 1u << (i % 8u);
int const bit = (byte & mask) != 0;
bitarr_set(bits, i, bit);
}
struct bigint* const integer =
bigint_new_bitarr(bits, type_is_sinteger(expr_actual_type));
bitarr_del(bits);
res = value_new_integer(expr_actual_type, integer);
if (expr->type->kind == TYPE_ENUM) {
// Convert from the underlying type to enum.
res->type = expr->type;
}
break;
}
case TYPE_ANY: /* fallthrough */
case TYPE_VOID: /* fallthrough */
case TYPE_INTEGER: /* fallthrough */
case TYPE_F32: /* fallthrough */
case TYPE_F64: /* fallthrough */
case TYPE_REAL: /* fallthrough */
case TYPE_FUNCTION: /* fallthrough */
case TYPE_POINTER: /* fallthrough */
case TYPE_ARRAY: /* fallthrough */
case TYPE_SLICE: /* fallthrough */
case TYPE_STRUCT: /* fallthrough */
case TYPE_UNION: /* fallthrough */
case TYPE_EXTERN: {
UNREACHABLE();
}
}
value_del(from);
sbuf_fini(bytes);
return res;
}
static struct value*
eval_rvalue_access_index(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_ACCESS_INDEX);
struct value* const lhs = eval_rvalue(expr->data.access_index.lhs);
struct value* const idx = eval_rvalue(expr->data.access_index.idx);
assert(idx->type->kind == TYPE_USIZE);
struct bigint const* const idx_bigint = idx->data.integer;
size_t idx_uz = 0u;
if (bigint_to_uz(&idx_uz, idx_bigint)) {
fatal(
expr->data.access_index.idx->location,
"index out-of-range (received %s)",
bigint_to_new_cstr(idx_bigint));
}
if (lhs->type->kind == TYPE_ARRAY) {
if (idx_uz >= lhs->type->data.array.count) {
fatal(
expr->data.access_index.idx->location,
"index out-of-bounds (array count is %ju, received %zu)",
lhs->type->data.array.count,
idx_uz);
}
sbuf(struct value*) const elements = lhs->data.array.elements;
struct value* const ellipsis = lhs->data.array.ellipsis;
assert(idx_uz < sbuf_count(elements) || ellipsis != NULL);
struct value* const res = idx_uz < sbuf_count(elements)
? value_clone(elements[idx_uz])
: value_clone(ellipsis);
value_del(lhs);
value_del(idx);
return res;
}
if (lhs->type->kind == TYPE_SLICE) {
// Slices are constructed from a (pointer, count) pair which makes them
// more-or-less normal pointers with some extra fancy bookkeeping.
// Pointers may not be dereferenced in a constant expression, so
// similarly we do not allow indexing a slice (which is more-or-less
// pointer dereferencing) in a constant expression.
fatal(
expr->location,
"indexing with left-hand-type `%s` not supported in compile-time expressions",
lhs->type->name);
}
UNREACHABLE();
return NULL;
}
static struct value*
eval_rvalue_access_slice(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_ACCESS_SLICE);
struct value* const lhs = eval_rvalue(expr->data.access_slice.lhs);
struct value* const begin = eval_rvalue(expr->data.access_slice.begin);
struct value* const end = eval_rvalue(expr->data.access_slice.end);
assert(begin->type->kind == TYPE_USIZE);
struct bigint const* const begin_bigint = begin->data.integer;
size_t begin_uz = 0u;
if (bigint_to_uz(&begin_uz, begin_bigint)) {
fatal(
expr->data.access_slice.begin->location,
"index out-of-range (received %s)",
bigint_to_new_cstr(begin_bigint));
}
assert(end->type->kind == TYPE_USIZE);
struct bigint const* const end_bigint = end->data.integer;
size_t end_uz = 0u;
if (bigint_to_uz(&end_uz, end_bigint)) {
fatal(
expr->data.access_slice.end->location,
"index out-of-range (received %s)",
bigint_to_new_cstr(end_bigint));
}
if (lhs->type->kind == TYPE_ARRAY) {
if (begin_uz >= lhs->type->data.array.count) {
fatal(
expr->data.access_slice.begin->location,
"index out-of-bounds (array count is %ju, received %zu)",
lhs->type->data.array.count,
begin_uz);
}
if (end_uz > lhs->type->data.array.count) {
fatal(
expr->data.access_slice.begin->location,
"index out-of-bounds (array count is %ju, received %zu)",
lhs->type->data.array.count,
end_uz);
}
struct value* const pointer = eval_lvalue(expr->data.access_slice.lhs);
assert(pointer->type->kind == TYPE_POINTER);
assert(pointer->data.pointer.kind == ADDRESS_STATIC);
pointer->type = type_unique_pointer(expr->type->data.slice.base);
pointer->data.pointer.data.static_.offset +=
begin_uz * expr->type->data.slice.base->size;
struct bigint* const count_bigint = bigint_new(BIGINT_ZERO);
bigint_sub(count_bigint, end_bigint, begin_bigint);
struct value* const count =
value_new_integer(context()->builtin.usize, count_bigint);
struct value* const res = value_new_slice(expr->type, pointer, count);
value_del(lhs);
value_del(begin);
value_del(end);
return res;
}
if (lhs->type->kind == TYPE_SLICE) {
// Slices are constructed from a (pointer, count) pair which makes them
// more-or-less normal pointers with some extra fancy bookkeeping.
// Pointers may not be dereferenced in a constant expression, so
// similarly we do not allow indexing a slice (which is more-or-less
// pointer dereferencing) in a constant expression.
fatal(
expr->location,
"slicing with left-hand-type `%s` not supported in compile-time expressions",
lhs->type->name);
}
UNREACHABLE();
return NULL;
}
static struct value*
eval_rvalue_access_member_variable(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_ACCESS_MEMBER_VARIABLE);
struct value* const lhs =
eval_rvalue(expr->data.access_member_variable.lhs);
struct value const* const member = value_xget_member_value(
expr->location,
lhs,
expr->data.access_member_variable.member_variable->name);
struct value* const res = value_clone(member);
value_del(lhs);
return res;
}
static struct value*
eval_rvalue_sizeof(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_SIZEOF);
assert(expr->type->kind == TYPE_USIZE);
return value_new_integer(
context()->builtin.usize,
bigint_new_umax(expr->data.sizeof_.rhs->size));
}
static struct value*
eval_rvalue_alignof(struct expr const* expr)
{
assert(expr != NULL);
assert(expr->kind == EXPR_ALIGNOF);
assert(expr->type->kind == TYPE_USIZE);
return value_new_integer(
context()->builtin.usize,
bigint_new_umax(expr->data.alignof_.rhs->align));
}
static struct value*