systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
primitives.py
Go to the documentation of this file.
1"""Primitive types from System C and C++"""
2import copy
3import warnings
4
5
6class TypeContext(object):
7 """
8 Type context provides a way to contextualize types, enabling generation of input ports and output ports
9 """
10 def __init__(self, prefix=None, suffix=None, sep=None):
11 self.prefix = prefix
12 self.suffix = suffix
13 self.sep = sep
14
15 @staticmethod
16 def merge_field_default_this(this, that):
17 if this is None and that is not None:
18 return that
19 elif this is not None and that is None:
20 return this
21 elif this is None and that is None:
22 return None
23 else:
24 return that
25
26 def update_not_none(self, that):
27 """merges fields of two contexts, throws error if both fields are non None"""
28 if that is 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)
34
35
37 """
38 Primitive base class,
39 handles automatically subclass registration for primitive types
40 """
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'}
44
45 def __init_subclass__(cls, **kwargs):
46 """registers subclass automatically"""
47 super().__init_subclass__(**kwargs)
48 cls.primitive_type_names.append(cls)
49
50 @staticmethod
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))
54
55 @staticmethod
56 def get_primitive(name):
57 """get the primitive type of name"""
58 type_dict = Primitive.get_primitive_name_dict()
59 name = Primitive.name_filter(name)
60 if name in type_dict:
61 return type_dict[name]
62 else:
63 return None
64
65 @staticmethod
66 def name_filter(name):
67 """maps type name to internal name"""
68 if name in Primitive.name_mapping:
69 return Primitive.name_mapping[name]
70 else:
71 return name
72
73
74# TODO: use decorators to embed context
75# TODO: use context object instead of string
76# TODO: not clear when to decide the `logic' part
78 def __init__(self, T):
79 self.T = T
80
81 @staticmethod
83 return TypeContext(prefix='input')
84
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)
88
90 def __init__(self, T):
91 self.T = T
92
93 @staticmethod
95 return TypeContext(prefix='inout')
96
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)
100
102 def __init__(self, T):
103 self.T = T
104
105 def to_str(self, var_name, context=None):
106 output_context = TypeContext(prefix='output', suffix=',') #
107 input_context = TypeContext(prefix='input', suffix=',') #
108 # print('sc_rvd_out ' + var_name)
109 res = self.T.to_str(var_name + '_data', context=output_context)
110 if res[-1] == ',':
111 res = res[0:-1]
112 # create
113 res += ',\n' + sc_in(sc_bv(1)).to_str(var_name + '_valid') + ',\n' + sc_out(sc_bv(1)).to_str(var_name + '_ready')
114 # print('...', res)
115 return res
116
117
119 def __init__(self, T):
120 self.T = T
121
122 def to_str(self, var_name, context=None):
123 output_context = TypeContext(prefix='output', suffix=',') #
124 input_context = TypeContext(prefix='input', suffix=',') #
125 # print('sc_rvd_in ' + var_name)
126 res = self.T.to_str(var_name + '_data', context=input_context)
127 if res[-1] == ',':
128 res = res[0:-1]
129 # create
130 res += ',\n' + sc_out(sc_bv(1)).to_str(var_name + '_valid') + ',\n' + sc_in(sc_bv(1)).to_str(var_name + '_ready')
131 # print('...', res)
132 return res
133
134
136 def __init__(self, T):
137 self.T = T
138
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)
142
143
145 def __init__(self, width):
146 self.width = width
147
148 def to_str(self, var_name, context=None):
149 prefix = ''
150 suffix = ';'
151 if context:
152 if context.prefix is not None:
153 prefix = context.prefix + ' '
154 if context.suffix is not None:
155 suffix = context.suffix
156 if var_name:
157 return f'{prefix}logic [{self.width-1}:0] {var_name}{suffix}'
158 else:
159 return f'{prefix}logic [{self.width-1}:0]'
160
162 def __init__(self, width):
163 self.width = width
164
165 def to_str(self, var_name, context=None):
166 prefix = ''
167 suffix = ';'
168 if context:
169 if context.prefix is not None:
170 prefix = context.prefix + ' '
171 if context.suffix is not None:
172 suffix = context.suffix
173 if var_name:
174 return f'{prefix}logic [{self.width-1}:0] {var_name}{suffix}'
175 else:
176 return f'{prefix}logic [{self.width-1}:0]'
177
179 def __init__(self, width):
180 self.width = width
181
182 def to_str(self, var_name, context=None):
183 prefix = ''
184 suffix = ';'
185 if context:
186 if context.prefix is not None:
187 prefix = context.prefix + ' '
188 if context.suffix is not None:
189 suffix = context.suffix
190 if var_name:
191 return f'{prefix}logic signed[{self.width-1}:0] {var_name}{suffix}'
192 else:
193 return f'{prefix}logic signed[{self.width-1}:0]'
194
195
197 def __new__(cls, width):
198 return sc_uint(width)
199
201 def __new__(cls):
202 warnings.warn('double detected, currently treated as integer')
203 return sc_int(64)
204
206 def __new__(cls, width):
207 return sc_int(width)
208
210 def __new__(cls):
211 return sc_int(128)
212
214 def __new__(cls):
215 return sc_uint(128)
216
218 def __new__(cls):
219 return sc_int(64)
220
222 def __new__(cls):
223 return sc_uint(64)
224
226 def to_str(self, var_name):
227 if var_name:
228 raise ValueError('Void type can only be used in function return types and should not have name')
229 return 'void'
230
231
233 def __init__(self, T):
234 self.T = T
235
236 def to_str(self, var_name, context=None):
237 return self.T.to_str(var_name, context)
238
240 def __init__(self, T, size):
241 self.T = T
242 self.sz = size
243
245 if isinstance(self.T, array):
246 return self.T.get_element_type()
247 else:
248 return self.T
249
250 def to_str(self, var_name, context=None):
251 sz_str = ''.join(['[0:{}]'.format(sz - 1) for sz in self.sz])
252 return self.T.to_str('{}{}'.format(var_name, sz_str), context)
253
255 def __new__(cls):
256 return sc_uint(1)
257
258
260 def __new__(cls):
261 return sc_int(32)
262
264 def __new__(cls):
265 return sc_int(8)
266
268 def __new__(cls):
269 return sc_int(8)
270
272 def __new__(cls):
273 return sc_uint(8)
274
276 def __new__(cls):
277 return sc_uint(32)
278
280 def __new__(cls):
281 return sc_int(16)
282
284 def __new__(cls):
285 return sc_uint(16)
286
288 def __new__(cls):
289 return sc_int(64)
290
292 def __new__(cls):
293 return sc_uint(64)
294
296 def __new__(cls):
297 return sc_uint(1)
298
299
300class vmodule(object):
301 def __init__(self, type_name, port_bindings=None):
302 self.type_name = type_name
303 self.port_bindings = port_bindings
304
305 def to_str(self, var_name, context=None):
306 warnings.warn('port binding not fully implemented')
307 binding_str = ',\n'.join(f'.{b[0]}({b[1]})' for b in self.port_bindings)
308 return f'{self.type_name} {var_name}(\n{binding_str}\n/* port bindings not fully implemented */);'
309
310
__init_subclass__(cls, **kwargs)
Definition primitives.py:45
__init__(self, prefix=None, suffix=None, sep=None)
Definition primitives.py:10
to_str(self, var_name, context=None)
Definition primitives.py:97
__init__(self, T, size)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
to_str(self, var_name, context=None)
Definition primitives.py:85
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)
to_str(self, var_name)