imperative-edsl-0.8: Deep embedding of imperative programs with code generation

Safe HaskellNone
LanguageHaskell2010

Language.C.Monad

Description

A monad for C code generation

Synopsis

Documentation

data Flags Source #

Code generation flags

Constructors

Flags 

data CEnv Source #

Code generator state.

Instances
Monad t => MonadState CEnv (CGenT t) Source # 
Instance details

Defined in Language.C.Monad

Methods

get :: CGenT t CEnv #

put :: CEnv -> CGenT t () #

state :: (CEnv -> (a, CEnv)) -> CGenT t a #

defaultCEnv :: Flags -> CEnv Source #

Default code generator state

type MonadC m = (Functor m, Applicative m, Monad m, MonadState CEnv m, MonadException m, MonadFix m) Source #

Code generation type constraints

newtype CGenT t a Source #

The C code generation monad transformer

Constructors

CGenT 

Fields

Instances
Monad t => MonadState CEnv (CGenT t) Source # 
Instance details

Defined in Language.C.Monad

Methods

get :: CGenT t CEnv #

put :: CEnv -> CGenT t () #

state :: (CEnv -> (a, CEnv)) -> CGenT t a #

Monad t => Monad (CGenT t) Source # 
Instance details

Defined in Language.C.Monad

Methods

(>>=) :: CGenT t a -> (a -> CGenT t b) -> CGenT t b #

(>>) :: CGenT t a -> CGenT t b -> CGenT t b #

return :: a -> CGenT t a #

fail :: String -> CGenT t a #

Functor t => Functor (CGenT t) Source # 
Instance details

Defined in Language.C.Monad

Methods

fmap :: (a -> b) -> CGenT t a -> CGenT t b #

(<$) :: a -> CGenT t b -> CGenT t a #

MonadFix t => MonadFix (CGenT t) Source # 
Instance details

Defined in Language.C.Monad

Methods

mfix :: (a -> CGenT t a) -> CGenT t a #

Monad t => Applicative (CGenT t) Source # 
Instance details

Defined in Language.C.Monad

Methods

pure :: a -> CGenT t a #

(<*>) :: CGenT t (a -> b) -> CGenT t a -> CGenT t b #

liftA2 :: (a -> b -> c) -> CGenT t a -> CGenT t b -> CGenT t c #

(*>) :: CGenT t a -> CGenT t b -> CGenT t b #

(<*) :: CGenT t a -> CGenT t b -> CGenT t a #

MonadIO t => MonadIO (CGenT t) Source # 
Instance details

Defined in Language.C.Monad

Methods

liftIO :: IO a -> CGenT t a #

Monad t => MonadException (CGenT t) Source # 
Instance details

Defined in Language.C.Monad

Methods

throw :: Exception e => e -> CGenT t a #

catch :: Exception e => CGenT t a -> (e -> CGenT t a) -> CGenT t a #

finally :: CGenT t a -> CGenT t b -> CGenT t a #

(CompExp exp, CompTypeClass ct) => Interp C_CMD CGen (Param2 exp ct :: (Type -> Type, (Type -> Constraint, Type))) Source # 
Instance details

Defined in Language.Embedded.Imperative.Backend.C

Methods

interp :: C_CMD (CGen, Param2 exp ct) a -> CGen a #

(CompExp exp, CompTypeClass ct) => Interp ControlCMD CGen (Param2 exp ct :: (Type -> Type, (Type -> Constraint, Type))) Source # 
Instance details

Defined in Language.Embedded.Imperative.Backend.C

Methods

interp :: ControlCMD (CGen, Param2 exp ct) a -> CGen a #

(CompExp exp, CompTypeClass ct, ct Bool) => Interp (FileCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) CGen (Param2 exp ct :: (Type -> Type, (Type -> Constraint, Type))) Source # 
Instance details

Defined in Language.Embedded.Imperative.Backend.C

Methods

interp :: FileCMD (CGen, Param2 exp ct) a -> CGen a #

(CompExp exp, CompTypeClass ct) => Interp (ArrCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) CGen (Param2 exp ct :: (Type -> Type, (Type -> Constraint, Type))) Source # 
Instance details

Defined in Language.Embedded.Imperative.Backend.C

Methods

interp :: ArrCMD (CGen, Param2 exp ct) a -> CGen a #

(CompExp exp, CompTypeClass ct) => Interp (RefCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) CGen (Param2 exp ct :: (Type -> Type, (Type -> Constraint, Type))) Source # 
Instance details

Defined in Language.Embedded.Imperative.Backend.C

Methods

interp :: RefCMD (CGen, Param2 exp ct) a -> CGen a #

(CompExp exp, CompTypeClass ct, ct Bool) => Interp (ChanCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) CGen (Param2 exp ct :: (Type -> Type, (Type -> Constraint, Type))) Source # 
Instance details

Defined in Language.Embedded.Concurrent.Backend.C

Methods

interp :: ChanCMD (CGen, Param2 exp ct) a -> CGen a #

CompExp exp => Interp (ThreadCMD :: (Type -> Type, (Type -> Type, (k2, Type))) -> Type -> Type) CGen (Param2 exp pred :: (Type -> Type, (k2, Type))) Source # 
Instance details

Defined in Language.Embedded.Concurrent.Backend.C

Methods

interp :: ThreadCMD (CGen, Param2 exp pred) a -> CGen a #

Interp (PtrCMD :: (Type -> Type, (k1, (k2, Type))) -> Type -> Type) CGen (Param2 exp ct :: (k1, (k2, Type))) Source # 
Instance details

Defined in Language.Embedded.Imperative.Backend.C

Methods

interp :: PtrCMD (CGen, Param2 exp ct) a -> CGen a #

runCGenT :: Monad m => CGenT m a -> CEnv -> m (a, CEnv) Source #

Run the C code generation monad

runCGen :: CGen a -> CEnv -> (a, CEnv) Source #

Run the C code generation monad

cenvToCUnit :: CEnv -> [Definition] Source #

Extract a compilation unit from the CEnv state

prettyCGenT :: Monad m => CGenT m a -> m [(String, Doc)] Source #

Generate C documents for each module

freshId :: MonadC m => m Integer Source #

Retrieve a fresh identifier

gensym :: MonadC m => String -> m String Source #

Generate a fresh symbol by appending a fresh id to a base name

touchVar :: (MonadC m, ToIdent v) => v -> m () Source #

Mark an identifier as used in this context.

setUsedVars :: MonadC m => String -> Set Id -> m () Source #

Set the Set of identifers used in the body of the given function.

addInclude :: MonadC m => String -> m () Source #

Add an include pre-processor directive. Specify <> or '""' around the file name.

addLocalInclude :: MonadC m => String -> m () Source #

Add a local include directive. The argument will be surrounded by '""'

addSystemInclude :: MonadC m => String -> m () Source #

Add a system include directive. The argument will be surrounded by <>

addTypedef :: MonadC m => Definition -> m () Source #

Add a type definition

addPrototype :: MonadC m => Definition -> m () Source #

Add a function prototype

addGlobal :: MonadC m => Definition -> m () Source #

Add a global definition

addGlobals :: MonadC m => [Definition] -> m () Source #

Add multiple global definitions

withAlias :: MonadC m => Integer -> String -> m a -> m a Source #

Let a variable be known by another name

addParam :: MonadC m => Param -> m () Source #

Add a function parameter when building a function definition

addParams :: MonadC m => [Param] -> m () Source #

addArg :: MonadC m => Exp -> m () Source #

Add a function argument when building a function call

addLocal :: MonadC m => InitGroup -> m () Source #

Add a local declaration (including initializations)

addItem :: MonadC m => BlockItem -> m () Source #

Add an item (a declaration or a statement) to the current block This functionality is necessary to declare C99 variable-length arrays in the middle of a block, as other local delcarations are lifted to the beginning of the block, and that makes the evaluation of the length expression impossible.

addLocals :: MonadC m => [InitGroup] -> m () Source #

Add multiple local declarations

addStm :: MonadC m => Stm -> m () Source #

Add a statement to the current block

addStms :: MonadC m => [Stm] -> m () Source #

Add a sequence of statements to the current block

addFinalStm :: MonadC m => Stm -> m () Source #

Add a statement to the end of the current block

inBlock :: MonadC m => m a -> m a Source #

Run an action in a new block

inNewBlock :: MonadC m => m a -> m (a, [BlockItem]) Source #

Run an action as a block and capture the items. Does not place the items in an actual C block.

inNewBlock_ :: MonadC m => m a -> m [BlockItem] Source #

Run an action as a block and capture the items. Does not place the items in an actual C block.

inNewFunction :: MonadC m => m a -> m (a, Set Id, [Param], [BlockItem]) Source #

Run an action as a function declaration. Does not create a new function.

inFunction :: MonadC m => String -> m a -> m a Source #

Declare a function

inFunctionTy :: MonadC m => Type -> String -> m a -> m a Source #

Declare a function with the given return type.

collectDefinitions :: MonadC m => m a -> m (a, [Definition]) Source #

Collect all global definitions in the current state

collectArgs :: MonadC m => m [Exp] Source #

Collect all function arguments in the current state

inModule :: MonadC m => String -> m a -> m a Source #

Declare a C translation unit

wrapMain :: MonadC m => m a -> m () Source #

Wrap a program in a main function

liftSharedLocals :: MonadC m => m a -> m () Source #

Lift the declarations of all variables that are shared between functions to the top level. This relies on variable IDs being unique across programs, not just across the functions in which they are declared.

Only affects locally declared vars, not function arguments.

extractDecls :: (Id -> Bool) -> Definition -> (Definition, Set InitGroup) Source #

Remove all declarations matching a predicate from the given function and return them in a separate list.