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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
|
gb_internal cgValue cg_flatten_value(cgProcedure *p, cgValue value) {
if (value.kind == cgValue_Symbol) {
GB_ASSERT(is_type_internally_pointer_like(value.type));
value = cg_value(tb_inst_get_symbol_address(p->func, value.symbol), value.type);
}
return value;
}
gb_internal cgContextData *cg_push_context_onto_stack(cgProcedure *p, cgAddr ctx) {
ctx.kind = cgAddr_Context;
cgContextData *cd = array_add_and_get(&p->context_stack);
cd->ctx = ctx;
cd->scope_index = p->scope_index;
return cd;
}
gb_internal cgAddr cg_find_or_generate_context_ptr(cgProcedure *p) {
if (p->context_stack.count > 0) {
return p->context_stack[p->context_stack.count-1].ctx;
}
Type *pt = base_type(p->type);
GB_ASSERT(pt->kind == Type_Proc);
GB_ASSERT(pt->Proc.calling_convention != ProcCC_Odin);
cgAddr c = cg_add_local(p, t_context, nullptr, true);
tb_node_append_attrib(c.addr.node, tb_function_attrib_variable(p->func, -1, "context", cg_debug_type(p->module, t_context)));
c.kind = cgAddr_Context;
// lb_emit_init_context(p, c);
cg_push_context_onto_stack(p, c);
// lb_add_debug_context_variable(p, c);
return c;
}
gb_internal cgValue cg_find_value_from_entity(cgModule *m, Entity *e) {
e = strip_entity_wrapping(e);
GB_ASSERT(e != nullptr);
GB_ASSERT(e->token.string != "_");
if (e->kind == Entity_Procedure) {
return cg_find_procedure_value_from_entity(m, e);
}
cgValue *found = nullptr;
rw_mutex_shared_lock(&m->values_mutex);
found = map_get(&m->values, e);
rw_mutex_shared_unlock(&m->values_mutex);
if (found) {
return *found;
}
GB_PANIC("\n\tError in: %s, missing value '%.*s'\n", token_pos_to_string(e->token.pos), LIT(e->token.string));
return {};
}
gb_internal cgAddr cg_build_addr_from_entity(cgProcedure *p, Entity *e, Ast *expr) {
GB_ASSERT(e != nullptr);
if (e->kind == Entity_Constant) {
Type *t = default_type(type_of_expr(expr));
cgValue v = cg_const_value(p, t, e->Constant.value);
GB_PANIC("TODO(bill): cg_add_global_generated");
// return cg_add_global_generated(p->module, t, v);
return {};
}
cgAddr *local_found = map_get(&p->variable_map, e);
if (local_found) {
return *local_found;
}
cgValue v = {};
cgModule *m = p->module;
rw_mutex_lock(&m->values_mutex);
cgValue *found = map_get(&m->values, e);
rw_mutex_unlock(&m->values_mutex);
if (found) {
v = *found;
} else if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) {
GB_PANIC("TODO(bill): cg_get_using_variable");
// NOTE(bill): Calculate the using variable every time
// v = cg_get_using_variable(p, e);
} else if (e->flags & EntityFlag_SoaPtrField) {
GB_PANIC("TODO(bill): cg_get_soa_variable_addr");
// return cg_get_soa_variable_addr(p, e);
}
if (v.node == nullptr) {
cgValue v = cg_find_value_from_entity(m, e);
v = cg_flatten_value(p, v);
return cg_addr(v);
}
return cg_addr(v);
}
gb_internal cgValue cg_typeid(cgModule *m, Type *t) {
GB_ASSERT("TODO(bill): cg_typeid");
return {};
}
gb_internal cgValue cg_correct_endianness(cgProcedure *p, cgValue value) {
Type *src = core_type(value.type);
GB_ASSERT(is_type_integer(src) || is_type_float(src));
if (is_type_different_to_arch_endianness(src)) {
GB_PANIC("TODO(bill): cg_correct_endianness");
// Type *platform_src_type = integer_endian_type_to_platform_type(src);
// value = cg_emit_byte_swap(p, value, platform_src_type);
}
return value;
}
gb_internal cgValue cg_emit_transmute(cgProcedure *p, cgValue value, Type *type) {
GB_ASSERT(type_size_of(value.type) == type_size_of(type));
value = cg_flatten_value(p, value);
if (are_types_identical(value.type, type)) {
return value;
}
if (are_types_identical(core_type(value.type), core_type(type))) {
value.type = type;
return value;
}
i64 src_align = type_align_of(value.type);
i64 dst_align = type_align_of(type);
if (dst_align > src_align) {
cgAddr local = cg_add_local(p, type, nullptr, false);
cgValue dst = local.addr;
dst.type = alloc_type_pointer(value.type);
cg_emit_store(p, dst, value);
return cg_addr_load(p, local);
}
TB_DataType dt = cg_data_type(type);
switch (value.kind) {
case cgValue_Value:
GB_ASSERT(!TB_IS_VOID_TYPE(dt));
value.type = type;
value.node = tb_inst_bitcast(p->func, value.node, dt);
return value;
case cgValue_Addr:
value.type = type;
return value;
case cgValue_Symbol:
GB_PANIC("should be handled above");
break;
}
return value;
}
gb_internal cgValue cg_emit_byte_swap(cgProcedure *p, cgValue value, Type *end_type) {
GB_ASSERT(type_size_of(value.type) == type_size_of(end_type));
if (type_size_of(value.type) < 2) {
return value;
}
if (is_type_float(value.type)) {
i64 sz = type_size_of(value.type);
Type *integer_type = nullptr;
switch (sz) {
case 2: integer_type = t_u16; break;
case 4: integer_type = t_u32; break;
case 8: integer_type = t_u64; break;
}
GB_ASSERT(integer_type != nullptr);
value = cg_emit_transmute(p, value, integer_type);
}
GB_ASSERT(value.kind == cgValue_Value);
value.node = tb_inst_bswap(p->func, value.node);
return cg_emit_transmute(p, value, end_type);
}
gb_internal cgValue cg_emit_conv(cgProcedure *p, cgValue value, Type *t) {
t = reduce_tuple_to_single_type(t);
value = cg_flatten_value(p, value);
Type *src_type = value.type;
if (are_types_identical(t, src_type)) {
return value;
}
if (is_type_untyped_uninit(src_type)) {
// return cg_const_undef(m, t);
return cg_const_nil(p, t);
}
if (is_type_untyped_nil(src_type)) {
return cg_const_nil(p, t);
}
Type *src = core_type(src_type);
Type *dst = core_type(t);
GB_ASSERT(src != nullptr);
GB_ASSERT(dst != nullptr);
if (are_types_identical(src, dst)) {
return cg_emit_transmute(p, value, t);
}
TB_DataType st = cg_data_type(src);
TB_DataType dt = cg_data_type(t);
if (is_type_integer(src) && is_type_integer(dst)) {
GB_ASSERT(src->kind == Type_Basic &&
dst->kind == Type_Basic);
GB_ASSERT(value.kind == cgValue_Value);
i64 sz = type_size_of(default_type(src));
i64 dz = type_size_of(default_type(dst));
if (sz == dz) {
if (dz > 1 && !types_have_same_internal_endian(src, dst)) {
return cg_emit_byte_swap(p, value, t);
}
value.type = t;
return value;
}
if (sz > 1 && is_type_different_to_arch_endianness(src)) {
Type *platform_src_type = integer_endian_type_to_platform_type(src);
value = cg_emit_byte_swap(p, value, platform_src_type);
}
TB_Node* (*op)(TB_Function* f, TB_Node* src, TB_DataType dt) = tb_inst_trunc;
if (dz < sz) {
op = tb_inst_trunc;
} else if (dz == sz) {
op = tb_inst_bitcast;
} else if (dz > sz) {
op = is_type_unsigned(src) ? tb_inst_zxt : tb_inst_sxt; // zero extent
}
if (dz > 1 && is_type_different_to_arch_endianness(dst)) {
Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
cgValue res = cg_value(op(p->func, value.node, cg_data_type(platform_dst_type)), platform_dst_type);
return cg_emit_byte_swap(p, res, t);
} else {
return cg_value(op(p->func, value.node, dt), t);
}
}
// boolean -> boolean/integer
if (is_type_boolean(src) && (is_type_boolean(dst) || is_type_integer(dst))) {
TB_Node *v = tb_inst_cmp_ne(p->func, value.node, tb_inst_uint(p->func, st, 0));
return cg_value(tb_inst_zxt(p->func, v, dt), t);
}
// integer -> boolean
if (is_type_integer(src) && is_type_boolean(dst)) {
TB_Node *v = tb_inst_cmp_ne(p->func, value.node, tb_inst_uint(p->func, st, 0));
return cg_value(tb_inst_zxt(p->func, v, dt), t);
}
if (is_type_cstring(src) && is_type_u8_ptr(dst)) {
return cg_emit_transmute(p, value, dst);
}
if (is_type_u8_ptr(src) && is_type_cstring(dst)) {
return cg_emit_transmute(p, value, dst);
}
if (is_type_cstring(src) && is_type_u8_multi_ptr(dst)) {
return cg_emit_transmute(p, value, dst);
}
if (is_type_u8_multi_ptr(src) && is_type_cstring(dst)) {
return cg_emit_transmute(p, value, dst);
}
if (is_type_cstring(src) && is_type_rawptr(dst)) {
return cg_emit_transmute(p, value, dst);
}
if (is_type_rawptr(src) && is_type_cstring(dst)) {
return cg_emit_transmute(p, value, dst);
}
if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) {
GB_PANIC("TODO(bill): cstring_to_string call");
// TEMPORARY_ALLOCATOR_GUARD();
// lbValue c = lb_emit_conv(p, value, t_cstring);
// auto args = array_make<lbValue>(temporary_allocator(), 1);
// args[0] = c;
// lbValue s = lb_emit_runtime_call(p, "cstring_to_string", args);
// return lb_emit_conv(p, s, dst);
}
// float -> float
if (is_type_float(src) && is_type_float(dst)) {
i64 sz = type_size_of(src);
i64 dz = type_size_of(dst);
if (sz == 2 || dz == 2) {
GB_PANIC("TODO(bill): f16 conversions");
}
if (dz == sz) {
if (types_have_same_internal_endian(src, dst)) {
return cg_value(value.node, t);
} else {
return cg_emit_byte_swap(p, value, t);
}
}
if (is_type_different_to_arch_endianness(src) || is_type_different_to_arch_endianness(dst)) {
Type *platform_src_type = integer_endian_type_to_platform_type(src);
Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
cgValue res = {};
res = cg_emit_conv(p, value, platform_src_type);
res = cg_emit_conv(p, res, platform_dst_type);
if (is_type_different_to_arch_endianness(dst)) {
res = cg_emit_byte_swap(p, res, t);
}
return cg_emit_conv(p, res, t);
}
if (dz >= sz) {
return cg_value(tb_inst_fpxt(p->func, value.node, dt), t);
}
return cg_value(tb_inst_trunc(p->func, value.node, dt), t);
}
if (is_type_complex(src) && is_type_complex(dst)) {
GB_PANIC("TODO(bill): complex -> complex");
}
if (is_type_quaternion(src) && is_type_quaternion(dst)) {
// @QuaternionLayout
GB_PANIC("TODO(bill): quaternion -> quaternion");
}
if (is_type_integer(src) && is_type_complex(dst)) {
GB_PANIC("TODO(bill): int -> complex");
}
if (is_type_float(src) && is_type_complex(dst)) {
GB_PANIC("TODO(bill): float -> complex");
}
if (is_type_integer(src) && is_type_quaternion(dst)) {
GB_PANIC("TODO(bill): int -> quaternion");
}
if (is_type_float(src) && is_type_quaternion(dst)) {
GB_PANIC("TODO(bill): float -> quaternion");
}
if (is_type_complex(src) && is_type_quaternion(dst)) {
GB_PANIC("TODO(bill): complex -> quaternion");
}
// float <-> integer
if (is_type_float(src) && is_type_integer(dst)) {
if (is_type_different_to_arch_endianness(src) || is_type_different_to_arch_endianness(dst)) {
Type *platform_src_type = integer_endian_type_to_platform_type(src);
Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
cgValue res = {};
res = cg_emit_conv(p, value, platform_src_type);
res = cg_emit_conv(p, res, platform_dst_type);
return cg_emit_conv(p, res, t);
}
// if (is_type_integer_128bit(dst)) {
// TEMPORARY_ALLOCATOR_GUARD();
// auto args = array_make<lbValue>(temporary_allocator(), 1);
// args[0] = value;
// char const *call = "fixunsdfdi";
// if (is_type_unsigned(dst)) {
// call = "fixunsdfti";
// }
// lbValue res_i128 = lb_emit_runtime_call(p, call, args);
// return lb_emit_conv(p, res_i128, t);
// }
bool is_signed = !is_type_unsigned(dst);
return cg_value(tb_inst_float2int(p->func, value.node, dt, is_signed), t);
}
if (is_type_integer(src) && is_type_float(dst)) {
if (is_type_different_to_arch_endianness(src) || is_type_different_to_arch_endianness(dst)) {
Type *platform_src_type = integer_endian_type_to_platform_type(src);
Type *platform_dst_type = integer_endian_type_to_platform_type(dst);
cgValue res = {};
res = cg_emit_conv(p, value, platform_src_type);
res = cg_emit_conv(p, res, platform_dst_type);
if (is_type_different_to_arch_endianness(dst)) {
res = cg_emit_byte_swap(p, res, t);
}
return cg_emit_conv(p, res, t);
}
// if (is_type_integer_128bit(src)) {
// TEMPORARY_ALLOCATOR_GUARD();
// auto args = array_make<lbValue>(temporary_allocator(), 1);
// args[0] = value;
// char const *call = "floattidf";
// if (is_type_unsigned(src)) {
// call = "floattidf_unsigned";
// }
// lbValue res_f64 = lb_emit_runtime_call(p, call, args);
// return lb_emit_conv(p, res_f64, t);
// }
bool is_signed = !is_type_unsigned(dst);
return cg_value(tb_inst_int2float(p->func, value.node, dt, is_signed), t);
}
if (is_type_simd_vector(dst)) {
GB_PANIC("TODO(bill): ? -> #simd vector");
}
// Pointer <-> uintptr
if (is_type_pointer(src) && is_type_uintptr(dst)) {
return cg_value(tb_inst_ptr2int(p->func, value.node, dt), t);
}
if (is_type_uintptr(src) && is_type_pointer(dst)) {
return cg_value(tb_inst_int2ptr(p->func, value.node), t);
}
if (is_type_multi_pointer(src) && is_type_uintptr(dst)) {
return cg_value(tb_inst_ptr2int(p->func, value.node, dt), t);
}
if (is_type_uintptr(src) && is_type_multi_pointer(dst)) {
return cg_value(tb_inst_int2ptr(p->func, value.node), t);
}
if (is_type_union(dst)) {
GB_PANIC("TODO(bill): ? -> union");
}
// NOTE(bill): This has to be done before 'Pointer <-> Pointer' as it's
// subtype polymorphism casting
if (check_is_assignable_to_using_subtype(src_type, t)) {
GB_PANIC("TODO(bill): ? -> subtyping");
}
// Pointer <-> Pointer
if (is_type_pointer(src) && is_type_pointer(dst)) {
return cg_value(tb_inst_bitcast(p->func, value.node, dt), t);
}
if (is_type_multi_pointer(src) && is_type_pointer(dst)) {
return cg_value(tb_inst_bitcast(p->func, value.node, dt), t);
}
if (is_type_pointer(src) && is_type_multi_pointer(dst)) {
return cg_value(tb_inst_bitcast(p->func, value.node, dt), t);
}
if (is_type_multi_pointer(src) && is_type_multi_pointer(dst)) {
return cg_value(tb_inst_bitcast(p->func, value.node, dt), t);
}
// proc <-> proc
if (is_type_proc(src) && is_type_proc(dst)) {
return cg_value(tb_inst_bitcast(p->func, value.node, dt), t);
}
// pointer -> proc
if (is_type_pointer(src) && is_type_proc(dst)) {
return cg_value(tb_inst_bitcast(p->func, value.node, dt), t);
}
// proc -> pointer
if (is_type_proc(src) && is_type_pointer(dst)) {
return cg_value(tb_inst_bitcast(p->func, value.node, dt), t);
}
// []byte/[]u8 <-> string
if (is_type_u8_slice(src) && is_type_string(dst)) {
return cg_emit_transmute(p, value, t);
}
if (is_type_string(src) && is_type_u8_slice(dst)) {
return cg_emit_transmute(p, value, t);
}
if (is_type_matrix(dst) && !is_type_matrix(src)) {
GB_PANIC("TODO(bill): !matrix -> matrix");
}
if (is_type_matrix(dst) && is_type_matrix(src)) {
GB_PANIC("TODO(bill): matrix -> matrix");
}
if (is_type_any(dst)) {
GB_PANIC("TODO(bill): ? -> any");
}
i64 src_sz = type_size_of(src);
i64 dst_sz = type_size_of(dst);
if (src_sz == dst_sz) {
// bit_set <-> integer
if (is_type_integer(src) && is_type_bit_set(dst)) {
cgValue v = cg_emit_conv(p, value, bit_set_to_int(dst));
return cg_emit_transmute(p, v, t);
}
if (is_type_bit_set(src) && is_type_integer(dst)) {
cgValue bs = cg_emit_transmute(p, value, bit_set_to_int(src));
return cg_emit_conv(p, bs, dst);
}
// typeid <-> integer
if (is_type_integer(src) && is_type_typeid(dst)) {
return cg_emit_transmute(p, value, dst);
}
if (is_type_typeid(src) && is_type_integer(dst)) {
return cg_emit_transmute(p, value, dst);
}
}
if (is_type_untyped(src)) {
if (is_type_string(src) && is_type_string(dst)) {
cgAddr result = cg_add_local(p, t, nullptr, false);
cg_addr_store(p, result, value);
return cg_addr_load(p, result);
}
}
gb_printf_err("%.*s\n", LIT(p->name));
gb_printf_err("cg_emit_conv: src -> dst\n");
gb_printf_err("Not Identical %s != %s\n", type_to_string(src_type), type_to_string(t));
gb_printf_err("Not Identical %s != %s\n", type_to_string(src), type_to_string(dst));
gb_printf_err("Not Identical %p != %p\n", src_type, t);
gb_printf_err("Not Identical %p != %p\n", src, dst);
GB_PANIC("Invalid type conversion: '%s' to '%s' for procedure '%.*s'",
type_to_string(src_type), type_to_string(t),
LIT(p->name));
return {};
}
gb_internal cgValue cg_emit_arith(cgProcedure *p, TokenKind op, cgValue lhs, cgValue rhs, Type *type) {
if (is_type_array_like(lhs.type) || is_type_array_like(rhs.type)) {
GB_PANIC("TODO(bill): cg_emit_arith_array");
} else if (is_type_matrix(lhs.type) || is_type_matrix(rhs.type)) {
GB_PANIC("TODO(bill): cg_emit_arith_matrix");
} else if (is_type_complex(type)) {
GB_PANIC("TODO(bill): cg_emit_arith complex");
} else if (is_type_quaternion(type)) {
GB_PANIC("TODO(bill): cg_emit_arith quaternion");
}
lhs = cg_flatten_value(p, cg_emit_conv(p, lhs, type));
rhs = cg_flatten_value(p, cg_emit_conv(p, rhs, type));
GB_ASSERT(lhs.kind == cgValue_Value);
GB_ASSERT(rhs.kind == cgValue_Value);
if (is_type_integer(type) && is_type_different_to_arch_endianness(type)) {
switch (op) {
case Token_AndNot:
case Token_And:
case Token_Or:
case Token_Xor:
goto handle_op;
}
Type *platform_type = integer_endian_type_to_platform_type(type);
cgValue x = cg_emit_byte_swap(p, lhs, integer_endian_type_to_platform_type(lhs.type));
cgValue y = cg_emit_byte_swap(p, rhs, integer_endian_type_to_platform_type(rhs.type));
cgValue res = cg_emit_arith(p, op, x, y, platform_type);
return cg_emit_byte_swap(p, res, type);
}
if (is_type_float(type) && is_type_different_to_arch_endianness(type)) {
Type *platform_type = integer_endian_type_to_platform_type(type);
cgValue x = cg_emit_conv(p, lhs, integer_endian_type_to_platform_type(lhs.type));
cgValue y = cg_emit_conv(p, rhs, integer_endian_type_to_platform_type(rhs.type));
cgValue res = cg_emit_arith(p, op, x, y, platform_type);
return cg_emit_byte_swap(p, res, type);
}
handle_op:;
// NOTE(bill): Bit Set Aliases for + and -
if (is_type_bit_set(type)) {
switch (op) {
case Token_Add: op = Token_Or; break;
case Token_Sub: op = Token_AndNot; break;
}
}
TB_ArithmeticBehavior arith_behavior = cast(TB_ArithmeticBehavior)50;
Type *integral_type = type;
if (is_type_simd_vector(integral_type)) {
GB_PANIC("TODO(bill): cg_emit_arith #simd vector");
// integral_type = core_array_type(integral_type);
}
switch (op) {
case Token_Add:
if (is_type_float(integral_type)) {
return cg_value(tb_inst_fadd(p->func, lhs.node, rhs.node), type);
}
return cg_value(tb_inst_add(p->func, lhs.node, rhs.node, arith_behavior), type);
case Token_Sub:
if (is_type_float(integral_type)) {
return cg_value(tb_inst_fsub(p->func, lhs.node, rhs.node), type);
}
return cg_value(tb_inst_sub(p->func, lhs.node, rhs.node, arith_behavior), type);
case Token_Mul:
if (is_type_float(integral_type)) {
return cg_value(tb_inst_fmul(p->func, lhs.node, rhs.node), type);
}
return cg_value(tb_inst_mul(p->func, lhs.node, rhs.node, arith_behavior), type);
case Token_Quo:
if (is_type_float(integral_type)) {
return cg_value(tb_inst_fdiv(p->func, lhs.node, rhs.node), type);
}
return cg_value(tb_inst_div(p->func, lhs.node, rhs.node, !is_type_unsigned(integral_type)), type);
case Token_Mod:
if (is_type_float(integral_type)) {
GB_PANIC("TODO(bill): float %% float");
}
return cg_value(tb_inst_mod(p->func, lhs.node, rhs.node, !is_type_unsigned(integral_type)), type);
case Token_ModMod:
if (is_type_unsigned(integral_type)) {
return cg_value(tb_inst_mod(p->func, lhs.node, rhs.node, false), type);
} else {
TB_Node *a = tb_inst_mod(p->func, lhs.node, rhs.node, true);
TB_Node *b = tb_inst_add(p->func, a, rhs.node, arith_behavior);
TB_Node *c = tb_inst_mod(p->func, b, rhs.node, true);
return cg_value(c, type);
}
case Token_And:
return cg_value(tb_inst_and(p->func, lhs.node, rhs.node), type);
case Token_Or:
return cg_value(tb_inst_or(p->func, lhs.node, rhs.node), type);
case Token_Xor:
return cg_value(tb_inst_xor(p->func, lhs.node, rhs.node), type);
case Token_Shl:
{
rhs = cg_emit_conv(p, rhs, lhs.type);
TB_DataType dt = cg_data_type(lhs.type);
TB_Node *lhsval = lhs.node;
TB_Node *bits = rhs.node;
TB_Node *bit_size = tb_inst_uint(p->func, dt, 8*type_size_of(lhs.type));
TB_Node *zero = tb_inst_uint(p->func, dt, 0);
TB_Node *width_test = tb_inst_cmp_ilt(p->func, bits, bit_size, false);
TB_Node *res = tb_inst_shl(p->func, lhsval, bits, arith_behavior);
res = tb_inst_select(p->func, width_test, res, zero);
return cg_value(res, type);
}
case Token_Shr:
{
rhs = cg_emit_conv(p, rhs, lhs.type);
TB_DataType dt = cg_data_type(lhs.type);
TB_Node *lhsval = lhs.node;
TB_Node *bits = rhs.node;
TB_Node *bit_size = tb_inst_uint(p->func, dt, 8*type_size_of(lhs.type));
TB_Node *zero = tb_inst_uint(p->func, dt, 0);
TB_Node *width_test = tb_inst_cmp_ilt(p->func, bits, bit_size, false);
TB_Node *res = nullptr;
if (is_type_unsigned(integral_type)) {
res = tb_inst_shr(p->func, lhsval, bits);
} else {
res = tb_inst_sar(p->func, lhsval, bits);
}
res = tb_inst_select(p->func, width_test, res, zero);
return cg_value(res, type);
}
case Token_AndNot:
return cg_value(tb_inst_and(p->func, lhs.node, tb_inst_not(p->func, rhs.node)), type);
}
GB_PANIC("unhandled operator of cg_emit_arith");
return {};
}
gb_internal cgAddr cg_build_addr_slice_expr(cgProcedure *p, Ast *expr) {
ast_node(se, SliceExpr, expr);
cgValue low = cg_const_int(p, t_int, 0);
cgValue high = {};
if (se->low != nullptr) {
low = cg_correct_endianness(p, cg_build_expr(p, se->low));
}
if (se->high != nullptr) {
high = cg_correct_endianness(p, cg_build_expr(p, se->high));
}
bool no_indices = se->low == nullptr && se->high == nullptr;
gb_unused(no_indices);
cgAddr addr = cg_build_addr(p, se->expr);
cgValue base = cg_addr_load(p, addr);
Type *type = base_type(base.type);
if (is_type_pointer(type)) {
type = base_type(type_deref(type));
addr = cg_addr(base);
base = cg_addr_load(p, addr);
}
switch (type->kind) {
case Type_Slice: {
// Type *slice_type = type;
// cgValue len = cg_slice_len(p, base);
// if (high.value == nullptr) high = len;
// if (!no_indices) {
// cg_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
// }
// cgValue elem = cg_emit_ptr_offset(p, cg_slice_elem(p, base), low);
// cgValue new_len = cg_emit_arith(p, Token_Sub, high, low, t_int);
// cgAddr slice = cg_add_local_generated(p, slice_type, false);
// cg_fill_slice(p, slice, elem, new_len);
// return slice;
GB_PANIC("cg_build_addr_slice_expr Type_Slice");
break;
}
case Type_RelativeSlice:
GB_PANIC("TODO(bill): Type_RelativeSlice should be handled above already on the cg_addr_load");
break;
case Type_DynamicArray: {
// Type *elem_type = type->DynamicArray.elem;
// Type *slice_type = alloc_type_slice(elem_type);
// lbValue len = lb_dynamic_array_len(p, base);
// if (high.value == nullptr) high = len;
// if (!no_indices) {
// lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
// }
// lbValue elem = lb_emit_ptr_offset(p, lb_dynamic_array_elem(p, base), low);
// lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
// lbAddr slice = lb_add_local_generated(p, slice_type, false);
// lb_fill_slice(p, slice, elem, new_len);
// return slice;
GB_PANIC("cg_build_addr_slice_expr Type_DynamicArray");
break;
}
case Type_MultiPointer: {
Type *res_type = type_of_expr(expr);
if (se->high == nullptr) {
cgAddr res = cg_add_local(p, res_type, nullptr, false);
GB_ASSERT(base.kind == cgValue_Value);
GB_ASSERT(low.kind == cgValue_Value);
i64 stride = type_size_of(type->MultiPointer.elem);
cgValue offset = cg_value(tb_inst_array_access(p->func, base.node, low.node, stride), base.type);
cg_addr_store(p, res, offset);
return res;
} else {
cgAddr res = cg_add_local(p, res_type, nullptr, true);
low = cg_emit_conv(p, low, t_int);
high = cg_emit_conv(p, high, t_int);
// cg_emit_multi_pointer_slice_bounds_check(p, se->open, low, high);
i64 stride = type_size_of(type->MultiPointer.elem);
TB_Node *offset = tb_inst_array_access(p->func, base.node, low.node, stride);
TB_Node *len = tb_inst_sub(p->func, high.node, low.node, cast(TB_ArithmeticBehavior)0);
TB_Node *data_ptr = tb_inst_member_access(p->func, res.addr.node, type_offset_of(res_type, 0));
TB_Node *len_ptr = tb_inst_member_access(p->func, res.addr.node, type_offset_of(res_type, 1));
tb_inst_store(p->func, TB_TYPE_PTR, data_ptr, offset, cast(TB_CharUnits)build_context.ptr_size, false);
tb_inst_store(p->func, TB_TYPE_INT, len_ptr, len, cast(TB_CharUnits)build_context.int_size, false);
return res;
}
}
case Type_Array: {
// Type *slice_type = alloc_type_slice(type->Array.elem);
// lbValue len = lb_const_int(p->module, t_int, type->Array.count);
// if (high.value == nullptr) high = len;
// bool low_const = type_and_value_of_expr(se->low).mode == Addressing_Constant;
// bool high_const = type_and_value_of_expr(se->high).mode == Addressing_Constant;
// if (!low_const || !high_const) {
// if (!no_indices) {
// lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
// }
// }
// lbValue elem = lb_emit_ptr_offset(p, lb_array_elem(p, lb_addr_get_ptr(p, addr)), low);
// lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
// lbAddr slice = lb_add_local_generated(p, slice_type, false);
// lb_fill_slice(p, slice, elem, new_len);
// return slice;
GB_PANIC("cg_build_addr_slice_expr Type_Array");
break;
}
case Type_Basic: {
// GB_ASSERT(type == t_string);
// lbValue len = lb_string_len(p, base);
// if (high.value == nullptr) high = len;
// if (!no_indices) {
// lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
// }
// lbValue elem = lb_emit_ptr_offset(p, lb_string_elem(p, base), low);
// lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
// lbAddr str = lb_add_local_generated(p, t_string, false);
// lb_fill_string(p, str, elem, new_len);
// return str;
GB_PANIC("cg_build_addr_slice_expr Type_Basic");
break;
}
case Type_Struct:
// if (is_type_soa_struct(type)) {
// lbValue len = lb_soa_struct_len(p, lb_addr_get_ptr(p, addr));
// if (high.value == nullptr) high = len;
// if (!no_indices) {
// lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr);
// }
// #if 1
// lbAddr dst = lb_add_local_generated(p, type_of_expr(expr), true);
// if (type->Struct.soa_kind == StructSoa_Fixed) {
// i32 field_count = cast(i32)type->Struct.fields.count;
// for (i32 i = 0; i < field_count; i++) {
// lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i);
// lbValue field_src = lb_emit_struct_ep(p, lb_addr_get_ptr(p, addr), i);
// field_src = lb_emit_array_ep(p, field_src, low);
// lb_emit_store(p, field_dst, field_src);
// }
// lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count);
// lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
// lb_emit_store(p, len_dst, new_len);
// } else if (type->Struct.soa_kind == StructSoa_Slice) {
// if (no_indices) {
// lb_addr_store(p, dst, base);
// } else {
// i32 field_count = cast(i32)type->Struct.fields.count - 1;
// for (i32 i = 0; i < field_count; i++) {
// lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i);
// lbValue field_src = lb_emit_struct_ev(p, base, i);
// field_src = lb_emit_ptr_offset(p, field_src, low);
// lb_emit_store(p, field_dst, field_src);
// }
// lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count);
// lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
// lb_emit_store(p, len_dst, new_len);
// }
// } else if (type->Struct.soa_kind == StructSoa_Dynamic) {
// i32 field_count = cast(i32)type->Struct.fields.count - 3;
// for (i32 i = 0; i < field_count; i++) {
// lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i);
// lbValue field_src = lb_emit_struct_ev(p, base, i);
// field_src = lb_emit_ptr_offset(p, field_src, low);
// lb_emit_store(p, field_dst, field_src);
// }
// lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count);
// lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int);
// lb_emit_store(p, len_dst, new_len);
// }
// return dst;
// #endif
// }
GB_PANIC("cg_build_addr_slice_expr Type_Struct");
break;
}
GB_PANIC("Unknown slicable type");
return {};
}
gb_internal cgValue cg_build_expr_internal(cgProcedure *p, Ast *expr);
gb_internal cgValue cg_build_expr(cgProcedure *p, Ast *expr) {
u16 prev_state_flags = p->state_flags;
defer (p->state_flags = prev_state_flags);
if (expr->state_flags != 0) {
u16 in = expr->state_flags;
u16 out = p->state_flags;
if (in & StateFlag_bounds_check) {
out |= StateFlag_bounds_check;
out &= ~StateFlag_no_bounds_check;
} else if (in & StateFlag_no_bounds_check) {
out |= StateFlag_no_bounds_check;
out &= ~StateFlag_bounds_check;
}
if (in & StateFlag_type_assert) {
out |= StateFlag_type_assert;
out &= ~StateFlag_no_type_assert;
} else if (in & StateFlag_no_type_assert) {
out |= StateFlag_no_type_assert;
out &= ~StateFlag_type_assert;
}
p->state_flags = out;
}
// IMPORTANT NOTE(bill):
// Selector Call Expressions (foo->bar(...))
// must only evaluate `foo` once as it gets transformed into
// `foo.bar(foo, ...)`
// And if `foo` is a procedure call or something more complex, storing the value
// once is a very good idea
// If a stored value is found, it must be removed from the cache
if (expr->state_flags & StateFlag_SelectorCallExpr) {
// cgValue *pp = map_get(&p->selector_values, expr);
// if (pp != nullptr) {
// cgValue res = *pp;
// map_remove(&p->selector_values, expr);
// return res;
// }
// cgAddr *pa = map_get(&p->selector_addr, expr);
// if (pa != nullptr) {
// cgAddr res = *pa;
// map_remove(&p->selector_addr, expr);
// return cg_addr_load(p, res);
// }
}
cgValue res = cg_build_expr_internal(p, expr);
if (res.kind == cgValue_Symbol) {
GB_ASSERT(is_type_internally_pointer_like(res.type));
res = cg_value(tb_inst_get_symbol_address(p->func, res.symbol), res.type);
}
if (expr->state_flags & StateFlag_SelectorCallExpr) {
// map_set(&p->selector_values, expr, res);
}
return res;
}
gb_internal cgValue cg_build_expr_internal(cgProcedure *p, Ast *expr) {
cgModule *m = p->module;
expr = unparen_expr(expr);
TokenPos expr_pos = ast_token(expr).pos;
TypeAndValue tv = type_and_value_of_expr(expr);
Type *type = type_of_expr(expr);
GB_ASSERT_MSG(tv.mode != Addressing_Invalid, "invalid expression '%s' (tv.mode = %d, tv.type = %s) @ %s\n Current Proc: %.*s : %s", expr_to_string(expr), tv.mode, type_to_string(tv.type), token_pos_to_string(expr_pos), LIT(p->name), type_to_string(p->type));
if (tv.value.kind != ExactValue_Invalid) {
// NOTE(bill): The commented out code below is just for debug purposes only
// if (is_type_untyped(type)) {
// gb_printf_err("%s %s : %s @ %p\n", token_pos_to_string(expr_pos), expr_to_string(expr), type_to_string(expr->tav.type), expr);
// GB_PANIC("%s\n", type_to_string(tv.type));
// }
// NOTE(bill): Short on constant values
return cg_const_value(p, type, tv.value);
} else if (tv.mode == Addressing_Type) {
// NOTE(bill, 2023-01-16): is this correct? I hope so at least
return cg_typeid(m, tv.type);
}
switch (expr->kind) {
case_ast_node(bl, BasicLit, expr);
TokenPos pos = bl->token.pos;
GB_PANIC("Non-constant basic literal %s - %.*s", token_pos_to_string(pos), LIT(token_strings[bl->token.kind]));
case_end;
case_ast_node(bd, BasicDirective, expr);
TokenPos pos = bd->token.pos;
GB_PANIC("Non-constant basic literal %s - %.*s", token_pos_to_string(pos), LIT(bd->name.string));
case_end;
case_ast_node(i, Ident, expr);
Entity *e = entity_from_expr(expr);
e = strip_entity_wrapping(e);
GB_ASSERT_MSG(e != nullptr, "%s in %.*s %p", expr_to_string(expr), LIT(p->name), expr);
if (e->kind == Entity_Builtin) {
Token token = ast_token(expr);
GB_PANIC("TODO(bill): lb_build_expr Entity_Builtin '%.*s'\n"
"\t at %s", LIT(builtin_procs[e->Builtin.id].name),
token_pos_to_string(token.pos));
return {};
} else if (e->kind == Entity_Nil) {
GB_PANIC("TODO: cg_find_ident nil");
// TODO(bill): is this correct?
return cg_value(cast(TB_Node *)nullptr, e->type);
}
GB_ASSERT(e->kind != Entity_ProcGroup);
cgAddr *addr = map_get(&p->variable_map, e);
if (addr) {
return cg_addr_load(p, *addr);
}
// return cg_find_ident(p, m, e, expr);
GB_PANIC("TODO: cg_find_ident");
return {};
case_end;
case_ast_node(i, Implicit, expr);
return cg_addr_load(p, cg_build_addr(p, expr));
case_end;
case_ast_node(u, Uninit, expr);
if (is_type_untyped(type)) {
return cg_value(cast(TB_Node *)nullptr, t_untyped_uninit);
}
return cg_value(tb_inst_poison(p->func), type);
case_end;
case_ast_node(de, DerefExpr, expr);
return cg_addr_load(p, cg_build_addr(p, expr));
case_end;
case_ast_node(se, SelectorExpr, expr);
TypeAndValue tav = type_and_value_of_expr(expr);
GB_ASSERT(tav.mode != Addressing_Invalid);
return cg_addr_load(p, cg_build_addr(p, expr));
case_end;
case_ast_node(ise, ImplicitSelectorExpr, expr);
TypeAndValue tav = type_and_value_of_expr(expr);
GB_ASSERT(tav.mode == Addressing_Constant);
return cg_const_value(p, type, tv.value);
case_end;
case_ast_node(se, SelectorCallExpr, expr);
GB_ASSERT(se->modified_call);
return cg_build_call_expr(p, se->call);
case_end;
case_ast_node(i, CallExpr, expr);
return cg_build_call_expr(p, expr);
case_end;
case_ast_node(te, TernaryIfExpr, expr);
GB_PANIC("TODO(bill): TernaryIfExpr");
case_end;
case_ast_node(te, TernaryWhenExpr, expr);
TypeAndValue tav = type_and_value_of_expr(te->cond);
GB_ASSERT(tav.mode == Addressing_Constant);
GB_ASSERT(tav.value.kind == ExactValue_Bool);
if (tav.value.value_bool) {
return cg_build_expr(p, te->x);
} else {
return cg_build_expr(p, te->y);
}
case_end;
case_ast_node(tc, TypeCast, expr);
cgValue e = cg_build_expr(p, tc->expr);
switch (tc->token.kind) {
case Token_cast:
return cg_emit_conv(p, e, type);
case Token_transmute:
return cg_emit_transmute(p, e, type);
}
GB_PANIC("Invalid AST TypeCast");
case_end;
case_ast_node(ac, AutoCast, expr);
cgValue value = cg_build_expr(p, ac->expr);
return cg_emit_conv(p, value, type);
case_end;
case_ast_node(se, SliceExpr, expr);
if (is_type_slice(type_of_expr(se->expr))) {
// NOTE(bill): Quick optimization
if (se->high == nullptr &&
(se->low == nullptr || cg_is_expr_constant_zero(se->low))) {
return cg_build_expr(p, se->expr);
}
}
return cg_addr_load(p, cg_build_addr(p, expr));
case_end;
case_ast_node(ie, IndexExpr, expr);
return cg_addr_load(p, cg_build_addr(p, expr));
case_end;
case_ast_node(ie, MatrixIndexExpr, expr);
return cg_addr_load(p, cg_build_addr(p, expr));
case_end;
}
GB_PANIC("TODO(bill): cg_build_expr_internal %.*s", LIT(ast_strings[expr->kind]));
return {};
}
gb_internal cgAddr cg_build_addr_internal(cgProcedure *p, Ast *expr);
gb_internal cgAddr cg_build_addr(cgProcedure *p, Ast *expr) {
expr = unparen_expr(expr);
// IMPORTANT NOTE(bill):
// Selector Call Expressions (foo->bar(...))
// must only evaluate `foo` once as it gets transformed into
// `foo.bar(foo, ...)`
// And if `foo` is a procedure call or something more complex, storing the value
// once is a very good idea
// If a stored value is found, it must be removed from the cache
if (expr->state_flags & StateFlag_SelectorCallExpr) {
// lbAddr *pp = map_get(&p->selector_addr, expr);
// if (pp != nullptr) {
// lbAddr res = *pp;
// map_remove(&p->selector_addr, expr);
// return res;
// }
}
cgAddr addr = cg_build_addr_internal(p, expr);
if (expr->state_flags & StateFlag_SelectorCallExpr) {
// map_set(&p->selector_addr, expr, addr);
}
return addr;
}
gb_internal cgAddr cg_build_addr_internal(cgProcedure *p, Ast *expr) {
switch (expr->kind) {
case_ast_node(i, Implicit, expr);
cgAddr v = {};
switch (i->kind) {
case Token_context:
v = cg_find_or_generate_context_ptr(p);
break;
}
GB_ASSERT(v.addr.node != nullptr);
return v;
case_end;
case_ast_node(i, Ident, expr);
if (is_blank_ident(expr)) {
cgAddr val = {};
return val;
}
String name = i->token.string;
Entity *e = entity_of_node(expr);
return cg_build_addr_from_entity(p, e, expr);
case_end;
case_ast_node(se, SliceExpr, expr);
return cg_build_addr_slice_expr(p, expr);
case_end;
case_ast_node(se, SelectorExpr, expr);
Ast *sel_node = unparen_expr(se->selector);
if (sel_node->kind != Ast_Ident) {
GB_PANIC("Unsupported selector expression");
}
String selector = sel_node->Ident.token.string;
TypeAndValue tav = type_and_value_of_expr(se->expr);
if (tav.mode == Addressing_Invalid) {
// NOTE(bill): Imports
Entity *imp = entity_of_node(se->expr);
if (imp != nullptr) {
GB_ASSERT(imp->kind == Entity_ImportName);
}
return cg_build_addr(p, unparen_expr(se->selector));
}
Type *type = base_type(tav.type);
if (tav.mode == Addressing_Type) { // Addressing_Type
Selection sel = lookup_field(tav.type, selector, true);
if (sel.pseudo_field) {
GB_ASSERT(sel.entity->kind == Entity_Procedure);
return cg_addr(cg_find_value_from_entity(p->module, sel.entity));
}
GB_PANIC("Unreachable %.*s", LIT(selector));
}
if (se->swizzle_count > 0) {
Type *array_type = base_type(type_deref(tav.type));
GB_ASSERT(array_type->kind == Type_Array);
u8 swizzle_count = se->swizzle_count;
u8 swizzle_indices_raw = se->swizzle_indices;
u8 swizzle_indices[4] = {};
for (u8 i = 0; i < swizzle_count; i++) {
u8 index = swizzle_indices_raw>>(i*2) & 3;
swizzle_indices[i] = index;
}
cgValue a = {};
if (is_type_pointer(tav.type)) {
a = cg_build_expr(p, se->expr);
} else {
cgAddr addr = cg_build_addr(p, se->expr);
a = cg_addr_get_ptr(p, addr);
}
GB_ASSERT(is_type_array(expr->tav.type));
GB_PANIC("TODO(bill): cg_addr_swizzle");
// return cg_addr_swizzle(a, expr->tav.type, swizzle_count, swizzle_indices);
}
Selection sel = lookup_field(type, selector, false);
GB_ASSERT(sel.entity != nullptr);
if (sel.pseudo_field) {
GB_ASSERT(sel.entity->kind == Entity_Procedure);
Entity *e = entity_of_node(sel_node);
return cg_addr(cg_find_value_from_entity(p->module, e));
}
{
cgAddr addr = cg_build_addr(p, se->expr);
if (addr.kind == cgAddr_Map) {
cgValue v = cg_addr_load(p, addr);
cgValue a = cg_address_from_load_or_generate_local(p, v);
a = cg_emit_deep_field_gep(p, a, sel);
return cg_addr(a);
} else if (addr.kind == cgAddr_Context) {
GB_ASSERT(sel.index.count > 0);
if (addr.ctx.sel.index.count >= 0) {
sel = selection_combine(addr.ctx.sel, sel);
}
addr.ctx.sel = sel;
addr.kind = cgAddr_Context;
return addr;
} else if (addr.kind == cgAddr_SoaVariable) {
cgValue index = addr.soa.index;
i64 first_index = sel.index[0];
Selection sub_sel = sel;
sub_sel.index.data += 1;
sub_sel.index.count -= 1;
cgValue arr = cg_emit_struct_ep(p, addr.addr, first_index);
Type *t = base_type(type_deref(addr.addr.type));
GB_ASSERT(is_type_soa_struct(t));
// TODO(bill): bounds checking for soa variable
// if (addr.soa.index_expr != nullptr && (!cg_is_const(addr.soa.index) || t->Struct.soa_kind != StructSoa_Fixed)) {
// cgValue len = cg_soa_struct_len(p, addr.addr);
// cg_emit_bounds_check(p, ast_token(addr.soa.index_expr), addr.soa.index, len);
// }
cgValue item = {};
if (t->Struct.soa_kind == StructSoa_Fixed) {
item = cg_emit_array_ep(p, arr, index);
} else {
item = cg_emit_ptr_offset(p, cg_emit_load(p, arr), index);
}
if (sub_sel.index.count > 0) {
item = cg_emit_deep_field_gep(p, item, sub_sel);
}
return cg_addr(item);
} else if (addr.kind == cgAddr_Swizzle) {
GB_ASSERT(sel.index.count > 0);
// NOTE(bill): just patch the index in place
sel.index[0] = addr.swizzle.indices[sel.index[0]];
} else if (addr.kind == cgAddr_SwizzleLarge) {
GB_ASSERT(sel.index.count > 0);
// NOTE(bill): just patch the index in place
sel.index[0] = addr.swizzle.indices[sel.index[0]];
}
cgValue a = cg_addr_get_ptr(p, addr);
a = cg_emit_deep_field_gep(p, a, sel);
return cg_addr(a);
}
case_end;
}
TokenPos token_pos = ast_token(expr).pos;
GB_PANIC("Unexpected address expression\n"
"\tAst: %.*s @ "
"%s\n",
LIT(ast_strings[expr->kind]),
token_pos_to_string(token_pos));
return {};
}
|