From 15f5b394cee2b8a30af84226b14b89079fa4e348 Mon Sep 17 00:00:00 2001 From: Azim Muradov Date: Fri, 4 Oct 2024 20:40:16 +0300 Subject: [PATCH 1/5] Refactor `llGDecl` in `Ll` --- lib/Transformations/Ll/Ll.hs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/Transformations/Ll/Ll.hs b/lib/Transformations/Ll/Ll.hs index d136a26..0e108e2 100644 --- a/lib/Transformations/Ll/Ll.hs +++ b/lib/Transformations/Ll/Ll.hs @@ -34,18 +34,19 @@ data FunDeclaration = FunDecl Common.Identifier' [Common.Identifier'] Lfr.Expres -- ** Lambda Lifters llGDecl :: Ast.Declaration -> LlState [Lfr.GlobalDeclaration] -llGDecl = \case - Ast.DeclVar ident value -> do - var <- ll1 (Lfr.VarDecl ident) value - genFuns <- gets genFunDecls - modify $ \env -> env {genFunDecls = []} - return $ reverse (Lfr.GlobVarDecl var : (convertFunDecl <$> genFuns)) - Ast.DeclFun ident _ (Ast.Fun params body) -> do - fun <- ll1 (FunDecl ident (NE.toList params)) body - genFuns <- gets genFunDecls - modify $ \env -> env {genFunDecls = []} - return $ convertFunDecl <$> reverse (fun : genFuns) +llGDecl decl = do + decl' <- case decl of + Ast.DeclVar ident value -> + ll1 (Lfr.GlobVarDecl . Lfr.VarDecl ident) value + Ast.DeclFun ident _ (Ast.Fun params body) -> + ll1 (Lfr.GlobFunDecl ident (NE.toList params)) body + genDecls <- collectGenFunDecls + return $ reverse (decl' : genDecls) where + collectGenFunDecls = do + genDecls <- gets genFunDecls + modify $ \env -> env {genFunDecls = []} + return (convertFunDecl <$> genDecls) convertFunDecl (FunDecl i ps b) = Lfr.GlobFunDecl i ps b llExpr :: Ast.Expression -> LlState Lfr.Expression From 904ff6ddd192f0c4c08a9ae370b52e30cc62adf9 Mon Sep 17 00:00:00 2001 From: Azim Muradov Date: Fri, 4 Oct 2024 20:41:35 +0300 Subject: [PATCH 2/5] Alias `IdCnt` to `Integer` --- lib/Trees/Common.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Trees/Common.hs b/lib/Trees/Common.hs index 0efcabc..cdea4cd 100644 --- a/lib/Trees/Common.hs +++ b/lib/Trees/Common.hs @@ -14,7 +14,7 @@ data Identifier' | Gen IdCnt Identifier deriving (Show, Eq, Ord) -type IdCnt = Int +type IdCnt = Integer -- ** Operators From f2f9dbe1f8dd874d8b0f2b6a595449b0cd013f15 Mon Sep 17 00:00:00 2001 From: Azim Muradov Date: Fri, 4 Oct 2024 20:44:56 +0300 Subject: [PATCH 3/5] Update `PrimValInt` doc --- lib/Trees/Common.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Trees/Common.hs b/lib/Trees/Common.hs index cdea4cd..dbfe014 100644 --- a/lib/Trees/Common.hs +++ b/lib/Trees/Common.hs @@ -102,6 +102,6 @@ data PrimitiveValue PrimValUnit | -- | Boolean value (@true@, @false@). PrimValBool Bool - | -- | Int value (e.g., @0@, @4@, @15@, @23@). + | -- | Int value (e.g., @0@, @4@, @8@, @-15@). PrimValInt Int64 deriving (Show, Eq) From 38825fa77ffe9c411a3e1159fbe7518c41153951 Mon Sep 17 00:00:00 2001 From: Azim Muradov Date: Fri, 4 Oct 2024 20:47:53 +0300 Subject: [PATCH 4/5] Update `relabelAst` doc --- lib/Transformations/Relabeler/Relabeler.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Transformations/Relabeler/Relabeler.hs b/lib/Transformations/Relabeler/Relabeler.hs index e8a9bc6..7ff782e 100644 --- a/lib/Transformations/Relabeler/Relabeler.hs +++ b/lib/Transformations/Relabeler/Relabeler.hs @@ -14,7 +14,7 @@ import qualified Trees.Common as Ast -- | Relabel identifiers in the AST so that each declaration creates an identifier with a unique name. -- --- It helps to avoid naming errors in the future. +-- It helps to avoid naming errors in the future compilation phases. relabelAst :: Ast.Program -> Ast.Program relabelAst (Ast.Program decls cnt) = let (decls', Env _ cnt') = runState (mapM relabelDecl decls) (Env [] cnt) From 3543796a929faec42154134ae427d771d20ebffa Mon Sep 17 00:00:00 2001 From: Azim Muradov Date: Fri, 4 Oct 2024 20:36:55 +0300 Subject: [PATCH 5/5] Refactor LLVM IR binary operations generation --- lib/CodeGen/Llvm/LlvmIrGen.hs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/CodeGen/Llvm/LlvmIrGen.hs b/lib/CodeGen/Llvm/LlvmIrGen.hs index 43f83d4..bc4d80a 100644 --- a/lib/CodeGen/Llvm/LlvmIrGen.hs +++ b/lib/CodeGen/Llvm/LlvmIrGen.hs @@ -144,19 +144,17 @@ genComp = \case ArithOp PlusOp -> LLVM.add ArithOp MinusOp -> LLVM.sub ArithOp MulOp -> LLVM.mul - ArithOp DivOp -> - ( \lhs'' rhs'' -> do - divF <- findFun (Txt "miniml_div") - LLVM.call divF [lhs'', rhs''] - ) + ArithOp DivOp -> \lhs'' rhs'' -> do + divF <- findFun (Txt "miniml_div") + LLVM.call divF [lhs'', rhs''] CompOp cOp -> let cOpF = case cOp of - EqOp -> LLVM.icmp LLVM.EQ - NeOp -> LLVM.icmp LLVM.NE - LtOp -> LLVM.icmp LLVM.SLT - LeOp -> LLVM.icmp LLVM.SLE - GtOp -> LLVM.icmp LLVM.SGT - GeOp -> LLVM.icmp LLVM.SGE + EqOp -> LLVM.eq + NeOp -> LLVM.ne + LtOp -> LLVM.slt + LeOp -> LLVM.sle + GtOp -> LLVM.sgt + GeOp -> LLVM.sge in (\a b -> cOpF a b >>= boolToInt) opF lhs' rhs' CompUnOp op x -> do