@@ -20,52 +20,47 @@ limitations under the License.
20
20
21
21
namespace P4 {
22
22
23
- void DoExpandLookahead::expandSetValid (const IR::Expression* base, const IR::Type* type,
24
- IR::IndexedVector<IR::StatOrDecl>* output) {
25
- if (type->is <IR::Type_Struct>()) {
26
- auto st = type->to <IR::Type_Struct>();
27
- for (auto f : st->fields ) {
28
- auto t = typeMap->getTypeType (f->type , true );
29
- if (t == nullptr )
30
- return ;
31
- auto mem = new IR::Member (base, f->name );
32
- expandSetValid (mem, t, output);
33
- }
34
- } else if (type->is <IR::Type_Header>()) {
35
- auto setValid = new IR::Member (base, IR::Type_Header::setValid);
36
- auto mc = new IR::MethodCallExpression (setValid);
37
- output->push_back (new IR::MethodCallStatement (mc));
38
- }
39
- }
40
-
41
- const IR::Expression* DoExpandLookahead::expand (
42
- const IR::PathExpression* base, const IR::Type* type, unsigned * offset) {
23
+ void DoExpandLookahead::expand (
24
+ const IR::PathExpression* bitvector, // source value containing all bits
25
+ const IR::Type* type, // type that is being extracted from source
26
+ unsigned * offset, // current bit offset in source
27
+ const IR::Expression* destination, // result is assigned to this expression
28
+ IR::IndexedVector<IR::StatOrDecl>* output) { // add here new assignments
43
29
if (type->is <IR::Type_Struct>() || type->is <IR::Type_Header>()) {
44
- auto vec = new IR::IndexedVector<IR::NamedExpression>();
30
+ if (type->is <IR::Type_Header>()) {
31
+ auto setValid = new IR::Member (destination, IR::Type_Header::setValid);
32
+ auto mc = new IR::MethodCallExpression (setValid);
33
+ output->push_back (new IR::MethodCallStatement (mc));
34
+ }
45
35
auto st = type->to <IR::Type_StructLike>();
46
36
for (auto f : st->fields ) {
47
37
auto t = typeMap->getTypeType (f->type , true );
48
38
if (t == nullptr )
49
39
continue ;
50
- auto e = expand (base, t, offset );
51
- vec-> push_back ( new IR::NamedExpression (f-> srcInfo , f-> name , e) );
40
+ auto member = new IR::Member (destination, f-> name );
41
+ expand (bitvector, t, offset, member, output );
52
42
}
53
- auto type = st->getP4Type ()->to <IR::Type_Name>();
54
- return new IR::StructExpression (
55
- base->srcInfo , type, type, *vec);
56
43
} else if (type->is <IR::Type_Bits>() || type->is <IR::Type_Boolean>()) {
57
44
unsigned size = type->width_bits ();
58
45
BUG_CHECK (size > 0 , " %1%: unexpected size %2%" , type, size);
59
46
const IR::Expression* expression =
60
- new IR::Slice (base ->clone (), *offset - 1 , *offset - size);
47
+ new IR::Slice (bitvector ->clone (), *offset - 1 , *offset - size);
61
48
auto tb = type->to <IR::Type_Bits>();
62
49
if (!tb || tb->isSigned )
63
50
expression = new IR::Cast (type, expression);
64
51
*offset -= size;
65
- return expression;
52
+ auto assignment = new IR::AssignmentStatement (
53
+ bitvector->srcInfo , destination, expression);
54
+ output->push_back (assignment);
55
+ } else if (auto ts = type->to <IR::Type_Stack>()) {
56
+ unsigned elements = ts->getSize ();
57
+ auto etype = ts->elementType ;
58
+ for (unsigned i = 0 ; i < elements; i++) {
59
+ auto member = new IR::ArrayIndex (destination, new IR::Constant (i));
60
+ expand (bitvector, etype, offset, member, output);
61
+ }
66
62
} else {
67
- ::error (" %1%: unexpected type in lookahead argument" , type);
68
- return nullptr ;
63
+ ::error (ErrorType::ERR_UNEXPECTED, " %1%: unexpected type in lookahead argument" , type);
69
64
}
70
65
}
71
66
@@ -129,12 +124,8 @@ const IR::Node* DoExpandLookahead::postorder(IR::AssignmentStatement* statement)
129
124
auto result = new IR::BlockStatement;
130
125
result->push_back (ei->statement );
131
126
132
- expandSetValid (statement->left ->clone (), ei->origType , &result->components );
133
- auto init = expand (ei->tmp ->clone (), ei->origType , &ei->width );
134
- if (init == nullptr )
135
- return statement;
136
- auto assignment = new IR::AssignmentStatement (statement->srcInfo , statement->left , init);
137
- result->push_back (assignment);
127
+ expand (ei->tmp ->clone (), ei->origType , &ei->width ,
128
+ statement->left ->clone (), &result->components );
138
129
return result;
139
130
}
140
131
0 commit comments