-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm_unparser.c
More file actions
executable file
·268 lines (247 loc) · 7.19 KB
/
asm_unparser.c
File metadata and controls
executable file
·268 lines (247 loc) · 7.19 KB
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
/* $Id: asm_unparser.c,v 1.30 2024/08/28 21:25:00 leavens Exp $ */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "ast.h"
#include "utilities.h"
#include "char_utilities.h"
#include "regname.h"
#include "asm_unparser.h"
// Note: newlines and indentation are only printed
// by a function which deals with a grammar that sensibly needs one printed,
// as this makes them those functions more reusable.
// Unparse prog, with output going to the file out
void unparseProgram(FILE *out, ast_program_t prog)
{
unparseTextSection(out, prog.textSection);
unparseDataSection(out, prog.dataSection);
unparseStackSection(out, prog.stackSection);
fprintf(out, ".end");
newline(out);
}
// Unparse the given AST, with output going to out
void unparseTextSection(FILE *out, ast_text_section_t ts)
{
fprintf(out, ".text ");
unparseEntryPoint(out, ts.entryPoint);
newline(out);
unparseAsmInstrs(out, ts.instrs);
}
// Unparse the given AST, with output going to out
void unparseEntryPoint(FILE *out, ast_addr_t ep)
{
unparseAddr(out, ep);
}
// Unparse the given AST, with output going to out
void unparseAddr(FILE *out, ast_addr_t addr)
{
if (addr.label != NULL) {
fprintf(out, "%s", addr.label);
} else {
fprintf(out, "%u", addr.addr);
}
}
// Unparse the given AST, with output going to out
void unparseAsmInstrs(FILE *out, ast_asm_instrs_t instrs)
{
ast_asm_instr_t *ip = instrs.instrs;
while (ip != NULL) {
unparseAsmInstr(out, *ip);
ip = ip->next;
}
}
// Unparse the given AST, with output going to out
void unparseAsmInstr(FILE *out, ast_asm_instr_t instr)
{
unparseLabelOpt(out, instr.label_opt);
unparseInstr(out, instr.instr);
newline(out);
}
// Unparse the given AST, with output going to out
void unparseLabelOpt(FILE *out, ast_label_opt_t lopt)
{
if (lopt.name == NULL) {
fprintf(out, "\t");
} else {
fprintf(out, "%s:\t", lopt.name);
}
}
// Unparse the given AST, with output going to out
void unparseInstr(FILE *out, ast_instr_t instr)
{
fprintf(out, "%s ", instr.opname);
// print any registers used
switch (instr.opcode) {
case COMP_O:
switch (instr.func) {
case NOP_F:
// no arguments, so nothing to print!
break;
case LWR_F:
fprintf(out, "%s, %s, %hd",
unparseReg(instr.reg), unparseReg(instr.reg2),
instr.offset2);
break;
case SWR_F:
fprintf(out, "%s, %hd, %s",
unparseReg(instr.reg), instr.offset,
unparseReg(instr.reg2));
break;
default: // all the rest of these instructions
fprintf(out, "%s, %hd, %s, %hd",
unparseReg(instr.reg), instr.offset,
unparseReg(instr.reg2), instr.offset2);
break;
}
break;
case OTHC_O:
switch (instr.func) {
case LIT_F:
fprintf(out, "%s, %hd, %hd",
unparseReg(instr.reg), instr.offset,
instr.immed_data.data.immed);
break;
case ARI_F: case SRI_F: case MUL_F: case DIV_F:
case CFHI_F: case CFLO_F: case JMP_F: case CSI_F:
fprintf(out, "%s, %hd",
unparseReg(instr.reg), instr.offset);
break;
case SLL_F: case SRL_F:
fprintf(out, "%s, %hd, %hu",
unparseReg(instr.reg), instr.offset,
instr.immed_data.data.uimmed);
break;
case JREL_F:
fprintf(out, "%hd", instr.immed_data.data.immed);
break;
case SYS_F:
switch (instr.immed_data.data.syscall_code) {
case exit_sc:
fprintf(out, "%hd", instr.offset);
break;
case print_str_sc: case print_char_sc: case read_char_sc:
fprintf(out, "%s, %hd", unparseReg(instr.reg), instr.offset);
break;
case start_tracing_sc: case stop_tracing_sc:
// no arguments!
break;
default:
bail_with_error("Unknown syscall_code in SYS_F case of unparseInstr!");
break;
}
break;
default:
bail_with_error("Unknown other computational opcode in unparseInstr %d", instr.opcode);
break;
}
break;
case ADDI_O:
fprintf(out, "%s, %hd, %hd",
unparseReg(instr.reg), instr.offset, instr.immed_data.data.immed);
break;
case ANDI_O: case BORI_O: case NORI_O: case XORI_O:
fprintf(out, "%s, %hd, %hx",
unparseReg(instr.reg), instr.offset, instr.immed_data.data.uimmed);
break;
case BEQ_O: case BGEZ_O: case BGTZ_O: case BLEZ_O: case BLTZ_O: case BNE_O:
fprintf(out, "%s, %hd, %hd",
unparseReg(instr.reg), instr.offset, instr.immed_data.data.immed);
break;
case JMPA_O: case CALL_O:
unparseImmediateAddress(out, instr.immed_data.data.addr);
break;
case RTN_O:
// no arguments, so nothing to print!
break;
default:
bail_with_error("Unknown opcode (%d) in unparseInstr!", instr.opcode);
break;
}
}
// Unparse the given AST, with output going to out
void unparseImmediateAddress(FILE *out, ast_addr_t addr)
{
if (addr.address_defined) {
fprintf(out, "%u", addr.addr);
} else {
fprintf(out, "%s", addr.label);
}
}
// Unparse a register, returning a string for the register's name
const char *unparseReg(reg_num_type n)
{
if (0 <= n && n < NUM_REGISTERS) {
return regname_get(n);
} else {
bail_with_error("Bad register number (%d)!", n);
return NULL; // never happens
}
}
// Unparse the given AST, with output going to out
void unparseDataSection(FILE *out, ast_data_section_t ds)
{
fprintf(out, ".data %u", ds.static_start_addr);
newline(out);
unparseStaticDecls(out, ds.staticDecls);
}
// Unparse the given AST, with output going to out
void unparseStaticDecls(FILE *out, ast_static_decls_t sds)
{
ast_static_decl_t *dcl = sds.decls;
while (dcl != NULL) {
unparseStaticDecl(out, *dcl);
dcl = dcl->next;
}
}
// Unparse the given AST, with output going to out
void unparseStaticDecl(FILE *out, ast_static_decl_t dcl)
{
unparseDataSize(out, dcl);
fprintf(out, "\t");
unparseIdent(out, dcl.ident);
fprintf(out, " ");
unparseInitializer(out, dcl.initializer);
newline(out);
}
// Unparse the data size declaration, with output going to out
void unparseDataSize(FILE *out, ast_static_decl_t dcl)
{
fprintf(out, "%s ", dcl.size_name);
if (dcl.dse == ds_string) {
fprintf(out, "[%u]", dcl.size_in_words);
}
}
// Unparse the given AST, with output going to out
void unparseIdent(FILE *out, ast_ident_t id)
{
fprintf(out, "%s", id.name);
}
// Unparse the given AST, with output going to out
void unparseInitializer(FILE *out, ast_initializer_opt_t init)
{
switch (init.kind) {
case initzlr_k_number:
fprintf(out, "= %d", init.num_value);
break;
case initzlr_k_char:
fprintf(out, "= '%s'", char_utilities_unescape_char(init.num_value));
break;
case initzlr_k_string:
fprintf(out, "= \"");
fprintf(out, "%s", char_utilities_unescape_string(init.str_value));
fprintf(out, "\"");
break;
case initzlr_k_none:
// don't print anything!
break;
default:
bail_with_error("Unknown initializer kind in unparseInitializer!");
break;
}
}
// Unparse the given AST, with output going to out
void unparseStackSection(FILE *out, ast_stack_section_t ss)
{
fprintf(out, ".stack %u", ss.stack_bottom_addr);
newline(out);
}