summaryrefslogtreecommitdiffhomepage
path: root/cpp.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-11-14 22:06:10 +0100
committerRoland Reichwein <mail@reichwein.it>2020-11-14 22:06:10 +0100
commit9e7f4c9d43b310c280cd6432cd4150411f4b914e (patch)
treec7be57a91602c228f05ff1cab2f186dcc6635733 /cpp.cpp
parent009e450626194299ee4b5ccb8463ac64e127fde6 (diff)
Added system tests
Diffstat (limited to 'cpp.cpp')
-rw-r--r--cpp.cpp22
1 files changed, 17 insertions, 5 deletions
diff --git a/cpp.cpp b/cpp.cpp
index 0b078ee..887dbbd 100644
--- a/cpp.cpp
+++ b/cpp.cpp
@@ -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