forked from parallaxsw/OpenSTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerilogParse.yy
527 lines (463 loc) · 11.8 KB
/
VerilogParse.yy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
%{
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2024, Parallax Software, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cstdlib>
#include <string>
#include <iostream>
#include "PortDirection.hh"
#include "verilog/VerilogReaderPvt.hh"
#include "VerilogReader.hh"
int VerilogLex_lex();
#define VerilogParse_lex VerilogLex_lex
// Use yacc generated parser errors.
#define YYERROR_VERBOSE
%}
%union{
int ival;
const char *string;
const char *constant;
const char *attribute_spec_value;
sta::VerilogModule *module;
sta::VerilogStmt *stmt;
sta::VerilogStmtSeq *stmt_seq;
sta::PortDirection *port_type;
sta::VerilogDclArgSeq *dcl_arg_seq;
sta::VerilogDclArg *dcl_arg;
sta::VerilogAssign *assign;
sta::VerilogInst *inst;
sta::VerilogNet *net;
sta::VerilogNetBitSelect *net_bit;
sta::VerilogNetSeq *nets;
sta::VerilogAttributeEntry *attribute_entry;
sta::VerilogAttributeEntrySeq *attribute_seq;
sta::VerilogAttributeStmt *attribute_stmt;
sta::VerilogAttributeStmtSeq *attribute_stmt_seq;
}
%token INT CONSTANT ID STRING MODULE ENDMODULE ASSIGN PARAMETER DEFPARAM
%token WIRE WAND WOR TRI INPUT OUTPUT INOUT SUPPLY1 SUPPLY0 REG
%token ATTRIBUTE_OPEN ATTRIBUTE_CLOSED
%left '-' '+'
%left '*' '/'
%left NEG /* negation--unary minus */
%type <string> ID STRING
%type <ival> WIRE WAND WOR TRI INPUT OUTPUT INOUT SUPPLY1 SUPPLY0
%type <ival> ATTRIBUTE_OPEN ATTRIBUTE_CLOSED
%type <ival> INT parameter_exprs parameter_expr module_begin
%type <constant> CONSTANT
%type <attribute_spec_value> attr_spec_value
%type <port_type> dcl_type port_dcl_type
%type <stmt> stmt declaration instance parameter parameter_dcls parameter_dcl
%type <stmt> defparam param_values param_value port_dcl
%type <stmt_seq> stmts stmt_seq net_assignments continuous_assign port_dcls
%type <assign> net_assignment
%type <dcl_arg> dcl_arg
%type <dcl_arg_seq> dcl_args
%type <net> port net_scalar net_bit_select net_part_select net_assign_lhs
%type <net> net_constant net_expr port_ref port_expr named_pin_net_expr
%type <net> inst_named_pin net_named net_expr_concat
%type <nets> port_list port_refs inst_ordered_pins
%type <nets> inst_named_pins net_exprs inst_pins
%type <attribute_entry> attr_spec
%type <attribute_seq> attr_specs
%type <attribute_stmt> attribute_instance
%type <attribute_stmt_seq> attribute_instance_seq
%start file
%{
%}
%%
file:
modules
;
modules:
// empty
| modules module
;
module_begin:
MODULE { $<ival>$ = sta::verilog_reader->line(); }
{ $$ = $<ival>2; }
;
module:
attribute_instance_seq module_begin ID ';' stmts ENDMODULE
{ sta::verilog_reader->makeModule($3, new sta::VerilogNetSeq, $5, $1, $2);}
| attribute_instance_seq module_begin ID '(' ')' ';' stmts ENDMODULE
{ sta::verilog_reader->makeModule($3, new sta::VerilogNetSeq, $7, $1, $2);}
| attribute_instance_seq module_begin ID '(' port_list ')' ';' stmts ENDMODULE
{ sta::verilog_reader->makeModule($3, $5, $8, $1, $2); }
| attribute_instance_seq module_begin ID '(' port_dcls ')' ';' stmts ENDMODULE
{ sta::verilog_reader->makeModule($3, $5, $8, $1, $2); }
;
port_list:
port
{ $$ = new sta::VerilogNetSeq;
$$->push_back($1);
}
| port_list ',' port
{ $1->push_back($3); }
;
port:
port_expr
| '.' ID '(' ')'
{ $$=sta::verilog_reader->makeNetNamedPortRefScalar($2, NULL);}
| '.' ID '(' port_expr ')'
{ $$=sta::verilog_reader->makeNetNamedPortRefScalar($2, $4);}
;
port_expr:
port_ref
| '{' port_refs '}'
{ $$ = sta::verilog_reader->makeNetConcat($2); } ;
port_refs:
port_ref
{ $$ = new sta::VerilogNetSeq;
$$->push_back($1);
}
| port_refs ',' port_ref
{ $1->push_back($3); }
;
port_ref:
net_scalar
| net_bit_select
| net_part_select
;
port_dcls:
port_dcl
{ $$ = new sta::VerilogStmtSeq;
$$->push_back($1);
}
| port_dcls ',' port_dcl
{ $$ = $1;
$1->push_back($3);
}
| port_dcls ',' dcl_arg
{
sta::VerilogDcl *dcl = dynamic_cast<sta::VerilogDcl*>($1->back());
dcl->appendArg($3);
$$ = $1;
}
;
port_dcl:
attribute_instance_seq port_dcl_type
{ $<ival>$ = sta::verilog_reader->line(); } dcl_arg
{ $$ = sta::verilog_reader->makeDcl($2, $4, $1, $<ival>3); }
| attribute_instance_seq port_dcl_type
{ $<ival>$ = sta::verilog_reader->line(); } '[' INT ':' INT ']' dcl_arg
{ $$ = sta::verilog_reader->makeDclBus($2, $5, $7, $9, $1, $<ival>3); }
;
port_dcl_type:
INPUT { $$ = sta::PortDirection::input(); }
| INPUT WIRE { $$ = sta::PortDirection::input(); }
| INOUT { $$ = sta::PortDirection::bidirect(); }
| INOUT REG { $$ = sta::PortDirection::bidirect(); }
| INOUT WIRE { $$ = sta::PortDirection::bidirect(); }
| OUTPUT { $$ = sta::PortDirection::output(); }
| OUTPUT WIRE { $$ = sta::PortDirection::output(); }
| OUTPUT REG { $$ = sta::PortDirection::output(); }
;
stmts:
// empty
{ $$ = new sta::VerilogStmtSeq; }
| stmts stmt
{ if ($2) $1->push_back($2); }
| stmts stmt_seq
// Append stmt_seq to stmts.
{ sta::VerilogStmtSeq::Iterator iter($2);
while (iter.hasNext())
$1->push_back(iter.next());
delete $2;
}
;
stmt:
parameter
| defparam
| declaration
| instance
| error ';'
{ yyerrok; $$ = NULL; }
;
stmt_seq:
continuous_assign
;
/* Parameters are parsed and ignored. */
parameter:
PARAMETER parameter_dcls ';'
{ $$ = NULL; }
| PARAMETER '[' INT ':' INT ']' parameter_dcls ';'
{ $$ = NULL; }
;
parameter_dcls:
parameter_dcl
{ $$ = NULL; }
| parameter_dcls ',' parameter_dcl
{ $$ = NULL; }
;
parameter_dcl:
ID '=' parameter_expr
{ sta::stringDelete($1);
$$ = NULL;
}
| ID '=' STRING
{ sta::stringDelete($1);
sta::stringDelete($3);
$$ = NULL;
}
;
parameter_expr:
ID
{ sta::stringDelete($1);
$$ = 0;
}
| '`' ID
{ sta::stringDelete($2);
$$ = 0;
}
| CONSTANT
{ sta::stringDelete($1);
$$ = 0;
}
| INT
| '-' parameter_expr %prec NEG
{ $$ = - $2; }
| parameter_expr '+' parameter_expr
{ $$ = $1 + $3; }
| parameter_expr '-' parameter_expr
{ $$ = $1 - $3; }
| parameter_expr '*' parameter_expr
{ $$ = $1 * $3; }
| parameter_expr '/' parameter_expr
{ $$ = $1 / $3; }
| '(' parameter_expr ')'
{ $$ = $2; }
;
defparam:
DEFPARAM param_values ';'
{ $$ = NULL; }
;
param_values:
param_value
{ $$ = NULL; }
| param_values ',' param_value
{ $$ = NULL; }
;
param_value:
ID '=' parameter_expr
{ sta::stringDelete($1);
$$ = NULL;
}
| ID '=' STRING
{ sta::stringDelete($1);
sta::stringDelete($3);
$$ = NULL;
}
;
declaration:
attribute_instance_seq dcl_type { $<ival>$ = sta::verilog_reader->line(); } dcl_args ';'
{ $$ = sta::verilog_reader->makeDcl($2, $4, $1, $<ival>3); }
| attribute_instance_seq dcl_type { $<ival>$ = sta::verilog_reader->line(); }
'[' INT ':' INT ']' dcl_args ';'
{ $$ = sta::verilog_reader->makeDclBus($2, $5, $7, $9, $1,$<ival>3); }
;
dcl_type:
INPUT { $$ = sta::PortDirection::input(); }
| INOUT { $$ = sta::PortDirection::bidirect(); }
| OUTPUT { $$ = sta::PortDirection::output(); }
| SUPPLY0 { $$ = sta::PortDirection::ground(); }
| SUPPLY1 { $$ = sta::PortDirection::power(); }
| TRI { $$ = sta::PortDirection::tristate(); }
| WAND { $$ = sta::PortDirection::internal(); }
| WIRE { $$ = sta::PortDirection::internal(); }
| WOR { $$ = sta::PortDirection::internal(); }
;
dcl_args:
dcl_arg
{ $$ = new sta::VerilogDclArgSeq;
$$->push_back($1);
}
| dcl_args ',' dcl_arg
{ $1->push_back($3);
$$ = $1;
}
;
dcl_arg:
ID
{ $$ = sta::verilog_reader->makeDclArg($1); }
| net_assignment
{ $$ = sta::verilog_reader->makeDclArg($1); }
;
continuous_assign:
ASSIGN net_assignments ';'
{ $$ = $2; }
;
net_assignments:
net_assignment
{ $$ = new sta::VerilogStmtSeq();
$$->push_back($1);
}
| net_assignments ',' net_assignment
{ $1->push_back($3); }
;
net_assignment:
net_assign_lhs { $<ival>$ = sta::verilog_reader->line(); } '=' net_expr
{ $$ = sta::verilog_reader->makeAssign($1, $4, $<ival>2); }
;
net_assign_lhs:
net_named
| net_expr_concat
;
instance:
attribute_instance_seq ID { $<ival>$ = sta::verilog_reader->line(); } ID '(' inst_pins ')' ';'
{ $$ = sta::verilog_reader->makeModuleInst($2, $4, $6, $1, $<ival>3); }
| attribute_instance_seq ID { $<ival>$ = sta::verilog_reader->line(); } parameter_values
ID '(' inst_pins ')' ';'
{ $$ = sta::verilog_reader->makeModuleInst($2, $5, $7, $1, $<ival>3); }
;
parameter_values:
'#' '(' parameter_exprs ')'
;
parameter_exprs:
parameter_expr
| '{' parameter_exprs '}'
{ $$ = $2; }
| parameter_exprs ',' parameter_expr
;
inst_pins:
// empty
{ $$ = NULL; }
| inst_ordered_pins
| inst_named_pins
;
// Positional pin connections.
inst_ordered_pins:
net_expr
{ $$ = new sta::VerilogNetSeq;
$$->push_back($1);
}
| inst_ordered_pins ',' net_expr
{ $1->push_back($3); }
;
// Named pin connections.
inst_named_pins:
inst_named_pin
{ $$ = new sta::VerilogNetSeq;
$$->push_back($1);
}
| inst_named_pins ',' inst_named_pin
{ $1->push_back($3); }
;
// The port reference is split out into cases to special case
// the most frequent case of .port_scalar(net_scalar).
inst_named_pin:
// Scalar port.
'.' ID '(' ')'
{ $$ = sta::verilog_reader->makeNetNamedPortRefScalarNet($2); }
| '.' ID '(' ID ')'
{ $$ = sta::verilog_reader->makeNetNamedPortRefScalarNet($2, $4); }
| '.' ID '(' ID '[' INT ']' ')'
{ $$ = sta::verilog_reader->makeNetNamedPortRefBitSelect($2, $4, $6); }
| '.' ID '(' named_pin_net_expr ')'
{ $$ = sta::verilog_reader->makeNetNamedPortRefScalar($2, $4); }
// Bus port bit select.
| '.' ID '[' INT ']' '(' ')'
{ $$ = sta::verilog_reader->makeNetNamedPortRefBit($2, $4, NULL); }
| '.' ID '[' INT ']' '(' net_expr ')'
{ $$ = sta::verilog_reader->makeNetNamedPortRefBit($2, $4, $7); }
// Bus port part select.
| '.' ID '[' INT ':' INT ']' '(' ')'
{ $$ = sta::verilog_reader->makeNetNamedPortRefPart($2, $4, $6, NULL); }
| '.' ID '[' INT ':' INT ']' '(' net_expr ')'
{ $$ = sta::verilog_reader->makeNetNamedPortRefPart($2, $4, $6, $9); }
;
named_pin_net_expr:
net_part_select
| net_constant
| net_expr_concat
;
net_named:
net_scalar
| net_bit_select
| net_part_select
;
net_scalar:
ID
{ $$ = sta::verilog_reader->makeNetScalar($1); }
;
net_bit_select:
ID '[' INT ']'
{ $$ = sta::verilog_reader->makeNetBitSelect($1, $3); }
;
net_part_select:
ID '[' INT ':' INT ']'
{ $$ = sta::verilog_reader->makeNetPartSelect($1, $3, $5); }
;
net_constant:
CONSTANT
{ $$ = sta::verilog_reader->makeNetConstant($1); }
;
net_expr_concat:
'{' net_exprs '}'
{ $$ = sta::verilog_reader->makeNetConcat($2); }
;
net_exprs:
net_expr
{ $$ = new sta::VerilogNetSeq;
$$->push_back($1);
}
| net_exprs ',' net_expr
{ $$->push_back($3); }
;
net_expr:
net_scalar
| net_bit_select
| net_part_select
| net_constant
| net_expr_concat
;
attribute_instance_seq:
// empty
{ $$ = new sta::VerilogAttributeStmtSeq; }
| attribute_instance_seq attribute_instance
{ if ($2) $1->push_back($2); }
;
attribute_instance:
ATTRIBUTE_OPEN attr_specs ATTRIBUTE_CLOSED
{ $$ = new sta::VerilogAttributeStmt($2); }
;
attr_specs:
attr_spec
{ $$ = new sta::VerilogAttributeEntrySeq;
$$->push_back($1);
}
| attr_specs ',' attr_spec
{ $$->push_back($3); }
;
attr_spec:
ID
{ $$ = new sta::VerilogAttributeEntry($1, "1");
delete[] $1;
}
| ID '=' attr_spec_value
{ $$ = new sta::VerilogAttributeEntry($1, $3);
delete[] $1;
delete[] $3;
}
;
attr_spec_value:
CONSTANT
{ $$ = $1; }
| STRING
{ $$ = $1; }
| INT
{ $$ = sta::stringCopy(std::to_string($1).c_str()); }
;
%%