Cerata
A library to generate structural hardware designs
parameter.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 <utility>
16 #include <vector>
17 #include <string>
18 #include <memory>
19 
20 #include "cerata/parameter.h"
21 #include "cerata/pool.h"
22 #include "cerata/graph.h"
23 #include "cerata/edge.h"
24 
25 namespace cerata {
26 
27 Parameter::Parameter(std::string name, const std::shared_ptr<Type> &type, std::shared_ptr<Literal> default_value)
28  : NormalNode(std::move(name), Node::NodeID::PARAMETER, type), default_value_(std::move(default_value)) {
29  if (default_value_ == nullptr) {
30  switch (type->id()) {
31  case Type::ID::STRING: default_value_ = strl("");
32  break;
33  case Type::ID::BOOLEAN: default_value_ = booll(false);
34  break;
35  case Type::ID::INTEGER: default_value_ = intl(0);
36  break;
37  default:CERATA_LOG(ERROR, "Parameter default value can not be set implicitly.");
38  }
39  } else if (!default_value_->IsLiteral()) {
40  CERATA_LOG(ERROR, "Parameter default value must be literal.");
41  }
42  Connect(this, default_value_);
43 }
44 
45 std::shared_ptr<Parameter> parameter(const std::string &name,
46  const std::shared_ptr<Type> &type,
47  std::shared_ptr<Literal> default_value) {
48  auto p = new Parameter(name, type, std::move(default_value));
49  return std::shared_ptr<Parameter>(p);
50 }
51 
52 std::shared_ptr<Parameter> parameter(const std::string &name, int default_value) {
53  return parameter(name, integer(), intl(default_value));
54 }
55 
56 std::shared_ptr<Parameter> parameter(const std::string &name, bool default_value) {
57  return parameter(name, boolean(), booll(default_value));
58 }
59 
60 std::shared_ptr<Parameter> parameter(const std::string &name, std::string default_value) {
61  return parameter(name, string(), strl(std::move(default_value)));
62 }
63 
64 std::shared_ptr<Parameter> parameter(const std::string &name) {
65  return parameter(name, 0);
66 }
67 
69  if (input()) {
70  return input().value()->src();
71  } else {
72  CERATA_LOG(FATAL, "Parameter node " + name() + " lost input edge.");
73  }
74 }
75 
76 Parameter *Parameter::SetValue(const std::shared_ptr<Node> &value) {
77  if (value->IsSignal() || value->IsPort()) {
78  CERATA_LOG(FATAL, "Parameter value can not be or refer to signal or port nodes.");
79  }
80 
81  Connect(this, value);
82  return this;
83 }
84 
85 std::shared_ptr<Object> Parameter::Copy() const {
86  auto result = parameter(name(), type_, default_value_);
87  result->meta = this->meta;
88  return result;
89 }
90 
91 void Parameter::TraceValue(std::vector<Node *> *trace) {
92  trace->push_back(this);
93  if (value()->IsParameter()) {
94  value()->AsParameter()->TraceValue(trace);
95  } else {
96  trace->push_back(value());
97  }
98 }
99 
100 } // namespace cerata
cerata::booll
std::shared_ptr< Literal > booll(bool value)
Return a literal node representing a Boolean.
Definition: pool.h:156
cerata::Parameter::TraceValue
void TraceValue(std::vector< Node * > *trace)
Append this node and nodes that source this node's value, until an expression or literal is encounter...
Definition: parameter.cc:91
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::Parameter::value
Node * value() const
Return the value node.
Definition: parameter.cc:68
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::Parameter::default_value_
std::shared_ptr< Literal > default_value_
Parameter value.
Definition: parameter.h:50
cerata::Node
A node.
Definition: node.h:42
cerata::Type::id
ID id() const
Return the Type ID.
Definition: type.h:88
cerata::Parameter::SetValue
Parameter * SetValue(const std::shared_ptr< Node > &value)
Set the value of the parameter node. Can only be expression, parameter, or literal.
Definition: parameter.cc:76
cerata::strl
std::shared_ptr< Literal > strl(std::string str)
Obtain a shared pointer to a string literal from the default node pool.
Definition: pool.h:151
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::Parameter::Parameter
Parameter(std::string name, const std::shared_ptr< Type > &type, std::shared_ptr< Literal > default_value)
Construct a new Parameter, optionally defining a default value Literal.
Definition: parameter.cc:27
cerata::NormalNode
A single-input, multiple-outputs node.
Definition: node.h:167
cerata::Parameter::Copy
std::shared_ptr< Object > Copy() const override
Create a copy of this Parameter.
Definition: parameter.cc:85
cerata::intl
std::shared_ptr< Literal > intl(int64_t i)
Obtain a shared pointer to an integer literal from the default node pool.
Definition: pool.h:144
cerata::Parameter
A Parameter node.
Definition: parameter.h:29
cerata::parameter
std::shared_ptr< Parameter > parameter(const std::string &name, const std::shared_ptr< Type > &type, std::shared_ptr< Literal > default_value)
Create a new parameter.
Definition: parameter.cc:45
cerata::integer
std::shared_ptr< Type > integer()
Return a static integer type.
Definition: type.cc:256
cerata::NormalNode::input
std::optional< Edge * > input() const
Return the single incoming edge.
Definition: node.cc:190
cerata::Node::type
Type * type() const
Return the node Type.
Definition: node.h:57
cerata::Connect
std::shared_ptr< Edge > Connect(Node *dst, Node *src)
Connect two nodes, returns the corresponding edge.
Definition: edge.cc:68