From a1574d69f8a74170c60e2995d5b7a26eeb4f2c32 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Thu, 24 Aug 2023 03:30:46 +0000 Subject: [PATCH] parser: allow immediate values to end with colon This is funky syntax, but it doesn't hurt the accuracy of a parser. Some people might prefer this to stylize references to labels. --- .gitignore | 4 +++- Makefile | 8 +++++--- parser.cpp | 26 ++++++++++++++------------ parser.hpp | 1 + 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 8664051..a39bbeb 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ scu-dsp-asm *.o *.gch *.d -test/*.s \ No newline at end of file +test/*.s +test/*.txt +*.bin \ No newline at end of file diff --git a/Makefile b/Makefile index 873befa..c72870e 100644 --- a/Makefile +++ b/Makefile @@ -15,18 +15,20 @@ SRC += stmt_string.cpp OBJ = $(patsubst %.cpp,%.o,$(SRC)) DEP = $(patsubst %.cpp,%.d,$(SRC)) -all: scu-dsp-asm +MAIN = scu-dsp-asm + +all: $(MAIN) -include $(DEP) %.o: %.cpp $(CXX) $(CXXFLAGS) -MMD -MF $(basename $<).d -c $< -o $@ -scu-dsp-asm: $(OBJ) +$(MAIN): $(OBJ) $(CXX) $(STATIC) $(LDFLAGS) $^ -o $@ clean: - rm -f *.o *.d *.gch main + rm -f *.o *.d *.gch $(MAIN) .SUFFIXES: .INTERMEDIATE: diff --git a/parser.cpp b/parser.cpp index 07c582e..111ba90 100644 --- a/parser.cpp +++ b/parser.cpp @@ -70,6 +70,14 @@ token_t parser_t::consume(enum token_t::type_t token_type, const std::string err throw error(peek(), error_message); } +expr_t * parser_t::immediate() +{ + match(hash); // optionally consume a hash + expr_t * expr = expression(); + match(colon); // optionally consume a colon + return expr; +} + expr_t * parser_t::expression() { return term(); @@ -272,8 +280,7 @@ std::optional parser_t::xyd1_bus() else throw error(peek(), "expected x-bus, y-bus, or d-bus destination operand"); } else { - match(hash); // optionally consume a hash - uimm_t<8> imm = uimm_t<8>(peek(), expression()); + uimm_t<8> imm = uimm_t<8>(peek(), immediate()); consume(comma, "expected `,`"); if (auto dest_o = d1_dest()) return {new op::mov_imm_d1_t(imm, *dest_o)}; @@ -362,8 +369,7 @@ std::optional parser_t::load() { if (match(_mvi)) { const token_t& expr_token = peek(); - match(hash); // optionally consume a hash - expr_t * expr = expression(); + expr_t * expr = immediate(); consume(comma, "expected `,`"); load::dest_t dest = parser_t::load_dest(); if (match(comma)) { @@ -508,8 +514,7 @@ std::optional parser_t::dma() if (auto length_ram_o = dma_length_ram()) { return {new dma::d0_dst_ram_t(hold, add, dst, *length_ram_o)}; } else { - match(hash); // optionally consume a hash - uimm_t<8> imm = uimm_t<8>(peek(), expression()); + uimm_t<8> imm = uimm_t<8>(peek(), immediate()); return {new dma::d0_dst_imm_t(hold, add, dst, imm)}; } } else { @@ -520,8 +525,7 @@ std::optional parser_t::dma() if (auto length_ram_o = dma_length_ram()) { return {new dma::src_d0_ram_t(hold, add, src, *length_ram_o)}; } else { - match(hash); // optionally consume a hash - uimm_t<8> imm = uimm_t<8>(peek(), expression()); + uimm_t<8> imm = uimm_t<8>(peek(), immediate()); return {new dma::src_d0_imm_t(hold, add, src, imm)}; } } @@ -551,12 +555,10 @@ std::optional parser_t::jump() if (match(_jmp)) { if (auto cond_o = jump_cond()) { consume(comma, "expected `,` after jump condition"); - match(hash); // optionally consume a hash - uimm_t<8> imm = uimm_t<8>(peek(), expression()); + uimm_t<8> imm = uimm_t<8>(peek(), immediate()); return {new jump::jmp_cond_t(*cond_o, imm)}; } else { - match(hash); // optionally consume a hash - uimm_t<8> imm = uimm_t<8>(peek(), expression()); + uimm_t<8> imm = uimm_t<8>(peek(), immediate()); return {new jump::jmp_t(imm)}; } } else diff --git a/parser.hpp b/parser.hpp index cf43082..58e6468 100644 --- a/parser.hpp +++ b/parser.hpp @@ -38,6 +38,7 @@ struct parser_t template bool match(enum token_t::type_t token_type, Targs... args); token_t consume(enum token_t::type_t token_type, const std::string error_message); + expr_t * immediate(); expr_t * expression(); expr_t * term();