-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmmgen.ml
2755 lines (2513 loc) · 98.6 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 Q Public License version 1.0. *)
(* *)
(***********************************************************************)
(* 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
(* Local binding of complex expressions *)
let bind name arg fn =
match arg with
Cvar _ | Cconst_int _ | Cconst_natint _ | Cconst_symbol _
| Cconst_pointer _ | Cconst_natpointer _
| Cconst_blockheader _ -> fn arg
| _ -> let id = Ident.create name in Clet(id, arg, fn (Cvar id))
let bind_nonvar name arg fn =
match arg with
Cconst_int _ | Cconst_natint _ | Cconst_symbol _
| Cconst_pointer _ | Cconst_natpointer _
| Cconst_blockheader _ -> 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 =
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_block_header tag sz = Cconst_blockheader(block_header tag sz)
let alloc_float_header = Cconst_blockheader(float_header)
let alloc_floatarray_header len = Cconst_blockheader(floatarray_header len)
let alloc_closure_header sz = Cconst_blockheader(white_closure_header sz)
let alloc_infix_header ofs = Cconst_blockheader(infix_header ofs)
let alloc_boxedint32_header = Cconst_blockheader(boxedint32_header)
let alloc_boxedint64_header = Cconst_blockheader(boxedint64_header)
let alloc_boxedintnat_header = Cconst_blockheader(boxedintnat_header)
(* 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 rec add_const c n =
if n = 0 then c
else match c with
| Cconst_int x when no_overflow_add x n -> Cconst_int (x + n)
| Cop(Csubi, [Cconst_int x; c]) when no_overflow_add n x ->
Cop(Csubi, [Cconst_int (n + x); c])
| Cop(Csubi, [c; Cconst_int x]) when no_overflow_sub n x ->
add_const c (n - x)
| c -> Cop(Caddi, [c; Cconst_int n])
let incr_int = function
Cconst_int n when n < max_int -> Cconst_int(n+1)
| Cop(Caddi, [c; Cconst_int n]) when n < max_int -> add_const c (n + 1)
| c -> add_const c 1
let decr_int = function
Cconst_int n when n > min_int -> Cconst_int(n-1)
| Cop(Caddi, [c; Cconst_int n]) when n > min_int -> add_const c (n - 1)
| c -> add_const c (-1)
let add_int c1 c2 =
match (c1, c2) with
(Cop(Caddi, [c1; Cconst_int n1]),
Cop(Caddi, [c2; Cconst_int n2])) when no_overflow_add n1 n2 ->
add_const (Cop(Caddi, [c1; c2])) (n1 + n2)
| (Cop(Caddi, [c1; Cconst_int n1]), c2) ->
add_const (Cop(Caddi, [c1; c2])) n1
| (c1, Cop(Caddi, [c2; Cconst_int n2])) ->
add_const (Cop(Caddi, [c1; c2])) n2
| (Cconst_int _, _) ->
Cop(Caddi, [c2; c1])
| (_, _) ->
Cop(Caddi, [c1; c2])
let sub_int c1 c2 =
match (c1, c2) with
(Cop(Caddi, [c1; Cconst_int n1]),
Cop(Caddi, [c2; Cconst_int n2])) when no_overflow_sub n1 n2 ->
add_const (Cop(Csubi, [c1; c2])) (n1 - n2)
| (Cop(Caddi, [c1; Cconst_int n1]), c2) ->
add_const (Cop(Csubi, [c1; c2])) n1
| (c1, Cop(Caddi, [c2; Cconst_int n2])) when n2 <> min_int ->
add_const (Cop(Csubi, [c1; c2])) (-n2)
| (c1, Cconst_int n) when n <> min_int ->
add_const c1 (-n)
| (c1, c2) ->
Cop(Csubi, [c1; c2])
let mul_int c1 c2 =
match (c1, c2) with
(c, Cconst_int 0) | (Cconst_int 0, 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
| (c, Cconst_int n) | (Cconst_int n, c) when n = 1 lsl Misc.log2 n->
Cop(Clsl, [c; Cconst_int(Misc.log2 n)])
| (c1, c2) ->
Cop(Cmuli, [c1; c2])
let lsl_int c1 c2 =
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)])
| (_, _) ->
Cop(Clsl, [c1; c2])
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 =
match c2 with
Cconst_int 0 ->
c1
| Cconst_int n when n > 0 ->
Cop(Clsr, [ignore_low_bit_int c1; c2])
| _ ->
Cop(Clsr, [c1; c2])
let asr_int c1 c2 =
match c2 with
Cconst_int 0 ->
c1
| Cconst_int n when n > 0 ->
Cop(Casr, [ignore_low_bit_int c1; c2])
| _ ->
Cop(Casr, [c1; c2])
let tag_int = function
Cconst_int n ->
int_const n
| Cop(Casr, [c; Cconst_int n]) when n > 0 ->
Cop(Cor, [asr_int c (Cconst_int (n - 1)); Cconst_int 1])
| c ->
incr_int (lsl_int c (Cconst_int 1))
let force_tag_int = function
Cconst_int n ->
int_const n
| Cop(Casr, [c; Cconst_int n]) when n > 0 ->
Cop(Cor, [asr_int c (Cconst_int (n - 1)); Cconst_int 1])
| c ->
Cop(Cor, [lsl_int c (Cconst_int 1); Cconst_int 1])
let untag_int = function
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)])
| 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)])
| Cop(Cor, [c; Cconst_int 1]) -> Cop(Casr, [c; Cconst_int 1])
| c -> Cop(Casr, [c; Cconst_int 1])
(* 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 rec div_int c1 c2 dbg =
match (c1, c2) with
(c1, Cconst_int 0) ->
Csequence(c1, Cop(Craise (Raise_regular, dbg),
[Cconst_symbol "caml_exn_Division_by_zero"]))
| (c1, Cconst_int 1) ->
c1
| (Cconst_int 0 as c1, c2) ->
Csequence(c2, 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)) in
let t = lsr_int t (Cconst_int (Nativeint.size - l)) in
add_int c1 t);
Cconst_int l])
else if n < 0 then
sub_int (Cconst_int 0) (div_int c1 (Cconst_int (-n)) 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]) in
let t = if m < 0n then Cop(Caddi, [t; c1]) else t in
let t = if p > 0 then Cop(Casr, [t; Cconst_int p]) else t in
add_int t (lsr_int c1 (Cconst_int (Nativeint.size - 1))))
end
| (c1, c2) when !Clflags.fast ->
Cop(Cdivi, [c1; c2])
| (c1, c2) ->
bind "divisor" c2 (fun c2 ->
Cifthenelse(c2,
Cop(Cdivi, [c1; c2]),
Cop(Craise (Raise_regular, dbg),
[Cconst_symbol "caml_exn_Division_by_zero"])))
let mod_int c1 c2 dbg =
match (c1, c2) with
(c1, Cconst_int 0) ->
Csequence(c1, Cop(Craise (Raise_regular, dbg),
[Cconst_symbol "caml_exn_Division_by_zero"]))
| (c1, Cconst_int 1) ->
c1
| (Cconst_int(0 | 1) as c1, c2) ->
Csequence(c2, c1)
| (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)) in
let t = lsr_int t (Cconst_int (Nativeint.size - l)) in
let t = add_int c1 t in
let t = Cop(Cand, [t; Cconst_int (-n)]) in
sub_int c1 t)
else
bind "dividend" c1 (fun c1 ->
sub_int c1 (mul_int (div_int c1 c2 dbg) c2))
| (c1, c2) when !Clflags.fast ->
Cop(Cmodi, [c1; c2])
| (c1, c2) ->
bind "divisor" c2 (fun c2 ->
Cifthenelse(c2,
Cop(Cmodi, [c1; c2]),
Cop(Craise (Raise_regular, dbg),
[Cconst_symbol "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 mkm1 c1 c2 bi dbg =
bind "dividend" c1 (fun c1 ->
bind "divisor" c2 (fun c2 ->
let c = mkop c1 c2 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)]), c, mkm1 c1)
else c))
let safe_div_bi =
safe_divmod_bi div_int (fun c1 -> Cop(Csubi, [Cconst_int 0; c1]))
let safe_mod_bi =
safe_divmod_bi mod_int (fun c1 -> Cconst_int 0)
(* Bool *)
let test_bool = function
Cop(Caddi, [Cop(Clsl, [c; Cconst_int 1]); Cconst_int 1]) -> c
| Cop(Clsl, [c; Cconst_int 1]) -> c
| c -> Cop(Ccmpi Cne, [c; Cconst_int 1])
(* Float *)
let box_float c = Cop(Calloc, [alloc_float_header; c])
let rec unbox_float = function
Cop(Calloc, [header; c]) -> c
| Clet(id, exp, body) -> Clet(id, exp, unbox_float body)
| Cifthenelse(cond, e1, e2) ->
Cifthenelse(cond, unbox_float e1, unbox_float e2)
| Csequence(e1, e2) -> Csequence(e1, unbox_float e2)
| Cswitch(e, tbl, el) -> Cswitch(e, tbl, Array.map unbox_float el)
| Ccatch(n, ids, e1, e2) -> Ccatch(n, ids, unbox_float e1, unbox_float e2)
| Ctrywith(e1, id, e2) -> Ctrywith(unbox_float e1, id, unbox_float e2)
| c -> Cop(Cload Double_u, [c])
(* Complex *)
let box_complex c_re c_im =
Cop(Calloc, [alloc_floatarray_header 2; c_re; c_im])
let complex_re c = Cop(Cload Double_u, [c])
let complex_im c = Cop(Cload Double_u,
[Cop(Cadda, [c; Cconst_int size_float])])
(* 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) ->
Cswitch(sel, index, Array.map remove_unit cases)
| Ccatch(io, ids, body, handler) ->
Ccatch(io, ids, remove_unit body, remove_unit handler)
| 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, dbg), args) ->
Cop(Capply (typ_void, dbg), args)
| Cop(Cextcall(proc, mty, alloc, dbg), args) ->
Cop(Cextcall(proc, typ_void, alloc, dbg), args)
| Cexit (_,_) as c -> c
| Ctuple [] as c -> c
| c -> Csequence(c, Ctuple [])
(* Access to block fields *)
let field_address ptr n =
if n = 0
then ptr
else Cop(Cadda, [ptr; Cconst_int(n * size_addr)])
let get_field ptr n =
Cop(Cload Word, [field_address ptr n])
let set_field ptr n newval =
Cop(Cstore Word, [field_address ptr n; newval])
let header ptr =
Cop(Cload Word, [Cop(Cadda, [ptr; Cconst_int(-size_int)])])
let tag_offset =
if big_endian then -1 else -size_int
let get_tag ptr =
if Proc.word_addressed then (* If byte loads are slow *)
Cop(Cand, [header ptr; Cconst_int 255])
else (* If byte loads are efficient *)
Cop(Cload Byte_unsigned,
[Cop(Cadda, [ptr; Cconst_int(tag_offset)])])
let get_size ptr =
Cop(Clsr, [header ptr; Cconst_int 10])
(* 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 =
Cop(Ccmpi Cne, [Cop(Cand, [hdr; Cconst_int 255]); floatarray_tag])
let is_addr_array_ptr ptr =
Cop(Ccmpi Cne, [get_tag ptr; floatarray_tag])
let addr_array_length hdr = Cop(Clsr, [hdr; Cconst_int wordsize_shift])
let float_array_length hdr = Cop(Clsr, [hdr; Cconst_int numfloat_shift])
let lsl_const c n =
Cop(Clsl, [c; Cconst_int n])
let array_indexing log2size ptr ofs =
match ofs with
Cconst_int n ->
let i = n asr 1 in
if i = 0 then ptr else Cop(Cadda, [ptr; Cconst_int(i lsl log2size)])
| Cop(Caddi, [Cop(Clsl, [c; Cconst_int 1]); Cconst_int 1]) ->
Cop(Cadda, [ptr; lsl_const c log2size])
| Cop(Caddi, [c; Cconst_int n]) ->
Cop(Cadda, [Cop(Cadda, [ptr; lsl_const c (log2size - 1)]);
Cconst_int((n-1) lsl (log2size - 1))])
| _ ->
Cop(Cadda, [Cop(Cadda, [ptr; lsl_const ofs (log2size - 1)]);
Cconst_int((-1) lsl (log2size - 1))])
let addr_array_ref arr ofs =
Cop(Cload Word, [array_indexing log2_size_addr arr ofs])
let unboxed_float_array_ref arr ofs =
Cop(Cload Double_u, [array_indexing log2_size_float arr ofs])
let float_array_ref arr ofs =
box_float(unboxed_float_array_ref arr ofs)
let addr_array_set arr ofs newval =
Cop(Cextcall("caml_modify", typ_void, false, Debuginfo.none),
[array_indexing log2_size_addr arr ofs; newval])
let int_array_set arr ofs newval =
Cop(Cstore Word, [array_indexing log2_size_addr arr ofs; newval])
let float_array_set arr ofs newval =
Cop(Cstore Double_u, [array_indexing log2_size_float arr ofs; newval])
(* String length *)
(* Length of string block *)
let string_length exp =
bind "str" exp (fun str ->
let tmp_var = Ident.create "tmp" in
Clet(tmp_var,
Cop(Csubi,
[Cop(Clsl,
[get_size str;
Cconst_int log2_size_addr]);
Cconst_int 1]),
Cop(Csubi,
[Cvar tmp_var;
Cop(Cload Byte_unsigned,
[Cop(Cadda, [str; Cvar tmp_var])])])))
(* Message sending *)
let lookup_tag obj tag =
bind "tag" tag (fun tag ->
Cop(Cextcall("caml_get_public_method", typ_addr, false, Debuginfo.none),
[obj; tag]))
let lookup_label obj lab =
bind "lab" lab (fun lab ->
let table = Cop (Cload Word, [obj]) in
addr_array_ref table lab)
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 in
Compilenv.need_send_fun arity;
Cop(Capply (typ_addr, dbg),
Cconst_symbol("caml_send" ^ string_of_int arity) ::
obj :: tag :: cache :: args)
(* Allocation *)
let make_alloc_generic set_fn tag wordsize args =
if wordsize <= Config.max_young_wosize then
Cop(Calloc, Cconst_blockheader(block_header tag wordsize) :: args)
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,
fill_fields (idx + 2) el) in
Clet(id,
Cop(Cextcall("caml_alloc", typ_addr, true, Debuginfo.none),
[Cconst_int wordsize; Cconst_int tag]),
fill_fields 1 args)
end
let make_alloc tag args =
make_alloc_generic addr_array_set tag (List.length args) args
let make_float_alloc tag args =
make_alloc_generic float_array_set 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 dbg, [a1; Cconst_int(m lsl n + 1 lsl n - 1)])
| args ->
Cop(Ccheckbound dbg, args)
(* To compile "let rec" over values *)
let fundecls_size fundecls =
let sz = ref (-1) in
List.iter
(fun f -> sz := !sz + 1 + (if f.arity = 1 then 2 else 3))
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(id, exp, body) ->
expr_size (Ident.add id (expr_size env exp) env) body
| Uletrec(bindings, body) ->
expr_size env body
| Uprim(Pmakeblock(tag, mut), 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, sz), _, _) ->
RHS_block sz
| Uprim (Pduprecord (Record_float, sz), _, _) ->
RHS_floatblock sz
| 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 *)
let constant_closures =
ref ([] : (string * ufunction list) list)
(* 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 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])
else arg in
Cop(Calloc, [alloc_header_boxed_int bi;
Cconst_symbol(operations_boxed_int bi);
arg'])
let rec unbox_int bi arg =
match arg with
Cop(Calloc, [hdr; ops; Cop(Clsl, [contents; Cconst_int 32])])
when bi = Pint32 && size_int = 8 && big_endian ->
(* Force sign-extension of low 32 bits *)
Cop(Casr, [Cop(Clsl, [contents; Cconst_int 32]); Cconst_int 32])
| Cop(Calloc, [hdr; ops; contents])
when bi = Pint32 && size_int = 8 && not big_endian ->
(* Force sign-extension of low 32 bits *)
Cop(Casr, [Cop(Clsl, [contents; Cconst_int 32]); Cconst_int 32])
| Cop(Calloc, [hdr; ops; contents]) ->
contents
| Clet(id, exp, body) -> Clet(id, exp, unbox_int bi body)
| Cifthenelse(cond, e1, e2) ->
Cifthenelse(cond, unbox_int bi e1, unbox_int bi e2)
| Csequence(e1, e2) -> Csequence(e1, unbox_int bi e2)
| Cswitch(e, tbl, el) -> Cswitch(e, tbl, Array.map (unbox_int bi) el)
| Ccatch(n, ids, e1, e2) -> Ccatch(n, ids, unbox_int bi e1, unbox_int bi e2)
| Ctrywith(e1, id, e2) -> Ctrywith(unbox_int bi e1, id, unbox_int bi e2)
| _ ->
Cop(Cload(if bi = Pint32 then Thirtytwo_signed else Word),
[Cop(Cadda, [arg; Cconst_int size_addr])])
let make_unsigned_int bi arg =
if bi = Pint32 && size_int = 8
then Cop(Cand, [arg; Cconst_natint 0xFFFFFFFFn])
else 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
let bigarray_indexing unsafe elt_kind layout b args dbg =
let check_bound a1 a2 k =
if unsafe then k else Csequence(make_checkbound dbg [a1;a2], k) in
let rec ba_indexing dim_ofs delta_ofs = function
[] -> assert false
| [arg] ->
bind "idx" (untag_int arg)
(fun idx ->
check_bound (Cop(Cload Word,[field_address b dim_ofs])) idx idx)
| arg1 :: argl ->
let rem = ba_indexing (dim_ofs + delta_ofs) delta_ofs argl in
bind "idx" (untag_int arg1)
(fun idx ->
bind "bound" (Cop(Cload Word, [field_address b dim_ofs]))
(fun bound ->
check_bound bound idx (add_int (mul_int rem bound) idx))) in
let offset =
match 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)) args)
and elt_size =
bigarray_elt_size elt_kind in
let byte_offset =
if elt_size = 1
then offset
else Cop(Clsl, [offset; Cconst_int(log2 elt_size)]) in
Cop(Cadda, [Cop(Cload Word, [field_address b 1]); byte_offset])
let bigarray_word_kind = 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
| Pbigarray_caml_int -> Word
| Pbigarray_native_int -> Word
| Pbigarray_complex32 -> Single
| Pbigarray_complex64 -> Double
let bigarray_get unsafe elt_kind layout b args dbg =
bind "ba" b (fun b ->
match elt_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 ->
box_complex
(Cop(Cload kind, [addr]))
(Cop(Cload kind, [Cop(Cadda, [addr; Cconst_int sz])])))
| _ ->
Cop(Cload (bigarray_word_kind elt_kind),
[bigarray_indexing unsafe elt_kind layout b args dbg]))
let bigarray_set unsafe elt_kind layout b args newval dbg =
bind "ba" b (fun b ->
match elt_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, [addr; complex_re newv]),
Cop(Cstore kind,
[Cop(Cadda, [addr; Cconst_int sz]); complex_im newv]))))
| _ ->
Cop(Cstore (bigarray_word_kind elt_kind),
[bigarray_indexing unsafe elt_kind layout b args dbg; newval]))
let unaligned_load_16 ptr idx =
if Arch.allow_unaligned_access
then Cop(Cload Sixteen_unsigned, [add_int ptr idx])
else
let v1 = Cop(Cload Byte_unsigned, [add_int ptr idx]) in
let v2 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 1)]) in
let b1, b2 = if Arch.big_endian then v1, v2 else v2, v1 in
Cop(Cor, [lsl_int b1 (Cconst_int 8); b2])
let unaligned_set_16 ptr idx newval =
if Arch.allow_unaligned_access
then Cop(Cstore Sixteen_unsigned, [add_int ptr idx; newval])
else
let v1 = Cop(Cand, [Cop(Clsr, [newval; Cconst_int 8]); Cconst_int 0xFF]) in
let v2 = Cop(Cand, [newval; Cconst_int 0xFF]) in
let b1, b2 = if Arch.big_endian then v1, v2 else v2, v1 in
Csequence(
Cop(Cstore Byte_unsigned, [add_int ptr idx; b1]),
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 1); b2]))
let unaligned_load_32 ptr idx =
if Arch.allow_unaligned_access
then Cop(Cload Thirtytwo_unsigned, [add_int ptr idx])
else
let v1 = Cop(Cload Byte_unsigned, [add_int ptr idx]) in
let v2 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 1)]) in
let v3 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 2)]) in
let v4 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 3)]) in
let b1, b2, b3, b4 =
if Arch.big_endian
then v1, v2, v3, v4
else v4, v3, v2, v1 in
Cop(Cor,
[Cop(Cor, [lsl_int b1 (Cconst_int 24); lsl_int b2 (Cconst_int 16)]);
Cop(Cor, [lsl_int b3 (Cconst_int 8); b4])])
let unaligned_set_32 ptr idx newval =
if Arch.allow_unaligned_access
then Cop(Cstore Thirtytwo_unsigned, [add_int ptr idx; newval])
else
let v1 =
Cop(Cand, [Cop(Clsr, [newval; Cconst_int 24]); Cconst_int 0xFF]) in
let v2 =
Cop(Cand, [Cop(Clsr, [newval; Cconst_int 16]); Cconst_int 0xFF]) in
let v3 =
Cop(Cand, [Cop(Clsr, [newval; Cconst_int 8]); Cconst_int 0xFF]) in
let v4 = Cop(Cand, [newval; Cconst_int 0xFF]) in
let b1, b2, b3, b4 =
if Arch.big_endian
then v1, v2, v3, v4
else v4, v3, v2, v1 in
Csequence(
Csequence(
Cop(Cstore Byte_unsigned, [add_int ptr idx; b1]),
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 1); b2])),
Csequence(
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 2); b3]),
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 3); b4])))
let unaligned_load_64 ptr idx =
assert(size_int = 8);
if Arch.allow_unaligned_access
then Cop(Cload Word, [add_int ptr idx])
else
let v1 = Cop(Cload Byte_unsigned, [add_int ptr idx]) in
let v2 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 1)]) in
let v3 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 2)]) in
let v4 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 3)]) in
let v5 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 4)]) in
let v6 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 5)]) in
let v7 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 6)]) in
let v8 = Cop(Cload Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 7)]) in
let b1, b2, b3, b4, b5, b6, b7, b8 =
if Arch.big_endian
then v1, v2, v3, v4, v5, v6, v7, v8
else v8, v7, v6, v5, v4, v3, v2, v1 in
Cop(Cor,
[Cop(Cor,
[Cop(Cor, [lsl_int b1 (Cconst_int (8*7));
lsl_int b2 (Cconst_int (8*6))]);
Cop(Cor, [lsl_int b3 (Cconst_int (8*5));
lsl_int b4 (Cconst_int (8*4))])]);
Cop(Cor,
[Cop(Cor, [lsl_int b5 (Cconst_int (8*3));
lsl_int b6 (Cconst_int (8*2))]);
Cop(Cor, [lsl_int b7 (Cconst_int 8);
b8])])])
let unaligned_set_64 ptr idx newval =
assert(size_int = 8);
if Arch.allow_unaligned_access
then Cop(Cstore Word, [add_int ptr idx; newval])
else
let v1 =
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*7)]); Cconst_int 0xFF]) in
let v2 =
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*6)]); Cconst_int 0xFF]) in
let v3 =
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*5)]); Cconst_int 0xFF]) in
let v4 =
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*4)]); Cconst_int 0xFF]) in
let v5 =
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*3)]); Cconst_int 0xFF]) in
let v6 =
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*2)]); Cconst_int 0xFF]) in
let v7 = Cop(Cand, [Cop(Clsr, [newval; Cconst_int 8]); Cconst_int 0xFF]) in
let v8 = Cop(Cand, [newval; Cconst_int 0xFF]) in
let b1, b2, b3, b4, b5, b6, b7, b8 =
if Arch.big_endian
then v1, v2, v3, v4, v5, v6, v7, v8
else v8, v7, v6, v5, v4, v3, v2, v1 in
Csequence(
Csequence(
Csequence(
Cop(Cstore Byte_unsigned, [add_int ptr idx; b1]),
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 1); b2])),
Csequence(
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 2); b3]),
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 3); b4]))),
Csequence(
Csequence(
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 4); b5]),
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 5); b6])),
Csequence(
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 6); b7]),
Cop(Cstore Byte_unsigned,
[add_int (add_int ptr idx) (Cconst_int 7); b8]))))