From 1e33e1510bc5891f040137b0c33fa8d49fe3a4c9 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 25 Sep 2022 20:53:10 +0100 Subject: Slightly better --- src/Micro/Asm/Sdcc.hs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Micro/Asm/Sdcc.hs b/src/Micro/Asm/Sdcc.hs index a3f83ed..af4e7d7 100644 --- a/src/Micro/Asm/Sdcc.hs +++ b/src/Micro/Asm/Sdcc.hs @@ -62,6 +62,17 @@ mul8 a b p = do toLabel loop0 True ] +mul :: A.Expr -> A.Expr -> SourcePos -> [String] +mul a b@(A.Num v _) p + | v == 2 = oCode (emit a) ++ ["\tsla a"] + | v == 4 = oCode (emit a) ++ replicate 2 "\tsla a" + | v == 8 = oCode (emit a) ++ replicate 3 "\tsla a" + | v == 16 = oCode (emit a) ++ replicate 4 "\tsla a" + | v == 32 = oCode (emit a) ++ replicate 5 "\tsla a" + | v < 6 = oCode (emit a) ++ ["\tld c, a"] ++ replicate (fromInteger v - 1) "\tadd c" + | otherwise = mul8 a b p +mul _ _ _ = [";; unimplemented"] + data Output = Output { oPre :: [String], oInit :: [String], @@ -104,14 +115,9 @@ emit x = o {oCode = ["\txor a"]} (A.BinOp A.Mul _ (A.Num 1 _) b) -> o {oCode = oCode (emit b)} (A.BinOp A.Mul _ a (A.Num 1 _)) -> o {oCode = oCode (emit a)} - (A.BinOp A.Mul _ a (A.Num 2 _)) -> - o {oCode = oCode (emit a) ++ ["\tsla a"]} - (A.BinOp A.Mul _ (A.Num 2 _) b) -> - o {oCode = oCode (emit b) ++ ["\tsla a"]} - (A.BinOp A.Mul _ a (A.Num v _)) -> - o {oCode = oCode (emit a) ++ ["\tld c, a"] ++ replicate (fromInteger v - 1) "\tadd c"} - (A.BinOp A.Mul p a b) -> do - o {oCode = mul8 a b p} + (A.BinOp A.Mul pos a b@(A.Num _ _)) -> o {oCode = mul a b pos} + (A.BinOp A.Mul pos a@(A.Num _ _) b) -> o {oCode = mul b a pos} + (A.BinOp A.Mul p a b) -> o {oCode = mul8 a b p} (A.BinOp A.Assign _ (A.Variable id _) b) -> o {oCode = oCode (emit b) ++ ["\tld (" ++ toIdent id ++ "), a"]} _ -> o {oCode = [";; unimplemented " ++ show x]} -- cgit v1.2.3