/
vlog.g
1936 lines (1716 loc) · 58 KB
/
vlog.g
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
header {
//The MIT License
//
//Copyright (c) 2006-2010 Karl W. Pfalzer
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package parser.v2k;
import parser.v2k.tree.*;
}
class VlogParser extends Parser;
options {
k=2;
}
//A.1.2 Verilog source text
source_text
: (description)*
EOF
;
description
{ ModuleDeclaration mdecl=null;}
: mdecl=module_declaration {getTree().moduleDeclaration(mdecl);}
//TODO| udp_declaration
//TODO| config_declaration
;
module_declaration returns [ModuleDeclaration mdecl]
{ mdecl=null; ModuleIdent mid=null; ListOf<ParameterDeclaration> lopd=null;
ListOf<PortDeclaration> lports=null;
NonPortModuleItem npmi=null; ListOf<Port> lop=null;
ModuleItem mi=null;
boolean isAnsi=false;
}
: (attribute_instance)*
module_keyword mid=module_identifier
{getTree().addSymbol(mid);}
(lopd=module_parameter_port_list)?
{mdecl = getTree().moduleDeclaration(mid,lopd);}
(
((list_of_port_declarations)?)=>
(lports=list_of_port_declarations)? SEMI {isAnsi=true;}
{getTree().moduleDeclaration(mdecl,isAnsi,lports);}
(npmi=non_port_module_item
{getTree().moduleDeclaration(mdecl,npmi);}
)*
| lop=list_of_ports SEMI
{getTree().moduleDeclaration(mdecl,lop);}
(mi=module_item {getTree().moduleDeclaration(mdecl,mi);})*
)
"endmodule"
{ getTree().popScope();}
;
module_keyword
: "module"
| "macromodule"
;
//A.1.3 Module parameters and ports
module_parameter_port_list returns [ListOf<ParameterDeclaration> lopd]
{ lopd=null;}
: POUND LPAREN
lopd=parameter_declarations
RPAREN
;
parameter_declarations returns [ListOf<ParameterDeclaration> lopd]
{lopd=null; ListOf<ParameterDeclaration> pd=null;}
: pd=parameter_declaration {lopd=getTree().addToList(lopd,pd);}
(COMMA pd=parameter_declaration {lopd=getTree().addToList(lopd,pd);})*
;
/**
list_of_ports
: (LPAREN port)=> LPAREN port (COMMA port)* RPAREN
| LPAREN RPAREN
;
**/
list_of_ports returns [ListOf<Port> lop]
{ lop=null; Port port=null;}
: LPAREN (options {greedy=true;}:
port=port {lop=getTree().addToList(lop,port);}
(COMMA port=port {lop=getTree().addToList(lop,port);})*
)? RPAREN
;
list_of_port_declarations returns [ListOf<PortDeclaration> lopd]
{ lopd=null; ListOf<PortDeclaration> pd=null;}
: LPAREN (lopd=port_declaration[true]
(COMMA pd=port_declaration[true] {lopd=getTree().addToList(lopd,pd);} )*
)? RPAREN
;
/**Dont support .id(expr) style
port returns [Port port]
{ port=null; PortExpression pexpr=null; PortIdent pid=null;}
: ( (pexpr=port_expression)?
| DOT pid=port_identifier LPAREN (pexpr=port_expression)? RPAREN
)
{ port = getTree().port(pid,pexpr);}
;
**/
port returns [Port port]
{ port=null; PortIdent pid=null;}
: (pid=port_identifier)?
{ port = getTree().port(pid);}
;
port_expression returns [PortExpression pexpr]
{ pexpr=null; PortReference pref=null;}
//NOTE: the LRM has 0 or >=1. However, usage always has port_expression?
// so we"ll let that cover 0.
: pref=port_reference {pexpr = getTree().portExpression(null,pref);}
| LCURLY pref=port_reference {pexpr = getTree().portExpression(null,pref);}
(COMMA pref=port_reference
{getTree().portExpression(pexpr,pref);})* RCURLY
;
port_reference returns [PortReference pref]
{ pref=null; PortIdent pid=null; ConstRangeExpression rexpr=null;}
: pid=port_identifier (LBRACK (rexpr=constant_range_expression)? RBRACK)?
{pref = getTree().portReference(pid,rexpr);}
;
//isOnlyDefn==true when defined through list_of_port_declarations
port_declaration[boolean isOnlyDefn] returns [ListOf<PortDeclaration> pd]
{ pd=null;}
: (attribute_instance)*
( pd=inout_declaration[isOnlyDefn]
| pd=input_declaration[isOnlyDefn]
| pd=output_declaration[isOnlyDefn]
)
;
module_item returns [ModuleItem mi]
{ mi=null; ListOf<PortDeclaration> pd=null; NonPortModuleItem npi=null;}
: (port_declaration[false])=> pd=port_declaration[false] SEMI
{mi = getTree().moduleItem(pd);}
| npi=non_port_module_item
{mi = getTree().moduleItem(npi);}
;
module_or_generate_item returns [ModuleOrGenerateItem mi]
{ mi=null; Object o1=null;}
: (attribute_instance)*
( o1=module_or_generate_item_declaration
{mi = getTree().moduleOrGenItem((ModuleOrGenerateItemDecl)o1);}
| o1=local_parameter_declaration SEMI
{mi = getTree().moduleOrGenItemLocalParam((ListOf<LocalParameterDecl>)o1);}
| o1=parameter_override
{mi = getTree().moduleOrGenItemParam((ListOf<DefparamAssign>)o1);}
| o1=continuous_assign
{mi = getTree().moduleOrGenItem((ContinuousAssign)o1);}
//TODO| gate_instantiation
//TODO| udp_instantiation
| o1=module_instantiation
{mi = getTree().moduleOrGenItemInst((ListOf<ModuleInstance>)o1);}
| o1=initial_construct
{mi = getTree().moduleOrGenItem((InitialConstruct)o1);}
| o1=always_construct
{mi = getTree().moduleOrGenItem((AlwaysConstruct)o1);}
| o1=loop_generate_construct
{mi = getTree().moduleOrGenItem((LoopGenerateConstruct)o1);}
| o1=conditional_generate_construct
{mi = getTree().moduleOrGenItem((ConditionalGenerateConstruct)o1);}
)
;
module_or_generate_item_declaration returns [ModuleOrGenerateItemDecl mogd]
{ mogd=null; Object o1=null;}
: o1=net_declaration
{mogd = getTree().moduleOrGenItemDeclNet((ListOf<NetDeclaration>)o1);}
| o1=reg_declaration
{mogd = getTree().moduleOrGenItemDeclReg((ListOf<RegDecl>)o1);}
| o1=integer_declaration
{mogd = getTree().moduleOrGenItemDeclInt((ListOf<IntegerDecl>)o1);}
| o1=real_declaration
{mogd = getTree().moduleOrGenItemDeclReal((ListOf<RealDecl>)o1);}
| o1=time_declaration
{mogd = getTree().moduleOrGenItemDeclTime((ListOf<TimeDecl>)o1);}
| o1=realtime_declaration
{mogd = getTree().moduleOrGenItemDeclRealtime((ListOf<RealtimeDecl>)o1);}
| o1=event_declaration
{mogd = getTree().moduleOrGenItemDeclEvent((ListOf<EventDecl>)o1);}
| o1=genvar_declaration
{mogd = getTree().moduleOrGenItemDeclGenvar((ListOf<GenvarIdent>)o1);}
| o1=task_declaration
{mogd = getTree().moduleOrGenItemDecl((TaskDeclaration)o1);}
| o1=function_declaration
{mogd = getTree().moduleOrGenItemDecl((FuncDecl)o1);}
;
non_port_module_item returns [NonPortModuleItem npmi]
{ npmi=null; Object o1=null;}
: (module_or_generate_item)=> o1=module_or_generate_item
{npmi = getTree().nonPortModuleItem((ModuleOrGenerateItem)o1);}
| o1=generate_region
{npmi = getTree().nonPortModuleItem((ListOf<ModuleOrGenerateItem>)o1);}
| (attribute_instance)*
( o1=parameter_declaration SEMI
{npmi = getTree().nonPortModuleItemParms((ListOf<ParameterDeclaration>)o1);}
| o1=specparam_declaration
{npmi = getTree().nonPortModuleItem((SpecparamDecl)o1);}
)
;
parameter_override returns [ListOf<DefparamAssign> lopa]
{lopa=null;}
: "defparam" lopa=list_of_defparam_assignments SEMI
;
//A.1.5 Configuration source text
//TODO
//A.2 Declarations
//A.2.1 Declaration types
//A.2.1.1 Module parameter declarations
local_parameter_declaration returns [ListOf<LocalParameterDecl> lpd]
{ lpd=null; Location isSigned=null; Range rng=null;
ListOf<ParamAssign> lopa=null; ParameterType pt=null;
}
: ( "localparam" ("signed" {isSigned=Location.getCurrent();})? (rng=range)?
lopa=list_of_param_assignments
| "localparam" pt=parameter_type lopa=list_of_param_assignments
)
{ lpd = getTree().localParameterDecl(isSigned,rng,pt,lopa);}
;
parameter_declaration returns [ListOf<ParameterDeclaration> pd]
{ pd=null; Location isSigned=null; Range rng=null;
ListOf<ParamAssign> lopa=null; ParameterType pt=null;
}
: ( "parameter" ("signed" {isSigned=Location.getCurrent();})? (rng=range)?
lopa=list_of_param_assignments
| "parameter" pt=parameter_type lopa=list_of_param_assignments
)
{ pd = getTree().parameterDecl(isSigned,rng,pt,lopa);}
;
specparam_declaration returns [SpecparamDecl spd]
{ spd=null; Range rng=null; ListOf<SpecparamAssign> spa=null; }
: "specparam" (rng=range)? spa=list_of_specparam_assignments SEMI
{ spd = getTree().specparamDecl(rng,spa);}
;
parameter_type returns [ParameterType pt]
{ pt = getTree().parameterType(LA(1));}
: "integer"
| "real"
| "realtime"
| "time"
;
//A.2.1.2 Port declarations
inout_declaration[boolean isOnlyDefn] returns [ListOf<PortDeclaration> pd]
{ pd=null; int dir=LA(1); NetType type=null; Range rng=null;
Location isSigned=null; PortDirection pdir=null;
ListOf<? extends PortIdent> ids = null;
}
: "inout" {pdir=new PortDirection(dir);}
(type=net_type)? ("signed" {isSigned=Location.getCurrent();})?
(rng=range)? ids=list_of_port_identifiers
{ pd = getTree().portDecl(isOnlyDefn,pdir,type,rng,null,null,isSigned,ids);}
;
input_declaration[boolean isOnlyDefn] returns [ListOf<PortDeclaration> pd]
{ pd=null; int dir=LA(1); NetType type=null; Range rng=null;
Location isSigned=null; PortDirection pdir=null;
ListOf<? extends PortIdent> ids = null;
}
: "input" {pdir=new PortDirection(dir);}
(type=net_type)? ("signed" {isSigned=Location.getCurrent();})?
(rng=range)? ids=list_of_port_identifiers
{ pd = getTree().portDecl(isOnlyDefn,pdir,type,rng,null,null,isSigned,ids);}
;
output_declaration[boolean isOnlyDefn] returns [ListOf<PortDeclaration> pd]
{ pd=null; int dir=LA(1); NetType type=null; Range rng=null;
Location isSigned=null, isReg=null; PortDirection pdir=null;
ListOf<? extends PortIdent> ids = null; OutputVarType ovt=null;
}
: ( ("output" output_variable_type)=>
"output" {pdir=new PortDirection(dir);} ovt=output_variable_type
ids=list_of_variable_port_identifiers
| ("output" ("reg")? ("signed")? (range)?
list_of_variable_port_identifiers)=>
"output" {pdir=new PortDirection(dir);}
("reg" {isReg=Location.getCurrent();})? ("signed" {isSigned=Location.getCurrent();})?
(rng=range)? ids=list_of_variable_port_identifiers
| "output" {pdir=new PortDirection(dir);}
(type=net_type)? ("signed" {isSigned=Location.getCurrent();})? (rng=range)?
ids=list_of_port_identifiers
)
{ pd = getTree().portDecl(isOnlyDefn,pdir,type,rng,ovt,isReg,isSigned,ids);}
;
//A.2.1.3 Type declarations
event_declaration returns [ListOf<EventDecl> ed]
{ ed=null; ListOf<EventIdentifiers> loei=null;}
: "event" loei=list_of_event_identifiers SEMI
{ed = getTree().eventDecl(loei);}
;
integer_declaration returns [ListOf<IntegerDecl> intd]
{ intd=null; ListOf<VariableType> lovt=null;}
: "integer" lovt=list_of_variable_identifiers SEMI
{ intd = getTree().integerDecl(lovt);}
;
//NOTE: greatly simplified
net_declaration returns [ListOf<NetDeclaration> nd]
{ nd=null; NetType type=null; Location isSigned=null; Range rng=null;
Delay3 del3=null; ListOf<NetIdentifiers> nets=null;
ListOf<NetDeclAssign> decls=null;
}
: type=net_type ("vectored" | "scalared")? ("signed" {isSigned=Location.getCurrent();})?
(rng=range)? (del3=delay3)?
( nets=list_of_net_identifiers
| decls=list_of_net_decl_assignments
)
SEMI
{nd=getTree().netDeclaration(type,isSigned,rng,del3,nets,decls);}
;
real_declaration returns [ListOf<RealDecl> rd]
{ rd=null; ListOf<RealType> lort=null;}
: "real" lort=list_of_real_identifiers SEMI
{rd=getTree().realDecl(lort);}
;
realtime_declaration returns [ListOf<RealtimeDecl> rtd]
{ rtd=null; ListOf<RealType> lort=null;}
: "realtime" lort=list_of_real_identifiers SEMI
{rtd=getTree().realtimeDecl(lort);}
;
reg_declaration returns [ListOf<RegDecl> rd]
{ rd=null; Location isSigned=null, loc=null; Range rng=null;
ListOf<VariableType> vid=null;}
: "reg" {loc=Location.getCurrent();} ("signed" {isSigned=Location.getCurrent();})? (rng=range)?
vid=list_of_variable_identifiers SEMI
{rd=getTree().regDecl(loc,isSigned,rng,vid);}
;
time_declaration returns [ListOf<TimeDecl> td]
{ td=null; ListOf<VariableType> vid=null; }
: "time" vid=list_of_variable_identifiers SEMI
{td = getTree().timeDecl(vid);}
;
//A.2.2 Declaration data types
//A.2.2.1 Net and variable types
net_type returns [NetType nt]
{ nt=null; int type=LA(1); nt = getTree().netType(type);}
: "supply0"
| "supply1"
| "tri"
| "triand"
| "trior"
| "tri0"
| "tri1"
| "uwire"
| "wire"
| "wand"
| "wor"
;
output_variable_type returns [OutputVarType ovt]
{ ovt=null; int type=LA(1); ovt = getTree().outputVarType(type);}
: "integer"
| "time"
;
real_type returns [RealType rt]
{ rt=null; RealIdent id=null;
Dimension dim=null; ListOf<Dimension> lod=null;
ConstExpression expr=null;}
: ( id=real_identifier
(dim=dimension {lod = getTree().addToList(lod, dim);} )*
| id=real_identifier ASSIGN expr=constant_expression
)
{ rt = getTree().realType(id,lod,expr);}
;
variable_type returns [VariableType vt]
{ vt=null; VariableIdent id=null;
Dimension dim=null; ListOf<Dimension> lod=null;
ConstExpression expr=null;}
: ( id=variable_identifier
(dim=dimension {lod = getTree().addToList(lod, dim);} )*
| id=variable_identifier ASSIGN expr=constant_expression
)
{ vt = getTree().varType(id,lod,expr);}
;
//SKIP: A.2.2.2 Strengths
//A.2.2.3 Delays
delay3 returns [Delay3 d3]
{ d3=null; ListOf<DelayValue> lo=null; DelayValue dv=null;}
: ( POUND dv=delay_value {lo = getTree().addToList(lo, dv);}
| POUND LPAREN (dv=delay_value {lo = getTree().addToList(lo, dv);}
(COMMA dv=delay_value {getTree().addToList(lo, dv);} )*)
RPAREN
)
{d3 = getTree().delay3(lo);}
;
delay2 returns [Delay2 d2]
{ d2=null; Delay3 d3=null; }
: d3=delay3 {d2 = getTree().delay2(d3); }
;
delay_value returns [DelayValue dv]
{ dv=null; Vnumber n=null; Ident id=null;}
: n=number {dv = getTree().delayValue(n);}
| id=identifier {dv = getTree().delayValue(id);}
;
//A.2.3 Declaration lists
list_of_defparam_assignments returns [ListOf<DefparamAssign> loda]
{ loda=null; DefparamAssign da=null;}
: da=defparam_assignment {loda=getTree().addToList(loda,da);}
(COMMA da=defparam_assignment {loda=getTree().addToList(loda,da);} )*
;
list_of_event_identifiers returns [ListOf<EventIdentifiers> loei]
{ loei=null; EventIdentifiers ei=null;}
: ei=event_identifiers {loei=getTree().addToList(loei,ei);}
(COMMA ei=event_identifiers {loei=getTree().addToList(loei,ei);} )*
;
event_identifiers returns [EventIdentifiers eis]
{ eis=null; EventIdent ei=null; Dimension dim=null;}
: ei=event_identifier {eis=getTree().eventIdentifiers(null,ei,null);}
(dim=dimension {eis=getTree().eventIdentifiers(eis,null,dim);} )*
;
list_of_net_decl_assignments returns [ListOf<NetDeclAssign> loda]
{ loda=null; NetDeclAssign nda=null;}
: nda=net_decl_assignment {loda=getTree().addToList(loda,nda);}
(COMMA nda=net_decl_assignment {loda=getTree().addToList(loda,nda);} )*
;
list_of_net_identifiers returns [ListOf<NetIdentifiers> loni]
{ loni=null; NetIdentifiers ni=null;}
: ni=net_identifiers {loni=getTree().addToList(loni,ni);}
(COMMA ni=net_identifiers {loni=getTree().addToList(loni,ni);} )*
;
net_identifiers returns [NetIdentifiers nis]
{ nis=null; NetIdent ni=null; Dimension dim=null;}
: ni=net_identifier {nis=getTree().netIdentifiers(null,ni,null);}
(dim=dimension {nis=getTree().netIdentifiers(nis,null,dim);} )*
;
list_of_param_assignments returns [ListOf<ParamAssign> lopa]
{ lopa=null; ParamAssign pa=null;}
: pa=param_assignment {lopa=getTree().addToList(lopa,pa);}
(options{greedy=true;}:
COMMA pa=param_assignment {lopa=getTree().addToList(lopa,pa);})*
;
list_of_port_identifiers returns [ListOf<PortIdent> lop]
{ lop=null; PortIdent pi=null;}
: pi=port_identifier {lop = getTree().addToList(lop,pi);}
(options{greedy=true;}: COMMA pi=port_identifier
{lop = getTree().addToList(lop,pi);}
)*
;
list_of_real_identifiers returns [ListOf<RealType> lort]
{ lort=null; RealType rt=null;}
: rt=real_type {lort=getTree().addToList(lort,rt);}
(COMMA rt=real_type {lort=getTree().addToList(lort,rt);} )*
;
list_of_specparam_assignments returns [ListOf<SpecparamAssign> spa]
{ spa=null; SpecparamAssign sa=null; }
: sa=specparam_assignment {spa=getTree().addToList(spa,sa);}
(COMMA sa=specparam_assignment {spa=getTree().addToList(spa,sa);} )*
;
list_of_variable_identifiers returns [ListOf<VariableType> lovt]
{ lovt=null; VariableType vt=null; }
: vt=variable_type {lovt = getTree().addToList(lovt,vt);}
(COMMA vt=variable_type {lovt = getTree().addToList(lovt,vt);} )*
;
list_of_variable_port_identifiers returns [ListOf<PortIdents> lopi]
{ lopi=null; PortIdents pis=null;}
: pis=port_identifiers {lopi=getTree().addToList(lopi,pis);}
(options{greedy=true;}: COMMA
pis=port_identifiers {lopi=getTree().addToList(lopi,pis);} )*
;
port_identifiers returns [PortIdents pis]
{ pis=null; PortIdent id=null; ConstExpression expr=null;}
: id=port_identifier (ASSIGN expr=constant_expression)?
{pis = getTree().portIdents(id, expr);}
;
//A.2.4 Declaration assignments
defparam_assignment returns [DefparamAssign dpa]
{ dpa=null; HierParameterIdent hpi=null; ConstMinTypMaxExpression mtm=null;}
: hpi=hierarchical_parameter_identifier ASSIGN
mtm=constant_mintypmax_expression
{dpa = getTree().defparamAssign(hpi,mtm);}
;
net_decl_assignment returns [NetDeclAssign nda]
{ nda=null; NetIdent id=null; Expression exp=null;}
: id=net_identifier ASSIGN exp=expression
{nda = getTree().netDeclAssign(id,exp);}
;
param_assignment returns [ParamAssign pa]
{ pa=null; ParameterIdent id=null; ConstMinTypMaxExpression mtm=null;}
: id=parameter_identifier ASSIGN mtm=constant_mintypmax_expression
{pa = getTree().paramAssign(id,mtm);}
;
specparam_assignment returns [SpecparamAssign spa]
{ spa=null; SpecparamIdent id=null; ConstMinTypMaxExpression mtm=null;}
: id=specparam_identifier ASSIGN mtm=constant_mintypmax_expression
{spa = getTree().specparamAssign(id,mtm);}
//TODO| pulse_control_specparam
;
//A.2.5 Declaration ranges
//NOTE: dimension usage appears as (dimension)?* so we"ll skip the optional here
dimension returns [Dimension dim]
{ dim=null; ConstExpression msb=null, lsb=null;}
: LBRACK msb=dimension_constant_expression
COLON lsb=dimension_constant_expression RBRACK
{dim = getTree().dimension(msb,lsb);}
;
//NOTE: range usage appears as (range)?* so we"ll skip the optional here
range returns [Range rng]
{ rng=null; ConstExpression msb=null, lsb=null;}
: LBRACK msb=msb_constant_expression COLON lsb=lsb_constant_expression RBRACK
{rng = getTree().range(msb,lsb);}
;
//A.2.6 Function declarations
function_declaration returns [FuncDecl fd]
{fd=null; boolean isAuto=false; FuncType ft=null; FuncIdent id=null;
FuncItemDecl fid=null; ListOf<FuncItemDecl> lofid=null;
ListOf<TfPortDeclaration> lop=null;
BlockItemDecl bid=null; ListOf<BlockItemDecl> lobid=null;
FuncStatement stmt=null;
}
: "function" ("automatic" {isAuto=true;})?
(ft=function_range_or_type)? id=function_identifier
{getTree().addSymbol(id);}
( SEMI (fid=function_item_declaration
{lofid=getTree().addToList(lofid,fid);})+
| LPAREN lop=function_port_list RPAREN SEMI
(bid=block_item_declaration
{lobid=getTree().addToList(lobid,bid);}
)*
)
stmt=function_statement "endfunction"
{ fd = getTree().funcDecl(isAuto,ft,id,lofid,lop,lobid,stmt);
getTree().popScope();
}
;
function_item_declaration returns [FuncItemDecl fid]
{ fid=null; TfPortDeclaration tpd=null; BlockItemDecl bid=null;}
: ( ((attribute_instance)* tf_input_declaration)=>
(attribute_instance)* tpd=tf_input_declaration SEMI
| bid=block_item_declaration
)
{ fid = getTree().funcItemDecl(tpd,bid);}
;
function_port_list returns [ListOf<TfPortDeclaration> lop]
{ lop=null; TfPortDeclaration tpd=null;}
: (attribute_instance)* tpd=tf_input_declaration
{lop = getTree().addToList(lop,tpd);}
(COMMA (attribute_instance)* tpd=tf_input_declaration
{getTree().addToList(lop,tpd);}
)*
;
function_range_or_type returns [FuncType ft]
{ ft=null; int tok=LA(1); Range rng=null;}
: ( ("signed")? {tok=-1;} (rng=range)?
| "integer"
| "real"
| "realtime"
| "time"
)
{ft = getTree().funcType(tok, rng);}
;
//A.2.7 Task declarations
task_declaration returns [TaskDeclaration td]
{ td=null; TaskIdent id=null; boolean isAuto=false;
TaskItemDecl tid=null; BlockItemDecl bid=null;
ListOf<TfPortDeclaration> lop=null; Statement st=null;
}
: "task" ("automatic" {isAuto=true;})? id=task_identifier
{ getTree().addSymbol(id);
td = getTree().taskDecl(isAuto, id);
}
( SEMI
(options {greedy=true;}: tid=task_item_declaration
{getTree().taskDecl(td, tid);}
)*
| LPAREN (lop=task_port_list {getTree().taskDecl(td,lop);})?
RPAREN SEMI
(options {greedy=true;}: bid=block_item_declaration
{getTree().taskDecl(td, bid);}
)*
)
st=statement_or_null
{getTree().taskDecl(td, st);}
"endtask"
{getTree().popScope();}
;
task_item_declaration returns [TaskItemDecl tid]
{ tid=null; BlockItemDecl bid=null; TfPortDeclaration tpd=null;}
: (block_item_declaration)=> bid=block_item_declaration
{tid = getTree().taskItemDecl(bid,null);}
| (attribute_instance)*
( tpd=tf_input_declaration SEMI
| tpd=tf_output_declaration SEMI
| tpd=tf_inout_declaration SEMI
)
{tid = getTree().taskItemDecl(null,tpd);}
;
task_port_list returns [ListOf<TfPortDeclaration> lop]
{ lop=null; TfPortDeclaration tpd=null;}
: tpd=task_port_item {lop = getTree().addToList(lop,tpd);}
(COMMA tpd=task_port_item {lop = getTree().addToList(lop,tpd);} )*
;
task_port_item returns [TfPortDeclaration tpd]
{ tpd=null;}
: (attribute_instance)*
( tpd=tf_input_declaration
| tpd=tf_output_declaration
| tpd=tf_inout_declaration
)
;
tf_inout_declaration returns [TfPortDeclaration tfp]
{ tfp=null; int dir=LA(1); TaskPortType tpt=null; ListOf<PortIdent> lop=null;
PortDirection pdir=null;}
: "inout" {pdir=new PortDirection(dir);}
tpt=task_port_type lop=list_of_port_identifiers
{tfp = getTree().tfPortDecl(pdir, tpt, lop);}
;
tf_input_declaration returns [TfPortDeclaration tfp]
{ tfp=null; int dir=LA(1); TaskPortType tpt=null; ListOf<PortIdent> lop=null;
PortDirection pdir=null;}
: "input" {pdir=new PortDirection(dir);}
tpt=task_port_type lop=list_of_port_identifiers
{tfp = getTree().tfPortDecl(pdir, tpt, lop);}
;
tf_output_declaration returns [TfPortDeclaration tfp]
{ tfp=null; int dir=LA(1); TaskPortType tpt=null; ListOf<PortIdent> lop=null;
PortDirection pdir=null;}
: "output" {pdir=new PortDirection(dir);}
tpt=task_port_type lop=list_of_port_identifiers
{tfp = getTree().tfPortDecl(pdir, tpt, lop);}
;
task_port_type returns [TaskPortType tpt]
{ tpt=null; int la=LA(1); Location isReg=null;
Location isSigned=null; Range rng=null;
}
: ( "integer"
| "real"
| "realtime"
| "time"
) {tpt = getTree().taskPortType(la);}
| ("reg" {isReg=Location.getCurrent();})? ("signed" {isSigned=Location.getCurrent();})? (rng=range)?
{tpt = getTree().taskPortType(isReg, isSigned, rng);}
;
//A.2.8 Block item declarations
block_item_declaration returns [BlockItemDecl bid] //TODO
{ bid=null; int la=LA(1); Location isSigned=null; Range rng=null;
ListOf<BlockVariableType> lov=null; ListOf<BlockRealType> lor=null;
ListOf<EventDecl> ed=null; ListOf<? extends ParameterDeclaration> pd=null;
}
: (attribute_instance)*
( "reg" ("signed" {isSigned=Location.getCurrent();})?
(rng=range)? lov=list_of_block_variable_identifiers SEMI
{bid = getTree().blockItemDecl(isSigned, rng, lov);}
| "integer" lov=list_of_block_variable_identifiers SEMI
{bid = getTree().blockItemDecl(la, lov);}
| "time" lov=list_of_block_variable_identifiers SEMI
{bid = getTree().blockItemDecl(la, lov);}
| "real" lor=list_of_block_real_identifiers SEMI
{bid = getTree().blockItemDecl(la, lor, true);}
| "realtime" lor=list_of_block_real_identifiers SEMI
{bid = getTree().blockItemDecl(la, lor, true);}
| ed=event_declaration
{bid = getTree().blockItemDeclEvent(ed);}
| pd=local_parameter_declaration SEMI
{bid = getTree().blockItemDecl(pd);}
| pd=parameter_declaration SEMI
{bid = getTree().blockItemDecl(pd);}
)
;
list_of_block_variable_identifiers returns [ListOf<BlockVariableType> lob]
{ lob=null; BlockVariableType bvt=null;}
: bvt=block_variable_type {lob = getTree().addToList(lob,bvt);}
(COMMA bvt=block_variable_type {lob = getTree().addToList(lob,bvt);} )*
;
list_of_block_real_identifiers returns [ListOf<BlockRealType> lob]
{ lob=null; BlockRealType brt=null;}
: brt=block_real_type {lob = getTree().addToList(lob,brt);}
(COMMA brt=block_real_type {lob = getTree().addToList(lob,brt);} )*
;
block_variable_type returns [BlockVariableType bvt]
{ bvt=null; VariableIdent id=null; Dimension dim=null;
ListOf<Dimension> lod=null;
}
: id=variable_identifier (dim=dimension {lod=getTree().addToList(lod,dim);} )*
{bvt = getTree().blockVariableType(id,lod);}
;
block_real_type returns [BlockRealType brt]
{ brt=null; RealIdent id=null; Dimension dim=null; ListOf<Dimension> lod=null;}
: id=real_identifier (dim=dimension {lod=getTree().addToList(lod,dim);} )*
{brt = getTree().blockRealType(id,lod);}
;
//SKIP altogether: A.3 Primitive instances
//A.4 Module instantiation and generate construct
//A.4.1 Module instantiation
module_instantiation returns [ListOf<ModuleInstance> lom]
{ lom=null; Ident refNm = null; ParameterValueAssignment pva=null;
ModuleInstance mi=null;
}
: refNm=module_identifier
//Deal w/ ambiguity of #delay and #(delay,...) by catching latter
//in pva.
//TODO: throw out delays for now.
//TODO: does not catch case of #(delay,...) #(params)
( (POUND delay_value)=> POUND delay_value
| pva=parameter_value_assignment //also catches delay3
)?
mi=module_instance
{lom = getTree().moduleInstantiation(lom,refNm,pva,mi);}
(COMMA mi=module_instance
{lom = getTree().moduleInstantiation(lom,refNm,pva,mi);}
)*
SEMI
;
parameter_value_assignment returns [ParameterValueAssignment pva]
{ pva=null; ListOf<ParameterAssignment> lopa=null;}
: POUND LPAREN lopa=list_of_parameter_assignments RPAREN
{pva = getTree().parameterValueAssignment(lopa);}
;
list_of_parameter_assignments returns [ListOf<ParameterAssignment> lopa]
{ lopa=null; Expression exp=null; NamedParamAssignment npa=null;}
: exp=ordered_parameter_assignment
{lopa = getTree().listOfParamAssigns(lopa,exp,null);}
(COMMA exp=ordered_parameter_assignment
{getTree().listOfParamAssigns(lopa,exp,null);}
)*
| npa=named_parameter_assignment
{lopa = getTree().listOfParamAssigns(lopa,null,npa);}
(COMMA npa=named_parameter_assignment
{getTree().listOfParamAssigns(lopa,null,npa);}
)*
;
ordered_parameter_assignment returns [Expression exp]
{ exp=null;}
: exp=expression
;
named_parameter_assignment returns [NamedParamAssignment npa]
{ npa=null; ParameterIdent id=null; MinTypMaxExpression exp=null;}
: DOT id=parameter_identifier LPAREN (exp=mintypmax_expression)? RPAREN
{npa = getTree().namedParameterAssignment(id,exp);}
;
module_instance returns [ModuleInstance inst]
{ inst=null; ListOf<PortConnection> lopc=null;}
: {inst = getTree().moduleInstance();}
name_of_module_instance[inst] LPAREN
(options{greedy=true;}: lopc=list_of_port_connections)? RPAREN
{getTree().moduleInstance(inst,lopc);}
;
name_of_module_instance [ModuleInstance inst]
{ ModuleInstanceIdent nm=null; Range rng=null;}
: (nm=module_instance_identifier (rng=range)?)?
{getTree().moduleInstance(inst, nm, rng);}
;
list_of_port_connections returns [ListOf<PortConnection> lopc]
{ lopc=null; Expression exp=null; NamedPortConnection npc=null;}
: (ordered_port_connection)=>
exp=ordered_port_connection
{lopc = getTree().listOfPortConnections(lopc, exp, null);}
(COMMA exp=ordered_port_connection
{lopc = getTree().listOfPortConnections(lopc, exp, null);}
)*
| npc=named_port_connection
{lopc = getTree().listOfPortConnections(lopc, null, npc);}
(COMMA npc=named_port_connection
{lopc = getTree().listOfPortConnections(lopc, null, npc);}
)*
;
ordered_port_connection returns [Expression exp]
{ exp=null;}
: (attribute_instance)* (exp=expression)?
;
named_port_connection returns [NamedPortConnection npc]
{ npc=null; PortIdent pi=null; Expression exp=null;}
: (attribute_instance)* DOT pi=port_identifier
(LPAREN (exp=expression)? RPAREN)?
{npc = getTree().namedPortConnection(pi, exp);}
;
//A.4.2 Generate construct
generate_region returns [ListOf<ModuleOrGenerateItem> loi]
{ loi=null; ModuleOrGenerateItem mi=null; }
: "generate"
//TODO: was: (mi=module_or_generate_item {loi = getTree().addToList(loi, mi);})*
(generate_block)* //TODO: hoist return returns [GenerateBlock gb]
"endgenerate"
;
genvar_declaration returns [ListOf<GenvarIdent> loi]
{ loi=null;}
: "genvar" loi=list_of_genvar_identifiers SEMI
;
list_of_genvar_identifiers returns [ListOf<GenvarIdent> loi]
{ loi=null; GenvarIdent id=null;}
: id=genvar_identifier {loi = getTree().addToList(loi, id);}
(COMMA id=genvar_identifier {getTree().addToList(loi, id);} )*
;
loop_generate_construct returns [LoopGenerateConstruct lgc]
{ lgc=null; GenvarInit init=null; GenvarExpression test=null;
GenvarIteration iter=null; GenerateBlock blk=null;}
: "for" LPAREN init=genvar_initialization SEMI test=genvar_expression
SEMI iter=genvar_iteration RPAREN blk=generate_block
{lgc = getTree().loopGenerateConstruct(init,test,iter,blk);}
;
genvar_initialization returns [GenvarInit gi]
{ gi=null; GenvarIdent lhs=null; ConstExpression rhs=null;}
: lhs=genvar_identifier ASSIGN rhs=constant_expression
{gi = getTree().genvarInit(lhs,rhs);}
;
/**REMOVE LEFT RECURSION
genvar_expression
: genvar_primary
| unary_operator (attribute_instance)* genvar_primary
| genvar_expression binary_operator (attribute_instance)* genvar_expression
| genvar_expression QMARK (attribute_instance)*
genvar_expression COLON genvar_expression
;
**/
genvar_expression returns [GenvarExpression ge] //TODO
{ ge=null; Object e1=null, e2=null;}
: e1=genvar_expression_1 e2=genvar_expression_2
;
genvar_expression_1 returns [Object e1]
{ e1=null; GenvarPrimary gp=null; UnaryOp uop=null;}
: ( gp=genvar_primary
| uop=unary_operator (attribute_instance)* gp=genvar_primary
)
{ e1 = getTree().genvarExpression(uop,gp);}
;
genvar_expression_2 returns [Object ge2]
{ ge2=null; BinaryOp bop=null; GenvarExpression gv1=null, gv2=null;
Object e2=null; boolean isNull = false;
}
: (options{warnWhenFollowAmbig=false;}:
bop=binary_operator (attribute_instance)*
gv1=genvar_expression e2=genvar_expression_2
| QMARK (attribute_instance)* gv1=genvar_expression
COLON gv2=genvar_expression e2=genvar_expression_2
| /*empty*/ {isNull=true;}
)
{ ge2 = (isNull) ? null : getTree().genvarExpression(bop,gv1,gv2,e2);}
;
genvar_iteration returns [GenvarIteration gi]
{ gi=null; GenvarIdent gv=null; GenvarExpression ge=null;}
: gv=genvar_identifier ASSIGN ge=genvar_expression
{gi = getTree().genvarIteration(gv,ge);}
;
genvar_primary returns [GenvarPrimary gp]
{ gp=null; ConstPrimary cp=null; GenvarIdent gi=null;}
: ( (constant_primary)=> cp=constant_primary
| gi=genvar_identifier
)
{gp = getTree().genvarPrimary(cp,gi);}
;
conditional_generate_construct returns [ConditionalGenerateConstruct cgc]
{ cgc=null; IfGenerateConstruct igc=null; CaseGenerateConstruct cg=null;}
: ( igc=if_generate_construct
| cg=case_generate_construct
)
{cgc = getTree().conditionalGenerateConstruct(igc,cg);}
;
if_generate_construct returns [IfGenerateConstruct igs]
{ igs=null; ConstExpression ifExpr=null; GenerateBlock ifb=null, elb=null;}
: "if" LPAREN ifExpr=constant_expression RPAREN ifb=generate_block_or_null
(
options {
warnWhenFollowAmbig = false;
}
: "else" elb=generate_block_or_null
)?
{ igs = getTree().ifGenerateConstruct(ifExpr, ifb, elb);}
;
case_generate_construct returns [CaseGenerateConstruct cgc]
{ cgc=null; ConstExpression ce=null; CaseGenerateItem cgi=null;}
: "case" LPAREN ce=constant_expression RPAREN
{cgc = getTree().caseGenerateConstruct(cgc,ce,null);}
cgi=case_generate_item
{getTree().caseGenerateConstruct(cgc,null,cgi);}
(cgi=case_generate_item
{getTree().caseGenerateConstruct(cgc,null,cgi);} )*
"endcase"
;
case_generate_item returns [CaseGenerateItem cgi]
{ cgi=null; ConstExpression ce=null; GenerateBlock gb=null;}
: ce=constant_expression {cgi = getTree().caseGenerateItem(null,ce,null);}
(COMMA ce=constant_expression
{getTree().caseGenerateItem(cgi,ce,null);} )*
COLON gb=generate_block_or_null
{getTree().caseGenerateItem(cgi,null,gb);}
| "default" (COLON)? gb=generate_block_or_null
{cgi = getTree().caseGenerateItem(null,null,gb);}
;
generate_block returns [GenerateBlock gb]
{ gb=null; ModuleOrGenerateItem mi=null; GenerateBlockIdent bi=null;}
: mi=module_or_generate_item {gb = getTree().generateBlock(gb,mi);}
| loc1:"begin" (COLON bi=generate_block_identifier )?
{gb = getTree().generateBlock(bi,mi,loc1);}
(mi=module_or_generate_item {getTree().generateBlock(gb,mi);})*
"end"
{ if (null != mi) {
getTree().popScope();
}
}
;
generate_block_or_null returns [GenerateBlock gb]
{ gb=null;}
: gb=generate_block
| SEMI
;