How to create a string from a pre-processor macro

前端 未结 3 1507
臣服心动
臣服心动 2021-02-01 23:46

I have a preprocessor macro that represents a hierarchical path into my design.

Example:

`define HPATH top.chip.block

I need to constru

相关标签:
3条回答
  • 2021-02-01 23:54

    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.

    0 讨论(0)
  • 2021-02-01 23:55

    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

    0 讨论(0)
  • 2021-02-02 00:17

    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`")
    
    0 讨论(0)
提交回复
热议问题