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

Inconsistent behavior with casts involving tuples / headers #529

Closed
jnfoster opened this issue Apr 26, 2017 · 5 comments
Closed

Inconsistent behavior with casts involving tuples / headers #529

jnfoster opened this issue Apr 26, 2017 · 5 comments
Labels
bug This behavior is unintended and should be fixed. fixed This topic is considered to be fixed.

Comments

@jnfoster
Copy link
Contributor

jnfoster commented Apr 26, 2017

Compiling the following program

#include <v1model.p4>

header h_t {
  bit<8> f;
}

struct my_packet {
  h_t h;
}

struct my_metadata { }

parser MyParser(packet_in b, out my_packet p, inout my_metadata m, inout standard_metadata_t s) {
  state start {
   transition accept;
  }
}

control MyVerifyChecksum(in my_packet hdr, inout my_metadata meta) {
  apply { }
}


control MyIngress(inout my_packet p, inout my_metadata m, inout standard_metadata_t s) {
  apply {
    h_t h1 = p.h;              // ok
    h_t h2 = (h_t) p.h;        // ok
    h_t h3 = { p.h.f };        // ok
    h_t h4 = (h_t) { p.h.f };  // fails
  }
}

control MyEgress(inout my_packet p, inout my_metadata m, inout standard_metadata_t s) {
  apply { }
}

control MyComputeChecksum(inout my_packet p, inout my_metadata m) {
  apply { }
}

control MyDeparser(packet_out b, in my_packet p) {
  apply { }
}

V1Switch(MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser()) main;

Produces an error:

/home/ubuntu/min.p4(29): error: cast: Illegal cast from Tuple(1) to header h_t
    h_t h4 = (h_t) { p.h.f };
             ^^^^^^^^^^^^^^^
/home/ubuntu/min.p4(28)
    h_t h3 = { p.h.f };
             ^^^^^^^^^
/home/ubuntu/min.p4(3)
header h_t {
       ^^^

It seems like these should all work...

In addition, reporting the tuple on line 28 as an error is wrong. It has nothing to do with the declaration involving h4.

@jnfoster jnfoster changed the title Inconsistent cast behavior with tuples / headers Inconsistent behavior with casts involving tuples / headers Apr 26, 2017
@jnfoster
Copy link
Contributor Author

jnfoster commented Apr 26, 2017

Some more examples:

    struct bs { bool y; bool z; }
    ...
    bool b = true;
    bit<1> c = 0;
    bool d = false;     
    // bs x = { b, c };    // fails
    bs x = { b, (bool) c}; // ok
    bs y = { b, d };       // ok

@mihaibudiu
Copy link
Contributor

What is bs?

@jnfoster
Copy link
Contributor Author

Sorry, just a pair of bools.

@jnfoster
Copy link
Contributor Author

Further, constant propagation seems inconsistent:

    const bool b = true;
    const bit<1> c = 0;
    const bool d = false;     
    bs x = { b, (bool) c}; // ok
    bs y = { b, d };       // ok

becomes

        const bool b = true;
        const bit<1> c = 1w0;
        const bool d = false;
        bs x = { b, false };
        bs y = { b, d };

after the first constant folding pass.

@mihaibudiu mihaibudiu added the bug This behavior is unintended and should be fixed. label Apr 27, 2017
mihaibudiu pushed a commit to mihaibudiu/p4c-clone that referenced this issue May 9, 2017
@mihaibudiu mihaibudiu added the fixed This topic is considered to be fixed. label May 9, 2017
jnfoster pushed a commit that referenced this issue May 12, 2017
* Fix for issue #529

* Updated reference outputs
@jnfoster
Copy link
Contributor Author

Closed by #593

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This behavior is unintended and should be fixed. fixed This topic is considered to be fixed.
Projects
None yet
Development

No branches or pull requests

2 participants