The life of a SystemVerilog variable
Named scopes and variable lifetimes
What is a lifetime?
In software programming, lifetime is defined by when and from where a variable is available for access. This is particularly important when there are multiple process threads with lifetimes of their own trying to access the same variable. A variable becomes a symbolic name for a particular range of memory locations allocated to a specific data type.
The most basic form of a variable’s lifetime is the kind from hardware design where everything always exists. This is what’s called a static lifetime.
All other forms of variable lifetimes involve the execution of procedural code which involves a deeper understanding of scopes, like those introduced by tasks and functions and other named blocks. As SystemVerilog is intended for both design and verification, it must blend hardware and software concepts together within the same language. This becomes especially important as verification testbenches typically cross probe into variables within the design. Understanding when variables get initialized and when they are accessible from other scopes becomes critical.
This paper shows various interactions between named scopes and variable lifetimes and provides guidance on how different data types allocate memory and consequences for choosing one type over another. We share several examples of coding errors due to misunderstanding variable lifetimes and tips to avoid them.