Cerata
A library to generate structural hardware designs
resolve.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/vhdl/resolve.h"
16 
17 #include <memory>
18 #include <vector>
19 #include <string>
20 #include <unordered_map>
21 
22 #include "cerata/logging.h"
23 #include "cerata/type.h"
24 #include "cerata/graph.h"
25 #include "cerata/edge.h"
26 #include "cerata/vhdl/vhdl_types.h"
27 #include "cerata/vhdl/vhdl.h"
28 
29 namespace cerata::vhdl {
30 
31 static int ResolvePorts(Component *comp, Instance *inst, NodeMap *rebinding) {
32  int i = 0;
33  auto ports = inst->GetAll<Port>();
34  for (const auto &port : ports) {
35  if (port->type()->meta.count(meta::NO_INSERT_SIGNAL) == 0) {
36  AttachSignalToNode(comp, port, rebinding);
37  i++;
38  } else {
39  CERATA_LOG(DEBUG, "Skipping signal insertion for port " + port->name());
40  }
41  }
42  return i;
43 }
44 
52 static int ResolvePortArrays(Component *comp, Instance *inst, NodeMap *rebinding) {
53  // There is something utterly annoying in VHDL; range expressions must be "locally static" in port map
54  // associativity lists on the left hand side. This means we can't use any type generic nodes.
55  // Thanks, VHDL.
56  int i = 0;
57  // To solve this, we're just going to insert a signal array for every port array.
58  for (const auto &pa : inst->GetAll<PortArray>()) {
59  if (pa->type()->meta.count(meta::NO_INSERT_SIGNAL) == 0) {
60  AttachSignalArrayToNodeArray(comp, pa, rebinding);
61  i++;
62  } else {
63  CERATA_LOG(DEBUG, "Skipping signal insertion for port " + pa->name());
64  }
65  }
66  return i;
67 }
68 
70  // We are potentially going to make a bunch of copies of type generic nodes and array sizes.
71  // Remember which ones we did already and where we left their copy through a map.
72  auto children = comp->children();
73  for (const auto &inst : children) {
74  ResolvePorts(comp, inst, comp->inst_to_comp_map());
75  ResolvePortArrays(comp, inst, comp->inst_to_comp_map());
76  }
77  return comp;
78 }
79 
80 } // namespace cerata::vhdl
cerata::Component
A Component graph.
Definition: graph.h:158
cerata::AttachSignalToNode
Signal * AttachSignalToNode(Component *comp, NormalNode *node, NodeMap *rebinding, std::string name)
Attach a Signal to a Node, redirecting all edges through the new Signal.
Definition: edge.cc:254
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::Component::children
std::vector< Instance * > children() const
Returns all Instance graphs from this Component.
Definition: graph.h:187
cerata::AttachSignalArrayToNodeArray
SignalArray * AttachSignalArrayToNodeArray(Component *comp, NodeArray *array, NodeMap *rebinding)
Attach a SignalArray to a Node, redirecting all edges through the new SignalArray.
Definition: edge.cc:327
cerata::Component::inst_to_comp_map
NodeMap * inst_to_comp_map()
Return the component node to instance node mapping.
Definition: graph.h:198
cerata::vhdl::Resolve::SignalizePorts
static Component * SignalizePorts(Component *comp)
Transforms the component, inserting signals for every instance port.
Definition: resolve.cc:69
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::vhdl
Contains everything related to the VHDL back-end.
Definition: architecture.cc:31