-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathemitaux.ml
596 lines (532 loc) · 19.2 KB
/
emitaux.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
(**************************************************************************)
(* *)
(* 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. *)
(* *)
(**************************************************************************)
(* Common functions for emitting assembly code *)
type error =
| Stack_frame_too_large of int
| Stack_frame_way_too_large of int
| Inconsistent_probe_init of string * Debuginfo.t
exception Error of error
let output_channel = ref stdout
let emit_string s = output_string !output_channel s
let emit_int n = output_string !output_channel (Int.to_string n)
let emit_char c = output_char !output_channel c
let emit_nativeint n = output_string !output_channel (Nativeint.to_string n)
let emit_printf fmt =
Printf.fprintf !output_channel fmt
let emit_int32 n = emit_printf "0x%lx" n
let emit_symbol s =
for i = 0 to String.length s - 1 do
let c = s.[i] in
match c with
'A'..'Z' | 'a'..'z' | '0'..'9' | '_' | '.' ->
output_char !output_channel c
| _ ->
Printf.fprintf !output_channel "$%02x" (Char.code c)
done
let emit_string_literal s =
let last_was_escape = ref false in
emit_string "\"";
for i = 0 to String.length s - 1 do
let c = s.[i] in
if c >= '0' && c <= '9' then
if !last_was_escape
then Printf.fprintf !output_channel "\\%o" (Char.code c)
else output_char !output_channel c
else if c >= ' ' && c <= '~' && c <> '"' (* '"' *) && c <> '\\' then begin
output_char !output_channel c;
last_was_escape := false
end else begin
Printf.fprintf !output_channel "\\%o" (Char.code c);
last_was_escape := true
end
done;
emit_string "\""
let emit_string_directive directive s =
let l = String.length s in
if l = 0 then ()
else if l < 80 then begin
emit_string directive;
emit_string_literal s;
emit_char '\n'
end else begin
let i = ref 0 in
while !i < l do
let n = min (l - !i) 80 in
emit_string directive;
emit_string_literal (String.sub s !i n);
emit_char '\n';
i := !i + n
done
end
let emit_bytes_directive directive s =
let pos = ref 0 in
for i = 0 to String.length s - 1 do
if !pos = 0
then emit_string directive
else emit_char ',';
emit_int(Char.code s.[i]);
incr pos;
if !pos >= 16 then begin emit_char '\n'; pos := 0 end
done;
if !pos > 0 then emit_char '\n'
let emit_float64_directive directive x =
emit_printf "\t%s\t0x%Lx\n" directive x
let emit_float64_split_directive directive x =
let lo = Int64.logand x 0xFFFF_FFFFL
and hi = Int64.shift_right_logical x 32 in
emit_printf "\t%s\t0x%Lx, 0x%Lx\n"
directive
(if Arch.big_endian then hi else lo)
(if Arch.big_endian then lo else hi)
let emit_float32_directive directive x =
emit_printf "\t%s\t0x%lx\n" directive x
(* Record live pointers at call points *)
type frame_debuginfo =
| Dbg_alloc of Debuginfo.alloc_dbginfo
| Dbg_raise of Debuginfo.t
| Dbg_other of Debuginfo.t
type frame_descr =
{ fd_lbl: int; (* Return address *)
fd_frame_size: int; (* Size of stack frame *)
fd_live_offset: int list; (* Offsets/regs of live addresses *)
fd_debuginfo: frame_debuginfo; (* Location, if any *)
fd_long: bool; (* Use 32 instead of 16 bit format. *)
}
let frame_descriptors = ref([] : frame_descr list)
let is_none_dbg d = Debuginfo.Dbg.is_none (Debuginfo.get_dbg d)
let get_flags debuginfo =
match debuginfo with
| Dbg_other d | Dbg_raise d ->
if is_none_dbg d then 0 else 1
| Dbg_alloc dbgs ->
if !Clflags.debug &&
List.exists (fun d ->
not (is_none_dbg d.Debuginfo.alloc_dbg)) dbgs
then 3 else 2
let is_long n =
assert (n >= 0);
(* Long frames must fit in 32-bit integer and
not truncated upon conversion from int on any target. *)
if n > 0x3FFF_FFFF then
raise (Error(Stack_frame_way_too_large n));
n >= !Flambda_backend_flags.long_frames_threshold
let record_frame_descr ~label ~frame_size ~live_offset debuginfo =
assert (frame_size land 3 = 0);
let fd_long =
is_long (frame_size + get_flags debuginfo) ||
(* The checks below are redundant
(if they fail, then frame size check above should have failed),
but they make the safety of [emit_frame] clear. *)
is_long (List.length live_offset) ||
(List.exists is_long live_offset)
in
if fd_long && not !Flambda_backend_flags.allow_long_frames then
raise (Error(Stack_frame_too_large frame_size));
frame_descriptors := { fd_lbl = label;
fd_frame_size = frame_size;
fd_live_offset = List.sort_uniq (-) live_offset;
fd_debuginfo = debuginfo;
fd_long;
} :: !frame_descriptors
type emit_frame_actions =
{ efa_code_label: int -> unit;
efa_data_label: int -> unit;
efa_8: int -> unit;
efa_16: int -> unit;
efa_32: int32 -> unit;
efa_word: int -> unit;
efa_align: int -> unit;
efa_label_rel: int -> int32 -> unit;
efa_def_label: int -> unit;
efa_string: string -> unit }
let emit_frames a =
let filenames = Hashtbl.create 7 in
let label_filename name =
try
Hashtbl.find filenames name
with Not_found ->
let lbl = Cmm.new_label () in
Hashtbl.add filenames name lbl;
lbl
in
let defnames = Hashtbl.create 7 in
let label_defname filename defname =
try
snd (Hashtbl.find defnames (filename, defname))
with Not_found ->
let file_lbl = label_filename filename in
let def_lbl = Cmm.new_label () in
Hashtbl.add defnames (filename, defname) (file_lbl, def_lbl);
def_lbl
in
let module Label_table =
Hashtbl.Make (struct
type t = bool * Debuginfo.Dbg.t
let equal ((rs1 : bool), dbg1) (rs2, dbg2) =
rs1 = rs2 && Debuginfo.Dbg.compare dbg1 dbg2 = 0
let hash (rs, dbg) =
Hashtbl.hash (rs, Debuginfo.Dbg.hash dbg)
end)
in
let debuginfos = Label_table.create 7 in
let label_debuginfos rs dbg =
let dbg = Debuginfo.get_dbg dbg in
let key = (rs, dbg) in
try Label_table.find debuginfos key
with Not_found ->
let lbl = Cmm.new_label () in
Label_table.add debuginfos key lbl;
lbl
in
let emit_32 n = n |> Int32.of_int |> a.efa_32 in
let emit_frame fd =
let flags = get_flags fd.fd_debuginfo in
a.efa_label_rel fd.fd_lbl 0l;
(* For short format, the size is guaranteed
to be less than the constant below. *)
if fd.fd_long then begin
a.efa_16 Flambda_backend_flags.max_long_frames_threshold;
a.efa_align 4;
end;
let emit_16_or_32 =
if fd.fd_long then emit_32 else a.efa_16
in
emit_16_or_32 (fd.fd_frame_size + flags);
emit_16_or_32 (List.length fd.fd_live_offset);
List.iter emit_16_or_32 fd.fd_live_offset;
begin match fd.fd_debuginfo with
| _ when flags = 0 ->
()
| Dbg_other dbg ->
a.efa_align 4;
a.efa_label_rel (label_debuginfos false dbg) Int32.zero
| Dbg_raise dbg ->
a.efa_align 4;
a.efa_label_rel (label_debuginfos true dbg) Int32.zero
| Dbg_alloc dbg ->
assert (List.length dbg < 256);
a.efa_8 (List.length dbg);
List.iter (fun Debuginfo.{alloc_words;_} ->
(* Possible allocations range between 2 and 257 *)
assert (2 <= alloc_words &&
alloc_words - 1 <= Config.max_young_wosize &&
Config.max_young_wosize <= 256);
a.efa_8 (alloc_words - 2)) dbg;
if flags = 3 then begin
a.efa_align 4;
List.iter (fun Debuginfo.{alloc_dbg; _} ->
if is_none_dbg alloc_dbg then
a.efa_32 Int32.zero
else
a.efa_label_rel (label_debuginfos false alloc_dbg) Int32.zero) dbg
end
end;
a.efa_align Arch.size_addr
in
let emit_filename name lbl =
a.efa_def_label lbl;
a.efa_string name
in
let emit_defname (_filename, defname) (file_lbl, lbl) =
(* These must be 32-bit aligned, both because they contain a
32-bit value, and because emit_debuginfo assumes the low 2 bits
of their addresses are 0. *)
a.efa_align 4;
a.efa_def_label lbl;
a.efa_label_rel file_lbl 0l;
a.efa_string defname
in
let pack_info fd_raise d has_next =
let line = min 0xFFFFF d.Debuginfo.dinfo_line
and char_start = min 0xFF d.Debuginfo.dinfo_char_start
and char_end = min 0x3FF d.Debuginfo.dinfo_char_end
and kind = if fd_raise then 1 else 0
and has_next = if has_next then 1 else 0 in
Int64.(add (shift_left (of_int line) 44)
(add (shift_left (of_int char_start) 36)
(add (shift_left (of_int char_end) 26)
(add (shift_left (of_int kind) 1)
(of_int has_next)))))
in
let emit_debuginfo (rs, dbg) lbl =
let rdbg = dbg |> Debuginfo.Dbg.to_list |> List.rev in
(* Due to inlined functions, a single debuginfo may have multiple locations.
These are represented sequentially in memory (innermost frame first),
with the low bit of the packed debuginfo being 0 on the last entry. *)
a.efa_align 4;
a.efa_def_label lbl;
let rec emit rs d rest =
let open Debuginfo in
let info = pack_info rs d (rest <> []) in
let defname = Scoped_location.string_of_scopes d.dinfo_scopes in
a.efa_label_rel
(label_defname d.dinfo_file defname)
(Int64.to_int32 info);
a.efa_32 (Int64.to_int32 (Int64.shift_right info 32));
match rest with
| [] -> ()
| d :: rest -> emit false d rest in
match rdbg with
| [] -> assert false
| d :: rest -> emit rs d rest in
a.efa_word (List.length !frame_descriptors);
List.iter emit_frame !frame_descriptors;
Label_table.iter emit_debuginfo debuginfos;
Hashtbl.iter emit_filename filenames;
Hashtbl.iter emit_defname defnames;
a.efa_align Arch.size_addr;
frame_descriptors := []
(* Detection of functions that can be duplicated between a DLL and
the main program (PR#4690) *)
let isprefix s1 s2 =
String.length s1 <= String.length s2
&& String.sub s2 0 (String.length s1) = s1
let is_generic_function name =
List.exists
(fun p -> isprefix p name)
["caml_apply"; "caml_curry"; "caml_send"; "caml_tuplify"]
(* CFI directives *)
let is_cfi_enabled () =
Config.asm_cfi_supported
let cfi_startproc () =
if is_cfi_enabled () then
emit_string "\t.cfi_startproc\n"
let cfi_endproc () =
if is_cfi_enabled () then
emit_string "\t.cfi_endproc\n"
let cfi_remember_state () =
if is_cfi_enabled () then
emit_string "\t.cfi_remember_state\n"
let cfi_restore_state () =
if is_cfi_enabled () then
emit_string "\t.cfi_restore_state\n"
let cfi_adjust_cfa_offset n =
if is_cfi_enabled () then
begin
emit_string "\t.cfi_adjust_cfa_offset\t"; emit_int n; emit_string "\n";
end
let cfi_def_cfa_offset n =
if is_cfi_enabled () then begin
emit_string "\t.cfi_def_cfa_offset\t"; emit_int n; emit_string "\n";
end
let cfi_offset ~reg ~offset =
if is_cfi_enabled () then begin
emit_string "\t.cfi_offset ";
emit_int reg;
emit_string ", ";
emit_int offset;
emit_string "\n"
end
let cfi_def_cfa_register ~reg =
if is_cfi_enabled () then begin
emit_string "\t.cfi_def_cfa_register ";
emit_int reg;
emit_string "\n"
end
(* Emit debug information *)
(* This assoc list is expected to be very short *)
let file_pos_nums =
(ref [] : (string * int) list ref)
(* Number of files *)
let file_pos_num_cnt = ref 1
(* Reset debug state at beginning of asm file *)
let reset_debug_info () =
file_pos_nums := [];
file_pos_num_cnt := 1
let get_file_num ~file_emitter file_name =
try List.assoc file_name !file_pos_nums
with Not_found ->
let file_num = !file_pos_num_cnt in
incr file_pos_num_cnt;
file_emitter ~file_num ~file_name;
file_pos_nums := (file_name,file_num) :: !file_pos_nums;
file_num
(* We only display .file if the file has not been seen before. We
display .loc for every instruction. *)
let emit_debug_info_gen ?discriminator dbg file_emitter loc_emitter =
let dbg = Debuginfo.Dbg.to_list (Debuginfo.get_dbg dbg) in
if is_cfi_enabled () &&
(!Clflags.debug || Config.with_frame_pointers) then begin
match List.rev dbg with
| [] -> ()
| { Debuginfo.dinfo_line = line;
dinfo_char_start = col;
dinfo_file = file_name; } :: _ ->
if line > 0 then begin (* PR#6243 *)
let file_num = get_file_num ~file_emitter file_name in
loc_emitter ~file_num ~line ~col ?discriminator ()
end
end
let emit_debug_info ?discriminator dbg =
ignore discriminator;
emit_debug_info_gen dbg (fun ~file_num ~file_name ->
emit_string "\t.file\t";
emit_int file_num; emit_char '\t';
emit_string_literal file_name; emit_char '\n';
)
(fun ~file_num ~line ~col:_ ?discriminator () ->
emit_string "\t.loc\t";
emit_int file_num; emit_char '\t';
emit_int line; emit_char '\t';
begin match discriminator with
| None -> ()
| Some k -> emit_string "discriminator "; emit_int k
end;
emit_char '\n')
let reset () =
reset_debug_info ();
frame_descriptors := []
let binary_backend_available = ref false
let reduce_heap_size ~reset =
let _minor, _promoted, major_words = Gc.counters () in
(* Uses [major_words] because it doesn't require a heap traversal to compute and
for this workload a majority of major words are live at this point. *)
let heap_reduction_threshold =
if !Flambda_backend_flags.heap_reduction_threshold >= 0 then
float !Flambda_backend_flags.heap_reduction_threshold
else
Float.infinity
in
if major_words > heap_reduction_threshold then begin
Profile.record_call "compact" (fun () ->
reset ();
Gc.compact ())
end
module Dwarf_helpers = struct
let dwarf = ref None
let sourcefile_for_dwarf = ref None
let begin_dwarf ~build_asm_directives ~code_begin ~code_end ~file_emitter =
match !sourcefile_for_dwarf with
| None -> ()
| Some sourcefile ->
let asm_directives = build_asm_directives () in
let (module Asm_directives : Asm_targets.Asm_directives_intf.S) = asm_directives in
Asm_targets.Asm_label.initialize ~new_label:Cmm.new_label;
Asm_directives.initialize ();
let unit_name =
(* CR lmaurer: This doesn't actually need to be an [Ident.t] *)
Symbol.for_current_unit ()
|> Symbol.linkage_name
|> Linkage_name.to_string
|> Ident.create_persistent
in
let code_begin = Asm_targets.Asm_symbol.create code_begin in
let code_end = Asm_targets.Asm_symbol.create code_end in
dwarf := Some (Dwarf.create
~sourcefile
~unit_name
~asm_directives
~get_file_id:(get_file_num ~file_emitter)
~code_begin ~code_end)
let reset_dwarf () =
dwarf := None;
sourcefile_for_dwarf := None
let init ~disable_dwarf sourcefile =
reset_dwarf ();
let can_emit_dwarf =
!Clflags.debug
&& ((not !Dwarf_flags.restrict_to_upstream_dwarf)
|| !Dwarf_flags.dwarf_inlined_frames)
&& not disable_dwarf
in
match can_emit_dwarf,
Target_system.architecture (),
Target_system.derived_system () with
| true, (X86_64 | AArch64), _ ->
sourcefile_for_dwarf := Some sourcefile
| true, _, _
| false, _, _ -> ()
let emit_dwarf () =
Option.iter (Dwarf.emit
~basic_block_sections:!Flambda_backend_flags.basic_block_sections
~binary_backend_available:!binary_backend_available)
!dwarf
let emit_delayed_dwarf () =
Option.iter (Dwarf.emit_delayed
~basic_block_sections:!Flambda_backend_flags.basic_block_sections
~binary_backend_available:!binary_backend_available)
!dwarf
let record_dwarf_for_fundecl fundecl =
match !dwarf with
| None -> None
| Some dwarf ->
let fun_end_label = Cmm.new_label () in
Some (Dwarf.dwarf_for_fundecl dwarf fundecl ~fun_end_label)
end
let report_error ppf = function
| Stack_frame_too_large n ->
Format.fprintf ppf "stack frame too large (%d bytes). \n\
Use -long-frames compiler flag." n
| Stack_frame_way_too_large n ->
Format.fprintf ppf "stack frame too large (%d bytes)." n
| Inconsistent_probe_init (name, dbg) ->
Format.fprintf ppf "Inconsistent use of ~enabled_at_init in [%%probe %s ..] at %a"
name Debuginfo.print_compact dbg
type preproc_stack_check_result =
{ max_frame_size : int;
contains_nontail_calls : bool }
let preproc_stack_check ~fun_body ~frame_size ~trap_size =
let rec loop (i:Linear.instruction) fs max_fs nontail_flag =
match i.desc with
| Lend -> { max_frame_size = max_fs;
contains_nontail_calls = nontail_flag}
| Ladjust_stack_offset { delta_bytes } ->
let s = fs + delta_bytes in
loop i.next s (max s max_fs) nontail_flag
| Lpushtrap _ ->
let s = fs + trap_size in
loop i.next s (max s max_fs) nontail_flag
| Lpoptrap ->
loop i.next (fs - trap_size) max_fs nontail_flag
| Lop (Istackoffset n) ->
let s = fs + n in
loop i.next s (max s max_fs) nontail_flag
| Lop (Icall_ind | Icall_imm _ ) ->
loop i.next fs max_fs true
| Lprologue | Lop _ | Lreloadretaddr | Lreturn | Llabel _
| Lbranch _ | Lcondbranch _ | Lcondbranch3 _ | Lswitch _
| Lentertrap | Lraise _ ->
loop i.next fs max_fs nontail_flag
| Lstackcheck _ ->
(* should not be already present *)
assert false
in
loop fun_body frame_size frame_size false
let add_stack_checks_if_needed (fundecl : Linear.fundecl) ~stack_offset ~stack_threshold_size ~trap_size =
if Config.no_stack_checks then
fundecl
else begin
let frame_size =
Proc.frame_size ~stack_offset
~num_stack_slots:fundecl.fun_num_stack_slots
~contains_calls:fundecl.fun_contains_calls
in
let { max_frame_size; contains_nontail_calls } =
preproc_stack_check ~fun_body:fundecl.fun_body ~frame_size ~trap_size
in
let insert_stack_check =
contains_nontail_calls || max_frame_size >= stack_threshold_size
in
if insert_stack_check
then
let fun_body =
Linear.instr_cons
(Lstackcheck { max_frame_size_bytes = max_frame_size })
[||] [||] ~available_before:fundecl.fun_body.available_before
~available_across:fundecl.fun_body.available_across fundecl.fun_body
in
{ fundecl with fun_body }
else fundecl
end