From fe063834e53e856823b9a42ad3a5e04153446849 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 9 Nov 2020 16:41:07 +0100 Subject: Support multiply (WIP) --- Makefile | 2 +- TODO | 4 ++++ asm/intel64/encode.cpp | 8 +++++++- cpp.cpp | 43 +++++++++++++++++++++++++++++++++++-------- flowgraph/node.h | 8 +++++--- 5 files changed, 52 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index db3c1bb..8cc8c0f 100644 --- a/Makefile +++ b/Makefile @@ -91,7 +91,7 @@ TESTSRC=\ SRC=$(PROGSRC) mcc.cpp all: test-$(PROJECTNAME) mcc - ./test-$(PROJECTNAME) --gtest_filter='CppTest.compile' + ./test-$(PROJECTNAME) --gtest_filter='CppTest.compile_2_times' # testsuite ---------------------------------------------- test-$(PROJECTNAME): $(TESTSRC:.cpp=.o) diff --git a/TODO b/TODO index f563810..cd92e4c 100644 --- a/TODO +++ b/TODO @@ -1,2 +1,6 @@ +asm/: mul +cpp.cpp: eval w/ Data vs. Node +encode.cpp: support temporaries + Support/test empty compile unit grammer.cpp: match() : return point of match error ("Compile error") diff --git a/asm/intel64/encode.cpp b/asm/intel64/encode.cpp index 8bf33c0..702efaf 100644 --- a/asm/intel64/encode.cpp +++ b/asm/intel64/encode.cpp @@ -58,7 +58,13 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) segment.push_back(makeOp("mov", args1)); Asm::Args args2{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate2)}}; - segment.push_back(makeOp("add", args2)); + + if (op.type() == FlowGraph::BinaryOperationType::Add) + segment.push_back(makeOp("add", args2)); + else if (op.type() == FlowGraph::BinaryOperationType::Multiply) + segment.push_back(makeOp("mul", args2)); + else + throw std::runtime_error("ICE: Asm: Unsupported binary operation type: "s + std::to_string(static_cast(FlowGraph::BinaryOperationType::Multiply))); } catch (const std::bad_cast& ex) { std::runtime_error("ICE: Encoding: Unsupported node: "s + ex.what()); diff --git a/cpp.cpp b/cpp.cpp index e4d935a..ce4a7c5 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -361,6 +361,8 @@ std::unordered_map> CPP::getNodeEv { if (childTypesOfNodeMatch(index, {"literal"})) return getValue(index, 0); + if (childTypesOfNodeMatch(index, {"(", "expression", ")"})) + return getValue(index, 1); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, @@ -394,21 +396,46 @@ std::unordered_map> CPP::getNodeEv }, { "multiplicative-expression", [&](index_t index) -> std::any { + if (childTypesOfNodeMatch(index, {"pm-expression", "multiplicative-expression-EXT"})) { + if (childTypesOfChildMatch(index, 1, {"*", "", ""})) { + FlowGraph::LocalScope scope; // TODO: move to context! + FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; + FlowGraph::Data value0 {std::any_cast(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast(getValue(index, 1))}; + + std::shared_ptr node {std::make_shared(FlowGraph::BinaryOperationType::Multiply, destination, value0, value1)}; + return node; + } + } if (childTypesOfNodeMatch(index, {"pm-expression", ""}) && !getValue(index, 1).has_value()) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, + { "multiplicative-expression-EXT", [&](index_t index) -> std::any + { + if (childTypesOfNodeMatch(index, {"*", "pm-expression", ""}) && !getValue(index, 2).has_value()) { + return getValue(index, 1); + } else if (childTypesOfNodeMatch(index, {})) { + return {}; + } + throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO + } + }, { "additive-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"multiplicative-expression", "additive-expression-EXT"}) && childTypesOfChildMatch(index, 1, {"+", "", ""})) { - FlowGraph::LocalScope scope; // TODO: move to context! - FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; - FlowGraph::Data value0 {std::any_cast(getValue(index, 0))}; - FlowGraph::Data value1 {std::any_cast(getValue(index, 1))}; - - std::shared_ptr node {std::make_shared(FlowGraph::BinaryOperationType::Add, destination, value0, value1)}; - return node; + if (childTypesOfNodeMatch(index, {"multiplicative-expression", "additive-expression-EXT"})) { + if (childTypesOfChildMatch(index, 1, {"+", "", ""})) { + FlowGraph::LocalScope scope; // TODO: move to context! + FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; + FlowGraph::Data value0 {std::any_cast(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast(getValue(index, 1))}; + + std::shared_ptr node {std::make_shared(FlowGraph::BinaryOperationType::Add, destination, value0, value1)}; + return node; + } else if (!getValue(index, 1).has_value()) { + return getValue(index, 0); + } } throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } diff --git a/flowgraph/node.h b/flowgraph/node.h index d4a1306..9ae5479 100644 --- a/flowgraph/node.h +++ b/flowgraph/node.h @@ -63,7 +63,7 @@ namespace FlowGraph { {} }; - enum class JumpVariant + enum class JumpVariant: int { Unconditional, GT, @@ -110,7 +110,8 @@ namespace FlowGraph { {} }; - enum class UnaryOperationType { + enum class UnaryOperationType: int + { Increment, Decrement, Negate @@ -128,7 +129,8 @@ namespace FlowGraph { UnaryOperationType m_type; }; - enum class BinaryOperationType { + enum class BinaryOperationType: int + { Add, Subtract, Multiply, -- cgit v1.2.3