diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | TODO | 4 | ||||
-rw-r--r-- | asm/intel64/encode.cpp | 8 | ||||
-rw-r--r-- | cpp.cpp | 43 | ||||
-rw-r--r-- | flowgraph/node.h | 8 |
5 files changed, 52 insertions, 13 deletions
@@ -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) @@ -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<int>(FlowGraph::BinaryOperationType::Multiply))); } catch (const std::bad_cast& ex) { std::runtime_error("ICE: Encoding: Unsupported node: "s + ex.what()); @@ -361,6 +361,8 @@ std::unordered_map<std::string, std::function<std::any(index_t)>> 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<std::string, std::function<std::any(index_t)>> 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<FlowGraph::Data>(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast<FlowGraph::Data>(getValue(index, 1))}; + + std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(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<FlowGraph::Data>(getValue(index, 0))}; - FlowGraph::Data value1 {std::any_cast<FlowGraph::Data>(getValue(index, 1))}; - - std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(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<FlowGraph::Data>(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast<FlowGraph::Data>(getValue(index, 1))}; + + std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(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, |