Skip to content

Refactor code and update some docs #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions lib/CodeGen/Llvm/LlvmIrGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions lib/Transformations/Ll/Ll.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/Transformations/Relabeler/Relabeler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/Trees/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data Identifier'
| Gen IdCnt Identifier
deriving (Show, Eq, Ord)

type IdCnt = Int
type IdCnt = Integer

-- ** Operators

Expand Down Expand Up @@ -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)