Sigbjorn Finne wrote:
> [...] Do you really need to use _casm_GC_?
Hmmm, I made some experiments with the example program from my
other mail yesterday. Here a slightly modified version:
-- Main.hs ------------------------------------------------------
{-# OPTIONS -#include "callhaskell.h" #-}
import Foreign(makeStablePtr)
haskellCB :: IO ()
haskellCB = _casm_ ``puts("haskellCB");'' -- (1)
main :: IO ()
main = do sp <- makeStablePtr haskellCB
_casm_GC_ ``callback = %0; loop();'' sp -- (2)
-- callhaskell.h ------------------------------------------------
#include "rtsdefs.h"
extern StgStablePtr callback;
extern void loop(void);
-- callhaskell.c ------------------------------------------------
#include "callhaskell.h"
StgStablePtr callback;
void loop(void) {
while (1) performIO(callback);
}
The rules for _casm_/_casm_GC_ seem to be as follows:
* Haskell code called via performIO from C-land can use _casm_
(see (1) above).
* Haskell code calling C-code which calls back Haskell *must* use
_casm_GC_ (see (2) above).
* Otherwise _casm_ and _casm_GC_ can be mixed freely.
A few hints/examples in GHC's documentation would have been very helpful.
> If you do, then what about just hacking up something like this:
> [ embarrassing code omitted ]
> where arg0,...,arg2 are global vars. Not a beauty contest contender,
> but it'll do the job.
I knew someone would come with this suggestion... :-)
But the whole story is: The _casm_(GC_)s are code generated by
GreenCard, so a consistent method for calling C is needed. With the
above observations my "solution" is as follows: Split GreenCard
sources files in two parts: The first part is the "pure-C" part
(i.e. no C-calls which make some Haskell callbacks before returning)
and a callback part. The latter one *must* be transformed with
--gc-safe and the former one doesn't need this flag. Consequently the
STG-restrictions don't apply to this "pure-C" part. Not very nice,
but I can live with that.
--
Sven Panne Tel.: +49/89/2178-2235
LMU, Institut fuer Informatik FAX : +49/89/2178-2211
LFE Programmier- und Modellierungssprachen Oettingenstr. 67
mailto:[EMAIL PROTECTED] D-80538 Muenchen
http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne