-
Notifications
You must be signed in to change notification settings - Fork 0
/
assem.c
276 lines (259 loc) · 8.73 KB
/
assem.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
/*
* mipscodegen.c - Functions to translate to Assem-instructions for
* the Jouette assembly language using Maximal Munch.
*/
#include "assem.h"
#include <stdio.h>
#include <stdlib.h> /* for atoi */
#include <string.h> /* for strcpy */
#include "absyn.h"
#include "errormsg.h"
#include "frame.h"
#include "symbol.h"
#include "temp.h"
#include "tree.h"
#include "util.h"
AS_targets AS_Targets(Temp_labelList labels) {
AS_targets p = checked_malloc(sizeof *p);
p->labels = labels;
return p;
}
AS_instr AS_Oper(string a, Temp_tempList d, Temp_tempList s, AS_targets j) {
AS_instr p = (AS_instr)checked_malloc(sizeof *p);
p->kind = I_OPER;
p->u.OPER.assem = a;
p->u.OPER.dst = d;
p->u.OPER.src = s;
p->u.OPER.jumps = j;
return p;
}
AS_instr AS_Label(string a, Temp_label label) {
AS_instr p = (AS_instr)checked_malloc(sizeof *p);
p->kind = I_LABEL;
p->u.LABEL.assem = a;
p->u.LABEL.label = label;
return p;
}
/**
* Like AS_Oper, but only performs data transfer
*/
AS_instr AS_Move(string a, Temp_tempList d, Temp_tempList s) {
AS_instr p = (AS_instr)checked_malloc(sizeof *p);
p->kind = I_MOVE;
p->u.MOVE.assem = a;
p->u.MOVE.dst = d;
p->u.MOVE.src = s;
return p;
}
AS_instrList AS_InstrList(AS_instr head, AS_instrList tail) {
AS_instrList p = (AS_instrList)checked_malloc(sizeof *p);
p->head = head;
p->tail = tail;
return p;
}
/* put list b at the end of list a */
AS_instrList AS_splice(AS_instrList a, AS_instrList b) {
AS_instrList p;
if (a == NULL) return b;
for (p = a; p->tail != NULL; p = p->tail)
;
p->tail = b;
return a;
}
static Temp_temp nthTemp(Temp_tempList list, int i) {
assert(list);
if (i == 0)
return list->head;
else
return nthTemp(list->tail, i - 1);
}
static Temp_label nthLabel(Temp_labelList list, int i) {
assert(list);
if (i == 0)
return list->head;
else
return nthLabel(list->tail, i - 1);
}
/* first param is string created by this function by reading 'assem' string
* and replacing `d `s and `j stuff.
* Last param is function to use to determine what to do with each temp.
*/
static void format(char *result, string assem, Temp_tempList dst,
Temp_tempList src, AS_targets jumps, Temp_map m) {
// fprintf(stdout, "a format: assem=%s, dst=%p, src=%p\n", assem, dst, src);
char *p;
int i = 0; /* offset to result string */
for (p = assem; p && *p != '\0'; p++) {
if (*p == '`') {
switch (*(++p)) {
case 's': {
int n = atoi(++p);
string s = Temp_look(m, nthTemp(src, n));
strcpy(result + i, s);
i += strlen(s);
} break;
case 'd': {
int n = atoi(++p);
string s = Temp_look(m, nthTemp(dst, n));
strcpy(result + i, s);
i += strlen(s);
} break;
case 'j':
assert(jumps);
{
int n = atoi(++p);
string s = Temp_labelstring(nthLabel(jumps->labels, n));
strcpy(result + i, s);
i += strlen(s);
}
break;
case '`':
result[i] = '`';
i++;
break;
default:
assert(0);
}
} else {
result[i] = *p;
i++;
}
}
result[i] = '\0';
}
void AS_print(FILE *out, AS_instr i, Temp_map m) {
char r[200]; /* result */
switch (i->kind) {
case I_OPER:
format(r, i->u.OPER.assem, i->u.OPER.dst, i->u.OPER.src,
i->u.OPER.jumps, m);
fprintf(out, "%s\n", r);
break;
case I_LABEL:
format(r, i->u.LABEL.assem, NULL, NULL, NULL, m);
fprintf(out, "%s:\n", r);
/* i->u.LABEL->label); */
break;
case I_MOVE: {
if ((i->u.MOVE.dst == NULL) && (i->u.MOVE.src == NULL)) {
char *src = strchr(i->u.MOVE.assem, '%');
if (src != NULL) {
char *dst = strchr(src + 1, '%');
if (dst != NULL) {
// fprintf(out, "src: %s; dst: %s\n", src, dst);
if ((src[1] == dst[1]) && (src[2] == dst[2]) &&
(src[3] == dst[3]))
break;
}
}
}
format(r, i->u.MOVE.assem, i->u.MOVE.dst, i->u.MOVE.src, NULL, m);
fprintf(out, "%s\n", r);
break;
}
}
}
/* c should be COL_color; temporarily it is not */
void AS_printInstrList(FILE *out, AS_instrList iList, Temp_map m) {
for (; iList; iList = iList->tail) {
AS_print(out, iList->head, m);
}
fprintf(out, "\n");
}
AS_proc AS_Proc(string p, AS_instrList b, string e) {
AS_proc proc = checked_malloc(sizeof(*proc));
proc->prolog = p;
proc->body = b;
proc->epilog = e;
return proc;
}
AS_instrList AS_rewrite(AS_instrList iList, Temp_map m) {
AS_instrList prev = NULL;
AS_instrList list = iList;
for (AS_instrList i = iList; i; i = i->tail) {
AS_instr instr = i->head;
if (instr->kind == I_MOVE) {
Temp_temp src = instr->u.MOVE.src->head;
Temp_temp dst = instr->u.MOVE.dst->head;
if (Temp_look(m, src) == Temp_look(m, dst)) {
if (prev) {
prev->tail = i->tail;
continue;
} else {
list = i->tail;
}
}
} else if (instr->kind == I_OPER &&
strncmp("jmp", instr->u.OPER.assem, 3) == 0) {
// remove redundant jump: jump instruction followed by target label
if (i->tail) {
AS_instr next = i->tail->head;
if (next->kind == I_LABEL &&
next->u.LABEL.label == instr->u.OPER.jumps->labels->head) {
i = i->tail;
if (prev) {
prev->tail = i->tail;
continue;
} else {
list = i->tail;
}
}
}
}
prev = i;
}
return list;
}
AS_instrList AS_rewriteSpill(F_frame f, AS_instrList il, Temp_tempList spills) {
for (; spills; spills = spills->tail) {
Temp_temp t = spills->head;
F_access acc = F_allocLocal(f, TRUE);
AS_instrList i = il;
int id = Temp_getnum(t);
Temp_temp newTemp = Temp_newtemp();
for (; i; i = i->tail) {
AS_instr instr = i->head;
AS_print2(stdout, instr);
if (instr->kind == I_LABEL) continue;
Temp_tempList dst = NULL, src = NULL;
if (instr->kind == I_OPER) {
dst = instr->u.OPER.dst;
src = instr->u.OPER.src;
} else {
dst = instr->u.MOVE.dst;
src = instr->u.MOVE.src;
}
if (listLook(src, t)) {
char buf[256];
sprintf(buf, "movq %d(`s0), `d0 # spill:use%d",
F_getOffset(acc), id);
AS_instr newInstr =
AS_Oper(String(buf), Temp_TempList(newTemp, NULL),
Temp_TempList(F_FP(), NULL), NULL);
Temp_tempReplace(src, t, newTemp);
i->tail = AS_InstrList(i->head, i->tail);
i->head = newInstr;
i = i->tail;
}
if (listLook(dst, t)) {
char buf[256];
sprintf(buf, "movq `s0, %d(`s1) # spill:def%d",
F_getOffset(acc), id);
AS_instr newInstr = AS_Oper(
String(buf), NULL,
Temp_TempList(newTemp, Temp_TempList(F_FP(), NULL)), NULL);
Temp_tempReplace(dst, t, newTemp);
i->tail = AS_InstrList(newInstr, i->tail);
i = i->tail;
}
}
}
return il;
}
void AS_print2(FILE *f, AS_instr instr) {
AS_print(f, instr, Temp_layerMap(F_tempMap, Temp_name()));
}
void printCfgInfo(void *p) {
AS_instr ins = (AS_instr)p;
AS_print(stdout, ins, Temp_layerMap(F_tempMap, Temp_name()));
}