// Intel specific conversion: Abstract Graph -> Machine specific segment #include "encode.h" #include "asm/assembler.h" #include "asm/parse.h" #include "minicc.h" #include #include namespace { std::shared_ptr 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()))); } 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(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(data_storage)}; 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 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()))); } 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(data_storage)}; 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 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()))); } 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(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(data_storage)}; 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> 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()))); } 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(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(data_storage)}; 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))); } } // namespace void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) { segment.clear(); for (const std::shared_ptr& node: graph) { if (node.get()) { auto& node_deref = *node.get(); if (typeid(node_deref) == typeid(FlowGraph::UnaryOperation)) { FlowGraph::UnaryOperation& op {dynamic_cast(*node)}; auto operands {op.operands()}; if (operands.size() != 2) throw std::runtime_error("ICE: Bad number of operands for UnaryOperation: "s + std::to_string(operands.size())); if (op.type() == FlowGraph::UnaryOperationType::BitwiseNot) { segment.push_back(makeLoadValue(operands[1], graph)); segment.push_back(parseAsm("not eax")); segment.push_back(makeStoreValue(operands[0], graph)); } else if (op.type() == FlowGraph::UnaryOperationType::LogicalNot) { segment.push_back(makeLoadValue(operands[1], graph)); segment.push_back(parseAsm("bsr eax")); // ZF=1 iff eax=0 segment.push_back(parseAsm("lahf")); // ZF in AH bit 6 segment.push_back(parseAsm("shr eax, 14")); // ZF in eax bit 0 segment.push_back(parseAsm("and eax, 1")); // now, 0 or 1 is in eax, negated because of zero flag segment.push_back(makeStoreValue(operands[0], graph)); } else if (op.type() == FlowGraph::UnaryOperationType::Minus) { segment.push_back(makeLoadValue(operands[1], graph)); segment.push_back(parseAsm("neg eax")); segment.push_back(makeStoreValue(operands[0], graph)); } else throw std::runtime_error("ICE: Asm: Unsupported unary operation type: "s + std::to_string(static_cast(op.type()))); } else if (typeid(node_deref) == typeid(FlowGraph::BinaryOperation)) { FlowGraph::BinaryOperation& op {dynamic_cast(*node)}; auto operands {op.operands()}; if (operands.size() != 3) throw std::runtime_error("ICE: Bad number of operands for BinaryOperation: "s + std::to_string(operands.size())); if (op.type() == FlowGraph::BinaryOperationType::Add) { 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], 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(op.type()))); } else if (typeid(node_deref) == typeid(FlowGraph::CreateScopeOp)) { //FlowGraph::CreateScopeOp& op {dynamic_cast(*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(*node)}; // TODO: Destroy stack frame //segment.push_back(makeOp("pop", Asm::Args{{Asm::Args::Register64("rbp")}})); 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 { throw std::runtime_error("ICE: Encoding: Unsupported node: "s + demangle(typeid(node_deref))); } } else { throw std::runtime_error("ICE: encode: flowgraph node is null"); } } }