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