參考:
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
注意:
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方法。