 |
Cerata
A library to generate structural hardware designs
|
21 #include <unordered_map>
25 #include "cerata/utils.h"
26 #include "cerata/flattype.h"
27 #include "cerata/domain.h"
36 std::shared_ptr<Literal>
intl(int64_t);
37 typedef std::unordered_map<const Node *, Node *>
NodeMap;
63 class Type :
public Named,
public std::enable_shared_from_this<Type> {
98 bool Is(
ID type_id)
const;
106 virtual std::optional<Node *>
width()
const {
return std::nullopt; }
108 std::string
ToString(
bool show_meta =
false,
bool show_mappers =
false)
const;
111 std::vector<std::shared_ptr<TypeMapper>>
mappers()
const;
113 void AddMapper(
const std::shared_ptr<TypeMapper> &mapper,
bool remove_existing =
true);
115 std::optional<std::shared_ptr<TypeMapper>>
GetMapper(
Type *other,
bool generate_implicit =
true);
119 std::optional<std::shared_ptr<TypeMapper>>
GetMapper(
const std::shared_ptr<Type> &other);
128 virtual std::vector<Type *>
GetNested()
const {
return {}; }
131 std::unordered_map<std::string, std::string>
meta;
141 virtual std::shared_ptr<Type>
Copy(
const NodeMap &rebinding)
const = 0;
147 virtual std::shared_ptr<Type>
Copy()
const {
return Copy({}); }
154 std::shared_ptr<Type>
operator()(std::vector<Node *> nodes);
161 std::shared_ptr<Type>
operator()(
const std::vector<std::shared_ptr<Node>> &nodes);
171 std::shared_ptr<Type>
bit(
const std::string &name =
"bit");
180 std::optional<Node *>
width()
const override;
182 std::shared_ptr<Type>
Copy(
const NodeMap &rebinding)
const override;
186 std::shared_ptr<Type>
boolean();
198 std::shared_ptr<Type>
integer();
210 std::shared_ptr<Type>
string();
231 std::optional<Node *>
width()
const override;
239 std::shared_ptr<Type>
Copy(
const NodeMap &rebinding)
const override;
243 std::shared_ptr<Node> width_;
247 std::shared_ptr<Type>
vector(
const std::string &name,
const std::shared_ptr<Node> &width);
249 std::shared_ptr<Type>
vector(
const std::shared_ptr<Node> &width);
251 std::shared_ptr<Type>
vector(
unsigned int width);
253 std::shared_ptr<Type>
vector(std::string name,
unsigned int width);
256 class Field :
public Named,
public std::enable_shared_from_this<Field> {
259 Field(std::string
name, std::shared_ptr<Type>
type,
bool reverse =
false,
bool sep =
true);
263 std::shared_ptr<Type>
type()
const {
return type_; }
267 std::shared_ptr<Field>
Reverse();
269 bool sep()
const {
return sep_; }
275 std::unordered_map<std::string, std::string>
meta;
277 std::shared_ptr<Field>
Copy(
const NodeMap &rebinding)
const;
281 std::shared_ptr<Type> type_;
289 std::shared_ptr<Field>
field(
const std::string &name,
290 const std::shared_ptr<Type> &type,
291 bool reverse =
false,
295 std::shared_ptr<Field>
field(
const std::shared_ptr<Type> &type,
bool reverse =
false,
bool sep =
true);
298 std::shared_ptr<Field>
NoSep(std::shared_ptr<Field>
field);
308 explicit Record(std::string
name, std::vector<std::shared_ptr<Field>>
fields = {});
310 Record &
AddField(
const std::shared_ptr<Field> &
field, std::optional<size_t> index = std::nullopt);
312 bool Has(
const std::string &
name)
const;
314 Field *
at(
const std::string &
name)
const;
316 Field *
at(
size_t i)
const;
330 std::vector<Type *>
GetNested()
const override;
332 std::shared_ptr<Type>
Copy(
const NodeMap &rebinding)
const override;
347 std::shared_ptr<Record>
record(
const std::string &name);
355 std::shared_ptr<Record>
record(
const std::string &name,
356 const std::vector<std::shared_ptr<Field>> &fields);
363 std::shared_ptr<Record>
record(
const std::vector<std::shared_ptr<Field>> &fields);
371 std::shared_ptr<Record>
record(
const std::initializer_list<std::shared_ptr<Field>> &fields);
std::vector< std::shared_ptr< TypeMapper > > mappers() const
Return possible type mappers.
virtual bool IsNested() const =0
Return true if the Type is a nested, false otherwise.
bool IsPhysical() const override
Return true if the Type has an immediate physical representation, false otherwise.
std::vector< std::shared_ptr< Field > > fields_
The fields of this Record.
size_t num_fields() const
Return the number of fields in this record.
std::shared_ptr< Field > field(const std::string &name, const std::shared_ptr< Type > &type, bool reverse, bool sep)
Create a new RecordField, and return a shared pointer to it.
std::string ToStringFieldNames() const
Return the names of the fields as a comma separated string.
bool IsPhysical() const override
Return true if the Type has an immediate physical representation, false otherwise.
std::unordered_map< std::string, std::string > meta
Metadata for back-end implementations.
Field(std::string name, std::shared_ptr< Type > type, bool reverse=false, bool sep=true)
RecordField constructor.
bool IsPhysical() const override
Return true if the Type has an immediate physical representation, false otherwise.
std::shared_ptr< Field > Reverse()
Reverse the direction of this field and return itself.
std::shared_ptr< Field > NoSep(std::shared_ptr< Field > field)
Convenience function to disable the separator for a record field.
bool IsNested() const override
Return true if the Type is a nested, false otherwise.
bool IsPhysical() const override
Return true if the Type has an immediate physical representation, false otherwise.
virtual bool IsEqual(const Type &other) const
Determine if this Type is exactly equal to an other Type.
std::optional< Node * > width() const override
Return a pointer to the node representing the width of this vector, if specified.
Record(std::string name, std::vector< std::shared_ptr< Field >> fields={})
Record constructor.
A Record type containing zero or more fields.
bool IsPhysical() const override
Return true if the Type has an immediate physical representation, false otherwise.
Type(std::string name, ID id)
Type constructor.
Contains every Cerata class, function, etc...
bool sep() const
Return true if in name generation of this field name for flattened types a separator should be placed...
Field * operator[](size_t i) const
Return the field at index i contained by this record.
void UseSep()
Enable the separator in name generation of this field.
std::optional< std::shared_ptr< TypeMapper > > GetMapper(Type *other, bool generate_implicit=true)
Get a mapper to another type, if it exists. Generates one, if possible, when generate_implicit = true...
Field * at(const std::string &name) const
Return the field with a specific name.
bool IsNested() const override
Return true if the Type is a nested, false otherwise.
Type & SetWidth(std::shared_ptr< Node > width)
Set the width of this vector.
Field & SetType(std::shared_ptr< Type > type)
Change the type of this field.
Integer(std::string name)
Integer constructor.
virtual bool CanGenerateMapper(const Type &other) const
Check if a mapper can be generated to another specific type.
std::unordered_map< std::string, std::string > meta
KV storage for metadata of tools or specific backend implementations.
std::unordered_map< const Node *, Node * > NodeMap
A mapping from one object to another object, used in e.g. type generic rebinding.
bool Is(ID type_id) const
Return true if the Type ID is type_id, false otherwise.
ID id() const
Return the Type ID.
bool IsGeneric() const override
Return true if the Type is a generic type.
virtual std::vector< Type * > GetNested() const
Obtain any nested types.
int RemoveMappersTo(Type *other)
Remove all mappers to a specific type.
bool IsGeneric() const override
Return true if the Type is a generic type.
String(std::string name)
String constructor.
virtual bool IsPhysical() const =0
Return true if the Type has an immediate physical representation, false otherwise.
std::string name() const
Return the name of the object.
std::string ToString(bool show_meta=false, bool show_mappers=false) const
Return the Type ID as a human-readable string.
std::vector< Node * > GetGenerics() const override
Return all nodes that potentially parametrize the fields of this record.
Vector(std::string name, const std::shared_ptr< Node > &width)
Vector constructor.
std::vector< std::shared_ptr< TypeMapper > > mappers_
A list of mappers that can map this type to another type.
std::shared_ptr< Type > string()
Return a static string type.
std::shared_ptr< Type > Copy(const NodeMap &rebinding) const override
Make a copy of the type, and rebind any type generic nodes that are keys in the rebinding to their va...
bool IsNested() const override
Return true if the Type is a nested, false otherwise.
std::shared_ptr< Type > boolean()
Return a static boolean type.
std::shared_ptr< Literal > intl(int64_t i)
Obtain a shared pointer to an integer literal from the default node pool.
void AddMapper(const std::shared_ptr< TypeMapper > &mapper, bool remove_existing=true)
Add a type mapper.
std::vector< Type * > GetNested() const override
Obtain any nested types.
virtual std::shared_ptr< TypeMapper > GenerateMapper(Type *other)
Generate a new mapper to a specific other type. Should be checked with CanGenerateMapper first,...
Bit(std::string name)
Bit type constructor.
Convenience structure for anything that is named. Names are case-sensitive.
bool Has(const std::string &name) const
Return true if record has field with name name.
bool IsGeneric() const override
Return true if the Type is a generic type.
bool IsNested() const override
Return true if the Type is a nested, false otherwise.
virtual std::optional< Node * > width() const
Return the width of the type, if it is synthesizable.
std::vector< std::shared_ptr< Field > > fields() const
Return all fields contained by this record.
std::shared_ptr< Type > vector(const std::string &name, const std::shared_ptr< Node > &width)
Create a new vector type, and return a shared pointer to it.
bool IsPhysical() const override
Return true if the Type has an immediate physical representation, false otherwise.
bool IsEqual(const Type &other) const override
Determine if this Type is exactly equal to an other Type.
bool IsGeneric() const override
Return true if the Type is a generic type.
std::shared_ptr< Record > record(const std::string &name, const std::vector< std::shared_ptr< Field >> &fields)
Create a new Record type, and return a shared pointer to it.
Record & AddField(const std::shared_ptr< Field > &field, std::optional< size_t > index=std::nullopt)
Add a field to this Record.
Boolean(std::string name)
Boolean constructor.
std::shared_ptr< Type > Copy(const NodeMap &rebinding) const override
Make a copy of the type, and rebind any type generic nodes that are keys in the rebinding to their va...
std::shared_ptr< Field > Copy(const NodeMap &rebinding) const
Create a copy of the field.
std::shared_ptr< Type > operator()(std::vector< Node * > nodes)
Make a copy of the type, and rebind any type generic nodes in order of the GetGenerics call.
bool IsGeneric() const override
Return true if the Type is a generic type.
std::shared_ptr< Type > Copy(const NodeMap &rebinding) const override
Make a copy of the type, and rebind any type generic nodes that are keys in the rebinding to their va...
bool IsGeneric() const override
Return true if the Type is a generic type.
virtual std::vector< Node * > GetGenerics() const
Obtain any nodes that this type uses as generics.
std::shared_ptr< Type > integer()
Return a static integer type.
std::optional< Node * > width() const override
Bit width returns integer literal 1.
std::shared_ptr< Type > bit(const std::string &name)
Return a static bit type.
std::shared_ptr< Type > type() const
Return the type of the RecordField.
bool reversed() const
Return if this individual field should be reversed w.r.t. parent Record type itself on graph edges.
void NoSep()
Disable the separator in name generation of this field.
bool IsNested() const override
Return true if the Type is a nested, false otherwise.
virtual std::shared_ptr< Type > Copy() const
Make a copy of the type without rebinding.
virtual std::shared_ptr< Type > Copy(const NodeMap &rebinding) const =0
Make a copy of the type, and rebind any type generic nodes that are keys in the rebinding to their va...
bool IsEqual(const Type &other) const override
Determine if this Type is exactly equal to an other Type.
std::vector< Node * > GetGenerics() const override
Returns the width parameter of this vector, if any. Otherwise an empty list;.
virtual bool IsGeneric() const =0
Return true if the Type is a generic type.
bool IsNested() const override
Return true if the Type is a nested, false otherwise.
ID
The Type ID. Used for convenient type checking.