SystemVerilog Series · SV-25

SystemVerilog Series — SV-25: Compiler Directives — VLSI Trainers
SystemVerilog Series · SV-25

Compiler Directives

Two enhancements to Verilog-2001 compiler directives: the 'define macro gains three new escape tokens for constructing string literals and identifiers from arguments, and the 'include directive gains an angle-bracket form for vendor-defined standard locations.

📋 What Verilog-2001 Provided

Verilog-2001 provided two compiler directives relevant here:

  • ‘define — text-substitution macro with optional arguments. Arguments replace named tokens in the macro body verbatim. A backslash at end-of-line continues the macro body onto the next line.
  • ‘include — file inclusion directive. Accepts only a double-quoted literal filename: 'include "filename".
// Verilog-2001 'define with arguments — unchanged in SV
`define NAND(dval) nand #(dval)
`NAND(3)       i1 (y, a, b);    // expands to: nand #(3) i1 (y, a, b)
`NAND(3:4:5)   i2 (o, c, d);    // expands to: nand #(3:4:5) i2 (o, c, d)

SV adds three new escape sequences inside macro bodies and extends 'include with an angle-bracket form. All existing Verilog-2001 macro behaviour is unchanged.

📋 New ‘define Escape Tokens

SV adds three special tokens that can appear inside a macro body, enabling two new capabilities — building string literals from macro arguments and concatenating tokens to form identifiers.

TokenMeaning inside macro body
`”Becomes a literal " in the expansion — used to wrap macro arguments inside a string literal
`\`”Becomes the escape sequence \" in the expansion — used to embed a quote inside a string literal
Token delimiter with no whitespace — joins two adjacent tokens into one, used to build identifiers from argument fragments

📋 Backtick-Quote — Building String Literals from Arguments

Placing `" at the beginning and end of a section of the macro body makes the argument values appear inside a string literal in the expansion. Without `", a macro argument is substituted as raw tokens — not wrapped in quotes.

Without `” — argument as raw token

// Verilog-2001 style
`define show(x) $display(x)
`show(hello)
// expands to: $display(hello)
// ERROR: hello is an identifier, not a string

With `” — argument inside a string

// SV style — `" wraps the argument
`define show(x) $display(`"x`")
`show(hello)
// expands to: $display("hello")
// OK: hello is now a string literal

📋 Backtick-Backslash-Backtick-Quote — Embedded Escaped Quotes

To include a literal escaped quote (\") inside a string being built by the macro, use the sequence `\`". This lets you construct strings with embedded double-quote characters.

// Build a string that includes inner quoted text
// Macro: `define msg(x,y) `"x: `\`"y`\`"`"
`define msg(x,y) `"x: `\`"y`\`"`"

// Call:
$display(`msg(left side, right side));

// Expands to:
$display("left side: \"right side\"");

// Output when executed:
// left side: "right side"
Reading the token sequence: inside the macro body, `" starts the string, the argument x substitutes as plain text, the literal colon and space are included verbatim, `\`" inserts \" (an escaped quote into the output string), the argument y substitutes, another `\`" closes the inner quoted section, and the final `" ends the string. The result is a properly-escaped C-style string.

📋 Double-Backtick — Token Concatenation

Two adjacent backticks (``) in a macro body act as a token-boundary marker without introducing any whitespace. This lets you build a single identifier by concatenating a macro argument with fixed text.

// Build a signal name by appending a suffix to the argument
`define foo(f) f``_suffix

`foo(bar)
// expands to: bar_suffix  (one identifier, no space)

// Without ``:
`define foo2(f) f_suffix
`foo2(bar)
// expands to: bar _suffix  (TWO tokens with whitespace — probably wrong)

// Practical use: generate unique instance names
`define MAKE_FF(name) ff_d name``_inst (.d(name``_d), .q(name``_q), .clk(clk));
`MAKE_FF(data)
// expands to: ff_d data_inst (.d(data_d), .q(data_q), .clk(clk));
“ separates tokens — it does not insert a space. In a Verilog/SV lexer, identifiers and keywords must be separated by whitespace or a non-identifier character. Without ``, concatenating a macro argument with adjacent text would result in two tokens separated by implicit whitespace. With ``, the lexer sees a single compound token.

📋 Including a File via a Macro

The 'include directive can now be followed by a macro expansion instead of a literal string, as long as the macro expands to a double-quoted filename.

// Define a macro that expands to a quoted path
`define home(filename) `"/home/foo/filename`"

// Use the macro with 'include
`include `home(myfile)
// expands 'include to:  'include "/home/foo/myfile"
The macro must expand to a double-quoted string. When the macro argument to 'include is expanded, the result must be of the form "filename". An angle-bracket form cannot be produced by macro expansion — it can only be written directly in source.

📋 ‘include with Angle Brackets

SV adds a second form of the 'include directive using angle brackets, modelled after the C language #include <file> convention. It searches only the vendor-defined location where standard library files are installed, rather than the user’s project file paths.

// Double-quote form (Verilog-2001, unchanged):
// Searches project include paths (tool-specific order)
`include "my_header.svh"

// Angle-bracket form (new in SV):
// Searches only the vendor-defined standard location
`include <sv_standard_lib.svh>

// Relative paths inside <> are relative to the vendor location, not the project
`include <ieee/uvm_pkg.svh>
FormWhere it searchesAbsolute path?
‘include “filename”Project include paths (unchanged from Verilog-2001)Only double-quote form supported for absolute paths
‘include <filename>Vendor-defined standard-file location onlyNo — relative to vendor location
Absolute paths can only use the double-quote form. When a filename is an absolute path (starts with / or a drive letter), only 'include "..." is valid. The angle-bracket form is exclusively for standard-location relative paths.

📋 Quick Reference

Token / FormWhat it doesExample
`”Starts or ends a string literal in macro expansion`define str(x) `"x`"
`\`”Inserts an escaped quote \" into the expansion stringBuilds strings with embedded quotes
Concatenates adjacent tokens without whitespace`define sig(n) n``_qdata_q
‘include `macro(…)Include file using a macro-expanded path'include `home(myfile)
‘include <file>Include from vendor-defined standard location only'include <uvm_pkg.svh>
Coming next: SV-26 covers Features Under Consideration for Removal — why defparam and procedural assign/deassign are deprecated, what problems they cause, and the preferred alternatives.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top