Backticks - ``, `", `\`"¶
There are 3 tokens that need some explanation
Backtick and Quotes¶
`" - 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
- Include the double quotes
- The arguments within the double-quotes should be substituted and
- Any embedded macros should be expanded.
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 -MODand".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 argumentMODhas to be substituted when the macro is expanded.
Double Backtick¶
`` - (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._2auses a “period” between the argumentMODand the stringmaster._2buses a “space” between argumentMODand stringmaster.`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 stringMODends.In
`append_front_2c_goodthe double-backtick``creates a delimiter betweenMODand_masterand that’s whyMODis correctly identified and substituted.`append_middleand`append_endare 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.
Slashes, Ticks and Quotes¶
`\`" - 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.