Skip to content

Commit e30d822

Browse files
rroohhhwhitequark
authored andcommitted
back.rtlil: espace { and } in format string
Fixes #1518
1 parent 8c5ed40 commit e30d822

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

amaranth/back/rtlil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ def emit_print(self, cell_idx, cell):
11321132
if cell.format is not None:
11331133
for chunk in cell.format.chunks:
11341134
if isinstance(chunk, str):
1135-
format.append(chunk)
1135+
format.append(chunk.replace("{", "{{").replace("}", "}}"))
11361136
else:
11371137
spec = _ast.Format._parse_format_spec(chunk.format_desc, _ast.Shape(len(chunk.value), chunk.signed))
11381138
type = spec["type"]

tests/test_back_rtlil.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,51 @@ def test_print_align(self):
20082008
end
20092009
""")
20102010

2011+
def test_escape_curly(self):
2012+
m = Module()
2013+
m.d.comb += [
2014+
Print("{"),
2015+
Print("}"),
2016+
]
2017+
self.assertRTLIL(m, [], R"""
2018+
attribute \generator "Amaranth"
2019+
attribute \top 1
2020+
module \top
2021+
wire width 1 $1
2022+
wire width 1 $2
2023+
process $3
2024+
assign $1 [0] 1'0
2025+
assign $1 [0] 1'1
2026+
end
2027+
cell $print $4
2028+
parameter \FORMAT "{{\n"
2029+
parameter \ARGS_WIDTH 0
2030+
parameter signed \PRIORITY 32'11111111111111111111111111111110
2031+
parameter \TRG_ENABLE 0
2032+
parameter \TRG_WIDTH 0
2033+
parameter \TRG_POLARITY 0
2034+
connect \EN $1 [0]
2035+
connect \ARGS { }
2036+
connect \TRG { }
2037+
end
2038+
process $5
2039+
assign $2 [0] 1'0
2040+
assign $2 [0] 1'1
2041+
end
2042+
cell $print $6
2043+
parameter \FORMAT "}}\n"
2044+
parameter \ARGS_WIDTH 0
2045+
parameter signed \PRIORITY 32'11111111111111111111111111111100
2046+
parameter \TRG_ENABLE 0
2047+
parameter \TRG_WIDTH 0
2048+
parameter \TRG_POLARITY 0
2049+
connect \EN $2 [0]
2050+
connect \ARGS { }
2051+
connect \TRG { }
2052+
end
2053+
end
2054+
""")
2055+
20112056

20122057
class DetailTestCase(RTLILTestCase):
20132058
def test_enum(self):

0 commit comments

Comments
 (0)