Add regression test for function calls with empty arguments

Check that function calls with empty arguments are supported. Check the
general case and special cases such as calling a function with empty
arguments as part of a module port binding or force statements in automatic
contexts.

Also check that calling a function with too many trailing empty arguments
as well as passing an empty argument for a port without a default value is
an error.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
This commit is contained in:
Lars-Peter Clausen 2023-01-08 21:30:47 -08:00
parent 6cf19ec964
commit fe5e60840f
9 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,33 @@
// Check that it is possible to call functins with empty arguments if they have
// default values.
module test;
bit failed = 1'b0;
`define check(expr, val) \
if (expr !== val) begin \
$display("FAILED. %s, expected %d, got %d", `"expr`", val, expr); \
failed = 1'b1; \
end
function integer f(integer a = 1, integer b = 2, integer c = 3);
return a * 100 + b * 10 + c;
endfunction
initial begin
`check(f(4, 5, 6), 456);
`check(f(4, 5, ), 453);
`check(f(4, , 6), 426);
`check(f( , 5, 6), 156);
`check(f(4, , ), 423);
`check(f( , 5, ), 153);
`check(f( , , 6), 126);
`check(f( , , ), 123);
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,29 @@
// Check that it is possible to call functins with empty arguments if they have
// default values. Check that this works if the function call is in a module
// port binding expression.
module M (input [31:0] x, input [31:0] y);
initial begin
#1
if (x === 623 && y == 624) begin
$display("PASSED");
end else begin
$display("FAILED");
end
end
endmodule
module test;
function [31:0] f(reg [31:0] a, reg [31:0] b = 2, reg [31:0] c = 3);
return a * 100 + b * 10 + c;
endfunction
M i_m (
.x(f(6, )),
.y(f(6, , 4))
);
endmodule

View File

@ -0,0 +1,41 @@
// Check that it is possible to call functins with empty arguments if they have
// default values. Check that this works if the function call is part of a force
// statement in an automatic context.
module test;
bit failed = 1'b0;
`define check(expr, val) \
if (expr !== val) begin \
$display("FAILED. %s, expected %d, got %d", `"expr`", val, expr); \
failed = 1'b1; \
end
integer x, y, z, w;
function automatic integer f(integer a = 1, integer b = 2, integer c = 3);
return a * 100 + b * 10 + c;
endfunction
task automatic t;
force x = f(4, , 6);
force y = f(4, 5, );
force z = f(4, , );
force w = f( , , 6);
endtask
initial begin
t;
`check(x, 426);
`check(y, 453);
`check(z, 423);
`check(w, 126);
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,16 @@
// Check that passing too many empty arguments to a function results in an error.
module test;
function f(integer a = 1, integer b = 2, integer c = 3);
return a + 10 * b + 100 * c;
endfunction
initial begin
integer x;
x = f( , , ,); // This should fail. 4 empty args, even though the function
// only takes 3 args
$display("FAILED");
end
endmodule

View File

@ -0,0 +1,16 @@
// Check that passing additional empty arguments to a function results in an error.
module test;
function f(integer a = 1, integer b = 2, integer c = 3);
return a + 10 * b + 100 * c;
endfunction
initial begin
integer x;
x = f(4, 5, 6, ); // This should fail. 4 empty args, even though the function
// only takes 3 args
$display("FAILED");
end
endmodule

View File

@ -0,0 +1,16 @@
// Check that passing empty arguments to a function without any ports is an
// error.
module test;
function f();
return 42;
endfunction
initial begin
integer x;
x = f( , ); // This should fail. The function takes no arguments.
$display("FAILED");
end
endmodule

View File

@ -0,0 +1,16 @@
// Check that an error is reported when passing an empty function argument for a
// port without a default value.
module test;
function f(integer a, integer b = 2);
return a + 10 * b + 100 * c;
endfunction
initial begin
integer x;
x = f( , 3); // This should fail. No default value specified.
$display("FAILED");
end
endmodule

View File

@ -313,6 +313,13 @@ fork_join_any normal,-g2009 ivltests
fork_join_dis normal,-g2009 ivltests
fork_join_none normal,-g2009 ivltests
fr49 normal,-g2009 ivltests
func_empty_arg1 normal,-g2005-sv ivltests
func_empty_arg2 normal,-g2005-sv ivltests
func_empty_arg3 normal,-g2005-sv ivltests
func_empty_arg_fail1 CE,-g2005-sv ivltests
func_empty_arg_fail2 CE,-g2005-sv ivltests
func_empty_arg_fail3 CE,-g2005-sv ivltests
func_empty_arg_fail4 CE,-g2005-sv ivltests
func_init_var1 normal,-g2009 ivltests
func_init_var2 normal,-g2009 ivltests
func_init_var3 normal,-g2009 ivltests

View File

@ -82,6 +82,7 @@ automatic_task3 CE ivltests
br942 CE ivltests
br_gh531 CE ivltests
def_nettype CE ivltests
func_empty_arg3 CE,-g2005-sv ivltests
func_init_var1 CE,-pallowsigned=1 ivltests
func_init_var2 CE,-pallowsigned=1 ivltests
func_init_var3 CE,-pallowsigned=1 ivltests