Skip to content

Commit 3dae94e

Browse files
committed
[P4Smith] Initialize arrays using tuple expressions
Signed-off-by: Enrico Green <Enrico.Green@amd.com>
1 parent ce25049 commit 3dae94e

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

backends/p4tools/modules/smith/common/declarations.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,20 @@ IR::Declaration_Variable *DeclarationGenerator::genVariableDeclaration() {
673673
auto *expr = target().expressionGenerator().genExpression(tp);
674674
ret = new IR::Declaration_Variable(name, tp, expr);
675675
} else if (tp->is<IR::Type_Array>()) {
676-
// header stacks do !have an initializer yet
677-
ret = new IR::Declaration_Variable(name, tp);
676+
IR::Vector<IR::Expression> elems;
677+
const IR::Type_Array *arrayTp = dynamic_cast<const IR::Type_Array *>(tp);
678+
for (unsigned i = 0; i < arrayTp->getSize(); i++) {
679+
const IR::Expression *elem =
680+
target().expressionGenerator().genExpression(arrayTp->elementType);
681+
// To be safe, cast tuple expressions to the element type
682+
if (elem->is<IR::ListExpression>()) {
683+
elems.push_back(new IR::Cast(arrayTp->elementType, elem));
684+
} else {
685+
elems.push_back(elem);
686+
}
687+
}
688+
IR::ListExpression *init = new IR::ListExpression(elems);
689+
ret = new IR::Declaration_Variable(name, tp, init);
678690
} else {
679691
BUG("Type %s not supported!", tp->node_type_name());
680692
}

0 commit comments

Comments
 (0)