Repository navigation
Expand file tree
/
Copy pathllvm_backend.cpp
More file actions
3745 lines (3035 loc) · 126 KB
/
Copy pathllvm_backend.cpp
File metadata and controls
3745 lines (3035 loc) · 126 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
#define MULTITHREAD_OBJECT_GENERATION 1
#ifndef MULTITHREAD_OBJECT_GENERATION
#define MULTITHREAD_OBJECT_GENERATION 0
#endif
#ifndef USE_SEPARATE_MODULES
#define USE_SEPARATE_MODULES build_context.use_separate_modules
#endif
#ifndef LLVM_IGNORE_VERIFICATION
#define LLVM_IGNORE_VERIFICATION build_context.internal_ignore_llvm_verification
#endif
#ifndef LLVM_WEAK_MONOMORPHIZATION
#define LLVM_WEAK_MONOMORPHIZATION (USE_SEPARATE_MODULES && build_context.internal_weak_monomorphization)
#endif
#include "llvm_backend.hpp"
#include "llvm_abi.cpp"
#include "llvm_backend_opt.cpp"
#include "llvm_backend_general.cpp"
#include "llvm_backend_debug.cpp"
#include "llvm_backend_const.cpp"
#include "llvm_backend_type.cpp"
#include "llvm_backend_utility.cpp"
#include "llvm_backend_expr.cpp"
#include "llvm_backend_stmt.cpp"
#include "llvm_backend_proc.cpp"
gb_internal String get_default_microarchitecture() {
String default_march = str_lit("generic");
if (build_context.metrics.arch == TargetArch_amd64) {
// NOTE(bill): x86-64-v2 is more than enough for everyone
//
// x86-64: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2
// x86-64-v2: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3
// x86-64-v3: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE
// x86-64-v4: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
if (ODIN_LLVM_MINIMUM_VERSION_12) {
if (build_context.metrics.os == TargetOs_freestanding) {
default_march = str_lit("x86-64");
} else {
default_march = str_lit("x86-64-v2");
}
}
} else if (build_context.metrics.arch == TargetArch_riscv64) {
default_march = str_lit("generic-rv64");
}
return default_march;
}
gb_internal String get_final_microarchitecture() {
BuildContext *bc = &build_context;
String microarch = bc->microarch;
if (microarch.len == 0) {
microarch = get_default_microarchitecture();
} else if (microarch == str_lit("native")) {
microarch = make_string_c(LLVMGetHostCPUName());
}
return microarch;
}
gb_internal String get_default_features() {
BuildContext *bc = &build_context;
int off = 0;
for (int i = 0; i < bc->metrics.arch; i += 1) {
off += target_microarch_counts[i];
}
String microarch = get_final_microarchitecture();
// NOTE(laytan): for riscv64 to work properly with Odin, we need to enforce some features.
// and we also overwrite the generic target to include more features so we don't default to
// a potato feature set.
if (bc->metrics.arch == TargetArch_riscv64) {
if (microarch == str_lit("generic-rv64")) {
// This is what clang does by default (on -march=rv64gc for General Computing), seems good to also default to.
String features = str_lit("64bit,a,c,d,f,m,relax,zicsr,zifencei");
// Update the features string so LLVM uses it later.
if (bc->target_features_string.len > 0) {
bc->target_features_string = concatenate3_strings(permanent_allocator(), features, str_lit(","), bc->target_features_string);
} else {
bc->target_features_string = features;
}
return features;
}
}
for (int i = off; i < off+target_microarch_counts[bc->metrics.arch]; i += 1) {
if (microarch_features_list[i].microarch == microarch) {
return microarch_features_list[i].features;
}
}
GB_PANIC("unknown microarch: %.*s", LIT(microarch));
return {};
}
gb_internal void lb_add_foreign_library_path(lbModule *m, Entity *e) {
if (e == nullptr) {
return;
}
GB_ASSERT(e->kind == Entity_LibraryName);
GB_ASSERT(e->flags & EntityFlag_Used);
mutex_lock(&m->gen->foreign_mutex);
if (!ptr_set_update(&m->gen->foreign_libraries_set, e)) {
array_add(&m->gen->foreign_libraries, e);
}
mutex_unlock(&m->gen->foreign_mutex);
}
gb_internal GB_COMPARE_PROC(foreign_library_cmp) {
int cmp = 0;
Entity *x = *(Entity **)a;
Entity *y = *(Entity **)b;
if (x == y) {
return 0;
}
GB_ASSERT(x->kind == Entity_LibraryName);
GB_ASSERT(y->kind == Entity_LibraryName);
cmp = i64_cmp(x->LibraryName.priority_index, y->LibraryName.priority_index);
if (cmp) {
return cmp;
}
if (x->pkg != y->pkg) {
isize order_x = x->pkg ? x->pkg->order : 0;
isize order_y = y->pkg ? y->pkg->order : 0;
cmp = isize_cmp(order_x, order_y);
if (cmp) {
return cmp;
}
}
if (x->file != y->file) {
String fullpath_x = x->file ? x->file->fullpath : (String{});
String fullpath_y = y->file ? y->file->fullpath : (String{});
String file_x = filename_from_path(fullpath_x);
String file_y = filename_from_path(fullpath_y);
cmp = string_compare(file_x, file_y);
if (cmp) {
return cmp;
}
}
cmp = u64_cmp(x->order_in_src, y->order_in_src);
if (cmp) {
return cmp;
}
return i32_cmp(x->token.pos.offset, y->token.pos.offset);
}
gb_internal void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module, Entity *e, String const &name) {
if (other_module == nullptr) {
return;
}
char const *cname = alloc_cstring(permanent_allocator(), name);
mpsc_enqueue(&other_module->gen->entities_to_correct_linkage, lbEntityCorrection{other_module, e, cname});
}
gb_internal void lb_correct_entity_linkage(lbGenerator *gen) {
for (lbEntityCorrection ec = {}; mpsc_dequeue(&gen->entities_to_correct_linkage, &ec); /**/) {
LLVMValueRef other_global = nullptr;
if (ec.e->kind == Entity_Variable) {
other_global = LLVMGetNamedGlobal(ec.other_module->mod, ec.cname);
if (other_global) {
LLVMSetLinkage(other_global, LLVMWeakAnyLinkage);
if (!ec.e->Variable.is_export && !ec.e->Variable.is_foreign) {
LLVMSetVisibility(other_global, LLVMHiddenVisibility);
}
}
} else if (ec.e->kind == Entity_Procedure) {
other_global = LLVMGetNamedFunction(ec.other_module->mod, ec.cname);
if (other_global) {
LLVMSetLinkage(other_global, LLVMWeakAnyLinkage);
if (!ec.e->Procedure.is_export && !ec.e->Procedure.is_foreign) {
LLVMSetVisibility(other_global, LLVMHiddenVisibility);
}
}
}
}
}
gb_internal void lb_emit_init_context(lbProcedure *p, lbAddr addr) {
TEMPORARY_ALLOCATOR_GUARD();
GB_ASSERT(addr.kind == lbAddr_Context);
GB_ASSERT(addr.ctx.sel.index.count == 0);
auto args = array_make<lbValue>(temporary_allocator(), 1);
args[0] = addr.addr;
lb_emit_runtime_call(p, "__init_context", args);
}
gb_internal lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p) {
Type *pt = base_type(p->type);
GB_ASSERT(pt->kind == Type_Proc);
GB_ASSERT(pt->Proc.calling_convention == ProcCC_Odin);
String name = str_lit("__.context_ptr");
Entity *e = alloc_entity_param(nullptr, make_token_ident(name), t_context_ptr, false, false);
e->flags |= EntityFlag_NoAlias;
LLVMValueRef context_ptr = LLVMGetParam(p->value, LLVMCountParams(p->value)-1);
LLVMSetValueName2(context_ptr, cast(char const *)name.text, name.len);
context_ptr = LLVMBuildPointerCast(p->builder, context_ptr, lb_type(p->module, e->type), "");
lbValue param = {context_ptr, e->type};
lb_add_entity(p->module, e, param);
lbAddr ctx_addr = {};
ctx_addr.kind = lbAddr_Context;
ctx_addr.addr = param;
lbContextData *cd = array_add_and_get(&p->context_stack);
cd->ctx = ctx_addr;
cd->scope_index = -1;
cd->uses = +1; // make sure it has been used already
return cd;
}
gb_internal lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx) {
ctx.kind = lbAddr_Context;
lbContextData *cd = array_add_and_get(&p->context_stack);
cd->ctx = ctx;
cd->scope_index = p->scope_index;
return cd;
}
gb_internal String lb_internal_gen_name_from_type(char const *prefix, Type *type) {
gbString str = gb_string_make(permanent_allocator(), prefix);
u64 hash = type_hash_canonical_type(type);
str = gb_string_appendc(str, "-");
str = gb_string_append_fmt(str, "%llu", cast(unsigned long long)hash);
String proc_name = make_string(cast(u8 const *)str, gb_string_length(str));
return proc_name;
}
gb_internal void lb_equal_proc_generate_body(lbModule *m, lbProcedure *p) {
Type *type = p->internal_gen_type;
Type *pt = alloc_type_pointer(type);
LLVMTypeRef ptr_type = lb_type(m, pt);
lb_begin_procedure_body(p);
LLVMSetLinkage(p->value, LLVMInternalLinkage);
// lb_add_attribute_to_proc(m, p->value, "readonly");
lb_add_attribute_to_proc(m, p->value, "nounwind");
LLVMValueRef x = LLVMGetParam(p->value, 0);
LLVMValueRef y = LLVMGetParam(p->value, 1);
x = LLVMBuildPointerCast(p->builder, x, ptr_type, "");
y = LLVMBuildPointerCast(p->builder, y, ptr_type, "");
lbValue lhs = {x, pt};
lbValue rhs = {y, pt};
lb_add_proc_attribute_at_index(p, 1+0, "nonnull");
lb_add_proc_attribute_at_index(p, 1+1, "nonnull");
lbBlock *block_same_ptr = lb_create_block(p, "same_ptr");
lbBlock *block_diff_ptr = lb_create_block(p, "diff_ptr");
lbValue same_ptr = lb_emit_comp(p, Token_CmpEq, lhs, rhs);
lb_emit_if(p, same_ptr, block_same_ptr, block_diff_ptr);
lb_start_block(p, block_same_ptr);
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
lb_start_block(p, block_diff_ptr);
if (type->kind == Type_Struct) {
type_set_offsets(type);
lbBlock *block_false = lb_create_block(p, "bfalse");
lbValue res = lb_const_bool(m, t_bool, true);
for_array(i, type->Struct.fields) {
lbBlock *next_block = lb_create_block(p, "btrue");
lbValue pleft = lb_emit_struct_ep(p, lhs, cast(i32)i);
lbValue pright = lb_emit_struct_ep(p, rhs, cast(i32)i);
lbValue left = lb_emit_load(p, pleft);
lbValue right = lb_emit_load(p, pright);
lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
lb_emit_if(p, ok, next_block, block_false);
lb_emit_jump(p, next_block);
lb_start_block(p, next_block);
}
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
lb_start_block(p, block_false);
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false));
} else if (type->kind == Type_Union) {
if (type_size_of(type) == 0) {
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
} else if (is_type_union_maybe_pointer(type)) {
Type *v = type->Union.variants[0];
Type *pv = alloc_type_pointer(v);
lbValue left = lb_emit_load(p, lb_emit_conv(p, lhs, pv));
lbValue right = lb_emit_load(p, lb_emit_conv(p, rhs, pv));
lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
ok = lb_emit_conv(p, ok, t_bool);
LLVMBuildRet(p->builder, ok.value);
} else {
lbBlock *block_false = lb_create_block(p, "bfalse");
lbBlock *block_switch = lb_create_block(p, "bswitch");
lbValue left_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, lhs));
lbValue right_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, rhs));
lbValue tag_eq = lb_emit_comp(p, Token_CmpEq, left_tag, right_tag);
lb_emit_if(p, tag_eq, block_switch, block_false);
lb_start_block(p, block_switch);
unsigned variant_count = cast(unsigned)type->Union.variants.count;
if (type->Union.kind != UnionType_no_nil) {
variant_count += 1;
}
LLVMValueRef v_switch = LLVMBuildSwitch(p->builder, left_tag.value, block_false->block, variant_count);
if (type->Union.kind != UnionType_no_nil) {
lbBlock *case_block = lb_create_block(p, "bcase");
lb_start_block(p, case_block);
lbValue case_tag = lb_const_int(p->module, union_tag_type(type), 0);
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
LLVMAddCase(v_switch, case_tag.value, case_block->block);
}
for (Type *v : type->Union.variants) {
lbBlock *case_block = lb_create_block(p, "bcase");
lb_start_block(p, case_block);
lbValue case_tag = lb_const_union_tag(p->module, type, v);
Type *vp = alloc_type_pointer(v);
lbValue left = lb_emit_load(p, lb_emit_conv(p, lhs, vp));
lbValue right = lb_emit_load(p, lb_emit_conv(p, rhs, vp));
lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
ok = lb_emit_conv(p, ok, t_bool);
LLVMBuildRet(p->builder, ok.value);
LLVMAddCase(v_switch, case_tag.value, case_block->block);
}
lb_start_block(p, block_false);
LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false));
}
} else {
lbValue left = lb_emit_load(p, lhs);
lbValue right = lb_emit_load(p, rhs);
lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
ok = lb_emit_conv(p, ok, t_bool);
LLVMBuildRet(p->builder, ok.value);
}
lb_end_procedure_body(p);
}
gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type) {
type = base_type(type);
GB_ASSERT(is_type_comparable(type));
String proc_name = lb_internal_gen_name_from_type("__$equal", type);
lbProcedure **found = string_map_get(&m->gen_procs, proc_name);
if (found) {
lbProcedure *p = *found;
GB_ASSERT(p != nullptr);
return {p->value, p->type};
}
lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_equal_proc);
string_map_set(&m->gen_procs, proc_name, p);
p->internal_gen_type = type;
p->generate_body = lb_equal_proc_generate_body;
// p->generate_body(m, p);
mpsc_enqueue(&m->procedures_to_generate, p);
return {p->value, p->type};
}
gb_internal lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue seed) {
TEMPORARY_ALLOCATOR_GUARD();
GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
auto args = array_make<lbValue>(temporary_allocator(), 3);
args[0] = data;
args[1] = seed;
args[2] = lb_const_int(p->module, t_int, type_size_of(type));
return lb_emit_runtime_call(p, "default_hasher", args);
}
gb_internal void lb_add_callsite_force_inline(lbProcedure *p, lbValue ret_value) {
LLVMAddCallSiteAttribute(ret_value.value, LLVMAttributeIndex_FunctionIndex, lb_create_enum_attribute(p->module->ctx, "alwaysinline"));
}
gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) {
type = core_type(type);
GB_ASSERT_MSG(is_type_comparable(type), "%s", type_to_string(type));
Type *pt = alloc_type_pointer(type);
String proc_name = lb_internal_gen_name_from_type("__$hasher", type);
lbProcedure **found = string_map_get(&m->gen_procs, proc_name);
if (found) {
GB_ASSERT(*found != nullptr);
return {(*found)->value, (*found)->type};
}
lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_hasher_proc);
string_map_set(&m->gen_procs, proc_name, p);
lb_begin_procedure_body(p);
defer (lb_end_procedure_body(p));
LLVMSetLinkage(p->value, LLVMInternalLinkage);
// lb_add_attribute_to_proc(m, p->value, "readonly");
lb_add_attribute_to_proc(m, p->value, "nounwind");
LLVMValueRef x = LLVMGetParam(p->value, 0);
LLVMValueRef y = LLVMGetParam(p->value, 1);
lbValue data = {x, t_rawptr};
lbValue seed = {y, t_uintptr};
lb_add_proc_attribute_at_index(p, 1+0, "nonnull");
// lb_add_proc_attribute_at_index(p, 1+0, "readonly");
if (is_type_simple_compare(type)) {
lbValue res = lb_simple_compare_hash(p, type, data, seed);
lb_add_callsite_force_inline(p, res);
LLVMBuildRet(p->builder, res.value);
return {p->value, p->type};
}
TEMPORARY_ALLOCATOR_GUARD();
if (type->kind == Type_Struct) {
type_set_offsets(type);
data = lb_emit_conv(p, data, t_u8_ptr);
auto args = array_make<lbValue>(temporary_allocator(), 2);
for_array(i, type->Struct.fields) {
GB_ASSERT(type->Struct.offsets != nullptr);
i64 offset = type->Struct.offsets[i];
Entity *field = type->Struct.fields[i];
lbValue field_hasher = lb_hasher_proc_for_type(m, field->type);
lbValue ptr = lb_emit_ptr_offset(p, data, lb_const_int(m, t_uintptr, offset));
args[0] = ptr;
args[1] = seed;
seed = lb_emit_call(p, field_hasher, args);
}
LLVMBuildRet(p->builder, seed.value);
} else if (type->kind == Type_Union) {
auto args = array_make<lbValue>(temporary_allocator(), 2);
if (is_type_union_maybe_pointer(type)) {
Type *v = type->Union.variants[0];
lbValue variant_hasher = lb_hasher_proc_for_type(m, v);
args[0] = data;
args[1] = seed;
lbValue res = lb_emit_call(p, variant_hasher, args);
lb_add_callsite_force_inline(p, res);
LLVMBuildRet(p->builder, res.value);
}
lbBlock *end_block = lb_create_block(p, "bend");
data = lb_emit_conv(p, data, pt);
lbValue tag_ptr = lb_emit_union_tag_ptr(p, data);
lbValue tag = lb_emit_load(p, tag_ptr);
LLVMValueRef v_switch = LLVMBuildSwitch(p->builder, tag.value, end_block->block, cast(unsigned)type->Union.variants.count);
for (Type *v : type->Union.variants) {
lbBlock *case_block = lb_create_block(p, "bcase");
lb_start_block(p, case_block);
lbValue case_tag = lb_const_union_tag(p->module, type, v);
lbValue variant_hasher = lb_hasher_proc_for_type(m, v);
args[0] = data;
args[1] = seed;
lbValue res = lb_emit_call(p, variant_hasher, args);
LLVMBuildRet(p->builder, res.value);
LLVMAddCase(v_switch, case_tag.value, case_block->block);
}
lb_start_block(p, end_block);
LLVMBuildRet(p->builder, seed.value);
} else if (type->kind == Type_Array) {
lbAddr pres = lb_add_local_generated(p, t_uintptr, false);
lb_addr_store(p, pres, seed);
auto args = array_make<lbValue>(temporary_allocator(), 2);
lbValue elem_hasher = lb_hasher_proc_for_type(m, type->Array.elem);
auto loop_data = lb_loop_start(p, cast(isize)type->Array.count, t_i32);
data = lb_emit_conv(p, data, pt);
lbValue ptr = lb_emit_array_ep(p, data, loop_data.idx);
args[0] = ptr;
args[1] = lb_addr_load(p, pres);
lbValue new_seed = lb_emit_call(p, elem_hasher, args);
lb_addr_store(p, pres, new_seed);
lb_loop_end(p, loop_data);
lbValue res = lb_addr_load(p, pres);
LLVMBuildRet(p->builder, res.value);
} else if (type->kind == Type_EnumeratedArray) {
lbAddr res = lb_add_local_generated(p, t_uintptr, false);
lb_addr_store(p, res, seed);
auto args = array_make<lbValue>(temporary_allocator(), 2);
lbValue elem_hasher = lb_hasher_proc_for_type(m, type->EnumeratedArray.elem);
auto loop_data = lb_loop_start(p, cast(isize)type->EnumeratedArray.count, t_i32);
data = lb_emit_conv(p, data, pt);
lbValue ptr = lb_emit_array_ep(p, data, loop_data.idx);
args[0] = ptr;
args[1] = lb_addr_load(p, res);
lbValue new_seed = lb_emit_call(p, elem_hasher, args);
lb_addr_store(p, res, new_seed);
lb_loop_end(p, loop_data);
lbValue vres = lb_addr_load(p, res);
LLVMBuildRet(p->builder, vres.value);
} else if (is_type_cstring(type)) {
auto args = array_make<lbValue>(temporary_allocator(), 2);
args[0] = data;
args[1] = seed;
lbValue res = lb_emit_runtime_call(p, "default_hasher_cstring", args);
lb_add_callsite_force_inline(p, res);
LLVMBuildRet(p->builder, res.value);
} else if (is_type_string(type)) {
auto args = array_make<lbValue>(temporary_allocator(), 2);
args[0] = data;
args[1] = seed;
lbValue res = lb_emit_runtime_call(p, "default_hasher_string", args);
lb_add_callsite_force_inline(p, res);
LLVMBuildRet(p->builder, res.value);
} else if (is_type_float(type)) {
lbValue ptr = lb_emit_conv(p, data, pt);
lbValue v = lb_emit_load(p, ptr);
v = lb_emit_conv(p, v, t_f64);
auto args = array_make<lbValue>(temporary_allocator(), 2);
args[0] = v;
args[1] = seed;
lbValue res = lb_emit_runtime_call(p, "default_hasher_f64", args);
lb_add_callsite_force_inline(p, res);
LLVMBuildRet(p->builder, res.value);
} else if (is_type_complex(type)) {
lbValue ptr = lb_emit_conv(p, data, pt);
lbValue xp = lb_emit_struct_ep(p, ptr, 0);
lbValue yp = lb_emit_struct_ep(p, ptr, 1);
lbValue x = lb_emit_conv(p, lb_emit_load(p, xp), t_f64);
lbValue y = lb_emit_conv(p, lb_emit_load(p, yp), t_f64);
auto args = array_make<lbValue>(temporary_allocator(), 3);
args[0] = x;
args[1] = y;
args[2] = seed;
lbValue res = lb_emit_runtime_call(p, "default_hasher_complex128", args);
lb_add_callsite_force_inline(p, res);
LLVMBuildRet(p->builder, res.value);
} else if (is_type_quaternion(type)) {
lbValue ptr = lb_emit_conv(p, data, pt);
lbValue xp = lb_emit_struct_ep(p, ptr, 0);
lbValue yp = lb_emit_struct_ep(p, ptr, 1);
lbValue zp = lb_emit_struct_ep(p, ptr, 2);
lbValue wp = lb_emit_struct_ep(p, ptr, 3);
lbValue x = lb_emit_conv(p, lb_emit_load(p, xp), t_f64);
lbValue y = lb_emit_conv(p, lb_emit_load(p, yp), t_f64);
lbValue z = lb_emit_conv(p, lb_emit_load(p, zp), t_f64);
lbValue w = lb_emit_conv(p, lb_emit_load(p, wp), t_f64);
auto args = array_make<lbValue>(temporary_allocator(), 5);
args[0] = x;
args[1] = y;
args[2] = z;
args[3] = w;
args[4] = seed;
lbValue res = lb_emit_runtime_call(p, "default_hasher_quaternion256", args);
lb_add_callsite_force_inline(p, res);
LLVMBuildRet(p->builder, res.value);
} else {
GB_PANIC("Unhandled type for hasher: %s", type_to_string(type));
}
return {p->value, p->type};
}
#define LLVM_SET_VALUE_NAME(value, name) LLVMSetValueName2((value), (name), gb_count_of((name))-1);
gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) {
GB_ASSERT(!build_context.dynamic_map_calls);
type = base_type(type);
GB_ASSERT(type->kind == Type_Map);
String proc_name = lb_internal_gen_name_from_type("__$map_get", type);
lbProcedure **found = string_map_get(&m->gen_procs, proc_name);
if (found) {
GB_ASSERT(*found != nullptr);
return {(*found)->value, (*found)->type};
}
lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_map_get_proc);
string_map_set(&m->gen_procs, proc_name, p);
p->internal_gen_type = type;
lb_begin_procedure_body(p);
defer (lb_end_procedure_body(p));
LLVMSetLinkage(p->value, LLVMInternalLinkage);
lb_add_attribute_to_proc(m, p->value, "nounwind");
if (build_context.ODIN_DEBUG) {
lb_add_attribute_to_proc(m, p->value, "noinline");
}
LLVMValueRef x = LLVMGetParam(p->value, 0);
LLVMValueRef y = LLVMGetParam(p->value, 1);
LLVMValueRef z = LLVMGetParam(p->value, 2);
lbValue map_ptr = {x, t_rawptr};
lbValue h = {y, t_uintptr};
lbValue key_ptr = {z, t_rawptr};
LLVM_SET_VALUE_NAME(h.value, "hash");
lb_add_proc_attribute_at_index(p, 1+0, "nonnull");
lb_add_proc_attribute_at_index(p, 1+0, "readonly");
lb_add_proc_attribute_at_index(p, 1+2, "nonnull");
lb_add_proc_attribute_at_index(p, 1+2, "readonly");
lbBlock *loop_block = lb_create_block(p, "loop");
lbBlock *hash_block = lb_create_block(p, "hash");
lbBlock *probe_block = lb_create_block(p, "probe");
lbBlock *increment_block = lb_create_block(p, "increment");
lbBlock *hash_compare_block = lb_create_block(p, "hash_compare");
lbBlock *key_compare_block = lb_create_block(p, "key_compare");
lbBlock *value_block = lb_create_block(p, "value");
lbBlock *nil_block = lb_create_block(p, "nil");
map_ptr = lb_emit_conv(p, map_ptr, t_raw_map_ptr);
LLVM_SET_VALUE_NAME(map_ptr.value, "map_ptr");
lbValue map = lb_emit_load(p, map_ptr);
LLVM_SET_VALUE_NAME(map.value, "map");
lbValue length = lb_map_len(p, map);
LLVM_SET_VALUE_NAME(length.value, "length");
lb_emit_if(p, lb_emit_comp(p, Token_CmpEq, length, lb_const_nil(m, t_int)), nil_block, hash_block);
lb_start_block(p, hash_block);
key_ptr = lb_emit_conv(p, key_ptr, alloc_type_pointer(type->Map.key));
LLVM_SET_VALUE_NAME(key_ptr.value, "key_ptr");
lbValue key = lb_emit_load(p, key_ptr);
LLVM_SET_VALUE_NAME(key.value, "key");
lbAddr pos = lb_add_local_generated(p, t_uintptr, false);
lbAddr distance = lb_add_local_generated(p, t_uintptr, true);
LLVM_SET_VALUE_NAME(pos.addr.value, "pos");
LLVM_SET_VALUE_NAME(distance.addr.value, "distance");
lbValue capacity = lb_map_cap(p, map);
LLVM_SET_VALUE_NAME(capacity.value, "capacity");
lbValue cap_minus_1 = lb_emit_arith(p, Token_Sub, capacity, lb_const_int(m, t_int, 1), t_int);
lbValue mask = lb_emit_conv(p, cap_minus_1, t_uintptr);
LLVM_SET_VALUE_NAME(mask.value, "mask");
{
// map_desired_position inlined
lbValue the_pos = lb_emit_arith(p, Token_And, h, mask, t_uintptr);
the_pos = lb_emit_conv(p, the_pos, t_uintptr);
lb_addr_store(p, pos, the_pos);
}
lbValue zero_uintptr = lb_const_int(m, t_uintptr, 0);
lbValue one_uintptr = lb_const_int(m, t_uintptr, 1);
lbValue ks = lb_map_data_uintptr(p, map);
lbValue vs = lb_map_cell_index_static(p, type->Map.key, ks, capacity);
lbValue hs = lb_map_cell_index_static(p, type->Map.value, vs, capacity);
ks = lb_emit_conv(p, ks, alloc_type_pointer(type->Map.key));
vs = lb_emit_conv(p, vs, alloc_type_pointer(type->Map.value));
hs = lb_emit_conv(p, hs, alloc_type_pointer(t_uintptr));
LLVM_SET_VALUE_NAME(ks.value, "ks");
LLVM_SET_VALUE_NAME(vs.value, "vs");
LLVM_SET_VALUE_NAME(hs.value, "hs");
lb_emit_jump(p, loop_block);
lb_start_block(p, loop_block);
lbValue element_hash = lb_emit_load(p, lb_emit_ptr_offset(p, hs, lb_addr_load(p, pos)));
LLVM_SET_VALUE_NAME(element_hash.value, "element_hash");
{
// if element_hash == 0 { return nil }
lb_emit_if(p, lb_emit_comp(p, Token_CmpEq, element_hash, zero_uintptr), nil_block, probe_block);
}
lb_start_block(p, probe_block);
{
// map_probe_distance inlined
lbValue probe_distance = lb_emit_arith(p, Token_And, h, mask, t_uintptr);
probe_distance = lb_emit_conv(p, probe_distance, t_uintptr);
lbValue cap = lb_emit_conv(p, capacity, t_uintptr);
lbValue base = lb_emit_arith(p, Token_Add, lb_addr_load(p, pos), cap, t_uintptr);
probe_distance = lb_emit_arith(p, Token_Sub, base, probe_distance, t_uintptr);
probe_distance = lb_emit_arith(p, Token_And, probe_distance, mask, t_uintptr);
LLVM_SET_VALUE_NAME(probe_distance.value, "probe_distance");
lbValue cond = lb_emit_comp(p, Token_Gt, lb_addr_load(p, distance), probe_distance);
lb_emit_if(p, cond, nil_block, hash_compare_block);
}
lb_start_block(p, hash_compare_block);
{
lb_emit_if(p, lb_emit_comp(p, Token_CmpEq, element_hash, h), key_compare_block, increment_block);
}
lb_start_block(p, key_compare_block);
{
lbValue element_key = lb_map_cell_index_static(p, type->Map.key, ks, lb_addr_load(p, pos));
element_key = lb_emit_conv(p, element_key, ks.type);
LLVM_SET_VALUE_NAME(element_key.value, "element_key_ptr");
lbValue cond = lb_emit_comp(p, Token_CmpEq, lb_emit_load(p, element_key), key);
lb_emit_if(p, cond, value_block, increment_block);
}
lb_start_block(p, value_block);
{
lbValue element_value = lb_map_cell_index_static(p, type->Map.value, vs, lb_addr_load(p, pos));
LLVM_SET_VALUE_NAME(element_value.value, "element_value_ptr");
element_value = lb_emit_conv(p, element_value, t_rawptr);
LLVMBuildRet(p->builder, element_value.value);
}
lb_start_block(p, increment_block);
{
lbValue pp = lb_addr_load(p, pos);
pp = lb_emit_arith(p, Token_Add, pp, one_uintptr, t_uintptr);
pp = lb_emit_arith(p, Token_And, pp, mask, t_uintptr);
lb_addr_store(p, pos, pp);
lb_emit_increment(p, distance.addr);
}
lb_emit_jump(p, loop_block);
lb_start_block(p, nil_block);
{
lbValue res = lb_const_nil(m, t_rawptr);
LLVMBuildRet(p->builder, res.value);
}
// gb_printf_err("%s\n", LLVMPrintValueToString(p->value));
return {p->value, p->type};
}
// gb_internal void lb_debug_print(lbProcedure *p, String const &str) {
// auto args = array_make<lbValue>(heap_allocator(), 1);
// args[0] = lb_const_string(p->module, str);
// lb_emit_runtime_call(p, "print_string", args);
// }
gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) {
TEMPORARY_ALLOCATOR_GUARD();
GB_ASSERT(!build_context.dynamic_map_calls);
type = base_type(type);
GB_ASSERT(type->kind == Type_Map);
String proc_name = lb_internal_gen_name_from_type("__$map_set", type);
lbProcedure **found = string_map_get(&m->gen_procs, proc_name);
if (found) {
GB_ASSERT(*found != nullptr);
return {(*found)->value, (*found)->type};
}
lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_map_set_proc);
string_map_set(&m->gen_procs, proc_name, p);
lb_begin_procedure_body(p);
defer (lb_end_procedure_body(p));
LLVMSetLinkage(p->value, LLVMInternalLinkage);
lb_add_attribute_to_proc(m, p->value, "nounwind");
if (build_context.ODIN_DEBUG) {
lb_add_attribute_to_proc(m, p->value, "noinline");
}
lbValue map_ptr = {LLVMGetParam(p->value, 0), t_rawptr};
lbValue hash_param = {LLVMGetParam(p->value, 1), t_uintptr};
lbValue key_ptr = {LLVMGetParam(p->value, 2), t_rawptr};
lbValue value_ptr = {LLVMGetParam(p->value, 3), t_rawptr};
lbValue location_ptr = {LLVMGetParam(p->value, 4), t_source_code_location_ptr};
map_ptr = lb_emit_conv(p, map_ptr, alloc_type_pointer(type));
key_ptr = lb_emit_conv(p, key_ptr, alloc_type_pointer(type->Map.key));
LLVM_SET_VALUE_NAME(map_ptr.value, "map_ptr");
LLVM_SET_VALUE_NAME(hash_param.value, "hash_param");
LLVM_SET_VALUE_NAME(key_ptr.value, "key_ptr");
LLVM_SET_VALUE_NAME(value_ptr.value, "value_ptr");
LLVM_SET_VALUE_NAME(location_ptr.value, "location");
lb_add_proc_attribute_at_index(p, 1+0, "nonnull");
lb_add_proc_attribute_at_index(p, 1+0, "noalias");
lb_add_proc_attribute_at_index(p, 1+2, "nonnull");
if (!are_types_identical(type->Map.key, type->Map.value)) {
lb_add_proc_attribute_at_index(p, 1+2, "noalias");
}
lb_add_proc_attribute_at_index(p, 1+2, "readonly");
lb_add_proc_attribute_at_index(p, 1+3, "nonnull");
if (!are_types_identical(type->Map.key, type->Map.value)) {
lb_add_proc_attribute_at_index(p, 1+3, "noalias");
}
lb_add_proc_attribute_at_index(p, 1+3, "readonly");
lb_add_proc_attribute_at_index(p, 1+4, "nonnull");
lb_add_proc_attribute_at_index(p, 1+4, "noalias");
lb_add_proc_attribute_at_index(p, 1+4, "readonly");
lbAddr hash_addr = lb_add_local_generated(p, t_uintptr, false);
lb_addr_store(p, hash_addr, hash_param);
LLVM_SET_VALUE_NAME(hash_addr.addr.value, "hash");
////
lbValue found_ptr = {};
{
lbValue map_get_proc = lb_map_get_proc_for_type(m, type);
auto args = array_make<lbValue>(temporary_allocator(), 3);
args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
args[1] = lb_addr_load(p, hash_addr);
args[2] = key_ptr;
found_ptr = lb_emit_call(p, map_get_proc, args);
}
LLVM_SET_VALUE_NAME(found_ptr.value, "found_ptr");
lbBlock *found_block = lb_create_block(p, "found");
lbBlock *check_grow_block = lb_create_block(p, "check-grow");
lbBlock *grow_fail_block = lb_create_block(p, "grow-fail");
lbBlock *insert_block = lb_create_block(p, "insert");
lbBlock *check_has_grown_block = lb_create_block(p, "check-has-grown");
lbBlock *rehash_block = lb_create_block(p, "rehash");
lb_emit_if(p, lb_emit_comp_against_nil(p, Token_NotEq, found_ptr), found_block, check_grow_block);
lb_start_block(p, found_block);
{
lb_mem_copy_non_overlapping(p, found_ptr, value_ptr, lb_const_int(m, t_int, type_size_of(type->Map.value)));
LLVMBuildRet(p->builder, lb_emit_conv(p, found_ptr, t_rawptr).value);
}
lb_start_block(p, check_grow_block);
lbValue map_info = lb_gen_map_info_ptr(p->module, type);
LLVM_SET_VALUE_NAME(map_info.value, "map_info");
{
auto args = array_make<lbValue>(temporary_allocator(), 3);
args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
args[1] = map_info;
args[2] = lb_emit_load(p, location_ptr);
lbValue grow_err_and_has_grown = lb_emit_runtime_call(p, "__dynamic_map_check_grow", args);
lbValue grow_err = lb_emit_struct_ev(p, grow_err_and_has_grown, 0);
lbValue has_grown = lb_emit_struct_ev(p, grow_err_and_has_grown, 1);
LLVM_SET_VALUE_NAME(grow_err.value, "grow_err");
LLVM_SET_VALUE_NAME(has_grown.value, "has_grown");
lb_emit_if(p, lb_emit_comp_against_nil(p, Token_NotEq, grow_err), grow_fail_block, check_has_grown_block);
lb_start_block(p, grow_fail_block);
LLVMBuildRet(p->builder, LLVMConstNull(lb_type(m, t_rawptr)));
lb_start_block(p, check_has_grown_block);
lb_emit_if(p, has_grown, rehash_block, insert_block);
lb_start_block(p, rehash_block);
lbValue key = lb_emit_load(p, key_ptr);
lbValue new_hash = lb_gen_map_key_hash(p, map_ptr, key, nullptr);
LLVM_SET_VALUE_NAME(new_hash.value, "new_hash");
lb_addr_store(p, hash_addr, new_hash);
lb_emit_jump(p, insert_block);
}
lb_start_block(p, insert_block);
{
auto args = array_make<lbValue>(temporary_allocator(), 5);
args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
args[1] = map_info;
args[2] = lb_addr_load(p, hash_addr);
args[3] = lb_emit_conv(p, key_ptr, t_uintptr);
args[4] = lb_emit_conv(p, value_ptr, t_uintptr);
lbValue result = lb_emit_runtime_call(p, "map_insert_hash_dynamic", args);
lb_emit_increment(p, lb_map_len_ptr(p, map_ptr));
LLVMBuildRet(p->builder, lb_emit_conv(p, result, t_rawptr).value);
}
return {p->value, p->type};
}
gb_internal lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type) {
lbAddr *found = map_get(&m->map_cell_info_map, type);
if (found) {
return found->addr;
}
i64 size = 0, len = 0;
map_cell_size_and_len(type, &size, &len);
LLVMValueRef const_values[4] = {};
const_values[0] = lb_const_int(m, t_uintptr, type_size_of(type)).value;
const_values[1] = lb_const_int(m, t_uintptr, type_align_of(type)).value;
const_values[2] = lb_const_int(m, t_uintptr, size).value;
const_values[3] = lb_const_int(m, t_uintptr, len).value;
LLVMValueRef llvm_res = llvm_const_named_struct(m, t_map_cell_info, const_values, gb_count_of(const_values));
lbValue res = {llvm_res, t_map_cell_info};
lbAddr addr = lb_add_global_generated_with_name(m, t_map_cell_info, res, lb_internal_gen_name_from_type("ggv$map_cell_info", type));
lb_make_global_private_const(addr);
map_set(&m->map_cell_info_map, type, addr);
return addr.addr;
}
gb_internal lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type) {
map_type = base_type(map_type);
GB_ASSERT(map_type->kind == Type_Map);
lbAddr *found = map_get(&m->map_info_map, map_type);
if (found) {
return found->addr;
}
GB_ASSERT(t_map_info != nullptr);
GB_ASSERT(t_map_cell_info != nullptr);
LLVMValueRef key_cell_info = lb_gen_map_cell_info_ptr(m, map_type->Map.key).value;
LLVMValueRef value_cell_info = lb_gen_map_cell_info_ptr(m, map_type->Map.value).value;
LLVMValueRef const_values[4] = {};
const_values[0] = key_cell_info;
const_values[1] = value_cell_info;
const_values[2] = lb_hasher_proc_for_type(m, map_type->Map.key).value;
const_values[3] = lb_equal_proc_for_type(m, map_type->Map.key).value;
LLVMValueRef llvm_res = llvm_const_named_struct(m, t_map_info, const_values, gb_count_of(const_values));
lbValue res = {llvm_res, t_map_info};