From 927eb99e75325164a541c2638e1e607294019381 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Tue, 17 Nov 2020 12:38:40 +0100 Subject: Complete hierarchical evaluation (unittest and systemtest fixed) --- Makefile | 2 +- TODO | 7 ++++--- asm/intel64/add.cpp | 18 ++++++++++++++++++ asm/intel64/encode.cpp | 51 ++++++++++++++++++++++++++------------------------ asm/intel64/mov.cpp | 6 ++++++ cpp.cpp | 11 +++-------- flowgraph/data.cpp | 20 ++++++++++++++++++++ flowgraph/data.h | 7 ++++--- flowgraph/graph.cpp | 8 +++++--- flowgraph/graph.h | 2 +- flowgraph/node.cpp | 34 ++++++++++++++++++++------------- flowgraph/node.h | 18 +++++++++++------- flowgraph/scope.cpp | 24 ++++++++++++++++++------ flowgraph/scope.h | 7 +++++-- flowgraph/storage.cpp | 16 +++------------- flowgraph/storage.h | 13 ++++--------- tests/test-cpp.cpp | 30 ++++++++++++++++++++++++++--- 17 files changed, 178 insertions(+), 96 deletions(-) diff --git a/Makefile b/Makefile index f1ea2dc..1ef9a12 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,7 @@ all: mcc unittest systemtest # Tests on C++ level unittest: test-$(PROJECTNAME) - ./test-$(PROJECTNAME) # --gtest_filter='CppTest.compile_2_times' + ./test-$(PROJECTNAME) #--gtest_filter='CppTest.compile_parentheses_right' # Testing mcc executable and compiled elf programs systemtest: mcc diff --git a/TODO b/TODO index 80faedc..0607485 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,6 @@ -cpp.cpp: eval w/ Data vs. Node -encode.cpp: support temporaries -Asm: Register indirection +Fix stack +encode.cpp: UnaryOperation +asm parser +ICE class grammer.cpp: match() : return point of match error ("Compile error") 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{ 0x05 } + std::any_cast(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{ 0x81 } + ModRM("/0", std::any_cast(args[0]).name()) + std::any_cast(args[1]).getCode(); + } else if (args[0].type() == typeid(Asm::Args::Register64) && std::any_cast(args[0]).name() == "rax" && args[1].type() == typeid(Asm::Args::Immediate32)) + { // add rax, imm32 machine_code = REX("W") + std::vector{ 0x05 } + std::any_cast(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{ 0x03 } + ModRM(std::any_cast(args[0]).name(), std::any_cast(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{ 0x03 } + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).reg()); + } else { throw std::runtime_error("Unimplemented: add "s + args[0].type().name() + " "s + args[1].type().name()); } @@ -35,8 +46,15 @@ bool registered { return std::make_shared(args); }) && registerOp(mangleName("add"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("add"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("add"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(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 makeLoadValue(FlowGraph::Data& data) +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()))); @@ -29,15 +29,15 @@ std::shared_ptr 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(data_storage)}; + //FlowGraph::TemporaryStorage& storage {dynamic_cast(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 makeStoreValue(FlowGraph::Data& data) +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()))); @@ -48,15 +48,15 @@ std::shared_ptr makeStoreValue(FlowGraph::Data& data) auto& data_storage{*data.storage()}; if (typeid(data_storage) == typeid(FlowGraph::TemporaryStorage)) { - FlowGraph::TemporaryStorage& storage {dynamic_cast(data_storage)}; + //FlowGraph::TemporaryStorage& storage {dynamic_cast(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 makeAddValue(FlowGraph::Data& data) +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()))); @@ -75,15 +75,15 @@ std::shared_ptr 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(data_storage)}; + //FlowGraph::TemporaryStorage& storage {dynamic_cast(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> makeMulValue(FlowGraph::Data& data) +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()))); @@ -105,9 +105,9 @@ std::vector> 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(data_storage)}; + //FlowGraph::TemporaryStorage& storage {dynamic_cast(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(*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(op.type()))); } else if (typeid(node_deref) == typeid(FlowGraph::CreateScopeOp)) { - //FlowGraph::CreateScopeOp& op {dynamic_cast(*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(*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)}; - segment.push_back(makeOp("pop", Asm::Args{{Asm::Args::Register64("rbp")}})); + //FlowGraph::DestroyScopeOp& op {dynamic_cast(*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{ 0x89 } + ModRM(std::any_cast(args[1]).name(), std::any_cast(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{ 0xC7 } + ModRM("/0", std::any_cast(args[0]).name()) + std::any_cast(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("mov"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); + }) && + registerOp(mangleName("mov"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); }) }; diff --git a/cpp.cpp b/cpp.cpp index 9690289..837eae9 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -562,7 +562,7 @@ void CPP::getValueOfNode(index_t index) size_t num_childs {m_nodes[index].child_ids.size()}; if (mValues.size() < num_childs) - throw std::runtime_error("ICE: Expected num_childs elements on Values stack at "s + locationOfNode(index)); + throw std::runtime_error("ICE: Expected num_childs elements on values stack at "s + locationOfNode(index)); auto function_it{node_eval_map.find(m_nodes[index].type)}; std::any result; @@ -587,6 +587,7 @@ void CPP::getValueOfToken(index_t index) FlowGraph::Graph graph{{std::make_shared(data)}}; mValues.push_back(graph); } else { + //throw std::runtime_error("ICE: Unsupported token: "s + m_tokens[index].type); mValues.push_back(std::any{}); } } @@ -629,13 +630,7 @@ void CPP::link() // mSegment -> elf - mCode = std::vector{ - 0x48, 0xc7, 0xc0, 0x3c, 0x00, 0x00, 0x00, // mov $0x3c,%rax # syscall 60 - 0x48, 0x31, 0xff, // xor %rdi,%rdi # exit code 0 - } + mSegment.getCode() + std::vector{ // # leave exit code in edi - 0x0f, 0x05, // syscall - } - ; + mCode = mSegment.getCode(); } // phases of translation, according to standard diff --git a/flowgraph/data.cpp b/flowgraph/data.cpp index 9dd6ef8..8568e0c 100644 --- a/flowgraph/data.cpp +++ b/flowgraph/data.cpp @@ -1 +1,21 @@ #include "data.h" + +FlowGraph::Data::Data(FlowGraph::DataType type, std::shared_ptr storage): + m_type(type), m_storage(storage) +{ +} + +FlowGraph::DataType FlowGraph::Data::type() const +{ + return m_type; +} + +std::shared_ptr FlowGraph::Data::storage() const +{ + return m_storage; +} + +bool FlowGraph::Data::operator==(const FlowGraph::Data& other) const +{ + return m_type == other.m_type && m_storage == other.m_storage; +} diff --git a/flowgraph/data.h b/flowgraph/data.h index d1c2588..e2def93 100644 --- a/flowgraph/data.h +++ b/flowgraph/data.h @@ -27,9 +27,10 @@ namespace FlowGraph { class Data { public: - Data(DataType type, std::shared_ptr storage): m_type(type), m_storage(storage) {} - DataType type() const { return m_type; } - std::shared_ptr storage() { return m_storage; } + Data(DataType type, std::shared_ptr storage); + DataType type() const; + std::shared_ptr storage() const; + bool operator==(const Data& other) const; private: const DataType m_type; std::shared_ptr m_storage; diff --git a/flowgraph/graph.cpp b/flowgraph/graph.cpp index 6398c68..229ded8 100644 --- a/flowgraph/graph.cpp +++ b/flowgraph/graph.cpp @@ -25,7 +25,7 @@ FlowGraph::Graph::~Graph() } // Assume first node of graph to be CreateScopeOp -FlowGraph::LocalScope& FlowGraph::Graph::scope() const +std::shared_ptr FlowGraph::Graph::scope() const { if (this->empty()) throw std::runtime_error("ICE: FlowGraph expected to be non-empty!"); @@ -42,12 +42,14 @@ FlowGraph::LocalScope& FlowGraph::Graph::scope() const void FlowGraph::Graph::append(const FlowGraph::Graph& other) { + // move graph nodes this->insert(this->end() - 1, other.begin() + 1, other.end() - 1); - this->scope().append(other.scope()); + // move scope nodes to new scope + this->scope()->append(*other.scope()); } -void FlowGraph::Graph::append(std::shared_ptr node) +void FlowGraph::Graph::append(std::shared_ptr node) { this->insert(this->end() - 1, node); } diff --git a/flowgraph/graph.h b/flowgraph/graph.h index 15c6aef..c0ab847 100644 --- a/flowgraph/graph.h +++ b/flowgraph/graph.h @@ -20,7 +20,7 @@ namespace FlowGraph { Graph& operator= (const Graph&) = default; // returns the outermost scope inside this graph - LocalScope& scope() const; + std::shared_ptr scope() const; // append other graph by joining the respective outermost scopes void append(const Graph& other); diff --git a/flowgraph/node.cpp b/flowgraph/node.cpp index 2d757f9..8e8b5eb 100644 --- a/flowgraph/node.cpp +++ b/flowgraph/node.cpp @@ -1,6 +1,7 @@ #include "node.h" #include "data.h" +#include "minicc.h" #include @@ -19,33 +20,40 @@ FlowGraph::Data& Node::destination() // 4 byte for now Data FlowGraph::MakeConstantInt(int i) { - std::vector value(size_t(4), uint8_t(0)); - *(reinterpret_cast(value.data())) = boost::endian::native_to_little(static_cast(i)); - return Data{DataType::Int, std::make_shared(value)}; + return Data{DataType::Int, std::make_shared(to_little_endian(int32_t(i)))}; } -Data FlowGraph::MakeLocalPointer(FlowGraph::LocalScope& scope, const std::string& name) +Data FlowGraph::MakeLocalPointer(std::shared_ptr scope, const std::string& name) { - Data data{DataType::Pointer, std::make_shared(scope, name)}; - scope.push_back(std::make_shared(data)); + Data data{DataType::Pointer, std::make_shared(name)}; + scope->push_back(std::make_shared(data)); return data; } -Data FlowGraph::MakeLocalSize(FlowGraph::LocalScope& scope, const std::string& name) +Data FlowGraph::MakeLocalSize(std::shared_ptr scope, const std::string& name) { - Data data{DataType::Size, std::make_shared(scope, name)}; - scope.push_back(std::make_shared(data)); + Data data{DataType::Size, std::make_shared(name)}; + scope->push_back(std::make_shared(data)); return data; } -Data FlowGraph::MakeTemporaryInt(FlowGraph::LocalScope& scope) +Data FlowGraph::MakeTemporaryInt(std::shared_ptr scope) { - Data data{DataType::Int, std::make_shared(scope)}; - scope.push_back(std::make_shared(data)); + Data data{DataType::Int, std::make_shared()}; + scope->push_back(std::make_shared(data)); return data; } -LocalScope& CreateScopeOp::scope() +FlowGraph::CreateScopeOp::CreateScopeOp(): m_scope(std::make_shared()) +{ +} + +std::shared_ptr FlowGraph::CreateScopeOp::scope() { return m_scope; } + +FlowGraph::DestroyScopeOp::DestroyScopeOp(std::shared_ptr scope): m_scope(std::make_shared()) +{ +} + diff --git a/flowgraph/node.h b/flowgraph/node.h index 5ea194d..6a3ef14 100644 --- a/flowgraph/node.h +++ b/flowgraph/node.h @@ -5,6 +5,8 @@ #include "data.h" #include "storage.h" +#include + namespace FlowGraph { // Node in Graph: Building block of the graph @@ -46,9 +48,9 @@ namespace FlowGraph { }; Data MakeConstantInt(int i); - Data MakeLocalPointer(FlowGraph::LocalScope& scope, const std::string& name); - Data MakeLocalSize(FlowGraph::LocalScope& scope, const std::string& name); - Data MakeTemporaryInt(LocalScope& scope); + Data MakeLocalPointer(std::shared_ptr scope, const std::string& name); + Data MakeLocalSize(std::shared_ptr scope, const std::string& name); + Data MakeTemporaryInt(std::shared_ptr scope); class MemCopy: public Node { @@ -176,17 +178,19 @@ namespace FlowGraph { class CreateScopeOp: public Node { public: - CreateScopeOp() {} - LocalScope& scope(); + CreateScopeOp(); + std::shared_ptr scope(); private: - LocalScope m_scope; + std::shared_ptr m_scope; }; // Close current scope, closing stack frame class DestroyScopeOp: public Node { public: - DestroyScopeOp(LocalScope& scope) {} + DestroyScopeOp(std::shared_ptr scope); + private: + std::shared_ptr m_scope; }; } // namespace FlowGraph diff --git a/flowgraph/scope.cpp b/flowgraph/scope.cpp index 54a3cca..dd6c62c 100644 --- a/flowgraph/scope.cpp +++ b/flowgraph/scope.cpp @@ -2,6 +2,14 @@ #include "storage.h" +FlowGraph::LocalScope::LocalScope() +{ +} + +FlowGraph::LocalScope::~LocalScope() +{ +} + void FlowGraph::LocalScope::push_back(std::shared_ptr data) { m_variables.push_back(data); @@ -9,18 +17,22 @@ void FlowGraph::LocalScope::push_back(std::shared_ptr data) void FlowGraph::LocalScope::append(const FlowGraph::LocalScope& other) { + // actually move variables to new scope m_variables.insert(m_variables.end(), other.m_variables.begin(), other.m_variables.end()); } -index_t FlowGraph::LocalScope::indexOfStorage(const Storage& storage) const +index_t FlowGraph::LocalScope::indexOfData(const FlowGraph::Data& data) const { - std::cout << "DEBUG: " << m_variables.size() << std::endl; for (index_t i = 0; i < m_variables.size(); i++) { - FlowGraph::Storage& i_storage {*(m_variables[i]->storage())}; - - if (&i_storage == &storage) // compare addresses + if (*m_variables[i] == data) return i; } - throw std::runtime_error("ICE: Storage not found"); + throw std::runtime_error("ICE: Data not found"); } + +size_t FlowGraph::LocalScope::size() const +{ + return m_variables.size(); +} + diff --git a/flowgraph/scope.h b/flowgraph/scope.h index 65898cf..3fc0fcc 100644 --- a/flowgraph/scope.h +++ b/flowgraph/scope.h @@ -16,11 +16,14 @@ namespace FlowGraph { class LocalScope { public: - LocalScope() = default; + LocalScope(); + ~LocalScope(); void push_back(std::shared_ptr data); void append(const LocalScope& other); - index_t indexOfStorage(const Storage& storage) const; + index_t indexOfData(const FlowGraph::Data& data) const; + + size_t size() const; private: std::vector> m_variables; diff --git a/flowgraph/storage.cpp b/flowgraph/storage.cpp index e9577d6..fb82bc2 100644 --- a/flowgraph/storage.cpp +++ b/flowgraph/storage.cpp @@ -2,22 +2,12 @@ using namespace std::string_literals; -FlowGraph::TemporaryStorage::TemporaryStorage(LocalScope& scope): - m_scope(scope) +FlowGraph::LocalStorage::LocalStorage(const std::string& name): + m_name(name) { } -std::string FlowGraph::TemporaryStorage::name() const +FlowGraph::TemporaryStorage::TemporaryStorage() { - return "__local_"s + std::to_string(m_scope.indexOfStorage(*this)); } -index_t FlowGraph::TemporaryStorage::indexOfStorage() const -{ - return m_scope.indexOfStorage(*this); -} - -index_t FlowGraph::LocalStorage::indexOfStorage() const -{ - return m_scope.indexOfStorage(*this); -} diff --git a/flowgraph/storage.h b/flowgraph/storage.h index 27c201e..7b5c53b 100644 --- a/flowgraph/storage.h +++ b/flowgraph/storage.h @@ -5,6 +5,7 @@ #include "scope.h" #include +#include #include #include @@ -23,7 +24,7 @@ namespace FlowGraph { class Constant: public Storage { public: - Constant(std::vector& value): m_value(value) {} // little endian data + Constant(const std::vector& value): m_value(value) {} // little endian data const std::vector& value() const { return m_value; } private: std::vector m_value; @@ -42,12 +43,10 @@ namespace FlowGraph { class LocalStorage : public Storage { public: - LocalStorage(LocalScope& scope, const std::string& name): m_name(name), m_scope(scope) {} + LocalStorage(const std::string& name); const std::string& name() const { return m_name; } - index_t indexOfStorage() const; private: std::string m_name; - LocalScope& m_scope; }; // intermediate results, anonymous values @@ -55,11 +54,7 @@ namespace FlowGraph { class TemporaryStorage : public Storage { public: - TemporaryStorage(LocalScope& scope); - std::string name() const; - index_t indexOfStorage() const; - private: - LocalScope& m_scope; + TemporaryStorage(); }; // dereferenced pointer diff --git a/tests/test-cpp.cpp b/tests/test-cpp.cpp index 5755516..95d271d 100644 --- a/tests/test-cpp.cpp +++ b/tests/test-cpp.cpp @@ -80,16 +80,40 @@ TEST_F(CppTest, preprocessing_tokenize_compile_error) { FAIL() << "Exception expected"; } -TEST_F(CppTest, compile) { +TEST_F(CppTest, compile_1) { + CPP cpp; + + cpp.compile("int main() { return 1; }"); +} + +TEST_F(CppTest, compile_add) { CPP cpp; cpp.compile("int main() { return 1 + 2; }"); } -TEST_F(CppTest, compile_1) { +TEST_F(CppTest, compile_123) { CPP cpp; - cpp.compile("int main() { return 1; }"); + cpp.compile("int main() { return 1 + 2 + 3; }"); +} + +TEST_F(CppTest, compile_parentheses_left) { + CPP cpp; + + cpp.compile("int main() { return (3 + 1) + 2; }"); +} + +TEST_F(CppTest, compile_parentheses_right) { + CPP cpp; + + cpp.compile("int main() { return 3 + (1 + 2); }"); +} + +TEST_F(CppTest, compile_add_mul) { + CPP cpp; + + cpp.compile("int main() { return 1 + (2 * 2); }"); } TEST_F(CppTest, compile_2_times) { -- cgit v1.2.3