SV——automatic
参考:
IEEE 1800 6.21 Scope and lifetime
1. SV中变量存储
- Variables declared outside a module, program, interface, checker, task, or function are local to the
compilation unit and have a static lifetime (exist for the whole simulation).
- Variables declared inside a module, interface, program, or checker, but outside a task, process, or function, are local in scope(局部有效) and have a static lifetime.
- Variables declared inside a static task, function, or block are local in scope and default to a static lifetime.
- Tasks and functions may be declared as automatic. Variables declared in an automatic task, function, or block are local in scope, default to the lifetime of the call or block(automatic类型的变量的生存周期是函数调用时间或者块语句的生存周期), and are initialized on each entry to the call or block。
- An automatic block is one in which declarations are automatic by default. Specific variables within an automatic task, function, or block can be explicitly declared as static. Such variables have a static lifetime.
上面前三条是一个意思,变量都是存在静态存储区,如果有多个线程同时调用任务或者函数,任务或函数内地变量是共享的,这样会发生问题。
后面两条是一个意思,变量存在动态存储区,每次对任务或者函数的调用都会分配动态存储区域来存储变量,任务或者函数执行完,自动释放动态存储区。
1800中的例子
1 |
|
注意:
Class methods (see Clause 8) and declared for loop variables (see 12.7.1) are by default automatic,
regardless of the lifetime attribute of the scope in which they are declared.
类方法和for循环中的变量默认类型就是automatic,这应该也就是上面为什么不加automatic也输出1 2 3 1 2 3 1 2 3的原因吧。
在SV中的方法,如果是ref类型,那么必须是automatic方法。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!