46 int num_mark_to_drop = 0;
48 for (
auto abc : action->body->components) {
51 if (abc->is<IR::AssignmentStatement>()) {
52 auto as = abc->to<IR::AssignmentStatement>();
54 if (p->name == as->left->toString())
56 else if (p->name == as->right->toString())
58 }
else if (abc->is<IR::MethodCallStatement>()) {
59 auto mcs = abc->to<IR::MethodCallStatement>();
60 auto mc = mcs->methodCall;
61 auto mce = mc->to<IR::MethodCallExpression>();
62 if (
"mark_to_drop" == mce->method->toString()) {
65 for (
auto arg : *(mce->arguments)) {
66 auto *argMem = arg->expression->to<IR::Member>();
67 if (argMem && p->name == argMem->toString()) paramUses++;
72 LOG3(
"not handling statement: " << abc);
75 if (num_mark_to_drop > 0 && paramUses == 0) {
81 const IR::Node *postorder(IR::P4Action *action) {
82 LOG1(
"Visiting " << dbp(action));
83 BUG_CHECK(getParent<IR::P4Control>() || getParent<IR::P4Program>(),
84 "%1%: unexpected parent %2%", getOriginal(), getContext()->node);
89 auto invocation = getInvocations()->get(getOriginal<IR::P4Action>());
90 if (invocation ==
nullptr)
return action;
91 auto args = invocation->arguments;
94 substitution.
populate(action->parameters, args);
96 bool removeAll = getInvocations()->removeAllParameters(getOriginal<IR::P4Action>());
97 int numParamsNotReplaced = 0;
98 for (
auto p : action->parameters->parameters) {
100 if (p->direction == IR::Direction::None && !removeAll) {
101 leftParams->push_back(p);
104 auto decl =
new IR::Declaration_Variable(p->srcInfo, p->name, p->annotations,
106 LOG3(
"Added declaration " << decl <<
" annotations " << p->annotations);
107 result->push_back(decl);
109 auto arg = substitution.lookup(p);
110 if (arg ==
nullptr) {
111 error(
"action %1%: parameter %2% must be bound", invocation, p);
115 if (p->direction == IR::Direction::In || p->direction == IR::Direction::InOut ||
116 p->direction == IR::Direction::None) {
117 auto left =
new IR::PathExpression(p->name);
119 new IR::AssignmentStatement(arg->srcInfo, left, arg->expression);
120 initializers->push_back(assign);
123 if (p->direction == IR::Direction::Out ||
124 p->direction == IR::Direction::InOut) {
125 auto right =
new IR::PathExpression(p->name);
127 new IR::AssignmentStatement(arg->srcInfo, arg->expression, right);
128 postamble->push_back(assign);
131 numParamsNotReplaced++;
136 if (result->empty()) {
137 if (numParamsNotReplaced > 0) {
138 for (
auto abc : action->body->components) {
139 if (abc->is<IR::MethodCallStatement>()) {
140 auto mcs = abc->to<IR::MethodCallStatement>();
141 auto mc = mcs->methodCall;
142 auto mce = mc->to<IR::MethodCallExpression>();
144 if (
"mark_to_drop" == mce->method->toString()) {
145 auto new_mce =
new IR::MethodCallExpression(mce->method, args);
146 abc =
new IR::MethodCallStatement(new_mce);
149 initializers->push_back(abc);
155 LOG1(
"To replace " << dbp(action));
156 initializers->append(action->body->components);
157 initializers->append(*postamble);
160 action->parameters =
new IR::ParameterList(action->parameters->srcInfo, *leftParams);
161 action->body =
new IR::BlockStatement(action->body->srcInfo, *initializers);
162 result->push_back(action);
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51