ffi: don't use C for obtaining an address of a haskell function
authorBernhard Urban <lewurm@gmail.com>
Mon, 11 Jun 2012 19:02:20 +0000 (21:02 +0200)
committerBernhard Urban <lewurm@gmail.com>
Mon, 11 Jun 2012 19:02:20 +0000 (21:02 +0200)
i.e. get rid of dirty FFI hacks.

thanks to simonmar:
http://stackoverflow.com/questions/10967598/get-the-address-of-a-function-without-ffi#comment14326263_10967598

Mate/X86CodeGen.hs
doc/TODO
ffi/trap.c

index 9ba2ddd31f6734140f9c0b3a0ef4cac9e6dfaa48..56b48f7061883b44402bcf99ea6b663dab9c18ef 100644 (file)
@@ -34,11 +34,8 @@ import Text.Printf
 #endif
 
 
-foreign import ccall "dynamic"
-   code_int :: FunPtr (CInt -> CInt -> IO CInt) -> CInt -> CInt -> IO CInt
-
-foreign import ccall "getMallocObjectAddr"
-  getMallocObjectAddr :: CUInt
+foreign import ccall "&mallocObject"
+  mallocObjectAddr :: FunPtr (Int -> IO CUInt)
 
 type EntryPoint = Ptr Word8
 type EntryPointOffset = Int
@@ -333,10 +330,7 @@ emitFromBB method sig cls hmap =  do
 
     callMalloc :: CodeGen e s ()
     callMalloc = do
-        calladdr <- getCurrentOffset
-        let w32_calladdr = 5 + calladdr
-        let malloaddr = fromIntegral getMallocObjectAddr :: Word32
-        call (malloaddr - w32_calladdr)
+        call mallocObjectAddr
         add esp (4 :: Word32)
         push eax
 
index b45b9272c14fdb27b69f1b98a3bbac6cd4b86169..c32d2430d1ddbbb530388caee24a5a00190d9438 100644 (file)
--- a/doc/TODO
+++ b/doc/TODO
@@ -50,6 +50,9 @@
 
 (l) check different types (byte, long, ...)
 
+(l) get rid of CUInt where appropriate
+       -> CPtrdiff and CSize (ptrdiff_t and size_t) is more portable
+
 (l) floating point support
 
 (h) better code generation
index 60b8048cb9c410d0078f0236d9c02d9a1f823277..1058004a1046afe6432f82fbceb9ab3b4194066a 100644 (file)
@@ -21,7 +21,6 @@
 
 #include <sys/ucontext.h>
 
-unsigned int mallocObject(int);
 unsigned int mateHandler(unsigned int, unsigned int, unsigned int, unsigned int);
 
 #ifdef DBG_TRAP
@@ -30,11 +29,6 @@ unsigned int mateHandler(unsigned int, unsigned int, unsigned int, unsigned int)
 #define dprintf(args...)
 #endif
 
-void mainresult(unsigned int a)
-{
-       dprintf("mainresult: 0x%08x\n", a);
-}
-
 void chandler(int nSignal, siginfo_t *info, void *ctx)
 {
        mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
@@ -64,13 +58,3 @@ void register_signal(void)
        segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
        sigaction(SIGSEGV, &segvaction, NULL);
 }
-
-unsigned int getaddr(void)
-{
-       return (unsigned int) mainresult;
-}
-
-unsigned int getMallocObjectAddr(void)
-{
-       return (unsigned int) mallocObject;
-}