-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvalue.c
More file actions
1568 lines (1468 loc) · 51.4 KB
/
Copy pathvalue.c
File metadata and controls
1568 lines (1468 loc) · 51.4 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
#include "value.h"
#include "function.h"
#include "source.h"
#include "calling_convention.h"
static inline bool
mass_value_is_static(
const Value *value
) {
if (!value) return false;
if (value->tag != Value_Tag_Forced) return false;
switch(value->Forced.storage.tag) {
case Storage_Tag_Static:
case Storage_Tag_Immediate: {
return true;
}
case Storage_Tag_Register:
case Storage_Tag_Xmm:
case Storage_Tag_Disjoint:
case Storage_Tag_Memory:
case Storage_Tag_Eflags:
default: {
return false;
}
}
}
static inline Slice
source_from_source_range(
Compilation *compilation,
const Source_Range *source_range
) {
if (!source_range->file) return (Slice){0};
Range_u64 offsets = {source_range->offsets.from, source_range->offsets.to};
return slice_sub_range(source_range->file->text, offsets);
}
static void
source_range_print_start_position(
Compilation *compilation,
const Source_Range *source_range
) {
if (!source_range->file) {
printf(":(0:0)\n");
return;
}
Source_Position from_position = {.line = 0, .column = 0};
const Source_File *file = source_range->file;
Slice source = file->text;
u64 line_start_offset = 0;
for (u64 i = 0; i < source.length; ++i) {
if (source.bytes[i] != '\n') continue;
// if the range starts at the \n it is considered part of the current line, not the next one
if (i == source_range->offsets.from) break;
from_position.line += 1;
if (i > source_range->offsets.from) break;
line_start_offset = i + 1;
}
from_position.column = source_range->offsets.from - line_start_offset;
slice_print(file->path);
printf(":(%" PRIu64 ":%" PRIu64 ")\n", from_position.line, from_position.column);
}
static void
mass_error_append_descriptor(
Bucket_Buffer *result,
const Descriptor *descriptor,
u64 level
);
static void
mass_append_function_signature_string(
Bucket_Buffer *result,
const Function_Info *info
) {
bucket_buffer_append(result, "(");
for (u64 i = 0; i < dyn_array_length(info->parameters); ++i) {
Resolved_Function_Parameter *arg = dyn_array_get(info->parameters, i);
if (i != 0) bucket_buffer_append(result, ", ");
if (arg->symbol) {
bucket_buffer_append(result, arg->symbol->name);
} else {
bucket_buffer_append(result, "_");
}
bucket_buffer_append(result, " : ");
mass_error_append_descriptor(result, arg->descriptor, 1);
}
bucket_buffer_append(result, ")");
const char *arrow = info->flags & Function_Info_Flags_Compile_Time ? " => " : " -> ";
bucket_buffer_append(result, arrow);
if (info->return_descriptor) {
mass_error_append_descriptor(result, info->return_descriptor, 1);
} else {
bucket_buffer_append(result, "_");
}
}
static void
mass_error_append_descriptor(
Bucket_Buffer *result,
const Descriptor *descriptor,
u64 level
) {
if (descriptor->brand) {
bucket_buffer_append(result, descriptor->brand->name);
return;
}
if (level > 3) {
bucket_buffer_append(result, "..");
return;
}
char print_buffer[32] = {0};
switch(descriptor->tag) {
case Descriptor_Tag_Void: {
bucket_buffer_append(result, "()");
} break;
case Descriptor_Tag_Never: {
bucket_buffer_append(result, "Never");
} break;
case Descriptor_Tag_Integer:
case Descriptor_Tag_Float: {
const char *prefix = descriptor->tag == Descriptor_Tag_Float ? "f" : descriptor->Integer.is_signed ? "s" : "u";
bucket_buffer_append(result, prefix);
snprintf(print_buffer, countof(print_buffer), "%"PRIu64, descriptor->bit_size.as_u64);
bucket_buffer_append(result, print_buffer);
} break;
case Descriptor_Tag_Raw: {
bucket_buffer_append(result, "i");
snprintf(print_buffer, countof(print_buffer), "%"PRIu64, descriptor->bit_size.as_u64);
bucket_buffer_append(result, print_buffer);
} break;
case Descriptor_Tag_Pointer_To: {
bucket_buffer_append(result, "&");
mass_error_append_descriptor(result, descriptor->Pointer_To.descriptor, level + 1);
} break;
case Descriptor_Tag_Fixed_Array: {
mass_error_append_descriptor(result, descriptor->Fixed_Array.item, level + 1);
u64 item_count = descriptor->Fixed_Array.length;
snprintf(print_buffer, countof(print_buffer), "%"PRIu64, item_count);
bucket_buffer_append(result, "*");
bucket_buffer_append(result, print_buffer);
} break;
case Descriptor_Tag_Struct: {
bucket_buffer_append(result, "[");
bool is_first = true;
DYN_ARRAY_FOREACH(Struct_Field, it, descriptor->Struct.fields) {
if (is_first) is_first = false; else bucket_buffer_append(result, ", ");
if (it->name.length) {
bucket_buffer_append(result, it->name);
bucket_buffer_append(result, " : ");
}
mass_error_append_descriptor(result, it->descriptor, level + 1);
}
bucket_buffer_append(result, "]");
} break;
case Descriptor_Tag_Function_Instance: {
mass_append_function_signature_string(result, descriptor->Function_Instance.info);
} break;
}
}
static Fixed_Buffer *
mass_error_to_string(
Compilation *compilation,
Mass_Error const* error
) {
Bucket_Buffer *buffer = bucket_buffer_make();
switch(error->tag) {
case Mass_Error_Tag_Unimplemented: {
bucket_buffer_append(buffer, "Unimplemented Feature: ");
bucket_buffer_append(buffer, error->detailed_message);
} break;
case Mass_Error_Tag_Unreachable_Statement: {
bucket_buffer_append(buffer, "Unreachable Statement");
if (error->detailed_message.length) {
bucket_buffer_append(buffer, ": ");
bucket_buffer_append(buffer, error->detailed_message);
}
} break;
case Mass_Error_Tag_User_Defined: {
bucket_buffer_append(buffer, error->User_Defined.name);
bucket_buffer_append(buffer, ": ");
bucket_buffer_append(buffer, error->detailed_message);
} break;
case Mass_Error_Tag_Assignment_To_Constant: {
bucket_buffer_append(buffer, "Trying to assign to a constant value");
} break;
case Mass_Error_Tag_Parse: {
bucket_buffer_append(buffer, "Unable to parse the expression");
if (error->detailed_message.length) {
bucket_buffer_append(buffer, ": ");
bucket_buffer_append(buffer, error->detailed_message);
}
} break;
case Mass_Error_Tag_Tokenizer: {
bucket_buffer_append(buffer, "Error while tokenizing");
if (error->detailed_message.length) {
bucket_buffer_append(buffer, ": ");
bucket_buffer_append(buffer, error->detailed_message);
}
} break;
case Mass_Error_Tag_Undefined_Variable: {
bucket_buffer_append(buffer, "Undefined variable '");
bucket_buffer_append(buffer, error->Undefined_Variable.name);
bucket_buffer_append(buffer, "'");
} break;
case Mass_Error_Tag_Redefinition: {
bucket_buffer_append(buffer, "Redefinition of binding ");
bucket_buffer_append(buffer, error->Redefinition.name);
bucket_buffer_append(buffer, "\n previously defined here:\n ");
Slice other_source = source_from_source_range(compilation, &error->other_source_range);
bucket_buffer_append(buffer, other_source);
} break;
case Mass_Error_Tag_Circular_Dependency: {
bucket_buffer_append(buffer, "Circular dependency when resolving ");
bucket_buffer_append(buffer, error->Circular_Dependency.name);
} break;
case Mass_Error_Tag_Unknown_Field: {
bucket_buffer_append(buffer, "Field ");
bucket_buffer_append(buffer, error->Unknown_Field.name);
bucket_buffer_append(buffer, " does not exist on type ");
mass_error_append_descriptor(buffer, error->Unknown_Field.type, 0);
} break;
case Mass_Error_Tag_Invalid_Identifier: {
bucket_buffer_append(buffer, "Invalid identifier");
} break;
case Mass_Error_Tag_Dynamic_Library_Load: {
bucket_buffer_append(buffer, "Unable to load a dynamic library ");
bucket_buffer_append(buffer, error->Dynamic_Library_Load.library_name);
} break;
case Mass_Error_Tag_Dynamic_Library_Symbol_Not_Found: {
bucket_buffer_append(buffer, "Unable to resolve a symbol ");
bucket_buffer_append(buffer, error->Dynamic_Library_Symbol_Not_Found.symbol_name);
bucket_buffer_append(buffer, " from a dynamic library ");
bucket_buffer_append(buffer, error->Dynamic_Library_Symbol_Not_Found.library_name);
} break;
case Mass_Error_Tag_File_Open: {
bucket_buffer_append(buffer, "Can not open file ");
bucket_buffer_append(buffer, error->File_Open.path);
} break;
case Mass_Error_Tag_File_Too_Large: {
bucket_buffer_append(buffer, "File ");
bucket_buffer_append(buffer, error->File_Too_Large.path);
bucket_buffer_append(buffer, " is larger than 4GB");
} break;
case Mass_Error_Tag_Expected_Static: {
bucket_buffer_append(buffer, "Expected value to be static (compile-time known)");
} break;
case Mass_Error_Tag_Operator_Fixity_Conflict: {
if (error->Operator_Fixity_Conflict.fixity == Operator_Fixity_Prefix) {
bucket_buffer_append(buffer, "There is already a prefix operator ");
} else {
bucket_buffer_append(buffer, "There is already a infix or postfix operator ");
}
bucket_buffer_append(buffer, error->Operator_Fixity_Conflict.symbol);
bucket_buffer_append(buffer, " defined in this scope");
} break;
case Mass_Error_Tag_Non_Trailing_Default_Argument: {
bucket_buffer_append(buffer, "An argument without a default value can not come after an argument that has one");
} break;
case Mass_Error_Tag_Type_Mismatch: {
Mass_Error_Type_Mismatch const *mismatch = &error->Type_Mismatch;
if (error->detailed_message.length) {
bucket_buffer_append(buffer, error->detailed_message);
} else {
bucket_buffer_append(buffer, "Type mismatch: expected ");
mass_error_append_descriptor(buffer, mismatch->expected, 0);
bucket_buffer_append(buffer, ", got ");
mass_error_append_descriptor(buffer, mismatch->actual, 0);
}
} break;
case Mass_Error_Tag_Integer_Range: {
bucket_buffer_append(buffer, "Value does not fit into integer of type ");
mass_error_append_descriptor(buffer, error->Integer_Range.descriptor, 0);
} break;
case Mass_Error_Tag_Epoch_Mismatch: {
bucket_buffer_append(buffer, "Trying to access a value from the wrong execution epoch ");
Slice source = source_from_source_range(compilation, &error->source_range);
bucket_buffer_append(buffer, source);
bucket_buffer_append(buffer, ".\n");
bucket_buffer_append(buffer,
"This happens when you access value from runtime in compile-time execution "
"or a runtime value from a different stack frame than current function call."
);
} break;
case Mass_Error_Tag_Undecidable_Overload: {
Mass_Error_Undecidable_Overload const *overloads = &error->Undecidable_Overload;
bucket_buffer_append(buffer, "Could not decide which overload is better: \n ");
for (u64 i = 0; i < dyn_array_length(overloads->matches); ++i) {
if (i != 0) bucket_buffer_append(buffer, "\n ");
const Undecidable_Match *match = dyn_array_get(overloads->matches, i);
mass_append_function_signature_string(buffer, match->info);
}
} break;
case Mass_Error_Tag_Non_Function_Overload: {
bucket_buffer_append(buffer, "Trying to define a non-function overload ");
Slice source = source_from_source_range(compilation, &error->source_range);
bucket_buffer_append(buffer, source);
} break;
case Mass_Error_Tag_No_Matching_Overload: {
Mass_Error_No_Matching_Overload const *no_match = &error->No_Matching_Overload;
bucket_buffer_append(buffer, "Could not find matching overload for call.\n Arguments: ");
for (u64 i = 0; i < dyn_array_length(no_match->arguments); ++i) {
if (i != 0) bucket_buffer_append(buffer, ", ");
const Resolved_Function_Parameter *arg = dyn_array_get(no_match->arguments, i);
mass_error_append_descriptor(buffer, arg->descriptor, 0);
}
bucket_buffer_append(buffer, "\n Source code: ");
Slice source = source_from_source_range(compilation, &error->source_range);
bucket_buffer_append(buffer, source);
} break;
case Mass_Error_Tag_No_Runtime_Use: {
if (error->detailed_message.length) {
bucket_buffer_append(buffer, error->detailed_message);
} else {
bucket_buffer_append(buffer, "Value can't be used at runtime");
}
bucket_buffer_append(buffer, ":\n");
Slice source = source_from_source_range(compilation, &error->source_range);
bucket_buffer_append(buffer, source);
} break;
case Mass_Error_Tag_Recursive_Intrinsic_Use: {
bucket_buffer_append(buffer, "Recursive calls to intrinsics are not allowed.\n");
bucket_buffer_append(buffer, "Since an intrinsic is called during compilation of a fn body\n");
bucket_buffer_append(buffer, "if it contains a direct or indirect call to itself\n");
bucket_buffer_append(buffer, "as the body is not read - we can not execute it.");
} break;
}
Fixed_Buffer *result = bucket_buffer_to_fixed_buffer(allocator_system, buffer);
bucket_buffer_destroy(buffer);
return result;
}
static inline const void *
get_static_storage_with_bit_size(
const Storage *storage,
Bits bit_size
) {
assert(storage->bit_size.as_u64 == bit_size.as_u64);
assert(storage->tag == Storage_Tag_Static);
return storage->Static.pointer;
}
static bool
same_function_signature(
const Function_Info *a_info,
const Function_Info *b_info
) {
if (!same_type(a_info->return_descriptor, b_info->return_descriptor)) {
return false;
}
if (dyn_array_length(a_info->parameters) != dyn_array_length(b_info->parameters)) {
return false;
}
for (u64 i = 0; i < dyn_array_length(a_info->parameters); ++i) {
Resolved_Function_Parameter *a_arg = dyn_array_get(a_info->parameters, i);
Resolved_Function_Parameter *b_arg = dyn_array_get(b_info->parameters, i);
if(!same_type(a_arg->descriptor, b_arg->descriptor)) return false;
}
return true;
}
typedef enum {
Brand_Comparison_Mode_Strict,
Brand_Comparison_Mode_Ignore,
Brand_Comparison_Mode_One_Unbranded,
} Brand_Comparison_Mode;
typedef struct Types_Equal_Nested_List {
const Descriptor *descriptor;
struct Types_Equal_Nested_List *previous;
} Types_Equal_Nested_List;
static inline bool
is_in_types_equal_nested_list(
const Types_Equal_Nested_List* list,
const Descriptor *descriptor
) {
for (; list; list = list->previous) {
if (list->descriptor == descriptor) return true;
}
return false;
}
static bool
types_equal_internal(
const Descriptor *a,
const Descriptor *b,
Brand_Comparison_Mode brand_comparison_mode,
Types_Equal_Nested_List *nested_list
) {
if (a->brand != b->brand) {
switch (brand_comparison_mode) {
case Brand_Comparison_Mode_Strict: {
return false;
}
case Brand_Comparison_Mode_Ignore: {
// nothing to do
} break;
case Brand_Comparison_Mode_One_Unbranded: {
if (a->brand && b->brand) return false;
} break;
}
}
if (a == b) return true;
if (a->tag != b->tag) return false;
if (a->bit_size.as_u64 != b->bit_size.as_u64) return false;
if (a->bit_alignment.as_u64 != b->bit_alignment.as_u64) return false;
if (is_in_types_equal_nested_list(nested_list, a)) return true;
nested_list = &(Types_Equal_Nested_List){a, nested_list};
switch(a->tag) {
case Descriptor_Tag_Void:
case Descriptor_Tag_Never:
case Descriptor_Tag_Float: {
return true;
}
case Descriptor_Tag_Integer: {
return a->Integer.is_signed == b->Integer.is_signed;
}
case Descriptor_Tag_Pointer_To: {
return types_equal_internal(a->Pointer_To.descriptor, b->Pointer_To.descriptor, brand_comparison_mode, nested_list);
}
case Descriptor_Tag_Fixed_Array: {
return types_equal_internal(a->Fixed_Array.item, b->Fixed_Array.item, brand_comparison_mode, nested_list) &&
a->Fixed_Array.length == b->Fixed_Array.length;
}
case Descriptor_Tag_Struct: {
if (dyn_array_length(a->Struct.fields) != dyn_array_length(b->Struct.fields)) {
return false;
}
for (u64 i = 0; i < dyn_array_length(a->Struct.fields); ++i) {
const Descriptor *a_field = dyn_array_get(a->Struct.fields, i)->descriptor;
const Descriptor *b_field = dyn_array_get(b->Struct.fields, i)->descriptor;
if (!types_equal_internal(a_field, b_field, brand_comparison_mode, nested_list)) return false;
}
return true;
}
case Descriptor_Tag_Raw: {
return true;
}
case Descriptor_Tag_Function_Instance: {
return same_function_signature(a->Function_Instance.info, b->Function_Instance.info);
}
default: {
assert(!"Unsupported descriptor type");
return false;
}
}
}
static bool
types_equal(
const Descriptor *a,
const Descriptor *b,
Brand_Comparison_Mode brand_comparison_mode
) {
return types_equal_internal(a, b, brand_comparison_mode, 0);
}
static inline bool
same_type(
const Descriptor *a,
const Descriptor *b
) {
return types_equal(a, b, Brand_Comparison_Mode_Strict);
}
static inline u64
descriptor_byte_size(
const Descriptor *descriptor
) {
u64 bit_size = descriptor->bit_size.as_u64;
u64 byte_size = (bit_size + (CHAR_BIT - 1)) / CHAR_BIT;
if (byte_size * CHAR_BIT != bit_size) {
panic("TODO support non-byte aligned sizes");
}
return byte_size;
}
static inline u64
descriptor_byte_alignment(
const Descriptor *descriptor
) {
u64 bit_alignment = descriptor->bit_alignment.as_u64;
u64 byte_alignment = (bit_alignment + (CHAR_BIT - 1)) / CHAR_BIT;
if (byte_alignment * CHAR_BIT != bit_alignment) {
panic("TODO support non-byte aligned sizes");
}
return byte_alignment;
}
static inline Label *
make_label(
const Allocator *allocator,
Program *program,
Section *section,
Slice name
) {
Label *label = allocator_allocate(allocator, Label);
*label = (Label) {
.program = program,
.section = section,
.name = name,
};
return label;
}
static inline Storage
data_label32(
Label *label,
Bits bit_size
) {
return (const Storage) {
.tag = Storage_Tag_Memory,
.bit_size = bit_size,
.Memory.location = {
.tag = Memory_Location_Tag_Instruction_Pointer_Relative,
.Instruction_Pointer_Relative.label = label
}
};
}
static inline Storage
code_label32(
Label *label
) {
return (const Storage) {
.tag = Storage_Tag_Memory,
// FIXME this is set at 32 as otherwise current encoder is unhappy
// about the size mismatch. It should be zero instead.
.bit_size = {32},
.Memory.location = {
.tag = Memory_Location_Tag_Instruction_Pointer_Relative,
.Instruction_Pointer_Relative.label = label,
}
};
}
static inline Storage
storage_static_heap(
const void *value,
Bits bit_size
) {
return (Storage){
.tag = Storage_Tag_Static,
.bit_size = bit_size,
.Static.pointer = value,
};
}
#define storage_static(_VALUE_)\
storage_static_heap((_VALUE_), (Bits){sizeof(*(_VALUE_)) * CHAR_BIT})
static inline Storage
storage_immediate_with_bit_size(
const void *source,
Bits bit_size
) {
Storage result = {
.tag = Storage_Tag_Immediate,
.bit_size = bit_size,
};
assert(bit_size.as_u64 <= sizeof(result.Immediate.bits) * CHAR_BIT);
result.Immediate.bits = 0;
if (bit_size.as_u64) {
assert(source);
memcpy(&result.Immediate.bits, source, bit_size.as_u64 / CHAR_BIT);
}
return result;
}
#define storage_immediate(_VALUE_)\
storage_immediate_with_bit_size((_VALUE_), (Bits){sizeof(*(_VALUE_)) * CHAR_BIT})
#define DEFINE_IMM_X(_BIT_SIZE_)\
static inline Storage\
imm##_BIT_SIZE_(\
u##_BIT_SIZE_ value\
) {\
return storage_immediate_with_bit_size(&value, (Bits){_BIT_SIZE_});\
}
DEFINE_IMM_X(8)
DEFINE_IMM_X(16)
DEFINE_IMM_X(32)
DEFINE_IMM_X(64)
static inline Storage
imm_auto_8_or_32(
s64 value
) {
if (s64_fits_into_s8(value)) {
return imm8((s8) value);
}
if (s64_fits_into_s32(value)) {
return imm32((s32) value);
}
panic("Storage is does not fit into either s8 or s32");
return (Storage){0};
}
static inline Storage
storage_stack(
s32 offset,
Bits bit_size,
Stack_Area area
) {
assert(bit_size.as_u64);
return (Storage) {
.tag = Storage_Tag_Memory,
.bit_size = bit_size,
.Memory.location = {
.tag = Memory_Location_Tag_Stack,
.Stack = {
.area = area,
.offset = offset
},
}
};
}
static inline Storage
storage_with_offset_and_bit_size(
const Storage *base,
s32 offset,
Bits bit_size
);
static bool
mass_maybe_find_disjoint_piece_for_move(
const Storage *base,
Bits offset_in_bits,
Bits bit_size,
Storage *out_storage
) {
assert(base->tag == Storage_Tag_Disjoint);
u64 bit_start = 0;
for (u64 i = 0; i < dyn_array_length(base->Disjoint.pieces); ++i) {
const Storage *piece = *dyn_array_get(base->Disjoint.pieces, i);
u64 bit_end = bit_start + piece->bit_size.as_u64;
bool starts_in_this_piece = offset_in_bits.as_u64 >= bit_start && offset_in_bits.as_u64 < bit_end;
if (starts_in_this_piece) {
u64 offset_in_bits_in_this_piece = offset_in_bits.as_u64 - bit_start;
// Spans more than one piece
if (offset_in_bits_in_this_piece + bit_size.as_u64 > piece->bit_size.as_u64) return false;
s32 nested_byte_offset = u64_to_s32(offset_in_bits_in_this_piece / 8);
*out_storage = storage_with_offset_and_bit_size(piece, nested_byte_offset, bit_size);
return true;
}
bit_start = bit_end;
}
return false;
}
static inline Storage
storage_with_offset_and_bit_size(
const Storage *base,
s32 offset,
Bits bit_size
) {
if (offset < 0) panic("Negative offsets are not supported");
Storage result = *base;
// Do not inherit flags as it causes issue when a struct or an array
// is iterated over. In this case storage might be released multiple times.
result.flags = 0;
result.bit_size = bit_size;
u64 offset_in_bits = s32_to_u64(offset) * 8;
if (offset_in_bits + bit_size.as_u64 > base->bit_size.as_u64) {
panic("Out of bounds access on a storage");
}
bool same_size_as_base = (bit_size.as_u64 == base->bit_size.as_u64);
if (same_size_as_base) {
assert(offset_in_bits == 0); // Implied by check for out of bounds above
return result;
}
switch(base->tag) {
default:
case Storage_Tag_Eflags:
case Storage_Tag_Xmm: {
panic("Internal Error: Unexpected storage type for structs");
} break;
case Storage_Tag_Immediate: {
result.Immediate.bits >>= offset_in_bits;
return result;
}
case Storage_Tag_Register: {
result.Register.packed |= bit_size.as_u64 != base->bit_size.as_u64;
result.Register.offset_in_bits += u64_to_u16(offset_in_bits);
} break;
case Storage_Tag_Disjoint: {
Storage maybe_piece_storage = {0};
bool fits_into_one_piece = mass_maybe_find_disjoint_piece_for_move(
base, (Bits){offset_in_bits}, bit_size, &maybe_piece_storage
);
if (fits_into_one_piece) {
return maybe_piece_storage;
} else {
result.Disjoint.packed |= bit_size.as_u64 != base->bit_size.as_u64;
result.Disjoint.offset_in_bits += u64_to_u32(offset_in_bits);
}
} break;
case Storage_Tag_Static: {
const s8 *pointer = get_static_storage_with_bit_size(base, base->bit_size);
result = storage_static_heap(pointer + offset, bit_size);
} break;
case Storage_Tag_Memory: {
switch(result.Memory.location.tag) {
case Memory_Location_Tag_Instruction_Pointer_Relative: {
result.Memory.location.Instruction_Pointer_Relative.offset += offset;
} break;
case Memory_Location_Tag_Indirect: {
result.Memory.location.Indirect.offset += offset;
} break;
case Memory_Location_Tag_Stack: {
result.Memory.location.Stack.offset += offset;
} break;
}
} break;
}
return result;
}
static inline bool
storage_is_register_or_memory(
const Storage *operand
) {
return operand->tag == Storage_Tag_Register || operand->tag == Storage_Tag_Memory;
}
static inline bool
storage_is_register_index(
const Storage *storage,
Register reg_index
) {
return storage->tag == Storage_Tag_Register && storage->Register.index == reg_index;
}
static bool
storage_static_equal_internal(
const Descriptor *a_descriptor,
const void *a_memory,
const Descriptor *b_descriptor,
const void *b_memory
) {
if (!same_type(a_descriptor, b_descriptor)) return false;
u64 byte_size = descriptor_byte_size(a_descriptor);
// TODO @Speed This should be a simple memcmp, but right now there is no good story
// for padding bytes in Mass, so safer to recurse.
//return memcmp(a_memory, b_memory, byte_size) == 0;
switch(a_descriptor->tag) {
case Descriptor_Tag_Never:
case Descriptor_Tag_Void: {
return true;
}
// Opaques, references and pointers can be compared with memcmp
case Descriptor_Tag_Float:
case Descriptor_Tag_Integer:
case Descriptor_Tag_Raw:
case Descriptor_Tag_Pointer_To: {
return memcmp(a_memory, b_memory, byte_size) == 0;
}
case Descriptor_Tag_Fixed_Array: {
// compare item by item
if (a_descriptor->Fixed_Array.length != b_descriptor->Fixed_Array.length) {
return false;
}
for (u64 i = 0; i < a_descriptor->Fixed_Array.length; ++i) {
const Descriptor *a_item = a_descriptor->Fixed_Array.item;
const Descriptor *b_item = b_descriptor->Fixed_Array.item;
u64 offset = descriptor_byte_size(a_item) * i;
if (!storage_static_equal_internal(
a_item, (s8 *)a_memory + offset, b_item, (s8 *)b_memory + offset
)) {
return false;
}
}
} break;
case Descriptor_Tag_Struct: {
// compare field by field
u64 field_count = dyn_array_length(a_descriptor->Struct.fields);
assert(field_count == dyn_array_length(b_descriptor->Struct.fields));
for (u64 i = 0; i < field_count; ++i) {
const Struct_Field *field = dyn_array_get(a_descriptor->Struct.fields, i);
assert(same_type(field->descriptor, dyn_array_get(b_descriptor->Struct.fields, i)->descriptor));
if (!storage_static_equal_internal(
field->descriptor, (s8 *)a_memory + field->offset,
field->descriptor, (s8 *)b_memory + field->offset
)) {
return false;
}
}
} break;
case Descriptor_Tag_Function_Instance: {
panic("Unexpected static storage function");
} break;
}
return true;
}
static inline const void *
storage_static_memory_with_bit_size(
const Storage *storage,
Bits bit_size
) {
assert(storage->bit_size.as_u64 == bit_size.as_u64);
if (storage->tag == Storage_Tag_Static) {
return get_static_storage_with_bit_size(storage, bit_size);
} else if (storage->tag == Storage_Tag_Immediate) {
return &storage->Immediate.bits;
} else {
panic("Unexpected static storage tag");
}
return 0;
}
static inline const void *
storage_static_memory(
const Storage *storage
) {
return storage_static_memory_with_bit_size(storage, storage->bit_size);
}
static bool
storage_static_equal(
const Descriptor *a_descriptor,
const Storage *a_storage,
const Descriptor *b_descriptor,
const Storage *b_storage
) {
if (!same_type(a_descriptor, b_descriptor)) return false;
assert(a_storage->bit_size.as_u64 == b_storage->bit_size.as_u64);
const void *a_memory = storage_static_memory_with_bit_size(a_storage, a_descriptor->bit_size);
const void *b_memory = storage_static_memory_with_bit_size(b_storage, b_descriptor->bit_size);
return storage_static_equal_internal(a_descriptor, a_memory, b_descriptor, b_memory);
}
static inline bool
storage_equal(
const Storage *a,
const Storage *b
) {
if (a->tag != b->tag) return false;
if (a->bit_size.as_u64 != b->bit_size.as_u64) return false;
switch(a->tag) {
case Storage_Tag_Eflags: {
return a->Eflags.compare_type == b->Eflags.compare_type;
}
case Storage_Tag_Static: {
// We need to know the memory layout (the descriptor) of the
// static values to properly compare them. `memcmp` does not
// work due to padding.
panic("Static values must be compared using storage_static_equal");
break;
}
case Storage_Tag_Memory: {
const Memory_Location *a_location = &a->Memory.location;
const Memory_Location *b_location = &b->Memory.location;
if (a_location->tag != b_location->tag) return false;
switch(a_location->tag) {
case Memory_Location_Tag_Instruction_Pointer_Relative: {
// TODO Should this do a compare based on offset?
return a_location->Instruction_Pointer_Relative.label
== b_location->Instruction_Pointer_Relative.label;
}
case Memory_Location_Tag_Indirect: {
return (
a_location->Indirect.base_register == b_location->Indirect.base_register &&
a_location->Indirect.offset == b_location->Indirect.offset
);
}
case Memory_Location_Tag_Stack: {
return (
a_location->Stack.area == b_location->Stack.area &&
a_location->Stack.offset == b_location->Stack.offset
);
}
}
panic("Internal Error: Unexpected Memory_Location_Tag");
return false;
}
case Storage_Tag_Disjoint: {
if (dyn_array_length(a->Disjoint.pieces) != dyn_array_length(b->Disjoint.pieces)) return false;
for (u64 i = 0; i < dyn_array_length(a->Disjoint.pieces); ++i) {
const Storage *a_piece = *dyn_array_get(a->Disjoint.pieces, i);
const Storage *b_piece = *dyn_array_get(b->Disjoint.pieces, i);
if (!storage_equal(a_piece, b_piece)) return false;
}
return true;
}
case Storage_Tag_Immediate: {
return memcmp(&a->Immediate.bits, &b->Immediate.bits, a->bit_size.as_u64 / 8) == 0;
}
case Storage_Tag_Xmm: {
return a->Xmm.index == b->Xmm.index;
}
case Storage_Tag_Register: {
return (
a->Register.index == b->Register.index &&
a->Register.offset_in_bits == b->Register.offset_in_bits
);
}
}
panic("Unknown operand type");
return false;
}
static inline Label *
allocate_section_memory(
const Allocator *allocator,
Program *program,
Section *section,
u64 byte_size,
u64 alignment
) {
virtual_memory_buffer_allocate_bytes(§ion->buffer, byte_size, alignment);
u64 offset_in_data_section = section->buffer.occupied - byte_size;
Label *label = make_label(allocator, program, section, slice_literal("global"));
label->offset_in_section = u64_to_u32(offset_in_data_section);
label->resolved = true;
return label;
}
static inline Storage
storage_indirect(
Bits bit_size,
Register reg
) {
return (Storage){
.tag = Storage_Tag_Memory,
.bit_size = bit_size,
.Memory.location = {
.tag = Memory_Location_Tag_Indirect,
.Indirect = {
.base_register = reg
},
},
};
}
static inline Storage
storage_eflags(
Compare_Type compare_type
) {
return (Storage){
.tag = Storage_Tag_Eflags,
.bit_size = {8},
.Eflags = { .compare_type = compare_type }
};
}
static inline Storage
storage_register(
Register reg,
Bits bit_size
) {
assert(
bit_size.as_u64 == 8 ||
bit_size.as_u64 == 16 ||
bit_size.as_u64 == 32 ||
bit_size.as_u64 == 64
);
Storage result = {
.tag = register_is_xmm(reg) ? Storage_Tag_Xmm : Storage_Tag_Register,
.Register.index = reg,
.bit_size = bit_size,
};
return result;
}
static inline Storage
storage_register_temp(
Function_Builder *builder,
Bits bit_size
) {
Register reg = register_acquire_temp(builder);
Storage storage = storage_register(reg, bit_size);
storage.flags |= Storage_Flags_Temporary;
return storage;
}
static inline void
storage_release_if_temporary(
Function_Builder *builder,
const Storage *storage
) {
if (!(storage->flags & Storage_Flags_Temporary)) return;
switch (storage->tag) {
case Storage_Tag_Register: {
register_release(builder, storage->Register.index);
break;
}
case Storage_Tag_Xmm: {
register_release(builder, storage->Xmm.index);