Skip to content

Commit

Permalink
Rename transitions to old start state
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai Budiu committed Apr 16, 2020
1 parent b510786 commit e952c14
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
41 changes: 33 additions & 8 deletions frontends/p4/moveDeclarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,29 @@ const IR::Node* MoveDeclarations::postorder(IR::Declaration_Constant* decl) {

////////////////////////////////////////////////////////////////////

const IR::Node* MoveInitializers::preorder(IR::P4Parser* parser) {
// Determine if we have anything to do in this parser.
// Check if we have top-level declarations with intializers.
auto someInitializers = false;
for (auto d : parser->parserLocals) {
if (auto dv = d->to<IR::Declaration_Variable>()) {
if (dv->initializer != nullptr) {
someInitializers = true;
break;
}}}
if (someInitializers) {
newStartName = refMap->newName(IR::ParserState::start);
oldStart = parser->states.getDeclaration(IR::ParserState::start)->to<IR::ParserState>();
CHECK_NULL(oldStart);
}
return parser;
}

const IR::Node* MoveInitializers::postorder(IR::P4Parser* parser) {
if (toMove->empty())
if (oldStart == nullptr)
return parser;
CHECK_NULL(oldStart);
auto newStart = new IR::ParserState(IR::ID(IR::ParserState::start), *toMove,
new IR::PathExpression(oldStart->name));
new IR::PathExpression(newStartName));
toMove = new IR::IndexedVector<IR::StatOrDecl>();
parser->states.insert(parser->states.begin(), newStart);
return parser;
Expand All @@ -129,12 +146,10 @@ const IR::Node* MoveInitializers::postorder(IR::Declaration_Variable* decl) {
}

const IR::Node* MoveInitializers::postorder(IR::ParserState* state) {
if (toMove->empty())
return state;
if (state->name != IR::ParserState::start)
if (oldStart == nullptr)
return state;
oldStart = state;
state->name = nameGen->newName("start");
if (state->name == IR::ParserState::start)
state->name = newStartName;
return state;
}

Expand All @@ -148,4 +163,14 @@ const IR::Node* MoveInitializers::postorder(IR::P4Control* control) {
return control;
}

const IR::Node* MoveInitializers::postorder(IR::Path* path) {
if (oldStart == nullptr || path->name != IR::ParserState::start)
return path;
auto decl = refMap->getDeclaration(getOriginal()->to<IR::Path>());
if (!decl->is<IR::ParserState>())
return path;
return new IR::Path(newStartName);
}


} // namespace P4
15 changes: 9 additions & 6 deletions frontends/p4/moveDeclarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
#define _FRONTENDS_P4_MOVEDECLARATIONS_H_

#include "ir/ir.h"
#include "frontends/p4/typeMap.h"
#include "frontends/common/resolveReferences/referenceMap.h"

namespace P4 {

Expand Down Expand Up @@ -67,23 +67,26 @@ class MoveDeclarations : public Transform {

/** After MoveDeclarations, some variable declarations in the "local"
* section of a parser and control may still have initializers; these are moved
* into the a new start state, and to the beginning of the apply body repectively.
* into a new start state, and to the beginning of the apply body repectively.
*
* @pre Must be run after MoveDeclarations.
*/
class MoveInitializers : public Transform {
NameGenerator* nameGen;
ReferenceMap* refMap;
IR::IndexedVector<IR::StatOrDecl> *toMove; // This contains just IR::AssignmentStatement
IR::ParserState* oldStart;
const IR::ParserState* oldStart; // nullptr if we do not want to rename the start state
cstring newStartName; // name allocated to the old start state

public:
explicit MoveInitializers(NameGenerator* nameGen): nameGen(nameGen), oldStart(nullptr) {
setName("MoveInitializers"); CHECK_NULL(nameGen);
MoveInitializers(ReferenceMap* refMap): refMap(refMap), oldStart(nullptr), newStartName("") {
setName("MoveInitializers"); CHECK_NULL(refMap);
toMove = new IR::IndexedVector<IR::StatOrDecl>(); }
const IR::Node* preorder(IR::P4Parser* parser) override;
const IR::Node* postorder(IR::P4Parser* parser) override;
const IR::Node* postorder(IR::Declaration_Variable* decl) override;
const IR::Node* postorder(IR::ParserState* state) override;
const IR::Node* postorder(IR::P4Control* control) override;
const IR::Node* postorder(IR::Path* path) override;
};

} // namespace P4
Expand Down
6 changes: 5 additions & 1 deletion testdata/p4_16_samples_outputs/issue2314-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ struct m {
parser MyParser(packet_in b, out h hdr, inout m meta, inout standard_metadata_t std) {
bit<16> l3_etherType;
state start {
transition start_0;
}
state start_0 {
b.extract<ethernet_t>(hdr.ether);
transition L3_start;
}
Expand All @@ -48,7 +51,8 @@ parser MyParser(packet_in b, out h hdr, inout m meta, inout standard_metadata_t
}
state L3_i {
b.extract<I>(hdr.i);
transition L3_start;
l3_etherType = hdr.i.etherType;
transition L3_start_0;
}
state start_1 {
transition accept;
Expand Down
11 changes: 7 additions & 4 deletions testdata/p4_16_samples_outputs/issue2314-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ struct m {
}

parser MyParser(packet_in b, out h hdr, inout m meta, inout standard_metadata_t std) {
bit<16> l3_etherType;
state start {
b.extract<ethernet_t>(hdr.ether);
transition L3_start;
l3_etherType = hdr.ether.etherType;
transition L3_start_0;
}
state L3_start {
transition select(hdr.ether.etherType) {
state L3_start_0 {
transition select(l3_etherType) {
16w0x800: L3_h0;
16w0x8100: L3_i;
default: start_1;
Expand All @@ -43,7 +45,8 @@ parser MyParser(packet_in b, out h hdr, inout m meta, inout standard_metadata_t
}
state L3_i {
b.extract<I>(hdr.i);
transition L3_start;
l3_etherType = hdr.i.etherType;
transition L3_start_0;
}
state start_1 {
transition accept;
Expand Down

0 comments on commit e952c14

Please sign in to comment.