Skip to content

Commit

Permalink
Use int.bit_length() when checking fitness for assignment width in or…
Browse files Browse the repository at this point in the history
…der to avoid OOM insanity #224
  • Loading branch information
amykyta3 committed Dec 18, 2024
1 parent afaf420 commit c8f87d6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/systemrdl/core/ExprVisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def visitNumberVerilog(self, ctx: SystemRDLParser.NumberVerilogContext):
src_ref_from_antlr(ctx.VLOG_INT())
)

if val >= (1 << width):
if val.bit_length() > width:
self.msg.fatal(
"Value of integer literal exceeds the specified width",
src_ref_from_antlr(ctx.VLOG_INT())
Expand Down
4 changes: 2 additions & 2 deletions src/systemrdl/properties/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def validate(self, node: m_node.Node, value: Any) -> None:
# 5.2.2.1-a: If value is a bit mask, the mask shall have the same width
# as the field
if not isinstance(value, bool) and isinstance(value, int):
if value >= (1 << node.width):
if value.bit_length() > node.width:
self.env.msg.error(
"Bit mask (%d) of property 'dontcompare' exceeds width (%d) of field '%s'"
% (value, node.width, node.inst_name),
Expand Down Expand Up @@ -433,7 +433,7 @@ def validate(self, node: m_node.Node, value: Any) -> None:
assert isinstance(node, m_node.FieldNode)
if isinstance(value, int):
# 9.5.1-c: The reset value cannot be larger than can fit in the field
if value >= (1 << node.width):
if value.bit_length() > node.width:
self.env.msg.error(
"The reset value (%d) of field '%s' cannot fit within its width (%d)"
% (value, node.inst_name, node.width),
Expand Down
4 changes: 2 additions & 2 deletions src/systemrdl/properties/user_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def validate(self, node: m_node.Node, value: Any) -> None:

# Spec does not specify, but assuming this check gets ignored for
# non-vector nodes
if isinstance(node, m_node.VectorNode):
if value >= (1 << node.width):
if isinstance(node, m_node.VectorNode) and isinstance(value, int):
if value.bit_length() > node.width:
self.env.msg.error(
"Value (%d) of the '%s' property cannot fit within the width (%d) of component '%s'"
% (value, self.name, node.width, node.inst_name),
Expand Down

0 comments on commit c8f87d6

Please sign in to comment.