Source file vmbytegen.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
open Util
open Names
open Vmvalues
open Vmbytecodes
open Vmemitcodes
open Genlambda
open Vmlambda
open Constr
open Declarations
open Environ
module Fv_elem =
struct
type t = fv_elem
let compare e1 e2 = match e1, e2 with
| FVnamed id1, FVnamed id2 -> Id.compare id1 id2
| FVnamed _, (FVrel _ | FVuniv_var _ | FVevar _) -> -1
| FVrel _, FVnamed _ -> 1
| FVrel r1, FVrel r2 -> Int.compare r1 r2
| FVrel _, (FVuniv_var _ | FVevar _) -> -1
| FVuniv_var i1, FVuniv_var i2 -> Int.compare i1 i2
| FVuniv_var _, (FVnamed _ | FVrel _) -> 1
| FVuniv_var _, FVevar _ -> -1
| FVevar _, (FVnamed _ | FVrel _ | FVuniv_var _) -> 1
| FVevar e1, FVevar e2 -> Evar.compare e1 e2
end
module FvMap = Map.Make(Fv_elem)
type vm_env = {
size : int;
fv_rev : fv_elem list;
fv_fwd : int FvMap.t;
}
type comp_env = {
arity : int;
nb_uni_stack : int ;
nb_stack : int;
in_stack : int Range.t;
pos_rec : instruction array;
offset : int;
in_env : vm_env ref;
max_stack_size : int ref;
}
type glob_env = {
env : Environ.env;
mutable fun_code : instruction list; (** Code of closures *)
}
let push_fun env c =
env.fun_code <- Ksequence c :: env.fun_code
module Config = struct
let stack_threshold = 256
let stack_safety_margin = 15
end
type argument = ArgLambda of lambda | ArgUniv of Univ.Level.t
let empty_fv = { size= 0; fv_rev = []; fv_fwd = FvMap.empty }
let push_fv d e = {
size = e.size + 1;
fv_rev = d :: e.fv_rev;
fv_fwd = FvMap.add d e.size e.fv_fwd;
}
let fv r = !(r.in_env)
let empty_comp_env ()=
{ arity = 0;
nb_uni_stack = 0;
nb_stack = 0;
in_stack = Range.empty;
pos_rec = [||];
offset = 0;
in_env = ref empty_fv;
max_stack_size = ref 0;
}
let set_max_stack_size (cenv : comp_env) stack_size =
if stack_size > cenv.max_stack_size.contents then
cenv.max_stack_size := stack_size
let ensure_stack_capacity (cenv : comp_env) code =
let used_safe =
cenv.max_stack_size.contents + Config.stack_safety_margin
in
if used_safe > Config.stack_threshold then
Kensurestackcapacity used_safe :: code
else code
let rec add_param n sz l =
if Int.equal n 0 then l else add_param (n - 1) sz (Range.cons (n+sz) l)
let comp_env_fun ?(univs=0) arity =
{ arity;
nb_uni_stack = univs ;
nb_stack = arity;
in_stack = add_param arity 0 Range.empty;
pos_rec = [||];
offset = 0;
in_env = ref empty_fv;
max_stack_size = ref 0;
}
let comp_env_fix_type rfv =
{ arity = 0;
nb_uni_stack = 0;
nb_stack = 0;
in_stack = Range.empty;
pos_rec = [||];
offset = 0;
in_env = rfv;
max_stack_size = ref 0;
}
let comp_env_fix ndef arity rfv =
{ arity;
nb_uni_stack = 0;
nb_stack = arity;
in_stack = add_param arity 0 Range.empty;
pos_rec = Array.init ndef (fun i -> Koffsetclosure i);
offset = 0;
in_env = rfv;
max_stack_size = ref 0;
}
let comp_env_cofix_type ndef rfv =
{ arity = 0;
nb_uni_stack = 0;
nb_stack = 0;
in_stack = Range.empty;
pos_rec = [||];
offset = ndef;
in_env = rfv;
max_stack_size = ref 0;
}
let comp_env_cofix ndef arity rfv =
{ arity;
nb_uni_stack = 0;
nb_stack = arity;
in_stack = add_param arity 0 Range.empty;
pos_rec = Array.init ndef (fun i -> Kenvacc (ndef - 1 - i));
offset = ndef;
in_env = rfv;
max_stack_size = ref 0;
}
let push_param n sz r =
{ r with
nb_stack = r.nb_stack + n;
in_stack = add_param n sz r.in_stack }
let push_local sz r =
{ r with
nb_stack = r.nb_stack + 1;
in_stack = Range.cons (sz + 1) r.in_stack }
let find_at fv env = FvMap.find fv env.fv_fwd
let pos_named id r =
let env = !(r.in_env) in
let cid = FVnamed id in
try Kenvacc(r.offset + find_at cid env)
with Not_found ->
let pos = env.size in
r.in_env := push_fv cid env;
Kenvacc (r.offset + pos)
let pos_rel i r sz =
if i <= r.nb_stack then
Kacc(sz - (Range.get r.in_stack (i-1)))
else
let i = i - r.nb_stack in
let nb_rec = Array.length r.pos_rec in
if i <= nb_rec then
r.pos_rec.(i - 1)
else
let i = i - nb_rec in
let db = FVrel(i) in
let env = !(r.in_env) in
try Kenvacc(r.offset + find_at db env)
with Not_found ->
let pos = env.size in
r.in_env := push_fv db env;
Kenvacc(r.offset + pos)
let pos_universe_var i r sz =
if r.nb_uni_stack != 0 then
Kacc (sz - r.arity - (r.nb_uni_stack - i))
else
let env = !(r.in_env) in
let db = FVuniv_var i in
try Kenvacc (r.offset + find_at db env)
with Not_found ->
let pos = env.size in
r.in_env := push_fv db env;
Kenvacc(r.offset + pos)
let pos_evar evk r =
let env = !(r.in_env) in
let cid = FVevar evk in
try Kenvacc(r.offset + find_at cid env)
with Not_found ->
let pos = env.size in
r.in_env := push_fv cid env;
Kenvacc (r.offset + pos)
let rec discard_dead_code = function
| [] -> []
| (Klabel _ | Krestart ) :: _ as cont -> cont
| _ :: cont -> discard_dead_code cont
let label_code = function
| Klabel lbl :: _ as cont -> (lbl, cont)
| Kbranch lbl :: _ as cont -> (lbl, cont)
| cont -> let lbl = Label.create() in (lbl, Klabel lbl :: cont)
let rec make_branch_2 lbl n cont =
function
Kreturn m :: _ -> (Kreturn (n + m), cont)
| Klabel _ :: c -> make_branch_2 lbl n cont c
| Kpop m :: c -> make_branch_2 lbl (n + m) cont c
| _ ->
match lbl with
Some lbl -> (Kbranch lbl, cont)
| None -> let lbl = Label.create() in (Kbranch lbl, Klabel lbl :: cont)
let make_branch cont =
match cont with
(Kbranch _ as branch) :: _ -> (branch, cont)
| (Kreturn _ as return) :: _ -> (return, cont)
| Klabel lbl :: _ -> make_branch_2 (Some lbl) 0 cont cont
| _ -> make_branch_2 (None) 0 cont cont
let rec is_tailcall = function
| Kreturn k :: _ -> Some k
| Klabel _ :: c -> is_tailcall c
| _ -> None
let rec add_pop n = function
| Kpop m :: cont -> add_pop (n+m) cont
| Kreturn m:: cont -> Kreturn (n+m) ::cont
| cont -> if Int.equal n 0 then cont else Kpop n :: cont
let add_grab arity lbl cont =
if Int.equal arity 1 then Klabel lbl :: cont
else Krestart :: Klabel lbl :: Kgrab (arity - 1) :: cont
let add_grabrec rec_arg arity lbl cont =
if Int.equal arity 1 && rec_arg < arity then
Klabel lbl :: Kgrabrec 0 :: Krestart :: cont
else
Krestart :: Klabel lbl :: Kgrabrec rec_arg ::
Krestart :: Kgrab (arity - 1) :: cont
let cont_cofix arity =
[ Kpush;
Kpush;
Kacc 2;
Kfield 2;
Kfield 0;
Kmakeblock(2, cofix_evaluated_tag);
Kpush;
Kacc 2;
Ksetfield 2;
Kacc 0;
Kreturn (arity+2) ]
let nest_block tag arity cont =
Kconst (Const_b0 (tag - Obj.last_non_constant_constructor_tag)) ::
Kmakeblock(arity+1, Obj.last_non_constant_constructor_tag) :: cont
let code_makeblock cenv ~stack_size ~arity ~tag cont =
if tag < Obj.last_non_constant_constructor_tag then
Kmakeblock(arity, tag) :: cont
else begin
set_max_stack_size cenv (stack_size + 1);
Kpush :: nest_block tag arity cont
end
let compile_structured_constant cenv sc sz cont =
set_max_stack_size cenv sz;
Kconst sc :: cont
let comp_args comp_expr cenv args sz cont =
let nargs_m_1 = Array.length args - 1 in
let c = ref (comp_expr cenv args.(0) (sz + nargs_m_1) cont) in
for i = 1 to nargs_m_1 do
c := comp_expr cenv args.(i) (sz + nargs_m_1 - i) (Kpush :: !c)
done;
!c
let comp_app comp_fun comp_arg cenv f args sz cont =
let nargs = Array.length args in
if Int.equal nargs 0 then comp_fun cenv f sz cont
else
match is_tailcall cont with
| Some k ->
comp_args comp_arg cenv args sz
(Kpush ::
comp_fun cenv f (sz + nargs)
(Kappterm(nargs, k + nargs) :: (discard_dead_code cont)))
| None ->
if nargs <= 4 then
comp_args comp_arg cenv args sz
(Kpush :: (comp_fun cenv f (sz+nargs) (Kshort_apply nargs :: cont)))
else
let lbl,cont1 = label_code cont in
Kpush_retaddr lbl ::
(comp_args comp_arg cenv args (sz + 3)
(Kpush :: (comp_fun cenv f (sz+3+nargs) (Kapply nargs :: cont1))))
let compile_fv_elem cenv fv sz cont =
match fv with
| FVrel i -> pos_rel i cenv sz :: cont
| FVnamed id -> pos_named id cenv :: cont
| FVuniv_var i -> pos_universe_var i cenv sz :: cont
| FVevar evk -> pos_evar evk cenv :: cont
let rec compile_fv cenv l sz cont =
match l with
| [] -> cont
| [fvn] ->
let () = set_max_stack_size cenv (sz + 1) in
compile_fv_elem cenv fvn sz cont
| fvn :: tl ->
compile_fv_elem cenv fvn sz
(Kpush :: compile_fv cenv tl (sz + 1) cont)
let rec get_alias env kn =
let cb = lookup_constant kn env in
let tps = cb.const_body_code in
match tps with
| None -> kn
| Some tps ->
(match tps with
| BCalias kn' -> get_alias env kn'
| _ -> kn)
let get_caml_prim = let open CPrimitives in function
| Arraymake -> Some CAML_Arraymake
| Arrayget -> Some CAML_Arrayget
| Arraydefault -> Some CAML_Arraydefault
| Arrayset -> Some CAML_Arrayset
| Arraycopy -> Some CAML_Arraycopy
| Arraylength -> Some CAML_Arraylength
| _ -> None
let rec compile_lam env cenv lam sz cont =
let () = set_max_stack_size cenv sz in
match lam with
| Lrel(_, i) -> pos_rel i cenv sz :: cont
| Lint i -> compile_structured_constant cenv (Const_b0 i) sz cont
| Lval v -> compile_structured_constant cenv (Const_val v) sz cont
| Luint i -> compile_structured_constant cenv (Const_uint i) sz cont
| Lfloat f -> compile_structured_constant cenv (Const_float f) sz cont
| Lproj (p,arg) ->
compile_lam env cenv arg sz (Kproj (Projection.Repr.arg p) :: cont)
| Lvar id -> pos_named id cenv :: cont
| Levar (evk, args) ->
if Array.is_empty args then
compile_fv_elem cenv (FVevar evk) sz cont
else
(** Arguments are reversed in evar instances *)
let args = Array.copy args in
let () = Array.rev args in
comp_app compile_fv_elem (compile_lam env) cenv (FVevar evk) args sz cont
| Lconst (kn,u) -> compile_constant env cenv kn u [||] sz cont
| Lind (ind,u) ->
if Univ.Instance.is_empty u then
compile_structured_constant cenv (Const_ind ind) sz cont
else comp_app compile_structured_constant compile_universe cenv
(Const_ind ind) (Univ.Instance.to_array u) sz cont
| Lsort s ->
let s, subs = match s with
| Sorts.Set | Sorts.Prop | Sorts.SProp as s -> s, []
| Sorts.Type u ->
let u, s = Univ.compact_univ u in
Sorts.sort_of_univ u, s
| Sorts.QSort (q, u) ->
let u, s = Univ.compact_univ u in
Sorts.qsort q u, s
in
let compile_get_univ cenv idx sz cont =
let () = set_max_stack_size cenv sz in
compile_fv_elem cenv (FVuniv_var idx) sz cont
in
if List.is_empty subs then
compile_structured_constant cenv (Const_sort s) sz cont
else
comp_app compile_structured_constant compile_get_univ cenv
(Const_sort s) (Array.of_list subs) sz cont
| Llet (_id,def,body) ->
compile_lam env cenv def sz
(Kpush ::
compile_lam env (push_local sz cenv) body (sz+1) (add_pop 1 cont))
| Lprod (dom,codom) ->
let cont1 =
Kpush :: compile_lam env cenv dom (sz+1) (Kmakeblock (2,0) :: cont) in
compile_lam env cenv codom sz cont1
| Llam (ids,body) ->
let arity = Array.length ids in
let r_fun = comp_env_fun arity in
let lbl_fun = Label.create() in
let cont_fun = compile_lam env r_fun body arity [Kreturn arity] in
let cont_fun = ensure_stack_capacity r_fun cont_fun in
let () = push_fun env (add_grab arity lbl_fun cont_fun) in
let fv = fv r_fun in
compile_fv cenv fv.fv_rev sz (Kclosure(lbl_fun,fv.size) :: cont)
| Lapp (f, args) ->
begin match f with
| Lconst (kn,u) -> compile_constant env cenv kn u args sz cont
| _ -> comp_app (compile_lam env) (compile_lam env) cenv f args sz cont
end
| Lfix ((rec_args, _, init), (_decl, types, bodies)) ->
let ndef = Array.length types in
let rfv = ref empty_fv in
let lbl_types = Array.make ndef Label.no in
let lbl_bodies = Array.make ndef Label.no in
for i = 0 to ndef - 1 do
let env_type = comp_env_fix_type rfv in
let fcode = compile_lam env env_type types.(i) 0 [Kstop] in
let fcode = ensure_stack_capacity env_type fcode in
let lbl,fcode = label_code fcode in
lbl_types.(i) <- lbl;
push_fun env fcode
done;
for i = 0 to ndef - 1 do
let params,body = decompose_Llam bodies.(i) in
let arity = Array.length params in
let env_body = comp_env_fix ndef arity rfv in
let cont1 = compile_lam env env_body body arity [Kreturn arity] in
let cont1 = ensure_stack_capacity env_body cont1 in
let lbl = Label.create () in
lbl_bodies.(i) <- lbl;
let fcode = add_grabrec rec_args.(i) arity lbl cont1 in
push_fun env fcode
done;
let fv = !rfv in
compile_fv cenv fv.fv_rev sz
(Kclosurerec(fv.size,init,lbl_types,lbl_bodies) :: cont)
| Lcofix(init, (_decl,types,bodies)) ->
let ndef = Array.length types in
let lbl_types = Array.make ndef Label.no in
let lbl_bodies = Array.make ndef Label.no in
let rfv = ref empty_fv in
for i = 0 to ndef - 1 do
let env_type = comp_env_cofix_type ndef rfv in
let fcode = compile_lam env env_type types.(i) 0 [Kstop] in
let fcode = ensure_stack_capacity env_type fcode in
let lbl,fcode = label_code fcode in
lbl_types.(i) <- lbl;
push_fun env fcode
done;
for i = 0 to ndef - 1 do
let params,body = decompose_Llam bodies.(i) in
let arity = Array.length params in
let env_body = comp_env_cofix ndef arity rfv in
let lbl = Label.create () in
let () = set_max_stack_size env_body (arity + 4) in
let cont = compile_lam env env_body body (arity+1) (cont_cofix arity) in
let cont = ensure_stack_capacity env_body cont in
lbl_bodies.(i) <- lbl;
push_fun env (add_grab (arity+1) lbl cont)
done;
let fv = !rfv in
let () = set_max_stack_size cenv (sz + fv.size + ndef + 2) in
compile_fv cenv fv.fv_rev sz
(Kclosurecofix(fv.size, init, lbl_types, lbl_bodies) :: cont)
| Lcase ((ci, rtbl, _), t, a, branches) ->
let ind = ci.ci_ind in
let mib = lookup_mind (fst ind) env.env in
let oib = mib.mind_packets.(snd ind) in
let lbl_consts = Array.make oib.mind_nb_constant Label.no in
let nallblock = oib.mind_nb_args + 1 in
let nconst = Array.length branches.constant_branches in
let nblock = min nallblock (Obj.last_non_constant_constructor_tag + 1) in
let lbl_blocks = Array.make nblock Label.no in
let neblock = max 0 (nallblock - Obj.last_non_constant_constructor_tag) in
let lbl_eblocks = Array.make neblock Label.no in
let branch1, cont = make_branch cont in
let ret_env = { cenv with max_stack_size = ref 0 } in
let fcode = compile_lam env ret_env t sz [Kpop sz; Kstop] in
let fcode = ensure_stack_capacity ret_env fcode in
let lbl_typ,fcode = label_code fcode in
let () = push_fun env fcode in
let lbl_sw = Label.create () in
let sz_b,branch,is_tailcall =
match branch1 with
| Kreturn k ->
assert (Int.equal k sz) ;
sz, branch1, true
| Kbranch _ -> sz+3, Kjump, false
| _ -> assert false
in
let cont = discard_dead_code cont in
let c = ref cont in
if neblock <> 0 then begin
let lbl_b, code_b =
label_code (
Kpush :: Kfield 0 :: Kswitch(lbl_eblocks, [||]) :: !c) in
lbl_blocks.(Obj.last_non_constant_constructor_tag) <- lbl_b;
c := code_b
end;
for i = nconst - 1 downto 0 do
let aux =
compile_lam env cenv branches.constant_branches.(i) sz_b (branch::!c)
in
let lbl_b,code_b = label_code aux in
lbl_consts.(i) <- lbl_b;
c := code_b
done;
for i = nallblock - 2 downto 0 do
let tag = i + 1 in
let (ids, body) = branches.nonconstant_branches.(i) in
let arity = Array.length ids in
let code_b =
compile_lam env (push_param arity sz_b cenv)
body (sz_b+arity) (add_pop arity (branch::!c)) in
let code_b =
if tag < Obj.last_non_constant_constructor_tag then begin
set_max_stack_size cenv (sz_b + arity);
Kpushfields arity :: code_b
end
else begin
set_max_stack_size cenv (sz_b + arity + 1);
Kacc 0::Kpop 1::Kpushfields(arity+1)::Kpop 1::code_b
end
in
let lbl_b, code_b = label_code code_b in
if tag < Obj.last_non_constant_constructor_tag then lbl_blocks.(tag) <- lbl_b
else lbl_eblocks.(tag - Obj.last_non_constant_constructor_tag) <- lbl_b;
c := code_b
done;
let annot =
{rtbl = rtbl; tailcall = is_tailcall;
Vmvalues.max_stack_size = cenv.max_stack_size.contents - sz}
in
let lbl_accu, code_accu =
set_max_stack_size cenv (sz+3);
label_code(Kmakeswitchblock(lbl_typ,lbl_sw,annot,sz) :: branch :: !c)
in
lbl_blocks.(0) <- lbl_accu;
c := Klabel lbl_sw :: Kswitch(lbl_consts,lbl_blocks) :: code_accu;
let code_sw =
match branch1 with
| Kbranch lbl -> Kpush_retaddr lbl :: !c
| _ -> !c
in
compile_lam env cenv a sz code_sw
| Lmakeblock (_, tag, args) ->
let arity = Array.length args in
let cont = code_makeblock cenv ~stack_size:(sz+arity-1) ~arity ~tag cont in
if Int.equal arity 0 then cont
else comp_args (compile_lam env) cenv args sz cont
| Lparray (args, def) ->
let dummy = KerName.make (ModPath.MPfile DirPath.empty) (Names.Label.of_id @@ Id.of_string "dummy") in
let dummy = (MutInd.make1 dummy, 0) in
let ar = Lmakeblock (dummy, 0, args) in
let kind = Lmakeblock (dummy, 0, [|ar; def|]) in
let v = Lmakeblock (dummy, 0, [|kind|]) in
compile_lam env cenv v sz cont
| Lprim (kn, op, args) ->
begin match get_caml_prim op with
| Some cop ->
let arity = CPrimitives.arity op in
let nparams = CPrimitives.nparams op in
let nargs = arity - nparams in
assert (arity = Array.length args && arity <= 4 && nargs >= 1);
let (jump, cont) = make_branch cont in
let lbl_default = Label.create () in
let default =
let cont = [Kshort_apply arity; jump] in
let cont = Kpush :: compile_get_global cenv kn (sz + arity) cont in
let cont =
if Int.equal nparams 0 then cont
else
let params = Array.sub args 0 nparams in
Kpush :: comp_args (compile_lam env) cenv params (sz + nargs) cont in
Klabel lbl_default :: cont in
let () = push_fun env default in
let cont = Kcamlprim (cop, lbl_default) :: cont in
comp_args (compile_lam env) cenv (Array.sub args nparams nargs) sz cont
| None ->
comp_args (compile_lam env) cenv args sz (Kprim(op, kn)::cont)
end
| Lforce -> CErrors.anomaly Pp.(str "The VM should not use force")
and compile_get_global cenv (kn,u) sz cont =
let () = set_max_stack_size cenv sz in
if Univ.Instance.is_empty u then
Kgetglobal kn :: cont
else
comp_app (fun _ _ _ cont -> Kgetglobal kn :: cont)
compile_universe cenv () (Univ.Instance.to_array u) sz cont
and compile_universe cenv uni sz cont =
let () = set_max_stack_size cenv sz in
match Univ.Level.var_index uni with
| None -> compile_structured_constant cenv (Const_univ_level uni) sz cont
| Some idx -> pos_universe_var idx cenv sz :: cont
and compile_constant env cenv kn u args sz cont =
let () = set_max_stack_size cenv sz in
if Univ.Instance.is_empty u then
comp_app (fun _ _ sz cont ->
compile_get_global cenv (kn,u) sz cont)
(compile_lam env) cenv () args sz cont
else
let compile_arg cenv constr_or_uni sz cont =
match constr_or_uni with
| ArgLambda t -> compile_lam env cenv t sz cont
| ArgUniv uni -> compile_universe cenv uni sz cont
in
let u = Univ.Instance.to_array u in
let lu = Array.length u in
let all =
Array.init (lu + Array.length args)
(fun i -> if i < lu then ArgUniv u.(i) else ArgLambda args.(i-lu))
in
comp_app (fun _ _ _ cont -> Kgetglobal kn :: cont)
compile_arg cenv () all sz cont
let is_univ_copy max u =
let u = Univ.Instance.to_array u in
if Array.length u = max then
Array.fold_left_i (fun i acc u ->
if acc then
match Univ.Level.var_index u with
| None -> false
| Some l -> l = i
else false) true u
else
false
let dump_bytecode = ref false
let dump_bytecodes init code fvs =
let open Pp in
(str "code =" ++ fnl () ++
pp_bytecodes init ++ fnl () ++
pp_bytecodes code ++ fnl () ++
str "fv = " ++
prlist_with_sep (fun () -> str "; ") pp_fv_elem fvs ++
fnl ())
let compile ~fail_on_error ?universes:(universes=0) env sigma c =
Label.reset_label_counter ();
let cont = [Kstop] in
try
let cenv, init_code, fun_code =
if Int.equal universes 0 then
let lam = lambda_of_constr ~optimize:true env sigma c in
let cenv = empty_comp_env () in
let env = { env; fun_code = [] } in
let cont = compile_lam env cenv lam 0 cont in
let cont = ensure_stack_capacity cenv cont in
cenv, cont, env.fun_code
else
let lam = lambda_of_constr ~optimize:true env sigma c in
let params, body = decompose_Llam lam in
let arity = Array.length params in
let cenv = empty_comp_env () in
let full_arity = arity + universes in
let r_fun = comp_env_fun ~univs:universes arity in
let lbl_fun = Label.create () in
let env = { env; fun_code = [] } in
let cont_fun = compile_lam env r_fun body full_arity [Kreturn full_arity] in
let cont_fun = ensure_stack_capacity r_fun cont_fun in
let () = push_fun env (add_grab full_arity lbl_fun cont_fun) in
let fv = fv r_fun in
let init_code = compile_fv cenv fv.fv_rev 0 (Kclosure(lbl_fun,fv.size) :: cont) in
let init_code = ensure_stack_capacity cenv init_code in
cenv, init_code, env.fun_code
in
let fv = List.rev (!(cenv.in_env).fv_rev) in
(if !dump_bytecode then
Feedback.msg_debug (dump_bytecodes init_code fun_code fv)) ;
let res = init_code @ fun_code in
Some (to_memory res, Array.of_list fv)
with TooLargeInductive msg as exn ->
let _, info = Exninfo.capture exn in
let fn = if fail_on_error then
CErrors.user_err ?loc:None ~info
else
(fun x -> Feedback.msg_warning x) in
fn msg; None
let compile_constant_body ~fail_on_error env univs = function
| Undef _ | OpaqueDef _ -> Some BCconstant
| Primitive _ -> None
| Def body ->
let instance_size = Univ.AbstractContext.size (Declareops.universes_context univs) in
let alias =
match kind body with
| Const (kn',u) when is_univ_copy instance_size u ->
let con = Constant.make1 (Constant.canonical kn') in
let kn = get_alias env con in
let cb = lookup_constant kn env in
begin match cb.const_body with
| Primitive _ -> None
| _ -> Some kn
end
| _ -> None in
match alias with
| Some kn -> Some (BCalias kn)
| _ ->
let res = compile ~fail_on_error ~universes:instance_size env empty_evars body in
Option.map (fun (code, fv) -> BCdefined (code, fv)) res
let compile_alias kn = BCalias (Constant.make1 (Constant.canonical kn))