P4C
The P4 Compiler
Loading...
Searching...
No Matches
stack_push_shims.h
1
19#ifndef BF_P4C_PARDE_STACK_PUSH_SHIMS_H_
20#define BF_P4C_PARDE_STACK_PUSH_SHIMS_H_
21
22#include "bf-p4c/common/header_stack.h"
23#include "parde_visitor.h"
24
33 const BFN::HeaderStackInfo *stacks = nullptr;
34
35 bool preorder(IR::BFN::Pipe *pipe) override {
36 BUG_CHECK(pipe->headerStackInfo != nullptr,
37 "Running StackPushShims without running "
38 "CollectHeaderStackInfo first?");
39 stacks = pipe->headerStackInfo;
40 return true;
41 }
42
43 bool preorder(IR::BFN::Parser *p) override {
44 BUG_CHECK(stacks != nullptr,
45 "No HeaderStackInfo; was StackPushShims "
46 "applied to a non-Pipe node?");
47 for (auto &stack : *stacks) {
48 if (stack.maxpush == 0 || !stack.inThread[p->gress]) continue;
49
50 // The layout of `$stkvalid` is an overlay over a header stack's
51 // `$push` field, its entry POV bits, and its `$pop` field:
52 //
53 // [ 1 1 .. $push .. 1 1 ] [ .. entry POVs .. ] [ 0 0 .. $pop .. 0 0 ]
54 //
55 // We need to initialize all of the `$push` bits to 1, but we need to
56 // do it using a write to `$stkvalid`, because in the MAU we'll
57 // access these bits only through `$stkvalid` and if we write to
58 // `$push` we'll end up thinking that write is dead and eliminate
59 // it.
60 const unsigned pushValue = (1U << stack.maxpush) - 1;
61 const unsigned stkValidSize = stack.size + stack.maxpush + stack.maxpop;
62 const unsigned stkValidValue = pushValue << (stack.size + stack.maxpop);
63
64 IR::Vector<IR::BFN::ParserPrimitive> initStkValid = {new IR::BFN::Extract(
65 new IR::Member(IR::Type::Bits::get(stkValidSize),
66 new IR::PathExpression(stack.name), "$stkvalid"),
67 new IR::BFN::ConstantRVal(stkValidValue))};
68 p->start = new IR::BFN::ParserState(stack.name + "$shim", p->gress, initStkValid, {},
69 {new IR::BFN::Transition(match_t(), 0, p->start)});
70 }
71 return false;
72 }
73};
74
75#endif /* BF_P4C_PARDE_STACK_PUSH_SHIMS_H_ */
Definition vector.h:59
Definition parde_visitor.h:37
Adds parser states to initialize the $stkvalid fields that are used to handle the push_front and pop_...
Definition stack_push_shims.h:32
Metadata about how header stacks are used in the program.
Definition header_stack.h:94