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 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 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) diff --git a/lib/Trees/Common.hs b/lib/Trees/Common.hs index 0efcabc..dbfe014 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 @@ -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)