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
14 import Foreign.C.Types
15 import Foreign.C.String
20 import Harpy.X86Disassembler
22 import Mate.BasicBlocks
24 import Mate.NativeMachine
30 foreign import ccall "dynamic"
31 code_void :: FunPtr (IO ()) -> IO ()
33 foreign import ccall "&printMemoryUsage"
34 printMemoryUsageAddr :: FunPtr (IO ())
36 foreign import ccall "&loadLibrary"
37 loadLibraryAddr :: FunPtr (IO ())
39 foreign import ccall "&printGCStats"
40 printGCStatsAddr :: FunPtr (IO ())
42 getMethodEntry :: MethodInfo -> IO (CPtrdiff, JpcNpcMap)
43 getMethodEntry mi@(MethodInfo method cm sig) = do
46 (entryaddr, jnmap) <- case M.lookup mi mmap of
48 cls <- getClassFile cm
49 printfMp $ printf "getMethodEntry: no method \"%s\" found. compile it\n" (show mi)
50 mm <- lookupMethodRecursive method sig [] cls
52 Just (mm', clsnames, cls') -> do
53 let flags = methodAccessFlags mm'
54 if S.member ACC_NATIVE flags
56 let scm = toString cm; smethod = toString method
57 if scm == "jmate/lang/MateRuntime" then
60 return (funPtrToAddr loadLibraryAddr, M.empty)
62 return (funPtrToAddr printGCStatsAddr, M.empty)
64 return (funPtrToAddr printMemoryUsageAddr, M.empty)
66 error $ "native-call: " ++ smethod ++ " not found."
68 -- TODO(bernhard): cleaner please... *do'h*
69 let sym1 = replace "/" "_" scm
70 parenth = replace "(" "_" $ replace ")" "_" $ toString $ encode sig
71 sym2 = replace ";" "_" $ replace "/" "_" parenth
72 symbol = sym1 ++ "__" ++ smethod ++ "__" ++ sym2
73 printfMp $ printf "native-call: symbol: %s\n" symbol
74 nf <- loadNativeFunction symbol
75 let nf' = (nf, M.empty)
76 setMethodMap $ M.insert mi nf' mmap
79 rawmethod <- parseMethod cls' method sig
80 entry <- compileBB rawmethod (MethodInfo method (thisClass cls') sig)
81 addMethodRef entry mi clsnames
83 Nothing -> error $ show method ++ " not found. abort"
84 Just w32 -> return w32
85 return (fromIntegral entryaddr, jnmap)
87 funPtrToAddr :: Num b => FunPtr a -> b
88 funPtrToAddr = fromIntegral . ptrToIntPtr . castFunPtrToPtr
90 lookupMethodRecursive :: B.ByteString -> MethodSignature -> [B.ByteString] -> Class Direct
91 -> IO (Maybe (Method Direct, [B.ByteString], Class Direct))
92 lookupMethodRecursive name sig clsnames cls =
94 Just x -> return $ Just (x, nextclsn, cls)
95 Nothing -> if thisname == "java/lang/Object"
98 supercl <- getClassFile (superClass cls)
99 lookupMethodRecursive name sig nextclsn supercl
101 res = lookupMethodSig name sig cls
102 thisname = thisClass cls
103 nextclsn :: [B.ByteString]
104 nextclsn = thisname:clsnames
106 -- TODO(bernhard): UBERHAX. ghc patch?
107 foreign import ccall safe "lookupSymbol"
108 c_lookupSymbol :: CString -> IO (Ptr a)
110 loadNativeFunction :: String -> IO NativeWord
111 loadNativeFunction sym = do
112 _ <- loadRawObject "ffi/native.o"
113 -- TODO(bernhard): WTF
114 resolveObjs (return ())
115 ptr <- withCString sym c_lookupSymbol
117 then error $ "dyn. loading of \"" ++ sym ++ "\" failed."
118 else return $ fromIntegral $ ptrToIntPtr ptr
122 -- (entry, _) <- testCase "./tests/Fib.class" "fib"
123 -- let int_entry = ((fromIntegral $ ptrToIntPtr entry) :: NativeWord)
124 -- let mmap = M.insert ("fib" :: String) int_entry M.empty
125 -- mapM_ (\(x,y) -> printf "%s at 0x%08x\n" x y) $ M.toList mmap
126 -- mmap2ptr mmap >>= set_mmap
127 -- demo_mmap -- access Data.Map from C
129 addMethodRef :: (NativeWord, JpcNpcMap) -> MethodInfo -> [B.ByteString] -> IO ()
130 addMethodRef entry (MethodInfo mmname _ msig) clsnames = do
132 let newmap = foldr (\i -> M.insert (MethodInfo mmname i msig) entry) M.empty clsnames
133 setMethodMap $ mmap `M.union` newmap
136 compileBB :: RawMethod -> MethodInfo -> IO (NativeWord, JpcNpcMap)
137 compileBB rawmethod methodinfo = do
140 cls <- getClassFile (methClassName methodinfo)
141 printfJit $ printf "emit code of \"%s\" from \"%s\":\n" (toString $ methName methodinfo) (toString $ methClassName methodinfo)
142 let ebb = emitFromBB cls rawmethod
143 let cgconfig = defaultCodeGenConfig { codeBufferSize = fromIntegral $ rawCodeLength rawmethod * 32 }
144 (jnmap, Right right) <- runCodeGenWithConfig ebb () M.empty cgconfig
146 let ((entry, _, new_tmap), _) = right
147 setTrapMap $ tmap `M.union` new_tmap -- prefers elements in tmap
149 printfJit $ printf "generated code of \"%s\" from \"%s\":\n" (toString $ methName methodinfo) (toString $ methClassName methodinfo)
150 printfJit $ printf "\tstacksize: 0x%04x, locals: 0x%04x\n" (rawStackSize rawmethod) (rawLocals rawmethod)
151 when mateDEBUG $ mapM_ (printfJit . printf "%s\n" . showIntel) (snd right)
152 printfJit $ printf "\n\n"
153 -- UNCOMMENT NEXT LINES FOR GDB FUN
154 -- if (toString $ methName methodinfo) == "thejavamethodIwant2debug"
155 -- then putStrLn "press CTRL+C now for setting a breakpoint. then `c' and ENTER for continue" >> getLine
157 -- (1) build a debug build (see HACKING) and execute `make tests/Fib.gdb'
158 -- for example, where the suffix is important
159 -- (2) on getLine, press CTRL+C
160 -- (3) `br *0x<addr>'; obtain the address from the disasm above
161 -- (4) `cont' and press enter
162 return (fromIntegral $ ptrToIntPtr entry, jnmap)
165 executeFuncPtr :: NativeWord -> IO ()
166 executeFuncPtr entry =
167 code_void ((castPtrToFunPtr $ intPtrToPtr $ fromIntegral entry) :: FunPtr (IO ()))