Merge pull request #1759 from zeldin/constant_with_comment_redux

refixed parsing of constant with comment between size and value
This commit is contained in:
Miodrag Milanović 2020-03-14 13:34:59 +02:00 committed by GitHub
commit 569e834df2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 19 deletions

View File

@ -113,8 +113,10 @@ extern int frontend_verilog_yylex(YYSTYPE *yylval_param, YYLTYPE *yyloc_param);
%x SYNOPSYS_TRANSLATE_OFF
%x SYNOPSYS_FLAGS
%x IMPORT_DPI
%x BASED_CONST
%%
int comment_caller;
<INITIAL,SYNOPSYS_TRANSLATE_OFF>"`file_push "[^\n]* {
fn_stack.push_back(current_filename);
@ -273,9 +275,21 @@ extern int frontend_verilog_yylex(YYSTYPE *yylval_param, YYLTYPE *yyloc_param);
return TOK_CONSTVAL;
}
[0-9]*[ \t]*\'[sS]?[bodhBODH]?[ \t\r\n]*[0-9a-fA-FzxZX?_]+ {
\'[01zxZX] {
yylval->string = new std::string(yytext);
return TOK_CONSTVAL;
return TOK_UNBASED_UNSIZED_CONSTVAL;
}
\'[sS]?[bodhBODH] {
BEGIN(BASED_CONST);
yylval->string = new std::string(yytext);
return TOK_BASE;
}
<BASED_CONST>[0-9a-fA-FzxZX?][0-9a-fA-FzxZX?_]* {
BEGIN(0);
yylval->string = new std::string(yytext);
return TOK_BASED_CONSTVAL;
}
[0-9][0-9_]*\.[0-9][0-9_]*([eE][-+]?[0-9_]+)? {
@ -478,16 +492,17 @@ import[ \t\r\n]+\"(DPI|DPI-C)\"[ \t\r\n]+function[ \t\r\n]+ {
return TOK_SPECIFY_AND;
}
"/*" { BEGIN(COMMENT); }
<INITIAL,BASED_CONST>"/*" { comment_caller=YY_START; BEGIN(COMMENT); }
<COMMENT>. /* ignore comment body */
<COMMENT>\n /* ignore comment body */
<COMMENT>"*/" { BEGIN(0); }
<COMMENT>"*/" { BEGIN(comment_caller); }
[ \t\r\n] /* ignore whitespaces */
\\[\r\n] /* ignore continuation sequence */
"//"[^\r\n]* /* ignore one-line comments */
<INITIAL,BASED_CONST>[ \t\r\n] /* ignore whitespaces */
<INITIAL,BASED_CONST>\\[\r\n] /* ignore continuation sequence */
<INITIAL,BASED_CONST>"//"[^\r\n]* /* ignore one-line comments */
. { return *yytext; }
<INITIAL>. { return *yytext; }
<*>. { BEGIN(0); return *yytext; }
%%

View File

@ -166,6 +166,7 @@ static void addRange(AstNode *parent, int msb = 31, int lsb = 0, bool isSigned =
%token <string> TOK_STRING TOK_ID TOK_CONSTVAL TOK_REALVAL TOK_PRIMITIVE
%token <string> TOK_SVA_LABEL TOK_SPECIFY_OPER TOK_MSG_TASKS
%token <string> TOK_BASE TOK_BASED_CONSTVAL TOK_UNBASED_UNSIZED_CONSTVAL
%token TOK_ASSERT TOK_ASSUME TOK_RESTRICT TOK_COVER TOK_FINAL
%token ATTR_BEGIN ATTR_END DEFATTR_BEGIN DEFATTR_END
%token TOK_MODULE TOK_ENDMODULE TOK_PARAMETER TOK_LOCALPARAM TOK_DEFPARAM
@ -188,7 +189,7 @@ static void addRange(AstNode *parent, int msb = 31, int lsb = 0, bool isSigned =
%type <ast> range range_or_multirange non_opt_range non_opt_multirange range_or_signed_int
%type <ast> wire_type expr basic_expr concat_list rvalue lvalue lvalue_concat_list
%type <string> opt_label opt_sva_label tok_prim_wrapper hierarchical_id hierarchical_type_id
%type <string> opt_label opt_sva_label tok_prim_wrapper hierarchical_id hierarchical_type_id integral_number
%type <ast> opt_enum_init
%type <boolean> opt_signed opt_property unique_case_attr always_comb_or_latch always_or_always_ff
%type <al> attr case_attr
@ -2514,7 +2515,7 @@ basic_expr:
rvalue {
$$ = $1;
} |
'(' expr ')' TOK_CONSTVAL {
'(' expr ')' integral_number {
if ($4->compare(0, 1, "'") != 0)
frontend_verilog_yyerror("Cast operation must be applied on sized constants e.g. (<expr>)<constval> , while %s is not a sized constant.", $4->c_str());
AstNode *bits = $2;
@ -2524,7 +2525,7 @@ basic_expr:
$$ = new AstNode(AST_TO_BITS, bits, val);
delete $4;
} |
hierarchical_id TOK_CONSTVAL {
hierarchical_id integral_number {
if ($2->compare(0, 1, "'") != 0)
frontend_verilog_yyerror("Cast operation must be applied on sized constants, e.g. <ID>\'d0, while %s is not a sized constant.", $2->c_str());
AstNode *bits = new AstNode(AST_IDENTIFIER);
@ -2537,14 +2538,7 @@ basic_expr:
delete $1;
delete $2;
} |
TOK_CONSTVAL TOK_CONSTVAL {
$$ = const2ast(*$1 + *$2, case_type_stack.size() == 0 ? 0 : case_type_stack.back(), !lib_mode);
if ($$ == NULL || (*$2)[0] != '\'')
log_error("Value conversion failed: `%s%s'\n", $1->c_str(), $2->c_str());
delete $1;
delete $2;
} |
TOK_CONSTVAL {
integral_number {
$$ = const2ast(*$1, case_type_stack.size() == 0 ? 0 : case_type_stack.back(), !lib_mode);
if ($$ == NULL)
log_error("Value conversion failed: `%s'\n", $1->c_str());
@ -2791,3 +2785,18 @@ concat_list:
$$ = $3;
$$->children.push_back($1);
};
integral_number:
TOK_CONSTVAL { $$ = $1; } |
TOK_UNBASED_UNSIZED_CONSTVAL { $$ = $1; } |
TOK_BASE TOK_BASED_CONSTVAL {
$1->append(*$2);
$$ = $1;
delete $2;
} |
TOK_CONSTVAL TOK_BASE TOK_BASED_CONSTVAL {
$1->append(*$2).append(*$3);
$$ = $1;
delete $2;
delete $3;
};

8
tests/various/bug1745.ys Normal file
View File

@ -0,0 +1,8 @@
logger -expect error "syntax error, unexpected TOK_CONSTVAL" 1
read_verilog <<EOT
module inverter(input a, output y);
assign y = (a == 1'b0? 1'b1 : 1'b0);
endmodule // inverter
EOT

View File

@ -0,0 +1,16 @@
read_verilog <<EOT
module top1;
localparam a = 8 /*foo*/ 'h ab;
localparam b = 8 'h /*foo*/ cd;
generate
if (a != 8'b10101011) $error("a incorrect!");
if (b != 8'b11001101) $error("b incorrect!");
endgenerate
endmodule
EOT
logger -expect error "syntax error, unexpected TOK_BASE" 1
read_verilog <<EOT
module top2;
localparam a = 12'h4 /*foo*/'b0;
endmodule
EOT