From 300219dc8519720a36525c7b40c6a327580fe0bd Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 15 Nov 2020 22:05:14 +0100 Subject: Evaluation (WIP) --- asm/intel64/encode.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'asm/intel64/encode.cpp') diff --git a/asm/intel64/encode.cpp b/asm/intel64/encode.cpp index 03a7897..681e407 100644 --- a/asm/intel64/encode.cpp +++ b/asm/intel64/encode.cpp @@ -47,7 +47,19 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) FlowGraph::BinaryOperation& op {dynamic_cast(*node)}; auto operands {op.operands()}; - // TODO: ignore destination (0) for now + +#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(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()))); @@ -83,11 +95,11 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) } 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("edi"), Asm::Args::Immediate32(immediate1)}})); + 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("edi"), Asm::Args::Immediate32(immediate2)}})); + segment.push_back(makeOp("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")}})); @@ -103,7 +115,7 @@ void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) //FlowGraph::DestroyScopeOp& op {dynamic_cast(*node)}; segment.push_back(makeOp("pop", Asm::Args{{Asm::Args::Register64("rbp")}})); - // Move + // Move eax 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)) { -- cgit v1.2.3