Cerata
A library to generate structural hardware designs
port.cc
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "cerata/port.h"
16 
17 #include <memory>
18 #include <utility>
19 
20 namespace cerata {
21 
22 std::shared_ptr<Port> port(const std::string &name,
23  const std::shared_ptr<Type> &type,
24  Term::Dir dir,
25  const std::shared_ptr<ClockDomain> &domain) {
26  return std::make_shared<Port>(name, type, dir, domain);
27 }
28 
29 std::shared_ptr<Port> port(const std::shared_ptr<Type> &type,
30  Term::Dir dir,
31  const std::shared_ptr<ClockDomain> &domain) {
32  return std::make_shared<Port>(type->name(), type, dir, domain);
33 }
34 
35 std::shared_ptr<Object> Port::Copy() const {
36  auto result = std::make_shared<Port>(name(), type_, dir(), domain_);
37  result->meta = meta;
38  return result;
39 }
40 
41 Port::Port(std::string name, std::shared_ptr<Type> type, Term::Dir dir, std::shared_ptr<ClockDomain> domain)
42  : NormalNode(std::move(name), Node::NodeID::PORT, std::move(type)), Synchronous(std::move(domain)), Term(dir) {}
43 
45  for (auto &e : edges()) {
46  RemoveEdge(e);
47  }
49  return *this;
50 }
51 
52 std::string Port::ToString() const {
53  return name() + ":" + type()->name() + ":" + Term::str(dir_);
54 }
55 
56 std::string Term::str(Term::Dir dir) {
57  switch (dir) {
58  case IN: return "in";
59  case OUT: return "out";
60  }
61  return "corrupt";
62 }
63 
65  switch (dir) {
66  case IN: return OUT;
67  case OUT: return IN;
68  }
69  CERATA_LOG(FATAL, "Corrupted terminator direction.");
70 }
71 
72 } // namespace cerata
cerata::Port::Copy
std::shared_ptr< Object > Copy() const override
Deep-copy the port.
Definition: port.cc:35
cerata::Port::Reverse
Port & Reverse()
Invert the direction of this port. Removes any edges.
Definition: port.cc:44
cerata::Object::meta
std::unordered_map< std::string, std::string > meta
KV storage for metadata of tools or specific backend implementations.
Definition: object.h:67
cerata::Term
A terminator structure to enable terminator sanity checks.
Definition: port.h:27
cerata::Term::str
static std::string str(Dir dir)
Convert a Dir to a human-readable string.
Definition: port.cc:56
cerata::Term::dir
Dir dir() const
Return the direction of this terminator.
Definition: port.h:36
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::Node::type_
std::shared_ptr< Type > type_
The Type of this Node.
Definition: node.h:126
cerata::Node
A node.
Definition: node.h:42
cerata::Term::Dir
Dir
Terminator direction.
Definition: port.h:30
cerata::Synchronous
Class to mark nodes with information for synchronous designs, e.g. clock domain.
Definition: domain.h:46
cerata::Named::name
std::string name() const
Return the name of the object.
Definition: utils.h:45
cerata::Node::NodeID
NodeID
Node type IDs with different properties.
Definition: node.h:45
cerata::Node::edges
virtual std::vector< Edge * > edges() const
Return all edges this Node is on.
Definition: node.cc:106
cerata::Synchronous::domain_
std::shared_ptr< ClockDomain > domain_
The clock domain.
Definition: domain.h:56
cerata::NormalNode
A single-input, multiple-outputs node.
Definition: node.h:167
cerata::Port::ToString
std::string ToString() const override
Return a human-readable string of this node.
Definition: port.cc:52
cerata::Term::Reverse
static Dir Reverse(Dir dir)
Return the inverse of a direction.
Definition: port.cc:64
cerata::Port::Port
Port(std::string name, std::shared_ptr< Type > type, Term::Dir dir, std::shared_ptr< ClockDomain > domain)
Construct a new port.
Definition: port.cc:41
cerata::Term::dir_
Dir dir_
The direction of this terminator.
Definition: port.h:51
cerata::Port
A port is a terminator node on a graph.
Definition: port.h:57
cerata::port
std::shared_ptr< Port > port(const std::string &name, const std::shared_ptr< Type > &type, Term::Dir dir, const std::shared_ptr< ClockDomain > &domain)
Make a new port with some name, type and direction.
Definition: port.cc:22
cerata::Node::type
Type * type() const
Return the node Type.
Definition: node.h:57
cerata::NormalNode::RemoveEdge
bool RemoveEdge(Edge *edge) override
Remove an edge from this node.
Definition: node.cc:223