From d0f984909842ef7009bb416666386423d6d2a441 Mon Sep 17 00:00:00 2001 From: ChUrl Date: Tue, 21 Mar 2023 18:35:44 +0100 Subject: [PATCH] Fix unnecessary instruction when MOV source and target is equal, add MOV to/from output/input --- src/parser/Parser.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/parser/Parser.cpp b/src/parser/Parser.cpp index c0fb759..6e00c61 100644 --- a/src/parser/Parser.cpp +++ b/src/parser/Parser.cpp @@ -45,20 +45,37 @@ void Parser::mov() { uint8_t source = 0; // Load from reg0 if (peek().getType() == Token::NUMBER) { ast->addChild(std::move(std::make_unique(static_cast(peek())))); // Load constant to reg0 - } else if (peek().getType() == Token::IDENTIFIER && static_cast(peek().subtoken(0, 3)) == "reg") { - source = static_cast(peek().subtoken(3, 1)); + } else if (peek().getType() == Token::IDENTIFIER) { + if (static_cast(peek().subtoken(0, 3)) == "reg") { + source = static_cast(peek().subtoken(3, 1)); + } else if (static_cast(peek()) == "input") { + source = 6; + } } else { throw "Parser Error: Expected Constant or Register!"; } get(); // Eat source - if (peek().getType() != Token::IDENTIFIER || static_cast(peek().subtoken(0, 3)) != "reg") { + uint8_t target = 0; + if (peek().getType() == Token::IDENTIFIER) { + if (static_cast(peek().subtoken(0, 3)) == "reg") { + target = static_cast(peek().subtoken(3, 1)); + } else if (static_cast(peek()) == "output") { + target = 6; + } + } else { throw "Parser Error: Expected Register!"; } - auto target = static_cast(peek().subtoken(3, 1)); get(); // Eat target - ast->addChild(std::move(std::make_unique(source, target))); + if (source > 6 || target > 6) { + throw "Parser Error: Invalid Register!"; + } + + if (source != target) { + // This happens on e.g. MOV 10, reg0 + ast->addChild(std::move(std::make_unique(source, target))); + } } void Parser::alu() { @@ -80,9 +97,11 @@ void Parser::alu() { } void Parser::jmp() { - std::map jmpMap = {{"JEQ", JumpNode::EQUAL_ZERO}, + std::map jmpMap = {{"JMP", JumpNode::ALWAYS}, + {"JEQ", JumpNode::EQUAL_ZERO}, {"JLE", JumpNode::LESS_ZERO}, {"JLEQ", JumpNode::LESS_EQUAL_ZERO}, + {"NOP", JumpNode::NEVER}, // TODO: ? {"JNEQ", JumpNode::NOT_ZERO}, {"JGR", JumpNode::GREATER_ZERO}, {"JGEQ", JumpNode::GREATER_EQUAL_ZERO}};