Generalise structs and add support for packed unions.
[yosys.git] / frontends / verilog / verilog_parser.y
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
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.
9 *
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.
17 *
18 * ---
19 *
20 * The Verilog frontend.
21 *
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.
26 *
27 * ---
28 *
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
32 * not reentrant.
33 *
34 */
35
36 %{
37 #include <list>
38 #include <stack>
39 #include <string.h>
40 #include "frontends/verilog/verilog_frontend.h"
41 #include "frontends/verilog/verilog_parser.tab.hh"
42 #include "kernel/log.h"
43
44 #define YYLEX_PARAM &yylval, &yylloc
45
46 USING_YOSYS_NAMESPACE
47 using namespace AST;
48 using namespace VERILOG_FRONTEND;
49
50 YOSYS_NAMESPACE_BEGIN
51 namespace VERILOG_FRONTEND {
52 int port_counter;
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;
72 std::istream *lexin;
73 }
74 YOSYS_NAMESPACE_END
75
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)
81
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)
87
88 int frontend_verilog_yylex(YYSTYPE *yylval_param, YYLTYPE *yyloc_param);
89
90 static void append_attr(AstNode *ast, dict<IdString, AstNode*> *al)
91 {
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;
96 }
97 delete al;
98 }
99
100 static void append_attr_clone(AstNode *ast, dict<IdString, AstNode*> *al)
101 {
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();
106 }
107 }
108
109 static void free_attr(dict<IdString, AstNode*> *al)
110 {
111 for (auto &it : *al)
112 delete it.second;
113 delete al;
114 }
115
116 struct specify_target {
117 char polarity_op;
118 AstNode *dst, *dat;
119 };
120
121 struct specify_triple {
122 AstNode *t_min, *t_avg, *t_max;
123 };
124
125 struct specify_rise_fall {
126 specify_triple rise;
127 specify_triple fall;
128 };
129
130 static void addTypedefNode(std::string *name, AstNode *node)
131 {
132 log_assert(node);
133 auto *tnode = new AstNode(AST_TYPEDEF, node);
134 tnode->str = *name;
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;
141 }
142 delete name;
143 ast_stack.back()->children.push_back(tnode);
144 }
145
146 static void enterTypeScope()
147 {
148 auto user_types = new UserTypeMap();
149 user_type_stack.push_back(user_types);
150 }
151
152 static void exitTypeScope()
153 {
154 user_type_stack.pop_back();
155 }
156
157 static bool isInLocalScope(const std::string *name)
158 {
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);
162 }
163
164 static AstNode *getTypeDefinitionNode(std::string type_name)
165 {
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];
172 }
173
174 static AstNode *copyTypeDefinition(std::string type_name)
175 {
176 // return a copy of the template from a typedef definition
177 auto typedef_node = getTypeDefinitionNode(type_name);
178 return typedef_node->clone();
179 }
180
181 static AstNode *makeRange(int msb = 31, int lsb = 0, bool isSigned = true)
182 {
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;
187 return range;
188 }
189
190 static void addRange(AstNode *parent, int msb = 31, int lsb = 0, bool isSigned = true)
191 {
192 auto range = makeRange(msb, lsb, isSigned);
193 parent->children.push_back(range);
194 }
195
196 static AstNode *checkRange(AstNode *type_node, AstNode *range_node)
197 {
198 if (type_node->range_left >= 0 && type_node->range_right >= 0) {
199 // type already restricts the range
200 if (range_node) {
201 frontend_verilog_yyerror("integer/genvar types cannot have packed dimensions.");
202 }
203 else {
204 range_node = makeRange(type_node->range_left, type_node->range_right, false);
205 }
206 }
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>]");
209 }
210 return range_node;
211 }
212
213 static void rewriteAsMemoryNode(AstNode *node, AstNode *rangeNode)
214 {
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));
220 }
221 node->children.push_back(rangeNode);
222 }
223
224 %}
225
226 %define api.prefix {frontend_verilog_yy}
227 %define api.pure
228
229 /* The union is defined in the header, so we need to provide all the
230 * includes it requires
231 */
232 %code requires {
233 #include <map>
234 #include <string>
235 #include "frontends/verilog/verilog_frontend.h"
236 }
237
238 %union {
239 std::string *string;
240 struct YOSYS_NAMESPACE_PREFIX AST::AstNode *ast;
241 YOSYS_NAMESPACE_PREFIX AST::AstNodeType type;
242 YOSYS_NAMESPACE_PREFIX dict<YOSYS_NAMESPACE_PREFIX RTLIL::IdString, YOSYS_NAMESPACE_PREFIX AST::AstNode*> *al;
243 struct specify_target *specify_target_ptr;
244 struct specify_triple *specify_triple_ptr;
245 struct specify_rise_fall *specify_rise_fall_ptr;
246 bool boolean;
247 char ch;
248 }
249
250 %token <string> TOK_STRING TOK_ID TOK_CONSTVAL TOK_REALVAL TOK_PRIMITIVE
251 %token <string> TOK_SVA_LABEL TOK_SPECIFY_OPER TOK_MSG_TASKS
252 %token <string> TOK_BASE TOK_BASED_CONSTVAL TOK_UNBASED_UNSIZED_CONSTVAL
253 %token <string> TOK_USER_TYPE TOK_PKG_USER_TYPE
254 %token TOK_ASSERT TOK_ASSUME TOK_RESTRICT TOK_COVER TOK_FINAL
255 %token ATTR_BEGIN ATTR_END DEFATTR_BEGIN DEFATTR_END
256 %token TOK_MODULE TOK_ENDMODULE TOK_PARAMETER TOK_LOCALPARAM TOK_DEFPARAM
257 %token TOK_PACKAGE TOK_ENDPACKAGE TOK_PACKAGESEP
258 %token TOK_INTERFACE TOK_ENDINTERFACE TOK_MODPORT TOK_VAR TOK_WILDCARD_CONNECT
259 %token TOK_INPUT TOK_OUTPUT TOK_INOUT TOK_WIRE TOK_WAND TOK_WOR TOK_REG TOK_LOGIC
260 %token TOK_INTEGER TOK_SIGNED TOK_ASSIGN TOK_ALWAYS TOK_INITIAL
261 %token TOK_ALWAYS_FF TOK_ALWAYS_COMB TOK_ALWAYS_LATCH
262 %token TOK_BEGIN TOK_END TOK_IF TOK_ELSE TOK_FOR TOK_WHILE TOK_REPEAT
263 %token TOK_DPI_FUNCTION TOK_POSEDGE TOK_NEGEDGE TOK_OR TOK_AUTOMATIC
264 %token TOK_CASE TOK_CASEX TOK_CASEZ TOK_ENDCASE TOK_DEFAULT
265 %token TOK_FUNCTION TOK_ENDFUNCTION TOK_TASK TOK_ENDTASK TOK_SPECIFY
266 %token TOK_IGNORED_SPECIFY TOK_ENDSPECIFY TOK_SPECPARAM TOK_SPECIFY_AND TOK_IGNORED_SPECIFY_AND
267 %token TOK_GENERATE TOK_ENDGENERATE TOK_GENVAR TOK_REAL
268 %token TOK_SYNOPSYS_FULL_CASE TOK_SYNOPSYS_PARALLEL_CASE
269 %token TOK_SUPPLY0 TOK_SUPPLY1 TOK_TO_SIGNED TOK_TO_UNSIGNED
270 %token TOK_POS_INDEXED TOK_NEG_INDEXED TOK_PROPERTY TOK_ENUM TOK_TYPEDEF
271 %token TOK_RAND TOK_CONST TOK_CHECKER TOK_ENDCHECKER TOK_EVENTUALLY
272 %token TOK_INCREMENT TOK_DECREMENT TOK_UNIQUE TOK_PRIORITY
273 %token TOK_STRUCT TOK_PACKED TOK_UNSIGNED TOK_INT TOK_BYTE TOK_SHORTINT TOK_UNION
274
275 %type <ast> range range_or_multirange non_opt_range non_opt_multirange range_or_signed_int
276 %type <ast> wire_type expr basic_expr concat_list rvalue lvalue lvalue_concat_list
277 %type <string> opt_label opt_sva_label tok_prim_wrapper hierarchical_id hierarchical_type_id integral_number
278 %type <string> type_name
279 %type <ast> opt_enum_init enum_type struct_type non_wire_data_type
280 %type <boolean> opt_signed opt_property unique_case_attr always_comb_or_latch always_or_always_ff
281 %type <al> attr case_attr
282 %type <type> struct_union
283
284 %type <specify_target_ptr> specify_target
285 %type <specify_triple_ptr> specify_triple specify_opt_triple
286 %type <specify_rise_fall_ptr> specify_rise_fall
287 %type <ast> specify_if specify_condition
288 %type <ch> specify_edge
289
290 // operator precedence from low to high
291 %left OP_LOR
292 %left OP_LAND
293 %left '|' OP_NOR
294 %left '^' OP_XNOR
295 %left '&' OP_NAND
296 %left OP_EQ OP_NE OP_EQX OP_NEX
297 %left '<' OP_LE OP_GE '>'
298 %left OP_SHL OP_SHR OP_SSHL OP_SSHR
299 %left '+' '-'
300 %left '*' '/' '%'
301 %left OP_POW
302 %right UNARY_OPS
303
304 %define parse.error verbose
305 %define parse.lac full
306
307 %nonassoc FAKE_THEN
308 %nonassoc TOK_ELSE
309
310 %debug
311 %locations
312
313 %%
314
315 input: {
316 ast_stack.clear();
317 ast_stack.push_back(current_ast);
318 } design {
319 ast_stack.pop_back();
320 log_assert(GetSize(ast_stack) == 0);
321 for (auto &it : default_attr_list)
322 delete it.second;
323 default_attr_list.clear();
324 };
325
326 design:
327 module design |
328 defattr design |
329 task_func_decl design |
330 param_decl design |
331 localparam_decl design |
332 typedef_decl design |
333 package design |
334 interface design |
335 /* empty */;
336
337 attr:
338 {
339 if (attr_list != nullptr)
340 attr_list_stack.push(attr_list);
341 attr_list = new dict<IdString, AstNode*>;
342 for (auto &it : default_attr_list)
343 (*attr_list)[it.first] = it.second->clone();
344 } attr_opt {
345 $$ = attr_list;
346 if (!attr_list_stack.empty()) {
347 attr_list = attr_list_stack.top();
348 attr_list_stack.pop();
349 } else
350 attr_list = nullptr;
351 };
352
353 attr_opt:
354 attr_opt ATTR_BEGIN opt_attr_list ATTR_END {
355 SET_RULE_LOC(@$, @2, @$);
356 }|
357 /* empty */;
358
359 defattr:
360 DEFATTR_BEGIN {
361 if (attr_list != nullptr)
362 attr_list_stack.push(attr_list);
363 attr_list = new dict<IdString, AstNode*>;
364 for (auto &it : default_attr_list)
365 delete it.second;
366 default_attr_list.clear();
367 } opt_attr_list {
368 attr_list->swap(default_attr_list);
369 delete attr_list;
370 if (!attr_list_stack.empty()) {
371 attr_list = attr_list_stack.top();
372 attr_list_stack.pop();
373 } else
374 attr_list = nullptr;
375 } DEFATTR_END;
376
377 opt_attr_list:
378 attr_list | /* empty */;
379
380 attr_list:
381 attr_assign |
382 attr_list ',' attr_assign;
383
384 attr_assign:
385 hierarchical_id {
386 if (attr_list->count(*$1) != 0)
387 delete (*attr_list)[*$1];
388 (*attr_list)[*$1] = AstNode::mkconst_int(1, false);
389 delete $1;
390 } |
391 hierarchical_id '=' expr {
392 if (attr_list->count(*$1) != 0)
393 delete (*attr_list)[*$1];
394 (*attr_list)[*$1] = $3;
395 delete $1;
396 };
397
398 hierarchical_id:
399 TOK_ID {
400 $$ = $1;
401 } |
402 hierarchical_id TOK_PACKAGESEP TOK_ID {
403 if ($3->compare(0, 1, "\\") == 0)
404 *$1 += "::" + $3->substr(1);
405 else
406 *$1 += "::" + *$3;
407 delete $3;
408 $$ = $1;
409 } |
410 hierarchical_id '.' TOK_ID {
411 if ($3->compare(0, 1, "\\") == 0)
412 *$1 += "." + $3->substr(1);
413 else
414 *$1 += "." + *$3;
415 delete $3;
416 $$ = $1;
417 };
418
419 hierarchical_type_id:
420 TOK_USER_TYPE
421 | TOK_PKG_USER_TYPE // package qualified type name
422 | '(' TOK_USER_TYPE ')' { $$ = $2; } // non-standard grammar
423 ;
424
425 module:
426 attr TOK_MODULE {
427 enterTypeScope();
428 } TOK_ID {
429 do_not_require_port_stubs = false;
430 AstNode *mod = new AstNode(AST_MODULE);
431 ast_stack.back()->children.push_back(mod);
432 ast_stack.push_back(mod);
433 current_ast_mod = mod;
434 port_stubs.clear();
435 port_counter = 0;
436 mod->str = *$4;
437 append_attr(mod, $1);
438 delete $4;
439 } module_para_opt module_args_opt ';' module_body TOK_ENDMODULE {
440 if (port_stubs.size() != 0)
441 frontend_verilog_yyerror("Missing details for module port `%s'.",
442 port_stubs.begin()->first.c_str());
443 SET_AST_NODE_LOC(ast_stack.back(), @2, @$);
444 ast_stack.pop_back();
445 log_assert(ast_stack.size() == 1);
446 current_ast_mod = NULL;
447 exitTypeScope();
448 };
449
450 module_para_opt:
451 '#' '(' { astbuf1 = nullptr; } module_para_list { if (astbuf1) delete astbuf1; } ')' | /* empty */;
452
453 module_para_list:
454 single_module_para | module_para_list ',' single_module_para;
455
456 single_module_para:
457 /* empty */ |
458 attr TOK_PARAMETER {
459 if (astbuf1) delete astbuf1;
460 astbuf1 = new AstNode(AST_PARAMETER);
461 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
462 append_attr(astbuf1, $1);
463 } param_type single_param_decl |
464 attr TOK_LOCALPARAM {
465 if (astbuf1) delete astbuf1;
466 astbuf1 = new AstNode(AST_LOCALPARAM);
467 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
468 append_attr(astbuf1, $1);
469 } param_type single_param_decl |
470 single_param_decl;
471
472 module_args_opt:
473 '(' ')' | /* empty */ | '(' module_args optional_comma ')';
474
475 module_args:
476 module_arg | module_args ',' module_arg;
477
478 optional_comma:
479 ',' | /* empty */;
480
481 module_arg_opt_assignment:
482 '=' expr {
483 if (ast_stack.back()->children.size() > 0 && ast_stack.back()->children.back()->type == AST_WIRE) {
484 AstNode *wire = new AstNode(AST_IDENTIFIER);
485 wire->str = ast_stack.back()->children.back()->str;
486 if (ast_stack.back()->children.back()->is_input) {
487 AstNode *n = ast_stack.back()->children.back();
488 if (n->attributes.count(ID::defaultvalue))
489 delete n->attributes.at(ID::defaultvalue);
490 n->attributes[ID::defaultvalue] = $2;
491 } else
492 if (ast_stack.back()->children.back()->is_reg || ast_stack.back()->children.back()->is_logic)
493 ast_stack.back()->children.push_back(new AstNode(AST_INITIAL, new AstNode(AST_BLOCK, new AstNode(AST_ASSIGN_LE, wire, $2))));
494 else
495 ast_stack.back()->children.push_back(new AstNode(AST_ASSIGN, wire, $2));
496 } else
497 frontend_verilog_yyerror("SystemVerilog interface in module port list cannot have a default value.");
498 } |
499 /* empty */;
500
501 module_arg:
502 TOK_ID {
503 if (ast_stack.back()->children.size() > 0 && ast_stack.back()->children.back()->type == AST_WIRE) {
504 AstNode *node = ast_stack.back()->children.back()->clone();
505 node->str = *$1;
506 node->port_id = ++port_counter;
507 ast_stack.back()->children.push_back(node);
508 SET_AST_NODE_LOC(node, @1, @1);
509 } else {
510 if (port_stubs.count(*$1) != 0)
511 frontend_verilog_yyerror("Duplicate module port `%s'.", $1->c_str());
512 port_stubs[*$1] = ++port_counter;
513 }
514 delete $1;
515 } module_arg_opt_assignment |
516 TOK_ID {
517 astbuf1 = new AstNode(AST_INTERFACEPORT);
518 astbuf1->children.push_back(new AstNode(AST_INTERFACEPORTTYPE));
519 astbuf1->children[0]->str = *$1;
520 delete $1;
521 } TOK_ID { /* SV interfaces */
522 if (!sv_mode)
523 frontend_verilog_yyerror("Interface found in port list (%s). This is not supported unless read_verilog is called with -sv!", $3->c_str());
524 astbuf2 = astbuf1->clone(); // really only needed if multiple instances of same type.
525 astbuf2->str = *$3;
526 delete $3;
527 astbuf2->port_id = ++port_counter;
528 ast_stack.back()->children.push_back(astbuf2);
529 delete astbuf1; // really only needed if multiple instances of same type.
530 } module_arg_opt_assignment |
531 attr wire_type range TOK_ID {
532 AstNode *node = $2;
533 node->str = *$4;
534 SET_AST_NODE_LOC(node, @4, @4);
535 node->port_id = ++port_counter;
536 if ($3 != NULL)
537 node->children.push_back($3);
538 if (!node->is_input && !node->is_output)
539 frontend_verilog_yyerror("Module port `%s' is neither input nor output.", $4->c_str());
540 if (node->is_reg && node->is_input && !node->is_output && !sv_mode)
541 frontend_verilog_yyerror("Input port `%s' is declared as register.", $4->c_str());
542 ast_stack.back()->children.push_back(node);
543 append_attr(node, $1);
544 delete $4;
545 } module_arg_opt_assignment |
546 '.' '.' '.' {
547 do_not_require_port_stubs = true;
548 };
549
550 package:
551 attr TOK_PACKAGE {
552 enterTypeScope();
553 } TOK_ID {
554 AstNode *mod = new AstNode(AST_PACKAGE);
555 ast_stack.back()->children.push_back(mod);
556 ast_stack.push_back(mod);
557 current_ast_mod = mod;
558 mod->str = *$4;
559 append_attr(mod, $1);
560 } ';' package_body TOK_ENDPACKAGE {
561 ast_stack.pop_back();
562 current_ast_mod = NULL;
563 exitTypeScope();
564 };
565
566 package_body:
567 package_body package_body_stmt
568 | // optional
569 ;
570
571 package_body_stmt:
572 typedef_decl
573 | localparam_decl
574 | param_decl
575 ;
576
577 interface:
578 TOK_INTERFACE {
579 enterTypeScope();
580 } TOK_ID {
581 do_not_require_port_stubs = false;
582 AstNode *intf = new AstNode(AST_INTERFACE);
583 ast_stack.back()->children.push_back(intf);
584 ast_stack.push_back(intf);
585 current_ast_mod = intf;
586 port_stubs.clear();
587 port_counter = 0;
588 intf->str = *$3;
589 delete $3;
590 } module_para_opt module_args_opt ';' interface_body TOK_ENDINTERFACE {
591 if (port_stubs.size() != 0)
592 frontend_verilog_yyerror("Missing details for module port `%s'.",
593 port_stubs.begin()->first.c_str());
594 ast_stack.pop_back();
595 log_assert(ast_stack.size() == 1);
596 current_ast_mod = NULL;
597 exitTypeScope();
598 };
599
600 interface_body:
601 interface_body interface_body_stmt |;
602
603 interface_body_stmt:
604 param_decl | localparam_decl | typedef_decl | defparam_decl | wire_decl | always_stmt | assign_stmt |
605 modport_stmt;
606
607 non_opt_delay:
608 '#' TOK_ID { delete $2; } |
609 '#' TOK_CONSTVAL { delete $2; } |
610 '#' TOK_REALVAL { delete $2; } |
611 '#' '(' expr ')' { delete $3; } |
612 '#' '(' expr ':' expr ':' expr ')' { delete $3; delete $5; delete $7; };
613
614 delay:
615 non_opt_delay | /* empty */;
616
617 wire_type:
618 {
619 astbuf3 = new AstNode(AST_WIRE);
620 current_wire_rand = false;
621 current_wire_const = false;
622 } wire_type_token_list {
623 $$ = astbuf3;
624 SET_RULE_LOC(@$, @2, @$);
625 };
626
627 wire_type_token_list:
628 wire_type_token |
629 wire_type_token_list wire_type_token |
630 wire_type_token_io |
631 hierarchical_type_id {
632 astbuf3->is_custom_type = true;
633 astbuf3->children.push_back(new AstNode(AST_WIRETYPE));
634 astbuf3->children.back()->str = *$1;
635 delete $1;
636 };
637
638 wire_type_token_io:
639 TOK_INPUT {
640 astbuf3->is_input = true;
641 } |
642 TOK_OUTPUT {
643 astbuf3->is_output = true;
644 } |
645 TOK_INOUT {
646 astbuf3->is_input = true;
647 astbuf3->is_output = true;
648 };
649
650 wire_type_token:
651 TOK_WIRE {
652 } |
653 TOK_WOR {
654 astbuf3->is_wor = true;
655 } |
656 TOK_WAND {
657 astbuf3->is_wand = true;
658 } |
659 TOK_REG {
660 astbuf3->is_reg = true;
661 } |
662 TOK_LOGIC {
663 astbuf3->is_logic = true;
664 } |
665 TOK_VAR {
666 astbuf3->is_logic = true;
667 } |
668 TOK_INTEGER {
669 astbuf3->is_reg = true;
670 astbuf3->range_left = 31;
671 astbuf3->range_right = 0;
672 astbuf3->is_signed = true;
673 } |
674 TOK_GENVAR {
675 astbuf3->type = AST_GENVAR;
676 astbuf3->is_reg = true;
677 astbuf3->is_signed = true;
678 astbuf3->range_left = 31;
679 astbuf3->range_right = 0;
680 } |
681 TOK_SIGNED {
682 astbuf3->is_signed = true;
683 } |
684 TOK_RAND {
685 current_wire_rand = true;
686 } |
687 TOK_CONST {
688 current_wire_const = true;
689 };
690
691 non_opt_range:
692 '[' expr ':' expr ']' {
693 $$ = new AstNode(AST_RANGE);
694 $$->children.push_back($2);
695 $$->children.push_back($4);
696 } |
697 '[' expr TOK_POS_INDEXED expr ']' {
698 $$ = new AstNode(AST_RANGE);
699 AstNode *expr = new AstNode(AST_SELFSZ, $2);
700 $$->children.push_back(new AstNode(AST_SUB, new AstNode(AST_ADD, expr->clone(), $4), AstNode::mkconst_int(1, true)));
701 $$->children.push_back(new AstNode(AST_ADD, expr, AstNode::mkconst_int(0, true)));
702 } |
703 '[' expr TOK_NEG_INDEXED expr ']' {
704 $$ = new AstNode(AST_RANGE);
705 AstNode *expr = new AstNode(AST_SELFSZ, $2);
706 $$->children.push_back(new AstNode(AST_ADD, expr, AstNode::mkconst_int(0, true)));
707 $$->children.push_back(new AstNode(AST_SUB, new AstNode(AST_ADD, expr->clone(), AstNode::mkconst_int(1, true)), $4));
708 } |
709 '[' expr ']' {
710 $$ = new AstNode(AST_RANGE);
711 $$->children.push_back($2);
712 };
713
714 non_opt_multirange:
715 non_opt_range non_opt_range {
716 $$ = new AstNode(AST_MULTIRANGE, $1, $2);
717 } |
718 non_opt_multirange non_opt_range {
719 $$ = $1;
720 $$->children.push_back($2);
721 };
722
723 range:
724 non_opt_range {
725 $$ = $1;
726 } |
727 /* empty */ {
728 $$ = NULL;
729 };
730
731 range_or_multirange:
732 range { $$ = $1; } |
733 non_opt_multirange { $$ = $1; };
734
735 range_or_signed_int:
736 range { $$ = $1; }
737 | TOK_INTEGER { $$ = makeRange(); }
738 ;
739
740 module_body:
741 module_body module_body_stmt |
742 /* the following line makes the generate..endgenrate keywords optional */
743 module_body gen_stmt |
744 /* empty */;
745
746 module_body_stmt:
747 task_func_decl | specify_block | param_decl | localparam_decl | typedef_decl | defparam_decl | specparam_declaration | wire_decl | assign_stmt | cell_stmt |
748 enum_decl | struct_decl |
749 always_stmt | TOK_GENERATE module_gen_body TOK_ENDGENERATE | defattr | assert_property | checker_decl | ignored_specify_block;
750
751 checker_decl:
752 TOK_CHECKER TOK_ID ';' {
753 AstNode *node = new AstNode(AST_GENBLOCK);
754 node->str = *$2;
755 ast_stack.back()->children.push_back(node);
756 ast_stack.push_back(node);
757 } module_body TOK_ENDCHECKER {
758 delete $2;
759 ast_stack.pop_back();
760 };
761
762 task_func_decl:
763 attr TOK_DPI_FUNCTION TOK_ID TOK_ID {
764 current_function_or_task = new AstNode(AST_DPI_FUNCTION, AstNode::mkconst_str(*$3), AstNode::mkconst_str(*$4));
765 current_function_or_task->str = *$4;
766 append_attr(current_function_or_task, $1);
767 ast_stack.back()->children.push_back(current_function_or_task);
768 delete $3;
769 delete $4;
770 } opt_dpi_function_args ';' {
771 current_function_or_task = NULL;
772 } |
773 attr TOK_DPI_FUNCTION TOK_ID '=' TOK_ID TOK_ID {
774 current_function_or_task = new AstNode(AST_DPI_FUNCTION, AstNode::mkconst_str(*$5), AstNode::mkconst_str(*$3));
775 current_function_or_task->str = *$6;
776 append_attr(current_function_or_task, $1);
777 ast_stack.back()->children.push_back(current_function_or_task);
778 delete $3;
779 delete $5;
780 delete $6;
781 } opt_dpi_function_args ';' {
782 current_function_or_task = NULL;
783 } |
784 attr TOK_DPI_FUNCTION TOK_ID ':' TOK_ID '=' TOK_ID TOK_ID {
785 current_function_or_task = new AstNode(AST_DPI_FUNCTION, AstNode::mkconst_str(*$7), AstNode::mkconst_str(*$3 + ":" + RTLIL::unescape_id(*$5)));
786 current_function_or_task->str = *$8;
787 append_attr(current_function_or_task, $1);
788 ast_stack.back()->children.push_back(current_function_or_task);
789 delete $3;
790 delete $5;
791 delete $7;
792 delete $8;
793 } opt_dpi_function_args ';' {
794 current_function_or_task = NULL;
795 } |
796 attr TOK_TASK opt_automatic TOK_ID {
797 current_function_or_task = new AstNode(AST_TASK);
798 current_function_or_task->str = *$4;
799 append_attr(current_function_or_task, $1);
800 ast_stack.back()->children.push_back(current_function_or_task);
801 ast_stack.push_back(current_function_or_task);
802 current_function_or_task_port_id = 1;
803 delete $4;
804 } task_func_args_opt ';' task_func_body TOK_ENDTASK {
805 current_function_or_task = NULL;
806 ast_stack.pop_back();
807 } |
808 attr TOK_FUNCTION opt_automatic opt_signed range_or_signed_int TOK_ID {
809 current_function_or_task = new AstNode(AST_FUNCTION);
810 current_function_or_task->str = *$6;
811 append_attr(current_function_or_task, $1);
812 ast_stack.back()->children.push_back(current_function_or_task);
813 ast_stack.push_back(current_function_or_task);
814 AstNode *outreg = new AstNode(AST_WIRE);
815 outreg->str = *$6;
816 outreg->is_signed = $4;
817 outreg->is_reg = true;
818 if ($5 != NULL) {
819 outreg->children.push_back($5);
820 outreg->is_signed = $4 || $5->is_signed;
821 $5->is_signed = false;
822 }
823 current_function_or_task->children.push_back(outreg);
824 current_function_or_task_port_id = 1;
825 delete $6;
826 } task_func_args_opt ';' task_func_body TOK_ENDFUNCTION {
827 current_function_or_task = NULL;
828 ast_stack.pop_back();
829 };
830
831 dpi_function_arg:
832 TOK_ID TOK_ID {
833 current_function_or_task->children.push_back(AstNode::mkconst_str(*$1));
834 delete $1;
835 delete $2;
836 } |
837 TOK_ID {
838 current_function_or_task->children.push_back(AstNode::mkconst_str(*$1));
839 delete $1;
840 };
841
842 opt_dpi_function_args:
843 '(' dpi_function_args ')' |
844 /* empty */;
845
846 dpi_function_args:
847 dpi_function_args ',' dpi_function_arg |
848 dpi_function_args ',' |
849 dpi_function_arg |
850 /* empty */;
851
852 opt_automatic:
853 TOK_AUTOMATIC |
854 /* empty */;
855
856 opt_signed:
857 TOK_SIGNED {
858 $$ = true;
859 } |
860 /* empty */ {
861 $$ = false;
862 };
863
864 task_func_args_opt:
865 '(' ')' | /* empty */ | '(' {
866 albuf = nullptr;
867 astbuf1 = nullptr;
868 astbuf2 = nullptr;
869 } task_func_args optional_comma {
870 delete astbuf1;
871 if (astbuf2 != NULL)
872 delete astbuf2;
873 free_attr(albuf);
874 } ')';
875
876 task_func_args:
877 task_func_port | task_func_args ',' task_func_port;
878
879 task_func_port:
880 attr wire_type range {
881 if (albuf) {
882 delete astbuf1;
883 if (astbuf2 != NULL)
884 delete astbuf2;
885 free_attr(albuf);
886 }
887 albuf = $1;
888 astbuf1 = $2;
889 astbuf2 = checkRange(astbuf1, $3);
890 } wire_name | wire_name;
891
892 task_func_body:
893 task_func_body behavioral_stmt |
894 /* empty */;
895
896 /*************************** specify parser ***************************/
897
898 specify_block:
899 TOK_SPECIFY specify_item_list TOK_ENDSPECIFY;
900
901 specify_item_list:
902 specify_item specify_item_list |
903 /* empty */;
904
905 specify_item:
906 specify_if '(' specify_edge expr TOK_SPECIFY_OPER specify_target ')' '=' specify_rise_fall ';' {
907 AstNode *en_expr = $1;
908 char specify_edge = $3;
909 AstNode *src_expr = $4;
910 string *oper = $5;
911 specify_target *target = $6;
912 specify_rise_fall *timing = $9;
913
914 if (specify_edge != 0 && target->dat == nullptr)
915 frontend_verilog_yyerror("Found specify edge but no data spec.\n");
916
917 AstNode *cell = new AstNode(AST_CELL);
918 ast_stack.back()->children.push_back(cell);
919 cell->str = stringf("$specify$%d", autoidx++);
920 cell->children.push_back(new AstNode(AST_CELLTYPE));
921 cell->children.back()->str = target->dat ? "$specify3" : "$specify2";
922 SET_AST_NODE_LOC(cell, en_expr ? @1 : @2, @10);
923
924 char oper_polarity = 0;
925 char oper_type = oper->at(0);
926
927 if (oper->size() == 3) {
928 oper_polarity = oper->at(0);
929 oper_type = oper->at(1);
930 }
931
932 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(oper_type == '*', false, 1)));
933 cell->children.back()->str = "\\FULL";
934
935 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(oper_polarity != 0, false, 1)));
936 cell->children.back()->str = "\\SRC_DST_PEN";
937
938 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(oper_polarity == '+', false, 1)));
939 cell->children.back()->str = "\\SRC_DST_POL";
940
941 cell->children.push_back(new AstNode(AST_PARASET, timing->rise.t_min));
942 cell->children.back()->str = "\\T_RISE_MIN";
943
944 cell->children.push_back(new AstNode(AST_PARASET, timing->rise.t_avg));
945 cell->children.back()->str = "\\T_RISE_TYP";
946
947 cell->children.push_back(new AstNode(AST_PARASET, timing->rise.t_max));
948 cell->children.back()->str = "\\T_RISE_MAX";
949
950 cell->children.push_back(new AstNode(AST_PARASET, timing->fall.t_min));
951 cell->children.back()->str = "\\T_FALL_MIN";
952
953 cell->children.push_back(new AstNode(AST_PARASET, timing->fall.t_avg));
954 cell->children.back()->str = "\\T_FALL_TYP";
955
956 cell->children.push_back(new AstNode(AST_PARASET, timing->fall.t_max));
957 cell->children.back()->str = "\\T_FALL_MAX";
958
959 cell->children.push_back(new AstNode(AST_ARGUMENT, en_expr ? en_expr : AstNode::mkconst_int(1, false, 1)));
960 cell->children.back()->str = "\\EN";
961
962 cell->children.push_back(new AstNode(AST_ARGUMENT, src_expr));
963 cell->children.back()->str = "\\SRC";
964
965 cell->children.push_back(new AstNode(AST_ARGUMENT, target->dst));
966 cell->children.back()->str = "\\DST";
967
968 if (target->dat)
969 {
970 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(specify_edge != 0, false, 1)));
971 cell->children.back()->str = "\\EDGE_EN";
972
973 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(specify_edge == 'p', false, 1)));
974 cell->children.back()->str = "\\EDGE_POL";
975
976 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(target->polarity_op != 0, false, 1)));
977 cell->children.back()->str = "\\DAT_DST_PEN";
978
979 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_int(target->polarity_op == '+', false, 1)));
980 cell->children.back()->str = "\\DAT_DST_POL";
981
982 cell->children.push_back(new AstNode(AST_ARGUMENT, target->dat));
983 cell->children.back()->str = "\\DAT";
984 }
985
986 delete oper;
987 delete target;
988 delete timing;
989 } |
990 TOK_ID '(' specify_edge expr specify_condition ',' specify_edge expr specify_condition ',' specify_triple specify_opt_triple ')' ';' {
991 if (*$1 != "$setup" && *$1 != "$hold" && *$1 != "$setuphold" && *$1 != "$removal" && *$1 != "$recovery" &&
992 *$1 != "$recrem" && *$1 != "$skew" && *$1 != "$timeskew" && *$1 != "$fullskew" && *$1 != "$nochange")
993 frontend_verilog_yyerror("Unsupported specify rule type: %s\n", $1->c_str());
994
995 AstNode *src_pen = AstNode::mkconst_int($3 != 0, false, 1);
996 AstNode *src_pol = AstNode::mkconst_int($3 == 'p', false, 1);
997 AstNode *src_expr = $4, *src_en = $5 ? $5 : AstNode::mkconst_int(1, false, 1);
998
999 AstNode *dst_pen = AstNode::mkconst_int($7 != 0, false, 1);
1000 AstNode *dst_pol = AstNode::mkconst_int($7 == 'p', false, 1);
1001 AstNode *dst_expr = $8, *dst_en = $9 ? $9 : AstNode::mkconst_int(1, false, 1);
1002
1003 specify_triple *limit = $11;
1004 specify_triple *limit2 = $12;
1005
1006 AstNode *cell = new AstNode(AST_CELL);
1007 ast_stack.back()->children.push_back(cell);
1008 cell->str = stringf("$specify$%d", autoidx++);
1009 cell->children.push_back(new AstNode(AST_CELLTYPE));
1010 cell->children.back()->str = "$specrule";
1011 SET_AST_NODE_LOC(cell, @1, @14);
1012
1013 cell->children.push_back(new AstNode(AST_PARASET, AstNode::mkconst_str(*$1)));
1014 cell->children.back()->str = "\\TYPE";
1015
1016 cell->children.push_back(new AstNode(AST_PARASET, limit->t_min));
1017 cell->children.back()->str = "\\T_LIMIT_MIN";
1018
1019 cell->children.push_back(new AstNode(AST_PARASET, limit->t_avg));
1020 cell->children.back()->str = "\\T_LIMIT_TYP";
1021
1022 cell->children.push_back(new AstNode(AST_PARASET, limit->t_max));
1023 cell->children.back()->str = "\\T_LIMIT_MAX";
1024
1025 cell->children.push_back(new AstNode(AST_PARASET, limit2 ? limit2->t_min : AstNode::mkconst_int(0, true)));
1026 cell->children.back()->str = "\\T_LIMIT2_MIN";
1027
1028 cell->children.push_back(new AstNode(AST_PARASET, limit2 ? limit2->t_avg : AstNode::mkconst_int(0, true)));
1029 cell->children.back()->str = "\\T_LIMIT2_TYP";
1030
1031 cell->children.push_back(new AstNode(AST_PARASET, limit2 ? limit2->t_max : AstNode::mkconst_int(0, true)));
1032 cell->children.back()->str = "\\T_LIMIT2_MAX";
1033
1034 cell->children.push_back(new AstNode(AST_PARASET, src_pen));
1035 cell->children.back()->str = "\\SRC_PEN";
1036
1037 cell->children.push_back(new AstNode(AST_PARASET, src_pol));
1038 cell->children.back()->str = "\\SRC_POL";
1039
1040 cell->children.push_back(new AstNode(AST_PARASET, dst_pen));
1041 cell->children.back()->str = "\\DST_PEN";
1042
1043 cell->children.push_back(new AstNode(AST_PARASET, dst_pol));
1044 cell->children.back()->str = "\\DST_POL";
1045
1046 cell->children.push_back(new AstNode(AST_ARGUMENT, src_en));
1047 cell->children.back()->str = "\\SRC_EN";
1048
1049 cell->children.push_back(new AstNode(AST_ARGUMENT, src_expr));
1050 cell->children.back()->str = "\\SRC";
1051
1052 cell->children.push_back(new AstNode(AST_ARGUMENT, dst_en));
1053 cell->children.back()->str = "\\DST_EN";
1054
1055 cell->children.push_back(new AstNode(AST_ARGUMENT, dst_expr));
1056 cell->children.back()->str = "\\DST";
1057
1058 delete $1;
1059 };
1060
1061 specify_opt_triple:
1062 ',' specify_triple {
1063 $$ = $2;
1064 } |
1065 /* empty */ {
1066 $$ = nullptr;
1067 };
1068
1069 specify_if:
1070 TOK_IF '(' expr ')' {
1071 $$ = $3;
1072 } |
1073 /* empty */ {
1074 $$ = nullptr;
1075 };
1076
1077 specify_condition:
1078 TOK_SPECIFY_AND expr {
1079 $$ = $2;
1080 } |
1081 /* empty */ {
1082 $$ = nullptr;
1083 };
1084
1085 specify_target:
1086 expr {
1087 $$ = new specify_target;
1088 $$->polarity_op = 0;
1089 $$->dst = $1;
1090 $$->dat = nullptr;
1091 } |
1092 '(' expr ':' expr ')'{
1093 $$ = new specify_target;
1094 $$->polarity_op = 0;
1095 $$->dst = $2;
1096 $$->dat = $4;
1097 } |
1098 '(' expr TOK_NEG_INDEXED expr ')'{
1099 $$ = new specify_target;
1100 $$->polarity_op = '-';
1101 $$->dst = $2;
1102 $$->dat = $4;
1103 } |
1104 '(' expr TOK_POS_INDEXED expr ')'{
1105 $$ = new specify_target;
1106 $$->polarity_op = '+';
1107 $$->dst = $2;
1108 $$->dat = $4;
1109 };
1110
1111 specify_edge:
1112 TOK_POSEDGE { $$ = 'p'; } |
1113 TOK_NEGEDGE { $$ = 'n'; } |
1114 { $$ = 0; };
1115
1116 specify_rise_fall:
1117 specify_triple {
1118 $$ = new specify_rise_fall;
1119 $$->rise = *$1;
1120 $$->fall.t_min = $1->t_min->clone();
1121 $$->fall.t_avg = $1->t_avg->clone();
1122 $$->fall.t_max = $1->t_max->clone();
1123 delete $1;
1124 } |
1125 '(' specify_triple ',' specify_triple ')' {
1126 $$ = new specify_rise_fall;
1127 $$->rise = *$2;
1128 $$->fall = *$4;
1129 delete $2;
1130 delete $4;
1131 } |
1132 '(' specify_triple ',' specify_triple ',' specify_triple ')' {
1133 $$ = new specify_rise_fall;
1134 $$->rise = *$2;
1135 $$->fall = *$4;
1136 delete $2;
1137 delete $4;
1138 delete $6;
1139 log_file_warning(current_filename, get_line_num(), "Path delay expressions beyond rise/fall not currently supported. Ignoring.\n");
1140 } |
1141 '(' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ')' {
1142 $$ = new specify_rise_fall;
1143 $$->rise = *$2;
1144 $$->fall = *$4;
1145 delete $2;
1146 delete $4;
1147 delete $6;
1148 delete $8;
1149 delete $10;
1150 delete $12;
1151 log_file_warning(current_filename, get_line_num(), "Path delay expressions beyond rise/fall not currently supported. Ignoring.\n");
1152 } |
1153 '(' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ',' specify_triple ')' {
1154 $$ = new specify_rise_fall;
1155 $$->rise = *$2;
1156 $$->fall = *$4;
1157 delete $2;
1158 delete $4;
1159 delete $6;
1160 delete $8;
1161 delete $10;
1162 delete $12;
1163 delete $14;
1164 delete $16;
1165 delete $18;
1166 delete $20;
1167 delete $22;
1168 delete $24;
1169 log_file_warning(current_filename, get_line_num(), "Path delay expressions beyond rise/fall not currently supported. Ignoring.\n");
1170 }
1171
1172 specify_triple:
1173 expr {
1174 $$ = new specify_triple;
1175 $$->t_min = $1;
1176 $$->t_avg = $1->clone();
1177 $$->t_max = $1->clone();
1178 } |
1179 expr ':' expr ':' expr {
1180 $$ = new specify_triple;
1181 $$->t_min = $1;
1182 $$->t_avg = $3;
1183 $$->t_max = $5;
1184 };
1185
1186 /******************** ignored specify parser **************************/
1187
1188 ignored_specify_block:
1189 TOK_IGNORED_SPECIFY ignored_specify_item_opt TOK_ENDSPECIFY |
1190 TOK_IGNORED_SPECIFY TOK_ENDSPECIFY ;
1191
1192 ignored_specify_item_opt:
1193 ignored_specify_item_opt ignored_specify_item |
1194 ignored_specify_item ;
1195
1196 ignored_specify_item:
1197 specparam_declaration
1198 // | pulsestyle_declaration
1199 // | showcancelled_declaration
1200 | path_declaration
1201 | system_timing_declaration
1202 ;
1203
1204 specparam_declaration:
1205 TOK_SPECPARAM list_of_specparam_assignments ';' |
1206 TOK_SPECPARAM specparam_range list_of_specparam_assignments ';' ;
1207
1208 // IEEE 1364-2005 calls this sinmply 'range' but the current 'range' rule allows empty match
1209 // and the 'non_opt_range' rule allows index ranges not allowed by 1364-2005
1210 // exxxxtending this for SV specparam would change this anyhow
1211 specparam_range:
1212 '[' ignspec_constant_expression ':' ignspec_constant_expression ']' ;
1213
1214 list_of_specparam_assignments:
1215 specparam_assignment | list_of_specparam_assignments ',' specparam_assignment;
1216
1217 specparam_assignment:
1218 ignspec_id '=' ignspec_expr ;
1219
1220 ignspec_opt_cond:
1221 TOK_IF '(' ignspec_expr ')' | /* empty */;
1222
1223 path_declaration :
1224 simple_path_declaration ';'
1225 // | edge_sensitive_path_declaration
1226 // | state_dependent_path_declaration
1227 ;
1228
1229 simple_path_declaration :
1230 ignspec_opt_cond parallel_path_description '=' path_delay_value |
1231 ignspec_opt_cond full_path_description '=' path_delay_value
1232 ;
1233
1234 path_delay_value :
1235 '(' ignspec_expr list_of_path_delay_extra_expressions ')'
1236 | ignspec_expr
1237 | ignspec_expr list_of_path_delay_extra_expressions
1238 ;
1239
1240 list_of_path_delay_extra_expressions :
1241 ',' ignspec_expr
1242 | ',' ignspec_expr list_of_path_delay_extra_expressions
1243 ;
1244
1245 specify_edge_identifier :
1246 TOK_POSEDGE | TOK_NEGEDGE ;
1247
1248 parallel_path_description :
1249 '(' specify_input_terminal_descriptor opt_polarity_operator '=' '>' specify_output_terminal_descriptor ')' |
1250 '(' specify_edge_identifier specify_input_terminal_descriptor '=' '>' '(' specify_output_terminal_descriptor opt_polarity_operator ':' ignspec_expr ')' ')' |
1251 '(' specify_edge_identifier specify_input_terminal_descriptor '=' '>' '(' specify_output_terminal_descriptor TOK_POS_INDEXED ignspec_expr ')' ')' ;
1252
1253 full_path_description :
1254 '(' list_of_path_inputs '*' '>' list_of_path_outputs ')' |
1255 '(' specify_edge_identifier list_of_path_inputs '*' '>' '(' list_of_path_outputs opt_polarity_operator ':' ignspec_expr ')' ')' |
1256 '(' specify_edge_identifier list_of_path_inputs '*' '>' '(' list_of_path_outputs TOK_POS_INDEXED ignspec_expr ')' ')' ;
1257
1258 // This was broken into 2 rules to solve shift/reduce conflicts
1259 list_of_path_inputs :
1260 specify_input_terminal_descriptor opt_polarity_operator |
1261 specify_input_terminal_descriptor more_path_inputs opt_polarity_operator ;
1262
1263 more_path_inputs :
1264 ',' specify_input_terminal_descriptor |
1265 more_path_inputs ',' specify_input_terminal_descriptor ;
1266
1267 list_of_path_outputs :
1268 specify_output_terminal_descriptor |
1269 list_of_path_outputs ',' specify_output_terminal_descriptor ;
1270
1271 opt_polarity_operator :
1272 '+'
1273 | '-'
1274 | ;
1275
1276 // Good enough for the time being
1277 specify_input_terminal_descriptor :
1278 ignspec_id ;
1279
1280 // Good enough for the time being
1281 specify_output_terminal_descriptor :
1282 ignspec_id ;
1283
1284 system_timing_declaration :
1285 ignspec_id '(' system_timing_args ')' ';' ;
1286
1287 system_timing_arg :
1288 TOK_POSEDGE ignspec_id |
1289 TOK_NEGEDGE ignspec_id |
1290 ignspec_expr ;
1291
1292 system_timing_args :
1293 system_timing_arg |
1294 system_timing_args TOK_IGNORED_SPECIFY_AND system_timing_arg |
1295 system_timing_args ',' system_timing_arg ;
1296
1297 // for the time being this is OK, but we may write our own expr here.
1298 // as I'm not sure it is legal to use a full expr here (probably not)
1299 // On the other hand, other rules requiring constant expressions also use 'expr'
1300 // (such as param assignment), so we may leave this as-is, perhaps adding runtime checks for constant-ness
1301 ignspec_constant_expression:
1302 expr { delete $1; };
1303
1304 ignspec_expr:
1305 expr { delete $1; } |
1306 expr ':' expr ':' expr {
1307 delete $1;
1308 delete $3;
1309 delete $5;
1310 };
1311
1312 ignspec_id:
1313 TOK_ID { delete $1; }
1314 range_or_multirange { delete $3; };
1315
1316 /**********************************************************************/
1317
1318 param_signed:
1319 TOK_SIGNED {
1320 astbuf1->is_signed = true;
1321 } | /* empty */;
1322
1323 param_integer:
1324 TOK_INTEGER {
1325 if (astbuf1->children.size() != 1)
1326 frontend_verilog_yyerror("Internal error in param_integer - should not happen?");
1327 astbuf1->children.push_back(new AstNode(AST_RANGE));
1328 astbuf1->children.back()->children.push_back(AstNode::mkconst_int(31, true));
1329 astbuf1->children.back()->children.push_back(AstNode::mkconst_int(0, true));
1330 astbuf1->is_signed = true;
1331 } | /* empty */;
1332
1333 param_real:
1334 TOK_REAL {
1335 if (astbuf1->children.size() != 1)
1336 frontend_verilog_yyerror("Parameter already declared as integer, cannot set to real.");
1337 astbuf1->children.push_back(new AstNode(AST_REALVALUE));
1338 } | /* empty */;
1339
1340 param_range:
1341 range {
1342 if ($1 != NULL) {
1343 if (astbuf1->children.size() != 1)
1344 frontend_verilog_yyerror("integer/real parameters should not have a range.");
1345 astbuf1->children.push_back($1);
1346 }
1347 };
1348
1349 param_type:
1350 param_signed param_integer param_real param_range |
1351 hierarchical_type_id {
1352 astbuf1->is_custom_type = true;
1353 astbuf1->children.push_back(new AstNode(AST_WIRETYPE));
1354 astbuf1->children.back()->str = *$1;
1355 };
1356
1357 param_decl:
1358 attr TOK_PARAMETER {
1359 astbuf1 = new AstNode(AST_PARAMETER);
1360 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
1361 append_attr(astbuf1, $1);
1362 } param_type param_decl_list ';' {
1363 delete astbuf1;
1364 };
1365
1366 localparam_decl:
1367 attr TOK_LOCALPARAM {
1368 astbuf1 = new AstNode(AST_LOCALPARAM);
1369 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
1370 append_attr(astbuf1, $1);
1371 } param_type param_decl_list ';' {
1372 delete astbuf1;
1373 };
1374
1375 param_decl_list:
1376 single_param_decl | param_decl_list ',' single_param_decl;
1377
1378 single_param_decl:
1379 TOK_ID '=' expr {
1380 AstNode *node;
1381 if (astbuf1 == nullptr) {
1382 if (!sv_mode)
1383 frontend_verilog_yyerror("In pure Verilog (not SystemVerilog), parameter/localparam with an initializer must use the parameter/localparam keyword");
1384 node = new AstNode(AST_PARAMETER);
1385 node->children.push_back(AstNode::mkconst_int(0, true));
1386 } else {
1387 node = astbuf1->clone();
1388 }
1389 node->str = *$1;
1390 delete node->children[0];
1391 node->children[0] = $3;
1392 ast_stack.back()->children.push_back(node);
1393 delete $1;
1394 };
1395
1396 defparam_decl:
1397 TOK_DEFPARAM defparam_decl_list ';';
1398
1399 defparam_decl_list:
1400 single_defparam_decl | defparam_decl_list ',' single_defparam_decl;
1401
1402 single_defparam_decl:
1403 range rvalue '=' expr {
1404 AstNode *node = new AstNode(AST_DEFPARAM);
1405 node->children.push_back($2);
1406 node->children.push_back($4);
1407 if ($1 != NULL)
1408 node->children.push_back($1);
1409 ast_stack.back()->children.push_back(node);
1410 };
1411
1412 /////////
1413 // enum
1414 /////////
1415
1416 enum_type: TOK_ENUM {
1417 static int enum_count;
1418 // create parent node for the enum
1419 astbuf2 = new AstNode(AST_ENUM);
1420 ast_stack.back()->children.push_back(astbuf2);
1421 astbuf2->str = std::string("$enum");
1422 astbuf2->str += std::to_string(enum_count++);
1423 // create the template for the names
1424 astbuf1 = new AstNode(AST_ENUM_ITEM);
1425 astbuf1->children.push_back(AstNode::mkconst_int(0, true));
1426 } enum_base_type '{' enum_name_list '}' { // create template for the enum vars
1427 auto tnode = astbuf1->clone();
1428 delete astbuf1;
1429 astbuf1 = tnode;
1430 tnode->type = AST_WIRE;
1431 tnode->attributes[ID::enum_type] = AstNode::mkconst_str(astbuf2->str);
1432 // drop constant but keep any range
1433 delete tnode->children[0];
1434 tnode->children.erase(tnode->children.begin());
1435 $$ = astbuf1; }
1436 ;
1437
1438 enum_base_type: type_atom type_signing
1439 | type_vec type_signing range { if ($3) astbuf1->children.push_back($3); }
1440 | /* nothing */ { astbuf1->is_reg = true; addRange(astbuf1); }
1441 ;
1442
1443 type_atom: TOK_INTEGER { astbuf1->is_reg = true; addRange(astbuf1); } // 4-state signed
1444 | TOK_INT { astbuf1->is_reg = true; addRange(astbuf1); } // 2-state signed
1445 | TOK_SHORTINT { astbuf1->is_reg = true; addRange(astbuf1, 15, 0); } // 2-state signed
1446 | TOK_BYTE { astbuf1->is_reg = true; addRange(astbuf1, 7, 0); } // 2-state signed
1447 ;
1448
1449 type_vec: TOK_REG { astbuf1->is_reg = true; } // unsigned
1450 | TOK_LOGIC { astbuf1->is_logic = true; } // unsigned
1451 ;
1452
1453 type_signing:
1454 TOK_SIGNED { astbuf1->is_signed = true; }
1455 | TOK_UNSIGNED { astbuf1->is_signed = false; }
1456 | // optional
1457 ;
1458
1459 enum_name_list: enum_name_decl
1460 | enum_name_list ',' enum_name_decl
1461 ;
1462
1463 enum_name_decl:
1464 TOK_ID opt_enum_init {
1465 // put in fn
1466 log_assert(astbuf1);
1467 log_assert(astbuf2);
1468 auto node = astbuf1->clone();
1469 node->str = *$1;
1470 delete $1;
1471 SET_AST_NODE_LOC(node, @1, @1);
1472 delete node->children[0];
1473 node->children[0] = $2 ?: new AstNode(AST_NONE);
1474 astbuf2->children.push_back(node);
1475 }
1476 ;
1477
1478 opt_enum_init:
1479 '=' basic_expr { $$ = $2; } // TODO: restrict this
1480 | /* optional */ { $$ = NULL; }
1481 ;
1482
1483 enum_var_list:
1484 enum_var
1485 | enum_var_list ',' enum_var
1486 ;
1487
1488 enum_var: TOK_ID {
1489 log_assert(astbuf1);
1490 log_assert(astbuf2);
1491 auto node = astbuf1->clone();
1492 ast_stack.back()->children.push_back(node);
1493 node->str = *$1;
1494 delete $1;
1495 SET_AST_NODE_LOC(node, @1, @1);
1496 node->is_enum = true;
1497 }
1498 ;
1499
1500 enum_decl: enum_type enum_var_list ';' { delete $1; }
1501 ;
1502
1503 //////////////////
1504 // struct or union
1505 //////////////////
1506
1507 struct_decl: struct_type struct_var_list ';' { delete astbuf2; }
1508 ;
1509
1510 struct_type: struct_union { astbuf2 = new AstNode($1); } opt_packed '{' struct_member_list '}' { $$ = astbuf2; }
1511 ;
1512
1513 struct_union:
1514 TOK_STRUCT { $$ = AST_STRUCT; }
1515 | TOK_UNION { $$ = AST_UNION; }
1516 ;
1517
1518
1519 opt_packed: TOK_PACKED opt_signed_struct
1520 | { frontend_verilog_yyerror("Only PACKED supported at this time"); }
1521 ;
1522
1523 opt_signed_struct:
1524 TOK_SIGNED { astbuf2->is_signed = true; }
1525 | TOK_UNSIGNED { astbuf2->is_signed = false; }
1526 | // default is unsigned
1527 ;
1528
1529 struct_member_list: struct_member
1530 | struct_member_list struct_member
1531 ;
1532
1533 struct_member: struct_member_type member_name_list ';' { delete astbuf1; }
1534 ;
1535
1536 member_name_list:
1537 member_name
1538 | member_name_list ',' member_name
1539 ;
1540
1541 member_name: TOK_ID {
1542 astbuf1->str = $1->substr(1);
1543 delete $1;
1544 auto member_node = astbuf1->clone();
1545 SET_AST_NODE_LOC(member_node, @1, @1);
1546 astbuf2->children.push_back(member_node);
1547 }
1548 ;
1549
1550 struct_member_type: { astbuf1 = new AstNode(AST_STRUCT_ITEM); } member_type_token_list
1551 ;
1552
1553 member_type_token_list:
1554 member_type
1555 | hierarchical_type_id {
1556 // use a clone of the typedef definition nodes
1557 auto template_node = copyTypeDefinition(*$1);
1558 delete $1;
1559 switch (template_node->type) {
1560 case AST_WIRE:
1561 template_node->type = AST_STRUCT_ITEM;
1562 break;
1563 case AST_STRUCT:
1564 case AST_UNION:
1565 break;
1566 default:
1567 frontend_verilog_yyerror("Invalid type for struct member: %s", type2str(template_node->type).c_str());
1568 }
1569 delete astbuf1;
1570 astbuf1 = template_node;
1571 }
1572 ;
1573
1574 member_type: type_atom type_signing
1575 | type_vec type_signing range { if ($3) astbuf1->children.push_back($3); }
1576 ;
1577
1578 struct_var_list: struct_var
1579 | struct_var_list ',' struct_var
1580 ;
1581
1582 struct_var: TOK_ID { auto *var_node = astbuf2->clone();
1583 var_node->str = *$1;
1584 delete $1;
1585 SET_AST_NODE_LOC(var_node, @1, @1);
1586 ast_stack.back()->children.push_back(var_node);
1587 }
1588 ;
1589
1590 /////////
1591 // wire
1592 /////////
1593
1594 wire_decl:
1595 attr wire_type range {
1596 albuf = $1;
1597 astbuf1 = $2;
1598 astbuf2 = checkRange(astbuf1, $3);
1599 } delay wire_name_list {
1600 delete astbuf1;
1601 if (astbuf2 != NULL)
1602 delete astbuf2;
1603 free_attr(albuf);
1604 } ';' |
1605 attr TOK_SUPPLY0 TOK_ID {
1606 ast_stack.back()->children.push_back(new AstNode(AST_WIRE));
1607 ast_stack.back()->children.back()->str = *$3;
1608 append_attr(ast_stack.back()->children.back(), $1);
1609 ast_stack.back()->children.push_back(new AstNode(AST_ASSIGN, new AstNode(AST_IDENTIFIER), AstNode::mkconst_int(0, false, 1)));
1610 ast_stack.back()->children.back()->children[0]->str = *$3;
1611 delete $3;
1612 } opt_supply_wires ';' |
1613 attr TOK_SUPPLY1 TOK_ID {
1614 ast_stack.back()->children.push_back(new AstNode(AST_WIRE));
1615 ast_stack.back()->children.back()->str = *$3;
1616 append_attr(ast_stack.back()->children.back(), $1);
1617 ast_stack.back()->children.push_back(new AstNode(AST_ASSIGN, new AstNode(AST_IDENTIFIER), AstNode::mkconst_int(1, false, 1)));
1618 ast_stack.back()->children.back()->children[0]->str = *$3;
1619 delete $3;
1620 } opt_supply_wires ';';
1621
1622 opt_supply_wires:
1623 /* empty */ |
1624 opt_supply_wires ',' TOK_ID {
1625 AstNode *wire_node = ast_stack.back()->children.at(GetSize(ast_stack.back()->children)-2)->clone();
1626 AstNode *assign_node = ast_stack.back()->children.at(GetSize(ast_stack.back()->children)-1)->clone();
1627 wire_node->str = *$3;
1628 assign_node->children[0]->str = *$3;
1629 ast_stack.back()->children.push_back(wire_node);
1630 ast_stack.back()->children.push_back(assign_node);
1631 delete $3;
1632 };
1633
1634 wire_name_list:
1635 wire_name_and_opt_assign | wire_name_list ',' wire_name_and_opt_assign;
1636
1637 wire_name_and_opt_assign:
1638 wire_name {
1639 bool attr_anyconst = false;
1640 bool attr_anyseq = false;
1641 bool attr_allconst = false;
1642 bool attr_allseq = false;
1643 if (ast_stack.back()->children.back()->get_bool_attribute(ID::anyconst)) {
1644 delete ast_stack.back()->children.back()->attributes.at(ID::anyconst);
1645 ast_stack.back()->children.back()->attributes.erase(ID::anyconst);
1646 attr_anyconst = true;
1647 }
1648 if (ast_stack.back()->children.back()->get_bool_attribute(ID::anyseq)) {
1649 delete ast_stack.back()->children.back()->attributes.at(ID::anyseq);
1650 ast_stack.back()->children.back()->attributes.erase(ID::anyseq);
1651 attr_anyseq = true;
1652 }
1653 if (ast_stack.back()->children.back()->get_bool_attribute(ID::allconst)) {
1654 delete ast_stack.back()->children.back()->attributes.at(ID::allconst);
1655 ast_stack.back()->children.back()->attributes.erase(ID::allconst);
1656 attr_allconst = true;
1657 }
1658 if (ast_stack.back()->children.back()->get_bool_attribute(ID::allseq)) {
1659 delete ast_stack.back()->children.back()->attributes.at(ID::allseq);
1660 ast_stack.back()->children.back()->attributes.erase(ID::allseq);
1661 attr_allseq = true;
1662 }
1663 if (current_wire_rand || attr_anyconst || attr_anyseq || attr_allconst || attr_allseq) {
1664 AstNode *wire = new AstNode(AST_IDENTIFIER);
1665 AstNode *fcall = new AstNode(AST_FCALL);
1666 wire->str = ast_stack.back()->children.back()->str;
1667 fcall->str = current_wire_const ? "\\$anyconst" : "\\$anyseq";
1668 if (attr_anyconst)
1669 fcall->str = "\\$anyconst";
1670 if (attr_anyseq)
1671 fcall->str = "\\$anyseq";
1672 if (attr_allconst)
1673 fcall->str = "\\$allconst";
1674 if (attr_allseq)
1675 fcall->str = "\\$allseq";
1676 fcall->attributes[ID::reg] = AstNode::mkconst_str(RTLIL::unescape_id(wire->str));
1677 ast_stack.back()->children.push_back(new AstNode(AST_ASSIGN, wire, fcall));
1678 }
1679 } |
1680 wire_name '=' expr {
1681 AstNode *wire = new AstNode(AST_IDENTIFIER);
1682 wire->str = ast_stack.back()->children.back()->str;
1683 if (astbuf1->is_input) {
1684 if (astbuf1->attributes.count(ID::defaultvalue))
1685 delete astbuf1->attributes.at(ID::defaultvalue);
1686 astbuf1->attributes[ID::defaultvalue] = $3;
1687 }
1688 else if (astbuf1->is_reg || astbuf1->is_logic){
1689 AstNode *assign = new AstNode(AST_ASSIGN_LE, wire, $3);
1690 AstNode *block = new AstNode(AST_BLOCK, assign);
1691 AstNode *init = new AstNode(AST_INITIAL, block);
1692
1693 SET_AST_NODE_LOC(assign, @1, @3);
1694 SET_AST_NODE_LOC(block, @1, @3);
1695 SET_AST_NODE_LOC(init, @1, @3);
1696
1697 ast_stack.back()->children.push_back(init);
1698 }
1699 else {
1700 AstNode *assign = new AstNode(AST_ASSIGN, wire, $3);
1701 SET_AST_NODE_LOC(assign, @1, @3);
1702 ast_stack.back()->children.push_back(assign);
1703 }
1704
1705 };
1706
1707 wire_name:
1708 TOK_ID range_or_multirange {
1709 if (astbuf1 == nullptr)
1710 frontend_verilog_yyerror("Internal error - should not happen - no AST_WIRE node.");
1711 AstNode *node = astbuf1->clone();
1712 node->str = *$1;
1713 append_attr_clone(node, albuf);
1714 if (astbuf2 != NULL)
1715 node->children.push_back(astbuf2->clone());
1716 if ($2 != NULL) {
1717 if (node->is_input || node->is_output)
1718 frontend_verilog_yyerror("input/output/inout ports cannot have unpacked dimensions.");
1719 if (!astbuf2 && !node->is_custom_type) {
1720 addRange(node, 0, 0, false);
1721 }
1722 rewriteAsMemoryNode(node, $2);
1723 }
1724 if (current_function_or_task == NULL) {
1725 if (do_not_require_port_stubs && (node->is_input || node->is_output) && port_stubs.count(*$1) == 0) {
1726 port_stubs[*$1] = ++port_counter;
1727 }
1728 if (port_stubs.count(*$1) != 0) {
1729 if (!node->is_input && !node->is_output)
1730 frontend_verilog_yyerror("Module port `%s' is neither input nor output.", $1->c_str());
1731 if (node->is_reg && node->is_input && !node->is_output && !sv_mode)
1732 frontend_verilog_yyerror("Input port `%s' is declared as register.", $1->c_str());
1733 node->port_id = port_stubs[*$1];
1734 port_stubs.erase(*$1);
1735 } else {
1736 if (node->is_input || node->is_output)
1737 frontend_verilog_yyerror("Module port `%s' is not declared in module header.", $1->c_str());
1738 }
1739 } else {
1740 if (node->is_input || node->is_output)
1741 node->port_id = current_function_or_task_port_id++;
1742 }
1743 //FIXME: for some reason, TOK_ID has a location which always points to one column *after* the real last column...
1744 SET_AST_NODE_LOC(node, @1, @1);
1745 ast_stack.back()->children.push_back(node);
1746
1747 delete $1;
1748 };
1749
1750 assign_stmt:
1751 TOK_ASSIGN delay assign_expr_list ';';
1752
1753 assign_expr_list:
1754 assign_expr | assign_expr_list ',' assign_expr;
1755
1756 assign_expr:
1757 lvalue '=' expr {
1758 AstNode *node = new AstNode(AST_ASSIGN, $1, $3);
1759 SET_AST_NODE_LOC(node, @$, @$);
1760 ast_stack.back()->children.push_back(node);
1761 };
1762
1763 type_name: TOK_ID // first time seen
1764 | TOK_USER_TYPE { if (isInLocalScope($1)) frontend_verilog_yyerror("Duplicate declaration of TYPEDEF '%s'", $1->c_str()+1); }
1765 ;
1766
1767 typedef_decl:
1768 TOK_TYPEDEF wire_type range type_name range_or_multirange ';' {
1769 astbuf1 = $2;
1770 astbuf2 = checkRange(astbuf1, $3);
1771 if (astbuf2)
1772 astbuf1->children.push_back(astbuf2);
1773
1774 if ($5 != NULL) {
1775 if (!astbuf2) {
1776 addRange(astbuf1, 0, 0, false);
1777 }
1778 rewriteAsMemoryNode(astbuf1, $5);
1779 }
1780 addTypedefNode($4, astbuf1); }
1781 | TOK_TYPEDEF non_wire_data_type type_name ';' { addTypedefNode($3, $2); }
1782 ;
1783
1784 non_wire_data_type:
1785 enum_type
1786 | struct_type
1787 ;
1788
1789 cell_stmt:
1790 attr TOK_ID {
1791 astbuf1 = new AstNode(AST_CELL);
1792 append_attr(astbuf1, $1);
1793 astbuf1->children.push_back(new AstNode(AST_CELLTYPE));
1794 astbuf1->children[0]->str = *$2;
1795 delete $2;
1796 } cell_parameter_list_opt cell_list ';' {
1797 delete astbuf1;
1798 } |
1799 attr tok_prim_wrapper delay {
1800 astbuf1 = new AstNode(AST_PRIMITIVE);
1801 astbuf1->str = *$2;
1802 append_attr(astbuf1, $1);
1803 delete $2;
1804 } prim_list ';' {
1805 delete astbuf1;
1806 };
1807
1808 tok_prim_wrapper:
1809 TOK_PRIMITIVE {
1810 $$ = $1;
1811 } |
1812 TOK_OR {
1813 $$ = new std::string("or");
1814 };
1815
1816 cell_list:
1817 single_cell |
1818 cell_list ',' single_cell;
1819
1820 single_cell:
1821 TOK_ID {
1822 astbuf2 = astbuf1->clone();
1823 if (astbuf2->type != AST_PRIMITIVE)
1824 astbuf2->str = *$1;
1825 delete $1;
1826 ast_stack.back()->children.push_back(astbuf2);
1827 } '(' cell_port_list ')' {
1828 SET_AST_NODE_LOC(astbuf2, @1, @$);
1829 } |
1830 TOK_ID non_opt_range {
1831 astbuf2 = astbuf1->clone();
1832 if (astbuf2->type != AST_PRIMITIVE)
1833 astbuf2->str = *$1;
1834 delete $1;
1835 ast_stack.back()->children.push_back(new AstNode(AST_CELLARRAY, $2, astbuf2));
1836 } '(' cell_port_list ')'{
1837 SET_AST_NODE_LOC(astbuf2, @1, @$);
1838 };
1839
1840 prim_list:
1841 single_prim |
1842 prim_list ',' single_prim;
1843
1844 single_prim:
1845 single_cell |
1846 /* no name */ {
1847 astbuf2 = astbuf1->clone();
1848 ast_stack.back()->children.push_back(astbuf2);
1849 } '(' cell_port_list ')' {
1850 SET_AST_NODE_LOC(astbuf2, @1, @$);
1851 }
1852
1853 cell_parameter_list_opt:
1854 '#' '(' cell_parameter_list ')' | /* empty */;
1855
1856 cell_parameter_list:
1857 cell_parameter | cell_parameter_list ',' cell_parameter;
1858
1859 cell_parameter:
1860 /* empty */ |
1861 expr {
1862 AstNode *node = new AstNode(AST_PARASET);
1863 astbuf1->children.push_back(node);
1864 node->children.push_back($1);
1865 } |
1866 '.' TOK_ID '(' expr ')' {
1867 AstNode *node = new AstNode(AST_PARASET);
1868 node->str = *$2;
1869 astbuf1->children.push_back(node);
1870 node->children.push_back($4);
1871 delete $2;
1872 };
1873
1874 cell_port_list:
1875 cell_port_list_rules {
1876 // remove empty args from end of list
1877 while (!astbuf2->children.empty()) {
1878 AstNode *node = astbuf2->children.back();
1879 if (node->type != AST_ARGUMENT) break;
1880 if (!node->children.empty()) break;
1881 if (!node->str.empty()) break;
1882 astbuf2->children.pop_back();
1883 delete node;
1884 }
1885
1886 // check port types
1887 bool has_positional_args = false;
1888 bool has_named_args = false;
1889 for (auto node : astbuf2->children) {
1890 if (node->type != AST_ARGUMENT) continue;
1891 if (node->str.empty())
1892 has_positional_args = true;
1893 else
1894 has_named_args = true;
1895 }
1896
1897 if (has_positional_args && has_named_args)
1898 frontend_verilog_yyerror("Mix of positional and named cell ports.");
1899 };
1900
1901 cell_port_list_rules:
1902 cell_port | cell_port_list_rules ',' cell_port;
1903
1904 cell_port:
1905 attr {
1906 AstNode *node = new AstNode(AST_ARGUMENT);
1907 astbuf2->children.push_back(node);
1908 free_attr($1);
1909 } |
1910 attr expr {
1911 AstNode *node = new AstNode(AST_ARGUMENT);
1912 astbuf2->children.push_back(node);
1913 node->children.push_back($2);
1914 free_attr($1);
1915 } |
1916 attr '.' TOK_ID '(' expr ')' {
1917 AstNode *node = new AstNode(AST_ARGUMENT);
1918 node->str = *$3;
1919 astbuf2->children.push_back(node);
1920 node->children.push_back($5);
1921 delete $3;
1922 free_attr($1);
1923 } |
1924 attr '.' TOK_ID '(' ')' {
1925 AstNode *node = new AstNode(AST_ARGUMENT);
1926 node->str = *$3;
1927 astbuf2->children.push_back(node);
1928 delete $3;
1929 free_attr($1);
1930 } |
1931 attr '.' TOK_ID {
1932 AstNode *node = new AstNode(AST_ARGUMENT);
1933 node->str = *$3;
1934 astbuf2->children.push_back(node);
1935 node->children.push_back(new AstNode(AST_IDENTIFIER));
1936 node->children.back()->str = *$3;
1937 delete $3;
1938 free_attr($1);
1939 } |
1940 attr TOK_WILDCARD_CONNECT {
1941 if (!sv_mode)
1942 frontend_verilog_yyerror("Wildcard port connections are only supported in SystemVerilog mode.");
1943 astbuf2->attributes[ID::wildcard_port_conns] = AstNode::mkconst_int(1, false);
1944 };
1945
1946 always_comb_or_latch:
1947 TOK_ALWAYS_COMB {
1948 $$ = false;
1949 } |
1950 TOK_ALWAYS_LATCH {
1951 $$ = true;
1952 };
1953
1954 always_or_always_ff:
1955 TOK_ALWAYS {
1956 $$ = false;
1957 } |
1958 TOK_ALWAYS_FF {
1959 $$ = true;
1960 };
1961
1962 always_stmt:
1963 attr always_or_always_ff {
1964 AstNode *node = new AstNode(AST_ALWAYS);
1965 append_attr(node, $1);
1966 if ($2)
1967 node->attributes[ID::always_ff] = AstNode::mkconst_int(1, false);
1968 ast_stack.back()->children.push_back(node);
1969 ast_stack.push_back(node);
1970 } always_cond {
1971 AstNode *block = new AstNode(AST_BLOCK);
1972 ast_stack.back()->children.push_back(block);
1973 ast_stack.push_back(block);
1974 } behavioral_stmt {
1975 SET_AST_NODE_LOC(ast_stack.back(), @6, @6);
1976 ast_stack.pop_back();
1977
1978 SET_AST_NODE_LOC(ast_stack.back(), @2, @$);
1979 ast_stack.pop_back();
1980
1981 SET_RULE_LOC(@$, @2, @$);
1982 } |
1983 attr always_comb_or_latch {
1984 AstNode *node = new AstNode(AST_ALWAYS);
1985 append_attr(node, $1);
1986 if ($2)
1987 node->attributes[ID::always_latch] = AstNode::mkconst_int(1, false);
1988 else
1989 node->attributes[ID::always_comb] = AstNode::mkconst_int(1, false);
1990 ast_stack.back()->children.push_back(node);
1991 ast_stack.push_back(node);
1992 AstNode *block = new AstNode(AST_BLOCK);
1993 ast_stack.back()->children.push_back(block);
1994 ast_stack.push_back(block);
1995 } behavioral_stmt {
1996 ast_stack.pop_back();
1997 ast_stack.pop_back();
1998 } |
1999 attr TOK_INITIAL {
2000 AstNode *node = new AstNode(AST_INITIAL);
2001 append_attr(node, $1);
2002 ast_stack.back()->children.push_back(node);
2003 ast_stack.push_back(node);
2004 AstNode *block = new AstNode(AST_BLOCK);
2005 ast_stack.back()->children.push_back(block);
2006 ast_stack.push_back(block);
2007 } behavioral_stmt {
2008 ast_stack.pop_back();
2009 ast_stack.pop_back();
2010 };
2011
2012 always_cond:
2013 '@' '(' always_events ')' |
2014 '@' '(' '*' ')' |
2015 '@' ATTR_BEGIN ')' |
2016 '@' '(' ATTR_END |
2017 '@' '*' |
2018 /* empty */;
2019
2020 always_events:
2021 always_event |
2022 always_events TOK_OR always_event |
2023 always_events ',' always_event;
2024
2025 always_event:
2026 TOK_POSEDGE expr {
2027 AstNode *node = new AstNode(AST_POSEDGE);
2028 SET_AST_NODE_LOC(node, @1, @1);
2029 ast_stack.back()->children.push_back(node);
2030 node->children.push_back($2);
2031 } |
2032 TOK_NEGEDGE expr {
2033 AstNode *node = new AstNode(AST_NEGEDGE);
2034 SET_AST_NODE_LOC(node, @1, @1);
2035 ast_stack.back()->children.push_back(node);
2036 node->children.push_back($2);
2037 } |
2038 expr {
2039 AstNode *node = new AstNode(AST_EDGE);
2040 ast_stack.back()->children.push_back(node);
2041 node->children.push_back($1);
2042 };
2043
2044 opt_label:
2045 ':' TOK_ID {
2046 $$ = $2;
2047 } |
2048 /* empty */ {
2049 $$ = NULL;
2050 };
2051
2052 opt_sva_label:
2053 TOK_SVA_LABEL ':' {
2054 $$ = $1;
2055 } |
2056 /* empty */ {
2057 $$ = NULL;
2058 };
2059
2060 opt_property:
2061 TOK_PROPERTY {
2062 $$ = true;
2063 } |
2064 TOK_FINAL {
2065 $$ = false;
2066 } |
2067 /* empty */ {
2068 $$ = false;
2069 };
2070
2071 modport_stmt:
2072 TOK_MODPORT TOK_ID {
2073 AstNode *modport = new AstNode(AST_MODPORT);
2074 ast_stack.back()->children.push_back(modport);
2075 ast_stack.push_back(modport);
2076 modport->str = *$2;
2077 delete $2;
2078 } modport_args_opt {
2079 ast_stack.pop_back();
2080 log_assert(ast_stack.size() == 2);
2081 } ';'
2082
2083 modport_args_opt:
2084 '(' ')' | '(' modport_args optional_comma ')';
2085
2086 modport_args:
2087 modport_arg | modport_args ',' modport_arg;
2088
2089 modport_arg:
2090 modport_type_token modport_member |
2091 modport_member
2092
2093 modport_member:
2094 TOK_ID {
2095 AstNode *modport_member = new AstNode(AST_MODPORTMEMBER);
2096 ast_stack.back()->children.push_back(modport_member);
2097 modport_member->str = *$1;
2098 modport_member->is_input = current_modport_input;
2099 modport_member->is_output = current_modport_output;
2100 delete $1;
2101 }
2102
2103 modport_type_token:
2104 TOK_INPUT {current_modport_input = 1; current_modport_output = 0;} | TOK_OUTPUT {current_modport_input = 0; current_modport_output = 1;}
2105
2106 assert:
2107 opt_sva_label TOK_ASSERT opt_property '(' expr ')' ';' {
2108 if (noassert_mode) {
2109 delete $5;
2110 } else {
2111 AstNode *node = new AstNode(assume_asserts_mode ? AST_ASSUME : AST_ASSERT, $5);
2112 SET_AST_NODE_LOC(node, @1, @6);
2113 if ($1 != nullptr)
2114 node->str = *$1;
2115 ast_stack.back()->children.push_back(node);
2116 }
2117 if ($1 != nullptr)
2118 delete $1;
2119 } |
2120 opt_sva_label TOK_ASSUME opt_property '(' expr ')' ';' {
2121 if (noassume_mode) {
2122 delete $5;
2123 } else {
2124 AstNode *node = new AstNode(assert_assumes_mode ? AST_ASSERT : AST_ASSUME, $5);
2125 SET_AST_NODE_LOC(node, @1, @6);
2126 if ($1 != nullptr)
2127 node->str = *$1;
2128 ast_stack.back()->children.push_back(node);
2129 }
2130 if ($1 != nullptr)
2131 delete $1;
2132 } |
2133 opt_sva_label TOK_ASSERT opt_property '(' TOK_EVENTUALLY expr ')' ';' {
2134 if (noassert_mode) {
2135 delete $6;
2136 } else {
2137 AstNode *node = new AstNode(assume_asserts_mode ? AST_FAIR : AST_LIVE, $6);
2138 SET_AST_NODE_LOC(node, @1, @7);
2139 if ($1 != nullptr)
2140 node->str = *$1;
2141 ast_stack.back()->children.push_back(node);
2142 }
2143 if ($1 != nullptr)
2144 delete $1;
2145 } |
2146 opt_sva_label TOK_ASSUME opt_property '(' TOK_EVENTUALLY expr ')' ';' {
2147 if (noassume_mode) {
2148 delete $6;
2149 } else {
2150 AstNode *node = new AstNode(assert_assumes_mode ? AST_LIVE : AST_FAIR, $6);
2151 SET_AST_NODE_LOC(node, @1, @7);
2152 if ($1 != nullptr)
2153 node->str = *$1;
2154 ast_stack.back()->children.push_back(node);
2155 }
2156 if ($1 != nullptr)
2157 delete $1;
2158 } |
2159 opt_sva_label TOK_COVER opt_property '(' expr ')' ';' {
2160 AstNode *node = new AstNode(AST_COVER, $5);
2161 SET_AST_NODE_LOC(node, @1, @6);
2162 if ($1 != nullptr) {
2163 node->str = *$1;
2164 delete $1;
2165 }
2166 ast_stack.back()->children.push_back(node);
2167 } |
2168 opt_sva_label TOK_COVER opt_property '(' ')' ';' {
2169 AstNode *node = new AstNode(AST_COVER, AstNode::mkconst_int(1, false));
2170 SET_AST_NODE_LOC(node, @1, @5);
2171 if ($1 != nullptr) {
2172 node->str = *$1;
2173 delete $1;
2174 }
2175 ast_stack.back()->children.push_back(node);
2176 } |
2177 opt_sva_label TOK_COVER ';' {
2178 AstNode *node = new AstNode(AST_COVER, AstNode::mkconst_int(1, false));
2179 SET_AST_NODE_LOC(node, @1, @2);
2180 if ($1 != nullptr) {
2181 node->str = *$1;
2182 delete $1;
2183 }
2184 ast_stack.back()->children.push_back(node);
2185 } |
2186 opt_sva_label TOK_RESTRICT opt_property '(' expr ')' ';' {
2187 if (norestrict_mode) {
2188 delete $5;
2189 } else {
2190 AstNode *node = new AstNode(AST_ASSUME, $5);
2191 SET_AST_NODE_LOC(node, @1, @6);
2192 if ($1 != nullptr)
2193 node->str = *$1;
2194 ast_stack.back()->children.push_back(node);
2195 }
2196 if (!$3)
2197 log_file_warning(current_filename, get_line_num(), "SystemVerilog does not allow \"restrict\" without \"property\".\n");
2198 if ($1 != nullptr)
2199 delete $1;
2200 } |
2201 opt_sva_label TOK_RESTRICT opt_property '(' TOK_EVENTUALLY expr ')' ';' {
2202 if (norestrict_mode) {
2203 delete $6;
2204 } else {
2205 AstNode *node = new AstNode(AST_FAIR, $6);
2206 SET_AST_NODE_LOC(node, @1, @7);
2207 if ($1 != nullptr)
2208 node->str = *$1;
2209 ast_stack.back()->children.push_back(node);
2210 }
2211 if (!$3)
2212 log_file_warning(current_filename, get_line_num(), "SystemVerilog does not allow \"restrict\" without \"property\".\n");
2213 if ($1 != nullptr)
2214 delete $1;
2215 };
2216
2217 assert_property:
2218 opt_sva_label TOK_ASSERT TOK_PROPERTY '(' expr ')' ';' {
2219 AstNode *node = new AstNode(assume_asserts_mode ? AST_ASSUME : AST_ASSERT, $5);
2220 SET_AST_NODE_LOC(node, @1, @6);
2221 ast_stack.back()->children.push_back(node);
2222 if ($1 != nullptr) {
2223 ast_stack.back()->children.back()->str = *$1;
2224 delete $1;
2225 }
2226 } |
2227 opt_sva_label TOK_ASSUME TOK_PROPERTY '(' expr ')' ';' {
2228 AstNode *node = new AstNode(AST_ASSUME, $5);
2229 SET_AST_NODE_LOC(node, @1, @6);
2230 ast_stack.back()->children.push_back(node);
2231 if ($1 != nullptr) {
2232 ast_stack.back()->children.back()->str = *$1;
2233 delete $1;
2234 }
2235 } |
2236 opt_sva_label TOK_ASSERT TOK_PROPERTY '(' TOK_EVENTUALLY expr ')' ';' {
2237 AstNode *node = new AstNode(assume_asserts_mode ? AST_FAIR : AST_LIVE, $6);
2238 SET_AST_NODE_LOC(node, @1, @7);
2239 ast_stack.back()->children.push_back(node);
2240 if ($1 != nullptr) {
2241 ast_stack.back()->children.back()->str = *$1;
2242 delete $1;
2243 }
2244 } |
2245 opt_sva_label TOK_ASSUME TOK_PROPERTY '(' TOK_EVENTUALLY expr ')' ';' {
2246 AstNode *node = new AstNode(AST_FAIR, $6);
2247 SET_AST_NODE_LOC(node, @1, @7);
2248 ast_stack.back()->children.push_back(node);
2249 if ($1 != nullptr) {
2250 ast_stack.back()->children.back()->str = *$1;
2251 delete $1;
2252 }
2253 } |
2254 opt_sva_label TOK_COVER TOK_PROPERTY '(' expr ')' ';' {
2255 AstNode *node = new AstNode(AST_COVER, $5);
2256 SET_AST_NODE_LOC(node, @1, @6);
2257 ast_stack.back()->children.push_back(node);
2258 if ($1 != nullptr) {
2259 ast_stack.back()->children.back()->str = *$1;
2260 delete $1;
2261 }
2262 } |
2263 opt_sva_label TOK_RESTRICT TOK_PROPERTY '(' expr ')' ';' {
2264 if (norestrict_mode) {
2265 delete $5;
2266 } else {
2267 AstNode *node = new AstNode(AST_ASSUME, $5);
2268 SET_AST_NODE_LOC(node, @1, @6);
2269 ast_stack.back()->children.push_back(node);
2270 if ($1 != nullptr) {
2271 ast_stack.back()->children.back()->str = *$1;
2272 delete $1;
2273 }
2274 }
2275 } |
2276 opt_sva_label TOK_RESTRICT TOK_PROPERTY '(' TOK_EVENTUALLY expr ')' ';' {
2277 if (norestrict_mode) {
2278 delete $6;
2279 } else {
2280 AstNode *node = new AstNode(AST_FAIR, $6);
2281 SET_AST_NODE_LOC(node, @1, @7);
2282 ast_stack.back()->children.push_back(node);
2283 if ($1 != nullptr) {
2284 ast_stack.back()->children.back()->str = *$1;
2285 delete $1;
2286 }
2287 }
2288 };
2289
2290 simple_behavioral_stmt:
2291 lvalue '=' delay expr {
2292 AstNode *node = new AstNode(AST_ASSIGN_EQ, $1, $4);
2293 ast_stack.back()->children.push_back(node);
2294 SET_AST_NODE_LOC(node, @1, @4);
2295 } |
2296 lvalue TOK_INCREMENT {
2297 AstNode *node = new AstNode(AST_ASSIGN_EQ, $1, new AstNode(AST_ADD, $1->clone(), AstNode::mkconst_int(1, true)));
2298 ast_stack.back()->children.push_back(node);
2299 SET_AST_NODE_LOC(node, @1, @2);
2300 } |
2301 lvalue TOK_DECREMENT {
2302 AstNode *node = new AstNode(AST_ASSIGN_EQ, $1, new AstNode(AST_SUB, $1->clone(), AstNode::mkconst_int(1, true)));
2303 ast_stack.back()->children.push_back(node);
2304 SET_AST_NODE_LOC(node, @1, @2);
2305 } |
2306 lvalue OP_LE delay expr {
2307 AstNode *node = new AstNode(AST_ASSIGN_LE, $1, $4);
2308 ast_stack.back()->children.push_back(node);
2309 SET_AST_NODE_LOC(node, @1, @4);
2310 };
2311
2312 // this production creates the obligatory if-else shift/reduce conflict
2313 behavioral_stmt:
2314 defattr | assert | wire_decl | param_decl | localparam_decl | typedef_decl |
2315 non_opt_delay behavioral_stmt |
2316 simple_behavioral_stmt ';' | ';' |
2317 hierarchical_id attr {
2318 AstNode *node = new AstNode(AST_TCALL);
2319 node->str = *$1;
2320 delete $1;
2321 ast_stack.back()->children.push_back(node);
2322 ast_stack.push_back(node);
2323 append_attr(node, $2);
2324 } opt_arg_list ';'{
2325 ast_stack.pop_back();
2326 } |
2327 TOK_MSG_TASKS attr {
2328 AstNode *node = new AstNode(AST_TCALL);
2329 node->str = *$1;
2330 delete $1;
2331 ast_stack.back()->children.push_back(node);
2332 ast_stack.push_back(node);
2333 append_attr(node, $2);
2334 } opt_arg_list ';'{
2335 ast_stack.pop_back();
2336 } |
2337 attr TOK_BEGIN {
2338 enterTypeScope();
2339 } opt_label {
2340 AstNode *node = new AstNode(AST_BLOCK);
2341 ast_stack.back()->children.push_back(node);
2342 ast_stack.push_back(node);
2343 append_attr(node, $1);
2344 if ($4 != NULL)
2345 node->str = *$4;
2346 } behavioral_stmt_list TOK_END opt_label {
2347 exitTypeScope();
2348 if ($4 != NULL && $8 != NULL && *$4 != *$8)
2349 frontend_verilog_yyerror("Begin label (%s) and end label (%s) don't match.", $4->c_str()+1, $8->c_str()+1);
2350 SET_AST_NODE_LOC(ast_stack.back(), @2, @8);
2351 delete $4;
2352 delete $8;
2353 ast_stack.pop_back();
2354 } |
2355 attr TOK_FOR '(' {
2356 AstNode *node = new AstNode(AST_FOR);
2357 ast_stack.back()->children.push_back(node);
2358 ast_stack.push_back(node);
2359 append_attr(node, $1);
2360 } simple_behavioral_stmt ';' expr {
2361 ast_stack.back()->children.push_back($7);
2362 } ';' simple_behavioral_stmt ')' {
2363 AstNode *block = new AstNode(AST_BLOCK);
2364 ast_stack.back()->children.push_back(block);
2365 ast_stack.push_back(block);
2366 } behavioral_stmt {
2367 SET_AST_NODE_LOC(ast_stack.back(), @13, @13);
2368 ast_stack.pop_back();
2369 SET_AST_NODE_LOC(ast_stack.back(), @2, @13);
2370 ast_stack.pop_back();
2371 } |
2372 attr TOK_WHILE '(' expr ')' {
2373 AstNode *node = new AstNode(AST_WHILE);
2374 ast_stack.back()->children.push_back(node);
2375 ast_stack.push_back(node);
2376 append_attr(node, $1);
2377 AstNode *block = new AstNode(AST_BLOCK);
2378 ast_stack.back()->children.push_back($4);
2379 ast_stack.back()->children.push_back(block);
2380 ast_stack.push_back(block);
2381 } behavioral_stmt {
2382 SET_AST_NODE_LOC(ast_stack.back(), @7, @7);
2383 ast_stack.pop_back();
2384 ast_stack.pop_back();
2385 } |
2386 attr TOK_REPEAT '(' expr ')' {
2387 AstNode *node = new AstNode(AST_REPEAT);
2388 ast_stack.back()->children.push_back(node);
2389 ast_stack.push_back(node);
2390 append_attr(node, $1);
2391 AstNode *block = new AstNode(AST_BLOCK);
2392 ast_stack.back()->children.push_back($4);
2393 ast_stack.back()->children.push_back(block);
2394 ast_stack.push_back(block);
2395 } behavioral_stmt {
2396 SET_AST_NODE_LOC(ast_stack.back(), @7, @7);
2397 ast_stack.pop_back();
2398 ast_stack.pop_back();
2399 } |
2400 attr TOK_IF '(' expr ')' {
2401 AstNode *node = new AstNode(AST_CASE);
2402 AstNode *block = new AstNode(AST_BLOCK);
2403 AstNode *cond = new AstNode(AST_COND, AstNode::mkconst_int(1, false, 1), block);
2404 SET_AST_NODE_LOC(cond, @4, @4);
2405 ast_stack.back()->children.push_back(node);
2406 node->children.push_back(new AstNode(AST_REDUCE_BOOL, $4));
2407 node->children.push_back(cond);
2408 ast_stack.push_back(node);
2409 ast_stack.push_back(block);
2410 append_attr(node, $1);
2411 } behavioral_stmt {
2412 SET_AST_NODE_LOC(ast_stack.back(), @7, @7);
2413 } optional_else {
2414 ast_stack.pop_back();
2415 SET_AST_NODE_LOC(ast_stack.back(), @2, @9);
2416 ast_stack.pop_back();
2417 } |
2418 case_attr case_type '(' expr ')' {
2419 AstNode *node = new AstNode(AST_CASE, $4);
2420 ast_stack.back()->children.push_back(node);
2421 ast_stack.push_back(node);
2422 append_attr(node, $1);
2423 SET_AST_NODE_LOC(ast_stack.back(), @4, @4);
2424 } opt_synopsys_attr case_body TOK_ENDCASE {
2425 SET_AST_NODE_LOC(ast_stack.back(), @2, @9);
2426 case_type_stack.pop_back();
2427 ast_stack.pop_back();
2428 };
2429
2430 ;
2431
2432 unique_case_attr:
2433 /* empty */ {
2434 $$ = false;
2435 } |
2436 TOK_PRIORITY case_attr {
2437 $$ = $2;
2438 } |
2439 TOK_UNIQUE case_attr {
2440 $$ = true;
2441 };
2442
2443 case_attr:
2444 attr unique_case_attr {
2445 if ($2) (*$1)[ID::parallel_case] = AstNode::mkconst_int(1, false);
2446 $$ = $1;
2447 };
2448
2449 case_type:
2450 TOK_CASE {
2451 case_type_stack.push_back(0);
2452 } |
2453 TOK_CASEX {
2454 case_type_stack.push_back('x');
2455 } |
2456 TOK_CASEZ {
2457 case_type_stack.push_back('z');
2458 };
2459
2460 opt_synopsys_attr:
2461 opt_synopsys_attr TOK_SYNOPSYS_FULL_CASE {
2462 if (ast_stack.back()->attributes.count(ID::full_case) == 0)
2463 ast_stack.back()->attributes[ID::full_case] = AstNode::mkconst_int(1, false);
2464 } |
2465 opt_synopsys_attr TOK_SYNOPSYS_PARALLEL_CASE {
2466 if (ast_stack.back()->attributes.count(ID::parallel_case) == 0)
2467 ast_stack.back()->attributes[ID::parallel_case] = AstNode::mkconst_int(1, false);
2468 } |
2469 /* empty */;
2470
2471 behavioral_stmt_list:
2472 behavioral_stmt_list behavioral_stmt |
2473 /* empty */;
2474
2475 optional_else:
2476 TOK_ELSE {
2477 AstNode *block = new AstNode(AST_BLOCK);
2478 AstNode *cond = new AstNode(AST_COND, new AstNode(AST_DEFAULT), block);
2479 SET_AST_NODE_LOC(cond, @1, @1);
2480
2481 ast_stack.pop_back();
2482 ast_stack.back()->children.push_back(cond);
2483 ast_stack.push_back(block);
2484 } behavioral_stmt {
2485 SET_AST_NODE_LOC(ast_stack.back(), @3, @3);
2486 } |
2487 /* empty */ %prec FAKE_THEN;
2488
2489 case_body:
2490 case_body case_item |
2491 /* empty */;
2492
2493 case_item:
2494 {
2495 AstNode *node = new AstNode(
2496 case_type_stack.size() && case_type_stack.back() == 'x' ? AST_CONDX :
2497 case_type_stack.size() && case_type_stack.back() == 'z' ? AST_CONDZ : AST_COND);
2498 ast_stack.back()->children.push_back(node);
2499 ast_stack.push_back(node);
2500 } case_select {
2501 AstNode *block = new AstNode(AST_BLOCK);
2502 ast_stack.back()->children.push_back(block);
2503 ast_stack.push_back(block);
2504 case_type_stack.push_back(0);
2505 } behavioral_stmt {
2506 case_type_stack.pop_back();
2507 SET_AST_NODE_LOC(ast_stack.back(), @4, @4);
2508 ast_stack.pop_back();
2509 ast_stack.pop_back();
2510 };
2511
2512 gen_case_body:
2513 gen_case_body gen_case_item |
2514 /* empty */;
2515
2516 gen_case_item:
2517 {
2518 AstNode *node = new AstNode(
2519 case_type_stack.size() && case_type_stack.back() == 'x' ? AST_CONDX :
2520 case_type_stack.size() && case_type_stack.back() == 'z' ? AST_CONDZ : AST_COND);
2521 ast_stack.back()->children.push_back(node);
2522 ast_stack.push_back(node);
2523 } case_select {
2524 case_type_stack.push_back(0);
2525 SET_AST_NODE_LOC(ast_stack.back(), @2, @2);
2526 } gen_stmt_or_null {
2527 case_type_stack.pop_back();
2528 ast_stack.pop_back();
2529 };
2530
2531 case_select:
2532 case_expr_list ':' |
2533 TOK_DEFAULT;
2534
2535 case_expr_list:
2536 TOK_DEFAULT {
2537 AstNode *node = new AstNode(AST_DEFAULT);
2538 SET_AST_NODE_LOC(node, @1, @1);
2539 ast_stack.back()->children.push_back(node);
2540 } |
2541 TOK_SVA_LABEL {
2542 AstNode *node = new AstNode(AST_IDENTIFIER);
2543 SET_AST_NODE_LOC(node, @1, @1);
2544 ast_stack.back()->children.push_back(node);
2545 ast_stack.back()->children.back()->str = *$1;
2546 delete $1;
2547 } |
2548 expr {
2549 ast_stack.back()->children.push_back($1);
2550 } |
2551 case_expr_list ',' expr {
2552 ast_stack.back()->children.push_back($3);
2553 };
2554
2555 rvalue:
2556 hierarchical_id '[' expr ']' '.' rvalue {
2557 $$ = new AstNode(AST_PREFIX, $3, $6);
2558 $$->str = *$1;
2559 delete $1;
2560 } |
2561 hierarchical_id range {
2562 $$ = new AstNode(AST_IDENTIFIER, $2);
2563 $$->str = *$1;
2564 SET_AST_NODE_LOC($$, @1, @1);
2565 delete $1;
2566 if ($2 == nullptr && ($$->str == "\\$initstate" ||
2567 $$->str == "\\$anyconst" || $$->str == "\\$anyseq" ||
2568 $$->str == "\\$allconst" || $$->str == "\\$allseq"))
2569 $$->type = AST_FCALL;
2570 } |
2571 hierarchical_id non_opt_multirange {
2572 $$ = new AstNode(AST_IDENTIFIER, $2);
2573 $$->str = *$1;
2574 SET_AST_NODE_LOC($$, @1, @1);
2575 delete $1;
2576 };
2577
2578 lvalue:
2579 rvalue {
2580 $$ = $1;
2581 } |
2582 '{' lvalue_concat_list '}' {
2583 $$ = $2;
2584 };
2585
2586 lvalue_concat_list:
2587 expr {
2588 $$ = new AstNode(AST_CONCAT);
2589 $$->children.push_back($1);
2590 } |
2591 expr ',' lvalue_concat_list {
2592 $$ = $3;
2593 $$->children.push_back($1);
2594 };
2595
2596 opt_arg_list:
2597 '(' arg_list optional_comma ')' |
2598 /* empty */;
2599
2600 arg_list:
2601 arg_list2 |
2602 /* empty */;
2603
2604 arg_list2:
2605 single_arg |
2606 arg_list ',' single_arg;
2607
2608 single_arg:
2609 expr {
2610 ast_stack.back()->children.push_back($1);
2611 };
2612
2613 module_gen_body:
2614 module_gen_body gen_stmt_or_module_body_stmt |
2615 /* empty */;
2616
2617 gen_stmt_or_module_body_stmt:
2618 gen_stmt | module_body_stmt;
2619
2620 // this production creates the obligatory if-else shift/reduce conflict
2621 gen_stmt:
2622 TOK_FOR '(' {
2623 AstNode *node = new AstNode(AST_GENFOR);
2624 ast_stack.back()->children.push_back(node);
2625 ast_stack.push_back(node);
2626 } simple_behavioral_stmt ';' expr {
2627 ast_stack.back()->children.push_back($6);
2628 } ';' simple_behavioral_stmt ')' gen_stmt_block {
2629 SET_AST_NODE_LOC(ast_stack.back(), @1, @11);
2630 ast_stack.pop_back();
2631 } |
2632 TOK_IF '(' expr ')' {
2633 AstNode *node = new AstNode(AST_GENIF);
2634 ast_stack.back()->children.push_back(node);
2635 ast_stack.push_back(node);
2636 ast_stack.back()->children.push_back($3);
2637 AstNode *block = new AstNode(AST_GENBLOCK);
2638 ast_stack.back()->children.push_back(block);
2639 ast_stack.push_back(block);
2640 } gen_stmt_or_null {
2641 ast_stack.pop_back();
2642 } opt_gen_else {
2643 SET_AST_NODE_LOC(ast_stack.back(), @1, @7);
2644 ast_stack.pop_back();
2645 } |
2646 case_type '(' expr ')' {
2647 AstNode *node = new AstNode(AST_GENCASE, $3);
2648 ast_stack.back()->children.push_back(node);
2649 ast_stack.push_back(node);
2650 } gen_case_body TOK_ENDCASE {
2651 case_type_stack.pop_back();
2652 SET_AST_NODE_LOC(ast_stack.back(), @1, @7);
2653 ast_stack.pop_back();
2654 } |
2655 TOK_BEGIN {
2656 enterTypeScope();
2657 } opt_label {
2658 AstNode *node = new AstNode(AST_GENBLOCK);
2659 node->str = $3 ? *$3 : std::string();
2660 ast_stack.back()->children.push_back(node);
2661 ast_stack.push_back(node);
2662 } module_gen_body TOK_END opt_label {
2663 exitTypeScope();
2664 delete $3;
2665 delete $7;
2666 SET_AST_NODE_LOC(ast_stack.back(), @1, @7);
2667 ast_stack.pop_back();
2668 } |
2669 TOK_MSG_TASKS {
2670 AstNode *node = new AstNode(AST_TECALL);
2671 node->str = *$1;
2672 delete $1;
2673 ast_stack.back()->children.push_back(node);
2674 ast_stack.push_back(node);
2675 } opt_arg_list ';'{
2676 SET_AST_NODE_LOC(ast_stack.back(), @1, @3);
2677 ast_stack.pop_back();
2678 };
2679
2680 gen_stmt_block:
2681 {
2682 AstNode *node = new AstNode(AST_GENBLOCK);
2683 ast_stack.back()->children.push_back(node);
2684 ast_stack.push_back(node);
2685 } gen_stmt_or_module_body_stmt {
2686 SET_AST_NODE_LOC(ast_stack.back(), @2, @2);
2687 ast_stack.pop_back();
2688 };
2689
2690 gen_stmt_or_null:
2691 gen_stmt_block | ';';
2692
2693 opt_gen_else:
2694 TOK_ELSE gen_stmt_or_null | /* empty */ %prec FAKE_THEN;
2695
2696 expr:
2697 basic_expr {
2698 $$ = $1;
2699 } |
2700 basic_expr '?' attr expr ':' expr {
2701 $$ = new AstNode(AST_TERNARY);
2702 $$->children.push_back($1);
2703 $$->children.push_back($4);
2704 $$->children.push_back($6);
2705 SET_AST_NODE_LOC($$, @1, @$);
2706 append_attr($$, $3);
2707 };
2708
2709 basic_expr:
2710 rvalue {
2711 $$ = $1;
2712 } |
2713 '(' expr ')' integral_number {
2714 if ($4->compare(0, 1, "'") != 0)
2715 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());
2716 AstNode *bits = $2;
2717 AstNode *val = const2ast(*$4, case_type_stack.size() == 0 ? 0 : case_type_stack.back(), !lib_mode);
2718 if (val == NULL)
2719 log_error("Value conversion failed: `%s'\n", $4->c_str());
2720 $$ = new AstNode(AST_TO_BITS, bits, val);
2721 delete $4;
2722 } |
2723 hierarchical_id integral_number {
2724 if ($2->compare(0, 1, "'") != 0)
2725 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());
2726 AstNode *bits = new AstNode(AST_IDENTIFIER);
2727 bits->str = *$1;
2728 SET_AST_NODE_LOC(bits, @1, @1);
2729 AstNode *val = const2ast(*$2, case_type_stack.size() == 0 ? 0 : case_type_stack.back(), !lib_mode);
2730 SET_AST_NODE_LOC(val, @2, @2);
2731 if (val == NULL)
2732 log_error("Value conversion failed: `%s'\n", $2->c_str());
2733 $$ = new AstNode(AST_TO_BITS, bits, val);
2734 delete $1;
2735 delete $2;
2736 } |
2737 integral_number {
2738 $$ = const2ast(*$1, case_type_stack.size() == 0 ? 0 : case_type_stack.back(), !lib_mode);
2739 SET_AST_NODE_LOC($$, @1, @1);
2740 if ($$ == NULL)
2741 log_error("Value conversion failed: `%s'\n", $1->c_str());
2742 delete $1;
2743 } |
2744 TOK_REALVAL {
2745 $$ = new AstNode(AST_REALVALUE);
2746 char *p = (char*)malloc(GetSize(*$1) + 1), *q;
2747 for (int i = 0, j = 0; j < GetSize(*$1); j++)
2748 if ((*$1)[j] != '_')
2749 p[i++] = (*$1)[j], p[i] = 0;
2750 $$->realvalue = strtod(p, &q);
2751 SET_AST_NODE_LOC($$, @1, @1);
2752 log_assert(*q == 0);
2753 delete $1;
2754 free(p);
2755 } |
2756 TOK_STRING {
2757 $$ = AstNode::mkconst_str(*$1);
2758 SET_AST_NODE_LOC($$, @1, @1);
2759 delete $1;
2760 } |
2761 hierarchical_id attr {
2762 AstNode *node = new AstNode(AST_FCALL);
2763 node->str = *$1;
2764 delete $1;
2765 ast_stack.push_back(node);
2766 SET_AST_NODE_LOC(node, @1, @1);
2767 append_attr(node, $2);
2768 } '(' arg_list optional_comma ')' {
2769 $$ = ast_stack.back();
2770 ast_stack.pop_back();
2771 } |
2772 TOK_TO_SIGNED attr '(' expr ')' {
2773 $$ = new AstNode(AST_TO_SIGNED, $4);
2774 append_attr($$, $2);
2775 } |
2776 TOK_TO_UNSIGNED attr '(' expr ')' {
2777 $$ = new AstNode(AST_TO_UNSIGNED, $4);
2778 append_attr($$, $2);
2779 } |
2780 '(' expr ')' {
2781 $$ = $2;
2782 } |
2783 '(' expr ':' expr ':' expr ')' {
2784 delete $2;
2785 $$ = $4;
2786 delete $6;
2787 } |
2788 '{' concat_list '}' {
2789 $$ = $2;
2790 } |
2791 '{' expr '{' concat_list '}' '}' {
2792 $$ = new AstNode(AST_REPLICATE, $2, $4);
2793 } |
2794 '~' attr basic_expr %prec UNARY_OPS {
2795 $$ = new AstNode(AST_BIT_NOT, $3);
2796 SET_AST_NODE_LOC($$, @1, @3);
2797 append_attr($$, $2);
2798 } |
2799 basic_expr '&' attr basic_expr {
2800 $$ = new AstNode(AST_BIT_AND, $1, $4);
2801 SET_AST_NODE_LOC($$, @1, @4);
2802 append_attr($$, $3);
2803 } |
2804 basic_expr OP_NAND attr basic_expr {
2805 $$ = new AstNode(AST_BIT_NOT, new AstNode(AST_BIT_AND, $1, $4));
2806 SET_AST_NODE_LOC($$, @1, @4);
2807 append_attr($$, $3);
2808 } |
2809 basic_expr '|' attr basic_expr {
2810 $$ = new AstNode(AST_BIT_OR, $1, $4);
2811 SET_AST_NODE_LOC($$, @1, @4);
2812 append_attr($$, $3);
2813 } |
2814 basic_expr OP_NOR attr basic_expr {
2815 $$ = new AstNode(AST_BIT_NOT, new AstNode(AST_BIT_OR, $1, $4));
2816 SET_AST_NODE_LOC($$, @1, @4);
2817 append_attr($$, $3);
2818 } |
2819 basic_expr '^' attr basic_expr {
2820 $$ = new AstNode(AST_BIT_XOR, $1, $4);
2821 SET_AST_NODE_LOC($$, @1, @4);
2822 append_attr($$, $3);
2823 } |
2824 basic_expr OP_XNOR attr basic_expr {
2825 $$ = new AstNode(AST_BIT_XNOR, $1, $4);
2826 SET_AST_NODE_LOC($$, @1, @4);
2827 append_attr($$, $3);
2828 } |
2829 '&' attr basic_expr %prec UNARY_OPS {
2830 $$ = new AstNode(AST_REDUCE_AND, $3);
2831 SET_AST_NODE_LOC($$, @1, @3);
2832 append_attr($$, $2);
2833 } |
2834 OP_NAND attr basic_expr %prec UNARY_OPS {
2835 $$ = new AstNode(AST_REDUCE_AND, $3);
2836 SET_AST_NODE_LOC($$, @1, @3);
2837 append_attr($$, $2);
2838 $$ = new AstNode(AST_LOGIC_NOT, $$);
2839 } |
2840 '|' attr basic_expr %prec UNARY_OPS {
2841 $$ = new AstNode(AST_REDUCE_OR, $3);
2842 SET_AST_NODE_LOC($$, @1, @3);
2843 append_attr($$, $2);
2844 } |
2845 OP_NOR attr basic_expr %prec UNARY_OPS {
2846 $$ = new AstNode(AST_REDUCE_OR, $3);
2847 SET_AST_NODE_LOC($$, @1, @3);
2848 append_attr($$, $2);
2849 $$ = new AstNode(AST_LOGIC_NOT, $$);
2850 SET_AST_NODE_LOC($$, @1, @3);
2851 } |
2852 '^' attr basic_expr %prec UNARY_OPS {
2853 $$ = new AstNode(AST_REDUCE_XOR, $3);
2854 SET_AST_NODE_LOC($$, @1, @3);
2855 append_attr($$, $2);
2856 } |
2857 OP_XNOR attr basic_expr %prec UNARY_OPS {
2858 $$ = new AstNode(AST_REDUCE_XNOR, $3);
2859 SET_AST_NODE_LOC($$, @1, @3);
2860 append_attr($$, $2);
2861 } |
2862 basic_expr OP_SHL attr basic_expr {
2863 $$ = new AstNode(AST_SHIFT_LEFT, $1, new AstNode(AST_TO_UNSIGNED, $4));
2864 SET_AST_NODE_LOC($$, @1, @4);
2865 append_attr($$, $3);
2866 } |
2867 basic_expr OP_SHR attr basic_expr {
2868 $$ = new AstNode(AST_SHIFT_RIGHT, $1, new AstNode(AST_TO_UNSIGNED, $4));
2869 SET_AST_NODE_LOC($$, @1, @4);
2870 append_attr($$, $3);
2871 } |
2872 basic_expr OP_SSHL attr basic_expr {
2873 $$ = new AstNode(AST_SHIFT_SLEFT, $1, new AstNode(AST_TO_UNSIGNED, $4));
2874 SET_AST_NODE_LOC($$, @1, @4);
2875 append_attr($$, $3);
2876 } |
2877 basic_expr OP_SSHR attr basic_expr {
2878 $$ = new AstNode(AST_SHIFT_SRIGHT, $1, new AstNode(AST_TO_UNSIGNED, $4));
2879 SET_AST_NODE_LOC($$, @1, @4);
2880 append_attr($$, $3);
2881 } |
2882 basic_expr '<' attr basic_expr {
2883 $$ = new AstNode(AST_LT, $1, $4);
2884 SET_AST_NODE_LOC($$, @1, @4);
2885 append_attr($$, $3);
2886 } |
2887 basic_expr OP_LE attr basic_expr {
2888 $$ = new AstNode(AST_LE, $1, $4);
2889 SET_AST_NODE_LOC($$, @1, @4);
2890 append_attr($$, $3);
2891 } |
2892 basic_expr OP_EQ attr basic_expr {
2893 $$ = new AstNode(AST_EQ, $1, $4);
2894 SET_AST_NODE_LOC($$, @1, @4);
2895 append_attr($$, $3);
2896 } |
2897 basic_expr OP_NE attr basic_expr {
2898 $$ = new AstNode(AST_NE, $1, $4);
2899 SET_AST_NODE_LOC($$, @1, @4);
2900 append_attr($$, $3);
2901 } |
2902 basic_expr OP_EQX attr basic_expr {
2903 $$ = new AstNode(AST_EQX, $1, $4);
2904 SET_AST_NODE_LOC($$, @1, @4);
2905 append_attr($$, $3);
2906 } |
2907 basic_expr OP_NEX attr basic_expr {
2908 $$ = new AstNode(AST_NEX, $1, $4);
2909 SET_AST_NODE_LOC($$, @1, @4);
2910 append_attr($$, $3);
2911 } |
2912 basic_expr OP_GE attr basic_expr {
2913 $$ = new AstNode(AST_GE, $1, $4);
2914 SET_AST_NODE_LOC($$, @1, @4);
2915 append_attr($$, $3);
2916 } |
2917 basic_expr '>' attr basic_expr {
2918 $$ = new AstNode(AST_GT, $1, $4);
2919 SET_AST_NODE_LOC($$, @1, @4);
2920 append_attr($$, $3);
2921 } |
2922 basic_expr '+' attr basic_expr {
2923 $$ = new AstNode(AST_ADD, $1, $4);
2924 SET_AST_NODE_LOC($$, @1, @4);
2925 append_attr($$, $3);
2926 } |
2927 basic_expr '-' attr basic_expr {
2928 $$ = new AstNode(AST_SUB, $1, $4);
2929 SET_AST_NODE_LOC($$, @1, @4);
2930 append_attr($$, $3);
2931 } |
2932 basic_expr '*' attr basic_expr {
2933 $$ = new AstNode(AST_MUL, $1, $4);
2934 SET_AST_NODE_LOC($$, @1, @4);
2935 append_attr($$, $3);
2936 } |
2937 basic_expr '/' attr basic_expr {
2938 $$ = new AstNode(AST_DIV, $1, $4);
2939 SET_AST_NODE_LOC($$, @1, @4);
2940 append_attr($$, $3);
2941 } |
2942 basic_expr '%' attr basic_expr {
2943 $$ = new AstNode(AST_MOD, $1, $4);
2944 SET_AST_NODE_LOC($$, @1, @4);
2945 append_attr($$, $3);
2946 } |
2947 basic_expr OP_POW attr basic_expr {
2948 $$ = new AstNode(AST_POW, $1, $4);
2949 SET_AST_NODE_LOC($$, @1, @4);
2950 append_attr($$, $3);
2951 } |
2952 '+' attr basic_expr %prec UNARY_OPS {
2953 $$ = new AstNode(AST_POS, $3);
2954 SET_AST_NODE_LOC($$, @1, @3);
2955 append_attr($$, $2);
2956 } |
2957 '-' attr basic_expr %prec UNARY_OPS {
2958 $$ = new AstNode(AST_NEG, $3);
2959 SET_AST_NODE_LOC($$, @1, @3);
2960 append_attr($$, $2);
2961 } |
2962 basic_expr OP_LAND attr basic_expr {
2963 $$ = new AstNode(AST_LOGIC_AND, $1, $4);
2964 SET_AST_NODE_LOC($$, @1, @4);
2965 append_attr($$, $3);
2966 } |
2967 basic_expr OP_LOR attr basic_expr {
2968 $$ = new AstNode(AST_LOGIC_OR, $1, $4);
2969 SET_AST_NODE_LOC($$, @1, @4);
2970 append_attr($$, $3);
2971 } |
2972 '!' attr basic_expr %prec UNARY_OPS {
2973 $$ = new AstNode(AST_LOGIC_NOT, $3);
2974 SET_AST_NODE_LOC($$, @1, @3);
2975 append_attr($$, $2);
2976 };
2977
2978 concat_list:
2979 expr {
2980 $$ = new AstNode(AST_CONCAT, $1);
2981 } |
2982 expr ',' concat_list {
2983 $$ = $3;
2984 $$->children.push_back($1);
2985 };
2986
2987 integral_number:
2988 TOK_CONSTVAL { $$ = $1; } |
2989 TOK_UNBASED_UNSIZED_CONSTVAL { $$ = $1; } |
2990 TOK_BASE TOK_BASED_CONSTVAL {
2991 $1->append(*$2);
2992 $$ = $1;
2993 delete $2;
2994 } |
2995 TOK_CONSTVAL TOK_BASE TOK_BASED_CONSTVAL {
2996 $1->append(*$2).append(*$3);
2997 $$ = $1;
2998 delete $2;
2999 delete $3;
3000 };