使用 $bits 时定义宏中的编译错误

Compile error in define macro when using $bits

在编写以下宏时,出现编译错误:

`define TEST1(out, in, sel)      \ \
  integer i;                      \ \
  always_comb begin                  \ \
    out = $bits(1'b0);               \

    for(i=0; i<=$bits(sel); i=i+1)   \
        out = out | ({$bits(out){sel[i]}} & in[i]);    \
    end          ;

错误消息:

near "[": syntax error, unexpected '[', expecting ',' or '}'.

去掉空行,每行只用一个反斜杠。请参阅 IEEE Std 1800-2017,第 22.5.1 节 `define:

If more than one line is necessary to specify the text, the newline character shall be preceded by a backslash ( \ ). The first newline character not preceded by a backslash shall end the macro text.

`define TEST1(out, in, sel)      \
  integer i;                      \
  always_comb begin                  \
    out = $bits(1'b0);               \
    for(i=0; i<=$bits(sel); i=i+1)   \
        out = out | ({$bits(out){sel[i]}} & in[i]);    \
    end          ;