systemc-clang
2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
plugins
hdl
parselib
grammar.py
Go to the documentation of this file.
1
from
lark
import
Lark
2
3
lark_grammar = Lark(
'''
4
start: modulelist typelist
5
6
modulelist: (hmodule)*
7
typelist: (htypedef)*
8
// hmodule: "hModule" ID "[" modportsiglist? (portbindinglist|processlist)* "]"
9
hmodule: "hModule" ID "[" modportsiglist? (hmodinitblock|processlist)* "]"
10
11
modportsiglist: "hPortsigvarlist" "NONAME" ("[" modportsigdecl+ "]" | "NOLIST")
12
13
?modportsigdecl: portdecltype
14
| sigdecltype
15
| vardeclinit
16
| moddecl
17
moddecl: "hModdecl" ID "[" htypeinfo "]"
18
portdecltype: portdecl "[" htypeinfo hvarinit? "]"
19
sigdecltype: sigdecl "[" htypeinfo "]"
20
sigdecl: "hSigdecl" ID
21
?portdecl: inportdecl | outportdecl
22
inportdecl: "hPortin" ID
23
outportdecl: "hPortout" ID
24
vardeclinit: "hVardecl" ID "[" htypeinfo (hvarinit | hvarinitint)? "]"
25
vardeclrn: "hVardeclrn" ID "[" htypeinfo hliteral "]"
26
funcparami: "hFunctionParamI" ID "[" htypeinfo (hvarinit | hvarinitint)? "]"
27
funcparamio: "hFunctionParamIO" ID "[" htypeinfo (hvarinit | hvarinitint)? "]"
28
| "hFunctionParamRef" ID "[" htypeinfo (hvarinit | hvarinitint)? "]"
29
?hvarinit: "hVarInit" "NONAME" expression
30
| "hVarInit" "NONAME" "[" hvarinitlist "]"
31
?hvarinitint: "hVarInit" NUM "NOLIST"
32
// can be no process at all in the module
33
processlist: "hProcesses" "NONAME" "[" (hprocess|hfunction|hthread)*"]"
34
// could be nothing
35
// temporarily ignore the hMethod node
36
hprocess: "hProcess" ID "[" "hMethod" (ID|"NONAME") "[" prevardecl hcstmt "]" "]"
37
38
// hthread consists of a synchronous block that sets state and a switch block that produces next state
39
// hfunction
40
hthread: "hProcess" ID "[" modportsiglist? hfunction* hthreadsync hthreadswitch "]"
41
hthreadsync: "hMethod" ID "[" ifstmt "]"
42
hthreadswitch: "hMethod" ID "[" stmts "]"
43
44
prevardecl: vardecl*
45
vardecl: vardeclinit | vardeclrn
46
47
// can be just an empty statement
48
hcstmt: "hCStmt" "NONAME" "[" stmts "]" // useful for raising variable decls
49
| "hCStmt" "NONAME" "NOLIST"
50
stmts: stmt+
51
stmt : expression_in_stmt
52
| syscwrite
53
| ifstmt
54
| forstmt
55
| hcstmt
56
| whilestmt
57
| dostmt
58
| switchstmt
59
| blkassign
60
| hnoop
61
| hreturnstmt
62
| breakstmt
63
| continuestmt
64
| hwait
65
| hslice
66
| portbinding
67
| hnamedsensvar
68
69
continuestmt: "hContinue" "NONAME" "NOLIST"
70
// hvarinitlist can be empty
71
hvarinitlist: "hVarInitList" "NONAME" "[" (hvarinitlist | expression)+ "]"
72
| "hVarInitList" "NONAME" "NOLIST"
73
// hvarinitlist: "hVarInitList" "NONAME" "NOLIST"
74
75
hwait: "hWait" "wait" "NOLIST" | "hWait" NUM ("[" hliteral "]" | "NOLIST")
76
77
breakstmt: "hBreak" "NONAME" "NOLIST"
78
79
?htotype: htouint | htoint | htolong | htoulong | hnoop | htoi64 | htou64
80
81
?htobool: ("hBuiltinFunction" "to_bool" | "hNoop" "to_bool") "[" harrayref "]"
82
htouint: "hBuiltinFunction" "to_uint" "[" (syscread|hvarref|hslice) "]"
83
htoint: "hBuiltinFunction" "to_int" "[" (syscread|hvarref|hslice|harrayref) "]"
84
htolong: "hBuiltinFunction" "to_long" "[" (syscread|hvarref|hslice) "]"
85
htoulong: "hBuiltinFunction" "to_ulong" "[" (syscread|hvarref|hslice) "]"
86
hnoop: "hNoop" "NONAME" "NOLIST"
87
htoi64: "hBuiltinFunction" "to_int64" "[" (syscread|hvarref|hslice) "]"
88
htou64: "hBuiltinFunction" "to_uint64" "[" (syscread|hvarref|hslice) "]"
89
hscmin: "hBuiltinFunction" "sc_min" "[" expression expression "]"
90
hscmax: "hBuiltinFunction" "sc_max" "[" expression expression "]"
91
hlength: "hBuiltinFunction" "length" "[" (syscread|hvarref) "]"
92
93
hbuiltin: hscmin | hscmax | hreduceop | hlength
94
95
// hmodinitblock:
96
// first component is the id of the module (in parent?)
97
// second component is initialization list
98
// third component is port binding list
99
hmodinitblock: "hModinitblock" ID "[" vardecl* hcstmt* hsenslist*"]"
100
| "hModinitblock" ID "NOLIST"
101
// Port Bindings
102
portbindinglist: "hCStmt" "NONAME" "[" portbinding* "]"
103
// hPortbinding u_dut [
104
// hVarref avg_out NOLIST
105
// hVarref dut_avg NOLIST
106
// ]
107
portbinding: "hPortbinding" ID "[" hvarref hvarref "]"
108
| "hPortbinding" ID "[" hbindingref hbindingref "]"
109
| "hPortbinding" ID "[" hvarref hbindingref "]"
110
| "hPortbinding" ID "[" hbindingarrayref hbindingarrayref "]"
111
| "hPortbinding" ID "[" hvarref hbindingarrayref "]"
112
| "hPortbinding" ID "[" hfieldaccess (hvarref|hbinop) "]"
113
| "hPortbinding" ID "[" hbinop (hvarref|hbinop) "]"
114
// TODO: replace portbinding with succinct syntax
115
hbindingref: "hVarref" ID "[" hliteral "]"
116
// compared array ref in normal expressions
117
// we use a more restrictive form here
118
hbindingarrayref: "hBinop" "ARRAYSUBSCRIPT" "[" (hfieldaccess | hvarref|hbindingarrayref) (hvarref|hliteral|hbinop) "]"
119
120
121
// This is solely for maintaining the semicolon
122
expression_in_stmt: expression
123
124
// while(condition) stmts
125
whilestmt: "hWhileStmt" "NONAME" "[" expression stmt "]"
126
127
// do stmts while(condition)
128
dostmt: "hDoStmt" "NONAME" "[" expression stmt "]"
129
130
// for(forinit; forcond; forpostcond) stmts
131
forstmt: "hForStmt" "NONAME" "[" forinit forcond forpostcond forbody? "]"
132
forinit: "hPortsigvarlist" "NONAME" "[" vardeclinit "]"
133
| vardeclinit
134
| hnoop
135
| blkassign
136
forcond: expression
137
| hnoop
138
forpostcond: expression
139
| hnoop
140
forbody: stmt
141
142
switchstmt: "hSwitchStmt" "NONAME" "[" switchcond switchbody "]"
143
switchcond: expression
144
// Note: we don't make this a noraml statement as in the context of switch,
145
// we don't use general statements
146
switchbody: "hCStmt" "NONAME" "[" ((casestmt* breakstmt?)+) "]"
147
casestmt: "hSwitchCase" "NONAME" "[" casevalue (casestmt | (stmt+)) breakstmt? "]" hnoop
148
| "hSwitchCase" "NONAME" "[" casevalue hnoop "]"
149
| "hSwitchCase" "NONAME" "[" casevalue (casestmt | (stmt+)) breakstmt? "]"
150
| "hSwitchDefault" "NONAME" "[" stmt+ "]"
151
casevalue: expression
152
153
// Function
154
hfunction : "hFunction" ID "[" hfunctionrettype hfunctionparams? hfunctionlocalvars hfunctionbody "]"
155
hfunctionlocalvars: vardeclinit*
156
hfunctionbody: hcstmt
157
hfunctionrettype: "hFunctionRetType" "NONAME" "[" htypeinfo "]"
158
| "hFunctionRetType" "NONAME" "NOLIST" // only appears in generated statements
159
hfunctionparams : "hFunctionParams" "NONAME" "[" (funcparami|funcparamio)* "]"
160
| "hFunctionParams" "NONAME" "NOLIST"
161
hreturnstmt: "hReturnStmt" "NONAME" "[" (expression|hslice) "]"
162
| "hReturnStmt" "NONAME" "NOLIST" // return;
163
164
hsenslist : "hSenslist" ID "[" hsensvar* "]"
165
| "hSenslist" ID "NOLIST"
166
hsensvar : "hSensvar" "NONAME" "[" (hsensedge|expression|hvalchange) ("hNoop" | "hBuiltinFunction") npa "NOLIST" ("hNoop" npa "NOLIST")* "]"
167
| hasync
168
hnamedsensvar : "hSensvar" ID "[" (hsensedge|expression|hvalchange) ("hNoop" | "hBuiltinFunction") npa "NOLIST" ("hNoop" npa "NOLIST")* "]"
169
hasync : "hSensvar" "ASYNC" "[" expression hliteral "]"
170
171
hvalchange: "hNoop" "value_changed_event" "[" expression "]"
172
hsensedge : "hNoop" npa "NOLIST"
173
| "hBuiltinFunction" npa "NOLIST"
174
| "hBuiltinFunction" npa "[" expression "]"
175
!npa : "neg" | "pos" | "always" | "posedge_event" | "negedge_event"
176
177
// if and if-else, not handling if-elseif case
178
ifstmt: "hIfStmt" "NONAME" "[" (expression|harrayref) stmt? stmt?"]"
179
180
181
?expression: hbinop
182
| hunop
183
| hliteral
184
| hvarref
185
| hunimp
186
| syscread
187
| hmethodcall
188
| "[" expression "]"
189
| htobool
190
| htouint
191
| htoi64
192
| htou64
193
| htoint
194
| htolong
195
| htoulong
196
| hcondop
197
| hlrotate
198
| horreduce
199
| hfieldaccess
200
| hbuiltin
201
202
hfieldaccess: "hFieldaccess" "NONAME" "[" (harrayref|syscread) hfieldname "]"
203
hfieldname: "hField" ID "NOLIST"
204
205
hlrotate : "hBuiltinFunction" "lrotate" "[" expression expression "]"
206
horreduce: "hBuiltinFunction" "or_reduce" "[" expression "]"
207
hcondop : "hCondop" "NONAME" "[" (hcondop | hslice | hliteral | hbinop | hunop | syscread | hvarref | hmethodcall) (hslice | expression | hprefix) (hslice | expression | hpostfix) "]"
208
209
syscread : hsigassignr "[" (expression | harrayref) "]"
210
syscwrite : hsigassignl "[" expression (expression | hfieldaccess) "]"
211
?hsigassignr : "hSigAssignR" "read"
212
?hsigassignl : "hSigAssignL" "write"
213
// function call
214
hvarref : "hVarref" ID "NOLIST"
215
hunimp: "hUnimpl" ID "NOLIST"
216
hbinop: "hBinop" BINOP "[" (expression|hslice|harrayref) (expression|hslice|blkassign) "]"
217
218
// A temporary hack to handle --
219
hunop: "hUnop" UNOP_NON_SUB "[" (expression|hslice) "]"
220
| "hUnop" UNOP_SUB "[" (expression|hslice) "]"
221
| "hUnop" UNOP_BNOT "[" (expression|hslice) "]"
222
| "hBinop" UNOP_NOT "[" (expression|hslice) "]"
223
| "hBinop" UNOP_BNOT "[" (expression|hslice) "]"
224
| hpostfix
225
| hprefix
226
| hunopdec
227
// | hreduceop
228
hpostfix: "hPostfix" (UNOP_INC | UNOP_DEC) "[" expression "]"
229
hprefix: "hPrefix" (UNOP_INC | UNOP_DEC) "[" expression "]"
230
hunopdec: "hUnop" "-" "-" "[" expression "]" // hack to work with --
231
232
hreduceop: "hBuiltinFunction" REDUCE_OP "[" expression "]"
233
REDUCE_OP: "and_reduce" | "or_reduce" | "xor_reduce" | "nand_reduce" | "nor_reduce" | "xnor_reduce"
234
235
// Separate '=' out from so that it is not an expression but a standalone statement
236
blkassign: "hBinop" "=" "[" (hconcat | hvarref | hliteral | hfieldaccess) (hbuiltin | htotype | hfieldaccess | hcomma | htobool | hunop | hvarref | hliteral | harrayref | hnsbinop | hunimp | syscread | hmethodcall | hcondop | hconcat) "]"
237
| "hBinop" "=" "[" harrayref arrayrhs "]"
238
| nblkassign
239
| vassign
240
// These assignments are only intended to be used as blocking assignments
241
// The semantics may not be straightforward in clocked block
242
hcompoundassign: "hBinop" COMPOUND_ASSIGN "[" hvarref hvarref "]"
243
?arrayrhs: horreduce
244
| htobool
245
| htoint
246
| htouint
247
| htolong
248
| htoulong
249
| hvarref
250
| hliteral
251
| harrayref
252
| hnsbinop
253
| hunimp
254
| syscread
255
| hmethodcall
256
| hunop
257
| hliteral
258
| hcondop
259
| htoint
260
| hconcat
261
262
nblkassign: "hSigAssignL" "write" "[" (hliteral | hvarref | harrayref) (syscread | hliteral | harrayref | hunop | hvarref | htobool | hmethodcall | hfieldaccess) "]"
263
| "hSigAssignL" "write" "[" (hliteral | hvarref | harrayref) nonrefexp "]"
264
hconcat: ("hBinop" "concat" "[" | "hMethodCall" "NONAME" "[" "hBinop" "concat" "NOLIST") (expression|harrayref|hconcat) (expression|harrayref|hconcat) "]"
265
266
267
vassign: "hVarAssign" "NONAME" "[" hvarref (hnsbinop | syscread | hliteral | hvarref | expression | harrayref | hvarinitlist)"]"
268
// Normal expressions that can not be expanded
269
nonrefexp: hbinop
270
271
harrayref: "hBinop" "ARRAYSUBSCRIPT" "[" (hliteral | hvarref | syscread | harrayref) expression "]"
272
| hslice
273
hslice: "hBinop" "SLICE" "[" hvarref expression expression "]"
274
| "hBuiltinFunction" "range" "[" (hvarref | harrayref | syscread | hmethodcall ) expression expression "]"
275
| "hBuiltinFunction" "bit" "[" (hvarref | harrayref | syscread | hmethodcall) expression "]"
276
hnsbinop: "hBinop" NONSUBBINOP "[" (expression|hslice) (expression|hslice) "]"
277
278
// Temporary hack to handle -= / +=
279
hmodassign : "hBinop" hmodassigntype "[" hvarref (hliteral|hvarref|hbinop) "]"
280
?hmodassigntype : haddassign | hsubassign
281
haddassign : "+" "="
282
hsubassign : "-" "="
283
284
// Comma op is the C++ comma where the latter part of the comma expression is returned
285
hcomma: "hBinop" "," "[" (blkassign | hunop | hmethodcall) (hunop | expression | hmethodcall) "]"
286
287
hmethodcall: "hMethodCall" hidorstr "[" (expression|hslice) (expression|hslice)* "]"
288
| "hMethodCall" hidorstr "NOLIST"
289
290
?hidorstr: ID | STRING
291
hliteral: idlit | numlit | numlitwidth
292
idlit : "hLiteral" ID "NOLIST"
293
numlit : "hLiteral" NUM "NOLIST"
294
numlitwidth : "hLiteral" NUM "[" htypeinfo "]"
295
htypeinfo: "hTypeinfo" "NONAME" "[" htype "]"
296
| "hTypeinfo" "NONAME" "NOLIST" // ?
297
htype: htypearray
298
| "hType" TYPESTR "NOLIST"
299
| "hType" TYPESTR "[" (htype|htypeint)+ "]" // nested types, type parameters
300
hdeptype: "hType" "typename" TYPESTR "NOLIST"
301
htypearray : "hType" "array" arraydimlength "[" (htype|htypeint)+ "]"
302
arraydimlength: "##" NUM
303
| "##" NUM arraydimlength
304
htypeint: "hLiteral" NUM "NOLIST" // integer type parameters
305
htypedef: "hTypedef" TYPESTR "[" htypefields "]"
306
307
htypefields: htypefield*
308
htypefield: "hTypeField" ID "[" (htype|hdeptype) "]"
309
310
ID: /[a-zA-Z_][a-zA-Z_0-9#]*/
311
NUM: /(\+|\-)?[0-9]+/
312
TYPESTR: /[a-zA-Z_]([a-zA-Z_0-9]|::)*/
313
BINOP: COMPOUND_ASSIGN | NONSUBBINOP | "ARRAYSUBSCRIPT" | "SLICE" | "concat"
314
NONSUBBINOP: "+=" | "-=" | "*=" | "/=" | "==" | "<<" | ">>" | "&&" | "||" | "|" | ">=" | ">" | ARITHOP | "<=" | "<" | "%" | "!=" | "&" | "@="
315
ARITHOP: "+" | "-" | "*" | "/" | "^"
316
UNOP_NON_SUB: "!" | "++" | "-" | "+"
317
UNOP_SUB: "-"
318
UNOP_DEC: "--"
319
UNOP_INC: "++"
320
// These are temporary nodes that should be removed when hBinop is fixed
321
UNOP_BOR: "|"
322
UNOP_NOT: "!"
323
UNOP_BNOT: "~"
324
// alias_translation.py
325
COMPOUND_ASSIGN: "*=" | "+=" | "-=" | "/=" | "%=" | "|=" | "&=" | "^=" | "<<=" | ">>="
326
%import common.WS
327
%ignore WS
328
%import common.ESCAPED_STRING -> STRING
329
'''
, parser=
'lalr'
, debug=
True
, propagate_positions=
True
)
330
331
332
class
UnexpectedHCodeStructureError
(Exception):
333
"""raised when a hcode node is not as expected"""
334
pass
parselib.grammar.UnexpectedHCodeStructureError
Definition
grammar.py:332
Generated by
1.12.0