degate  0.1.2
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends
degate::Module Class Reference

Implements a container to build up higher level entities. More...

#include <Module.h>

Inheritance diagram for degate::Module:
Inheritance graph
[legend]

List of all members.

Public Types

typedef std::set< Module_shptr,
LMOCompare
module_collection
typedef std::set< Gate_shptr,
LMOCompare
gate_collection
typedef std::map< std::string,
GatePort_shptr
port_collection
 This map defines module ports.

Public Member Functions

 Module (std::string const &module_name="", std::string const &_entity_name="", bool is_root=false)
 Construct a new module.
virtual ~Module ()
 Destroy the module.
bool is_main_module () const
 Check if module is the main module.
void set_main_module ()
 Set a module as main module.
void set_entity_name (std::string const &name)
 Set an identifier for the module type.
std::string get_entity_name () const
 Get name of the entity type.
void add_gate (Gate_shptr gate, bool detect_ports=true)
 Add a gate to a module.
bool remove_gate (Gate_shptr gate)
 Remove a gate from a module.
void add_module (Module_shptr module)
 Add a sub-module to a module.
bool remove_module (Module_shptr module)
 Remove a submodule.
module_collection::iterator modules_begin ()
module_collection::iterator modules_end ()
gate_collection::iterator gates_begin ()
gate_collection::iterator gates_end ()
port_collection::iterator ports_begin ()
port_collection::iterator ports_end ()
module_collection::const_iterator modules_begin () const
module_collection::const_iterator modules_end () const
gate_collection::const_iterator gates_begin () const
gate_collection::const_iterator gates_end () const
port_collection::const_iterator ports_begin () const
port_collection::const_iterator ports_end () const
void determine_module_ports ()
 Determine ports of a module.
void determine_module_ports_recursive ()
 Determine ports of children and sub-children (not including the "local" module, on which this function is called).
Module_shptr lookup_module (std::string const &module_path) const
 Lookup a sub-module.
void set_module_port_name (std::string const &module_port_name, GatePort_shptr adjacent_gate_port)
 Set module port name.
bool exists_module_port_name (std::string const &module_port_name) const
 Check if a module port name exists.
boost::optional< std::string > lookup_module_port_name (GatePort_shptr gate_port)
 Lookup the module port name.
DeepCopyable_shptr cloneShallow () const
 Return a copy of this object, but with all references to other DeepCopyables cleared (e.g.
void cloneDeepInto (DeepCopyable_shptr destination, oldnew_t *oldnew) const
 Deep-copy all members to destination.

Private Member Functions

void move_gates_recursive (Module *dst_mod)
void automove_gates ()
bool exists_gate_port_recursive (object_id_t oid) const
 Check if there is a gate is the current module or any child module that has a gate port with the object ID oid .
GatePort_shptr lookup_gate_port_recursive (object_id_t oid) const
void add_module_port (std::string const &module_port_name, GatePort_shptr adjacent_gate_port)
bool net_feeded_internally (Net_shptr net) const
bool net_completely_internal (Net_shptr net) const
Module_shptr lookup_module (std::list< std::string > &path_elements) const
 Internal implementation of lookup_module().

Private Attributes

module_collection modules
gate_collection gates
port_collection ports
std::string entity_name
bool is_root

Friends

class LogicModelImporter
void determine_module_ports_for_root (LogicModel_shptr lmodel)
 Determine ports of a root module.

Detailed Description

Implements a container to build up higher level entities.

Definition at line 40 of file Module.h.


Member Typedef Documentation

Definition at line 48 of file Module.h.

Definition at line 47 of file Module.h.

typedef std::map<std::string, GatePort_shptr> degate::Module::port_collection

This map defines module ports.

A port is identified by a name. A module port is 'connected' to a list of gate ports.

Definition at line 56 of file Module.h.


Constructor & Destructor Documentation

Module::Module ( std::string const &  module_name = "",
std::string const &  _entity_name = "",
bool  is_root = false 
)

Construct a new module.

Definition at line 33 of file Module.cc.

References degate::LogicModelObjectBase::set_name().

                              :
  entity_name(_entity_name), 
  is_root(_is_root) {

  set_name(module_name);
}

Here is the call graph for this function:

Module::~Module ( ) [virtual]

Destroy the module.

Definition at line 42 of file Module.cc.

{}

Member Function Documentation

void Module::add_gate ( Gate_shptr  gate,
bool  detect_ports = true 
)

Add a gate to a module.

Parameters:
gateThe gate to add.
detect_portsSwitch for enabling or disabling automatic module port detection.
Exceptions:
InvalidPointerExceptionThis exception is thrown, if gate is a NULL pointer.

Definition at line 88 of file Module.cc.

References determine_module_ports(), gates, and is_root.

Referenced by move_gates_recursive().

                                                        {
  if(gate == NULL)
    throw InvalidPointerException("Invalid pointer passed to add_gate().");

  gates.insert(gate);
  if(!is_root && detect_ports) determine_module_ports();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Module::add_module ( Module_shptr  module)

Add a sub-module to a module.

Exceptions:
InvalidPointerExceptionThis exception is thrown, if gate is a NULL pointer.

Definition at line 121 of file Module.cc.

References modules.

                                           {
  if(module == NULL)
    throw InvalidPointerException("Invalid pointer passed to add_modue().");

  modules.insert(module);
}
void Module::add_module_port ( std::string const &  module_port_name,
GatePort_shptr  adjacent_gate_port 
) [private]

Definition at line 375 of file Module.cc.

References ports.

                                                                                                 {
  ports[module_port_name] = adjacent_gate_port;
}
void Module::automove_gates ( ) [private]

Definition at line 218 of file Module.cc.

                            {
  /*
    from top to down in the module hierarchy:

    for each module: iterate over gates:

      iterate over gate ports:
      - port unconnected - ignore
      - port connected - check all related

   */
}
void Module::cloneDeepInto ( DeepCopyable_shptr  destination,
oldnew_t oldnew 
) const [virtual]

Deep-copy all members to destination.

Todo:
Find out whether the default assignment operator can be used to simplify implementations of this method.

Reimplemented from degate::LogicModelObjectBase.

Definition at line 49 of file Module.cc.

References gates, modules, and ports.

                                                                          {
  auto clone = std::dynamic_pointer_cast<Module>(dest);
  
  // modules
  std::transform(modules.begin(), modules.end(), std::inserter(clone->modules, clone->modules.begin()), [&](const module_collection::value_type &v) {
    return std::dynamic_pointer_cast<Module>(v->cloneDeep(oldnew));
  });
  
  // gates
  std::transform(gates.begin(), gates.end(), std::inserter(clone->gates, clone->gates.begin()), [&](const gate_collection::value_type &v) {
    return std::dynamic_pointer_cast<Gate>(v->cloneDeep(oldnew));
  });
  
  // ports
  std::for_each(ports.begin(), ports.end(), [&](const port_collection::value_type &v) {
    clone->ports[v.first] = std::dynamic_pointer_cast<GatePort>(v.second->cloneDeep(oldnew));
  });
  
  LogicModelObjectBase::cloneDeepInto(dest, oldnew);
}

Return a copy of this object, but with all references to other DeepCopyables cleared (e.g.

set to 0-pointer).

Implements degate::DeepCopyable.

Definition at line 44 of file Module.cc.

References entity_name, degate::LogicModelObjectBase::get_name(), and is_root.

                                              {
  auto clone = std::make_shared<Module>(get_name(), entity_name, is_root);
  return clone;
}

Here is the call graph for this function:

Determine ports of a module.

Definition at line 267 of file Module.cc.

References exists_gate_port_recursive(), gate_port_already_named(), gates_begin(), gates_end(), is_main_module(), modules, net_completely_internal(), net_feeded_internally(), and ports.

Referenced by add_gate(), and remove_gate().

                                    {
  
  if (is_main_module()) {
    throw std::logic_error("determine_module_ports() is not suited for main modules. See determine_module_ports_for_root().");
  }
  
  int pnum = 0;
  port_collection new_ports;
  std::set<Net_shptr> known_net;

  for(gate_collection::iterator g_iter = gates_begin(); g_iter != gates_end(); ++g_iter) {

    Gate_shptr gate = *g_iter;
    assert(gate != NULL);

    for(Gate::port_const_iterator p_iter = gate->ports_begin(); p_iter != gate->ports_end(); ++p_iter) {

      GatePort_shptr gate_port = *p_iter;
      assert(gate_port != NULL);

      Net_shptr net = gate_port->get_net();
      std::cout << "Check net for object gate port " << gate_port->get_descriptive_identifier() << "?\n";

      bool net_already_processed = known_net.find(net) != known_net.end();
      if((net != NULL) && !net_already_processed && !net_completely_internal(net)) {

        bool is_a_port = false;
 
        for(Net::connection_iterator c_iter = net->begin(); c_iter != net->end() && !is_a_port; ++c_iter) {

          object_id_t oid = *c_iter;

          if(!exists_gate_port_recursive(oid)) { // outbound connection

            // Now we check, whether the connection is feeded by an outside entity or feeded
            // from this module.
            // Problem: We can't see the object outside this module, because we only have an
            // object ID and no logic model object to look up the object ID. Therefore we have
            // to derive the state of feeding from the objects we have in this or any sub-module.
            // If we see only in-ports in the net, the module port must be driven by an outside
            // port.

            GateTemplatePort_shptr tmpl_port = gate_port->get_template_port();
            assert(tmpl_port != NULL); // if a gate has no standard cell type, the gate cannot have a port
            
            if(net_feeded_internally(net) && tmpl_port->is_inport()) {
              std::cout << "  Net feeded internally, but port is inport. Will check where the net is driven.\n";        
            }
            else {
              std::string mod_port_name = gate_port_already_named(ports, gate_port);
              if(mod_port_name == "") {

                // generate a new port name and check if the port name is already in use
                do {
                  pnum++;
                  boost::format f("p%1%");
                  f % pnum;
                  mod_port_name = f.str();              
                }while(ports.find(mod_port_name) != ports.end());
              }
              
              std::cout << "  New module port: " << gate_port->get_descriptive_identifier() << " == "
                        << mod_port_name << "\n";
              new_ports[mod_port_name] = gate_port;
              
              is_a_port = true;
              std::cout << "  Set net as known net: " << net->get_descriptive_identifier() << "\n";
              known_net.insert(net);
            }
          }
        }
      }
      else if(net == NULL) {
        std::cout << "  Will not check net -- no net.\n";
      }
      else if(net_already_processed) {
        std::cout << "  Will not check net -- already processed " << net->get_descriptive_identifier() << ".\n";
      }
    }
  }
  

  // check sub-modules
  BOOST_FOREACH(Module_shptr sub, modules) {
    
    BOOST_FOREACH(port_collection::value_type const& p, sub->ports) {
      std::string mod_port_name = p.first;
      GatePort_shptr gate_port = p.second;
      Net_shptr net = gate_port->get_net();

      bool net_already_processed = known_net.find(net) != known_net.end();
      
      if(net != NULL && !net_already_processed && !net_completely_internal(net)) { // outbound connection
        new_ports[mod_port_name] = gate_port;
      }    
    }
  }
  ports = new_ports;

}

Here is the call graph for this function:

Here is the caller graph for this function:

Determine ports of children and sub-children (not including the "local" module, on which this function is called).

Definition at line 368 of file Module.cc.

References modules.

                                              {
  for (module_collection::iterator it = modules.begin(); it != modules.end(); ++it) {
    (*it)->determine_module_ports();
    (*it)->determine_module_ports_recursive();
  }
}
bool Module::exists_gate_port_recursive ( object_id_t  oid) const [private]

Check if there is a gate is the current module or any child module that has a gate port with the object ID oid .

Definition at line 379 of file Module.cc.

References lookup_gate_port_recursive().

Referenced by determine_module_ports().

                                                             {
  return lookup_gate_port_recursive(oid) != NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool Module::exists_module_port_name ( std::string const &  module_port_name) const

Check if a module port name exists.

Definition at line 539 of file Module.cc.

References ports.

                                                                            {
  return ports.find(module_port_name) != ports.end();
}
Module::gate_collection::iterator Module::gates_begin ( )

Definition at line 177 of file Module.cc.

References gates.

Referenced by determine_module_ports(), and move_gates_recursive().

                                                  {
  return gates.begin();
}

Here is the caller graph for this function:

Module::gate_collection::const_iterator Module::gates_begin ( ) const

Definition at line 201 of file Module.cc.

References gates.

                                                              {
  return gates.begin();
}
Module::gate_collection::iterator Module::gates_end ( )

Definition at line 181 of file Module.cc.

References gates.

Referenced by determine_module_ports(), and move_gates_recursive().

                                                {
  return gates.end();
}

Here is the caller graph for this function:

Module::gate_collection::const_iterator Module::gates_end ( ) const

Definition at line 205 of file Module.cc.

References gates.

                                                            {
  return gates.end();
}
std::string Module::get_entity_name ( ) const

Get name of the entity type.

Definition at line 83 of file Module.cc.

References entity_name.

                                        {
  return entity_name;
}
bool Module::is_main_module ( ) const

Check if module is the main module.

Definition at line 70 of file Module.cc.

References is_root.

Referenced by determine_module_ports(), and lookup_module().

                                  {
  return is_root;
}

Here is the caller graph for this function:

Definition at line 383 of file Module.cc.

References gates, and modules.

Referenced by exists_gate_port_recursive(), net_completely_internal(), and net_feeded_internally().

                                                                       {
  assert(oid != 0);

  for(gate_collection::const_iterator g_iter = gates.begin();
      g_iter != gates.end(); ++g_iter) {

    Gate_shptr gate = *g_iter;

    for(Gate::port_const_iterator p_iter = gate->ports_begin();
        p_iter != gate->ports_end(); ++p_iter)
      if((*p_iter)->get_object_id() == oid) return *p_iter;
  }

  for(module_collection::const_iterator iter = modules.begin();
      iter != modules.end(); ++iter)
    if(GatePort_shptr gport = (*iter)->lookup_gate_port_recursive(oid)) return gport;

  return GatePort_shptr();
}

Here is the caller graph for this function:

Module_shptr Module::lookup_module ( std::string const &  module_path) const

Lookup a sub-module.

Parameters:
module_pathPath to the module, e.g. : an absolute path "main_module/crypto/lfsr_a" an relative path "crypto/lfsr_a"
Returns:
Returns the module. In case of lookup failure a NULL pointer is returned.

Definition at line 464 of file Module.cc.

References degate::LogicModelObjectBase::get_name(), and is_main_module().

                                                                     {


  std::list<std::string> path_elements;
  boost::split(path_elements, module_path, boost::is_any_of("/"));
  
  if(is_main_module()) {

    // handle absolut path
    while(path_elements.size() > 0 &&
          get_name() != path_elements.front()) {
      path_elements.pop_front();
    }
  }

  if(path_elements.size() == 0)
    return Module_shptr();


  // leading path element should equals current module name
  if(get_name() != path_elements.front())
    return Module_shptr();
  else {
    path_elements.pop_front();
    return lookup_module(path_elements); // first real name is name of the main module, skipped
  }

}

Here is the call graph for this function:

Module_shptr Module::lookup_module ( std::list< std::string > &  path_elements) const [private]

Internal implementation of lookup_module().

Parameters:
path_elementsReference to a list of path elements. List is modified.
Returns:
Returns a valid pointer on success. Else a null pointer is returned.

Definition at line 493 of file Module.cc.

References modules.

                                                                           {
  
  if(path_elements.size() > 0) {

    BOOST_FOREACH(Module_shptr m, modules) {

      if(m->get_name() == path_elements.front()) {

        if(path_elements.size() == 1)
          return m;
        else {
          path_elements.pop_front();
          return m->lookup_module(path_elements);
        }
      }

    }
  }

  return Module_shptr();
}
boost::optional< std::string > Module::lookup_module_port_name ( GatePort_shptr  gate_port)

Lookup the module port name.

Returns:
Returns the module port name.

Definition at line 528 of file Module.cc.

References ports.

                                                                                 {
  
  for(Module::port_collection::const_iterator iter = ports.begin(); iter != ports.end(); ++iter) {
    if(iter->second == gate_port) {
      return boost::optional<std::string>(iter->first);
    }
  }
  return boost::optional<std::string>();
}
Module::module_collection::iterator Module::modules_begin ( )

Definition at line 169 of file Module.cc.

References modules.

                                                      {
  return modules.begin();
}
Module::module_collection::const_iterator Module::modules_begin ( ) const

Definition at line 193 of file Module.cc.

References modules.

                                                                  {
  return modules.begin();
}
Module::module_collection::iterator Module::modules_end ( )

Definition at line 173 of file Module.cc.

References modules.

                                                    {
  return modules.end();
}
Module::module_collection::const_iterator Module::modules_end ( ) const

Definition at line 197 of file Module.cc.

References modules.

                                                                {
  return modules.end();
}
void Module::move_gates_recursive ( Module dst_mod) [private]
Exceptions:
InvalidPointerExceptionThis exception is thrown if the parameter is a NULL pointer.

Definition at line 152 of file Module.cc.

References add_gate(), gates_begin(), gates_end(), degate::LogicModelObjectBase::get_name(), and modules.

                                                  {

  if(dst_mod == NULL)
    throw InvalidPointerException("Invalid pointer passed to move_all_child_gates_into_current_module().");

  for(gate_collection::iterator g_iter = gates_begin();
      g_iter != gates_end(); ++g_iter) {
    std::cout << "Add gate " << (*g_iter)->get_name() << " to module " << dst_mod->get_name() << std::endl;

    dst_mod->add_gate(*g_iter);
  }

  for(module_collection::iterator iter = modules.begin();
      iter != modules.end(); ++iter)
    (*iter)->move_gates_recursive(dst_mod);
}

Here is the call graph for this function:

bool Module::net_completely_internal ( Net_shptr  net) const [private]

Definition at line 232 of file Module.cc.

References lookup_gate_port_recursive().

Referenced by determine_module_ports().

                                                        {
  for(Net::connection_iterator c_iter = net->begin(); c_iter != net->end(); ++c_iter) {
    
    object_id_t oid = *c_iter;   
    GatePort_shptr gport = lookup_gate_port_recursive(oid);
    if(gport == NULL) { // external entity
      return false;
    }
  }
  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool Module::net_feeded_internally ( Net_shptr  net) const [private]

Definition at line 244 of file Module.cc.

References lookup_gate_port_recursive().

Referenced by determine_module_ports().

                                                      {
  for(Net::connection_iterator c_iter = net->begin(); c_iter != net->end(); ++c_iter) {
    
    object_id_t oid = *c_iter;   
    GatePort_shptr gport = lookup_gate_port_recursive(oid);
    
    if(gport != NULL) { // internal entity
      GateTemplatePort_shptr tmpl_port = gport->get_template_port();
      if(tmpl_port->is_outport()) return true;
    }
  }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Module::port_collection::iterator Module::ports_begin ( )

Definition at line 185 of file Module.cc.

References ports.

                                                  {
  return ports.begin();
}
Module::port_collection::const_iterator Module::ports_begin ( ) const

Definition at line 209 of file Module.cc.

References ports.

                                                              {
  return ports.begin();
}
Module::port_collection::iterator Module::ports_end ( )

Definition at line 189 of file Module.cc.

References ports.

                                                {
  return ports.end();
}
Module::port_collection::const_iterator Module::ports_end ( ) const

Definition at line 213 of file Module.cc.

References ports.

                                                            {
  return ports.end();
}

Remove a gate from a module.

This method even works if the gate is not a direct child. Afterwards the gate is completely removed from the module hierarchy (if you run this method for the "right" root node). In most cases you want to add the gate to the module hierarchy again. Therefore you have to call method add_gate() on your own.

Returns:
Returns true if a module was removed, else false.
See also:
add_gate()
Exceptions:
InvalidPointerExceptionThis exception is thrown, if gate is a NULL pointer.

Definition at line 96 of file Module.cc.

References determine_module_ports(), gates, is_root, and modules.

                                        {
  if(gate == NULL)
    throw InvalidPointerException("Invalid pointer passed to remove_gate().");


  gate_collection::const_iterator g_iter = gates.find(gate);
  if(g_iter != gates.end()) {
    gates.erase(g_iter);
    if(!is_root) determine_module_ports();
    return true;
  }
  else {

    for(module_collection::iterator iter = modules.begin();
        iter != modules.end(); ++iter) {

      Module_shptr child = *iter;
      if((*iter)->remove_gate(gate) == true) return true;
    }
  }

  return false;
}

Here is the call graph for this function:

Remove a submodule.

This method even works if the submodule is not a direct child. If you remove a child module that contains gates, the gates are moved one layer above.

Returns:
Returns true if a module was removed, else false.
Exceptions:
InvalidPointerExceptionThis exception is thrown, if gate is a NULL pointer.

Definition at line 130 of file Module.cc.

References modules.

                                              {

  if(module == NULL)
    throw InvalidPointerException("Invalid pointer passed to remove_module().");

  for(module_collection::iterator iter = modules.begin();
      iter != modules.end(); ++iter) {

    Module_shptr child = *iter;

    if(child == module) {
      child->move_gates_recursive(this);
      modules.erase(iter);
      return true;
    }
    else if((*iter)->remove_module(module) == true)
      return true;
  }

  return false;
}
void Module::set_entity_name ( std::string const &  name)

Set an identifier for the module type.

Definition at line 79 of file Module.cc.

References entity_name, and degate::LogicModelObjectBase::name.

                                                  {
  entity_name = name;
}

Set a module as main module.

This will set the new state only in the current module and will not disable the root-node-state of any other module.

Definition at line 74 of file Module.cc.

References is_root.

                             {
  is_root = true;
}
void Module::set_module_port_name ( std::string const &  module_port_name,
GatePort_shptr  adjacent_gate_port 
)

Set module port name.

Definition at line 515 of file Module.cc.

References ports.

                                                                                                      {

  for(Module::port_collection::const_iterator iter = ports.begin(); iter != ports.end(); ++iter) {
    if(iter->second == adjacent_gate_port) {
      ports.erase(iter->first);
      ports[module_port_name] = adjacent_gate_port;
      return;
    }
  }
  
}

Friends And Related Function Documentation

Determine ports of a root module.

Note: It would b more nice to have this function as a member function of class Module. But this would intorduce a dependency of LogicModel. The lmodel is neccessary for looking up objects, because main module's ports are modelled by having a special emarker in the net. I have no idea how the main module's ports could be identified else. (Using all ports is obviously not an option.)

friend class LogicModelImporter [friend]

Definition at line 43 of file Module.h.


Member Data Documentation

std::string degate::Module::entity_name [private]

Definition at line 64 of file Module.h.

Referenced by cloneShallow(), get_entity_name(), and set_entity_name().

bool degate::Module::is_root [private]

Definition at line 65 of file Module.h.

Referenced by add_gate(), cloneShallow(), is_main_module(), remove_gate(), and set_main_module().


The documentation for this class was generated from the following files: