-
Notifications
You must be signed in to change notification settings - Fork 0
/
exterior.c
202 lines (190 loc) · 4.49 KB
/
exterior.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
Operand new_temp(){
static int used_no = 0;
Operand newop = (Operand)malloc(sizeof(struct Operand_));
memset(newop,0,sizeof(struct Operand_);
sprintf(newop->u.ID,"t%d",used_no);
used_no++;
return newop;
}
Operand new_lable(){
static int used_label = 1;
Operand newLabel = (Operand)malloc(sizeof(struct Operand_));
memset(newLabel,0,sizeof(struct Operand_);
sprintf(newLabe->u.ID,"t%d",used_label);
used_label++;
return newLabel;
}
void printCode(struct InterCodes *root){
while (root != NULL){
switch (root->code.kind){
case ASSIGN:
printOperand(root->code.u.assign.left);
printf(":= ");
printOperand(root->code.u.assign.right);
printf("\n");
break;
case ADD:
printOperand(root->code.u.binop.result);
printf(":= ");
printOperand(root->code.u.binop.op1);
printf("+ ");
printOperand(root->code.u.binop.op2);
printf("\n");
break;
case SUB:
printOperand(root->code.u.binop.result);
printf(":= ");
printOperand(root->code.u.binop.op1);
printf("- ");
printOperand(root->code.u.binop.op2);
printf("\n");
break;
case MUL:
printOperand(root->code.u.binop.result);
printf(":= ");
printOperand(root->code.u.binop.op1);
printf("* ");
printOperand(root->code.u.binop.op2);
printf("\n");
break;
case DIV:
printOperand(root->code.u.binop.result);
printf(":= ");
printOperand(root->code.u.binop.op1);
printf("/ ");
printOperand(root->code.u.binop.op2);
printf("\n");
break;
case MINUS:;
printf("occur MINUS \n");
break;
case IF:
printf("IF ");
printOperand(root->code.u.ifcode.op1);
printf("%s ",root->code.u.ifcode.op_type);
printOperand(root->code.u.ifcode.op2);
printf(" GOTO ");
printOperand(root->code.u.ifcode.label);
printf("\n");
break;
case GOTO:
printf("GOTO ");
printOperand(root->code.u.gotocode.label);
printf("\n");
break;
case LABEL:
printf("LABEL ");
printOperand(root->code.u.labelocode.label);
printf("\n");
break;
case RETURN:
printf("RETURN ");
printOperand(root->code.u.returncode.r);
printf("\n");
break;
case READ:
printf("READ ");
printOperand(root->code.u.read.rd);
printf("\n");
break;
case WRITE:
printf("WRITE ");
printOperand(root->code.u.write.wr);
printf("\n");
break;
case ARG:
printf("ARG ");
printOperand(root->code.u.arg.argument);
break;
case CALL:
printOperand(root->code.u.call.left);
printf(":= CALL ");
printOperand(root->code.u.call.function);
printf("\n");
break;
case DEC:
printf("DEC ")£»
printOperand(root->code.u.dec.space);
printf("%d ",root->code.u.dec.size);
printf("\n");
break;
case FUNCTION:
printf("FUNCTION ");
printf("%s :",root->code.u.function.func);
printf("\n");
break;
case PARAM:
printf("PARAM ");
printOperand(root->code.u.param.op);
printf("\n");
case NONE:
break;
default : printf("undifined op\n");
}
root = root->next;
}
}
void printOperand(Operand op){
switch (op->kind){
case VARIABLE:
printf("%s ",op->u.ID);
break;
case CONSTANT:
printf("#%d ",op->u.value);
break;
case ADDRESS:
printf("&%s ",op->u.ID);
break;
case REFERENCE:
printf("*%s ",op->u.ID);
break;
case GOTOOP:
printf("%s ",op->u.ID);
break;
case LABELOP:
printf("%s ",op->u.ID);
break;
case RETURNOP:
printf("%s ",op->u.ID);
break;
}
}
int dec_size(Type item){
if (item->kind == basic)
return 4;
else if (item->kind == array)
return item->u.array.size * dec_size(item->u.array.elem);
else if (item->kind == variable ){
int total = 0;
FieldList p = item->u.variable;
while ( p != NULL)
total += dec_size(p->type);
p = p->tail;
}
return total;
}
return -1;
}
int index_size(FieldList father,char *memberName){
int index = 0;
while (strcmp(father->name,memberName) != 0){
index += dec_size(father->type);
father = father->tail;
}
return index;
}
int newlookupTable(char *name,FieldList *t,int way){
if (lookupTable(name,t,way) == 1 )
return 1;
else{
FieldList temp = pFunc->type->u.function->tail;
while(temp != NULL){
if(strcmp(temp->name,name) == 0){
*t = temp;
return 0;
}
temp = temp->tail;
}
}
return -1;
}