@@ -49,20 +49,25 @@ let function_arity info x =
4949 in
5050 arity info x []
5151
52- let specialize_instr function_arity (acc , free_pc , extra ) i =
52+ let add_event loc instrs =
53+ match loc with
54+ | Some loc -> (Event loc, noloc) :: instrs
55+ | None -> instrs
56+
57+ let specialize_instr function_arity ((acc , free_pc , extra ), loc ) i =
5358 match i with
54- | Let (x , Apply { f; args; exact = false } ), loc when Config.Flag. optcall () -> (
59+ | Let (x , Apply { f; args; exact = false } ), loc' when Config.Flag. optcall () -> (
5560 let n' = List. length args in
5661 match function_arity f with
5762 | None -> i :: acc, free_pc, extra
5863 | Some n when n = n' ->
59- (Let (x, Apply { f; args; exact = true }), loc) :: acc, free_pc, extra
64+ (Let (x, Apply { f; args; exact = true }), loc' ) :: acc, free_pc, extra
6065 | Some n when n < n' ->
6166 let v = Code.Var. fresh () in
6267 let args, rest = List. take n args in
63- ( (Let (v, Apply { f; args; exact = true }), loc )
64- :: (Let (x, Apply { f = v; args = rest; exact = false }), loc)
65- :: acc
68+ ( (* Reversed * )
69+ (Let (x, Apply { f = v; args = rest; exact = false }), loc' )
70+ :: add_event loc (( Let (v, Apply { f; args; exact = true }), loc') :: acc)
6671 , free_pc
6772 , extra )
6873 | Some n when n > n' ->
@@ -74,11 +79,14 @@ let specialize_instr function_arity (acc, free_pc, extra) i =
7479 let return' = Code.Var. fresh () in
7580 { params = params'
7681 ; body =
77- [ Let (return', Apply { f; args = args @ params'; exact = true }), noloc ]
82+ add_event
83+ loc
84+ [ Let (return', Apply { f; args = args @ params'; exact = true }), noloc
85+ ]
7886 ; branch = Return return', noloc
7987 }
8088 in
81- ( (Let (x, Closure (missing, (free_pc, missing))), loc) :: acc
89+ ( (Let (x, Closure (missing, (free_pc, missing))), loc' ) :: acc
8290 , free_pc + 1
8391 , (free_pc, block) :: extra )
8492 | _ -> i :: acc, free_pc, extra)
@@ -88,15 +96,22 @@ let specialize_instrs ~function_arity p =
8896 let blocks, free_pc =
8997 Addr.Map. fold
9098 (fun pc block (blocks , free_pc ) ->
91- let body, free_pc, extra =
92- List. fold_right block.body ~init: ([] , free_pc, [] ) ~f: (fun i acc ->
93- specialize_instr function_arity acc i)
99+ let (body, free_pc, extra), _ =
100+ List. fold_left
101+ block.body
102+ ~init: (([] , free_pc, [] ), None )
103+ ~f: (fun acc i ->
104+ match fst i with
105+ | Event loc ->
106+ let (body, free_pc, extra), _ = acc in
107+ (i :: body, free_pc, extra), Some loc
108+ | _ -> specialize_instr function_arity acc i, None )
94109 in
95110 let blocks =
96111 List. fold_left extra ~init: blocks ~f: (fun blocks (pc , b ) ->
97112 Addr.Map. add pc b blocks)
98113 in
99- Addr.Map. add pc { block with Code. body } blocks, free_pc)
114+ Addr.Map. add pc { block with Code. body = List. rev body } blocks, free_pc)
100115 p.blocks
101116 (Addr.Map. empty, p.free_pc)
102117 in
0 commit comments