forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselectgen.ml
1023 lines (962 loc) · 40 KB
/
selectgen.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* 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. *)
(* *)
(**************************************************************************)
(* Selection of pseudo-instructions, assignment of pseudo-registers,
sequentialization. *)
[@@@ocaml.warning "+a-4-9-40-41-42"]
open Cmm
module Int = Numbers.Int
module VP = Backend_var.With_provenance
(* The default instruction selection class *)
let maybe_emit_naming_op env ~bound_name seq regs =
match bound_name with
| None -> ()
| Some bound_name ->
let provenance = VP.provenance bound_name in
if Option.is_some provenance
then
let bound_name = VP.var bound_name in
let naming_op =
Mach.Iname_for_debugger
{ ident = bound_name;
provenance;
which_parameter = None;
is_assignment = false;
regs
}
in
seq#insert_debug env (Mach.Iop naming_op) Debuginfo.none [||] [||]
(* "Join" two instruction sequences, making sure they return their results in
the same registers. *)
let join env opt_r1 seq1 opt_r2 seq2 ~bound_name =
let maybe_emit_naming_op = maybe_emit_naming_op env ~bound_name in
match opt_r1, opt_r2 with
| None, _ -> opt_r2
| _, None -> opt_r1
| Some r1, Some r2 ->
let l1 = Array.length r1 in
assert (l1 = Array.length r2);
let r = Array.make l1 Reg.dummy in
for i = 0 to l1 - 1 do
if Reg.anonymous r1.(i) && Cmm.ge_component r1.(i).Reg.typ r2.(i).Reg.typ
then (
r.(i) <- r1.(i);
seq2#insert_move env r2.(i) r1.(i);
maybe_emit_naming_op seq2 [| r1.(i) |])
else if Reg.anonymous r2.(i)
&& Cmm.ge_component r2.(i).Reg.typ r1.(i).Reg.typ
then (
r.(i) <- r2.(i);
seq1#insert_move env r1.(i) r2.(i);
maybe_emit_naming_op seq1 [| r2.(i) |])
else
let typ = Cmm.lub_component r1.(i).Reg.typ r2.(i).Reg.typ in
r.(i) <- Reg.create typ;
seq1#insert_move env r1.(i) r.(i);
maybe_emit_naming_op seq1 [| r.(i) |];
seq2#insert_move env r2.(i) r.(i);
maybe_emit_naming_op seq2 [| r.(i) |]
done;
Some r
(* Same, for N branches *)
let join_array env rs ~bound_name =
let maybe_emit_naming_op = maybe_emit_naming_op env ~bound_name in
let some_res = ref None in
for i = 0 to Array.length rs - 1 do
let r, _ = rs.(i) in
match r with
| None -> ()
| Some r -> (
match !some_res with
| None -> some_res := Some (r, Array.map (fun r -> r.Reg.typ) r)
| Some (r', types) ->
let types =
Array.map2 (fun r typ -> Cmm.lub_component r.Reg.typ typ) r types
in
some_res := Some (r', types))
done;
match !some_res with
| None -> None
| Some (template, types) ->
let size_res = Array.length template in
let res = Array.make size_res Reg.dummy in
for i = 0 to size_res - 1 do
res.(i) <- Reg.create types.(i)
done;
for i = 0 to Array.length rs - 1 do
let r, s = rs.(i) in
match r with
| None -> ()
| Some r ->
s#insert_moves env r res;
maybe_emit_naming_op s res
done;
Some res
type environment = unit Select_utils.environment
class virtual selector_generic =
object (self : 'self)
inherit
[unit, Mach.operation, Mach.instruction_desc] Select_utils.common_selector
method is_store op = match op with Istore (_, _, _) -> true | _ -> false
method lift_op op = Iop op
method make_store mem_chunk addr_mode is_assignment =
Mach.Iop (Mach.Istore (mem_chunk, addr_mode, is_assignment))
method make_stack_offset stack_ofs = Iop (Istackoffset stack_ofs)
method make_name_for_debugger ~ident ~which_parameter ~provenance
~is_assignment ~regs =
Mach.Iop
(Mach.Iname_for_debugger
{ ident; which_parameter; provenance; is_assignment; regs })
method make_const_int x = Mach.Iconst_int x
method make_const_float32 x = Mach.Iconst_float32 x
method make_const_float x = Mach.Iconst_float x
method make_const_vec128 x = Mach.Iconst_vec128 x
method make_const_symbol x = Mach.Iconst_symbol x
method make_opaque () = Mach.Iopaque
(* Default instruction selection for stores (of words) *)
method select_store is_assign addr arg : Mach.operation * Cmm.expression =
Istore (Word_val, addr, is_assign), arg
(* call marking methods, documented in selectgen.mli *)
val contains_calls = ref false
method mark_call = contains_calls := true
method mark_tailcall = ()
method mark_c_tailcall = if !Clflags.debug then contains_calls := true
method mark_instr : Mach.instruction_desc -> unit =
function
| Iop (Icall_ind | Icall_imm _ | Iextcall _ | Iprobe _) -> self#mark_call
| Iop (Itailcall_ind | Itailcall_imm _) -> self#mark_tailcall
| Iop (Ialloc _) | Iop (Ipoll _) ->
self#mark_call (* caml_alloc*, caml_garbage_collection (incl. polls) *)
| Iraise raise_kind -> (
match raise_kind with
| Lambda.Raise_notrace -> ()
| Lambda.Raise_regular | Lambda.Raise_reraise ->
(* PR#6239 *)
(* caml_stash_backtrace; we #mark_call rather than #mark_c_tailcall to
get a good stack backtrace *)
self#mark_call)
| Itrywith _ -> self#mark_call
| _ -> ()
(* Default instruction selection for operators *)
method select_operation (op : Cmm.operation) (args : Cmm.expression list)
_dbg : Mach.operation * Cmm.expression list =
let open Mach in
let open Simple_operation in
match op, args with
| Capply _, Cconst_symbol (func, _dbg) :: rem -> Icall_imm { func }, rem
| Capply _, _ -> Icall_ind, args
| Cextcall { func; builtin = true }, _ ->
Misc.fatal_errorf
"Selection.select_operation: builtin not recognized %s" func ()
| Cextcall { func; alloc; ty; ty_args; returns; builtin = false }, _ ->
( Iextcall { func; alloc; ty_res = ty; ty_args; returns; stack_ofs = -1 },
args )
| Cload { memory_chunk; mutability; is_atomic }, [arg] ->
let addressing_mode, eloc = self#select_addressing memory_chunk arg in
Iload { memory_chunk; addressing_mode; mutability; is_atomic }, [eloc]
| Cstore (chunk, init), [arg1; arg2] ->
let addr, eloc = self#select_addressing chunk arg1 in
let is_assign =
match init with Initialization -> false | Assignment -> true
in
if chunk = Word_int || chunk = Word_val
then
let op, newarg2 = self#select_store is_assign addr arg2 in
op, [newarg2; eloc]
else Istore (chunk, addr, is_assign), [arg2; eloc]
(* Inversion addr/datum in Istore *)
| Cdls_get, _ -> Idls_get, args
| Cpoll, _ -> Ipoll { return_label = None }, args
| Calloc mode, _ -> Ialloc { bytes = 0; dbginfo = []; mode }, args
| Caddi, _ -> self#select_arith_comm Iadd args
| Csubi, _ -> self#select_arith Isub args
| Cmuli, _ -> self#select_arith_comm Imul args
| Cmulhi { signed }, _ -> self#select_arith_comm (Imulh { signed }) args
| Cdivi, _ -> Iintop Idiv, args
| Cmodi, _ -> Iintop Imod, args
| Cand, _ -> self#select_arith_comm Iand args
| Cor, _ -> self#select_arith_comm Ior args
| Cxor, _ -> self#select_arith_comm Ixor args
| Clsl, _ -> self#select_arith Ilsl args
| Clsr, _ -> self#select_arith Ilsr args
| Casr, _ -> self#select_arith Iasr args
| Cclz { arg_is_non_zero }, _ -> Iintop (Iclz { arg_is_non_zero }), args
| Cctz { arg_is_non_zero }, _ -> Iintop (Ictz { arg_is_non_zero }), args
| Cpopcnt, _ -> Iintop Ipopcnt, args
| Ccmpi comp, _ -> self#select_arith_comp (Isigned comp) args
| Caddv, _ -> self#select_arith_comm Iadd args
| Cadda, _ -> self#select_arith_comm Iadd args
| Ccmpa comp, _ -> self#select_arith_comp (Iunsigned comp) args
| Ccmpf (w, comp), _ -> Ifloatop (w, Icompf comp), args
| Ccsel _, [cond; ifso; ifnot] ->
let cond, earg = self#select_condition cond in
Icsel cond, [earg; ifso; ifnot]
| Cnegf w, _ -> Ifloatop (w, Inegf), args
| Cabsf w, _ -> Ifloatop (w, Iabsf), args
| Caddf w, _ -> Ifloatop (w, Iaddf), args
| Csubf w, _ -> Ifloatop (w, Isubf), args
| Cmulf w, _ -> Ifloatop (w, Imulf), args
| Cdivf w, _ -> Ifloatop (w, Idivf), args
| Creinterpret_cast cast, _ -> Ireinterpret_cast cast, args
| Cstatic_cast cast, _ -> Istatic_cast cast, args
| Catomic { op = Fetch_and_add; size }, [src; dst] ->
let dst_size =
match size with
| Word | Sixtyfour -> Word_int
| Thirtytwo -> Thirtytwo_signed
in
let addr, eloc = self#select_addressing dst_size dst in
Iintop_atomic { op = Fetch_and_add; size; addr }, [src; eloc]
| Catomic { op = Compare_and_swap; size }, [compare_with; set_to; dst] ->
let dst_size =
match size with
| Word | Sixtyfour -> Word_int
| Thirtytwo -> Thirtytwo_signed
in
let addr, eloc = self#select_addressing dst_size dst in
( Iintop_atomic { op = Compare_and_swap; size; addr },
[compare_with; set_to; eloc] )
| Cprobe { name; handler_code_sym; enabled_at_init }, _ ->
Iprobe { name; handler_code_sym; enabled_at_init }, args
| Cprobe_is_enabled { name }, _ -> Iprobe_is_enabled { name }, []
| Cbeginregion, _ -> Ibeginregion, []
| Cendregion, _ -> Iendregion, args
| _ -> Misc.fatal_error "Selection.select_oper"
method private select_arith_comm (op : Simple_operation.integer_operation)
(args : Cmm.expression list) : Mach.operation * Cmm.expression list =
match args with
| [arg; Cconst_int (n, _)] when self#is_immediate op n ->
Iintop_imm (op, n), [arg]
| [Cconst_int (n, _); arg] when self#is_immediate op n ->
Iintop_imm (op, n), [arg]
| _ -> Iintop op, args
method private select_arith (op : Simple_operation.integer_operation)
(args : Cmm.expression list) : Mach.operation * Cmm.expression list =
match args with
| [arg; Cconst_int (n, _)] when self#is_immediate op n ->
Iintop_imm (op, n), [arg]
| _ -> Iintop op, args
method private select_arith_comp (cmp : Simple_operation.integer_comparison)
(args : Cmm.expression list) : Mach.operation * Cmm.expression list =
match args with
| [arg; Cconst_int (n, _)]
when self#is_immediate (Simple_operation.Icomp cmp) n ->
Iintop_imm (Icomp cmp, n), [arg]
| [Cconst_int (n, _); arg]
when self#is_immediate
(Simple_operation.Icomp (Select_utils.swap_intcomp cmp))
n ->
Iintop_imm (Icomp (Select_utils.swap_intcomp cmp), n), [arg]
| _ -> Iintop (Icomp cmp), args
(* Buffering of instruction sequences *)
val mutable instr_seq = Mach.dummy_instr
method insert_debug _env desc dbg arg res =
instr_seq <- Mach.instr_cons_debug desc arg res dbg instr_seq
method insert _env desc arg res =
(* CR mshinwell: fix debuginfo *)
instr_seq <- Mach.instr_cons_debug desc arg res Debuginfo.none instr_seq
method extract_onto (o : Mach.instruction) : Mach.instruction =
let rec extract res i =
if i == Mach.dummy_instr
then res
else extract { i with Mach.next = res } i.Mach.next
in
extract o instr_seq
method extract = self#extract_onto (Mach.end_instr ())
(* Insert a sequence of moves from one pseudoreg set to another. *)
method insert_move env src dst =
if src.Reg.stamp <> dst.Reg.stamp
then self#insert env Mach.(Iop Imove) [| src |] [| dst |]
method private emit_expr_aux_raise env k arg dbg =
match self#emit_expr env arg ~bound_name:None with
| None -> None
| Some r1 ->
let rd = [| Proc.loc_exn_bucket |] in
self#insert env (Mach.Iop Imove) r1 rd;
self#insert_debug env (Mach.Iraise k) dbg rd [||];
Select_utils.set_traps_for_raise env;
None
method private emit_expr_aux_op env bound_name op args dbg =
let ret res = Some res in
match self#emit_parts_list env args with
| None -> None
| Some (simple_args, env) -> (
let add_naming_op_for_bound_name regs =
match bound_name with
| None -> ()
| Some bound_name ->
let provenance = VP.provenance bound_name in
if Option.is_some provenance
then
let bound_name = VP.var bound_name in
let naming_op =
Mach.Iname_for_debugger
{ ident = bound_name;
provenance;
which_parameter = None;
is_assignment = false;
regs
}
in
self#insert_debug env (Mach.Iop naming_op) Debuginfo.none [||] [||]
in
let ty = Select_utils.oper_result_type op in
let new_op, new_args = self#select_operation op simple_args dbg in
match new_op with
| Icall_ind ->
let r1 = self#emit_tuple env new_args in
let rarg = Array.sub r1 1 (Array.length r1 - 1) in
let rd = self#regs_for ty in
let loc_arg, stack_ofs_args = Proc.loc_arguments (Reg.typv rarg) in
let loc_res, stack_ofs_res = Proc.loc_results_call (Reg.typv rd) in
let stack_ofs = Stdlib.Int.max stack_ofs_args stack_ofs_res in
self#insert_move_args env rarg loc_arg stack_ofs;
self#insert_debug env (Mach.Iop new_op) dbg
(Array.append [| r1.(0) |] loc_arg)
loc_res;
(* The destination registers (as per the procedure calling convention)
need to be named right now, otherwise the result of the function
call may be unavailable in the debugger immediately after the
call. *)
add_naming_op_for_bound_name loc_res;
self#insert_move_results env loc_res rd stack_ofs;
Select_utils.set_traps_for_raise env;
Some rd
| Icall_imm _ ->
let r1 = self#emit_tuple env new_args in
let rd = self#regs_for ty in
let loc_arg, stack_ofs_args = Proc.loc_arguments (Reg.typv r1) in
let loc_res, stack_ofs_res = Proc.loc_results_call (Reg.typv rd) in
let stack_ofs = Stdlib.Int.max stack_ofs_args stack_ofs_res in
self#insert_move_args env r1 loc_arg stack_ofs;
self#insert_debug env (Mach.Iop new_op) dbg loc_arg loc_res;
add_naming_op_for_bound_name loc_res;
self#insert_move_results env loc_res rd stack_ofs;
Select_utils.set_traps_for_raise env;
Some rd
| Iextcall ({ func; ty_args; returns; _ } as r) ->
let loc_arg, stack_ofs =
self#emit_extcall_args env ty_args new_args
in
let keep_for_checking =
(not returns)
&& !Select_utils.current_function_is_check_enabled
&& String.equal func Cmm.caml_flambda2_invalid
in
let returns, ty =
if keep_for_checking then true, typ_int else returns, ty
in
let rd = self#regs_for ty in
let loc_res =
self#insert_op_debug env
(Mach.Iextcall { r with stack_ofs; returns })
dbg loc_arg
(Proc.loc_external_results (Reg.typv rd))
in
add_naming_op_for_bound_name loc_res;
self#insert_move_results env loc_res rd stack_ofs;
Select_utils.set_traps_for_raise env;
if returns then ret rd else None
| Ialloc { bytes = _; mode } ->
let rd = self#regs_for typ_val in
let bytes = Select_utils.size_expr env (Ctuple new_args) in
let alloc_words = (bytes + Arch.size_addr - 1) / Arch.size_addr in
let op =
Mach.Ialloc
{ bytes = alloc_words * Arch.size_addr;
dbginfo = [{ alloc_words; alloc_dbg = dbg }];
mode
}
in
self#insert_debug env (Mach.Iop op) dbg [||] rd;
add_naming_op_for_bound_name rd;
self#emit_stores env dbg new_args rd;
Select_utils.set_traps_for_raise env;
ret rd
| Iprobe _ ->
let r1 = self#emit_tuple env new_args in
let rd = self#regs_for ty in
let rd = self#insert_op_debug env new_op dbg r1 rd in
Select_utils.set_traps_for_raise env;
ret rd
| op ->
let r1 = self#emit_tuple env new_args in
let rd = self#regs_for ty in
add_naming_op_for_bound_name rd;
ret (self#insert_op_debug env op dbg r1 rd))
method private emit_expr_aux_ifthenelse env bound_name econd _ifso_dbg eif
_ifnot_dbg eelse dbg _value_kind =
let cond, earg = self#select_condition econd in
match self#emit_expr env earg ~bound_name:None with
| None -> None
| Some rarg ->
let rif, (sif : 'self) = self#emit_sequence env eif ~bound_name in
let relse, (selse : 'self) = self#emit_sequence env eelse ~bound_name in
let r = join env rif sif relse selse ~bound_name in
self#insert_debug env
(Mach.Iifthenelse (cond, sif#extract, selse#extract))
dbg rarg [||];
r
method private emit_expr_aux_switch env bound_name esel index ecases dbg
_value_kind =
match self#emit_expr env esel ~bound_name:None with
| None -> None
| Some rsel ->
let rscases =
Array.map
(fun (case, _dbg) -> self#emit_sequence env case ~bound_name)
ecases
in
let r = join_array env rscases ~bound_name in
self#insert_debug env
(Mach.Iswitch (index, Array.map (fun (_, s) -> s#extract) rscases))
dbg rsel [||];
r
method private emit_expr_aux_catch env bound_name rec_flag handlers body
_value_kind =
let handlers =
List.map
(fun (nfail, ids, e2, dbg, is_cold) ->
let rs =
List.map
(fun (id, typ) ->
let r = self#regs_for typ in
Select_utils.name_regs id r;
r)
ids
in
nfail, ids, rs, e2, dbg, is_cold)
handlers
in
let env, handlers_map =
(* Since the handlers may be recursive, and called from the body, the
same environment is used for translating both the handlers and the
body. *)
List.fold_left
(fun (env, map) (nfail, ids, rs, e2, dbg, is_cold) ->
let env, r =
Select_utils.env_add_static_exception nfail rs env ()
in
env, Int.Map.add nfail (r, (ids, rs, e2, dbg, is_cold)) map)
(env, Int.Map.empty) handlers
in
let r_body, s_body = self#emit_sequence env body ~bound_name in
let translate_one_handler nfail (traps_info, (ids, rs, e2, _dbg, is_cold))
=
assert (List.length ids = List.length rs);
let trap_stack =
match (!traps_info : Select_utils.trap_stack_info) with
| Unreachable -> assert false
| Reachable t -> t
in
let ids_and_rs = List.combine ids rs in
let new_env =
List.fold_left
(fun env ((id, _typ), r) -> Select_utils.env_add id r env)
(Select_utils.env_set_trap_stack env trap_stack)
ids_and_rs
in
let r, s =
self#emit_sequence new_env e2 ~bound_name:None ~at_start:(fun seq ->
List.iter
(fun ((var, _typ), r) ->
let provenance = VP.provenance var in
if Option.is_some provenance
then
let var = VP.var var in
let naming_op =
Mach.Iname_for_debugger
{ ident = var;
provenance;
which_parameter = None;
is_assignment = false;
regs = r
}
in
seq#insert_debug env (Mach.Iop naming_op) Debuginfo.none
[||] [||])
ids_and_rs)
in
(nfail, trap_stack, is_cold), (r, s)
in
let rec build_all_reachable_handlers ~already_built ~not_built =
let not_built, to_build =
Int.Map.partition
(fun _n (r, _) -> !r = Select_utils.Unreachable)
not_built
in
if Int.Map.is_empty to_build
then already_built
else
let already_built =
Int.Map.fold
(fun nfail handler already_built ->
translate_one_handler nfail handler :: already_built)
to_build already_built
in
build_all_reachable_handlers ~already_built ~not_built
in
let l =
build_all_reachable_handlers ~already_built:[] ~not_built:handlers_map
(* Note: we're dropping unreachable handlers here *)
in
let a = Array.of_list ((r_body, s_body) :: List.map snd l) in
let r = join_array env a ~bound_name in
let aux ((nfail, ts, is_cold), (_r, s)) = nfail, ts, s#extract, is_cold in
let final_trap_stack =
match r_body with
| Some _ -> env.Select_utils.trap_stack
| None -> (
(* The body never returns, so the trap stack at the end of the catch
is the one from the handlers *)
match l with
| [] ->
(* This whole catch never returns *)
env.Select_utils.trap_stack
| ((_, ts, _), (_, _)) :: tl ->
assert (List.for_all (fun ((_, ts', _), (_, _)) -> ts = ts') tl);
ts)
in
self#insert env
(Mach.Icatch (rec_flag, final_trap_stack, List.map aux l, s_body#extract))
[||] [||];
r
method private emit_expr_aux_exit env lbl args traps =
match self#emit_parts_list env args with
| None -> None
| Some (simple_list, ext_env) -> (
match lbl with
| Lbl nfail ->
let src = self#emit_tuple ext_env simple_list in
let handler =
try Select_utils.env_find_static_exception nfail env
with Not_found ->
Misc.fatal_error
("Selection.emit_expr: unbound label "
^ Stdlib.Int.to_string nfail)
in
(* Intermediate registers to handle cases where some registers from
src are present in dest *)
let tmp_regs = Reg.createv_like src in
(* Ccatch registers must not contain out of heap pointers *)
Array.iter (fun reg -> assert (reg.Reg.typ <> Addr)) src;
self#insert_moves env src tmp_regs;
self#insert_moves env tmp_regs (Array.concat handler.regs);
self#insert env (Mach.Iexit (nfail, traps)) [||] [||];
Select_utils.set_traps nfail handler.Select_utils.traps_ref
env.Select_utils.trap_stack traps;
None
| Return_lbl -> (
match simple_list with
| [expr] ->
self#emit_return ext_env expr traps;
None
| [] ->
Misc.fatal_error "Selection.emit_expr: Return without arguments"
| _ :: _ :: _ ->
Misc.fatal_error
"Selection.emit_expr: Return with too many arguments"))
method private emit_expr_aux_trywith env bound_name e1 exn_cont v e2 dbg
_value_kind =
let env_body = Select_utils.env_enter_trywith env exn_cont () in
let r1, s1 = self#emit_sequence env_body e1 ~bound_name in
let rv = self#regs_for typ_val in
let with_handler env_handler e2 =
let r2, s2 =
self#emit_sequence env_handler e2 ~bound_name ~at_start:(fun seq ->
let provenance = VP.provenance v in
if Option.is_some provenance
then
let var = VP.var v in
let naming_op =
Mach.Iname_for_debugger
{ ident = var;
provenance;
which_parameter = None;
is_assignment = false;
regs = rv
}
in
seq#insert_debug env (Mach.Iop naming_op) Debuginfo.none [||]
[||])
in
let r = join env r1 s1 r2 s2 ~bound_name in
self#insert env
(Mach.Itrywith
( s1#extract,
exn_cont,
( env_handler.Select_utils.trap_stack,
Mach.instr_cons_debug (Mach.Iop Imove)
[| Proc.loc_exn_bucket |] rv dbg s2#extract ) ))
[||] [||];
r
in
let env = Select_utils.env_add v rv env in
match Select_utils.env_find_static_exception exn_cont env_body with
| { traps_ref = { contents = Reachable ts }; _ } ->
with_handler (Select_utils.env_set_trap_stack env ts) e2
| { traps_ref = { contents = Unreachable }; _ } ->
let dummy_constant = Cconst_int (1, Debuginfo.none) in
let segfault =
Cmm.(
Cop
( Cload
{ memory_chunk = Word_int;
mutability = Mutable;
is_atomic = false
},
[Cconst_int (0, Debuginfo.none)],
Debuginfo.none ))
in
let dummy_raise =
Cop (Craise Raise_notrace, [dummy_constant], Debuginfo.none)
in
let unreachable =
(* The use of a raise operation means that this handler is known not
to return, making it compatible with any layout for the body or
surrounding code. We also set the trap stack to [Uncaught] to
ensure that we don't introduce spurious control-flow edges inside
the function. *)
Csequence (segfault, dummy_raise)
in
let env = Select_utils.env_set_trap_stack env Uncaught in
with_handler env unreachable
(* Misc.fatal_errorf "Selection.emit_expr: \ * Unreachable exception
handler %d" lbl *)
| exception Not_found ->
Misc.fatal_errorf "Selection.emit_expr: Unbound handler %d" exn_cont
method private emit_sequence ?at_start (env : environment) exp ~bound_name
: _ * 'self =
let s : 'self = {<instr_seq = Mach.dummy_instr>} in
(match at_start with None -> () | Some f -> f s);
let r = s#emit_expr_aux env exp ~bound_name in
r, s
(* Same, but in tail position *)
method private insert_return (env : environment) r
(traps : trap_action list) =
match r with
| None -> ()
| Some r ->
let loc = Proc.loc_results_return (Reg.typv r) in
self#insert_moves env r loc;
self#insert env (Mach.Ireturn traps) loc [||]
method private emit_return (env : environment) exp traps =
self#insert_return env (self#emit_expr_aux env exp ~bound_name:None) traps
method private emit_tail_apply env ty op args dbg =
match self#emit_parts_list env args with
| None -> ()
| Some (simple_args, env) -> (
let new_op, new_args = self#select_operation op simple_args dbg in
match new_op with
| Icall_ind ->
let r1 = self#emit_tuple env new_args in
let rd = self#regs_for ty in
let rarg = Array.sub r1 1 (Array.length r1 - 1) in
let loc_arg, stack_ofs_args = Proc.loc_arguments (Reg.typv rarg) in
let loc_res, stack_ofs_res = Proc.loc_results_call (Reg.typv rd) in
let stack_ofs = Stdlib.Int.max stack_ofs_args stack_ofs_res in
if stack_ofs = 0 && Select_utils.trap_stack_is_empty env
then (
let call = Mach.Iop Itailcall_ind in
self#insert_moves env rarg loc_arg;
self#insert_debug env call dbg
(Array.append [| r1.(0) |] loc_arg)
[||])
else (
self#insert_move_args env rarg loc_arg stack_ofs;
self#insert_debug env (Mach.Iop new_op) dbg
(Array.append [| r1.(0) |] loc_arg)
loc_res;
Select_utils.set_traps_for_raise env;
self#insert env (Mach.Iop (Istackoffset (-stack_ofs))) [||] [||];
self#insert env
(Mach.Ireturn (Select_utils.pop_all_traps env))
loc_res [||])
| Icall_imm { func } ->
let r1 = self#emit_tuple env new_args in
let rd = self#regs_for ty in
let loc_arg, stack_ofs_args = Proc.loc_arguments (Reg.typv r1) in
let loc_res, stack_ofs_res = Proc.loc_results_call (Reg.typv rd) in
let stack_ofs = Stdlib.Int.max stack_ofs_args stack_ofs_res in
if stack_ofs = 0 && Select_utils.trap_stack_is_empty env
then (
let call = Mach.Iop (Itailcall_imm { func }) in
self#insert_moves env r1 loc_arg;
self#insert_debug env call dbg loc_arg [||])
else if func.sym_name = !Select_utils.current_function_name
&& Select_utils.trap_stack_is_empty env
then (
let call = Mach.Iop (Itailcall_imm { func }) in
let loc_arg' = Proc.loc_parameters (Reg.typv r1) in
self#insert_moves env r1 loc_arg';
self#insert_debug env call dbg loc_arg' [||])
else (
self#insert_move_args env r1 loc_arg stack_ofs;
self#insert_debug env (Mach.Iop new_op) dbg loc_arg loc_res;
Select_utils.set_traps_for_raise env;
self#insert env (Mach.Iop (Istackoffset (-stack_ofs))) [||] [||];
self#insert env
(Mach.Ireturn (Select_utils.pop_all_traps env))
loc_res [||])
| _ -> Misc.fatal_error "Selection.emit_tail")
method private emit_tail_ifthenelse env econd _ifso_dbg eif _ifnot_dbg eelse
dbg _value_kind =
let cond, earg = self#select_condition econd in
match self#emit_expr env earg ~bound_name:None with
| None -> ()
| Some rarg ->
self#insert_debug env
(Mach.Iifthenelse
( cond,
self#emit_tail_sequence env eif,
self#emit_tail_sequence env eelse ))
dbg rarg [||]
method private emit_tail_switch env esel index ecases dbg _value_kind =
match self#emit_expr env esel ~bound_name:None with
| None -> ()
| Some rsel ->
let cases =
Array.map
(fun (case, _dbg) -> self#emit_tail_sequence env case)
ecases
in
self#insert_debug env (Mach.Iswitch (index, cases)) dbg rsel [||]
method private emit_tail_catch env rec_flag handlers e1 _value_kind =
let handlers =
List.map
(fun (nfail, ids, e2, dbg, is_cold) ->
let rs =
List.map
(fun (id, typ) ->
let r = self#regs_for typ in
Select_utils.name_regs id r;
r)
ids
in
nfail, ids, rs, e2, dbg, is_cold)
handlers
in
let env, handlers_map =
List.fold_left
(fun (env, map) (nfail, ids, rs, e2, dbg, is_cold) ->
let env, r =
Select_utils.env_add_static_exception nfail rs env ()
in
env, Int.Map.add nfail (r, (ids, rs, e2, dbg, is_cold)) map)
(env, Int.Map.empty) handlers
in
let s_body = self#emit_tail_sequence env e1 in
let translate_one_handler nfail (trap_info, (ids, rs, e2, _dbg, is_cold))
=
assert (List.length ids = List.length rs);
let trap_stack =
match (!trap_info : Select_utils.trap_stack_info) with
| Unreachable -> assert false
| Reachable t -> t
in
let ids_and_rs = List.combine ids rs in
let new_env =
List.fold_left
(fun env ((id, _typ), r) -> Select_utils.env_add id r env)
(Select_utils.env_set_trap_stack env trap_stack)
ids_and_rs
in
let seq =
self#emit_tail_sequence new_env e2 ~at_start:(fun seq ->
List.iter
(fun ((var, _typ), r) ->
let provenance = VP.provenance var in
if Option.is_some provenance
then
let var = VP.var var in
let naming_op =
Mach.Iname_for_debugger
{ ident = var;
provenance;
which_parameter = None;
is_assignment = false;
regs = r
}
in
seq#insert_debug new_env (Mach.Iop naming_op) Debuginfo.none
[||] [||])
ids_and_rs)
in
nfail, trap_stack, seq, is_cold
in
let rec build_all_reachable_handlers ~already_built ~not_built =
let not_built, to_build =
Int.Map.partition
(fun _n (r, _) -> !r = Select_utils.Unreachable)
not_built
in
if Int.Map.is_empty to_build
then already_built
else
let already_built =
Int.Map.fold
(fun nfail handler already_built ->
translate_one_handler nfail handler :: already_built)
to_build already_built
in
build_all_reachable_handlers ~already_built ~not_built
in
let new_handlers =
build_all_reachable_handlers ~already_built:[] ~not_built:handlers_map
(* Note: we're dropping unreachable handlers here *)
in
(* The final trap stack doesn't matter, as it's not reachable. *)
self#insert env
(Mach.Icatch
(rec_flag, env.Select_utils.trap_stack, new_handlers, s_body))
[||] [||]
method private emit_tail_trywith env e1 exn_cont v e2 dbg _value_kind =
let env_body = Select_utils.env_enter_trywith env exn_cont () in
let s1 = self#emit_tail_sequence env_body e1 in
let rv = self#regs_for typ_val in
let with_handler env_handler e2 =
let s2 =
self#emit_tail_sequence env_handler e2 ~at_start:(fun seq ->
let provenance = VP.provenance v in
if Option.is_some provenance
then
let var = VP.var v in
let naming_op =
Mach.Iname_for_debugger
{ ident = var;
provenance;
which_parameter = None;
is_assignment = false;
regs = rv
}
in
seq#insert_debug env_handler (Mach.Iop naming_op) Debuginfo.none
[||] [||])
in
self#insert env
(Mach.Itrywith
( s1,
exn_cont,
( env_handler.Select_utils.trap_stack,
Mach.instr_cons_debug (Iop Imove) [| Proc.loc_exn_bucket |] rv
dbg s2 ) ))
[||] [||]
in
let env = Select_utils.env_add v rv env in
match Select_utils.env_find_static_exception exn_cont env_body with
| { traps_ref = { contents = Reachable ts }; _ } ->
with_handler (Select_utils.env_set_trap_stack env ts) e2
| { traps_ref = { contents = Unreachable }; _ } ->
(* Note: The following [unreachable] expression has machtype [|Int|],
but this might not be the correct machtype for this function's return
value. It doesn't matter at runtime since the expression cannot
return, but if we start checking (or joining) the machtypes of the
different tails we will need to implement something like the
[emit_expr_aux] version above, that hides the machtype. *)
let unreachable =
Cmm.(
Cop
( Cload
{ memory_chunk = Word_int;
mutability = Mutable;
is_atomic = false
},
[Cconst_int (0, Debuginfo.none)],
Debuginfo.none ))
in
with_handler env unreachable
(* Misc.fatal_errorf "Selection.emit_expr: \ Unreachable exception handler
%d" lbl *)
| exception Not_found ->
Misc.fatal_errorf "Selection.emit_expr: Unbound handler %d" exn_cont
method private emit_tail_sequence ?at_start env exp =
let s = {<instr_seq = Mach.dummy_instr>} in
(match at_start with None -> () | Some f -> f s);
s#emit_tail env exp;
s#extract
(* Sequentialization of a function definition *)
method emit_fundecl ~future_funcnames f =
Select_utils.current_function_name := f.Cmm.fun_name.sym_name;
Select_utils.current_function_is_check_enabled
:= Zero_alloc_checker.is_check_enabled f.Cmm.fun_codegen_options
f.Cmm.fun_name.sym_name f.Cmm.fun_dbg;
let num_regs_per_arg = Array.make (List.length f.Cmm.fun_args) 0 in
let rargs =
List.mapi
(fun arg_index (var, ty) ->
let r = self#regs_for ty in
Select_utils.name_regs var r;
num_regs_per_arg.(arg_index) <- Array.length r;
r)
f.Cmm.fun_args
in
let rarg = Array.concat rargs in
let loc_arg = Proc.loc_parameters (Reg.typv rarg) in
let env =
List.fold_right2
(fun (id, _ty) r env -> Select_utils.env_add id r env)
f.Cmm.fun_args rargs Select_utils.env_empty
in
self#emit_tail env f.Cmm.fun_body;
let body = self#extract in
instr_seq <- Mach.dummy_instr;
let loc_arg_index = ref 0 in
List.iteri
(fun param_index (var, _ty) ->
let provenance = VP.provenance var in
let var = VP.var var in
let num_regs_for_arg = num_regs_per_arg.(param_index) in
let hard_regs_for_arg =
Array.init num_regs_for_arg (fun index ->
loc_arg.(!loc_arg_index + index))
in
loc_arg_index := !loc_arg_index + num_regs_for_arg;
if Option.is_some provenance
then
let naming_op =
Mach.Iname_for_debugger
{ ident = var;
provenance;
which_parameter = Some param_index;
is_assignment = false;
regs = hard_regs_for_arg
}
in
self#insert_debug env (Mach.Iop naming_op) Debuginfo.none
hard_regs_for_arg [||])
f.Cmm.fun_args;
self#insert_moves env loc_arg rarg;