Skip to content

Commit 5515283

Browse files
committed
Fix importer support for assigning array values to properties. #276
1 parent 31e33bc commit 5515283

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

src/systemrdl/properties/bases.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,17 @@ def assign_value(self, comp_def: 'comp.Component', value: Any, src_ref: 'SourceR
8080
assign_type = rdltypes.UserEnum
8181
else:
8282
# Value is already evaluated
83-
assign_type = type(value)
83+
if isinstance(value, list):
84+
# Value is a list. Construct an arrayed type
85+
if value:
86+
# List is not empty. Get member type
87+
member_type = type(value[0])
88+
else:
89+
# List is empty. Member type is unknown
90+
member_type = None
91+
assign_type = rdltypes.ArrayedType(member_type)
92+
else:
93+
assign_type = type(value)
8494

8595
# First check if the value's type is already directly compatible
8696
for valid_type in self.valid_types:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
property udp_string {
2+
type = string;
3+
component = all;
4+
};
5+
6+
property udp_string_array {
7+
type = string[];
8+
component = all;
9+
};
10+
11+
property udp_boolean {
12+
type = boolean;
13+
component = all;
14+
};
15+
16+
property udp_boolean_array {
17+
type = boolean[];
18+
component = all;
19+
};
20+
21+
property udp_longint {
22+
type = longint;
23+
component = all;
24+
};
25+
26+
property udp_longint_array {
27+
type = longint[];
28+
component = all;
29+
};

test/test_importer.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import unittest
2+
import os
23

34
from systemrdl import RDLCompiler, FieldNode, AddressableNode
45
from systemrdl.importer import RDLImporter
56

7+
#-------------------------------------------------------------------------------
68
class MyImporter(RDLImporter):
7-
89
def import_file(self, path: str) -> None:
910
super().import_file(path)
1011

@@ -79,6 +80,7 @@ def import_file(self, path: str) -> None:
7980
"reg1.some_signal", 0)
8081
self.add_child(my_rf, reg1)
8182

83+
8284
class TypeNameErrorTestcaseImporter(RDLImporter):
8385

8486
def import_file(self, path: str) -> None:
@@ -90,6 +92,42 @@ def import_file(self, path: str) -> None:
9092
# Creates a register instance with an illegal name.
9193
reg_t = self.create_reg_definition("illegal.type.name")
9294

95+
#-------------------------------------------------------------------------------
96+
class TypeTestImporter(RDLImporter):
97+
def import_file(self, path: str) -> None:
98+
super().import_file(path)
99+
100+
top = self.create_addrmap_definition("top")
101+
self.register_root_component(top)
102+
103+
reg = self.instantiate_reg(
104+
self.create_reg_definition(),
105+
"r",
106+
0x0,
107+
)
108+
self.add_child(top, reg)
109+
110+
field = self.instantiate_field(
111+
self.create_field_definition(),
112+
"f",
113+
0,
114+
32,
115+
)
116+
self.add_child(reg, field)
117+
118+
self.assign_property(top, "udp_string", "my string")
119+
self.assign_property(top, "udp_string_array", ["str1", "str2", "str3"])
120+
self.assign_property(top, "udp_boolean", True)
121+
self.assign_property(top, "udp_boolean_array", [True, False, True])
122+
self.assign_property(top, "udp_longint", 123)
123+
self.assign_property(top, "udp_longint_array", [12, 34, 56])
124+
125+
self.assign_property(reg, "udp_string_array", [])
126+
self.assign_property(reg, "udp_boolean_array", [])
127+
self.assign_property(reg, "udp_longint_array", [])
128+
129+
#-------------------------------------------------------------------------------
130+
93131
class TestImporter(unittest.TestCase):
94132
def test_importer(self):
95133
rdlc = RDLCompiler()
@@ -141,3 +179,28 @@ def test_illegal_type_name_import_raises_error(self):
141179
i = TypeNameErrorTestcaseImporter(rdlc)
142180
with self.assertRaisesRegex(ValueError, "Type name has invalid characters: 'illegal.type.name'"):
143181
i.import_file("asdf")
182+
183+
184+
def test_importer_types(self):
185+
rdlc = RDLCompiler()
186+
187+
this_dir = os.path.dirname(os.path.realpath(__file__))
188+
rdlc.compile_file(os.path.join(this_dir, "rdl_src/incdir/importer_udps.rdl"))
189+
190+
i = TypeTestImporter(rdlc)
191+
i.import_file("asdf")
192+
193+
root = rdlc.elaborate()
194+
195+
top = root.top
196+
self.assertEqual(top.get_property("udp_string"), "my string")
197+
self.assertListEqual(top.get_property("udp_string_array"), ["str1", "str2", "str3"])
198+
self.assertIs(top.get_property("udp_boolean"), True)
199+
self.assertListEqual(top.get_property("udp_boolean_array"), [True, False, True])
200+
self.assertEqual(top.get_property("udp_longint"), 123)
201+
self.assertListEqual(top.get_property("udp_longint_array"), [12, 34, 56])
202+
203+
reg = top.get_child_by_name("r")
204+
self.assertListEqual(reg.get_property("udp_string_array"), [])
205+
self.assertListEqual(reg.get_property("udp_boolean_array"), [])
206+
self.assertListEqual(reg.get_property("udp_longint_array"), [])

0 commit comments

Comments
 (0)