SystemVerilog Coding Guidelines: Package import versus `include
来源:互联网 发布:大数据的技术架构 编辑:程序博客网 时间:2024/05/29 04:30
Another frequently asked question: Should I import my classes from a packageor `include them? To answer this properly, you need to know more about SystemVerilog’s type system, especially the difference between itsstrong and weak typing systems.
In programming languages, weak typing is characterized by implicit or ad-hoc conversions without explicit casting between values of different data types. Verilog’s bit vectors, or integral types, represent these weak typing aspects by implicitly padding and truncating values to be the proper bit lengths – at least proper by Verilog standards. If you perform a bitwise AND of a 7-bit and 8-bit vector, Verilog implicitly zero pads an 8th bit to the 7-bit operand and returns an 8-bit result. In contrast using VHDL, you would have to explicitly state whether you wanted the 7-bit operand to be padded, or the 8-bit operand to be truncated so that you have an expression with operands of equal size.
With a few exceptions, all other types in SystemVerilog follow strong typing rules. Strong typing rules require explicit conversions or casts when assigning or expressing operands of unequal types. And understanding what SystemVerilog considers equivalent types is key to understanding the effect of importing a class from a package versus including it from a file.
Inheritance aside, SystemVerilog uses the name of a type alone to determine type equivalence of a class. For example, suppose I have these two class definitions A and B below:
class A;
int i;
endclass : A
class B;
int i;
endclass : B
SystemVerilog considers these two class definitions unequal types because they have different names, even though their contents, or class bodies, are identical. The name of a class includes more than just the simple namesA and B; the names also include the scope where the definition is declared. When you declare a class in a package, the package name becomes a prefix to the class name:
package P;
class A;
int i;
endclass : A
A a1;
endpackage : P
package Q;
class A;
int i;
endclass : A
A a1;
endpackage : Q
Now there are two definitions of class A, one called P::A and the other calledQ::A. And the variables P::a1 and Q::a1 are type incompatible referencing two different class A’s. Re-writing the above example using an include file creates the same situation – two incompatible class definitions.
File A.svFile P.svFile Q.svclass A;
int i;
endclass : A
package P;
`include “A.sv"
A a1;
endpackage : P
package Q;
`include “A.sv"
A a1;
endpackage : Q
After `including class A into each package, you wind up with two definitions of class A. Using `include is just a shortcut for cut and pasting text in a file. Importing a name from a package does not duplicate text; it makes that name visible from another package without copying the definition.
File A.svFile P.svFile R.svFile S.svclass A;
int i;
endclass : A
package P;
`include “A.sv"
endpackage : P
package R;
import P::A;
A a1;
endpackage : R
package S;
import P::A;
A a1;
endpackage : S
Class A is declared in package P, and only in package P. The variablesR::a1 and S::a1 are type compatible because they are both of typeP::A. The fact that class A was `included from another file once it is expanded is no longer relevant once you consider the placement of the text from the file.
When you get compiler errors claiming that two types are incompatible even though they appear to have the same name, make sure you consider the scope where the types are declared as part of the full name. Class names declared in a module are prefixed by the module instance name, so the same module instantiated multiple times will create unique class names, all incompatible types.
For further information about packages, check out the June Verification Horizons article entitled “Using SystemVerilog Packages in Real Verification Projects”.
Dave Rich
- SystemVerilog Coding Guidelines: Package import versus `include
- Coding Guidelines
- 关于package的import 和文件include
- 编码规则 - coding guidelines
- Coding Guidelines for Cocoa
- C# coding-guidelines
- Coding Guidelines for Cocoa
- Coding GuideLines For Cocoa
- Could import package replace the include .aidl file?
- package,import,include的联系与区别(转载)
- Java 入门语法<include ClassPath,import,package.etc>
- import和 include 及 package 猜测 (编译,声明)
- package,import,include的联系与区别【好】
- TIZEN RPM Package Guidelines
- Flex3 & AS3.0 Coding Guidelines
- Objective-C Coding Style Guidelines
- Objective-C Coding Style Guidelines
- Alibaba Java Coding Guidelines使用
- 大型网站架构演化
- Filter介绍
- 设计模式(一):单例模式(Singleton Pattern)
- redis配置文件参数说明及命令操作
- 对证书和公钥的基本理解
- SystemVerilog Coding Guidelines: Package import versus `include
- Application 调用
- 学习检查Linux是否遭到入侵篡改
- Android 百度统计
- hibernate多对多,中间表无数据问题
- jQuery学习之基本选择器、层次选择器和并且选择器
- Joomla入门 HEELOWORLD工作方式
- Spring MVC 下,不使用AJAX 登录时的错误提示的实现
- 《LeetCode》:Two Sum