summaryrefslogtreecommitdiffhomepage
path: root/asm/intel64/encode.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-11-17 12:38:40 +0100
committerRoland Reichwein <mail@reichwein.it>2020-11-17 12:38:40 +0100
commit927eb99e75325164a541c2638e1e607294019381 (patch)
tree5b5476456f0f957fc7492465ff08ace54e1a9e48 /asm/intel64/encode.cpp
parentc9cb051fae190acfc36813e4a23759fb9b9c3df3 (diff)
Complete hierarchical evaluation (unittest and systemtest fixed)
Diffstat (limited to 'asm/intel64/encode.cpp')
-rw-r--r--asm/intel64/encode.cpp51
1 files changed, 27 insertions, 24 deletions
diff --git a/asm/intel64/encode.cpp b/asm/intel64/encode.cpp
index 0806b56..4648194 100644
--- a/asm/intel64/encode.cpp
+++ b/asm/intel64/encode.cpp
@@ -10,7 +10,7 @@
namespace {
-std::shared_ptr<Op> makeLoadValue(FlowGraph::Data& data)
+std::shared_ptr<Op> makeLoadValue(const FlowGraph::Data& data, const FlowGraph::Graph& graph)
{
if (data.type() != FlowGraph::DataType::Int) {
std::runtime_error("Bad type for operand: "s + std::to_string(int(data.type())));
@@ -29,15 +29,15 @@ std::shared_ptr<Op> makeLoadValue(FlowGraph::Data& data)
return makeOp("mov", Asm::Args{{Asm::Args::Register32("eax"), Asm::Args::Immediate32(immediate)}});
} else if (typeid(data_storage) == typeid(FlowGraph::TemporaryStorage)) {
- FlowGraph::TemporaryStorage& storage {dynamic_cast<FlowGraph::TemporaryStorage&>(data_storage)};
+ //FlowGraph::TemporaryStorage& storage {dynamic_cast<FlowGraph::TemporaryStorage&>(data_storage)};
- index_t index { storage.indexOfStorage()};
+ index_t index { graph.scope()->indexOfData(data)};
return makeOp("mov", Asm::Args{{Asm::Args::Register32("eax"), Asm::Args::Mem32Ptr64("rbp", int32_t(index) * -4)}});
} else
throw std::runtime_error("ICE: Unsupported type for operand data at load: "s + demangle(typeid(data_storage)));
}
-std::shared_ptr<Op> makeStoreValue(FlowGraph::Data& data)
+std::shared_ptr<Op> makeStoreValue(const FlowGraph::Data& data, const FlowGraph::Graph& graph)
{
if (data.type() != FlowGraph::DataType::Int) {
std::runtime_error("Bad type for operand: "s + std::to_string(int(data.type())));
@@ -48,15 +48,15 @@ std::shared_ptr<Op> makeStoreValue(FlowGraph::Data& data)
auto& data_storage{*data.storage()};
if (typeid(data_storage) == typeid(FlowGraph::TemporaryStorage)) {
- FlowGraph::TemporaryStorage& storage {dynamic_cast<FlowGraph::TemporaryStorage&>(data_storage)};
+ //FlowGraph::TemporaryStorage& storage {dynamic_cast<FlowGraph::TemporaryStorage&>(data_storage)};
- index_t index { storage.indexOfStorage()};
+ index_t index { graph.scope()->indexOfData(data)};
return makeOp("mov", Asm::Args{{Asm::Args::Mem32Ptr64("rbp", int32_t(index) * -4), Asm::Args::Register32("eax")}});
} else
throw std::runtime_error("ICE: Unsupported type for operand data at store: "s + demangle(typeid(data_storage)));
}
-std::shared_ptr<Op> makeAddValue(FlowGraph::Data& data)
+std::shared_ptr<Op> makeAddValue(const FlowGraph::Data& data, const FlowGraph::Graph& graph)
{
if (data.type() != FlowGraph::DataType::Int) {
std::runtime_error("Bad type for operand: "s + std::to_string(int(data.type())));
@@ -75,15 +75,15 @@ std::shared_ptr<Op> makeAddValue(FlowGraph::Data& data)
return makeOp("add", Asm::Args{{Asm::Args::Register32("eax"), Asm::Args::Immediate32(immediate)}});
} else if (typeid(data_storage) == typeid(FlowGraph::TemporaryStorage)) {
- FlowGraph::TemporaryStorage& storage {dynamic_cast<FlowGraph::TemporaryStorage&>(data_storage)};
+ //FlowGraph::TemporaryStorage& storage {dynamic_cast<FlowGraph::TemporaryStorage&>(data_storage)};
- index_t index { storage.indexOfStorage()};
+ index_t index { graph.scope()->indexOfData(data)};
return makeOp("add", Asm::Args{{Asm::Args::Register32("eax"), Asm::Args::Mem32Ptr64("rbp", int32_t(index) * -4)}});
} else
throw std::runtime_error("ICE: Unsupported type for operand data at add: "s + demangle(typeid(data_storage)));
}
-std::vector<std::shared_ptr<Chunk>> makeMulValue(FlowGraph::Data& data)
+std::vector<std::shared_ptr<Chunk>> makeMulValue(const FlowGraph::Data& data, const FlowGraph::Graph& graph)
{
if (data.type() != FlowGraph::DataType::Int) {
std::runtime_error("Bad type for operand: "s + std::to_string(int(data.type())));
@@ -105,9 +105,9 @@ std::vector<std::shared_ptr<Chunk>> makeMulValue(FlowGraph::Data& data)
makeOp("mul", Asm::Args{{Asm::Args::Register32("ebx")}})
}};
} else if (typeid(data_storage) == typeid(FlowGraph::TemporaryStorage)) {
- FlowGraph::TemporaryStorage& storage {dynamic_cast<FlowGraph::TemporaryStorage&>(data_storage)};
+ //FlowGraph::TemporaryStorage& storage {dynamic_cast<FlowGraph::TemporaryStorage&>(data_storage)};
- index_t index { storage.indexOfStorage()};
+ index_t index { graph.scope()->indexOfData(data)};
return {{makeOp("mul", Asm::Args{{Asm::Args::Mem32Ptr64("rbp", int32_t(index) * -4)}})}};
} else
throw std::runtime_error("ICE: Unsupported type for operand data at mul: "s + demangle(typeid(data_storage)));
@@ -123,6 +123,7 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment)
if (node.get()) {
auto& node_deref = *node.get();
if (typeid(node_deref) == typeid(FlowGraph::UnaryOperation)) {
+ // TODO:
FlowGraph::UnaryOperation& op {dynamic_cast<FlowGraph::UnaryOperation&>(*node)};
auto operands {op.operands()};
@@ -156,28 +157,30 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment)
auto operands {op.operands()};
if (op.type() == FlowGraph::BinaryOperationType::Add) {
- segment.push_back(makeLoadValue(operands[1]));
- segment.push_back(makeAddValue(operands[2]));
- segment.push_back(makeStoreValue(operands[0]));
+ segment.push_back(makeLoadValue(operands[1], graph));
+ segment.push_back(makeAddValue(operands[2], graph));
+ segment.push_back(makeStoreValue(operands[0], graph));
} else if (op.type() == FlowGraph::BinaryOperationType::Multiply) {
- segment.push_back(makeLoadValue(operands[1]));
- segment.append(makeMulValue(operands[2]));
- segment.push_back(makeStoreValue(operands[0]));
+ segment.push_back(makeLoadValue(operands[1], graph));
+ segment.append(makeMulValue(operands[2], graph));
+ segment.push_back(makeStoreValue(operands[0], graph));
} else
throw std::runtime_error("ICE: Asm: Unsupported binary operation type: "s + std::to_string(static_cast<int>(op.type())));
} else if (typeid(node_deref) == typeid(FlowGraph::CreateScopeOp)) {
- //FlowGraph::CreateScopeOp& op {dynamic_cast<FlowGraph::CreateScopeOp&>(*node)};
- segment.push_back(makeOp("push", Asm::Args{{Asm::Args::Register64("rbp")}}));
- segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register64("rbp"), Asm::Args::Register64("rsp")}}));
+ //FlowGraph::CreateScopeOp& op {dynamic_cast<FlowGraph::CreateScopeOp&>(*node)}; // TODO: Create stack frame
+ //segment.push_back(makeOp("push", Asm::Args{{Asm::Args::Register64("rbp")}}));
+ //segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register64("rbp"), Asm::Args::Register64("rsp")}}));
} else if (typeid(node_deref) == typeid(FlowGraph::DestroyScopeOp)) {
- //FlowGraph::DestroyScopeOp& op {dynamic_cast<FlowGraph::DestroyScopeOp&>(*node)};
- segment.push_back(makeOp("pop", Asm::Args{{Asm::Args::Register64("rbp")}}));
+ //FlowGraph::DestroyScopeOp& op {dynamic_cast<FlowGraph::DestroyScopeOp&>(*node)}; // TODO: Destroy stack frame
+ //segment.push_back(makeOp("pop", Asm::Args{{Asm::Args::Register64("rbp")}}));
- // Move eax (still present from last operation) for exit() via rdi
+ segment.push_back(makeLoadValue(graph.lastOp()->destination(), graph)); // TODO: Just get last operation result to eax for now
segment.push_back(makeOp("xor", Asm::Args{{Asm::Args::Register64("rdi"), Asm::Args::Register64("rdi")}}));
segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register32("edi"), Asm::Args::Register32("eax")}}));
+ segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register64("rax"), Asm::Args::Immediate32(60)}})); // syscall 60: exit()
+ segment.push_back(makeOp("syscall")); // rax: #syscall, rdi: exit code value
} else if (typeid(node_deref) == typeid(FlowGraph::DataNode)) {
// ignore: Immediate data is used in subsequent nodes
} else {