From afbf2ba2dbcae481f431099d11c66c1ccee57c0d Mon Sep 17 00:00:00 2001 From: Tom Jack Date: Fri, 25 Oct 2019 00:42:31 -0500 Subject: [PATCH] Less stupid way to combine adjacent drops --- src/passes/9-self_michelson/self_michelson.ml | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/passes/9-self_michelson/self_michelson.ml b/src/passes/9-self_michelson/self_michelson.ml index a10096478..711bf64f2 100644 --- a/src/passes/9-self_michelson/self_michelson.ml +++ b/src/passes/9-self_michelson/self_michelson.ml @@ -372,20 +372,23 @@ let rec opt_tail_fail : michelson -> michelson = Prim (l, p, List.map opt_tail_fail args, annot) | x -> x -let opt_combine_drops : peep2 = function - (* DROP ; DROP ↦ DROP 2 *) - | Prim (_, I_DROP, [], _), Prim (_, I_DROP, [], _) -> - Some [i_dropn 2] - (* DROP ; DROP m ↦ DROP 1+m *) - | Prim (_, I_DROP, [], _), Prim (_, I_DROP, [Int (_, m)], _) -> - Some [i_dropn (1 + Z.to_int m)] - (* DROP n ; DROP ↦ DROP n+1 *) - | Prim (_, I_DROP, [Int (_, n)], _), Prim (_, I_DROP, [], _) -> - Some [i_dropn (Z.to_int n + 1)] - (* DROP n ; DROP m ↦ DROP n+m *) - | Prim (_, I_DROP, [Int (_, n)], _), Prim (_, I_DROP, [Int (_, m)], _) -> - Some [i_dropn (Z.to_int n + Z.to_int m)] - | _ -> None +let rec opt_combine_drops (x : michelson) : michelson = + let rec combine : michelson list -> michelson list = function + | [] -> [] + | Prim (_, I_DROP, [], []) :: xs -> + let xs' = combine xs in + begin match xs' with + | [] -> [Prim (-1, I_DROP, [], [])] + | Prim (_, I_DROP, [], []) :: xs' -> Prim (-1, I_DROP, [Int (-1, Z.of_int 2)], []) :: xs' + | Prim (_, I_DROP, [Int (_, n)], []) :: xs' -> Prim (-1, I_DROP, [Int (-1, Z.of_int (1 + Z.to_int n))], []) :: xs' + | x' :: xs' -> Prim (-1, I_DROP, [], []) :: x' :: xs' + end + | x :: xs -> x :: combine xs in + match x with + | Seq (l, args) -> Seq (l, combine (List.map opt_combine_drops args)) + | Prim (l, p, args, annot) -> + Prim (l, p, List.map opt_combine_drops args, annot) + | x -> x let optimize : michelson -> michelson = fun x -> @@ -400,5 +403,5 @@ let optimize : michelson -> michelson = peephole @@ peep2 opt_swap2 ; ] in let x = iterate_optimizer (sequence_optimizers optimizers) x in - let x = iterate_optimizer (peephole @@ peep2 opt_combine_drops) x in + let x = opt_combine_drops x in x