Support 2D bit arrays in structures. Optimise array indexing.

This commit is contained in:
Peter Crozier 2020-06-08 20:34:52 +01:00
parent 4ef9ee3c42
commit 01ec681373
2 changed files with 102 additions and 31 deletions

View File

@ -380,31 +380,100 @@ static int size_packed_struct(AstNode *snode, int base_offset)
static AstNode *node_int(int ival) static AstNode *node_int(int ival)
{ {
// maybe mkconst_int should have default values for the common integer case return AstNode::mkconst_int(ival, true);
return AstNode::mkconst_int(ival, true, 32);
} }
static AstNode *offset_indexed_range(int offset_right, int stride, AstNode *left_expr, AstNode *right_expr) static AstNode *node_uint(uint ival)
{
return AstNode::mkconst_int(ival, false);
}
static unsigned int power_of_two(int n)
{
// iff n is a power of two then return the power, else return 0
// caller must ensure n > 1
log_assert(n > 1);
if (n & (n - 1)) {
// not a power of 2
return 0;
}
// brute force the shift
for (unsigned int i = 1; i < 32; i++) {
n >>= 1;
if (n & 1) {
return i;
}
}
return 0;
}
static AstNode *multiply_by_const(AstNode *expr_node, int stride)
{
// the stride is very likely a power of 2, e.g. 8 for bytes
// and so could be optimised with a shift
AstNode *node;
unsigned int shift;
if ((shift = power_of_two(stride)) > 0) {
node = new AstNode(AST_SHIFT_LEFT, expr_node, node_uint(shift));
}
else {
node = new AstNode(AST_MUL, expr_node, node_int(stride));
}
return node;
}
static AstNode *offset_indexed_range(int offset, int stride, AstNode *left_expr, AstNode *right_expr)
{ {
// adjust the range expressions to add an offset into the struct // adjust the range expressions to add an offset into the struct
// and maybe index using an array stride // and maybe index using an array stride
auto left = left_expr->clone(); auto left = left_expr->clone();
auto right = right_expr->clone(); auto right = right_expr->clone();
if (stride == 1) { if (stride > 1) {
// just add the offset // newleft = (left + 1) * stride - 1
left = new AstNode(AST_ADD, node_int(offset_right), left); left = new AstNode(AST_SUB, multiply_by_const(new AstNode(AST_ADD, left, node_int(1)), stride), node_int(1));
right = new AstNode(AST_ADD, node_int(offset_right), right); // newright = right * stride
right = multiply_by_const(right, stride);
} }
else { // add the offset
// newleft = offset_right - 1 + (left + 1) * stride if (offset) {
left = new AstNode(AST_ADD, new AstNode(AST_SUB, node_int(offset_right), node_int(1)), left = new AstNode(AST_ADD, node_int(offset), left);
new AstNode(AST_MUL, node_int(stride), new AstNode(AST_ADD, left, node_int(1)))); right = new AstNode(AST_ADD, node_int(offset), right);
// newright = offset_right + right * stride
right = new AstNode(AST_ADD, node_int(offset_right), new AstNode(AST_MUL, right, node_int(stride)));
} }
return new AstNode(AST_RANGE, left, right); return new AstNode(AST_RANGE, left, right);
} }
static AstNode *make_struct_index_range(AstNode *node, AstNode *rnode, int stride, int offset)
{
// generate a range node to perform either bit or array indexing
if (rnode->children.size() == 1) {
// index e.g. s.a[i]
return offset_indexed_range(offset, stride, rnode->children[0], rnode->children[0]);
}
else if (rnode->children.size() == 2) {
// slice e.g. s.a[i:j]
return offset_indexed_range(offset, stride, rnode->children[0], rnode->children[1]);
}
else {
struct_op_error(node);
}
}
static AstNode *slice_range(AstNode *rnode, AstNode *snode)
{
// apply the bit slice indicated by snode to the range rnode
log_assert(rnode->type==AST_RANGE);
auto left = rnode->children[0];
auto right = rnode->children[1];
log_assert(snode->type==AST_RANGE);
auto slice_left = snode->children[0];
auto slice_right = snode->children[1];
auto width = new AstNode(AST_SUB, slice_left->clone(), slice_right->clone());
right = new AstNode(AST_ADD, right->clone(), slice_right->clone());
left = new AstNode(AST_ADD, right->clone(), width);
return new AstNode(AST_RANGE, left, right);
}
static AstNode *make_struct_member_range(AstNode *node, AstNode *member_node) static AstNode *make_struct_member_range(AstNode *node, AstNode *member_node)
{ {
// Work out the range in the packed array that corresponds to a struct member // Work out the range in the packed array that corresponds to a struct member
@ -414,27 +483,26 @@ static AstNode *make_struct_member_range(AstNode *node, AstNode *member_node)
int range_right = member_node->range_right; int range_right = member_node->range_right;
if (node->children.empty()) { if (node->children.empty()) {
// no range operations apply, return the whole width // no range operations apply, return the whole width
return make_range(range_left, range_right);
} }
else if (node->children.size() == 1 && node->children[0]->type == AST_RANGE) { int stride = get_struct_array_width(member_node);
auto rnode = node->children[0]; if (node->children.size() == 1 && node->children[0]->type == AST_RANGE) {
int stride = get_struct_array_width(member_node); // bit or array indexing e.g. s.a[2] or s.a[1:0]
if (rnode->children.size() == 1) { return make_struct_index_range(node, node->children[0], stride, range_right);
// index e.g. s.a[i] }
return offset_indexed_range(range_right, stride, rnode->children[0], rnode->children[0]); else if (node->children.size() == 1 && node->children[0]->type == AST_MULTIRANGE) {
} // multirange, i.e. bit slice after array index, e.g. s.a[i][p:q]
else if (rnode->children.size() == 2) { log_assert(stride > 1);
// slice e.g. s.a[i:j] auto mrnode = node->children[0];
return offset_indexed_range(range_right, stride, rnode->children[0], rnode->children[1]); auto element_range = make_struct_index_range(node, mrnode->children[0], stride, range_right);
} // then apply bit slice range
else { auto range = slice_range(element_range, mrnode->children[1]);
struct_op_error(node); delete element_range;
} return range;
} }
else { else {
// TODO multirange, i.e. bit slice after array index s.a[i][p:q]
struct_op_error(node); struct_op_error(node);
} }
return make_range(range_left, range_right);
} }
static void add_members_to_scope(AstNode *snode, std::string name) static void add_members_to_scope(AstNode *snode, std::string name)

View File

@ -3,7 +3,7 @@
module top; module top;
struct packed { struct packed {
bit [5:0] [7:0] a; // 6 element packed array of bytes bit [7:0] [7:0] a; // 8 element packed array of bytes
bit [15:0] b; // filler for non-zero offset bit [15:0] b; // filler for non-zero offset
} s; } s;
@ -13,10 +13,13 @@ module top;
s.a[2:1] = 16'h1234; s.a[2:1] = 16'h1234;
s.a[5] = 8'h42; s.a[5] = 8'h42;
s.a[7] = '1;
s.a[7][1:0] = '0;
s.b = '1; s.b = '1;
s.b[1:0] = '0; s.b[1:0] = '0;
end end
always_comb assert(s==64'h4200_0012_3400_FFFC); always_comb assert(s==80'hFC00_4200_0012_3400_FFFC);
endmodule endmodule