Everywhere you look, Verilog and SV resemble - but seldom accurately mimic - C.I would like the behavior to match the C preprocessor, but did not mean to imply that what I want was already LRM specified.
## is described as "token gluing". In SV, ``
is described as a non-white token delimiter.Although outer-to-inner order of actual/formal substitutions is indicated, the role of ``The LRM says outer levels are always expanded first, so using CONCAT or not shouldn't matter.
I misspoke here - LRM does say thatThe LRM is not specific about whether the (first) actual argument (to `CONCAT) below should be expanded before it is substituted or after.
`CNT expands
only after substitution.``.
Here's an example to disect further. The VALUEb one seems
to fail on (I suspect) every simulator.
`define ZERO_0 "0"
`define CONCAT(a, b) a``b
`define NOTNOT(a) a
`define VALUEa(a) `CONCAT(`ZERO_,a)
`define VALUEb(a) `CONCAT(`ZERO_,`NOTNOT(a))
module t;
initial begin
`define CNT 0
// Works on at least one
$write("EXP: '0' GOT '"); $write(`VALUEa(`CNT)); $write("'\n");
// Fails on the one that DOES work
$write("EXP: '0' GOT '"); $write(`VALUEb(`CNT)); $write("'\n");
end
endmodule
Follwing the `VALUEb expansion, I would like the LRM to require:
`VALUEb(`CNT) // LRM: outer first
`CONCAT(`ZERO_,`NOTNOT(`CNT)) // LRM: outer first
`ZERO_```NOTNOT(`CNT) // See below
`ZERO_``CNT // Same rule as above
The three possibilities I see are`ZERO_```CNT ,`ZERO_`NOTNOT(`CNT) , or*** Undefined macro `ZERO_ ***.`` cannot delimit tokens indefinitely; it should vanish
along with the formal argument(s) it conjoined.`` is "allowing
identifiers to be constructed from arguments."
`VALUEb(`CNT) // LRM: outer first
`CONCAT(`ZERO_,`NOTNOT(`CNT)) // LRM: outer first
`ZERO_```NOTNOT(`CNT) // `` collapses without whitespace
`ZERO_`NOTNOT(`CNT) // NOTNOT expands and adds whitespace
`ZERO_ `CNT // Extra space now in the token
`ZERO_ 0 // Extra space now in the token
[Does anyone know a workaround to get the behavior I would
like?]
Yes - parrot the LRM examples and write:
`define REPEAT(n, d) `REPEAT_``n(d)But how does your proposal permit your example to "work"?The question is what does `` imply as to expanding any macro on its LHS or RHS? I think the LRM is silent on this, but propse that `` occurs after complete expansions macros on either side of it. (Note the alternative, that `` occurs "first" results in the extra space as shown above, which I don't see how would be useful?)
`` are `ZERO_
and `CNT , one ofI ran that idea through our regression suite a year or two ago and found thatI would also propose the LRM to say that `` collapses whitespace on both sides to match C99. It now says "without introducing white space" which is not sufficient if a space was mis-introduced into a define value, possibly from an earlier substitution.
`` delimits" to mean`` works something like quotation marks.``formal``_suffix.`` was the ##
token-gluing operator. It's not.`define a glue
operator to packageThis archive was generated by hypermail 2.1.8 : Tue Jun 22 2010 - 23:18:34 PDT