SV——automatic

 

参考:

IEEE 1800 6.21 Scope and lifetime

 

1. SV中变量存储

  1. 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).

  1. 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.

  2. Variables declared inside a static task, function, or block are local in scope and default to a static lifetime.

  3. 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。

  4. 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中的例子

module top_legal;
int svar1 = 1; // static keyword optional
initial begin
    for (int i=0; i<3; i++) begin
        automatic int loop3 = 0; // executes every loop
        for (int j=0; j<3; j++) begin
        loop3++;
        $display(loop3);
        end
    end // prints 1 2 3 1 2 3 1 2 3
    for (int i=0; i<3; i++) begin
        static int loop2 = 0; // executes once at time zero
        for (int j=0; j<3; j++) begin
            loop2++;
            $display(loop2);
        end
    end // prints 1 2 3 4 5 6 7 8 9
end
endmodule : top_legal
​
module top_illegal; // should not compile
initial begin
    int svar2 = 2; // static/automatic needed to show intent
    for (int i=0; i<3; i++) begin
        int loop3 = 0; // illegal statement 
        for (int i=0; i<3; i++) begin
            loop3++;
            $display(loop3);
        end
    end
end
endmodule : top_illegal
// 其实int loop3=2,不加static也不加automatic输出是 1 2 3 1 2 3 1 2 3;不知道上面为什么说是illegal statement

 

注意:

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的原因吧。

 

 

 

上一篇:Java 报时助手


下一篇:lly dependent on columns in GROUP BY clause; this is incompatible with sql_m