forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintcmm.ml
466 lines (429 loc) · 16.3 KB
/
printcmm.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
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
466
(**************************************************************************)
(* *)
(* 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 C-- code *)
open Format
open Cmm
module V = Backend_var
module VP = Backend_var.With_provenance
open Location_tracker_formatter
let with_location_mapping ?label ~dbg ppf f =
with_location_mapping ?label ~loc:(Debuginfo.to_location dbg) ppf f
let rec_flag ppf = function
| Nonrecursive -> ()
| Recursive -> fprintf ppf " rec"
let machtype_component ppf (ty : machtype_component) =
match ty with
| Val -> fprintf ppf "val"
| Addr -> fprintf ppf "addr"
| Int -> fprintf ppf "int"
| Float -> fprintf ppf "float"
| Vec128 -> fprintf ppf "vec128"
| Float32 -> fprintf ppf "float32"
let machtype ppf mty =
match Array.length mty with
| 0 -> fprintf ppf "unit"
| n -> machtype_component ppf mty.(0);
for i = 1 to n-1 do
fprintf ppf "*%a" machtype_component mty.(i)
done
let exttype ppf = function
| XInt -> fprintf ppf "int"
| XInt32 -> fprintf ppf "int32"
| XInt64 -> fprintf ppf "int64"
| XFloat -> fprintf ppf "float"
| XFloat32 -> fprintf ppf "float32"
| XVec128 -> fprintf ppf "vec128"
let extcall_signature ppf (ty_res, ty_args) =
begin match ty_args with
| [] -> ()
| ty_arg1 :: ty_args ->
exttype ppf ty_arg1;
List.iter (fun ty -> fprintf ppf ",%a" exttype ty) ty_args
end;
begin match ty_res with
| None ->
fprintf ppf "->."
| Some ty_res ->
fprintf ppf "->%a" machtype ty_res
end
let is_global ppf = function
| Global -> fprintf ppf "G"
| Local -> fprintf ppf "L"
let symbol ppf s =
fprintf ppf "%a:\"%s\"" is_global s.sym_global s.sym_name
let integer_comparison = function
| Ceq -> "=="
| Cne -> "!="
| Clt -> "<"
| Cle -> "<="
| Cgt -> ">"
| Cge -> ">="
let float_comparison = function
| CFeq -> "=="
| CFneq -> "!="
| CFlt -> "<"
| CFnlt -> "!<"
| CFle -> "<="
| CFnle -> "!<="
| CFgt -> ">"
| CFngt -> "!>"
| CFge -> ">="
| CFnge -> "!>="
let vec128_name = function
| Int8x16 -> "int8x16"
| Int16x8 -> "int16x8"
| Int32x4 -> "int32x4"
| Int64x2 -> "int64x2"
| Float32x4 -> "float32x4"
| Float64x2 -> "float64x2"
let chunk = function
| Byte_unsigned -> "unsigned int8"
| Byte_signed -> "signed int8"
| Sixteen_unsigned -> "unsigned int16"
| Sixteen_signed -> "signed int16"
| Thirtytwo_unsigned -> "unsigned int32"
| Thirtytwo_signed -> "signed int32"
| Onetwentyeight_unaligned -> "unaligned vec128"
| Onetwentyeight_aligned -> "aligned vec128"
| Word_int -> "int"
| Word_val -> "val"
| Single { reg = Float64 } -> "float32_as_float64"
| Single { reg = Float32 } -> "float32"
| Double -> "float64"
let atomic_bitwidth : Cmm.atomic_bitwidth -> string = function
| Word -> "int"
| Thirtytwo -> "int32"
| Sixtyfour -> "int64"
let temporal_locality = function
| Nonlocal -> "nonlocal"
| Low -> "low"
| Moderate -> "moderate"
| High -> "high"
let atomic_op = function
| Fetch_and_add -> "fetch_and_add"
| Compare_and_swap -> "compare_and_swap"
let phantom_defining_expr ppf defining_expr =
match defining_expr with
| Cphantom_const_int i -> Targetint.print ppf i
| Cphantom_const_symbol sym -> Format.pp_print_string ppf sym
| Cphantom_var var -> V.print ppf var
| Cphantom_offset_var { var; offset_in_words; } ->
Format.fprintf ppf "%a+(%d)" V.print var offset_in_words
| Cphantom_read_field { var; field; } ->
Format.fprintf ppf "%a[%d]" V.print var field
| Cphantom_read_symbol_field { sym; field; } ->
Format.fprintf ppf "%s[%d]" sym field
| Cphantom_block { tag; fields; } ->
Format.fprintf ppf "[%d: " tag;
List.iter (fun field ->
Format.fprintf ppf "%a; " V.print field)
fields;
Format.fprintf ppf "]"
let phantom_defining_expr_opt ppf defining_expr =
match defining_expr with
| None -> Format.pp_print_string ppf "()"
| Some defining_expr -> phantom_defining_expr ppf defining_expr
let location d =
if not !Clflags.locations then ""
else Debuginfo.to_string d
let exit_label ppf = function
| Return_lbl -> fprintf ppf "*return*"
| Lbl lbl -> fprintf ppf "%d" lbl
let trap_action ppf ta =
match ta with
| Push i -> fprintf ppf "push(%d)" i
| Pop i -> fprintf ppf "pop(%d)" i
let trap_action_list ppf traps =
match traps with
| [] -> ()
| t :: rest ->
fprintf ppf "<%a" trap_action t;
List.iter (fun t -> fprintf ppf " %a" trap_action t) rest;
fprintf ppf ">"
let to_string msg =
let b = Buffer.create 17 in
let ppf = Format.formatter_of_buffer b in
Format.kfprintf (fun ppf ->
Format.pp_print_flush ppf ();
Buffer.contents b
) ppf msg
let reinterpret_cast : Cmm.reinterpret_cast -> string = function
| V128_of_v128 -> "vec128 as vec128"
| Value_of_int -> "int as value"
| Int_of_value -> "value as int"
| Float32_of_float -> "float as float32"
| Float_of_float32 -> "float32 as float"
| Float_of_int64 -> "int64 as float"
| Int64_of_float -> "float as int64"
| Float32_of_int32 -> "int32 as float32"
| Int32_of_float32 -> "float32 as int32"
let static_cast : Cmm.static_cast -> string = function
| Int_of_float Float64 -> "float->int"
| Float_of_int Float64 -> "int->float"
| Int_of_float Float32 -> "float32->int"
| Float_of_int Float32 -> "int->float32"
| Float32_of_float -> "float->float32"
| Float_of_float32 -> "float32->float"
| Scalar_of_v128 ty -> Printf.sprintf "%s->scalar" (vec128_name ty)
| V128_of_scalar ty -> Printf.sprintf "scalar->%s" (vec128_name ty)
let operation d = function
| Capply(_ty, _) -> "app" ^ location d
| Cextcall { func = lbl; _ } ->
Printf.sprintf "extcall \"%s\"%s" lbl (location d)
| Cload {memory_chunk; mutability} -> (
match mutability with
| Asttypes.Immutable -> Printf.sprintf "load %s" (chunk memory_chunk)
| Asttypes.Mutable -> Printf.sprintf "load_mut %s" (chunk memory_chunk))
| Calloc Alloc_mode.Heap -> "alloc" ^ location d
| Calloc Alloc_mode.Local -> "alloc_local" ^ location d
| Cstore (c, init) ->
let init =
match init with
| Initialization -> "(init)"
| Assignment -> ""
in
Printf.sprintf "store %s%s" (chunk c) init
| Caddi -> "+"
| Csubi -> "-"
| Cmuli -> "*"
| Cmulhi { signed } -> "*h"^(if signed then "" else "u")
| Cdivi -> "/"
| Cmodi -> "mod"
| Cand -> "and"
| Cor -> "or"
| Cxor -> "xor"
| Clsl -> "<<"
| Clsr -> ">>u"
| Casr -> ">>s"
| Cbswap { bitwidth = Sixteen } -> "bswap_16"
| Cbswap { bitwidth = Thirtytwo } -> "bswap_32"
| Cbswap { bitwidth = Sixtyfour } -> "bswap_64"
| Cclz { arg_is_non_zero; } -> Printf.sprintf "clz %B" arg_is_non_zero
| Cctz { arg_is_non_zero; } -> Printf.sprintf "ctz %B" arg_is_non_zero
| Cpopcnt -> "popcnt"
| Ccmpi c -> integer_comparison c
| Caddv -> "+v"
| Cadda -> "+a"
| Ccmpa c -> Printf.sprintf "%sa" (integer_comparison c)
| Cnegf Float64 -> "~f"
| Cabsf Float64 -> "absf"
| Caddf Float64 -> "+f"
| Csubf Float64 -> "-f"
| Cmulf Float64 -> "*f"
| Cdivf Float64 -> "/f"
| Cnegf Float32 -> "~f32"
| Cabsf Float32 -> "absf32"
| Caddf Float32 -> "+f32"
| Csubf Float32 -> "-f32"
| Cmulf Float32 -> "*f32"
| Cdivf Float32 -> "/f32"
| Cpackf32 -> "packf32"
| Ccsel ret_typ ->
to_string "csel %a" machtype ret_typ
| Creinterpret_cast cast -> reinterpret_cast cast
| Cstatic_cast cast -> static_cast cast
| Ccmpf (Float64, c) -> Printf.sprintf "%sf" (float_comparison c)
| Ccmpf (Float32, c) -> Printf.sprintf "%sf32" (float_comparison c)
| Craise k -> Lambda.raise_kind k ^ location d
| Cprobe { name; handler_code_sym; enabled_at_init; } ->
Printf.sprintf "probe[%s %s%s]" name handler_code_sym
(if enabled_at_init then " enabled_at_init" else "")
| Cprobe_is_enabled {name} -> Printf.sprintf "probe_is_enabled[%s]" name
| Cprefetch { is_write; locality; } ->
Printf.sprintf "prefetch is_write=%b prefetch_temporal_locality_hint=%s"
is_write (temporal_locality locality)
| Catomic { op; size = _ } -> Printf.sprintf "atomic %s" (atomic_op op)
| Copaque -> "opaque"
| Cbeginregion -> "beginregion"
| Cendregion -> "endregion"
| Ctuple_field (field, _ty) ->
to_string "tuple_field %i" field
| Cdls_get -> "dls_get"
| Cpoll -> "poll"
let rec expr ppf = function
| Cconst_int (n, _dbg) -> fprintf ppf "%i" n
| Cconst_natint (n, _dbg) ->
fprintf ppf "%s" (Nativeint.to_string n)
| Cconst_vec128 ({low; high}, _dbg) -> fprintf ppf "%016Lx:%016Lx" high low
| Cconst_float32 (n, _dbg) -> fprintf ppf "%Fs" n
| Cconst_float (n, _dbg) -> fprintf ppf "%F" n
| Cconst_symbol (s, _dbg) -> fprintf ppf "%a:\"%s\"" is_global s.sym_global s.sym_name
| Cvar id -> V.print ppf id
| Clet(id, def, (Clet(_, _, _) as body)) ->
let print_binding id ppf def =
fprintf ppf "@[<2>%a@ %a@]"
VP.print id expr def in
let rec in_part ppf = function
| Clet(id, def, body) ->
fprintf ppf "@ %a" (print_binding id) def;
in_part ppf body
| exp -> exp in
fprintf ppf "@[<2>(let@ @[<1>(%a" (print_binding id) def;
let exp = in_part ppf body in
fprintf ppf ")@]@ %a)@]" sequence exp
| Clet(id, def, body) ->
fprintf ppf
"@[<2>(let@ @[<2>%a@ %a@]@ %a)@]"
VP.print id expr def sequence body
| Clet_mut(id, kind, def, body) ->
fprintf ppf
"@[<2>(let_mut@ @[<2>%a: %a@ %a@]@ %a)@]"
VP.print id machtype kind expr def sequence body
| Cphantom_let(var, def, (Cphantom_let(_, _, _) as body)) ->
let print_binding var ppf def =
fprintf ppf "@[<2>%a@ %a@]" VP.print var
phantom_defining_expr_opt def
in
let rec in_part ppf = function
| Cphantom_let(var, def, body) ->
fprintf ppf "@ %a" (print_binding var) def;
in_part ppf body
| exp -> exp in
fprintf ppf "@[<2>(let?@ @[<1>(%a" (print_binding var) def;
let exp = in_part ppf body in
fprintf ppf ")@]@ %a)@]" sequence exp
| Cphantom_let(var, def, body) ->
fprintf ppf
"@[<2>(let?@ @[<2>%a@ %a@]@ %a)@]"
VP.print var
phantom_defining_expr_opt def
sequence body
| Cassign(id, exp) ->
fprintf ppf "@[<2>(assign @[<2>%a@ %a@])@]" V.print id expr exp
| Ctuple el ->
let tuple ppf el =
let first = ref true in
List.iter
(fun e ->
if !first then first := false else fprintf ppf "@ ";
expr ppf e)
el in
fprintf ppf "@[<1>[%a]@]" tuple el
| Cop(op, el, dbg) ->
with_location_mapping ~label:"Cop" ~dbg ppf (fun () ->
fprintf ppf "@[<2>(%s" (operation dbg op);
List.iter (fun e -> fprintf ppf "@ %a" expr e) el;
begin match op with
| Capply(mty, _) -> fprintf ppf "@ %a" machtype mty
| Cextcall { ty; ty_args; alloc = _; func = _; returns; } ->
let ty = if returns then Some ty else None in
fprintf ppf "@ %a" extcall_signature (ty, ty_args)
| _ -> ()
end;
fprintf ppf ")@]")
| Csequence(e1, e2) ->
fprintf ppf "@[<2>(seq@ %a@ %a)@]" sequence e1 sequence e2
| Cifthenelse(e1, e2_dbg, e2, e3_dbg, e3, dbg, _kind) ->
with_location_mapping ~label:"Cifthenelse-e1" ~dbg ppf (fun () ->
fprintf ppf "@[<2>(if@ %a@ " expr e1;
with_location_mapping ~label:"Cifthenelse-e2" ~dbg:e2_dbg ppf (fun () ->
fprintf ppf "%a@ " expr e2);
with_location_mapping ~label:"Cifthenelse-e3" ~dbg:e3_dbg ppf (fun () ->
fprintf ppf "%a" expr e3);
fprintf ppf ")@]")
| Cswitch(e1, index, cases, dbg, _kind) ->
with_location_mapping ~label:"Cswitch" ~dbg ppf (fun () ->
let print_case i ppf =
for j = 0 to Array.length index - 1 do
if index.(j) = i then fprintf ppf "case %i:" j
done in
let print_cases ppf =
for i = 0 to Array.length cases - 1 do
fprintf ppf "@ @[<2>%t@ %a@]" (print_case i) sequence (fst cases.(i))
done in
fprintf ppf "@[<v 0>@[<2>(switch@ %a@ @]%t)@]" expr e1 print_cases)
| Ccatch(flag, handlers, e1, _kind) ->
let print_handler ppf (i, ids, e2, dbg, is_cold) =
with_location_mapping ~label:"Ccatch-handler" ~dbg ppf (fun () ->
fprintf ppf "(%d%a)%s@ %a"
i
(fun ppf ids ->
List.iter
(fun (id, ty) ->
fprintf ppf "@ %a: %a"
VP.print id machtype ty)
ids) ids
(if is_cold then "(cold)" else "")
sequence e2)
in
let print_handlers ppf l =
List.iter (print_handler ppf) l
in
fprintf ppf
"@[<2>(catch%a@ %a@;<1 -2>with%a)@]"
rec_flag flag
sequence e1
print_handlers handlers
| Cexit (i, el, traps) ->
fprintf ppf "@[<2>(exit%a %a" trap_action_list traps exit_label i;
List.iter (fun e -> fprintf ppf "@ %a" expr e) el;
fprintf ppf ")@]"
| Ctrywith(e1, exn_cont, id, e2, dbg, _value_kind) ->
fprintf ppf "@[<2>(try@ %a@;<1 -2>with(%d)@ %a@ "
sequence e1 exn_cont VP.print id;
with_location_mapping ~label:"Ctrywith" ~dbg ppf (fun () ->
fprintf ppf "%a)@]" sequence e2);
and sequence ppf = function
| Csequence(e1, e2) -> fprintf ppf "%a@ %a" sequence e1 sequence e2
| e -> expression ppf e
and expression ppf e = fprintf ppf "%a" expr e
let codegen_option = function
| Reduce_code_size -> "reduce_code_size"
| No_CSE -> "no_cse"
| Use_linscan_regalloc -> "linscan"
| Assume_zero_alloc { strict; never_returns_normally; never_raises; loc = _ } ->
Printf.sprintf "assume_zero_alloc_%s%s%s"
(if strict then "_strict" else "")
(if never_returns_normally then "_never_returns_normally" else "")
(if never_raises then "_never_raises" else "")
| Check_zero_alloc { strict; loc = _ } ->
Printf.sprintf "assert_zero_alloc%s"
(if strict then "_strict" else "")
let print_codegen_options ppf l =
List.iter (fun c -> fprintf ppf " %s" (codegen_option c)) l
let fundecl ppf f =
let print_cases ppf cases =
let first = ref true in
List.iter
(fun (id, ty) ->
if !first then first := false else fprintf ppf "@ ";
fprintf ppf "%a: %a" VP.print id machtype ty)
cases in
with_location_mapping ~label:"Function" ~dbg:f.fun_dbg ppf (fun () ->
fprintf ppf "@[<1>(function%s%a@ %s@;<1 4>@[<1>(%a)@]@ @[%a@])@]@."
(location f.fun_dbg) print_codegen_options f.fun_codegen_options f.fun_name.sym_name
print_cases f.fun_args sequence f.fun_body)
let data_item ppf = function
| Cdefine_symbol {sym_name; sym_global = Local} -> fprintf ppf "\"%s\":" sym_name
| Cdefine_symbol {sym_name; sym_global = Global} -> fprintf ppf "global \"%s\":" sym_name
| Cint8 n -> fprintf ppf "byte %i" n
| Cint16 n -> fprintf ppf "int16 %i" n
| Cint32 n -> fprintf ppf "int32 %s" (Nativeint.to_string n)
| Cint n -> fprintf ppf "int %s" (Nativeint.to_string n)
| Csingle f -> fprintf ppf "single %F" f
| Cdouble f -> fprintf ppf "double %F" f
| Cvec128 {high; low} ->
fprintf ppf "vec128 %s:%s" (Int64.to_string high) (Int64.to_string low)
| Csymbol_address s -> fprintf ppf "addr %a:\"%s\"" is_global s.sym_global s.sym_name
| Csymbol_offset (s, o) -> fprintf ppf "addr %a:\"%s+%d\"" is_global s.sym_global s.sym_name o
| Cstring s -> fprintf ppf "string \"%s\"" s
| Cskip n -> fprintf ppf "skip %i" n
| Calign n -> fprintf ppf "align %i" n
let data ppf dl =
let items ppf = List.iter (fun d -> fprintf ppf "@ %a" data_item d) dl in
fprintf ppf "@[<hv 1>(data%t)@]" items
let phrase ppf = function
| Cfunction f -> fundecl ppf f
| Cdata dl -> data ppf dl