Skip to content

Commit

Permalink
Fixup/cleanup IR::Type::width_bits()
Browse files Browse the repository at this point in the history
- this method is used by backends to get the size of a type, and should
  only be used for Types for which the concept of `size in bits' makes
  sense
  • Loading branch information
ChrisDodd committed Aug 9, 2024
1 parent 63c4071 commit 97771fd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion backends/bmv2/psa_switch/psaSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class PsaSwitchExpressionConverter : public ExpressionConverter {
auto jsn = new Util::JsonObject();
jsn->emplace("name", param->toString());
jsn->emplace("type", "hexstr");
auto bitwidth = param->type->width_bits();
// FIXME -- how big is a PSA_CounterType_t? Being an enum type, we can't
// sensibly call param->width_bits() here.
auto bitwidth = 0;

// encode the counter type from enum -> int
if (fieldName == "BYTES") {
Expand Down
2 changes: 1 addition & 1 deletion ir/base.def
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract Type {
typedef Type_Void Void;
#end
/// Well-defined only for types with fixed width
virtual int width_bits() const { return 0; }
virtual int width_bits() const { BUG("width_bits() on type with unknown size: %1%", this); }
/// When possible returns the corresponding type that can be inserted
/// in a P4 program; may return a Type_Name
virtual const Type* getP4Type() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion ir/expression.def
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class TypeNameExpression : Expression {
dbprint{ out << typeName; }
toString { return typeName->toString(); }
validate { BUG_CHECK(typeName->is<Type_Name>() || typeName->is<Type_Specialized>(),
"%1 unexpected type in TypeNameExpression", typeName); }
"%1% unexpected type in TypeNameExpression", typeName); }
}

class Slice : Operation_Ternary {
Expand Down
4 changes: 4 additions & 0 deletions ir/type.def
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class Type_Varbits : Type_Base {
static Type_Varbits get();
toString{ return "varbit<"_cs + Util::toString(size) + ">"_cs; }
dbprint { out << "varbit<" << size << ">"; }
int width_bits() const override { return size; }
}

class Parameter : Declaration, IAnnotated {
Expand Down Expand Up @@ -268,6 +269,7 @@ class Type_InfInt : Type, ITypeVar {
return true; /* ignore declid */
}
const Type* getP4Type() const override { return this; }
int width_bits() const override { return 0; }
}

class Type_Dontcare : Type_Base {
Expand Down Expand Up @@ -583,6 +585,7 @@ class Type_Stack : Type_Indexed, Type {
static const cstring pop_front;
const Type* getP4Type() const override
{ return new IR::Type_Stack(srcInfo, elementType->getP4Type(), size); }
int width_bits() const override { return getSize() * elementType->width_bits(); }
}

/** Given a declaration
Expand Down Expand Up @@ -680,6 +683,7 @@ class Type_SerEnum : Type_Declaration, ISimpleNamespace, IAnnotated {
return members.getDeclaration(name); }
#nodbprint
validate{ members.check_null(); }
int width_bits() const override { return type->width_bits(); }
}

class Type_Table : Type, IApply {
Expand Down

0 comments on commit 97771fd

Please sign in to comment.