summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-11-17 12:38:40 +0100
committerRoland Reichwein <mail@reichwein.it>2020-11-17 12:38:40 +0100
commit927eb99e75325164a541c2638e1e607294019381 (patch)
tree5b5476456f0f957fc7492465ff08ace54e1a9e48
parentc9cb051fae190acfc36813e4a23759fb9b9c3df3 (diff)
Complete hierarchical evaluation (unittest and systemtest fixed)
-rw-r--r--Makefile2
-rw-r--r--TODO7
-rw-r--r--asm/intel64/add.cpp18
-rw-r--r--asm/intel64/encode.cpp51
-rw-r--r--asm/intel64/mov.cpp6
-rw-r--r--cpp.cpp11
-rw-r--r--flowgraph/data.cpp20
-rw-r--r--flowgraph/data.h7
-rw-r--r--flowgraph/graph.cpp8
-rw-r--r--flowgraph/graph.h2
-rw-r--r--flowgraph/node.cpp34
-rw-r--r--flowgraph/node.h18
-rw-r--r--flowgraph/scope.cpp24
-rw-r--r--flowgraph/scope.h7
-rw-r--r--flowgraph/storage.cpp16
-rw-r--r--flowgraph/storage.h13
-rw-r--r--tests/test-cpp.cpp30
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<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);
})
};
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<FlowGraph::DataNode>(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<uint8_t>{
- 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<uint8_t>{ // # 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<FlowGraph::Storage> storage):
+ m_type(type), m_storage(storage)
+{
+}
+
+FlowGraph::DataType FlowGraph::Data::type() const
+{
+ return m_type;
+}
+
+std::shared_ptr<FlowGraph::Storage> 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> storage): m_type(type), m_storage(storage) {}
- DataType type() const { return m_type; }
- std::shared_ptr<Storage> storage() { return m_storage; }
+ Data(DataType type, std::shared_ptr<Storage> storage);
+ DataType type() const;
+ std::shared_ptr<Storage> storage() const;
+ bool operator==(const Data& other) const;
private:
const DataType m_type;
std::shared_ptr<Storage> 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::LocalScope> 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> node)
+void FlowGraph::Graph::append(std::shared_ptr<FlowGraph::Node> 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<LocalScope> 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 <boost/endian/conversion.hpp>
@@ -19,33 +20,40 @@ FlowGraph::Data& Node::destination()
// 4 byte for now
Data FlowGraph::MakeConstantInt(int i)
{
- std::vector<uint8_t> value(size_t(4), uint8_t(0));
- *(reinterpret_cast<int32_t*>(value.data())) = boost::endian::native_to_little(static_cast<int32_t>(i));
- return Data{DataType::Int, std::make_shared<Constant>(value)};
+ return Data{DataType::Int, std::make_shared<Constant>(to_little_endian(int32_t(i)))};
}
-Data FlowGraph::MakeLocalPointer(FlowGraph::LocalScope& scope, const std::string& name)
+Data FlowGraph::MakeLocalPointer(std::shared_ptr<FlowGraph::LocalScope> scope, const std::string& name)
{
- Data data{DataType::Pointer, std::make_shared<LocalStorage>(scope, name)};
- scope.push_back(std::make_shared<Data>(data));
+ Data data{DataType::Pointer, std::make_shared<LocalStorage>(name)};
+ scope->push_back(std::make_shared<Data>(data));
return data;
}
-Data FlowGraph::MakeLocalSize(FlowGraph::LocalScope& scope, const std::string& name)
+Data FlowGraph::MakeLocalSize(std::shared_ptr<FlowGraph::LocalScope> scope, const std::string& name)
{
- Data data{DataType::Size, std::make_shared<LocalStorage>(scope, name)};
- scope.push_back(std::make_shared<Data>(data));
+ Data data{DataType::Size, std::make_shared<LocalStorage>(name)};
+ scope->push_back(std::make_shared<Data>(data));
return data;
}
-Data FlowGraph::MakeTemporaryInt(FlowGraph::LocalScope& scope)
+Data FlowGraph::MakeTemporaryInt(std::shared_ptr<FlowGraph::LocalScope> scope)
{
- Data data{DataType::Int, std::make_shared<TemporaryStorage>(scope)};
- scope.push_back(std::make_shared<Data>(data));
+ Data data{DataType::Int, std::make_shared<TemporaryStorage>()};
+ scope->push_back(std::make_shared<Data>(data));
return data;
}
-LocalScope& CreateScopeOp::scope()
+FlowGraph::CreateScopeOp::CreateScopeOp(): m_scope(std::make_shared<LocalScope>())
+{
+}
+
+std::shared_ptr<LocalScope> FlowGraph::CreateScopeOp::scope()
{
return m_scope;
}
+
+FlowGraph::DestroyScopeOp::DestroyScopeOp(std::shared_ptr<LocalScope> scope): m_scope(std::make_shared<LocalScope>())
+{
+}
+
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 <memory>
+
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<FlowGraph::LocalScope> scope, const std::string& name);
+ Data MakeLocalSize(std::shared_ptr<FlowGraph::LocalScope> scope, const std::string& name);
+ Data MakeTemporaryInt(std::shared_ptr<LocalScope> scope);
class MemCopy: public Node
{
@@ -176,17 +178,19 @@ namespace FlowGraph {
class CreateScopeOp: public Node
{
public:
- CreateScopeOp() {}
- LocalScope& scope();
+ CreateScopeOp();
+ std::shared_ptr<LocalScope> scope();
private:
- LocalScope m_scope;
+ std::shared_ptr<LocalScope> m_scope;
};
// Close current scope, closing stack frame
class DestroyScopeOp: public Node
{
public:
- DestroyScopeOp(LocalScope& scope) {}
+ DestroyScopeOp(std::shared_ptr<LocalScope> scope);
+ private:
+ std::shared_ptr<LocalScope> 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> data)
{
m_variables.push_back(data);
@@ -9,18 +17,22 @@ void FlowGraph::LocalScope::push_back(std::shared_ptr<Data> 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> 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<std::shared_ptr<Data>> 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 <cstdint>
+#include <memory>
#include <string>
#include <vector>
@@ -23,7 +24,7 @@ namespace FlowGraph {
class Constant: public Storage
{
public:
- Constant(std::vector<uint8_t>& value): m_value(value) {} // little endian data
+ Constant(const std::vector<uint8_t>& value): m_value(value) {} // little endian data
const std::vector<uint8_t>& value() const { return m_value; }
private:
std::vector<uint8_t> 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) {