summaryrefslogtreecommitdiffhomepage
path: root/asm
diff options
context:
space:
mode:
Diffstat (limited to 'asm')
-rw-r--r--asm/intel64/add.cpp18
-rw-r--r--asm/intel64/encode.cpp51
-rw-r--r--asm/intel64/mov.cpp6
3 files changed, 51 insertions, 24 deletions
diff --git a/asm/intel64/add.cpp b/asm/intel64/add.cpp
index 236436c..957c27f 100644
--- a/asm/intel64/add.cpp
+++ b/asm/intel64/add.cpp
@@ -14,15 +14,26 @@ Op_add::Op_add(const Asm::Args& args)
args[1].type() == typeid(Asm::Args::Immediate32))
{ // add eax, imm32 (before "add reg32, imm32"! It's shorter.)
machine_code = std::vector<uint8_t>{ 0x05 } + std::any_cast<Asm::Args::Immediate32>(args[1]).getCode();
+
} else if (args[0].type() == typeid(Asm::Args::Register32) &&
args[1].type() == typeid(Asm::Args::Immediate32))
+
{ // add reg32, imm32
machine_code = std::vector<uint8_t>{ 0x81 } + ModRM("/0", std::any_cast<Asm::Args::Register32>(args[0]).name()) + std::any_cast<Asm::Args::Immediate32>(args[1]).getCode();
+
} else if (args[0].type() == typeid(Asm::Args::Register64) &&
std::any_cast<Asm::Args::Register64>(args[0]).name() == "rax" &&
args[1].type() == typeid(Asm::Args::Immediate32))
+
{ // add rax, imm32
machine_code = REX("W") + std::vector<uint8_t>{ 0x05 } + std::any_cast<Asm::Args::Immediate32>(args[1]).getCode();
+
+ } else if (args[0].type() == typeid(Asm::Args::Register32) && args[1].type() == typeid(Asm::Args::Mem32Ptr64)) { // add reg32, [reg64]
+ machine_code = std::vector<uint8_t>{ 0x03 } + ModRM(std::any_cast<Asm::Args::Register32>(args[0]).name(), std::any_cast<Asm::Args::Mem32Ptr64>(args[1]).reg());
+
+ } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Mem64Ptr64)) { // add reg64, [reg64]
+ machine_code = REX("W") + std::vector<uint8_t>{ 0x03 } + ModRM(std::any_cast<Asm::Args::Register64>(args[0]).name(), std::any_cast<Asm::Args::Mem64Ptr64>(args[1]).reg());
+
} else {
throw std::runtime_error("Unimplemented: add "s + args[0].type().name() + " "s + args[1].type().name());
}
@@ -36,7 +47,14 @@ bool registered {
}) &&
registerOp(mangleName<Asm::Args::Register64, Asm::Args::Immediate32>("add"), [](const Asm::Args& args) -> std::shared_ptr<Op>{
return std::make_shared<Op_add>(args);
+ }) &&
+ registerOp(mangleName<Asm::Args::Register32, Asm::Args::Mem32Ptr64>("add"), [](const Asm::Args& args) -> std::shared_ptr<Op>{
+ return std::make_shared<Op_add>(args);
+ }) &&
+ registerOp(mangleName<Asm::Args::Register64, Asm::Args::Mem64Ptr64>("add"), [](const Asm::Args& args) -> std::shared_ptr<Op>{
+ return std::make_shared<Op_add>(args);
})
};
}
+
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 {
diff --git a/asm/intel64/mov.cpp b/asm/intel64/mov.cpp
index e36b40d..5f09c82 100644
--- a/asm/intel64/mov.cpp
+++ b/asm/intel64/mov.cpp
@@ -44,6 +44,9 @@ Op_mov::Op_mov(const Asm::Args& args)
} else if (args[0].type() == typeid(Asm::Args::Mem64Ptr64) && args[1].type() == typeid(Asm::Args::Register64)) { // mov [reg64], reg64
machine_code = REX("W") + std::vector<uint8_t>{ 0x89 } + ModRM(std::any_cast<Asm::Args::Register64>(args[1]).name(), std::any_cast<Asm::Args::Mem64Ptr64>(args[0]).reg());
+ } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Immediate32)) { // mov reg64, imm32 (sign-extended)
+ machine_code = REX("W") + std::vector<uint8_t>{ 0xC7 } + ModRM("/0", std::any_cast<Asm::Args::Register64>(args[0]).name()) + std::any_cast<Asm::Args::Immediate32>(args[1]).getCode();
+
} else {
throw std::runtime_error("Unimplemented: mov "s + args[0].type().name() + " "s + args[1].type().name());
}
@@ -78,6 +81,9 @@ bool registered {
}) &&
registerOp(mangleName<Asm::Args::Mem64Ptr64, Asm::Args::Register64>("mov"), [](const Asm::Args& args) -> std::shared_ptr<Op>{
return std::make_shared<Op_mov>(args);
+ }) &&
+ registerOp(mangleName<Asm::Args::Register64, Asm::Args::Immediate32>("mov"), [](const Asm::Args& args) -> std::shared_ptr<Op>{
+ return std::make_shared<Op_mov>(args);
})
};