Skip to content

Commit

Permalink
Make the new operator protected for some types.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed May 20, 2024
1 parent 4a5fd99 commit e79584b
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 77 deletions.
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
10 changes: 5 additions & 5 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
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 @@ -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 @@ -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
22 changes: 11 additions & 11 deletions frontends/parsers/p4/p4parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,11 +1019,11 @@ 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); }
| STRING { $$ = IR::Type::String::get(@1); }
| INT { $$ = new IR::Type_InfInt(@1); }
| BIT l_angle INTEGER r_angle
{ $$ = IR::Type::Bits::get(@1+@4, parseConstantChecked(@3, $3), false); }
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
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);
toString{ return "Unknown type"; }
}

Expand Down
10 changes: 4 additions & 6 deletions ir/irutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,20 @@ const IR::Constant *convertBoolLiteral(const IR::BoolLiteral *lit) {
const IR::Expression *getDefaultValue(const IR::Type *type, const Util::SourceInfo &srcInfo,
bool valueRequired) {
if (const auto *tb = type->to<IR::Type_Bits>()) {
// TODO: Use getConstant.
return new IR::Constant(srcInfo, tb, 0);
return IR::Constant::get(tb, 0, srcInfo);
}
if (type->is<IR::Type_Boolean>()) {
// TODO: Use getBoolLiteral.
return new BoolLiteral(srcInfo, Type::Boolean::get(), false);
return IR::BoolLiteral::get(false, srcInfo);
}
if (type->is<IR::Type_InfInt>()) {
return new IR::Constant(srcInfo, 0);
return IR::Constant::get(type, 0, srcInfo);
}
if (const auto *te = type->to<IR::Type_Enum>()) {
return new IR::Member(srcInfo, new IR::TypeNameExpression(te->name),
te->members.at(0)->getName());
}
if (const auto *te = type->to<IR::Type_SerEnum>()) {
return new IR::Cast(srcInfo, type->getP4Type(), new IR::Constant(srcInfo, te->type, 0));
return new IR::Cast(srcInfo, type->getP4Type(), IR::Constant::get(te->type, 0, srcInfo));
}
if (const auto *te = type->to<IR::Type_Error>()) {
return new IR::Member(srcInfo, new IR::TypeNameExpression(te->name), "NoError");
Expand Down
Loading

0 comments on commit e79584b

Please sign in to comment.