-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcode.c
300 lines (230 loc) · 6.12 KB
/
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
This is a very simple c compiler written by Prof. Jenq Kuen Lee,
Department of Computer Science, National Tsing-Hua Univ., Taiwan,
Fall 1995.
This is used in compiler class.
This file contains Symbol Table Handling.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <error.h>
#include "code.h"
extern FILE *codegen;
int cur_counter = 0;
int cur_scope = 1;
int symbol_t_index = 0;
char *copys();
/*
init_symbol_table();
*/
void init_symbol_table()
{
bzero(&table[0], sizeof(struct symbol_entry)*MAX_TABLE_SIZE);
}
/*
To install a symbol in the symbol table
*/
char * install_symbol(char *s)
{
if (cur_counter >= MAX_TABLE_SIZE)
perror("Symbol Table Full");
else {
table[cur_counter].scope = cur_scope;
table[cur_counter].name = copys(s);
table[cur_counter].mode = LOCAL_MODE;
cur_counter++;
}
return s;
}
/*
To return an integer as an index of the symbol table
*/
int look_up_symbol(char *s)
{
int i;
if (cur_counter == 0) return -1;
for (i=cur_counter-1;i>=0; i--)
{
if (!strcmp(s,table[i].name)) return i;
}
return -1;
}
int look_up_symbol_kw(char* s)
{
if (symbol_t_index == 0) return -1;
for (int i = symbol_t_index - 1; i >= 0; i--) {
if (!strcmp(s, symbol_t[i].lexptr)) return i;
}
return -1;
}
/*
Pop up symbols of the given scope from the symbol table upon the
exit of a given scope.
*/
void pop_up_symbol(int scope)
{
int i;
if (cur_counter==0) return;
for (i=cur_counter-1;i>=0; i--)
{
if (table[i].scope !=scope) break;
}
if (i<0) cur_counter = 0;
cur_counter = i+1;
}
/*
Set up parameter scope and offset
*/
void set_scope_and_offset_of_param(char *s)
{
int i,j,index;
int total_args;
index = look_up_symbol(s);
if (index<0) perror("Error in function header");
else {
table[index].type = T_FUNCTION;
total_args = cur_counter -index -1;
table[index].total_args=total_args;
for (j=total_args, i=cur_counter-1;i>index; i--,j--)
{
table[i].scope= cur_scope;
table[i].offset= j;
table[i].mode = ARGUMENT_MODE;
table[i].functor_index = index;
}
}
}
/*
Set up local var offset
*/
void set_local_vars(char *functor)
{
int i,j,index,index1;
int total_locals;
index = look_up_symbol(functor);
index1 = index + table[index].total_args;
total_locals = cur_counter -index1 -1;
if (total_locals <0)
perror("Error in number of local variables");
table[index].total_locals = total_locals;
for (j = total_locals, i = cur_counter-1;j>0; i--,j--)
{
table[i].scope = cur_scope;
table[i].offset = j;
table[i].mode = LOCAL_MODE;
printf("set %s to scope %d, offset %d\n", table[i].name, table[i].scope, table[i].offset);
}
}
/*
Set GLOBAL_MODE to global variables
*/
void set_global_vars(char *s)
{
int index;
index =look_up_symbol(s);
table[index].mode = GLOBAL_MODE;
table[index].scope =1;
}
/*
To generate house-keeping work at the beginning of the function
*/
void code_gen_func_header(char *functor)
{
fprintf(codegen, "%s:\n", functor);
fprintf(codegen, " // BEGIN PROLOGUE\n");
fprintf(codegen, " sw s0, -4(sp) // save frame pointer\n");
fprintf(codegen, " addi sp, sp, -4\n");
fprintf(codegen, " addi s0, sp, 0 // set new frame pointer\n");
fprintf(codegen, " sw sp, -4(s0)\n");
fprintf(codegen, " sw s1, -8(s0)\n");
fprintf(codegen, " sw s2, -12(s0)\n");
fprintf(codegen, " sw s3, -16(s0)\n");
fprintf(codegen, " sw s4, -20(s0)\n");
fprintf(codegen, " sw s5, -24(s0)\n");
fprintf(codegen, " sw s6, -28(s0)\n");
fprintf(codegen, " sw s7, -32(s0)\n");
fprintf(codegen, " sw s8, -36(s0)\n");
fprintf(codegen, " sw s9, -40(s0)\n");
fprintf(codegen, " sw s10, -44(s0)\n");
fprintf(codegen, " sw s11, -48(s0)\n");
fprintf(codegen, " addi sp, s0, -48 // update stack pointer\n");
fprintf(codegen, " // END PROLOGUE\n");
fprintf(codegen, " \n");
}
/*
To generate global symbol vars
*/
void code_gen_global_vars()
{
int i;
for (i=0; i<cur_counter; i++)
{
if (table[i].mode == GLOBAL_MODE)
{
fprintf(codegen, " .type %s,@object\n",table[i].name);
fprintf(codegen, " .comm %s,4,4\n",table[i].name);
}
}
fprintf(codegen, " \n");
fprintf(codegen, " .ident \"NTHU Compiler Class Code Generator for RISC-V\"\n");
fprintf(codegen, " .section \"note.stack\",\"\",@progbits\n");
}
/*
To geenrate house-keeping work at the end of a function
*/
void code_gen_at_end_of_function_body(char *functor)
{
int i;
fprintf(codegen, " \n");
fprintf(codegen, " // BEGIN EPILOGUE\n");
fprintf(codegen, " lw s11, -48(s0)\n");
fprintf(codegen, " lw s10, -44(s0)\n");
fprintf(codegen, " lw s9, -40(s0)\n");
fprintf(codegen, " lw s8, -36(s0)\n");
fprintf(codegen, " lw s7, -32(s0)\n");
fprintf(codegen, " lw s6, -28(s0)\n");
fprintf(codegen, " lw s5, -24(s0)\n");
fprintf(codegen, " lw s4, -20(s0)\n");
fprintf(codegen, " lw s3, -16(s0)\n");
fprintf(codegen, " lw s2, -12(s0)\n");
fprintf(codegen, " lw s1, -8(s0)\n");
fprintf(codegen, " lw sp, -4(s0)\n");
fprintf(codegen, " addi sp, sp, 4\n");
fprintf(codegen, " lw s0, -4(sp)\n");
fprintf(codegen, " // END EPILOGUE\n");
fprintf(codegen, " \n");
fprintf(codegen, " jalr zero, 0(ra) // return\n");
}
/*******************Utility Functions ********************/
/*
* copyn -- makes a copy of a string with known length
*
* input:
* n - lenght of the string "s"
* s - the string to be copied
*
* output:
* pointer to the new string
*/
char * copyn(register int n, register char *s)
{
register char *p, *q;
p = q = calloc(1,n);
while (--n >= 0)
*q++ = *s++;
return (p);
}
/*
* copys -- makes a copy of a string
*
* input:
* s - string to be copied
*
* output:
* pointer to the new string
*/
char * copys(char *s)
{
return (copyn(strlen(s) + 1, s));
}