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
21 import Harpy.X86Disassembler
27 import Mate.BasicBlocks
29 import Mate.NativeMaschine
34 foreign import ccall "dynamic"
35 code_void :: FunPtr (IO ()) -> IO ()
38 getMethodEntry :: CPtrdiff -> CPtrdiff -> IO CPtrdiff
39 getMethodEntry signal_from methodtable = do
44 let w32_from = fromIntegral signal_from
45 let mi = tmap M.! w32_from
46 let mi'@(MethodInfo method cm sig) =
49 (VirtualMethod _ (MethodInfo methname _ msig)) -> newMi methname msig
50 (InterfaceMethod _ (MethodInfo methname _ msig)) -> newMi methname msig
51 _ -> error "getMethodEntry: no TrapCause found. abort."
52 where newMi mn = MethodInfo mn (vmap M.! fromIntegral methodtable)
53 -- bernhard (TODO): doesn't work with gnu classpath at some point. didn't
54 -- figured out the problem yet :/ therefore, I have no
55 -- testcase for replaying the situation.
56 -- setTrapMap $ M.delete w32_from tmap
57 entryaddr <- case M.lookup mi' mmap of
59 cls <- getClassFile cm
60 printfMp "getMethodEntry(from 0x%08x): no method \"%s\" found. compile it\n" w32_from (show mi')
61 mm <- lookupMethodRecursive method sig [] cls
63 Just (mm', clsnames, cls') -> do
64 let flags = methodAccessFlags mm'
65 if S.member ACC_NATIVE flags
67 -- TODO(bernhard): cleaner please... *do'h*
68 let sym1 = replace "/" "_" $ toString cm
69 parenth = replace "(" "_" $ replace ")" "_" $ toString $ encode sig
70 sym2 = replace ";" "_" $ replace "/" "_" parenth
71 symbol = sym1 ++ "__" ++ toString method ++ "__" ++ sym2
72 printfMp "native-call: symbol: %s\n" symbol
73 nf <- loadNativeFunction symbol
74 setMethodMap $ M.insert mi' nf mmap
77 rawmethod <- parseMethod cls' method sig
78 entry <- compileBB rawmethod (MethodInfo method (thisClass cls') sig)
79 addMethodRef entry mi' clsnames
80 return $ fromIntegral entry
81 Nothing -> error $ show method ++ " not found. abort"
82 Just w32 -> return w32
83 return $ fromIntegral entryaddr
85 lookupMethodRecursive :: B.ByteString -> MethodSignature -> [B.ByteString] -> Class Direct
86 -> IO (Maybe (Method Direct, [B.ByteString], Class Direct))
87 lookupMethodRecursive name sig clsnames cls =
89 Just x -> return $ Just (x, nextclsn, cls)
90 Nothing -> if thisname == "java/lang/Object"
93 supercl <- getClassFile (superClass cls)
94 lookupMethodRecursive name sig nextclsn supercl
96 res = lookupMethodSig name sig cls
97 thisname = thisClass cls
98 nextclsn :: [B.ByteString]
99 nextclsn = thisname:clsnames
101 -- TODO(bernhard): UBERHAX. ghc patch?
102 foreign import ccall safe "lookupSymbol"
103 c_lookupSymbol :: CString -> IO (Ptr a)
105 loadNativeFunction :: String -> IO Word32
106 loadNativeFunction sym = do
107 _ <- loadRawObject "ffi/native.o"
108 -- TODO(bernhard): WTF
109 resolveObjs (return ())
110 ptr <- withCString sym c_lookupSymbol
112 then error $ "dyn. loading of \"" ++ sym ++ "\" failed."
113 else return $ fromIntegral $ ptrToIntPtr ptr
117 -- (entry, _) <- testCase "./tests/Fib.class" "fib"
118 -- let int_entry = ((fromIntegral $ ptrToIntPtr entry) :: Word32)
119 -- let mmap = M.insert ("fib" :: String) int_entry M.empty
120 -- mapM_ (\(x,y) -> printf "%s at 0x%08x\n" x y) $ M.toList mmap
121 -- mmap2ptr mmap >>= set_mmap
122 -- demo_mmap -- access Data.Map from C
124 addMethodRef :: Word32 -> MethodInfo -> [B.ByteString] -> IO ()
125 addMethodRef entry (MethodInfo mmname _ msig) clsnames = do
127 let newmap = foldr (\i -> M.insert (MethodInfo mmname i msig) entry) M.empty clsnames
128 setMethodMap $ mmap `M.union` newmap
131 compileBB :: RawMethod -> MethodInfo -> IO Word32
132 compileBB rawmethod methodinfo = do
135 cls <- getClassFile (methClassName methodinfo)
136 let ebb = emitFromBB cls rawmethod
137 (_, Right right) <- runCodeGen ebb () ()
139 let ((entry, _, _, new_tmap), _) = right
140 setTrapMap $ tmap `M.union` new_tmap -- prefers elements in tmap
142 printfJit "generated code of \"%s\" from \"%s\":\n" (toString $ methName methodinfo) (toString $ methClassName methodinfo)
143 printfJit "\tstacksize: 0x%04x, locals: 0x%04x\n" (rawStackSize rawmethod) (rawLocals rawmethod)
144 mapM_ (printfJit "%s\n" . showAtt) (snd right)
146 -- UNCOMMENT NEXT LINES FOR GDB FUN
147 -- if (toString $ methName methodinfo) == "thejavamethodIwant2debug"
148 -- then putStrLn "press CTRL+C now for setting a breakpoint. then `c' and ENTER for continue" >> getLine
150 -- (1) build a debug build (see HACKING) and execute `make tests/Fib.gdb'
151 -- for example, where the suffix is important
152 -- (2) on getLine, press CTRL+C
153 -- (3) `br *0x<addr>'; obtain the address from the disasm above
154 -- (4) `cont' and press enter
155 return $ fromIntegral $ ptrToIntPtr entry
158 executeFuncPtr :: Word32 -> IO ()
159 executeFuncPtr entry =
160 code_void ((castPtrToFunPtr $ intPtrToPtr $ fromIntegral entry) :: FunPtr (IO ()))