From a8bd2cac6d7b3d71f2ff3d9ce655e65a827d5775 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 25 Sep 2022 20:14:20 +0100 Subject: Attempt at mul --- src/Micro/Asm/Sdcc.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Micro/Asm/Sdcc.hs b/src/Micro/Asm/Sdcc.hs index 238fe2e..3aa9a36 100644 --- a/src/Micro/Asm/Sdcc.hs +++ b/src/Micro/Asm/Sdcc.hs @@ -1,6 +1,7 @@ module Micro.Asm.Sdcc where import qualified Micro.Ast as A +import Text.Parsec (SourcePos, sourceColumn, sourceLine) toIdent :: A.Ident -> String toIdent id = "_" ++ id @@ -23,6 +24,9 @@ toInit (A.Type t) (A.Num v _) | otherwise = ".dw " ++ show v toInit _ v = ".dw " ++ show v +posToIdent :: SourcePos -> String +posToIdent p = "l" ++ show (sourceLine p) ++ "$" ++ show (sourceColumn p) + header :: String -> [String] header version = [";", "; File created by Micro v" ++ version ++ " (SDCC)", ";"] @@ -38,6 +42,26 @@ area name = "\n\t.area " ++ name globl :: String -> String globl id = "\t.globl " ++ id +mul8 :: A.Expr -> A.Expr -> SourcePos -> [String] +mul8 a b p = do + let loop = posToIdent p + let loop0 = loop ++ "z" + oCode (emit a) + ++ [ "\tor a", + "\tjr z, " ++ toIdent loop0, + "\tld b, a", + "\tdec b", + "\tjr z, " ++ toIdent loop0 + ] + ++ oCode (emit b) + ++ [ "\tld c, a", + toLabel loop True, + "\tadd c", + "\tdjnz " + ++ toIdent loop, + toLabel loop0 True + ] + data Output = Output { oPre :: [String], oInit :: [String], @@ -74,6 +98,15 @@ emit x = o {oCode = oCode (emit a) ++ ["\tsub #" ++ show v]} (A.BinOp A.Minus _ a b) -> o {oCode = oCode (emit a) ++ ["\tld c, a"] ++ oCode (emit b) ++ ["\tld b, a", "\tld a, c", "\tsub b"]} + (A.BinOp A.Mul _ _ (A.Num 0 _)) -> + o {oCode = ["\txor a"]} + (A.BinOp A.Mul _ _ (A.Num 1 _)) -> o + (A.BinOp A.Mul _ a (A.Num 2 _)) -> + o {oCode = oCode (emit a) ++ ["\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.Assign _ (A.Variable id _) b) -> o {oCode = oCode (emit b) ++ ["\tld (" ++ toIdent id ++ "), a"]} _ -> o {oCode = [";; unimplemented " ++ show x]} -- cgit v1.2.3