|
| 1 | +#include "eliminateVerify.h" |
| 2 | +#include "frontends/p4/methodInstance.h" |
| 3 | + |
| 4 | +namespace BMV2 { |
| 5 | + |
| 6 | +// Convert verify(b, e) |
| 7 | +// into |
| 8 | +// transition select(b) { |
| 9 | +// false : reject; |
| 10 | +// true : continuation; |
| 11 | +// } |
| 12 | +// The error is just ignored - BMv2 does not expose it |
| 13 | +const IR::Node* EliminateVerify::postorder(IR::ParserState* state) { |
| 14 | + // This code is very similar to RemoveParserControlFlow |
| 15 | + LOG1("Visiting " << dbp(state)); |
| 16 | + // Set of newly created states |
| 17 | + auto states = new IR::IndexedVector<IR::ParserState>(); |
| 18 | + IR::ParserState* currentState = state; |
| 19 | + // components of the currentState |
| 20 | + auto currentComponents = new IR::IndexedVector<IR::StatOrDecl>(); |
| 21 | + auto origComponents = state->components; |
| 22 | + auto origSelect = state->selectExpression; |
| 23 | + |
| 24 | + cstring joinName; // non-empty if we split the state |
| 25 | + for (auto c : *origComponents) { |
| 26 | + if (c->is<IR::MethodCallStatement>()) { |
| 27 | + auto mcs = c->to<IR::MethodCallStatement>(); |
| 28 | + auto mi = P4::MethodInstance::resolve(mcs, refMap, typeMap); |
| 29 | + auto ef = mi->to<P4::ExternFunction>(); |
| 30 | + if (ef != nullptr && |
| 31 | + ef->method->name.name == IR::ParserState::verify) { |
| 32 | + LOG1("Converting " << dbp(c) << " into states"); |
| 33 | + |
| 34 | + states->push_back(currentState); |
| 35 | + BUG_CHECK(mcs->methodCall->arguments->size() == 2, "%1%: Expected 2 arguments", mcs); |
| 36 | + auto cond = mcs->methodCall->arguments->at(0); |
| 37 | + cstring joinName = refMap->newName(state->name.name + "_join"); |
| 38 | + |
| 39 | + auto vec = new IR::Vector<IR::Expression>(); |
| 40 | + vec->push_back(cond); |
| 41 | + auto trueCase = new IR::SelectCase( |
| 42 | + Util::SourceInfo(), new IR::BoolLiteral(true), |
| 43 | + new IR::PathExpression(IR::ID(joinName))); |
| 44 | + auto falseCase = new IR::SelectCase( |
| 45 | + Util::SourceInfo(), new IR::BoolLiteral(false), |
| 46 | + new IR::PathExpression(IR::ID(IR::ParserState::reject))); |
| 47 | + auto cases = new IR::Vector<IR::SelectCase>(); |
| 48 | + cases->push_back(trueCase); |
| 49 | + cases->push_back(falseCase); |
| 50 | + currentState->selectExpression = new IR::SelectExpression( |
| 51 | + Util::SourceInfo(), new IR::ListExpression(vec), std::move(*cases)); |
| 52 | + |
| 53 | + currentState->components = currentComponents; |
| 54 | + currentComponents = new IR::IndexedVector<IR::StatOrDecl>(); |
| 55 | + currentState = new IR::ParserState( |
| 56 | + Util::SourceInfo(), joinName, IR::Annotations::empty, |
| 57 | + currentComponents, origSelect); // may be overriten |
| 58 | + } |
| 59 | + } else { |
| 60 | + currentComponents->push_back(c); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (states->empty()) |
| 65 | + return state; |
| 66 | + states->push_back(currentState); |
| 67 | + return states; |
| 68 | +} |
| 69 | + |
| 70 | +} // namespace BMV2 |
0 commit comments