From 930324987ff23f8972befb8f78c0788485ede594 Mon Sep 17 00:00:00 2001 From: Samuel Bayliss Date: Thu, 13 Jun 2013 16:37:16 +0100 Subject: [PATCH] Code Generation for Gravel now completes tests and propagates leaving no orphans --- CMakeLists.txt | 65 ++++ src/Actor.cxx | 77 +++-- src/Assignment.cxx | 188 ++++++++++++ src/CMakeLists.txt | 5 +- src/Context.cxx | 365 +++++++++++++++++------ src/Expression.cxx | 282 +++++++++++++++-- src/Module.cxx | 108 ++++--- src/StateMachine.cpp | 14 - src/StateMachine.cxx | 65 ++++ src/Symbol.cxx | 171 ++++++++--- src/codegen/AssignmentModule.cxx | 2 +- src/codegen/CodegenModule.cxx | 32 +- src/codegen/ForModule.cxx | 40 ++- src/codegen/Generator.cxx | 159 +++++++++- src/codegen/GuardModule.cxx | 2 +- src/codegen/SequentialModule.cxx | 2 + src/include/gravel/Assignment.h | 110 +++++++ src/include/gravel/AssignmentFunctions.h | 19 ++ src/include/gravel/Context.h | 129 ++++++-- src/include/gravel/Expression.h | 40 ++- src/include/gravel/Gravel.h | 9 +- src/include/gravel/Module.h | 58 ++-- src/include/gravel/StateMachine.h | 59 ++++ src/include/gravel/Symbol.h | 105 +++++-- src/include/gravel/private/Actor.h | 102 +++++-- src/include/gravel/private/Expression.h | 35 ++- src/include/gravel/private/Module.h | 10 +- src/include/gravel/private/Symbol.h | 17 +- src/include/potholes/codegen/ForModule.h | 16 + src/include/potholes/codegen/Generator.h | 17 +- src/include/potholes/codegen/Module.h | 13 +- test/CMakeLists.txt | 4 +- test/CodegenTest.cxx | 112 +------ test/StateMachineTest.cxx | 81 +++++ 34 files changed, 2001 insertions(+), 512 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 src/Assignment.cxx delete mode 100644 src/StateMachine.cpp create mode 100644 src/StateMachine.cxx create mode 100644 src/include/gravel/Assignment.h create mode 100644 src/include/gravel/AssignmentFunctions.h create mode 100644 src/include/gravel/StateMachine.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..26389de --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,65 @@ + +cmake_minimum_required(VERSION 2.8) + +if(DEFINED ENV{BUILT_TOOLS}) + set (BUILT_TOOLS $ENV{BUILT_TOOLS} ) +else() + message(FATAL_ERROR "BUILT_TOOLS VARIABLE NOT SET") +endif() + +if(DEFINED ENV{BUILT_TOOLS_ARCH}) + +set (BUILT_TOOLS_ARCH $ENV{BUILT_TOOLS_ARCH}) +else() + message(FATAL_ERROR "BUILT_TOOLS_ARCH VARIABLE NOT SET") +endif() + +if (APPLE) + if (DEFINED ENV{MACPORTS_PREFIX}) + set (MACPORTS_PREFIX $ENV{MACPORTS_PREFIX} ) + else() + message(FATAL_ERROR "MACPORTS_PREFIX must be set for apple platform") + endif() +endif(APPLE) + + + + +if (APPLE) + set (CLANG_PREFIX ${BUILT_TOOLS_ARCH}) + set (GMP_PREFIX ${MACPORTS_PREFIX}) + set (LLVM_PREFIX ${BUILT_TOOLS_ARCH}) + message(STATUS "LLVM_PREFIX is ${BUILT_TOOLS_ARCH}") +elseif(APPLE) + set (CLANG_PREFIX /cas/apps/llvm-3.0) + set (GMP_PREFIX ${BUILT_TOOLS_ARCH}) + set (LLVM_PREFIX ${BUILT_TOOLS_ARCH}) +endif(APPLE) + +find_package(Boost 1.52 COMPONENTS program_options system filesystem unit_test_framework REQUIRED) +set(Boost_USE_STATIC_LIBS OFF) +set(Boost_USE_MULTITHREADED ON) +set(Boost_USE_STATIC_RUNTIME OFF) +set(Boost_ALL_DYN_LINK ON) + +include_directories( + ${Boost_INCLUDE_DIR} + ) + +set (CMAKE_PREFIX_PATH ${BUILT_TOOLS_ARCH}) + +set (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/cmake) + + +find_package(Cloog REQUIRED) +find_package(OSL REQUIRED) +find_package(gmp REQUIRED) + +add_definitions(-DCLOOG_INT_GMP) +include_directories(${CMAKE_SOURCE_DIR}/src/include) +include_directories(${CLOOG_INCLUDE_DIR}) + + + +add_subdirectory(src) +add_subdirectory(test) \ No newline at end of file diff --git a/src/Actor.cxx b/src/Actor.cxx index 93c215a..a5c75da 100644 --- a/src/Actor.cxx +++ b/src/Actor.cxx @@ -1,10 +1,16 @@ #include -void Gravel::GraphNode::connect(boost::weak_ptr na, boost::weak_ptr nb) { +#include "gravel/Context.h" + + + +void Gravel::GraphNode::connect(Gravel::Pointer::GraphNode na, Gravel::Pointer::GraphNode nb) { + + back.insert(BackMapPair( Gravel::WeakPointer::GraphNode(na), Gravel::WeakPointer::GraphNode(nb))); } -Gravel::GraphNode::GraphNode() { +Gravel::GraphNode::GraphNode() :width(1) { } @@ -12,22 +18,25 @@ Gravel::GraphNode::~GraphNode() { } +unsigned Gravel::GraphNode::getWidth() { + return width; +} + Gravel::GraphNode::BackMap Gravel::GraphNode::back; -Gravel::GraphNode::ParentMap Gravel::GraphNode::parent; - - boost::weak_ptr Gravel::Actor::getOutput() const { + +Gravel::Pointer::GraphNode Gravel::Actor::getOutput() const { Gravel::GraphNode::NodeMap::const_iterator nit = nodes.find(Gravel::Output); if (nit == nodes.end()) { assert(false); } else { - return boost::weak_ptr(nit->second); + return (nit->second); } } -Gravel::GraphNode::BackMapRange Gravel::GraphNode::getConnections(NodePtr np) { +Gravel::GraphNode::BackMapRange Gravel::GraphNode::getConnections(Gravel::Pointer::GraphNode np) { return Gravel::GraphNode::back.equal_range(np); } @@ -38,27 +47,53 @@ Gravel::GraphNode::ConstNodeRange Gravel::Actor::getInputs() const { } - void Gravel::Actor::setWidth(Gravel::GraphNode::NodePtr node, unsigned width) { + void Gravel::Actor::setWidth(Gravel::Pointer::GraphNode node, unsigned width) { } -Gravel::Pointer::Actor Gravel::GraphNode::getParent(Gravel::GraphNode::NodePtr np) { + Gravel::Actor::Actor() : initialized(false) { + + } -std::map::iterator nit = parent.find(np); -if(nit == parent.end()) { - // throw exception -} else { - return nit->second; -} - +Gravel::Pointer::Actor Gravel::GraphNode::getParent(Gravel::Pointer::GraphNode np) { + + assert(np.get() != NULL); + + Gravel::Context * ctx = Gravel::Context::getInstance(); + return ctx->getParent(np); +}; + +Gravel::Pointer::Actor Gravel::GraphNode::getParent(Gravel::GraphNode::ConstNodeIterator it) { + + Gravel::Context * ctx = Gravel::Context::getInstance(); + return ctx->getParent(it->second); }; -void Gravel::GraphNode::setParents(Gravel::Pointer::Actor ptr) { - - Gravel::GraphNode::ConstNodeRange inputs = ptr->getInputs(); - Gravel::GraphNode::NodePtr output = ptr->getOutput(); - } +void Gravel::Actor::initialize(Gravel::Pointer::Actor actor) { + Gravel::Context * ctx = Gravel::Context::getInstance(); + // store a weak pointer + ctx->insert(actor); + initialized = true; + /* Gravel::GraphNode::ConstNodeIterator cit; + for (cit = nodes.begin() ; cit != nodes.end() ; cit++) { + Gravel::WeakPointer::GraphNode wn = cit->second; + Gravel::Pointer::GraphNode node = wn.lock(); + + + } + */ +} + + +Gravel::Pointer::GraphNode Gravel::GraphNode::Create(GraphNode::NodeMap & nodes, const Gravel::GraphEdgeDirection & direction) { + Gravel::Pointer::GraphNode node(new Gravel::GraphNode()); + + + nodes.insert(Gravel::GraphNode::NodePair(direction, node)); + return node; +} + diff --git a/src/Assignment.cxx b/src/Assignment.cxx new file mode 100644 index 0000000..b78b02b --- /dev/null +++ b/src/Assignment.cxx @@ -0,0 +1,188 @@ +#include +#include + +#include + +#include +#include + +#include + +#include +#include +#include +#include + +#include + + Gravel::Assignment Gravel::Assignment::Create(const Gravel::Symbol& sym, const Gravel::Expression& exp, unsigned delay) { + + Gravel::Context * ctx = Gravel::Context::getInstance(); + + Gravel::Pointer::Symbol sptr = sym.symbol; + Gravel::Pointer::Expression eptr = exp.ptr; + + Gravel::Pointer::Assignment aptr = Gravel::Pointer::Assignment(new Gravel::Implementation::Assignment(sptr, eptr, delay)); + // register assignment; + + ctx->insert(aptr); + ctx->propagate(); + // std::cout << " Created Assignment " << "\n"; + + return Gravel::Assignment(aptr); + + } + + void Gravel::Assignment::emit(std::ostream& os){ + assignment->emit(os); + } + + void Gravel::Implementation::Assignment::emit(std::ostream& os){ + Gravel::Assignment::AssignmentMap::iterator amit; + for (amit = normal.begin() ; amit != normal.end() ; amit++ ) { + os << Gravel::Expression(amit->second) << "\n"; + if (delay == 0) { + os << "assign " << Gravel::Symbol(amit->first) << " = " << Gravel::Symbol(amit->second->getSymbol()) << ";" << "\n"; + } else { + Gravel::Assignment::AssignmentMap::iterator rit = reset.find(amit->first); + + assert(delay == 1); // only supports single delay at the moment + Gravel::Context * ctx = Gravel::Context::getInstance(); + Gravel::Symbol registered_symbol = ctx->getRegisteredSymbol(amit->first); + + if (rit == reset.end() ) { + + // get registered_symbol + + os << "always @" << "(" << "posedge clk" << ") " << "begin" << "\n"; + os << registered_symbol << " <= " << Gravel::Symbol(amit->second->getSymbol()) << ";" <<"\n"; + os << "end " << "\n"; + os << "assign " << amit->first << " = " << registered_symbol << ";" << "\n"; + } else { + + os << "always @" << "(" << "posedge clk or posedge reset" << ") " << "begin" << "\n"; + os << "if " << "(" << "reset" << ")" << "begin"; + os << registered_symbol << " <= " << Gravel::Symbol(rit->second->getSymbol()) << ";" << "\n"; + os << "end"; + + os << "end " << "\n"; + + } + } + } + + + } + + void Gravel::Reset( Gravel::Symbol symbol, Gravel::Expression expression) { + + } + void Gravel::Delay( Gravel::Assignment assignment, Gravel::Expression expression) { + + } + + +class register_module : public std::binary_function { +public: + void operator() (Gravel::Pointer::Module module, Gravel::Pointer::Actor actor) { + Gravel::Context * ctx = Gravel::Context::getInstance(); + ctx->insert(actor, module); + } +}; + +class find_parent_module : public std::unary_function { +public : + const Gravel::Pointer::Module operator() (const Gravel::Pointer::Actor& actor) { + + + Gravel::Context * ctx = Gravel::Context::getInstance(); + + Gravel::Pointer::Module module = ctx->owner(actor); + + return module; + + }; +}; + + +class lacks_parent_module : public std::unary_function { +public: + bool operator() (const Gravel::Pointer::Actor& actor) { + Gravel::Context * ctx = Gravel::Context::getInstance(); + return !(ctx->isOwned(actor)); + }; +}; + + void Gravel::Implementation::Assignment::propagate() { + // std::cerr << "Calling Propagator" << "\n"; + Gravel::Collection::Actor actors; + + std::transform(normal.begin(), normal.end(), + std::inserter(actors, actors.begin()), + boost::bind(&std::map::value_type::first, _1)); + + std::transform(normal.begin(), normal.end(), + std::inserter(actors, actors.begin()), + boost::bind(&std::map::value_type::second, _1)); + + std::transform(reset.begin(), reset.end(), + std::inserter(actors, actors.begin()), + boost::bind(&std::map::value_type::first, _1)); + + std::transform(reset.begin(), reset.end(), + std::inserter(actors, actors.begin()), + boost::bind(&std::map::value_type::second, _1)); + + // std::cerr << "There are " << actors.size() << "\n"; + // Make a list of Actor Pointers from all the expressions and symbols + + Gravel::Collection::Actor registered_actors; + + std::insert_iterator rait = std::inserter(registered_actors,registered_actors.begin()); + std::remove_copy_if(actors.begin(), actors.end(), rait, lacks_parent_module() ); + + + std::set modules; + std::insert_iterator > mit = std::inserter(modules,modules.begin()); + std::transform(registered_actors.begin(), registered_actors.end(), mit, find_parent_module()); + + //std::cerr << "There is " << modules.size() << " registered modules" << "\n"; + Gravel::Pointer::Module module = *(modules.begin()); + + boost::function register_function = boost::bind(register_module(), module, _1); + + std::for_each(actors.begin(), actors.end(), register_function ); + + + + } + + + bool Gravel::Assignment::operator<(const Gravel::Assignment& cmp) const { + return assignment.get() < cmp.assignment.get(); + } + + + + Gravel::Assignment::Assignment(Gravel::Pointer::Assignment assignment) : assignment(assignment) { + + } + Gravel::Implementation::Assignment::Assignment(Gravel::Pointer::Symbol& sym, Gravel::Pointer::Expression& exp, unsigned delay) : + delay(delay) + { + normal.insert(Gravel::Assignment::AssignmentPair(sym, exp)); + } + + Gravel::Collection::Symbol Gravel::Assignment::get_assigned_symbols() const { + return assignment->get_assigned_symbols(); + } + + Gravel::Collection::Symbol Gravel::Implementation::Assignment::get_assigned_symbols() const { + Gravel::Collection::Symbol keys; + + std::transform(normal.begin(), normal.end(), + std::inserter(keys, keys.begin()), + boost::bind(&std::map::value_type::first, _1)); + + return keys; + } \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d2531cb..1c4e2e6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,13 +1,14 @@ -set (GRAVEL_SRC Context Module Object Symbol Expression Actor) +set (GRAVEL_SRC Context Module Object Symbol Expression Actor StateMachine Assignment) set (CODEGEN_SRC codegen/Generator codegen/Definitions codegen/SequentialModule codegen/AssignmentModule codegen/GuardModule codegen/ForModule - codegen/CodegenModule) + codegen/CodegenModule + ) add_library(gravel ${GRAVEL_SRC} ${CODEGEN_SRC}) diff --git a/src/Context.cxx b/src/Context.cxx index 89a1102..a2b8a8b 100644 --- a/src/Context.cxx +++ b/src/Context.cxx @@ -1,11 +1,20 @@ -#include -#include #include #include #include #include +#include #include + +#include +#include + +#include +#include + +#include + + Gravel::Context * Gravel::Context::pInstance = NULL; Gravel::Context * Gravel::Context::getInstance() @@ -16,6 +25,17 @@ Gravel::Context * Gravel::Context::getInstance() return pInstance; } +Gravel::Module Gravel::Context::getPointer(Gravel::ModuleImplementation * impl) { + Gravel::Context * ctx = Gravel::Context::getInstance(); + return ctx->getModule(impl->getName()); +} + +Gravel::Module Gravel::Context::getPointer(const Gravel::ModuleImplementation * impl) { + Gravel::Context * ctx = Gravel::Context::getInstance(); + return ctx->getModule(impl->getName()); +} + + Gravel::Context::Context() : tempIdentifier(0) { } @@ -28,170 +48,342 @@ struct name_matches : public std::binary_function match_object(name_matches(), name); - Gravel::ModuleList::iterator mlit = std::find_if(ml.begin(), ml.end(), match_object); + Gravel::ModuleSet::iterator mlit = std::find_if(ml.begin(), ml.end(), match_object); if (mlit == ml.end()) { - throw Gravel::ModuleNotFound(name); + throw Gravel::Exception::ModuleNotFound(name); } else { return *mlit; } } -void Gravel::Context::insert(const Gravel::Module& module) { +void Gravel::Context::insert(const Gravel::Pointer::Module& module) { ml.insert(module); } -bool Gravel::Context::exists(const Gravel::Module& module) { - // lookup in module - return (ml.find(module) != ml.end()); - +void Gravel::Context::insert(const Gravel::Pointer::Assignment& assignment) { + // std::cerr << "Inserted Assignment\n"; + al.insert(assignment); } -void Gravel::Context::insert(const Gravel::Module& module, const Gravel::Symbol symbol, const Gravel::Interface::Symbol::Direction& direction) { - SymbolKey key(module, direction); - sm.insert(std::pair(key, symbol)); +void Gravel::Context::insert(Gravel::Pointer::Module& module, Gravel::Pointer::Symbol symbol, const Gravel::Interface::Symbol::Direction& direction) { + SymbolKey key(module, symbol); + sm.insert(std::pair(key, direction)); + + Gravel::Pointer::Actor actor = boost::static_pointer_cast(symbol); + + am.insert(std::pair(actor, module)); + propagate(); } -struct symbol_matches : public std::binary_function, Gravel::Symbol, bool> { - bool operator()(const std::pair& p, const Gravel::Symbol & n) const { - return (p.second) == n; +struct symbol_matches : public std::binary_function, Gravel::Interface::Symbol::Direction, bool> { + bool operator()(const std::pair & p, const Gravel::Interface::Symbol::Direction & direction) const { + return (p.second) == direction; } }; +bool Gravel::Context::exists(const Gravel::Pointer::Module& module) { + return (ml.find(module) != ml.end()); +} + + -bool Gravel::Context::exists(const Gravel::Module& module, const Gravel::Symbol symbol, const Gravel::Interface::Symbol::Direction& direction) { - SymbolKey key(module, direction); +bool Gravel::Context::exists(const Gravel::Pointer::Module& module, const Gravel::Pointer::Symbol symbol, const Gravel::Interface::Symbol::Direction& direction) { + SymbolKey key(module, symbol); ConstSymbolRange sr = sm.equal_range(key); - std::binder2nd match_object((symbol_matches()), symbol); + std::binder2nd match_object((symbol_matches()), direction); // Gravel::ModuleList::iterator mlit = std::find_if(ml.begin(), ml.end(), match_object); return (std::find_if(sr.first, sr.second, match_object) == sr.second);//std::bind2nd, Gravel::Symbol>(eq,symbol)) != sr.second); } -Gravel::ConstModuleList Gravel::Context::getModules() const { +Gravel::ModuleSet Gravel::Context::getModules() const { return ml; } -Gravel::ConstSymbolList Gravel::Context::getSymbols(const Gravel::Module& module) const { +Gravel::SymbolSet Gravel::Context::getSymbols(const Gravel::Pointer::Module& module) const { Gravel::ConstSymbolMapIterator mit; - Gravel::SymbolList sl; + Gravel::SymbolSet sl; for (mit = sm.begin() ; mit != sm.end() ; mit++ ) { Gravel::SymbolKey sk = mit->first; if (sk.first == module) { - sl.insert(mit->second); + sl.insert(Gravel::Symbol(sk.second)); } } return sl; } -Gravel::ConstExpressionList Gravel::Context::getExpressions(const Gravel::Module& module) const { - Gravel::ConstExpressionMapIterator mit; - - Gravel::ExpressionList el; + + +class find_parent_module : public std::unary_function { +public : + const Gravel::Module operator() (const Gravel::Symbol& symbol) { + + + Gravel::Context * ctx = Gravel::Context::getInstance(); + + Gravel::Module module = symbol.getOwner(); + + return module; + + }; +}; + + +Gravel::AssignmentSet Gravel::Context::getAssignments(const Gravel::Module& module) const { + Gravel::AssignmentSet::iterator it; + + Gravel::AssignmentSet module_assignments; + + + for (it = al.begin() ; it != al.end() ; it++) { + Gravel::Pointer::Assignment assignment = *it; + Gravel::Collection::Symbol s = assignment->get_assigned_symbols(); + std::set m; + + // if all symbols in module, add this assignment to module_assignments + + std::insert_iterator > sit = std::inserter(m, m.begin()); + + std::transform(s.begin(), s.end(), sit, find_parent_module()); - for (mit = em.begin() ; mit != em.end() ; mit++ ) { - if (mit->second == module) { - el.insert(mit->first); - } + // do some magic binding to a predicate here + if (m.size() > 1) { + assert(false); // should throw exception + } + if (m.find(module) != m.end()) module_assignments.insert(*it); + } - return el; + + return module_assignments; } -Gravel::ConstSymbolRange Gravel::Context::getSymbols(Gravel::Module module, Symbol::Direction direction) const { - SymbolKey key(module, direction); - return sm.equal_range(key); +Gravel::SymbolSet Gravel::Context::getSymbols(Gravel::Pointer::Module module, Symbol::Direction direction) const { + + Gravel::SymbolMap::const_iterator sit; + + Gravel::SymbolSet ss; + + for (sit = sm.begin() ; sit != sm.end() ; sit++) { + + + Gravel::SymbolKey key = sit->first; + + if (key.first == module && sit->second == direction) { + ss.insert(Gravel::Symbol(key.second)); + } + + + } + + return ss; } void Gravel::Context::emit(std::ostream& os) { // emit to file + ModuleSet::iterator mlit; + os << "//" << ml.size() << " Modules" << std::endl; + for (mlit = ml.begin() ; mlit != ml.end() ; mlit++ ) { + Gravel::Pointer::Module mptr = *mlit; + Gravel::Module(mptr).emit(os); + } + - os << ml.size() << std::endl; } void Gravel::Context::printSymbols(std::ostream& os) { - Gravel::SymbolMapIterator sit; + Gravel::SymbolMap::iterator sit; for (sit = sm.begin() ; sit != sm.end() ; sit++) { Gravel::SymbolKey key = sit->first; - Gravel::Symbol symbol = sit->second; + Gravel::Symbol symbol = Gravel::Symbol(key.second); os << symbol << "\n"; } } -bool Gravel::Context::isOwned(const Gravel::Symbol& symbol) { - normalize(); - - Gravel::ConstSymbolMapIterator mit; - for (mit = sm.begin() ; mit != sm.end() ; mit++ ) { - if (mit->second == symbol) { - return true; - } - } - return false; +bool Gravel::Context::isOwned(const Gravel::Pointer::Actor& actor) { + + Gravel::ActorMap::iterator amit = am.find(actor); + + return (amit != am.end()); } -bool Gravel::Context::isOwned(const Gravel::Expression& expression) { - normalize(); + +Gravel::SymbolVector Gravel::Context::getDelayedSymbols(const Gravel::Symbol&) { + return Gravel::SymbolVector(); +} + +Gravel::Symbol Gravel::Context::getRegisteredSymbol(const Gravel::Symbol&) { - Gravel::ConstExpressionMapIterator mit; - for (mit = em.begin() ; mit != em.end() ; mit++ ) { - if (mit->first == expression) { - return true; - } - } - return false; } +void Gravel::Context::insert(Gravel::Pointer::Actor actor, Gravel::Pointer::Module module) { + am.insert(std::pair(actor, module)); +} -Gravel::Module Gravel::Context::owner(const Gravel::Symbol&) { - +void Gravel::Context::insert(Gravel::Pointer::Actor actor) { + as.insert(actor); } + + + //Gravel::Pointer::GraphNode output = actor->getOutput(); + + + + //Gravel::GraphNode::ConstNodeRange inputs = actor->getInputs(); + + // boost::function) > my_func = boost::bind(insert_parent_function(), actor, _1); + + // for_each(inputs.first, inputs.second, my_func ); + + -Gravel::Module Gravel::Context::owner(const Gravel::Expression&) { +/*void Gravel::Context::insert(Gravel::Pointer::Actor actor, Gravel::Pointer::GraphNode node) { + + // typedef std::map ActorMap; + + am.insert(std::pair(node, actor)); +}*/ + + +struct insert_parent_function : public std::binary_function, void> { +void operator()(const Gravel::Pointer::Actor& actor, std::pair gn) { + // get gravel context + Gravel::Context * ctx = Gravel::Context::getInstance(); + Gravel::Pointer::GraphNode node = gn.second; + + // ctx->insert(actor, node); } - +}; -void find_edges(const Gravel::Module& module, const Gravel::Symbol& symbol) { + Gravel::Pointer::Actor Gravel::Context::getParent(Gravel::Pointer::GraphNode candidate) { + + + Gravel::ActorSet::iterator amit; + + + for (amit = as.begin() ; amit != as.end() ; amit++ ) { + + Gravel::Pointer::Actor actor = *amit; + Gravel::Pointer::GraphNode output = actor->getOutput(); + + if (output == candidate) { + + return actor; + } + + Gravel::GraphNode::ConstNodeRange inputs = actor->getInputs(); + Gravel::GraphNode::ConstNodeIterator cit; + for (cit = inputs.first ; cit != inputs.second ; cit++) { + Gravel::Pointer::GraphNode input = cit->second; + if (input == candidate) { + return actor; + } + } + + } + + throw Gravel::Exception::ActorNotFound(); + + } + +/*Gravel::Module Gravel::Context::owner(const Gravel::Pointer::Actor& actor) { + + ActorMap::iterator smit = am.find(actor); - // register symbol with module + if (smit == am.end()) throw Gravel::Exception::OwnerNotFound(); - // add all the inputs and outputs for this symbol to symbol and expression lists(registering them with module) + return Gravel::Module(smit->second); +}*/ + +Gravel::Pointer::Module Gravel::Context::owner(const Gravel::Pointer::Actor& actor) { + ActorMap::iterator smit = am.find(actor); + + if (smit == am.end()) throw Gravel::Exception::OwnerNotFound(); -} -void find_edges(const Gravel::Module& module, const Gravel::Expression& expression) { - // register symbol with module + return smit->second; - // add all the inputs and outputs for this expression to symbol and expression lists(registering them with module) } -unsigned add_edges(const Gravel::Module& module) { + +struct add_iterator_to_module : public std::binary_function, void> { +void operator()(const Gravel::Pointer::Module& module, std::pair node_pair) { + Gravel::Context * ctx = Gravel::Context::getInstance(); + Gravel::Pointer::Actor actor = ctx->getParent(node_pair.second); + + ctx->insert(actor, module); + }; +}; + +struct add_node_to_module : public std::binary_function { +void operator()(const Gravel::Pointer::Module& module, Gravel::Pointer::GraphNode node) { Gravel::Context * ctx = Gravel::Context::getInstance(); + Gravel::Pointer::Actor actor = ctx->getParent(node); - Gravel::ConstSymbolList sl = ctx->getSymbols(module); - Gravel::ConstExpressionList el = ctx->getExpressions(module); + ctx->insert(actor, module); + + }; +}; + +void find_edges(const Gravel::Pointer::Module& module, const Gravel::Pointer::Actor& actor) { + + // add all the inputs and outputs for this symbol to symbol and expression lists(registering them with module) + + Gravel::Pointer::GraphNode output = actor->getOutput(); - unsigned size = sl.size() + el.size(); + Gravel::GraphNode::ConstNodeRange inputs = actor->getInputs(); + + boost::function) > add_iterator = boost::bind(add_iterator_to_module(),module, _1); + boost::function add_node = boost::bind(add_node_to_module(),module, _1); + add_node(output); - for (Gravel::ConstSymbolList::iterator cslit = sl.begin() ; cslit != sl.end() ; cslit++) { - find_edges(module, *cslit); - } - for (Gravel::ConstExpressionList::iterator celit = el.begin() ; celit != el.end() ; celit++) { - find_edges(module, *celit); - } + std::for_each(inputs.first, inputs.second, add_iterator); + - return sl.size() + el.size() - size; +} + + +unsigned Gravel::Context::add_edges(const Gravel::Pointer::Module& module) { + + Gravel::Context * ctx = Gravel::Context::getInstance(); + + //Gravel::ConstSymbolList sl = ctx->getSymbols(module); + + // Find the number of actors with a mapping + unsigned size = am.size(); + + ActorMap::const_iterator amit; + + AssignmentSet::const_iterator asit; + + for (amit = am.begin() ; amit != am.end() ; amit++ ) { + if (amit->second == module) { + find_edges(amit->second, amit->first); + + } + } + // std::cerr << "Looking for assignments\n"; + for (asit = al.begin() ; asit != al.end() ; asit++ ) { + // std::cerr << "Found Assignment\n"; + Gravel::Pointer::Assignment assignment = *asit; + assignment->propagate(); + } + + unsigned edges_added = am.size() - size; + + return edges_added; } std::string Gravel::Context::getUniqueIdentifier() { @@ -202,16 +394,21 @@ std::string Gravel::Context::getUniqueIdentifier() { } -void Gravel::Context::normalize() { +void Gravel::Context::propagate() { - Gravel::ModuleList::iterator mlit; + Gravel::ModuleSet::iterator mlit; for(mlit = ml.begin() ; mlit != ml.end() ; mlit++) { - - while ( add_edges(*mlit) != 0) {;} - + + unsigned edges = 0; + do { + edges = add_edges(*mlit); + + } + while ( edges != 0); + } - + // go through all modules // collect all registered nodes // register all edges from those nodes diff --git a/src/Expression.cxx b/src/Expression.cxx index 02718ab..13c763a 100644 --- a/src/Expression.cxx +++ b/src/Expression.cxx @@ -13,30 +13,179 @@ } */ - const Gravel::Symbol Gravel::Implementation::Expression::getSymbol() { +void Gravel::Expression::attach(const Gravel::Pointer::Module& module) const { + + Gravel::Context * ctx = Gravel::Context::getInstance(); + ctx->insert(ptr, module); +} + +Gravel::Module Gravel::Expression::getOwner() const { + Gravel::Context * ctx = Gravel::Context::getInstance(); + return ctx->owner(ptr); +} + + const Gravel::Pointer::Symbol Gravel::Implementation::Expression::getSymbol() const { + // find output graphNode + + Gravel::Pointer::GraphNode cr = this->getOutput(); + + // find nodes connected to the output graphnode + + Gravel::GraphNode::BackMapRange br = Gravel::GraphNode::getConnections(cr); + + Gravel::GraphNode::BackMapIterator bit; + for (bit = br.first ; bit != br.second ; bit++ ) { + + Gravel::WeakPointer::GraphNode wgn = bit->second; + Gravel::Pointer::GraphNode gn = wgn.lock(); + + boost::shared_ptr te = + boost::dynamic_pointer_cast(Gravel::GraphNode::getParent(gn)); + + if (te) { + return te->getSymbol(); + } + + } + assert(false); + // one should be a TermExpression + + // return symbol from TermExpression } -Gravel::Implementation::TermExpression::TermExpression(Gravel::Symbol sym) : symbol(sym) { + + + /* Gravel::Implementation::TermExpression::TermExpression(Gravel::Constant constant) { + + boost::shared_ptr onode = boost::shared_ptr(new Gravel::GraphNode()); + nodes.insert(Gravel::GraphNode::NodePair(Gravel::Output, onode)); + GraphNode::connect(inode, sym.getOutput()); + + }*/ + + + +Gravel::Implementation::TermExpression::TermExpression(Gravel::Pointer::Constant constant) { + + + Gravel::Pointer::GraphNode onode = Gravel::GraphNode::Create(nodes, Gravel::Output); + + Gravel::Pointer::GraphNode inode = Gravel::GraphNode::Create(nodes, Gravel::Input); + + GraphNode::connect(inode, constant->getOutput()); +} + +Gravel::Implementation::TermExpression::TermExpression(Gravel::Pointer::Symbol symbol) { + + + Gravel::Pointer::GraphNode onode = Gravel::GraphNode::Create(nodes, Gravel::Output); + + Gravel::Pointer::GraphNode inode = Gravel::GraphNode::Create(nodes, Gravel::Input); + + + GraphNode::connect(inode, symbol->getOutput()); +} + + + + /*try { + const Gravel::Implementation::Constant & constant = dynamic_cast(actor); + + // build a symbol to accept the constant + Gravel::TemporarySymbol symbol; + // get inputs of symbol and connect to this term expression output + + Gravel::GraphNode::ConstNodeRange cr = this->getInputs(); + Gravel::GraphNode::ConstNodeIterator nit; + for (nit = cr.first ; nit != cr.second ; nit++) { + GraphNode::connect(onode, nit->second); + } + + + } catch(std::bad_cast&) { + + } + */ // normalize expression and symbol ownership - - boost::shared_ptr onode = boost::shared_ptr(new Gravel::GraphNode()); - nodes.insert(Gravel::GraphNode::NodePair(Gravel::Output, onode)); - boost::shared_ptr inode = boost::shared_ptr(new Gravel::GraphNode()); - nodes.insert(Gravel::GraphNode::NodePair(Gravel::Input, inode)); - // GraphNode::connect(inode, sym.getOutput()); +Gravel::Expression::~Expression() { + Gravel::Context * ctx = Gravel::Context::getInstance(); + if (! ctx->isOwned(ptr) ) { + std::cout << "Orphan Expression was destroyed" << std::endl; + } } -const Gravel::Symbol Gravel::Implementation::TermExpression::getSymbol() { - return symbol; + + +const Gravel::Pointer::Symbol Gravel::Implementation::TermExpression::getSymbol() const { + + // get input actors + + //Gravel::Context * ctx = Gravel::Context::getInstance(); + + Gravel::GraphNode::ConstNodeRange cr = this->getInputs(); + + Gravel::GraphNode::ConstNodeIterator nit; + for (nit = cr.first ; nit != cr.second ; nit++) { + + // if input actor is a constant, return the symbol attached to output actor + + Gravel::GraphNode::BackMapRange br = Gravel::GraphNode::getConnections(nit->second); + + Gravel::GraphNode::BackMapIterator bit; + + for (bit = br.first ; bit != br.second ; bit++ ) { + + Gravel::WeakPointer::GraphNode wgn = bit->second; + Gravel::Pointer::GraphNode gn = wgn.lock(); + + boost::shared_ptr constant = + boost::dynamic_pointer_cast(Gravel::GraphNode::getParent(gn)); + + if (constant ) { + + Gravel::Pointer::GraphNode onode = this->getOutput(); + + Gravel::GraphNode::BackMapRange bnr = Gravel::GraphNode::getConnections(onode); + + Gravel::WeakPointer::GraphNode bwgn = bnr.first->second; + Gravel::Pointer::GraphNode bgn = bwgn.lock(); + + return boost::dynamic_pointer_cast(Gravel::GraphNode::getParent(bgn)); + + } + + boost::shared_ptr symbol = + boost::dynamic_pointer_cast(Gravel::GraphNode::getParent(gn)); + + if (symbol != boost::shared_ptr() ) { + + return symbol; + + } + + } + // if input actor is a symbol, return the symbol + + + } + assert(false); + + } + bool Gravel::Expression::Owned(const Gravel::Expression& e) { + Gravel::Context * ctx = Gravel::Context::getInstance(); + + return ctx->isOwned(e.ptr); + } + Gravel::Implementation::BinaryExpression::BinaryExpression(const Gravel::Expression lhs, const Gravel::Expression rhs, const Gravel::BinaryExpressionType binop) : @@ -45,21 +194,16 @@ lhs(lhs), rhs(rhs), binop(binop) { Gravel::Context * ctx = Context::getInstance(); - if (ctx->isOwned(lhs) && ctx->isOwned(rhs)) { - if (ctx->owner(lhs) != ctx->owner(rhs)) { + if (Gravel::Expression::Owned(lhs) && Gravel::Expression::Owned(rhs)) { + if (lhs.getOwner() != rhs.getOwner()) { // throw InvalidExpressionException } } - boost::shared_ptr lnode = boost::shared_ptr(new Gravel::GraphNode()); - nodes.insert(Gravel::GraphNode::NodePair(Gravel::Input, lnode)); - boost::shared_ptr rnode = boost::shared_ptr(new Gravel::GraphNode()); - nodes.insert(Gravel::GraphNode::NodePair(Gravel::Input, rnode)); - boost::shared_ptr onode = boost::shared_ptr(new Gravel::GraphNode()); - nodes.insert(Gravel::GraphNode::NodePair(Gravel::Output, rnode)); - - - + Gravel::Pointer::GraphNode lnode = Gravel::GraphNode::Create(nodes, Gravel::Input); + Gravel::Pointer::GraphNode rnode = Gravel::GraphNode::Create(nodes, Gravel::Input); + Gravel::Pointer::GraphNode onode = Gravel::GraphNode::Create(nodes, Gravel::Output); + GraphNode::connect(rnode, rhs.getOutput()); GraphNode::connect(lnode, lhs.getOutput()); @@ -67,9 +211,9 @@ lhs(lhs), rhs(rhs), binop(binop) { GraphNode::ConstNodeRange nr = temp.getInputs(); for (GraphNode::ConstNodeIterator ni = nr.first ; ni != nr.second ; ni++) { - GraphNode::connect(onode, Gravel::GraphNode::NodePtr(ni->second)); + GraphNode::connect(onode, Gravel::Pointer::GraphNode(ni->second)); } - ctx->normalize(); + ctx->propagate(); // normalize expression and symbol ownership @@ -106,11 +250,11 @@ lhs(lhs), rhs(rhs), binop(binop) { } */ -Gravel::GraphNode::NodePtr Gravel::Expression::getOutput() const { +Gravel::Pointer::GraphNode Gravel::Expression::getOutput() const { return ptr->getOutput(); } -void Gravel::Expression::setWidth(Gravel::GraphNode::NodePtr node, unsigned width) { +void Gravel::Expression::setWidth(Gravel::Pointer::GraphNode node, unsigned width) { return ptr->setWidth(node, width); } @@ -118,23 +262,92 @@ void Gravel::Expression::setWidth(Gravel::GraphNode::NodePtr node, unsigned widt return ptr->getInputs(); } - Gravel::Expression::Expression(int) { + Gravel::Implementation::NullExpression::NullExpression() { + + } + + std::ostream& Gravel::Implementation::NullExpression::emit(std::ostream&) const { + + } + + Gravel::Expression::Expression(int i) : ptr(Gravel::Pointer::Expression(new Gravel::Implementation::TermExpression( + Gravel::Pointer::Constant(new Gravel::Implementation::Constant(i))))) { + ptr->initialize(ptr); + + } // promotion of int, creates constant and registers termExpression Gravel::Expression::Expression(const Gravel::Pointer::Expression e) : ptr(e) { - + + } + + const Gravel::Pointer::Symbol Gravel::Expression::getSymbol() const{ + return ptr->getSymbol(); } Gravel::Expression Gravel::operator*(const Gravel::Expression& lhs, const Gravel::Expression& rhs) { Gravel::Pointer::Expression ptr(new Gravel::Implementation::BinaryExpression(lhs,rhs, Gravel::Multiply)); + ptr->initialize(ptr); + + return Gravel::Expression(ptr); +} + +Gravel::Expression Gravel::operator+(const Gravel::Expression& lhs, const Gravel::Expression& rhs) { + + Gravel::Pointer::Expression ptr(new Gravel::Implementation::BinaryExpression(lhs,rhs, Gravel::Add)); + // GraphNode::setParents(ptr); - + ptr->initialize(ptr); + return Gravel::Expression(ptr); } + Gravel::RelativeExpression operator==(const Gravel::Symbol& lhs, const Gravel::Expression& rhs) { + Gravel::Pointer::Expression ptr(new Gravel::Implementation::RelativeExpression(lhs, rhs, Gravel::Equals)); + + ptr->initialize(ptr); + + return Gravel::RelativeExpression(ptr); + } + + + std::ostream& Gravel::Implementation::RelativeExpression::emit(std::ostream& ) const { + + } + + Gravel::Implementation::RelativeExpression::RelativeExpression(const Gravel::Expression lhs, const Gravel::Expression rhs, const Gravel::RelativeExpressionType) { + + Gravel::Pointer::GraphNode lnode = Gravel::GraphNode::Create(nodes, Gravel::Input); + Gravel::Pointer::GraphNode rnode = Gravel::GraphNode::Create(nodes, Gravel::Input); + Gravel::Pointer::GraphNode onode = Gravel::GraphNode::Create(nodes, Gravel::Output); + + GraphNode::connect(rnode, rhs.getOutput()); + GraphNode::connect(lnode, lhs.getOutput()); + + } + + Gravel::RelativeExpression::RelativeExpression(const Gravel::Pointer::Expression e) : Gravel::Expression(e) { + + } + + Gravel::RelativeExpression Gravel::operator==(Gravel::Symbol const& lhs, Gravel::Expression const& rhs) { + Gravel::Pointer::Expression ptr(new Gravel::Implementation::RelativeExpression(lhs, rhs, Gravel::Equals)); + + ptr->initialize(ptr); + return RelativeExpression(ptr); + } + + /* Gravel::RelativeExpression operator==(const Gravel::Symbol& lhs, const Gravel::Constant& rhs) { + Gravel::Expression elhs = lhs; + Gravel::Expression erhs = rhs; + Gravel::Pointer::Expression ptr(new Gravel::Implementation::RelativeExpression(elhs, erhs, Gravel::Equals)); + + return Gravel::RelativeExpression(ptr); + } +*/ const Gravel::Expression& Gravel::Implementation::BinaryExpression::getLHS() { return lhs; } @@ -182,11 +395,16 @@ Gravel::Expression Gravel::operator*(const Gravel::Expression& lhs, const Gravel std::ostream& Gravel::Implementation::BinaryExpression::emit(std::ostream& os) const { try { - Gravel::Symbol tmp = this->getOutput(); - Gravel::Symbol lhs = lhs.getOutput(); - Gravel::Symbol rhs = rhs.getOutput(); + Gravel::Pointer::GraphNode onode = this->getOutput(); + + Gravel::Collection::GraphNodeRange inodes = this->getInputs(); + + Gravel::Symbol output = GraphNode::getParent(onode); + + Gravel::Symbol lhs = GraphNode::getParent(inodes.first); + Gravel::Symbol rhs = GraphNode::getParent(inodes.second); - os << "assign" << " " << tmp << "=" << lhs << " "; + os << "assign" << " " << output << "=" << lhs << " "; switch(binop) { case Sub : { diff --git a/src/Module.cxx b/src/Module.cxx index 46ae771..bed6b73 100644 --- a/src/Module.cxx +++ b/src/Module.cxx @@ -18,30 +18,38 @@ */ -Gravel::ModuleNotFound::ModuleNotFound(std::string _name) : name(_name) {}; +Gravel::Exception::ModuleNotFound::ModuleNotFound(std::string _name) : name(_name) {}; -const char * Gravel::ModuleNotFound::what() const throw() { +const char * Gravel::Exception::ModuleNotFound::what() const throw() { std::stringstream ss; ss << "Module (\"" << name << "\") Not Found "; return ss.str().c_str(); } -Gravel::ModuleNotFound::~ModuleNotFound() throw() { +Gravel::Exception::ModuleNotFound::~ModuleNotFound() throw() { } - - - Gravel::Module::Module(){ + /* Gravel::Module::Module(){ } - - Gravel::Module::Module(const std::string& name) : module(ModulePtr(new ModuleImplementation(name))) { + */ + + + Gravel::Module::Module(const std::string& name) : module(Pointer::Module(new ModuleImplementation(name))) { + + } - +/* Gravel::Module::Module(const Module & m) : module(m.module) { + + } + */ + Gravel::Module::Module(Pointer::Module m ) : module(m) { + + } /* template class FormattedBuffer : public std::streambuf, SeparatorInterface { @@ -97,62 +105,71 @@ Gravel::ModuleNotFound::~ModuleNotFound() throw() { //operator<<() std::ostream& Gravel::Module::emit(std::ostream& os) const { + + os << "module " << this->getName(); Gravel::Context * ctx = Gravel::Context::getInstance(); + - Gravel::ConstSymbolRange si = ctx->getSymbols(*this, Gravel::Symbol::Input); - Gravel::ConstSymbolRange so = ctx->getSymbols(*this, Gravel::Symbol::Output); + Gravel::SymbolSet si = ctx->getSymbols(module, Gravel::Symbol::Input); + Gravel::SymbolSet so = ctx->getSymbols(module, Gravel::Symbol::Output); - os << "(" << FormattedList(si.first, si.second) << FormattedList(so.first, so.second) << ");" << "\n"; + os << "(" << FormattedList(si.begin(), si.end()) << FormattedList(so.begin(), so.end()) << ");" << "\n"; - Gravel::ConstSymbolMapIterator it; + Gravel::SymbolSet::iterator it; - for (it = si.first ; it != si.second ; it++) { - Gravel::SymbolDeclaration decl(it->second); + for (it = si.begin() ; it != si.end() ; it++) { + Gravel::SymbolDeclaration decl(*it); os << decl << "\n"; } - for (it = so.first ; it != so.second ; it++) { - Gravel::SymbolDeclaration decl(it->second); + for (it = so.begin() ; it != so.end() ; it++) { + Gravel::SymbolDeclaration decl(*it); os << decl << "\n"; } - Gravel::ConstExpressionList el = ctx->getExpressions(*this); + Gravel::AssignmentSet el = ctx->getAssignments(module); + + Gravel::AssignmentSet::iterator elit; - Gravel::ConstExpressionList::iterator elit; - for (elit = el.begin() ; elit != el.end() ; elit++) { - Gravel::Expression expression = *elit; - os << expression << "\n"; + Gravel::Assignment assignment = *elit; + assignment.emit(os); } os << "endmodule" << "\n"; return os; + } + + + + const std::string Gravel::Module::getName() const { return module->getName(); } - void Gravel::Module::operator>>(Symbol& gs) { + void Gravel::Module::operator>>(Symbol& symbol) { // lookup context Gravel::Context * context = Gravel::Context::getInstance(); // register a copy of the shared_ptr - if (!context->exists(*this)) { - context->insert(*this); + if (!context->exists(module)) { + context->insert(module); } - context->insert(*this, gs, Gravel::Interface::Symbol::Output); - + symbol.attach(module, Gravel::Interface::Symbol::Output); } - void Gravel::Module::operator<<(Symbol& gs){ + + + void Gravel::Module::operator<<(Symbol& symbol){ Gravel::Context * context = Gravel::Context::getInstance(); // register a copy of the shared_ptr - if (!context->exists(*this)) { - context->insert(*this); + if (!context->exists(module)) { + context->insert(module); } - context->insert(*this, gs, Gravel::Interface::Symbol::Input); + symbol.attach(module, Gravel::Interface::Symbol::Input); } bool Gravel::Module::operator<(const Module & rhs) const { @@ -165,6 +182,13 @@ Gravel::ModuleNotFound::~ModuleNotFound() throw() { } + bool Gravel::ModuleImplementation::operator<(const ModuleImplementation & rhs) const { + if (getName() < rhs.getName()) { + return true; + } + return false; + } + bool Gravel::Module::operator!=(const Module & m) const { if (m.module == module) { return false; @@ -179,27 +203,35 @@ Gravel::ModuleNotFound::~ModuleNotFound() throw() { return false; } - + /* Gravel::Module & Gravel::Module::operator=(const Gravel::Module & rhs) { + if (this = &rhs) { + return *this; + } + module = rhs.module; + return *this; + } + */ /* Module Implementation */ - Gravel::ModuleInstantiation::ModuleInstantiation(const std::string& name, const Gravel::Module module, const InputMap inputs) : - name(name), module(module), inputs(inputs) { + Gravel::InstantiatedModule::InstantiatedModule(const std::string& name, const Gravel::Module module, const SymbolMap connections) : + name(name), module(module), connections(connections) { } - - + Gravel::ModuleImplementation::ModuleImplementation() { assert(false); } Gravel::ModuleImplementation::ModuleImplementation(const std::string& name_) : name(name_) { - + + + } - const std::string Gravel::ModuleImplementation::getName() { + const std::string Gravel::ModuleImplementation::getName() const { return name; } \ No newline at end of file diff --git a/src/StateMachine.cpp b/src/StateMachine.cpp deleted file mode 100644 index 2cd898f..0000000 --- a/src/StateMachine.cpp +++ /dev/null @@ -1,14 +0,0 @@ -class StateMachine : SynthesizableModule{ - -}; -addTransition - -StateMachine sm; - -Symbol x; -Symbol y; - -sm.addTransition( "S1" >> "S2" ) -> ( y > x ); - -sm.addAction ( "S1") -> ( x2 += x + 1); - diff --git a/src/StateMachine.cxx b/src/StateMachine.cxx new file mode 100644 index 0000000..f18925f --- /dev/null +++ b/src/StateMachine.cxx @@ -0,0 +1,65 @@ + +/*class StateMachine : SynthesizableModule{ + +}; +addTransition + +StateMachine sm; + +Symbol x; +Symbol y; + +sm.addTransition( "S1" >> "S2") <= ( y > x ); + +sm.addAction ( "S1" ) <= ( x2 += x + 1); + +*/ + +#include + +#include + + Gravel::StateMachine::StateMachine(Gravel::State state) : resetState(state) { + + } + + void Gravel::StateMachine::synthesize() { + + } + Gravel::StateTransition Gravel::StateMachine::addTransition(StateTransition) { + + } + + Gravel::StateAction Gravel::StateMachine::addAction(Gravel::State s) { + + } + + Gravel::State::State(const std::string& ) { + + } + + void Gravel::StateTransition::condition(Gravel::RelativeExpression) { + + } + Gravel::StateAction Gravel::StateAction::action(Gravel::Assignment) { + + } + + Gravel::StateTransition * Gravel::StateTransition::operator ->() { + return this; + + } + + Gravel::StateAction * Gravel::StateAction::operator ->() { + return this; + } + + Gravel::StateAction Gravel::StateAction::operator ()(Gravel::Assignment) { + return *this; + } + + + Gravel::StateTransition operator>> (Gravel::State, Gravel::State) { + Gravel::StateTransition a; + return a; + } \ No newline at end of file diff --git a/src/Symbol.cxx b/src/Symbol.cxx index dcbdd60..929a784 100644 --- a/src/Symbol.cxx +++ b/src/Symbol.cxx @@ -1,20 +1,47 @@ #include -#include +//#include -#include "gravel/Expression.h" +#include +#include +#include -#include "gravel/private/Expression.h" -#include "gravel/Context.h" +#include Gravel::Symbol::Symbol() { } - Gravel::Symbol::Symbol(const std::string& name_) : symbol(new Gravel::Implementation::Symbol(name_)) { + Gravel::Symbol::~Symbol() { + + + Gravel::Context * ctx = Gravel::Context::getInstance(); + if (!ctx->isOwned(symbol)) { + std::cout << "Orphan Symbol (" << getName() << ") was destroyed" << std::endl; + } + +} + + void Gravel::Symbol::attach(const Gravel::Pointer::Module& module) const { + // dynamic cast to a symbol + // attach to context + Gravel::Context * ctx = Gravel::Context::getInstance(); + ctx->insert(symbol, module); + + + } + +Gravel::Module Gravel::Symbol::getOwner() const { + Gravel::Context * ctx = Gravel::Context::getInstance(); + // std::cout << "Finding owner for " << symbol->getName() << "\n"; + return ctx->owner(symbol); + } + + Gravel::Symbol::Symbol(const std::string& name_) : symbol(new Gravel::Implementation::Symbol(name_)) { + symbol->initialize(symbol); } const std::string Gravel::Symbol::getName() const { return symbol->getName(); @@ -25,39 +52,78 @@ return symbol->getType(); } - Gravel::Implementation::Symbol::Symbol(const std::string& name_) : name(name_) { + Gravel::Implementation::Symbol::Symbol(const std::string& name_) : name(name_) { + + Gravel::Pointer::GraphNode inode = Gravel::GraphNode::Create(nodes, Gravel::Input); + Gravel::Pointer::GraphNode onode = Gravel::GraphNode::Create(nodes, Gravel::Output); } + Gravel::Implementation::Constant::Constant(int i) : value(i) { + Gravel::Pointer::GraphNode onode = Gravel::GraphNode::Create(nodes, Gravel::Output); + } + + Gravel::Pointer::GraphNode Gravel::Implementation::Constant::getOutput() const { + Gravel::GraphNode::ConstNodeIterator cni = this->nodes.find(Gravel::Output); + + if (cni == nodes.end()) { + throw Gravel::Exception::GraphNodeNotFound(); + } + + return cni->second; + } + + Gravel::GraphNode::ConstNodeRange Gravel::Implementation::Constant::getInputs() const { + // return empty set + return Gravel::GraphNode::ConstNodeRange(nodes.end(), nodes.end()); + } + Gravel::TemporarySymbol::TemporarySymbol() : Gravel::Symbol(Context::getInstance()->getUniqueIdentifier()) { } - Gravel::Symbol::Symbol(boost::weak_ptr graphNode ) { +// Make t + +Gravel::Pointer::Symbol FindSymbol(Gravel::Pointer::Actor actor) { + Gravel::Pointer::Symbol symbol = boost::dynamic_pointer_cast(actor); + Gravel::Pointer::Expression expression = boost::dynamic_pointer_cast(actor); + + if (!symbol) { + + if (expression) { + symbol = expression->getSymbol(); + } else { + Gravel::Pointer::Constant constant = boost::dynamic_pointer_cast(actor); + + Gravel::Pointer::GraphNode onode = constant->getOutput(); + Gravel::GraphNode::BackMapRange range = Gravel::GraphNode::getConnections(onode); + + Gravel::GraphNode::BackMapIterator bit; + + for (bit = range.first ; bit != range.second ; bit++ ) { + + Gravel::WeakPointer::GraphNode wgn = bit->second; + + Gravel::Pointer::GraphNode gn = wgn.lock(); + + actor = Gravel::GraphNode::getParent(gn); + symbol = FindSymbol(actor); + } + + + + } + } + if (!symbol) throw Gravel::SymbolNotFoundException(); + + return symbol; +} + + Gravel::Symbol::Symbol(Gravel::Pointer::Actor actor) { // find graph node connections + + - Gravel::GraphNode::BackMapRange nr = GraphNode::getConnections(graphNode); - - Gravel::GraphNode::BackMapIterator ci; - - - bool found = false; - - for(ci = nr.first ; ci != nr.second ; ci++ ) { - - Gravel::Pointer::Actor actor = GraphNode::getParent(ci->second); - - Gravel::Implementation::TermExpression * te = dynamic_cast< Gravel::Implementation::TermExpression * >(actor.get()); - if(te) { - symbol = te->getSymbol().symbol; - found = true; - break; - } - } - - if (!found) { - throw SymbolNotFoundException(); - } // find out the parent of each connected graph node and try and cast to a term expression @@ -89,10 +155,21 @@ bool Gravel::Symbol::operator==(const Gravel::Symbol& sm) const { */ Gravel::SymbolDeclaration::SymbolDeclaration(const Gravel::Symbol& symbol) : symbol(symbol) { Gravel::Context * ctx = Gravel::Context::getInstance(); - assert(ctx->isOwned(symbol)); + assert (Symbol::Owned(symbol)); } + bool Gravel::Symbol::Owned(const Gravel::Symbol& symbol) { + Gravel::Context * ctx = Gravel::Context::getInstance(); + return ctx->isOwned(symbol.symbol); + } + + +void Gravel::Symbol::attach(Gravel::Pointer::Module& module, const Gravel::Interface::Symbol::Direction& direction) const { + Gravel::Context * ctx = Gravel::Context::getInstance(); + ctx->insert(module, symbol, direction); +} + std::ostream& Gravel::operator<< (std::ostream& os, const Gravel::Symbol& s) { os << s.getName(); return os; @@ -100,7 +177,10 @@ std::ostream& Gravel::operator<< (std::ostream& os, const Gravel::Symbol& s) { unsigned Gravel::Symbol::getSymbolWidth(const Gravel::Symbol& sym) { Gravel::Context * ctx = Gravel::Context::getInstance(); - return (rand() % 16); + + Gravel::Pointer::GraphNode node = sym.getOutput(); + + return node->getWidth(); } const Gravel::Symbol Gravel::SymbolDeclaration::getSymbol() const { @@ -108,15 +188,19 @@ unsigned Gravel::Symbol::getSymbolWidth(const Gravel::Symbol& sym) { } - void Gravel::Symbol::setWidth(Gravel::GraphNode::NodePtr, unsigned) { + void Gravel::Symbol::setWidth(Gravel::Pointer::GraphNode, unsigned) { } - Gravel::GraphNode::NodePtr Gravel::Symbol::getOutput() const { + void Gravel::Implementation::Constant::setWidth(Gravel::Pointer::GraphNode, unsigned int) { } + + Gravel::Pointer::GraphNode Gravel::Symbol::getOutput() const { + return symbol->getOutput(); + } Gravel::GraphNode::ConstNodeRange Gravel::Symbol::getInputs() const { - + return symbol->getInputs(); } @@ -150,21 +234,30 @@ std::ostream& Gravel::operator<< (std::ostream& os, const Gravel::SymbolDeclarat return os; } -Gravel::Symbol Gravel::Symbol::operator=(const Gravel::Expression& rhs) { +Gravel::Assignment Gravel::Symbol::operator=(const Gravel::Expression& rhs) { // assign rhs correctly + Gravel::Symbol lhs = *this; + Gravel::Assignment a = Gravel::Assignment::Create(lhs, rhs, 0); - return *this; + return a; } bool Gravel::Symbol::operator<(const Gravel::Symbol& s) const { return s.symbol.get() < symbol.get(); } - Gravel::Symbol::operator Gravel::Expression() { + Gravel::Symbol::operator const Gravel::Expression() const { // make a concrete termExpression wrapped in a shared ptr // build an Expression from that shared pointer - Gravel::Pointer::Expression ptr(new Gravel::Implementation::TermExpression(*this)); + Gravel::Pointer::Expression ptr(new Gravel::Implementation::TermExpression(this->symbol)); + + ptr->initialize(ptr); + return Gravel::Expression(ptr); - } \ No newline at end of file + } + + Gravel::Symbol::Symbol(Gravel::Pointer::Symbol symbol ) : symbol(symbol) { + + } \ No newline at end of file diff --git a/src/codegen/AssignmentModule.cxx b/src/codegen/AssignmentModule.cxx index 129174a..30c14f4 100644 --- a/src/codegen/AssignmentModule.cxx +++ b/src/codegen/AssignmentModule.cxx @@ -4,7 +4,7 @@ Potholes::Generator::AssModule::AssModule(clast_stmt * stmt) : Potholes::Generator::Module(stmt) { // Calculate Output Range - + std::cout << "Ass"; } void Potholes::Generator::AssModule::register_symbols() { diff --git a/src/codegen/CodegenModule.cxx b/src/codegen/CodegenModule.cxx index 7b6cadd..51692a5 100644 --- a/src/codegen/CodegenModule.cxx +++ b/src/codegen/CodegenModule.cxx @@ -5,4 +5,34 @@ Potholes::Generator::Module::Module(clast_stmt* stmt) : Gravel::ModuleImplementation(Potholes::Generator::create_unique_module_name(stmt)){ -} \ No newline at end of file +} + + void Potholes::Generator::Module::register_symbols() { + + // Gravel::Context * ctx = Gravel::Context::getInstance(); + + Gravel::Module mod = Gravel::Context::getPointer(this); + + // register the control path symbols + + Gravel::Symbol clk("clk"); + mod << clk; + Gravel::Symbol reset("reset"); + mod << reset; + + Gravel::Symbol guard_in("guard_in"); + mod << guard_in; + Gravel::Symbol guard_out("guard_out"); + mod >> guard_out; + Gravel::Symbol update_in("update_in"); + mod << update_in; + Gravel::Symbol update_out("update_out"); + mod >> update_out; + + } + void Potholes::Generator::Module::build_chain() { + + } + + + \ No newline at end of file diff --git a/src/codegen/ForModule.cxx b/src/codegen/ForModule.cxx index 153e4a8..37e3b3f 100644 --- a/src/codegen/ForModule.cxx +++ b/src/codegen/ForModule.cxx @@ -2,19 +2,53 @@ #include #include +#include -Potholes::Generator::ForModule::ForModule(clast_stmt * stmt) : Potholes::Generator::Module(stmt) { +Potholes::Generator::ForModule::ForModule(clast_stmt * stmt) : + Potholes::Generator::Module(stmt), + upper_bound(((clast_for *)(stmt))->UB), + lower_bound(((clast_for *)(stmt))->LB), + fill_counter(Gravel::Symbol("FillCounter")) + { // Calculate Output Range + for_stmt = (clast_for *)(stmt); + + + + + +} + + +Potholes::BoundExpression::BoundExpression(clast_expr * e) : +Gravel::Expression(Gravel::Pointer::Expression(new Gravel::Implementation::NullExpression())) { + // build some kind of expression from this clast expr } void Potholes::Generator::ForModule::register_symbols() { - + + Potholes::Generator::Module::register_symbols(); + + Gravel::Context * ctx = Gravel::Context::getInstance(); + + + //ctx->insert(Gravel::Context::getPointer(this), fill_counter, Gravel::Interface::Symbol::Local); + /* Build the two expressions for upper bound and lower bound */ /* Build the necessary state machine for controlling module */ - + Gravel::State fill("Fill"); + Gravel::State run("Run"); + + Gravel::StateMachine sm(fill); + + + + sm.addTransition(fill >> run) -> condition( fill_counter == 1); + sm.addAction(fill) ->action(fill_counter = fill_counter + 1); + /* add appropriate symbols to module definition */ diff --git a/src/codegen/Generator.cxx b/src/codegen/Generator.cxx index 0489a9f..1ab4cab 100644 --- a/src/codegen/Generator.cxx +++ b/src/codegen/Generator.cxx @@ -12,6 +12,8 @@ #include +#include + namespace PG = Potholes::Generator; @@ -29,18 +31,22 @@ PG::Generator::Generator(std::string openscop): input_file(openscop), state(boos } osl_scop_p scop = osl_scop_read(openscop_file); - input = boost::shared_ptr(cloog_input_from_osl_scop(state.get(), scop)); - + // input = boost::shared_ptr(cloog_input_from_osl_scop(state.get(), scop)); + input = cloog_input_from_osl_scop(state.get(), scop); } void PG::Generator::generate(Target_t target) { options = boost::shared_ptr(cloog_options_malloc(state.get())); - root = boost::shared_ptr(cloog_clast_create_from_input(input.get(), options.get())); + root = boost::shared_ptr(cloog_clast_create_from_input(input, options.get())); switch (target) { case PG::Verilog : { + register_modules(); + register_symbols(); + build_chains(); + } break; default : { @@ -49,10 +55,10 @@ void PG::Generator::generate(Target_t target) { } -void PG::Generator::emit(Target_t target) { +void PG::Generator::emit(Target_t target, std::ostream& os) { switch (target) { case PG::Verilog : { - Gravel::Context::getInstance()->emit(std::cout); + Gravel::Context::getInstance()->emit(os); } break; case PG::C : { @@ -110,7 +116,18 @@ template class identify_for_nodes { public : void operator()(clast_stmt * node, T& usr) { - + +} + +}; + +template <> +class identify_for_nodes { + public : + void operator()(clast_stmt * node, Potholes::Generator::Generator::StatementSet& ss) { + if (PG::find_type(node) == PG::stmt_for_t) { + ss.insert(node); + } } }; @@ -123,13 +140,33 @@ class identify_guard_nodes { } }; +template <> +class identify_guard_nodes { + public: + void operator()(clast_stmt * node, Potholes::Generator::Generator::StatementSet& ss) const { + if (PG::find_type(node) == PG::stmt_guard_t) { + ss.insert(node); + } + } +}; + template -class identify_assignment_nodes { +class identify_assignment_nodes{ public : void operator()(clast_stmt * node, T& usr) { } }; + +template <> +class identify_assignment_nodes{ +public : + void operator()(clast_stmt * node, Potholes::Generator::Generator::StatementSet& ss) { + if (PG::find_type(node) == PG::stmt_ass_t) { + ss.insert(node); + } + } +}; @@ -209,17 +246,29 @@ void PG::Generator::create_and_append_nodes(StatementSet ss, ModuleSet& ms) { StatementSet::iterator ssit; + Gravel::Context * ctx = Gravel::Context::getInstance(); + for(ssit = ss.begin() ; ssit != ss.end() ; ssit++ ) { + Potholes::Generator::clast_stmt_type type = PG::find_type(*ssit); switch(type) { case PG::stmt_for_t : { - modules.insert(boost::shared_ptr(new PG::ForModule(*ssit))); + Gravel::Pointer::Module mp = Gravel::Pointer::Module(new PG::ForModule(*ssit)); + Gravel::Module m = Gravel::Module(mp); + ctx->insert(mp); + /// modules.insert(m); } break; case PG::stmt_guard_t : { - modules.insert(boost::shared_ptr(new PG::GuardModule(*ssit))); + Gravel::Pointer::Module mp = Gravel::Pointer::Module(new PG::GuardModule(*ssit)); + Gravel::Module m = Gravel::Module(mp); + ctx->insert(mp); + // modules.insert(Gravel::Module(mp)); } break; case PG::stmt_ass_t : { - modules.insert(boost::shared_ptr(new PG::AssModule(*ssit))); + Gravel::Pointer::Module mp = Gravel::Pointer::Module(new PG::AssModule(*ssit)); + Gravel::Module m = Gravel::Module(mp); + ctx->insert(mp); + // modules.insert(Gravel::Module(mp)); } break; } @@ -245,16 +294,20 @@ std::string Potholes::Generator::create_unique_module_name(clast_stmt * stmt) { switch(type) { case PG::stmt_for_t : { ss << "For_"; + clast_for * for_stmt = (clast_for *)(stmt); + ss << for_stmt->iterator; } break; case PG::stmt_guard_t : { ss << "Guard_"; + ss << stmt ; } break; case PG::stmt_ass_t : { ss << "Ass_"; + ss << stmt ; } break; } - ss << stmt ; + return ss.str(); } @@ -275,8 +328,8 @@ void PG::Generator::create_and_append_nodes(EdgeSet edges, ModuleSet & modules ) clast_stmt * start_node = find_start_node(*(sosit), edge_set); // create SeqModule initiated with this clast_stmt * - - modules.insert(boost::shared_ptr(new PG::SequentialModule(start_node))); + Gravel::Pointer::Module mp = Gravel::Pointer::Module(new PG::SequentialModule(start_node)); + modules.insert(Gravel::Module(mp)); } } @@ -285,7 +338,13 @@ void PG::Generator::connect_edges(PG::Generator::EdgeSet es, PG::Generator::Modu /* every modules should have exactly one parent */ - /* every module (except SEQ modules) should have exactly one child */ + // go through modules + // if module is in the seq_set, it's parent is a seq module, + // go through seq modules, if next chain leads to module statement then the parent of this module is that seq module + + // otherwise, the parent is taken from the edge set. + + // every parent calls register_child on it's parent module } @@ -569,8 +628,10 @@ void PG::Generator::register_modules() { walk_nodes >(root.get(), identify_for_nodes(), for_nodes); walk_nodes >(root.get(), identify_assignment_nodes(), assignment_nodes); + + create_and_append_nodes(guard_nodes, modules); - create_and_append_nodes(for_nodes, modules); + create_and_append_nodes(for_nodes, modules); create_and_append_nodes(assignment_nodes, modules); create_and_append_nodes(seq_edges, modules); @@ -578,6 +639,74 @@ void PG::Generator::register_modules() { } + void PG::Generator::register_symbols() { + + Gravel::Context * ctx = Gravel::Context::getInstance(); + + Gravel::ModuleSet cml = ctx->getModules(); + Gravel::ModuleSet::iterator cmlit; + for (cmlit = cml.begin() ; cmlit != cml.end() ; cmlit++ ) { + //Gravel::Pointer::Module ptr = cmlit->getPointer(); + + Potholes::Generator::CodegenModulePtr cgptr = boost::dynamic_pointer_cast(*cmlit); + + if ( cgptr != Potholes::Generator::CodegenModulePtr() ) { + cgptr->register_symbols(); + } + + } + + } + + + void PG::Generator::gather_iterator_edges() { + + + } + + + void PG::Generator::register_iterator_edge_symbols() { + + } + + void PG::Generator::gather_control_edges() { + + + + } + void PG::Generator::insert_control_edge_symbols() { + + } + + void PG::Generator::build_chains() { + + Gravel::Module top("top"); + /* Automatically Registered */ + Gravel::Context * ctx = Gravel::Context::getInstance(); + + + + + Gravel::Symbol clk("clk"); + top << clk; + Gravel::Symbol reset("reset"); + top << reset; + + /* create symbols to link together modules at top level */ + + gather_iterator_edges(); + register_iterator_edge_symbols(); + + /* create symbols to link together modules at top level */ + + gather_control_edges(); + insert_control_edge_symbols(); + + } + + + + /* std::cerr << "Seq edges has " << seq_edges.size() << " edges" << std::endl; std::cerr << "For Nodes has " << for_nodes.size() << " nodes" << std::endl; std::cerr << "Guard Nodes has " << guard_nodes.size() << " nodes"<< std::endl; diff --git a/src/codegen/GuardModule.cxx b/src/codegen/GuardModule.cxx index 714ea02..7174360 100644 --- a/src/codegen/GuardModule.cxx +++ b/src/codegen/GuardModule.cxx @@ -6,7 +6,7 @@ Potholes::Generator::GuardModule::GuardModule(clast_stmt * stmt) : Potholes::Generator::Module(stmt) { - + std::cout << "Guard" << "\n"; } diff --git a/src/codegen/SequentialModule.cxx b/src/codegen/SequentialModule.cxx index 823167b..cae740e 100644 --- a/src/codegen/SequentialModule.cxx +++ b/src/codegen/SequentialModule.cxx @@ -1,6 +1,8 @@ #include Potholes::Generator::SequentialModule::SequentialModule(clast_stmt * stmt) : Potholes::Generator::Module(stmt) { + + std::cout << "Seq" << "\n"; } diff --git a/src/include/gravel/Assignment.h b/src/include/gravel/Assignment.h new file mode 100644 index 0000000..f0759df --- /dev/null +++ b/src/include/gravel/Assignment.h @@ -0,0 +1,110 @@ +/* + * File: Assignment.h + * Author: sb1708 + * + * Created on May 29, 2013, 9:20 PM + */ + +#ifndef ASSIGNMENT_H +#define ASSIGNMENT_H + +//#include +//#include + +namespace Gravel { + + class Expression; + class Symbol; + + + namespace Implementation { + class Symbol; + class Constant; + class Expression; + }; + + namespace Pointer{ + typedef boost::shared_ptr Symbol; + typedef boost::shared_ptr Constant; + typedef boost::shared_ptr Expression; + }; + + namespace Collection { + typedef std::set Symbol; + }; + + +}; + + +namespace Gravel { + + namespace Interface { + class Assignment { + virtual void emit(std::ostream& os) = 0; + virtual Gravel::Collection::Symbol get_assigned_symbols() const = 0; + }; + }; + + namespace Implementation { + class Assignment; + }; + + namespace Pointer { + typedef boost::shared_ptr Assignment; + } + + class Assignment { + public: + + static Gravel::Assignment Create(const Gravel::Symbol&, const Gravel::Expression&, unsigned delay = 0); + + typedef std::map AssignmentMap; + typedef std::pair AssignmentPair; + private: + Gravel::Pointer::Assignment assignment; + public: + bool operator<(const Gravel::Assignment&) const; + virtual void emit(std::ostream& os); + Gravel::Collection::Symbol get_assigned_symbols() const; + Assignment(Gravel::Pointer::Assignment); + + }; + + namespace Implementation { + + class Assignment : public Gravel::Interface::Assignment { + private : + unsigned delay; + Gravel::Assignment::AssignmentMap normal; + Gravel::Assignment::AssignmentMap reset; + public: + virtual void emit(std::ostream& os); + Gravel::Collection::Symbol get_assigned_symbols() const; + void propagate(); + Assignment(Gravel::Pointer::Symbol&, Gravel::Pointer::Expression&, unsigned delay); + }; + + class CompoundAssignment : public Gravel::Assignment { + public : + CompoundAssignment(unsigned delay = 0); + void insert(Gravel::Symbol, Gravel::Expression); + virtual void emit(std::ostream& os); + private : + + }; + + class ConditionalAssignment : public Gravel::Assignment { + public: + virtual void emit(std::ostream& os); + private : + Gravel::Pointer::Expression condition; + }; + + }; + +}; + + +#endif /* ASSIGNMENT_H */ + diff --git a/src/include/gravel/AssignmentFunctions.h b/src/include/gravel/AssignmentFunctions.h new file mode 100644 index 0000000..a4ddeda --- /dev/null +++ b/src/include/gravel/AssignmentFunctions.h @@ -0,0 +1,19 @@ +/* + * File: AssignmentFunctions.h + * Author: sb1708 + * + * Created on June 13, 2013, 1:20 PM + */ + +#ifndef ASSIGNMENTFUNCTIONS_H +#define ASSIGNMENTFUNCTIONS_H + +#include +#include +#include +namespace Gravel { + void Reset( Gravel::Symbol, Gravel::Expression); + void Delay( Gravel::Assignment, Gravel::Expression); +}; +#endif /* ASSIGNMENTFUNCTIONS_H */ + diff --git a/src/include/gravel/Context.h b/src/include/gravel/Context.h index f0c766a..a65b3f9 100644 --- a/src/include/gravel/Context.h +++ b/src/include/gravel/Context.h @@ -8,26 +8,57 @@ #ifndef CONTEXT_H #define CONTEXT_H +#include +#include +#include + #include +#include + #include #include +#include + -#include -#include -#include namespace Gravel { - class ModuleInstantiation; + class InstantiatedModule; + class ModuleImplementation; -typedef std::map ModuleInstantiationMap; -typedef std::set ModuleList; -typedef std::set SymbolList; -typedef std::set ExpressionList; + namespace Exception { + class OwnerNotFound : public std::exception { + + }; + + + class ActorNotFound : public std::exception { + + }; + + + }; + + + +typedef std::map ModuleInstantiationMap; +typedef std::set ModuleSet; +typedef std::map RegisteredSymbolMap; +typedef std::set SymbolSet; + +typedef std::set ActorSet; +typedef std::map ActorMap; + +typedef std::set AssignmentSet; typedef const std::set ConstModuleList; typedef const std::set ConstSymbolList; typedef const std::set ConstExpressionList; +typedef const std::set ConstAssignmentList; + +typedef std::vector SymbolVector; + +typedef std::map SymbolVectorMap; // Module : Direction (Key for Multimap) @@ -35,57 +66,96 @@ typedef const std::set ConstExpressionList; // Symbol (Value) -typedef std::multimap::iterator SymbolMapIterator; -typedef std::pair SymbolRange; + +typedef std::pair SymbolRange; typedef std::pair ConstSymbolRange; typedef std::map ExpressionMap; typedef ExpressionMap::iterator ExpressionMapIterator; typedef ExpressionMap::const_iterator ConstExpressionMapIterator; +typedef std::vector SymbolVector; + class Context { private : static Gravel::Context * pInstance; - ModuleList ml; + ModuleSet ml; + + /* Should map Symbol:Module pairs to direction */ SymbolMap sm; - ExpressionMap em; + + ActorSet as; // list of all actors + ActorMap am; // map of actors to modules + + AssignmentSet al; + + RegisteredSymbolMap rm; + //SymbolVectorMap svm; + Gravel::ModuleInstantiationMap mm; unsigned tempIdentifier; + + unsigned add_edges(const Gravel::Pointer::Module& ); + protected : Context(); public : static Context * getInstance(); + + /* These are bad... remove them */ + + static Gravel::Module getPointer(Gravel::ModuleImplementation *); + static Gravel::Module getPointer(const Gravel::ModuleImplementation *); + void reset(); // should clear all context without memory leaks void printSymbols(std::ostream&); // does a dump of all symbols - ConstSymbolList getSymbols(const Gravel::Module&) const ; - ConstSymbolRange getSymbols(Gravel::Module module, Symbol::Direction direction) const; - - ConstExpressionList getExpressions(const Gravel::Module&) const ; - - std::string getUniqueIdentifier(); + SymbolSet getSymbols(const Gravel::Pointer::Module&) const ; + SymbolSet getSymbols(Gravel::Pointer::Module module, Symbol::Direction direction) const; + + Gravel::SymbolVector getDelayedSymbols(const Gravel::Symbol&); + Gravel::Symbol getRegisteredSymbol(const Gravel::Symbol&); - ConstModuleList getModules() const; + //ConstExpressionList getExpressions(const Gravel::Module&) const ; - void insert(const Gravel::Module&, Gravel::ModuleInstantiation); + AssignmentSet getAssignments(const Gravel::Module&) const; + std::string getUniqueIdentifier(); - void insert(const Gravel::Module&); - bool exists(const Gravel::Module&); - void insert(const Gravel::Module&, const Gravel::Symbol, const Gravel::Interface::Symbol::Direction&); - bool exists(const Gravel::Module&, const Gravel::Symbol,const Gravel::Interface::Symbol::Direction&); + Gravel::Pointer::Actor getParent(Gravel::Pointer::GraphNode); + + Gravel::ModuleSet getModules() const; - bool isOwned(const Gravel::Symbol&); - bool isOwned(const Gravel::Expression&); + // Registers an instantiated module against it's parent module + // void insert(const Gravel::Module&, Gravel::InstantiatedModule); + // Registers a 'reg' symbol + // void insert(const Gravel::Symbol, Gravel::RegisteredSymbol); + // Registers a sequence of 'wire' nodes associated with an assignment (with delay > 1) + void insert(const Gravel::Pointer::Symbol, Gravel::SymbolVector); + // Registers a module + void insert(const Gravel::Pointer::Module&); + bool exists(const Gravel::Pointer::Module&); + // Registers a symbol with associated module and direction + void insert(Gravel::Pointer::Module&, Gravel::Pointer::Symbol, const Gravel::Interface::Symbol::Direction&); + bool exists(const Gravel::Pointer::Module&, const Gravel::Pointer::Symbol, const Gravel::Interface::Symbol::Direction&); - Gravel::Module owner(const Gravel::Symbol&); - Gravel::Module owner(const Gravel::Expression&); + /* Inserts an actor into the actor set*/ + void insert(Gravel::Pointer::Actor actor); + /* Probably Obsolete*/ + //void insert(Gravel::Pointer::Actor actor, Gravel::Pointer::GraphNode); + /* Adds a actor into the actor map*/ + void insert(Gravel::Pointer::Actor actor, Gravel::Pointer::Module); - void normalize(); + void insert(const Gravel::Pointer::Assignment&); + Gravel::Pointer::Module owner(const Gravel::Pointer::Actor& actor); + // Gravel::Module owner(const Gravel::Pointer::Actor& actor); + bool isOwned(const Gravel::Pointer::Actor& actor); + + void propagate(); Gravel::Module getModule(const std::string&) const; void emit(std::ostream& os); @@ -93,6 +163,7 @@ class Context { }; + }; #endif /* CONTEXT_H */ diff --git a/src/include/gravel/Expression.h b/src/include/gravel/Expression.h index e9d23aa..0d31453 100644 --- a/src/include/gravel/Expression.h +++ b/src/include/gravel/Expression.h @@ -8,29 +8,38 @@ #ifndef EXPRESSION_H #define EXPRESSION_H -#include #include -#include "private/Actor.h" +#include +#include namespace Gravel { class Expression; - typedef enum RelativeExpresionType {LessThan, MoreThan, Equals, LessThanEquals, MoreThanEquals}; + namespace Implementation { + class Symbol; + }; + + namespace Pointer { + typedef boost::shared_ptr Symbol; + }; + + typedef enum RelativeExpressionType {LessThan, MoreThan, Equals, LessThanEquals, MoreThanEquals}; typedef enum UnaryExpressionType {Negate, Invert, Floor, Ceil}; typedef enum BinaryExpressionType {Multiply, Add, Sub, XOR}; typedef enum ReductionExpressionType {Min, Max}; namespace Interface { class Expression : public virtual Gravel::Interface::Actor { + public: virtual std::ostream& emit(std::ostream&) const = 0; - + virtual const Gravel::Pointer::Symbol getSymbol() const = 0; }; /* class TermExpression : public Gravel::Interface::Expression { @@ -81,11 +90,12 @@ namespace Gravel { class MuxExpression; class ReductionExpression; class RelativeExpression; + } namespace Pointer { typedef boost::shared_ptr Actor; - typedef boost::shared_ptr Expression; + typedef boost::shared_ptr Expression; } class Expression : public Gravel::Interface::Actor { @@ -93,17 +103,27 @@ namespace Gravel { Expression(int); // promotion of int, creates constant bool operator==(const Gravel::Expression&) const; bool operator<(const Gravel::Expression &) const; - virtual GraphNode::NodePtr getOutput() const; + virtual Gravel::Pointer::GraphNode getOutput() const; virtual GraphNode::ConstNodeRange getInputs() const; - void setWidth(Gravel::GraphNode::NodePtr, unsigned); + void setWidth(Gravel::Pointer::GraphNode, unsigned); Expression(const Gravel::Pointer::Expression); std::ostream& emit(std::ostream&) const; - const Gravel::Symbol getSymbol(); + const Gravel::Pointer::Symbol getSymbol() const; void setPipelineDepth(unsigned); + static bool Owned(const Gravel::Expression& e); + Gravel::Module getOwner() const; + void attach(const Gravel::Pointer::Module&) const; + ~Expression(); + friend Gravel::Assignment Assignment::Create(const Gravel::Symbol&, const Gravel::Expression&, unsigned delay); private : Gravel::Pointer::Expression ptr; }; + class RelativeExpression : public Expression { + public: + RelativeExpression(const Gravel::Pointer::Expression); + }; + /* class TermExpression : public Gravel::Expression, public Gravel::Interface::TermExpression { public: TermExpression(const Gravel::Symbol); @@ -151,6 +171,10 @@ namespace Gravel { std::ostream& operator<<(std::ostream& os, const Gravel::Expression& expression); + Gravel::RelativeExpression operator==(const Gravel::Symbol&, const Gravel::Expression&); + // Gravel::RelativeExpression operator==(const Gravel::Symbol&, const Gravel::Constant&); + + /* Symbols implicitly turned into TermExpressions or Constants implicitly turned into TermExpressions*/ diff --git a/src/include/gravel/Gravel.h b/src/include/gravel/Gravel.h index 793a024..7b8020b 100644 --- a/src/include/gravel/Gravel.h +++ b/src/include/gravel/Gravel.h @@ -7,9 +7,12 @@ #ifndef GRAVEL_H #define GRAVEL_H - - - +#include +#include +#include +#include +#include +#include #endif /* GRAVEL_H */ diff --git a/src/include/gravel/Module.h b/src/include/gravel/Module.h index 8c9e109..61fa2b3 100644 --- a/src/include/gravel/Module.h +++ b/src/include/gravel/Module.h @@ -8,63 +8,80 @@ #ifndef MODULE_H #define MODULE_H -#include +#include +#include +#include + +#include #include -#include "private/Symbol.h" -#include "Context.h" -#include -#include + namespace Gravel { + namespace Implementation { + class Symbol; + }; class ModuleImplementation; class ModuleInterface { virtual const std::string getName() const = 0; - virtual std::ostream& emit(std::ostream& os) const = 0; +// virtual std::ostream& emit(std::ostream& os) const = 0; }; -typedef boost::shared_ptr ModulePtr; +namespace Pointer { + +typedef boost::shared_ptr Module; + +}; class Module : public Gravel::ModuleInterface { typedef std::vector SymbolList; protected : - Module(); - ModulePtr module; + // Module(); + Gravel::Pointer::Module module; public : - ; - - + Module(const std::string& name); + Module(Gravel::Pointer::Module); + // Module(const Module &); const std::string getName() const; + std::ostream& emit(std::ostream& os) const; // SymbolList getSymbols(Gravel::Symbol::Direction); bool operator==(const Module & ) const; void operator>>(Gravel::Symbol& gs); void operator<<(Gravel::Symbol& gs); bool operator<(const Module &) const; - bool operator!=(const Module &) const; + bool operator!=(const Module &) const; + //Gravel::Module & operator=(const Gravel::Module & rhs); + }; - class ModuleInstantiation { + +class SynthesizableModule { public: - typedef std::map InputMap; - ModuleInstantiation(const std::string&, const Gravel::Module, const InputMap); + virtual void synthesize() = 0; +}; + + class InstantiatedModule { + public: + typedef std::map SymbolMap; + InstantiatedModule(const std::string&, const Gravel::Module, const SymbolMap); private: const std::string name; const Gravel::Module module; - const InputMap inputs; + const SymbolMap connections; }; - +namespace Exception { class ModuleNotFound : public std::exception { public: ModuleNotFound(std::string = ""); @@ -73,6 +90,7 @@ class ModuleNotFound : public std::exception { private: std::string name; }; +}; struct MixedSeparatorException : public std::exception {}; @@ -149,7 +167,7 @@ struct MixedSeparatorException : public std::exception {}; static void emit(FormattedStream & fs, FormattedListObj fv) { - Gravel::ConstSymbolMapIterator it; + Gravel::SymbolSet::iterator it; for (it = fv.be ; it != fv.en ; it++) { @@ -160,7 +178,7 @@ struct MixedSeparatorException : public std::exception {}; fs.empty = false; - fs << it->second; + fs << *it; } // std::ostream_iterator > out_it (fs,fv.getSeparator().c_str()); diff --git a/src/include/gravel/StateMachine.h b/src/include/gravel/StateMachine.h new file mode 100644 index 0000000..8d74df6 --- /dev/null +++ b/src/include/gravel/StateMachine.h @@ -0,0 +1,59 @@ +/* + * File: StateMachine.h + * Author: sb1708 + * + * Created on May 29, 2013, 12:24 PM + */ + +#ifndef STATEMACHINE_H +#define STATEMACHINE_H + +#include +#include + + +namespace Gravel { + +class StateTransition { +public: + StateTransition * operator ->(); + StateTransition operator()(); + void condition(Gravel::RelativeExpression); +}; + +class StateAction { +public: + StateAction * operator ->(); + StateAction operator()(Gravel::Assignment); + StateAction action(Gravel::Assignment); +}; + + +class State { +public: + + State(const std::string&); +}; + +class StateMachine : public SynthesizableModule{ +public : + StateMachine(Gravel::State); + void synthesize(); + StateTransition addTransition (StateTransition); + StateAction addAction(Gravel::State); +private: + Gravel::State resetState; +}; + + + +}; + + + +Gravel::StateTransition operator>> (Gravel::State,Gravel::State); + + + +#endif /* STATEMACHINE_H */ + diff --git a/src/include/gravel/Symbol.h b/src/include/gravel/Symbol.h index ecac996..d3fd28c 100644 --- a/src/include/gravel/Symbol.h +++ b/src/include/gravel/Symbol.h @@ -8,28 +8,44 @@ #ifndef SYMBOL_H #define SYMBOL_H -#include +#include +#include + +#include -#include -#include "private/Actor.h" namespace Gravel { class Expression; class TermExpression; class BinaryExpression; - class Module; + class Assignment; + class Symbol; - namespace Pointer{ - - } namespace Implementation { class Symbol; class Constant; - } + }; + + namespace Pointer{ + typedef boost::shared_ptr Symbol; + typedef boost::shared_ptr Constant; + }; + + namespace Collection { + typedef std::set Symbol; + }; + + +}; + + #include + #include + +namespace Gravel { namespace Interface { class Symbol : public virtual Gravel::Interface::Actor { @@ -43,48 +59,82 @@ namespace Gravel { protected: }; - }; - + + + }; - typedef boost::shared_ptr SymbolPtr; + + + - class Constant { - + + + +namespace Implementation { + class Constant : public virtual Gravel::Actor { + public: + Constant(int); + Gravel::Pointer::GraphNode getOutput() const; + GraphNode::ConstNodeRange getInputs() const; + // set width + void setWidth(Gravel::Pointer::GraphNode, unsigned); + protected: + int value; }; +}; + class SymbolNotFoundException : std::exception { }; + - - class Symbol : public Interface::Symbol { + class Symbol : public Interface::Symbol, public Interface::ActorHandle { private : - SymbolPtr symbol; + Gravel::Pointer::Symbol symbol; protected : Symbol(); public : Symbol(const std::string&); - Symbol(boost::weak_ptr); + Symbol(Gravel::Pointer::Actor); + Symbol(Gravel::Pointer::Symbol); + ~Symbol(); const std::string getName() const; Interface::Symbol::Type getType() const; + static boost::shared_ptr Create(const std::string&); + static unsigned getSymbolWidth(const Gravel::Symbol&); + + static bool Owned(const Gravel::Symbol&); + + //static void Attach(const Gravel::Symbol&, const Gravel::Module&, const Gravel::Interface::Symbol::Direction&); + + void attach(const Gravel::Pointer::Module&) const; + void attach(Gravel::Pointer::Module&, const Gravel::Interface::Symbol::Direction&) const ; + Gravel::Module getOwner() const; + bool operator==(const Gravel::Symbol&) const; - operator Gravel::Expression(); + operator const Gravel::Expression() const; - GraphNode::NodePtr getOutput() const; + Gravel::Pointer::GraphNode getOutput() const; GraphNode::ConstNodeRange getInputs() const; - void setWidth(Gravel::GraphNode::NodePtr, unsigned); + void setWidth(Gravel::Pointer::GraphNode, unsigned); - Gravel::Symbol operator=(const Gravel::Expression&); + Gravel::Assignment operator=(const Gravel::Expression&); bool operator<(const Gravel::Symbol&) const; - + friend Gravel::Assignment Assignment::Create(const Gravel::Symbol&, const Gravel::Expression&, unsigned delay); }; + class RegisteredSymbol : public Gravel::Symbol { + + }; + + class TemporarySymbol : public Gravel::Symbol { public: TemporarySymbol(); @@ -101,9 +151,13 @@ namespace Gravel { }; - typedef std::pair SymbolKey; - typedef std::multimap SymbolMap; - typedef std::multimap::const_iterator ConstSymbolMapIterator; + typedef std::pair SymbolKey; + typedef std::multimap SymbolMap; + + + typedef std::set SymbolSet; + + typedef SymbolMap::const_iterator ConstSymbolMapIterator; std::ostream& operator<< (std::ostream& os, const Gravel::Symbol& s); std::ostream& operator<< (std::ostream& os, const Gravel::SymbolDeclaration& s); @@ -111,5 +165,6 @@ namespace Gravel { }; + #endif /* SYMBOL_H */ diff --git a/src/include/gravel/private/Actor.h b/src/include/gravel/private/Actor.h index 25bb3a1..5a8ae22 100644 --- a/src/include/gravel/private/Actor.h +++ b/src/include/gravel/private/Actor.h @@ -11,6 +11,8 @@ #include #include #include +#include + namespace Gravel { @@ -21,54 +23,71 @@ namespace Gravel { class Actor; class Symbol; + class Module; + class GraphNode; + class ModuleImplementation; + + namespace Interface{ - class Actor; - } + }; namespace Pointer{ - - typedef boost::shared_ptr Actor; + typedef boost::shared_ptr Actor; + typedef boost::shared_ptr GraphNode; + typedef boost::shared_ptr Module; + }; + namespace WeakPointer { + typedef boost::weak_ptr GraphNode; + } + + class GraphNode { public : - typedef std::multimap > NodeMap; + typedef std::multimap NodeMap; + typedef std::pair NodePair; typedef NodeMap::const_iterator ConstNodeIterator; typedef NodeMap::iterator NodeIterator; - typedef std::pair > NodePair; + typedef std::pair NodeRange; typedef std::pair ConstNodeRange; - typedef boost::weak_ptr NodePtr; - - typedef std::map BackMap; - typedef BackMap::iterator BackMapIterator; - typedef std::pair BackMapRange; - typedef std::map ParentMap; + + typedef std::map BackMap; + typedef std::pair BackMapPair; + typedef BackMap::iterator BackMapIterator; + typedef std::pair BackMapRange; + + typedef std::map ParentMap; + // Destructor ~GraphNode(); // points to implementation (which is an actor) - - static Gravel::Pointer::Actor getParent(Gravel::GraphNode::NodePtr); - static void setParents(Gravel::Pointer::Actor); + + static Gravel::Pointer::Actor getParent(Gravel::Pointer::GraphNode); + static Gravel::Pointer::Actor getParent(Gravel::GraphNode::ConstNodeIterator); - static BackMapRange getConnections(NodePtr); + + + static BackMapRange getConnections(Gravel::Pointer::GraphNode); // Graph Node destructor removes back-link and removes from ring - static void connect(boost::weak_ptr, boost::weak_ptr); - + static void connect(Gravel::Pointer::GraphNode, Gravel::Pointer::GraphNode); + unsigned getWidth(); // Default Constructor - GraphNode(); + static Gravel::Pointer::GraphNode Create(Gravel::GraphNode::NodeMap&, const Gravel::GraphEdgeDirection&); private: + GraphNode(); unsigned width; static BackMap back; - static std::map parent; + @@ -76,38 +95,59 @@ namespace Gravel { // Equality Assignment is declared but not implemented }; + + namespace Collection { + typedef std::pair GraphNodeRange; + typedef std::set Actor; + } + + + namespace Exception { + class GraphNodeNotFound : public std::exception { + + }; + } + namespace Interface { class Actor { public: // std::set getInputs(); - virtual GraphNode::NodePtr getOutput() const = 0; + virtual Gravel::Pointer::GraphNode getOutput() const = 0; virtual GraphNode::ConstNodeRange getInputs() const = 0; // set width - virtual void setWidth(Gravel::GraphNode::NodePtr, unsigned) = 0; - + virtual void setWidth(Gravel::Pointer::GraphNode, unsigned) = 0; }; + class ActorHandle { + virtual void attach(const Gravel::Pointer::Module&) const = 0 ; + virtual Gravel::Module getOwner() const = 0; }; + - class Actor : public virtual Gravel::Interface::Actor { - + }; + + class Actor : public virtual Gravel::Interface::Actor { public: - + Actor(); + /* Graph Connectivity */ GraphNode::ConstNodeRange getInputs() const; - boost::weak_ptr getOutput() const; - void setWidth(Gravel::GraphNode::NodePtr, unsigned); + Gravel::Pointer::GraphNode getOutput() const; + /* Width Selection */ + void setWidth(Gravel::Pointer::GraphNode, unsigned); + /* Lifetime Management */ + void initialize(Gravel::Pointer::Actor); protected: - // Has a smart pointer to Graph Node - + + bool initialized; // All nodes are owned by a particular actor which creates them // Deletion of an actor means their nodes are deleted and dangling `back' nodes and next // nodes are amended GraphNode::NodeMap nodes; }; -} +}; #endif /* ACTOR_H */ diff --git a/src/include/gravel/private/Expression.h b/src/include/gravel/private/Expression.h index 3f915f4..26a379c 100644 --- a/src/include/gravel/private/Expression.h +++ b/src/include/gravel/private/Expression.h @@ -2,20 +2,36 @@ #include #include +#include namespace Gravel { namespace Implementation { - typedef std::map MuxInputMapping; + typedef std::map MuxInputMapping; - class Expression : public virtual Gravel::Interface::Expression { - virtual const Gravel::Symbol getSymbol(); + + + class Expression : public virtual Gravel::Actor, public virtual Gravel::Interface::Expression { + public: + virtual const Gravel::Pointer::Symbol getSymbol() const ; void setPipelineDepth(unsigned); }; - class MuxExpression : public virtual Gravel::Actor , public Gravel::Implementation::Expression { + class NullExpression : public Gravel::Implementation::Expression { + public: + NullExpression(); + std::ostream& emit(std::ostream&) const; + }; + + class RelativeExpression : public Gravel::Implementation::Expression { + public : + RelativeExpression(const Gravel::Expression, const Gravel::Expression, const Gravel::RelativeExpressionType); + std::ostream& emit(std::ostream& ) const; + }; + + class MuxExpression : public Gravel::Implementation::Expression { public: MuxExpression(Gravel::Symbol sym); std::ostream& emit(std::ostream& ) const; @@ -23,17 +39,18 @@ namespace Gravel { MuxInputMapping mim; }; - class TermExpression : public virtual Gravel::Actor, public Gravel::Implementation::Expression { + class TermExpression : public Gravel::Implementation::Expression { public: - TermExpression(Gravel::Symbol sym); + TermExpression(Gravel::Pointer::Symbol); + TermExpression(Gravel::Pointer::Constant); std::ostream& emit(std::ostream&) const; - const Gravel::Symbol getSymbol(); + const Gravel::Pointer::Symbol getSymbol() const ; // This is output symbol of term expression private: - const Gravel::Symbol symbol; + //const Gravel::Symbol symbol; }; - class BinaryExpression : public virtual Gravel::Actor, public Gravel::Implementation::Expression { + class BinaryExpression : public Gravel::Implementation::Expression { public : const Gravel::Expression& getLHS(); const Gravel::Expression& getRHS(); diff --git a/src/include/gravel/private/Module.h b/src/include/gravel/private/Module.h index 03bbf92..8435241 100644 --- a/src/include/gravel/private/Module.h +++ b/src/include/gravel/private/Module.h @@ -10,9 +10,13 @@ #include +#include + namespace Gravel { -class ModuleImplementation { + + +class ModuleImplementation : public Gravel::ModuleInterface { private: std::string name; @@ -21,7 +25,9 @@ class ModuleImplementation { ModuleImplementation(); public: ModuleImplementation(const std::string&); - const std::string getName(); + const std::string getName() const; + bool operator<(const ModuleImplementation &) const; + // virtual std::ostream& emit(std::ostream& os) const; }; diff --git a/src/include/gravel/private/Symbol.h b/src/include/gravel/private/Symbol.h index ffd7fa5..527e2f7 100644 --- a/src/include/gravel/private/Symbol.h +++ b/src/include/gravel/private/Symbol.h @@ -22,28 +22,13 @@ namespace Gravel { public: Symbol(const std::string&); + const std::string getName() const; Gravel::Interface::Symbol::Type getType() const; - }; -class Constant : public Gravel::Actor { - -private : - unsigned value; -public: - - }; - }; - - - - - - - }; #endif /* SYMBOL_H */ diff --git a/src/include/potholes/codegen/ForModule.h b/src/include/potholes/codegen/ForModule.h index 336ecd3..cb2c3c1 100644 --- a/src/include/potholes/codegen/ForModule.h +++ b/src/include/potholes/codegen/ForModule.h @@ -8,15 +8,31 @@ #ifndef SEQMODULE_H #define SEQMODULE_H + +#include + #include + + namespace Potholes { + + class BoundExpression : public Gravel::Expression { + public: + BoundExpression(clast_expr *); + }; + namespace Generator { class ForModule : public Potholes::Generator::Module { public: ForModule(clast_stmt *); void register_symbols(); void build_chain(); + private: + clast_for * for_stmt; + Gravel::Symbol fill_counter; + Potholes::BoundExpression upper_bound; + Potholes::BoundExpression lower_bound; }; } diff --git a/src/include/potholes/codegen/Generator.h b/src/include/potholes/codegen/Generator.h index 0da20af..b1ed029 100644 --- a/src/include/potholes/codegen/Generator.h +++ b/src/include/potholes/codegen/Generator.h @@ -114,7 +114,7 @@ template class Generator { public: - typedef std::set < boost::shared_ptr > ModuleSet; + typedef std::set ModuleSet; typedef std::set StatementSet; typedef std::multimap EdgeSet; typedef std::set< std::set >DisjointStatementSets; @@ -124,16 +124,19 @@ template Generator(std::string openscop = ""); void generate(Target_t); - void emit(Target_t); + void emit(Target_t, std::ostream&); - void register_signals(); + //void register_signals(); void register_modules(); + void register_symbols(); + void build_chains(); Potholes::Generator::interval calculate_output_range(clast_expr *); Potholes::Generator::interval find_output_range_for_iterator(std::string); private: std::string input_file; - boost::shared_ptr input; + // boost::shared_ptr input; + CloogInput * input; boost::shared_ptr state; boost::shared_ptr root; boost::shared_ptr options; @@ -144,6 +147,12 @@ template void create_and_append_nodes(StatementSet, ModuleSet &) ; void create_and_append_nodes(EdgeSet, ModuleSet &); + void gather_iterator_edges(); + void register_iterator_edge_symbols(); + + void gather_control_edges(); + void insert_control_edge_symbols(); + }; diff --git a/src/include/potholes/codegen/Module.h b/src/include/potholes/codegen/Module.h index ccc3ad3..53a5e86 100644 --- a/src/include/potholes/codegen/Module.h +++ b/src/include/potholes/codegen/Module.h @@ -11,17 +11,26 @@ #include #include + #include namespace Potholes { + + + namespace Generator { + + class Module : public Gravel::ModuleImplementation { public: Module(clast_stmt *); - virtual void register_symbols() = 0; - virtual void build_chain() = 0; + virtual void register_symbols(); + virtual void build_chain(); + }; + typedef boost::shared_ptr CodegenModulePtr; + } }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 470e5cd..96ed0ee 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,7 +1,7 @@ include_directories(${CMAKE_SOURCE_DIR}/src/include) #SimulateTest SimulateFixture CodegenTest -#add_executable(gravel-testcases Tests ContextTest GenerateFixture ModuleTest StreamTest) -add_executable(gravel-testcases Tests CodegenTest) +#add_executable(gravel-testcases Tests ContextTest GenerateFixture ModuleTest StreamTest StateMachineTest) +add_executable(gravel-testcases Tests StateMachineTest) target_link_libraries(gravel-testcases ${Boost_LIBRARIES}) target_link_libraries(gravel-testcases gravel) diff --git a/test/CodegenTest.cxx b/test/CodegenTest.cxx index bf4cfa3..d491334 100644 --- a/test/CodegenTest.cxx +++ b/test/CodegenTest.cxx @@ -23,7 +23,7 @@ BOOST_AUTO_TEST_CASE( codegen_print_code ) PG::Generator gen("/Users/sb1708/Work/shared-tools/Gravel/test/input/mmm.scop"); gen.generate(PG::C); - gen.emit(PG::C); + gen.emit(PG::C, std::cout); } BOOST_AUTO_TEST_CASE( codegen_print_verilog ) @@ -31,115 +31,7 @@ BOOST_AUTO_TEST_CASE( codegen_print_verilog ) PG::Generator gen("/Users/sb1708/Work/shared-tools/Gravel/test/input/mmm.scop"); gen.generate(PG::Verilog); - gen.emit(PG::Verilog); -} - -BOOST_AUTO_TEST_CASE( codegen_walk_ranges_i ) -{ - PG::Generator gen("/Users/sb1708/Work/shared-tools/Gravel/test/input/mmm.scop"); - gen.generate(PG::Verilog); - // walk the output ranges - PG::interval range = gen.find_output_range_for_iterator("x1"); - - - //cloog_int_print(stdout, range.first); - - - - // BOOST_CHECK(range.first); - // BOOST_CHECK(range.second == 49); -} - -BOOST_AUTO_TEST_CASE( codegen_walk_ranges_j ) -{ - PG::Generator gen("/Users/sb1708/Work/shared-tools/Gravel/test/input/mmm.scop"); - gen.generate(PG::Verilog); - // walk the output ranges - PG::interval range = gen.find_output_range_for_iterator("x2"); - /* std::pair range = gen.calculate_output_range(stmt); - BOOST_CHECK(range.first == 0); - BOOST_CHECK(range.second == 49); - * */ -} - -BOOST_AUTO_TEST_CASE( codegen_walk_ranges_k ) -{ - PG::Generator gen("/Users/sb1708/Work/shared-tools/Gravel/test/input/mmm.scop"); - gen.generate(PG::Verilog); - // walk the output ranges - PG::interval range = gen.find_output_range_for_iterator("x3"); - /* - std::pair range = gen.calculate_output_range(stmt); - BOOST_CHECK(range.first == 0); - BOOST_CHECK(range.second == 49); - */ -} - -BOOST_AUTO_TEST_CASE ( test_disjoint_sets ) { - // test our ability to create disjoint sets - - std::multimap edges; - - typedef std::pair Edge; - - edges.insert(Edge(1,2)); - edges.insert(Edge(4,5)); - edges.insert(Edge(5,6)); - edges.insert(Edge(2,3)); - - std::set< std::set < int > > disjoint_sets = find_disjoint_sets(edges); - - BOOST_CHECK(disjoint_sets.size() == 2); - - -} - -BOOST_AUTO_TEST_CASE ( test_disjoint_sets_2 ) { - // test our ability to create disjoint sets - - std::multimap edges; - - typedef std::pair Edge; - - edges.insert(Edge(1,2)); - edges.insert(Edge(3,4)); - - BOOST_CHECK(find_disjoint_sets(edges).size() == 2); - - -} - -BOOST_AUTO_TEST_CASE ( test_disjoint_sets_3 ) { - // test our ability to create disjoint sets - - std::multimap edges; - - typedef std::pair Edge; - - edges.insert(Edge(1,2)); - edges.insert(Edge(2,3)); - edges.insert(Edge(3,4)); - - BOOST_CHECK(find_disjoint_sets(edges).size() == 1); - - -} - -BOOST_AUTO_TEST_CASE ( test_disjoint_sets_4 ) { - // test our ability to create disjoint sets - - std::multimap edges; - - typedef std::pair Edge; - - edges.insert(Edge(1,2)); - edges.insert(Edge(4,5)); - edges.insert(Edge(3,4)); - edges.insert(Edge(5,6)); - edges.insert(Edge(2,3)); - BOOST_CHECK(find_disjoint_sets(edges).size() == 1); - - + gen.emit(PG::Verilog,std::cout); } diff --git a/test/StateMachineTest.cxx b/test/StateMachineTest.cxx index 8b13789..fac7cdf 100644 --- a/test/StateMachineTest.cxx +++ b/test/StateMachineTest.cxx @@ -1 +1,82 @@ +#include + +#include + + +BOOST_AUTO_TEST_SUITE ( StateMachineTest ) + +BOOST_AUTO_TEST_CASE( state_machine_constructor_test) +{ + Gravel::Context * ctx = Gravel::Context::getInstance(); + + BOOST_CHECK ( ctx != NULL); + Gravel::State a("A"); + + Gravel::StateMachine sm(a); + +} + + + + +/* + * BOOST_AUTO_TEST_CASE( state_machine_add_state_test) +{ + Gravel::Context * ctx = Gravel::Context::getInstance(); + + BOOST_CHECK ( ctx != NULL); + + Gravel::Module top("state_top"); + + Gravel::Symbol x("x"); + top << x; + Gravel::Symbol y("y"); + top << y; + Gravel::Symbol z("z"); + top >> z; + + Gravel::Expression te(y); + + Gravel::State a("A"); + + Gravel::State b("B"); + + Gravel::StateMachine sm(a); + + sm.addTransition(a >> b) -> condition( x == te ); // on condition that x == te, make transition from a to b + sm.addAction(a) -> action ( y = 5 )( x = 10)(z = 15); + + sm.synthesize(); // when synthesize is called, state machine must have attached to a module (otherwise runtime error) + + + ctx->emit(std::cout); +} +*/ + +BOOST_AUTO_TEST_CASE( sequential_assignment ) { + Gravel::Context * ctx = Gravel::Context::getInstance(); + + BOOST_CHECK ( ctx != NULL); + + Gravel::Module top("top"); + + Gravel::Symbol x("x"); + top << x; + Gravel::Symbol y("y"); + top << y; + Gravel::Symbol z("z"); + top >> z; + + + z = Gravel::Expression(x); + + //Gravel::Delay(z = Gravel::Expression(x) , 2); + //Gravel::Reset( z , 2); + + ctx->emit(std::cout); + +} + +BOOST_AUTO_TEST_SUITE_END() + \ No newline at end of file