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 prefix == 'genvar ': # genvar does not have a specific type
174 return f'{prefix}{var_name}{suffix}'
175 if var_name:
176 return f'{prefix}logic [{self.width-1}:0] {var_name}{suffix}'
177 else:
178 return f'{prefix}logic [{self.width-1}:0]'
179
181 def __init__(self, width):
182 self.width = width
183
184 def to_str(self, var_name, context=None):
185 prefix = ''
186 suffix = ';'
187 if context:
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 ': # genvar does not have a specific type
193 return f'{prefix}{var_name}{suffix}'
194 if var_name:
195 return f'{prefix}logic signed[{self.width-1}:0] {var_name}{suffix}'
196 else:
197 return f'{prefix}logic signed[{self.width-1}:0]'
198
199
201 def __new__(cls, width):
202 return sc_uint(width)
203
205 def __new__(cls):
206 warnings.warn('double detected, currently treated as integer')
207 return sc_int(64)
208
210 def __new__(cls, width):
211 return sc_int(width)
212
214 def __new__(cls):
215 return sc_int(128)
216
218 def __new__(cls):
219 return sc_uint(128)
220
222 def __new__(cls):
223 return sc_int(64)
224
226 def __new__(cls):
227 return sc_uint(64)
228
230 def to_str(self, var_name):
231 if var_name:
232 raise ValueError('Void type can only be used in function return types and should not have name')
233 return 'void'
234
235
237 def __init__(self, T):
238 self.T = T
239
240 def to_str(self, var_name, context=None):
241 return self.T.to_str(var_name, context)
242
244 def __init__(self, T, size):
245 self.T = T
246 self.sz = size
247
249 if isinstance(self.T, array):
250 return self.T.get_element_type()
251 else:
252 return self.T
253
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)
257
259 def __new__(cls):
260 return sc_uint(1)
261
262
264 def __new__(cls):
265 return sc_int(32)
266
268 def __new__(cls):
269 return sc_int(8)
270
272 def __new__(cls):
273 return sc_int(8)
274
276 def __new__(cls):
277 return sc_uint(8)
278
280 def __new__(cls):
281 return sc_uint(32)
282
284 def __new__(cls):
285 return sc_int(16)
286
288 def __new__(cls):
289 return sc_uint(16)
290
292 def __new__(cls):
293 return sc_int(64)
294
296 def __new__(cls):
297 return sc_uint(64)
298
300 def __new__(cls):
301 return sc_uint(1)
302
303
304class vmodule(object):
305 def __init__(self, type_name, port_bindings=None):
306 self.type_name = type_name
307 self.port_bindings = port_bindings
308
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 */);'
313
314
__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)