2025-02-26 04:48:53 +01:00
// DESCRIPTION: Verilator: Test automatic function variables lifetime
//
2026-01-27 02:24:34 +01:00
// This file ONLY is placed under the Creative Commons Public Domain.
// SPDX-FileCopyrightText: 2025 Augustin Fabre
2025-02-26 04:48:53 +01:00
// SPDX-License-Identifier: CC0-1.0
2025-12-21 03:46:43 +01:00
// verilog_format: off
2025-02-26 04:48:53 +01:00
`define stop $stop
`define checkh(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got=%0x exp=%0x (%s !== %s)\n", `__FILE__,`__LINE__, (gotv), (expv), `"gotv`", `"expv`"); `stop; end while(0);
`define checks(gotv,expv) do if ((gotv) != (expv)) begin $write("%%Error: %s:%0d: got='%s' exp='%s'\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
2025-12-21 03:46:43 +01:00
// verilog_format: on
2025-02-26 04:48:53 +01:00
// Bug5747: Make sure that a variable with automatic storage is freshly
// allocated when entering the function.
2026-03-03 13:21:24 +01:00
module t ;
2026-03-08 23:26:40 +01:00
function automatic int ts_queue ( ) ;
static int qs [ $ ] ;
qs . push_back ( 0 ) ;
// $display(" qs: %p", qs);
return qs . size ( ) ;
endfunction
function automatic int t_queue ( ) ;
int q [ $ ] ;
q . push_back ( 0 ) ;
// $display(" q: %p", q);
return q . size ( ) ;
endfunction
function automatic int t_scalar ( ) ;
int x ;
+ + x ;
return x ;
endfunction
typedef struct { int y ; } y_t ;
function automatic int t_struct ( ) ;
y_t y ;
+ + y . y ;
return y . y ;
endfunction
function automatic string t_string ( ) ;
string x ;
x = { x , " s " } ;
return x ;
endfunction
class ClsZ ;
int z ;
endclass
function automatic int t_class ( ) ;
ClsZ z = new ( ) ;
+ + z . z ;
return z . z ;
endfunction
typedef string dyn_t [ ] ;
function automatic dyn_t t_dyn ( ) ;
dyn_t x ;
x = { x , " s " } ;
return x ;
endfunction
typedef string assoc_t [ int ] ;
function automatic assoc_t t_assoc ( ) ;
static int ins = 0 ;
assoc_t x ;
ins = ins + 1 ;
x [ ins ] = " s " ;
return x ;
endfunction
typedef string wild_t [ * ] ;
function automatic wild_t t_wild ( ) ;
static int ins = 0 ;
wild_t x ;
ins = ins + 1 ;
x [ ins ] = " s " ;
return x ;
endfunction
typedef int unpack_t [ 8 ] ;
function automatic unpack_t t_unpack ( ) ;
static int ins = 0 ;
unpack_t x ;
ins = ins + 1 ;
x [ ins ] = ins ;
return x ;
endfunction
// =======================
function automatic void main ( ) ;
for ( int i = 0 ; i < 3 ; + + i ) begin
int qn = ts_queue ( ) ;
int qo = ts_queue ( ) ;
`checkh ( qn , i * 2 + 1 ) ;
`checkh ( qo , i * 2 + 2 ) ;
end
for ( int i = 0 ; i < 3 ; + + i ) begin
int qn = t_queue ( ) ;
`checkh ( qn , 1 ) ;
end
for ( int i = 0 ; i < 3 ; + + i ) begin
int x = t_scalar ( ) ;
`checkh ( x , 1 ) ;
end
for ( int i = 0 ; i < 3 ; + + i ) begin
int y = t_struct ( ) ;
`checkh ( y , 1 ) ;
end
for ( int i = 0 ; i < 3 ; + + i ) begin
int z = t_class ( ) ;
`checkh ( z , 1 ) ;
end
for ( int i = 0 ; i < 3 ; + + i ) begin
string z = t_string ( ) ;
`checks ( z , " s " ) ;
end
for ( int i = 0 ; i < 3 ; + + i ) begin
dyn_t z = t_dyn ( ) ;
`checkh ( z . size ( ) , 1 ) ;
end
for ( int i = 0 ; i < 3 ; + + i ) begin
assoc_t z = t_assoc ( ) ;
`checkh ( z . size ( ) , 1 ) ;
end
for ( int i = 0 ; i < 3 ; + + i ) begin
wild_t z = t_wild ( ) ;
`checkh ( z . size ( ) , 1 ) ;
end
for ( int i = 0 ; i < 3 ; + + i ) begin
int cnt ;
unpack_t z = t_unpack ( ) ;
cnt = 0 ;
for ( int j = 0 ; j < $high ( z ) ; + + j ) begin
if ( z [ j ] ! = 0 ) cnt = cnt + 1 ;
2025-02-26 04:48:53 +01:00
end
2026-03-08 23:26:40 +01:00
`checkh ( cnt , 1 ) ;
end
2025-02-26 04:48:53 +01:00
2026-03-08 23:26:40 +01:00
endfunction
2025-02-26 04:48:53 +01:00
2026-03-08 23:26:40 +01:00
initial begin
main ( ) ;
$write ( " *-* All Finished *-* \n " ) ;
$finish ;
end
2025-02-26 04:48:53 +01:00
endmodule