After reading LRM sections 23.7–23.9, I have a few questions about the concept of hierarchical references.
Q1.
int i = 8;
module tb;
initial begin: init_blk
$display("i: %0d", i);
end
endmodule
According to LRM Chapter 23.9: “If an identifier is referenced directly (without a hierarchical path)… If the item is a variable, it shall stop at a module boundary.”
So the search should stop once it reaches module tb. But in practice, the variable i can still be referenced. What is meant by “module boundary” in this context?
Q2.
interface intf;
int i = 8;
endinterface
module tb;
initial begin: init_blk
$display("i: %0d", intf.i);
end
endmodule
According to LRM Chapter 23.9: “If an identifier is referenced with a hierarchical name, the path can start with a module name, interface name…”.
In my case, I used the interface name intf, but the compiler still reports an error.
Q3.
Based on the rules and examples in LRM Chapter 23.8 Upwards Name Referencing, it appears that scope_name in a hierarchical reference can be a module name only when it refers to the module containing the statement. In all other cases, only instance names are allowed, and item_name always refers to an instance name. Is my understanding of this correct?
Also, based on my understanding, the search first proceeds in the declaration scope until it reaches the design element scope, and then it continues in the scope where the module is instantiated. Does this mean the lookup alternates between the declaration scope and the instantiation scope?
This section of the LRM has several issues, some of which are listed below.
A1: The declaration in i is in the compilation unit ($unit), which is treated like an implicit package import from the compilation unit. See Section. 3.12.1 Compilation unit. Other sections of the LRM were never updated to deal with this functionality.
A2: You need to read the sentence that follows the one you quoted. The names shall be searched first at the current level and then in higher level modules until found.
In your example, tb at the highest level. Modules and interfaces are not part of $unit
A3. No, scope_name is always a module (or interface) name in either the module in which the reference appears or at an upper hierarchical level. item_name is almost any identifier you’ve declared except for type names.
Thank you, Dave. I have some new questions regarding your explanation.
Q1.
module harness;
int i = 8;
initial $display("i: %0d", u_harness.i);
endmodule
module tb;
harness u_harness ();
endmodule
You mentioned that “scope_name is always a module (or interface) name in either the module in which the reference appears or at an upper hierarchical level.” However, in the code above, u_harness is an instance name, yet it can still be resolved correctly. Or does the tool implicitly prepend the module name tb in front of u_harness, so that it is equivalent to tb.u_harness.i?
My expression may not have been clear enough. I am aware that the original rules include “instance name” and that is exactly why my original question Q3 asked whether the name is a module name only when the hierarchical reference statement is contained within a module, and otherwise is always an instance name.