google / xls

XLS: Accelerated HW Synthesis
http://google.github.io/xls/
Apache License 2.0
1.21k stars 179 forks source link

Struct member defaults #1696

Open mikex-oss opened 2 weeks ago

mikex-oss commented 2 weeks ago

What's hard to do? (limit 100 words)

Sometimes you want to specify struct defaults within the struct definition itself.

C++ allows this, e.g.

struct MyStruct
{
    int x;
    int y {};
    int z { 2 };
};

So does SystemVerilog:

  typedef struct {
    logic[31:0] x;
    logic[31:0] y = '0;
    logic[31:0] z = 'd2;
  } my_struct_t;

Rust does not, though see discussion in https://github.com/rust-lang/rfcs/pull/1806.

Current best alternative workaround (limit 100 words)

zero!<MyStruct>() works when everything can be zero-initialized. Otherwise, you can do something like (maybe even later attached to the struct with impl:

fn new_my_struct(x: u32) -> MyStruct {
    MyStruct {
        x: x
        y: u32:0,
        z: u32:2,
    }
}

which is sorta like implementing the Default trait in Rust in that you need to write a separate function that specifies the defaults.

Your view of the "best case XLS enhancement" (limit 100 words)

Even though the Rust RFC languished with mixed opinions, it'd be nice to support this in DSLX for readability and conciseness.