@@ -326,7 +326,10 @@ let rec constant_equal a b =
326
326
| Int _ , (String _ | NativeString _ | Float_array _ | Int64 _ | Tuple (_ , _ , _ )) ->
327
327
Some false
328
328
329
- type loc = Addr .t
329
+ type loc =
330
+ | No
331
+ | Before of Addr .t
332
+ | After of Addr .t
330
333
331
334
type prim_arg =
332
335
| Pv of Var .t
@@ -345,26 +348,26 @@ type expr =
345
348
| Prim of prim * prim_arg list
346
349
347
350
type instr =
348
- | Let of Var .t * expr * loc
349
- | Assign of Var .t * Var .t * loc
350
- | Set_field of Var .t * int * Var .t * loc
351
- | Offset_ref of Var .t * int * loc
352
- | Array_set of Var .t * Var .t * Var .t * loc
351
+ | Let of Var .t * expr
352
+ | Assign of Var .t * Var .t
353
+ | Set_field of Var .t * int * Var .t
354
+ | Offset_ref of Var .t * int
355
+ | Array_set of Var .t * Var .t * Var .t
353
356
354
357
type last =
355
- | Return of Var .t * loc
356
- | Raise of Var .t * [ `Normal | `Notrace | `Reraise ] * loc
357
- | Stop of loc
358
+ | Return of Var .t
359
+ | Raise of Var .t * [ `Normal | `Notrace | `Reraise ]
360
+ | Stop
358
361
| Branch of cont
359
- | Cond of Var .t * cont * cont * loc
360
- | Switch of Var .t * cont array * cont array * loc
361
- | Pushtrap of cont * Var .t * cont * Addr.Set .t * loc
362
+ | Cond of Var .t * cont * cont
363
+ | Switch of Var .t * cont array * cont array
364
+ | Pushtrap of cont * Var .t * cont * Addr.Set .t
362
365
| Poptrap of cont
363
366
364
367
type block =
365
368
{ params : Var .t list
366
- ; body : instr list
367
- ; branch : last
369
+ ; body : ( instr * loc ) list
370
+ ; branch : last * loc
368
371
}
369
372
370
373
type program =
@@ -373,6 +376,9 @@ type program =
373
376
; free_pc : Addr .t
374
377
}
375
378
379
+ let noloc = No
380
+
381
+ let location_of_pc pc = Before pc
376
382
(* ***)
377
383
378
384
module Print = struct
@@ -480,31 +486,31 @@ module Print = struct
480
486
| Constant c -> Format. fprintf f " CONST{%a}" constant c
481
487
| Prim (p , l ) -> prim f p l
482
488
483
- let instr f i =
489
+ let instr f ( i , _loc ) =
484
490
match i with
485
- | Let (x , e , _ ) -> Format. fprintf f " %a = %a" Var. print x expr e
486
- | Assign (x , y , _ ) -> Format. fprintf f " (assign) %a = %a" Var. print x Var. print y
487
- | Set_field (x , i , y , _ ) -> Format. fprintf f " %a[%d] = %a" Var. print x i Var. print y
488
- | Offset_ref (x , i , _ ) -> Format. fprintf f " %a[0] += %d" Var. print x i
489
- | Array_set (x , y , z , _ ) ->
491
+ | Let (x , e ) -> Format. fprintf f " %a = %a" Var. print x expr e
492
+ | Assign (x , y ) -> Format. fprintf f " (assign) %a = %a" Var. print x Var. print y
493
+ | Set_field (x , i , y ) -> Format. fprintf f " %a[%d] = %a" Var. print x i Var. print y
494
+ | Offset_ref (x , i ) -> Format. fprintf f " %a[0] += %d" Var. print x i
495
+ | Array_set (x , y , z ) ->
490
496
Format. fprintf f " %a[%a] = %a" Var. print x Var. print y Var. print z
491
497
492
- let last f l =
498
+ let last f ( l , _loc ) =
493
499
match l with
494
- | Return ( x , _ ) -> Format. fprintf f " return %a" Var. print x
495
- | Raise (x , `Normal, _ ) -> Format. fprintf f " raise %a" Var. print x
496
- | Raise (x , `Reraise, _ ) -> Format. fprintf f " reraise %a" Var. print x
497
- | Raise (x , `Notrace, _ ) -> Format. fprintf f " raise_notrace %a" Var. print x
498
- | Stop _ -> Format. fprintf f " stop"
500
+ | Return x -> Format. fprintf f " return %a" Var. print x
501
+ | Raise (x , `Normal) -> Format. fprintf f " raise %a" Var. print x
502
+ | Raise (x , `Reraise) -> Format. fprintf f " reraise %a" Var. print x
503
+ | Raise (x , `Notrace) -> Format. fprintf f " raise_notrace %a" Var. print x
504
+ | Stop -> Format. fprintf f " stop"
499
505
| Branch c -> Format. fprintf f " branch %a" cont c
500
- | Cond (x , cont1 , cont2 , _ ) ->
506
+ | Cond (x , cont1 , cont2 ) ->
501
507
Format. fprintf f " if %a then %a else %a" Var. print x cont cont1 cont cont2
502
- | Switch (x , a1 , a2 , _ ) ->
508
+ | Switch (x , a1 , a2 ) ->
503
509
Format. fprintf f " switch %a {" Var. print x;
504
510
Array. iteri a1 ~f: (fun i c -> Format. fprintf f " int %d -> %a; " i cont c);
505
511
Array. iteri a2 ~f: (fun i c -> Format. fprintf f " tag %d -> %a; " i cont c);
506
512
Format. fprintf f " }"
507
- | Pushtrap (cont1 , x , cont2 , pcs , _ ) ->
513
+ | Pushtrap (cont1 , x , cont2 , pcs ) ->
508
514
Format. fprintf
509
515
f
510
516
" pushtrap %a handler %a => %a continuation %s"
@@ -518,8 +524,8 @@ module Print = struct
518
524
| Poptrap c -> Format. fprintf f " poptrap %a" cont c
519
525
520
526
type xinstr =
521
- | Instr of instr
522
- | Last of last
527
+ | Instr of ( instr * loc )
528
+ | Last of ( last * loc )
523
529
524
530
let block annot pc block =
525
531
Format. eprintf " ==== %d (%a) ====@." pc var_list block.params;
538
544
let fold_closures p f accu =
539
545
Addr.Map. fold
540
546
(fun _ block accu ->
541
- List. fold_left block.body ~init: accu ~f: (fun accu i ->
547
+ List. fold_left block.body ~init: accu ~f: (fun accu ( i , _loc ) ->
542
548
match i with
543
- | Let (x , Closure (params , cont ), _ ) -> f (Some x) params cont accu
549
+ | Let (x , Closure (params , cont )) -> f (Some x) params cont accu
544
550
| _ -> accu))
545
551
p.blocks
546
552
(f None [] (p.start, [] ) accu)
@@ -559,12 +565,12 @@ let prepend ({ start; blocks; free_pc } as p) body =
559
565
| exception Not_found ->
560
566
let new_start = free_pc in
561
567
let blocks =
562
- Addr.Map. add new_start { params = [] ; body; branch = Stop ( - 1 ) } blocks
568
+ Addr.Map. add new_start { params = [] ; body; branch = Stop , noloc } blocks
563
569
in
564
570
let free_pc = free_pc + 1 in
565
571
{ start = new_start; blocks; free_pc })
566
572
567
- let empty_block = { params = [] ; body = [] ; branch = Stop ( - 1 ) }
573
+ let empty_block = { params = [] ; body = [] ; branch = Stop , noloc }
568
574
569
575
let empty =
570
576
let start = 0 in
@@ -577,28 +583,28 @@ let is_empty p =
577
583
| 1 -> (
578
584
let _, v = Addr.Map. choose p.blocks in
579
585
match v with
580
- | { body; branch = Stop _ ; params = _ } -> (
586
+ | { body; branch = Stop , _ ; params = _ } -> (
581
587
match body with
582
- | ([] | [ Let (_, Prim (Extern " caml_get_global_data" , _), _) ]) when true ->
588
+ | ([] | [ ( Let (_, Prim (Extern " caml_get_global_data" , _) ), _) ]) when true ->
583
589
true
584
590
| _ -> false )
585
591
| _ -> false )
586
592
| _ -> false
587
593
588
594
let fold_children blocks pc f accu =
589
595
let block = Addr.Map. find pc blocks in
590
- match block.branch with
591
- | Return _ | Raise _ | Stop _ -> accu
596
+ match fst block.branch with
597
+ | Return _ | Raise _ | Stop -> accu
592
598
| Branch (pc' , _ ) | Poptrap (pc' , _ ) -> f pc' accu
593
- | Pushtrap ((pc' , _ ), _ , (pc_h , _ ), _ , _ ) ->
599
+ | Pushtrap ((pc' , _ ), _ , (pc_h , _ ), _ ) ->
594
600
let accu = f pc' accu in
595
601
let accu = f pc_h accu in
596
602
accu
597
- | Cond (_ , (pc1 , _ ), (pc2 , _ ), _ ) ->
603
+ | Cond (_ , (pc1 , _ ), (pc2 , _ )) ->
598
604
let accu = f pc1 accu in
599
605
let accu = f pc2 accu in
600
606
accu
601
- | Switch (_ , a1 , a2 , _ ) ->
607
+ | Switch (_ , a1 , a2 ) ->
602
608
let accu = Array. fold_right ~init: accu ~f: (fun (pc , _ ) accu -> f pc accu) a1 in
603
609
let accu = Array. fold_right ~init: accu ~f: (fun (pc , _ ) accu -> f pc accu) a2 in
604
610
accu
@@ -651,7 +657,7 @@ let fold_closures_innermost_first { start; blocks; _ } f accu =
651
657
let block = Addr.Map. find pc blocks in
652
658
List. fold_left block.body ~init: accu ~f: (fun accu i ->
653
659
match i with
654
- | Let (x , Closure (params , cont ), _ ) ->
660
+ | Let (x , Closure (params , cont )) , _ ->
655
661
let accu = visit blocks (fst cont) f accu in
656
662
f (Some x) params cont accu
657
663
| _ -> accu))
@@ -707,27 +713,29 @@ let invariant { blocks; start; _ } =
707
713
| Constant _ -> ()
708
714
| Prim (_ , _ ) -> ()
709
715
in
710
- let check_instr = function
711
- | Let (x , e , _ ) ->
716
+ let check_instr (i , _loc ) =
717
+ match i with
718
+ | Let (x , e ) ->
712
719
define x;
713
720
check_expr e
714
721
| Assign _ -> ()
715
- | Set_field (_ , _i , _ , _ ) -> ()
716
- | Offset_ref (_x , _i , _ ) -> ()
717
- | Array_set (_x , _y , _z , _ ) -> ()
722
+ | Set_field (_ , _i , _ ) -> ()
723
+ | Offset_ref (_x , _i ) -> ()
724
+ | Array_set (_x , _y , _z ) -> ()
718
725
in
719
- let check_last = function
726
+ let check_last (l , _loc ) =
727
+ match l with
720
728
| Return _ -> ()
721
729
| Raise _ -> ()
722
- | Stop _ -> ()
730
+ | Stop -> ()
723
731
| Branch cont -> check_cont cont
724
- | Cond (_x , cont1 , cont2 , _ ) ->
732
+ | Cond (_x , cont1 , cont2 ) ->
725
733
check_cont cont1;
726
734
check_cont cont2
727
- | Switch (_x , a1 , a2 , _ ) ->
735
+ | Switch (_x , a1 , a2 ) ->
728
736
Array. iteri a1 ~f: (fun _ cont -> check_cont cont);
729
737
Array. iteri a2 ~f: (fun _ cont -> check_cont cont)
730
- | Pushtrap (cont1 , _x , cont2 , _pcs , _ ) ->
738
+ | Pushtrap (cont1 , _x , cont2 , _pcs ) ->
731
739
check_cont cont1;
732
740
check_cont cont2
733
741
| Poptrap cont -> check_cont cont
0 commit comments