-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintro.h
More file actions
2814 lines (2415 loc) · 86.3 KB
/
intro.h
File metadata and controls
2814 lines (2415 loc) · 86.3 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
// MIT License
//
// Copyright (c) 2022 cyman
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef INTRO_H
#define INTRO_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#ifdef __INTRO__
#define_forced I I
#else
#define I(...)
#endif
#ifndef INTRO_CTX
#define INTRO_CTX (&__intro_ctx)
#endif
#define ITYPE(x) (&INTRO_CTX->types[ITYPE_##x])
#ifndef INTRO_API_INLINE
#define INTRO_API_INLINE static inline
#endif
#ifndef INTRO_MAX_ATTRIBUTES
#define INTRO_MAX_ATTRIBUTES 128
#endif
#ifndef __INTRO__
#if __STDC_VERSION__ >= 201112L || (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(_MSC_VER)
#define INTRO_HAVE_UNNAMED_FIELDS 1
#endif
#endif
#if defined(__GNUC__)
#define INTRO_ALIGN(x) __attribute__((aligned(x)))
#elif defined(_MSC_VER)
#define INTRO_ALIGN(x) __declspec(align(x))
#else
#define INTRO_ALIGN(x)
#endif
typedef uint8_t IntroGuiColor [4] I(gui_edit_color);
typedef enum IntroCategory {
INTRO_UNKNOWN = 0x0,
INTRO_U8 = 0x11,
INTRO_U16 = 0x12,
INTRO_U32 = 0x14,
INTRO_U64 = 0x18,
INTRO_S8 = 0x21,
INTRO_S16 = 0x22,
INTRO_S32 = 0x24,
INTRO_S64 = 0x28,
INTRO_F32 = 0x34,
INTRO_F64 = 0x38,
INTRO_F128 = 0x3f,
INTRO_ARRAY = 0x40,
INTRO_POINTER = 0x50,
INTRO_ENUM = 0x60,
INTRO_STRUCT = 0x70,
INTRO_UNION = 0x71,
INTRO_FUNCTION = 0x80,
INTRO_VA_LIST = 0x81,
} IntroCategory;
typedef enum {
INTRO_UNSIGNED = 0x10,
INTRO_SIGNED = 0x20,
INTRO_FLOATING = 0x30,
} IntroCategoryFlags;
typedef struct IntroLocation {
const char * path;
uint32_t offset;
} IntroLocation;
typedef enum IntroFlags {
INTRO_EMBEDDED_DEFINITION = 0x01,
INTRO_CONST = 0x02,
// ? 0x04
INTRO_EXPLICITLY_GENERATED = 0x08,
INTRO_HAS_BODY = 0x10,
INTRO_IS_FLAGS = 0x20,
// reused
INTRO_IS_SEQUENTIAL = INTRO_HAS_BODY,
// persistent
INTRO_PERSISTENT_FLAGS = INTRO_CONST | INTRO_EXPLICITLY_GENERATED,
} IntroFlags;
typedef struct IntroType IntroType I(~gui_edit);
typedef struct {uint32_t offset;} IntroAttributeDataId;
typedef uint32_t IntroAttribute;
typedef struct IntroMember {
const char * name;
IntroType * type;
uint32_t offset;
IntroAttributeDataId attr;
} IntroMember;
typedef struct IntroEnumValue {
const char * name;
int32_t value;
IntroAttributeDataId attr;
} IntroEnumValue;
struct IntroType {
#ifdef INTRO_HAVE_UNNAMED_FIELDS
union {
#endif
union {
void * __data I(~gui_show);
IntroType * of I(when <-category == INTRO_ARRAY || <-category == INTRO_POINTER);
IntroMember * members I(length <-count, when (<-category & 0xf0) == INTRO_STRUCT);
IntroEnumValue * values I(length <-count, when <-category == INTRO_ENUM);
IntroType ** arg_types I(length <-count, when <-category == INTRO_FUNCTION);
} u;
#ifdef INTRO_HAVE_UNNAMED_FIELDS
union {
void * __data;
IntroType * of;
IntroMember * members;
IntroEnumValue * values;
IntroType ** arg_types;
};
};
#endif
IntroType * parent;
const char * name;
uint32_t count;
IntroAttributeDataId attr;
uint32_t size;
uint16_t flags I(imitate IntroFlags);
uint8_t align;
uint8_t category I(imitate IntroCategory);
};
typedef struct IntroFunction {
const char * name;
IntroType * type;
IntroType * return_type;
const char ** arg_names I(length count_args);
IntroType ** arg_types I(length count_args);
IntroLocation location;
uint32_t count_args;
uint16_t flags;
} IntroFunction;
typedef enum IntroAttributeCategory {
INTRO_AT_FLAG = 0,
INTRO_AT_INT,
INTRO_AT_FLOAT,
INTRO_AT_VALUE,
INTRO_AT_MEMBER,
INTRO_AT_TYPE,
INTRO_AT_EXPR,
INTRO_AT_REMOVE,
INTRO_AT_COUNT
} IntroAttributeCategory;
typedef struct IntroAttributeInfo {
const char * name;
uint32_t category;
IntroAttribute id;
} IntroAttributeInfo;
typedef struct IntroAttributeData {
INTRO_ALIGN(16) uint32_t bitset [(INTRO_MAX_ATTRIBUTES+31) / 32];
//uint32_t value_offsets []; // this is here in spirit. C++ doesn't like flexible array members
} IntroAttributeData;
typedef struct IntroAttributeContext {
IntroAttributeInfo * available I(length count_available);
IntroAttributeData * data;
uint32_t count_available;
uint16_t first_flag;
struct IntroBuiltinAttributes {
uint8_t id;
uint8_t bitfield;
uint8_t fallback;
uint8_t length;
uint8_t when;
uint8_t alias;
uint8_t imitate;
uint8_t header;
uint8_t city;
uint8_t cstring;
uint8_t remove;
uint8_t gui_note;
uint8_t gui_name;
uint8_t gui_min;
uint8_t gui_max;
uint8_t gui_format;
uint8_t gui_scale;
uint8_t gui_vector;
uint8_t gui_color;
uint8_t gui_show;
uint8_t gui_edit;
uint8_t gui_edit_color;
uint8_t gui_edit_text;
} builtin;
} IntroAttributeContext;
typedef struct IntroMacro {
const char * name;
const char ** parameters I(length count_parameters);
const char * replace;
IntroLocation location;
uint32_t count_parameters;
} IntroMacro;
typedef struct IntroContext {
IntroType * types I(length count_types);
uint8_t * values I(length size_values);
IntroFunction * functions I(length count_functions);
IntroMacro * macros I(length count_macros);
uint32_t count_types;
uint32_t size_values;
uint32_t count_functions;
uint32_t count_macros;
IntroAttributeContext attr;
const char * version;
} IntroContext;
typedef struct IntroVariant {
void * data;
const IntroType * type;
} IntroVariant;
typedef struct IntroContainer {
const struct IntroContainer * parent;
const IntroType * type;
uint8_t * data;
size_t index;
} IntroContainer;
union IntroRegisterData {
uint64_t ui;
int64_t si;
float sf;
double df;
};
I(apply_to (char *) (cstring))
I(apply_to (void *) (~city))
// TODO: this should happen automatically
I(apply_to (const char *) (cstring))
I(apply_to (const void *) (~city))
I(attribute @global (
id: int,
bitfield: int,
fallback: value(@inherit) @propagate,
length: expr,
when: expr,
alias: value(char *),
imitate: type @propagate,
header: type @propagate,
city: flag @global,
cstring: flag @propagate,
remove: __remove @transient,
))
I(attribute gui_ (
note: value(char *),
name: value(char *),
min: value(@inherit) @propagate,
max: value(@inherit) @propagate,
format: value(char *) @propagate,
scale: float @propagate,
vector: flag @propagate,
color: value(IntroGuiColor) @propagate,
show: flag @global,
edit: flag @global,
edit_color: flag @propagate,
edit_text: flag @propagate,
))
#define intro_var_get(var, T) (assert(var.type == ITYPE(T)), *(T *)var.data)
INTRO_API_INLINE bool
intro_is_scalar(const IntroType * type) {
return (type->category >= INTRO_U8 && type->category <= INTRO_F64);
}
INTRO_API_INLINE bool
intro_is_int(const IntroType * type) {
return (type->category >= INTRO_U8 && type->category <= INTRO_S64);
}
INTRO_API_INLINE bool
intro_is_floating(const IntroType * type) {
return (type->category & 0xf0) == INTRO_FLOATING;
}
INTRO_API_INLINE bool
intro_is_complex(const IntroType * type) {
return (type->category == INTRO_STRUCT
|| type->category == INTRO_UNION
|| type->category == INTRO_ENUM);
}
INTRO_API_INLINE bool
intro_has_members(const IntroType * type) {
return (type->category & 0xf0) == INTRO_STRUCT;
}
INTRO_API_INLINE bool
intro_has_of(const IntroType * type) {
return type->category == INTRO_ARRAY || type->category == INTRO_POINTER;
}
INTRO_API_INLINE const IntroType *
intro_origin(const IntroType * type) {
while (type->parent) {
type = type->parent;
}
return type;
}
#define intro_has_attribute(m, a) intro_has_attribute_x(INTRO_CTX, m->attr, IATTR_##a)
INTRO_API_INLINE bool
intro_has_attribute_x(IntroContext * ctx, IntroAttributeDataId data_id, uint32_t attr_id) {
assert(attr_id < INTRO_MAX_ATTRIBUTES);
IntroAttributeData * data = ctx->attr.data + data_id.offset;
uint32_t bitset_index = attr_id >> 5;
uint32_t bit_index = attr_id & 31;
uint32_t attr_bit = 1 << bit_index;
return (data->bitset[bitset_index] & attr_bit);
}
INTRO_API_INLINE IntroContainer
intro_cntr(void * data, const IntroType * type) {
IntroContainer cntr;
cntr.data = (uint8_t *)data;
cntr.type = type;
cntr.parent = NULL;
cntr.index = 0;
return cntr;
}
INTRO_API_INLINE IntroAttributeDataId
intro_get_attr(IntroContainer cntr) {
if (cntr.parent && intro_has_members(cntr.parent->type)) {
return cntr.parent->type->u.members[cntr.index].attr;
} else {
return cntr.type->attr;
}
}
INTRO_API_INLINE const IntroMember *
intro_get_member(IntroContainer cntr) {
if (intro_has_members(cntr.parent->type)) {
return &cntr.parent->type->u.members[cntr.index];
} else {
return NULL;
}
}
typedef struct {
int indent;
const char * tab;
} IntroPrintOptions;
// ATTRIBUTE INFO
#define intro_attribute_value(m, a, out) intro_attribute_value_x(INTRO_CTX, m->type, m->attr, IATTR_##a, out)
bool intro_attribute_value_x(IntroContext * ctx, const IntroType * type, IntroAttributeDataId data_id, IntroAttribute attr_id, IntroVariant * o_var);
#define intro_attribute_int(m, a, out) intro_attribute_int_x(INTRO_CTX, m->attr, IATTR_##a, out)
bool intro_attribute_int_x(IntroContext * ctx, IntroAttributeDataId data_id, IntroAttribute attr_id, int32_t * o_int);
#define intro_attribute_member(m, a, out) intro_attribute_member_x(INTRO_CTX, m->attr, IATTR_##a, out)
bool intro_attribute_member_x(IntroContext * ctx, IntroAttributeDataId data_id, IntroAttribute attr_id, int32_t * o_int);
#define intro_attribute_float(m, a, out) intro_attribute_float_x(INTRO_CTX, m->attr, IATTR_##a, out)
bool intro_attribute_float_x(IntroContext * ctx, IntroAttributeDataId data_id, IntroAttribute attr_id, float * o_float);
#define intro_attribute_length(container, out) intro_attribute_length_x(INTRO_CTX, container, out)
bool intro_attribute_length_x(IntroContext * ctx, IntroContainer cont, int64_t * o_length);
#define intro_attribute_run_expr(C, A, OUT) intro_attribute_expr_x(INTRO_CTX, C, IATTR_##A, OUT)
bool intro_attribute_expr_x(IntroContext * ctx, IntroContainer cntr, IntroAttribute attr_id, int64_t * o_result);
#define intro_attribute_type(M, A) intro_attribute_type_x(INTRO_CTX, M->attr, IATTR_##A)
const IntroType * intro_attribute_type_x(IntroContext * ctx, IntroAttributeDataId data_id, IntroAttribute attr_id);
// INITIALIZERS
#define intro_set_value(DST, TYPE, A) intro_set_value_x(INTRO_CTX, intro_cntr(DST, TYPE), IATTR_##A)
void intro_set_value_x(IntroContext * ctx, IntroContainer cntr, uint32_t attr_id);
#define intro_fallback(dest, type) intro_set_value_x(INTRO_CTX, intro_cntr(dest, type), INTRO_CTX->attr.builtin.fallback)
// PRINTERS
void intro_sprint_type_name(char * dest, const IntroType * type);
void intro_print_type_name(const IntroType * type);
#define intro_print(DATA, TYPE, OPT) intro_print_x(INTRO_CTX, intro_cntr(DATA, TYPE), OPT)
void intro_print_x(IntroContext * ctx, IntroContainer container, const IntroPrintOptions * opt);
void intro_sprint_json_x(IntroContext * ctx, char * buf, const void * data, const IntroType * type, const IntroPrintOptions * opt);
// CITY IMPLEMENTATION
char * intro_read_file(const char * filename, size_t * o_size);
int intro_dump_file(const char * filename, void * data, size_t data_size);
#define intro_load_city_file(dest, dest_type, filename) intro_load_city_file_x(INTRO_CTX, dest, dest_type, filename)
bool intro_load_city_file_x(IntroContext * ctx, void * dest, const IntroType * dest_type, const char * filename);
#define intro_create_city_file(filename, src, src_type) intro_create_city_file_x(INTRO_CTX, filename, src, src_type)
bool intro_create_city_file_x(IntroContext * ctx, const char * filename, void * src, const IntroType * src_type);
#define intro_create_city(src, s_type, o_size) intro_create_city_x(INTRO_CTX, src, s_type, o_size)
void * intro_create_city_x(IntroContext * ctx, const void * src, const IntroType * s_type, size_t *o_size);
#define intro_load_city(dest, dest_type, data, data_size) intro_load_city_x(INTRO_CTX, dest, dest_type, data, data_size)
int intro_load_city_x(IntroContext * ctx, void * dest, const IntroType * d_type, void * data, size_t data_size);
// DEAR IMGUI (must link with intro_imgui.cpp to use)
#define intro_imgui_edit(data, data_type) intro_imgui_edit_x(INTRO_CTX, intro_cntr(data, data_type), #data)
void intro_imgui_edit_x(IntroContext * ctx, IntroContainer cont, const char * name);
// MISC
IntroContainer intro_push(const IntroContainer * parent, int32_t index);
IntroType * intro_type_with_name_x(IntroContext * ctx, const char * name);
const char * intro_enum_name(const IntroType * type, int value);
int64_t intro_int_value(const void * data, const IntroType * type);
#define intro_member_by_name(t, name) intro_member_by_name_x(t, #name)
const IntroMember * intro_member_by_name_x(const IntroType * type, const char * name);
union IntroRegisterData intro_run_bytecode(const uint8_t * code, const void * data);
#define INTRO_LIB_VERSION 402
///////////////////////////////
// INTROLIB IMPLEMENTATION //
///////////////////////////////
#ifdef INTRO_IMPL
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#if defined(__GNUC__)
#define INTRO_UNUSED __attribute__((unused))
#else
#define INTRO_UNUSED
#endif
#ifndef LENGTH
#define LENGTH(a) (sizeof(a)/sizeof(*(a)))
#endif
#if defined(__x86_64__) && defined(__has_include) && __has_include(<x86intrin.h>)
#include <x86intrin.h>
#define INTRO_POPCNT32 __popcntd
#define INTRO_BSR64 __bsrq
#else
#define INTRO_POPCNT32 intro_popcnt32_x
#define INTRO_BSR64 intro_bsr64_x
// taken from https://github.com/BartMassey/popcount/blob/master/popcount.c
INTRO_API_INLINE int
intro_popcnt32_x(uint32_t x) {
const uint32_t m1 = 0x55555555;
const uint32_t m2 = 0x33333333;
const uint32_t m4 = 0x0f0f0f0f;
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x = (x + (x >> 4)) & m4;
x += x >> 8;
return (x + (x >> 16)) & 0x3f;
}
INTRO_API_INLINE uint64_t
intro_bsr64_x(uint64_t x) {
uint64_t index = 63;
for (; index > 0; index--) {
if ((x & (1 << index))) {
return index;
}
}
return 0;
}
#endif
#ifdef __cplusplus
#define restrict
#endif
const static int MAX_EXPOSED_LENGTH = 64;
static const char * tab = " ";
typedef uint8_t u8;
typedef struct {
int current;
int current_used;
int capacity;
struct {
void * data;
} buckets [256]; // should be enough for anyone
} MemArena;
static void *
arena_alloc(MemArena * arena, int amount) {
if (arena->current_used + amount > arena->capacity) {
if (amount <= arena->capacity) {
arena->current++;
assert(arena->current < (int)LENGTH(arena->buckets));
if (arena->buckets[arena->current].data == NULL) {
arena->buckets[arena->current].data = calloc(1, arena->capacity);
}
} else {
arena->current++;
arena->buckets[arena->current].data = realloc(arena->buckets[arena->current].data, amount);
memset(arena->buckets[arena->current].data, 0, amount);
}
arena->current_used = 0;
}
void * result = (u8 *)arena->buckets[arena->current].data + arena->current_used;
arena->current_used += amount;
arena->current_used += 16 - (arena->current_used & 15);
return result;
}
static MemArena *
new_arena(int capacity) {
MemArena * arena = (MemArena *)calloc(1, sizeof(MemArena));
arena->capacity = capacity;
arena->buckets[0].data = calloc(1, arena->capacity);
return arena;
}
static void INTRO_UNUSED
reset_arena(MemArena * arena) {
for (int i=0; i <= arena->current; i++) {
memset(arena->buckets[i].data, 0, arena->capacity);
}
arena->current = 0;
arena->current_used = 0;
}
static void
free_arena(MemArena * arena) {
for (uint32_t i=0; i < LENGTH(arena->buckets); i++) {
if (arena->buckets[i].data) free(arena->buckets[i].data);
}
free(arena);
}
typedef struct {
size_t cap;
size_t len;
} DynArrayHeader;
#define arr_header(ARR) ((DynArrayHeader *)(ARR) - 1)
#define arr_init(ARR) arr_init_((void **)&(ARR), sizeof(*(ARR)))
#define arr_free(ARR) (free(arr_header(ARR)), ARR = 0)
#define arr_len(ARR) arr_header(ARR)->len
#define arr_grow(ARR) arr_grow_((void **)&(ARR), sizeof(*(ARR)))
#define arr_append(ARR, VAL) (arr_len(ARR)+=1, arr_grow(ARR), (ARR)[arr_header(ARR)->len - 1] = VAL)
#define arr_append_range(ARR, PTR, COUNT) (arr_len(ARR)+=(COUNT), arr_grow(ARR), memcpy(&(ARR)[arr_len(ARR) - (COUNT)], PTR, (COUNT) * sizeof(*(ARR))))
#define arr_alloc_ptr(ARR, COUNT) (arr_len(ARR)+=(COUNT), arr_grow(ARR), (ARR) + arr_len(ARR) - (COUNT))
#define arr_alloc_idx(ARR, COUNT) (arr_len(ARR)+=(COUNT), arr_grow(ARR), arr_len(ARR) - (COUNT))
static void
arr_init_(void ** o_arr, size_t elem_size) {
#define ARR_INIT_CAP 128
void * handle = malloc(elem_size * ARR_INIT_CAP + sizeof(DynArrayHeader));
DynArrayHeader * header = (DynArrayHeader *)handle;
header->cap = ARR_INIT_CAP;
header->len = 0;
*o_arr = (DynArrayHeader *)handle + 1;
#undef ARR_INIT_CAP
}
static void
arr_grow_(void ** o_arr, size_t elem_size) {
bool do_realloc = false;
while (arr_header(*o_arr)->len > arr_header(*o_arr)->cap) {
arr_header(*o_arr)->cap <<= 1;
do_realloc = true;
}
if (do_realloc) {
void * handle = realloc(arr_header(*o_arr), arr_header(*o_arr)->cap * elem_size + sizeof(DynArrayHeader));
*o_arr = (DynArrayHeader *)handle + 1;
}
}
static const uint32_t MURMUR32_SEED = 0x67FD5513;
// borrowed and adapted from from gingerBill's gb.h
static uint32_t
gb_murmur32_seed(void const *data, size_t len, uint32_t seed) {
const uint32_t c1 = 0xcc9e2d51;
const uint32_t c2 = 0x1b873593;
const uint32_t r1 = 15;
const uint32_t r2 = 13;
const uint32_t m = 5;
const uint32_t n = 0xe6546b64;
size_t count_blocks = len / 4;
uint32_t hash = seed, k1 = 0;
const uint32_t * blocks = (const uint32_t *)data;
const uint8_t * tail = (const uint8_t *)data + count_blocks * 4;
for (size_t i=0; i < count_blocks; i++) {
uint32_t k = blocks[i];
k *= c1;
k = (k << r1) | (k >> (32 - r1));
k *= c2;
hash ^= k;
hash = ((hash << r2) | (hash >> (32 - r2))) * m + n;
}
switch (len & 3) {
case 3:
k1 ^= tail[2] << 16;
// FALLTHROUGH
case 2:
k1 ^= tail[1] << 8;
// FALLTHROUGH
case 1:
k1 ^= tail[0];
k1 *= c1;
k1 = (k1 << r1) | (k1 >> (32 - r1));
k1 *= c2;
hash ^= k1;
}
hash ^= len;
hash ^= (hash >> 16);
hash *= 0x85ebca6b;
hash ^= (hash >> 13);
hash *= 0xc2b2ae35;
hash ^= (hash >> 16);
return hash;
}
static const uint32_t TABLE_INVALID_INDEX = UINT32_MAX;
static const size_t TABLE_INVALID_VALUE = SIZE_MAX;
typedef struct {
uint32_t hash;
uint32_t index;
} HashLookup;
typedef struct {
const void * key_data;
size_t key_size;
size_t value;
} HashEntry;
typedef struct {
HashLookup * hashes;
HashEntry * entries;
MemArena * arena;
} HashTable;
static void
table_clear_lookups_(HashTable * tbl, size_t start, size_t end) {
for (size_t i=start; i < end; i++) {
tbl->hashes[i].index = TABLE_INVALID_INDEX;
}
}
static HashTable *
new_table(size_t cap) {
assert((size_t)1 << INTRO_BSR64(cap) == cap); // must be power of 2
cap <<= 1;
HashTable * tbl = (HashTable *)malloc(sizeof(*tbl));
arr_init(tbl->hashes);
arr_init(tbl->entries);
(void) arr_alloc_idx(tbl->hashes, cap);
tbl->arena = new_arena(1024);
table_clear_lookups_(tbl, 0, cap);
return tbl;
}
#define table_count(TBL) arr_header((TBL)->entries)->len
#define free_table(TBL) (free_table_(TBL), TBL = 0)
static void
free_table_(HashTable * tbl) {
arr_free(tbl->hashes);
arr_free(tbl->entries);
free_arena(tbl->arena);
free(tbl);
}
static void
table_insert_lookup_(HashTable * tbl, HashLookup * p_lookup) {
uint32_t index = p_lookup->hash & (arr_len(tbl->hashes) - 1);
HashLookup * p_slot = &tbl->hashes[index];
while (p_slot->index != TABLE_INVALID_INDEX) {
if (p_slot->index == p_lookup->index) {
return;
}
index += 1;
index &= arr_len(tbl->hashes) - 1;
p_slot = &tbl->hashes[index];
}
*p_slot = *p_lookup;
p_lookup->index = TABLE_INVALID_INDEX;
}
static void
table_grow_lookup_(HashTable * tbl) {
size_t old_len = arr_len(tbl->hashes);
arr_header(tbl->hashes)->len <<= 1;
arr_grow(tbl->hashes);
table_clear_lookups_(tbl, old_len, arr_len(tbl->hashes));
for (size_t i=0; i < old_len; i++) {
HashLookup * p_lookup = &tbl->hashes[i];
if (p_lookup->index != TABLE_INVALID_INDEX) {
table_insert_lookup_(tbl, p_lookup);
}
}
}
static uint32_t
table_get(const HashTable * tbl, HashEntry * p_entry) {
uint32_t hash = gb_murmur32_seed(p_entry->key_data, p_entry->key_size, MURMUR32_SEED);
uint32_t lookup_index_mask = arr_len(tbl->hashes) - 1;
uint32_t lookup_index = hash & lookup_index_mask;
while (1) {
HashLookup lookup = tbl->hashes[lookup_index];
if (lookup.index == TABLE_INVALID_INDEX) {
p_entry->value = TABLE_INVALID_VALUE;
return TABLE_INVALID_INDEX;
}
if (lookup.hash == hash) {
HashEntry s_entry = tbl->entries[lookup.index];
if (
s_entry.key_size == p_entry->key_size
&& 0==memcmp(s_entry.key_data, p_entry->key_data, s_entry.key_size)
)
{
p_entry->value = s_entry.value;
return lookup.index;
}
}
lookup_index += 1;
lookup_index &= lookup_index_mask;
}
}
static void
table_set(HashTable * tbl, HashEntry entry) {
size_t new_value = entry.value;
uint32_t entry_index = table_get(tbl, &entry);
if (entry_index != TABLE_INVALID_INDEX) {
tbl->entries[entry_index].value = new_value;
return;
}
void * stored_key = arena_alloc(tbl->arena, entry.key_size);
memcpy(stored_key, entry.key_data, entry.key_size);
entry.key_data = stored_key;
entry.value = new_value;
entry_index = arr_len(tbl->entries);
arr_append(tbl->entries, entry);
if (arr_len(tbl->entries) > arr_len(tbl->hashes) / 2) {
table_grow_lookup_(tbl);
}
HashLookup lookup;
lookup.hash = gb_murmur32_seed(entry.key_data, entry.key_size, MURMUR32_SEED);
lookup.index = entry_index;
table_insert_lookup_(tbl, &lookup);
}
const char *
intro_enum_name(const IntroType * type, int value) {
if ((type->flags & INTRO_IS_SEQUENTIAL)) {
return type->u.values[value].name;
} else {
for (uint32_t i=0; i < type->count; i++) {
if (type->u.values[i].value == value) {
return type->u.values[i].name;
}
}
}
return NULL;
}
int64_t
intro_int_value(const void * data, const IntroType * type) {
int64_t result;
switch(type->category) {
case INTRO_U8:
result = *(uint8_t *)data;
break;
case INTRO_U16:
result = *(uint16_t *)data;
break;
case INTRO_U32:
result = *(uint32_t *)data;
break;
case INTRO_U64: {
uint64_t u_result = *(uint64_t *)data;
result = (u_result <= INT64_MAX)? u_result : INT64_MAX;
break;
}
case INTRO_S8:
result = *(int8_t *)data;
break;
case INTRO_S16:
result = *(int16_t *)data;
break;
case INTRO_S32:
result = *(int32_t *)data;
break;
case INTRO_S64:
result = *(int64_t *)data;
break;
default:
result = 0;
break;
}
return result;
}
static bool
get_attribute_value_offset(IntroContext * ctx, IntroAttributeDataId data_id, uint32_t attr_id, uint32_t * out) {
assert(attr_id < INTRO_MAX_ATTRIBUTES);
IntroAttributeData * data = &ctx->attr.data[data_id.offset];
uint32_t bitset_index = attr_id >> 5;
uint32_t bit_index = attr_id & 31;
uint32_t attr_bit = 1 << bit_index;
uint32_t pop_count_mask = attr_bit - 1;
bool has = data->bitset[bitset_index] & attr_bit;
if (!has) {
return false;
}
int pop = 0;
for (uint32_t i=0; i < bitset_index; i++) {
pop += INTRO_POPCNT32(data->bitset[i]);
}
pop += INTRO_POPCNT32(data->bitset[bitset_index] & pop_count_mask);
uint32_t * value_offsets = (uint32_t *)(data + 1);
*out = value_offsets[pop];
return true;
}
#define ASSERT_ATTR_CATEGORY(A_CTGRY) assert(ctx->attr.available[attr_id].category == A_CTGRY)
bool
intro_attribute_value_x(IntroContext * ctx, const IntroType * type, IntroAttributeDataId data_id, IntroAttribute attr_id, IntroVariant * o_var) {
ASSERT_ATTR_CATEGORY(INTRO_AT_VALUE);
uint32_t value_offset;
bool has = get_attribute_value_offset(ctx, data_id, attr_id, &value_offset);
if (!has) return false;
o_var->data = ctx->values + value_offset;
uint32_t id = ctx->attr.available[attr_id].id;
o_var->type = (id != 0)? &ctx->types[id] : type;
if (o_var->type->category == INTRO_POINTER) {
uintptr_t val = *(uintptr_t *)o_var->data;
o_var->data = ctx->values + val;
}
return true;
}
bool
intro_attribute_int_x(IntroContext * ctx, IntroAttributeDataId data_id, IntroAttribute attr_id, int32_t * o_int) {
ASSERT_ATTR_CATEGORY(INTRO_AT_INT);
uint32_t value_offset;
bool has = get_attribute_value_offset(ctx, data_id, attr_id, &value_offset);
if (has) {
memcpy(o_int, &value_offset, sizeof(*o_int));
return true;
} else {
return false;
}
}
bool
intro_attribute_member_x(IntroContext * ctx, IntroAttributeDataId data_id, IntroAttribute attr_id, int32_t * o_int) {
ASSERT_ATTR_CATEGORY(INTRO_AT_MEMBER);
uint32_t value_offset;
bool has = get_attribute_value_offset(ctx, data_id, attr_id, &value_offset);
if (has) {
memcpy(o_int, &value_offset, sizeof(*o_int));
return true;
} else {
return false;
}
}
bool
intro_attribute_float_x(IntroContext * ctx, IntroAttributeDataId data_id, IntroAttribute attr_id, float * o_float) {
ASSERT_ATTR_CATEGORY(INTRO_AT_FLOAT);
uint32_t value_offset;
bool has = get_attribute_value_offset(ctx, data_id, attr_id, &value_offset);
if (has) {
memcpy(o_float, &value_offset, sizeof(*o_float));
return true;
} else {
return false;
}
}
const IntroType *
intro_attribute_type_x(IntroContext * ctx, IntroAttributeDataId data_id, IntroAttribute attr_id) {
ASSERT_ATTR_CATEGORY(INTRO_AT_TYPE);
uint32_t type_id;
bool has = get_attribute_value_offset(ctx, data_id, attr_id, &type_id);
if (has) {
return &ctx->types[type_id];
} else {
return NULL;
}
}
bool
intro_attribute_length_x(IntroContext * ctx, IntroContainer cntr, int64_t * o_length) {
assert(cntr.parent && intro_has_members(cntr.parent->type));
return intro_attribute_expr_x(ctx, cntr, ctx->attr.builtin.length, o_length);
}
static const void *
intro_expr_data(IntroContext * ctx, const IntroContainer * pcntr) {
const IntroType * header = intro_attribute_type_x(ctx, intro_get_attr(*pcntr), ctx->attr.builtin.header);
if (header) {
u8 * ptr = *(u8 **)pcntr->data;
if (ptr) {
return ptr - header->size;
} else {
return NULL;
}
} else {
if (pcntr->parent) {
pcntr = pcntr->parent;
}
while (pcntr->parent && (pcntr->type->flags & INTRO_EMBEDDED_DEFINITION)) {
pcntr = pcntr->parent;
}
return pcntr->data;
}
}