Repository navigation
Expand file tree
/
Copy pathtilde_expr.cpp
More file actions
3961 lines (3320 loc) · 125 KB
/
Copy pathtilde_expr.cpp
File metadata and controls
3961 lines (3320 loc) · 125 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
gb_internal cgValue cg_flatten_value(cgProcedure *p, cgValue value) {
GB_ASSERT(value.kind != cgValue_Multi);
if (value.kind == cgValue_Symbol) {
GB_ASSERT(is_type_internally_pointer_like(value.type));
return cg_value(tb_inst_get_symbol_address(p->func, value.symbol), value.type);
} else if (value.kind == cgValue_Addr) {
// TODO(bill): Is this a good idea?
// this converts an lvalue to an rvalue if trivially possible
TB_DataType dt = cg_data_type(value.type);
if (!TB_IS_VOID_TYPE(dt)) {
TB_CharUnits align = cast(TB_CharUnits)type_align_of(value.type);
return cg_value(tb_inst_load(p->func, dt, value.node, align, false), value.type);
}
}
return value;
}
gb_internal cgValue cg_emit_select(cgProcedure *p, cgValue const &cond, cgValue const &x, cgValue const &y) {
GB_ASSERT(x.kind == y.kind);
GB_ASSERT(cond.kind == cgValue_Value);
cgValue res = x;
res.node = tb_inst_select(p->func, cond.node, x.node, y.node);
return res;
}
gb_internal bool cg_is_expr_untyped_const(Ast *expr) {
auto const &tv = type_and_value_of_expr(expr);
if (is_type_untyped(tv.type)) {
return tv.value.kind != ExactValue_Invalid;
}
return false;
}
gb_internal cgValue cg_expr_untyped_const_to_typed(cgProcedure *p, Ast *expr, Type *t) {
GB_ASSERT(is_type_typed(t));
auto const &tv = type_and_value_of_expr(expr);
return cg_const_value(p, t, tv.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_function_attrib_variable(p->func, c.addr.node, nullptr, -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 cgValue cg_get_using_variable(cgProcedure *p, Entity *e) {
GB_ASSERT(e->kind == Entity_Variable && e->flags & EntityFlag_Using);
String name = e->token.string;
Entity *parent = e->using_parent;
Selection sel = lookup_field(parent->type, name, false);
GB_ASSERT(sel.entity != nullptr);
cgValue *pv = map_get(&p->module->values, parent);
cgValue v = {};
if (pv == nullptr && parent->flags & EntityFlag_SoaPtrField) {
// NOTE(bill): using SOA value (probably from for-in statement)
GB_PANIC("TODO(bill): cg_get_soa_variable_addr");
// cgAddr parent_addr = cg_get_soa_variable_addr(p, parent);
// v = cg_addr_get_ptr(p, parent_addr);
} else if (pv != nullptr) {
v = *pv;
} else {
GB_ASSERT_MSG(e->using_expr != nullptr, "%.*s %.*s", LIT(e->token.string), LIT(name));
v = cg_build_addr_ptr(p, e->using_expr);
}
GB_ASSERT(v.node != nullptr);
GB_ASSERT_MSG(parent->type == type_deref(v.type), "%s %s", type_to_string(parent->type), type_to_string(v.type));
cgValue ptr = cg_emit_deep_field_gep(p, v, sel);
// if (parent->scope) {
// if ((parent->scope->flags & (ScopeFlag_File|ScopeFlag_Pkg)) == 0) {
// cg_add_debug_local_variable(p, ptr.value, e->type, e->token);
// }
// } else {
// cg_add_debug_local_variable(p, ptr.value, e->type, e->token);
// }
return ptr;
}
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) {
// NOTE(bill): Calculate the using variable every time
v = cg_get_using_variable(p, e);
} else if (e->flags & EntityFlag_SoaPtrField) {
return map_must_get(&p->soa_values_map, 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_emit_union_tag_ptr(cgProcedure *p, cgValue const &parent_ptr) {
Type *t = parent_ptr.type;
Type *ut = base_type(type_deref(t));
GB_ASSERT_MSG(is_type_pointer(t), "%s", type_to_string(t));
GB_ASSERT_MSG(ut->kind == Type_Union, "%s", type_to_string(t));
GB_ASSERT(!is_type_union_maybe_pointer_original_alignment(ut));
GB_ASSERT(!is_type_union_maybe_pointer(ut));
GB_ASSERT(type_size_of(ut) > 0);
Type *tag_type = union_tag_type(ut);
i64 tag_offset = ut->Union.variant_block_size;
GB_ASSERT(parent_ptr.kind == cgValue_Value);
TB_Node *ptr = parent_ptr.node;
TB_Node *tag_ptr = tb_inst_member_access(p->func, ptr, tag_offset);
return cg_value(tag_ptr, alloc_type_pointer(tag_type));
}
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_MSG(!TB_IS_VOID_TYPE(dt), "%d %s -> %s", dt.type, type_to_string(value.type), type_to_string(type));
value.type = type;
if (value.node->dt.raw != dt.raw) {
switch (value.node->dt.type) {
case TB_INT:
switch (value.node->dt.type) {
case TB_INT:
break;
case TB_FLOAT:
value.node = tb_inst_bitcast(p->func, value.node, dt);
break;
case TB_PTR:
value.node = tb_inst_int2ptr(p->func, value.node);
break;
}
break;
case TB_FLOAT:
switch (value.node->dt.type) {
case TB_INT:
value.node = tb_inst_bitcast(p->func, value.node, dt);
break;
case TB_FLOAT:
break;
case TB_PTR:
value.node = tb_inst_bitcast(p->func, value.node, TB_TYPE_INTPTR);
value.node = tb_inst_int2ptr(p->func, value.node);
break;
}
break;
case TB_PTR:
switch (value.node->dt.type) {
case TB_INT:
value.node = tb_inst_ptr2int(p->func, value.node, dt);
break;
case TB_FLOAT:
value.node = tb_inst_ptr2int(p->func, value.node, TB_TYPE_INTPTR);
value.node = tb_inst_bitcast(p->func, value.node, dt);
break;
case TB_PTR:
break;
}
break;
}
}
return value;
case cgValue_Addr:
value.type = type;
return value;
case cgValue_Symbol:
GB_PANIC("should be handled above");
break;
case cgValue_Multi:
GB_PANIC("cannot transmute multiple values at once");
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);
// TODO(bill): bswap
// value.node = tb_inst_bswap(p->func, value.node);
return cg_emit_transmute(p, value, end_type);
}
gb_internal cgValue cg_emit_comp_records(cgProcedure *p, TokenKind op_kind, cgValue left, cgValue right, Type *type) {
GB_ASSERT((is_type_struct(type) || is_type_union(type)) && is_type_comparable(type));
cgValue left_ptr = cg_address_from_load_or_generate_local(p, left);
cgValue right_ptr = cg_address_from_load_or_generate_local(p, right);
cgValue res = {};
if (type_size_of(type) == 0) {
switch (op_kind) {
case Token_CmpEq:
return cg_const_bool(p, t_bool, true);
case Token_NotEq:
return cg_const_bool(p, t_bool, false);
}
GB_PANIC("invalid operator");
}
TEMPORARY_ALLOCATOR_GUARD();
if (is_type_simple_compare(type)) {
// TODO(bill): Test to see if this is actually faster!!!!
auto args = slice_make<cgValue>(temporary_allocator(), 3);
args[0] = cg_emit_conv(p, left_ptr, t_rawptr);
args[1] = cg_emit_conv(p, right_ptr, t_rawptr);
args[2] = cg_const_int(p, t_int, type_size_of(type));
res = cg_emit_runtime_call(p, "memory_equal", args);
} else {
cgProcedure *equal_proc = cg_equal_proc_for_type(p->module, type);
cgValue value = cg_value(tb_inst_get_symbol_address(p->func, equal_proc->symbol), equal_proc->type);
auto args = slice_make<cgValue>(temporary_allocator(), 2);
args[0] = cg_emit_conv(p, left_ptr, t_rawptr);
args[1] = cg_emit_conv(p, right_ptr, t_rawptr);
res = cg_emit_call(p, value, args);
}
if (op_kind == Token_NotEq) {
res = cg_emit_unary_arith(p, Token_Not, res, res.type);
}
return res;
}
gb_internal cgValue cg_emit_comp(cgProcedure *p, TokenKind op_kind, cgValue left, cgValue right) {
GB_ASSERT(gb_is_between(op_kind, Token__ComparisonBegin+1, Token__ComparisonEnd-1));
Type *a = core_type(left.type);
Type *b = core_type(right.type);
cgValue nil_check = {};
if (is_type_array_like(left.type) || is_type_array_like(right.type)) {
// don't do `nil` check if it is array-like
} else if (is_type_untyped_nil(left.type)) {
nil_check = cg_emit_comp_against_nil(p, op_kind, right);
} else if (is_type_untyped_nil(right.type)) {
nil_check = cg_emit_comp_against_nil(p, op_kind, left);
}
if (nil_check.node != nullptr) {
return nil_check;
}
if (are_types_identical(a, b)) {
// NOTE(bill): No need for a conversion
} /*else if (cg_is_const(left) || cg_is_const_nil(left)) {
left = cg_emit_conv(p, left, right.type);
} else if (cg_is_const(right) || cg_is_const_nil(right)) {
right = cg_emit_conv(p, right, left.type);
}*/ else {
Type *lt = left.type;
Type *rt = right.type;
lt = left.type;
rt = right.type;
i64 ls = type_size_of(lt);
i64 rs = type_size_of(rt);
// NOTE(bill): Quick heuristic, larger types are usually the target type
if (ls < rs) {
left = cg_emit_conv(p, left, rt);
} else if (ls > rs) {
right = cg_emit_conv(p, right, lt);
} else {
if (is_type_union(rt)) {
left = cg_emit_conv(p, left, rt);
} else {
right = cg_emit_conv(p, right, lt);
}
}
}
a = core_type(left.type);
b = core_type(right.type);
left = cg_flatten_value(p, left);
right = cg_flatten_value(p, right);
if (is_type_matrix(a) && (op_kind == Token_CmpEq || op_kind == Token_NotEq)) {
GB_PANIC("TODO(bill): cg_emit_comp matrix");
// Type *tl = base_type(a);
// lbValue lhs = lb_address_from_load_or_generate_local(p, left);
// lbValue rhs = lb_address_from_load_or_generate_local(p, right);
// // TODO(bill): Test to see if this is actually faster!!!!
// auto args = array_make<lbValue>(permanent_allocator(), 3);
// args[0] = lb_emit_conv(p, lhs, t_rawptr);
// args[1] = lb_emit_conv(p, rhs, t_rawptr);
// args[2] = lb_const_int(p->module, t_int, type_size_of(tl));
// lbValue val = lb_emit_runtime_call(p, "memory_compare", args);
// lbValue res = lb_emit_comp(p, op_kind, val, lb_const_nil(p->module, val.type));
// return lb_emit_conv(p, res, t_bool);
}
if (is_type_array_like(a)) {
GB_PANIC("TODO(bill): cg_emit_comp is_type_array_like");
// Type *tl = base_type(a);
// lbValue lhs = lb_address_from_load_or_generate_local(p, left);
// lbValue rhs = lb_address_from_load_or_generate_local(p, right);
// TokenKind cmp_op = Token_And;
// lbValue res = lb_const_bool(p->module, t_bool, true);
// if (op_kind == Token_NotEq) {
// res = lb_const_bool(p->module, t_bool, false);
// cmp_op = Token_Or;
// } else if (op_kind == Token_CmpEq) {
// res = lb_const_bool(p->module, t_bool, true);
// cmp_op = Token_And;
// }
// bool inline_array_arith = lb_can_try_to_inline_array_arith(tl);
// i32 count = 0;
// switch (tl->kind) {
// case Type_Array: count = cast(i32)tl->Array.count; break;
// case Type_EnumeratedArray: count = cast(i32)tl->EnumeratedArray.count; break;
// }
// if (inline_array_arith) {
// // inline
// lbAddr val = lb_add_local_generated(p, t_bool, false);
// lb_addr_store(p, val, res);
// for (i32 i = 0; i < count; i++) {
// lbValue x = lb_emit_load(p, lb_emit_array_epi(p, lhs, i));
// lbValue y = lb_emit_load(p, lb_emit_array_epi(p, rhs, i));
// lbValue cmp = lb_emit_comp(p, op_kind, x, y);
// lbValue new_res = lb_emit_arith(p, cmp_op, lb_addr_load(p, val), cmp, t_bool);
// lb_addr_store(p, val, lb_emit_conv(p, new_res, t_bool));
// }
// return lb_addr_load(p, val);
// } else {
// if (is_type_simple_compare(tl) && (op_kind == Token_CmpEq || op_kind == Token_NotEq)) {
// // TODO(bill): Test to see if this is actually faster!!!!
// auto args = array_make<lbValue>(permanent_allocator(), 3);
// args[0] = lb_emit_conv(p, lhs, t_rawptr);
// args[1] = lb_emit_conv(p, rhs, t_rawptr);
// args[2] = lb_const_int(p->module, t_int, type_size_of(tl));
// lbValue val = lb_emit_runtime_call(p, "memory_compare", args);
// lbValue res = lb_emit_comp(p, op_kind, val, lb_const_nil(p->module, val.type));
// return lb_emit_conv(p, res, t_bool);
// } else {
// lbAddr val = lb_add_local_generated(p, t_bool, false);
// lb_addr_store(p, val, res);
// auto loop_data = lb_loop_start(p, count, t_i32);
// {
// lbValue i = loop_data.idx;
// lbValue x = lb_emit_load(p, lb_emit_array_ep(p, lhs, i));
// lbValue y = lb_emit_load(p, lb_emit_array_ep(p, rhs, i));
// lbValue cmp = lb_emit_comp(p, op_kind, x, y);
// lbValue new_res = lb_emit_arith(p, cmp_op, lb_addr_load(p, val), cmp, t_bool);
// lb_addr_store(p, val, lb_emit_conv(p, new_res, t_bool));
// }
// lb_loop_end(p, loop_data);
// return lb_addr_load(p, val);
// }
// }
}
if ((is_type_struct(a) || is_type_union(a)) && is_type_comparable(a)) {
return cg_emit_comp_records(p, op_kind, left, right, a);
}
if ((is_type_struct(b) || is_type_union(b)) && is_type_comparable(b)) {
return cg_emit_comp_records(p, op_kind, left, right, b);
}
if (is_type_string(a)) {
if (is_type_cstring(a)) {
left = cg_emit_conv(p, left, t_string);
right = cg_emit_conv(p, right, t_string);
}
char const *runtime_procedure = nullptr;
switch (op_kind) {
case Token_CmpEq: runtime_procedure = "string_eq"; break;
case Token_NotEq: runtime_procedure = "string_ne"; break;
case Token_Lt: runtime_procedure = "string_lt"; break;
case Token_Gt: runtime_procedure = "string_gt"; break;
case Token_LtEq: runtime_procedure = "string_le"; break;
case Token_GtEq: runtime_procedure = "string_gt"; break;
}
GB_ASSERT(runtime_procedure != nullptr);
auto args = slice_make<cgValue>(permanent_allocator(), 2);
args[0] = left;
args[1] = right;
return cg_emit_runtime_call(p, runtime_procedure, args);
}
if (is_type_complex(a)) {
char const *runtime_procedure = "";
i64 sz = 8*type_size_of(a);
switch (sz) {
case 32:
switch (op_kind) {
case Token_CmpEq: runtime_procedure = "complex32_eq"; break;
case Token_NotEq: runtime_procedure = "complex32_ne"; break;
}
break;
case 64:
switch (op_kind) {
case Token_CmpEq: runtime_procedure = "complex64_eq"; break;
case Token_NotEq: runtime_procedure = "complex64_ne"; break;
}
break;
case 128:
switch (op_kind) {
case Token_CmpEq: runtime_procedure = "complex128_eq"; break;
case Token_NotEq: runtime_procedure = "complex128_ne"; break;
}
break;
}
GB_ASSERT(runtime_procedure != nullptr);
GB_PANIC("TODO(bill): cg_emit_runtime_call");
// auto args = array_make<lbValue>(permanent_allocator(), 2);
// args[0] = left;
// args[1] = right;
// return lb_emit_runtime_call(p, runtime_procedure, args);
}
if (is_type_quaternion(a)) {
char const *runtime_procedure = "";
i64 sz = 8*type_size_of(a);
switch (sz) {
case 64:
switch (op_kind) {
case Token_CmpEq: runtime_procedure = "quaternion64_eq"; break;
case Token_NotEq: runtime_procedure = "quaternion64_ne"; break;
}
break;
case 128:
switch (op_kind) {
case Token_CmpEq: runtime_procedure = "quaternion128_eq"; break;
case Token_NotEq: runtime_procedure = "quaternion128_ne"; break;
}
break;
case 256:
switch (op_kind) {
case Token_CmpEq: runtime_procedure = "quaternion256_eq"; break;
case Token_NotEq: runtime_procedure = "quaternion256_ne"; break;
}
break;
}
GB_ASSERT(runtime_procedure != nullptr);
GB_PANIC("TODO(bill): cg_emit_runtime_call");
// auto args = array_make<lbValue>(permanent_allocator(), 2);
// args[0] = left;
// args[1] = right;
// return lb_emit_runtime_call(p, runtime_procedure, args);
}
if (is_type_bit_set(a)) {
switch (op_kind) {
case Token_Lt:
case Token_LtEq:
case Token_Gt:
case Token_GtEq:
{
Type *it = bit_set_to_int(a);
cgValue lhs = cg_emit_transmute(p, left, it);
cgValue rhs = cg_emit_transmute(p, right, it);
cgValue res = cg_emit_arith(p, Token_And, lhs, rhs, it);
GB_ASSERT(lhs.kind == cgValue_Value);
GB_ASSERT(rhs.kind == cgValue_Value);
GB_ASSERT(res.kind == cgValue_Value);
if (op_kind == Token_Lt || op_kind == Token_LtEq) {
// (lhs & rhs) == lhs
res = cg_value(tb_inst_cmp_eq(p->func, res.node, lhs.node), t_bool);
} else if (op_kind == Token_Gt || op_kind == Token_GtEq) {
// (lhs & rhs) == rhs
res = cg_value(tb_inst_cmp_eq(p->func, res.node, rhs.node), t_bool);
}
// NOTE(bill): Strict subsets
if (op_kind == Token_Lt || op_kind == Token_Gt) {
// res &~ (lhs == rhs)
cgValue eq = cg_value(tb_inst_cmp_eq(p->func, lhs.node, rhs.node), t_bool);
res = cg_emit_arith(p, Token_AndNot, res, eq, t_bool);
}
return res;
}
case Token_CmpEq:
GB_ASSERT(left.kind == cgValue_Value);
GB_ASSERT(right.kind == cgValue_Value);
return cg_value(tb_inst_cmp_eq(p->func, left.node, right.node), t_bool);
case Token_NotEq:
GB_ASSERT(left.kind == cgValue_Value);
GB_ASSERT(right.kind == cgValue_Value);
return cg_value(tb_inst_cmp_ne(p->func, left.node, right.node), t_bool);
}
}
if (op_kind != Token_CmpEq && op_kind != Token_NotEq) {
Type *t = left.type;
if (is_type_integer(t) && is_type_different_to_arch_endianness(t)) {
Type *platform_type = integer_endian_type_to_platform_type(t);
cgValue x = cg_emit_byte_swap(p, left, platform_type);
cgValue y = cg_emit_byte_swap(p, right, platform_type);
left = x;
right = y;
} else if (is_type_float(t) && is_type_different_to_arch_endianness(t)) {
Type *platform_type = integer_endian_type_to_platform_type(t);
cgValue x = cg_emit_conv(p, left, platform_type);
cgValue y = cg_emit_conv(p, right, platform_type);
left = x;
right = y;
}
}
a = core_type(left.type);
b = core_type(right.type);
if (is_type_integer(a) ||
is_type_boolean(a) ||
is_type_pointer(a) ||
is_type_multi_pointer(a) ||
is_type_proc(a) ||
is_type_enum(a) ||
is_type_typeid(a)) {
TB_Node *lhs = left.node;
TB_Node *rhs = right.node;
TB_Node *res = nullptr;
bool is_signed = is_type_integer(left.type) && !is_type_unsigned(left.type);
switch (op_kind) {
case Token_CmpEq: res = tb_inst_cmp_eq(p->func, lhs, rhs); break;
case Token_NotEq: res = tb_inst_cmp_ne(p->func, lhs, rhs); break;
case Token_Gt: res = tb_inst_cmp_igt(p->func, lhs, rhs, is_signed); break;
case Token_GtEq: res = tb_inst_cmp_ige(p->func, lhs, rhs, is_signed); break;
case Token_Lt: res = tb_inst_cmp_ilt(p->func, lhs, rhs, is_signed); break;
case Token_LtEq: res = tb_inst_cmp_ile(p->func, lhs, rhs, is_signed); break;
}
GB_ASSERT(res != nullptr);
return cg_value(res, t_bool);
} else if (is_type_float(a)) {
TB_Node *lhs = left.node;
TB_Node *rhs = right.node;
TB_Node *res = nullptr;
switch (op_kind) {
case Token_CmpEq: res = tb_inst_cmp_eq(p->func, lhs, rhs); break;
case Token_NotEq: res = tb_inst_cmp_ne(p->func, lhs, rhs); break;
case Token_Gt: res = tb_inst_cmp_fgt(p->func, lhs, rhs); break;
case Token_GtEq: res = tb_inst_cmp_fge(p->func, lhs, rhs); break;
case Token_Lt: res = tb_inst_cmp_flt(p->func, lhs, rhs); break;
case Token_LtEq: res = tb_inst_cmp_fle(p->func, lhs, rhs); break;
}
GB_ASSERT(res != nullptr);
return cg_value(res, t_bool);
} else if (is_type_simd_vector(a)) {
GB_PANIC("TODO(bill): #simd vector");
// LLVMValueRef mask = nullptr;
// Type *elem = base_array_type(a);
// if (is_type_float(elem)) {
// LLVMRealPredicate pred = {};
// switch (op_kind) {
// case Token_CmpEq: pred = LLVMRealOEQ; break;
// case Token_NotEq: pred = LLVMRealONE; break;
// }
// mask = LLVMBuildFCmp(p->builder, pred, left.value, right.value, "");
// } else {
// LLVMIntPredicate pred = {};
// switch (op_kind) {
// case Token_CmpEq: pred = LLVMIntEQ; break;
// case Token_NotEq: pred = LLVMIntNE; break;
// }
// mask = LLVMBuildICmp(p->builder, pred, left.value, right.value, "");
// }
// GB_ASSERT_MSG(mask != nullptr, "Unhandled comparison kind %s (%s) %.*s %s (%s)", type_to_string(left.type), type_to_string(base_type(left.type)), LIT(token_strings[op_kind]), type_to_string(right.type), type_to_string(base_type(right.type)));
// /* NOTE(bill, 2022-05-28):
// Thanks to Per Vognsen, sign extending <N x i1> to
// a vector of the same width as the input vector, bit casting to an integer,
// and then comparing against zero is the better option
// See: https://lists.llvm.org/pipermail/llvm-dev/2012-September/053046.html
// // Example assuming 128-bit vector
// %1 = <4 x float> ...
// %2 = <4 x float> ...
// %3 = fcmp oeq <4 x float> %1, %2
// %4 = sext <4 x i1> %3 to <4 x i32>
// %5 = bitcast <4 x i32> %4 to i128
// %6 = icmp ne i128 %5, 0
// br i1 %6, label %true1, label %false2
// This will result in 1 cmpps + 1 ptest + 1 br
// (even without SSE4.1, contrary to what the mail list states, because of pmovmskb)
// */
// unsigned count = cast(unsigned)get_array_type_count(a);
// unsigned elem_sz = cast(unsigned)(type_size_of(elem)*8);
// LLVMTypeRef mask_type = LLVMVectorType(LLVMIntTypeInContext(p->module->ctx, elem_sz), count);
// mask = LLVMBuildSExtOrBitCast(p->builder, mask, mask_type, "");
// LLVMTypeRef mask_int_type = LLVMIntTypeInContext(p->module->ctx, cast(unsigned)(8*type_size_of(a)));
// LLVMValueRef mask_int = LLVMBuildBitCast(p->builder, mask, mask_int_type, "");
// res.value = LLVMBuildICmp(p->builder, LLVMIntNE, mask_int, LLVMConstNull(LLVMTypeOf(mask_int)), "");
// return res;
}
GB_PANIC("Unhandled comparison kind %s (%s) %.*s %s (%s)", type_to_string(left.type), type_to_string(base_type(left.type)), LIT(token_strings[op_kind]), type_to_string(right.type), type_to_string(base_type(right.type)));
return {};
}
gb_internal cgValue cg_emit_comp_against_nil(cgProcedure *p, TokenKind op_kind, cgValue x) {
GB_ASSERT(op_kind == Token_CmpEq || op_kind == Token_NotEq);
x = cg_flatten_value(p, x);
cgValue res = {};
Type *t = x.type;
TB_DataType dt = cg_data_type(t);
Type *bt = base_type(t);
TypeKind type_kind = bt->kind;
switch (type_kind) {
case Type_Basic:
switch (bt->Basic.kind) {
case Basic_rawptr:
case Basic_cstring:
GB_ASSERT(x.kind == cgValue_Value);
if (op_kind == Token_CmpEq) {
return cg_value(tb_inst_cmp_eq(p->func, x.node, tb_inst_uint(p->func, dt, 0)), t_bool);
} else if (op_kind == Token_NotEq) {
return cg_value(tb_inst_cmp_ne(p->func, x.node, tb_inst_uint(p->func, dt, 0)), t_bool);
}
break;
case Basic_any:
{
GB_ASSERT(x.kind == cgValue_Addr);
// // TODO(bill): is this correct behaviour for nil comparison for any?
cgValue data = cg_emit_struct_ev(p, x, 0);
cgValue id = cg_emit_struct_ev(p, x, 1);
if (op_kind == Token_CmpEq) {
TB_Node *a = tb_inst_cmp_eq(p->func, data.node, tb_inst_uint(p->func, data.node->dt, 0));
TB_Node *b = tb_inst_cmp_eq(p->func, id.node, tb_inst_uint(p->func, id.node->dt, 0));
TB_Node *c = tb_inst_or(p->func, a, b);
return cg_value(c, t_bool);
} else if (op_kind == Token_NotEq) {
TB_Node *a = tb_inst_cmp_ne(p->func, data.node, tb_inst_uint(p->func, data.node->dt, 0));
TB_Node *b = tb_inst_cmp_ne(p->func, id.node, tb_inst_uint(p->func, id.node->dt, 0));
TB_Node *c = tb_inst_and(p->func, a, b);
return cg_value(c, t_bool);
}
}
break;
case Basic_typeid:
cgValue invalid_typeid = cg_const_value(p, t_typeid, exact_value_i64(0));
return cg_emit_comp(p, op_kind, x, invalid_typeid);
}
break;
case Type_Enum:
case Type_Pointer:
case Type_MultiPointer:
case Type_Proc:
case Type_BitSet:
GB_ASSERT(x.kind == cgValue_Value);
if (op_kind == Token_CmpEq) {
return cg_value(tb_inst_cmp_eq(p->func, x.node, tb_inst_uint(p->func, dt, 0)), t_bool);
} else if (op_kind == Token_NotEq) {
return cg_value(tb_inst_cmp_ne(p->func, x.node, tb_inst_uint(p->func, dt, 0)), t_bool);
}
break;
case Type_Slice:
case Type_DynamicArray:
case Type_Map:
{
// NOTE(bill): all of their data "pointer-like" fields are at the 0-index
cgValue data = cg_emit_struct_ev(p, x, 0);
if (op_kind == Token_CmpEq) {
TB_Node *a = tb_inst_cmp_eq(p->func, data.node, tb_inst_uint(p->func, data.node->dt, 0));
return cg_value(a, t_bool);
} else if (op_kind == Token_NotEq) {
TB_Node *a = tb_inst_cmp_ne(p->func, data.node, tb_inst_uint(p->func, data.node->dt, 0));
return cg_value(a, t_bool);
}
}
break;
case Type_Union:
{
if (type_size_of(t) == 0) {
return cg_const_bool(p, t_bool, op_kind == Token_CmpEq);
} else if (is_type_union_maybe_pointer(t)) {
cgValue tag = cg_emit_transmute(p, x, t_rawptr);
return cg_emit_comp_against_nil(p, op_kind, tag);
} else {
GB_ASSERT("TODO(bill): cg_emit_union_tag_value");
// cgValue tag = cg_emit_union_tag_value(p, x);
// return cg_emit_comp(p, op_kind, tag, cg_zero(p->module, tag.type));
}
}
break;
case Type_Struct:
GB_PANIC("TODO(bill): cg_emit_struct_ev");
// if (is_type_soa_struct(t)) {
// Type *bt = base_type(t);
// if (bt->Struct.soa_kind == StructSoa_Slice) {
// LLVMValueRef the_value = {};
// if (bt->Struct.fields.count == 0) {
// cgValue len = cg_soa_struct_len(p, x);
// the_value = len.value;
// } else {
// cgValue first_field = cg_emit_struct_ev(p, x, 0);
// the_value = first_field.value;
// }
// if (op_kind == Token_CmpEq) {
// res.value = LLVMBuildIsNull(p->builder, the_value, "");
// return res;
// } else if (op_kind == Token_NotEq) {
// res.value = LLVMBuildIsNotNull(p->builder, the_value, "");
// return res;
// }
// } else if (bt->Struct.soa_kind == StructSoa_Dynamic) {
// LLVMValueRef the_value = {};
// if (bt->Struct.fields.count == 0) {
// cgValue cap = cg_soa_struct_cap(p, x);
// the_value = cap.value;
// } else {
// cgValue first_field = cg_emit_struct_ev(p, x, 0);
// the_value = first_field.value;
// }
// if (op_kind == Token_CmpEq) {
// res.value = LLVMBuildIsNull(p->builder, the_value, "");
// return res;
// } else if (op_kind == Token_NotEq) {
// res.value = LLVMBuildIsNotNull(p->builder, the_value, "");
// return res;
// }
// }
// } else if (is_type_struct(t) && type_has_nil(t)) {
// auto args = array_make<cgValue>(permanent_allocator(), 2);
// cgValue lhs = cg_address_from_load_or_generate_local(p, x);
// args[0] = cg_emit_conv(p, lhs, t_rawptr);
// args[1] = cg_const_int(p->module, t_int, type_size_of(t));
// cgValue val = cg_emit_runtime_call(p, "memory_compare_zero", args);
// cgValue res = cg_emit_comp(p, op_kind, val, cg_const_int(p->module, t_int, 0));
// return res;
// }
break;
}
GB_PANIC("Unknown handled type: %s -> %s", type_to_string(t), type_to_string(bt));
return {};
}
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);
if (value.kind == cgValue_Value && !TB_IS_VOID_TYPE(value.node->dt)) {
st = value.node->dt;
}
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)) {
TEMPORARY_ALLOCATOR_GUARD();
cgValue c = cg_emit_conv(p, value, t_cstring);
auto args = slice_make<cgValue>(temporary_allocator(), 1);
args[0] = c;
cgValue s = cg_emit_runtime_call(p, "cstring_to_string", args);
return cg_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)) {