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

Fix for issue #513: better error message #518

Merged
merged 3 commits into from
Apr 25, 2017
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
14 changes: 10 additions & 4 deletions midend/predication.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ limitations under the License.
#include "ir/ir.h"
#include "frontends/p4/typeChecking/typeChecker.h"

// Converts if statements to ?: in actions, if possible
// For this to work all statements must be assignments or other ifs.
namespace P4 {

/*
/**

This pass operates on action bodies. It converts 'if' statements to
'?:' expressions, if possible. Otherwise this pass will signal an
error. This pass should be used only on architectures that do not
support conditionals in actions.

For this to work all statements must be assignments or other ifs.

if (e)
a = f(b);
Expand Down Expand Up @@ -65,7 +70,8 @@ class Predication final : public Transform {
return new IR::PathExpression(IR::ID(predicateName.back())); }
const IR::Statement* error(const IR::Statement* statement) const {
if (inside_action && ifNestingLevel > 0)
::error("%1%: Predication cannot be applied", statement);
::error("%1%: Conditional execution in actions is not supported on this target",
statement);
return statement;
}

Expand Down
95 changes: 95 additions & 0 deletions testdata/p4_16_errors/issue513.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2017 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <core.p4>
#include <v1model.p4>

typedef bit<48> EthernetAddress;

header Ethernet_h {
EthernetAddress dstAddr;
EthernetAddress srcAddr;
bit<16> etherType;
}

struct Parsed_packet {
Ethernet_h ethernet;
}

struct mystruct1 {
bit<4> a;
bit<4> b;
}

control DeparserI(packet_out packet,
in Parsed_packet hdr) {
apply { packet.emit(hdr.ethernet); }
}

parser parserI(packet_in pkt,
out Parsed_packet hdr,
inout mystruct1 meta,
inout standard_metadata_t stdmeta) {
state start {
pkt.extract(hdr.ethernet);
transition accept;
}
}

control cIngress(inout Parsed_packet hdr,
inout mystruct1 meta,
inout standard_metadata_t stdmeta) {
action foo() {
meta.b = meta.b + 5;
if (meta.b > 10) {
// Next line causes error for p4test: MethodCallStatement:
// Predication cannot be applied.
mark_to_drop();
}
}
table guh {
key = { hdr.ethernet.srcAddr : exact; }
actions = { foo; }
default_action = foo;
}

apply {
guh.apply();
}
}

control cEgress(inout Parsed_packet hdr,
inout mystruct1 meta,
inout standard_metadata_t stdmeta) {
apply { }
}

control vc(in Parsed_packet hdr,
inout mystruct1 meta) {
apply { }
}

control uc(inout Parsed_packet hdr,
inout mystruct1 meta) {
apply { }
}

V1Switch<Parsed_packet, mystruct1>(parserI(),
vc(),
cIngress(),
cEgress(),
uc(),
DeparserI()) main;
68 changes: 68 additions & 0 deletions testdata/p4_16_errors_outputs/issue513-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <core.p4>
#include <v1model.p4>

typedef bit<48> EthernetAddress;
header Ethernet_h {
EthernetAddress dstAddr;
EthernetAddress srcAddr;
bit<16> etherType;
}

struct Parsed_packet {
Ethernet_h ethernet;
}

struct mystruct1 {
bit<4> a;
bit<4> b;
}

control DeparserI(packet_out packet, in Parsed_packet hdr) {
apply {
packet.emit<Ethernet_h>(hdr.ethernet);
}
}

parser parserI(packet_in pkt, out Parsed_packet hdr, inout mystruct1 meta, inout standard_metadata_t stdmeta) {
state start {
pkt.extract<Ethernet_h>(hdr.ethernet);
transition accept;
}
}

control cIngress(inout Parsed_packet hdr, inout mystruct1 meta, inout standard_metadata_t stdmeta) {
@name("foo") action foo_0() {
meta.b = meta.b + 4w5;
if (meta.b > 4w10)
mark_to_drop();
}
@name("guh") table guh_0 {
key = {
hdr.ethernet.srcAddr: exact @name("hdr.ethernet.srcAddr") ;
}
actions = {
foo_0();
}
default_action = foo_0();
}
apply {
guh_0.apply();
}
}

control cEgress(inout Parsed_packet hdr, inout mystruct1 meta, inout standard_metadata_t stdmeta) {
apply {
}
}

control vc(in Parsed_packet hdr, inout mystruct1 meta) {
apply {
}
}

control uc(inout Parsed_packet hdr, inout mystruct1 meta) {
apply {
}
}

V1Switch<Parsed_packet, mystruct1>(parserI(), vc(), cIngress(), cEgress(), uc(), DeparserI()) main;
69 changes: 69 additions & 0 deletions testdata/p4_16_errors_outputs/issue513.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <core.p4>
#include <v1model.p4>

typedef bit<48> EthernetAddress;
header Ethernet_h {
EthernetAddress dstAddr;
EthernetAddress srcAddr;
bit<16> etherType;
}

struct Parsed_packet {
Ethernet_h ethernet;
}

struct mystruct1 {
bit<4> a;
bit<4> b;
}

control DeparserI(packet_out packet, in Parsed_packet hdr) {
apply {
packet.emit(hdr.ethernet);
}
}

parser parserI(packet_in pkt, out Parsed_packet hdr, inout mystruct1 meta, inout standard_metadata_t stdmeta) {
state start {
pkt.extract(hdr.ethernet);
transition accept;
}
}

control cIngress(inout Parsed_packet hdr, inout mystruct1 meta, inout standard_metadata_t stdmeta) {
action foo() {
meta.b = meta.b + 5;
if (meta.b > 10) {
mark_to_drop();
}
}
table guh {
key = {
hdr.ethernet.srcAddr: exact;
}
actions = {
foo;
}
default_action = foo;
}
apply {
guh.apply();
}
}

control cEgress(inout Parsed_packet hdr, inout mystruct1 meta, inout standard_metadata_t stdmeta) {
apply {
}
}

control vc(in Parsed_packet hdr, inout mystruct1 meta) {
apply {
}
}

control uc(inout Parsed_packet hdr, inout mystruct1 meta) {
apply {
}
}

V1Switch<Parsed_packet, mystruct1>(parserI(), vc(), cIngress(), cEgress(), uc(), DeparserI()) main;
3 changes: 3 additions & 0 deletions testdata/p4_16_errors_outputs/issue513.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
../testdata/p4_16_errors/issue513.p4(60): error: MethodCallStatement: Conditional execution in actions is not supported on this target
mark_to_drop();
^^^^^^^^^^^^^^