Skip to content

Commit eb513fe

Browse files
committed
Polish attribute porting when allocate_type doesn't match type
SIMICS-23126
1 parent 6033899 commit eb513fe

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

py/dml/structure.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,15 @@ def field_msb(field):
22052205
type_site = None
22062206
# integer attributes of all sizes are converted to
22072207
# the corresponding 64-bit type
2208-
if atype.startswith('uint'):
2208+
expected_type = {'double': 'f', 'bool': 'b'}.get(atype, 'i')
2209+
if param_str(obj, 'type') not in {expected_type,
2210+
f'{expected_type}|n'}:
2211+
# allocate_type doesn't match attribute type, could e.g.
2212+
# be enum saved as string. Trust that the getter/setter
2213+
# works in this case, but remove the allocate_type
2214+
# (SIMICS-23126).
2215+
report(PATTRIBUTE(obj.site, None, param.site, None))
2216+
elif atype.startswith('uint'):
22092217
report(PATTRIBUTE(obj.site, 'uint64_attr', param.site,
22102218
type_site))
22112219
elif atype.startswith('int'):

py/port_dml.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,20 @@ def remove_param_decl(self, f, param_site):
654654
def apply(self, f):
655655
offs = self.offset(f)
656656
(template, allocate_type_param, type_param) = self.params
657-
if os.path.normcase(self.loc) in self.uint64_event_sites:
658-
template = template.replace('custom_', 'uint64_')
659-
instantiate_template(f, offs, template)
660-
self.remove_param_decl(f, allocate_type_param)
657+
if template:
658+
if os.path.normcase(self.loc) in self.uint64_event_sites:
659+
template = template.replace('custom_', 'uint64_')
660+
instantiate_template(f, offs, template)
661+
if allocate_type_param:
662+
if template is None:
663+
# Add `.val` manually if removing allocate_type param
664+
# without adding `is <type>_attr`.
665+
alloc_type_offs = self.offset(f, allocate_type_param)
666+
alloc_type_str = f.read_line(alloc_type_offs)
667+
m = re.search('"(.*)"', alloc_type_str)
668+
if m is not None:
669+
f.edit(alloc_type_offs, 0, f'session {m.group(1)} val;\n')
670+
self.remove_param_decl(f, allocate_type_param)
661671
if type_param:
662672
self.remove_param_decl(f, type_param)
663673

test/1.2/misc/porting.dml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ attribute ad {
296296
parameter allocate_type = "double"; // hello
297297
}
298298

299+
attribute enum_attr {
300+
parameter allocate_type = "uint64";
301+
parameter type = "s";
302+
method get() -> (attr_value_t v) {
303+
v = SIM_make_attr_string($this == 0 ? "zero" : "nonzero");
304+
}
305+
method set(attr_value_t v) {
306+
$this = SIM_attr_string(v)[0] == 'z' ? 0 : 1;
307+
}
308+
}
309+
299310
// PORT-DML-WARNING WUNUSED
300311
if (false) {
301312
attribute ab { parameter allocate_type = "bool"; }

test/1.4/misc/porting.dml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,17 @@ attribute ad is double_attr {
319319
// hello
320320
}
321321

322+
attribute enum_attr {
323+
session uint64 val;
324+
param type = "s";
325+
method get() -> (attr_value_t) {
326+
return SIM_make_attr_string(this.val == 0 ? "zero" : "nonzero");
327+
}
328+
method set(attr_value_t v) throws {
329+
this.val = SIM_attr_string(v)[0] == 'z' ? 0 : 1;
330+
}
331+
}
332+
322333
// PORT-DML-WARNING WUNUSED
323334
#if (false) {
324335
attribute ab { param allocate_type = "bool"; }

0 commit comments

Comments
 (0)