From 414953eba1b1cc3570da1e59dc2392ce1b1ae5ef Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 9 Nov 2020 10:40:16 +0100 Subject: Move encode.h|cpp to intel specific --- Makefile | 2 +- asm/encode.cpp | 68 -------------------------------------------------- asm/encode.h | 14 ----------- asm/intel64/encode.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ asm/intel64/encode.h | 14 +++++++++++ cpp.cpp | 2 +- 6 files changed, 84 insertions(+), 84 deletions(-) delete mode 100644 asm/encode.cpp delete mode 100644 asm/encode.h create mode 100644 asm/intel64/encode.cpp create mode 100644 asm/intel64/encode.h diff --git a/Makefile b/Makefile index de84081..db3c1bb 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,6 @@ endif PROGSRC=\ asm/assembler.cpp \ asm/chunk.cpp \ - asm/encode.cpp \ asm/intel64/add.cpp \ asm/intel64/inc.cpp \ asm/intel64/int.cpp \ @@ -57,6 +56,7 @@ PROGSRC=\ asm/intel64/ret.cpp \ asm/intel64/xor.cpp \ asm/intel64/codes.cpp \ + asm/intel64/encode.cpp \ asm/operators.cpp \ asm/segment.cpp \ bnf.cpp \ diff --git a/asm/encode.cpp b/asm/encode.cpp deleted file mode 100644 index 8bf33c0..0000000 --- a/asm/encode.cpp +++ /dev/null @@ -1,68 +0,0 @@ -// Intel specific conversion: Abstract Graph -> Machine specific segment -#include "encode.h" - -#include "asm/assembler.h" -#include "minicc.h" - -#include - -#include - -void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) -{ - segment.clear(); - - for (const std::shared_ptr& node: graph) { - try { - FlowGraph::BinaryOperation& op {dynamic_cast(*node)}; - - auto operands {op.operands()}; - // TODO: ignore destination (0) for now - - 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(*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(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(*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(value2.value().data()))); - } catch (const std::bad_cast& ex) { - std::runtime_error("Bad value for operand 2: Constant expected"); - } - - Asm::Args args1{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate1)}}; - segment.push_back(makeOp("mov", args1)); - - Asm::Args args2{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate2)}}; - segment.push_back(makeOp("add", args2)); - - } catch (const std::bad_cast& ex) { - std::runtime_error("ICE: Encoding: Unsupported node: "s + ex.what()); - } - } -} - diff --git a/asm/encode.h b/asm/encode.h deleted file mode 100644 index d9e5674..0000000 --- a/asm/encode.h +++ /dev/null @@ -1,14 +0,0 @@ -// Convert: Abstract FlowGraph to Machine dependent Code - -#pragma once - -#include "flowgraph/graph.h" -#include "segment.h" - -namespace Asm { - -// in: graph -// out: segment -void toMachineCode(const FlowGraph::Graph& graph, Segment& segment); - -} // namespace Asm diff --git a/asm/intel64/encode.cpp b/asm/intel64/encode.cpp new file mode 100644 index 0000000..8bf33c0 --- /dev/null +++ b/asm/intel64/encode.cpp @@ -0,0 +1,68 @@ +// Intel specific conversion: Abstract Graph -> Machine specific segment +#include "encode.h" + +#include "asm/assembler.h" +#include "minicc.h" + +#include + +#include + +void Asm::toMachineCode(const FlowGraph::Graph& graph, Segment& segment) +{ + segment.clear(); + + for (const std::shared_ptr& node: graph) { + try { + FlowGraph::BinaryOperation& op {dynamic_cast(*node)}; + + auto operands {op.operands()}; + // TODO: ignore destination (0) for now + + 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(*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(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(*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(value2.value().data()))); + } catch (const std::bad_cast& ex) { + std::runtime_error("Bad value for operand 2: Constant expected"); + } + + Asm::Args args1{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate1)}}; + segment.push_back(makeOp("mov", args1)); + + Asm::Args args2{{Asm::Args::Register32("edi"), Asm::Args::Immediate32(immediate2)}}; + segment.push_back(makeOp("add", args2)); + + } catch (const std::bad_cast& ex) { + std::runtime_error("ICE: Encoding: Unsupported node: "s + ex.what()); + } + } +} + diff --git a/asm/intel64/encode.h b/asm/intel64/encode.h new file mode 100644 index 0000000..7fd23d6 --- /dev/null +++ b/asm/intel64/encode.h @@ -0,0 +1,14 @@ +// Convert: Abstract FlowGraph to Machine dependent Code + +#pragma once + +#include "asm/segment.h" +#include "flowgraph/graph.h" + +namespace Asm { + +// in: graph +// out: segment +void toMachineCode(const FlowGraph::Graph& graph, Segment& segment); + +} // namespace Asm diff --git a/cpp.cpp b/cpp.cpp index c8de4c8..e4d935a 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -1,6 +1,6 @@ #include "cpp.h" -#include "asm/encode.h" +#include "asm/intel64/encode.h" #include "asm/operators.h" #include "bnf.h" #include "cppbnf.h" -- cgit v1.2.3