forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmm_helpers.ml
2822 lines (2542 loc) · 99 KB
/
cmm_helpers.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. *)
(* *)
(**************************************************************************)
[@@@ocaml.warning "+a-4-9-40-41-42-44-45"]
module V = Backend_var
module VP = Backend_var.With_provenance
open Cmm
open Arch
(* Local binding of complex expressions *)
let bind name arg fn =
match arg with
Cvar _ | Cconst_int _ | Cconst_natint _ | Cconst_symbol _ -> fn arg
| _ -> let id = V.create_local name in Clet(VP.create id, arg, fn (Cvar id))
let bind_load name arg fn =
match arg with
| Cop(Cload _, [Cvar _], _) -> fn arg
| _ -> bind name arg fn
let bind_nonvar name arg fn =
match arg with
Cconst_int _ | Cconst_natint _ | Cconst_symbol _ -> fn arg
| _ -> let id = V.create_local name in Clet(VP.create id, arg, fn (Cvar id))
let caml_black = Nativeint.shift_left (Nativeint.of_int 3) 8
(* cf. runtime/caml/gc.h *)
(* Block headers. Meaning of the tag field: see stdlib/obj.ml *)
let floatarray_tag dbg = Cconst_int (Obj.double_array_tag, dbg)
let block_header tag sz =
Nativeint.add (Nativeint.shift_left (Nativeint.of_int sz) 10)
(Nativeint.of_int tag)
(* Static data corresponding to "value"s must be marked black in case we are
in no-naked-pointers mode. See [caml_darken] and the code below that emits
structured constants and static module definitions. *)
let black_block_header tag sz = Nativeint.logor (block_header tag sz) caml_black
let white_closure_header sz = block_header Obj.closure_tag sz
let black_closure_header sz = black_block_header Obj.closure_tag sz
let infix_header ofs = block_header Obj.infix_tag ofs
let float_header = block_header Obj.double_tag (size_float / size_addr)
let floatarray_header len =
(* Zero-sized float arrays have tag zero for consistency with
[caml_alloc_float_array]. *)
assert (len >= 0);
if len = 0 then block_header 0 0
else block_header Obj.double_array_tag (len * size_float / size_addr)
let string_header len =
block_header Obj.string_tag ((len + size_addr) / size_addr)
let boxedint32_header = block_header Obj.custom_tag 2
let boxedint64_header = block_header Obj.custom_tag (1 + 8 / size_addr)
let boxedintnat_header = block_header Obj.custom_tag 2
let caml_nativeint_ops = "caml_nativeint_ops"
let caml_int32_ops = "caml_int32_ops"
let caml_int64_ops = "caml_int64_ops"
let pos_arity_in_closinfo = 8 * size_addr - 8
(* arity = the top 8 bits of the closinfo word *)
let closure_info ~arity ~startenv =
assert (-128 <= arity && arity <= 127);
assert (0 <= startenv && startenv < 1 lsl (pos_arity_in_closinfo - 1));
Nativeint.(add (shift_left (of_int arity) pos_arity_in_closinfo)
(add (shift_left (of_int startenv) 1)
1n))
let alloc_float_header dbg = Cconst_natint (float_header, dbg)
let alloc_floatarray_header len dbg = Cconst_natint (floatarray_header len, dbg)
let alloc_closure_header sz dbg = Cconst_natint (white_closure_header sz, dbg)
let alloc_infix_header ofs dbg = Cconst_natint (infix_header ofs, dbg)
let alloc_closure_info ~arity ~startenv dbg =
Cconst_natint (closure_info ~arity ~startenv, dbg)
let alloc_boxedint32_header dbg = Cconst_natint (boxedint32_header, dbg)
let alloc_boxedint64_header dbg = Cconst_natint (boxedint64_header, dbg)
let alloc_boxedintnat_header dbg = Cconst_natint (boxedintnat_header, dbg)
(* Integers *)
let max_repr_int = max_int asr 1
let min_repr_int = min_int asr 1
let int_const dbg n =
if n <= max_repr_int && n >= min_repr_int
then Cconst_int((n lsl 1) + 1, dbg)
else Cconst_natint
(Nativeint.add (Nativeint.shift_left (Nativeint.of_int n) 1) 1n, dbg)
let natint_const_untagged dbg n =
if n > Nativeint.of_int max_int
|| n < Nativeint.of_int min_int
then Cconst_natint (n,dbg)
else Cconst_int (Nativeint.to_int n, dbg)
let cint_const n =
Cint(Nativeint.add (Nativeint.shift_left (Nativeint.of_int n) 1) 1n)
let targetint_const n =
Targetint.add (Targetint.shift_left (Targetint.of_int n) 1)
Targetint.one
let add_no_overflow n x c dbg =
let d = n + x in
if d = 0 then c else Cop(Caddi, [c; Cconst_int (d, dbg)], dbg)
let rec add_const c n dbg =
if n = 0 then c
else match c with
| Cconst_int (x, _) when Misc.no_overflow_add x n -> Cconst_int (x + n, dbg)
| Cop(Caddi, [Cconst_int (x, _); c], _)
when Misc.no_overflow_add n x ->
add_no_overflow n x c dbg
| Cop(Caddi, [c; Cconst_int (x, _)], _)
when Misc.no_overflow_add n x ->
add_no_overflow n x c dbg
| Cop(Csubi, [Cconst_int (x, _); c], _) when Misc.no_overflow_add n x ->
Cop(Csubi, [Cconst_int (n + x, dbg); c], dbg)
| Cop(Csubi, [c; Cconst_int (x, _)], _) when Misc.no_overflow_sub n x ->
add_const c (n - x) dbg
| c -> Cop(Caddi, [c; Cconst_int (n, dbg)], dbg)
let incr_int c dbg = add_const c 1 dbg
let decr_int c dbg = add_const c (-1) dbg
let rec add_int c1 c2 dbg =
match (c1, c2) with
| (Cconst_int (n, _), c) | (c, Cconst_int (n, _)) ->
add_const c n dbg
| (Cop(Caddi, [c1; Cconst_int (n1, _)], _), c2) ->
add_const (add_int c1 c2 dbg) n1 dbg
| (c1, Cop(Caddi, [c2; Cconst_int (n2, _)], _)) ->
add_const (add_int c1 c2 dbg) n2 dbg
| (_, _) ->
Cop(Caddi, [c1; c2], dbg)
let rec sub_int c1 c2 dbg =
match (c1, c2) with
| (c1, Cconst_int (n2, _)) when n2 <> min_int ->
add_const c1 (-n2) dbg
| (c1, Cop(Caddi, [c2; Cconst_int (n2, _)], _)) when n2 <> min_int ->
add_const (sub_int c1 c2 dbg) (-n2) dbg
| (Cop(Caddi, [c1; Cconst_int (n1, _)], _), c2) ->
add_const (sub_int c1 c2 dbg) n1 dbg
| (c1, c2) ->
Cop(Csubi, [c1; c2], dbg)
let rec lsl_int c1 c2 dbg =
match (c1, c2) with
| (Cop(Clsl, [c; Cconst_int (n1, _)], _), Cconst_int (n2, _))
when n1 > 0 && n2 > 0 && n1 + n2 < size_int * 8 ->
Cop(Clsl, [c; Cconst_int (n1 + n2, dbg)], dbg)
| (Cop(Caddi, [c1; Cconst_int (n1, _)], _), Cconst_int (n2, _))
when Misc.no_overflow_lsl n1 n2 ->
add_const (lsl_int c1 c2 dbg) (n1 lsl n2) dbg
| (_, _) ->
Cop(Clsl, [c1; c2], dbg)
let is_power2 n = n = 1 lsl Misc.log2 n
and mult_power2 c n dbg = lsl_int c (Cconst_int (Misc.log2 n, dbg)) dbg
let rec mul_int c1 c2 dbg =
match (c1, c2) with
| (c, Cconst_int (0, _)) | (Cconst_int (0, _), c) ->
Csequence (c, Cconst_int (0, dbg))
| (c, Cconst_int (1, _)) | (Cconst_int (1, _), c) ->
c
| (c, Cconst_int(-1, _)) | (Cconst_int(-1, _), c) ->
sub_int (Cconst_int (0, dbg)) c dbg
| (c, Cconst_int (n, _)) when is_power2 n -> mult_power2 c n dbg
| (Cconst_int (n, _), c) when is_power2 n -> mult_power2 c n dbg
| (Cop(Caddi, [c; Cconst_int (n, _)], _), Cconst_int (k, _)) |
(Cconst_int (k, _), Cop(Caddi, [c; Cconst_int (n, _)], _))
when Misc.no_overflow_mul n k ->
add_const (mul_int c (Cconst_int (k, dbg)) dbg) (n * k) dbg
| (c1, c2) ->
Cop(Cmuli, [c1; c2], dbg)
let ignore_low_bit_int = function
Cop(Caddi,
[(Cop(Clsl, [_; Cconst_int (n, _)], _) as c); Cconst_int (1, _)], _)
when n > 0
-> c
| Cop(Cor, [c; Cconst_int (1, _)], _) -> c
| c -> c
(* removes the 1-bit sign-extension left by untag_int (tag_int c) *)
let ignore_high_bit_int = function
Cop(Casr,
[Cop(Clsl, [c; Cconst_int (1, _)], _); Cconst_int (1, _)], _) -> c
| c -> c
let lsr_int c1 c2 dbg =
match c2 with
Cconst_int (0, _) ->
c1
| Cconst_int (n, _) when n > 0 ->
Cop(Clsr, [ignore_low_bit_int c1; c2], dbg)
| _ ->
Cop(Clsr, [c1; c2], dbg)
let asr_int c1 c2 dbg =
match c2 with
Cconst_int (0, _) ->
c1
| Cconst_int (n, _) when n > 0 ->
Cop(Casr, [ignore_low_bit_int c1; c2], dbg)
| _ ->
Cop(Casr, [c1; c2], dbg)
let tag_int i dbg =
match i with
Cconst_int (n, _) ->
int_const dbg n
| Cop(Casr, [c; Cconst_int (n, _)], _) when n > 0 ->
Cop(Cor,
[asr_int c (Cconst_int (n - 1, dbg)) dbg; Cconst_int (1, dbg)],
dbg)
| c ->
incr_int (lsl_int c (Cconst_int (1, dbg)) dbg) dbg
let untag_int i dbg =
match i with
Cconst_int (n, _) -> Cconst_int(n asr 1, dbg)
| Cop(Cor, [Cop(Casr, [c; Cconst_int (n, _)], _); Cconst_int (1, _)], _)
when n > 0 && n < size_int * 8 ->
Cop(Casr, [c; Cconst_int (n+1, dbg)], dbg)
| Cop(Cor, [Cop(Clsr, [c; Cconst_int (n, _)], _); Cconst_int (1, _)], _)
when n > 0 && n < size_int * 8 ->
Cop(Clsr, [c; Cconst_int (n+1, dbg)], dbg)
| c -> asr_int c (Cconst_int (1, dbg)) dbg
let mk_if_then_else dbg cond ifso_dbg ifso ifnot_dbg ifnot =
match cond with
| Cconst_int (0, _) -> ifnot
| Cconst_int (1, _) -> ifso
| _ ->
Cifthenelse(cond, ifso_dbg, ifso, ifnot_dbg, ifnot, dbg)
let mk_not dbg cmm =
match cmm with
| Cop(Caddi,
[Cop(Clsl, [c; Cconst_int (1, _)], _); Cconst_int (1, _)], dbg') ->
begin
match c with
| Cop(Ccmpi cmp, [c1; c2], dbg'') ->
tag_int
(Cop(Ccmpi (negate_integer_comparison cmp), [c1; c2], dbg'')) dbg'
| Cop(Ccmpa cmp, [c1; c2], dbg'') ->
tag_int
(Cop(Ccmpa (negate_integer_comparison cmp), [c1; c2], dbg'')) dbg'
| Cop(Ccmpf cmp, [c1; c2], dbg'') ->
tag_int
(Cop(Ccmpf (negate_float_comparison cmp), [c1; c2], dbg'')) dbg'
| _ ->
(* 0 -> 3, 1 -> 1 *)
Cop(Csubi,
[Cconst_int (3, dbg); Cop(Clsl, [c; Cconst_int (1, dbg)], dbg)],
dbg)
end
| Cconst_int (3, _) -> Cconst_int (1, dbg)
| Cconst_int (1, _) -> Cconst_int (3, dbg)
| c ->
(* 1 -> 3, 3 -> 1 *)
Cop(Csubi, [Cconst_int (4, dbg); c], dbg)
let mk_compare_ints dbg a1 a2 =
match (a1,a2) with
| Cconst_int (c1, _), Cconst_int (c2, _) ->
int_const dbg (Int.compare c1 c2)
| Cconst_natint (c1, _), Cconst_natint (c2, _) ->
int_const dbg (Nativeint.compare c1 c2)
| Cconst_int (c1, _), Cconst_natint (c2, _) ->
int_const dbg Nativeint.(compare (of_int c1) c2)
| Cconst_natint (c1, _), Cconst_int (c2, _) ->
int_const dbg Nativeint.(compare c1 (of_int c2))
| a1, a2 -> begin
bind "int_cmp" a2 (fun a2 ->
bind "int_cmp" a1 (fun a1 ->
let op1 = Cop(Ccmpi(Cgt), [a1; a2], dbg) in
let op2 = Cop(Ccmpi(Clt), [a1; a2], dbg) in
tag_int(sub_int op1 op2 dbg) dbg))
end
let mk_compare_floats dbg a1 a2 =
bind "float_cmp" a2 (fun a2 ->
bind "float_cmp" a1 (fun a1 ->
let op1 = Cop(Ccmpf(CFgt), [a1; a2], dbg) in
let op2 = Cop(Ccmpf(CFlt), [a1; a2], dbg) in
let op3 = Cop(Ccmpf(CFeq), [a1; a1], dbg) in
let op4 = Cop(Ccmpf(CFeq), [a2; a2], dbg) in
(* If both operands a1 and a2 are not NaN, then op3 = op4 = 1,
and the result is op1 - op2.
If at least one of the operands is NaN,
then op1 = op2 = 0, and the result is op3 - op4,
which orders NaN before other values.
To detect if the operand is NaN, we use the property:
for all x, NaN is not equal to x, even if x is NaN.
Therefore, op3 is 0 if and only if a1 is NaN,
and op4 is 0 if and only if a2 is NaN.
See also caml_float_compare_unboxed in runtime/floats.c *)
tag_int (add_int (sub_int op1 op2 dbg) (sub_int op3 op4 dbg) dbg) dbg))
let create_loop body dbg =
let cont = Lambda.next_raise_count () in
let call_cont = Cexit (cont, []) in
let body = Csequence (body, call_cont) in
Ccatch (Recursive, [cont, [], body, dbg], call_cont)
(* Turning integer divisions into multiply-high then shift.
The [division_parameters] function is used in module Emit for
those target platforms that support this optimization. *)
(* Unsigned comparison between native integers. *)
let ucompare x y = Nativeint.(compare (add x min_int) (add y min_int))
(* Unsigned division and modulus at type nativeint.
Algorithm: Hacker's Delight section 9.3 *)
let udivmod n d = Nativeint.(
if d < 0n then
if ucompare n d < 0 then (0n, n) else (1n, sub n d)
else begin
let q = shift_left (div (shift_right_logical n 1) d) 1 in
let r = sub n (mul q d) in
if ucompare r d >= 0 then (succ q, sub r d) else (q, r)
end)
(* Compute division parameters.
Algorithm: Hacker's Delight chapter 10, fig 10-1. *)
let divimm_parameters d = Nativeint.(
assert (d > 0n);
let twopsm1 = min_int in (* 2^31 for 32-bit archs, 2^63 for 64-bit archs *)
let nc = sub (pred twopsm1) (snd (udivmod twopsm1 d)) in
let rec loop p (q1, r1) (q2, r2) =
let p = p + 1 in
let q1 = shift_left q1 1 and r1 = shift_left r1 1 in
let (q1, r1) =
if ucompare r1 nc >= 0 then (succ q1, sub r1 nc) else (q1, r1) in
let q2 = shift_left q2 1 and r2 = shift_left r2 1 in
let (q2, r2) =
if ucompare r2 d >= 0 then (succ q2, sub r2 d) else (q2, r2) in
let delta = sub d r2 in
if ucompare q1 delta < 0 || (q1 = delta && r1 = 0n)
then loop p (q1, r1) (q2, r2)
else (succ q2, p - size)
in loop (size - 1) (udivmod twopsm1 nc) (udivmod twopsm1 d))
(* The result [(m, p)] of [divimm_parameters d] satisfies the following
inequality:
2^(wordsize + p) < m * d <= 2^(wordsize + p) + 2^(p + 1) (i)
from which it follows that
floor(n / d) = floor(n * m / 2^(wordsize+p))
if 0 <= n < 2^(wordsize-1)
ceil(n / d) = floor(n * m / 2^(wordsize+p)) + 1
if -2^(wordsize-1) <= n < 0
The correctness condition (i) above can be checked by the code below.
It was exhaustively tested for values of d from 2 to 10^9 in the
wordsize = 64 case.
let add2 (xh, xl) (yh, yl) =
let zl = add xl yl and zh = add xh yh in
((if ucompare zl xl < 0 then succ zh else zh), zl)
let shl2 (xh, xl) n =
assert (0 < n && n < size + size);
if n < size
then (logor (shift_left xh n) (shift_right_logical xl (size - n)),
shift_left xl n)
else (shift_left xl (n - size), 0n)
let mul2 x y =
let halfsize = size / 2 in
let halfmask = pred (shift_left 1n halfsize) in
let xl = logand x halfmask and xh = shift_right_logical x halfsize in
let yl = logand y halfmask and yh = shift_right_logical y halfsize in
add2 (mul xh yh, 0n)
(add2 (shl2 (0n, mul xl yh) halfsize)
(add2 (shl2 (0n, mul xh yl) halfsize)
(0n, mul xl yl)))
let ucompare2 (xh, xl) (yh, yl) =
let c = ucompare xh yh in if c = 0 then ucompare xl yl else c
let validate d m p =
let md = mul2 m d in
let one2 = (0n, 1n) in
let twoszp = shl2 one2 (size + p) in
let twop1 = shl2 one2 (p + 1) in
ucompare2 twoszp md < 0 && ucompare2 md (add2 twoszp twop1) <= 0
*)
let raise_symbol dbg symb =
Cop(Craise Lambda.Raise_regular, [Cconst_symbol (symb, dbg)], dbg)
let rec div_int c1 c2 is_safe dbg =
match (c1, c2) with
(c1, Cconst_int (0, _)) ->
Csequence(c1, raise_symbol dbg "caml_exn_Division_by_zero")
| (c1, Cconst_int (1, _)) ->
c1
| (Cconst_int (n1, _), Cconst_int (n2, _)) ->
Cconst_int (n1 / n2, dbg)
| (c1, Cconst_int (n, _)) when n <> min_int ->
let l = Misc.log2 n in
if n = 1 lsl l then
(* Algorithm:
t = shift-right-signed(c1, l - 1)
t = shift-right(t, W - l)
t = c1 + t
res = shift-right-signed(c1 + t, l)
*)
Cop(Casr, [bind "dividend" c1 (fun c1 ->
let t = asr_int c1 (Cconst_int (l - 1, dbg)) dbg in
let t =
lsr_int t (Cconst_int (Nativeint.size - l, dbg)) dbg
in
add_int c1 t dbg);
Cconst_int (l, dbg)], dbg)
else if n < 0 then
sub_int (Cconst_int (0, dbg))
(div_int c1 (Cconst_int (-n, dbg)) is_safe dbg)
dbg
else begin
let (m, p) = divimm_parameters (Nativeint.of_int n) in
(* Algorithm:
t = multiply-high-signed(c1, m)
if m < 0, t = t + c1
if p > 0, t = shift-right-signed(t, p)
res = t + sign-bit(c1)
*)
bind "dividend" c1 (fun c1 ->
let t = Cop(Cmulhi, [c1; natint_const_untagged dbg m], dbg) in
let t = if m < 0n then Cop(Caddi, [t; c1], dbg) else t in
let t =
if p > 0 then Cop(Casr, [t; Cconst_int (p, dbg)], dbg) else t
in
add_int t (lsr_int c1 (Cconst_int (Nativeint.size - 1, dbg)) dbg) dbg)
end
| (c1, c2) when !Clflags.unsafe || is_safe = Lambda.Unsafe ->
Cop(Cdivi, [c1; c2], dbg)
| (c1, c2) ->
bind "divisor" c2 (fun c2 ->
bind "dividend" c1 (fun c1 ->
Cifthenelse(c2,
dbg,
Cop(Cdivi, [c1; c2], dbg),
dbg,
raise_symbol dbg "caml_exn_Division_by_zero",
dbg)))
let mod_int c1 c2 is_safe dbg =
match (c1, c2) with
(c1, Cconst_int (0, _)) ->
Csequence(c1, raise_symbol dbg "caml_exn_Division_by_zero")
| (c1, Cconst_int ((1 | (-1)), _)) ->
Csequence(c1, Cconst_int (0, dbg))
| (Cconst_int (n1, _), Cconst_int (n2, _)) ->
Cconst_int (n1 mod n2, dbg)
| (c1, (Cconst_int (n, _) as c2)) when n <> min_int ->
let l = Misc.log2 n in
if n = 1 lsl l then
(* Algorithm:
t = shift-right-signed(c1, l - 1)
t = shift-right(t, W - l)
t = c1 + t
t = bit-and(t, -n)
res = c1 - t
*)
bind "dividend" c1 (fun c1 ->
let t = asr_int c1 (Cconst_int (l - 1, dbg)) dbg in
let t = lsr_int t (Cconst_int (Nativeint.size - l, dbg)) dbg in
let t = add_int c1 t dbg in
let t = Cop(Cand, [t; Cconst_int (-n, dbg)], dbg) in
sub_int c1 t dbg)
else
bind "dividend" c1 (fun c1 ->
sub_int c1 (mul_int (div_int c1 c2 is_safe dbg) c2 dbg) dbg)
| (c1, c2) when !Clflags.unsafe || is_safe = Lambda.Unsafe ->
(* Flambda already generates that test *)
Cop(Cmodi, [c1; c2], dbg)
| (c1, c2) ->
bind "divisor" c2 (fun c2 ->
bind "dividend" c1 (fun c1 ->
Cifthenelse(c2,
dbg,
Cop(Cmodi, [c1; c2], dbg),
dbg,
raise_symbol dbg "caml_exn_Division_by_zero",
dbg)))
(* Division or modulo on boxed integers. The overflow case min_int / -1
can occur, in which case we force x / -1 = -x and x mod -1 = 0. (PR#5513). *)
let is_different_from x = function
Cconst_int (n, _) -> n <> x
| Cconst_natint (n, _) -> n <> Nativeint.of_int x
| _ -> false
let safe_divmod_bi mkop is_safe mkm1 c1 c2 bi dbg =
bind "divisor" c2 (fun c2 ->
bind "dividend" c1 (fun c1 ->
let c = mkop c1 c2 is_safe dbg in
if Arch.division_crashes_on_overflow
&& (size_int = 4 || bi <> Primitive.Pint32)
&& not (is_different_from (-1) c2)
then
Cifthenelse(Cop(Ccmpi Cne, [c2; Cconst_int (-1, dbg)], dbg),
dbg, c,
dbg, mkm1 c1 dbg,
dbg)
else
c))
let safe_div_bi is_safe =
safe_divmod_bi div_int is_safe
(fun c1 dbg -> Cop(Csubi, [Cconst_int (0, dbg); c1], dbg))
let safe_mod_bi is_safe =
safe_divmod_bi mod_int is_safe (fun _ dbg -> Cconst_int (0, dbg))
(* Bool *)
let test_bool dbg cmm =
match cmm with
| Cop(Caddi, [Cop(Clsl, [c; Cconst_int (1, _)], _); Cconst_int (1, _)], _) ->
c
| Cconst_int (n, dbg) ->
if n = 1 then
Cconst_int (0, dbg)
else
Cconst_int (1, dbg)
| c -> Cop(Ccmpi Cne, [c; Cconst_int (1, dbg)], dbg)
(* Float *)
let box_float dbg c = Cop(Calloc, [alloc_float_header dbg; c], dbg)
let unbox_float dbg =
map_tail
(function
| Cop(Calloc, [Cconst_natint (hdr, _); c], _)
when Nativeint.equal hdr float_header ->
c
| Cconst_symbol (s, _dbg) as cmm ->
begin match Cmmgen_state.structured_constant_of_sym s with
| Some (Uconst_float x) ->
Cconst_float (x, dbg) (* or keep _dbg? *)
| _ ->
Cop(Cload {memory_chunk=Double; mutability=Immutable;
is_atomic=false},
[cmm], dbg)
end
| cmm -> Cop(Cload {memory_chunk=Double; mutability=Immutable;
is_atomic=false},
[cmm], dbg)
)
(* Complex *)
let box_complex dbg c_re c_im =
Cop(Calloc, [alloc_floatarray_header 2 dbg; c_re; c_im], dbg)
let complex_re c dbg =
Cop(Cload {memory_chunk=Double; mutability=Immutable; is_atomic=false},
[c], dbg)
let complex_im c dbg =
Cop(Cload {memory_chunk=Double; mutability=Immutable; is_atomic=false},
[Cop(Cadda, [c; Cconst_int (size_float, dbg)], dbg)], dbg)
(* Unit *)
let return_unit dbg c = Csequence(c, Cconst_int (1, dbg))
let rec remove_unit = function
Cconst_int (1, _) -> Ctuple []
| Csequence(c, Cconst_int (1, _)) -> c
| Csequence(c1, c2) ->
Csequence(c1, remove_unit c2)
| Cifthenelse(cond, ifso_dbg, ifso, ifnot_dbg, ifnot, dbg) ->
Cifthenelse(cond,
ifso_dbg, remove_unit ifso,
ifnot_dbg,
remove_unit ifnot, dbg)
| Cswitch(sel, index, cases, dbg) ->
Cswitch(sel, index,
Array.map (fun (case, dbg) -> remove_unit case, dbg) cases,
dbg)
| Ccatch(rec_flag, handlers, body) ->
let map_h (n, ids, handler, dbg) = (n, ids, remove_unit handler, dbg) in
Ccatch(rec_flag, List.map map_h handlers, remove_unit body)
| Ctrywith(body, exn, handler, dbg) ->
Ctrywith(remove_unit body, exn, remove_unit handler, dbg)
| Clet(id, c1, c2) ->
Clet(id, c1, remove_unit c2)
| Cop(Capply _mty, args, dbg) ->
Cop(Capply typ_void, args, dbg)
| Cop(Cextcall(proc, _ty_res, ty_args, alloc), args, dbg) ->
Cop(Cextcall(proc, typ_void, ty_args, alloc), args, dbg)
| Cexit (_,_) as c -> c
| Ctuple [] as c -> c
| c -> Csequence(c, Ctuple [])
(* Access to block fields *)
let mk_load_mut memory_chunk =
Cload {memory_chunk; mutability=Mutable; is_atomic=false}
let mk_load_atomic memory_chunk =
Cload {memory_chunk; mutability=Mutable; is_atomic=true}
let field_address ptr n dbg =
if n = 0
then ptr
else Cop(Cadda, [ptr; Cconst_int(n * size_addr, dbg)], dbg)
let get_field_gen mutability ptr n dbg =
Cop(Cload {memory_chunk=Word_val; mutability; is_atomic=false},
[field_address ptr n dbg], dbg)
let set_field ptr n newval init dbg =
Cop(Cstore (Word_val, init), [field_address ptr n dbg; newval], dbg)
let non_profinfo_mask =
if Config.profinfo
then (1 lsl (64 - Config.profinfo_width)) - 1
else 0 (* [non_profinfo_mask] is unused in this case *)
let get_header ptr dbg =
(* We cannot deem this as [Immutable] due to the presence of [Obj.truncate]
and [Obj.set_tag]. *)
Cop(mk_load_mut Word_int,
[Cop(Cadda, [ptr; Cconst_int(-size_int, dbg)], dbg)], dbg)
let get_header_without_profinfo ptr dbg =
if Config.profinfo then
Cop(Cand, [get_header ptr dbg; Cconst_int (non_profinfo_mask, dbg)], dbg)
else
get_header ptr dbg
let tag_offset =
if big_endian then -1 else -size_int
let get_tag ptr dbg =
if Proc.word_addressed then (* If byte loads are slow *)
Cop(Cand, [get_header ptr dbg; Cconst_int (255, dbg)], dbg)
else (* If byte loads are efficient *)
(* Same comment as [get_header] above *)
Cop(mk_load_mut Byte_unsigned,
[Cop(Cadda, [ptr; Cconst_int(tag_offset, dbg)], dbg)], dbg)
let get_size ptr dbg =
Cop(Clsr, [get_header_without_profinfo ptr dbg; Cconst_int (10, dbg)], dbg)
(* Array indexing *)
let log2_size_addr = Misc.log2 size_addr
let log2_size_float = Misc.log2 size_float
let wordsize_shift = 9
let numfloat_shift = 9 + log2_size_float - log2_size_addr
let is_addr_array_hdr hdr dbg =
Cop(Ccmpi Cne,
[Cop(Cand, [hdr; Cconst_int (255, dbg)], dbg); floatarray_tag dbg],
dbg)
let is_addr_array_ptr ptr dbg =
Cop(Ccmpi Cne, [get_tag ptr dbg; floatarray_tag dbg], dbg)
let addr_array_length_shifted hdr dbg =
Cop(Clsr, [hdr; Cconst_int (wordsize_shift, dbg)], dbg)
let float_array_length_shifted hdr dbg =
Cop(Clsr, [hdr; Cconst_int (numfloat_shift, dbg)], dbg)
let lsl_const c n dbg =
if n = 0 then c
else Cop(Clsl, [c; Cconst_int (n, dbg)], dbg)
(* Produces a pointer to the element of the array [ptr] on the position [ofs]
with the given element [log2size] log2 element size. [ofs] is given as a
tagged int expression.
The optional ?typ argument is the C-- type of the result.
By default, it is Addr, meaning we are constructing a derived pointer
into the heap. If we know the pointer is outside the heap
(this is the case for bigarray indexing), we give type Int instead. *)
let array_indexing ?typ log2size ptr ofs dbg =
let add =
match typ with
| None | Some Addr -> Cadda
| Some Int -> Caddi
| _ -> assert false in
match ofs with
| Cconst_int (n, _) ->
let i = n asr 1 in
if i = 0 then ptr
else Cop(add, [ptr; Cconst_int(i lsl log2size, dbg)], dbg)
| Cop(Caddi,
[Cop(Clsl, [c; Cconst_int (1, _)], _); Cconst_int (1, _)], dbg') ->
Cop(add, [ptr; lsl_const c log2size dbg], dbg')
| Cop(Caddi, [c; Cconst_int (n, _)], dbg') when log2size = 0 ->
Cop(add,
[Cop(add, [ptr; untag_int c dbg], dbg); Cconst_int (n asr 1, dbg)],
dbg')
| Cop(Caddi, [c; Cconst_int (n, _)], _) ->
Cop(add, [Cop(add, [ptr; lsl_const c (log2size - 1) dbg], dbg);
Cconst_int((n-1) lsl (log2size - 1), dbg)], dbg)
| _ when log2size = 0 ->
Cop(add, [ptr; untag_int ofs dbg], dbg)
| _ ->
Cop(add, [Cop(add, [ptr; lsl_const ofs (log2size - 1) dbg], dbg);
Cconst_int((-1) lsl (log2size - 1), dbg)], dbg)
let addr_array_ref arr ofs dbg =
Cop(mk_load_mut Word_val,
[array_indexing log2_size_addr arr ofs dbg], dbg)
let int_array_ref arr ofs dbg =
Cop(mk_load_mut Word_int,
[array_indexing log2_size_addr arr ofs dbg], dbg)
let unboxed_float_array_ref arr ofs dbg =
Cop(mk_load_mut Double,
[array_indexing log2_size_float arr ofs dbg], dbg)
let float_array_ref arr ofs dbg =
box_float dbg (unboxed_float_array_ref arr ofs dbg)
let addr_array_set arr ofs newval dbg =
Cop(Cextcall("caml_modify", typ_void, [], false),
[array_indexing log2_size_addr arr ofs dbg; newval], dbg)
let addr_array_initialize arr ofs newval dbg =
Cop(Cextcall("caml_initialize", typ_void, [], false),
[array_indexing log2_size_addr arr ofs dbg; newval], dbg)
let int_array_set arr ofs newval dbg =
Cop(Cstore (Word_int, Lambda.Assignment),
[array_indexing log2_size_addr arr ofs dbg; newval], dbg)
let float_array_set arr ofs newval dbg =
Cop(Cstore (Double, Lambda.Assignment),
[array_indexing log2_size_float arr ofs dbg; newval], dbg)
(* String length *)
(* Length of string block *)
let string_length exp dbg =
bind "str" exp (fun str ->
let tmp_var = V.create_local "tmp" in
Clet(VP.create tmp_var,
Cop(Csubi,
[Cop(Clsl,
[get_size str dbg;
Cconst_int (log2_size_addr, dbg)],
dbg);
Cconst_int (1, dbg)],
dbg),
Cop(Csubi,
[Cvar tmp_var;
Cop(mk_load_mut Byte_unsigned,
[Cop(Cadda, [str; Cvar tmp_var], dbg)], dbg)], dbg)))
let bigstring_length ba dbg =
Cop(mk_load_mut Word_int, [field_address ba 5 dbg], dbg)
(* Message sending *)
let lookup_tag obj tag dbg =
bind "tag" tag (fun tag ->
Cop(Cextcall("caml_get_public_method", typ_val, [], false),
[obj; tag],
dbg))
let lookup_label obj lab dbg =
bind "lab" lab (fun lab ->
let table = Cop (mk_load_mut Word_val, [obj], dbg) in
addr_array_ref table lab dbg)
let call_cached_method obj tag cache pos args dbg =
let arity = List.length args in
let cache = array_indexing log2_size_addr cache pos dbg in
Compilenv.need_send_fun arity;
Cop(Capply typ_val,
Cconst_symbol("caml_send" ^ Int.to_string arity, dbg) ::
obj :: tag :: cache :: args,
dbg)
(* Allocation *)
let make_alloc_generic set_fn dbg tag wordsize args =
if wordsize <= Config.max_young_wosize then
Cop(Calloc, Cconst_natint(block_header tag wordsize, dbg) :: args, dbg)
else begin
let id = V.create_local "*alloc*" in
let rec fill_fields idx = function
[] -> Cvar id
| e1::el -> Csequence(set_fn (Cvar id) (Cconst_int (idx, dbg)) e1 dbg,
fill_fields (idx + 2) el) in
Clet(VP.create id,
Cop(Cextcall("caml_alloc_shr_check_gc", typ_val, [], true),
[Cconst_int (wordsize, dbg); Cconst_int (tag, dbg)], dbg),
fill_fields 1 args)
end
let make_alloc dbg tag args =
let addr_array_init arr ofs newval dbg =
Cop(Cextcall("caml_initialize", typ_void, [], false),
[array_indexing log2_size_addr arr ofs dbg; newval], dbg)
in
make_alloc_generic addr_array_init dbg tag (List.length args) args
let make_float_alloc dbg tag args =
make_alloc_generic float_array_set dbg tag
(List.length args * size_float / size_addr) args
(* Bounds checking *)
let make_checkbound dbg = function
| [Cop(Clsr, [a1; Cconst_int (n, _)], _); Cconst_int (m, _)]
when (m lsl n) > n ->
Cop(Ccheckbound, [a1; Cconst_int(m lsl n + 1 lsl n - 1, dbg)], dbg)
| args ->
Cop(Ccheckbound, args, dbg)
(* Record application and currying functions *)
let apply_function_sym n =
Compilenv.need_apply_fun n; "caml_apply" ^ Int.to_string n
let curry_function_sym n =
Compilenv.need_curry_fun n;
if n >= 0
then "caml_curry" ^ Int.to_string n
else "caml_tuplify" ^ Int.to_string (-n)
(* Big arrays *)
let bigarray_elt_size : Lambda.bigarray_kind -> int = function
Pbigarray_unknown -> assert false
| Pbigarray_float32 -> 4
| Pbigarray_float64 -> 8
| Pbigarray_sint8 -> 1
| Pbigarray_uint8 -> 1
| Pbigarray_sint16 -> 2
| Pbigarray_uint16 -> 2
| Pbigarray_int32 -> 4
| Pbigarray_int64 -> 8
| Pbigarray_caml_int -> size_int
| Pbigarray_native_int -> size_int
| Pbigarray_complex32 -> 8
| Pbigarray_complex64 -> 16
(* Produces a pointer to the element of the bigarray [b] on the position
[args]. [args] is given as a list of tagged int expressions, one per array
dimension. *)
let bigarray_indexing unsafe elt_kind layout b args dbg =
let check_ba_bound bound idx v =
Csequence(make_checkbound dbg [bound;idx], v) in
(* Validates the given multidimensional offset against the array bounds and
transforms it into a one dimensional offset. The offsets are expressions
evaluating to tagged int. *)
let rec ba_indexing dim_ofs delta_ofs = function
[] -> assert false
| [arg] ->
if unsafe then arg
else
bind "idx" arg (fun idx ->
(* Load the untagged int bound for the given dimension *)
let bound =
Cop(mk_load_mut Word_int,
[field_address b dim_ofs dbg], dbg)
in
let idxn = untag_int idx dbg in
check_ba_bound bound idxn idx)
| arg1 :: argl ->
(* The remainder of the list is transformed into a one dimensional offset
*)
let rem = ba_indexing (dim_ofs + delta_ofs) delta_ofs argl in
(* Load the untagged int bound for the given dimension *)
let bound =
Cop(mk_load_mut Word_int,
[field_address b dim_ofs dbg], dbg)
in
if unsafe then add_int (mul_int (decr_int rem dbg) bound dbg) arg1 dbg
else
bind "idx" arg1 (fun idx ->
bind "bound" bound (fun bound ->
let idxn = untag_int idx dbg in
(* [offset = rem * (tag_int bound) + idx] *)
let offset =
add_int (mul_int (decr_int rem dbg) bound dbg) idx dbg
in
check_ba_bound bound idxn offset)) in
(* The offset as an expression evaluating to int *)
let offset =
match (layout : Lambda.bigarray_layout) with
Pbigarray_unknown_layout ->
assert false
| Pbigarray_c_layout ->
ba_indexing (4 + List.length args) (-1) (List.rev args)
| Pbigarray_fortran_layout ->
ba_indexing 5 1
(List.map (fun idx -> sub_int idx (Cconst_int (2, dbg)) dbg) args)
and elt_size =
bigarray_elt_size elt_kind in
(* [array_indexing] can simplify the given expressions *)
array_indexing ~typ:Addr (Misc.log2 elt_size)
(Cop(mk_load_mut Word_int,
[field_address b 1 dbg], dbg)) offset dbg
let bigarray_word_kind : Lambda.bigarray_kind -> memory_chunk = function
Pbigarray_unknown -> assert false
| Pbigarray_float32 -> Single
| Pbigarray_float64 -> Double
| Pbigarray_sint8 -> Byte_signed
| Pbigarray_uint8 -> Byte_unsigned
| Pbigarray_sint16 -> Sixteen_signed
| Pbigarray_uint16 -> Sixteen_unsigned
| Pbigarray_int32 -> Thirtytwo_signed
| Pbigarray_int64 -> Word_int
| Pbigarray_caml_int -> Word_int
| Pbigarray_native_int -> Word_int
| Pbigarray_complex32 -> Single
| Pbigarray_complex64 -> Double
let bigarray_get unsafe elt_kind layout b args dbg =
bind "ba" b (fun b ->
match (elt_kind : Lambda.bigarray_kind) with
Pbigarray_complex32 | Pbigarray_complex64 ->
let kind = bigarray_word_kind elt_kind in
let sz = bigarray_elt_size elt_kind / 2 in
bind "addr"
(bigarray_indexing unsafe elt_kind layout b args dbg) (fun addr ->
bind "reval"
(Cop(mk_load_mut kind, [addr], dbg)) (fun reval ->
bind "imval"
(Cop(mk_load_mut kind,
[Cop(Cadda, [addr; Cconst_int (sz, dbg)], dbg)], dbg))
(fun imval -> box_complex dbg reval imval)))
| _ ->
Cop(mk_load_mut (bigarray_word_kind elt_kind),
[bigarray_indexing unsafe elt_kind layout b args dbg],
dbg))
let bigarray_set unsafe elt_kind layout b args newval dbg =
bind "ba" b (fun b ->
match (elt_kind : Lambda.bigarray_kind) with
Pbigarray_complex32 | Pbigarray_complex64 ->
let kind = bigarray_word_kind elt_kind in
let sz = bigarray_elt_size elt_kind / 2 in
bind "newval" newval (fun newv ->
bind "addr" (bigarray_indexing unsafe elt_kind layout b args dbg)
(fun addr ->
Csequence(
Cop(Cstore (kind, Assignment), [addr; complex_re newv dbg], dbg),
Cop(Cstore (kind, Assignment),
[Cop(Cadda, [addr; Cconst_int (sz, dbg)], dbg);
complex_im newv dbg],
dbg))))
| _ ->
Cop(Cstore (bigarray_word_kind elt_kind, Assignment),
[bigarray_indexing unsafe elt_kind layout b args dbg; newval],
dbg))
(* the three functions below assume either 32-bit or 64-bit words *)
let () = assert (size_int = 4 || size_int = 8)
(* low_32 x is a value which agrees with x on at least the low 32 bits *)
let rec low_32 dbg = function
| x when size_int = 4 -> x
(* Ignore sign and zero extensions, which do not affect the low bits *)
| Cop(Casr, [Cop(Clsl, [x; Cconst_int (32, _)], _);
Cconst_int (32, _)], _)
| Cop(Cand, [x; Cconst_natint (0xFFFFFFFFn, _)], _) ->
low_32 dbg x
| Clet(id, e, body) ->
Clet(id, e, low_32 dbg body)
| x -> x
(* sign_extend_32 sign-extends values from 32 bits to the word size.
(if the word size is 32, this is a no-op) *)
let sign_extend_32 dbg e =