39 static std::map<cstring, funcType> *cvtForType;
42 bool replaceNextWithLast;
44 : structure(structure), p4lib(P4::P4CoreLibrary::instance()), replaceNextWithLast(
false) {
45 setName(
"ExpressionConverter");
47 const IR::Type *getFieldType(
const IR::Type_StructLike *ht,
cstring fieldName);
48 const IR::Node *postorder(IR::Constant *expression)
override;
49 const IR::Node *postorder(IR::Member *field)
override;
50 const IR::Node *postorder(IR::FieldList *fl)
override;
51 const IR::Node *postorder(IR::Mask *expression)
override;
52 const IR::Node *postorder(IR::ActionArg *arg)
override;
53 const IR::Node *postorder(IR::Primitive *primitive)
override;
54 const IR::Node *postorder(IR::PathExpression *ref)
override;
55 const IR::Node *postorder(IR::ConcreteHeaderRef *nhr)
override;
56 const IR::Node *postorder(IR::HeaderStackItemRef *ref)
override;
57 const IR::Node *postorder(IR::GlobalRef *gr)
override;
58 const IR::Node *postorder(IR::Equ *equ)
override;
59 const IR::Node *postorder(IR::Neq *neq)
override;
60 const IR::Expression *convert(
const IR::Node *node) {
61 auto result = node->apply(*
this);
62 return result->
to<IR::Expression>();
64 static void addConverter(
cstring type, funcType);
65 static funcType get(
cstring type);
98 static std::map<cstring, ExternConverter *> *cvtForType;
101 virtual const IR::Type_Extern *convertExternType(
ProgramStructure *,
const IR::Type_Extern *,
103 virtual const IR::Declaration_Instance *convertExternInstance(
107 const IR::Declaration_Instance *,
108 const IR::Primitive *);
109 virtual bool convertAsGlobal(
ProgramStructure *,
const IR::Declaration_Instance *) {
117 static ExternConverter *get(
const IR::Type_Extern *type) {
return get(type->name); }
119 return get(ext->type->to<IR::Type_Extern>());
121 static const IR::Type_Extern *cvtExternType(
ProgramStructure *s,
const IR::Type_Extern *e,
123 return get(e)->convertExternType(s, e, name);
125 static const IR::Declaration_Instance *cvtExternInstance(
128 return get(di)->convertExternInstance(s, di, name, scope);
131 const IR::Declaration_Instance *di,
132 const IR::Primitive *p) {
133 return get(di)->convertExternCall(s, di, p);
135 static bool cvtAsGlobal(
ProgramStructure *s,
const IR::Declaration_Instance *di) {
136 return get(di)->convertAsGlobal(s, di);
181 std::map<cstring, cstring> reserved_names = {{
"standard_metadata_t"_cs,
"type"_cs},
182 {
"standard_metadata"_cs,
"metadata"_cs},
183 {
"egress"_cs,
"control"_cs}};
186 auto it = reserved_names.find(nodeName);
187 if (it == reserved_names.end())
return;
188 if (it->second != kind)
189 ::P4::error(ErrorType::ERR_INVALID,
"%1%: invalid name; it can only be used for %2%",
193 checkReserved(node, nodeName,
nullptr);
198 CHECK_NULL(structure);
199 setName(
"DiscoverStructure");
202 void postorder(
const IR::ParserException *ex)
override {
203 warn(ErrorType::WARN_UNSUPPORTED,
"%1%: parser exception is not translated to P4-16", ex);
205 void postorder(
const IR::Metadata *md)
override {
206 structure->metadata.emplace(md);
207 checkReserved(md, md->name,
"metadata"_cs);
209 void postorder(
const IR::Header *hd)
override {
210 structure->headers.emplace(hd);
211 checkReserved(hd, hd->name);
213 void postorder(
const IR::Type_StructLike *t)
override {
214 structure->types.emplace(t);
215 checkReserved(t, t->name,
"type"_cs);
217 void postorder(
const IR::V1Control *control)
override {
218 structure->controls.emplace(control);
219 checkReserved(control, control->name,
"control"_cs);
221 void postorder(
const IR::V1Parser *parser)
override {
222 structure->parserStates.emplace(parser);
223 checkReserved(parser, parser->name);
225 void postorder(
const IR::V1Table *table)
override {
226 structure->tables.emplace(table);
227 checkReserved(table, table->name);
229 void postorder(
const IR::ActionFunction *action)
override {
230 structure->actions.emplace(action);
231 checkReserved(action, action->name);
233 void postorder(
const IR::HeaderStack *stack)
override {
234 structure->stacks.emplace(stack);
235 checkReserved(stack, stack->name);
237 void postorder(
const IR::Counter *count)
override {
238 structure->counters.emplace(count);
239 checkReserved(count, count->name);
241 void postorder(
const IR::Register *reg)
override {
242 structure->registers.emplace(reg);
243 checkReserved(reg, reg->name);
245 void postorder(
const IR::ActionProfile *ap)
override {
246 structure->action_profiles.emplace(ap);
247 checkReserved(ap, ap->name);
249 void postorder(
const IR::FieldList *fl)
override {
250 structure->field_lists.emplace(fl);
251 checkReserved(fl, fl->name);
253 void postorder(
const IR::FieldListCalculation *flc)
override {
254 structure->field_list_calculations.emplace(flc);
255 checkReserved(flc, flc->name);
257 void postorder(
const IR::CalculatedField *cf)
override {
258 structure->calculated_fields.push_back(cf);
260 void postorder(
const IR::Meter *m)
override {
261 structure->meters.emplace(m);
262 checkReserved(m, m->name);
264 void postorder(
const IR::ActionSelector *as)
override {
265 structure->action_selectors.emplace(as);
266 checkReserved(as, as->name);
268 void postorder(
const IR::Type_Extern *ext)
override {
269 structure->extern_types.emplace(ext);
270 checkReserved(ext, ext->name);
272 void postorder(
const IR::Declaration_Instance *ext)
override {
273 structure->externs.emplace(ext);
274 checkReserved(ext, ext->name);
276 void postorder(
const IR::ParserValueSet *pvs)
override {
277 structure->value_sets.emplace(pvs);
278 checkReserved(pvs, pvs->name);
287 CHECK_NULL(structure);
288 setName(
"ComputeCallGraph");
291 void postorder(
const IR::V1Parser *parser)
override {
292 LOG3(
"Scanning parser " << parser->name);
293 structure->parsers.add(parser->name);
294 if (!parser->default_return.name.isNullOrEmpty())
295 structure->parsers.calls(parser->name, parser->default_return);
296 if (parser->cases !=
nullptr)
297 for (
auto ce : *parser->cases) structure->parsers.calls(parser->name, ce->action.name);
298 for (
auto expr : parser->stmts) {
299 if (expr->is<IR::Primitive>()) {
300 auto primitive = expr->to<IR::Primitive>();
301 if (primitive->name ==
"extract") {
302 BUG_CHECK(primitive->operands.size() == 1,
"Expected 1 operand for %1%",
304 auto dest = primitive->operands.at(0);
305 LOG3(
"Parser " << parser->name <<
" extracts into " << dest);
306 structure->extracts[parser->name].push_back(dest);
311 void postorder(
const IR::Primitive *primitive)
override {
312 auto name = primitive->name;
313 const IR::GlobalRef *glob =
nullptr;
314 const IR::Declaration_Instance *extrn =
nullptr;
315 if (!primitive->operands.empty()) glob = primitive->operands[0]->to<IR::GlobalRef>();
316 if (glob) extrn = glob->obj->to<IR::Declaration_Instance>();
319 auto parent = findContext<IR::ActionFunction>();
320 BUG_CHECK(parent !=
nullptr,
"%1%: Extern call not within action", primitive);
321 structure->calledExterns.calls(parent->name, extrn->name.name);
323 }
else if (primitive->name ==
"count") {
325 auto ctrref = primitive->operands.at(0);
326 const IR::Counter *ctr =
nullptr;
327 if (
auto gr = ctrref->to<IR::GlobalRef>())
328 ctr = gr->obj->to<IR::Counter>();
329 else if (
auto nr = ctrref->to<IR::PathExpression>())
330 ctr = structure->counters.get(nr->path->name);
331 if (ctr ==
nullptr) {
332 ::P4::error(ErrorType::ERR_NOT_FOUND,
"%1%: Cannot find counter", ctrref);
335 auto parent = findContext<IR::ActionFunction>();
336 BUG_CHECK(parent !=
nullptr,
"%1%: Counter call not within action", primitive);
337 structure->calledCounters.calls(parent->name, ctr->name.name);
339 }
else if (primitive->name ==
"execute_meter") {
340 auto mtrref = primitive->operands.at(0);
341 const IR::Meter *mtr =
nullptr;
342 if (
auto gr = mtrref->to<IR::GlobalRef>())
343 mtr = gr->obj->to<IR::Meter>();
344 else if (
auto nr = mtrref->to<IR::PathExpression>())
345 mtr = structure->meters.get(nr->path->name);
346 if (mtr ==
nullptr) {
347 ::P4::error(ErrorType::ERR_NOT_FOUND,
"%1%: Cannot find meter", mtrref);
350 auto parent = findContext<IR::ActionFunction>();
351 BUG_CHECK(parent !=
nullptr,
"%1%: not within action", primitive);
352 structure->calledMeters.calls(parent->name, mtr->name.name);
354 }
else if (primitive->name ==
"register_read" || primitive->name ==
"register_write") {
355 const IR::Expression *regref;
356 if (primitive->name ==
"register_read")
357 regref = primitive->operands.at(1);
359 regref = primitive->operands.at(0);
360 const IR::Register *reg =
nullptr;
361 if (
auto gr = regref->to<IR::GlobalRef>())
362 reg = gr->obj->to<IR::Register>();
363 else if (
auto nr = regref->to<IR::PathExpression>())
364 reg = structure->registers.get(nr->path->name);
365 if (reg ==
nullptr) {
366 ::P4::error(ErrorType::ERR_NOT_FOUND,
"%1%: Cannot find register", regref);
369 auto parent = findContext<IR::ActionFunction>();
370 BUG_CHECK(parent !=
nullptr,
"%1%: not within action", primitive);
371 structure->calledRegisters.calls(parent->name, reg->name.name);
373 }
else if (structure->actions.contains(name)) {
374 auto parent = findContext<IR::ActionFunction>();
375 BUG_CHECK(parent !=
nullptr,
"%1%: Action call not within action", primitive);
376 structure->calledActions.calls(parent->name, name);
377 }
else if (structure->controls.contains(name)) {
378 auto parent = findContext<IR::V1Control>();
379 BUG_CHECK(parent !=
nullptr,
"%1%: Control call not within control", primitive);
380 structure->calledControls.calls(parent->name, name);
383 void postorder(
const IR::GlobalRef *gref)
override {
385 if (
auto af = findContext<IR::ActionFunction>()) {
387 }
else if (
auto di = findContext<IR::Declaration_Instance>()) {
390 BUG(
"%1%: GlobalRef not within action or extern", gref);
392 if (
auto ctr = gref->obj->to<IR::Counter>())
393 structure->calledCounters.calls(caller, ctr->name.name);
394 else if (
auto mtr = gref->obj->to<IR::Meter>())
395 structure->calledMeters.calls(caller, mtr->name.name);
396 else if (
auto reg = gref->obj->to<IR::Register>())
397 structure->calledRegisters.calls(caller, reg->name.name);
398 else if (
auto ext = gref->obj->to<IR::Declaration_Instance>())
399 structure->calledExterns.calls(caller, ext->name.name);
411 CHECK_NULL(structure);
412 setName(
"ComputeTableCallGraph");
415 void postorder(
const IR::Apply *apply)
override {
416 LOG3(
"Scanning " << apply->name);
417 auto tbl = structure->tables.get(apply->name.name);
418 if (tbl ==
nullptr) {
419 ::P4::error(ErrorType::ERR_NOT_FOUND,
"%1%: Could not find table", apply->name);
422 auto parent = findContext<IR::V1Control>();
424 ::P4::error(ErrorType::ERR_UNEXPECTED,
"%1%: Apply not within a control block?", apply);
428 auto ctrl = get(structure->tableMapping, tbl);
431 if (!structure->calledControls.isCallee(parent->name) &&
432 parent->name != P4V1::V1Model::instance.ingress.name &&
433 parent->name != P4V1::V1Model::instance.egress.name)
436 if (ctrl !=
nullptr && ctrl != parent) {
437 auto previous = get(structure->tableInvocation, tbl);
439 "%1%: Table invoked from two different controls: %2% and %3%", tbl, apply,
442 LOG3(
"Invoking " << tbl <<
" in " << parent->name);
443 structure->tableMapping.emplace(tbl, parent);
444 structure->tableInvocation.emplace(tbl, apply);
512 const IR::Type_Header *fixedHeaderType;
514 const IR::Expression *headerLength;
524 std::map<cstring, HeaderSplit *> fixedPart;
529 HeaderSplit *splitHeaderType(
const IR::Type_Header *type) {
531 auto fixed = ::P4::get(fixedPart, type->name.name);
532 if (fixed !=
nullptr)
return fixed;
534 const IR::Expression *headerLength =
nullptr;
536 const IR::Type_Header *fixedHeaderType =
nullptr;
539 for (
auto f : type->fields) {
540 if (f->type->is<IR::Type_Varbits>()) {
541 cstring hname = structure->makeUniqueName(type->name.name);
542 if (fixedHeaderType !=
nullptr) {
544 "%1%: header types with multiple varbit fields are not supported",
548 fixedHeaderType =
new IR::Type_Header(
IR::ID(hname), fields);
550 auto anno = f->getAnnotation(IR::Annotation::lengthAnnotation);
551 BUG_CHECK(anno !=
nullptr,
"No length annotation on varbit field", f);
552 BUG_CHECK(anno->getExpr().size() == 1,
"Expected exactly 1 argument",
554 headerLength = anno->getExpr(0);
557 }
else if (fixedHeaderType ==
nullptr) {
562 if (fixedHeaderType !=
nullptr) {
563 LOG3(
"Extracted fixed-size header type from " << type <<
" into " << fixedHeaderType);
564 fixed =
new HeaderSplit;
565 fixed->fixedHeaderType = fixedHeaderType;
566 fixed->headerLength = headerLength;
567 fixedPart.emplace(type->name.name, fixed);
568 allTypeDecls.push_back(fixedHeaderType);
581 class RewriteLength final :
public Transform {
582 const IR::Type_Header *header;
583 const IR::Declaration *var;
586 explicit RewriteLength(
const IR::Type_Header *header,
const IR::Declaration *var)
587 : header(header), var(var) {
588 setName(
"RewriteLength");
591 const IR::Node *postorder(IR::PathExpression *expression)
override {
592 if (expression->path->absolute)
return expression;
593 for (
auto f : header->fields) {
594 if (f->name == expression->path->name)
595 return new IR::Member(expression->srcInfo,
new IR::PathExpression(var->name),
604 CHECK_NULL(structure);
605 setName(
"FixExtracts");
608 const IR::Node *postorder(IR::P4Program *program)
override {
611 allTypeDecls.append(program->objects);
612 program->objects = allTypeDecls;
616 const IR::Node *postorder(IR::P4Parser *parser)
override {
617 if (!varDecls.empty()) {
618 parser->parserLocals.append(varDecls);
624 const IR::Node *postorder(IR::MethodCallStatement *statement)
override {
625 auto mce = getOriginal<IR::MethodCallStatement>()->methodCall;
626 LOG3(
"Looking up in extracts " << dbp(mce));
627 auto ht = ::P4::get(structure->extractsSynthesized, mce);
633 BUG_CHECK(mce->arguments->size() == 1,
"%1%: expected 1 argument", mce);
634 auto arg = mce->arguments->at(0);
636 auto fixed = splitHeaderType(ht);
637 if (fixed ==
nullptr)
return statement;
638 CHECK_NULL(fixed->headerLength);
639 CHECK_NULL(fixed->fixedHeaderType);
642 cstring varName = structure->makeUniqueName(
"tmp_hdr"_cs);
644 new IR::Declaration_Variable(
IR::ID(varName), fixed->fixedHeaderType->to<IR::Type>());
645 varDecls.push_back(var);
648 auto member = mce->method->to<IR::Member>();
651 typeArgs->push_back(fixed->fixedHeaderType->getP4Type());
652 auto lookaheadMethod =
653 new IR::Member(member->expr, P4::P4CoreLibrary::instance().packetIn.lookahead.name);
654 auto lookahead =
new IR::MethodCallExpression(mce->srcInfo, lookaheadMethod, typeArgs,
657 new IR::AssignmentStatement(mce->srcInfo,
new IR::PathExpression(varName), lookahead);
658 result->push_back(assign);
659 LOG3(
"Created lookahead " << assign);
662 RewriteLength rewrite(fixed->fixedHeaderType, var);
663 rewrite.setCalledBy(
this);
664 auto length = fixed->headerLength->apply(rewrite);
666 args->push_back(arg->clone());
667 auto type = IR::Type_Bits::get(P4::P4CoreLibrary::instance().packetIn.extractSecondArgSize);
669 args->push_back(
new IR::Argument(cast));
670 auto expression =
new IR::MethodCallExpression(mce->srcInfo, mce->method->clone(), args);
671 result->push_back(
new IR::MethodCallStatement(expression));
799 setName(
"InsertCompilerGeneratedStartState");
800 structure->allNames.insert({IR::ParserState::start, 0});
801 structure->allNames.insert({
"InstanceType"_cs, 0});
802 newStartState = structure->makeUniqueName(IR::ParserState::start);
803 newInstanceType = structure->makeUniqueName(
"InstanceType"_cs);
806 const IR::Node *postorder(IR::P4Program *program)
override {
807 allTypeDecls.append(program->objects);
808 program->objects = allTypeDecls;
813 const IR::Node *postorder(IR::ParserState *state)
override {
814 if (structure->parserEntryPoints.empty())
return state;
815 if (state->name == IR::ParserState::start) {
816 state->name = newStartState;
822 const IR::Node *postorder(IR::Path *path)
override {
823 if (structure->parserEntryPoints.empty())
return path;
827 if (path->name.name != IR::ParserState::start)
return path;
829 auto pe = getContext()->node->
to<IR::PathExpression>();
830 auto sc = findContext<IR::SelectCase>();
831 auto ps = findContext<IR::ParserState>();
833 if (pe && ((sc && pe->equiv(*sc->state->to<IR::PathExpression>())) ||
835 (ps && pe->equiv(*ps->selectExpression->to<IR::PathExpression>()))))
836 path->name = newStartState;
840 const IR::Node *postorder(IR::P4Parser *parser)
override {
841 if (structure->parserEntryPoints.empty())
return parser;
844 members.push_back(
new IR::SerEnumMember(
"START",
new IR::Constant(0)));
845 selCases.push_back(
new IR::SelectCase(
846 new IR::Member(
new IR::TypeNameExpression(
new IR::Type_Name(newInstanceType)),
848 new IR::PathExpression(
new IR::Path(newStartState))));
852 for (
auto p : structure->parserEntryPoints) {
853 members.push_back(
new IR::SerEnumMember(p.first,
new IR::Constant(idx++)));
854 selCases.push_back(
new IR::SelectCase(
855 new IR::Member(
new IR::TypeNameExpression(
new IR::Type_Name(newInstanceType)),
857 new IR::PathExpression(
new IR::Path(p.second->name))));
859 auto instEnum =
new IR::Type_SerEnum(
861 {
new IR::Annotation(IR::Annotation::nameAnnotation,
".$InstanceType"_cs)},
862 IR::Type_Bits::get(32), members);
863 allTypeDecls.push_back(instEnum);
866 selExpr.push_back(
new IR::Cast(
867 new IR::Type_Name(newInstanceType),
868 new IR::Member(
new IR::PathExpression(
new IR::Path(
"standard_metadata"_cs)),
869 "instance_type"_cs)));
870 auto selects =
new IR::SelectExpression(
new IR::ListExpression(selExpr), selCases);
871 auto startState =
new IR::ParserState(
872 IR::ParserState::start,
873 {
new IR::Annotation(IR::Annotation::nameAnnotation,
".$start"_cs)}, selects);
874 parserStates.push_back(startState);
876 if (!parserStates.empty()) {
877 parser->states.append(parserStates);
878 parserStates.clear();
906 const IR::Type_Struct *stdType =
nullptr;
907 const IR::Type_Struct *userType =
nullptr;
908 const IR::Type_Struct *intrType =
nullptr;
909 const IR::Type_Struct *queueType =
nullptr;
910 const IR::StructField *intrField =
nullptr;
911 const IR::StructField *queueField =
nullptr;
915 CHECK_NULL(structure);
916 setName(
"MoveIntrinsicMetadata");
918 const IR::Node *preorder(IR::P4Program *program)
override {
919 stdType = program->getDeclsByName(structure->v1model.standardMetadataType.name)
921 ->
to<IR::Type_Struct>();
922 userType = program->getDeclsByName(structure->v1model.metadataType.name)
924 ->to<IR::Type_Struct>();
926 CHECK_NULL(userType);
927 intrField = userType->getField(structure->v1model.intrinsicMetadata.name);
928 if (intrField !=
nullptr) {
929 auto intrTypeName = intrField->type;
930 auto tn = intrTypeName->to<IR::Type_Name>();
931 BUG_CHECK(tn,
"%1%: expected a Type_Name", intrTypeName);
932 auto nt = program->getDeclsByName(tn->path->name)->nextOrDefault();
933 if (nt ==
nullptr || !nt->is<IR::Type_Struct>()) {
934 ::P4::error(ErrorType::ERR_INVALID,
"%1%: expected a structure", tn);
937 intrType = nt->to<IR::Type_Struct>();
938 LOG2(
"Intrinsic metadata type " << intrType);
941 queueField = userType->getField(structure->v1model.queueingMetadata.name);
942 if (queueField !=
nullptr) {
943 auto queueTypeName = queueField->type;
944 auto tn = queueTypeName->to<IR::Type_Name>();
945 BUG_CHECK(tn,
"%1%: expected a Type_Name", queueTypeName);
946 auto nt = program->getDeclsByName(tn->path->name)->nextOrDefault();
947 if (nt ==
nullptr || !nt->is<IR::Type_Struct>()) {
948 ::P4::error(ErrorType::ERR_INVALID,
"%1%: expected a structure", tn);
951 queueType = nt->to<IR::Type_Struct>();
952 LOG2(
"Queueing metadata type " << queueType);
957 const IR::Node *postorder(IR::Type_Struct *type)
override {
958 if (getOriginal() == stdType) {
959 if (intrType !=
nullptr) {
960 for (
auto f : intrType->fields) {
961 if (type->fields.getDeclaration(f->name) ==
nullptr) {
963 "%1%: no such field in standard_metadata", f->name);
964 LOG2(
"standard_metadata: " << type);
968 if (queueType !=
nullptr) {
969 for (
auto f : queueType->fields) {
970 if (type->fields.getDeclaration(f->name) ==
nullptr) {
972 "%1%: no such field in standard_metadata", f->name);
973 LOG2(
"standard_metadata: " << type);
981 const IR::Node *postorder(IR::StructField *field)
override {
982 if (getOriginal() == intrField || getOriginal() == queueField)
988 const IR::Node *postorder(IR::Member *member)
override {
992 if (member->member != structure->v1model.intrinsicMetadata.name &&
993 member->member != structure->v1model.queueingMetadata.name)
995 auto pe = member->expr->
to<IR::PathExpression>();
996 if (pe ==
nullptr || pe->path->absolute)
return member;
997 if (pe->path->name == structure->v1model.parser.metadataParam.name) {
998 LOG2(
"Renaming reference " << member);
999 return new IR::PathExpression(
new IR::Path(
1000 member->expr->srcInfo,
1001 IR::ID(pe->path->name.srcInfo, structure->v1model.standardMetadata.name)));
1012 void add(
const IR::Primitive *primitive,
unsigned operand) {
1013 if (primitive->operands.size() <= operand) {
1018 auto expression = primitive->operands.at(operand);
1019 if (!expression->is<IR::PathExpression>()) {
1020 ::P4::error(ErrorType::ERR_EXPECTED,
"%1%: expected a field list", expression);
1023 auto nr = expression->to<IR::PathExpression>();
1024 auto fl = structure->field_lists.get(nr->path->name);
1025 if (fl ==
nullptr) {
1026 ::P4::error(ErrorType::ERR_EXPECTED,
"%1%: Expected a field list", expression);
1029 LOG3(
"Recirculated " << nr->path->name);
1035 CHECK_NULL(structure);
1036 setName(
"FindRecirculated");
1039 void postorder(
const IR::Primitive *primitive)
override {
1040 if (primitive->name ==
"recirculate" || primitive->name ==
"resubmit") {
1042 }
else if (primitive->name.startsWith(
"clone") && primitive->operands.size() == 2) {
Definition frontends/p4-14/fromv1.0/programStructure.h:32