Skip to content

Commit a6c0334

Browse files
committed
Add ansi parameter list parsing and param2 test
1 parent 5ea5753 commit a6c0334

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

src/lexer.l

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ UDP_INDICATOR "("{UDP_LEVEL}{UDP_LEVEL}")"
139139
SCALAR_ZERO ('b0|'B0|1'b0|1'B0)
140140
SCALAR_ONE ('b1|'B1|1'b1|1'B1)
141141
PAREN_STAR "("{SPACE}*"*"{SPACE}*")"
142+
HASH_LPAREN #{SPACE}*"("
142143

143144
%x COMMENT C_COMMENT PSL VLOG UDP SDF SDF_EXPR
144145

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

737739
<INITIAL,PSL>{DECIMAL} { return parse_decimal_literal(yytext); }
738740
<INITIAL,PSL>{BASED} { return parse_based_literal(yytext); }

src/scan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ const char *token_str(token_t tok)
265265
"shortreal", "realtime", "`__nvc_push", "`__nvc_pop", "++", "--",
266266
"var", "`default_nettype", "tri", "tri0", "tri1", "wand", "triand",
267267
"wor", "trior", "trireg", "uwire", "none", "localparam", "always_comb",
268-
"always_ff", "always_latch", "(*)",
268+
"always_ff", "always_latch", "(*)", "#(",
269269
};
270270

271271
if (tok >= 200 && tok - 200 < ARRAY_LEN(token_strs))

src/scan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,5 +441,6 @@ void reset_sdf_parser(void);
441441
#define tALWAYSFF 540
442442
#define tALWAYSLATCH 541
443443
#define tPARENSTAR 542
444+
#define tHASHLPAREN 543
444445

445446
#endif // _SCAN_H

src/vlog/vlog-parse.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,6 +3325,57 @@ static void p_list_of_port_declarations(vlog_node_t mod)
33253325
consume(tRPAREN);
33263326
}
33273327

3328+
static void p_parameter_port_declaration(vlog_node_t mod, vlog_kind_t kind)
3329+
{
3330+
// parameter_port_declaration ::= parameter_declaration
3331+
// | local_parameter_declaration
3332+
// | data_type list_of_param_assignments
3333+
// | type list_of_type_assignments
3334+
3335+
BEGIN("parameter port declaration");
3336+
3337+
// TODO: Add parsing of "type" declarations example #(type T = bit)
3338+
vlog_node_t datatype = p_data_type_or_implicit();
3339+
vlog_add_decl(mod, p_param_assignment(datatype, kind));
3340+
}
3341+
3342+
static void p_parameter_port_list(vlog_node_t mod)
3343+
{
3344+
// parameter_port_list ::=
3345+
// | # ( parameter_port_declaration { , parameter_port_declaration } )
3346+
// | #( )
3347+
3348+
BEGIN("parameter port list");
3349+
3350+
consume(tHASHLPAREN);
3351+
3352+
if (peek() != tRPAREN) {
3353+
vlog_kind_t kind = V_PARAM_DECL;
3354+
3355+
do {
3356+
switch(peek()) {
3357+
case tLOCALPARAM:
3358+
kind = V_LOCALPARAM;
3359+
consume(tLOCALPARAM);
3360+
break;
3361+
case tPARAMETER:
3362+
kind = V_PARAM_DECL;
3363+
consume(tPARAMETER);
3364+
break;
3365+
case tTYPE:
3366+
// TODO: Add parsing of "type" declarations example #(type T = bit)
3367+
kind = V_LAST_NODE_KIND;
3368+
break;
3369+
default:
3370+
break;
3371+
}
3372+
p_parameter_port_declaration(mod, kind);
3373+
} while(optional(tCOMMA));
3374+
}
3375+
3376+
consume(tRPAREN);
3377+
}
3378+
33283379
static void p_module_ansi_header(vlog_node_t mod)
33293380
{
33303381
// { attribute_instance } module_keyword [ lifetime ] module_identifier
@@ -3333,6 +3384,9 @@ static void p_module_ansi_header(vlog_node_t mod)
33333384

33343385
EXTEND("module ANSI header");
33353386

3387+
if (peek() == tHASHLPAREN)
3388+
p_parameter_port_list(mod);
3389+
33363390
if (peek() == tLPAREN)
33373391
p_list_of_port_declarations(mod);
33383392

test/test_vlog.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,28 @@ START_TEST(test_param1)
653653
}
654654
END_TEST
655655

656+
START_TEST(test_param2)
657+
{
658+
input_from_file(TESTDIR "/vlog/param2.v");
659+
660+
const error_t expect[] = {
661+
{ 13, "duplicate declaration" },
662+
{ -1, NULL }
663+
};
664+
expect_errors(expect);
665+
666+
vlog_node_t m = vlog_parse();
667+
fail_if(m == NULL);
668+
fail_unless(vlog_kind(m) == V_MODULE);
669+
670+
vlog_check(m);
671+
672+
fail_unless(vlog_parse() == NULL);
673+
674+
check_expected_errors();
675+
}
676+
END_TEST
677+
656678
START_TEST(test_pp3)
657679
{
658680
input_from_file(TESTDIR "/vlog/pp3.v");
@@ -802,6 +824,7 @@ Suite *get_vlog_tests(void)
802824
tcase_add_test(tc, test_enum1);
803825
tcase_add_test(tc, test_union1);
804826
tcase_add_test(tc, test_param1);
827+
tcase_add_test(tc, test_param2);
805828
tcase_add_test(tc, test_pp3);
806829
tcase_add_test(tc, test_concat1);
807830
tcase_add_test(tc, test_pp4);

test/vlog/param2.v

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module param2 #(
2+
[7:0] p1 = 8'd5,
3+
parameter p2 = 8,
4+
p3 = 7,
5+
localparam bit p4 = 0,
6+
p5 = 5,
7+
parameter p6 = 4
8+
) (
9+
input x, y,
10+
output reg z
11+
);
12+
assign w1 = p1; // OK
13+
parameter logic p1 = 1; // Error
14+
endmodule // param2

0 commit comments

Comments
 (0)