Skip to content

Parse ANSI parameters declaration #1160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ UDP_INDICATOR "("{UDP_LEVEL}{UDP_LEVEL}")"
SCALAR_ZERO ('b0|'B0|1'b0|1'B0)
SCALAR_ONE ('b1|'B1|1'b1|1'B1)
PAREN_STAR "("{SPACE}*"*"{SPACE}*")"
HASH_LPAREN #{SPACE}*"("

%x COMMENT C_COMMENT PSL VLOG UDP SDF SDF_EXPR

Expand Down Expand Up @@ -733,6 +734,7 @@ BEFORE ?i:before
<VLOG>{VLOG_NUMBER} { yylval.str = xstrdup(yytext); return tNUMBER; }
<VLOG>{VLOG_REAL} { return parse_verilog_real(yytext); };
<VLOG>{PAREN_STAR} { return tPARENSTAR; }
<VLOG>{HASH_LPAREN} { return tHASHLPAREN; }

<INITIAL,PSL>{DECIMAL} { return parse_decimal_literal(yytext); }
<INITIAL,PSL>{BASED} { return parse_based_literal(yytext); }
Expand Down
2 changes: 1 addition & 1 deletion src/scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ const char *token_str(token_t tok)
"shortreal", "realtime", "`__nvc_push", "`__nvc_pop", "++", "--",
"var", "`default_nettype", "tri", "tri0", "tri1", "wand", "triand",
"wor", "trior", "trireg", "uwire", "none", "localparam", "always_comb",
"always_ff", "always_latch", "(*)",
"always_ff", "always_latch", "(*)", "#(",
};

if (tok >= 200 && tok - 200 < ARRAY_LEN(token_strs))
Expand Down
1 change: 1 addition & 0 deletions src/scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,6 @@ void reset_sdf_parser(void);
#define tALWAYSFF 540
#define tALWAYSLATCH 541
#define tPARENSTAR 542
#define tHASHLPAREN 543

#endif // _SCAN_H
54 changes: 54 additions & 0 deletions src/vlog/vlog-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -3325,6 +3325,57 @@ static void p_list_of_port_declarations(vlog_node_t mod)
consume(tRPAREN);
}

static void p_parameter_port_declaration(vlog_node_t mod, vlog_kind_t kind)
{
// parameter_port_declaration ::= parameter_declaration
// | local_parameter_declaration
// | data_type list_of_param_assignments
// | type list_of_type_assignments

BEGIN("parameter port declaration");

// TODO: Add parsing of "type" declarations example #(type T = bit)
vlog_node_t datatype = p_data_type_or_implicit();
vlog_add_decl(mod, p_param_assignment(datatype, kind));
}

static void p_parameter_port_list(vlog_node_t mod)
{
// parameter_port_list ::=
// | # ( parameter_port_declaration { , parameter_port_declaration } )
// | #( )

BEGIN("parameter port list");

consume(tHASHLPAREN);

if (peek() != tRPAREN) {
vlog_kind_t kind = V_PARAM_DECL;

do {
switch(peek()) {
case tLOCALPARAM:
kind = V_LOCALPARAM;
consume(tLOCALPARAM);
break;
case tPARAMETER:
kind = V_PARAM_DECL;
consume(tPARAMETER);
break;
case tTYPE:
// TODO: Add parsing of "type" declarations example #(type T = bit)
kind = V_LAST_NODE_KIND;
break;
default:
break;
}
p_parameter_port_declaration(mod, kind);
} while(optional(tCOMMA));
}

consume(tRPAREN);
}

static void p_module_ansi_header(vlog_node_t mod)
{
// { attribute_instance } module_keyword [ lifetime ] module_identifier
Expand All @@ -3333,6 +3384,9 @@ static void p_module_ansi_header(vlog_node_t mod)

EXTEND("module ANSI header");

if (peek() == tHASHLPAREN)
p_parameter_port_list(mod);

if (peek() == tLPAREN)
p_list_of_port_declarations(mod);

Expand Down
23 changes: 23 additions & 0 deletions test/test_vlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,28 @@ START_TEST(test_param1)
}
END_TEST

START_TEST(test_param2)
{
input_from_file(TESTDIR "/vlog/param2.v");

const error_t expect[] = {
{ 13, "duplicate declaration" },
{ -1, NULL }
};
expect_errors(expect);

vlog_node_t m = vlog_parse();
fail_if(m == NULL);
fail_unless(vlog_kind(m) == V_MODULE);

vlog_check(m);

fail_unless(vlog_parse() == NULL);

check_expected_errors();
}
END_TEST

START_TEST(test_pp3)
{
input_from_file(TESTDIR "/vlog/pp3.v");
Expand Down Expand Up @@ -802,6 +824,7 @@ Suite *get_vlog_tests(void)
tcase_add_test(tc, test_enum1);
tcase_add_test(tc, test_union1);
tcase_add_test(tc, test_param1);
tcase_add_test(tc, test_param2);
tcase_add_test(tc, test_pp3);
tcase_add_test(tc, test_concat1);
tcase_add_test(tc, test_pp4);
Expand Down
14 changes: 14 additions & 0 deletions test/vlog/param2.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module param2 # (
[7:0] p1 = 8'd5,
parameter p2 = 8,
p3 = 7,
localparam bit p4 = 0,
p5 = 5,
parameter p6 = 4
) (
input x, y,
output reg z
);
assign w1 = p1; // OK
parameter logic p1 = 1; // Error
endmodule // param2
Loading