summaryrefslogtreecommitdiffhomepage
path: root/asm/intel64/encode.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-11-16 12:48:44 +0100
committerRoland Reichwein <mail@reichwein.it>2020-11-16 12:48:44 +0100
commitc9cb051fae190acfc36813e4a23759fb9b9c3df3 (patch)
treefcd8c93cd5dc2a3272eac253b0291611e16ea13f /asm/intel64/encode.cpp
parent300219dc8519720a36525c7b40c6a327580fe0bd (diff)
Implement hierarchical evaluation (WIP)
Diffstat (limited to 'asm/intel64/encode.cpp')
-rw-r--r--asm/intel64/encode.cpp168
1 files changed, 114 insertions, 54 deletions
diff --git a/asm/intel64/encode.cpp b/asm/intel64/encode.cpp
index 681e407..0806b56 100644
--- a/asm/intel64/encode.cpp
+++ b/asm/intel64/encode.cpp
@@ -8,6 +8,113 @@
#include <exception>
+namespace {
+
+std::shared_ptr<Op> makeLoadValue(FlowGraph::Data& data)
+{
+ if (data.type() != FlowGraph::DataType::Int) {
+ std::runtime_error("Bad type for operand: "s + std::to_string(int(data.type())));
+ }
+
+ if (!data.storage())
+ throw std::runtime_error("ICE: Operand storage is 0");
+
+ auto& data_storage{*data.storage()};
+ if (typeid(data_storage) == typeid(FlowGraph::Constant)) {
+ FlowGraph::Constant& value {dynamic_cast<FlowGraph::Constant&>(data_storage)};
+ if (value.value().size() < sizeof(uint32_t))
+ throw std::runtime_error("ICE: Int data from operand needs at least 4 bytes, got "s + std::to_string(value.value().size()));
+
+ uint32_t immediate = from_little_endian(value.value());
+
+ 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)};
+
+ index_t index { storage.indexOfStorage()};
+ 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)
+{
+ if (data.type() != FlowGraph::DataType::Int) {
+ std::runtime_error("Bad type for operand: "s + std::to_string(int(data.type())));
+ }
+
+ if (!data.storage())
+ throw std::runtime_error("ICE: Operand storage is 0");
+
+ auto& data_storage{*data.storage()};
+ if (typeid(data_storage) == typeid(FlowGraph::TemporaryStorage)) {
+ FlowGraph::TemporaryStorage& storage {dynamic_cast<FlowGraph::TemporaryStorage&>(data_storage)};
+
+ index_t index { storage.indexOfStorage()};
+ 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)
+{
+ if (data.type() != FlowGraph::DataType::Int) {
+ std::runtime_error("Bad type for operand: "s + std::to_string(int(data.type())));
+ }
+
+ if (!data.storage())
+ throw std::runtime_error("ICE: Operand storage is 0");
+
+ auto& data_storage{*data.storage()};
+ if (typeid(data_storage) == typeid(FlowGraph::Constant)) {
+ FlowGraph::Constant& value {dynamic_cast<FlowGraph::Constant&>(data_storage)};
+ if (value.value().size() < sizeof(uint32_t))
+ throw std::runtime_error("ICE: Int data from operand needs at least 4 bytes, got "s + std::to_string(value.value().size()));
+
+ uint32_t immediate = from_little_endian(value.value());
+
+ 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)};
+
+ index_t index { storage.indexOfStorage()};
+ 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)
+{
+ if (data.type() != FlowGraph::DataType::Int) {
+ std::runtime_error("Bad type for operand: "s + std::to_string(int(data.type())));
+ }
+
+ if (!data.storage())
+ throw std::runtime_error("ICE: Operand storage is 0");
+
+ auto& data_storage{*data.storage()};
+ if (typeid(data_storage) == typeid(FlowGraph::Constant)) {
+ FlowGraph::Constant& value {dynamic_cast<FlowGraph::Constant&>(data_storage)};
+ if (value.value().size() < sizeof(uint32_t))
+ throw std::runtime_error("ICE: Int data from operand needs at least 4 bytes, got "s + std::to_string(value.value().size()));
+
+ uint32_t immediate = from_little_endian(value.value());
+
+ return {{
+ makeOp("mov", Asm::Args{{Asm::Args::Register32("ebx"), Asm::Args::Immediate32(immediate)}}),
+ 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)};
+
+ index_t index { storage.indexOfStorage()};
+ 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)));
+}
+
+} // namespace
+
void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment)
{
segment.clear();
@@ -48,61 +155,14 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment)
auto operands {op.operands()};
-#if 0
- if (op.type() == FlowGraph::BinaryOperationType::Add) {
- segment.push_back(loadmakeOp("add", Asm::Args{{Asm::Args::Register32("eax"), Asm::Args::Immediate32(immediate2)}}));
- } else if (op.type() == FlowGraph::BinaryOperationType::Multiply) {
- segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register32("ebx"), Asm::Args::Immediate32(immediate2)}}));
- segment.push_back(makeOp("mul", Asm::Args{{Asm::Args::Register32("ebx")}}));
- } else
- throw std::runtime_error("ICE: Asm: Unsupported binary operation type: "s + std::to_string(static_cast<int>(op.type())));
-#endif
-
-
-
-
- if (operands[1].type() != FlowGraph::DataType::Int) {
- std::runtime_error("Bad type for operand 1: "s + std::to_string(int(operands[1].type())));
- }
-
- if (operands[2].type() != FlowGraph::DataType::Int) {
- std::runtime_error("Bad type for operand 2: "s + std::to_string(int(operands[2].type())));
- }
-
- if (!operands[1].storage())
- throw std::runtime_error("ICE: Operand 1 storage is 0");
- if (!operands[2].storage())
- throw std::runtime_error("ICE: Operand 2 storage is 0");
-
- uint32_t immediate1{};
- try {
- FlowGraph::Constant& value1 {dynamic_cast<FlowGraph::Constant&>(*operands[1].storage())};
- if (value1.value().size() < sizeof(uint32_t))
- throw std::runtime_error("ICE: Int data from operand 1 needs at least 4 bytes, got "s + std::to_string(value1.value().size()));
-
- immediate1 = boost::endian::little_to_native(*(reinterpret_cast<const uint32_t*>(value1.value().data())));
- } catch (const std::bad_cast& ex) {
- std::runtime_error("Bad value for operand 1: Constant expected");
- }
-
- uint32_t immediate2{};
- try {
- FlowGraph::Constant& value2 {dynamic_cast<FlowGraph::Constant&>(*operands[2].storage())};
- if (value2.value().size() < sizeof(uint32_t))
- throw std::runtime_error("ICE: Int data from operand 2 needs at least 4 bytes, got "s + std::to_string(value2.value().size()));
-
- immediate2 = boost::endian::little_to_native(*(reinterpret_cast<const uint32_t*>(value2.value().data())));
- } catch (const std::bad_cast& ex) {
- std::runtime_error("Bad value for operand 2: Constant expected");
- }
-
- segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register32("eax"), Asm::Args::Immediate32(immediate1)}}));
-
if (op.type() == FlowGraph::BinaryOperationType::Add) {
- segment.push_back(makeOp("add", Asm::Args{{Asm::Args::Register32("eax"), Asm::Args::Immediate32(immediate2)}}));
+ segment.push_back(makeLoadValue(operands[1]));
+ segment.push_back(makeAddValue(operands[2]));
+ segment.push_back(makeStoreValue(operands[0]));
} else if (op.type() == FlowGraph::BinaryOperationType::Multiply) {
- segment.push_back(makeOp("mov", Asm::Args{{Asm::Args::Register32("ebx"), Asm::Args::Immediate32(immediate2)}}));
- segment.push_back(makeOp("mul", Asm::Args{{Asm::Args::Register32("ebx")}}));
+ segment.push_back(makeLoadValue(operands[1]));
+ segment.append(makeMulValue(operands[2]));
+ segment.push_back(makeStoreValue(operands[0]));
} else
throw std::runtime_error("ICE: Asm: Unsupported binary operation type: "s + std::to_string(static_cast<int>(op.type())));
@@ -115,7 +175,7 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment)
//FlowGraph::DestroyScopeOp& op {dynamic_cast<FlowGraph::DestroyScopeOp&>(*node)};
segment.push_back(makeOp("pop", Asm::Args{{Asm::Args::Register64("rbp")}}));
- // Move eax for exit() via rdi
+ // Move eax (still present from last operation) for exit() via rdi
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")}}));
} else if (typeid(node_deref) == typeid(FlowGraph::DataNode)) {