futhark-0.19.4: An optimising compiler for a functional, array-oriented language.
Safe HaskellTrustworthy
LanguageHaskell2010

Futhark.CodeGen.Backends.GenericC

Description

C code generator framework.

Synopsis

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.

data CParts Source #

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.

Constructors

CParts 

Fields

asLibrary :: CParts -> (String, String) Source #

Produce header and implementation files.

asExecutable :: CParts -> String Source #

As executable with command-line interface.

asServer :: CParts -> String Source #

As server executable.

Pluggable compiler

data Operations op s Source #

Constructors

Operations 

Fields

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 ErrorCompiler op s = ErrorMsg Exp -> String -> CompilerM op s () Source #

type CallCompiler op s = [VName] -> Name -> [Exp] -> CompilerM op s () Source #

Call a 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.

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.

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

Instances details
MonadState (CompilerState s) (CompilerM op s) Source # 
Instance details

Defined in Futhark.CodeGen.Backends.GenericC

Methods

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 # 
Instance details

Defined in Futhark.CodeGen.Backends.GenericC

Methods

(>>=) :: CompilerM op s a -> (a -> CompilerM op s b) -> CompilerM op s b #

(>>) :: CompilerM op s a -> CompilerM op s b -> CompilerM op s b #

return :: a -> CompilerM op s a #

Functor (CompilerM op s) Source # 
Instance details

Defined in Futhark.CodeGen.Backends.GenericC

Methods

fmap :: (a -> b) -> CompilerM op s a -> CompilerM op s b #

(<$) :: a -> CompilerM op s b -> CompilerM op s a #

Applicative (CompilerM op s) Source # 
Instance details

Defined in Futhark.CodeGen.Backends.GenericC

Methods

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 # 
Instance details

Defined in Futhark.CodeGen.Backends.GenericC

data CompilerState s Source #

Instances

Instances details
MonadState (CompilerState s) (CompilerM op s) Source # 
Instance details

Defined in Futhark.CodeGen.Backends.GenericC

Methods

get :: CompilerM op s (CompilerState s) #

put :: CompilerState s -> CompilerM op s () #

state :: (CompilerState s -> (a, CompilerState s)) -> CompilerM op s a #

modifyUserState :: (s -> s) -> CompilerM op s () 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 #

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.

items :: [BlockItem] -> CompilerM op s () Source #

stm :: Stm -> CompilerM op s () Source #

stms :: [Stm] -> CompilerM op s () Source #

atInit :: Stm -> 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.

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.

publicName :: String -> CompilerM op s String Source #

Public names must have a consitent prefix.

contextType :: CompilerM op s Type Source #

The generated code must define a struct with this name.

cacheMem :: ToExp a => a -> CompilerM op s (Maybe VName) Source #

cproduct :: [Exp] -> Exp Source #

Return an expression multiplying together the given expressions. If an empty list is given, the expression 1 is returned.

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.