aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDag Lem <dag@nimrod.no>2023-02-09 19:27:51 +0100
committerDag Lem <dag@nimrod.no>2023-02-15 11:44:24 +0100
commitc1e12877f0de5a96356f22c28275d7a7546f771e (patch)
tree2f7217e4502267ae1232bf849b87d60fcd05873f
parent53bda9de542e2e39763520e7291f26f85e57b67b (diff)
downloadyosys-c1e12877f0de5a96356f22c28275d7a7546f771e.tar.gz
yosys-c1e12877f0de5a96356f22c28275d7a7546f771e.tar.bz2
yosys-c1e12877f0de5a96356f22c28275d7a7546f771e.zip
Support for data and array queries on struct/union item expressions
For now, $bits, $left, $right, $low, $high, and $size are supported.
-rw-r--r--frontends/ast/simplify.cc61
-rw-r--r--tests/svtypes/struct_sizebits.sv107
2 files changed, 156 insertions, 12 deletions
diff --git a/frontends/ast/simplify.cc b/frontends/ast/simplify.cc
index f77b59d83..7edff38d9 100644
--- a/frontends/ast/simplify.cc
+++ b/frontends/ast/simplify.cc
@@ -2077,6 +2077,14 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
auto range = make_struct_member_range(this, item_node);
newNode = new AstNode(AST_IDENTIFIER, range);
newNode->str = sname;
+ // save type and original number of dimensions for $size() etc.
+ newNode->attributes[ID::wiretype] = item_node->clone();
+ if (!item_node->multirange_dimensions.empty() && children.size() > 0) {
+ if (children[0]->type == AST_RANGE)
+ newNode->integer = 1;
+ else if (children[0]->type == AST_MULTIRANGE)
+ newNode->integer = children[0]->children.size();
+ }
newNode->basic_prep = true;
if (item_node->is_signed)
newNode = new AstNode(AST_TO_SIGNED, newNode);
@@ -3393,24 +3401,42 @@ skip_dynamic_range_lvalue_expansion:;
id_ast = current_scope.at(buf->str);
if (!id_ast)
log_file_error(filename, location.first_line, "Failed to resolve identifier %s for width detection!\n", buf->str.c_str());
- // a slice of our identifier means we advance to the next dimension, e.g. $size(a[3])
- if (buf->children.size() > 0) {
- // something is hanging below this identifier
- if (buf->children[0]->type == AST_RANGE && buf->integer == 0)
- // if integer == 0, this node was originally created as AST_RANGE so it's dimension is 1
- dim++;
- // more than one range, e.g. $size(a[3][2])
- else // created an AST_MULTIRANGE, converted to AST_RANGE, but original dimension saved in 'integer' field
- dim += buf->integer; // increment by multirange size
+
+ // Check for item in packed struct / union
+ AST::AstNode *item_node;
+ if (id_ast->type == AST_WIRE &&
+ buf->attributes.count(ID::wiretype) && (item_node = buf->attributes[ID::wiretype]) &&
+ (item_node->type == AST_STRUCT_ITEM || item_node->type == AST_STRUCT || item_node->type == AST_UNION))
+ {
+ // The dimension of the original array expression is saved in the 'integer' field
+ dim += buf->integer;
+ if (item_node->multirange_dimensions.empty()) {
+ if (dim != 1)
+ log_file_error(filename, location.first_line, "Dimension %d out of range in `%s', as it only has one dimension!\n", dim, item_node->str.c_str());
+ left = high = item_node->range_left;
+ right = low = item_node->range_right;
+ } else {
+ int dims = GetSize(item_node->multirange_dimensions)/2;
+ if (dim < 1 || dim > dims)
+ log_file_error(filename, location.first_line, "Dimension %d out of range in `%s', as it only has dimensions 1..%d!\n", dim, item_node->str.c_str(), dims);
+ right = low = get_struct_range_offset(item_node, dim - 1);
+ left = high = low + get_struct_range_width(item_node, dim - 1) - 1;
+ if (item_node->multirange_swapped[dim - 1]) {
+ std::swap(left, right);
+ }
+ for (int i = dim; i < dims; i++) {
+ mem_depth *= get_struct_range_width(item_node, i);
+ }
+ }
}
- // We have 4 cases:
+ // Otherwise, we have 4 cases:
// wire x; ==> AST_WIRE, no AST_RANGE children
// wire [1:0]x; ==> AST_WIRE, AST_RANGE children
// wire [1:0]x[1:0]; ==> AST_MEMORY, two AST_RANGE children (1st for packed, 2nd for unpacked)
// wire [1:0]x[1:0][1:0]; ==> AST_MEMORY, one AST_RANGE child (0) for packed, then AST_MULTIRANGE child (1) for unpacked
// (updated: actually by the time we are here, AST_MULTIRANGE is converted into one big AST_RANGE)
// case 0 handled by default
- if ((id_ast->type == AST_WIRE || id_ast->type == AST_MEMORY) && id_ast->children.size() > 0) {
+ else if ((id_ast->type == AST_WIRE || id_ast->type == AST_MEMORY) && id_ast->children.size() > 0) {
// handle packed array left/right for case 1, and cases 2/3 when requesting the last dimension (packed side)
AstNode *wire_range = id_ast->children[0];
left = wire_range->children[0]->integer;
@@ -3419,6 +3445,17 @@ skip_dynamic_range_lvalue_expansion:;
low = min(left, right);
}
if (id_ast->type == AST_MEMORY) {
+ // a slice of our identifier means we advance to the next dimension, e.g. $size(a[3])
+ if (buf->children.size() > 0) {
+ // something is hanging below this identifier
+ if (buf->children[0]->type == AST_RANGE && buf->integer == 0)
+ // if integer == 0, this node was originally created as AST_RANGE so it's dimension is 1
+ dim++;
+ // more than one range, e.g. $size(a[3][2])
+ else // created an AST_MULTIRANGE, converted to AST_RANGE, but original dimension saved in 'integer' field
+ dim += buf->integer; // increment by multirange size
+ }
+
// We got here only if the argument is a memory
// Otherwise $size() and $bits() return the expression width
AstNode *mem_range = id_ast->children[1];
@@ -3478,7 +3515,7 @@ skip_dynamic_range_lvalue_expansion:;
result = right;
else if (str == "\\$size")
result = width;
- else {
+ else { // str == "\\$bits"
result = width * mem_depth;
}
newNode = mkconst_int(result, true);
diff --git a/tests/svtypes/struct_sizebits.sv b/tests/svtypes/struct_sizebits.sv
new file mode 100644
index 000000000..ec35eb08f
--- /dev/null
+++ b/tests/svtypes/struct_sizebits.sv
@@ -0,0 +1,107 @@
+// These tests are adapted from tests/sat/sizebits.sv
+
+module top;
+
+typedef struct packed {
+ logic [2:7][3:0] y;
+} sy_t;
+
+struct packed {
+ logic t;
+ logic [5:2] x;
+ sy_t sy;
+ union packed {
+ logic [7:2][2:9][1:4] z;
+ logic [1:6*8*4] z2;
+ } sz;
+} s;
+
+//wire [$size(s.x)-1:0]x_size;
+//wire [$size({s.x, s.x})-1:0]xx_size;
+//wire [$size(s.sy.y)-1:0]y_size;
+//wire [$size(s.sz.z)-1:0]z_size;
+
+assert property ($size(s) == $size(s.t) + $size(s.x) + $size(s.sy) + $size(s.sz));
+assert property ($size(s) == 1 + 4 + 6*4 + 6*8*4);
+
+assert property ($size(t) == 1);
+assert property ($size(s.x) == 4);
+assert property ($size({3{s.x}}) == 3*4);
+assert property ($size(s.sy.y) == 6);
+assert property ($size(s.sy.y, 1) == 6);
+assert property ($size(s.sy.y, (1+1)) == 4);
+assert property ($size(s.sy.y[2], 1) == 4);
+// This is unsupported at the moment
+//assert property ($size(s.sy.y[2][1], 1) == 1);
+
+assert property ($size(s.sz.z) == 6);
+assert property ($size(s.sz.z, 1) == 6);
+assert property ($size(s.sz.z, 2) == 8);
+assert property ($size(s.sz.z, 3) == 4);
+assert property ($size(s.sz.z[3], 1) == 8);
+assert property ($size(s.sz.z[3][3], 1) == 4);
+// This is unsupported at the moment
+//assert property ($size(s.sz.z[3][3][3], 1) == 1);
+// This should trigger an error if enabled (it does).
+//assert property ($size(s.sz.z, 4) == 4);
+
+//wire [$bits(s.x)-1:0]x_bits;
+//wire [$bits({s.x, s.x})-1:0]xx_bits;
+
+assert property ($bits(t) == 1);
+assert property ($bits(s.x) == 4);
+assert property ($bits(s.sy.y) == 4*6);
+assert property ($bits(s.sz.z) == 4*6*8);
+
+assert property ($high(s.x) == 5);
+assert property ($high(s.sy.y) == 7);
+assert property ($high(s.sy.y, 1) == 7);
+assert property ($high(s.sy.y, (1+1)) == 3);
+
+assert property ($high(s.sz.z) == 7);
+assert property ($high(s.sz.z, 1) == 7);
+assert property ($high(s.sz.z, 2) == 9);
+assert property ($high(s.sz.z, 3) == 4);
+assert property ($high(s.sz.z[3]) == 9);
+assert property ($high(s.sz.z[3][3]) == 4);
+assert property ($high(s.sz.z[3], 2) == 4);
+
+assert property ($low(s.x) == 2);
+assert property ($low(s.sy.y) == 2);
+assert property ($low(s.sy.y, 1) == 2);
+assert property ($low(s.sy.y, (1+1)) == 0);
+
+assert property ($low(s.sz.z) == 2);
+assert property ($low(s.sz.z, 1) == 2);
+assert property ($low(s.sz.z, 2) == 2);
+assert property ($low(s.sz.z, 3) == 1);
+assert property ($low(s.sz.z[3]) == 2);
+assert property ($low(s.sz.z[3][3]) == 1);
+assert property ($low(s.sz.z[3], 2) == 1);
+
+assert property ($left(s.x) == 5);
+assert property ($left(s.sy.y) == 2);
+assert property ($left(s.sy.y, 1) == 2);
+assert property ($left(s.sy.y, (1+1)) == 3);
+
+assert property ($left(s.sz.z) == 7);
+assert property ($left(s.sz.z, 1) == 7);
+assert property ($left(s.sz.z, 2) == 2);
+assert property ($left(s.sz.z, 3) == 1);
+assert property ($left(s.sz.z[3]) == 2);
+assert property ($left(s.sz.z[3][3]) == 1);
+assert property ($left(s.sz.z[3], 2) == 1);
+
+assert property ($right(s.x) == 2);
+assert property ($right(s.sy.y) == 7);
+assert property ($right(s.sy.y, 1) == 7);
+assert property ($right(s.sy.y, (1+1)) == 0);
+
+assert property ($right(s.sz.z) == 2);
+assert property ($right(s.sz.z, 1) == 2);
+assert property ($right(s.sz.z, 2) == 9);
+assert property ($right(s.sz.z, 3) == 4);
+assert property ($right(s.sz.z[3]) == 9);
+assert property ($right(s.sz.z[3][3]) == 4);
+assert property ($right(s.sz.z[3], 2) == 4);
+endmodule