silikonspin.blogg.se

Haskell free library
Haskell free library








haskell free library
  1. Haskell free library how to#
  2. Haskell free library code#

We introduce padding bytes to align the "i" field (Word32) on 4 bytes.

  • We indicate the offset of each field explicitly for peek and poke methods.
  • haskell free library

    8 for Double, 1 for Word8 and 4 for Word32). The alignment of primitive types is equal to their size in bytes (e.g. The structure alignment is the least common multiple of the alignments of the structure fields.Instance Storable MyStruct where alignment _ = 8 sizeOf _ = 16 peek ptr = MyStruct peekByteOff ptr 0 peekByteOff ptr 8 peekByteOff ptr 12 - skip padding bytes after "c" poke ptr ( MyStruct d c i ) = do pokeByteOff ptr 0 d pokeByteOff ptr 8 c pokeByteOff ptr 12 i The allocation is ephemeral: it lasts the time of the execution of an IO action, as in the following example: on the Haskell heap, using `alloca*` functions in.There are basically two ways to allocate memory: NULL pointer is represented with `nullPtr`. You can easily cast between pointer types using `castPtr` or perform pointer arithmetic using `plusPtr`, `minusPtr` and `alignPtr`. GHC has no other way to detect it.Īdd :: Int -> Int add = x + y foreign import ccall "wrapper" createAddPtr :: ( Int -> Int ) -> IO ( FunPtr ( Int -> Int )) main = do addPtr OtherStuff)" but it is not function pointer in the foreign language: it is just a pointer tagged with the "Stuff -> OtherStuff" type. If the foreign function performs side-effects, you have to explicitly indicate it in its type (using IO). The Foreign.C.Types module contains renaming of some of these marshallable foreign types with names closer to those of C types (e.g. Warning: GHC does not support passing structures as values yet. the return type is either a marshallable foreign type or has the form IO t where t is a marshallable foreign type or ().Char, Int, Double, Float, Bool, Int8, Int16, Int32, Int64, Word8, Word16, Word32, Word64, Ptr a, FunPtr a, StablePtr a or a renaming of any of these using newtype. the argument types can be marshallable foreign types, i.e.Only some Haskell types can be directly used as parameters for foreign functions, because they correspond to basic types of low-level languages such as C and are used to define calling conventions.Īccording to, the type of a foreign function is a foreign type, that is a function type with zero or more arguments where:

    Haskell free library how to#

    As an example, in you can find the algorithm describing how to pass parameters to functions on Linux on a x86-64 architecture depending on the types of the parameters. Several values can be combined into a single vector register.

    haskell free library

    For instance, floating-point values (Double, Float) may be passed into floating-point registers.

    haskell free library

    Pascal convention).Ĭalling conventions depend on parameter types. Other available conventions supported by GHC include "stdcall" (i.e.

    Haskell free library code#

    GHC will generate code to put (and to retrieve) parameters into memory and registers conforming to what is expected by a code generated with a C compiler (or any other compiler conforming to this convention). We see that the C calling convention ("ccall") is used. For instance, to call the exponential function ("exp") of the libc, you only have to translate its prototype:įoreign import ccall "exp" c_exp :: Double -> Double Haskell programs can call foreign functions and foreign functions can call Haskell code.Ĭompared to many other languages, Haskell FFI is very easy to use: in the most common case, you only have to translate the prototype of the foreign function into the equivalent Haskell prototype and you're done. The Foreign Function Interface (FFI) allows Haskell programs to cooperate with programs written with other languages.

  • 7 Enhancing performance and advanced topics.









  • Haskell free library