-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.c
399 lines (332 loc) · 12.9 KB
/
generate.c
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
///////////////////////////////////////////////////////////////////////////////////
// School: Brno University of Technology, Faculty of Information Technology //
// Course: Formal Languages and Compilers //
// Project: IFJ17 //
// Module: Generator of code //
// Authors: Kristián Liščinský (xlisci01) //
// Matúš Liščinský (xlisci02) //
// Šimon Stupinský (xstupi00) //
// Vladimír Marcin (xmarci10) //
///////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <math.h>
#include "generate.h"
#include "error.h"
#include "semantic_control.h"
#include "scanner.h"
#include "clear.h"
/// replace malloc & calloc calls with our malloc & calloc wrapper
#define malloc(size) _malloc(size)
#define calloc(num, size) _calloc(num, size)
list_t * list;
// generator of label names
char * gen_label_name(int i, char c){
unsigned digits = floor(log10(abs(i)))+1; /// getting count of digits
char * name = (char *) malloc(digits*sizeof(int)+3);
if(!name)
print_err(99);
name[0] = '&'; /// first character of string will be '&'
name[1] = c; /// add singificant char
sprintf(name+2, "%d", i); /// concat with number represented by param i
return name;
}
void list_init()
{
list = (list_t*) malloc(sizeof(list_t));
if(!list)
print_err(99);
list->First=NULL;
list->Last=NULL;
}
variable_t * create_var(char *str1, bool constant){
variable_t * tmp = init_variable();
tmp->data.str = (char*)malloc(sizeof(char)*(strlen(str1)+2));
if(!tmp->data.str)
print_err(99);
strcpy(tmp->data.str, str1);
strcat(tmp->data.str, " ");
tmp->constant = constant;
return tmp;
}
void concat()
{
/// defines uniq variables on newly created and pushed frame, pops strings to that variables
/// does operation concat with these strings, pushs it on data stack and pops frame
variable_t * tmp1 = create_var("&C1 ", false);
variable_t * tmp2 = create_var("&C2 ", false);
list_insert("CREATEFRAME ",NULL, NULL, NULL);
list_insert("PUSHFRAME ",NULL, NULL, NULL);
list_insert("DEFVAR ", tmp1, NULL, NULL);
list_insert("DEFVAR ", tmp2, NULL, NULL);
list_insert("POPS ", tmp1, NULL, NULL);
list_insert("POPS ", tmp2, NULL, NULL);
list_insert("CONCAT ",tmp2, tmp2, tmp1);
list_insert("PUSHS ",tmp2, NULL, NULL);
list_insert("POPFRAME ",NULL, NULL, NULL);
}
/// function to create & initialize instruction
instruction_t * instr_init (){
instruction_t * new = (instruction_t *)malloc(sizeof(instruction_t));
if(!new)
print_err(99);
new->instr_name = (char *) calloc(15,sizeof(char));
new->op1 = new->op2 = new->op3 = NULL;
new->next = NULL;
return new;
}
/// function to create new variable same as src variable
variable_t * copy_variable(variable_t * src){
variable_t * new = init_variable();
new->data_type = src->data_type;
switch(new->data_type){
case INTEGER: new->data.i = src->data.i;break;
case DOUBLE: new->data.d = src->data.d;break;
default: if(src->data.str){
new->data.str = (char *) malloc(sizeof(char)*(strlen(src->data.str)+1));
if(!new->data.str)
print_err(99);
strcpy(new->data.str, src->data.str);
}break;
}
new->constant = src->constant;
return new;
}
void list_insert(char * instr, variable_t * par1, variable_t * par2, variable_t * par3 )
{
instruction_t * new = instr_init();
strcpy(new->instr_name,instr);
if(par1)
new->op1 = copy_variable(par1);
else new->op1 = NULL;
if(par2)
new->op2 = copy_variable(par2);
else new->op2 = NULL;
if(par3)
new->op3 = copy_variable(par3);
else new->op3 = NULL;
if(list){
if(list->First == NULL)
list->First=list->Last=new;
else {
list->Last->next=new;
list->Last=new;
}
}
}
void process_string (char * orig_string){
if (!orig_string)
return;
for(unsigned i = 0; orig_string[i]!='\0' ; i++){
if(isalpha(orig_string[i]) || isdigit(orig_string[i]))
printf("%c",orig_string[i]);
else
printf("\\%03d", (int)((unsigned char)orig_string[i]));
}
printf(" ");
}
void length_of_str(variable_t * l_value){
/// defines own variables on newly created and pushed frame, pops strings to that variables
/// using operation STRLEN computes length of string and pushs it on data stack
/// default changes type if it's needed and pops frame
list_insert("CREATEFRAME ",NULL, NULL, NULL);
list_insert("PUSHFRAME ",NULL, NULL, NULL);
variable_t * tmp = create_var("RETURN ", false);
list_insert("DEFVAR ",tmp, NULL, NULL);
variable_t * str = create_var("PRINT ", false);
list_insert("DEFVAR ",str, NULL, NULL);
list_insert("POPS ", str, NULL, NULL);
list_insert("STRLEN ",tmp, str, NULL);
list_insert("PUSHS ",tmp, NULL, NULL);
if(l_value && l_value->data_type == DOUBLE)
list_insert("INT2FLOATS ",NULL, NULL, NULL);
list_insert("POPFRAME ",NULL, NULL, NULL);
}
void retype(variable_t * var){
if(var->data_type == INTEGER){
list_insert("INT2FLOATS ",NULL, NULL, NULL);
}
else if(var->data_type == DOUBLE){
list_insert("FLOAT2R2EINTS ",NULL, NULL, NULL);
}
}
void substr(){
/// defines own variables on newly created and pushed frame, pops real params to that variables
/// using operations GETCHAR and CONCAT gets char on current position in string and concat it with already created string in one loop
/// also created LABELS for loop and in case of wrong real parameters
/// pushs result on data stack and pops frame
list_insert("CREATEFRAME ",NULL, NULL, NULL);
list_insert("PUSHFRAME ",NULL, NULL, NULL);
variable_t * ret = create_var("RETURN ", false);
variable_t * tmp = create_var("PRINT ", false);
variable_t * jedna = create_var("INT@1", true);
variable_t * nula = create_var("INT@0", true);
variable_t * s = create_var("ASC ", false);
variable_t * i = create_var("NEXT ", false);
variable_t * n = create_var("SUBSTR ", false);
variable_t * b = create_var("BOOLEAN ", false);
variable_t * btrue = create_var("bool@true", true);
static unsigned substr_counter;
substr_counter++;
char *name = gen_label_name(substr_counter, 'S');
variable_t * zero = create_var(name, true);
substr_counter++;
name = gen_label_name(substr_counter, 'S');
variable_t * end = create_var(name, true);
substr_counter++;
name = gen_label_name(substr_counter, 'S');
variable_t * change = create_var(name, true);
substr_counter++;
name = gen_label_name(substr_counter, 'S');
variable_t * normal = create_var(name, true);
list_insert("DEFVAR ",ret, NULL, NULL);
list_insert("DEFVAR ",tmp, NULL, NULL);
list_insert("DEFVAR ",s, NULL, NULL);
list_insert("DEFVAR ",i, NULL, NULL);
list_insert("DEFVAR ",n, NULL, NULL);
list_insert("DEFVAR ",b, NULL, NULL);
variable_t * var = create_var("STRING@", true);
list_insert("MOVE ", ret, var, NULL);
list_insert("POPS ", n, NULL, NULL);
list_insert("POPS ", i, NULL, NULL);
list_insert("POPS ", s, NULL, NULL);
list_insert("JUMPIFEQ ", end, n, nula);
// condition jump
list_insert("STRLEN ", tmp, s, NULL);
list_insert("JUMPIFEQ ", zero ,tmp, nula);
list_insert("GT ", b, i, nula);
list_insert("NOT ", b, b, NULL);
list_insert("JUMPIFEQ ", zero, b, btrue);
list_insert("SUB ", tmp, tmp, i);
list_insert("GT ", b, n, tmp);
list_insert("JUMPIFEQ ", change ,b, btrue);
list_insert("LT ",b ,n, nula);
list_insert("JUMPIFEQ ", change ,b, btrue);
list_insert("JUMP ", normal, NULL, NULL);
// jump normal
//label change
list_insert("LABEL ", change, NULL, NULL);
list_insert("MOVE ", n, tmp, NULL);
list_insert("ADD ", n, n, jedna);
// label normal
list_insert("LABEL ", normal, NULL, NULL);
list_insert("SUB ", i, i, jedna);
substr_counter++;
name = gen_label_name(substr_counter, 'S');
var = create_var(name, true);
list_insert("LABEL ", var, NULL, NULL);
list_insert("GETCHAR ",tmp, s, i);
list_insert("CONCAT ", ret, ret, tmp);
list_insert("SUB ", n, n, jedna);
list_insert("ADD ", i, i, jedna);
list_insert("JUMPIFNEQ ", var, n, nula);
list_insert("JUMP ", end, NULL, NULL);
// JUMP END
// LABEL NULA
list_insert("LABEL ", zero, NULL, NULL);
var = create_var("STRING@", true);
list_insert("MOVE ", ret, var, NULL);
// LABEL END
list_insert("LABEL ", end, NULL, NULL);
list_insert("PUSHS ",ret, NULL, NULL);
list_insert("POPFRAME ",NULL, NULL, NULL);
}
void asc (variable_t * l_value){
/// defines own variables on newly created and pushed frame, pops real params to that variables
/// using operation STR2INT gets ASCII value of char on defined position in string
/// also uses condition labels in case of wrong real params
/// default changes type if it's needed and pops frame
list_insert("CREATEFRAME ",NULL, NULL, NULL);
list_insert("PUSHFRAME ",NULL, NULL, NULL);
variable_t * tmp = create_var("RETURN ", false);
variable_t * s = create_var("STRING ", false);
variable_t * b = create_var("BOOLEAN ", false);
variable_t * i = create_var("NEXT ", false);
variable_t * jedna = create_var("INT@1", true);
variable_t * nula = create_var("INT@0", true);
variable_t * btrue = create_var("bool@true", true);
static unsigned asc_counter;
asc_counter++;
char *name = gen_label_name(asc_counter, 'A');
variable_t * zero = create_var(name, true);
asc_counter++;
name = gen_label_name(asc_counter, 'A');
variable_t * end = create_var(name, true);
list_insert("DEFVAR ",tmp, NULL, NULL);
list_insert("DEFVAR ",s, NULL, NULL);
list_insert("DEFVAR ",b, NULL, NULL);
list_insert("DEFVAR ",i, NULL, NULL);
list_insert("POPS ", i, NULL, NULL);
list_insert("POPS ", s, NULL, NULL);
list_insert("GT ", b, i, nula);
list_insert("NOT ", b, b, NULL);
list_insert("JUMPIFEQ ", zero, b, btrue);
list_insert("SUB ", i, i, jedna);
list_insert("STRLEN ", tmp, s, NULL);
list_insert("LT ", b, i, tmp);
list_insert("NOT ", b, b, NULL);
list_insert("JUMPIFEQ ", zero, b, btrue);
list_insert("STRI2INT ", tmp, s, i);
list_insert("JUMP ", end, NULL, NULL);
list_insert("LABEL ", zero, NULL, NULL);
list_insert("MOVE ", tmp, nula, NULL );
list_insert("LABEL ", end, NULL, NULL);
list_insert("PUSHS ", tmp, NULL, NULL);
if(l_value && l_value->data_type == DOUBLE)
list_insert("INT2FLOATS ",NULL, NULL, NULL);
list_insert("POPFRAME ",NULL, NULL, NULL);
}
void chr(){
/// real param is already on stack, so the only thing this function does is calling operation INT2CHARS
/// which converts int number to char and pushs it on stack perfectly as we need
list_insert("CREATEFRAME ",NULL, NULL, NULL);
list_insert("PUSHFRAME ",NULL, NULL, NULL);
list_insert("INT2CHARS", NULL, NULL, NULL);
list_insert("POPFRAME ",NULL, NULL, NULL);
}
void print_list(){
printf(".IFJcode17\nJUMP SCOPE\n"); /// printing header and jump to scope
for(instruction_t * tmp = list->First; tmp!=NULL; tmp=tmp->next){ /// going through the instr. tape and printing instructions
if (tmp->instr_name) printf("%s",tmp->instr_name); /// print instruction name
if(tmp->op1){
if(tmp->op1->constant){ /// print var as constant
switch (tmp->op1->data_type){
case INTEGER:printf("int@%d",tmp->op1->data.i);break;
case DOUBLE:printf("float@%g",tmp->op1->data.d);break;
case STRING:printf("string@");
process_string(tmp->op1->data.str); break;
default: if(tmp->op1->data.str)printf("%s", tmp->op1->data.str);break; /// print only stored string as constant
}
}
else if(tmp->op1->data.str) printf("LF@%s ", tmp->op1->data.str); /// print name of variable this way: LF@var_name
}
if(tmp->op2){
if(tmp->op2->constant){
switch (tmp->op2->data_type){
case INTEGER:printf("int@%d",tmp->op2->data.i);break;
case DOUBLE:printf("float@%g",tmp->op2->data.d);break;
case STRING:printf("string@");
process_string(tmp->op2->data.str); break;
default: if(tmp->op2->data.str)printf("%s", tmp->op2->data.str);break;
}
}
else if(tmp->op2->data.str) printf("LF@%s ", tmp->op2->data.str);
}
if(tmp->op3){
if(tmp->op3->constant){
switch (tmp->op3->data_type){
case INTEGER:printf("int@%d",tmp->op3->data.i);break;
case DOUBLE:printf("float@%g",tmp->op3->data.d);break;
case STRING:printf("string@");
process_string(tmp->op3->data.str); break;
default: if(tmp->op3->data.str)printf("%s", tmp->op3->data.str);break;
}
}
else if(tmp->op3->data.str) printf("LF@%s ", tmp->op3->data.str);
}
printf("\n");
}
}