-
Notifications
You must be signed in to change notification settings - Fork 20
SystemVerilog: conversion from/to packed union #738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| KNOWNBUG | ||
| CORE | ||
| unions1.sv | ||
| --bound 0 | ||
| ^EXIT=0$ | ||
| ^SIGNAL=0$ | ||
| -- | ||
| -- | ||
| cast bitvector to union missing |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| CORE | ||
| unions4.sv | ||
| --bound 0 | ||
| ^EXIT=0$ | ||
| ^SIGNAL=0$ | ||
| -- | ||
| -- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| module main; | ||
|
|
||
| union packed { | ||
| bit [6:0] field1; | ||
| bit [6:0] field2; | ||
| } u; | ||
|
|
||
| initial u.field1 = 7'b1010101; | ||
|
|
||
| // Packed unions can be treated like bit-vectors. | ||
| // Expected to pass. | ||
| p0: assert property ($bits(u) == 7); | ||
| p1: assert property (u == 7'b1010101); | ||
| p2: assert property (u[1] == 0); | ||
|
|
||
| endmodule |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2144,6 +2144,34 @@ typet verilog_typecheck_exprt::enum_decay(const typet &type) | |
|
|
||
| /*******************************************************************\ | ||
|
|
||
| Function: verilog_typecheck_exprt::union_decay | ||
|
|
||
| Inputs: | ||
|
|
||
| Outputs: | ||
|
|
||
| Purpose: | ||
|
|
||
| \*******************************************************************/ | ||
|
|
||
| void verilog_typecheck_exprt::union_decay(exprt &expr) const | ||
| { | ||
| // 1800-2017 7.3.1 | ||
| // Verilog union types decay to a vector type [$bits(t)-1:0] when | ||
| // used in relational or arithmetic expressions. | ||
| auto &type = expr.type(); | ||
| if(type.id() == ID_union) | ||
| { | ||
| auto new_type = | ||
| unsignedbv_typet{numeric_cast_v<std::size_t>(get_width(type))}; | ||
| // The synthesis stage turns these typecasts into a member | ||
| // expression. | ||
| expr = typecast_exprt{expr, new_type}; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this reach our back-end? I don't think we'd support casts from a union. I suspect we'd need an extractbits here instead.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the synthesis stage turns these into a member expression -- will add comment. |
||
| } | ||
| } | ||
|
|
||
| /*******************************************************************\ | ||
|
|
||
| Function: verilog_typecheck_exprt::tc_binary_expr | ||
|
|
||
| Inputs: | ||
|
|
@@ -2158,6 +2186,9 @@ void verilog_typecheck_exprt::tc_binary_expr( | |
| const exprt &expr, | ||
| exprt &op0, exprt &op1) | ||
| { | ||
| union_decay(op0); | ||
| union_decay(op1); | ||
|
|
||
| // follows 1800-2017 11.8.2 | ||
| const typet new_type = | ||
| max_type(enum_decay(op0.type()), enum_decay(op1.type())); | ||
|
|
@@ -2223,6 +2254,9 @@ void verilog_typecheck_exprt::typecheck_relation(binary_exprt &expr) | |
| auto &lhs = expr.lhs(); | ||
| auto &rhs = expr.rhs(); | ||
|
|
||
| union_decay(lhs); | ||
| union_decay(rhs); | ||
|
|
||
| // Relations are special-cased in 1800-2017 11.8.2. | ||
| const typet new_type = | ||
| max_type(enum_decay(lhs.type()), enum_decay(rhs.type())); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious: does Verilog require that all fields have the same width? If not, could there be any problems if the first field wasn't sufficiently wide?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in Verilog packed unions all fields must be the same width. Will add comment to say so.