-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathprintmach.ml
405 lines (377 loc) · 14.2 KB
/
printmach.ml
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
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Pretty-printing of pseudo machine code *)
open Format
open Cmm
open Reg
open Mach
open Interval
module V = Backend_var
let loc ?(wrap_out = fun ppf f -> f ppf) ~unknown ppf loc typ =
match loc with
| Unknown -> unknown ppf
| Reg r ->
wrap_out ppf (fun ppf -> fprintf ppf "%s" (Proc.register_name typ r))
| Stack(Local s) ->
wrap_out ppf (fun ppf ->
fprintf ppf "s[%s:%i]" (Proc.stack_class_tag (Proc.stack_slot_class typ)) s)
| Stack(Incoming s) ->
wrap_out ppf (fun ppf -> fprintf ppf "par[%i]" s)
| Stack(Outgoing s) ->
wrap_out ppf (fun ppf -> fprintf ppf "arg[%i]" s)
| Stack(Domainstate s) ->
wrap_out ppf (fun ppf -> fprintf ppf "ds[%i]" s)
let reg ppf r =
if not (Reg.anonymous r) then
fprintf ppf "%s" (Reg.name r)
else
fprintf ppf "%s"
(match (r.typ : machtype_component) with
| Val -> "V"
| Addr -> "A"
| Int -> "I"
| Float -> "F"
| Vec128 -> "X"
| Float32 -> "S");
fprintf ppf "/%i" r.stamp;
loc
~wrap_out:(fun ppf f -> fprintf ppf "[%t]" f)
~unknown:(fun _ -> ()) ppf r.loc r.typ
let regs' ?(print_reg = reg) ppf v =
let reg = print_reg in
match Array.length v with
| 0 -> ()
| 1 -> reg ppf v.(0)
| n -> reg ppf v.(0);
for i = 1 to n-1 do fprintf ppf " %a" reg v.(i) done
let regs ppf v = regs' ppf v
let regset ppf s =
let first = ref true in
Reg.Set.iter
(fun r ->
if !first then begin first := false; fprintf ppf "%a" reg r end
else fprintf ppf "@ %a" reg r)
s
let regsetaddr' ?(print_reg = reg) ppf s =
let reg = print_reg in
let first = ref true in
Reg.Set.iter
(fun r ->
if !first then begin first := false; fprintf ppf "%a" reg r end
else fprintf ppf "@ %a" reg r;
match r.typ with
| Val -> fprintf ppf "*"
| Addr -> fprintf ppf "!"
| _ -> ())
s
let regsetaddr ppf s = regsetaddr' ppf s
let trap_stack ppf (ts : Mach.trap_stack) =
let has_specific = function
| Uncaught -> false
| Specific_trap _ -> true
in
if has_specific ts then begin
let rec p ppf = function
| Uncaught -> Format.fprintf ppf "U"
| Specific_trap (lbl, ts) -> Format.fprintf ppf "S%d:%a" lbl p ts
in
Format.fprintf ppf "<%a>" p ts
end else ()
let intcomp = function
| Isigned c -> Printf.sprintf " %ss " (Printcmm.integer_comparison c)
| Iunsigned c -> Printf.sprintf " %su " (Printcmm.integer_comparison c)
let is_unary_op = function
| Iclz _
| Ictz _
| Ipopcnt -> true
| Iadd | Isub | Imul | Imulh _ | Idiv | Imod
| Iand | Ior | Ixor | Ilsl | Ilsr | Iasr
| Icomp _
-> false
let intop = function
| Iadd -> " + "
| Isub -> " - "
| Imul -> " * "
| Imulh { signed } -> " *h "^(if signed then "" else "u")
| Idiv -> " div "
| Imod -> " mod "
| Iand -> " & "
| Ior -> " | "
| Ixor -> " ^ "
| Ilsl -> " << "
| Ilsr -> " >>u "
| Iasr -> " >>s "
| Iclz { arg_is_non_zero; } -> Printf.sprintf "clz %B " arg_is_non_zero
| Ictz { arg_is_non_zero; } -> Printf.sprintf "ctz %B " arg_is_non_zero
| Ipopcnt -> "popcnt "
| Icomp cmp -> intcomp cmp
let floatop ppf op =
match op with
| Iaddf -> fprintf ppf "+."
| Isubf -> fprintf ppf "-."
| Imulf -> fprintf ppf "*."
| Idivf -> fprintf ppf "/."
| Iabsf -> fprintf ppf "abs"
| Inegf -> fprintf ppf "neg"
| Icompf cmp -> fprintf ppf "%s" (Printcmm.float_comparison cmp)
let test' ?(print_reg = reg) tst ppf arg =
let reg = print_reg in
match tst with
| Itruetest -> reg ppf arg.(0)
| Ifalsetest -> fprintf ppf "not %a" reg arg.(0)
| Iinttest cmp -> fprintf ppf "%a%s%a" reg arg.(0) (intcomp cmp) reg arg.(1)
| Iinttest_imm(cmp, n) -> fprintf ppf "%a%s%i" reg arg.(0) (intcomp cmp) n
| Ifloattest (_, cmp) ->
fprintf ppf "%a %s %a"
reg arg.(0) (Printcmm.float_comparison cmp) reg arg.(1)
| Ieventest -> fprintf ppf "%a & 1 == 0" reg arg.(0)
| Ioddtest -> fprintf ppf "%a & 1 == 1" reg arg.(0)
let test tst ppf arg = test' tst ppf arg
let operation' ?(print_reg = reg) op arg ppf res =
let reg = print_reg in
let regs = regs' ~print_reg in
if Array.length res > 0 then fprintf ppf "%a := " regs res;
match op with
| Imove -> regs ppf arg
| Ispill -> fprintf ppf "%a (spill)" regs arg
| Ireload -> fprintf ppf "%a (reload)" regs arg
| Iconst_int n -> fprintf ppf "%s" (Nativeint.to_string n)
| Iconst_float32 f -> fprintf ppf "%Fs" (Int32.float_of_bits f)
| Iconst_float f -> fprintf ppf "%F" (Int64.float_of_bits f)
| Iconst_symbol s -> fprintf ppf "\"%s\"" s.sym_name
| Iconst_vec128 {high; low} -> fprintf ppf "%016Lx:%016Lx" high low
| Icall_ind -> fprintf ppf "call %a" regs arg
| Icall_imm { func; } -> fprintf ppf "call \"%s\" %a" func.sym_name regs arg
| Itailcall_ind -> fprintf ppf "tailcall %a" regs arg
| Itailcall_imm { func; } -> fprintf ppf "tailcall \"%s\" %a" func.sym_name regs arg
| Iextcall { func; alloc; _ } ->
fprintf ppf "extcall \"%s\" %a%s" func regs arg
(if alloc then "" else " (noalloc)")
| Istackoffset n ->
fprintf ppf "offset stack %i" n
| Iload { memory_chunk; addressing_mode; mutability=Immutable; is_atomic } ->
fprintf ppf "%s %a[%a]"
(Printcmm.chunk memory_chunk)
(fun pp a -> if a then fprintf pp "atomic" else ()) is_atomic
(Arch.print_addressing reg addressing_mode) arg
| Iload { memory_chunk; addressing_mode; mutability=Mutable; is_atomic } ->
fprintf ppf "%s %a mut[%a]"
(Printcmm.chunk memory_chunk)
(fun pp a -> if a then fprintf pp "atomic" else ()) is_atomic
(Arch.print_addressing reg addressing_mode) arg
| Istore(chunk, addr, is_assign) ->
fprintf ppf "%s[%a] := %a %s"
(Printcmm.chunk chunk)
(Arch.print_addressing reg addr)
(Array.sub arg 1 (Array.length arg - 1))
reg arg.(0)
(if is_assign then "(assign)" else "(init)")
| Ialloc { bytes = n; mode = Alloc_heap } ->
fprintf ppf "alloc %i" n;
| Ialloc { bytes = n; mode = Alloc_local } ->
fprintf ppf "alloc_local %i" n;
| Iintop(op) ->
if is_unary_op op then begin
assert (Array.length arg = 1);
fprintf ppf "%s%a" (intop op) reg arg.(0)
end else begin
assert (Array.length arg = 2);
fprintf ppf "%a%s%a" reg arg.(0) (intop op) reg arg.(1)
end
| Iintop_imm(op, n) -> fprintf ppf "%a%s%i" reg arg.(0) (intop op) n
| Iintop_atomic {op = Compare_and_swap; size; addr} ->
fprintf ppf "lock cas %s[%a] ?%a %a"
(Printcmm.atomic_bitwidth size)
(Arch.print_addressing reg addr) (Array.sub arg 2 (Array.length arg - 2))
reg arg.(0) reg arg.(1)
| Iintop_atomic {op = Fetch_and_add; size; addr} ->
fprintf ppf "lock %s[%a] += %a"
(Printcmm.atomic_bitwidth size)
(Arch.print_addressing reg addr) (Array.sub arg 1 (Array.length arg - 1))
reg arg.(0)
| Ifloatop (_, (Icompf _ | Iaddf | Isubf | Imulf | Idivf as op)) ->
fprintf ppf "%a %a %a" reg arg.(0) floatop op reg arg.(1)
| Ifloatop (_, (Inegf | Iabsf as op)) ->
fprintf ppf "%a %a" floatop op reg arg.(0)
| Icsel tst ->
let len = Array.length arg in
fprintf ppf "csel %a ? %a : %a"
(test tst) arg reg arg.(len-2) reg arg.(len-1)
| Ivalueofint -> fprintf ppf "valueofint %a" reg arg.(0)
| Iintofvalue -> fprintf ppf "intofvalue %a" reg arg.(0)
| Ivectorcast Bits128 ->
fprintf ppf "vec128->vec128 %a"
reg arg.(0)
| Iscalarcast (Float_of_int Float64) -> fprintf ppf "int->float %a" reg arg.(0)
| Iscalarcast (Float_to_int Float64) -> fprintf ppf "float->int %a" reg arg.(0)
| Iscalarcast (Float_of_int Float32) -> fprintf ppf "int->float32 %a" reg arg.(0)
| Iscalarcast (Float_to_int Float32) -> fprintf ppf "float32->int %a" reg arg.(0)
| Iscalarcast (Float_of_float32) -> fprintf ppf "float32->float %a" reg arg.(0)
| Iscalarcast (Float_to_float32) -> fprintf ppf "float->float32 %a" reg arg.(0)
| Iscalarcast (V128_of_scalar ty) ->
fprintf ppf "scalar->%s %a"
(Primitive.vec128_name ty) reg arg.(0)
| Iscalarcast (V128_to_scalar ty) ->
fprintf ppf "%s->scalar %a"
(Primitive.vec128_name ty) reg arg.(0)
| Iopaque -> fprintf ppf "opaque %a" reg arg.(0)
| Iname_for_debugger { ident; which_parameter; regs = r } ->
fprintf ppf "%a holds the value of %a%s"
regs r
V.print ident
(match which_parameter with
| None -> ""
| Some index -> sprintf "[P%d]" index)
| Ibeginregion -> fprintf ppf "beginregion"
| Iendregion -> fprintf ppf "endregion %a" reg arg.(0)
| Ispecific op ->
Arch.print_specific_operation reg op ppf arg
| Idls_get -> fprintf ppf "dls_get"
| Ipoll { return_label } ->
fprintf ppf "poll call";
(match return_label with
| None -> ()
| Some return_label ->
fprintf ppf " returning to L%d" return_label)
| Iprobe {name;handler_code_sym} ->
fprintf ppf "probe \"%s\" %s %a" name handler_code_sym regs arg
| Iprobe_is_enabled {name} -> fprintf ppf "probe_is_enabled \"%s\"" name
let operation op arg ppf res = operation' op arg ppf res
let rec instr ppf i =
if !Clflags.dump_live then begin
fprintf ppf "@[<1>{%a" regsetaddr i.live;
if Array.length i.arg > 0 then fprintf ppf "@ +@ %a" regs i.arg;
fprintf ppf "}@]@,"
end;
if !Flambda_backend_flags.davail then begin
let module RAS = Reg_availability_set in
let ras_is_nonempty (set : RAS.t) =
match set with
| Ok set -> not (Reg_with_debug_info.Set.is_empty set)
| Unreachable -> true
in
if ras_is_nonempty i.available_before
|| match i.available_across with
| None -> false
| Some available_across -> ras_is_nonempty available_across
then (
if Option.equal RAS.equal (Some i.available_before) i.available_across
then
fprintf ppf "@[<1>AB=AA={%a}@]@," (RAS.print ~print_reg:reg)
i.available_before
else (
fprintf ppf "@[<1>AB={%a}" (RAS.print ~print_reg:reg)
i.available_before;
begin match i.available_across with
| None -> ()
| Some available_across ->
fprintf ppf ",AA={%a}" (RAS.print ~print_reg:reg) available_across
end;
fprintf ppf "@]@,"
)
)
end;
begin match i.desc with
| Iend -> ()
| Iop op ->
operation op i.arg ppf i.res
| Ireturn traps ->
fprintf ppf "return%a %a" Printcmm.trap_action_list traps regs i.arg
| Iifthenelse(tst, ifso, ifnot) ->
fprintf ppf "@[<v 2>if %a then@,%a" (test tst) i.arg instr ifso;
begin match ifnot.desc with
| Iend -> ()
| _ -> fprintf ppf "@;<0 -2>else@,%a" instr ifnot
end;
fprintf ppf "@;<0 -2>endif@]"
| Iswitch(index, cases) ->
fprintf ppf "switch %a" reg i.arg.(0);
for i = 0 to Array.length cases - 1 do
fprintf ppf "@,@[<v 2>@[";
for j = 0 to Array.length index - 1 do
if index.(j) = i then fprintf ppf "case %i:@," j
done;
fprintf ppf "@]@,%a@]" instr cases.(i)
done;
fprintf ppf "@,endswitch"
| Icatch(flag, ts, handlers, body) ->
fprintf ppf "@[<v 2>catch%a%a@,%a@;<0 -2>with"
Printcmm.rec_flag flag trap_stack ts instr body;
let h (nfail, ts, handler, is_cold) =
fprintf ppf "(%d)%s%a@,%a@;" nfail (if is_cold then "(cold)" else "") trap_stack ts instr handler in
let rec aux = function
| [] -> ()
| [v] -> h v
| v :: t ->
h v;
fprintf ppf "@ and";
aux t
in
aux handlers;
fprintf ppf "@;<0 -2>endcatch@]"
| Iexit (i, traps) ->
fprintf ppf "exit%a(%d)" Printcmm.trap_action_list traps i
| Itrywith(body, exn_cont, (ts, handler)) ->
fprintf ppf "@[<v 2>try@,%a@;<0 -2>with(%d)%a@,%a@;<0 -2>endtry@]"
instr body exn_cont trap_stack ts instr handler
| Iraise k ->
fprintf ppf "%s %a" (Lambda.raise_kind k) reg i.arg.(0)
end;
if not (Debuginfo.is_none i.dbg) && !Clflags.locations then
fprintf ppf "%s" (Debuginfo.to_string i.dbg);
begin match i.next.desc with
Iend -> ()
| _ -> fprintf ppf "@,%a" instr i.next
end
let fundecl ppf f =
let dbg =
if Debuginfo.is_none f.fun_dbg || not !Clflags.locations then
""
else
" " ^ Debuginfo.to_string f.fun_dbg in
fprintf ppf "@[<v 2>%s(%a)%s%a@,%a@]"
f.fun_name regs f.fun_args dbg
Printcmm.print_codegen_options f.fun_codegen_options
instr f.fun_body
let phase msg ppf f =
fprintf ppf "*** %s@.%a@." msg fundecl f
let interference ppf r =
let interf ppf =
List.iter
(fun r -> fprintf ppf "@ %a" reg r)
r.interf in
fprintf ppf "@[<2>%a:%t@]@." reg r interf
let interferences ppf () =
fprintf ppf "*** Interferences@.";
List.iter (interference ppf) (Reg.all_registers())
let interval ppf i =
let interv ppf =
List.iter
(fun r -> fprintf ppf "@ [%d;%d]" r.rbegin r.rend)
i.ranges in
fprintf ppf "@[<2>%a:%t@]@." reg i.reg interv
let intervals ppf () =
fprintf ppf "*** Intervals@.";
List.iter (interval ppf) (Interval.all_fixed_intervals());
List.iter (interval ppf) (Interval.all_intervals())
let preference ppf r =
let prefs ppf =
List.iter
(fun (r, w) -> fprintf ppf "@ %a weight %i" reg r w)
r.prefer in
fprintf ppf "@[<2>%a: %t@]@." reg r prefs
let preferences ppf () =
fprintf ppf "*** Preferences@.";
List.iter (preference ppf) (Reg.all_registers())