blob: cc5d0a1614da64c423a9213777aab980cfb39438 (
plain)
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
|
// See LICENSE for license details.
grammar FIRRTL;
tokens { INDENT, DEDENT }
@lexer::header {
import firrtl.LexerHelper;
}
@lexer::members {
private final LexerHelper denter = new firrtl.LexerHelper()
{
@Override
public Token pullToken() {
return FIRRTLLexer.super.nextToken();
}
};
@Override
public Token nextToken() {
return denter.nextToken();
}
}
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
/* TODO
* - Add [info] support (all over the place)
* - Add support for extmodule
*/
// Does there have to be at least one module?
circuit
: 'circuit' id ':' info? INDENT module* DEDENT
;
module
: 'module' id ':' info? INDENT port* moduleBlock DEDENT
| 'extmodule' id ':' info? INDENT port* defname? parameter* DEDENT
;
port
: dir id ':' type info? NEWLINE
;
dir
: 'input'
| 'output'
;
type
: 'UInt' ('<' intLit '>')?
| 'SInt' ('<' intLit '>')?
| 'Fixed' ('<' intLit '>')? ('<' '<' intLit '>' '>')?
| 'Clock'
| 'Analog' ('<' intLit '>')?
| '{' field* '}' // Bundle
| type '[' intLit ']' // Vector
;
field
: 'flip'? fieldId ':' type
;
defname
: 'defname' '=' id NEWLINE
;
parameter
: 'parameter' id '=' intLit NEWLINE
| 'parameter' id '=' StringLit NEWLINE
| 'parameter' id '=' DoubleLit NEWLINE
| 'parameter' id '=' RawString NEWLINE
;
moduleBlock
: simple_stmt*
;
simple_reset0: 'reset' '=>' '(' exp exp ')';
simple_reset
: simple_reset0
| '(' simple_reset0 ')'
;
reset_block
: INDENT simple_reset info? NEWLINE DEDENT
| '(' + simple_reset + ')'
;
stmt
: 'wire' id ':' type info?
| 'reg' id ':' type exp ('with' ':' reset_block)? info?
| 'mem' id ':' info? INDENT memField* DEDENT
| 'cmem' id ':' type info?
| 'smem' id ':' type info?
| mdir 'mport' id '=' id '[' exp ']' exp info?
| 'inst' id 'of' id info?
| 'node' id '=' exp info?
| exp '<=' exp info?
| exp '<-' exp info?
| exp 'is' 'invalid' info?
| when
| 'stop(' exp exp intLit ')' info?
| 'printf(' exp exp StringLit ( exp)* ')' info?
| 'skip' info?
| 'attach' '(' exp+ ')' info?
;
memField
: 'data-type' '=>' type NEWLINE
| 'depth' '=>' intLit NEWLINE
| 'read-latency' '=>' intLit NEWLINE
| 'write-latency' '=>' intLit NEWLINE
| 'read-under-write' '=>' ruw NEWLINE
| 'reader' '=>' id+ NEWLINE
| 'writer' '=>' id+ NEWLINE
| 'readwriter' '=>' id+ NEWLINE
;
simple_stmt
: stmt | NEWLINE
;
/*
We should provide syntatctical distinction between a "moduleBody" and a "suite":
- statements require a "suite" which means they can EITHER have a "simple statement" (one-liner) on the same line
OR a group of one or more _indented_ statements after a new-line. A "suite" may _not_ be empty
- modules on the other hand require a group of one or more statements without any indentation to follow "port"
definitions. Let's call that _the_ "moduleBody". A "moduleBody" could possibly be empty
*/
suite
: simple_stmt
| INDENT simple_stmt+ DEDENT
;
when
: 'when' exp ':' info? suite? ('else' ( when | ':' info? suite?) )?
;
info
: FileInfo
;
mdir
: 'infer'
| 'read'
| 'write'
| 'rdwr'
;
ruw
: 'old'
| 'new'
| 'undefined'
;
exp
: 'UInt' ('<' intLit '>')? '(' intLit ')'
| 'SInt' ('<' intLit '>')? '(' intLit ')'
| id // Ref
| exp '.' fieldId
| exp '.' DoubleLit // TODO Workaround for #470
| exp '[' intLit ']'
| exp '[' exp ']'
| 'mux(' exp exp exp ')'
| 'validif(' exp exp ')'
| primop exp* intLit* ')'
;
id
: Id
| keywordAsId
;
fieldId
: Id
| RelaxedId
| UnsignedInt
| keywordAsId
;
intLit
: UnsignedInt
| SignedInt
| HexLit
;
// Keywords that are also legal ids
keywordAsId
: 'circuit'
| 'module'
| 'extmodule'
| 'parameter'
| 'input'
| 'output'
| 'UInt'
| 'SInt'
| 'Clock'
| 'Analog'
| 'Fixed'
| 'flip'
| 'wire'
| 'reg'
| 'with'
| 'reset'
| 'mem'
| 'depth'
| 'reader'
| 'writer'
| 'readwriter'
| 'inst'
| 'of'
| 'node'
| 'is'
| 'invalid'
| 'when'
| 'else'
| 'stop'
| 'printf'
| 'skip'
| 'old'
| 'new'
| 'undefined'
| 'mux'
| 'validif'
| 'cmem'
| 'smem'
| 'mport'
| 'infer'
| 'read'
| 'write'
| 'rdwr'
;
// Parentheses are added as part of name because semantics require no space between primop and open parentheses
// (And ANTLR either ignores whitespace or considers it everywhere)
primop
: 'add('
| 'sub('
| 'mul('
| 'div('
| 'rem('
| 'lt('
| 'leq('
| 'gt('
| 'geq('
| 'eq('
| 'neq('
| 'pad('
| 'asUInt('
| 'asSInt('
| 'asClock('
| 'shl('
| 'shr('
| 'dshl('
| 'dshr('
| 'cvt('
| 'neg('
| 'not('
| 'and('
| 'or('
| 'xor('
| 'andr('
| 'orr('
| 'xorr('
| 'cat('
| 'bits('
| 'head('
| 'tail('
| 'asFixedPoint('
| 'bpshl('
| 'bpshr('
| 'bpset('
;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
UnsignedInt
: '0'
| PosInt
;
SignedInt
: ( '+' | '-' ) PosInt
;
fragment
PosInt
: [1-9] ( Digit )*
;
HexLit
: '"' 'h' ( '+' | '-' )? ( HexDigit )+ '"'
;
DoubleLit
: ( '+' | '-' )? Digit+ '.' Digit+ ( 'E' ( '+' | '-' )? Digit+ )?
;
fragment
Digit
: [0-9]
;
fragment
HexDigit
: [a-fA-F0-9]
;
StringLit
: '"' UnquotedString? '"'
;
RawString
: '\'' UnquotedString? '\''
;
fragment
UnquotedString
: ( '\\\'' | '\\"' | ~[\r\n] )+?
;
FileInfo
: '@[' ('\\]'|.)*? ']'
;
Id
: LegalStartChar (LegalIdChar)*
;
RelaxedId
: (LegalIdChar)+
;
fragment
LegalIdChar
: LegalStartChar
| Digit
| '$'
;
fragment
LegalStartChar
: [a-zA-Z_]
;
fragment COMMENT
: ';' ~[\r\n]*
;
fragment WHITESPACE
: [ \t,]+
;
SKIP_
: ( WHITESPACE | COMMENT ) -> skip
;
NEWLINE
:'\r'? '\n' ' '*
;
|