Skip to content

Commit

Permalink
Replace IR::NamedRef -> IR::PathExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Dodd committed Dec 20, 2016
1 parent 0f2b63e commit 640a429
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 69 deletions.
5 changes: 3 additions & 2 deletions frontends/p4-14/p4-14-parse.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,8 @@ expression:
{ $$ = new IR::Primitive(@1+@6, $1, $3, $5); }
| field_ref { $$ = $1; }
| header_ref
| LATEST '.' name { $$ = new IR::Member(@1+@3, new IR::NamedRef(@1, $1), IR::ID(@3, $3)); }
| LATEST '.' name { $$ = new IR::Member(@1+@3,
new IR::PathExpression(IR::ID(@1, $1)), IR::ID(@3, $3)); }
| INTEGER { $$ = $1; }
| '(' expression ')' { $$ = $2; }
| NOT expression %prec PREFIX { $$ = new IR::LNot(@1, $2); }
Expand Down Expand Up @@ -857,7 +858,7 @@ header_or_field_ref:
;

header_ref:
name { $$ = new IR::NamedRef(@1, IR::ID(@1, $1)); }
name { $$ = new IR::PathExpression(IR::ID(@1, $1)); }
| header_ref '[' expression ']' { $$ = new IR::HeaderStackItemRef(@1+@4, $1, $3); }
;

Expand Down
28 changes: 14 additions & 14 deletions frontends/p4-14/typecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ limitations under the License.
class TypeCheck::Pass1 : public Transform {
const IR::V1Program *global = nullptr;
const IR::Node *preorder(IR::V1Program *glob) override { global = glob; return glob; }
const IR::Node *preorder(IR::NamedRef *ref) override {
const IR::Node *preorder(IR::PathExpression *ref) override {
if (auto af = findContext<IR::ActionFunction>())
if (auto arg = af->arg(ref->name))
if (auto arg = af->arg(ref->path->name))
return arg;
if (auto bbox = findContext<IR::Declaration_Instance>()) {
if (auto bbox_type = bbox->type->to<IR::Type_Extern>()) {
if (auto attr = bbox_type->attributes.get<IR::Attribute>(ref->name))
if (auto attr = bbox_type->attributes.get<IR::Attribute>(ref->path->name))
return new IR::AttributeRef(ref->srcInfo, attr->type,
bbox->name, bbox_type, attr);
} else {
Expand Down Expand Up @@ -78,44 +78,44 @@ class TypeCheck::Pass1 : public Transform {
} else {
auto fl = new IR::FieldList();
for (auto &name : flc->input->names) {
fl->fields.push_back(new IR::NamedRef(name));
fl->fields.push_back(new IR::PathExpression(name));
fl->srcInfo += name.srcInfo; } }
return flc; }
const IR::Node *postorder(IR::NamedRef *ref) override {
const IR::Node *postorder(IR::PathExpression *ref) override {
if (!global) return ref;
const Visitor::Context *prop_ctxt = nullptr;
if (auto prop = findContext<IR::Property>(prop_ctxt)) {
if (auto bbox = prop_ctxt->parent->node->to<IR::Declaration_Instance>()) {
auto bbox_type = bbox->type->to<IR::Type_Extern>();
auto attr = bbox_type->attributes.get<IR::Attribute>(prop->name);
if (attr->locals && contains(attr->locals->names, ref->name)) {
if (attr->locals && contains(attr->locals->names, ref->path->name)) {
/* ref to local of property -- do something with it? */
return ref; } } }
IR::Node *new_node = ref;
if (auto hdr = global->get<IR::HeaderOrMetadata>(ref->name)) {
if (auto hdr = global->get<IR::HeaderOrMetadata>(ref->path->name)) {
visit(hdr);
new_node = new IR::ConcreteHeaderRef(ref->srcInfo, hdr);
} else if (auto obj = global->get<IR::IInstance>(ref->name)) {
} else if (auto obj = global->get<IR::IInstance>(ref->path->name)) {
const IR::Node *tmp = obj->getNode(); // FIXME -- can't visit an interface directly
visit(tmp);
obj = tmp->to<IR::IInstance>();
new_node = new IR::GlobalRef(ref->srcInfo, obj->getType(), tmp);
} else if (/*auto obj = */global->get<IR::Node>(ref->name)) {
} else if (/*auto obj = */global->get<IR::Node>(ref->path->name)) {
/* FIXME -- is something, should probably be typechecked */
} else if (getParent<IR::Member>()) {
if (ref->name != "latest")
error("%s: No header or metadata named %s", ref->srcInfo, ref->name);
if (ref->path->name != "latest")
error("%s: No header or metadata named %s", ref->srcInfo, ref->path->name);
} else {
if (getParent<IR::HeaderStackItemRef>()) {
if (ref->name == "next" || ref->name == "last")
if (ref->path->name == "next" || ref->path->name == "last")
return ref; }
if (auto hdr = findContext<IR::Type_StructLike>()) {
if (auto field = hdr->getField(ref->name)) {
if (auto field = hdr->getField(ref->path->name)) {
/* FIXME -- Should this be converted to something else? */
ref->type = field->type;
visit(ref->type);
return ref; } }
error("%s: No defintion for %s", ref->srcInfo, ref->name); }
error("%s: No defintion for %s", ref->srcInfo, ref->path->name); }
return new_node; }
const IR::Node *postorder(IR::Type_Name *ref) override {
if (!global) return ref;
Expand Down
30 changes: 15 additions & 15 deletions frontends/p4/fromv1.0/converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ const IR::Node* ExpressionConverter::postorder(IR::Primitive* primitive) {
BUG("Unexpected primitive %1%", primitive);
}

const IR::Node* ExpressionConverter::postorder(IR::NamedRef* ref) {
if (ref->name.name == "latest") {
const IR::Node* ExpressionConverter::postorder(IR::PathExpression *ref) {
if (ref->path->name.name == "latest") {
return structure->latest;
}
if (ref->name.name == "next") {
if (ref->path->name.name == "next") {
return ref;
}
auto fl = structure->field_lists.get(ref->name);
auto fl = structure->field_lists.get(ref->path->name);
if (fl != nullptr) {
ExpressionConverter conv(structure);
return conv.convert(fl);
}
return new IR::PathExpression(ref->name);
return ref;
}

const IR::Node* ExpressionConverter::postorder(IR::ConcreteHeaderRef* nhr) {
Expand All @@ -150,10 +150,10 @@ const IR::Node* ExpressionConverter::postorder(IR::ConcreteHeaderRef* nhr) {
}

const IR::Node* ExpressionConverter::postorder(IR::HeaderStackItemRef* ref) {
if (ref->index_->is<IR::NamedRef>()) {
auto nr = ref->index_->to<IR::NamedRef>();
if (nr->name == "last" || nr->name == "next") {
cstring name = nr->name == "last" ? IR::Type_Stack::last : IR::Type_Stack::next;
if (ref->index_->is<IR::PathExpression>()) {
auto nr = ref->index_->to<IR::PathExpression>();
if (nr->path->name == "last" || nr->path->name == "next") {
cstring name = nr->path->name == "last" ? IR::Type_Stack::last : IR::Type_Stack::next;
if (replaceNextWithLast && name == IR::Type_Stack::next)
name = IR::Type_Stack::last;
auto result = new IR::Member(ref->srcInfo, ref->base_, name);
Expand Down Expand Up @@ -433,8 +433,8 @@ class ComputeCallGraph : public Inspector {
const IR::Counter *ctr = nullptr;
if (auto gr = ctrref->to<IR::GlobalRef>())
ctr = gr->obj->to<IR::Counter>();
else if (auto nr = ctrref->to<IR::NamedRef>())
ctr = structure->counters.get(nr->name);
else if (auto nr = ctrref->to<IR::PathExpression>())
ctr = structure->counters.get(nr->path->name);
if (ctr == nullptr)
::error("Cannot find counter %1%", ctrref);
auto parent = findContext<IR::ActionFunction>();
Expand All @@ -446,8 +446,8 @@ class ComputeCallGraph : public Inspector {
const IR::Meter *mtr = nullptr;
if (auto gr = mtrref->to<IR::GlobalRef>())
mtr = gr->obj->to<IR::Meter>();
else if (auto nr = mtrref->to<IR::NamedRef>())
mtr = structure->meters.get(nr->name);
else if (auto nr = mtrref->to<IR::PathExpression>())
mtr = structure->meters.get(nr->path->name);
if (mtr == nullptr)
::error("Cannot find meter %1%", mtrref);
auto parent = findContext<IR::ActionFunction>();
Expand All @@ -464,8 +464,8 @@ class ComputeCallGraph : public Inspector {
const IR::Register *reg = nullptr;
if (auto gr = regref->to<IR::GlobalRef>())
reg = gr->obj->to<IR::Register>();
else if (auto nr = regref->to<IR::NamedRef>())
reg = structure->registers.get(nr->name);
else if (auto nr = regref->to<IR::PathExpression>())
reg = structure->registers.get(nr->path->name);
if (reg == nullptr)
::error("Cannot find register %1%", regref);
auto parent = findContext<IR::ActionFunction>();
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/fromv1.0/converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ExpressionConverter : public Transform {
const IR::Node* postorder(IR::Mask* expression) override;
const IR::Node* postorder(IR::ActionArg* arg) override;
const IR::Node* postorder(IR::Primitive* primitive) override;
const IR::Node* postorder(IR::NamedRef* ref) override;
const IR::Node* postorder(IR::PathExpression* ref) override;
const IR::Node* postorder(IR::ConcreteHeaderRef* nhr) override;
const IR::Node* postorder(IR::HeaderStackItemRef* ref) override;
const IR::Expression* convert(const IR::Node* node) {
Expand Down
38 changes: 19 additions & 19 deletions frontends/p4/fromv1.0/programStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ void ProgramStructure::createTypes() {
}

const IR::Type_Struct* ProgramStructure::createFieldListType(const IR::Expression* expression) {
if (!expression->is<IR::NamedRef>())
if (!expression->is<IR::PathExpression>())
::error("%1%: expected a field list", expression);
auto nr = expression->to<IR::NamedRef>();
auto fl = field_lists.get(nr->name);
auto nr = expression->to<IR::PathExpression>();
auto fl = field_lists.get(nr->path->name);
if (fl == nullptr)
::error("%1%: Expected a field list", expression);
auto fields = new IR::IndexedVector<IR::StructField>();
Expand All @@ -168,8 +168,8 @@ const IR::Type_Struct* ProgramStructure::createFieldListType(const IR::Expressio
fields->push_back(sf);
}

auto name = makeUniqueName(nr->name);
auto annos = addNameAnnotation(nr->name);
auto name = makeUniqueName(nr->path->name);
auto annos = addNameAnnotation(nr->path->name);
auto result = new IR::Type_Struct(expression->srcInfo, name, annos, fields);
return result;
}
Expand Down Expand Up @@ -470,7 +470,7 @@ class HeaderRepresentation {
} else if (expression->is<IR::HeaderStackItemRef>()) {
auto hsir = expression->to<IR::HeaderStackItemRef>();
auto hdr = getHeader(hsir->base_);
if (hsir->index_->is<IR::NamedRef>())
if (hsir->index_->is<IR::PathExpression>())
// This is most certainly 'next'.
return hdr;
BUG_CHECK(hsir->index_->is<IR::Constant>(), "%1%: expected a constant", hsir->index_);
Expand Down Expand Up @@ -841,10 +841,10 @@ const IR::Statement* ProgramStructure::sliceAssign(
const IR::Expression* ProgramStructure::convertFieldList(const IR::Expression* expression) {
ExpressionConverter conv(this);

if (!expression->is<IR::NamedRef>())
if (!expression->is<IR::PathExpression>())
::error("%1%: expected a field list", expression);
auto nr = expression->to<IR::NamedRef>();
auto fl = field_lists.get(nr->name);
auto nr = expression->to<IR::PathExpression>();
auto fl = field_lists.get(nr->path->name);
if (fl == nullptr)
::error("%1%: Expected a field list", expression);
auto result = conv.convert(fl);
Expand Down Expand Up @@ -1003,8 +1003,8 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
const IR::Counter *counter = nullptr;
if (auto gr = ref->to<IR::GlobalRef>())
counter = gr->obj->to<IR::Counter>();
else if (auto nr = ref->to<IR::NamedRef>())
counter = counters.get(nr->name);
else if (auto nr = ref->to<IR::PathExpression>())
counter = counters.get(nr->path->name);
if (counter == nullptr) {
::error("Expected a counter reference %1%", ref);
return nullptr; }
Expand Down Expand Up @@ -1130,8 +1130,8 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
const IR::Meter *meter = nullptr;
if (auto gr = ref->to<IR::GlobalRef>())
meter = gr->obj->to<IR::Meter>();
else if (auto nr = ref->to<IR::NamedRef>())
meter = meters.get(nr->name);
else if (auto nr = ref->to<IR::PathExpression>())
meter = meters.get(nr->path->name);
if (!meter) {
::error("Expected a meter reference %1%", ref);
return nullptr; }
Expand All @@ -1158,8 +1158,8 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
auto max = conv.convert(primitive->operands.at(3));
auto args = new IR::Vector<IR::Expression>();

auto nr = primitive->operands.at(2)->to<IR::NamedRef>();
auto flc = field_list_calculations.get(nr->name);
auto nr = primitive->operands.at(2)->to<IR::PathExpression>();
auto flc = field_list_calculations.get(nr->path->name);
if (flc == nullptr)
::error("%1%: Expected a field_list_calculation", primitive->operands.at(1));
auto ttype = IR::Type_Bits::get(flc->output_width);
Expand Down Expand Up @@ -1206,8 +1206,8 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
const IR::Register *reg = nullptr;
if (auto gr = ref->to<IR::GlobalRef>())
reg = gr->obj->to<IR::Register>();
else if (auto nr = ref->to<IR::NamedRef>())
reg = registers.get(nr->name);
else if (auto nr = ref->to<IR::PathExpression>())
reg = registers.get(nr->path->name);
if (!reg) {
::error("Expected a register reference %1%", ref);
return nullptr; }
Expand All @@ -1229,8 +1229,8 @@ const IR::Statement* ProgramStructure::convertPrimitive(const IR::Primitive* pri
const IR::Register *reg = nullptr;
if (auto gr = ref->to<IR::GlobalRef>())
reg = gr->obj->to<IR::Register>();
else if (auto nr = ref->to<IR::NamedRef>())
reg = registers.get(nr->name);
else if (auto nr = ref->to<IR::PathExpression>())
reg = registers.get(nr->path->name);
if (!reg) {
::error("Expected a register reference %1%", ref);
return nullptr; }
Expand Down
5 changes: 0 additions & 5 deletions frontends/p4/toP4/toP4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,6 @@ bool ToP4::preorder(const IR::Member* e) {
return false;
}

bool ToP4::preorder(const IR::NamedRef* e) {
builder.append(e->name);
return false;
}

bool ToP4::preorder(const IR::SelectCase* e) {
dump(2);
int prec = expressionPrecedence;
Expand Down
1 change: 0 additions & 1 deletion frontends/p4/toP4/toP4.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ class ToP4 : public Inspector {
bool preorder(const IR::Mux* a) override;
bool preorder(const IR::ConstructorCallExpression* e) override;
bool preorder(const IR::Member* e) override;
bool preorder(const IR::NamedRef* e) override;
bool preorder(const IR::SelectCase* e) override;
bool preorder(const IR::SelectExpression* e) override;
bool preorder(const IR::ListExpression* e) override;
Expand Down
4 changes: 0 additions & 4 deletions ir/dbprint-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ void IR::Constant::dbprint(std::ostream &out) const {
// if (getprec(out) == 0) out << ';';
}

void IR::NamedRef::dbprint(std::ostream &out) const {
out << name;
if (getprec(out) == 0) out << ';';
}
void IR::Member::dbprint(std::ostream &out) const {
int prec = getprec(out);
out << setprec(Prec_Postfix) << expr << setprec(prec) << '.' << member;
Expand Down
5 changes: 0 additions & 5 deletions ir/v1.def
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ class HeaderStackItemRef : HeaderRef {
toString{ return base_->toString() + "[" + index_->toString() + "]"; }
}

class NamedRef : Expression {
ID name;
toString{ return name; }
}

class If : Expression {
Expression pred;
NullOK Vector<Expression> ifTrue;
Expand Down
3 changes: 0 additions & 3 deletions midend/expr_uses.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class exprUses : public Inspector {
bool preorder(const IR::Primitive *p) override {
if (p->name == look_for) result = true;
return !result; }
bool preorder(const IR::NamedRef *n) override {
if (n->name == look_for) result = true;
return !result; }
bool preorder(const IR::Expression *) override { return !result; }
public:
exprUses(const IR::Expression *e, cstring n) : look_for(n) { e->apply(*this); }
Expand Down

0 comments on commit 640a429

Please sign in to comment.