File: amd64Peephole.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (364 lines) | stat: -rw-r--r-- 20,372 bytes parent folder | download
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
(* WARNING: this is generated by running 'nowhere amd64Peephole.peep'.
 * Do not edit this file directly.
 * Version 1.2.2
 *)

(*#line 20.1 "amd64Peephole.peep"*)
functor AMD64Peephole(
(*#line 21.5 "amd64Peephole.peep"*)
                      structure Instr : AMD64INSTR

(*#line 22.5 "amd64Peephole.peep"*)
                      structure Eval : MLTREE_EVAL

(*#line 23.7 "amd64Peephole.peep"*)
                      (* sharing Instr.T = Eval.T *)
                      where type T.Basis.cond = Instr.T.Basis.cond
                       and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
                       and type T.Basis.ext = Instr.T.Basis.ext
                       and type T.Basis.fcond = Instr.T.Basis.fcond
                       and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
                       and type T.Constant.const = Instr.T.Constant.const
                       and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
                       and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
                       and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
                       and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
                       and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
                       and type T.Region.region = Instr.T.Region.region
                       and type T.ccexp = Instr.T.ccexp
                       and type T.fexp = Instr.T.fexp
                       (* and type T.labexp = Instr.T.labexp *)
                       and type T.mlrisc = Instr.T.mlrisc
                       and type T.oper = Instr.T.oper
                       and type T.rep = Instr.T.rep
                       and type T.rexp = Instr.T.rexp
                       and type T.stm = Instr.T.stm
                     ): PEEPHOLE =
struct

(*#line 26.4 "amd64Peephole.peep"*)
   structure I = Instr

(*#line 27.4 "amd64Peephole.peep"*)
   structure C = I.C

(*#line 28.4 "amd64Peephole.peep"*)
   structure CBase = CellsBasis

(*#line 31.4 "amd64Peephole.peep"*)
   fun peephole instrs = 
       let 
(*#line 32.8 "amd64Peephole.peep"*)
           fun isStackPtr (I.Direct(_, r)) = CBase.sameColor (r, C.rsp)
             | isStackPtr _ = false

(*#line 35.8 "amd64Peephole.peep"*)
           fun isZeroLE le = (((Eval.valueOf le) = 0) handle _ => false
)

(*#line 37.8 "amd64Peephole.peep"*)
           fun isZero (I.Immed n) = n = 0
             | isZero (I.ImmedLabel le) = isZeroLE le
             | isZero _ = false

(*#line 41.8 "amd64Peephole.peep"*)
           fun isZeroOpt NONE = true
             | isZeroOpt (SOME opn) = isZero opn

(*#line 44.8 "amd64Peephole.peep"*)
           fun loop (code, instrs) = 
               let val v_39 = code
                   fun state_10 (v_0, v_3) = 
                       let val i = v_0
                           and rest = v_3
                       in loop (rest, i :: instrs)
                       end
                   fun state_12 (v_0, v_19, v_3) = 
                       let val le = v_19
                           and rest = v_3
                       in (if (isZeroLE le)
                             then (loop (rest, instrs))
                             else (state_10 (v_0, v_3)))
                       end
                   fun state_67 (v_0, v_1, v_2, v_3) = 
                       (case v_1 of
                         I.Direct v_35 => 
                         let val dst = v_1
                             and rest = v_3
                             and src = v_2
                         in (if (isZero src)
                               then (loop (rest, (I.binary {binOp=I.XORQ, src=dst, dst=dst}) :: instrs))
                               else (state_10 (v_0, v_3)))
                         end
                       | _ => state_10 (v_0, v_3)
                       )
                   fun state_13 (v_0, v_2, v_3) = 
                       (case v_2 of
                         I.ImmedLabel v_19 => state_12 (v_0, v_19, v_3)
                       | _ => state_10 (v_0, v_3)
                       )
               in 
                  (case v_39 of
                    op :: v_38 => 
                    let val (v_0, v_3) = v_38
                    in 
                       (case v_0 of
                         I.INSTR v_37 => 
                         (case v_37 of
                           I.BINARY v_21 => 
                           let val {binOp=v_36, dst=v_1, src=v_2, ...} = v_21
                           in 
                              (case v_36 of
                                I.ADDL => state_13 (v_0, v_2, v_3)
                              | I.ADDQ => 
                                (case v_2 of
                                  I.Immed v_19 => 
                                  (case v_1 of
                                    I.Direct v_35 => 
                                    let val (v_34, v_29) = v_35
                                    in 
                                       (case v_3 of
                                         op :: v_16 => 
                                         let val (v_15, v_4) = v_16
                                         in 
                                            (case v_15 of
                                              I.INSTR v_14 => 
                                              (case v_14 of
                                                I.BINARY v_13 => 
                                                let val {binOp=v_12, dst=v_11, src=v_8, ...} = v_13
                                                in 
                                                   (case v_12 of
                                                     I.SUBQ => 
                                                     (case v_11 of
                                                       I.Direct v_10 => 
                                                       let val (v_9, v_5) = v_10
                                                       in 
                                                          (case v_8 of
                                                            I.Immed v_7 => 
                                                            let val d_i = v_29
                                                                and d_j = v_5
                                                                and m = v_7
                                                                and n = v_19
                                                                and rest = v_4
                                                                and ty = v_9
                                                            in (if ((CBase.sameColor (d_i, C.rsp)) andalso (CBase.sameColor (d_j, C.rsp)))
                                                                  then (if (m = n)
                                                                     then (loop (rest, instrs))
                                                                     else (if (m < n)
                                                                        then (loop (rest, (I.binary {binOp=I.ADDQ, src=I.Immed (n - m), dst=I.Direct (ty, C.rsp)}) :: instrs))
                                                                        else (loop (rest, (I.binary {binOp=I.SUBQ, src=I.Immed (m - n), dst=I.Direct (ty, C.rsp)}) :: instrs))))
                                                                  else (state_10 (v_0, v_3)))
                                                            end
                                                          | _ => state_10 (v_0, v_3)
                                                          )
                                                       end
                                                     | _ => state_10 (v_0, v_3)
                                                     )
                                                   | _ => state_10 (v_0, v_3)
                                                   )
                                                end
                                              | _ => state_10 (v_0, v_3)
                                              )
                                            | _ => state_10 (v_0, v_3)
                                            )
                                         end
                                       | nil => state_10 (v_0, v_3)
                                       )
                                    end
                                  | _ => state_10 (v_0, v_3)
                                  )
                                | I.ImmedLabel v_19 => state_12 (v_0, v_19, v_3)
                                | _ => state_10 (v_0, v_3)
                                )
                              | I.SUBL => state_13 (v_0, v_2, v_3)
                              | I.SUBQ => 
                                (case v_2 of
                                  I.Immed v_19 => 
                                  (case v_1 of
                                    I.Direct v_35 => 
                                    let val (v_34, v_29) = v_35
                                    in 
                                       (case v_19 of
                                         4 => 
                                         (case v_3 of
                                           op :: v_16 => 
                                           let val (v_15, v_4) = v_16
                                           in 
                                              (case v_15 of
                                                I.INSTR v_14 => 
                                                (case v_14 of
                                                  I.MOVE v_13 => 
                                                  let val {dst=v_11, mvOp=v_31, src=v_8, ...} = v_13
                                                  in 
                                                     (case v_11 of
                                                       I.Displace v_10 => 
                                                       let val {base=v_30, disp=v_33, ...} = v_10
                                                       in 
                                                          (case v_33 of
                                                            I.Immed v_32 => 
                                                            (case v_32 of
                                                              0 => 
                                                              (case v_31 of
                                                                I.MOVQ => 
                                                                let val base = v_30
                                                                    and dst_i = v_29
                                                                    and rest = v_4
                                                                    and src = v_8
                                                                in (if (((CBase.sameColor (base, C.rsp)) andalso (CBase.sameColor (dst_i, C.rsp))) andalso (not (isStackPtr src)))
                                                                      then (loop (rest, (I.push src) :: instrs))
                                                                      else (state_10 (v_0, v_3)))
                                                                end
                                                              | _ => state_10 (v_0, v_3)
                                                              )
                                                            | _ => state_10 (v_0, v_3)
                                                            )
                                                          | _ => state_10 (v_0, v_3)
                                                          )
                                                       end
                                                     | _ => state_10 (v_0, v_3)
                                                     )
                                                  end
                                                | _ => state_10 (v_0, v_3)
                                                )
                                              | _ => state_10 (v_0, v_3)
                                              )
                                           end
                                         | nil => state_10 (v_0, v_3)
                                         )
                                       | _ => state_10 (v_0, v_3)
                                       )
                                    end
                                  | _ => state_10 (v_0, v_3)
                                  )
                                | I.ImmedLabel v_19 => state_12 (v_0, v_19, v_3)
                                | _ => state_10 (v_0, v_3)
                                )
                              | _ => state_10 (v_0, v_3)
                              )
                           end
                         | I.LEAL v_21 => 
                           let val {addr=v_27, r32=v_28, ...} = v_21
                           in 
                              (case v_27 of
                                I.Displace v_26 => 
                                let val {base=v_24, disp=v_25, ...} = v_26
                                in 
                                   (case v_25 of
                                     I.ImmedLabel v_23 => 
                                     let val base = v_24
                                         and le = v_23
                                         and r32 = v_28
                                         and rest = v_3
                                     in (if ((isZeroLE le) andalso (CBase.sameColor (r32, base)))
                                           then (loop (rest, instrs))
                                           else (state_10 (v_0, v_3)))
                                     end
                                   | _ => state_10 (v_0, v_3)
                                   )
                                end
                              | _ => state_10 (v_0, v_3)
                              )
                           end
                         | I.LEAQ v_21 => 
                           let val {addr=v_27, r64=v_22, ...} = v_21
                           in 
                              (case v_27 of
                                I.Displace v_26 => 
                                let val {base=v_24, disp=v_25, ...} = v_26
                                in 
                                   (case v_25 of
                                     I.ImmedLabel v_23 => 
                                     let val base = v_24
                                         and le = v_23
                                         and r64 = v_22
                                         and rest = v_3
                                     in (if ((isZeroLE le) andalso (CBase.sameColor (r64, base)))
                                           then (loop (rest, instrs))
                                           else (state_10 (v_0, v_3)))
                                     end
                                   | _ => state_10 (v_0, v_3)
                                   )
                                end
                              | _ => state_10 (v_0, v_3)
                              )
                           end
                         | I.MOVE v_21 => 
                           let val {dst=v_1, mvOp=v_20, src=v_2, ...} = v_21
                           in 
                              (case v_20 of
                                I.MOVQ => 
                                (case v_2 of
                                  I.Displace v_19 => 
                                  let val {base=v_6, disp=v_18, ...} = v_19
                                  in 
                                     (case v_18 of
                                       I.Immed v_17 => 
                                       (case v_17 of
                                         0 => 
                                         (case v_3 of
                                           op :: v_16 => 
                                           let val (v_15, v_4) = v_16
                                           in 
                                              (case v_15 of
                                                I.INSTR v_14 => 
                                                (case v_14 of
                                                  I.BINARY v_13 => 
                                                  let val {binOp=v_12, dst=v_11, src=v_8, ...} = v_13
                                                  in 
                                                     (case v_12 of
                                                       I.ADDQ => 
                                                       (case v_11 of
                                                         I.Direct v_10 => 
                                                         let val (v_9, v_5) = v_10
                                                         in 
                                                            (case v_8 of
                                                              I.Immed v_7 => 
                                                              (case v_7 of
                                                                4 => 
                                                                let val base = v_6
                                                                    and dst = v_1
                                                                    and dst_i = v_5
                                                                    and rest = v_4
                                                                in (if (((CBase.sameColor (base, C.rsp)) andalso (CBase.sameColor (dst_i, C.rsp))) andalso (not (isStackPtr dst)))
                                                                      then (loop (rest, (I.pop dst) :: instrs))
                                                                      else (state_67 (v_0, v_1, v_2, v_3)))
                                                                end
                                                              | _ => state_67 (v_0, v_1, v_2, v_3)
                                                              )
                                                            | _ => state_67 (v_0, v_1, v_2, v_3)
                                                            )
                                                         end
                                                       | _ => state_67 (v_0, v_1, v_2, v_3)
                                                       )
                                                     | _ => state_67 (v_0, v_1, v_2, v_3)
                                                     )
                                                  end
                                                | _ => state_67 (v_0, v_1, v_2, v_3)
                                                )
                                              | _ => state_67 (v_0, v_1, v_2, v_3)
                                              )
                                           end
                                         | nil => state_67 (v_0, v_1, v_2, v_3)
                                         )
                                       | _ => state_67 (v_0, v_1, v_2, v_3)
                                       )
                                     | _ => state_67 (v_0, v_1, v_2, v_3)
                                     )
                                  end
                                | _ => state_67 (v_0, v_1, v_2, v_3)
                                )
                              | _ => state_10 (v_0, v_3)
                              )
                           end
                         | _ => state_10 (v_0, v_3)
                         )
                       | _ => state_10 (v_0, v_3)
                       )
                    end
                  | nil => instrs
                  )
               end
       in loop (instrs, [])
       end
end