module X64Optimiser(x64Optimise) where import Defs import X64 x64Optimise :: Asm -> Error Asm x64Optimise asm = return $ optUnnecessaryJumps $ funcopt optSimpleInstructions $ asm funcopt :: (Func -> Func) -> Asm -> Asm funcopt f (Asm funcs) = Asm (map f funcs) optUnnecessaryJumps :: Asm -> Asm optUnnecessaryJumps (Asm funcs) = Asm $ map goF (zip funcs (tail funcs)) ++ [last funcs] where goF :: (Func, Func) -> Func goF (f1@(_, f1i), (f2n, _)) = case last f1i of JMP n | n == f2n -> fmap init f1 _ -> f1 optSimpleInstructions :: Func -> Func optSimpleInstructions (name, inss) = (name, concat $ map goI inss) where goI :: Ins -> [Ins] goI (MOV (RegMem a) (RegMem b)) | a == b = [] goI (MOVi (RegMem (XReg 8 r)) (Imm (XImm 0))) = [XOR (RegMem (XReg 4 r)) (RegMemImm (XReg 4 r))] goI (MOVi (RegMem a@(XReg _ _)) (Imm (XImm 0))) = [XOR (RegMem a) (RegMemImm a)] goI (MOVi64 (Reg (XReg 8 r)) (Imm (XImm 0))) = [XOR (RegMem (XReg 4 r)) (RegMemImm (XReg 4 r))] goI (MOVi64 (Reg a) (Imm (XImm 0))) = [XOR (RegMem a) (RegMemImm a)] goI (MOVSX (Reg a) (RegMem b)) | a == b = [] goI ins = [ins]