|
|
@@ -1,3 +1,4 @@
|
|
|
+open Printf
|
|
|
open Types
|
|
|
open Util
|
|
|
|
|
|
@@ -54,12 +55,28 @@ let rec peephole = function
|
|
|
| hd :: tl -> hd :: (peephole tl)
|
|
|
| [] -> []
|
|
|
|
|
|
+(* Count actual instructions, ignoring comments and labels *)
|
|
|
+let count_instrs instrs =
|
|
|
+ let rec trav n = function
|
|
|
+ | [] -> n
|
|
|
+ | (Comment _ | Label _ | EmptyLine) :: tl -> trav n tl
|
|
|
+ | InlineComment (hd, _) :: tl -> trav (trav n [hd]) tl
|
|
|
+ | hd :: tl -> trav (n + 1) tl
|
|
|
+ in trav 0 instrs
|
|
|
+
|
|
|
let phase input =
|
|
|
match input with
|
|
|
| Assembly instrs ->
|
|
|
if args.optimize then (
|
|
|
log_line 1 "- Peephole optimization";
|
|
|
- Assembly (peephole (strip_comments instrs))
|
|
|
+ let oldcount = count_instrs instrs in
|
|
|
+ let instrs = peephole (strip_comments instrs) in
|
|
|
+ let newcount = count_instrs instrs in
|
|
|
+ log_line 1 (sprintf
|
|
|
+ " Optimized %d to %d instructions (%d difference)"
|
|
|
+ oldcount newcount (newcount - oldcount)
|
|
|
+ );
|
|
|
+ Assembly instrs
|
|
|
) else
|
|
|
input
|
|
|
| _ -> raise (InvalidInput "peephole")
|