Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/komaljai/p4c into support_d…
Browse files Browse the repository at this point in the history
…irect_counter
  • Loading branch information
komaljai committed Jun 7, 2024
2 parents 05bfcc5 + c3c2401 commit f539088
Show file tree
Hide file tree
Showing 325 changed files with 4,028 additions and 3,673 deletions.
226 changes: 113 additions & 113 deletions backends/bmv2/common/JsonObjects.cpp

Large diffs are not rendered by default.

36 changes: 19 additions & 17 deletions backends/bmv2/common/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ limitations under the License.

namespace BMV2 {

using namespace P4::literals;

cstring ActionConverter::jsonAssignment(const IR::Type *type) {
if (type->is<IR::Type_Varbits>()) return "assign_VL";
if (type->is<IR::Type_HeaderUnion>()) return "assign_union";
if (type->is<IR::Type_Header>() || type->is<IR::Type_Struct>()) return "assign_header";
if (type->is<IR::Type_Varbits>()) return "assign_VL"_cs;
if (type->is<IR::Type_HeaderUnion>()) return "assign_union"_cs;
if (type->is<IR::Type_Header>() || type->is<IR::Type_Struct>()) return "assign_header"_cs;
if (auto ts = type->to<IR::Type_Stack>()) {
auto et = ts->elementType;
if (et->is<IR::Type_HeaderUnion>())
return "assign_union_stack";
return "assign_union_stack"_cs;
else
return "assign_header_stack";
return "assign_header_stack"_cs;
}
return "assign";
return "assign"_cs;
}

void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
Expand Down Expand Up @@ -98,9 +100,9 @@ void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
} else if (s->is<IR::ReturnStatement>()) {
break;
} else if (s->is<IR::ExitStatement>()) {
auto primitive = mkPrimitive("exit", result);
auto primitive = mkPrimitive("exit"_cs, result);
(void)mkParameters(primitive);
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
primitive->emplace_non_null("source_info"_cs, s->sourceInfoJsonObj());
break;
} else if (s->is<IR::AssignmentStatement>()) {
const IR::Expression *l, *r;
Expand All @@ -111,7 +113,7 @@ void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
cstring operation = jsonAssignment(type);
auto primitive = mkPrimitive(operation, result);
auto parameters = mkParameters(primitive);
primitive->emplace_non_null("source_info", assign->sourceInfoJsonObj());
primitive->emplace_non_null("source_info"_cs, assign->sourceInfoJsonObj());
bool convertBool = type->is<IR::Type_Boolean>();
Util::IJson *left;
if (ctxt->conv->isArrayIndexRuntime(l)) {
Expand Down Expand Up @@ -141,25 +143,25 @@ void ActionConverter::convertActionBody(const IR::Vector<IR::StatOrDecl> *body,
parameters->append(obj);

if (builtin->name == IR::Type_Header::setValid) {
prim = "add_header";
prim = "add_header"_cs;
} else if (builtin->name == IR::Type_Header::setInvalid) {
prim = "remove_header";
prim = "remove_header"_cs;
} else if (builtin->name == IR::Type_Stack::push_front) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
auto arg = ctxt->conv->convert(mc->arguments->at(0)->expression);
prim = "push";
prim = "push"_cs;
parameters->append(arg);
} else if (builtin->name == IR::Type_Stack::pop_front) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
auto arg = ctxt->conv->convert(mc->arguments->at(0)->expression);
prim = "pop";
prim = "pop"_cs;
parameters->append(arg);
} else {
BUG("%1%: Unexpected built-in method", s);
}
auto primitive = mkPrimitive(prim, result);
primitive->emplace("parameters", parameters);
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
primitive->emplace("parameters"_cs, parameters);
primitive->emplace_non_null("source_info"_cs, s->sourceInfoJsonObj());
continue;
} else if (mi->is<P4::ExternMethod>()) {
auto em = mi->to<P4::ExternMethod>();
Expand Down Expand Up @@ -190,14 +192,14 @@ void ActionConverter::convertActionParams(const IR::ParameterList *parameters,
warn(ErrorType::WARN_UNUSED, "Unused action parameter %1%", p);

auto param = new Util::JsonObject();
param->emplace("name", p->externalName());
param->emplace("name"_cs, p->externalName());
auto type = ctxt->typeMap->getType(p, true);
// TODO: added IR::Type_Enum here to support PSA_MeterColor_t
// should re-consider how to support action parameters that is neither bit<> nor int<>
if (!(type->is<IR::Type_Bits>() || type->is<IR::Type_Enum>()))
::error(ErrorType::ERR_INVALID,
"%1%: action parameters must be bit<> or int<> on this target", p);
param->emplace("bitwidth", type->width_bits());
param->emplace("bitwidth"_cs, type->width_bits());
params->append(param);
}
}
Expand Down
16 changes: 9 additions & 7 deletions backends/bmv2/common/annotations.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ limitations under the License.

namespace BMV2 {

using namespace P4::literals;

/// Parses BMV2-specific annotations.
class ParseAnnotations : public P4::ParseAnnotations {
public:
ParseAnnotations()
: P4::ParseAnnotations("BMV2", false,
: P4::ParseAnnotations("BMV2"_cs, false,
{
PARSE_EMPTY("metadata"),
PARSE_EXPRESSION_LIST("field_list"),
PARSE("alias", StringLiteral),
PARSE("priority", Constant),
PARSE_EXPRESSION_LIST("p4runtime_translation_mappings"),
PARSE_P4RUNTIME_TRANSLATION("p4runtime_translation"),
PARSE_EMPTY("metadata"_cs),
PARSE_EXPRESSION_LIST("field_list"_cs),
PARSE("alias"_cs, StringLiteral),
PARSE("priority"_cs, Constant),
PARSE_EXPRESSION_LIST("p4runtime_translation_mappings"_cs),
PARSE_P4RUNTIME_TRANSLATION("p4runtime_translation"_cs),
}) {}
};

Expand Down
7 changes: 4 additions & 3 deletions backends/bmv2/common/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
#include "frontends/p4/coreLibrary.h"
#include "helpers.h"
#include "ir/ir.h"
#include "lib/cstring.h"
#include "lib/error.h"
#include "lib/exceptions.h"
#include "lib/gc.h"
Expand Down Expand Up @@ -153,7 +154,7 @@ class RenameUserMetadata : public Transform {
IR::IndexedVector<IR::StructField> fields;
for (auto f : type->fields) {
auto anno = f->getAnnotation(IR::Annotation::nameAnnotation);
cstring suffix = "";
cstring suffix = cstring::empty;
if (anno != nullptr) suffix = anno->getName();
if (suffix.startsWith(".")) {
// We can't change the name of this field.
Expand All @@ -163,9 +164,9 @@ class RenameUserMetadata : public Transform {
}

if (!suffix.isNullOrEmpty())
suffix = cstring(".") + suffix;
suffix = "."_cs + suffix;
else
suffix = cstring(".") + f->name;
suffix = "."_cs + f->name;
cstring newName = namePrefix + suffix;
auto stringLit = new IR::StringLiteral(newName);
LOG2("Renaming " << f << " to " << newName);
Expand Down
Loading

0 comments on commit f539088

Please sign in to comment.