diff options
Diffstat (limited to 'cpp.cpp')
-rw-r--r-- | cpp.cpp | 22 |
1 files changed, 17 insertions, 5 deletions
@@ -315,6 +315,7 @@ bool CPP::childTypesOfChildMatch(index_t index, index_t child_index, const std:: return childTypesOfNodeMatch(m_nodes[index].child_ids[child_index], pattern); } +// Get value for specified node, at bnf rule index child_index std::any CPP::getValue(index_t node_id, index_t child_index) { size_t num_values_on_top {m_nodes[node_id].child_ids.size()}; @@ -506,9 +507,19 @@ std::unordered_map<std::string, std::function<std::any(index_t)>> CPP::getNodeEv { "expression", [&](index_t index) -> std::any { if (childTypesOfNodeMatch(index, {"assignment-expression"})) { - std::shared_ptr<FlowGraph::Node> node {std::any_cast<std::shared_ptr<FlowGraph::Node>>(getValue(index, 0))}; - mCPPContext.graph.push_back(node); - return getValue(index, 0); + if (getValue(index, 0).type() == typeid(FlowGraph::Data)) { // got Data -> make trivial Node out of it and return it + FlowGraph::LocalScope scope; // TODO: move to context! + FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; + FlowGraph::Data source {std::any_cast<FlowGraph::Data>(getValue(index, 0))}; + + std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::UnaryOperation>(FlowGraph::UnaryOperationType::Store, destination, source)}; + mCPPContext.graph.push_back(node); + return node; + } else { + std::shared_ptr<FlowGraph::Node> node {std::any_cast<std::shared_ptr<FlowGraph::Node>>(getValue(index, 0))}; + mCPPContext.graph.push_back(node); + return getValue(index, 0); + } } throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } @@ -592,9 +603,10 @@ void CPP::link() mCode = std::vector<uint8_t>{ 0x48, 0xc7, 0xc0, 0x3c, 0x00, 0x00, 0x00, // mov $0x3c,%rax # syscall 60 0x48, 0x31, 0xff, // xor %rdi,%rdi # exit code 0 - } + mSegment.getCode() + std::vector<uint8_t>{ // add to edi + } + mSegment.getCode() + std::vector<uint8_t>{ // # leave exit code in edi 0x0f, 0x05, // syscall - }; + } + ; } // phases of translation, according to standard |