How SV Macro get processed?

Hi folks,

I am trying to understand how SV Macro get processed. Here are the two questions:

  1. Why undef cannot be there?
  2. Interestingly different simulators output differently on stringify macro. Any comments?
`define SN _ma
`define NAME_S(A) tom``A
`define NAME `NAME_S(`SN)

`define STRF(PATH) `"PATH`"

//why cannot undef here, I assume that `SN already expand for Macro NAME definition
//`undef SN

module tb;
   string tom_ma = "tom_ma";
   
   initial $display("%s", `NAME);
   initial begin
     //why verdi expand to "tom_ma", VCS print out `NAME_S(_ma) different from other simulators??
     $display("%s", `STRF(`NAME_S(`SN)));
      //expand as expect
     $display("%s", `STRF(`SN));
   end
endmodule

https://www.edaplayground.com/x/PDr3

In reply to mlsxdx:

  1. The LRM says: “The macro text and argument defaults may contain usages of other text macros. Such usages shall be substituted after the outer macro is substituted, not when it is defined”. So the macro SN needs to be defined at the point when it gets expanded my the call to the NAME macro in the module body.
  2. The LRM also says: “An `” overrides the usual lexical meaning of " and indicates that the expansion shall include the quotation mark, substitution of actual arguments, and expansions of embedded macros." So the PATH argument needs to get expanded before it gets encapsulated by the quotes. The correct expansion is “tom_ma”
  3. Your EDAPlayground example has the macro definitions compiled in a separate file from the module declaration that uses them. Some tools treat the two files as separate compilation units and macros defined in one compilation unit are not visible in other compilation units. Typically macros definition files are `included into the compilation unit.

In reply to dave_59:

Thanks for your reply. I read that part earlier but to understand it just until you point out here. That is really helpful for me. :-)

For your last comment above, this same result comes even when the macro defines put/include with the tb.sv.