From 22625c7c9034df2d47783112595240a1adcbcf25 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Sat, 19 Aug 2023 19:40:20 -0700 Subject: [PATCH] ast: add printers for all instructions --- Makefile | 3 +- ast.cpp | 183 +++++++++++++++++++++++++++++++++++++++++++++++- ast.hpp | 13 +++- parser.cpp | 2 +- stmt.hpp | 35 +++++---- stmt_string.cpp | 7 +- stmt_string.hpp | 1 + visitable.hpp | 2 +- visitor.hpp | 2 +- 9 files changed, 224 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 8aafc0b..5f42fb0 100644 --- a/Makefile +++ b/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)) diff --git a/ast.cpp b/ast.cpp index e3079d4..a45e010 100644 --- a/ast.cpp +++ b/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(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(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(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(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(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(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(mov_ram_d1->src)], + op::d1_dest_string[static_cast(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(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(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(mvi_cond->dest)], + load::cond_string[static_cast(mvi_cond->cond)]); +} + +static std::string dma_hold_add(bool hold, dma::add_mode_t add) +{ + return dma::hold_mode_string[static_cast(hold)] + + dma::add_mode_string[static_cast(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(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(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(ingress_ram->ingress)], + dma::length_ram_string[static_cast(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(egress_ram->egress)], + dma::length_ram_string[static_cast(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(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"); } } diff --git a/ast.hpp b/ast.hpp index 3c0bffd..39e60b0 100644 --- a/ast.hpp +++ b/ast.hpp @@ -30,9 +30,9 @@ struct ast_printer_t : visitor_t 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 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; + }; } diff --git a/parser.cpp b/parser.cpp index fb31cd8..8db5c75 100644 --- a/parser.cpp +++ b/parser.cpp @@ -300,7 +300,7 @@ std::optional parser_t::op() else break; } if (ops.size() != 0) - return {new op::instruction_t(ops)}; + return {new op::control_word_t(ops)}; else return {}; } diff --git a/stmt.hpp b/stmt.hpp index 37b2e0a..621ce34 100644 --- a/stmt.hpp +++ b/stmt.hpp @@ -122,9 +122,9 @@ struct mov_ram_d1_t : op_t, stmt_accept_t const d1_dest_t dest; }; -struct instruction_t : stmt_accept_t +struct control_word_t : stmt_accept_t { - instruction_t(std::vector ops) + control_word_t(std::vector ops) : ops(ops) {} const std::vector ops; @@ -136,18 +136,21 @@ namespace load { struct mvi_t : stmt_accept_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(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(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(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(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(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; }; diff --git a/stmt_string.cpp b/stmt_string.cpp index ddd2606..4513c96 100644 --- a/stmt_string.cpp +++ b/stmt_string.cpp @@ -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" , diff --git a/stmt_string.hpp b/stmt_string.hpp index 5bc9d9c..985a3d7 100644 --- a/stmt_string.hpp +++ b/stmt_string.hpp @@ -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[]; diff --git a/visitable.hpp b/visitable.hpp index cb2e577..25ee816 100644 --- a/visitable.hpp +++ b/visitable.hpp @@ -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{ diff --git a/visitor.hpp b/visitor.hpp index 2966ed2..402d79f 100644 --- a/visitor.hpp +++ b/visitor.hpp @@ -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;