forked from ArgonDesign/alogic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlogicParser.g4
More file actions
228 lines (191 loc) · 8.48 KB
/
AlogicParser.g4
File metadata and controls
228 lines (191 loc) · 8.48 KB
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
////////////////////////////////////////////////////////////////////////////////
// Argon Design Ltd. Project P8009 Alogic
// Copyright (c) 2017-2018 Argon Design Ltd. All rights reserved.
//
// This file is covered by the BSD (with attribution) license.
// See the LICENSE file for the precise wording of the license.
//
// Module: Alogic Compiler
// Author: Peter de Rivaz/Geza Lore
//
// DESCRIPTION:
//
// Antlr4 parser grammar for Alogic
////////////////////////////////////////////////////////////////////////////////
parser grammar AlogicParser;
options {
tokenVocab = AlogicLexer;
}
///////////////////////////////////////////////////////////////////////////////
// Start rule for whole source file
///////////////////////////////////////////////////////////////////////////////
start
: (type_definition ';')*
entity
EOF
;
///////////////////////////////////////////////////////////////////////////////
// Type definitions
///////////////////////////////////////////////////////////////////////////////
type_definition
: 'typedef' kind IDENTIFIER # TypeDefinitionTypedef
| 'struct' IDENTIFIER '{'
field+
'}' # TypeDefinitionStruct
;
field: kind IDENTIFIER SEMICOLON;
///////////////////////////////////////////////////////////////////////////////
// Type names
///////////////////////////////////////////////////////////////////////////////
kind
: kind ('[' expr ']')+ # TypeVec
| 'bool' # TypeBool
| INTTYPE # TypeInt
| UINTTYPE # TypeUInt
| 'int' '(' expr ')' # TypeIntN
| 'uint' '(' expr ')' # TypeUIntN
| IDENTIFIER # TypeIdent
| 'void' # TypeVoid
;
///////////////////////////////////////////////////////////////////////////////
// Entity
///////////////////////////////////////////////////////////////////////////////
entity
: attr?
(variant='fsm' | variant='network' | variant='verbatim' 'entity') IDENTIFIER '{'
(decl ';')*
(entity_content)*
'}'
;
///////////////////////////////////////////////////////////////////////////////
// Declarations
///////////////////////////////////////////////////////////////////////////////
decl: attr? declbase ('=' expr)? ;
declbase
: kind IDENTIFIER # DeclVar
| 'out' flow_control_type? storage_type? kind IDENTIFIER # DeclOut
| 'in' flow_control_type? kind IDENTIFIER # DeclIn
| 'param' kind IDENTIFIER # DeclParam
| 'const' kind IDENTIFIER # DeclConst
| 'pipeline' kind IDENTIFIER # DeclPipeline
| kind IDENTIFIER '[' expr ']' # DeclArr
| 'sram' (wire='wire')? kind IDENTIFIER '[' expr ']' # DeclSram
;
flow_control_type
: 'sync' # FlowControlTypeSync
| SYNC_READY # FlowControlTypeSyncReady
| SYNC_ACCEPT # FlowControlTypeSyncAccept
;
storage_type
: 'wire' # StorageTypeWire
| (slices+=('bubble' | 'fslice' | 'bslice'))+ # StorageTypeSlices
;
///////////////////////////////////////////////////////////////////////////////
// Entity contents
///////////////////////////////////////////////////////////////////////////////
entity_content
: attr? instance ';' # EntityContentInstance
| connect ';' # EntityContentConnect
| (attr? autoinst='new')? entity # EntityContentEntity
| 'fence' block # EntityContentFenceBlock
| attr? 'void' IDENTIFIER '(' ')' block # EntityContentFunction
| 'verbatim' IDENTIFIER VERBATIM_BODY # EntityContentVerbatimBlock
;
connect : lhs=expr '->' rhs+=expr (',' rhs+=expr)* ;
instance : IDENTIFIER '=' 'new' IDENTIFIER '(' param_assigns ')' ;
param_assigns : (IDENTIFIER '=' expr (',' IDENTIFIER '=' expr)*)? ;
///////////////////////////////////////////////////////////////////////////////
// Statements
///////////////////////////////////////////////////////////////////////////////
block
: '{' statement* '}'
;
statement
: block # StmtBlock
| 'if' '(' expr ')' thenStmt=statement ('else' elseStmt=statement)? # StmtIf
| 'case' '(' expr ')' '{' case_clause+ '}' # StmtCase
| let loop # StmtLet
| loop # StatementLoop
| 'goto' IDENTIFIER ';' # StmtGoto
| 'fence' ';' # StmtFence
| 'break' ';' # StmtBreak
| 'return' ';' # StmtReturn
| decl ';' # StmtDecl
| assignment ';' # StatementAssignment
| expr ';' # StmtExpr
| '$' '(' STRING ')' ';' # StmtDollarComment
;
loop
: 'loop' block # StmtLoop
| 'do' block 'while' '(' expr ')' ';' # StmtDo
| 'while' '(' expr ')' block # StmtWhile
| 'for' '(' loop_init? ';' expr? ';' for_steps? ')' block # StmtFor
;
let
: 'let' '(' loop_init ')'
;
case_clause
: 'default' ':' statement # DefaultCase
| commaexpr ':' statement # NormalCase
;
assignment
: expr op='=' expr # StmtAssign
| expr ASSIGNOP expr # StmtUpdate
| expr op=('++'|'--') # StmtPost
;
loop_init
: loop_init_item (',' loop_init_item)*
;
loop_init_item
: expr '=' expr # LoopInitAssign
| kind IDENTIFIER '=' expr # LoopInitDecl
;
for_steps
: step+=assignment (',' step+=assignment)*
;
///////////////////////////////////////////////////////////////////////////////
// Expressions
///////////////////////////////////////////////////////////////////////////////
expr
: '(' expr ')' # ExprBracket
// Literals
| 'true' # ExprTrue
| 'false' # ExprFalse
| sign=('+' | '-')? SIZEDINT # ExprSizedInt
| sign=('+' | '-')? UNSIZEDINT # ExprUnsizedInt
| STRING # ExprString
// Names
| IDENTIFIER # ExprIdent
| ATID # ExprAtid
| DOLLARID # ExprDollarid
// Call
| expr '(' commaexpr? ')' # ExprCall
// Index/Slice
| expr '[' idx=expr ']' # ExprIndex
| expr '[' lidx=expr op=(':' | '-:' | '+:') ridx=expr ']' # ExprSlice
// Select
| expr '.' IDENTIFIER # ExprSelect
// Operators
| op=('+' | '-' | '~' | '!' | '&' | '|' | '^' ) expr # ExprUnary
| expr op=('*' | '/' | '%') expr # ExprBinary
| expr op=('+' | '-') expr # ExprBinary
| expr op=('<<' | '>>' | '>>>' | '<<<' ) expr # ExprBinary
| expr op=('>' | '>=' | '<' | '<=') expr # ExprBinary
| expr op=('==' | '!=') expr # ExprBinary
| expr op='&' expr # ExprBinary
| expr op='^' expr # ExprBinary
| expr op='|' expr # ExprBinary
| expr op='&&' expr # ExprBinary
| expr op='||' expr # ExprBinary
|<assoc=right> expr op='?' expr ':' expr # ExprTernary
| '{' expr '{' commaexpr '}' '}' # ExprRep
| '{' commaexpr '}' # ExprCat
// Type
| kind # ExprType
;
commaexpr: expr (',' expr)* ;
///////////////////////////////////////////////////////////////////////////////
// Attributes
///////////////////////////////////////////////////////////////////////////////
attr: '(*' attrspec (',' attrspec)* '*)' ;
attrspec: IDENTIFIER ('=' expr)? ;