-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast_printer.cpp
371 lines (314 loc) · 7.8 KB
/
ast_printer.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "ast_printer.hpp"
#include "stmt_string.hpp"
#include "control_word.hpp"
namespace dsp {
namespace ast {
void printer_t::visit(const binary_t * binary) const
{
parenthesize((binary->oper).lexeme, binary->left, binary->right);
}
void printer_t::visit(const grouping_t * grouping) const
{
parenthesize("grouping", grouping->expr);
}
void printer_t::visit(const identifier_t * identifier) const
{
parenthesize("identifier", identifier->name.lexeme);
}
void printer_t::visit(const literal_t * literal) const
{
os << "0x" << std::hex << literal->value;
}
void printer_t::visit(const unary_t * unary) const
{
parenthesize((unary->oper).lexeme, unary->right);
}
void printer_t::parenthesize(const std::string_view s, const expr_t * a) const
{
os << '(' << s << ' ';
a->accept(this);
os << ')';
}
void printer_t::parenthesize(const std::string_view s1, const std::string_view s2, const expr_t * a) const
{
os << '(' << s1 << ' ' << s2 << ' ';
a->accept(this);
os << ')';
}
void printer_t::parenthesize(const std::string_view s1, const std::string_view s2, const std::string_view s3, const expr_t * a) const
{
os << '(' << s1 << ' ' << s2 << ' ' << s3 << ' ';
a->accept(this);
os << ')';
}
void printer_t::parenthesize(const std::string_view s1, const expr_t * a, const std::string_view s2) const
{
os << '(' << s1 << ' ';
a->accept(this);
os << ' ' << s2 << ')';
}
void printer_t::parenthesize(const std::string_view s1, const expr_t * a, const std::string_view s2, const std::string_view s3) const
{
os << '(' << s1 << ' ';
a->accept(this);
os << ' ' << s2 << ' ' << s3 << ')';
}
void printer_t::parenthesize(const std::string_view s) const
{
os << '(' << s << ')';
}
void printer_t::parenthesize(const std::string_view s1, const std::string_view s2) const
{
os << '(' << s1 << ' ' << s2 << ')';
}
void printer_t::parenthesize(const std::string_view s1, const std::string_view s2, const std::string_view s3) const
{
os << '(' << s1 << ' ' << s2 << ' ' << s3 << ')';
}
void printer_t::parenthesize(const std::string_view s1, const std::string_view s2, const std::string_view s3, const std::string_view s4) const
{
os << '(' << s1 << ' ' << s2 << ' ' << s3 << ' ' << s4 << ')';
}
void printer_t::parenthesize(const std::string_view s, const expr_t * a, const expr_t * b) const
{
os << '(' << s << ' ';
a->accept(this);
os << ' ';
b->accept(this);
os << ')';
}
// instructions
void printer_t::visit(const op::andl_t * andl) const
{
(void)andl;
os << "and";
}
void printer_t::visit(const op::orl_t * orl) const
{
(void)orl;
os << "or";
}
void printer_t::visit(const op::xorl_t * xorl) const
{
(void)xorl;
os << "xor";
}
void printer_t::visit(const op::add_t * add) const
{
(void)add;
os << "add";
}
void printer_t::visit(const op::sub_t * sub) const
{
(void)sub;
os << "sub";
}
void printer_t::visit(const op::ad2_t * ad2) const
{
(void)ad2;
os << "ad2";
}
void printer_t::visit(const op::sr_t * sr) const
{
(void)sr;
os << "sr";
}
void printer_t::visit(const op::rr_t * rr) const
{
(void)rr;
os << "rr";
}
void printer_t::visit(const op::sl_t * sl) const
{
(void)sl;
os << "sl";
}
void printer_t::visit(const op::rl_t * rl) const
{
(void)rl;
os << "rl";
}
void printer_t::visit(const op::rl8_t * rl8) const
{
(void)rl8;
os << "rl8";
}
void printer_t::visit(const op::mov_ram_x_t * mov_ram_x) const
{
os << "mov"
<< ' '
<< op::x_src_string[static_cast<int>(mov_ram_x->src.value)]
<< ",x";
}
void printer_t::visit(const op::mov_mul_p_t * mov_mul_p) const
{
(void)mov_mul_p;
os << "mov mul,p";
}
void printer_t::visit(const op::mov_ram_p_t * mov_ram_p) const
{
os << "mov"
<< ' '
<< op::x_src_string[static_cast<int>(mov_ram_p->src.value)]
<< ",p";
}
void printer_t::visit(const op::mov_ram_y_t * mov_ram_y) const
{
os << "mov"
<< ' '
<< op::y_src_string[static_cast<int>(mov_ram_y->src.value)]
<< ",y";
}
void printer_t::visit(const op::clr_a_t * clr_a) const
{
(void)clr_a;
os << "clr a";
}
void printer_t::visit(const op::mov_alu_a_t * mov_alu_a) const
{
(void)mov_alu_a;
os << "mov alu,a";
}
void printer_t::visit(const op::mov_ram_a_t * mov_ram_a) const
{
os << "mov"
<< ' '
<< op::y_src_string[static_cast<int>(mov_ram_a->src.value)]
<< ",a";
}
void printer_t::visit(const op::mov_imm_d1_t * mov_imm_d1) const
{
os << "mov"
<< ' ';
mov_imm_d1->imm.expr->accept(this);
os << ','
<< op::d1_dst_string[static_cast<int>(mov_imm_d1->dst.value)];
}
void printer_t::visit(const op::mov_ram_d1_t * mov_ram_d1) const
{
os << "mov"
<< ' '
<< op::d1_src_string[static_cast<int>(mov_ram_d1->src.value)]
<< ','
<< op::d1_dst_string[static_cast<int>(mov_ram_d1->dst.value)];
}
void printer_t::visit(const op::control_word_t * control_word) const
{
for (decltype(control_word->ops)::size_type ix = 0; ix < control_word->ops.size(); ix++) {
const auto& op = control_word->ops[ix];
std::visit([*this](auto&& arg){ (arg).accept(this); }, op);
if (ix < (control_word->ops.size() - 1))
os << " ";
}
}
void printer_t::visit(const load::mvi_t * mvi) const
{
os << "mvi"
<< ' ';
mvi->imm.expr->accept(this);
os << ','
<< load::dst_string[static_cast<int>(mvi->dst.value)];
}
void printer_t::visit(const load::mvi_cond_t * mvi_cond) const
{
os << "mvi"
<< ' ';
mvi_cond->imm.expr->accept(this);
os << ','
<< load::dst_string[static_cast<int>(mvi_cond->dst.value)]
<< load::cond_string[static_cast<int>(mvi_cond->cond.value)];
}
static std::string dma_hold_add(dma::hold_t hold, dma::add_t add)
{
return dma::hold_mode_string[static_cast<int>(hold.value)]
+ dma::add_mode_string[static_cast<int>(add.value)];
}
void printer_t::visit(const dma::src_d0_imm_t * src_d0_imm) const
{
os << dma_hold_add(src_d0_imm->hold, src_d0_imm->add)
<< ' '
<< dma::src_string[static_cast<int>(src_d0_imm->src.value)]
<< ','
<< "d0"
<< ',';
src_d0_imm->imm.expr->accept(this);
}
void printer_t::visit(const dma::d0_dst_imm_t * d0_dst_imm) const
{
os << dma_hold_add(d0_dst_imm->hold, d0_dst_imm->add)
<< ' '
<< "d0"
<< ','
<< dma::dst_string[static_cast<int>(d0_dst_imm->dst.value)]
<< ',';
d0_dst_imm->imm.expr->accept(this);
}
void printer_t::visit(const dma::src_d0_ram_t * src_d0_ram) const
{
os << dma_hold_add(src_d0_ram->hold, src_d0_ram->add)
<< ' '
<< dma::src_string[static_cast<int>(src_d0_ram->src.value)]
<< ','
<< "d0"
<< ','
<< dma::length_ram_string[static_cast<int>(src_d0_ram->ram.value)];
}
void printer_t::visit(const dma::d0_dst_ram_t * d0_dst_ram) const
{
os << dma_hold_add(d0_dst_ram->hold, d0_dst_ram->add)
<< ' '
<< "d0"
<< ','
<< dma::dst_string[static_cast<int>(d0_dst_ram->dst.value)]
<< ','
<< dma::length_ram_string[static_cast<int>(d0_dst_ram->ram.value)];
}
void printer_t::visit(const jump::jmp_t * jmp) const
{
os << "jmp"
<< ' ';
jmp->imm.expr->accept(this);
}
void printer_t::visit(const jump::jmp_cond_t * jmp_cond) const
{
os << "jmp"
<< ' '
<< jump::cond_string[static_cast<int>(jmp_cond->cond.value)]
<< ',';
jmp_cond->imm.expr->accept(this);
}
void printer_t::visit(const loop::btm_t * btm) const
{
(void)btm;
os << "btm";
}
void printer_t::visit(const loop::lps_t * lps) const
{
(void)lps;
os << "lps";
}
void printer_t::visit(const end::end_t * end) const
{
(void)end;
os << "end";
}
void printer_t::visit(const end::endi_t * endi) const
{
(void)endi;
os << "endi";
}
void printer_t::visit(const nop::nop_t * nop) const
{
(void)nop;
os << "nop";
}
void printer_t::visit(const assign_t * assign) const
{
parenthesize("assign", assign->name.lexeme, assign->value);
}
void printer_t::visit(const label_t * label) const
{
parenthesize("label", label->name.lexeme);
}
}
}