Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the new operator protected for some IR types. #4670

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backends/bmv2/common/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const IR::Node *LowerExpressions::postorder(IR::Cast *expression) {
} else if (destType->width_bits() < srcType->width_bits()) {
// explicitly discard un needed bits from src
auto one = new IR::Constant(srcType, 1);
auto shift_value = new IR::Constant(new IR::Type_InfInt(), destType->width_bits());
auto shift_value = new IR::Constant(IR::Type_InfInt::get(), destType->width_bits());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this (and many other places) could just be rewritten as

auto shift_value = new IR::Constant(destType->width_bits());

the default type for constants created via the ctor that just takes an int is InfInt. I don't think explicitly including the type adds anything -- its more of an obfuscation than a clarification.

auto shl = new IR::Shl(one->srcInfo, one, shift_value);
auto mask = new IR::Sub(shl->srcInfo, shl, one);
auto and0 = new IR::BAnd(expression->srcInfo, expression->expr, mask);
Expand Down Expand Up @@ -115,7 +115,7 @@ const IR::Node *LowerExpressions::postorder(IR::Slice *expression) {
BUG_CHECK(e0type->is<IR::Type_Bits>(), "%1%: expected a bit<> type", e0type);
const IR::Expression *expr;
if (l != 0) {
auto one = new IR::Constant(new IR::Type_InfInt(), l);
auto one = new IR::Constant(IR::Type_InfInt::get(), l);
expr = new IR::Shr(expression->e0->srcInfo, expression->e0, one);
typeMap->setType(expr, e0type);
typeMap->setType(one, one->type);
Expand Down Expand Up @@ -150,7 +150,7 @@ const IR::Node *LowerExpressions::postorder(IR::Concat *expression) {
unsigned sizeofb = type->to<IR::Type_Bits>()->size;
auto cast0 = new IR::Cast(expression->left->srcInfo, resulttype, expression->left);
auto cast1 = new IR::Cast(expression->right->srcInfo, resulttype, expression->right);
auto sizefb0 = new IR::Constant(new IR::Type_InfInt(), sizeofb);
auto sizefb0 = new IR::Constant(IR::Type_InfInt::get(), sizeofb);
auto sh = new IR::Shl(cast0->srcInfo, cast0, sizefb0);
big_int m = Util::maskFromSlice(sizeofb - 1, 0);
auto mask = new IR::Constant(expression->right->srcInfo, resulttype, m, 16);
Expand Down
2 changes: 1 addition & 1 deletion backends/dpdk/dpdkArch.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ class InjectInternetChecksumIntermediateValue : public Transform {
auto fields = new IR::IndexedVector<IR::StructField>;
for (auto fld : *csum_vec) {
fields->push_back(
new IR::StructField(IR::ID(fld), new IR::Type_Bits(16, false)));
new IR::StructField(IR::ID(fld), IR::Type::Bits::get(16, false)));
}
new_objs->push_back(new IR::Type_Header(IR::ID("cksum_state_t"), *fields));
}
Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/ebpfType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ EBPFType *EBPFTypeFactory::create(const IR::Type *type) {
result = new EBPFScalarType(tv);
} else if (type->is<IR::Type_Error>()) {
// Implement error type as scalar of width 8 bits
result = new EBPFScalarType(new IR::Type_Bits(8, false));
result = new EBPFScalarType(IR::Type_Bits::get(8, false));
} else {
::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET, "Type %1% not supported", type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ const IR::MethodCallStatement *generateStacksetValid(const IR::Expression *stack
stackRef->type->checkedTo<IR::Type_Stack>()->elementType, stackRef, index);
auto name = (isValid) ? IR::Type_Header::setValid : IR::Type_Header::setInvalid;
return new IR::MethodCallStatement(new IR::MethodCallExpression(
new IR::Type_Void(),
new IR::Member(new IR::Type_Method(new IR::Type_Void(), new IR::ParameterList(), name),
IR::Type_Void::get(),
new IR::Member(new IR::Type_Method(IR::Type_Void::get(), new IR::ParameterList(), name),
arrayIndex, name)));
}

Expand Down
10 changes: 5 additions & 5 deletions backends/p4tools/modules/testgen/targets/pna/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace P4Tools::P4Testgen::Pna {

const IR::Member PnaConstants::DROP_VAR =
IR::Member(IR::Type_Boolean::get(), new IR::PathExpression("*pna_internal"), "drop_var");
const IR::Member PnaConstants::OUTPUT_PORT_VAR = IR::Member(
new IR::Type_Bits(32, false), new IR::PathExpression("*pna_internal"), "output_port");
const IR::Member PnaConstants::PARSER_ERROR = IR::Member(
new IR::Type_Bits(32, false), new IR::PathExpression("*pna_internal"), "parser_error");
const IR::Member PnaConstants::OUTPUT_PORT_VAR =
IR::Member(IR::Type_Bits::get(32), new IR::PathExpression("*pna_internal"), "output_port");
const IR::Member PnaConstants::PARSER_ERROR =
IR::Member(IR::Type_Bits::get(32), new IR::PathExpression("*pna_internal"), "parser_error");
// TODO: Make this a proper variables variable.
// We can not use the utilities because of an issue related to the garbage collector.
const IR::SymbolicVariable PnaSymbolicVars::DIRECTION =
IR::SymbolicVariable(new IR::Type_Bits(32, false), "direction");
IR::SymbolicVariable(IR::Type_Bits::get(32), "direction");

} // namespace P4Tools::P4Testgen::Pna
4 changes: 4 additions & 0 deletions backends/p4tools/p4tools.def
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class SymbolicVariable : Expression {

/// This type replaces Type_Varbits and can store information about the current size
class Extracted_Varbits : Type_Bits {
public:
#emit
void *operator new(size_t size) { return ::operator new(size); }
#end
/// The assigned size of this varbit (assigned by extract calls).
int assignedSize;

Expand Down
12 changes: 6 additions & 6 deletions control-plane/addMissingIds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const IR::Property *MissingIdAssigner::postorder(IR::Property *property) {
auto *newAnnos = annos->clone();
IR::Vector<IR::Expression> annoExprs;
const auto *idConst =
new IR::Constant(new IR::Type_Bits(ID_BIT_WIDTH, false), symbolId + 1);
new IR::Constant(IR::Type_Bits::get(ID_BIT_WIDTH, false), symbolId + 1);
annoExprs.push_back(idConst);
newAnnos->add(new IR::Annotation("id", annoExprs));
keyElement->annotations = newAnnos;
Expand All @@ -60,7 +60,7 @@ const IR::P4Table *MissingIdAssigner::postorder(IR::P4Table *table) {
auto *newAnnos = annos->clone();
IR::Vector<IR::Expression> annoExprs;
auto symbolId = symbols->getId(ControlPlaneAPI::P4RuntimeSymbolType::P4RT_TABLE(), table);
const auto *idConst = new IR::Constant(new IR::Type_Bits(ID_BIT_WIDTH, false), symbolId);
const auto *idConst = new IR::Constant(IR::Type_Bits::get(ID_BIT_WIDTH, false), symbolId);
annoExprs.push_back(idConst);
newAnnos->add(new IR::Annotation("id", annoExprs));
table->annotations = newAnnos;
Expand All @@ -80,7 +80,7 @@ const IR::Type_Header *MissingIdAssigner::postorder(IR::Type_Header *hdr) {
IR::Vector<IR::Expression> annoExprs;
auto symbolId =
symbols->getId(ControlPlaneAPI::P4RuntimeSymbolType::P4RT_CONTROLLER_HEADER(), hdr);
const auto *idConst = new IR::Constant(new IR::Type_Bits(ID_BIT_WIDTH, false), symbolId);
const auto *idConst = new IR::Constant(IR::Type_Bits::get(ID_BIT_WIDTH, false), symbolId);
annoExprs.push_back(idConst);
newAnnos->add(new IR::Annotation("id", annoExprs));
hdr->annotations = newAnnos;
Expand All @@ -101,7 +101,7 @@ const IR::P4ValueSet *MissingIdAssigner::postorder(IR::P4ValueSet *valueSet) {
IR::Vector<IR::Expression> annoExprs;
auto symbolId =
symbols->getId(ControlPlaneAPI::P4RuntimeSymbolType::P4RT_VALUE_SET(), valueSet);
const auto *idConst = new IR::Constant(new IR::Type_Bits(ID_BIT_WIDTH, false), symbolId);
const auto *idConst = new IR::Constant(IR::Type_Bits::get(ID_BIT_WIDTH, false), symbolId);
annoExprs.push_back(idConst);
newAnnos->add(new IR::Annotation("id", annoExprs));
valueSet->annotations = newAnnos;
Expand All @@ -120,7 +120,7 @@ const IR::P4Action *MissingIdAssigner::postorder(IR::P4Action *action) {
auto *newAnnos = annos->clone();
IR::Vector<IR::Expression> annoExprs;
auto symbolId = symbols->getId(ControlPlaneAPI::P4RuntimeSymbolType::P4RT_ACTION(), action);
const auto *idConst = new IR::Constant(new IR::Type_Bits(ID_BIT_WIDTH, false), symbolId);
const auto *idConst = new IR::Constant(IR::Type_Bits::get(ID_BIT_WIDTH, false), symbolId);
annoExprs.push_back(idConst);
newAnnos->add(new IR::Annotation("id", annoExprs));
action->annotations = newAnnos;
Expand All @@ -135,7 +135,7 @@ const IR::P4Action *MissingIdAssigner::postorder(IR::P4Action *action) {
auto *newAnnos = annos->clone();
IR::Vector<IR::Expression> annoExprs;
const auto *idConst =
new IR::Constant(new IR::Type_Bits(ID_BIT_WIDTH, false), symbolId + 1);
new IR::Constant(IR::Type_Bits::get(ID_BIT_WIDTH, false), symbolId + 1);
annoExprs.push_back(idConst);
newAnnos->add(new IR::Annotation("id", annoExprs));
param->annotations = newAnnos;
Expand Down
4 changes: 2 additions & 2 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CloneConstants : public Transform {
} else if (auto ii = type->to<IR::Type_InfInt>()) {
// You can't just clone a InfInt value, because
// you get the same declid. We want a new declid.
type = new IR::Type_InfInt(ii->srcInfo);
type = IR::Type_InfInt::get(ii->srcInfo);
} else {
BUG("unexpected type %2% for constant %2%", type, constant);
}
Expand Down Expand Up @@ -163,7 +163,7 @@ const IR::Node *DoConstantFolding::postorder(IR::Declaration_Constant *d) {
} else {
// Destination type is InfInt; we must "erase" the width of the source
if (!cst->type->is<IR::Type_InfInt>()) {
init = new IR::Constant(cst->srcInfo, new IR::Type_InfInt(), cst->value,
init = new IR::Constant(cst->srcInfo, IR::Type_InfInt::get(), cst->value,
cst->base);
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/defaultArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TypeNameSubstitutionVisitor : public TypeVariableSubstitutionVisitor {
}
// When cloning the value of an argument we want to make a fresh
// copy for each new InfInt type, and not share the same one.
const IR::Node *postorder(IR::Type_InfInt *) override { return new IR::Type_InfInt(); }
const IR::Node *postorder(IR::Type_InfInt *) override { return IR::Type_InfInt::get(); }
};
} // namespace

Expand Down
18 changes: 9 additions & 9 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ std::pair<const IR::Type *, const IR::Vector<IR::Argument> *> TypeInference::con
auto params = constructor->parameters;
for (auto param : params->parameters) {
if (auto v = param->type->to<IR::Type_InfInt>()) {
auto tv = new IR::Type_InfInt(param->type->srcInfo);
auto tv = IR::Type_InfInt::get(param->type->srcInfo);
bool b = tvs.setBinding(v, tv);
BUG_CHECK(b, "failed replacing %2% with %3%", v, tv);
}
Expand Down Expand Up @@ -1236,7 +1236,7 @@ std::pair<const IR::Type *, const IR::Vector<IR::Argument> *> TypeInference::con
forAllMatching<IR::Type_Var>(p, [tvs, dontCares, typeParams](const IR::Type_Var *tv) {
if (tvs->lookup(tv)) return; // already bound
if (typeParams->getDeclByName(tv->name) != tv) return; // not a tv of this call
dontCares->setBinding(tv, new IR::Type_Dontcare);
dontCares->setBinding(tv, IR::Type_Dontcare::get());
});
}
addSubstitutions(dontCares);
Expand Down Expand Up @@ -2533,7 +2533,7 @@ const IR::Node *TypeInference::binaryArith(const IR::Operation_Binary *expressio
expression->getStringOp(), expression->right, rtype->toString());
return expression;
} else if (ltype->is<IR::Type_InfInt>() && rtype->is<IR::Type_InfInt>()) {
auto t = new IR::Type_InfInt();
auto t = IR::Type_InfInt::get();
auto result = constantFold(expression);
setType(getOriginal(), t);
setCompileTimeConstant(result);
Expand Down Expand Up @@ -2660,7 +2660,7 @@ const IR::Node *TypeInference::shift(const IR::Operation_Binary *expression) {
// If the amount is signed but positive, make it unsigned
if (auto bt = rtype->to<IR::Type_Bits>()) {
if (bt->isSigned) {
rtype = new IR::Type_Bits(rtype->srcInfo, bt->width_bits(), false);
rtype = IR::Type_Bits::get(rtype->srcInfo, bt->width_bits(), false);
auto amt = new IR::Constant(cst->srcInfo, rtype, cst->value, cst->base);
if (expression->is<IR::Shl>()) {
expression = new IR::Shl(expression->srcInfo, expression->left, amt);
Expand Down Expand Up @@ -3168,8 +3168,8 @@ const IR::Node *TypeInference::postorder(IR::Slice *expression) {

const IR::Node *TypeInference::postorder(IR::Dots *expression) {
if (done()) return expression;
setType(expression, new IR::Type_Any());
setType(getOriginal(), new IR::Type_Any());
setType(expression, IR::Type_Any::get());
setType(getOriginal(), IR::Type_Any::get());
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
return expression;
Expand Down Expand Up @@ -3267,7 +3267,7 @@ const IR::Node *TypeInference::postorder(IR::Member *expression) {
// Built-in methods
if (inMethod && (member == IR::Type::minSizeInBits || member == IR::Type::minSizeInBytes ||
member == IR::Type::maxSizeInBits || member == IR::Type::maxSizeInBytes)) {
auto type = new IR::Type_Method(new IR::Type_InfInt(), new IR::ParameterList(), member);
auto type = new IR::Type_Method(IR::Type_InfInt::get(), new IR::ParameterList(), member);
auto ctype = canonicalize(type);
if (ctype == nullptr) return expression;
setType(getOriginal(), ctype);
Expand Down Expand Up @@ -3381,7 +3381,7 @@ const IR::Node *TypeInference::postorder(IR::Member *expression) {
typeError("%1%: must be applied to a left-value", expression);
auto params = new IR::IndexedVector<IR::Parameter>();
auto param = new IR::Parameter(IR::ID("count", nullptr), IR::Direction::None,
new IR::Type_InfInt());
IR::Type_InfInt::get());
auto tt = new IR::Type_Type(param->type);
setType(param->type, tt);
setType(param, param->type);
Expand Down Expand Up @@ -3736,7 +3736,7 @@ const IR::Node *TypeInference::postorder(IR::MethodCallExpression *expression) {
return; // already bound
if (tvs->lookup(tv)) return; // already bound
if (typeParams->getDeclByName(tv->name) != tv) return; // not a tv of this call
dontCares->setBinding(tv, new IR::Type_Dontcare);
dontCares->setBinding(tv, IR::Type_Dontcare::get());
});
}
addSubstitutions(dontCares);
Expand Down
24 changes: 12 additions & 12 deletions frontends/parsers/p4/p4parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,12 +1019,12 @@ specializedType
;

baseType
: BOOL { $$ = new IR::Type_Boolean(@1); }
| MATCH_KIND { $$ = new IR::Type_MatchKind(@1); }
: BOOL { $$ = IR::Type_Boolean::get(@1); }
| MATCH_KIND { $$ = IR::Type_MatchKind::get(@1); }
| ERROR { $$ = new IR::Type_Name(@1, new IR::Path(IR::ID(@1, "error"))); }
| BIT { $$ = IR::Type::Bits::get(@1, 1); }
| STRING { $$ = new IR::Type::String(@1); }
| INT { $$ = new IR::Type_InfInt(@1); }
| STRING { $$ = IR::Type::String::get(@1); }
| INT { $$ = IR::Type_InfInt::get(@1); }
| BIT l_angle INTEGER r_angle
{ $$ = IR::Type::Bits::get(@1+@4, parseConstantChecked(@3, $3), false); }
| INT l_angle INTEGER r_angle
Expand All @@ -1033,16 +1033,16 @@ baseType
{ $$ = IR::Type::Varbits::get(@1+@4, parseConstantChecked(@3, $3)); }

| BIT l_angle "(" expression ")" r_angle
{ $$ = new IR::Type_Bits(@1+@6, $4, false); }
{ $$ = IR::Type_Bits::get(@1+@6, $4, false); }
| INT l_angle "(" expression ")" r_angle
{ $$ = new IR::Type_Bits(@1+@6, $4, true); }
{ $$ = IR::Type_Bits::get(@1+@6, $4, true); }
| VARBIT l_angle "(" expression ")" r_angle
{ $$ = new IR::Type_Varbits(@1+@6, $4); }
{ $$ = IR::Type_Varbits::get(@1+@6, $4); }
;

typeOrVoid
: typeRef { $$ = $1; }
| VOID { $$ = new IR::Type_Void(@1); }
| VOID { $$ = IR::Type_Void::get(@1); }
| IDENTIFIER { $$ = new IR::Type_Name(@1, new IR::Path(*(new IR::ID(@1, $1)))); }
// This is necessary because template arguments may introduce the return type
;
Expand All @@ -1066,8 +1066,8 @@ typeArg
: typeRef { $$ = $1; }
| nonTypeName { $$ = new IR::Type_Name(@1, new IR::Path(*$1)); }
// This is necessary because template arguments may introduce the return type
| VOID { $$ = new IR::Type_Void(@1); }
| "_" { $$ = new IR::Type_Dontcare(@1); }
| VOID { $$ = IR::Type_Void::get(@1); }
| "_" { $$ = IR::Type_Dontcare::get(@1); }
;

typeArgumentList
Expand All @@ -1078,8 +1078,8 @@ typeArgumentList

realTypeArg
: typeRef { $$ = $1; }
| VOID { $$ = new IR::Type_Void(@1); }
| "_" { $$ = new IR::Type_Dontcare(@1); }
| VOID { $$ = IR::Type_Void::get(@1); }
| "_" { $$ = IR::Type_Dontcare::get(@1); }
;

// For use in contexts where the `<` might be a less-than rather than introducing a type
Expand Down
2 changes: 1 addition & 1 deletion frontends/parsers/v1/v1parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ type: BIT { $$ = IR::Type::Bits::get(@1, 1); }
| COUNTER { $$ = IR::Type_Counter::get(); }
| EXPRESSION { $$ = IR::Type_Expression::get(); }
| FIELD_LIST_CALCULATION { $$ = IR::Type_FieldListCalculation::get(); }
| INT { $$ = new IR::Type_InfInt; }
| INT { $$ = IR::Type_InfInt::get(); }
| INT "<" INTEGER ">"
{ $$ = IR::Type::Bits::get(@3, parseConstant(@3, $3, 0)->asInt(), true); }
| METER { $$ = IR::Type_Meter::get(); }
Expand Down
1 change: 1 addition & 0 deletions ir/base.def
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ abstract Type_Base : Type {
class Type_Unknown : Type_Base {
#nodbprint
static Type_Unknown get();
static Type_Unknown get(const Util::SourceInfo &si);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there ever a case where we have an unknown type with a source info? I can't think of how it could occur.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think so. At least we do not store any source info for an unknown type when parsing. I mostly mirrored what the auto-generated constructors allow, so I do not think at can hurt at least.

toString{ return "Unknown type"; }
}

Expand Down
18 changes: 9 additions & 9 deletions ir/expression.def
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,28 @@ class Constant : Literal {
// 32-bit systems (which ain't long!) and mpz_import is too big a hammer because and it loses
// the signess of the value.
Constant(int v, unsigned base = 10) :
Literal(new Type_InfInt()), value(v), base(base) {}
Literal(Type_InfInt::get()), value(v), base(base) {}
Constant(unsigned v, unsigned base = 10) :
Literal(new Type_InfInt()), value(v), base(base) {}
Literal(Type_InfInt::get()), value(v), base(base) {}
#emit
#if __WORDSIZE == 64
Constant(intmax_t v, unsigned base = 10) :
Literal(new Type_InfInt()), value(v), base(base) {}
Literal(Type_InfInt::get()), value(v), base(base) {}
#else
Constant(long v, unsigned base = 10) :
Literal(new Type_InfInt()), value(v), base(base) {}
Literal(Type_InfInt::get()), value(v), base(base) {}
Constant(unsigned long v, unsigned base = 10) :
Literal(new Type_InfInt()), value(v), base(base) {}
Literal(Type_InfInt::get()), value(v), base(base) {}
Constant(intmax_t v, unsigned base = 10) :
Literal(new Type_InfInt()), value(v), base(base) {}
Literal(Type_InfInt::get()), value(v), base(base) {}
#endif
#end
Constant(uint64_t v, unsigned base = 10) :
Literal(new Type_InfInt()), value(v), base(base) {}
Literal(Type_InfInt::get()), value(v), base(base) {}
Constant(big_int v, unsigned base = 10) :
Literal(new Type_InfInt()), value(v), base(base) {}
Literal(Type_InfInt::get()), value(v), base(base) {}
Constant(Util::SourceInfo si, big_int v, unsigned base = 10) :
Literal(si, new Type_InfInt()), value(v), base(base) {}
Literal(si, Type_InfInt::get()), value(v), base(base) {}
Constant(const Type *t, big_int v, unsigned base = 10, bool noWarning = false) :
Literal(t), value(v), base(base) { CHECK_NULL(t); handleOverflow(noWarning); }
Constant(Util::SourceInfo si, const Type *t, big_int v,
Expand Down
Loading
Loading