Safe Haskell | Trustworthy |
---|---|
Language | Haskell2010 |
C code generator framework.
Synopsis
- compileProg :: MonadFreshNames m => String -> Operations op () -> CompilerM op () () -> String -> [Space] -> [Option] -> Definitions op -> m CParts
- data CParts = CParts {}
- asLibrary :: CParts -> (String, String)
- asExecutable :: CParts -> String
- asServer :: CParts -> String
- data Operations op s = Operations {
- opsWriteScalar :: WriteScalar op s
- opsReadScalar :: ReadScalar op s
- opsAllocate :: Allocate op s
- opsDeallocate :: Deallocate op s
- opsCopy :: Copy op s
- opsStaticArray :: StaticArray op s
- opsMemoryType :: MemoryType op s
- opsCompiler :: OpCompiler op s
- opsError :: ErrorCompiler op s
- opsCall :: CallCompiler op s
- opsFatMemory :: Bool
- opsCritical :: ([BlockItem], [BlockItem])
- defaultOperations :: Operations op s
- type OpCompiler op s = op -> CompilerM op s ()
- type ErrorCompiler op s = ErrorMsg Exp -> String -> CompilerM op s ()
- type CallCompiler op s = [VName] -> Name -> [Exp] -> CompilerM op s ()
- type PointerQuals op s = String -> CompilerM op s [TypeQual]
- type MemoryType op s = SpaceId -> CompilerM op s Type
- type WriteScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> Exp -> CompilerM op s ()
- writeScalarPointerWithQuals :: PointerQuals op s -> WriteScalar op s
- type ReadScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> CompilerM op s Exp
- readScalarPointerWithQuals :: PointerQuals op s -> ReadScalar op s
- type Allocate op s = Exp -> Exp -> Exp -> SpaceId -> CompilerM op s ()
- type Deallocate op s = Exp -> Exp -> SpaceId -> CompilerM op s ()
- type Copy op s = Exp -> Exp -> Space -> Exp -> Exp -> Space -> Exp -> CompilerM op s ()
- type StaticArray op s = VName -> SpaceId -> PrimType -> ArrayContents -> CompilerM op s ()
- data CompilerM op s a
- data CompilerState s
- getUserState :: CompilerM op s s
- modifyUserState :: (s -> s) -> CompilerM op s ()
- contextContents :: CompilerM op s ([FieldGroup], [Stm])
- contextFinalInits :: CompilerM op s [Stm]
- runCompilerM :: Operations op s -> VNameSource -> s -> CompilerM op s a -> (a, CompilerState s)
- inNewFunction :: Bool -> CompilerM op s a -> CompilerM op s a
- cachingMemory :: Map VName Space -> ([BlockItem] -> [Stm] -> CompilerM op s a) -> CompilerM op s a
- blockScope :: CompilerM op s () -> CompilerM op s [BlockItem]
- compileFun :: [BlockItem] -> [Param] -> (Name, Function op) -> CompilerM op s (Definition, Func)
- compileCode :: Code op -> CompilerM op s ()
- compileExp :: Exp -> CompilerM op s Exp
- compilePrimExp :: Monad m => (v -> m Exp) -> PrimExp v -> m Exp
- compilePrimValue :: PrimValue -> Exp
- compileExpToName :: String -> PrimType -> Exp -> CompilerM op s VName
- rawMem :: VName -> CompilerM op s Exp
- item :: BlockItem -> CompilerM op s ()
- items :: [BlockItem] -> CompilerM op s ()
- stm :: Stm -> CompilerM op s ()
- stms :: [Stm] -> CompilerM op s ()
- decl :: InitGroup -> CompilerM op s ()
- atInit :: Stm -> CompilerM op s ()
- headerDecl :: HeaderSection -> Definition -> CompilerM op s ()
- publicDef :: String -> HeaderSection -> (String -> (Definition, Definition)) -> CompilerM op s String
- publicDef_ :: String -> HeaderSection -> (String -> (Definition, Definition)) -> CompilerM op s ()
- profileReport :: BlockItem -> CompilerM op s ()
- onClear :: BlockItem -> CompilerM op s ()
- data HeaderSection
- libDecl :: Definition -> CompilerM op s ()
- earlyDecl :: Definition -> CompilerM op s ()
- publicName :: String -> CompilerM op s String
- contextType :: CompilerM op s Type
- contextField :: Id -> Type -> Maybe Exp -> CompilerM op s ()
- memToCType :: VName -> Space -> CompilerM op s Type
- cacheMem :: ToExp a => a -> CompilerM op s (Maybe VName)
- fatMemory :: Space -> CompilerM op s Bool
- rawMemCType :: Space -> CompilerM op s Type
- cproduct :: [Exp] -> Exp
- fatMemType :: Space -> Type
- primTypeToCType :: PrimType -> Type
- intTypeToCType :: IntType -> Type
- copyMemoryDefaultSpace :: Exp -> Exp -> Exp -> Exp -> Exp -> CompilerM op s ()
Documentation
compileProg :: MonadFreshNames m => String -> Operations op () -> CompilerM op () () -> String -> [Space] -> [Option] -> Definitions op -> m CParts Source #
Compile imperative program to a C program. Always uses the function named "main" as entry point, so make sure it is defined.
The result of compilation to C is multiple parts, which can be put together in various ways. The obvious way is to concatenate all of them, which yields a CLI program. Another is to compile the library part by itself, and use the header file to call into it.
asExecutable :: CParts -> String Source #
As executable with command-line interface.
Pluggable compiler
data Operations op s Source #
Operations | |
|
defaultOperations :: Operations op s Source #
A set of operations that fail for every operation involving
non-default memory spaces. Uses plain pointers and malloc
for
memory management.
type OpCompiler op s = op -> CompilerM op s () Source #
A substitute expression compiler, tried before the main compilation function.
type PointerQuals op s = String -> CompilerM op s [TypeQual] Source #
The address space qualifiers for a pointer of the given type with the given annotation.
type MemoryType op s = SpaceId -> CompilerM op s Type Source #
The type of a memory block in the given memory space.
type WriteScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> Exp -> CompilerM op s () Source #
Write a scalar to the given memory block with the given element index and in the given memory space.
writeScalarPointerWithQuals :: PointerQuals op s -> WriteScalar op s Source #
type ReadScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> CompilerM op s Exp Source #
Read a scalar from the given memory block with the given element index and in the given memory space.
readScalarPointerWithQuals :: PointerQuals op s -> ReadScalar op s Source #
type Allocate op s = Exp -> Exp -> Exp -> SpaceId -> CompilerM op s () Source #
Allocate a memory block of the given size and with the given tag in the given memory space, saving a reference in the given variable name.
type Deallocate op s = Exp -> Exp -> SpaceId -> CompilerM op s () Source #
De-allocate the given memory block with the given tag, which is in the given memory space.
type Copy op s = Exp -> Exp -> Space -> Exp -> Exp -> Space -> Exp -> CompilerM op s () Source #
Copy from one memory block to another.
type StaticArray op s = VName -> SpaceId -> PrimType -> ArrayContents -> CompilerM op s () Source #
Create a static array of values - initialised at load time.
Monadic compiler interface
data CompilerM op s a Source #
Instances
MonadState (CompilerState s) (CompilerM op s) Source # | |
Defined in Futhark.CodeGen.Backends.GenericC get :: CompilerM op s (CompilerState s) # put :: CompilerState s -> CompilerM op s () # state :: (CompilerState s -> (a, CompilerState s)) -> CompilerM op s a # | |
Monad (CompilerM op s) Source # | |
Functor (CompilerM op s) Source # | |
Applicative (CompilerM op s) Source # | |
Defined in Futhark.CodeGen.Backends.GenericC pure :: a -> CompilerM op s a # (<*>) :: CompilerM op s (a -> b) -> CompilerM op s a -> CompilerM op s b # liftA2 :: (a -> b -> c) -> CompilerM op s a -> CompilerM op s b -> CompilerM op s c # (*>) :: CompilerM op s a -> CompilerM op s b -> CompilerM op s b # (<*) :: CompilerM op s a -> CompilerM op s b -> CompilerM op s a # | |
MonadFreshNames (CompilerM op s) Source # | |
Defined in Futhark.CodeGen.Backends.GenericC getNameSource :: CompilerM op s VNameSource Source # putNameSource :: VNameSource -> CompilerM op s () Source # |
data CompilerState s Source #
Instances
MonadState (CompilerState s) (CompilerM op s) Source # | |
Defined in Futhark.CodeGen.Backends.GenericC get :: CompilerM op s (CompilerState s) # put :: CompilerState s -> CompilerM op s () # state :: (CompilerState s -> (a, CompilerState s)) -> CompilerM op s a # |
getUserState :: CompilerM op s s Source #
modifyUserState :: (s -> s) -> CompilerM op s () Source #
contextContents :: CompilerM op s ([FieldGroup], [Stm]) Source #
contextFinalInits :: CompilerM op s [Stm] Source #
runCompilerM :: Operations op s -> VNameSource -> s -> CompilerM op s a -> (a, CompilerState s) Source #
inNewFunction :: Bool -> CompilerM op s a -> CompilerM op s a Source #
Used when we, inside an existing CompilerM
action, want to
generate code for a new function. Use this so that the compiler
understands that previously declared memory doesn't need to be
freed inside this action.
cachingMemory :: Map VName Space -> ([BlockItem] -> [Stm] -> CompilerM op s a) -> CompilerM op s a Source #
compileFun :: [BlockItem] -> [Param] -> (Name, Function op) -> CompilerM op s (Definition, Func) Source #
compileCode :: Code op -> CompilerM op s () Source #
compilePrimExp :: Monad m => (v -> m Exp) -> PrimExp v -> m Exp Source #
Tell me how to compile a v
, and I'll Compile any PrimExp v
for you.
compilePrimValue :: PrimValue -> Exp Source #
headerDecl :: HeaderSection -> Definition -> CompilerM op s () Source #
publicDef :: String -> HeaderSection -> (String -> (Definition, Definition)) -> CompilerM op s String Source #
Construct a publicly visible definition using the specified name as the template. The first returned definition is put in the header file, and the second is the implementation. Returns the public name.
publicDef_ :: String -> HeaderSection -> (String -> (Definition, Definition)) -> CompilerM op s () Source #
As publicDef
, but ignores the public name.
profileReport :: BlockItem -> CompilerM op s () Source #
data HeaderSection Source #
In which part of the header file we put the declaration. This is to ensure that the header file remains structured and readable.
Instances
Eq HeaderSection Source # | |
Defined in Futhark.CodeGen.Backends.GenericC (==) :: HeaderSection -> HeaderSection -> Bool # (/=) :: HeaderSection -> HeaderSection -> Bool # | |
Ord HeaderSection Source # | |
Defined in Futhark.CodeGen.Backends.GenericC compare :: HeaderSection -> HeaderSection -> Ordering # (<) :: HeaderSection -> HeaderSection -> Bool # (<=) :: HeaderSection -> HeaderSection -> Bool # (>) :: HeaderSection -> HeaderSection -> Bool # (>=) :: HeaderSection -> HeaderSection -> Bool # max :: HeaderSection -> HeaderSection -> HeaderSection # min :: HeaderSection -> HeaderSection -> HeaderSection # |
libDecl :: Definition -> CompilerM op s () Source #
earlyDecl :: Definition -> CompilerM op s () Source #
contextType :: CompilerM op s Type Source #
The generated code must define a struct with this name.
cproduct :: [Exp] -> Exp Source #
Return an expression multiplying together the given expressions.
If an empty list is given, the expression 1
is returned.
fatMemType :: Space -> Type Source #
Building Blocks
primTypeToCType :: PrimType -> Type Source #
The C type corresponding to a primitive type. Integers are assumed to be unsigned.
intTypeToCType :: IntType -> Type Source #
The C type corresponding to a signed integer type.