Skip to content

Commit 72ae017

Browse files
committed
ast_printer: emit sega assembly syntax
1 parent 5cd19c5 commit 72ae017

File tree

3 files changed

+102
-65
lines changed

3 files changed

+102
-65
lines changed

ast_printer.cpp

Lines changed: 97 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void printer_t::visit(const identifier_t * identifier) const
2323

2424
void printer_t::visit(const literal_t * literal) const
2525
{
26-
os << std::to_string(literal->value);
26+
os << "0x" << std::hex << literal->value;
2727
}
2828

2929
void printer_t::visit(const unary_t * unary) const
@@ -100,142 +100,164 @@ void printer_t::parenthesize(const std::string_view s, const expr_t * a, const e
100100
void printer_t::visit(const op::andl_t * andl) const
101101
{
102102
(void)andl;
103-
parenthesize("andl");
103+
os << "and";
104104
}
105105

106106
void printer_t::visit(const op::orl_t * orl) const
107107
{
108108
(void)orl;
109-
parenthesize("orl");
109+
os << "or";
110110
}
111111

112112
void printer_t::visit(const op::xorl_t * xorl) const
113113
{
114114
(void)xorl;
115-
parenthesize("xorl");
115+
os << "xor";
116116
}
117117

118118
void printer_t::visit(const op::add_t * add) const
119119
{
120120
(void)add;
121-
parenthesize("add");
121+
os << "add";
122122
}
123123

124124
void printer_t::visit(const op::sub_t * sub) const
125125
{
126126
(void)sub;
127-
parenthesize("sub");
127+
os << "sub";
128128
}
129129

130130
void printer_t::visit(const op::ad2_t * ad2) const
131131
{
132132
(void)ad2;
133-
parenthesize("ad2");
133+
os << "ad2";
134134
}
135135

136136
void printer_t::visit(const op::sr_t * sr) const
137137
{
138138
(void)sr;
139-
parenthesize("sr");
139+
os << "sr";
140140
}
141141

142142
void printer_t::visit(const op::rr_t * rr) const
143143
{
144144
(void)rr;
145-
parenthesize("rr");
145+
os << "rr";
146146
}
147147

148148
void printer_t::visit(const op::sl_t * sl) const
149149
{
150150
(void)sl;
151-
parenthesize("sl");
151+
os << "sl";
152152
}
153153

154154
void printer_t::visit(const op::rl_t * rl) const
155155
{
156156
(void)rl;
157-
parenthesize("rl");
157+
os << "rl";
158158
}
159159

160160
void printer_t::visit(const op::rl8_t * rl8) const
161161
{
162162
(void)rl8;
163-
parenthesize("rl8");
163+
os << "rl8";
164164
}
165165

166166
void printer_t::visit(const op::mov_ram_x_t * mov_ram_x) const
167167
{
168-
parenthesize("mov", op::x_src_string[static_cast<int>(mov_ram_x->src.value)], "x");
168+
os << "mov"
169+
<< ' '
170+
<< op::x_src_string[static_cast<int>(mov_ram_x->src.value)]
171+
<< ",x";
169172
}
170173

171174
void printer_t::visit(const op::mov_mul_p_t * mov_mul_p) const
172175
{
173176
(void)mov_mul_p;
174-
parenthesize("mov mul p");
177+
os << "mov mul,p";
175178
}
176179

177180
void printer_t::visit(const op::mov_ram_p_t * mov_ram_p) const
178181
{
179-
parenthesize("mov", op::x_src_string[static_cast<int>(mov_ram_p->src.value)], "p");
182+
os << "mov"
183+
<< ' '
184+
<< op::x_src_string[static_cast<int>(mov_ram_p->src.value)]
185+
<< ",p";
180186
}
181187

182188
void printer_t::visit(const op::mov_ram_y_t * mov_ram_y) const
183189
{
184-
parenthesize("mov", op::y_src_string[static_cast<int>(mov_ram_y->src.value)], "y");
190+
os << "mov"
191+
<< ' '
192+
<< op::y_src_string[static_cast<int>(mov_ram_y->src.value)]
193+
<< ",y";
185194
}
186195

187196
void printer_t::visit(const op::clr_a_t * clr_a) const
188197
{
189198
(void)clr_a;
190-
parenthesize("clr a");
199+
os << "clr a";
191200
}
192201

193202
void printer_t::visit(const op::mov_alu_a_t * mov_alu_a) const
194203
{
195204
(void)mov_alu_a;
196-
parenthesize("mov alu a");
205+
os << "mov alu,a";
197206
}
198207

199208
void printer_t::visit(const op::mov_ram_a_t * mov_ram_a) const
200209
{
201-
parenthesize("mov", op::y_src_string[static_cast<int>(mov_ram_a->src.value)], "a");
210+
os << "mov"
211+
<< ' '
212+
<< op::y_src_string[static_cast<int>(mov_ram_a->src.value)]
213+
<< ",a";
202214
}
203215

204216
void printer_t::visit(const op::mov_imm_d1_t * mov_imm_d1) const
205217
{
206-
parenthesize("mov",
207-
mov_imm_d1->imm.expr,
208-
op::d1_dst_string[static_cast<int>(mov_imm_d1->dst.value)]);
218+
os << "mov"
219+
<< ' ';
220+
mov_imm_d1->imm.expr->accept(this);
221+
os << ','
222+
<< op::d1_dst_string[static_cast<int>(mov_imm_d1->dst.value)];
209223
}
210224

211225
void printer_t::visit(const op::mov_ram_d1_t * mov_ram_d1) const
212226
{
213-
parenthesize("mov",
214-
op::d1_src_string[static_cast<int>(mov_ram_d1->src.value)],
215-
op::d1_dst_string[static_cast<int>(mov_ram_d1->dst.value)]);
227+
os << "mov"
228+
<< ' '
229+
<< op::d1_src_string[static_cast<int>(mov_ram_d1->src.value)]
230+
<< ','
231+
<< op::d1_dst_string[static_cast<int>(mov_ram_d1->dst.value)];
216232
}
217233

218234
void printer_t::visit(const op::control_word_t * control_word) const
219235
{
220-
os << "(control_word ";
221-
for (const auto& op : control_word->ops) {
236+
for (decltype(control_word->ops)::size_type ix = 0; ix < control_word->ops.size(); ix++) {
237+
const auto& op = control_word->ops[ix];
222238
std::visit([*this](auto&& arg){ (arg).accept(this); }, op);
223-
os << ' ';
239+
if (ix < (control_word->ops.size() - 1))
240+
os << " ";
224241
}
225-
os << ')';
226242
}
227243

228244
void printer_t::visit(const load::mvi_t * mvi) const
229245
{
230-
parenthesize("mvi", mvi->imm.expr, load::dst_string[static_cast<int>(mvi->dst.value)]);
246+
os << "mvi"
247+
<< ' ';
248+
mvi->imm.expr->accept(this);
249+
os << ','
250+
<< load::dst_string[static_cast<int>(mvi->dst.value)];
231251
}
232252

233253
void printer_t::visit(const load::mvi_cond_t * mvi_cond) const
234254
{
235-
parenthesize("mvi",
236-
mvi_cond->imm.expr,
237-
load::dst_string[static_cast<int>(mvi_cond->dst.value)],
238-
load::cond_string[static_cast<int>(mvi_cond->cond.value)]);
255+
os << "mvi"
256+
<< ' ';
257+
mvi_cond->imm.expr->accept(this);
258+
os << ','
259+
<< load::dst_string[static_cast<int>(mvi_cond->dst.value)]
260+
<< load::cond_string[static_cast<int>(mvi_cond->cond.value)];
239261
}
240262

241263
static std::string dma_hold_add(dma::hold_t hold, dma::add_t add)
@@ -246,76 +268,92 @@ static std::string dma_hold_add(dma::hold_t hold, dma::add_t add)
246268

247269
void printer_t::visit(const dma::src_d0_imm_t * src_d0_imm) const
248270
{
249-
parenthesize(dma_hold_add(src_d0_imm->hold, src_d0_imm->add),
250-
dma::src_string[static_cast<int>(src_d0_imm->src.value)],
251-
"d0",
252-
src_d0_imm->imm.expr);
271+
os << dma_hold_add(src_d0_imm->hold, src_d0_imm->add)
272+
<< ' '
273+
<< dma::src_string[static_cast<int>(src_d0_imm->src.value)]
274+
<< ','
275+
<< "d0"
276+
<< ',';
277+
src_d0_imm->imm.expr->accept(this);
253278
}
254279

255280
void printer_t::visit(const dma::d0_dst_imm_t * d0_dst_imm) const
256281
{
257-
parenthesize(dma_hold_add(d0_dst_imm->hold, d0_dst_imm->add),
258-
"d0",
259-
dma::dst_string[static_cast<int>(d0_dst_imm->dst.value)],
260-
d0_dst_imm->imm.expr);
282+
os << dma_hold_add(d0_dst_imm->hold, d0_dst_imm->add)
283+
<< ' '
284+
<< "d0"
285+
<< ','
286+
<< dma::dst_string[static_cast<int>(d0_dst_imm->dst.value)]
287+
<< ',';
288+
d0_dst_imm->imm.expr->accept(this);
261289
}
262290

263291
void printer_t::visit(const dma::src_d0_ram_t * src_d0_ram) const
264292
{
265-
parenthesize(dma_hold_add(src_d0_ram->hold, src_d0_ram->add),
266-
dma::src_string[static_cast<int>(src_d0_ram->src.value)],
267-
"d0",
268-
dma::length_ram_string[static_cast<int>(src_d0_ram->ram.value)]);
293+
os << dma_hold_add(src_d0_ram->hold, src_d0_ram->add)
294+
<< ' '
295+
<< dma::src_string[static_cast<int>(src_d0_ram->src.value)]
296+
<< ','
297+
<< "d0"
298+
<< ','
299+
<< dma::length_ram_string[static_cast<int>(src_d0_ram->ram.value)];
269300
}
270301

271302
void printer_t::visit(const dma::d0_dst_ram_t * d0_dst_ram) const
272303
{
273-
parenthesize(dma_hold_add(d0_dst_ram->hold, d0_dst_ram->add),
274-
"d0",
275-
dma::dst_string[static_cast<int>(d0_dst_ram->dst.value)],
276-
dma::length_ram_string[static_cast<int>(d0_dst_ram->ram.value)]);
304+
os << dma_hold_add(d0_dst_ram->hold, d0_dst_ram->add)
305+
<< ' '
306+
<< "d0"
307+
<< ','
308+
<< dma::dst_string[static_cast<int>(d0_dst_ram->dst.value)]
309+
<< ','
310+
<< dma::length_ram_string[static_cast<int>(d0_dst_ram->ram.value)];
277311
}
278312

279313
void printer_t::visit(const jump::jmp_t * jmp) const
280314
{
281-
parenthesize("jmp", jmp->imm.expr);
315+
os << "jmp"
316+
<< ' ';
317+
jmp->imm.expr->accept(this);
282318
}
283319

284320
void printer_t::visit(const jump::jmp_cond_t * jmp_cond) const
285321
{
286-
parenthesize("jmp",
287-
jump::cond_string[static_cast<int>(jmp_cond->cond.value)],
288-
jmp_cond->imm.expr);
322+
os << "jmp"
323+
<< ' '
324+
<< jump::cond_string[static_cast<int>(jmp_cond->cond.value)]
325+
<< ',';
326+
jmp_cond->imm.expr->accept(this);
289327
}
290328

291329
void printer_t::visit(const loop::btm_t * btm) const
292330
{
293331
(void)btm;
294-
parenthesize("btm");
332+
os << "btm";
295333
}
296334

297335
void printer_t::visit(const loop::lps_t * lps) const
298336
{
299337
(void)lps;
300-
parenthesize("lps");
338+
os << "lps";
301339
}
302340

303341
void printer_t::visit(const end::end_t * end) const
304342
{
305343
(void)end;
306-
parenthesize("end");
344+
os << "end";
307345
}
308346

309347
void printer_t::visit(const end::endi_t * endi) const
310348
{
311349
(void)endi;
312-
parenthesize("endi");
350+
os << "endi";
313351
}
314352

315353
void printer_t::visit(const nop::nop_t * nop) const
316354
{
317355
(void)nop;
318-
parenthesize("nop");
356+
os << "nop";
319357
}
320358

321359
void printer_t::visit(const assign_t * assign) const

disassemble.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ static dsp::stmt_t * decompile(uint32_t code)
2525
using namespace dsp::end;
2626
using namespace dsp::nop;
2727

28-
if (control_word_t::pred(code)) return new control_word_t(code);
28+
if (nop_t::pred(code)) return new nop_t(code);
29+
else if (control_word_t::pred(code)) return new control_word_t(code);
2930
else if (mvi_t::pred(code)) return new mvi_t(code);
3031
else if (mvi_cond_t::pred(code)) return new mvi_cond_t(code);
3132
else if (d0_dst_imm_t::pred(code)) return new d0_dst_imm_t(code);
@@ -78,8 +79,6 @@ static int run_file(char const * const input_filename)
7879

7980
int main(const int argc, char const * const argv[])
8081
{
81-
const std::string c_source("-s");
82-
8382
switch (argc) {
8483
case 2: return run_file(argv[1]);
8584
default:

stmt.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ struct nop_t : stmt_accept_t<nop_t>, ins_t<nop_t>
1919
(void)code;
2020
}
2121

22-
uint32_t mask() const { return 0xffff'ffff; }
23-
uint32_t code() const { return 0x0000'0000; }
24-
uint32_t bits() const { return 0; }
22+
static constexpr uint32_t mask() { return 0xffff'ffff; }
23+
static constexpr uint32_t code() { return 0x0000'0000; }
24+
static constexpr uint32_t bits() { return 0; }
2525
};
2626

2727
} // namespace nop

0 commit comments

Comments
 (0)