-
Notifications
You must be signed in to change notification settings - Fork 36
/
compiler.h
1700 lines (1431 loc) · 53 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 PEACHCOMPILER_H
#define PEACHCOMPILER_H
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <linux/limits.h>
#include <assert.h>
#define FAIL_ERR(message) assert(0 == 1 && message)
#define S_EQ(str, str2) \
(str && str2 && (strcmp(str, str2) == 0))
struct pos
{
int line;
int col;
const char *filename;
};
// In C We have a stack alignment of 16 bytes.
#define C_STACK_ALIGNMENT 16
#define STACK_PUSH_SIZE 4
#define C_ALIGN(size) (size % C_STACK_ALIGNMENT) ? size + (C_STACK_ALIGNMENT - (size % C_STACK_ALIGNMENT)) : size
#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 their is whitespace between the token and the next token
// i.e * a for operator token * would mean whitespace would be set for token "a"
bool whitespace;
// (5+10+20)
const char *between_brackets;
// Anything between function call arguments. ABC(hello world)
const char* between_arguments;
};
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 *parentheses_buffer;
struct buffer* argument_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_EXECUTE_NASM = 0b00000001,
COMPILE_PROCESS_EXPORT_AS_OBJECT = 0b00000010,
};
struct scope
{
int flags;
// void*
struct vector *entities;
// The total number of bytes this scope uses. Aligned to 16 bytes.
size_t size;
// NULL if no parent.
struct scope *parent;
};
enum
{
SYMBOL_TYPE_NODE,
SYMBOL_TYPE_NATIVE_FUNCTION,
SYMBOL_TYPE_UNKNOWN
};
struct symbol
{
const char *name;
int type;
void *data;
};
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
{
// This is the string that the element is related too. "Hello world"
const char *str;
// This is the assembly label that points to the memory
// where the string can be found.
const char label[50];
};
struct code_generator
{
struct generator_switch_stmt
{
struct generator_switch_stmt_entity
{
int id;
} current;
// Vector of generatr_switch_stmt_entity
struct vector* swtiches;
} _switch;
// A 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* that will go in the data section
struct vector* custom_data_section;
// vector of struct response*
struct vector *responses;
};
struct resolver_process;
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_exp(gen)
typedef void(*ASM_PUSH_PROTOTYPE)(const char* ins, ...);
typedef void (*NATIVE_FUNCTION_CALL)(struct generator* generator, struct native_function* func, struct vector* arguments);
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 gen_exp;
GENERATOR_END_EXPRESSION end_exp;
GENERATOR_ENTITY_ADDRESS entity_address;
GENERATOR_FUNCTION_RETURN ret;
struct compile_process* compiler;
// Private data for the generator
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;
struct preprocessor_definition;
struct preprocessor_function_argument;
struct preprocessor_included_file;
typedef void (*PREPROCESSOR_STATIC_INCLUDE_HANDLER_POST_CREATION)(struct preprocessor* preprocessor, struct preprocessor_included_file* included_file);
enum
{
PREPROCESSOR_DEFINITION_STANDARD,
PREPROCESSOR_DEFINITION_MACRO_FUNCTION,
PREPROCESSOR_DEFINITION_NATIVE_CALLBACK,
PREPROCESSOR_DEFINITION_TYPEDEF
};
struct preprocessor;
struct preprocessor_definition;
struct preprocessor_function_argument
{
// Tokens for this argument struct token
struct vector* tokens;
};
struct preprocessor_function_arguments
{
// Vector of struct preprocessor_function_argument
struct vector* arguments;
};
typedef int (*PREPROCESSOR_DEFINITION_NATIVE_CALL_EVALUATE)(struct preprocessor_definition* definition, struct preprocessor_function_arguments* arguments);
typedef struct vector* (*PREPROCESSOR_DEFINITION_NATIVE_CALL_VALUE)(struct preprocessor_definition* definition, struct preprocessor_function_arguments* arguments);
struct preprocessor_definition
{
// I.e standard or macro function
int type;
// The name i.e #define ABC ABC is the name
const char* name;
union
{
struct standard_preprocessor_definition
{
// A vecor of definition value tokens. Values can be multiple lines
// vector of struct token
struct vector* value;
// A vector of const char* representing function arguments in order
// for example: ABC(a, b, c)
struct vector* arguments;
} 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];
};
// Function pointers
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 preprocessor
{
// A vector of struct preprocessor_definition*
struct vector* definitions;
// Vector of struct preprocessor_node*
struct vector* exp_vector;
struct expressionable* expressionable;
struct compile_process* compiler;
// A vector of included files struct preprocessor_included_file*
struct vector* includes;
};
struct preprocessor* preprocessor_create(struct compile_process* compiler);
int preprocessor_run(struct compile_process* compiler);
struct compile_process
{
// The flags in regards to how this file should be compiled
int flags;
struct pos pos;
struct compile_process_input_file
{
FILE *fp;
const char *abs_path;
} cfile;
// Untampered token vector, contains definitions, and source code tokens, the preprocessor
// will go through this vector and populate the "token_vec" vector after it is done.
struct vector* token_vec_original;
// A 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 symbol*
struct vector *table;
// struct vector* multiple symbol tables stored in here..
struct vector *tables;
} symbols;
// Pointer to our codegenerator.
struct code_generator *generator;
struct resolver_process *resolver;
// A vector of const char* that represents include directories.
struct vector* include_dirs;
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_PARENTHESES,
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 type of long, int, float ect..
int type;
// i.e long int. int being the secondary.
struct datatype *secondary;
// long
const char *type_str;
// The sizeof the datatype.
size_t size;
int pointer_depth;
union
{
struct node *struct_node;
struct node *union_node;
};
struct array
{
struct array_brackets *brackets;
/**
*
* The total array size: Equation = DATATYPE_SIZE * EACH_INDEX
*/
size_t size;
} array;
};
struct parsed_switch_case
{
// Index of the parsed case
int index;
};
struct stack_frame_data
{
/*
* The datatype that was pushed to the stack
*/
struct datatype dtype;
};
struct stack_frame_element
{
// Stack frame element flags
int flags;
// The type of frame element
int type;
/**
* The name of the frame element, not a variable name. I.e result_value
*/
const char *name;
// The offset this element is on the base pointer
int offset_from_bp;
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_BP,
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_DATATYPE = 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);
enum
{
UNARY_FLAG_IS_LEFT_OPERANDED_UNARY = 0b00000001,
};
struct node;
struct unary
{
int flags;
// "*" for pointer access.. **** even for multiple pointer access only the first operator
// is here
const char *op;
struct node *operand;
union
{
struct indirection
{
// The pointer depth
int depth;
} indirection;
};
};
struct node
{
int type;
int flags;
struct pos pos;
struct node_binded
{
// Pointer to our body node
struct node *owner;
// Pointer to the function this node is in.
struct node *function;
} binded;
union
{
struct exp
{
struct node *left;
struct node *right;
const char *op;
} exp;
struct parenthesis
{
// The expression inside the parenthesis node.
struct node *exp;
} parenthesis;
struct var
{
struct datatype type;
int padding;
// Aligned offset
int aoffset;
const char *name;
struct node *val;
} var;
struct node_tenary
{
struct node *true_node;
struct node *false_node;
} tenary;
struct varlist
{
// A list of struct node* variables.
struct vector *list;
} var_list;
struct bracket
{
// int x[50]; [50] would be our bracket node. The inner would NODE_TYPE_NUMBER value of 50
struct node *inner;
} bracket;
struct _struct
{
const char *name;
struct node *body_n;
/**
* struct abc
* {
*
* } var_name;
*
* NULL if no variable attached to structure.
*
*/
struct node *var;
} _struct;
struct _union
{
const char *name;
struct node *body_n;
/**
* struct abc
* {
*
* } var_name;
*
* NULL if no variable attached to structure.
*
*/
struct node *var;
} _union;
struct body
{
/**
* struct node* vector of statements
*/
struct vector *statements;
// The size of combined variables inside this body.
size_t size;
// True if the variable size had to be increased due to padding in the body.
bool padded;
// A pointer to the largest variable node in the statements vector.
struct node *largest_var_node;
} body;
struct function
{
// Special flags
int flags;
// Return type i.e void, int, long ect...
struct datatype rtype;
// I.e function name "main"
const char *name;
struct function_arguments
{
// Vector of struct node* . Must be type NODE_TYPE_VARIABLE
struct vector *vector;
// How much to add to the EBP to find the first argument.
size_t stack_addition;
} args;
// Pointer to the function body node, NULL if this is a function prototype
struct node *body_n;
struct stack_frame
{
// A vector of stack_frame_element
struct vector *elements;
} frame;
// The stack size for all variables inside this function.
size_t stack_size;
} func;
struct statement
{
struct return_stmt
{
// The expression of the return
struct node *exp;
} return_stmt;
struct if_stmt
{
// if(COND) {// body }
struct node *cond_node;
struct node *body_node;
// if(COND) {} else {}
struct node *next;
} if_stmt;
struct else_stmt
{
struct node *body_node;
} else_stmt;
struct for_stmt
{
struct node *init_node;
struct node *cond_node;
struct node *loop_node;
struct node *body_node;
} for_stmt;
struct while_stmt
{
struct node *exp_node;
struct node *body_node;
} while_stmt;
struct do_while_stmt
{
struct node *exp_node;
struct node *body_node;
} do_while_stmt;
struct switch_stmt
{
struct node *exp;
struct node *body;
struct vector *cases;
bool has_default_case;
} switch_stmt;
struct _case_stmt
{
struct node *exp;
} _case;
struct _goto_stmt
{
struct node *label;
} _goto;
} stmt;
struct node_label
{
struct node *name;
} label;
struct cast
{
struct datatype dtype;
struct node *operand;
} 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_STRUCTURE,
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_result;
struct resolver_process;
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 compile_process;
struct resolver_process
{
struct resolver_scopes
{
struct resolver_scope *root;
struct resolver_scope *current;
} scope;
struct compile_process *compiler;
struct resolver_callbacks callbacks;
};
struct resolver_array_data
{
// Holds nodes of type resolver_entity
struct vector *array_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
{
// i.e variable, function, structure
int type;
// This is the address [ebp-4], [var_name+4]
char address[60];
// ebp, var_name
char base_address[60];
// -4
int offset;
// Flags relating to the entity data
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
{
// This is the first entity in our resolver result
struct resolver_entity *first_entity_const;
// This entity represents the variable at the start of this expression.
struct resolver_entity *identifier;
// Equal to the last structure or union entity discovered.
struct resolver_entity *last_struct_union_entity;
struct resolver_array_data array_data;
// The root entity of our result
struct resolver_entity *entity;