From 16024a28f32c48dd2a002de86d555469e156f8e4 Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Mon, 11 Jun 2012 21:02:20 +0200 Subject: [PATCH] ffi: don't use C for obtaining an address of a haskell function 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 | 12 +++--------- doc/TODO | 3 +++ ffi/trap.c | 16 ---------------- 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/Mate/X86CodeGen.hs b/Mate/X86CodeGen.hs index 9ba2ddd..56b48f7 100644 --- a/Mate/X86CodeGen.hs +++ b/Mate/X86CodeGen.hs @@ -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 diff --git a/doc/TODO b/doc/TODO index b45b927..c32d243 100644 --- 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 diff --git a/ffi/trap.c b/ffi/trap.c index 60b8048..1058004 100644 --- a/ffi/trap.c +++ b/ffi/trap.c @@ -21,7 +21,6 @@ #include -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; -} -- 2.25.1