-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_A4_translator.h
211 lines (192 loc) · 8.27 KB
/
3_A4_translator.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
#ifndef __PARSER_H
#define __PARSER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define size_of_void 0;
#define size_of_char 1;
#define size_of_int 4;
#define size_of_pointer 4;
#define MAX_STACK 100
// Global Symbol Tables
extern struct symboltable* currST; // Current Symbol Table
extern struct symboltable* globalST; // Global Symbol Table
extern struct symboltable* new_ST; // New Symbol Table -- used in function declaration
extern struct var_type_stack var_type;// stack for storing the type of the variable
extern struct string_list* string_head; // head of the string list
extern char* yytext;
extern void yyerror(char *s);
extern int yyparse(void);
enum symboltype_enum {
TYPE_VOID,
TYPE_INT,
TYPE_CHAR,
TYPE_PTR,
TYPE_FUNC,
TYPE_ARRAY,
TYPE_STRING
};
enum category_enum {
TYPE_LOCAL,
TYPE_GLOBAL,
TYPE_PARAM,
TYPE_FUNCTION,
TYPE_TEMP
};
enum op_code{
OP_PLUS,
OP_MINUS,
OP_MULT,
OP_DIV,
OP_MOD,
OP_EQUALS,
OP_NOT_EQUALS,
OP_LT,
OP_LT_EQUALS,
OP_GT,
OP_GT_EQUALS,
OP_GOTO,
OP_ASSIGN,
OP_ASSIGN_STR,
OP_ASSIGN_AMPER,
OP_ASSIGN_ASTERISK,
OP_ASTERISK_ASSIGN,
OP_UMINUS,
OP_ASSIGN_BOX,
OP_BOX_ASSIGN,
OP_RETURN,
OP_PARAM,
OP_CALL,
OP_CALL_VOID,
OP_FUNC,
OP_LABEL,
OP_ENDFUNC,
OP_RETURN_VOID,
};
/**************************************************************************/
/* QUADS and TAC */
/**************************************************************************/
struct quad{
enum op_code op; // Operator
char* arg1; // Argument 1
char* arg2; // Argument 2
char* result; // Result
};
typedef struct quad quad;
struct qArray{ // Linked list of quads
quad* arr; // Array of quads
int count; // Total number of quads
struct qArray* nextQuad; // Pointer to next qArray
};
typedef struct qArray qArray;
void print_quadArray(qArray* head); // Print the quads
void print_quad(quad* q); // Print a single quad
void emit(enum op_code op, char* arg1, char* arg2, char* result); // Emit a quad -- add to quadArray
char* printOP(enum op_code op); // Print the operator
int nextInstr(); // Get the next instruction number
qArray* quadArray_initialize(qArray* head); // Initialize the quadArray
/**************************************************************************/
/* SYMBOL TABLE STRUCTURES */
/**************************************************************************/
struct symboltype{
enum symboltype_enum type; // Type of symbol
int width; // Size of Array (default 1)
struct symboltype* ptr; // Pointer to type of symbol (for TYPE_PTR)
};
typedef struct symboltype symboltype;
// Name Type Category Initial Value Size Nested Table
struct symboltableentry{ // Structure of a symbol table entry (ROW) for variables
char* name; // Name of symbol
symboltype* type; // Type of symbol
char* initial_value; // Initial value of symbol
int size; // Size of symbol
int offset; // Offset of symbol
enum category_enum category; // Category of symbol
struct symboltable* next; // Pointer to next symboltableentry -- nested
};
typedef struct symboltableentry symboltableentry;
struct symboltable{ // Structure of a symbol table (TABLE)
char* name; // Name of symbol table
struct symboltable* parent; // Pointer to parent symbol table
int count; // Count of entries in symbol table
// int tempCount; // Count of temporary variables in symbol table
int paramCount; // Count of parameters in symbol table
symboltableentry** _argList; // List of arguments of function
symboltableentry** table_entries; // Pointer to entries in symbol table -- linked list of entries
symboltype* _retVal; // Return type of function
struct symboltable* next; // Pointer to next symbol table
int returnLabel; // Return label
};
typedef struct symboltable symboltable;
/**************************************************************************/
/* Expression, Statement, and Function Structures */
/**************************************************************************/
struct expression{
symboltableentry *loc; // Pointer to symbol table entry of variable
symboltableentry *arrBase; // Pointer to symbol table entry of array base
bool isBool; // Is expression boolean?
bool isPtr; // Is expression pointer?
bool isArray; // Is expression array?
bool isFunc; // Is expression function?
int* trueList; // List of true labels
int* falseList; // List of false labels
int* nextList; // List of next labels
int returnLabel; // Return label
};
typedef struct expression expression;
struct statement{
int* nextList; // List of next labels
int returnLabel; // Return label
enum symboltype_enum Type; // Does statement have a type?
};
typedef struct statement statement;
void backpatch(int* list, int label); // Backpatch a list of labels with a label
int* makelist(int label); // Make a list of labels
int* merge(int* list1, int* list2); // Merge two lists of labels
statement* create_statement();
expression* create_expression();
expression* bool2int(expression* e); // Convert a boolean expression to an integer expression
expression* int2bool(expression* e); // Convert an integer expression to a boolean expression
/**************************************************************************/
/* VARIABLE STACK */
/**************************************************************************/
// stack for variable type
struct var_type_stack{
enum symboltype_enum type[MAX_STACK]; // Type of symbol
int top; // Top of stack
};
typedef struct var_type_stack var_type_stack;
// Stack Functions
void stack_intialize(var_type_stack *s);
void push(var_type_stack* stack, enum symboltype_enum type); // Push a type to the stack
enum symboltype_enum pop(var_type_stack *s); // Pop a type from the stack
// linked list for string
struct string_list{
char* str;
int entries;
struct string_list* next;
};
typedef struct string_list string_list;
// String List Functions
string_list* string_list_initialize();
void ll_insert(string_list* head, char* str);
void ll_delete(string_list* head);
/**************************************************************************/
/* SYMBOL TABLE FUNCTIONS */
/**************************************************************************/
symboltableentry *lookup(symboltable* currST, char* yytext); // Lookup a symbol in the symbol table
symboltableentry* parentLookup(symboltable* currST, char* yytext); // Lookup a symbol in the parent symbol table
symboltable* create_symboltable(char* name, symboltable* parent); // Create a new symbol table
symboltype* create_symboltype(enum symboltype_enum type, int width, symboltype* ptr); // Create a new symbol type
symboltableentry* gentemp(symboltype* type, char* initial_value); // Generate a temporary variableint get_size(symboltype* type); // Get the width of a symbol
symboltableentry* genparam(symboltype* type, char* initial_value); // Generate a parameter
void update_return_ST(symboltable* currST, int update); // Update the return type of a function
void update_type(symboltableentry* entry, symboltype* type); // Update the type of a symbol
void print_ST(symboltable *currST); // Print the symbol table
void update_ST(symboltable* currST, symboltableentry* entry); // Update the symbol table
char* printType(symboltype* type); // Print the type of a symbol
char* printCategory(enum category_enum category); // Print the category of a symbol
int typecheck(symboltype* type1, symboltype* type2); // Check if two types are equal
void push_args(symboltable* currST, symboltableentry* arg); // Push arguments to the symbol table
#endif // __PARSER_H