-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqbe.grm
465 lines (415 loc) · 18.3 KB
/
qbe.grm
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
structure T = QbeTypes
datatype par = Par of T.ty * Atom.atom
| Pare of Atom.atom
| Parc of Atom.atom * Atom.atom
datatype arg' = Arg of T.ty * T.value
| Arge of T.value
| Argv
%%
%name Qbe
%term
EOF
| TYPE | ALIGN | EXPORT | SECTION | DATA | FUNCTION
| ADD | SUB | DIV | MUL | NEG | UDIV | REM | UREM | OR | XOR | AND | SAR | SHR
| SHL
| STORED | STORES | STOREL | STOREW | STOREH | STOREB
| LOAD | LOADD | LOADS | LOADL | LOADW | LOADSW | LOADUW | LOADSH | LOADUH
| LOADSB | LOADUB
| ALLOC4 | ALLOC8 | ALLOC16
| CEQD | CEQL | CEQS | CEQW | CGED | CGES | CGTD | CGTS | CLED | CLES | CLTD
| CLTS | CNED | CNEL | CNES | CNEW | COD | COS | CSGEL | CSGEW | CSGTL
| CSGTW | CSLEL | CSLEW | CSLTL | CSLTW | CUGEL | CUGEW | CUGTL | CUGTW
| CULEL | CULEW | CULTL | CULTW | CUOD | CUOS
| DTOSI | DTOUI | EXTS | EXTSB | EXTSH | EXTSW | EXTUB | EXTUH | EXTUW
| SLTOF | ULTOF | STOSI | STOUI | SWTOF | UWTOF | TRUNCD
| CAST | COPY
| CALL
| VASTART | VAARG
| PHI
| JMP | JNZ | RET | RET0 | RETW | RETL | RETS | RETD | RETC
| NOP | PAR | PARC | PARE | ARGW | ARGL | ARGS | ARGD | ARGC | ARGE | ARGV
| W | L | S | D | B | H | Z
| PLUS | EQ | COMMA | LPAREN | RPAREN | LBRACE | RBRACE | ENV | DOTS
| TYP of Atom.atom
| GLO of Atom.atom
| TMP of Atom.atom
| LBL of Atom.atom
| INT of Int64.int
| FLTS of Real32.real
| FLTD of Real64.real
| STR of string
%pos int
%pure
%eop EOF
%noshift EOF
%value TYP (Atom.atom ":bogus")
%value GLO (Atom.atom "$bogus")
%value TMP (Atom.atom "%bogus")
%value LBL (Atom.atom "@bogus")
%value INT (1)
%value FLTS (1.0)
%value FLTD (1.0)
%value STR ("")
%keyword TYPE ALIGN EXPORT SECTION DATA FUNCTION
%prefer LBL
%nonterm
module of T.def list
| defs of T.def list
| def of T.def
| i32 of int
| tylist of (T.ty * int) list
| ty_num of T.ty * int
| typ of T.ty
| linkage of T.linkage
| section of T.section
| align_opt of int option
| data_fields of T.datafield list
| data_field of T.datafield
| data_items of T.dataitem list
| data_item of T.dataitem
| const of T.const
| typ_opt of T.ty option
| env_params of {envp: Atom.atom option,
params: (T.ty * Atom.atom) list,
variadic: bool}
| params of {params: (T.ty * Atom.atom) list, variadic: bool}
| param of T.ty * Atom.atom
| blocks of {blocks: T.block list,
envp: Atom.atom option,
params: (T.ty * Atom.atom) list,
rty: T.ty option}
| block of {block: T.block,
envp: Atom.atom option,
params: (T.ty * Atom.atom) list,
rty: T.ty option}
| pars_phis_stmts of {envp: Atom.atom option,
params: (T.ty * Atom.atom) list,
phis: T.phi list,
stmts: T.stmt list}
| par of par
| phis_stmts of T.phi list * T.stmt list
| phi of T.phi
| phiargs of (Atom.atom * T.value) list
| phiarg of Atom.atom * T.value
| stmts of T.stmt list
| stmt of T.stmt
| jump_rty of T.jump option * T.ty option
| jump of T.jump option
| value of T.value
| retty of T.ty
| tmp_opt of (Atom.atom * T.ty) option
| env_args of {envp: T.value option,
args: (T.ty * T.value) list,
vararg: int option}
| args of {args: (T.ty * T.value) list, vararg: int option, i: int}
| arg of (T.ty * T.value) option
| args_call of T.call * int
| arg' of arg'
| ins1 of T.value -> T.instr
| ins2 of T.value * T.value -> T.instr
| store of T.value * T.value -> T.stmt
%%
module : defs (defs)
defs : ([])
| def defs (def::defs)
def : TYPE TYP EQ LBRACE tylist RBRACE (T.Type{name=TYP,align=NONE,items=tylist})
| TYPE TYP EQ ALIGN i32 LBRACE tylist RBRACE(T.Type{name=TYP,align=SOME i32,items=tylist})
| TYPE TYP EQ ALIGN i32 LBRACE i32 RBRACE (T.OpaqueType{name=TYP,align=i32,size=i32})
| linkage DATA GLO EQ align_opt LBRACE data_fields RBRACE
(T.Data {name = GLO,
linkage = linkage,
align = align_opt,
fields = data_fields})
| linkage FUNCTION typ_opt GLO LPAREN env_params RPAREN LBRACE blocks RBRACE
(
let val {envp, params, variadic} = env_params
val {blocks, envp=envp', params=params', rty} = blocks
in T.Function {name = GLO,
linkage = linkage,
envp = if isSome envp then envp else envp',
params = if null params then params' else params,
variadic = variadic,
result = if isSome typ_opt then typ_opt else rty,
blocks = blocks}
end
)
tylist : ([])
| ty_num ([ty_num])
| ty_num COMMA tylist (ty_num::tylist)
ty_num : typ (typ, 1)
| typ i32 (typ, i32)
i32 : INT (Int64.toInt INT)
typ : W (T.W)
| L (T.L)
| S (T.S)
| D (T.D)
| B (T.B)
| H (T.H)
| TYP (T.Agg TYP)
linkage : ({exported=false, section=NONE})
| EXPORT ({exported=true, section=NONE})
| EXPORT section ({exported=true, section=SOME section})
| section EXPORT ({exported=true, section=SOME section})
section : SECTION STR ({name=STR, flags=NONE})
| SECTION STR STR ({name=STR1, flags=SOME STR2})
align_opt : (NONE)
| ALIGN i32 (SOME i32)
data_fields : ([])
| data_field ([data_field])
| data_field COMMA data_fields(data_field::data_fields)
data_field : typ data_items (T.DataTy(typ, data_items))
| Z i32 (T.DataZ i32)
data_items : data_item ([data_item])
| data_item data_items (data_item::data_items)
data_item : GLO (T.DataSym(GLO, 0))
| GLO PLUS i32 (T.DataSym(GLO, i32))
| STR (T.DataStr STR)
| const (T.DataCon const)
const : INT (T.Int INT)
| FLTS (T.Flts FLTS)
| FLTD (T.Fltd FLTD)
typ_opt : (NONE)
| typ (SOME typ)
env_params : params ({envp = NONE,
params = #params params,
variadic = #variadic params})
| ENV TMP ({envp = SOME TMP,
params = [],
variadic = false})
| ENV TMP COMMA params ({envp = SOME TMP,
params = #params params,
variadic = #variadic params})
params : ({params=[], variadic=false})
| DOTS ({params=[], variadic=true})
| DOTS COMMA ({params=[], variadic=true})
| param ({params=[param], variadic=false})
| param COMMA params ({params = param::(#params params),
variadic = #variadic params})
param : typ TMP ((typ, TMP))
blocks : block
(let val {block, envp, params, rty} = block
in {blocks=[block], envp=envp, params=params, rty=rty}
end)
| block blocks
(let val {block, envp, params, rty=rty'} = block
val {blocks, rty, ...} = blocks
in {blocks=block::blocks, envp=envp, params=params,
rty = if isSome rty then rty else rty'}
end)
block : LBL pars_phis_stmts jump_rty
(
let val {envp, params, phis, stmts} = pars_phis_stmts
in {block = {label=LBL, phis=phis, stmts=stmts, jump= #1 jump_rty},
envp = envp,
params = params,
rty = #2 jump_rty}
end
)
pars_phis_stmts : par pars_phis_stmts
(
let val {envp, params, phis, stmts} = pars_phis_stmts
in case par
of Par p => {envp=envp, params=p::params, phis=phis, stmts=stmts}
| Pare tmp => {envp=SOME tmp, params=params, phis=phis, stmts=stmts}
| Parc(agg, tmp) => {envp=envp, params=(T.Agg agg, tmp)::params,
phis=phis, stmts=stmts}
end
)
| phis_stmts ({envp = NONE,
params = [],
phis = #1 phis_stmts,
stmts = #2 phis_stmts})
par : TMP EQ typ PAR (Par(typ, TMP))
| TMP EQ L PARE (Pare TMP)
| TMP EQ L PARC TYP (Parc(TYP, TMP))
phis_stmts : phi phis_stmts (phi::(#1 phis_stmts), #2 phis_stmts)
| stmts ([], stmts)
phi : TMP EQ typ PHI phiargs ({temp=(TMP, typ), args=phiargs})
phiargs : phiarg ([phiarg])
| phiarg COMMA phiargs (phiarg::phiargs)
phiarg : LBL value ((LBL, value))
stmts : ([])
| NOP stmts (stmts)
| stmt stmts (stmt::stmts)
stmt : TMP EQ typ ins1 value (T.Assign(TMP, typ, ins1 value))
| TMP EQ typ ins2 value COMMA value (T.Assign(TMP, typ, ins2(value1, value2)))
| store value COMMA value (store(value1, value2))
| tmp_opt CALL GLO LPAREN env_args RPAREN
(
let val {envp, args, vararg} = env_args
in T.Call {result = tmp_opt,
name = GLO,
envp = envp,
args = args,
vararg = Option.map (fn i => length args - i) vararg}
end
)
| args_call
(
let val ({result, name, envp, args, vararg}, _) = args_call
in T.Call {result = result,
name = name,
envp = envp,
args = args,
vararg = Option.map (fn i => length args - i) vararg}
end
)
| VASTART value (T.Vastart value)
jump_rty : jump ((jump, NONE))
| retty value ((SOME(T.Ret(SOME value)), SOME retty))
| RETC value COMMA TYP ((SOME(T.Ret(SOME value)), SOME(T.Agg TYP)))
jump : (NONE)
| JMP LBL (SOME(T.Jmp LBL))
| JNZ value COMMA LBL COMMA LBL (SOME(T.Jnz(value, LBL1, LBL2)))
| RET (SOME(T.Ret NONE))
| RET0 (SOME(T.Ret NONE))
| RET value (SOME(T.Ret(SOME value)))
value : TMP (T.Tmp TMP)
| GLO (T.Glo GLO)
| const (T.Con const)
retty : RETW (T.W)
| RETL (T.L)
| RETS (T.S)
| RETD (T.D)
tmp_opt : (NONE)
| TMP EQ typ (SOME(TMP, typ))
env_args : args ({envp = NONE,
args = #args args,
vararg = #vararg args})
| ENV value ({envp = SOME value,
args = [],
vararg = NONE})
| ENV value COMMA args ({envp = SOME value,
args = #args args,
vararg = #vararg args})
args : ({args=[], vararg=NONE, i= ~1})
| arg
(
case arg
of NONE => {args=[], vararg=SOME 0, i=0}
| SOME a => {args=[a], vararg=NONE, i=0}
)
| arg COMMA args
(
let val {args, vararg, i} = args
val i = i + 1
in case arg
of NONE => {args=args, vararg=SOME i, i=i}
| SOME a => {args=a::args, vararg=vararg, i=i}
end
)
arg : typ value (SOME(typ, value))
| DOTS (NONE) (* variadic marker *)
args_call : arg' args_call
(
let val ({result, name, envp, args, vararg}, i) = args_call
fun call (e, a, v) = {result=result, name=name, envp=e, args=a, vararg=v}
val i = i + 1
in
case arg'
of Arg a => (call(envp, a::args, vararg), i)
| Arge v => (call(SOME v, args, vararg), i)
| Argv => (call(envp, args, SOME i), i)
end
)
| tmp_opt CALL GLO (({result = tmp_opt,
name = GLO,
envp = NONE,
args = [],
vararg = NONE}, ~1))
arg' : ARGW value (Arg(T.W, value))
| ARGL value (Arg(T.L, value))
| ARGS value (Arg(T.S, value))
| ARGD value (Arg(T.D, value))
| ARGC TYP COMMA value (Arg(T.Agg TYP, value))
| ARGE value (Arge value)
| ARGV (Argv)
ins1 : NEG (T.Neg)
| LOAD (T.Load)
| LOADD (T.Loadd)
| LOADS (T.Loads)
| LOADL (T.Loadl)
| LOADW (T.Loadw)
| LOADSW (T.Loadsw)
| LOADUW (T.Loaduw)
| LOADSH (T.Loadsh)
| LOADUH (T.Loaduh)
| LOADSB (T.Loadsb)
| LOADUB (T.Loadub)
| ALLOC4 (T.Alloc4)
| ALLOC8 (T.Alloc8)
| ALLOC16 (T.Alloc16)
| DTOSI (T.Dtosi)
| DTOUI (T.Dtoui)
| EXTS (T.Exts)
| EXTSB (T.Extsb)
| EXTSH (T.Extsh)
| EXTSW (T.Extsw)
| EXTUB (T.Extub)
| EXTUH (T.Extuh)
| EXTUW (T.Extuw)
| SLTOF (T.Sltof)
| ULTOF (T.Ultof)
| STOSI (T.Stosi)
| STOUI (T.Stoui)
| SWTOF (T.Swtof)
| UWTOF (T.Uwtof)
| TRUNCD (T.Truncd)
| CAST (T.Cast)
| COPY (T.Copy)
| VAARG (T.Vaarg)
ins2 : ADD (T.Add)
| SUB (T.Sub)
| DIV (T.Div)
| MUL (T.Mul)
| UDIV (T.Udiv)
| REM (T.Rem)
| UREM (T.Urem)
| OR (T.Or)
| XOR (T.Xor)
| AND (T.And)
| SAR (T.Sar)
| SHR (T.Shr)
| SHL (T.Shl)
| CEQD (T.Ceqd)
| CEQL (T.Ceql)
| CEQS (T.Ceqs)
| CEQW (T.Ceqw)
| CGED (T.Cged)
| CGES (T.Cges)
| CGTD (T.Cgtd)
| CGTS (T.Cgts)
| CLED (T.Cled)
| CLES (T.Cles)
| CLTD (T.Cltd)
| CLTS (T.Clts)
| CNED (T.Cned)
| CNEL (T.Cnel)
| CNES (T.Cnes)
| CNEW (T.Cnew)
| COD (T.Cod)
| COS (T.Cos)
| CSGEL (T.Csgel)
| CSGEW (T.Csgew)
| CSGTL (T.Csgtl)
| CSGTW (T.Csgtw)
| CSLEL (T.Cslel)
| CSLEW (T.Cslew)
| CSLTL (T.Csltl)
| CSLTW (T.Csltw)
| CUGEL (T.Cugel)
| CUGEW (T.Cugew)
| CUGTL (T.Cugtl)
| CUGTW (T.Cugtw)
| CULEL (T.Culel)
| CULEW (T.Culew)
| CULTL (T.Cultl)
| CULTW (T.Cultw)
| CUOD (T.Cuod)
| CUOS (T.Cuos)
store : STORED (T.Stored)
| STORES (T.Stores)
| STOREL (T.Storel)
| STOREW (T.Storew)
| STOREH (T.Storeh)
| STOREB (T.Storeb)