forked from ashishzero/Kano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resolver.cpp
2588 lines (2133 loc) · 81.3 KB
/
Resolver.cpp
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 "Kr/KrBasic.h"
#include "CodeNode.h"
#include "Parser.h"
#include "Interp.h"
#include "Resolver.h"
//
//
//
int Write(String_Builder *builder, Code_Type *type) {
switch (type->kind) {
case CODE_TYPE_NULL: return Write(builder, "void");
case CODE_TYPE_CHARACTER: return Write(builder, "byte");
case CODE_TYPE_INTEGER: return Write(builder, "int");
case CODE_TYPE_REAL: return Write(builder, "float");
case CODE_TYPE_BOOL: return Write(builder, "bool");
case CODE_TYPE_POINTER: {
int count = Write(builder, "*");
return count + Write(builder, ((Code_Type_Pointer *)type)->base_type);
}
case CODE_TYPE_PROCEDURE: {
auto proc = (Code_Type_Procedure *)type;
int count = Write(builder, "proc (");
for (int64_t index = 0; index < proc->argument_count; ++index) {
count += Write(builder, proc->arguments[index]);
if (index < proc->argument_count - 1)
count += Write(builder, ", ");
}
count += Write(builder, ")");
if (proc->return_type) {
count += Write(builder, " -> ");
count += Write(builder, proc->return_type);
}
return count;
}
case CODE_TYPE_STRUCT: {
auto strt = (Code_Type_Struct *)type;
return Write(builder, strt->name.data);
}
case CODE_TYPE_ARRAY_VIEW: {
auto arr = (Code_Type_Array_View *)type;
int count = Write(builder, "[] ");
return Write(builder, arr->element_type);
}
case CODE_TYPE_STATIC_ARRAY: {
auto arr = (Code_Type_Static_Array *)type;
int count = WriteFormatted(builder, "[%] ", arr->element_count);
return Write(builder, arr->element_type);
}
}
Unreachable();
return 0;
}
//
//
//
static Unary_Operator_Kind token_to_unary_operator(Token_Kind kind)
{
switch (kind)
{
case TOKEN_KIND_PLUS:
return UNARY_OPERATOR_PLUS;
case TOKEN_KIND_MINUS:
return UNARY_OPERATOR_MINUS;
case TOKEN_KIND_BITWISE_NOT:
return UNARY_OPERATOR_BITWISE_NOT;
case TOKEN_KIND_LOGICAL_NOT:
return UNARY_OPERATOR_LOGICAL_NOT;
case TOKEN_KIND_ASTERISK:
return UNARY_OPERATOR_POINTER_TO;
case TOKEN_KIND_DEREFERENCE:
return UNARY_OPERATOR_DEREFERENCE;
NoDefaultCase();
}
Unreachable();
return _UNARY_OPERATOR_COUNT;
}
static Binary_Operator_Kind token_to_binary_operator(Token_Kind kind)
{
switch (kind)
{
case TOKEN_KIND_PLUS:
return BINARY_OPERATOR_ADDITION;
case TOKEN_KIND_MINUS:
return BINARY_OPERATOR_SUBTRACTION;
case TOKEN_KIND_ASTERISK:
return BINARY_OPERATOR_MULTIPLICATION;
case TOKEN_KIND_DIVISION:
return BINARY_OPERATOR_DIVISION;
case TOKEN_KIND_REMAINDER:
return BINARY_OPERATOR_REMAINDER;
case TOKEN_KIND_BITWISE_SHIFT_RIGHT:
return BINARY_OPERATOR_BITWISE_SHIFT_RIGHT;
case TOKEN_KIND_BITWISE_SHIFT_LEFT:
return BINARY_OPERATOR_BITWISE_SHIFT_LEFT;
case TOKEN_KIND_BITWISE_AND:
return BINARY_OPERATOR_BITWISE_AND;
case TOKEN_KIND_BITWISE_XOR:
return BINARY_OPERATOR_BITWISE_XOR;
case TOKEN_KIND_BITWISE_OR:
return BINARY_OPERATOR_BITWISE_OR;
case TOKEN_KIND_RELATIONAL_GREATER:
return BINARY_OPERATOR_RELATIONAL_GREATER;
case TOKEN_KIND_RELATIONAL_LESS:
return BINARY_OPERATOR_RELATIONAL_LESS;
case TOKEN_KIND_RELATIONAL_GREATER_EQUAL:
return BINARY_OPERATOR_RELATIONAL_GREATER_EQUAL;
case TOKEN_KIND_RELATIONAL_LESS_EQUAL:
return BINARY_OPERATOR_RELATIONAL_LESS_EQUAL;
case TOKEN_KIND_COMPARE_EQUAL:
return BINARY_OPERATOR_COMPARE_EQUAL;
case TOKEN_KIND_COMPARE_NOT_EQUAL:
return BINARY_OPERATOR_COMPARE_NOT_EQUAL;
case TOKEN_KIND_COMPOUND_PLUS:
return BINARY_OPERATOR_COMPOUND_ADDITION;
case TOKEN_KIND_COMPOUND_MINUS:
return BINARY_OPERATOR_COMPOUND_SUBTRACTION;
case TOKEN_KIND_COMPOUND_MULTIPLY:
return BINARY_OPERATOR_COMPOUND_MULTIPLICATION;
case TOKEN_KIND_COMPOUND_DIVIDE:
return BINARY_OPERATOR_COMPOUND_DIVISION;
case TOKEN_KIND_COMPOUND_REMAINDER:
return BINARY_OPERATOR_COMPOUND_REMAINDER;
case TOKEN_KIND_COMPOUND_BITWISE_SHIFT_RIGHT:
return BINARY_OPERATOR_COMPOUND_BITWISE_SHIFT_RIGHT;
case TOKEN_KIND_COMPOUND_BITWISE_SHIFT_LEFT:
return BINARY_OPERATOR_COMPOUND_BITWISE_SHIFT_LEFT;
case TOKEN_KIND_COMPOUND_BITWISE_AND:
return BINARY_OPERATOR_COMPOUND_BITWISE_AND;
case TOKEN_KIND_COMPOUND_BITWISE_XOR:
return BINARY_OPERATOR_COMPOUND_BITWISE_XOR;
case TOKEN_KIND_COMPOUND_BITWISE_OR:
return BINARY_OPERATOR_COMPOUND_BITWISE_OR;
case TOKEN_KIND_LOGICAL_AND:
return BINARY_OPERATOR_LOGICAL_AND;
case TOKEN_KIND_LOGICAL_OR:
return BINARY_OPERATOR_LOGICAL_OR;
NoDefaultCase();
}
Unreachable();
return _BINARY_OPERATOR_COUNT;
}
static inline uint32_t next_power2(uint32_t n)
{
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static void symbol_table_put(Symbol_Table *table, Symbol *sym)
{
table->map.Put(sym->name, sym);
}
static const Symbol *symbol_table_find(Symbol_Table *root_table, String name, bool recursive = true)
{
if (recursive)
{
for (auto table = root_table; table; table = table->parent)
{
auto symbol = table->map.Find(name);
if (symbol)
return *symbol;
}
return nullptr;
}
auto symbol = root_table->map.Find(name);
return symbol ? *symbol : nullptr;
}
template <typename T, uint32_t N> struct Bucket_Array {
struct Bucket {
T data[N] = {};
Bucket *next = nullptr;
};
Bucket first;
Bucket *last;
uint32_t index;
Bucket_Array() {
last = &first;
index = 0;
}
T *add() {
if (index == N) {
Bucket *buk = new Bucket();
index = 0;
last->next = buk;
last = buk;
}
return &last->data[index++];
}
void add(T d) {
auto dst = add();
*dst = d;
}
constexpr uint32_t bucket_size() {
return N;
}
};
struct Code_Type_Resolver
{
Symbol_Table symbols;
uint32_t virtual_address[2] = {0, 0};
Symbol_Address::Kind address_kind = Symbol_Address::CODE;
int error_count = 0;
String_Builder *error = nullptr;
Array<Code_Type *> return_stack;
uint64_t loop = 0;
Bucket_Array<Symbol, 64> symbols_allocator;
Bucket_Array<Unary_Operator, 8> unary_operators[_UNARY_OPERATOR_COUNT];
Bucket_Array<Binary_Operator, 8> binary_operators[_BINARY_OPERATOR_COUNT];
};
static Code_Type_Resolver_On_Error ResolverOnError;
void code_type_resolver_register_error_proc(Code_Type_Resolver_On_Error proc)
{
ResolverOnError = proc;
}
template <typename ...Args>
static void report_error(Code_Type_Resolver *resolver, Syntax_Node *node, const char *format, Args... args) {
if (resolver->error) {
int line = (int)node->location.start_row;
int column = (int)node->location.start_column;
WriteFormatted(resolver->error, "ERROR:%,% : ", line, column);
WriteFormatted(resolver->error, format, args...);
}
resolver->error_count += 1;
if (ResolverOnError) {
ResolverOnError(resolver);
}
}
static bool code_type_are_same(Code_Type *a, Code_Type *b, bool recurse_pointer_type = true);
static Code_Node_Type_Cast *code_type_cast(Code_Node *node, Code_Type *to_type, bool explicit_cast = false)
{
bool cast_success = false;
bool implicity_casted = true;
switch (to_type->kind)
{
case CODE_TYPE_CHARACTER: {
auto from_type = node->type->kind;
cast_success = (from_type == CODE_TYPE_BOOL);
if (!cast_success && explicit_cast)
{
cast_success = (from_type == CODE_TYPE_REAL || from_type == CODE_TYPE_INTEGER);
}
}
break;
case CODE_TYPE_INTEGER: {
auto from_type = node->type->kind;
cast_success = (from_type == CODE_TYPE_BOOL || from_type == CODE_TYPE_CHARACTER);
if (!cast_success && explicit_cast)
{
cast_success = (from_type == CODE_TYPE_REAL);
}
}
break;
case CODE_TYPE_REAL: {
auto from_type = node->type->kind;
cast_success = (from_type == CODE_TYPE_INTEGER || from_type == CODE_TYPE_CHARACTER);
if (!cast_success && explicit_cast)
{
cast_success = (from_type == CODE_TYPE_BOOL);
}
}
break;
case CODE_TYPE_BOOL: {
auto from_type = node->type->kind;
cast_success = (from_type == CODE_TYPE_CHARACTER || from_type == CODE_TYPE_INTEGER || from_type == CODE_TYPE_REAL);
}
break;
case CODE_TYPE_POINTER: {
auto from_type = node->type;
if (from_type->kind == CODE_TYPE_POINTER)
{
auto to_ptr = (Code_Type_Pointer *)to_type;
auto from_ptr = (Code_Type_Pointer *)from_type;
cast_success = to_ptr->base_type->kind == CODE_TYPE_NULL || from_ptr->base_type->kind == CODE_TYPE_NULL;
}
}
break;
case CODE_TYPE_ARRAY_VIEW: {
auto from_type = node->type;
if (from_type->kind == CODE_TYPE_STATIC_ARRAY)
{
auto to_view = (Code_Type_Array_View *)to_type;
auto from_arr = (Code_Type_Static_Array *)from_type;
cast_success = code_type_are_same(to_view->element_type, from_arr->element_type);
}
}
break;
}
if (!cast_success && explicit_cast)
{
implicity_casted = false;
auto from_type = node->type->kind;
cast_success = (to_type->kind == CODE_TYPE_POINTER && from_type == CODE_TYPE_POINTER) ||
(to_type->kind == CODE_TYPE_PROCEDURE && from_type == CODE_TYPE_PROCEDURE) ||
(to_type->kind == CODE_TYPE_ARRAY_VIEW && from_type == CODE_TYPE_STATIC_ARRAY);
}
if (cast_success)
{
Code_Node_Expression *expression = nullptr;
if (node->kind != CODE_NODE_EXPRESSION)
{
expression = new Code_Node_Expression;
expression->flags = node->flags;
expression->type = node->type;
expression->child = node;
}
else
{
expression = (Code_Node_Expression *)node;
}
auto cast = new Code_Node_Type_Cast;
cast->child = expression;
cast->type = to_type;
cast->implicit = implicity_casted;
return cast;
}
return nullptr;
}
static bool code_type_are_same(Code_Type *a, Code_Type *b, bool recurse_pointer_type)
{
Assert(a && b);
if (a == b)
return true;
if (a->kind == b->kind && a->runtime_size == b->runtime_size && a->alignment == b->alignment)
{
switch (a->kind)
{
case CODE_TYPE_POINTER: {
if (recurse_pointer_type)
{
auto a_ptr = (Code_Type_Pointer *)a;
auto b_ptr = (Code_Type_Pointer *)b;
return code_type_are_same(a_ptr->base_type, b_ptr->base_type, recurse_pointer_type);
}
return true;
}
break;
case CODE_TYPE_PROCEDURE: {
auto a_proc = (Code_Type_Procedure *)a;
auto b_proc = (Code_Type_Procedure *)b;
if (a_proc->return_type && b_proc->return_type)
{
if (!code_type_are_same(a_proc->return_type, b_proc->return_type, recurse_pointer_type))
return false;
}
else if (!a_proc->return_type && b_proc->return_type)
return false;
else if (!b_proc->return_type && a_proc->return_type)
return false;
if (a_proc->argument_count != b_proc->argument_count)
return false;
auto a_args = a_proc->arguments;
auto b_args = b_proc->arguments;
auto arg_count = a_proc->argument_count;
for (int64_t arg_index = 0; arg_index < arg_count; ++arg_index)
{
if (!code_type_are_same(a_args[arg_index], b_args[arg_index], recurse_pointer_type))
return false;
}
return true;
}
break;
case CODE_TYPE_STRUCT: {
auto a_struct = (Code_Type_Struct *)a;
auto b_struct = (Code_Type_Struct *)b;
return a_struct->id == b_struct->id;
}
break;
}
return true;
}
return false;
}
static Code_Node_Literal *code_resolve_literal(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Literal *root);
static Code_Node_Address *code_resolve_identifier(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Identifier *root);
static Code_Node * code_resolve_expression(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node *root);
static Code_Node_Unary_Operator *code_resolve_unary_operator(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Unary_Operator *root);
static Code_Node * code_resolve_binary_operator(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Binary_Operator *root);
static Code_Node_Expression * code_resolve_root_expression(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Expression *root);
static Code_Node_Assignment * code_resolve_assignment(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Assignment *root);
static Code_Type * code_resolve_type(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Type *root, int depth = 0);
static Code_Node_Assignment * code_resolve_declaration(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Declaration *root, Code_Type **out_type = nullptr);
static Code_Node_Statement * code_resolve_statement(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Statement *root);
static Code_Node_Block * code_resolve_block(Code_Type_Resolver *resolver, Symbol_Table *parent_symbols, int64_t procedure_source_row, Syntax_Node_Block *root);
//
//
//
static Code_Node_Literal *code_resolve_literal(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Literal *root)
{
auto node = new Code_Node_Literal;
node->flags = SYMBOL_BIT_CONST_EXPR;
switch (root->value.kind)
{
case Literal::BYTE: {
auto symbol = symbol_table_find(&resolver->symbols, "byte");
Assert(symbol->flags & SYMBOL_BIT_TYPE);
node->type = symbol->type;
node->data.integer.value = root->value.data.integer;
}
break;
case Literal::INTEGER: {
auto symbol = symbol_table_find(&resolver->symbols, "int");
Assert(symbol->flags & SYMBOL_BIT_TYPE);
node->type = symbol->type;
node->data.integer.value = root->value.data.integer;
}
break;
case Literal::REAL: {
auto symbol = symbol_table_find(&resolver->symbols, "float");
Assert(symbol->flags & SYMBOL_BIT_TYPE);
node->type = symbol->type;
node->data.real.value = root->value.data.real;
}
break;
case Literal::STRING: {
auto symbol = symbol_table_find(&resolver->symbols, "string");
Assert(symbol->flags & SYMBOL_BIT_TYPE);
node->type = symbol->type;
node->data.string.value = root->value.data.string;
}
break;
case Literal::BOOL: {
auto symbol = symbol_table_find(&resolver->symbols, "bool");
Assert(symbol->flags & SYMBOL_BIT_TYPE);
node->type = symbol->type;
node->data.boolean.value = root->value.data.boolean;
}
break;
case Literal::NULL_POINTER: {
auto symbol = symbol_table_find(&resolver->symbols, "*void");
Assert(symbol->flags & SYMBOL_BIT_TYPE);
node->type = symbol->type;
}
break;
NoDefaultCase();
}
return node;
}
static Code_Node_Address *code_resolve_identifier(Code_Type_Resolver *resolver, Symbol_Table *symbols,
Syntax_Node_Identifier *root)
{
auto symbol = symbol_table_find(symbols, root->name);
if (symbol)
{
auto address = new Code_Node_Address;
address->address = &symbol->address;
address->flags = symbol->flags;
if (!(symbol->flags & SYMBOL_BIT_CONSTANT))
address->flags |= SYMBOL_BIT_LVALUE;
address->type = symbol->type;
return address;
}
report_error(resolver, root, "Identifier not found: %", root->name);
return nullptr;
}
static Code_Node_Break *code_resolve_break(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Break *root)
{
auto node = new Code_Node_Break;
if (!resolver->loop)
{
report_error(resolver, root, "Invalid break statement, break statement are only allowed with loops(for, while, do)");
}
return node;
}
static Code_Node_Continue *code_resolve_continue(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Continue *root)
{
auto node = new Code_Node_Continue;
if (!resolver->loop)
{
report_error(resolver, root, "Invalid continue statement, break statement are only allowed with loops(for, while, do)");
}
return node;
}
static Code_Node_Return *code_resolve_return(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Return *root)
{
auto node = new Code_Node_Return;
if (root->expression)
{
node->expression = code_resolve_expression(resolver, symbols, root->expression);
node->flags = node->expression->flags;
node->type = node->expression->type;
}
if (resolver->return_stack.count)
{
auto return_type = resolver->return_stack.Last();
if (return_type)
{
if (node->type)
{
if (!code_type_are_same(return_type, node->type)) {
auto cast = code_type_cast(node->expression, return_type);
if (cast)
{
node->expression = cast;
node->type = cast->type;
}
else {
report_error(resolver, root,
"Type mismatch, procedure return type is %, but got expression of type %",
return_type, node->type);
}
}
}
else
{
report_error(resolver, root, "Expected return of type %, but return is empty", return_type);
}
}
else if (node->type)
{
report_error(resolver, root, "Procedure does not return anything but return with type % is given", node->type);
}
}
else
{
Unreachable();
}
return node;
}
static Code_Node_Procedure_Call *code_resolve_procedure_call(Code_Type_Resolver *resolver, Symbol_Table *symbols,
Syntax_Node_Procedure_Call *root)
{
auto procedure = code_resolve_root_expression(resolver, symbols, root->procedure);
if (procedure->type->kind == CODE_TYPE_PROCEDURE)
{
auto proc = (Code_Type_Procedure *)procedure->type;
if (proc->argument_count == root->parameter_count && !proc->is_variadic)
{
auto node = new Code_Node_Procedure_Call;
node->procedure_type = proc;
node->procedure = procedure;
node->type = proc->return_type;
node->parameter_count = root->parameter_count;
node->parameters = new Code_Node_Expression *[node->parameter_count];
auto stack_top = resolver->virtual_address[Symbol_Address::STACK];
node->stack_top = stack_top;
if (proc->return_type)
{
resolver->virtual_address[Symbol_Address::STACK] += proc->return_type->runtime_size;
}
uint32_t param_index = 0;
for (auto param = root->parameters; param; param = param->next, ++param_index)
{
auto code_param = code_resolve_root_expression(resolver, symbols, param->expression);
if (!code_param->type)
{
report_error(resolver, param,
"Type mismatch, expected argument of type % but got void", proc->arguments[param_index]);
}
if (!code_type_are_same(proc->arguments[param_index], code_param->type))
{
auto cast = code_type_cast(code_param->child, proc->arguments[param_index]);
if (cast)
{
code_param->child = cast;
code_param->type = cast->type;
}
else
{
report_error(resolver, param, "On procedure: '%', expected type '%' but got '%' on % parameter",
proc->name, proc->arguments[param_index], code_param->type, param_index + 1);
}
}
resolver->virtual_address[Symbol_Address::STACK] += proc->arguments[param_index]->runtime_size;
node->parameters[param_index] = code_param;
}
resolver->virtual_address[Symbol_Address::STACK] = stack_top;
return node;
}
else if (proc->is_variadic && root->parameter_count >= proc->argument_count - 1)
{
auto node = new Code_Node_Procedure_Call;
node->procedure_type = proc;
node->procedure = procedure;
node->type = proc->return_type;
node->parameter_count = proc->argument_count;
node->parameters = new Code_Node_Expression *[node->parameter_count];
auto stack_top = resolver->virtual_address[Symbol_Address::STACK];
node->stack_top = stack_top;
if (proc->return_type)
{
resolver->virtual_address[Symbol_Address::STACK] += proc->return_type->runtime_size;
}
uint32_t param_index = 0;
auto param = root->parameters;
for (; param_index < proc->argument_count - 1; param = param->next, ++param_index)
{
auto code_param = code_resolve_root_expression(resolver, symbols, param->expression);
if (!code_param->type)
{
report_error(resolver, param,
"Type mismatch, expected argument of type % but got void", proc->arguments[param_index]);
}
if (!code_type_are_same(proc->arguments[param_index], code_param->type))
{
auto cast = code_type_cast(code_param->child, proc->arguments[param_index]);
if (cast)
{
code_param->child = cast;
code_param->type = cast->type;
}
else
{
report_error(resolver, param,
"Type mismatch, expected argument of type % but got %",
proc->arguments[param_index], code_param->type);
}
}
resolver->virtual_address[Symbol_Address::STACK] += proc->arguments[param_index]->runtime_size;
node->parameters[param_index] = code_param;
}
resolver->virtual_address[Symbol_Address::STACK] = stack_top;
if (proc->return_type)
{
resolver->virtual_address[Symbol_Address::STACK] += proc->return_type->runtime_size;
}
Code_Node *child = nullptr;
if (root->parameter_count >= proc->argument_count)
{
auto address = new Code_Node_Address;
address->type = symbol_table_find(&resolver->symbols, "*void")->type;
address->subscript = nullptr;
address->offset = stack_top;
auto pointer_to = new Code_Node_Unary_Operator;
pointer_to->type = address->type;
pointer_to->child = address;
pointer_to->op_kind = UNARY_OPERATOR_POINTER_TO;
child = pointer_to;
auto va_arg_count = root->parameter_count - proc->argument_count + 1;
node->variadic_count = va_arg_count;
node->variadics = new Code_Node_Expression *[va_arg_count];
int64_t index = 0;
for (; param; param = param->next, ++index)
{
Assert(index < va_arg_count);
auto code_param = code_resolve_root_expression(resolver, symbols, param->expression);
if (!code_param->type)
{
report_error(resolver, param,
"Type mismatch, expected argument of type % but got void", proc->arguments[param_index]);
}
if (code_param->child->type->kind == CODE_TYPE_CHARACTER)
{
auto int_type = symbol_table_find(&resolver->symbols, "int")->type;
auto cast = code_type_cast(code_param->child, int_type);
code_param->child = cast;
code_param->type = int_type;
}
resolver->virtual_address[Symbol_Address::STACK] += code_param->type->runtime_size;
node->variadics[index] = code_param;
}
}
else
{
auto null_ptr = new Code_Node_Literal;
null_ptr->type = symbol_table_find(&resolver->symbols, "*void")->type;
null_ptr->data.pointer.value = 0;
child = null_ptr;
}
resolver->virtual_address[Symbol_Address::STACK] = stack_top;
auto va_arg = new Code_Node_Expression;
va_arg->type = child->type;
va_arg->child = child;
node->parameters[node->parameter_count - 1] = va_arg;
return node;
}
else
{
report_error(resolver, root, "Mismatch number of arguments, expected % but % arguments",
proc->argument_count, root->parameter_count);
}
}
report_error(resolver, root, "Invalid procedure call");
return nullptr;
}
static Code_Node_Address *code_resolve_subscript(Code_Type_Resolver *resolver, Symbol_Table *symbols,
Syntax_Node_Subscript *root)
{
auto expression = code_resolve_root_expression(resolver, symbols, root->expression);
auto subscript = code_resolve_root_expression(resolver, symbols, root->subscript);
bool expr_type_is_string = false;
if (expression->type->kind == CODE_TYPE_STRUCT)
{
auto strt = (Code_Type_Struct *)expression->type;
expr_type_is_string = (strt->name == "string");
}
if (expression->type->kind == CODE_TYPE_ARRAY_VIEW ||
expression->type->kind == CODE_TYPE_STATIC_ARRAY ||
expr_type_is_string)
{
if (subscript->type->kind == CODE_TYPE_INTEGER || subscript->type->kind == CODE_TYPE_CHARACTER)
{
auto node = new Code_Node_Subscript;
node->expression = expression;
node->subscript = subscript;
node->flags = expression->flags | SYMBOL_BIT_LVALUE;
if (expression->type->kind == CODE_TYPE_ARRAY_VIEW)
{
auto type = (Code_Type_Array_View *)expression->type;
node->type = type->element_type;
}
else if (expression->type->kind == CODE_TYPE_STATIC_ARRAY)
{
auto type = (Code_Type_Static_Array *)expression->type;
node->type = type->element_type;
}
else if (expression->type->kind == CODE_TYPE_STRUCT)
{
Assert(expr_type_is_string);
node->type = symbol_table_find(&resolver->symbols, "byte", false)->type;
}
auto address = new Code_Node_Address;
address->type = node->type;
address->flags = node->flags;
address->subscript = node;
return address;
}
else
{
report_error(resolver, root->subscript, "Invalid expression for subscript");
}
}
else
{
report_error(resolver, root->expression, "Invalid subscript. Expression does not support subscript");
}
return nullptr;
}
static Code_Node_Type_Cast *code_resolve_type_cast(Code_Type_Resolver *resolver, Symbol_Table *symbols,
Syntax_Node_Type_Cast *root)
{
auto expression = code_resolve_root_expression(resolver, symbols, root->expression);
auto type = code_resolve_type(resolver, symbols, root->type);
auto cast = code_type_cast(expression, type, true);
if (!cast)
{
report_error(resolver, root, "Type cast from % to % is not valid", expression->type, type);
}
cast->flags |= expression->flags;
return cast;
}
static Code_Node_Literal *code_resolve_size_of(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node_Size_Of *root)
{
auto type = code_resolve_type(resolver, symbols, root->type);
auto node = new Code_Node_Literal;
node->type = symbol_table_find(&resolver->symbols, "int")->type;
node->data.integer.value = type->runtime_size;
return node;
}
static Code_Node_Block *code_resolve_procedure(Code_Type_Resolver *resolver, Syntax_Node_Procedure *proc, Code_Type_Procedure **type)
{
auto proc_type = new Code_Type_Procedure;
proc_type->argument_count = proc->argument_count;
proc_type->arguments = new Code_Type *[proc_type->argument_count];
if (proc->return_type)
{
proc_type->return_type = code_resolve_type(resolver, &resolver->symbols, proc->return_type);
}
auto proc_symbols = new Symbol_Table;
proc_symbols->parent = &resolver->symbols;
auto stack_top = resolver->virtual_address[Symbol_Address::STACK];
auto address_kind = resolver->address_kind;
if (proc_type->return_type)
resolver->virtual_address[Symbol_Address::STACK] = proc_type->return_type->runtime_size;
else
resolver->virtual_address[Symbol_Address::STACK] = 0;
resolver->address_kind = Symbol_Address::STACK;
auto last_index = proc_type->argument_count - 1;
uint64_t arg_index = 0;
for (auto arg = proc->arguments; arg; arg = arg->next, ++arg_index)
{
auto assign = code_resolve_declaration(resolver, proc_symbols, arg->declaration,
&proc_type->arguments[arg_index]);
Assert(assign == nullptr);
auto decl_type = arg->declaration->type;
if (arg_index == last_index)
{
proc_type->is_variadic = (decl_type->id == Syntax_Node_Type::VARIADIC_ARGUMENT);
}
else if (decl_type->id == Syntax_Node_Type::VARIADIC_ARGUMENT)
{
report_error(resolver, arg->declaration, "Variadic argument is only valid as the last parameter");
}
}
resolver->return_stack.Add(proc_type->return_type);
auto procedure_body = code_resolve_block(resolver, proc_symbols, (int64_t)proc->location.start_row, proc->body);
resolver->return_stack.count -= 1;
resolver->virtual_address[Symbol_Address::STACK] = stack_top;
resolver->address_kind = address_kind;
*type = proc_type;
return procedure_body;
}
static Code_Node_Literal *code_resolve_procedure_literal(Code_Type_Resolver *resolver, Syntax_Node_Procedure *proc)
{
Code_Type_Procedure *type = nullptr;
auto block = code_resolve_procedure(resolver, proc, &type);
auto node = new Code_Node_Literal;
node->type = type;
node->flags |= (SYMBOL_BIT_CONST_EXPR | SYMBOL_BIT_CONSTANT);
node->data.procedure.block = block;
return node;
}
static Code_Node *code_resolve_expression(Code_Type_Resolver *resolver, Symbol_Table *symbols, Syntax_Node *root)
{
switch (root->kind)
{
case SYNTAX_NODE_LITERAL:
return code_resolve_literal(resolver, symbols, (Syntax_Node_Literal *)root);
case SYNTAX_NODE_TYPE_CAST:
return code_resolve_type_cast(resolver, symbols, (Syntax_Node_Type_Cast *)root);
case SYNTAX_NODE_IDENTIFIER:
return code_resolve_identifier(resolver, symbols, (Syntax_Node_Identifier *)root);
case SYNTAX_NODE_UNARY_OPERATOR:
return code_resolve_unary_operator(resolver, symbols, (Syntax_Node_Unary_Operator *)root);
case SYNTAX_NODE_BINARY_OPERATOR:
return code_resolve_binary_operator(resolver, symbols, (Syntax_Node_Binary_Operator *)root);
case SYNTAX_NODE_ASSIGNMENT: