-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjcc.c
18768 lines (16184 loc) · 756 KB
/
jcc.c
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 <raylib.h>
#include <stdarg.h>
#include <dlfcn.h>
#include <limits.h>
#include <unistd.h>
#include "basic.h"
#include "stb_ds.h"
#include "stb_sprintf.h"
#include "pool.h"
#include "arena.h"
#include "lexer.c"
#include "preload.c"
#define PIPE_STAGES \
X(NONE) \
X(PARSE) \
X(TYPECHECK) \
X(IR) \
#define JOB_STATES \
X(READY) \
X(WAIT) \
X(ERROR) \
#define ASTKINDS \
X(ifstatement) \
X(switchstatement) \
X(casestatement) \
X(whilestatement) \
X(forstatement) \
X(breakstatement) \
X(continuestatement) \
X(returnstatement) \
X(usingstatement) \
X(statement) \
X(push_context) \
X(block) \
X(import_directive) \
X(run_directive) \
X(structdecl) \
X(uniondecl) \
X(vardecl) \
X(procdecl) \
X(paramdecl) \
X(retdecl) \
X(expr_list) \
X(member_list) \
X(expr) \
X(param) \
X(atom) \
X(array_literal) \
X(struct_literal) \
X(call) \
X(proctype) \
#define OPERATOR_PREC_TABLE \
/* indexing */\
X((Token)'.', 10)\
X((Token)'[', 10)\
/* multiplicative */\
X((Token)'*', 9)\
X((Token)'/', 9)\
X((Token)'%', 9)\
/* additive */\
X((Token)'+', 8)\
X((Token)'-', 8)\
/* bitwise */\
X((Token)'^', 7)\
X((Token)'&', 7)\
X((Token)'|', 7)\
X(TOKEN_LSHIFT, 6)\
X(TOKEN_RSHIFT, 6)\
/* comparison */\
X((Token)'<', 5)\
X((Token)'>', 5)\
X(TOKEN_LESSEQUAL, 5)\
X(TOKEN_GREATEQUAL, 5)\
X(TOKEN_EXCLAMEQUAL, 5)\
X(TOKEN_EQUALEQUAL, 5)\
/* logical */\
X(TOKEN_AND, 4)\
X(TOKEN_OR, 4)\
#define VALUEKINDS \
X(NIL) \
X(BOOL) \
X(CHAR) \
X(INT) \
X(UINT) \
X(FLOAT) \
X(DFLOAT) \
X(TYPE) \
X(TYPEINFO) \
X(STRING) \
X(ARRAY) \
X(STRUCT) \
X(PARAM) \
X(TOKEN) \
X(POINTER) \
X(PROC) \
#define TYPEKINDS \
X(VOID) \
X(BOOL) \
X(CHAR) \
X(S8) \
X(U8) \
X(S16) \
X(U16) \
X(S32) \
X(U32) \
X(S64) \
X(U64) \
X(INT) \
X(FLOAT) \
X(F32) \
X(F64) \
X(TYPE) \
X(STRING) \
X(ARRAY) \
X(DYNAMIC_ARRAY) \
X(ARRAY_VIEW) \
X(POINTER) \
X(STRUCT) \
X(UNION) \
X(ENUM) \
X(PROC) \
X(WILDCARD) \
#define MESSAGEKINDS \
X(ERROR) \
X(WARNING) \
X(INFO) \
#define IR_INT_BINOPS \
X(ADD, +) \
X(SUB, -) \
X(MUL, *) \
X(DIV, /) \
X(MOD, %) \
X(AND, &) \
X(OR, |) \
X(XOR, ^) \
X(LSHIFT, <<) \
X(RSHIFT, >>) \
X(EQ, ==) \
X(NE, !=) \
X(LE, <=) \
X(GT, >) \
#define IR_INT_UNOPS \
X(NOT, ~) \
X(NEG, -) \
#define IR_FLOAT_BINOPS \
X(FADD, +) \
X(FSUB, -) \
X(FMUL, *) \
X(FDIV, /) \
#define IR_FLOAT_CMPOPS \
X(FEQ, ==) \
X(FNE, !=) \
X(FLE, <=) \
X(FGT, >) \
#define IR_FLOAT_UNOPS \
X(FNEG, -) \
#define IROPCODES \
X(NOOP, _) \
IR_INT_BINOPS \
IR_INT_UNOPS \
IR_FLOAT_BINOPS \
IR_FLOAT_CMPOPS \
IR_FLOAT_UNOPS \
X(IF, _) \
X(IFZ, _) \
X(JMP, _) \
X(CALL, _) \
X(RET, _) \
X(LABEL, _) \
X(LOAD, _) \
X(STOR, _) \
X(LOADF, _) \
X(STORF, _) \
X(CALCPTROFFSET, _) \
X(ADDRVAR, _) \
X(GETVAR, _) \
X(SETVAR, _) \
X(SETARG, _) \
X(SETRET, _) \
X(GETARG, _) \
X(GETRET, _) \
X(GETVARF, _) \
X(SETVARF, _) \
X(SETARGF, _) \
X(SETRETF, _) \
X(GETARGF, _) \
X(GETRETF, _) \
X(ITOF, _) \
X(FTOB, _) \
X(ITOB, _) \
X(FTOI, _) \
X(ITOI, _) \
X(FTOF, _) \
X(GETCONTEXTARG, _) \
X(SETCONTEXTARG, _) \
X(HINT_BEGIN_FOREIGN_CALL, _) \
X(HINT_END_FOREIGN_CALL, _) \
X(HINT_BEGIN_CALL, _) \
X(HINT_END_CALL, _) \
X(HINT_BEGIN_PASS_NON_SCALAR, _) \
X(HINT_END_PASS_NON_SCALAR, _) \
#define IRSEGMENTS \
X(LOCAL, "local_segment") \
X(GLOBAL, "global_segment") \
X(BSS, "bss_segment") \
X(TYPE, "type_segment") \
X(STRING, "string_segment") \
X(CODE, "code_segment") \
#define PLATFORMS \
X(X64) \
X(ARM64) \
#define AST_KIND_IS_RECORD(kind) ((bool)(kind == AST_KIND_structdecl || kind == AST_KIND_uniondecl))
#define AST_KIND_IS_AGGREGATE_LITERAL(kind) ((bool)(kind == AST_KIND_array_literal || kind == AST_KIND_struct_literal))
#define TOKEN_TO_TYPEKIND(t) (Typekind)((t-TOKEN_VOID)+TYPE_KIND_VOID)
#define TYPE_KIND_IS_SIGNED_INT(kind) ((bool)(kind == TYPE_KIND_INT || kind == TYPE_KIND_S8 || kind == TYPE_KIND_S16 || kind == TYPE_KIND_S32 || kind == TYPE_KIND_S64))
#define TYPE_KIND_IS_UNSIGNED_INT(kind) ((bool)(kind == TYPE_KIND_U8 || kind == TYPE_KIND_U16 || kind == TYPE_KIND_U32 || kind == TYPE_KIND_U64))
#define TYPE_KIND_IS_NOT_SCALAR(kind) ((bool)(kind >= TYPE_KIND_TYPE && kind != TYPE_KIND_POINTER))
#define TYPE_KIND_IS_SCALAR(kind) ((bool)(kind < TYPE_KIND_TYPE || kind == TYPE_KIND_POINTER))
#define TYPE_KIND_IS_RECORD(kind) ((bool)(kind == TYPE_KIND_STRUCT || kind == TYPE_KIND_UNION))
#define TYPE_KIND_IS_RECORD_OR_ARRAY(kind) ((bool)(TYPE_KIND_IS_RECORD(kind) || kind == TYPE_KIND_ARRAY))
#define TYPE_KIND_IS_FLOAT(kind) ((bool)(kind >= TYPE_KIND_FLOAT && kind <= TYPE_KIND_F64))
#define TYPE_KIND_IS_FLOAT32(kind) ((bool)(kind == TYPE_KIND_FLOAT || kind == TYPE_KIND_F32))
#define TYPE_KIND_IS_INTEGER(kind) ((bool)(kind >= TYPE_KIND_BOOL && kind <= TYPE_KIND_INT))
#define TYPE_KIND_IS_INTEGER_OR_FLOAT(kind) ((bool)(TYPE_KIND_IS_INTEGER(kind) || TYPE_KIND_IS_FLOAT(kind)))
#define TYPE_KIND_IS_ARRAY_LIKE(kind) ((bool)(kind >= TYPE_KIND_ARRAY && kind <= TYPE_KIND_ARRAY_VIEW))
#define TYPE_KIND_IS_VIEW_LIKE(kind) ((bool)(kind == TYPE_KIND_ARRAY_VIEW || kind == TYPE_KIND_STRING))
#define TYPE_IS_VOID_POINTER(t) ((bool)((assert(t), 1) && t->kind == TYPE_KIND_POINTER && (assert(t->pointer.to), 1) && t->pointer.to->kind == TYPE_KIND_VOID))
#define TOKEN_IS_BITWISE_OP(token) ((bool)(token == '|' || token == TOKEN_LSHIFT || token == TOKEN_RSHIFT || token == '&' || token == '^' || token == '~'))
#define IROP_IS_INT_ARITH(op) ((bool)(op >= IROP_ADD && op <= IROP_NEG))
typedef struct Scope_entry* Scope;
typedef int Jobid;
typedef struct Value Value;
typedef struct Sym Sym;
typedef struct Type Type;
typedef struct IRlabel IRlabel;
typedef struct IRinst IRinst;
typedef union IRvalue IRvalue;
typedef struct IRmachine IRmachine;
typedef void (*IR_foreign_proc)(IRmachine *);
typedef struct IRproc IRproc;
typedef struct Job Job;
typedef struct Job_memory Job_memory;
typedef struct Message Message;
typedef struct AST AST;
typedef struct AST_expr_base AST_expr_base;
#define X(x) typedef struct AST_##x AST_##x;
ASTKINDS
#undef X
typedef enum ASTkind {
AST_KIND_INVALID = -1,
#define X(x) AST_KIND_##x,
ASTKINDS
#undef X
AST_KIND_MAX,
} ASTkind;
typedef enum Typecheck_step {
TYPECHECK_STEP_INVALID = -1,
#define X(s) TYPECHECK_STEP_##s,
TYPECHECK_STEPS
#undef X
} Typecheck_step;
typedef enum Pipe_stage {
PIPE_STAGE_INVALID = -1,
#define X(s) PIPE_STAGE_##s,
PIPE_STAGES
#undef X
} Pipe_stage;
typedef enum Job_state {
JOB_STATE_INVALID = -1,
#define X(s) JOB_STATE_##s,
JOB_STATES
#undef X
} Job_state;
typedef enum Valuekind {
VALUE_KIND_INVALID = -1,
#define X(x) VALUE_KIND_##x,
VALUEKINDS
#undef X
} Valuekind;
typedef enum Typekind {
TYPE_KIND_INVALID = -1,
#define X(x) TYPE_KIND_##x,
TYPEKINDS
#undef X
} Typekind;
char *typekind_debug[] = {
#define X(x) #x,
TYPEKINDS
#undef X
};
typedef enum Messagekind {
MESSAGE_KIND_INVALID = -1,
#define X(x) MESSAGE_KIND_##x,
MESSAGEKINDS
#undef X
} Messagekind;
typedef enum IRop {
IROP_INVALID = -1,
#define X(x, _) IROP_##x,
IROPCODES
#undef X
} IRop;
typedef enum IRsegment {
IRSEG_INVALID = -1,
#define X(x,_) IRSEG_##x,
IRSEGMENTS
#undef X
} IRsegment;
typedef enum Platform {
PLATFORM_INVALID = -1,
#define X(x) PLATFORM_##x,
PLATFORMS
#undef X
} Platform;
char *IRop_debug[] = {
#define X(x, _) #x,
IROPCODES
#undef X
};
char *IRsegment_debug[] = {
#define X(x,_) #x,
IRSEGMENTS
#undef X
};
char *ASTkind_debug[] = {
#define X(x) #x,
ASTKINDS
#undef X
};
union IRvalue {
u64 integer;
f32 floating32;
f64 floating64;
};
struct IRlabel {
char *key;
Token keyword;
u64 continue_label;
u64 break_label;
Loc_info loc;
};
#define IRMACHINE_BODY \
X(Arr(u64) pc_stack;) \
X(Arr(int) procid_stack;) \
X(Arr(u64*) jump_table_stack;) \
\
X(u8 *global_segment;) \
X(u8 *bss_segment;) \
X(u8 *local_segment;) \
\
X(u64 global_segment_size;) \
X(u64 bss_segment_size;) \
X(u64 local_segment_size;) \
\
X(u64 iregs[8];) \
X(f32 f32regs[8];) \
X(f64 f64regs[8];) \
\
X(Arr(IRvalue) ports;) \
X(u64 context_pointer;) \
struct IRmachine {
#define X(x) x
IRMACHINE_BODY
#undef X
};
const u64 IR_LOCAL_SEGMENT_BYTES = 1<<16;
struct IRinst {
IRop opcode;
union {
union {
Type *proc_type;
} hint;
struct {
u64 operand_bytes[3];
u64 reg[3];
IRvalue imm;
bool immediate;
bool sign;
} arith;
struct {
u64 cond_reg;
u64 label_id;
} branch;
struct {
u64 reg_dest;
} getcontextarg;
struct {
u64 reg_src;
} setcontextarg;
struct {
u64 id_reg;
u64 id_imm;
char *name;
bool c_call;
bool immediate;
} call;
struct {
bool c_call;
} ret;
struct {
u64 id;
} label;
struct {
u64 reg_dest;
u64 reg_src_ptr;
u64 offset_reg;
u64 stride;
} calcptroffset;
struct {
u64 reg_dest;
u64 reg_src_ptr;
IRvalue imm;
u64 offset_reg;
u64 byte_offset_imm;
u64 bytes;
bool has_immediate_offset;
bool has_indirect_offset;
bool immediate;
} load;
struct {
u64 reg_dest_ptr;
u64 reg_src;
IRvalue imm;
u64 offset_reg;
u64 byte_offset_imm;
u64 bytes;
bool has_immediate_offset;
bool has_indirect_offset;
bool immediate;
} stor;
struct {
IRsegment segment;
u64 offset;
Sym *sym;
u64 reg_dest;
} addrvar;
struct {
IRsegment segment;
u64 offset;
Sym *sym;
u64 reg_dest;
u64 bytes;
} getvar;
struct {
IRsegment segment;
u64 offset;
Sym *sym;
u64 reg_src;
IRvalue imm;
u64 bytes;
bool immediate;
} setvar;
struct {
u64 port;
u64 bytes;
u64 reg_src;
bool c_call;
} setport;
struct {
u64 reg_dest;
u64 bytes;
u64 port;
bool c_call;
} getport;
struct {
u64 to_reg;
u64 from_reg;
u64 to_bytes;
u64 from_bytes;
bool sign;
} typeconv;
};
Loc_info loc;
};
struct IRproc {
int procid;
char *name;
bool is_foreign;
bool is_inline;
bool was_polymorphed;
union {
struct {
IRinst *instructions;
u64 n_instructions;
u64 *jump_table;
u64 local_segment_size;
char *assembly;
};
struct {
IR_foreign_proc foreign_proc;
void *wrapper_dll;
};
};
};
struct Scope_entry {
char *key;
Sym *value;
};
struct Message {
Messagekind kind;
char *text;
Loc_info loc;
};
struct Job_memory {
Arena *scratch;
Pool *value;
Pool *sym;
Pool *type;
#define X(x) Pool ast_##x;
ASTKINDS
#undef X
struct {
bool scratch : 1;
bool value : 1;
bool sym : 1;
bool type : 1;
bool ast : 1;
} active;
};
struct Job {
Jobid id;
Jobid parent_job;
Pipe_stage pipe_stage;
Job_state state;
char *handling_name;
Sym *symbol;
Lexer *lexer;
int parsing_uniondecl;
int parser_encountered_polymorphic_var;
Pool *global_sym_allocator;
Scope *global_scope;
AST *root;
Arr(AST*) tree_pos_stack;
union {
Loc_info non_returning_path_loc;
Loc_info returning_path_loc;
};
/* typechecking */
char *waiting_on_name;
Jobid waiting_on_id;
Type *cur_proc_type;
Arr(Sym*) run_dependencies;
/* procedure polymorphism */
Arr(Type*) save_polymorphic_proc_param_and_return_types;
AST *save_polymorphic_proc_body;
/* struct and union */
Arr(Type*) record_types;
/* blocks */
//bool sharing_scopes;
Arr(Scope) scopes;
/* expressions */
//TODO remove these stacks
Arr(Value*) value_stack;
Arr(Type*) type_stack;
Arr(AST*) expr;
u64 expr_pos;
AST_paramdecl *cur_paramdecl;
AST_retdecl *cur_retdecl;
/* return statement */
Arr(AST_expr_list*) expr_list;
u64 expr_list_pos;
/* code generation */
Arr(AST_statement*) defer_list_stack;
u64 max_local_offset;
Arr(u64) local_offset;
IRlabel* label_table;
Arr(u64) continue_label;
Arr(u64) break_label;
u64 label_alloc;
Arr(IRinst) instructions;
u64 reg_alloc;
u64 float_reg_alloc;
Loc_info cur_loc;
IRmachine interp;
u64 cur_run_local_segment_size;
Arr(Message) messages;
Job_memory allocator;
bool parser_at_top_level : 1;
bool dont_free_allocators : 1;
bool dont_free_ast_allocators : 1;
bool doing_polymorph : 1;
bool typechecker_encountered_wildcard : 1;
};
struct Sym {
char *name;
Loc_info loc;
Jobid declared_by;
int procid;
IRsegment segment;
u64 segment_offset;
Type *type;
//union {
//};
Value *value;
AST *initializer;
AST *polymorphic_proc_ast;
Jobid being_polymorphed_by_jobid;
struct {
Arr(Type**) key;
Arr(int) procid;
u64 n_wildcards;
} polymorph_cache;
bool job_encountered_error : 1;
bool ir_generated : 1;
bool ready_to_run : 1;
bool is_system : 1;
bool is_foreign : 1;
bool is_global : 1;
bool is_argument : 1;
bool is_record_argument : 1;
bool constant : 1;
bool is_polymorphic_procedure : 1;
bool is_polymorphic_var : 1;
bool is_being_used_in_polymorph : 1;
};
struct AST {
ASTkind kind;
u32 weight;
Loc_info loc;
};
struct AST_import_directive {
AST base;
char *path;
bool just_load_the_source;
};
struct AST_run_directive {
AST base;
Type *type_annotation; /* for use in expressions */
Value *value_annotation;
Type **type_annotation_list; /* because callables can return multiple values */
Value **value_annotation_list;
AST_call *call_to_run;
int n_types_returned;
};
struct AST_expr_base {
AST base;
Type *type_annotation;
Value *value_annotation;
};
struct AST_expr {
AST base;
Type *type_annotation;
Value *value_annotation;
union {
u64 dot_offset_annotation;
};
Token token;
AST *left;
AST *right;
};
struct AST_param {
AST base;
Type *type_annotation;
Value *value_annotation;
char *name;
AST *value;
AST_param *next;
int index;
bool has_nested_call : 1;
bool is_vararg : 1;
};
struct AST_call {
AST base;
Type *type_annotation; /* for use in expressions */
Value *value_annotation;
Type **type_annotation_list; /* because callables can return multiple values */
Value **value_annotation_list;
AST *callee;
AST_param *params;
int n_types_returned;
int n_params;
int first_named_param;
bool has_named_params : 1;
bool checked_call : 1;
};
struct AST_array_literal {
AST base;
Type *type_annotation;
Value *value_annotation;
int n_elements;
AST *type;
AST_expr_list *elements;
Arr(AST**) flattened_array_literal;
Arr(u64) dimensions;
};
struct AST_struct_literal {
AST base;
Type *type_annotation;
Value *value_annotation;
int n_members;
char *name;
AST_member_list *members;
};
struct AST_atom {
AST base;
Type *type_annotation;
Value *value_annotation;
Sym *symbol_annotation;
Token token;
union {
s64 integer;
u64 uinteger;
f32 floating;
f64 dfloating;
char character;
char *text;
};
};
struct AST_member_list {
AST base;
char *name;
AST *value;
AST_member_list *next;
};
struct AST_expr_list {
AST base;
AST *expr;
AST_expr_list *next;
};
struct AST_proctype {
AST base;
Type *type_annotation;
Value *value_annotation;
AST_expr_list *params;
AST_expr_list *rets;
int n_params;
int n_rets;
};
#define AST_RECORD_BODY \
AST base; \
AST *next; \
Sym *symbol_annotation; \
char *name; \
AST_paramdecl *params; \
int first_default_param; \
int n_params; \
int n_members; \
int n_using; \
AST *body; \
Type *record_type; \
bool has_defaults : 1; \
bool checked_params : 1; \
bool visited : 1; \
bool is_name_spaced : 1; \
struct AST_structdecl {
AST_RECORD_BODY
};
struct AST_uniondecl {
AST_RECORD_BODY
};
struct AST_vardecl {
AST base;
AST *next;
Sym *symbol_annotation;
char *name;
AST *type;
AST *init;
bool constant : 1;
bool uninitialized : 1;
bool checked_type : 1;
bool checked_init : 1;
};
struct AST_paramdecl {
AST base;
AST_paramdecl *next;
Sym *symbol_annotation;
char *name;
AST *type;
AST *init;
int index;
bool checked_type : 1;
bool checked_init : 1;
bool vararg : 1;
bool is_polymorphic : 1;
bool type_has_wildcard : 1;
};
struct AST_retdecl {
AST base;
AST_retdecl *next;
AST *expr;
int index;
bool checked_expr : 1;
bool type_has_wildcard : 1;
};
struct AST_ifstatement {
AST base;
AST *next;
char *label;
AST *condition;
AST *body;
AST *branch;
};
struct AST_switchstatement {
AST base;
AST *next;
char *label;
AST *expr;
AST *cases;
};
struct AST_casestatement {
AST base;
AST_casestatement *next;
AST *expr;
AST *body;
};
struct AST_whilestatement {
AST base;
AST *next;
char *label;
AST *condition;
AST *body;
};
struct AST_forstatement {
AST base;
AST *next;
char *label;
union {
AST *expr;
AST *begin_range_expr;
};
AST *end_range_expr;
AST *body;
union {
Type *expr_type;
Type *begin_range_type;
};
Type *end_range_type;
Sym *it_symbol;
Sym *it_index_symbol;
Sym *named_it_symbol;
Sym *named_it_index_symbol;
u64 checked_begin_range : 1;
u64 checked_end_range : 1;
u64 by_pointer : 1;
u64 reverse_order : 1;
u64 is_range_for : 1;
u64 visited : 1;
};
struct AST_breakstatement {
AST base;
AST *next;
char *label;
};
struct AST_continuestatement {
AST base;
AST *next;
char *label;
};
struct AST_usingstatement {
AST base;
AST *next;
char *name;
};
struct AST_returnstatement {
AST base;
AST *next;
AST_expr_list *expr_list;
};
struct AST_push_context {
AST base;
AST *next;