1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ForeignFunctionInterface #-}
3 module Mate.MethodPool where
6 import Data.String.Utils
7 import qualified Data.Map as M
8 import qualified Data.Set as S
9 import qualified Data.ByteString.Lazy as B
13 import Foreign.C.Types
14 import Foreign.C.String
19 import Harpy.X86Disassembler
21 import Mate.BasicBlocks
23 import Mate.NativeMachine
29 foreign import ccall "dynamic"
30 code_void :: FunPtr (IO ()) -> IO ()
32 foreign import ccall "&printMemoryUsage"
33 printMemoryUsageAddr :: FunPtr (IO ())
35 foreign import ccall "&loadLibrary"
36 loadLibraryAddr :: FunPtr (IO ())
38 foreign import ccall "&printGCStats"
39 printGCStatsAddr :: FunPtr (IO ())
41 getMethodEntry :: CPtrdiff -> CPtrdiff -> IO CPtrdiff
42 getMethodEntry signal_from methodtable = do
47 let w32_from = fromIntegral signal_from
48 let mi = tmap M.! w32_from
49 let mi'@(MethodInfo method cm sig) =
52 (VirtualCall _ (MethodInfo methname _ msig) _) -> newMi methname msig
53 _ -> error "getMethodEntry: no TrapCause found. abort."
54 where newMi mn = MethodInfo mn (vmap M.! fromIntegral methodtable)
55 entryaddr <- case M.lookup mi' mmap of
57 cls <- getClassFile cm
58 printfMp $ printf "getMethodEntry(from 0x%08x): no method \"%s\" found. compile it\n" w32_from (show mi')
59 mm <- lookupMethodRecursive method sig [] cls
61 Just (mm', clsnames, cls') -> do
62 let flags = methodAccessFlags mm'
63 if S.member ACC_NATIVE flags
65 let scm = toString cm; smethod = toString method
66 if scm == "jmate/lang/MateRuntime" then do
69 return . funPtrToAddr $ loadLibraryAddr
71 return . funPtrToAddr $ printGCStatsAddr
73 return . funPtrToAddr $ printMemoryUsageAddr
75 error $ "native-call: " ++ smethod ++ " not found."
77 -- TODO(bernhard): cleaner please... *do'h*
78 let sym1 = replace "/" "_" scm
79 parenth = replace "(" "_" $ replace ")" "_" $ toString $ encode sig
80 sym2 = replace ";" "_" $ replace "/" "_" parenth
81 symbol = sym1 ++ "__" ++ smethod ++ "__" ++ sym2
82 printfMp $ printf "native-call: symbol: %s\n" symbol
83 nf <- loadNativeFunction symbol
84 setMethodMap $ M.insert mi' nf mmap
87 rawmethod <- parseMethod cls' method sig
88 entry <- compileBB rawmethod (MethodInfo method (thisClass cls') sig)
89 addMethodRef entry mi' clsnames
90 return $ fromIntegral entry
91 Nothing -> error $ show method ++ " not found. abort"
92 Just w32 -> return w32
93 return $ fromIntegral entryaddr
95 funPtrToAddr :: Num b => FunPtr a -> b
96 funPtrToAddr = fromIntegral . ptrToIntPtr . castFunPtrToPtr
98 lookupMethodRecursive :: B.ByteString -> MethodSignature -> [B.ByteString] -> Class Direct
99 -> IO (Maybe (Method Direct, [B.ByteString], Class Direct))
100 lookupMethodRecursive name sig clsnames cls =
102 Just x -> return $ Just (x, nextclsn, cls)
103 Nothing -> if thisname == "java/lang/Object"
106 supercl <- getClassFile (superClass cls)
107 lookupMethodRecursive name sig nextclsn supercl
109 res = lookupMethodSig name sig cls
110 thisname = thisClass cls
111 nextclsn :: [B.ByteString]
112 nextclsn = thisname:clsnames
114 -- TODO(bernhard): UBERHAX. ghc patch?
115 foreign import ccall safe "lookupSymbol"
116 c_lookupSymbol :: CString -> IO (Ptr a)
118 loadNativeFunction :: String -> IO NativeWord
119 loadNativeFunction sym = do
120 _ <- loadRawObject "ffi/native.o"
121 -- TODO(bernhard): WTF
122 resolveObjs (return ())
123 ptr <- withCString sym c_lookupSymbol
125 then error $ "dyn. loading of \"" ++ sym ++ "\" failed."
126 else return $ fromIntegral $ ptrToIntPtr ptr
130 -- (entry, _) <- testCase "./tests/Fib.class" "fib"
131 -- let int_entry = ((fromIntegral $ ptrToIntPtr entry) :: NativeWord)
132 -- let mmap = M.insert ("fib" :: String) int_entry M.empty
133 -- mapM_ (\(x,y) -> printf "%s at 0x%08x\n" x y) $ M.toList mmap
134 -- mmap2ptr mmap >>= set_mmap
135 -- demo_mmap -- access Data.Map from C
137 addMethodRef :: NativeWord -> MethodInfo -> [B.ByteString] -> IO ()
138 addMethodRef entry (MethodInfo mmname _ msig) clsnames = do
140 let newmap = foldr (\i -> M.insert (MethodInfo mmname i msig) entry) M.empty clsnames
141 setMethodMap $ mmap `M.union` newmap
144 compileBB :: RawMethod -> MethodInfo -> IO NativeWord
145 compileBB rawmethod methodinfo = do
148 cls <- getClassFile (methClassName methodinfo)
149 let ebb = emitFromBB cls rawmethod
150 let cgconfig = defaultCodeGenConfig { codeBufferSize = fromIntegral $ (rawCodeLength rawmethod) * 32 }
151 (_, Right right) <- runCodeGenWithConfig ebb () () cgconfig
153 let ((entry, _, _, new_tmap), _) = right
154 setTrapMap $ tmap `M.union` new_tmap -- prefers elements in tmap
156 printfJit $ printf "generated code of \"%s\" from \"%s\":\n" (toString $ methName methodinfo) (toString $ methClassName methodinfo)
157 printfJit $ printf "\tstacksize: 0x%04x, locals: 0x%04x\n" (rawStackSize rawmethod) (rawLocals rawmethod)
159 then mapM_ (printfJit . printf "%s\n" . showAtt) (snd right)
161 printfJit $ printf "\n\n"
162 -- UNCOMMENT NEXT LINES FOR GDB FUN
163 -- if (toString $ methName methodinfo) == "thejavamethodIwant2debug"
164 -- then putStrLn "press CTRL+C now for setting a breakpoint. then `c' and ENTER for continue" >> getLine
166 -- (1) build a debug build (see HACKING) and execute `make tests/Fib.gdb'
167 -- for example, where the suffix is important
168 -- (2) on getLine, press CTRL+C
169 -- (3) `br *0x<addr>'; obtain the address from the disasm above
170 -- (4) `cont' and press enter
171 return $ fromIntegral $ ptrToIntPtr entry
174 executeFuncPtr :: NativeWord -> IO ()
175 executeFuncPtr entry =
176 code_void ((castPtrToFunPtr $ intPtrToPtr $ fromIntegral entry) :: FunPtr (IO ()))