2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE ForeignFunctionInterface #-}
5 module Mate.MethodPool where
8 import Data.String.Utils
9 import qualified Data.Map as M
10 import qualified Data.Set as S
11 import qualified Data.ByteString.Lazy as B
15 import Foreign.C.Types
16 import Foreign.C.String
22 import Harpy.X86Disassembler
29 import Mate.BasicBlocks
31 import Mate.NativeMachine
36 foreign import ccall "dynamic"
37 code_void :: FunPtr (IO ()) -> IO ()
40 getMethodEntry :: CPtrdiff -> CPtrdiff -> IO CPtrdiff
41 getMethodEntry signal_from methodtable = do
46 let w32_from = fromIntegral signal_from
47 let mi = tmap M.! w32_from
48 let mi'@(MethodInfo method cm sig) =
51 (VirtualCall _ (MethodInfo methname _ msig) _) -> newMi methname msig
52 _ -> error "getMethodEntry: no TrapCause found. abort."
53 where newMi mn = MethodInfo mn (vmap M.! fromIntegral methodtable)
54 -- bernhard (TODO): doesn't work with gnu classpath at some point. didn't
55 -- figured out the problem yet :/ therefore, I have no
56 -- testcase for replaying the situation.
57 -- setTrapMap $ M.delete w32_from tmap
58 entryaddr <- case M.lookup mi' mmap of
60 cls <- getClassFile cm
61 printfMp "getMethodEntry(from 0x%08x): no method \"%s\" found. compile it\n" w32_from (show mi')
62 mm <- lookupMethodRecursive method sig [] cls
64 Just (mm', clsnames, cls') -> do
65 let flags = methodAccessFlags mm'
66 if S.member ACC_NATIVE flags
68 -- TODO(bernhard): cleaner please... *do'h*
69 let sym1 = replace "/" "_" $ toString cm
70 parenth = replace "(" "_" $ replace ")" "_" $ toString $ encode sig
71 sym2 = replace ";" "_" $ replace "/" "_" parenth
72 symbol = sym1 ++ "__" ++ toString method ++ "__" ++ sym2
73 printfMp "native-call: symbol: %s\n" symbol
74 nf <- loadNativeFunction symbol
75 setMethodMap $ M.insert mi' nf mmap
78 rawmethod <- parseMethod cls' method sig
79 entry <- compileBB rawmethod (MethodInfo method (thisClass cls') sig)
80 addMethodRef entry mi' clsnames
81 return $ fromIntegral entry
82 Nothing -> error $ show method ++ " not found. abort"
83 Just w32 -> return w32
84 return $ fromIntegral entryaddr
86 lookupMethodRecursive :: B.ByteString -> MethodSignature -> [B.ByteString] -> Class Direct
87 -> IO (Maybe (Method Direct, [B.ByteString], Class Direct))
88 lookupMethodRecursive name sig clsnames cls =
90 Just x -> return $ Just (x, nextclsn, cls)
91 Nothing -> if thisname == "java/lang/Object"
94 supercl <- getClassFile (superClass cls)
95 lookupMethodRecursive name sig nextclsn supercl
97 res = lookupMethodSig name sig cls
98 thisname = thisClass cls
99 nextclsn :: [B.ByteString]
100 nextclsn = thisname:clsnames
102 -- TODO(bernhard): UBERHAX. ghc patch?
103 foreign import ccall safe "lookupSymbol"
104 c_lookupSymbol :: CString -> IO (Ptr a)
106 loadNativeFunction :: String -> IO NativeWord
107 loadNativeFunction sym = do
108 _ <- loadRawObject "ffi/native.o"
109 -- TODO(bernhard): WTF
110 resolveObjs (return ())
111 ptr <- withCString sym c_lookupSymbol
113 then error $ "dyn. loading of \"" ++ sym ++ "\" failed."
114 else return $ fromIntegral $ ptrToIntPtr ptr
118 -- (entry, _) <- testCase "./tests/Fib.class" "fib"
119 -- let int_entry = ((fromIntegral $ ptrToIntPtr entry) :: NativeWord)
120 -- let mmap = M.insert ("fib" :: String) int_entry M.empty
121 -- mapM_ (\(x,y) -> printf "%s at 0x%08x\n" x y) $ M.toList mmap
122 -- mmap2ptr mmap >>= set_mmap
123 -- demo_mmap -- access Data.Map from C
125 addMethodRef :: NativeWord -> MethodInfo -> [B.ByteString] -> IO ()
126 addMethodRef entry (MethodInfo mmname _ msig) clsnames = do
128 let newmap = foldr (\i -> M.insert (MethodInfo mmname i msig) entry) M.empty clsnames
129 setMethodMap $ mmap `M.union` newmap
132 compileBB :: RawMethod -> MethodInfo -> IO NativeWord
133 compileBB rawmethod methodinfo = do
136 cls <- getClassFile (methClassName methodinfo)
137 let ebb = emitFromBB cls rawmethod
138 let cgconfig = defaultCodeGenConfig { codeBufferSize = fromIntegral $ (rawCodeLength rawmethod) * 32 }
139 (_, Right right) <- runCodeGenWithConfig ebb () () cgconfig
141 let ((entry, _, _, new_tmap), _) = right
142 setTrapMap $ tmap `M.union` new_tmap -- prefers elements in tmap
144 printfJit "generated code of \"%s\" from \"%s\":\n" (toString $ methName methodinfo) (toString $ methClassName methodinfo)
145 printfJit "\tstacksize: 0x%04x, locals: 0x%04x\n" (rawStackSize rawmethod) (rawLocals rawmethod)
147 mapM_ (printfJit "%s\n" . showAtt) (snd right)
150 -- UNCOMMENT NEXT LINES FOR GDB FUN
151 -- if (toString $ methName methodinfo) == "thejavamethodIwant2debug"
152 -- then putStrLn "press CTRL+C now for setting a breakpoint. then `c' and ENTER for continue" >> getLine
154 -- (1) build a debug build (see HACKING) and execute `make tests/Fib.gdb'
155 -- for example, where the suffix is important
156 -- (2) on getLine, press CTRL+C
157 -- (3) `br *0x<addr>'; obtain the address from the disasm above
158 -- (4) `cont' and press enter
159 return $ fromIntegral $ ptrToIntPtr entry
162 executeFuncPtr :: NativeWord -> IO ()
163 executeFuncPtr entry =
164 code_void ((castPtrToFunPtr $ intPtrToPtr $ fromIntegral entry) :: FunPtr (IO ()))