"foreign export dynamic" in GHC

Simon Marlow simonmar@microsoft.com
Wed, 8 Aug 2001 13:57:20 +0100

[ moved to glasgow-haskell-users ]

> When I make a function generator with "foreign export=20
> dynamic", GHC spits=20
> out stub files which it apparently then needs for the link. What are=20
> these for?

The stub files contain the helper function which invokes the Haskell
code when called from C. =20

Each time the foreign export dynamic is invoked to create a new callback
function, a small piece of code has to be dynamically generated.  It is
the address of this dynamically generated bit of code that is returned
as the Addr (or Ptr).  When called from C, the dynamically generated
code must somehow invoke the Haskell function which was originally
passed to the f.e.d. function - it does this by invoking the helper
function, passing it a StablePtr to the Haskell function.  It's split
this way for two reasons: the same helper function can be used each time
the f.e.d. function is called, and to keep the amount of dynamically
generated code to a minimum.