1"""Primitive types from System C and C++"""
8 Type context provides a way to contextualize types, enabling generation of input ports and output ports
10 def __init__(self, prefix=None, suffix=None, sep=None):
17 if this
is None and that
is not None:
19 elif this
is not None and that
is None:
21 elif this
is None and that
is None:
27 """merges fields of two contexts, throws error if both fields are non None"""
29 return copy.copy(self)
30 prefix = TypeContext.merge_field_default_this(self.
prefix, that.prefix)
31 suffix = TypeContext.merge_field_default_this(self.
suffix, that.suffix)
32 sep = TypeContext.merge_field_default_this(self.
sep, that.sep)
33 return TypeContext(prefix=prefix, suffix=suffix, sep=sep)
39 handles automatically subclass registration for primitive types
41 primitive_type_names = []
42 name_mapping = {
'_Bool':
'cppbool',
'bool':
'cppbool',
'unsigned_int':
'cppuint',
'int':
'cppint',
'unsigned_short':
'cppushort',
'short':
'cppshort',
43 'char':
'cppchar',
'unsigned_char':
'cppunsignedchar',
'signed_char':
'cppsignedchar',
'long_long':
'cpplonglong',
'unsigned_long_long':
'cppulonglong'}
46 """registers subclass automatically"""
52 """convert the primitive name string -> type to a dict"""
53 return dict(zip(map(
lambda x: x.__name__, Primitive.primitive_type_names), Primitive.primitive_type_names))
57 """get the primitive type of name"""
58 type_dict = Primitive.get_primitive_name_dict()
59 name = Primitive.name_filter(name)
61 return type_dict[name]
67 """maps type name to internal name"""
68 if name
in Primitive.name_mapping:
69 return Primitive.name_mapping[name]
85 def to_str(self, var_name, context=None):
86 input_context =
TypeContext(prefix=
'input', suffix=
',').update_not_none(context)
87 return self.
T.
to_str(var_name=var_name, context=input_context)
97 def to_str(self, var_name, context=None):
98 input_context =
TypeContext(prefix=
'inout', suffix=
',').update_not_none(context)
99 return self.
T.
to_str(var_name=var_name, context=input_context)
105 def to_str(self, var_name, context=None):
106 output_context =
TypeContext(prefix=
'output', suffix=
',')
107 input_context =
TypeContext(prefix=
'input', suffix=
',')
109 res = self.
T.
to_str(var_name +
'_data', context=output_context)
122 def to_str(self, var_name, context=None):
123 output_context =
TypeContext(prefix=
'output', suffix=
',')
124 input_context =
TypeContext(prefix=
'input', suffix=
',')
126 res = self.
T.
to_str(var_name +
'_data', context=input_context)
139 def to_str(self, var_name, context=None):
140 output_context =
TypeContext(prefix=
'output', suffix=
',').update_not_none(context)
141 return self.
T.
to_str(var_name=var_name, context=output_context)
148 def to_str(self, var_name, context=None):
152 if context.prefix
is not None:
153 prefix = context.prefix +
' '
154 if context.suffix
is not None:
155 suffix = context.suffix
157 return f
'{prefix}logic [{self.width-1}:0] {var_name}{suffix}'
159 return f
'{prefix}logic [{self.width-1}:0]'
165 def to_str(self, var_name, context=None):
169 if context.prefix
is not None:
170 prefix = context.prefix +
' '
171 if context.suffix
is not None:
172 suffix = context.suffix
173 if prefix ==
'genvar ':
174 return f
'{prefix}{var_name}{suffix}'
176 return f
'{prefix}logic [{self.width-1}:0] {var_name}{suffix}'
178 return f
'{prefix}logic [{self.width-1}:0]'
184 def to_str(self, var_name, context=None):
188 if context.prefix
is not None:
189 prefix = context.prefix +
' '
190 if context.suffix
is not None:
191 suffix = context.suffix
192 if prefix ==
'genvar ':
193 return f
'{prefix}{var_name}{suffix}'
195 return f
'{prefix}logic signed[{self.width-1}:0] {var_name}{suffix}'
197 return f
'{prefix}logic signed[{self.width-1}:0]'
206 warnings.warn(
'double detected, currently treated as integer')
232 raise ValueError(
'Void type can only be used in function return types and should not have name')
240 def to_str(self, var_name, context=None):
241 return self.
T.
to_str(var_name, context)
249 if isinstance(self.
T, array):
254 def to_str(self, var_name, context=None):
255 sz_str =
''.join([
'[0:{}]'.format(sz - 1)
for sz
in self.
sz])
256 return self.
T.
to_str(
'{}{}'.format(var_name, sz_str), context)
309 def to_str(self, var_name, context=None):
310 warnings.warn(
'port binding not fully implemented')
311 binding_str =
',\n'.join(f
'.{b[0]}({b[1]})' for b
in self.
port_bindings)
312 return f
'{self.type_name} {var_name}(\n{binding_str}\n/* port bindings not fully implemented */);'
__init_subclass__(cls, **kwargs)
get_primitive_name_dict()
list primitive_type_names
update_not_none(self, that)
__init__(self, prefix=None, suffix=None, sep=None)
merge_field_default_this(this, that)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
__init__(self, type_name, port_bindings=None)