aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-09-25 20:14:20 +0100
committerJuan J. Martinez <jjm@usebox.net>2022-09-25 20:14:20 +0100
commita8bd2cac6d7b3d71f2ff3d9ce655e65a827d5775 (patch)
treeec38cc48e7b085cdb29d49721e68152cb2b9ac2d /src
parent90125e9571b5281bb65c194211fb0c1b948e3393 (diff)
downloadmicro-lang-hs-a8bd2cac6d7b3d71f2ff3d9ce655e65a827d5775.tar.gz
micro-lang-hs-a8bd2cac6d7b3d71f2ff3d9ce655e65a827d5775.zip
Attempt at mul
Diffstat (limited to 'src')
-rw-r--r--src/Micro/Asm/Sdcc.hs33
1 files changed, 33 insertions, 0 deletions
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]}