/
c8c.c
547 lines (539 loc) · 18.9 KB
/
c8c.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
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
#include <stdio.h>
#include <stdlib.h>
#include "calc3.h"
#include "y.tab.h"
#include <string.h>
static int lbl;
static int lel;//label of exception
extern bool iserror;
extern char err[50];
extern char *errtext;
struct stack{
int size;
int i;
int (*data)[2];//data[0] for start lable, data[1] for end lable.
}s;
typedef struct funnode{
int funid;
int label;
char *name;
int returnType;
nodeType * parameter;
struct funnode * next;
}FunNode;
FunNode *fn = NULL;
bool insertFunction(SymbolType type, nodeType* p, int funid);
FunNode * getFunc(char * name);
typedef struct symbolnode{
char * name;//Variable name
SymbolType type;
int index;//Variable offset in assembler, based on fp
int dimension;
int * size;//size of array size[0] -> size[dimension-1] from high dimension to low dimension
int totalsize;//totalsize of this variable
struct symbolnode * next;
} SymbolNode;
SymbolNode *sn = NULL;
bool insertSymbolDriver(SymbolType type, nodeType* p);
bool insertSymbol(char * name, SymbolType type, int dimension, int *size);
SymbolNode * getSymbol(char * name);
bool isDeclared(nodeType* p);
void storeOffsetInIn(nodeType* p);//store dynamical offset value of specified variable in 'in'
//void init();//reserve label for build in functions
void testOutofBoundary(nodeType* p);//test whether value of 'in' is larger than p->totalsize(out of index)
int getSymbolOffset(char * name, int dimension, int *size);
void push(int start, int end);
int top(int index);
int* pop();
int ex(nodeType *p) {
int lblx, lbly, lbl1, lbl2, lblz, lbl3;
int i,j;
SymbolType type;
char* fun_name;
int numofparas;
FunNode* func;
nodeType* paras;
int *size;
if (!p) return 0;
switch(p->type) {
case typeCon:
printf("\tpush\t%d\n", p->con.value);
break;
case typeId:
printf("\tpush\tfp[%d]\n",getSymbol(p->id.i)->index);
break;
case typeStr:
printf("\tpush\t\"%s\"\n",p->str.value);// " not include in con.value
break;
case typeArr:
storeOffsetInIn(p);
testOutofBoundary(p);
printf("\tpush\tfp[in]\n");
break;
case typeOpr:
switch(p->opr.oper) {
case INT:
case CHAR://Declaration
if(p->opr.oper == INT)
type = INTTYPE;
else if(p->opr.oper == CHAR)
type = CHARTYPE;
else{
printf("Compile Error(1072): Unknown indentifier type\n");
exit(-1);
}
switch(p->opr.op[0]->type){
case typeId://A variable
printf("\tpush\tsp\n");//allocate space in stack
printf("\tpush\t1\n");
printf("\tadd\t\n");
printf("\tpop\tsp\n");
break;
case typeArr://An array
size = (int *)malloc(sizeof(int) * p->opr.op[0]->arr.dimension);
for(i = 0; i < p->opr.op[0]->arr.dimension; i++){
ex(p->opr.op[0]->arr.index[i]);
}
for(i = 0; i < p->opr.op[0]->arr.dimension - 1; i++){
printf("\tmul\t\n");
}
printf("\tpush\tsp\n");
printf("\tadd\t\n");
printf("\tpop\tsp\n");
break;
default:
printf("Compile Error(1070): Unknown declaration type. <%d>\n",p->opr.op[0]->type);
exit(-1);
break;
}
break;
case CODEBLOCK:
ex(p->opr.op[0]);
ex(p->opr.op[1]);
break;
case FOR:
ex(p->opr.op[0]);
printf("L%03d:\n", lblx = lbl++);
ex(p->opr.op[1]);
printf("\tj0\tL%03d\n", lbly = lbl++);
lblz = lbl++;
push(lblz,lbly);
ex(p->opr.op[3]);
printf("L%03d:\n", lblz);
ex(p->opr.op[2]);
printf("\tjmp\tL%03d\n", lblx);
printf("L%03d:\n", lbly);
pop();
break;
case CALL:
fun_name = p->opr.op[0]->id.i;//get function name;
numofparas = 0;
/*push parameters in stack*/
for(paras = p->opr.op[1];paras != NULL;paras = paras->para.next){
storeOffsetInIn(paras->para.name);
if(paras->para.name->type == typeArr)
testOutofBoundary(paras->para.name);
printf("\tpush\tfp[in]\n");//push para in stack
numofparas++;
}
/*actually, this exist check should be done in c8.y*/
if(func = getFunc(fun_name) == NULL){
printf("Error 1051: function (%s())not declared.\n",fun_name);
exit(-1);
}
printf("\tcall\tL%03d,%d\n",func->label,numofparas);
break;
case WHILE:
printf("L%03d:\n", lbl1 = lbl++);
ex(p->opr.op[0]);
printf("\tj0\tL%03d\n", lbl2 = lbl++);
push(lbl1,lbl2);
ex(p->opr.op[1]);
printf("\tjmp\tL%03d\n", lbl1);
printf("L%03d:\n", lbl2);
pop();
break;
case DO-WHILE:
lbl1 = lbl++;
lbl2 = lbl++;
lbl3 = lbl++;
push(lbl3,lbl2);
printf("L%03d:\n",lbl1);
ex(p->opr.op[0]);
printf("L%03d:\n", lbl3);
ex(p->opr.op[1]);
printf("\tj0\tL%03d\n", lbl2);
printf("\tjmp\tL%03d\n", lbl1);
printf("L%03d:\n", lbl2);
pop();
break;
case BREAK: printf("\tjmp\tL%03d\n", top(1)); break;
case CONTINUE: printf("\tjmp\tL%03d\n", top(0)); break;
case IF:
ex(p->opr.op[0]);
if (p->opr.nops > 2) {
printf("\tj0\tL%03d\n", lbl1 = lbl++);
ex(p->opr.op[1]);
printf("\tjmp\tL%03d\n", lbl2 = lbl++);
printf("L%03d:\n", lbl1);
ex(p->opr.op[2]);
printf("L%03d:\n", lbl2);
} else {
printf("\tj0\tL%03d\n", lbl1 = lbl++);
ex(p->opr.op[1]);
printf("L%03d:\n", lbl1);
}
break;
case READ:
printf("\tgeti\n");
storeOffsetInIn(p->opr.op[0]);
if(p->opr.op[0]->type == typeArr)
testOutofBoundary(p->opr.op[0]);
printf("\tpop\tfp[in]\n");
break;
case PRINT:
ex(p->opr.op[0]);
printf("\tputi\n");
break;
case '=':
ex(p->opr.op[1]);
storeOffsetInIn(p->opr.op[0]);
if(p->opr.op[0]->type == typeArr)
testOutofBoundary(p->opr.op[0]);
printf("\tpop\tfp[in]\n");
break;
case UMINUS:
ex(p->opr.op[0]);
printf("\tneg\n");
break;
default:/*Expr*/
/*semicolon*/
ex(p->opr.op[0]);
ex(p->opr.op[1]);
switch(p->opr.oper) {
case '+': printf("\tadd\n"); break;
case '-': printf("\tsub\n"); break;
case '*': printf("\tmul\n"); break;
case '/': printf("\tdiv\n"); break;
case '%': printf("\tmod\n"); break;
case '<': printf("\tcomplt\n"); break;
case '>': printf("\tcompgt\n"); break;
case GE: printf("\tcompge\n"); break;
case LE: printf("\tcomple\n"); break;
case NE: printf("\tcompne\n"); break;
case EQ: printf("\tcompeq\n"); break;
case AND: printf("\tand\n"); break;
case OR: printf("\tor\n"); break;
}
}
}
return 0;
}
void storeOffsetInIn(nodeType* p){
int offset;//Calculate varaible offset to fp
int i,j;
printf("\tpush\t0\n");
printf("\tpop\tin\n");//in = 0
switch(p->type){
case typeId:
offset = getSymbol(p->id.i)->index;
break;
case typeArr:
offset = getSymbol(p->arr.id)->index;
for(i = 0; i < p->arr.dimension;i++){
ex(p->arr.index[i]);
for(j = i + 1; j < p->arr.dimension; j++){
printf("\tpush\t%d\n",getSymbol(p->arr.id)->size[j]);
printf("\tmul\t\n");
}
printf("\tpush\tin\n");
printf("\tadd\t\n");
printf("\tpop\tin\n");
}
break;
default:
printf("Compile Error(1072): Unknown type\n");
break;
}
printf("\tpush\t%d\n",offset);
printf("\tpush\tin\n");
printf("\tadd\n");
printf("\tpop\tin\n");
}
void setEmpty(){
if (s.data)
{
free(s.data);
s.data = NULL;
s.size = 0;
s.i = -1;
}
}
void push(int start, int end){
if(!s.data){//Didn't creat
s.data = (int (*)[2])malloc(sizeof(int [2]) * 100);
s.i = -1;
s.size = 100;
}
else if(s.i == s.size - 1){//Full, double the size
int (*new_data)[2] = (int (*)[2])malloc(sizeof(int [2]) * s.size * 2);
memcpy(new_data,s.data,sizeof(int [2]) * s.size);
free(s.data);
s.data = new_data;
s.size = s.size * 2;
//s.i remains unchange
}
s.i++;
s.data[s.i][0] = start;
s.data[s.i][1] = end;
}
int top(int index){
if(index != 0 && index != 1){
printf("Compile Error(0001):\n");
exit(-1);
}
return s.data[s.i][index];
}
int * pop(void){
if(s.i == -1){
printf("Compile Error(1060): stack is empty!\n");
exit(-1);
}
return s.data[s.i--];
}
/*Maintain Symbol Table*/
bool insertSymbolDriver(SymbolType type, nodeType* p){
int i;
int *size;
if(!p) return false;
switch(p->type){
case typeId://A variable
size = (int *)malloc(sizeof(int));
*size = 1;
return insertSymbol(p->id.i,type,1,size);//name type dimesion size
break;
case typeArr://An array
size = (int *)malloc(sizeof(int) * p->arr.dimension);
int tempsize;
for(i = 0;i < p->arr.dimension; i++){
tempsize = inter(p->arr.index[i]);
if(tempsize > 0)
size[i] = tempsize;
else{
strcat(err,"Error: size of array is zero or negative"); errtext = p->arr.id;
return false;
}
}
return insertSymbol(p->arr.id, type, p->arr.dimension, size);//last parameter: size
break;
}
}
bool insertSymbol(char * name, SymbolType type, int dimension, int *size){
SymbolNode * p, *lastNode;
int lastIndex = 0;
int i = 0;
int j = 0;
if(sn==NULL){
if(!(sn = (SymbolNode *)malloc(sizeof(SymbolNode)))){
strcat(err,"Error: Can not allocate memory for symbol "); errtext = name;
return false;
}
sn->index = 0;
sn->name = name;//Name has already been allocated enough space in bison
sn->dimension = dimension;
sn->size = size;//* Size has already been allocated enough space in ex()
sn->totalsize = 0;
for(i = 0; i < dimension ; i++){
int thissize = size[i];
for(j = i+1; j < dimension ; j++)
thissize *= size[j];
sn->totalsize += thissize;
}
sn->type = type;
sn->next = NULL;
}
else{
for(p = sn; p != NULL;p = p->next){
if (!strcmp(p->name,name))//Same symbol name
{
strcat(err,"Error: Variable redeclared"); errtext = name;
return false;
}
lastIndex += p->totalsize;
lastNode = p;
}
if(!(lastNode->next = (SymbolNode *)malloc(sizeof(SymbolNode)))){
strcat(err,"Error: Can not allocate memory for symbol "); errtext = name;
return false;
}
/*Move pointer to last node*/
lastNode = lastNode->next;
/*Copy infomation*/
lastNode->name = name;
lastNode->dimension = dimension;
lastNode->size = size;
lastNode->totalsize = 0;
for(i = 0; i < dimension ; i++){
int thissize = size[i];
for(j = i+1; j < dimension ; j++)
thissize *= size[j];
lastNode->totalsize += thissize;
}
lastNode->type = type;
lastNode->index = lastIndex;
lastNode->next = NULL;
}
return true;
}
bool isDeclared(nodeType* p){
switch(p->type){
case typeId:
if(getSymbol(p->id.i) == NULL)//symbol not exist
return false;
else
return true;
break;
case typeArr:
if(getSymbol(p->arr.id) == NULL)//symbol not exist
return false;
else
return true;
break;
}
}
SymbolNode * getSymbol(char * name){
SymbolNode * p;
for(p = sn; p != NULL;p = p->next){
if (!strcmp(p->name,name))//Same symbol name
{
return p;
}
}
return NULL;
//exit(-1);
}
bool insertFunction(SymbolType type, nodeType* p, int funid){
FunNode * pTmp, *lastNode;
if(!p) return false;
if(!fn){
if(!(fn = (FunNode *)malloc(sizeof(FunNode)))){
strcat(err,"Error: Can not allocate memory for function "); errtext = p->fun.name;
return false;
}
fn->funid = funid;
fn->name = p->fun.name;
fn->returnType = p->fun.returnType;
fn->parameter = p->fun.paraHead;
fn->label = lbl++;
fn->next = NULL;
}
else{
for(pTmp = fn; pTmp != NULL;pTmp = pTmp->next){
if (!strcmp(pTmp->name, p->fun.name))//Same symbol name
{
strcat(err,"Error: Function redeclared"); errtext = p->fun.name;
return false;
}
lastNode = pTmp;
}
if(!(lastNode->next = (FunNode *)malloc(sizeof(FunNode)))){
strcat(err,"Error: Can not allocate memory for function "); errtext = p->fun.name;
return false;
}
/*Move pointer to last node*/
lastNode = lastNode->next;
/*Copy infomation*/
lastNode->name = p->fun.name;
lastNode->funid = funid;
lastNode->returnType = p->fun.returnType;
lastNode->parameter = p->fun.paraHead;
lastNode->label = lbl++;
lastNode->next = NULL;
}
return true;
}
FunNode * getFunc(char * name){
FunNode * p;
for(p = fn; p != NULL;p = p->next){
if (!strcmp(p->name,name))
{
return p;
}
}
return NULL;
//exit(-1);
}
/**
* Test whether the index of array is out of boundary.
*/
void testOutofBoundary(nodeType* p){
int lbx;
SymbolNode * symbol;
switch(p->type){
case typeId:
symbol = getSymbol(p->id.i);
break;
case typeArr:
symbol = getSymbol(p->arr.id);
break;
default:
printf("Compile Error(1071): Unknown type.\n");
exit(-1);
break;
}
printf("\tpush\tin\n");
printf("\tpush\t%d\n",symbol->totalsize + symbol->index);
printf("\tcomplt\n");// 1 if in < upperbound
printf("\tpush\tin\n");
printf("\tpush\t%d\n",symbol->index);
printf("\tcompge\n"); // 1 if in >= lowerbound
printf("\tand\n");//if (in < upperbound && in >= lowerbound) jump
printf("\tj1\tL%03d\n",lbx = lbl++);
printf("\tpush\t\"Runtime Error(100): Index out of boundary.\"\n");
printf("\tcall\tL%03d,1\n", lel);
printf("\tend\n");
printf("L%03d:\n", lbx);
}
void c8c_init(){
lel = lbl++;//label for runtime error exception
}
/**
* Setup inbuild function for exception handler
* It should be executed for one time at the end of main function
*/
void setupInbuildFunc(){
printf("\tend\n");//end main function.
/*RuntimeException(char * msg)*/
printf("L%03d:\n",lel);
printf("\tpush\tfp[-4]\n");
printf("\tputs\n");
printf("\tret\n");
/*to be continued*/
}
/**
* Interprete Expression
*/
int inter(nodeType *p) {
if (!p) {printf("Compile Error(1010): Unknown Type\n");exit(-1);return 0;}
switch(p->type) {
case typeCon: return p->con.value;
case typeOpr:
switch(p->opr.oper) {
case UMINUS: return -inter(p->opr.op[0]);
case '+': return inter(p->opr.op[0]) + inter(p->opr.op[1]);
case '-': return inter(p->opr.op[0]) - inter(p->opr.op[1]);
case '*': return inter(p->opr.op[0]) * inter(p->opr.op[1]);
case '/': return inter(p->opr.op[0]) / inter(p->opr.op[1]);
case '%': return inter(p->opr.op[0]) % inter(p->opr.op[1]);
case '<': return inter(p->opr.op[0]) < inter(p->opr.op[1]);
case '>': return inter(p->opr.op[0]) > inter(p->opr.op[1]);
case GE: return inter(p->opr.op[0]) >= inter(p->opr.op[1]);
case LE: return inter(p->opr.op[0]) <= inter(p->opr.op[1]);
case NE: return inter(p->opr.op[0]) != inter(p->opr.op[1]);
case EQ: return inter(p->opr.op[0]) == inter(p->opr.op[1]);
case AND: return inter(p->opr.op[0]) && inter(p->opr.op[1]);
case OR: return inter(p->opr.op[0]) || inter(p->opr.op[1]);
}
}
return 0;
}