2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * The Verilog frontend.
22 * This frontend is using the AST frontend library (see frontends/ast/).
23 * Thus this frontend does not generate RTLIL code directly but creates an
24 * AST directly from the Verilog parse tree and then passes this AST to
25 * the AST frontend library.
29 * This is the actual bison parser for Verilog code. The AST ist created directly
30 * from the bison reduce functions here. Note that this code uses a few global
31 * variables to hold the state of the AST generator and therefore this parser is
40 #include "frontends/verilog/verilog_frontend.h"
41 #include "frontends/verilog/verilog_parser.tab.hh"
42 #include "kernel/log.h"
44 #define YYLEX_PARAM &yylval, &yylloc
48 using namespace VERILOG_FRONTEND;
51 namespace VERILOG_FRONTEND {
53 dict<std::string, int> port_stubs;
54 dict<IdString, AstNode*> *attr_list, default_attr_list;
55 std::stack<dict<IdString, AstNode*> *> attr_list_stack;
56 dict<IdString, AstNode*> *albuf;
57 std::vector<UserTypeMap*> user_type_stack;
58 dict<std::string, AstNode*> pkg_user_types;
59 std::vector<AstNode*> ast_stack;
60 struct AstNode *astbuf1, *astbuf2, *astbuf3;
61 struct AstNode *current_function_or_task;
62 struct AstNode *current_ast, *current_ast_mod;
63 int current_function_or_task_port_id;
64 std::vector<char> case_type_stack;
65 bool do_not_require_port_stubs;
66 bool default_nettype_wire;
67 bool sv_mode, formal_mode, lib_mode, specify_mode;
68 bool noassert_mode, noassume_mode, norestrict_mode;
69 bool assume_asserts_mode, assert_assumes_mode;
70 bool current_wire_rand, current_wire_const;
71 bool current_modport_input, current_modport_output;
76 #define SET_AST_NODE_LOC(WHICH, BEGIN, END) \
77 do { (WHICH)->location.first_line = (BEGIN).first_line; \
78 (WHICH)->location.first_column = (BEGIN).first_column; \
79 (WHICH)->location.last_line = (END).last_line; \
80 (WHICH)->location.last_column = (END).last_column; } while(0)
82 #define SET_RULE_LOC(LHS, BEGIN, END) \
83 do { (LHS).first_line = (BEGIN).first_line; \
84 (LHS).first_column = (BEGIN).first_column; \
85 (LHS).last_line = (END).last_line; \
86 (LHS).last_column = (END).last_column; } while(0)
88 int frontend_verilog_yylex(YYSTYPE *yylval_param, YYLTYPE *yyloc_param);
90 static void append_attr(AstNode *ast, dict<IdString, AstNode*> *al)
92 for (auto &it : *al) {
93 if (ast->attributes.count(it.first) > 0)
94 delete ast->attributes[it.first];
95 ast->attributes[it.first] = it.second;
100 static void append_attr_clone(AstNode *ast, dict<IdString, AstNode*> *al)
102 for (auto &it : *al) {
103 if (ast->attributes.count(it.first) > 0)
104 delete ast->attributes[it.first];
105 ast->attributes[it.first] = it.second->clone();
109 static void free_attr(dict<IdString, AstNode*> *al)
116 struct specify_target {
121 struct specify_triple {
122 AstNode *t_min, *t_avg, *t_max;
125 struct specify_rise_fall {
130 static void addTypedefNode(std::string *name, AstNode *node)
133 auto *tnode = new AstNode(AST_TYPEDEF, node);
135 auto user_types = user_type_stack.back();
136 (*user_types)[*name] = tnode;
137 if (current_ast_mod && current_ast_mod->type == AST_PACKAGE) {
138 // typedef inside a package so we need the qualified name
139 auto qname = current_ast_mod->str + "::" + (*name).substr(1);
140 pkg_user_types[qname] = tnode;
143 ast_stack.back()->children.push_back(tnode);
146 static void enterTypeScope()
148 auto user_types = new UserTypeMap();
149 user_type_stack.push_back(user_types);
152 static void exitTypeScope()
154 user_type_stack.pop_back();
157 static bool isInLocalScope(const std::string *name)
159 // tests if a name was declared in the current block scope
160 auto user_types = user_type_stack.back();
161 return (user_types->count(*name) > 0);
164 static AstNode *getTypeDefinitionNode(std::string type_name)
166 // return the definition nodes from the typedef statement
167 auto user_types = user_type_stack.back();
168 log_assert(user_types->count(type_name) > 0);
169 auto typedef_node = (*user_types)[type_name];
170 log_assert(typedef_node->type == AST_TYPEDEF);
171 return typedef_node->children[0];
174 static AstNode *copyTypeDefinition(std::string type_name)
176 // return a copy of the template from a typedef definition
177 auto typedef_node = getTypeDefinitionNode(type_name);
178 return typedef_node->clone();
181 static AstNode *makeRange(int msb = 31, int lsb = 0, bool isSigned = true)
183 auto range = new AstNode(AST_RANGE);
184 range->children.push_back(AstNode::mkconst_int(msb, true));
185 range->children.push_back(AstNode::mkconst_int(lsb, true));
186 range->is_signed = isSigned;
190 static void addRange(AstNode *parent, int msb = 31, int lsb = 0, bool isSigned = true)
192 auto range = makeRange(msb, lsb, isSigned);
193 parent->children.push_back(range);
196 static AstNode *checkRange(AstNode *type_node, AstNode *range_node)
198 if (type_node->range_left >= 0 && type_node->range_right >= 0) {
199 // type already restricts the range
201 frontend_verilog_yyerror("integer/genvar types cannot have packed dimensions.");
204 range_node = makeRange(type_node->range_left, type_node->range_right, false);
207 if (range_node && range_node->children.size() != 2) {
208 frontend_verilog_yyerror("wire/reg/logic packed dimension must be of the form: [<expr>:<expr>], [<expr>+:<expr>], or [<expr>-:<expr>]");
213 static void rewriteAsMemoryNode(AstNode *node, AstNode *rangeNode)
215 node->type = AST_MEMORY;
216 if (rangeNode->type == AST_RANGE && rangeNode->children.size() == 1) {
217 // SV array size [n], rewrite as [n-1:0]
218 rangeNode->children[0] = new AstNode(AST_SUB, rangeNode->children[0], AstNode::mkconst_int(1, true));
219 rangeNode->children.push_back(AstNode::mkconst_int(0, false));
221 node->children.push_back(rangeNode);
226 %define api.prefix {frontend_verilog_yy}
229 /* The union is defined in the header, so we need to provide all the
230 * includes it requires
235 #include "frontends/verilog/verilog_frontend.h"
240 struct YOSYS_NAMESPACE_PREFIX AST::AstNode *ast;
241 YOSYS_NAMESPACE_PREFIX dict<YOSYS_NAMESPACE_PREFIX RTLIL::IdString, YOSYS_NAMESPACE_PREFIX AST::AstNode*> *al;
242 struct specify_target *specify_target_ptr;
243 struct specify_triple *specify_triple_ptr;
244 struct specify_rise_fall *specify_rise_fall_ptr;
249 %token <string> TOK_STRING TOK_ID TOK_CONSTVAL TOK_REALVAL TOK_PRIMITIVE
250 %token <string> TOK_SVA_LABEL TOK_SPECIFY_OPER TOK_MSG_TASKS
251 %token <string> TOK_BASE TOK_BASED_CONSTVAL TOK_UNBASED_UNSIZED_CONSTVAL
252 %token <string> TOK_USER_TYPE TOK_PKG_USER_TYPE
253 %token TOK_ASSERT TOK_ASSUME TOK_RESTRICT TOK_COVER TOK_FINAL
254 %token ATTR_BEGIN ATTR_END DEFATTR_BEGIN DEFATTR_END
255 %token TOK_MODULE TOK_ENDMODULE TOK_PARAMETER TOK_LOCALPARAM TOK_DEFPARAM
256 %token TOK_PACKAGE TOK_ENDPACKAGE TOK_PACKAGESEP
257 %token TOK_INTERFACE TOK_ENDINTERFACE TOK_MODPORT TOK_VAR TOK_WILDCARD_CONNECT
258 %token TOK_INPUT TOK_OUTPUT TOK_INOUT TOK_WIRE TOK_WAND TOK_WOR TOK_REG TOK_LOGIC
259 %token TOK_INTEGER TOK_SIGNED TOK_ASSIGN TOK_ALWAYS TOK_INITIAL
260 %token TOK_ALWAYS_FF TOK_ALWAYS_COMB TOK_ALWAYS_LATCH
261 %token TOK_BEGIN TOK_END TOK_IF TOK_ELSE TOK_FOR TOK_WHILE TOK_REPEAT
262 %token TOK_DPI_FUNCTION TOK_POSEDGE TOK_NEGEDGE TOK_OR TOK_AUTOMATIC
263 %token TOK_CASE TOK_CASEX TOK_CASEZ TOK_ENDCASE TOK_DEFAULT
264 %token TOK_FUNCTION TOK_ENDFUNCTION TOK_TASK TOK_ENDTASK TOK_SPECIFY
265 %token TOK_IGNORED_SPECIFY TOK_ENDSPECIFY TOK_SPECPARAM TOK_SPECIFY_AND TOK_IGNORED_SPECIFY_AND
266 %token TOK_GENERATE TOK_ENDGENERATE TOK_GENVAR TOK_REAL
267 %token TOK_SYNOPSYS_FULL_CASE TOK_SYNOPSYS_PARALLEL_CASE
268 %token TOK_SUPPLY0 TOK_SUPPLY1 TOK_TO_SIGNED TOK_TO_UNSIGNED
269 %token TOK_POS_INDEXED TOK_NEG_INDEXED TOK_PROPERTY TOK_ENUM TOK_TYPEDEF
270 %token TOK_RAND TOK_CONST TOK_CHECKER TOK_ENDCHECKER TOK_EVENTUALLY
271 %token TOK_INCREMENT TOK_DECREMENT TOK_UNIQUE TOK_PRIORITY
272 %token TOK_STRUCT TOK_PACKED TOK_UNSIGNED TOK_INT TOK_BYTE TOK_SHORTINT TOK_UNION
274 %type <ast> range range_or_multirange non_opt_range non_opt_multirange range_or_signed_int
275 %type <ast> wire_type expr basic_expr concat_list rvalue lvalue lvalue_concat_list
276 %type <string> opt_label opt_sva_label tok_prim_wrapper hierarchical_id hierarchical_type_id integral_number
277 %type <string> type_name
278 %type <ast> opt_enum_init enum_type struct_type non_wire_data_type
279 %type <boolean> opt_signed opt_property unique_case_attr always_comb_or_latch always_or_always_ff
280 %type <al> attr case_attr
281 %type <ast> struct_union
283 %type <specify_target_ptr> specify_target
284 %type <specify_triple_ptr> specify_triple specify_opt_triple
285 %type <specify_rise_fall_ptr> specify_rise_fall
286 %type <ast> specify_if specify_condition
287 %type <ch> specify_edge
289 // operator precedence from low to high
295 %left OP_EQ OP_NE OP_EQX OP_NEX
296 %left '<' OP_LE OP_GE '>'
297 %left OP_SHL OP_SHR OP_SSHL OP_SSHR
303 %define parse.error verbose
304 %define parse.lac full
316 ast_stack.push_back(current_ast);
318 ast_stack.pop_back();
319 log_assert(GetSize(ast_stack) == 0);
320 for (auto &it : default_attr_list)
322 default_attr_list.clear();
328 task_func_decl design |
330 localparam_decl design |
331 typedef_decl design |
338 if (attr_list != nullptr)
339 attr_list_stack.push(attr_list);
340 attr_list = new dict<IdString, AstNode*>;
341 for (auto &it : default_attr_list)
342 (*attr_list)[it.first] = it.second->clone();
345 if (!attr_list_stack.empty()) {
346 attr_list = attr_list_stack.top();
347 attr_list_stack.pop();
353 attr_opt ATTR_BEGIN opt_attr_list ATTR_END {
354 SET_RULE_LOC(@$, @2, @$);
360 if (attr_list != nullptr)
361 attr_list_stack.push(attr_list);
362 attr_list = new dict<IdString, AstNode*>;
363 for (auto &it : default_attr_list)
365 default_attr_list.clear();
367 attr_list->swap(default_attr_list);
369 if (!attr_list_stack.empty()) {
370 attr_list = attr_list_stack.top();
371 attr_list_stack.pop();
377 attr_list | /* empty */;
381 attr_list ',' attr_assign;
385 if (attr_list->count(*$1) != 0)
386 delete (*attr_list)[*$1];
387 (*attr_list)[*$1] = AstNode::mkconst_int(1, false);
390 hierarchical_id '=' expr {
391 if (attr_list->count(*$1) != 0)
392 delete (*attr_list)[*$1];
393 (*attr_list)[*$1] = $3;
401 hierarchical_id TOK_PACKAGESEP TOK_ID {
402 if ($3->compare(0, 1, "\\") == 0)
403 *$1 += "::" + $3->substr(1);
409 hierarchical_id '.' TOK_ID {
410 if ($3->compare(0, 1, "\\") == 0)
411 *$1 += "." + $3->substr(1);
418 hierarchical_type_id:
420 | TOK_PKG_USER_TYPE // package qualified type name
421 | '(' TOK_USER_TYPE ')' { $$ = $2; } // non-standard grammar
428 do_not_require_port_stubs = false;
429 AstNode *mod = new AstNode(AST_MODULE);
430 ast_stack.back()->children.push_back(mod);
431 ast_stack.push_back(mod);
432 current_ast_mod = mod;
436 append_attr(mod, $1);
438 } module_para_opt module_args_opt ';' module_body TOK_ENDMODULE {
439 if (port_stubs.size() != 0)
440 frontend_verilog_yyerror("Missing details for module port `%s'.",
441 port_stubs.begin()->first.c_str());
442 SET_AST_NODE_LOC(ast_stack.back(), @2, @$);
443 ast_stack.pop_back();
444 log_assert(ast_stack.size() == 1);
445 current_ast_mod = NULL;
450 '#' '(' { astbuf1 = nullptr; } module_para_list { if (astbuf1) delete astbuf1; } ')' | /* empty */;
453 single_module_para | module_para_list ',' single_module_para;
458 if (astbuf1) delete astbuf1;
459 astbuf1 = new AstNode(AST_PARAMETER);
460 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
461 append_attr(astbuf1, $1);
462 } param_type single_param_decl |
463 attr TOK_LOCALPARAM {
464 if (astbuf1) delete astbuf1;
465 astbuf1 = new AstNode(AST_LOCALPARAM);
466 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
467 append_attr(astbuf1, $1);
468 } param_type single_param_decl |
472 '(' ')' | /* empty */ | '(' module_args optional_comma ')';
475 module_arg | module_args ',' module_arg;
480 module_arg_opt_assignment:
482 if (ast_stack.back()->children.size() > 0 && ast_stack.back()->children.back()->type == AST_WIRE) {
483 AstNode *wire = new AstNode(AST_IDENTIFIER);
484 wire->str = ast_stack.back()->children.back()->str;
485 if (ast_stack.back()->children.back()->is_input) {
486 AstNode *n = ast_stack.back()->children.back();
487 if (n->attributes.count(ID::defaultvalue))
488 delete n->attributes.at(ID::defaultvalue);
489 n->attributes[ID::defaultvalue] = $2;
491 if (ast_stack.back()->children.back()->is_reg || ast_stack.back()->children.back()->is_logic)
492 ast_stack.back()->children.push_back(new AstNode(AST_INITIAL, new AstNode(AST_BLOCK, new AstNode(AST_ASSIGN_LE, wire, $2))));
494 ast_stack.back()->children.push_back(new AstNode(AST_ASSIGN, wire, $2));
496 frontend_verilog_yyerror("SystemVerilog interface in module port list cannot have a default value.");
502 if (ast_stack.back()->children.size() > 0 && ast_stack.back()->children.back()->type == AST_WIRE) {
503 AstNode *node = ast_stack.back()->children.back()->clone();
505 node->port_id = ++port_counter;
506 ast_stack.back()->children.push_back(node);
507 SET_AST_NODE_LOC(node, @1, @1);
509 if (port_stubs.count(*$1) != 0)
510 frontend_verilog_yyerror("Duplicate module port `%s'.", $1->c_str());
511 port_stubs[*$1] = ++port_counter;
514 } module_arg_opt_assignment |
516 astbuf1 = new AstNode(AST_INTERFACEPORT);
517 astbuf1->children.push_back(new AstNode(AST_INTERFACEPORTTYPE));
518 astbuf1->children[0]->str = *$1;
520 } TOK_ID { /* SV interfaces */
522 frontend_verilog_yyerror("Interface found in port list (%s). This is not supported unless read_verilog is called with -sv!", $3->c_str());
523 astbuf2 = astbuf1->clone(); // really only needed if multiple instances of same type.
526 astbuf2->port_id = ++port_counter;
527 ast_stack.back()->children.push_back(astbuf2);
528 delete astbuf1; // really only needed if multiple instances of same type.
529 } module_arg_opt_assignment |
530 attr wire_type range TOK_ID {
533 SET_AST_NODE_LOC(node, @4, @4);
534 node->port_id = ++port_counter;
536 node->children.push_back($3);
537 if (!node->is_input && !node->is_output)
538 frontend_verilog_yyerror("Module port `%s' is neither input nor output.", $4->c_str());
539 if (node->is_reg && node->is_input && !node->is_output && !sv_mode)
540 frontend_verilog_yyerror("Input port `%s' is declared as register.", $4->c_str());
541 ast_stack.back()->children.push_back(node);
542 append_attr(node, $1);
544 } module_arg_opt_assignment |
546 do_not_require_port_stubs = true;
553 AstNode *mod = new AstNode(AST_PACKAGE);
554 ast_stack.back()->children.push_back(mod);
555 ast_stack.push_back(mod);
556 current_ast_mod = mod;
558 append_attr(mod, $1);
559 } ';' package_body TOK_ENDPACKAGE {
560 ast_stack.pop_back();
561 current_ast_mod = NULL;
566 package_body package_body_stmt
580 do_not_require_port_stubs = false;
581 AstNode *intf = new AstNode(AST_INTERFACE);
582 ast_stack.back()->children.push_back(intf);
583 ast_stack.push_back(intf);
584 current_ast_mod = intf;
589 } module_para_opt module_args_opt ';' interface_body TOK_ENDINTERFACE {
590 if (port_stubs.size() != 0)
591 frontend_verilog_yyerror("Missing details for module port `%s'.",
592 port_stubs.begin()->first.c_str());
593 ast_stack.pop_back();
594 log_assert(ast_stack.size() == 1);
595 current_ast_mod = NULL;
600 interface_body interface_body_stmt |;
603 param_decl | localparam_decl | typedef_decl | defparam_decl | wire_decl | always_stmt | assign_stmt |
607 '#' TOK_ID { delete $2; } |
608 '#' TOK_CONSTVAL { delete $2; } |
609 '#' TOK_REALVAL { delete $2; } |
610 '#' '(' expr ')' { delete $3; } |
611 '#' '(' expr ':' expr ':' expr ')' { delete $3; delete $5; delete $7; };
614 non_opt_delay | /* empty */;
618 astbuf3 = new AstNode(AST_WIRE);
619 current_wire_rand = false;
620 current_wire_const = false;
621 } wire_type_token_list {
623 SET_RULE_LOC(@$, @2, @$);
626 wire_type_token_list:
628 wire_type_token_list wire_type_token |
630 hierarchical_type_id {
631 astbuf3->is_custom_type = true;
632 astbuf3->children.push_back(new AstNode(AST_WIRETYPE));
633 astbuf3->children.back()->str = *$1;
639 astbuf3->is_input = true;
642 astbuf3->is_output = true;
645 astbuf3->is_input = true;
646 astbuf3->is_output = true;
653 astbuf3->is_wor = true;
656 astbuf3->is_wand = true;
659 astbuf3->is_reg = true;
662 astbuf3->is_logic = true;
665 astbuf3->is_logic = true;
668 astbuf3->is_reg = true;
669 astbuf3->range_left = 31;
670 astbuf3->range_right = 0;
671 astbuf3->is_signed = true;
674 astbuf3->type = AST_GENVAR;
675 astbuf3->is_reg = true;
676 astbuf3->is_signed = true;
677 astbuf3->range_left = 31;
678 astbuf3->range_right = 0;
681 astbuf3->is_signed = true;
684 current_wire_rand = true;
687 current_wire_const = true;
691 '[' expr ':' expr ']' {
692 $$ = new AstNode(AST_RANGE);
693 $$->children.push_back($2);
694 $$->children.push_back($4);
696 '[' expr TOK_POS_INDEXED expr ']' {
697 $$ = new AstNode(AST_RANGE);
698 AstNode *expr = new AstNode(AST_SELFSZ, $2);
699 $$->children.push_back(new AstNode(AST_SUB, new AstNode(AST_ADD, expr->clone(), $4), AstNode::mkconst_int(1, true)));
700 $$->children.push_back(new AstNode(AST_ADD, expr, AstNode::mkconst_int(0, true)));
702 '[' expr TOK_NEG_INDEXED expr ']' {
703 $$ = new AstNode(AST_RANGE);
704 AstNode *expr = new AstNode(AST_SELFSZ, $2);
705 $$->children.push_back(new AstNode(AST_ADD, expr, AstNode::mkconst_int(0, true)));
706 $$->children.push_back(new AstNode(AST_SUB, new AstNode(AST_ADD, expr->clone(), AstNode::mkconst_int(1, true)), $4));
709 $$ = new AstNode(AST_RANGE);
710 $$->children.push_back($2);
714 non_opt_range non_opt_range {
715 $$ = new AstNode(AST_MULTIRANGE, $1, $2);
717 non_opt_multirange non_opt_range {
719 $$->children.push_back($2);
732 non_opt_multirange { $$ = $1; };
736 | TOK_INTEGER { $$ = makeRange(); }
740 module_body module_body_stmt |
741 /* the following line makes the generate..endgenrate keywords optional */
742 module_body gen_stmt |
746 task_func_decl | specify_block | param_decl | localparam_decl | typedef_decl | defparam_decl | specparam_declaration | wire_decl | assign_stmt | cell_stmt |
747 enum_decl | struct_decl |
748 always_stmt | TOK_GENERATE module_gen_body TOK_ENDGENERATE | defattr | assert_property | checker_decl | ignored_specify_block;
751 TOK_CHECKER TOK_ID ';' {
752 AstNode *node = new AstNode(AST_GENBLOCK);
754 ast_stack.back()->children.push_back(node);
755 ast_stack.push_back(node);
756 } module_body TOK_ENDCHECKER {
758 ast_stack.pop_back();
762 attr TOK_DPI_FUNCTION TOK_ID TOK_ID {
763 current_function_or_task = new AstNode(AST_DPI_FUNCTION, AstNode::mkconst_str(*$3), AstNode::mkconst_str(*$4));
764 current_function_or_task->str = *$4;
765 append_attr(current_function_or_task, $1);
766 ast_stack.back()->children.push_back(current_function_or_task);
769 } opt_dpi_function_args ';' {
770 current_function_or_task = NULL;
772 attr TOK_DPI_FUNCTION TOK_ID '=' TOK_ID TOK_ID {
773 current_function_or_task = new AstNode(AST_DPI_FUNCTION, AstNode::mkconst_str(*$5), AstNode::mkconst_str(*$3));
774 current_function_or_task->str = *$6;
775 append_attr(current_function_or_task, $1);
776 ast_stack.back()->children.push_back(current_function_or_task);
780 } opt_dpi_function_args ';' {
781 current_function_or_task = NULL;
783 attr TOK_DPI_FUNCTION TOK_ID ':' TOK_ID '=' TOK_ID TOK_ID {
784 current_function_or_task = new AstNode(AST_DPI_FUNCTION, AstNode::mkconst_str(*$7), AstNode::mkconst_str(*$3 + ":" + RTLIL::unescape_id(*$5)));
785 current_function_or_task->str = *$8;
786 append_attr(current_function_or_task, $1);
787 ast_stack.back()->children.push_back(current_function_or_task);
792 } opt_dpi_function_args ';' {
793 current_function_or_task = NULL;
795 attr TOK_TASK opt_automatic TOK_ID {
796 current_function_or_task = new AstNode(AST_TASK);
797 current_function_or_task->str = *$4;
798 append_attr(current_function_or_task, $1);
799 ast_stack.back()->children.push_back(current_function_or_task);
800 ast_stack.push_back(current_function_or_task);
801 current_function_or_task_port_id = 1;
803 } task_func_args_opt ';' task_func_body TOK_ENDTASK {
804 current_function_or_task = NULL;
805 ast_stack.pop_back();
807 attr TOK_FUNCTION opt_automatic opt_signed range_or_signed_int TOK_ID {
808 current_function_or_task = new AstNode(AST_FUNCTION);
809 current_function_or_task->str = *$6;
810 append_attr(current_function_or_task, $1);
811 ast_stack.back()->children.push_back(current_function_or_task);
812 ast_stack.push_back(current_function_or_task);
813 AstNode *outreg = new AstNode(AST_WIRE);
815 outreg->is_signed = $4;
816 outreg->is_reg = true;
818 outreg->children.push_back($5);
819 outreg->is_signed = $4 || $5->is_signed;
820 $5->is_signed = false;
822 current_function_or_task->children.push_back(outreg);
823 current_function_or_task_port_id = 1;
825 } task_func_args_opt ';' task_func_body TOK_ENDFUNCTION {
826 current_function_or_task = NULL;
827 ast_stack.pop_back();
832 current_function_or_task->children.push_back(AstNode::mkconst_str(*$1));
837 current_function_or_task->children.push_back(AstNode::mkconst_str(*$1));
841 opt_dpi_function_args:
842 '(' dpi_function_args ')' |
846 dpi_function_args ',' dpi_function_arg |
847 dpi_function_args ',' |
864 '(' ')' | /* empty */ | '(' {
868 } task_func_args optional_comma {
876 task_func_port | task_func_args ',' task_func_port;
879 attr wire_type range {
888 astbuf2 = checkRange(astbuf1, $3);
893 frontend_verilog_yyerror("task/function argument direction missing");
894 albuf = new dict<IdString, AstNode*>;
895 astbuf1 = new AstNode(AST_WIRE);
896 current_wire_rand = false;
897 current_wire_const = false;
898 astbuf1->is_input = true;
904 task_func_body behavioral_stmt |
907 /*************************** specify parser ***************************/
910 TOK_SPECIFY specify_item_list TOK_ENDSPECIFY;
913 specify_item specify_item_list |
917 specify_if '(' specify_edge expr TOK_SPECIFY_OPER specify_target ')' '=' specify_rise_fall ';' {
918 AstNode *en_expr = $1;
919 char specify_edge = $3;
920 AstNode *src_expr = $4;
922 specify_target *target = $6;
923 specify_rise_fall *timing = $9;
925 if (specify_edge != 0 && target->dat == nullptr)
926 frontend_verilog_yyerror("Found specify edge but no data spec.\n");
928 AstNode *cell = new AstNode(AST_CELL);
929 ast_stack.back()->children.push_back(cell);
930 cell->str = stringf("$specify$%d", autoidx++);
931 cell->children.push_back(new AstNode(AST_CELLTYPE));
932 cell->children.back()->str = target->dat ? "$specify3" : "$specify2";
933 SET_AST_NODE_LOC(cell, en_expr ? @1 : @2, @10);
935 char oper_polarity = 0;
936 char oper_type = oper->at(0);
938 if (oper->size() == 3) {
939 oper_polarity = oper->at(0);
940 oper_type = oper->at(1);
943 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(oper_type == '*', false, 1)));
944 cell->children.back()->str = "\\FULL";
946 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(oper_polarity != 0, false, 1)));
947 cell->children.back()->str = "\\SRC_DST_PEN";
949 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(oper_polarity == '+', false, 1)));
950 cell->children.back()->str = "\\SRC_DST_POL";
952 cell->children.push_back(new AstNode(AST_PARASET, timing->rise.t_min));
953 cell->children.back()->str = "\\T_RISE_MIN";
955 cell->children.push_back(new AstNode(AST_PARASET, timing->rise.t_avg));
956 cell->children.back()->str = "\\T_RISE_TYP";
958 cell->children.push_back(new AstNode(AST_PARASET, timing->rise.t_max));
959 cell->children.back()->str = "\\T_RISE_MAX";
961 cell->children.push_back(new AstNode(AST_PARASET, timing->fall.t_min));
962 cell->children.back()->str = "\\T_FALL_MIN";
964 cell->children.push_back(new AstNode(AST_PARASET, timing->fall.t_avg));
965 cell->children.back()->str = "\\T_FALL_TYP";
967 cell->children.push_back(new AstNode(AST_PARASET, timing->fall.t_max));
968 cell->children.back()->str = "\\T_FALL_MAX";
970 cell->children.push_back(new AstNode(AST_ARGUMENT, en_expr ? en_expr : AstNode::mkconst_int(1, false, 1)));
971 cell->children.back()->str = "\\EN";
973 cell->children.push_back(new AstNode(AST_ARGUMENT, src_expr));
974 cell->children.back()->str = "\\SRC";
976 cell->children.push_back(new AstNode(AST_ARGUMENT, target->dst));
977 cell->children.back()->str = "\\DST";
981 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(specify_edge != 0, false, 1)));
982 cell->children.back()->str = "\\EDGE_EN";
984 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(specify_edge == 'p', false, 1)));
985 cell->children.back()->str = "\\EDGE_POL";
987 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(target->polarity_op != 0, false, 1)));
988 cell->children.back()->str = "\\DAT_DST_PEN";
990 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(target->polarity_op == '+', false, 1)));
991 cell->children.back()->str = "\\DAT_DST_POL";
993 cell->children.push_back(new AstNode(AST_ARGUMENT, target->dat));
994 cell->children.back()->str = "\\DAT";
1001 TOK_ID '(' specify_edge expr specify_condition ',' specify_edge expr specify_condition ',' specify_triple specify_opt_triple ')' ';' {
1002 if (*$1 != "$setup" && *$1 != "$hold" && *$1 != "$setuphold" && *$1 != "$removal" && *$1 != "$recovery" &&
1003 *$1 != "$recrem" && *$1 != "$skew" && *$1 != "$timeskew" && *$1 != "$fullskew" && *$1 != "$nochange")
1004 frontend_verilog_yyerror("Unsupported specify rule type: %s\n", $1->c_str());
1006 AstNode *src_pen = AstNode::mkconst_int($3 != 0, false, 1);
1007 AstNode *src_pol = AstNode::mkconst_int($3 == 'p', false, 1);
1008 AstNode *src_expr = $4, *src_en = $5 ? $5 : AstNode::mkconst_int(1, false, 1);
1010 AstNode *dst_pen = AstNode::mkconst_int($7 != 0, false, 1);
1011 AstNode *dst_pol = AstNode::mkconst_int($7 == 'p', false, 1);
1012 AstNode *dst_expr = $8, *dst_en = $9 ? $9 : AstNode::mkconst_int(1, false, 1);
1014 specify_triple *limit = $11;
1015 specify_triple *limit2 = $12;
1017 AstNode *cell = new AstNode(AST_CELL);
1018 ast_stack.back()->children.push_back(cell);
1019 cell->str = stringf("$specify$%d", autoidx++);
1020 cell->children.push_back(new AstNode(AST_CELLTYPE));
1021 cell->children.back()->str = "$specrule";
1022 SET_AST_NODE_LOC(cell, @1, @14);
1024 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_str(*$1)));
1025 cell->children.back()->str = "\\TYPE";
1027 cell->children.push_back(new AstNode(AST_PARASET, limit->t_min));
1028 cell->children.back()->str = "\\T_LIMIT_MIN";
1030 cell->children.push_back(new AstNode(AST_PARASET, limit->t_avg));
1031 cell->children.back()->str = "\\T_LIMIT_TYP";
1033 cell->children.push_back(new AstNode(AST_PARASET, limit->t_max));
1034 cell->children.back()->str = "\\T_LIMIT_MAX";
1036 cell->children.push_back(new AstNode(AST_PARASET, limit2 ? limit2->t_min : AstNode::mkconst_int(0, true)));
1037 cell->children.back()->str = "\\T_LIMIT2_MIN";
1039 cell->children.push_back(new AstNode(AST_PARASET, limit2 ? limit2->t_avg : AstNode::mkconst_int(0, true)));
1040 cell->children.back()->str = "\\T_LIMIT2_TYP";
1042 cell->children.push_back(new AstNode(AST_PARASET, limit2 ? limit2->t_max : AstNode::mkconst_int(0, true)));
1043 cell->children.back()->str = "\\T_LIMIT2_MAX";
1045 cell->children.push_back(new AstNode(AST_PARASET, src_pen));
1046 cell->children.back()->str = "\\SRC_PEN";
1048 cell->children.push_back(new AstNode(AST_PARASET, src_pol));
1049 cell->children.back()->str = "\\SRC_POL";
1051 cell->children.push_back(new AstNode(AST_PARASET, dst_pen));
1052 cell->children.back()->str = "\\DST_PEN";
1054 cell->children.push_back(new AstNode(AST_PARASET, dst_pol));
1055 cell->children.back()->str = "\\DST_POL";
1057 cell->children.push_back(new AstNode(AST_ARGUMENT, src_en));
1058 cell->children.back()->str = "\\SRC_EN";
1060 cell->children.push_back(new AstNode(AST_ARGUMENT, src_expr));
1061 cell->children.back()->str = "\\SRC";
1063 cell->children.push_back(new AstNode(AST_ARGUMENT, dst_en));
1064 cell->children.back()->str = "\\DST_EN";
1066 cell->children.push_back(new AstNode(AST_ARGUMENT, dst_expr));
1067 cell->children.back()->str = "\\DST";
1073 ',' specify_triple {
1081 TOK_IF '(' expr ')' {
1089 TOK_SPECIFY_AND expr {
1098 $$ = new specify_target;
1099 $$->polarity_op = 0;
1103 '(' expr ':' expr ')'{
1104 $$ = new specify_target;
1105 $$->polarity_op = 0;
1109 '(' expr TOK_NEG_INDEXED expr ')'{
1110 $$ = new specify_target;
1111 $$->polarity_op = '-';
1115 '(' expr TOK_POS_INDEXED expr ')'{
1116 $$ = new specify_target;
1117 $$->polarity_op = '+';
1123 TOK_POSEDGE { $$ = 'p'; } |
1124 TOK_NEGEDGE { $$ = 'n'; } |
1129 $$ = new specify_rise_fall;
1131 $$->fall.t_min = $1->t_min->clone();
1132 $$->fall.t_avg = $1->t_avg->clone();
1133 $$->fall.t_max = $1->t_max->clone();
1136 '(' specify_triple ',' specify_triple ')' {
1137 $$ = new specify_rise_fall;
1143 '(' specify_triple ',' specify_triple ',' specify_triple ')' {
1144 $$ = new specify_rise_fall;
1150 log_file_warning(current_filename, get_line_num(), "Path delay expressions beyond rise/fall not currently supported. Ignoring.\n");
1152 '(' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ')' {
1153 $$ = new specify_rise_fall;
1162 log_file_warning(current_filename, get_line_num(), "Path delay expressions beyond rise/fall not currently supported. Ignoring.\n");
1164 '(' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ')' {
1165 $$ = new specify_rise_fall;
1180 log_file_warning(current_filename, get_line_num(), "Path delay expressions beyond rise/fall not currently supported. Ignoring.\n");
1185 $$ = new specify_triple;
1187 $$->t_avg = $1->clone();
1188 $$->t_max = $1->clone();
1190 expr ':' expr ':' expr {
1191 $$ = new specify_triple;
1197 /******************** ignored specify parser **************************/
1199 ignored_specify_block:
1200 TOK_IGNORED_SPECIFY ignored_specify_item_opt TOK_ENDSPECIFY |
1201 TOK_IGNORED_SPECIFY TOK_ENDSPECIFY ;
1203 ignored_specify_item_opt:
1204 ignored_specify_item_opt ignored_specify_item |
1205 ignored_specify_item ;
1207 ignored_specify_item:
1208 specparam_declaration
1209 // | pulsestyle_declaration
1210 // | showcancelled_declaration
1212 | system_timing_declaration
1215 specparam_declaration:
1216 TOK_SPECPARAM list_of_specparam_assignments ';' |
1217 TOK_SPECPARAM specparam_range list_of_specparam_assignments ';' ;
1219 // IEEE 1364-2005 calls this sinmply 'range' but the current 'range' rule allows empty match
1220 // and the 'non_opt_range' rule allows index ranges not allowed by 1364-2005
1221 // exxxxtending this for SV specparam would change this anyhow
1223 '[' ignspec_constant_expression ':' ignspec_constant_expression ']' ;
1225 list_of_specparam_assignments:
1226 specparam_assignment | list_of_specparam_assignments ',' specparam_assignment;
1228 specparam_assignment:
1229 ignspec_id '=' ignspec_expr ;
1232 TOK_IF '(' ignspec_expr ')' | /* empty */;
1235 simple_path_declaration ';'
1236 // | edge_sensitive_path_declaration
1237 // | state_dependent_path_declaration
1240 simple_path_declaration :
1241 ignspec_opt_cond parallel_path_description '=' path_delay_value |
1242 ignspec_opt_cond full_path_description '=' path_delay_value
1246 '(' ignspec_expr list_of_path_delay_extra_expressions ')'
1248 | ignspec_expr list_of_path_delay_extra_expressions
1251 list_of_path_delay_extra_expressions :
1253 | ',' ignspec_expr list_of_path_delay_extra_expressions
1256 specify_edge_identifier :
1257 TOK_POSEDGE | TOK_NEGEDGE ;
1259 parallel_path_description :
1260 '(' specify_input_terminal_descriptor opt_polarity_operator '=' '>' specify_output_terminal_descriptor ')' |
1261 '(' specify_edge_identifier specify_input_terminal_descriptor '=' '>' '(' specify_output_terminal_descriptor opt_polarity_operator ':' ignspec_expr ')' ')' |
1262 '(' specify_edge_identifier specify_input_terminal_descriptor '=' '>' '(' specify_output_terminal_descriptor TOK_POS_INDEXED ignspec_expr ')' ')' ;
1264 full_path_description :
1265 '(' list_of_path_inputs '*' '>' list_of_path_outputs ')' |
1266 '(' specify_edge_identifier list_of_path_inputs '*' '>' '(' list_of_path_outputs opt_polarity_operator ':' ignspec_expr ')' ')' |
1267 '(' specify_edge_identifier list_of_path_inputs '*' '>' '(' list_of_path_outputs TOK_POS_INDEXED ignspec_expr ')' ')' ;
1269 // This was broken into 2 rules to solve shift/reduce conflicts
1270 list_of_path_inputs :
1271 specify_input_terminal_descriptor opt_polarity_operator |
1272 specify_input_terminal_descriptor more_path_inputs opt_polarity_operator ;
1275 ',' specify_input_terminal_descriptor |
1276 more_path_inputs ',' specify_input_terminal_descriptor ;
1278 list_of_path_outputs :
1279 specify_output_terminal_descriptor |
1280 list_of_path_outputs ',' specify_output_terminal_descriptor ;
1282 opt_polarity_operator :
1287 // Good enough for the time being
1288 specify_input_terminal_descriptor :
1291 // Good enough for the time being
1292 specify_output_terminal_descriptor :
1295 system_timing_declaration :
1296 ignspec_id '(' system_timing_args ')' ';' ;
1299 TOK_POSEDGE ignspec_id |
1300 TOK_NEGEDGE ignspec_id |
1303 system_timing_args :
1305 system_timing_args TOK_IGNORED_SPECIFY_AND system_timing_arg |
1306 system_timing_args ',' system_timing_arg ;
1308 // for the time being this is OK, but we may write our own expr here.
1309 // as I'm not sure it is legal to use a full expr here (probably not)
1310 // On the other hand, other rules requiring constant expressions also use 'expr'
1311 // (such as param assignment), so we may leave this as-is, perhaps adding runtime checks for constant-ness
1312 ignspec_constant_expression:
1313 expr { delete $1; };
1316 expr { delete $1; } |
1317 expr ':' expr ':' expr {
1324 TOK_ID { delete $1; }
1325 range_or_multirange { delete $3; };
1327 /**********************************************************************/
1331 astbuf1->is_signed = true;
1333 astbuf1->is_signed = false;
1338 if (astbuf1->children.size() != 1)
1339 frontend_verilog_yyerror("Internal error in param_integer - should not happen?");
1340 astbuf1->children.push_back(new AstNode(AST_RANGE));
1341 astbuf1->children.back()->children.push_back(AstNode::mkconst_int(31, true));
1342 astbuf1->children.back()->children.push_back(AstNode::mkconst_int(0, true));
1343 astbuf1->is_signed = true;
1348 if (astbuf1->children.size() != 1)
1349 frontend_verilog_yyerror("Parameter already declared as integer, cannot set to real.");
1350 astbuf1->children.push_back(new AstNode(AST_REALVALUE));
1356 if (astbuf1->children.size() != 1)
1357 frontend_verilog_yyerror("integer/real parameters should not have a range.");
1358 astbuf1->children.push_back($1);
1362 param_integer_type: param_integer param_signed
1363 param_range_type: type_vec param_signed param_range
1364 param_implicit_type: param_signed param_range
1367 param_integer_type | param_real | param_range_type | param_implicit_type |
1368 hierarchical_type_id {
1369 astbuf1->is_custom_type = true;
1370 astbuf1->children.push_back(new AstNode(AST_WIRETYPE));
1371 astbuf1->children.back()->str = *$1;
1375 attr TOK_PARAMETER {
1376 astbuf1 = new AstNode(AST_PARAMETER);
1377 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
1378 append_attr(astbuf1, $1);
1379 } param_type param_decl_list ';' {
1384 attr TOK_LOCALPARAM {
1385 astbuf1 = new AstNode(AST_LOCALPARAM);
1386 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
1387 append_attr(astbuf1, $1);
1388 } param_type param_decl_list ';' {
1393 single_param_decl | param_decl_list ',' single_param_decl;
1398 if (astbuf1 == nullptr) {
1400 frontend_verilog_yyerror("In pure Verilog (not SystemVerilog), parameter/localparam with an initializer must use the parameter/localparam keyword");
1401 node = new AstNode(AST_PARAMETER);
1402 node->children.push_back(AstNode::mkconst_int(0, true));
1404 node = astbuf1->clone();
1407 delete node->children[0];
1408 node->children[0] = $3;
1409 ast_stack.back()->children.push_back(node);
1414 TOK_DEFPARAM defparam_decl_list ';';
1417 single_defparam_decl | defparam_decl_list ',' single_defparam_decl;
1419 single_defparam_decl:
1420 range rvalue '=' expr {
1421 AstNode *node = new AstNode(AST_DEFPARAM);
1422 node->children.push_back($2);
1423 node->children.push_back($4);
1425 node->children.push_back($1);
1426 ast_stack.back()->children.push_back(node);
1433 enum_type: TOK_ENUM {
1434 static int enum_count;
1435 // create parent node for the enum
1436 astbuf2 = new AstNode(AST_ENUM);
1437 ast_stack.back()->children.push_back(astbuf2);
1438 astbuf2->str = std::string("$enum");
1439 astbuf2->str += std::to_string(enum_count++);
1440 // create the template for the names
1441 astbuf1 = new AstNode(AST_ENUM_ITEM);
1442 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
1443 } enum_base_type '{' enum_name_list '}' { // create template for the enum vars
1444 auto tnode = astbuf1->clone();
1447 tnode->type = AST_WIRE;
1448 tnode->attributes[ID::enum_type] = AstNode::mkconst_str(astbuf2->str);
1449 // drop constant but keep any range
1450 delete tnode->children[0];
1451 tnode->children.erase(tnode->children.begin());
1455 enum_base_type: type_atom type_signing
1456 | type_vec type_signing range { if ($3) astbuf1->children.push_back($3); }
1457 | /* nothing */ { astbuf1->is_reg = true; addRange(astbuf1); }
1460 type_atom: TOK_INTEGER { astbuf1->is_reg = true; addRange(astbuf1); } // 4-state signed
1461 | TOK_INT { astbuf1->is_reg = true; addRange(astbuf1); } // 2-state signed
1462 | TOK_SHORTINT { astbuf1->is_reg = true; addRange(astbuf1, 15, 0); } // 2-state signed
1463 | TOK_BYTE { astbuf1->is_reg = true; addRange(astbuf1, 7, 0); } // 2-state signed
1466 type_vec: TOK_REG { astbuf1->is_reg = true; } // unsigned
1467 | TOK_LOGIC { astbuf1->is_logic = true; } // unsigned
1471 TOK_SIGNED { astbuf1->is_signed = true; }
1472 | TOK_UNSIGNED { astbuf1->is_signed = false; }
1476 enum_name_list: enum_name_decl
1477 | enum_name_list ',' enum_name_decl
1481 TOK_ID opt_enum_init {
1483 log_assert(astbuf1);
1484 log_assert(astbuf2);
1485 auto node = astbuf1->clone();
1488 SET_AST_NODE_LOC(node, @1, @1);
1489 delete node->children[0];
1490 node->children[0] = $2 ? $2 : new AstNode(AST_NONE);
1491 astbuf2->children.push_back(node);
1496 '=' basic_expr { $$ = $2; } // TODO: restrict this
1497 | /* optional */ { $$ = NULL; }
1502 | enum_var_list ',' enum_var
1506 log_assert(astbuf1);
1507 log_assert(astbuf2);
1508 auto node = astbuf1->clone();
1509 ast_stack.back()->children.push_back(node);
1512 SET_AST_NODE_LOC(node, @1, @1);
1513 node->is_enum = true;
1517 enum_decl: enum_type enum_var_list ';' { delete $1; }
1524 struct_decl: struct_type struct_var_list ';' { delete astbuf2; }
1527 struct_type: struct_union { astbuf2 = $1; } struct_body { $$ = astbuf2; }
1531 TOK_STRUCT { $$ = new AstNode(AST_STRUCT); }
1532 | TOK_UNION { $$ = new AstNode(AST_UNION); }
1535 struct_body: opt_packed '{' struct_member_list '}'
1538 opt_packed: TOK_PACKED opt_signed_struct
1539 | { frontend_verilog_yyerror("Only PACKED supported at this time"); }
1543 TOK_SIGNED { astbuf2->is_signed = true; }
1544 | TOK_UNSIGNED { astbuf2->is_signed = false; }
1545 | // default is unsigned
1548 struct_member_list: struct_member
1549 | struct_member_list struct_member
1552 struct_member: struct_member_type member_name_list ';' { delete astbuf1; }
1557 | member_name_list ',' member_name
1560 member_name: TOK_ID {
1561 astbuf1->str = $1->substr(1);
1563 astbuf3 = astbuf1->clone();
1564 SET_AST_NODE_LOC(astbuf3, @1, @1);
1565 astbuf2->children.push_back(astbuf3);
1566 } range { if ($3) astbuf3->children.push_back($3); }
1569 struct_member_type: { astbuf1 = new AstNode(AST_STRUCT_ITEM); } member_type_token
1574 | hierarchical_type_id {
1575 // use a clone of the typedef definition nodes
1576 auto template_node = copyTypeDefinition(*$1);
1578 switch (template_node->type) {
1580 template_node->type = AST_STRUCT_ITEM;
1586 frontend_verilog_yyerror("Invalid type for struct member: %s", type2str(template_node->type).c_str());
1589 astbuf1 = template_node;
1592 // stash state on ast_stack
1593 ast_stack.push_back(astbuf2);
1598 astbuf2 = ast_stack.back();
1599 ast_stack.pop_back();
1603 member_type: type_atom type_signing
1604 | type_vec type_signing range_or_multirange { if ($3) astbuf1->children.push_back($3); }
1607 struct_var_list: struct_var
1608 | struct_var_list ',' struct_var
1611 struct_var: TOK_ID { auto *var_node = astbuf2->clone();
1612 var_node->str = *$1;
1614 SET_AST_NODE_LOC(var_node, @1, @1);
1615 ast_stack.back()->children.push_back(var_node);
1624 attr wire_type range {
1627 astbuf2 = checkRange(astbuf1, $3);
1628 } delay wire_name_list {
1630 if (astbuf2 != NULL)
1634 attr TOK_SUPPLY0 TOK_ID {
1635 ast_stack.back()->children.push_back(new AstNode(AST_WIRE));
1636 ast_stack.back()->children.back()->str = *$3;
1637 append_attr(ast_stack.back()->children.back(), $1);
1638 ast_stack.back()->children.push_back(new AstNode(AST_ASSIGN, new AstNode(AST_IDENTIFIER), AstNode::mkconst_int(0, false, 1)));
1639 ast_stack.back()->children.back()->children[0]->str = *$3;
1641 } opt_supply_wires ';' |
1642 attr TOK_SUPPLY1 TOK_ID {
1643 ast_stack.back()->children.push_back(new AstNode(AST_WIRE));
1644 ast_stack.back()->children.back()->str = *$3;
1645 append_attr(ast_stack.back()->children.back(), $1);
1646 ast_stack.back()->children.push_back(new AstNode(AST_ASSIGN, new AstNode(AST_IDENTIFIER), AstNode::mkconst_int(1, false, 1)));
1647 ast_stack.back()->children.back()->children[0]->str = *$3;
1649 } opt_supply_wires ';';
1653 opt_supply_wires ',' TOK_ID {
1654 AstNode *wire_node = ast_stack.back()->children.at(GetSize(ast_stack.back()->children)-2)->clone();
1655 AstNode *assign_node = ast_stack.back()->children.at(GetSize(ast_stack.back()->children)-1)->clone();
1656 wire_node->str = *$3;
1657 assign_node->children[0]->str = *$3;
1658 ast_stack.back()->children.push_back(wire_node);
1659 ast_stack.back()->children.push_back(assign_node);
1664 wire_name_and_opt_assign | wire_name_list ',' wire_name_and_opt_assign;
1666 wire_name_and_opt_assign:
1668 bool attr_anyconst = false;
1669 bool attr_anyseq = false;
1670 bool attr_allconst = false;
1671 bool attr_allseq = false;
1672 if (ast_stack.back()->children.back()->get_bool_attribute(ID::anyconst)) {
1673 delete ast_stack.back()->children.back()->attributes.at(ID::anyconst);
1674 ast_stack.back()->children.back()->attributes.erase(ID::anyconst);
1675 attr_anyconst = true;
1677 if (ast_stack.back()->children.back()->get_bool_attribute(ID::anyseq)) {
1678 delete ast_stack.back()->children.back()->attributes.at(ID::anyseq);
1679 ast_stack.back()->children.back()->attributes.erase(ID::anyseq);
1682 if (ast_stack.back()->children.back()->get_bool_attribute(ID::allconst)) {
1683 delete ast_stack.back()->children.back()->attributes.at(ID::allconst);
1684 ast_stack.back()->children.back()->attributes.erase(ID::allconst);
1685 attr_allconst = true;
1687 if (ast_stack.back()->children.back()->get_bool_attribute(ID::allseq)) {
1688 delete ast_stack.back()->children.back()->attributes.at(ID::allseq);
1689 ast_stack.back()->children.back()->attributes.erase(ID::allseq);
1692 if (current_wire_rand || attr_anyconst || attr_anyseq || attr_allconst || attr_allseq) {
1693 AstNode *wire = new AstNode(AST_IDENTIFIER);
1694 AstNode *fcall = new AstNode(AST_FCALL);
1695 wire->str = ast_stack.back()->children.back()->str;
1696 fcall->str = current_wire_const ? "\\$anyconst" : "\\$anyseq";
1698 fcall->str = "\\$anyconst";
1700 fcall->str = "\\$anyseq";
1702 fcall->str = "\\$allconst";
1704 fcall->str = "\\$allseq";
1705 fcall->attributes[ID::reg] = AstNode::mkconst_str(RTLIL::unescape_id(wire->str));
1706 ast_stack.back()->children.push_back(new AstNode(AST_ASSIGN, wire, fcall));
1709 wire_name '=' expr {
1710 AstNode *wire = new AstNode(AST_IDENTIFIER);
1711 wire->str = ast_stack.back()->children.back()->str;
1712 if (astbuf1->is_input) {
1713 if (astbuf1->attributes.count(ID::defaultvalue))
1714 delete astbuf1->attributes.at(ID::defaultvalue);
1715 astbuf1->attributes[ID::defaultvalue] = $3;
1717 else if (astbuf1->is_reg || astbuf1->is_logic){
1718 AstNode *assign = new AstNode(AST_ASSIGN_LE, wire, $3);
1719 AstNode *block = new AstNode(AST_BLOCK, assign);
1720 AstNode *init = new AstNode(AST_INITIAL, block);
1722 SET_AST_NODE_LOC(assign, @1, @3);
1723 SET_AST_NODE_LOC(block, @1, @3);
1724 SET_AST_NODE_LOC(init, @1, @3);
1726 ast_stack.back()->children.push_back(init);
1729 AstNode *assign = new AstNode(AST_ASSIGN, wire, $3);
1730 SET_AST_NODE_LOC(assign, @1, @3);
1731 ast_stack.back()->children.push_back(assign);
1737 TOK_ID range_or_multirange {
1738 if (astbuf1 == nullptr)
1739 frontend_verilog_yyerror("Internal error - should not happen - no AST_WIRE node.");
1740 AstNode *node = astbuf1->clone();
1742 append_attr_clone(node, albuf);
1743 if (astbuf2 != NULL)
1744 node->children.push_back(astbuf2->clone());
1746 if (node->is_input || node->is_output)
1747 frontend_verilog_yyerror("input/output/inout ports cannot have unpacked dimensions.");
1748 if (!astbuf2 && !node->is_custom_type) {
1749 addRange(node, 0, 0, false);
1751 rewriteAsMemoryNode(node, $2);
1753 if (current_function_or_task == NULL) {
1754 if (do_not_require_port_stubs && (node->is_input || node->is_output) && port_stubs.count(*$1) == 0) {
1755 port_stubs[*$1] = ++port_counter;
1757 if (port_stubs.count(*$1) != 0) {
1758 if (!node->is_input && !node->is_output)
1759 frontend_verilog_yyerror("Module port `%s' is neither input nor output.", $1->c_str());
1760 if (node->is_reg && node->is_input && !node->is_output && !sv_mode)
1761 frontend_verilog_yyerror("Input port `%s' is declared as register.", $1->c_str());
1762 node->port_id = port_stubs[*$1];
1763 port_stubs.erase(*$1);
1765 if (node->is_input || node->is_output)
1766 frontend_verilog_yyerror("Module port `%s' is not declared in module header.", $1->c_str());
1769 if (node->is_input || node->is_output)
1770 node->port_id = current_function_or_task_port_id++;
1772 //FIXME: for some reason, TOK_ID has a location which always points to one column *after* the real last column...
1773 SET_AST_NODE_LOC(node, @1, @1);
1774 ast_stack.back()->children.push_back(node);
1780 TOK_ASSIGN delay assign_expr_list ';';
1783 assign_expr | assign_expr_list ',' assign_expr;
1787 AstNode *node = new AstNode(AST_ASSIGN, $1, $3);
1788 SET_AST_NODE_LOC(node, @$, @$);
1789 ast_stack.back()->children.push_back(node);
1792 type_name: TOK_ID // first time seen
1793 | TOK_USER_TYPE { if (isInLocalScope($1)) frontend_verilog_yyerror("Duplicate declaration of TYPEDEF '%s'", $1->c_str()+1); }
1797 TOK_TYPEDEF wire_type range type_name range_or_multirange ';' {
1799 astbuf2 = checkRange(astbuf1, $3);
1801 astbuf1->children.push_back(astbuf2);
1805 addRange(astbuf1, 0, 0, false);
1807 rewriteAsMemoryNode(astbuf1, $5);
1809 addTypedefNode($4, astbuf1); }
1810 | TOK_TYPEDEF non_wire_data_type type_name ';' { addTypedefNode($3, $2); }
1820 astbuf1 = new AstNode(AST_CELL);
1821 append_attr(astbuf1, $1);
1822 astbuf1->children.push_back(new AstNode(AST_CELLTYPE));
1823 astbuf1->children[0]->str = *$2;
1825 } cell_parameter_list_opt cell_list ';' {
1828 attr tok_prim_wrapper delay {
1829 astbuf1 = new AstNode(AST_PRIMITIVE);
1831 append_attr(astbuf1, $1);
1842 $$ = new std::string("or");
1847 cell_list ',' single_cell;
1851 astbuf2 = astbuf1->clone();
1852 if (astbuf2->type != AST_PRIMITIVE)
1855 ast_stack.back()->children.push_back(astbuf2);
1856 } '(' cell_port_list ')' {
1857 SET_AST_NODE_LOC(astbuf2, @1, @$);
1859 TOK_ID non_opt_range {
1860 astbuf2 = astbuf1->clone();
1861 if (astbuf2->type != AST_PRIMITIVE)
1864 ast_stack.back()->children.push_back(new AstNode(AST_CELLARRAY, $2, astbuf2));
1865 } '(' cell_port_list ')'{
1866 SET_AST_NODE_LOC(astbuf2, @1, @$);
1871 prim_list ',' single_prim;
1876 astbuf2 = astbuf1->clone();
1877 ast_stack.back()->children.push_back(astbuf2);
1878 } '(' cell_port_list ')' {
1879 SET_AST_NODE_LOC(astbuf2, @1, @$);
1882 cell_parameter_list_opt:
1883 '#' '(' cell_parameter_list ')' | /* empty */;
1885 cell_parameter_list:
1886 cell_parameter | cell_parameter_list ',' cell_parameter;
1891 AstNode *node = new AstNode(AST_PARASET);
1892 astbuf1->children.push_back(node);
1893 node->children.push_back($1);
1895 '.' TOK_ID '(' expr ')' {
1896 AstNode *node = new AstNode(AST_PARASET);
1898 astbuf1->children.push_back(node);
1899 node->children.push_back($4);
1904 cell_port_list_rules {
1905 // remove empty args from end of list
1906 while (!astbuf2->children.empty()) {
1907 AstNode *node = astbuf2->children.back();
1908 if (node->type != AST_ARGUMENT) break;
1909 if (!node->children.empty()) break;
1910 if (!node->str.empty()) break;
1911 astbuf2->children.pop_back();
1916 bool has_positional_args = false;
1917 bool has_named_args = false;
1918 for (auto node : astbuf2->children) {
1919 if (node->type != AST_ARGUMENT) continue;
1920 if (node->str.empty())
1921 has_positional_args = true;
1923 has_named_args = true;
1926 if (has_positional_args && has_named_args)
1927 frontend_verilog_yyerror("Mix of positional and named cell ports.");
1930 cell_port_list_rules:
1931 cell_port | cell_port_list_rules ',' cell_port;
1935 AstNode *node = new AstNode(AST_ARGUMENT);
1936 astbuf2->children.push_back(node);
1940 AstNode *node = new AstNode(AST_ARGUMENT);
1941 astbuf2->children.push_back(node);
1942 node->children.push_back($2);
1945 attr '.' TOK_ID '(' expr ')' {
1946 AstNode *node = new AstNode(AST_ARGUMENT);
1948 astbuf2->children.push_back(node);
1949 node->children.push_back($5);
1953 attr '.' TOK_ID '(' ')' {
1954 AstNode *node = new AstNode(AST_ARGUMENT);
1956 astbuf2->children.push_back(node);
1961 AstNode *node = new AstNode(AST_ARGUMENT);
1963 astbuf2->children.push_back(node);
1964 node->children.push_back(new AstNode(AST_IDENTIFIER));
1965 node->children.back()->str = *$3;
1969 attr TOK_WILDCARD_CONNECT {
1971 frontend_verilog_yyerror("Wildcard port connections are only supported in SystemVerilog mode.");
1972 astbuf2->attributes[ID::wildcard_port_conns] = AstNode::mkconst_int(1, false);
1975 always_comb_or_latch:
1983 always_or_always_ff:
1992 attr always_or_always_ff {
1993 AstNode *node = new AstNode(AST_ALWAYS);
1994 append_attr(node, $1);
1996 node->attributes[ID::always_ff] = AstNode::mkconst_int(1, false);
1997 ast_stack.back()->children.push_back(node);
1998 ast_stack.push_back(node);
2000 AstNode *block = new AstNode(AST_BLOCK);
2001 ast_stack.back()->children.push_back(block);
2002 ast_stack.push_back(block);
2004 SET_AST_NODE_LOC(ast_stack.back(), @6, @6);
2005 ast_stack.pop_back();
2007 SET_AST_NODE_LOC(ast_stack.back(), @2, @$);
2008 ast_stack.pop_back();
2010 SET_RULE_LOC(@$, @2, @$);
2012 attr always_comb_or_latch {
2013 AstNode *node = new AstNode(AST_ALWAYS);
2014 append_attr(node, $1);
2016 node->attributes[ID::always_latch] = AstNode::mkconst_int(1, false);
2018 node->attributes[ID::always_comb] = AstNode::mkconst_int(1, false);
2019 ast_stack.back()->children.push_back(node);
2020 ast_stack.push_back(node);
2021 AstNode *block = new AstNode(AST_BLOCK);
2022 ast_stack.back()->children.push_back(block);
2023 ast_stack.push_back(block);
2025 ast_stack.pop_back();
2026 ast_stack.pop_back();
2029 AstNode *node = new AstNode(AST_INITIAL);
2030 append_attr(node, $1);
2031 ast_stack.back()->children.push_back(node);
2032 ast_stack.push_back(node);
2033 AstNode *block = new AstNode(AST_BLOCK);
2034 ast_stack.back()->children.push_back(block);
2035 ast_stack.push_back(block);
2037 ast_stack.pop_back();
2038 ast_stack.pop_back();
2042 '@' '(' always_events ')' |
2044 '@' ATTR_BEGIN ')' |
2051 always_events TOK_OR always_event |
2052 always_events ',' always_event;
2056 AstNode *node = new AstNode(AST_POSEDGE);
2057 SET_AST_NODE_LOC(node, @1, @1);
2058 ast_stack.back()->children.push_back(node);
2059 node->children.push_back($2);
2062 AstNode *node = new AstNode(AST_NEGEDGE);
2063 SET_AST_NODE_LOC(node, @1, @1);
2064 ast_stack.back()->children.push_back(node);
2065 node->children.push_back($2);
2068 AstNode *node = new AstNode(AST_EDGE);
2069 ast_stack.back()->children.push_back(node);
2070 node->children.push_back($1);
2101 TOK_MODPORT TOK_ID {
2102 AstNode *modport = new AstNode(AST_MODPORT);
2103 ast_stack.back()->children.push_back(modport);
2104 ast_stack.push_back(modport);
2107 } modport_args_opt {
2108 ast_stack.pop_back();
2109 log_assert(ast_stack.size() == 2);
2113 '(' ')' | '(' modport_args optional_comma ')';
2116 modport_arg | modport_args ',' modport_arg;
2119 modport_type_token modport_member |
2124 AstNode *modport_member = new AstNode(AST_MODPORTMEMBER);
2125 ast_stack.back()->children.push_back(modport_member);
2126 modport_member->str = *$1;
2127 modport_member->is_input = current_modport_input;
2128 modport_member->is_output = current_modport_output;
2133 TOK_INPUT {current_modport_input = 1; current_modport_output = 0;} | TOK_OUTPUT {current_modport_input = 0; current_modport_output = 1;}
2136 opt_sva_label TOK_ASSERT opt_property '(' expr ')' ';' {
2137 if (noassert_mode) {
2140 AstNode *node = new AstNode(assume_asserts_mode ? AST_ASSUME : AST_ASSERT, $5);
2141 SET_AST_NODE_LOC(node, @1, @6);
2144 ast_stack.back()->children.push_back(node);
2149 opt_sva_label TOK_ASSUME opt_property '(' expr ')' ';' {
2150 if (noassume_mode) {
2153 AstNode *node = new AstNode(assert_assumes_mode ? AST_ASSERT : AST_ASSUME, $5);
2154 SET_AST_NODE_LOC(node, @1, @6);
2157 ast_stack.back()->children.push_back(node);
2162 opt_sva_label TOK_ASSERT opt_property '(' TOK_EVENTUALLY expr ')' ';' {
2163 if (noassert_mode) {
2166 AstNode *node = new AstNode(assume_asserts_mode ? AST_FAIR : AST_LIVE, $6);
2167 SET_AST_NODE_LOC(node, @1, @7);
2170 ast_stack.back()->children.push_back(node);
2175 opt_sva_label TOK_ASSUME opt_property '(' TOK_EVENTUALLY expr ')' ';' {
2176 if (noassume_mode) {
2179 AstNode *node = new AstNode(assert_assumes_mode ? AST_LIVE : AST_FAIR, $6);
2180 SET_AST_NODE_LOC(node, @1, @7);
2183 ast_stack.back()->children.push_back(node);
2188 opt_sva_label TOK_COVER opt_property '(' expr ')' ';' {
2189 AstNode *node = new AstNode(AST_COVER, $5);
2190 SET_AST_NODE_LOC(node, @1, @6);
2191 if ($1 != nullptr) {
2195 ast_stack.back()->children.push_back(node);
2197 opt_sva_label TOK_COVER opt_property '(' ')' ';' {
2198 AstNode *node = new AstNode(AST_COVER, AstNode::mkconst_int(1, false));
2199 SET_AST_NODE_LOC(node, @1, @5);
2200 if ($1 != nullptr) {
2204 ast_stack.back()->children.push_back(node);
2206 opt_sva_label TOK_COVER ';' {
2207 AstNode *node = new AstNode(AST_COVER, AstNode::mkconst_int(1, false));
2208 SET_AST_NODE_LOC(node, @1, @2);
2209 if ($1 != nullptr) {
2213 ast_stack.back()->children.push_back(node);
2215 opt_sva_label TOK_RESTRICT opt_property '(' expr ')' ';' {
2216 if (norestrict_mode) {
2219 AstNode *node = new AstNode(AST_ASSUME, $5);
2220 SET_AST_NODE_LOC(node, @1, @6);
2223 ast_stack.back()->children.push_back(node);
2226 log_file_warning(current_filename, get_line_num(), "SystemVerilog does not allow \"restrict\" without \"property\".\n");
2230 opt_sva_label TOK_RESTRICT opt_property '(' TOK_EVENTUALLY expr ')' ';' {
2231 if (norestrict_mode) {
2234 AstNode *node = new AstNode(AST_FAIR, $6);
2235 SET_AST_NODE_LOC(node, @1, @7);
2238 ast_stack.back()->children.push_back(node);
2241 log_file_warning(current_filename, get_line_num(), "SystemVerilog does not allow \"restrict\" without \"property\".\n");
2247 opt_sva_label TOK_ASSERT TOK_PROPERTY '(' expr ')' ';' {
2248 AstNode *node = new AstNode(assume_asserts_mode ? AST_ASSUME : AST_ASSERT, $5);
2249 SET_AST_NODE_LOC(node, @1, @6);
2250 ast_stack.back()->children.push_back(node);
2251 if ($1 != nullptr) {
2252 ast_stack.back()->children.back()->str = *$1;
2256 opt_sva_label TOK_ASSUME TOK_PROPERTY '(' expr ')' ';' {
2257 AstNode *node = new AstNode(AST_ASSUME, $5);
2258 SET_AST_NODE_LOC(node, @1, @6);
2259 ast_stack.back()->children.push_back(node);
2260 if ($1 != nullptr) {
2261 ast_stack.back()->children.back()->str = *$1;
2265 opt_sva_label TOK_ASSERT TOK_PROPERTY '(' TOK_EVENTUALLY expr ')' ';' {
2266 AstNode *node = new AstNode(assume_asserts_mode ? AST_FAIR : AST_LIVE, $6);
2267 SET_AST_NODE_LOC(node, @1, @7);
2268 ast_stack.back()->children.push_back(node);
2269 if ($1 != nullptr) {
2270 ast_stack.back()->children.back()->str = *$1;
2274 opt_sva_label TOK_ASSUME TOK_PROPERTY '(' TOK_EVENTUALLY expr ')' ';' {
2275 AstNode *node = new AstNode(AST_FAIR, $6);
2276 SET_AST_NODE_LOC(node, @1, @7);
2277 ast_stack.back()->children.push_back(node);
2278 if ($1 != nullptr) {
2279 ast_stack.back()->children.back()->str = *$1;
2283 opt_sva_label TOK_COVER TOK_PROPERTY '(' expr ')' ';' {
2284 AstNode *node = new AstNode(AST_COVER, $5);
2285 SET_AST_NODE_LOC(node, @1, @6);
2286 ast_stack.back()->children.push_back(node);
2287 if ($1 != nullptr) {
2288 ast_stack.back()->children.back()->str = *$1;
2292 opt_sva_label TOK_RESTRICT TOK_PROPERTY '(' expr ')' ';' {
2293 if (norestrict_mode) {
2296 AstNode *node = new AstNode(AST_ASSUME, $5);
2297 SET_AST_NODE_LOC(node, @1, @6);
2298 ast_stack.back()->children.push_back(node);
2299 if ($1 != nullptr) {
2300 ast_stack.back()->children.back()->str = *$1;
2305 opt_sva_label TOK_RESTRICT TOK_PROPERTY '(' TOK_EVENTUALLY expr ')' ';' {
2306 if (norestrict_mode) {
2309 AstNode *node = new AstNode(AST_FAIR, $6);
2310 SET_AST_NODE_LOC(node, @1, @7);
2311 ast_stack.back()->children.push_back(node);
2312 if ($1 != nullptr) {
2313 ast_stack.back()->children.back()->str = *$1;
2319 simple_behavioral_stmt:
2320 attr lvalue '=' delay expr {
2321 AstNode *node = new AstNode(AST_ASSIGN_EQ, $2, $5);
2322 ast_stack.back()->children.push_back(node);
2323 SET_AST_NODE_LOC(node, @2, @5);
2324 append_attr(node, $1);
2326 attr lvalue TOK_INCREMENT {
2327 AstNode *node = new AstNode(AST_ASSIGN_EQ, $2, new AstNode(AST_ADD, $2->clone(), AstNode::mkconst_int(1, true)));
2328 ast_stack.back()->children.push_back(node);
2329 SET_AST_NODE_LOC(node, @2, @3);
2330 append_attr(node, $1);
2332 attr lvalue TOK_DECREMENT {
2333 AstNode *node = new AstNode(AST_ASSIGN_EQ, $2, new AstNode(AST_SUB, $2->clone(), AstNode::mkconst_int(1, true)));
2334 ast_stack.back()->children.push_back(node);
2335 SET_AST_NODE_LOC(node, @2, @3);
2336 append_attr(node, $1);
2338 attr lvalue OP_LE delay expr {
2339 AstNode *node = new AstNode(AST_ASSIGN_LE, $2, $5);
2340 ast_stack.back()->children.push_back(node);
2341 SET_AST_NODE_LOC(node, @2, @5);
2342 append_attr(node, $1);
2345 // this production creates the obligatory if-else shift/reduce conflict
2347 defattr | assert | wire_decl | param_decl | localparam_decl | typedef_decl |
2348 non_opt_delay behavioral_stmt |
2349 simple_behavioral_stmt ';' |
2353 attr hierarchical_id {
2354 AstNode *node = new AstNode(AST_TCALL);
2357 ast_stack.back()->children.push_back(node);
2358 ast_stack.push_back(node);
2359 append_attr(node, $1);
2361 ast_stack.pop_back();
2363 attr TOK_MSG_TASKS {
2364 AstNode *node = new AstNode(AST_TCALL);
2367 ast_stack.back()->children.push_back(node);
2368 ast_stack.push_back(node);
2369 append_attr(node, $1);
2371 ast_stack.pop_back();
2376 AstNode *node = new AstNode(AST_BLOCK);
2377 ast_stack.back()->children.push_back(node);
2378 ast_stack.push_back(node);
2379 append_attr(node, $1);
2382 } behavioral_stmt_list TOK_END opt_label {
2384 if ($4 != NULL && $8 != NULL && *$4 != *$8)
2385 frontend_verilog_yyerror("Begin label (%s) and end label (%s) don't match.", $4->c_str()+1, $8->c_str()+1);
2386 SET_AST_NODE_LOC(ast_stack.back(), @2, @8);
2389 ast_stack.pop_back();
2392 AstNode *node = new AstNode(AST_FOR);
2393 ast_stack.back()->children.push_back(node);
2394 ast_stack.push_back(node);
2395 append_attr(node, $1);
2396 } simple_behavioral_stmt ';' expr {
2397 ast_stack.back()->children.push_back($7);
2398 } ';' simple_behavioral_stmt ')' {
2399 AstNode *block = new AstNode(AST_BLOCK);
2400 ast_stack.back()->children.push_back(block);
2401 ast_stack.push_back(block);
2403 SET_AST_NODE_LOC(ast_stack.back(), @13, @13);
2404 ast_stack.pop_back();
2405 SET_AST_NODE_LOC(ast_stack.back(), @2, @13);
2406 ast_stack.pop_back();
2408 attr TOK_WHILE '(' expr ')' {
2409 AstNode *node = new AstNode(AST_WHILE);
2410 ast_stack.back()->children.push_back(node);
2411 ast_stack.push_back(node);
2412 append_attr(node, $1);
2413 AstNode *block = new AstNode(AST_BLOCK);
2414 ast_stack.back()->children.push_back($4);
2415 ast_stack.back()->children.push_back(block);
2416 ast_stack.push_back(block);
2418 SET_AST_NODE_LOC(ast_stack.back(), @7, @7);
2419 ast_stack.pop_back();
2420 ast_stack.pop_back();
2422 attr TOK_REPEAT '(' expr ')' {
2423 AstNode *node = new AstNode(AST_REPEAT);
2424 ast_stack.back()->children.push_back(node);
2425 ast_stack.push_back(node);
2426 append_attr(node, $1);
2427 AstNode *block = new AstNode(AST_BLOCK);
2428 ast_stack.back()->children.push_back($4);
2429 ast_stack.back()->children.push_back(block);
2430 ast_stack.push_back(block);
2432 SET_AST_NODE_LOC(ast_stack.back(), @7, @7);
2433 ast_stack.pop_back();
2434 ast_stack.pop_back();
2436 attr TOK_IF '(' expr ')' {
2437 AstNode *node = new AstNode(AST_CASE);
2438 AstNode *block = new AstNode(AST_BLOCK);
2439 AstNode *cond = new AstNode(AST_COND, AstNode::mkconst_int(1, false, 1), block);
2440 SET_AST_NODE_LOC(cond, @4, @4);
2441 ast_stack.back()->children.push_back(node);
2442 node->children.push_back(new AstNode(AST_REDUCE_BOOL, $4));
2443 node->children.push_back(cond);
2444 ast_stack.push_back(node);
2445 ast_stack.push_back(block);
2446 append_attr(node, $1);
2448 SET_AST_NODE_LOC(ast_stack.back(), @7, @7);
2450 ast_stack.pop_back();
2451 SET_AST_NODE_LOC(ast_stack.back(), @2, @9);
2452 ast_stack.pop_back();
2454 case_attr case_type '(' expr ')' {
2455 AstNode *node = new AstNode(AST_CASE, $4);
2456 ast_stack.back()->children.push_back(node);
2457 ast_stack.push_back(node);
2458 append_attr(node, $1);
2459 SET_AST_NODE_LOC(ast_stack.back(), @4, @4);
2460 } opt_synopsys_attr case_body TOK_ENDCASE {
2461 SET_AST_NODE_LOC(ast_stack.back(), @2, @9);
2462 case_type_stack.pop_back();
2463 ast_stack.pop_back();
2470 TOK_PRIORITY case_attr {
2473 TOK_UNIQUE case_attr {
2478 attr unique_case_attr {
2479 if ($2) (*$1)[ID::parallel_case] = AstNode::mkconst_int(1, false);
2485 case_type_stack.push_back(0);
2488 case_type_stack.push_back('x');
2491 case_type_stack.push_back('z');
2495 opt_synopsys_attr TOK_SYNOPSYS_FULL_CASE {
2496 if (ast_stack.back()->attributes.count(ID::full_case) == 0)
2497 ast_stack.back()->attributes[ID::full_case] = AstNode::mkconst_int(1, false);
2499 opt_synopsys_attr TOK_SYNOPSYS_PARALLEL_CASE {
2500 if (ast_stack.back()->attributes.count(ID::parallel_case) == 0)
2501 ast_stack.back()->attributes[ID::parallel_case] = AstNode::mkconst_int(1, false);
2505 behavioral_stmt_list:
2506 behavioral_stmt_list behavioral_stmt |
2511 AstNode *block = new AstNode(AST_BLOCK);
2512 AstNode *cond = new AstNode(AST_COND, new AstNode(AST_DEFAULT), block);
2513 SET_AST_NODE_LOC(cond, @1, @1);
2515 ast_stack.pop_back();
2516 ast_stack.back()->children.push_back(cond);
2517 ast_stack.push_back(block);
2519 SET_AST_NODE_LOC(ast_stack.back(), @3, @3);
2521 /* empty */ %prec FAKE_THEN;
2524 case_body case_item |
2529 AstNode *node = new AstNode(
2530 case_type_stack.size() && case_type_stack.back() == 'x' ? AST_CONDX :
2531 case_type_stack.size() && case_type_stack.back() == 'z' ? AST_CONDZ : AST_COND);
2532 ast_stack.back()->children.push_back(node);
2533 ast_stack.push_back(node);
2535 AstNode *block = new AstNode(AST_BLOCK);
2536 ast_stack.back()->children.push_back(block);
2537 ast_stack.push_back(block);
2538 case_type_stack.push_back(0);
2540 case_type_stack.pop_back();
2541 SET_AST_NODE_LOC(ast_stack.back(), @4, @4);
2542 ast_stack.pop_back();
2543 ast_stack.pop_back();
2547 gen_case_body gen_case_item |
2552 AstNode *node = new AstNode(
2553 case_type_stack.size() && case_type_stack.back() == 'x' ? AST_CONDX :
2554 case_type_stack.size() && case_type_stack.back() == 'z' ? AST_CONDZ : AST_COND);
2555 ast_stack.back()->children.push_back(node);
2556 ast_stack.push_back(node);
2558 case_type_stack.push_back(0);
2559 SET_AST_NODE_LOC(ast_stack.back(), @2, @2);
2561 case_type_stack.pop_back();
2562 ast_stack.pop_back();
2566 case_expr_list ':' |
2571 AstNode *node = new AstNode(AST_DEFAULT);
2572 SET_AST_NODE_LOC(node, @1, @1);
2573 ast_stack.back()->children.push_back(node);
2576 AstNode *node = new AstNode(AST_IDENTIFIER);
2577 SET_AST_NODE_LOC(node, @1, @1);
2578 ast_stack.back()->children.push_back(node);
2579 ast_stack.back()->children.back()->str = *$1;
2583 ast_stack.back()->children.push_back($1);
2585 case_expr_list ',' expr {
2586 ast_stack.back()->children.push_back($3);
2590 hierarchical_id '[' expr ']' '.' rvalue {
2591 $$ = new AstNode(AST_PREFIX, $3, $6);
2595 hierarchical_id range {
2596 $$ = new AstNode(AST_IDENTIFIER, $2);
2598 SET_AST_NODE_LOC($$, @1, @1);
2600 if ($2 == nullptr && ($$->str == "\\$initstate" ||
2601 $$->str == "\\$anyconst" || $$->str == "\\$anyseq" ||
2602 $$->str == "\\$allconst" || $$->str == "\\$allseq"))
2603 $$->type = AST_FCALL;
2605 hierarchical_id non_opt_multirange {
2606 $$ = new AstNode(AST_IDENTIFIER, $2);
2608 SET_AST_NODE_LOC($$, @1, @1);
2616 '{' lvalue_concat_list '}' {
2622 $$ = new AstNode(AST_CONCAT);
2623 $$->children.push_back($1);
2625 expr ',' lvalue_concat_list {
2627 $$->children.push_back($1);
2631 '(' arg_list optional_comma ')' |
2640 arg_list ',' single_arg;
2644 ast_stack.back()->children.push_back($1);
2648 module_gen_body gen_stmt_or_module_body_stmt |
2651 gen_stmt_or_module_body_stmt:
2652 gen_stmt | module_body_stmt |
2657 // this production creates the obligatory if-else shift/reduce conflict
2660 AstNode *node = new AstNode(AST_GENFOR);
2661 ast_stack.back()->children.push_back(node);
2662 ast_stack.push_back(node);
2663 } simple_behavioral_stmt ';' expr {
2664 ast_stack.back()->children.push_back($6);
2665 } ';' simple_behavioral_stmt ')' gen_stmt_block {
2666 SET_AST_NODE_LOC(ast_stack.back(), @1, @11);
2667 ast_stack.pop_back();
2669 TOK_IF '(' expr ')' {
2670 AstNode *node = new AstNode(AST_GENIF);
2671 ast_stack.back()->children.push_back(node);
2672 ast_stack.push_back(node);
2673 ast_stack.back()->children.push_back($3);
2674 AstNode *block = new AstNode(AST_GENBLOCK);
2675 ast_stack.back()->children.push_back(block);
2676 ast_stack.push_back(block);
2678 ast_stack.pop_back();
2680 SET_AST_NODE_LOC(ast_stack.back(), @1, @7);
2681 ast_stack.pop_back();
2683 case_type '(' expr ')' {
2684 AstNode *node = new AstNode(AST_GENCASE, $3);
2685 ast_stack.back()->children.push_back(node);
2686 ast_stack.push_back(node);
2687 } gen_case_body TOK_ENDCASE {
2688 case_type_stack.pop_back();
2689 SET_AST_NODE_LOC(ast_stack.back(), @1, @7);
2690 ast_stack.pop_back();
2695 AstNode *node = new AstNode(AST_GENBLOCK);
2696 node->str = $3 ? *$3 : std::string();
2697 ast_stack.back()->children.push_back(node);
2698 ast_stack.push_back(node);
2699 } module_gen_body TOK_END opt_label {
2703 SET_AST_NODE_LOC(ast_stack.back(), @1, @7);
2704 ast_stack.pop_back();
2707 AstNode *node = new AstNode(AST_TECALL);
2710 ast_stack.back()->children.push_back(node);
2711 ast_stack.push_back(node);
2713 SET_AST_NODE_LOC(ast_stack.back(), @1, @3);
2714 ast_stack.pop_back();
2719 AstNode *node = new AstNode(AST_GENBLOCK);
2720 ast_stack.back()->children.push_back(node);
2721 ast_stack.push_back(node);
2722 } gen_stmt_or_module_body_stmt {
2723 SET_AST_NODE_LOC(ast_stack.back(), @2, @2);
2724 ast_stack.pop_back();
2728 TOK_ELSE gen_stmt_block | /* empty */ %prec FAKE_THEN;
2734 basic_expr '?' attr expr ':' expr {
2735 $$ = new AstNode(AST_TERNARY);
2736 $$->children.push_back($1);
2737 $$->children.push_back($4);
2738 $$->children.push_back($6);
2739 SET_AST_NODE_LOC($$, @1, @$);
2740 append_attr($$, $3);
2747 '(' expr ')' integral_number {
2748 if ($4->compare(0, 1, "'") != 0)
2749 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());
2751 AstNode *val = const2ast(*$4, case_type_stack.size() == 0 ? 0 : case_type_stack.back(), !lib_mode);
2753 log_error("Value conversion failed: `%s'\n", $4->c_str());
2754 $$ = new AstNode(AST_TO_BITS, bits, val);
2757 hierarchical_id integral_number {
2758 if ($2->compare(0, 1, "'") != 0)
2759 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());
2760 AstNode *bits = new AstNode(AST_IDENTIFIER);
2762 SET_AST_NODE_LOC(bits, @1, @1);
2763 AstNode *val = const2ast(*$2, case_type_stack.size() == 0 ? 0 : case_type_stack.back(), !lib_mode);
2764 SET_AST_NODE_LOC(val, @2, @2);
2766 log_error("Value conversion failed: `%s'\n", $2->c_str());
2767 $$ = new AstNode(AST_TO_BITS, bits, val);
2772 $$ = const2ast(*$1, case_type_stack.size() == 0 ? 0 : case_type_stack.back(), !lib_mode);
2773 SET_AST_NODE_LOC($$, @1, @1);
2775 log_error("Value conversion failed: `%s'\n", $1->c_str());
2779 $$ = new AstNode(AST_REALVALUE);
2780 char *p = (char*)malloc(GetSize(*$1) + 1), *q;
2781 for (int i = 0, j = 0; j < GetSize(*$1); j++)
2782 if ((*$1)[j] != '_')
2783 p[i++] = (*$1)[j], p[i] = 0;
2784 $$->realvalue = strtod(p, &q);
2785 SET_AST_NODE_LOC($$, @1, @1);
2786 log_assert(*q == 0);
2791 $$ = AstNode::mkconst_str(*$1);
2792 SET_AST_NODE_LOC($$, @1, @1);
2795 hierarchical_id attr {
2796 AstNode *node = new AstNode(AST_FCALL);
2799 ast_stack.push_back(node);
2800 SET_AST_NODE_LOC(node, @1, @1);
2801 append_attr(node, $2);
2802 } '(' arg_list optional_comma ')' {
2803 $$ = ast_stack.back();
2804 ast_stack.pop_back();
2806 TOK_TO_SIGNED attr '(' expr ')' {
2807 $$ = new AstNode(AST_TO_SIGNED, $4);
2808 append_attr($$, $2);
2810 TOK_TO_UNSIGNED attr '(' expr ')' {
2811 $$ = new AstNode(AST_TO_UNSIGNED, $4);
2812 append_attr($$, $2);
2817 '(' expr ':' expr ':' expr ')' {
2822 '{' concat_list '}' {
2825 '{' expr '{' concat_list '}' '}' {
2826 $$ = new AstNode(AST_REPLICATE, $2, $4);
2828 '~' attr basic_expr %prec UNARY_OPS {
2829 $$ = new AstNode(AST_BIT_NOT, $3);
2830 SET_AST_NODE_LOC($$, @1, @3);
2831 append_attr($$, $2);
2833 basic_expr '&' attr basic_expr {
2834 $$ = new AstNode(AST_BIT_AND, $1, $4);
2835 SET_AST_NODE_LOC($$, @1, @4);
2836 append_attr($$, $3);
2838 basic_expr OP_NAND attr basic_expr {
2839 $$ = new AstNode(AST_BIT_NOT, new AstNode(AST_BIT_AND, $1, $4));
2840 SET_AST_NODE_LOC($$, @1, @4);
2841 append_attr($$, $3);
2843 basic_expr '|' attr basic_expr {
2844 $$ = new AstNode(AST_BIT_OR, $1, $4);
2845 SET_AST_NODE_LOC($$, @1, @4);
2846 append_attr($$, $3);
2848 basic_expr OP_NOR attr basic_expr {
2849 $$ = new AstNode(AST_BIT_NOT, new AstNode(AST_BIT_OR, $1, $4));
2850 SET_AST_NODE_LOC($$, @1, @4);
2851 append_attr($$, $3);
2853 basic_expr '^' attr basic_expr {
2854 $$ = new AstNode(AST_BIT_XOR, $1, $4);
2855 SET_AST_NODE_LOC($$, @1, @4);
2856 append_attr($$, $3);
2858 basic_expr OP_XNOR attr basic_expr {
2859 $$ = new AstNode(AST_BIT_XNOR, $1, $4);
2860 SET_AST_NODE_LOC($$, @1, @4);
2861 append_attr($$, $3);
2863 '&' attr basic_expr %prec UNARY_OPS {
2864 $$ = new AstNode(AST_REDUCE_AND, $3);
2865 SET_AST_NODE_LOC($$, @1, @3);
2866 append_attr($$, $2);
2868 OP_NAND attr basic_expr %prec UNARY_OPS {
2869 $$ = new AstNode(AST_REDUCE_AND, $3);
2870 SET_AST_NODE_LOC($$, @1, @3);
2871 append_attr($$, $2);
2872 $$ = new AstNode(AST_LOGIC_NOT, $$);
2874 '|' attr basic_expr %prec UNARY_OPS {
2875 $$ = new AstNode(AST_REDUCE_OR, $3);
2876 SET_AST_NODE_LOC($$, @1, @3);
2877 append_attr($$, $2);
2879 OP_NOR attr basic_expr %prec UNARY_OPS {
2880 $$ = new AstNode(AST_REDUCE_OR, $3);
2881 SET_AST_NODE_LOC($$, @1, @3);
2882 append_attr($$, $2);
2883 $$ = new AstNode(AST_LOGIC_NOT, $$);
2884 SET_AST_NODE_LOC($$, @1, @3);
2886 '^' attr basic_expr %prec UNARY_OPS {
2887 $$ = new AstNode(AST_REDUCE_XOR, $3);
2888 SET_AST_NODE_LOC($$, @1, @3);
2889 append_attr($$, $2);
2891 OP_XNOR attr basic_expr %prec UNARY_OPS {
2892 $$ = new AstNode(AST_REDUCE_XNOR, $3);
2893 SET_AST_NODE_LOC($$, @1, @3);
2894 append_attr($$, $2);
2896 basic_expr OP_SHL attr basic_expr {
2897 $$ = new AstNode(AST_SHIFT_LEFT, $1, new AstNode(AST_TO_UNSIGNED, $4));
2898 SET_AST_NODE_LOC($$, @1, @4);
2899 append_attr($$, $3);
2901 basic_expr OP_SHR attr basic_expr {
2902 $$ = new AstNode(AST_SHIFT_RIGHT, $1, new AstNode(AST_TO_UNSIGNED, $4));
2903 SET_AST_NODE_LOC($$, @1, @4);
2904 append_attr($$, $3);
2906 basic_expr OP_SSHL attr basic_expr {
2907 $$ = new AstNode(AST_SHIFT_SLEFT, $1, new AstNode(AST_TO_UNSIGNED, $4));
2908 SET_AST_NODE_LOC($$, @1, @4);
2909 append_attr($$, $3);
2911 basic_expr OP_SSHR attr basic_expr {
2912 $$ = new AstNode(AST_SHIFT_SRIGHT, $1, new AstNode(AST_TO_UNSIGNED, $4));
2913 SET_AST_NODE_LOC($$, @1, @4);
2914 append_attr($$, $3);
2916 basic_expr '<' attr basic_expr {
2917 $$ = new AstNode(AST_LT, $1, $4);
2918 SET_AST_NODE_LOC($$, @1, @4);
2919 append_attr($$, $3);
2921 basic_expr OP_LE attr basic_expr {
2922 $$ = new AstNode(AST_LE, $1, $4);
2923 SET_AST_NODE_LOC($$, @1, @4);
2924 append_attr($$, $3);
2926 basic_expr OP_EQ attr basic_expr {
2927 $$ = new AstNode(AST_EQ, $1, $4);
2928 SET_AST_NODE_LOC($$, @1, @4);
2929 append_attr($$, $3);
2931 basic_expr OP_NE attr basic_expr {
2932 $$ = new AstNode(AST_NE, $1, $4);
2933 SET_AST_NODE_LOC($$, @1, @4);
2934 append_attr($$, $3);
2936 basic_expr OP_EQX attr basic_expr {
2937 $$ = new AstNode(AST_EQX, $1, $4);
2938 SET_AST_NODE_LOC($$, @1, @4);
2939 append_attr($$, $3);
2941 basic_expr OP_NEX attr basic_expr {
2942 $$ = new AstNode(AST_NEX, $1, $4);
2943 SET_AST_NODE_LOC($$, @1, @4);
2944 append_attr($$, $3);
2946 basic_expr OP_GE attr basic_expr {
2947 $$ = new AstNode(AST_GE, $1, $4);
2948 SET_AST_NODE_LOC($$, @1, @4);
2949 append_attr($$, $3);
2951 basic_expr '>' attr basic_expr {
2952 $$ = new AstNode(AST_GT, $1, $4);
2953 SET_AST_NODE_LOC($$, @1, @4);
2954 append_attr($$, $3);
2956 basic_expr '+' attr basic_expr {
2957 $$ = new AstNode(AST_ADD, $1, $4);
2958 SET_AST_NODE_LOC($$, @1, @4);
2959 append_attr($$, $3);
2961 basic_expr '-' attr basic_expr {
2962 $$ = new AstNode(AST_SUB, $1, $4);
2963 SET_AST_NODE_LOC($$, @1, @4);
2964 append_attr($$, $3);
2966 basic_expr '*' attr basic_expr {
2967 $$ = new AstNode(AST_MUL, $1, $4);
2968 SET_AST_NODE_LOC($$, @1, @4);
2969 append_attr($$, $3);
2971 basic_expr '/' attr basic_expr {
2972 $$ = new AstNode(AST_DIV, $1, $4);
2973 SET_AST_NODE_LOC($$, @1, @4);
2974 append_attr($$, $3);
2976 basic_expr '%' attr basic_expr {
2977 $$ = new AstNode(AST_MOD, $1, $4);
2978 SET_AST_NODE_LOC($$, @1, @4);
2979 append_attr($$, $3);
2981 basic_expr OP_POW attr basic_expr {
2982 $$ = new AstNode(AST_POW, $1, $4);
2983 SET_AST_NODE_LOC($$, @1, @4);
2984 append_attr($$, $3);
2986 '+' attr basic_expr %prec UNARY_OPS {
2987 $$ = new AstNode(AST_POS, $3);
2988 SET_AST_NODE_LOC($$, @1, @3);
2989 append_attr($$, $2);
2991 '-' attr basic_expr %prec UNARY_OPS {
2992 $$ = new AstNode(AST_NEG, $3);
2993 SET_AST_NODE_LOC($$, @1, @3);
2994 append_attr($$, $2);
2996 basic_expr OP_LAND attr basic_expr {
2997 $$ = new AstNode(AST_LOGIC_AND, $1, $4);
2998 SET_AST_NODE_LOC($$, @1, @4);
2999 append_attr($$, $3);
3001 basic_expr OP_LOR attr basic_expr {
3002 $$ = new AstNode(AST_LOGIC_OR, $1, $4);
3003 SET_AST_NODE_LOC($$, @1, @4);
3004 append_attr($$, $3);
3006 '!' attr basic_expr %prec UNARY_OPS {
3007 $$ = new AstNode(AST_LOGIC_NOT, $3);
3008 SET_AST_NODE_LOC($$, @1, @3);
3009 append_attr($$, $2);
3014 $$ = new AstNode(AST_CONCAT, $1);
3016 expr ',' concat_list {
3018 $$->children.push_back($1);
3022 TOK_CONSTVAL { $$ = $1; } |
3023 TOK_UNBASED_UNSIZED_CONSTVAL { $$ = $1; } |
3024 TOK_BASE TOK_BASED_CONSTVAL {
3029 TOK_CONSTVAL TOK_BASE TOK_BASED_CONSTVAL {
3030 $1->append(*$2).append(*$3);