``, `", `\`"¶There are 3 tokens that need some explanation
`" - If the macrotext is enclosed in plain quotation("), it essentially becomes a string. Arguments within the double-quotes are not substituted and if the macrotext has other macros embedded, they are not expanded. The `" (back-tick before the double-quote) overrides the meaning of " and indicates that the macro expansion should
Let’s look at example 1.1
Example 1.1
/* Example 1.1 */
`define append_front_bad(MOD) "MOD.master"
`define append_front_good(MOD) `"MOD.master`"
program automatic test;
initial begin
$display(`append_front_bad(clock1));
$display(`append_front_good(clock1));
end
endprogram: test
CONSOLE OUTPUT
# MOD.master
# clock1.master
`append_front_bad - accepts a argument, MOD, and the expectation is that the macro will result in concatenation of 2 strings - MOD and ".master". But because of the double quotes the output results in the string "MOD.master" and the argument is not substituted.
`append_front_good - This is the correct version of `append_front_bad. By using the back-tick before the double-quotes, we tell the compiler that the argument MOD has to be substituted when the macro is expanded.
`` - (The double-backtick) is essentially a token delimiter. It helps the compiler clearly differentiate between the Argument and the rest of the string in the macro text. This is best explained with an example.
Example 1.2
/* Example 1.2 */
`define append_front_2a(MOD) `"MOD.master`"
`define append_front_2b(MOD) `"MOD master`"
`define append_front_2c_bad(MOD) `"MOD_master`"
`define append_front_2c_good(MOD) `"MOD``_master`"
`define append_middle(MOD) `"top_``MOD``_master`"
`define append_end(MOD) `"top_``MOD`"
`define append_front_3(MOD) `"MOD``.master`"
program automatic test;
initial begin
/* Example 1.2 */
$display(`append_front_2a(clock2a));
$display(`append_front_2b(clock2b));
$display(`append_front_2c_bad(clock2c));
$display(`append_front_2c_good(clock2c));
$display(`append_front_3(clock3));
$display(`append_middle(clock4));
$display(`append_end(clock5));
end
endprogram: test
CONSOLE OUTPUT
# clock2a.master
# clock2b master
# MOD_master
# clock2c_master
# clock3.master
# top_clock4_master
# top_clock5
`append_front_2a and 2b - Space and period are natural token delimiters, i.e., the compiler can clearly identify the argument in the macro text. _2a uses a “period” between the argument MOD and the string master. _2b uses a “space” between argument MOD and string master.
`append_front_2c_bad - If the argument has a non-space or non-period character attached to it, such as the underscore(_) in `"MOD_master`", then the compiler can’t figure out where the argument string MOD ends.
In `append_front_2c_good the double-backtick `` creates a delimiter between MOD and _master and that’s why MOD is correctly identified and substituted. `append_middle and `append_end are 2 more examples that show this.
`append_front_3 - It is ok to place a `` before or after a natural token delimiter (i.e., space or period) but it will have no effect.
`\`" - This results in a literal \" when the macro is expanded. Use this if you need double quotes within your expanded macro text.
Example 1.3
/* Example 1.3 */
`define complex_string(ARG1, ARG2) \
`"This `\`"Blue``ARG1`\`" is Really `\`"ARG2`\`"`"
program automatic test;
initial begin
$display(`complex_string(Beast, Black));
end
endprogram: test
CONSOLE OUTPUT
# This "BlueBeast" is Really "Black"
If you still feel a little shaky on this concept, don’t worry! … We’ll examine a bunch of more examples further down this article and that should clear things up.