I'm trying to stringify a macro in System Verilog, so I can use it in a string for printing.
I created a STRINGIFY macro:
`define STRINGIFY(x) `"x`"
as suggested here: How to create a string from a pre-processor macro
However, the macro I'm trying to stringify takes arguments.
Consider the following code:
`define STRINGIFY(x) `"x`"
`define HPATH(signal) top.chip.block.signal
string hpath = `STRINGIFY(`HPATH(wire));
$display(hpath); // Output: "`HPATH(wire)"
I want the output to be
"top.chip.block.wire"
instead.
Notice that the HPATH
macro takes an argument wire
.
This is the example code run here: http://www.edaplayground.com/x/CKB
EDIT: The problem is that the `STRINGIFY macro stops the expansion of the macro inside when the macro inside has arguments. For example:
`define STRINGIFY(x) `"x`"
`define HPATH top.chip.block
`define HPATH_SIGNAL(signal) top.chip.block.signal
$display(`"`HPATH`"); // correctly outputs "top.chip.block"
$display(`STRINGIFY(`HPATH)); // correctly outputs "top.chip.block"
$display(`"`HPATH_SIGNAL(sig)`"); // correctly outputs "top.chip.block.sig"
$display(`STRINGIFY(`HPATH_SIGNAL(sig))); // incorrectly outputs "`HPATH_SIGNAL(sig)"
It seems that the `HPATH_SIGNAL(sig) is not resolved when nested inside the `STRINGIFY macro
The question is why isn't the HPATH_SIGNAL(sig) expanded on the last line?
Here's the example code run: http://www.edaplayground.com/x/RF2