Skip to content

Commit 0dbac26

Browse files
author
mbudiu-vmw
committed
default_action has to bind ALL parameters; fix for issue #164
1 parent 1f8ddd7 commit 0dbac26

32 files changed

+394
-104
lines changed

frontends/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ p4_frontend_SOURCES = \
3737
frontends/p4/typeChecking/typeConstraints.cpp \
3838
frontends/p4/typeChecking/typeUnification.h \
3939
frontends/p4/typeChecking/typeUnification.cpp \
40+
frontends/p4/typeChecking/typeUnification.h \
41+
frontends/p4/typeChecking/syntacticEquivalence.h \
42+
frontends/p4/typeChecking/syntacticEquivalence.cpp \
4043
frontends/p4/strengthReduction.h \
4144
frontends/p4/strengthReduction.cpp \
4245
frontends/p4/unusedDeclarations.h \

frontends/p4/p4-parse.ypp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,9 @@ prefixedNonTypeName
887887

888888
lvalue
889889
: prefixedNonTypeName { $$ = new IR::PathExpression($1); }
890-
| THIS { $$ = new IR::This(@1); }
890+
| THIS { $$ = new IR::This(@1); } // experimental
891891
| lvalue '.' name { $$ = new IR::Member(@1 + @3, $1, *$3); }
892-
| lvalue '[' expression ']' { $$ = new IR::ArrayIndex(@1 + @3, $1, $3); }
892+
| lvalue '[' expression ']' { $$ = new IR::ArrayIndex(@1 + @4, $1, $3); }
893893
| lvalue '[' expression ':' expression ']' { $$ = new IR::Slice(@1 + @6, $1, $3, $5); }
894894
;
895895

@@ -898,7 +898,7 @@ expression
898898
| STRING_LITERAL { $$ = new IR::StringLiteral(@1, $1); }
899899
| TRUE { $$ = new IR::BoolLiteral(@1, true); }
900900
| FALSE { $$ = new IR::BoolLiteral(@1, false); }
901-
| THIS { $$ = new IR::This(@1); }
901+
| THIS { $$ = new IR::This(@1); } // experimental
902902
| nonTypeName { $$ = new IR::PathExpression(*$1); }
903903
| '.' nonTypeName { $$ = new IR::PathExpression(new IR::Path(*$2, true)); }
904904
| expression '[' expression ']' { $$ = new IR::ArrayIndex(@1 + @4, $1, $3); }

frontends/p4/simplifyParsers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class RemoveUnreachableStates : public Transform {
3939
transitions->reachable(start->to<IR::ParserState>(), reachable);
4040
// Remove unreachable states from call-graph
4141
transitions->restrict(reachable);
42-
LOG1("Parser " << parser << " has " << transitions->size() << " reachable states");
42+
LOG1("Parser " << dbp(parser) << " has " << transitions->size() << " reachable states");
4343
}
4444
return parser;
4545
}
@@ -54,7 +54,7 @@ class RemoveUnreachableStates : public Transform {
5454
::warning("%1% state in %2% is unreachable", state, findContext<IR::P4Parser>());
5555
return state;
5656
} else {
57-
LOG1("Removing unreachable state " << state);
57+
LOG1("Removing unreachable state " << dbp(state));
5858
return nullptr;
5959
}
6060
}
@@ -119,15 +119,15 @@ class CollapseChains : public Transform {
119119
// collapse chain
120120
auto components = new IR::IndexedVector<IR::StatOrDecl>();
121121
auto crt = s;
122-
LOG1("Chaining states into " << crt);
122+
LOG1("Chaining states into " << dbp(crt));
123123
const IR::Expression *select = nullptr;
124124
while (true) {
125125
components->append(*crt->components);
126126
select = crt->selectExpression;
127127
crt = ::get(chain, crt);
128128
if (crt == nullptr)
129129
break;
130-
LOG1("Adding " << crt << " to chain");
130+
LOG1("Adding " << dbp(crt) << " to chain");
131131
}
132132
s = new IR::ParserState(s->srcInfo, s->name, s->annotations,
133133
components, select);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2016 VMware, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#include "syntacticEquivalence.h"
18+
19+
namespace P4 {
20+
21+
bool SameExpression::sameType(const IR::Type* left, const IR::Type* right) const {
22+
auto lt = typeMap->getType(left, true);
23+
auto rt = typeMap->getType(right, true);
24+
return TypeMap::equivalent(lt, rt);
25+
}
26+
27+
bool SameExpression::sameExpressions(const IR::Vector<IR::Expression>* left,
28+
const IR::Vector<IR::Expression>* right) const {
29+
if (left->size() != right->size())
30+
return false;
31+
for (unsigned i = 0; i < left->size(); i++)
32+
if (!sameExpression(left->at(i), right->at(i)))
33+
return false;
34+
return true;
35+
}
36+
37+
bool SameExpression::sameExpression(const IR::Expression* left, const IR::Expression* right) const {
38+
CHECK_NULL(left); CHECK_NULL(right);
39+
if (left->node_type_name() != right->node_type_name())
40+
return false;
41+
if (left->is<IR::Operation_Unary>()) {
42+
auto lu = left->to<IR::Operation_Unary>();
43+
auto ru = right->to<IR::Operation_Unary>();
44+
if (left->is<IR::Member>()) {
45+
auto lm = left->to<IR::Member>();
46+
auto rm = right->to<IR::Member>();
47+
if (lm->member != rm->member)
48+
return false;
49+
} else if (left->is<IR::Cast>()) {
50+
auto lc = left->to<IR::Cast>();
51+
auto rc = right->to<IR::Cast>();
52+
if (!sameType(lc->type, rc->type))
53+
return false;
54+
}
55+
return sameExpression(lu->expr, ru->expr);
56+
} else if (left->is<IR::Operation_Binary>()) {
57+
auto lb = left->to<IR::Operation_Binary>();
58+
auto rb = right->to<IR::Operation_Binary>();
59+
return sameExpression(lb->left, rb->left) && sameExpression(lb->right, rb->right);
60+
} else if (left->is<IR::Operation_Ternary>()) {
61+
auto lt = left->to<IR::Operation_Ternary>();
62+
auto rt = right->to<IR::Operation_Ternary>();
63+
return sameExpression(lt->e0, rt->e0) &&
64+
sameExpression(lt->e1, rt->e1) &&
65+
sameExpression(lt->e2, rt->e2);
66+
} else if (left->is<IR::Constant>()) {
67+
return left->to<IR::Constant>()->value == right->to<IR::Constant>()->value;
68+
} else if (left->is<IR::Literal>()) {
69+
return *left == *right;
70+
} else if (left->is<IR::PathExpression>()) {
71+
auto ld = refMap->getDeclaration(left->to<IR::PathExpression>()->path, true);
72+
auto rd = refMap->getDeclaration(right->to<IR::PathExpression>()->path, true);
73+
return ld == rd;
74+
} else if (left->is<IR::TypeNameExpression>()) {
75+
auto ld = refMap->getDeclaration(left->to<IR::TypeNameExpression>()->typeName->path, true);
76+
auto rd = refMap->getDeclaration(right->to<IR::TypeNameExpression>()->typeName->path, true);
77+
return ld == rd;
78+
} else if (left->is<IR::ListExpression>()) {
79+
auto ll = left->to<IR::ListExpression>();
80+
auto rl = right->to<IR::ListExpression>();
81+
return sameExpressions(ll->components, rl->components);
82+
} else if (left->is<IR::MethodCallExpression>()) {
83+
auto lm = left->to<IR::MethodCallExpression>();
84+
auto rm = right->to<IR::MethodCallExpression>();
85+
if (!sameExpression(lm->method, rm->method))
86+
return false;
87+
if (lm->typeArguments->size() != rm->typeArguments->size())
88+
return false;
89+
for (unsigned i = 0; i < lm->typeArguments->size(); i++)
90+
if (!sameType(lm->typeArguments->at(i), rm->typeArguments->at(i)))
91+
return false;
92+
return sameExpressions(lm->arguments, rm->arguments);
93+
} else if (left->is<IR::ConstructorCallExpression>()) {
94+
auto lc = left->to<IR::ConstructorCallExpression>();
95+
auto rc = right->to<IR::ConstructorCallExpression>();
96+
if (!sameType(lc->constructedType, rc->constructedType))
97+
return false;
98+
return sameExpressions(lc->arguments, rc->arguments);
99+
} else {
100+
BUG("%1%: Unexpected expression", left);
101+
}
102+
}
103+
104+
105+
} // namespace P4
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2016 VMware, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#ifndef _TYPECHECKING_SYNTACTICEQUIVALENCE_H_
18+
#define _TYPECHECKING_SYNTACTICEQUIVALENCE_H_
19+
20+
#include "ir/ir.h"
21+
#include "frontends/p4/typeMap.h"
22+
#include "frontends/common/resolveReferences/referenceMap.h"
23+
24+
namespace P4 {
25+
26+
// Check if two expressions are syntactically equivalent
27+
class SameExpression {
28+
const ReferenceMap* refMap;
29+
const TypeMap* typeMap;
30+
public:
31+
explicit SameExpression(const ReferenceMap* refMap, const TypeMap* typeMap) :
32+
refMap(refMap), typeMap(typeMap) { CHECK_NULL(refMap); CHECK_NULL(typeMap); }
33+
bool sameType(const IR::Type* left, const IR::Type* right) const;
34+
bool sameExpression(const IR::Expression* left, const IR::Expression* right) const;
35+
bool sameExpressions(const IR::Vector<IR::Expression>* left,
36+
const IR::Vector<IR::Expression>* right) const;
37+
};
38+
39+
} // namespace P4
40+
41+
#endif /* _TYPECHECKING_SYNTACTICEQUIVALENCE_H_ */

0 commit comments

Comments
 (0)