Cerata
A library to generate structural hardware designs
array.h
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 #pragma once
16 
17 #include <utility>
18 #include <optional>
19 #include <string>
20 #include <memory>
21 #include <vector>
22 
23 #include "cerata/node.h"
24 #include "cerata/port.h"
25 #include "cerata/type.h"
26 #include "cerata/signal.h"
27 
28 namespace cerata {
29 
31 class NodeArray : public Object {
32  public:
35 
37  NodeArray(std::string name, Node::NodeID id, std::shared_ptr<Node> base, const std::shared_ptr<Node> &size);
38 
40  void SetParent(Graph *new_parent) override;
41 
43  inline Node *size() const { return size_.get(); }
44 
46  void SetSize(const std::shared_ptr<Node> &size);
48  void SetType(const std::shared_ptr<Type> &type);
50  Type *type() const { return base_->type(); }
51 
53  std::shared_ptr<Object> Copy() const override;
54 
56  NodeArray *CopyOnto(Graph *dst, const std::string &name, NodeMap *rebinding);
57 
59  std::shared_ptr<Node> Append(bool increment_size = true);
61  std::vector<Node *> nodes() const { return ToRawPointers(nodes_); }
63  Node *node(size_t i) const;
65  Node *operator[](size_t i) const { return node(i); }
67  size_t num_nodes() const { return nodes_.size(); }
69  size_t IndexOf(const Node &n) const;
70 
72  std::string ToString() const { return name(); }
73 
75  std::shared_ptr<Node> base() const { return base_; }
76 
77  // @brief Append all objects referenced by this node array.
78  void AppendReferences(std::vector<Object *> *out) const override {
79  // This includes the size node and its references.
80  out->push_back(size_.get());
81  size_->AppendReferences(out);
82  // And the references of the base node.
83  base_->AppendReferences(out);
84  }
85 
86  protected:
90  void IncrementSize();
92  std::shared_ptr<Node> base_;
94  std::shared_ptr<Node> size_;
96  std::vector<std::shared_ptr<Node>> nodes_;
97 };
98 
100 class SignalArray : public NodeArray {
101  public:
103  SignalArray(const std::shared_ptr<Signal> &base, const std::shared_ptr<Node> &size)
104  : NodeArray(base->name(), Node::NodeID::SIGNAL, base, size) {}
105 };
106 
115 std::shared_ptr<SignalArray> signal_array(const std::string &name,
116  const std::shared_ptr<Type> &type,
117  const std::shared_ptr<Node>& size,
118  const std::shared_ptr<ClockDomain> &domain = default_domain());
119 
123 class PortArray : public NodeArray, public Term {
124  public:
126  PortArray(const std::shared_ptr<Port> &base, const std::shared_ptr<Node> &size);
128  std::shared_ptr<Object> Copy() const override;
129 };
130 
132 std::shared_ptr<PortArray> port_array(const std::string &name,
133  const std::shared_ptr<Type> &type,
134  const std::shared_ptr<Node> &size,
135  Port::Dir dir = Port::Dir::IN,
136  const std::shared_ptr<ClockDomain> &domain = default_domain());
137 
139 std::shared_ptr<PortArray> port_array(const std::shared_ptr<Port> &base_node,
140  const std::shared_ptr<Node> &size);
141 
142 } // namespace cerata
cerata::NodeArray::AppendReferences
void AppendReferences(std::vector< Object * > *out) const override
Append all objects that this object owns to the output.
Definition: array.h:78
cerata::NodeArray::node_id_
Node::NodeID node_id_
The type ID of the nodes in this NodeArray.
Definition: array.h:88
cerata::NodeArray::type
Type * type() const
Return the type of the nodes in the NodeArray.
Definition: array.h:50
cerata::NodeArray::SetParent
void SetParent(Graph *new_parent) override
Set the parent of this NodeArray base node and array nodes.
Definition: array.cc:104
cerata::NodeArray::nodes
std::vector< Node * > nodes() const
Return all nodes of this NodeArray.
Definition: array.h:61
cerata::SignalArray
An array of signal nodes.
Definition: array.h:100
cerata::SignalArray::SignalArray
SignalArray(const std::shared_ptr< Signal > &base, const std::shared_ptr< Node > &size)
SignalArray constructor.
Definition: array.h:103
cerata::NodeArray::CopyOnto
NodeArray * CopyOnto(Graph *dst, const std::string &name, NodeMap *rebinding)
Copy the NodeArray onto a graph, but not the array nodes. Creates a new size node set to zero.
Definition: array.cc:139
cerata::NodeArray::base_
std::shared_ptr< Node > base_
A node representing the template for each of the element nodes.
Definition: array.h:92
cerata::NodeArray::Append
std::shared_ptr< Node > Append(bool increment_size=true)
Append a node to this array, optionally incrementing the size node. Returns a pointer to that node.
Definition: array.cc:78
cerata::Term
A terminator structure to enable terminator sanity checks.
Definition: port.h:27
cerata::NodeArray::num_nodes
size_t num_nodes() const
Return the number of element nodes.
Definition: array.h:67
cerata::NodeArray::node_id
Node::NodeID node_id()
Return the type ID of the nodes in this NodeArray.
Definition: array.h:34
cerata::Graph
A graph representing a hardware structure.
Definition: graph.h:37
cerata::NodeArray
An array of nodes.
Definition: array.h:31
cerata::ToRawPointers
std::vector< T * > ToRawPointers(const std::vector< std::shared_ptr< T >> &list)
Convert a list of shared pointers to raw pointers.
Definition: utils.h:125
cerata::Type
A Type.
Definition: type.h:63
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::NodeArray::nodes_
std::vector< std::shared_ptr< Node > > nodes_
The nodes contained by this array.
Definition: array.h:96
cerata::Node
A node.
Definition: node.h:42
cerata::Term::Dir
Dir
Terminator direction.
Definition: port.h:30
cerata::NodeArray::Copy
std::shared_ptr< Object > Copy() const override
Deep-copy the NodeArray, but not the array nodes. Resets the size node to an integer literal of 0.
Definition: array.cc:134
cerata::NodeMap
std::unordered_map< const Node *, Node * > NodeMap
A mapping from one object to another object, used in e.g. type generic rebinding.
Definition: node.h:135
cerata::NodeArray::ToString
std::string ToString() const
Return a human-readable representation of this NodeArray.
Definition: array.h:72
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::PortArray::PortArray
PortArray(const std::shared_ptr< Port > &base, const std::shared_ptr< Node > &size)
Construct a new port array.
Definition: array.cc:171
cerata::Object
A Cerata Object on a graph.
Definition: object.h:34
cerata::NodeArray::base
std::shared_ptr< Node > base() const
Return the base node of this NodeArray.
Definition: array.h:75
cerata::NodeArray::IndexOf
size_t IndexOf(const Node &n) const
Return the index of a specific node.
Definition: array.cc:112
cerata::PortArray::Copy
std::shared_ptr< Object > Copy() const override
Make a copy of this port array.
Definition: array.cc:191
cerata::default_domain
std::shared_ptr< ClockDomain > default_domain()
Return a static default clock domain.
Definition: domain.cc:28
cerata::NodeArray::operator[]
Node * operator[](size_t i) const
Return element node i.
Definition: array.h:65
cerata::NodeArray::SetSize
void SetSize(const std::shared_ptr< Node > &size)
Set the size node.
Definition: array.cc:57
cerata::NodeArray::IncrementSize
void IncrementSize()
Increment the size of the ArrayNode.
Definition: array.cc:74
cerata::signal_array
std::shared_ptr< SignalArray > signal_array(const std::string &name, const std::shared_ptr< Type > &type, const std::shared_ptr< Node > &size, const std::shared_ptr< ClockDomain > &domain)
Construct a new node array and return a shared pointer to it.
Definition: array.cc:198
cerata::NodeArray::SetType
void SetType(const std::shared_ptr< Type > &type)
Set the type of the base node and array nodes.
Definition: array.cc:121
cerata::PortArray
An array of port nodes.
Definition: array.h:123
cerata::NodeArray::size_
std::shared_ptr< Node > size_
A node representing the number of concatenated edges.
Definition: array.h:94
cerata::NodeArray::size
Node * size() const
Return the size node.
Definition: array.h:43
cerata::NodeArray::NodeArray
NodeArray(std::string name, Node::NodeID id, std::shared_ptr< Node > base, const std::shared_ptr< Node > &size)
ArrayNode constructor.
Definition: array.cc:128
cerata::NodeArray::node
Node * node(size_t i) const
Return element node i.
Definition: array.cc:96
cerata::port_array
std::shared_ptr< PortArray > port_array(const std::string &name, const std::shared_ptr< Type > &type, const std::shared_ptr< Node > &size, Port::Dir dir, const std::shared_ptr< ClockDomain > &domain)
Get a smart pointer to a new ArrayPort.
Definition: array.cc:175