-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassem.c
181 lines (150 loc) · 4.37 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
#include <stdio.h>
#include <stdlib.h> /* for atoi */
#include <string.h> /* for strcpy */
#include "assem.h"
#include "symbol.h"
#include "absyn.h"
#include "tree.h"
#include "frame.h"
#include "errormsg.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;
}
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) {
if (a == NULL)
return b;
AS_instrList p;
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) {
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:
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;
}