-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.h
1677 lines (1410 loc) · 54.7 KB
/
compiler.h
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
#ifndef ROSEBUDCOMPILER_H
#define ROSEBUDCOMPILER_H
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define S_EQ(str1, str2) (str1 && str2 && (strcmp(str1, str2) == 0))
#define STACK_PUSH_SIZE 4
#define C_STACK_ALIGNMENT 16
#define C_ALIGN(size) \
(size % C_STACK_ALIGNMENT) \
? size + (C_STACK_ALIGNMENT - (size % C_STACK_ALIGNMENT)) \
: size
#define PATH_MAX 4096
#define FAIL_ERR(msg) assert(0 == 1 && msg)
struct pos {
int line;
int col;
const char *filename;
};
#define NUMERIC_CASE \
case '0': \
case '1': \
case '2': \
case '3': \
case '4': \
case '5': \
case '6': \
case '7': \
case '8': \
case '9'
#define OPERATOR_CASE_EXCLUDING_DIVISION \
case '+': \
case '-': \
case '*': \
case '>': \
case '<': \
case '^': \
case '%': \
case '!': \
case '=': \
case '~': \
case '|': \
case '&': \
case '(': \
case '[': \
case ',': \
case '.': \
case '?'
#define SYMBOL_CASE \
case '{': \
case '}': \
case ':': \
case ';': \
case '#': \
case '\\': \
case ')': \
case ']'
enum { LEXICAL_ANALYSIS_ALL_OK, LEXICAL_ANALYSIS_INPUT_ERROR };
enum {
TOKEN_TYPE_IDENTIFIER,
TOKEN_TYPE_KEYWORD,
TOKEN_TYPE_OPERATOR,
TOKEN_TYPE_SYMBOL,
TOKEN_TYPE_NUMBER,
TOKEN_TYPE_STRING,
TOKEN_TYPE_COMMENT,
TOKEN_TYPE_NEWLINE,
};
enum {
NUMBER_TYPE_NORMAL,
NUMBER_TYPE_LONG,
NUMBER_TYPE_FLOAT,
NUMBER_TYPE_DOUBLE,
};
enum {
TOKEN_FLAG_IS_CUSTOM_OPERATOR = 0b00000001,
};
struct token {
int type;
int flags;
struct pos pos;
union {
char cval;
const char *sval;
unsigned int inum;
unsigned long lnum;
unsigned long long llnum;
void *any;
};
struct token_number {
int type;
} num;
// True if there is a whitespace between the current token and next token
bool whitespace;
// (5+10+20)
const char *between_brackets;
// TEST(test hello, 50)
const char *between_args;
};
struct lex_process;
typedef char (*LEX_PROCESS_NEXT_CHAR)(struct lex_process *process);
typedef char (*LEX_PROCESS_PEEK_CHAR)(struct lex_process *process);
typedef void (*LEX_PROCESS_PUSH_CHAR)(struct lex_process *process, char c);
struct lex_process_functions {
LEX_PROCESS_NEXT_CHAR next_char;
LEX_PROCESS_PEEK_CHAR peek_char;
LEX_PROCESS_PUSH_CHAR push_char;
};
struct lex_process {
struct pos pos;
struct vector *token_vec;
struct compile_process *compiler;
/**
* ((50))
*/
int current_expression_count;
struct buffer *parenthesis_buffer;
// TEST(hello test, 50)
struct buffer *arg_string_buffer;
struct lex_process_functions *function;
// This will be private data that the lexer does not understand
// but the person using the lexer does understand.
void *private;
};
enum {
COMPILER_FILE_COMPILED_OK,
COMPILER_FAILED_WITH_ERRORS,
};
enum {
COMPILE_PROCESS_EXEC_NASM = 0b00000001,
COMPILE_PROCESS_EXPORT_AS_OBJECT = 0b00000010,
};
struct scope {
int flags;
// void*
struct vector *entities;
// total number of bytes this scope occupies. Aligned to 16 bytes.
size_t size;
// null if this is the global scope
struct scope *parent;
};
struct codegen_entry_point {
// ID of the entry point
int id;
};
struct codegen_exit_point {
// ID of the exit point
int id;
};
struct string_table_element {
// string that the element is related to
const char *str;
// assembly label for the string in the .rodata section
const char label[50];
};
struct code_generator {
struct generator_switch_stmt {
struct generator_switch_stmt_entity {
int id;
} current;
// vector of generator_switch_stmt_entity
struct vector *switches;
} _switch;
// vector of struct string_table_element*
struct vector *string_table;
// vector of struct codegen_entry_point*
struct vector *entry_points;
// vector of struct codegen_exit_point*
struct vector *exit_points;
// vector of const char*
struct vector *custom_data_section;
// vector of struct response*
struct vector *responses;
};
enum {
SYMBOL_TYPE_NODE,
SYMBOL_TYPE_NATIVE_FUNCTION,
SYMBOL_TYPE_UNKNOWN,
};
struct symbol {
const char *name;
int type;
void *data;
};
enum {
PREPROCESSOR_DEFINITION_STANDARD,
PREPROCESSOR_DEFINITION_MACRO_FUNCTION,
PREPROCESSOR_DEFINITION_NATIVE_CALLBACK,
PREPROCESSOR_DEFINITION_TYPEDEF,
};
enum { PREPROCESS_ALL_OK };
struct compile_process;
struct preprocessor;
struct preprocessor_definition;
struct preprocessor_included_file;
struct preprocessor_function_arg {
// vector of struct token*
struct vector *tokens;
};
struct preprocessor_function_args {
// vector of struct preprocessor_function_arg
struct vector *args;
};
typedef int (*PREPROCESSOR_DEFINITION_NATIVE_CALL_EVALUATE)(
struct preprocessor_definition *definition,
struct preprocessor_function_args *args);
typedef struct vector *(*PREPROCESSOR_DEFINITION_NATIVE_CALL_VALUE)(
struct preprocessor_definition *definition,
struct preprocessor_function_args *args);
typedef void (*PREPROCESSOR_STATIC_INCLUDE_HANDLER_POST_CREATION)(
struct preprocessor *preprocessor,
struct preprocessor_included_file *included_file);
struct preprocessor_definition {
int type; // .i.e standard or function like macro
// name of the macro
const char *name;
union {
struct standard_preprocessor_definition {
// vector of struct token*
struct vector *value;
// vector of const char* (.i.e. arguments like ABC(a, b, c))
struct vector *args;
} standard;
struct typedef_preprocessor_definition {
struct vector *value;
} _typedef;
struct native_callback_preprocessor_definition {
PREPROCESSOR_DEFINITION_NATIVE_CALL_EVALUATE evaluate;
PREPROCESSOR_DEFINITION_NATIVE_CALL_VALUE value;
} native;
};
struct preprocessor *preprocessor;
};
struct preprocessor_included_file {
char filename[PATH_MAX];
};
typedef void (*PREPROCESSOR_STATIC_INCLUDE_HANDLER_POST_CREATION)(
struct preprocessor *preprocessor,
struct preprocessor_included_file *included_file);
PREPROCESSOR_STATIC_INCLUDE_HANDLER_POST_CREATION
preprocessor_static_include_handler_for(const char *filename);
struct generator;
struct native_function;
struct node;
struct resolver_entity;
struct datatype;
struct generator_entity_address {
bool is_stack;
long offset;
const char *address;
const char *base_address;
};
#define GENERATOR_BEGIN_EXPRESSION(gen)
#define GENERATOR_END_EXPRESSION(gen) (gen)->end_expression(gen)
typedef void (*ASM_PUSH_PROTOTYPE)(const char *ins, ...);
typedef void (*NATIVE_FUNCTION_CALL)(struct generator *generator,
struct native_function *func,
struct vector *args);
typedef void (*GENERATOR_GENERATE_EXPRESSION)(struct generator *generator,
struct node *node, int flags);
typedef void (*GENERATOR_ENTITY_ADDRESS)(
struct generator *generator, struct resolver_entity *entity,
struct generator_entity_address *address_out);
typedef void (*GENERATOR_END_EXPRESSION)(struct generator *generator);
typedef void (*GENERATOR_FUNCTION_RETURN)(struct datatype *dtype,
const char *fmt, ...);
struct generator {
ASM_PUSH_PROTOTYPE asm_push;
GENERATOR_GENERATE_EXPRESSION generate_expression;
GENERATOR_END_EXPRESSION end_expression;
GENERATOR_ENTITY_ADDRESS entity_address;
GENERATOR_FUNCTION_RETURN ret;
struct compile_process *compiler;
void *private;
};
struct native_function_callbacks {
NATIVE_FUNCTION_CALL call;
};
struct native_function {
const char *name;
struct native_function_callbacks callbacks;
};
struct symbol *
native_create_function(struct compile_process *compiler, const char *name,
struct native_function_callbacks *callbacks);
struct native_function *native_function_get(struct compile_process *compiler,
const char *name);
struct preprocessor {
// vector of struct_definition*
struct vector *defs;
// vector of struct preprocessor_node*
struct vector *exp_vec;
struct expressionable *expressionable;
struct compile_process *compiler;
// vector of struct preprocessor_included_file*
struct vector *includes;
};
struct resolver_process;
struct compile_process {
// The flags in regard on how this file should be compiled
int flags;
struct pos pos;
struct compile_process_input_file {
FILE *fp;
const char *abs_path;
} cfile;
// untampered vector of tokens from lexical analysis for preprocessing
struct vector *token_vec_original;
// The vector of tokens from lexical analysis
struct vector *token_vec;
struct vector *node_vec;
struct vector *node_tree_vec;
FILE *ofile;
struct {
struct scope *root;
struct scope *current;
} scope;
struct {
// current active symbol table
struct vector *table;
// all symbol tables
struct vector *tables;
} symbols;
// pointer to code generator
struct code_generator *generator;
// pointer to resolver process
struct resolver_process *resolver;
// vector of const char* (include directories)
struct vector *include_dirs;
// pointer to preprocessor
struct preprocessor *preprocessor;
};
enum { PARSE_ALL_OK, PARSE_GENERAL_ERROR };
enum { VALIDATION_ALL_OK, VALIDATION_GENERAL_ERROR };
enum { CODEGEN_ALL_OK, CODEGEN_GENERAL_ERROR };
enum {
NODE_TYPE_EXPRESSION,
NODE_TYPE_EXPRESSION_PARENTHESIS,
NODE_TYPE_NUMBER,
NODE_TYPE_IDENTIFIER,
NODE_TYPE_STRING,
NODE_TYPE_VARIABLE,
NODE_TYPE_VARIABLE_LIST,
NODE_TYPE_FUNCTION,
NODE_TYPE_BODY,
NODE_TYPE_STATEMENT_RETURN,
NODE_TYPE_STATEMENT_IF,
NODE_TYPE_STATEMENT_ELSE,
NODE_TYPE_STATEMENT_WHILE,
NODE_TYPE_STATEMENT_DO_WHILE,
NODE_TYPE_STATEMENT_FOR,
NODE_TYPE_STATEMENT_BREAK,
NODE_TYPE_STATEMENT_CONTINUE,
NODE_TYPE_STATEMENT_SWITCH,
NODE_TYPE_STATEMENT_CASE,
NODE_TYPE_STATEMENT_DEFAULT,
NODE_TYPE_STATEMENT_GOTO,
NODE_TYPE_UNARY,
NODE_TYPE_TENARY,
NODE_TYPE_LABEL,
NODE_TYPE_STRUCT,
NODE_TYPE_UNION,
NODE_TYPE_BRACKET,
NODE_TYPE_CAST,
NODE_TYPE_BLANK
};
enum {
NODE_FLAG_INSIDE_EXPRESSION = 0b00000001,
NODE_FLAG_IS_FORWARD_DECLARATION = 0b00000010,
NODE_FLAG_HAS_VARIABLE_COMBINED = 0b00000100,
};
struct array_brackets {
// vector of struct node*
struct vector *n_brackets;
};
struct node;
struct datatype {
int flags;
// .i.e int, char, float, double, etc
int type;
// .e.g long int a; int is the secondary datatype
struct datatype *secondary;
// .i.e int, char, float, double, etc
const char *type_str;
// size of the datatype
size_t size;
// .e.g int **pointer = 0;
int pointer_depth;
union {
struct node *struct_node;
struct node *union_node;
};
struct array {
struct array_brackets *brackets;
// size of the array in bytes (DATATYPE_SIZE * array.size)
size_t size;
} array;
};
struct stack_frame_data {
// data type that the stack frame is for
struct datatype dtype;
};
struct stack_frame_element {
// stack frame flags
int flags;
// type of the stack frame element
int type;
// name of the stack frame element
const char *name;
// offset from the base pointer
int offset_from_bp;
// data for the stack frame element
struct stack_frame_data data;
};
#define STACK_PUSH_SIZE 4
enum {
STACK_FRAME_ELEMENT_TYPE_LOCAL_VARIABLE,
STACK_FRAME_ELEMENT_TYPE_SAVED_REGISTER,
STACK_FRAME_ELEMENT_TYPE_SAVED_BASE_POINTER,
STACK_FRAME_ELEMENT_TYPE_PUSHED_VALUE,
STACK_FRAME_ELEMENT_TYPE_UNKNOWN,
};
enum {
STACK_FRAME_ELEMENT_FLAG_IS_PUSHED_ADDRESS = 0b00000001,
STACK_FRAME_ELEMENT_FLAG_ELEMENT_NOT_FOUND = 0b00000010,
STACK_FRAME_ELEMENT_FLAG_IS_NUMERICAL = 0b00000100,
STACK_FRAME_ELEMENT_FLAG_HAS_DATA_TYPE = 0b00001000,
};
void stackframe_pop(struct node *func_node);
struct stack_frame_element *stackframe_back(struct node *func_node);
struct stack_frame_element *stackframe_back_expect(struct node *func_node,
int expecting_type,
const char *expecting_name);
void stackframe_pop_expecting(struct node *func_node, int expecting_type,
const char *expecting_name);
void stackframe_peek_start(struct node *func_node);
struct stack_frame_element *stackframe_peek(struct node *func_node);
void stackframe_push(struct node *func_node,
struct stack_frame_element *element);
void stackframe_sub(struct node *func_node, int type, const char *name,
size_t amount);
void stackframe_add(struct node *func_node, int type, const char *name,
size_t amount);
void stackframe_assert_empty(struct node *func_node);
struct parsed_switch_case {
// index of parsed case
int index;
};
enum {
UNARY_FLAG_IS_LEFT_OPERANDED_UNARY = 0b00000001,
};
struct node;
struct unary {
int flags;
// operator of the unary expression (i.e. !, ~, *, etc), even for multiple
// pointer access only one operator is stored
const char *op;
// operand of the unary expression
struct node *operand;
union {
struct indirection {
// depth of the indirection
int depth;
} indirection;
};
};
struct node {
int type;
int flags;
struct pos pos;
struct node_binded {
// Pointer to body node
struct node *owner;
// Pointer to function this node is in
struct node *function;
} binded;
union {
struct exp {
struct node *left;
struct node *right;
const char *op;
} exp;
struct parenthesis {
// expression inside the parenthesis node
struct node *exp;
} paren;
struct var {
struct datatype type;
int padding;
int aoffset; // aligned offset
const char *name;
struct node *val;
} var;
struct node_tenary {
// exp ? true : false
struct node *true_node;
struct node *false_node;
} tenary;
struct var_list {
// list of struct node* variables
struct vector *list;
} var_list;
struct bracket {
// int x[50]; [50] is bracket node and the inner node is NODE_TYPE_NUMBER
// with value 50
struct node *inner;
} bracket;
struct _struct {
const char *name;
struct node *body_n;
// struct abc {} var_name; var_name is the variable name
struct node *var;
} _struct;
struct _union {
const char *name;
struct node *body_n;
// union abc {} var_name; var_name is the variable name
struct node *var;
} _union;
struct body {
// vector of struct node*
struct vector *statements;
// sum of all the sizes of the variables in the body
size_t size;
// true if the variable size had to be increased to align to 16 bytes
bool padded;
// pointer to the largest variable node in the body based on size
struct node *largest_variable_node;
} body;
struct function {
// special flags for function nodes
int flags;
// return type of the function
struct datatype rtype;
// function name
const char *name;
struct function_args {
// vector of struct node* (variable nodes)
struct vector *args;
// how much to add to EBP to get to the first argument
size_t stack_addition;
} args;
// body of the function (NULL if it is a function prototype)
struct node *body_n;
// stack frame
struct stack_frame {
// vector of struct stack_frame_element*
struct vector *elements;
} stack_frame;
// stack size for all the variables in the function
size_t stack_size;
} func;
struct statement {
struct return_stmt {
// return expression
struct node *exp;
} return_stmt;
struct if_stmt {
// if (cond) { body }
struct node *cond_node;
// body of the if statement
struct node *body_node;
// else if statement
struct node *next;
} if_stmt;
struct else_stmt {
// else { body }
struct node *body_node;
} else_stmt;
struct for_stmt {
// for (init; cond; inc) { body }
struct node *init;
// condition
struct node *cond;
// increment
struct node *inc;
// body of the for statement
struct node *body;
} for_stmt;
struct while_stmt {
// while (cond) { body }
struct node *cond;
// body of the while statement
struct node *body;
} while_stmt;
struct do_while_stmt {
// do { body } while (cond)
struct node *cond;
// body of the do while statement
struct node *body;
} do_while_stmt;
struct switch_stmt {
// switch (exp) { body }
struct node *exp;
// body of the switch statement
struct node *body;
// vector of struct parsed_switch_case*
struct vector *cases;
// has default case
bool has_default_case;
} switch_stmt;
struct _case_stmt {
// case 50:
struct node *exp;
} _case;
struct _goto_stmt {
// goto label;
struct node *label;
} _goto;
} stmt;
struct node_label {
// label name
struct node *name;
} label;
struct cast {
// (type) exp
struct datatype dtype;
struct node *exp;
} cast;
struct unary unary;
};
union {
char cval;
const char *sval;
unsigned int inum;
unsigned long lnum;
unsigned long long llnum;
};
};
enum {
RESOLVER_ENTITY_FLAG_IS_STACK = 0b00000001,
RESOLVER_ENTITY_FLAG_NO_MERGE_WITH_NEXT_ENTITY = 0b00000010,
RESOLVER_ENTITY_FLAG_NO_MERGE_WITH_LEFT_ENTITY = 0b00000100,
RESOLVER_ENTITY_FLAG_DO_INDIRECTION = 0b00001000,
RESOLVER_ENTITY_FLAG_JUST_USE_OFFSET = 0b00010000,
RESOLVER_ENTITY_FLAG_IS_POINTER_ARRAY_ENTITY = 0b00100000,
RESOLVER_ENTITY_FLAG_WAS_CASTED = 0b01000000,
RESOLVER_ENTITY_FLAG_USES_ARRAY_BRACKETS = 0b10000000,
};
enum {
RESOLVER_ENTITY_TYPE_VARIABLE,
RESOLVER_ENTITY_TYPE_FUNCTION,
RESOLVER_ENTITY_TYPE_NATIVE_FUNCTION,
RESOLVER_ENTITY_TYPE_STRUCT,
RESOLVER_ENTITY_TYPE_FUNCTION_CALL,
RESOLVER_ENTITY_TYPE_ARRAY_BRACKET,
RESOLVER_ENTITY_TYPE_RULE,
RESOLVER_ENTITY_TYPE_GENERAL,
RESOLVER_ENTITY_TYPE_UNARY_GET_ADDRESS,
RESOLVER_ENTITY_TYPE_UNARY_INDIRECTION,
RESOLVER_ENTITY_TYPE_UNSUPPORTED,
RESOLVER_ENTITY_TYPE_CAST,
};
enum {
RESOLVER_SCOPE_FLAG_IS_STACK = 0b00000001,
};
struct resolver_process;
struct resolver_result;
struct resolver_scope;
struct resolver_entity;
typedef void *(*RESOLVER_NEW_ARRAY_BRACKET_ENTITY)(
struct resolver_result *result, struct node *array_entity_node);
typedef void (*RESOLVER_DELETE_SCOPE)(struct resolver_scope *scope);
typedef void (*RESOLVER_DELETE_ENTITY)(struct resolver_entity *entity);
typedef struct resolver_entity *(*RESOLVER_MERGE_ENTITIES)(
struct resolver_process *process, struct resolver_result *result,
struct resolver_entity *left_entity, struct resolver_entity *right_entity);
typedef void *(*RESOLVER_MAKE_PRIVATE)(struct resolver_entity *entity,
struct node *node, int offset,
struct resolver_scope *scope);
typedef void (*RESOLVER_SET_RESULT_BASE)(struct resolver_result *result,
struct resolver_entity *base_entity);
struct resolver_callbacks {
RESOLVER_NEW_ARRAY_BRACKET_ENTITY new_array_entity;
RESOLVER_DELETE_SCOPE delete_scope;
RESOLVER_DELETE_ENTITY delete_entity;
RESOLVER_MERGE_ENTITIES merge_entities;
RESOLVER_MAKE_PRIVATE make_private;
RESOLVER_SET_RESULT_BASE set_result_base;
};
struct resolver_process {
// resolver scopes
struct resolver_scopes {
// root scope
struct resolver_scope *root;
// current scope
struct resolver_scope *current;
} scopes;
// compile process
struct compile_process *compile_process;
// resolver callbacks
struct resolver_callbacks callbacks;
};
struct resolver_array_data {
// vector of struct resolver_entity*
struct vector *entities;
};
enum {
RESOLVER_DEFAULT_ENTITY_TYPE_STACK,
RESOLVER_DEFAULT_ENTITY_TYPE_SYMBOL
};
enum { RESOLVER_DEFAULT_ENTITY_FLAG_IS_LOCAL_STACK = 0b00000001 };
enum {
RESOLVER_DEFAULT_ENTITY_DATA_TYPE_VARIABLE,
RESOLVER_DEFAULT_ENTITY_DATA_TYPE_FUNCTION,
RESOLVER_DEFAULT_ENTITY_DATA_TYPE_ARRAY_BRACKET,
};
struct resolver_default_entity_data {
// data type of the entity
int type;
// address of the entity
char address[60];
// base address of the entity
char base_address[60];
// offset from the base
int offset;
// flags for the entity
int flags;
};
struct resolver_default_scope_data {
int flags;
};
enum {
RESOLVER_RESULT_FLAG_FAILED = 0b00000001,
RESOLVER_RESULT_FLAG_RUNTIME_NEEDED_TO_FINISH_PATH = 0b00000010,
RESOLVER_RESULT_FLAG_PROCESSING_ARRAY_ENTITIES = 0b00000100,
RESOLVER_RESULT_FLAG_HAS_POINTER_ARRAY_ACCESS = 0b00001000,
RESOLVER_RESULT_FLAG_FIRST_ENTITY_LOAD_TO_EBX = 0b00010000,
RESOLVER_RESULT_FLAG_FIRST_ENTITY_PUSH_VALUE = 0b00100000,
RESOLVER_RESULT_FLAG_FINAL_INDIRECTION_REQUIRED_FOR_VALUE = 0b01000000,
RESOLVER_RESULT_FLAG_DOES_GET_ADDRESS = 0b10000000,
};
struct resolver_result {
// first entity in the result
struct resolver_entity *first_entity_const;
// enitity identifier that represents the variable at the start of expression
struct resolver_entity *identifier;
// last struct or union entity that was discovered
struct resolver_entity *last_struct_union_entity;
// array data
struct resolver_array_data array_data;
// root entity
struct resolver_entity *entity;
// last entity
struct resolver_entity *last_entity;
// flags for the result
int flags;
// total number of entities in the result
size_t count;
// result base
struct resolver_result_base {
// address of the base
char address[60];
// base address
char base_address[60];
// offset from the base
int offset;
} base;
};
struct resolver_scope {
// flags for the scope
int flags;
// vector of struct resolver_entity*
struct vector *entities;
// next scope
struct resolver_scope *next;
// previous scope
struct resolver_scope *prev;
// private data for the scope
void *private;
};
struct resolver_entity {
// flags for the entity
int flags;
// type of the entity
int type;
// name of the entity
const char *name;
// offset from the base pointer
int offset_from_bp;
// node that the entity is related to
struct node *node;
union {
// data for the variable entity
struct resolver_entity_var_data {
// data type of the variable
struct datatype dtype;
// array runtime data
struct resolver_array_runtime {
// data type of the array
struct datatype dtype;
// node to index the array
struct node *index_node;
// multiplier for the index
int multiplier;
} array_runtime;
} var_data;