Changes between Initial Version and Version 1 of Commentary/Compiler/FFI_Exports

Nov 2, 2018 4:16:35 PM (15 months ago)



  • Commentary/Compiler/FFI_Exports

    v1 v1  
     1= FFI Exports =
     3On this page I've collected some information about how we handle FFI exports which I found while working on #15808.
     5It's incomplete but should hopefully make the journey less painful for these who follow my footsteps.
     6Much of the information is in DsForeign as well but I found it hard to grep for it so here we go.
     8=== The Rundown ===
     10During desugaring from HsSyn to Core we look for foreign in/exports (DsForeign.hs). If you deal with issues with FFI this is a good starting point.
     12After running the pipeline we get:
     14* The haskell function
     15* A stable name wrapper for it.
     16* The exported C function
     17* A initialization stub.
     19* The haskell function:
     20  * This is called by regular Haskell code.
     21  * It might also be jumped to eventually when calling the exported function from C name.
     22* The stable name wrapper:
     23  * In trivial cases it can end up as a duplication of the normale function.
     24  * For anything doing actual work it seems to just be an indirection to the regular haskell function.
     25* The exported function
     26  * It grabs a worker from the runtime.
     27  * It converts the arguments to the Haskell variants
     28  * Applies the the arguments, returns the result, releases the worker.
     29  * It does the above by calling into the RTS. Using functions like rts_apply, rts_mkInt, rts_getInt, rts_lock, ... rts_unlock.
     30  * Important to note that the exported function always calls the stable name function.
     31    The reason isn't entire clear to me at this point. The comments explain what they do well enough but not much
     32    about why. So if you can clarify please do!
     33* The initialization stub:
     34  * This is a small snippet of C code which registers the exported function and it's stable name with the RTS.
     35  * It gets compiled via gcc during codegen.