0

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

Community
  • 1
  • 1
Zamfir Yonchev
  • 151
  • 1
  • 10

3 Answers3

0

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. This allows string literals to be constructed from macro arguments.

A mixture of `" and " is allowed in the macro text, however the use of " always starts a string literal and must have a terminating ". Any characters embedded inside this string literal, including ", the " starts a string literal whose last charater is is terminated by the " of ".

`define HPATH(signal) `"top.chip.block.signal`"
`define W wire
module a;
  initial begin
    string hpath = `HPATH(wire);
    //string hpath = `HPATH(`W);
    $display("%s",hpath);
  end
endmodule

output : top.chip.block.wire

Hope you understand the context of `" and " (or just a macro definition)

Emman
  • 1,180
  • 1
  • 22
  • 38
  • Well, I still want to use the HPATH macro as a hierarchical path (without quotes). I will revise my question since I narrowed down the issue. – Zamfir Yonchev Sep 16 '15 at 13:23
0

I don't agree with the output you are getting on EDA playground. I am getting the desired output using ModelSim/Questa. Regardless of where the macro argument text is expanded before or after being passed through the outer level macro (The LRM says after) the `" should have allowed the macro that was passed through to be expanded.

dave_59
  • 39,096
  • 3
  • 24
  • 63
0

It seems to be a VCS issue.

When the same code is run on Icarus Verilog 0.10.0 and Riviera-PRO EDU 2015.06 simulators the output is correct.

The issue appears when running with VCS 2014.12 simulator.

Zamfir Yonchev
  • 151
  • 1
  • 10