1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ForeignFunctionInterface #-}
3 module Mate.MethodPool where
6 import qualified Data.Map as M
12 import Foreign.C.Types
13 import Foreign.C.String
14 import Foreign.StablePtr
16 import Mate.X86CodeGen
19 foreign import ccall "get_mmap"
20 get_mmap :: IO (Ptr ())
22 foreign import ccall "set_mmap"
23 set_mmap :: Ptr () -> IO ()
25 foreign import ccall "demo_mmap"
29 type MMap = M.Map String Word32
31 foreign export ccall getMethodEntry :: Ptr () -> CString -> IO CUInt
32 getMethodEntry :: Ptr () -> CString -> IO CUInt
33 getMethodEntry ptr_mmap cstr = do
34 mmap <- deRefStablePtr $ ((castPtrToStablePtr ptr_mmap) :: StablePtr MMap)
36 case M.lookup k mmap of
38 Just w32 -> return (fromIntegral w32)
42 (entry, _) <- testCase "./tests/Fib.class" "fib"
43 let int_entry = ((fromIntegral $ ptrToIntPtr entry) :: Word32)
44 let mmap = M.insert ("fib" :: String) int_entry M.empty
45 mapM_ (\(x,y) -> printf "%s at 0x%08x\n" x y) $ M.toList mmap
46 ptr_mmap <- newStablePtr mmap
47 set_mmap $ castStablePtrToPtr ptr_mmap
48 demo_mmap -- access Data.Map from C