-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_generator.h
169 lines (158 loc) · 5 KB
/
code_generator.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
/************************************************************************
*
* Compiler implementation for imperative programming language IFJ18
*
* Autors:
* Sasák Tomáš - xsasak01
* Venkrbec Tomáš - xvenkr01
* Krajči Martin - xkrajc21
* Dižová Natália - xdizov00
*
***********************************************************************/
#include "scanner.h"
// This constant is numerical difference between builtin functions with return value moved or not
#define NOCALL_CALL_DIFFERENCE 22
// Macros for instruction inside instruction list
#define DEFVAR 228
// User's functions
#define FUN_DEF 200
#define FUN_END 201
#define FUN_CALL 202 // function return is going to be saved
#define NOFUN_CALL 216 // function return value is not going to be saved
// If calls
#define IF_CALL 203
#define ELSE_CALL 204
#define IF_END 205
#define IF_COND_END 260
// While calls
#define WHILE_CALL 206
#define WHILE_END 207
#define WHILE_COND_END 238
// Built-in functions
#define INPUTS_CALL 208
#define INPUTI_CALL 209
#define INPUTF_CALL 210
#define PRINT_CALL 211
#define LENGTH_CALL 212
#define SUBSTR_CALL 213
#define ORD_CALL 214
#define CHR_CALL 215
#define NOINPUTS_CALL 230
#define NOINPUTI_CALL 231
#define NOINPUTF_CALL 232
#define NOPRINT_CALL 233
#define NOLENGTH_CALL 234
#define NOSUBSTR_CALL 235
#define NOORD_CALL 236
#define NOCHR_CALL 237
#define CONCAT_CALL 253
#define CONCAT_END 254
// Operators
#define ADD 217 // '+'
#define SUB 218 // '-'
#define MUL 219 // '*'
#define DIV 220 // '/'
#define MOVE 221 // '='
#define EQ 222 // '=='
#define LT 223 // '<'
#define LE 224 // '<='
#define GT 225 // '>'
#define GE 226 // '>='
#define NE 227 // '!='
#define CONCAT 252 // string + string
// Data stack instructions
#define EXPRESSION_CALL 255
#define EXPRESSION_END 256
#define ADDS 241
#define SUBS 242
#define MULS 243
#define DIVS 244
#define LTS 246
#define GTS 247
#define EQS 248
#define PUSHS 249
#define POPS 250
#define NOTS 251
// Empty instruction
#define NOP 299
typedef struct tokenList tTList;
typedef struct instructionList tIList;
typedef struct instructionNode tInstr;
typedef struct pointerNode tPtr;
typedef struct pointerList tPList;
/**
* Structure used for deallocating strings which are shared for multiple tokens to prevent double free (SEGABRT).
* Structure works as ADS single-linked list.
*/
typedef struct pointerList
{
tPtr *head; //< Pointer to the head of the single-linked list.
tPtr *active; //< Pointer to the last member of the list, which speed's up adding new member to the list.
}pointerList;
/**
* Structure which signifies one member(node) of single-linked list with strings to prevent double free.
* @see tTList
*/
typedef struct pointerNode
{
string freed; //< String which is going to get free.
tPtr *next; //< Pointer to the next member(node).
}pointerNode;
/**
* Structure ADS single-linked list which contains parameters saved as tokens of ONE instruction in instruction list.
* Example: ord(s, i) - token s and token i is in this list.
* @see tIList
*/
typedef struct tokenList
{
tToken param; //< Token which signifies one parameter of instruction.
tTList *next; //< Pointer to the next parameter. (single-linked list)
}tokenList;
/**
* Structure which signifies one instruction with parameters inside instruction list (single-linked list ADS).
* @see tIList
*/
typedef struct instructionNode
{
int instr; //< Macro of one instruction generated by parser, macros are in code_generator.h.
tTList *params; //< Pointer to the parameters of instruction (single-linked list).
tInstr *next; //< Pointer to the next instruction.
}instructionNode;
/**
* Structures which is main structure of the code generator, this ADS (single-linked list) signifies inside mid-code of compiler which is generated by parser, after parsing, this list is submited for code generator
* to generate final compiled code.
*/
typedef struct instructionList
{
tInstr *head; //< First generated instruction, head of the list.
tInstr *active; //< Latest generated instruction, to speed appending of list.
}instructionList;
/**
* This global structure is code generator data structure, which holds final list instructions
* and requests for builtted-in functions.
*/
typedef struct codegenData
{
tIList *ilist;
bool lengthRequest;
bool substrRequest;
bool ordRequest;
bool chrRequest;
int uniqueCounter;
bool inExpression;
}tcgData;
tcgData cgData;
tToken choose_return(tInstr *instruction);
void init_plist(tPList *plist);
void init_ilist(tIList *instrs);
void init_cgData();
void insert_instr(tIList *instrs, int instr);
void insert_param(tIList *instrs, tToken param);
void insert_ptr(tPList *plist, string freed);
bool search_ptr(tPList *ptr, string freed);
void free_ilist(tIList *instrs);
void free_plist(tPList *plist);
void generate_code();
void generate_instruction(FILE *f, tInstr *instruction);
int generate_if(FILE *f, tInstr *instruction, bool scoped, int uniqueIf);
int generate_while(FILE *f, tInstr *instruction, bool scoped, int uniqueWhile);