How to create a string from a pre-processor macro

寵の児 提交于 2019-12-02 19:24:31
dwikle

It's not possible to use a `define macro within a string literal. According to the SystemVerilog LRM:

Macro substitution and argument substitution shall not occur within string literals.

However a string literal can be constructed by using a macro that takes an argument and including the quotes in the macro by using ``"`.

Again, from the LRM:

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.

So this works:

`define STRINGIFY(x) `"x`"
`define HPATH top.chip.block
string hpath = `STRINGIFY(`HPATH);
$display(hpath);                       // Output: "top.chip.block"

The example code can be run here: http://www.edaplayground.com/s/4/879

I think this is what you're looking for.

`define HPATH `"top.chip.block`"
string hpath = `HPATH;

As toolic pointed out, the escape sequence %m will give you the current hierarchy when used in a $display statement so that may be a better option.

I know this is an old thread, but I thought I'd share our solution. The use of the $sformatf allows additional information to be added if needed.

`define STRINGIFY(DEFINE) $sformatf("%0s", `"DEFINE`")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!