-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGen_code.c
274 lines (234 loc) · 5.33 KB
/
Gen_code.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "state.h"
#include "ylib.h"
/*blockÕ»²Ù×÷*/
int Stack[STACK_SIZE];
int *top = NULL;
int *bottom = NULL;
void initStack()
{
top = Stack;
bottom = Stack;
}
void push(int num)
{
top ++;
if(top == (bottom + STACK_SIZE))
{
printf("STACK FULL!\n");
exit(1);
}
*top = num;
}
int pop()
{
int result = 0;
if(top == bottom)
{
printf("STACK FULL!\n");
}
else
{
result = *top;
top--;
}
return result;
}
int searchStack(int num)
{
int * current = top;
while(current != bottom)
{
if(num == *current--)
return YES;
}
return NO;
}
/***********************Éú³ÉÖмä´úÂë***************************/
struct CodeNode *initCode(struct CodeNode* h)
{
char sentence[20] = "BEGIN";
h = addCode(h,sentence,01,NEXT);
printf("Intermediate code generation begins.\n");
return h;
}
struct CodeNode* gencode(struct CodeNode* h,int addr,char* LEFT,char*OP,char* RIGHT)
{
char sentence[200]="";
char *blank = " ";
strcat(sentence,LEFT);
strcat(sentence,blank);
strcat(sentence,OP);
strcat(sentence,blank);
strcat(sentence,RIGHT);
h = addCode(h,sentence,addr,NEXT);
return h;
}
struct CodeNode* emit(struct CodeNode* h,int addr,char*RESULT, char* OP1, char*LEFT, char* OP2,char*RIGHT)
{
char sentence[200]="";
char *blank = " ";
strcat(sentence,RESULT);
strcat(sentence,blank);
strcat(sentence,OP1);
strcat(sentence,blank);
strcat(sentence,LEFT);
strcat(sentence,blank);
strcat(sentence,OP2);
strcat(sentence,blank);
strcat(sentence,RIGHT);
h = addCode(h,sentence,addr,NEXT);
return h;
}
struct CodeNode* genControlCode(struct CodeNode* h,int bool_e,int addr)
{
char sentence[200]="";
char* pre = "if &";
char*mid = " goto ";
char num[5];
sprintf(num,"%d",bool_e);
strcat(sentence,pre);
strcat(sentence,num);
strcat(sentence,mid);
h = addCode(h,sentence,addr,NEXT);
return h;
}
struct CodeNode* genGotoCode(struct CodeNode* h,int addr)
{
char sentence[200]="";
char*mid = "goto ";
strcat(sentence,mid);
h = addCode(h,sentence,addr,NEXT);
return h;
}
struct CodeNode* genFunCode(struct CodeNode* h,int addr,char* name)
{
char sentence[200]="";
char*mid = "FUNCTION &";
strcat(sentence,mid);
strcat(sentence,name);
h = addCode(h,sentence,addr,NEXT);
return h;
}
struct CodeNode* genReturnCode(struct CodeNode* h,int addr,char* p_addr)
{
char sentence[20] = "RETURN ";
strcat(sentence,p_addr);
h = addCode(h,sentence,addr,NEXT);
return h;
}
void editCode(struct CodeNode* h,int goto_addr)
{
struct CodeNode *temp = h;
h->_goto = goto_addr;
}
struct CodeNode* addCode(struct CodeNode* h,char* sentence,int addr,int _goto)
{
struct CodeNode* newCodeNode= NULL;
struct CodeNode* temp = h;
newCodeNode = (struct CodeNode*)malloc(sizeof(struct CodeNode));
if(newCodeNode == NULL)
{
printf("fail in creating code node .\n");
exit(0);
}
strcpy(newCodeNode->sentence,sentence);
newCodeNode->addr = addr;
newCodeNode->_goto = _goto;
newCodeNode->next = NULL;
if(h == NULL)
h = newCodeNode;
else
{
while(temp->next != NULL)
temp = temp->next;
temp->next = newCodeNode;
}
return h;
}
int widen(int left,int right,int line)
{
if(left == INT || left == CONST_INT)
{
if(right == INT || right == CONST_INT)
{
return right;
}
else if(right == FLOAT || right == CONST_FLOAT)
{
printf("Warning: Line:%d type \'int\' is converting to \'float\'\n",line);
return right;
}
else
{
printf("Warning: Line:%d type \'char\' is converting to \'int\'\n",line);
return left;
}
}
else if(left == FLOAT || left == CONST_FLOAT)
{
if(right == INT || right == CONST_INT)
{
printf("Warning: Line:%d type \'int\' is converting to \'float\'\n",line);
}
else if(right == CHAR || right == CONST_CHAR)
{
printf("Warning: Line:%d type \'char\' is converting to \'float\'\n",line);
}
return left;
}
else if(left == CHAR || left == CONST_CHAR)
{
if(right == INT || right == CONST_INT)
{
printf("Warning: Line:%d type \'char\' is converting to \'int\'\n",line);
}
else if(right == FLOAT || right == CONST_FLOAT)
{
printf("Warning: Line:%d type \'char\' is converting to \'float\'\n",line);
}
return right;
}
}
void printCode(struct CodeNode* h)
{
struct CodeNode* p = h;
printf("------------------------Intermediate code -----------------------\n");
while(p != NULL)
{
char* c = p->sentence;
printf("%d : ",p->addr);
while(*c != '\0')
printf("%c",*c++);
if(p->_goto != NEXT)
printf("%d ",p->_goto);
printf("\n");
p = p->next;
}
}
void writeCode(struct CodeNode* h)
{
printf("\n----------------------------------\n");
printf("Intemediate Code is generating. ....\n");
struct CodeNode* p = h;
FILE * oc = NULL;
if ( ( oc = fopen("objcode.s","w+") ) == NULL)
{
printf("YuiSem->Error: Create file fail.\n");
exit(0);
}
while(p != NULL)
{
char* c = p->sentence;
fprintf(oc,"%d : ",p->addr);
while(*c != '\0')
fprintf(oc,"%c",*c++);
if(p->_goto != NEXT)
fprintf(oc,"%d",p->_goto);
fprintf(oc,"\r\n");
p = p->next;
}
printf("Done ^^\n");
}