forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmmgen.ml
3504 lines (3212 loc) · 128 KB
/
cmmgen.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. *)
(* *)
(**************************************************************************)
(* Translation from closed lambda to C-- *)
open Misc
open Arch
open Asttypes
open Primitive
open Types
open Lambda
open Clambda
open Cmm
open Cmx_format
(* Environments used for translation to Cmm. *)
type boxed_number =
| Boxed_float of Debuginfo.t
| Boxed_integer of boxed_integer * Debuginfo.t
type env = {
unboxed_ids : (Ident.t * boxed_number) Ident.tbl;
environment_param : Ident.t option;
}
let empty_env =
{
unboxed_ids =Ident.empty;
environment_param = None;
}
let create_env ~environment_param =
{ unboxed_ids = Ident.empty;
environment_param;
}
let is_unboxed_id id env =
try Some (Ident.find_same id env.unboxed_ids)
with Not_found -> None
let add_unboxed_id id unboxed_id bn env =
{ env with
unboxed_ids = Ident.add id (unboxed_id, bn) env.unboxed_ids;
}
(* Local binding of complex expressions *)
let bind name arg fn =
match arg with
Cvar _ | Cconst_int _ | Cconst_natint _ | Cconst_symbol _
| Cconst_pointer _ | Cconst_natpointer _
| Cblockheader _ -> fn arg
| _ -> let id = Ident.create name in Clet(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 _
| Cconst_pointer _ | Cconst_natpointer _
| Cblockheader _ -> fn arg
| _ -> let id = Ident.create name in Clet(id, arg, fn (Cvar id))
let caml_black = Nativeint.shift_left (Nativeint.of_int 3) 8
(* cf. byterun/gc.h *)
(* Block headers. Meaning of the tag field: see stdlib/obj.ml *)
let floatarray_tag = Cconst_int Obj.double_array_tag
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 alloc_float_header dbg = Cblockheader (float_header, dbg)
let alloc_floatarray_header len dbg = Cblockheader (floatarray_header len, dbg)
let alloc_closure_header sz dbg = Cblockheader (white_closure_header sz, dbg)
let alloc_infix_header ofs dbg = Cblockheader (infix_header ofs, dbg)
let alloc_boxedint32_header dbg = Cblockheader (boxedint32_header, dbg)
let alloc_boxedint64_header dbg = Cblockheader (boxedint64_header, dbg)
let alloc_boxedintnat_header dbg = Cblockheader (boxedintnat_header, dbg)
(* Integers *)
let max_repr_int = max_int asr 1
let min_repr_int = min_int asr 1
let int_const n =
if n <= max_repr_int && n >= min_repr_int
then Cconst_int((n lsl 1) + 1)
else Cconst_natint
(Nativeint.add (Nativeint.shift_left (Nativeint.of_int n) 1) 1n)
let cint_const n =
Cint(Nativeint.add (Nativeint.shift_left (Nativeint.of_int n) 1) 1n)
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)
let rec add_const c n dbg =
if n = 0 then c
else match c with
| Cconst_int x when no_overflow_add x n -> Cconst_int (x + n)
| Cop(Caddi, [Cconst_int x; c], _)
when no_overflow_add n x ->
add_no_overflow n x c dbg
| Cop(Caddi, [c; Cconst_int x], _)
when no_overflow_add n x ->
add_no_overflow n x c dbg
| Cop(Csubi, [Cconst_int x; c], _) when no_overflow_add n x ->
Cop(Csubi, [Cconst_int (n + x); c], dbg)
| Cop(Csubi, [c; Cconst_int x], _) when no_overflow_sub n x ->
add_const c (n - x) dbg
| c -> Cop(Caddi, [c; Cconst_int n], 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)
| (Cop(Caddi, [c1; Cconst_int n1], _), Cconst_int n2)
when 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
let rec mul_int c1 c2 dbg =
match (c1, c2) with
| (c, Cconst_int 0) | (Cconst_int 0, c) -> Csequence (c, Cconst_int 0)
| (c, Cconst_int 1) | (Cconst_int 1, c) ->
c
| (c, Cconst_int(-1)) | (Cconst_int(-1), c) ->
sub_int (Cconst_int 0) 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 no_overflow_mul n k ->
add_const (mul_int c (Cconst_int k) 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
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 n
| Cop(Casr, [c; Cconst_int n], _) when n > 0 ->
Cop(Cor, [asr_int c (Cconst_int (n - 1)) dbg; Cconst_int 1], dbg)
| c ->
incr_int (lsl_int c (Cconst_int 1) dbg) dbg
let force_tag_int i dbg =
match i with
Cconst_int n ->
int_const n
| Cop(Casr, [c; Cconst_int n], dbg) when n > 0 ->
Cop(Cor, [asr_int c (Cconst_int (n - 1)) dbg; Cconst_int 1], dbg)
| c ->
Cop(Cor, [lsl_int c (Cconst_int 1) dbg; Cconst_int 1], dbg)
let untag_int i dbg =
match i with
Cconst_int n -> Cconst_int(n asr 1)
| Cop(Caddi, [Cop(Clsl, [c; Cconst_int 1], _); Cconst_int 1], _) -> c
| 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)
| 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)
| Cop(Cor, [c; Cconst_int 1], _) -> Cop(Casr, [c; Cconst_int 1], dbg)
| c -> Cop(Casr, [c; Cconst_int 1], dbg)
let if_then_else (cond, ifso, ifnot) =
match cond with
| Cconst_int 0 -> ifnot
| Cconst_int 1 -> ifso
| _ ->
Cifthenelse(cond, ifso, ifnot)
(* 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_regular dbg exc =
Csequence(
Cop(Cstore (Thirtytwo_signed, Assignment),
[(Cconst_symbol "caml_backtrace_pos"); Cconst_int 0], dbg),
Cop(Craise Raise_withtrace,[exc], dbg))
let raise_symbol dbg symb =
raise_regular dbg (Cconst_symbol symb)
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)
| (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 in
let t = lsr_int t (Cconst_int (Nativeint.size - l)) dbg in
add_int c1 t dbg);
Cconst_int l], dbg)
else if n < 0 then
sub_int (Cconst_int 0) (div_int c1 (Cconst_int (-n)) 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; Cconst_natint 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) else t in
add_int t (lsr_int c1 (Cconst_int (Nativeint.size - 1)) dbg) dbg)
end
| (c1, c2) when !Clflags.fast || is_safe = Lambda.Unsafe ->
Cop(Cdivi, [c1; c2], dbg)
| (c1, c2) ->
bind "divisor" c2 (fun c2 ->
Cifthenelse(c2,
Cop(Cdivi, [c1; c2], dbg),
raise_symbol dbg "caml_exn_Division_by_zero"))
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)
| (Cconst_int n1, Cconst_int n2) ->
Cconst_int (n1 mod n2)
| (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 in
let t = lsr_int t (Cconst_int (Nativeint.size - l)) dbg in
let t = add_int c1 t dbg in
let t = Cop(Cand, [t; Cconst_int (-n)], 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.fast || is_safe = Lambda.Unsafe ->
(* Flambda already generates that test *)
Cop(Cmodi, [c1; c2], dbg)
| (c1, c2) ->
bind "divisor" c2 (fun c2 ->
Cifthenelse(c2,
Cop(Cmodi, [c1; c2], dbg),
raise_symbol dbg "caml_exn_Division_by_zero"))
(* 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 "dividend" c1 (fun c1 ->
bind "divisor" c2 (fun c2 ->
let c = mkop c1 c2 is_safe dbg in
if Arch.division_crashes_on_overflow
&& (size_int = 4 || bi <> Pint32)
&& not (is_different_from (-1) c2)
then Cifthenelse(Cop(Ccmpi Cne, [c2; Cconst_int(-1)], dbg), c, mkm1 c1 dbg)
else c))
let safe_div_bi is_safe =
safe_divmod_bi div_int is_safe
(fun c1 dbg -> Cop(Csubi, [Cconst_int 0; c1], dbg))
let safe_mod_bi is_safe =
safe_divmod_bi mod_int is_safe (fun _ _ -> Cconst_int 0)
(* Bool *)
let test_bool dbg cmm =
match cmm with
| Cop(Caddi, [Cop(Clsl, [c; Cconst_int 1], _); Cconst_int 1], _) -> c
| Cconst_int n ->
if n = 1 then
Cconst_int 0
else
Cconst_int 1
| c -> Cop(Ccmpi Cne, [c; Cconst_int 1], dbg)
(* Float *)
let box_float dbg c = Cop(Calloc, [alloc_float_header dbg; c], dbg)
let map_ccatch f rec_flag handlers body =
let handlers = List.map
(fun (n, ids, handler) -> (n, ids, f handler))
handlers in
Ccatch(rec_flag, handlers, f body)
let rec unbox_float dbg cmm =
match cmm with
| Cop(Calloc, [_header; c], _) -> c
| Clet(id, exp, body) -> Clet(id, exp, unbox_float dbg body)
| Cifthenelse(cond, e1, e2) ->
Cifthenelse(cond, unbox_float dbg e1, unbox_float dbg e2)
| Csequence(e1, e2) -> Csequence(e1, unbox_float dbg e2)
| Cswitch(e, tbl, el, dbg) ->
Cswitch(e, tbl, Array.map (unbox_float dbg) el, dbg)
| Ccatch(rec_flag, handlers, body) ->
map_ccatch (unbox_float dbg) rec_flag handlers body
| Ctrywith(e1, id, e2) -> Ctrywith(unbox_float dbg e1, id, unbox_float dbg e2)
| c -> Cop(Cload (Double_u, Immutable), [c], 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 (Double_u, Immutable), [c], dbg)
let complex_im c dbg = Cop(Cload (Double_u, Immutable),
[Cop(Cadda, [c; Cconst_int size_float], dbg)], dbg)
(* Unit *)
let return_unit c = Csequence(c, Cconst_pointer 1)
let rec remove_unit = function
Cconst_pointer 1 -> Ctuple []
| Csequence(c, Cconst_pointer 1) -> c
| Csequence(c1, c2) ->
Csequence(c1, remove_unit c2)
| Cifthenelse(cond, ifso, ifnot) ->
Cifthenelse(cond, remove_unit ifso, remove_unit ifnot)
| Cswitch(sel, index, cases, dbg) ->
Cswitch(sel, index, Array.map remove_unit cases, dbg)
| Ccatch(rec_flag, handlers, body) ->
map_ccatch remove_unit rec_flag handlers body
| Ctrywith(body, exn, handler) ->
Ctrywith(remove_unit body, exn, remove_unit handler)
| Clet(id, c1, c2) ->
Clet(id, c1, remove_unit c2)
| Cop(Capply _mty, args, dbg) ->
Cop(Capply typ_void, args, dbg)
| Cop(Cextcall(proc, _mty, alloc, label_after), args, dbg) ->
Cop(Cextcall(proc, typ_void, alloc, label_after), args, dbg)
| Cexit (_,_) as c -> c
| Ctuple [] as c -> c
| c -> Csequence(c, Ctuple [])
(* Access to block fields *)
let field_address ptr n dbg =
if n = 0
then ptr
else Cop(Cadda, [ptr; Cconst_int(n * size_addr)], dbg)
let get_field env ptr n dbg =
let mut =
match env.environment_param with
| None -> Mutable
| Some environment_param ->
match ptr with
| Cvar ptr ->
(* Loads from the current function's closure are immutable. *)
if Ident.same environment_param ptr then Immutable
else Mutable
| _ -> Mutable
in
Cop(Cload (Word_val, mut), [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 = (1 lsl (64 - Config.profinfo_width)) - 1
let get_header ptr dbg =
(* We cannot deem this as [Immutable] due to the presence of [Obj.truncate]
and [Obj.set_tag]. *)
Cop(Cload (Word_int, Mutable),
[Cop(Cadda, [ptr; Cconst_int(-size_int)], 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)
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)
else (* If byte loads are efficient *)
Cop(Cload (Byte_unsigned, Mutable), (* Same comment as [get_header] above *)
[Cop(Cadda, [ptr; Cconst_int(tag_offset)], dbg)], dbg)
let get_size ptr dbg =
Cop(Clsr, [get_header_without_profinfo ptr dbg; Cconst_int 10], 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); floatarray_tag], dbg)
let is_addr_array_ptr ptr dbg =
Cop(Ccmpi Cne, [get_tag ptr dbg; floatarray_tag], dbg)
let addr_array_length hdr dbg =
Cop(Clsr, [hdr; Cconst_int wordsize_shift], dbg)
let float_array_length hdr dbg =
Cop(Clsr, [hdr; Cconst_int numfloat_shift], dbg)
let lsl_const c n dbg =
if n = 0 then c
else Cop(Clsl, [c; Cconst_int n], 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)
| Cop(Caddi, [Cop(Clsl, [c; Cconst_int 1], _); Cconst_int 1], _) ->
Cop(add, [ptr; lsl_const c log2size dbg], dbg)
| Cop(Caddi, [c; Cconst_int n], _) when log2size = 0 ->
Cop(add, [Cop(add, [ptr; untag_int c dbg], dbg); Cconst_int (n asr 1)],
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)
| _ 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)
let addr_array_ref arr ofs dbg =
Cop(Cload (Word_val, Mutable),
[array_indexing log2_size_addr arr ofs dbg], dbg)
let int_array_ref arr ofs dbg =
Cop(Cload (Word_int, Mutable),
[array_indexing log2_size_addr arr ofs dbg], dbg)
let unboxed_float_array_ref arr ofs dbg =
Cop(Cload (Double_u, Mutable),
[array_indexing log2_size_float arr ofs dbg], dbg)
let float_array_ref dbg arr ofs =
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, None),
[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, None),
[array_indexing log2_size_addr arr ofs dbg; newval], dbg)
let int_array_set arr ofs newval dbg =
Cop(Cstore (Word_int, Assignment),
[array_indexing log2_size_addr arr ofs dbg; newval], dbg)
let float_array_set arr ofs newval dbg =
Cop(Cstore (Double_u, 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 = Ident.create "tmp" in
Clet(tmp_var,
Cop(Csubi,
[Cop(Clsl,
[get_size str dbg;
Cconst_int log2_size_addr],
dbg);
Cconst_int 1],
dbg),
Cop(Csubi,
[Cvar tmp_var;
Cop(Cload (Byte_unsigned, Mutable),
[Cop(Cadda, [str; Cvar tmp_var], dbg)], dbg)], dbg)))
(* Message sending *)
let lookup_tag obj tag dbg =
bind "tag" tag (fun tag ->
Cop(Cextcall("caml_get_public_method", typ_val, false, None),
[obj; tag],
dbg))
let lookup_label obj lab dbg =
bind "lab" lab (fun lab ->
let table = Cop (Cload (Word_val, Mutable), [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" ^ string_of_int arity) ::
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, Cblockheader(block_header tag wordsize, dbg) :: args, dbg)
else begin
let id = Ident.create "alloc" in
let rec fill_fields idx = function
[] -> Cvar id
| e1::el -> Csequence(set_fn (Cvar id) (Cconst_int idx) e1 dbg,
fill_fields (idx + 2) el) in
Clet(id,
Cop(Cextcall("caml_alloc", typ_val, true, None),
[Cconst_int wordsize; Cconst_int tag], 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, None),
[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)
| args ->
Cop(Ccheckbound, args, dbg)
(* To compile "let rec" over values *)
let fundecls_size fundecls =
let sz = ref (-1) in
List.iter
(fun f ->
let indirect_call_code_pointer_size =
match f.arity with
| 0 | 1 -> 0
(* arity 1 does not need an indirect call handler.
arity 0 cannot be indirect called *)
| _ -> 1
(* For other arities there is an indirect call handler.
if arity >= 2 it is caml_curry...
if arity < 0 it is caml_tuplify... *)
in
sz := !sz + 1 + 2 + indirect_call_code_pointer_size)
fundecls;
!sz
type rhs_kind =
| RHS_block of int
| RHS_floatblock of int
| RHS_nonrec
;;
let rec expr_size env = function
| Uvar id ->
begin try Ident.find_same id env with Not_found -> RHS_nonrec end
| Uclosure(fundecls, clos_vars) ->
RHS_block (fundecls_size fundecls + List.length clos_vars)
| Ulet(_str, _kind, id, exp, body) ->
expr_size (Ident.add id (expr_size env exp) env) body
| Uletrec(bindings, body) ->
let env =
List.fold_right
(fun (id, exp) env -> Ident.add id (expr_size env exp) env)
bindings env
in
expr_size env body
| Uprim(Pmakeblock _, args, _) ->
RHS_block (List.length args)
| Uprim(Pmakearray((Paddrarray | Pintarray), _), args, _) ->
RHS_block (List.length args)
| Uprim(Pmakearray(Pfloatarray, _), args, _) ->
RHS_floatblock (List.length args)
| Uprim (Pduprecord ((Record_regular | Record_inlined _), sz), _, _) ->
RHS_block sz
| Uprim (Pduprecord (Record_unboxed _, _), _, _) ->
assert false
| Uprim (Pduprecord (Record_extension, sz), _, _) ->
RHS_block (sz + 1)
| Uprim (Pduprecord (Record_float, sz), _, _) ->
RHS_floatblock sz
| Uprim (Pccall { prim_name; _ }, closure::_, _)
when prim_name = "caml_check_value_is_closure" ->
(* Used for "-clambda-checks". *)
expr_size env closure
| Usequence(_exp, exp') ->
expr_size env exp'
| _ -> RHS_nonrec
(* Record application and currying functions *)
let apply_function n =
Compilenv.need_apply_fun n; "caml_apply" ^ string_of_int n
let curry_function n =
Compilenv.need_curry_fun n;
if n >= 0
then "caml_curry" ^ string_of_int n
else "caml_tuplify" ^ string_of_int (-n)
(* Comparisons *)
let transl_comparison = function
Lambda.Ceq -> Ceq
| Lambda.Cneq -> Cne
| Lambda.Cge -> Cge
| Lambda.Cgt -> Cgt
| Lambda.Cle -> Cle
| Lambda.Clt -> Clt
(* Translate structured constants *)
let transl_constant = function
| Uconst_int n ->
int_const n
| Uconst_ptr n ->
if n <= max_repr_int && n >= min_repr_int
then Cconst_pointer((n lsl 1) + 1)
else Cconst_natpointer
(Nativeint.add (Nativeint.shift_left (Nativeint.of_int n) 1) 1n)
| Uconst_ref (label, _) ->
Cconst_symbol label
let transl_structured_constant cst =
let label = Compilenv.new_structured_constant cst ~shared:true in
Cconst_symbol label
(* Translate constant closures *)
type is_global = Global | Not_global
type symbol_defn = string * is_global
type cmm_constant =
| Const_closure of symbol_defn * ufunction list * uconstant list
| Const_table of symbol_defn * data_item list
let cmm_constants =
ref ([] : cmm_constant list)
let add_cmm_constant c =
cmm_constants := c :: !cmm_constants
(* Boxed integers *)
let box_int_constant bi n =
match bi with
Pnativeint -> Uconst_nativeint n
| Pint32 -> Uconst_int32 (Nativeint.to_int32 n)
| Pint64 -> Uconst_int64 (Int64.of_nativeint n)
let operations_boxed_int bi =
match bi with
Pnativeint -> "caml_nativeint_ops"
| Pint32 -> "caml_int32_ops"
| Pint64 -> "caml_int64_ops"
let alloc_header_boxed_int bi =
match bi with
Pnativeint -> alloc_boxedintnat_header
| Pint32 -> alloc_boxedint32_header
| Pint64 -> alloc_boxedint64_header
let box_int dbg bi arg =
match arg with
Cconst_int n ->
transl_structured_constant (box_int_constant bi (Nativeint.of_int n))
| Cconst_natint n ->
transl_structured_constant (box_int_constant bi n)
| _ ->
let arg' =
if bi = Pint32 && size_int = 8 && big_endian
then Cop(Clsl, [arg; Cconst_int 32], dbg)
else arg in
Cop(Calloc, [alloc_header_boxed_int bi dbg;
Cconst_symbol(operations_boxed_int bi);
arg'], dbg)
let split_int64_for_32bit_target arg dbg =
bind "split_int64" arg (fun arg ->
let first = Cop (Cadda, [Cconst_int size_int; arg], dbg) in
let second = Cop (Cadda, [Cconst_int (2 * size_int); arg], dbg) in
Ctuple [Cop (Cload (Thirtytwo_unsigned, Mutable), [first], dbg);
Cop (Cload (Thirtytwo_unsigned, Mutable), [second], dbg)])
let rec unbox_int bi arg dbg =
match arg with
Cop(Calloc, [_hdr; _ops; Cop(Clsl, [contents; Cconst_int 32], dbg')], dbg)
when bi = Pint32 && size_int = 8 && big_endian ->
(* Force sign-extension of low 32 bits *)
Cop(Casr, [Cop(Clsl, [contents; Cconst_int 32], dbg'); Cconst_int 32],
dbg)
| Cop(Calloc, [_hdr; _ops; contents], dbg)
when bi = Pint32 && size_int = 8 && not big_endian ->
(* Force sign-extension of low 32 bits *)
Cop(Casr, [Cop(Clsl, [contents; Cconst_int 32], dbg); Cconst_int 32], dbg)
| Cop(Calloc, [_hdr; _ops; contents], _dbg) ->
contents
| Clet(id, exp, body) -> Clet(id, exp, unbox_int bi body dbg)
| Cifthenelse(cond, e1, e2) ->
Cifthenelse(cond, unbox_int bi e1 dbg, unbox_int bi e2 dbg)
| Csequence(e1, e2) -> Csequence(e1, unbox_int bi e2 dbg)
| Cswitch(e, tbl, el, dbg) ->
Cswitch(e, tbl, Array.map (fun e -> unbox_int bi e dbg) el, dbg)
| Ccatch(rec_flag, handlers, body) ->
map_ccatch (fun e -> unbox_int bi e dbg) rec_flag handlers body
| Ctrywith(e1, id, e2) ->
Ctrywith(unbox_int bi e1 dbg, id, unbox_int bi e2 dbg)
| _ ->
if size_int = 4 && bi = Pint64 then
split_int64_for_32bit_target arg dbg
else
Cop(
Cload((if bi = Pint32 then Thirtytwo_signed else Word_int), Mutable),
[Cop(Cadda, [arg; Cconst_int size_addr], dbg)], dbg)
let make_unsigned_int bi arg dbg =
if bi = Pint32 && size_int = 8
then Cop(Cand, [arg; Cconst_natint 0xFFFFFFFFn], dbg)
else arg
(* Boxed numbers *)
let equal_unboxed_integer ui1 ui2 =
match ui1, ui2 with
| Pnativeint, Pnativeint -> true
| Pint32, Pint32 -> true
| Pint64, Pint64 -> true
| _, _ -> false
let equal_boxed_number bn1 bn2 =
match bn1, bn2 with
| Boxed_float _, Boxed_float _ -> true
| Boxed_integer(ui1, _), Boxed_integer(ui2, _) ->
equal_unboxed_integer ui1 ui2
| _, _ -> false
let box_number bn arg =
match bn with
| Boxed_float dbg -> box_float dbg arg
| Boxed_integer (bi, dbg) -> box_int dbg bi arg
(* Big arrays *)
let bigarray_elt_size = 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