Linking FFI and DSL

In Haskell LLVM bindings , I am trying to define a function with a variable number of arguments (in fact, I mean a constant number that is not known at compile time). I found this question and I am trying to fulfill the answer.

I do not want to completely abandon the use of FFI to generate LLVM, I want to use DSL as much as I can, and use FFI only to do what I can not do through DSL.

I managed to determine the type via functionType, I still can’t add the function to the module created by calling defineModule . I also think that the next step is to add the base blocks to the function via FFI.appendBasicBlock , which, in my opinion, is easy, but how can I get the arguments through FFI.getParam inside the do block in the CodeGenFunction monad.

+54
haskell llvm
Jan 26 2018-12-12T00:
source share
1 answer

If the size of the argument list is unknown "before runtime", you will need to convert the function into something that works in the list anyway. Note that the type (IORef [Word32]) means that when the program is executed, the IO action will read / write the (modifiable) Word32 list. Haskell programs should only say how to mutate / read / write a list - hence the IO () monad.

The LLVM git file you are referring to has examples /List.hs. It creates the LLVM build procedure "arrayLoop",

 arrayLoop :: (Phi a, IsType b, Num i, IsConst i, IsInteger i, IsFirstClass i, CmpRet i Bool) => Value i -> Value (Ptr b) -> a -> (Value (Ptr b) -> a -> CodeGenFunction ra) -> CodeGenFunction ra arrayLoop len ptr start loopBody = do 

which increases the pointer to the list ints, p and reduces the remaining length i in each call to the body block. This block repeatedly calls "loopBody" and stores the result in "vars", which ultimately returns (untouched to zero) to "s" inside the mList function:

 mList :: CodeGenModule (Function (StablePtr (IORef [Word32]) -> Word32 -> Ptr Word32 -> IO Int32)) mList = createFunction ExternalLinkage $ \ ref size ptr -> do next <- staticFunction nelem let _ = next :: Function (StablePtr (IORef [Word32]) -> IO Word32) s <- arrayLoop size ptr (valueOf 0) $ \ ptri y -> do flip store ptri =<< call next ref return y ret (s :: Value Int32) 

All the additional information about nelem / NextListElement is used in their example for "loopBody", which moves the list once to the left. This repo also mentions a mailing list: haskell-llvm@projects.haskellorg.

GHC7 can compile using LLVM, but I think that would not help with a Haskell program that interprets the language if GHC also does not compile JIT - does anyone know if this is so?

+2
Dec 21
source share



All Articles