-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.jai
1751 lines (1445 loc) · 61.4 KB
/
module.jai
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
#module_parameters(count_type: Type = int, unicode_strings: bool = false, write_constants: bool = false);
// #run assert(!reuse_memory || (count_type == int && !unicode_strings && !write_constants), "You cannot change any of the serializer settings if reuse memory is enabled.");
// TODO the current problem is serializer_read always memcpys for reuse mode, even though it should only do it on the first call
// what makes this so complicated to read, is typically the reuse serializer will write the raw bytes of an object or array into the bytes array/memory
// and then it will update the value data pointer to point back to that newly written data, so if it then changes something like value.data (the data pointer of an array)
// the data change will be reflected in the raw byte array
serializer_read :: (bytes: [] u8, value: *$T, $reuse_memory: bool = false, $loc := #caller_location) {
serializer_read(bytes, value, 0, reuse_memory);
}
serializer_read :: (bytes: [] u8, value: *$T, offset: int, $reuse_memory: bool = false, $loc := #caller_location) -> int {
assert(valid_alignment(T), "Invalid serialization type read alignment.\n");
// info :: #run type_info(T);
info_type :: #run type_info(T).type;
t_size :: #run size_of(T);
pod :: #run is_pod(T);
#if reuse_memory {
assert(offset == 0, "You cannot currently use reuse_memory mode with an offset.");
view: [] u8;
view.data = bytes.data + offset;
view.count = bytes.count - offset;
}
#if reuse_memory || pod {
memcpy(value, bytes.data + offset, t_size);
offset += t_size;
}
#if !pod {
#if info_type == .STRING {
#if reuse_memory {
inline read_string_reuse(view, value);
} else {
offset = inline read_string(bytes, value, offset);
}
} else #if info_type == .POINTER {
#if reuse_memory {
inline read_pointer_reuse(view, value);
} else {
offset = inline read_pointer(bytes, value, offset);
}
} else #if info_type == .STRUCT {
#if reuse_memory {
inline read_struct_reuse(view, value);
} else {
offset = inline read_struct(bytes, value, offset);
}
} else #if info_type == .ARRAY {
#if reuse_memory {
inline read_array_reuse(view, value);
} else {
offset = inline read_array(bytes, value, offset);
}
} else #if info_type == .VOID {
} else {
#run print("Unhandled serialization type. %\n", T);
#run assert(false, tprint("Unhandled serialization type. %\n", T));
}
}
return offset;
}
serializer_write :: (bytes: *[..] u8, value: $T, $reuse_memory: bool = false) {
assert(valid_alignment(T), "Invalid serialization type write alignment.\n");
// info :: #run type_info(T);
info_type :: #run type_info(T).type;
t_size :: #run size_of(T);
pod :: #run is_pod(T);
#if pod {
array_reserve(bytes, bytes.count + t_size);
} else {
#if reuse_memory {
allocate_size := get_write_size_reuse(value);
} else {
allocate_size := get_write_size(value);
}
array_reserve(bytes, bytes.count + allocate_size);
}
#if reuse_memory || pod {
memcpy(bytes.data + bytes.count, *value, t_size);
bytes.count += t_size;
}
#if !pod {
#if info_type == .STRING {
#if reuse_memory {
inline write_string_reuse(bytes, cast(*T) (bytes.data + bytes.count - t_size));
} else {
inline write_string(bytes, value);
}
} else #if info_type == .POINTER {
#if reuse_memory {
inline write_pointer_reuse(bytes, cast(*T) (bytes.data + bytes.count - t_size));
} else {
inline write_pointer(bytes, value);
}
} else #if info_type == .STRUCT {
#if reuse_memory {
inline write_struct_reuse(bytes, cast(*T) (bytes.data + bytes.count - t_size));
} else {
inline write_struct(bytes, value);
}
} else #if info_type == .ARRAY {
#if reuse_memory {
inline write_array_reuse(bytes, cast(*T) (bytes.data + bytes.count - t_size));
} else {
inline write_array(bytes, value);
}
} else #if info_type == .VOID {
} else {
#run print("Unhandled serialization type. %\n", T);
#run assert(false, "Unhandled serialization type.");
}
}
assert(bytes.count == bytes.allocated, tprint("Allocated and count size must match. % = %\n", bytes.count, bytes.allocated));
}
// this function errs on the side of caution, some types aren't even considered
is_pod :: (T: Type) -> bool {
info := cast(*Type_Info) T;
if info.type == {
case .INTEGER;
return true;
case .FLOAT;
return true;
case .BOOL;
return true;
case .ENUM;
return true;
case .STRUCT;
struct_info := cast(*Type_Info_Struct) info;
pod := true;
for member: struct_info.members {
// could a constant member be non-constant?
is_pod_member := (member.flags & .CONSTANT) || is_pod(get_type(member.type));
pod = pod && is_pod_member;
}
return pod;
case .ARRAY;
array_info := cast(*Type_Info_Array) info;
pod := true;
pod = pod && array_info.array_type == .FIXED;
pod = pod && is_pod(get_type(array_info.element_type));
return pod;
case;
return false;
}
}
#scope_file
should_visited :: (info: *Type_Info_Struct, member_index: int) -> bool {
if info.members[member_index].flags & .CONSTANT {
return false;
}
for i: 0..member_index - 1 {
if info.members[i].offset_in_bytes == info.members[member_index].offset_in_bytes return false;
}
return true;
}
get_alignment :: (T: Type) -> int {
info := cast(*Type_Info) T;
if info.type == {
case .STRUCT;
struct_info := cast(*Type_Info_Struct) info;
largest := 0;
for member: struct_info.members {
if !should_visited(struct_info, it_index) {
continue;
}
largest = max(largest, get_alignment((cast(*Type) *member.type).*));
}
return largest;
case .ARRAY;
array_info := cast(*Type_Info_Array) info;
if #complete array_info.array_type == {
case .FIXED;
return get_alignment((cast(*Type) *array_info.element_type).*);
case .VIEW;
return max(8, get_alignment((cast(*Type) *array_info.element_type).*));
case .RESIZABLE;
return max(8, get_alignment((cast(*Type) *array_info.element_type).*));
}
case .STRING; #through;
case .PROCEDURE; #through;
case .POINTER; #through;
case .TYPE;
return 8;
case .FLOAT; #through;
case .INTEGER; #through;
case .VOID; #through;
case .BOOL; #through;
case .ENUM;
return min(next_power_of_two((cast(*Type_Info) T).runtime_size), 8);
case;
assert(false, "Invalid alignment type. %\n", T);
return 0;
}
}
valid_alignment :: (T: Type) -> bool {
info := cast(*Type_Info) T;
expected_offset := 0;
if info.type == {
case .STRUCT;
struct_info := cast(*Type_Info_Struct) info;
for member: struct_info.members {
if !should_visited(struct_info, it_index) {
continue;
}
expected_alignment := get_alignment((cast(*Type) *member.type).*);
if expected_alignment == 0 {
continue;
}
// alignment based on the type size
misaligment := expected_offset % expected_alignment;
if misaligment > 0 {
expected_offset += expected_alignment - misaligment;
}
if member.offset_in_bytes != expected_offset {
assert(false, "Invalid struct member offset. % %\nActual %\nExpected %\n", struct_info.name, member.name, member.offset_in_bytes, expected_offset);
return false;
}
expected_offset += member.type.runtime_size;
}
if struct_info.runtime_size != expected_offset {
assert(false, "Invalid struct size alignment. %\nActual %\nExpected %\n", struct_info.name, struct_info.runtime_size, expected_offset);
return false;
}
return true;
case .POINTER;
pointer_info := cast(*Type_Info_Pointer) info;
return valid_alignment((cast(*Type) *pointer_info.pointer_to).*);
case .ARRAY;
array_info := cast(*Type_Info_Array) info;
if #complete array_info.array_type == {
case .FIXED;
case .VIEW;
arr: [] int;
if cast(int) *arr.count != cast(int) *arr {
assert(false, "Invalid array count offset.\nActual %\nExpected %\n", *arr.count, cast(int) *arr);
return false;
}
if cast(int) *arr.data != cast(int) *arr + 8 {
assert(false, "Invalid array data offset.\nActual %\nExpected %\n", cast(int) *arr.data, cast(int) *arr + 8);
return false;
}
case .RESIZABLE;
arr: [..] int;
if cast(int) *arr.count != cast(int) *arr {
assert(false, "Invalid array count offset.\nActual %\nExpected %\n", *arr.count, cast(int) *arr);
return false;
}
if cast(int) *arr.data != cast(int) *arr + 8 {
assert(false, "Invalid array data offset.\nActual %\nExpected %\n", cast(int) *arr.data, cast(int) *arr + 8);
return false;
}
if cast(int) *arr.allocated != cast(int) *arr + 16 {
assert(false, "Invalid array allocated offset.\nActual %\nExpected %\n", cast(int) *arr.allocated, cast(int) *arr + 16);
return false;
}
if cast(int) *arr.allocator != cast(int) *arr + 24 {
assert(false, "Invalid array allocator offset.\nActual %\nExpected %\n", cast(int) *arr.allocator, cast(int) *arr + 24);
return false;
}
}
return valid_alignment((cast(*Type) *array_info.element_type).*);
case .STRING;
str: string;
if cast(int) *str.count != cast(int) *str {
assert(false, "Invalid string count offset.\nActual %\nExpected %\n", *str.count, cast(int) *str);
return false;
}
if cast(int) *str.data != cast(int) *str + 8 {
assert(false, "Invalid string data offset.\nActual %\nExpected %\n", cast(int) *str.data, cast(int) *str + 8);
return false;
}
return true;
case .PROCEDURE; #through;
case .TYPE; #through;
case .FLOAT; #through;
case .INTEGER; #through;
case .VOID; #through;
case .BOOL; #through;
case .ENUM;
return true;
case;
assert(false, "Unhandled alignment type. %", T);
return false;
}
}
serializer_read_reuse :: (bytes: [] u8, value: *$T) {
// info :: #run type_info(T);
info_type :: #run type_info(T).type;
t_size :: #run size_of(T);
pod :: #run is_pod(T);
#if !pod {
#if info_type == .STRING {
inline read_string_reuse(bytes, value);
} else #if info_type == .POINTER {
inline read_pointer_reuse(bytes, value);
} else #if info_type == .STRUCT {
inline read_struct_reuse(bytes, value);
} else #if info_type == .ARRAY {
inline read_array_reuse(bytes, value);
} else #if info_type == .VOID {
} else {
#run print("Unhandled serialization type. %\n", T);
#run assert(false, "Unhandled serialization type.");
}
}
}
read_string_reuse :: (bytes: [] u8, value: *$T) {
string_size :: #run size_of(T);
value.data = bytes.data + cast(int) value.data;
// (cast(*u8) value.data) += bytes.data;
}
read_string :: (bytes: [] u8, value: *$T, offset: int) -> int {
count_size :: #run size_of(count_type);
string_count := (cast(*count_type) (bytes.data + offset)).*;
#if unicode_strings {
allocate_size := string_count * 4;
} else {
allocate_size := string_count;
}
value.data = alloc(allocate_size);
value.count = string_count;
memcpy(value.data, bytes.data + offset + count_size, allocate_size);
return offset + count_size + allocate_size;
}
read_pointer_reuse :: (bytes: [] u8, value: *$T) {
value.* = cast(T) (cast(*u8) value.* + cast(int) bytes.data);
if value.* {
serializer_read_reuse(bytes, value.*);
}
}
read_pointer :: (bytes: [] u8, value: *$T, offset: int) -> int {
bool_size :: #run size_of(bool);
pointer_type :: #run get_type(type_info(T).pointer_to);
pointer_size :: #run size_of(pointer_type);
exists := (cast(*bool) bytes.data + offset).*;
if !exists {
value.* = null;
return offset + bool_size;
} else {
value.* = New(pointer_type);
return serializer_read(bytes, value.*, offset + bool_size);
}
}
read_struct_reuse :: (bytes: [] u8, value: *$T) {
#insert #run () -> string {
info := cast(*Type_Info_Struct) type_info(T);
builder: String_Builder;
for member: info.members {
member_info := member.type;
member_type := get_type(member_info);
if should_skip_member(member) {
continue;
}
if is_pod(member_type) {
continue;
}
// TODO check place & union
if member_info.type == .STRING {
append(*builder, tprint("inline read_string_reuse(bytes, *value.%);", member.name));
} else if member_info.type == .POINTER {
append(*builder, tprint("inline read_pointer_reuse(bytes, *value.%);", member.name));
} else if member_info.type == .STRUCT {
append(*builder, tprint("inline read_struct_reuse(bytes, *value.%);", member.name));
} else if member_info.type == .ARRAY {
append(*builder, tprint("inline read_array_reuse(bytes, *value.%);", member.name));
} else {
print("Unhandled serialization type. % %\n", member_info.type, T);
assert(false, tprint("Unhandled serialization type. % %\n", member_info.type, T));
}
}
return builder_to_string(*builder);
}();
}
read_struct :: (bytes: [] u8, value: *$T, offset: int) -> int {
// it only gets to this method if there is at least some non-pod chunk
#insert #run () -> string {
info := cast(*Type_Info_Struct) type_info(T);
builder: String_Builder;
visited_byte_offsets: [..] int;
visited_byte_offsets.allocator = temp;
for member: info.members {
member_info := member.type;
member_type := get_type(member_info);
if should_skip_member(member) {
continue;
}
if array_find(visited_byte_offsets, member.offset_in_bytes) {
continue;
}
array_add(*visited_byte_offsets, member.offset_in_bytes);
// TODO check place & union
// TODO continue checking for pod members and memcpy them all at once
if is_pod(member_type) {
t_size := member_info.runtime_size;
append(*builder, tprint("memcpy(*value.%, bytes.data + offset, %);", member.name, t_size));
append(*builder, tprint("offset += %;", t_size));
} else if member_info.type == .STRING {
append(*builder, tprint("offset = inline read_string(bytes, *value.%, offset);", member.name));
} else if member_info.type == .POINTER {
append(*builder, tprint("offset = inline read_pointer(bytes, *value.%, offset);", member.name));
} else if member_info.type == .STRUCT {
append(*builder, tprint("offset = inline read_struct(bytes, *value.%, offset);", member.name));
} else if member_info.type == .ARRAY {
append(*builder, tprint("offset = inline read_array(bytes, *value.%, offset);", member.name));
} else {
print("Unhandled serialization type. %\n", T);
assert(false, "Unhandled serialization type.");
}
}
return builder_to_string(*builder);
}();
return offset;
}
read_array_reuse :: (bytes: [] u8, value: *$T) {
// info :: #run cast(*Type_Info_Array) type_info(T);
// element_info :: #run (cast(*Type_Info_Array) type_info(T)).element_type;
info_array_type :: #run (cast(*Type_Info_Array) type_info(T)).array_type;
element_info_type :: #run (cast(*Type_Info_Array) type_info(T)).element_type.type;
element_type :: #run get_type((cast(*Type_Info_Array) type_info(T)).element_type);
element_size :: #run size_of(element_type);
#if info_array_type == .FIXED {
// copy memory basically, unfortunately
// value.* = (cast(*T) (bytes.data + cast(int) value.data)).*;
} else {
// move data pointer
value.data = cast(*element_type) (cast(int) bytes.data + cast(int) value.data);
}
#if #run info_array_type == .RESIZABLE {
value.allocator = reuse_allocator;
}
// if this array data isn't pod we have to go through 1 by 1
#if #run !is_pod(element_type) {
for i: 0..value.count - 1 {
element_pointer: *element_type = value.data + i;
#if element_info_type == .STRING {
inline read_string_reuse(bytes, element_pointer);
} else #if element_info_type == .POINTER {
inline read_pointer_reuse(bytes, element_pointer);
} else #if element_info_type == .STRUCT {
inline read_struct_reuse(bytes, element_pointer);
} else #if element_info_type == .ARRAY {
inline read_array_reuse(bytes, element_pointer);
} else #if element_info_type == .VOID {
} else {
#run print("Unhandled serialization type. %\n", element_type);
#run assert(false, "Unhandled serialization type.");
}
}
}
}
read_array :: (bytes: [] u8, value: *$T, offset: int) -> int {
// info :: #run cast(*Type_Info_Array) type_info(T);
// element_info :: #run info.element_type;
element_info_type :: #run (cast(*Type_Info_Array) type_info(T)).element_type.type;
element_type :: #run get_type((cast(*Type_Info_Array) type_info(T)).element_type);
element_size :: #run size_of(element_type);
#if #run (cast(*Type_Info_Array) type_info(T)).array_type != .FIXED {
count_size :: #run size_of(count_type);
count: count_type;
memcpy(*count, bytes.data + offset, count_size);
array_resize(value, count);
offset += count_size;
}
#if #run is_pod(element_type) {
allocate_size := value.count * element_size;
memcpy(value.data, bytes.data + offset, allocate_size);
offset += allocate_size;
} else {
for i: 0..value.count - 1 {
element_pointer := value.data + i;
#if element_info_type == .STRING {
offset = inline read_string(bytes, element_pointer, offset);
} else #if element_info_type == .POINTER {
offset = inline read_pointer(bytes, element_pointer, offset);
} else #if element_info_type == .STRUCT {
offset = inline read_struct(bytes, element_pointer, offset);
} else #if element_info_type == .ARRAY {
offset = inline read_array(bytes, element_pointer, offset);
} else #if element_info_type == .VOID {
} else {
#run print("Unhandled serialization type. %\n", element_type);
#run assert(false, "Unhandled serialization type.");
}
}
}
return offset;
}
write_string_reuse :: (bytes: *[..] u8, value: *$T) {
memcpy(bytes.data + bytes.count, value.data, value.count);
value.data = cast(*u8) bytes.count;
bytes.count += value.count;
}
write_string :: (bytes: *[..] u8, value: $T) {
count_size :: #run size_of(count_type);
count := cast(count_type) value.count;
#if unicode_strings {
allocate_size := value.count * 4;
} else {
allocate_size := value.count;
}
memcpy(bytes.data + bytes.count, *count, count_size);
memcpy(bytes.data + bytes.count + count_size, value.data, allocate_size);
bytes.count += count_size + allocate_size;
}
write_pointer_reuse :: (bytes: *[..] u8, value: *$T) {
// info :: #run cast(*Type_Info_Pointer) type_info(T);
// element_info :: #run (cast(*Type_Info_Pointer) type_info(T)).pointer_to;
element_info_type :: #run (cast(*Type_Info_Pointer) type_info(T)).pointer_to.type;
element_type :: #run get_type((cast(*Type_Info_Pointer) type_info(T)).pointer_to);
element_size :: #run size_of(element_type);
if value.* {
memcpy(bytes.data + bytes.count, value.*, element_size);
value.* = cast(T) bytes.count;
bytes.count += element_size;
#if #run !is_pod(element_type) {
element_pointer := cast(T) (bytes.data + bytes.count - element_size);
#if element_info_type == .STRING {
inline write_string_reuse(bytes, element_pointer);
} else #if element_info_type == .POINTER {
inline write_pointer_reuse(bytes, element_pointer);
} else #if element_info_type == .STRUCT {
inline write_struct_reuse(bytes, element_pointer);
} else #if element_info_type == .ARRAY {
inline write_array_reuse(bytes, element_pointer);
} else #if element_info_type == .VOID {
} else {
#run print("Unhandled serialization type. %\n", element_type);
#run assert(false, "Unhandled serialization type.");
}
}
}
}
write_pointer :: (bytes: *[..] u8, value: $T) {
// info :: #run cast(*Type_Info_Pointer) type_info(T);
// element_info :: #run (cast(*Type_Info_Pointer) type_info(T)).pointer_to;
bool_size :: #run size_of(bool);
element_info_type :: #run (cast(*Type_Info_Pointer) type_info(T)).pointer_to.type;
element_type :: #run get_type((cast(*Type_Info_Pointer) type_info(T)).pointer_to);
element_size :: #run size_of(element_type);
if value {
exists := true;
memcpy(bytes.data + bytes.count, *exists, bool_size);
bytes.count += bool_size;
#if #run is_pod(element_type) {
memcpy(bytes.data + bytes.count, value, element_size);
bytes.count += element_size;
} else #if element_info_type == .STRING {
inline write_string(bytes, value.*);
} else #if element_info_type == .POINTER {
inline write_pointer(bytes, value.*);
} else #if element_info_type == .STRUCT {
inline write_struct(bytes, value.*);
} else #if element_info_type == .ARRAY {
inline write_array(bytes, value.*);
} else #if element_info_type == .VOID {
} else {
#run print("Unhandled serialization type. % %\n", element_type, element_info_type);
#run assert(false, tprint("Unhandled serialization type. % %\n", element_type, element_info_type));
}
} else {
exists := false;
memcpy(bytes.data + bytes.count, *exists, bool_size);
bytes.count += bool_size;
}
}
write_struct_reuse :: (bytes: *[..] u8, value: *$T) {
#insert #run () -> string {
info := cast(*Type_Info_Struct) type_info(T);
builder: String_Builder;
for member: info.members {
member_info := member.type;
member_type := get_type(member_info);
if should_skip_member(member) {
continue;
}
if is_pod(member_type) {
continue;
}
// TODO check place & union
if member_info.type == .STRING {
append(*builder, tprint("inline write_string_reuse(bytes, *value.%);", member.name));
} else if member_info.type == .POINTER {
append(*builder, tprint("inline write_pointer_reuse(bytes, *value.%);", member.name));
} else if member_info.type == .STRUCT {
append(*builder, tprint("inline write_struct_reuse(bytes, *value.%);", member.name));
} else if member_info.type == .ARRAY {
append(*builder, tprint("inline write_array_reuse(bytes, *value.%);", member.name));
} else {
print("Unhandled serialization type. %\n", T);
assert(false, "Unhandled serialization type.");
}
}
return builder_to_string(*builder);
}();
}
write_struct :: (bytes: *[..] u8, value: $T) {
#insert #run () -> string {
info := cast(*Type_Info_Struct) type_info(T);
builder: String_Builder;
visited_byte_offsets: [..] int;
visited_byte_offsets.allocator = temp;
for member: info.members {
member_info := member.type;
member_type := get_type(member_info);
if should_skip_member(member) {
continue;
}
if array_find(visited_byte_offsets, member.offset_in_bytes) {
continue;
}
array_add(*visited_byte_offsets, member.offset_in_bytes);
// TODO check place & union
// TODO continue checking for pod members and memcpy them all at once
if is_pod(member_type) {
t_size := member_info.runtime_size;
append(*builder, tprint("memcpy(bytes.data + bytes.count, *value.%, %);", member.name, t_size));
append(*builder, tprint("bytes.count += %;", t_size));
} else if member_info.type == .STRING {
append(*builder, tprint("inline write_string(bytes, value.%);", member.name));
} else if member_info.type == .POINTER {
append(*builder, tprint("inline write_pointer(bytes, value.%);", member.name));
} else if member_info.type == .STRUCT {
append(*builder, tprint("inline write_struct(bytes, value.%);", member.name));
} else if member_info.type == .ARRAY {
append(*builder, tprint("inline write_array(bytes, value.%);", member.name));
} else {
print("Unhandled serialization type. % %\n", member_info.type, T);
assert(false, "Unhandled serialization type.");
}
}
return builder_to_string(*builder);
}();
}
write_array_reuse :: (bytes: *[..] u8, value: *$T) {
element_info_type :: #run (cast(*Type_Info_Array) type_info(T)).element_type.type;
array_type :: #run (cast(*Type_Info_Array) type_info(T)).array_type;
element_type :: #run get_type((cast(*Type_Info_Array) type_info(T)).element_type);
element_size :: #run size_of(element_type);
#if array_type == .FIXED {
#if #run !is_pod(element_type) {
for i: 0..value.count - 1 {
#if element_info_type == .STRING {
inline write_string_reuse(bytes, value.data + i);
} else #if element_info_type == .POINTER {
inline write_pointer_reuse(bytes, value.data + i);
} else #if element_info_type == .STRUCT {
inline write_struct_reuse(bytes, value.data + i);
} else #if element_info_type == .ARRAY {
inline write_array_reuse(bytes, value.data + i);
} else #if element_info_type == .VOID {
} else {
#run print("Unhandled serialization type. %\n", element_type);
#run assert(false, "Unhandled serialization type.");
}
}
}
} else {
allocate_bytes := value.count * element_size;
memcpy(bytes.data + bytes.count, value.data, allocate_bytes);
value.data = cast(*element_type) bytes.count;
bytes.count += allocate_bytes;
// if this array data isn't pod we have to go through 1 by 1
#if #run !is_pod(element_type) {
start_pointer := cast(*element_type) (bytes.data + cast(int) value.data);
for i: 0..value.count - 1 {
element_pointer: *element_type = start_pointer + i;
#if element_info_type == .STRING {
inline write_string_reuse(bytes, element_pointer);
} else #if element_info_type == .POINTER {
inline write_pointer_reuse(bytes, element_pointer);
} else #if element_info_type == .STRUCT {
inline write_struct_reuse(bytes, element_pointer);
} else #if element_info_type == .ARRAY {
inline write_array_reuse(bytes, element_pointer);
} else #if element_info_type == .VOID {
} else {
#run print("Unhandled serialization type. %\n", element_type);
#run assert(false, "Unhandled serialization type.");
}
}
}
}
}
write_array :: (bytes: *[..] u8, value: $T) {
// info :: #run cast(*Type_Info_Array) type_info(T);
// element_info :: #run (cast(*Type_Info_Array) type_info(T)).element_type;
element_info_type :: #run (cast(*Type_Info_Array) type_info(T)).element_type.type;
element_type :: #run get_type((cast(*Type_Info_Array) type_info(T)).element_type);
element_size :: #run size_of(element_type);
count_size :: #run size_of(count_type);
count := cast(count_type) value.count;
#if #run is_pod(element_type) {
allocate_size := value.count * element_size;
memcpy(bytes.data + bytes.count, *count, count_size);
memcpy(bytes.data + bytes.count + count_size, value.data, allocate_size);
bytes.count += count_size + allocate_size;
} else {
if (cast(*Type_Info_Array) type_info(T)).array_type != .FIXED {
memcpy(bytes.data + bytes.count, *count, count_size);
bytes.count += count_size;
}
for element: value {
#if #run element_info_type == .STRING {
inline write_string(bytes, element);
} else #if #run element_info_type == .POINTER {
inline write_pointer(bytes, element);
} else #if #run element_info_type == .STRUCT {
inline write_struct(bytes, element);
} else #if #run element_info_type == .ARRAY {
inline write_array(bytes, element);
} else #if #run element_info_type == .VOID {
} else {
#run print("Unhandled serialization type. %\n", element_type);
#run assert(false, "Unhandled serialization type.");
}
}
}
}
should_skip_member :: (member: Type_Info_Struct_Member) -> bool {
member_info := member.type;
member_type := get_type(member_info);
skip := !!(member.flags & .CONSTANT);
skip = skip || (member.flags & .USING && !member.name);
skip = skip || (member_info.type == .VOID || member_info.type == .PROCEDURE);
for note: member.notes {
skip = skip || equal_nocase(note, "serializer(skip)");
}
return skip;
}
get_type :: (info: *Type_Info) -> Type {
return (cast(*Type) *info).*;
}
get_write_size :: (value: $T) -> int {
// info :: #run type_info(T);
info_type :: #run type_info(T).type;
#if #run is_pod(T) {
return size_of(T);
} else #if info_type == .STRING {
return size_of(count_type) + value.count;
} else #if info_type == .POINTER {
if value {
return size_of(bool) + get_write_size(value.*);
} else {
return size_of(bool);
}
} else #if info_type == .STRUCT {
#insert #run () -> string {
info := type_info(T);
struct_info := cast(*Type_Info_Struct) info;
builder: String_Builder;
written: bool;
visited_byte_offsets: [..] int;
for member: struct_info.members {
if should_skip_member(member) {
continue;
}
if array_find(visited_byte_offsets, member.offset_in_bytes) {
continue;
}
array_add(*visited_byte_offsets, member.offset_in_bytes);
if !written {
written = true;
append(*builder, tprint("return get_write_size(value.%)", member.name));
} else {
append(*builder, tprint(" + get_write_size(value.%)", member.name));
}
}
append(*builder, ";");
return builder_to_string(*builder);
}();
} else #if info_type == .ARRAY {
// array_info :: #run cast(*Type_Info_Array) info;
array_info_array_type :: #run (cast(*Type_Info_Array) type_info(T)).array_type;
// element_info :: #run (cast(*Type_Info_Array) info).element_type;
element_type :: #run get_type((cast(*Type_Info_Array) type_info(T)).element_type);
element_size :: #run size_of(element_type);
#if #run is_pod(element_type) {
return size_of(count_type) + value.count * element_size;
} else #if #run array_info_array_type == .FIXED {
total_size := 0;
for element: value {
total_size += get_write_size(element);
}
return total_size;
} else {
total_size := size_of(count_type);
for element: value {
total_size += get_write_size(element);
}
return total_size;
}
} else #if info_type == .VOID {
return 0;
} else {
#run print("Unhandled serialization type. % %\n", info_type, element_type);
#run assert(false, tprint("Unhandled serialization type. % %\n", info_type, element_type));
}
}
get_write_size_reuse :: (value: $T) -> int {
#if #run is_pod(T) {
return size_of(T);
} else {
return size_of(T) + get_data_write_size_reuse(value);
}
}
// this is just the allocated size of memory
get_data_write_size_reuse :: (value: $T) -> int {
// info :: #run type_info(T);
info_type :: #run type_info(T).type;
#if #run is_pod(T) {
return 0;
} else #if info_type == .STRING {
return value.count;
} else #if info_type == .POINTER {
return get_write_size_reuse(value.*);
} else #if info_type == .STRUCT {
#insert #run () -> string {
info := type_info(T);
struct_info := cast(*Type_Info_Struct) info;
builder: String_Builder;
written: bool;
for member: struct_info.members {
member_info := member.type;
member_type := get_type(member_info);
if should_skip_member(member) {
continue;
}
if is_pod(member_type) {
continue;
}
if !written {
written = true;
append(*builder, tprint("return get_data_write_size_reuse(value.%)", member.name));
} else {
append(*builder, tprint(" + get_data_write_size_reuse(value.%)", member.name));
}
}
if !written {
append(*builder, "return 0");
}
append(*builder, ";");
return builder_to_string(*builder);