ast: add printers for all instructions
This commit is contained in:
parent
e520a0de19
commit
22625c7c90
3
Makefile
3
Makefile
@ -1,4 +1,4 @@
|
||||
CXXFLAGS = -Og -g -Wall -Wextra -Werror -Wfatal-errors -Wpedantic -std=c++20
|
||||
CXXFLAGS = -Og -g -Wall -Wextra -Werror -Wfatal-errors -Wpedantic -Wno-c99-designator -std=c++20
|
||||
LDFLAGS =
|
||||
|
||||
TARGET =
|
||||
@ -8,6 +8,7 @@ SRC = main.cpp
|
||||
SRC += lexer.cpp
|
||||
SRC += ast.cpp
|
||||
SRC += parser.cpp
|
||||
SRC += stmt_string.cpp
|
||||
OBJ = $(patsubst %.cpp,%.o,$(SRC))
|
||||
DEP = $(patsubst %.cpp,%.d,$(SRC))
|
||||
|
||||
|
183
ast.cpp
183
ast.cpp
@ -1,4 +1,5 @@
|
||||
#include "ast.hpp"
|
||||
#include "stmt_string.hpp"
|
||||
|
||||
namespace dsp {
|
||||
|
||||
@ -29,6 +30,42 @@ void ast_printer_t::parenthesize(const std::string_view s, const expr_t * a) con
|
||||
os << ')';
|
||||
}
|
||||
|
||||
void ast_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 ast_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 ast_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 ast_printer_t::parenthesize(const std::string_view s) const
|
||||
{
|
||||
os << '(' << s << ')';
|
||||
}
|
||||
|
||||
void ast_printer_t::parenthesize(const std::string_view s1, const std::string_view s2) const
|
||||
{
|
||||
os << '(' << s1 << ' ' << s2 << ')';
|
||||
}
|
||||
|
||||
void ast_printer_t::parenthesize(const std::string_view s1, const std::string_view s2, const std::string_view s3) const
|
||||
{
|
||||
os << '(' << s1 << ' ' << s2 << ' ' << s3 << ')';
|
||||
}
|
||||
|
||||
void ast_printer_t::parenthesize(const std::string_view s, const expr_t * a, const expr_t * b) const
|
||||
{
|
||||
os << '(' << s << ' ';
|
||||
@ -42,7 +79,151 @@ void ast_printer_t::parenthesize(const std::string_view s, const expr_t * a, con
|
||||
|
||||
void ast_printer_t::visit(const op::alu_t * alu) const
|
||||
{
|
||||
(void)alu;
|
||||
parenthesize(op::alu_type_string[static_cast<int>(alu->type)]);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::mov_ram_x_t * mov_ram_x) const
|
||||
{
|
||||
parenthesize("mov", op::xy_src_string[static_cast<int>(mov_ram_x->src)], "x");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::mov_mul_p_t * mov_mul_p) const
|
||||
{
|
||||
(void)mov_mul_p;
|
||||
parenthesize("mov mul p");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::mov_ram_p_t * mov_ram_p) const
|
||||
{
|
||||
parenthesize("mov", op::xy_src_string[static_cast<int>(mov_ram_p->src)], "p");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::mov_ram_y_t * mov_ram_y) const
|
||||
{
|
||||
parenthesize("mov", op::xy_src_string[static_cast<int>(mov_ram_y->src)], "y");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::clr_a_t * clr_a) const
|
||||
{
|
||||
(void)clr_a;
|
||||
parenthesize("clr a");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::mov_alu_a_t * mov_alu_a) const
|
||||
{
|
||||
(void)mov_alu_a;
|
||||
parenthesize("mov alu a");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::mov_ram_a_t * mov_ram_a) const
|
||||
{
|
||||
parenthesize("mov", op::xy_src_string[static_cast<int>(mov_ram_a->src)], "a");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::mov_imm_d1_t * mov_imm_d1) const
|
||||
{
|
||||
parenthesize("mov",
|
||||
mov_imm_d1->imm.expr,
|
||||
op::d1_dest_string[static_cast<int>(mov_imm_d1->dest)]);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::mov_ram_d1_t * mov_ram_d1) const
|
||||
{
|
||||
parenthesize("mov",
|
||||
op::d1_src_string[static_cast<int>(mov_ram_d1->src)],
|
||||
op::d1_dest_string[static_cast<int>(mov_ram_d1->dest)]);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const op::control_word_t * control_word) const
|
||||
{
|
||||
os << "(control_word ";
|
||||
for (const auto& op : control_word->ops) {
|
||||
reinterpret_cast<const stmt_t *>(op)->accept(this);
|
||||
}
|
||||
os << ')';
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const load::mvi_t * mvi) const
|
||||
{
|
||||
parenthesize("mvi", mvi->imm.expr, load::dest_string[static_cast<int>(mvi->dest)]);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const load::mvi_cond_t * mvi_cond) const
|
||||
{
|
||||
parenthesize("mvi",
|
||||
mvi_cond->imm.expr,
|
||||
load::dest_string[static_cast<int>(mvi_cond->dest)],
|
||||
load::cond_string[static_cast<int>(mvi_cond->cond)]);
|
||||
}
|
||||
|
||||
static std::string dma_hold_add(bool hold, dma::add_mode_t add)
|
||||
{
|
||||
return dma::hold_mode_string[static_cast<int>(hold)]
|
||||
+ dma::add_mode_string[static_cast<int>(add)];
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const dma::ingress_imm_t * ingress_imm) const
|
||||
{
|
||||
parenthesize(dma_hold_add(ingress_imm->hold, ingress_imm->add),
|
||||
dma::ingress_string[static_cast<int>(ingress_imm->ingress)],
|
||||
ingress_imm->imm.expr);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const dma::egress_imm_t * egress_imm) const
|
||||
{
|
||||
parenthesize(dma_hold_add(egress_imm->hold, egress_imm->add),
|
||||
dma::egress_string[static_cast<int>(egress_imm->egress)],
|
||||
egress_imm->imm.expr);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const dma::ingress_ram_t * ingress_ram) const
|
||||
{
|
||||
parenthesize(dma_hold_add(ingress_ram->hold, ingress_ram->add),
|
||||
dma::ingress_string[static_cast<int>(ingress_ram->ingress)],
|
||||
dma::length_ram_string[static_cast<int>(ingress_ram->ram)]);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const dma::egress_ram_t * egress_ram) const
|
||||
{
|
||||
parenthesize(dma_hold_add(egress_ram->hold, egress_ram->add),
|
||||
dma::egress_string[static_cast<int>(egress_ram->egress)],
|
||||
dma::length_ram_string[static_cast<int>(egress_ram->ram)]);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const jump::jmp_t * jmp) const
|
||||
{
|
||||
parenthesize("jmp", jmp->imm.expr);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const jump::jmp_cond_t * jmp_cond) const
|
||||
{
|
||||
parenthesize("jmp",
|
||||
jmp_cond->imm.expr,
|
||||
jump::cond_string[static_cast<int>(jmp_cond->cond)]);
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const loop::btm_t * btm) const
|
||||
{
|
||||
(void)btm;
|
||||
parenthesize("btm");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const loop::lps_t * lps) const
|
||||
{
|
||||
(void)lps;
|
||||
parenthesize("lps");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const end::end_t * end) const
|
||||
{
|
||||
(void)end;
|
||||
parenthesize("end");
|
||||
}
|
||||
|
||||
void ast_printer_t::visit(const end::endi_t * endi) const
|
||||
{
|
||||
(void)endi;
|
||||
parenthesize("endi");
|
||||
}
|
||||
|
||||
}
|
||||
|
13
ast.hpp
13
ast.hpp
@ -30,9 +30,9 @@ struct ast_printer_t : visitor_t<void>
|
||||
void visit(const op::clr_a_t * clr_a) const;
|
||||
void visit(const op::mov_alu_a_t * mov_alu_a) const;
|
||||
void visit(const op::mov_ram_a_t * mov_ram_a) const;
|
||||
void visit(const op::mov_imm_d1_t * mov_imm_) const;
|
||||
void visit(const op::mov_ram_d1_t * mov_ram_) const;
|
||||
void visit(const op::instruction_t * instruction) const;
|
||||
void visit(const op::mov_imm_d1_t * mov_imm_d1) const;
|
||||
void visit(const op::mov_ram_d1_t * mov_ram_d1) const;
|
||||
void visit(const op::control_word_t * control_word) const;
|
||||
|
||||
void visit(const load::mvi_t * mvi) const;
|
||||
void visit(const load::mvi_cond_t * mvi_cond) const;
|
||||
@ -52,7 +52,14 @@ struct ast_printer_t : visitor_t<void>
|
||||
void visit(const end::endi_t * endi) const;
|
||||
|
||||
void parenthesize(const std::string_view s, const expr_t * a) const;
|
||||
void parenthesize(const std::string_view s1, const std::string_view s2, const expr_t * a) const;
|
||||
void parenthesize(const std::string_view s1, const expr_t * a, const std::string_view s2) const;
|
||||
void parenthesize(const std::string_view s1, const expr_t * a, const std::string_view s2, const std::string_view s3) const;
|
||||
void parenthesize(const std::string_view s, const expr_t * a, const expr_t * b) const;
|
||||
void parenthesize(const std::string_view s) const;
|
||||
void parenthesize(const std::string_view s1, const std::string_view s2) const;
|
||||
void parenthesize(const std::string_view s1, const std::string_view s2, const std::string_view s3) const;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -300,7 +300,7 @@ std::optional<stmt_t *> parser_t::op()
|
||||
else break;
|
||||
}
|
||||
if (ops.size() != 0)
|
||||
return {new op::instruction_t(ops)};
|
||||
return {new op::control_word_t(ops)};
|
||||
else
|
||||
return {};
|
||||
}
|
||||
|
35
stmt.hpp
35
stmt.hpp
@ -122,9 +122,9 @@ struct mov_ram_d1_t : op_t, stmt_accept_t<mov_ram_d1_t>
|
||||
const d1_dest_t dest;
|
||||
};
|
||||
|
||||
struct instruction_t : stmt_accept_t<instruction_t>
|
||||
struct control_word_t : stmt_accept_t<control_word_t>
|
||||
{
|
||||
instruction_t(std::vector<const op_t *> ops)
|
||||
control_word_t(std::vector<const op_t *> ops)
|
||||
: ops(ops) {}
|
||||
|
||||
const std::vector<const op_t *> ops;
|
||||
@ -136,18 +136,21 @@ namespace load {
|
||||
|
||||
struct mvi_t : stmt_accept_t<mvi_t>
|
||||
{
|
||||
mvi_t(uimm_t<25> imm)
|
||||
: imm(imm) {}
|
||||
mvi_t(uimm_t<25> imm, dest_t dest)
|
||||
: imm(imm), dest(dest) {}
|
||||
|
||||
const uimm_t<25> imm;
|
||||
const dest_t dest;
|
||||
};
|
||||
|
||||
struct mvi_cond_t : stmt_accept_t<mvi_cond_t>
|
||||
{
|
||||
mvi_cond_t(uimm_t<19> imm)
|
||||
: imm(imm) {}
|
||||
mvi_cond_t(uimm_t<19> imm, dest_t dest, cond_t cond)
|
||||
: imm(imm), dest(dest), cond(cond) {}
|
||||
|
||||
const uimm_t<19> imm;
|
||||
const dest_t dest;
|
||||
const cond_t cond;
|
||||
};
|
||||
|
||||
} // load
|
||||
@ -156,40 +159,44 @@ namespace dma {
|
||||
|
||||
struct ingress_imm_t : stmt_accept_t<ingress_imm_t>
|
||||
{
|
||||
ingress_imm_t(bool hold, ingress_t ingress, simm_t<8> imm)
|
||||
: hold(hold), ingress(ingress), imm(imm) {}
|
||||
ingress_imm_t(bool hold, add_mode_t add, ingress_t ingress, simm_t<8> imm)
|
||||
: hold(hold), add(add), ingress(ingress), imm(imm) {}
|
||||
|
||||
const bool hold;
|
||||
const add_mode_t add;
|
||||
const ingress_t ingress;
|
||||
const simm_t<8> imm;
|
||||
};
|
||||
|
||||
struct egress_imm_t : stmt_accept_t<egress_imm_t>
|
||||
{
|
||||
egress_imm_t(bool hold, egress_t egress, simm_t<8> imm)
|
||||
: hold(hold), egress(egress), imm(imm) {}
|
||||
egress_imm_t(bool hold, add_mode_t add, egress_t egress, simm_t<8> imm)
|
||||
: hold(hold), add(add), egress(egress), imm(imm) {}
|
||||
|
||||
const bool hold;
|
||||
const add_mode_t add;
|
||||
const egress_t egress;
|
||||
const simm_t<8> imm;
|
||||
};
|
||||
|
||||
struct ingress_ram_t : stmt_accept_t<ingress_ram_t>
|
||||
{
|
||||
ingress_ram_t(bool hold, ingress_t ingress, length_ram_t ram)
|
||||
: hold(hold), ingress(ingress), ram(ram) {}
|
||||
ingress_ram_t(bool hold, add_mode_t add, ingress_t ingress, length_ram_t ram)
|
||||
: hold(hold), add(add), ingress(ingress), ram(ram) {}
|
||||
|
||||
const bool hold;
|
||||
const add_mode_t add;
|
||||
const ingress_t ingress;
|
||||
const length_ram_t ram;
|
||||
};
|
||||
|
||||
struct egress_ram_t : stmt_accept_t<egress_ram_t>
|
||||
{
|
||||
egress_ram_t(bool hold, egress_t egress, length_ram_t ram)
|
||||
: hold(hold), egress(egress), ram(ram) {}
|
||||
egress_ram_t(bool hold, add_mode_t add, egress_t egress, length_ram_t ram)
|
||||
: hold(hold), add(add), egress(egress), ram(ram) {}
|
||||
|
||||
const bool hold;
|
||||
const add_mode_t add;
|
||||
const egress_t egress;
|
||||
const length_ram_t ram;
|
||||
};
|
||||
|
@ -1,5 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#include "stmt_string.hpp"
|
||||
#include "stmt_enum.hpp"
|
||||
|
||||
@ -92,6 +90,11 @@ const std::string cond_string[] = {
|
||||
|
||||
namespace dma {
|
||||
|
||||
const std::string hold_mode_string[] = {
|
||||
[i(false)] = "dma",
|
||||
[i(true )] = "dmah",
|
||||
};
|
||||
|
||||
const std::string add_mode_string[] = {
|
||||
[i(add_mode_t::_0 )] = "0" ,
|
||||
[i(add_mode_t::_1 )] = "1" ,
|
||||
|
@ -22,6 +22,7 @@ extern const std::string cond_string[];
|
||||
|
||||
namespace dma {
|
||||
|
||||
extern const std::string hold_mode_string[];
|
||||
extern const std::string add_mode_string[];
|
||||
extern const std::string ingress_string[];
|
||||
extern const std::string egress_string[];
|
||||
|
@ -22,7 +22,7 @@ struct mov_alu_a_t;
|
||||
struct mov_ram_a_t;
|
||||
struct mov_imm_d1_t;
|
||||
struct mov_ram_d1_t;
|
||||
struct instruction_t;
|
||||
struct control_word_t;
|
||||
}
|
||||
|
||||
namespace load{
|
||||
|
@ -23,7 +23,7 @@ struct visitor_t
|
||||
virtual T visit(const op::mov_ram_a_t * mov_ram_a) const = 0;
|
||||
virtual T visit(const op::mov_imm_d1_t * mov_imm_) const = 0;
|
||||
virtual T visit(const op::mov_ram_d1_t * mov_ram_) const = 0;
|
||||
virtual T visit(const op::instruction_t * instruction) const = 0;
|
||||
virtual T visit(const op::control_word_t * control_word) const = 0;
|
||||
|
||||
virtual T visit(const load::mvi_t * mvi) const = 0;
|
||||
virtual T visit(const load::mvi_cond_t * mvi_cond) const = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user