Skip to content

Commit

Permalink
add: fresh and freshName local functions
Browse files Browse the repository at this point in the history
  • Loading branch information
G0nzal0zz committed Jan 13, 2025
1 parent a2d4de3 commit 681d2ff
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/Codegen/Codegen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ type GlobalState = [(String, AST.Operand)]
-- | Type alias for the loop code generation state.
type LoopState = Maybe (AST.Name, AST.Name)

-- | Type alias for the variables name .
type UniqueNameState = Integer

-- | Combined state for code generation.
data CodegenState = CodegenState
{ localState :: LocalState,
globalState :: GlobalState,
loopState :: LoopState,
allocatedVars :: LocalState
allocatedVars :: LocalState,
uniqueNameState :: UniqueNameState
}
deriving (Show)

Expand Down Expand Up @@ -160,7 +164,7 @@ codegen program =
E.runExcept $
M.buildModuleT (U.stringToByteString $ AT.sourceFile program) $
IRM.runIRBuilderT IRM.emptyIRBuilder $
S.evalStateT (mapM_ (generateGlobal . snd) (AT.globals program)) (CodegenState [] [] Nothing [])
S.evalStateT (mapM_ (generateGlobal . snd) (AT.globals program)) (CodegenState [] [] Nothing [] 0)

-- | Generate LLVM code for global expressions.
generateGlobal :: (MonadCodegen m) => AT.Expr -> m ()
Expand All @@ -169,6 +173,24 @@ generateGlobal expr = case expr of
AT.ForeignFunction {} -> CM.void $ generateForeignFunction expr
_ -> E.throwError $ CodegenError (SU.getLoc expr) $ UnsupportedTopLevel expr

-- Generates a fresh unique name.
fresh :: (MonadCodegen m) => m AST.Name
fresh = do
state <- S.get
let uniqueName = uniqueNameState state
S.put $ state {uniqueNameState = uniqueName + 1}
let fullName = "_" ++ show uniqueName
return $ AST.Name (U.stringToByteString fullName)

-- Generates a fresh unique name with the given prefix.
freshName :: (MonadCodegen m) => String -> m AST.Name
freshName prefix = do
state <- S.get
let uniqueName = uniqueNameState state
S.put $ state {uniqueNameState = uniqueName + 1}
let fullName = prefix ++ show uniqueName
return $ AST.Name (U.stringToByteString fullName)

-- | Generate LLVM code for an expression.
class ExprGen a where
generateExpr :: (MonadCodegen m) => a -> m AST.Operand
Expand Down Expand Up @@ -237,7 +259,7 @@ createGlobalString str = do
(T.IntegerType 8)
(map (C.Int 8 . fromIntegral . fromEnum) (str ++ "\0"))
let strType = T.ArrayType (fromIntegral $ length str + 1) (T.IntegerType 8)
name <- IRM.fresh
name <- fresh
let global =
AST.GlobalVariable
{ G.name = name,
Expand Down

0 comments on commit 681d2ff

Please # to comment.