Skip to content

Commit

Permalink
Fix for issue p4lang#532
Browse files Browse the repository at this point in the history
  • Loading branch information
mbudiu-vmw committed Apr 26, 2017
1 parent a6eaf28 commit 1c88f05
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 8 deletions.
17 changes: 9 additions & 8 deletions frontends/p4/sideEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ struct EvaluationOrder {
}

const IR::Expression* addAssignment(
Util::SourceInfo srcInfo,
cstring varName, const IR::Expression* expression) {
auto left = new IR::PathExpression(IR::ID(varName, nullptr));
auto stat = new IR::AssignmentStatement(left, expression);
auto stat = new IR::AssignmentStatement(srcInfo, left, expression);
statements->push_back(stat);
auto result = left->clone();
return result;
Expand Down Expand Up @@ -106,7 +107,7 @@ class DismantleExpression : public Transform {
if (!right->is<IR::Constant>()) {
auto indexType = typeMap->getType(expression->right, true);
auto tmp = result->createTemporary(indexType);
right = result->addAssignment(tmp, right);
right = result->addAssignment(expression->srcInfo, tmp, right);
typeMap->setType(right, indexType);
}

Expand Down Expand Up @@ -192,7 +193,7 @@ class DismantleExpression : public Transform {
clone->right = right;
typeMap->setType(clone, type);
auto tmp = result->createTemporary(type);
auto path = result->addAssignment(tmp, clone);
auto path = result->addAssignment(expression->srcInfo, tmp, clone);
result->final = path;
}
typeMap->setType(result->final, type);
Expand Down Expand Up @@ -220,21 +221,21 @@ class DismantleExpression : public Transform {
bool land = expression->is<IR::LAnd>();
auto constant = new IR::BoolLiteral(!land);
auto tmp = result->createTemporary(type);
auto ifTrue = new IR::AssignmentStatement(
auto ifTrue = new IR::AssignmentStatement(expression->srcInfo,
new IR::PathExpression(IR::ID(tmp, nullptr)), constant);
auto ifFalse = new IR::IndexedVector<IR::StatOrDecl>();

auto save = result->statements;
result->statements = ifFalse;
visit(expression->right);
auto path = result->addAssignment(tmp, result->final);
auto path = result->addAssignment(expression->srcInfo, tmp, result->final);
result->statements = save;
if (land) {
cond = new IR::LNot(cond);
typeMap->setType(cond, type);
}
auto block = new IR::BlockStatement(*ifFalse);
auto ifStatement = new IR::IfStatement(cond, ifTrue, block);
auto ifStatement = new IR::IfStatement(expression->srcInfo, cond, ifTrue, block);
result->statements->push_back(ifStatement);
result->final = path->clone();
}
Expand All @@ -256,12 +257,12 @@ class DismantleExpression : public Transform {
auto ifTrue = new IR::IndexedVector<IR::StatOrDecl>();
result->statements = ifTrue;
visit(expression->e1);
(void)result->addAssignment(tmp, result->final);
(void)result->addAssignment(expression->srcInfo, tmp, result->final);

auto ifFalse = new IR::IndexedVector<IR::StatOrDecl>();
result->statements = ifFalse;
visit(expression->e2);
auto path = result->addAssignment(tmp, result->final);
auto path = result->addAssignment(expression->srcInfo, tmp, result->final);
result->statements = save;

auto ifStatement = new IR::IfStatement(
Expand Down
6 changes: 6 additions & 0 deletions midend/copyStructures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ namespace P4 {
const IR::Node* DoCopyStructures::postorder(IR::AssignmentStatement* statement) {
auto ltype = typeMap->getType(statement->left, true);
if (ltype->is<IR::Type_StructLike>()) {
if (statement->right->is<IR::MethodCallExpression>()) {
::error("%1%: functions or methods returning structures are not supported on this target",
statement->right);
return statement;
}

auto retval = new IR::Vector<IR::StatOrDecl>();
auto strct = ltype->to<IR::Type_StructLike>();
if (statement->right->is<IR::ListExpression>()) {
Expand Down
68 changes: 68 additions & 0 deletions testdata/p4_16_errors/issue532.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <core.p4>
#include <v1model.p4>

struct s1_t {
bit<8> f8;
}

struct choices_t {
s1_t entry0;
s1_t entry1;
s1_t entry2;
s1_t entry3;
}

struct my_meta_t {
s1_t entry;
}

struct parsed_packet_t { }

parser parse(packet_in pk, out parsed_packet_t hdr,
inout my_meta_t my_metadata,
inout standard_metadata_t standard_metadata) {
state start {
transition accept;
}
}

extern s1_t choose_entry(in choices_t choices);

control ingress (inout parsed_packet_t hdr,
inout my_meta_t my_meta,
inout standard_metadata_t standard_metadata) {
action select_entry(choices_t choices) {
my_meta.entry = choose_entry(choices);
}
table t {
actions = {
select_entry; NoAction;
}
const default_action = NoAction();
}

apply { t.apply(); }
}

control egress(inout parsed_packet_t hdr,
inout my_meta_t my_meta,
inout standard_metadata_t standard_metadata) {
apply { }
}

control deparser(packet_out b, in parsed_packet_t hdr) {
apply { }
}

control verify_checksum(in parsed_packet_t hdr,
inout my_meta_t my_meta) {
apply { }
}

control compute_checksum(inout parsed_packet_t hdr,
inout my_meta_t my_meta) {
apply { }
}

V1Switch(parse(), verify_checksum(), ingress(), egress(),
compute_checksum(), deparser()) main;
67 changes: 67 additions & 0 deletions testdata/p4_16_errors_outputs/issue532-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <core.p4>
#include <v1model.p4>

struct s1_t {
bit<8> f8;
}

struct choices_t {
s1_t entry0;
s1_t entry1;
s1_t entry2;
s1_t entry3;
}

struct my_meta_t {
s1_t entry;
}

struct parsed_packet_t {
}

parser parse(packet_in pk, out parsed_packet_t hdr, inout my_meta_t my_metadata, inout standard_metadata_t standard_metadata) {
state start {
transition accept;
}
}

extern s1_t choose_entry(in choices_t choices);
control ingress(inout parsed_packet_t hdr, inout my_meta_t my_meta, inout standard_metadata_t standard_metadata) {
s1_t tmp;
@name("select_entry") action select_entry_0(choices_t choices) {
tmp = choose_entry(choices);
my_meta.entry = tmp;
}
@name("t") table t_0 {
actions = {
select_entry_0();
NoAction();
}
const default_action = NoAction();
}
apply {
t_0.apply();
}
}

control egress(inout parsed_packet_t hdr, inout my_meta_t my_meta, inout standard_metadata_t standard_metadata) {
apply {
}
}

control deparser(packet_out b, in parsed_packet_t hdr) {
apply {
}
}

control verify_checksum(in parsed_packet_t hdr, inout my_meta_t my_meta) {
apply {
}
}

control compute_checksum(inout parsed_packet_t hdr, inout my_meta_t my_meta) {
apply {
}
}

V1Switch<parsed_packet_t, my_meta_t>(parse(), verify_checksum(), ingress(), egress(), compute_checksum(), deparser()) main;
65 changes: 65 additions & 0 deletions testdata/p4_16_errors_outputs/issue532.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <core.p4>
#include <v1model.p4>

struct s1_t {
bit<8> f8;
}

struct choices_t {
s1_t entry0;
s1_t entry1;
s1_t entry2;
s1_t entry3;
}

struct my_meta_t {
s1_t entry;
}

struct parsed_packet_t {
}

parser parse(packet_in pk, out parsed_packet_t hdr, inout my_meta_t my_metadata, inout standard_metadata_t standard_metadata) {
state start {
transition accept;
}
}

extern s1_t choose_entry(in choices_t choices);
control ingress(inout parsed_packet_t hdr, inout my_meta_t my_meta, inout standard_metadata_t standard_metadata) {
action select_entry(choices_t choices) {
my_meta.entry = choose_entry(choices);
}
table t {
actions = {
select_entry;
NoAction;
}
const default_action = NoAction();
}
apply {
t.apply();
}
}

control egress(inout parsed_packet_t hdr, inout my_meta_t my_meta, inout standard_metadata_t standard_metadata) {
apply {
}
}

control deparser(packet_out b, in parsed_packet_t hdr) {
apply {
}
}

control verify_checksum(in parsed_packet_t hdr, inout my_meta_t my_meta) {
apply {
}
}

control compute_checksum(inout parsed_packet_t hdr, inout my_meta_t my_meta) {
apply {
}
}

V1Switch(parse(), verify_checksum(), ingress(), egress(), compute_checksum(), deparser()) main;
3 changes: 3 additions & 0 deletions testdata/p4_16_errors_outputs/issue532.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
../testdata/p4_16_errors/issue532.p4(35): error: choose_entry: functions or methods returning structures are not supported on this target
my_meta.entry = choose_entry(choices);
^^^^^^^^^^^^^^^^^^^^^

0 comments on commit 1c88f05

Please sign in to comment.