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.X86CodeGen
34 foreign import ccall "dynamic"
35 code_void :: FunPtr (IO ()) -> IO ()
37 foreign export ccall getTrapType :: CUInt -> CUInt -> IO CUInt
38 getTrapType :: CUInt -> CUInt -> IO CUInt
39 getTrapType signal_from from2 = do
41 case M.lookup (fromIntegral signal_from) tmap of
42 (Just (MI _)) -> return 0
43 (Just (VI _)) -> return 1
44 (Just (SFI _)) -> return 2
45 (Just (II _)) -> return 4
46 -- maybe we've a hit on the second `from' value
47 Nothing -> case M.lookup (fromIntegral from2) tmap of
48 (Just (VI _)) -> return 1
49 (Just (II _)) -> return 4
50 (Just _) -> error "getTrapType: abort #1 :-("
51 Nothing -> error "getTrapType: abort #2 :-("
53 foreign export ccall getMethodEntry :: CUInt -> CUInt -> IO CUInt
54 getMethodEntry :: CUInt -> CUInt -> IO CUInt
55 getMethodEntry signal_from methodtable = do
60 let w32_from = fromIntegral signal_from
61 let mi = tmap M.! w32_from
62 let mi'@(MethodInfo method cm sig) =
65 (VI (MethodInfo methname _ msig)) ->
66 MethodInfo methname (vmap M.! fromIntegral methodtable) msig
67 (II (MethodInfo methname _ msig)) ->
68 MethodInfo methname (vmap M.! fromIntegral methodtable) msig
69 _ -> error "getMethodEntry: no trapInfo. abort."
70 case M.lookup mi' mmap of
72 cls <- getClassFile cm
73 printfMp "getMethodEntry(from 0x%08x): no method \"%s\" found. compile it\n" w32_from (show mi')
74 mm <- lookupMethodRecursive method [] cls
76 Just (mm', clsnames, cls') -> do
77 let flags = methodAccessFlags mm'
78 if S.member ACC_NATIVE flags
80 -- TODO(bernhard): cleaner please... *do'h*
81 let sym1 = replace "/" "_" $ toString cm
82 parenth = replace "(" "_" $ replace ")" "_" $ toString $ encode sig
83 sym2 = replace ";" "_" $ replace "/" "_" parenth
84 symbol = sym1 ++ "__" ++ toString method ++ "__" ++ sym2
85 printfMp "native-call: symbol: %s\n" symbol
86 nf <- loadNativeFunction symbol
87 let w32_nf = fromIntegral nf
88 setMethodMap $ M.insert mi' w32_nf mmap
91 hmap <- parseMethod cls' method
94 entry <- compileBB hmap' (MethodInfo method (thisClass cls') sig)
95 addMethodRef entry mi' clsnames
96 return $ fromIntegral entry
97 Nothing -> error $ show method ++ " not found. abort"
98 Nothing -> error $ show method ++ " not found. abort"
99 Just w32 -> return (fromIntegral w32)
101 lookupMethodRecursive :: B.ByteString -> [B.ByteString] -> Class Resolved
102 -> IO (Maybe (Method Resolved, [B.ByteString], Class Resolved))
103 lookupMethodRecursive name clsnames cls =
105 Just x -> return $ Just (x, nextclsn, cls)
106 Nothing -> if thisname == "java/lang/Object"
109 supercl <- getClassFile (superClass cls)
110 lookupMethodRecursive name nextclsn supercl
112 res = lookupMethod name cls
113 thisname = thisClass cls
114 nextclsn :: [B.ByteString]
115 nextclsn = thisname:clsnames
117 -- TODO(bernhard): UBERHAX. ghc patch?
118 foreign import ccall safe "lookupSymbol"
119 c_lookupSymbol :: CString -> IO (Ptr a)
121 loadNativeFunction :: String -> IO CUInt
122 loadNativeFunction sym = do
123 _ <- loadRawObject "ffi/native.o"
124 -- TODO(bernhard): WTF
125 resolveObjs (return ())
126 ptr <- withCString sym c_lookupSymbol
128 then error $ "dyn. loading of \"" ++ sym ++ "\" failed."
129 else return $ fromIntegral $ ptrToIntPtr ptr
133 -- (entry, _) <- testCase "./tests/Fib.class" "fib"
134 -- let int_entry = ((fromIntegral $ ptrToIntPtr entry) :: Word32)
135 -- let mmap = M.insert ("fib" :: String) int_entry M.empty
136 -- mapM_ (\(x,y) -> printf "%s at 0x%08x\n" x y) $ M.toList mmap
137 -- mmap2ptr mmap >>= set_mmap
138 -- demo_mmap -- access Data.Map from C
140 initMethodPool :: IO ()
141 initMethodPool = ctx2ptr emptyMateCtx >>= set_mate_context
144 addMethodRef :: Word32 -> MethodInfo -> [B.ByteString] -> IO ()
145 addMethodRef entry (MethodInfo mmname _ msig) clsnames = do
147 let newmap = M.fromList $ map (\x -> (MethodInfo mmname x msig, entry)) clsnames
148 setMethodMap $ mmap `M.union` newmap
151 compileBB :: MapBB -> MethodInfo -> IO Word32
152 compileBB hmap methodinfo = do
155 cls <- getClassFile (methClassName methodinfo)
156 let ebb = emitFromBB (methName methodinfo) cls hmap
157 (_, Right right) <- runCodeGen ebb () ()
159 let ((entry, _, _, new_tmap), _) = right
160 setTrapMap $ tmap `M.union` new_tmap -- prefers elements in tmap
162 printfJit "generated code of \"%s\":\n" (toString $ methName methodinfo)
163 mapM_ (printfJit "%s\n" . showAtt) (snd right)
165 -- UNCOMMENT NEXT LINE FOR GDB FUN
167 -- (1) start it with `gdb ./mate' and then `run <classfile>'
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 :: Word32 -> IO ()
175 executeFuncPtr entry =
176 code_void ((castPtrToFunPtr $ intPtrToPtr $ fromIntegral entry) :: FunPtr (IO ()))