2 {-# LANGUAGE OverloadedStrings #-}
4 module Mate.ClassPool (
13 getInterfaceMethodOffset,
20 import qualified Data.Map as M
21 import qualified Data.Set as S
23 import qualified Data.ByteString.Lazy as B
24 import Data.String.Utils
35 import Foreign.C.Types
36 import Foreign.Storable
39 import System.IO.Unsafe
40 import System.Directory
44 import Java.ClassPath hiding (Directory)
47 import Mate.BasicBlocks
48 import {-# SOURCE #-} Mate.MethodPool
51 import Mate.GarbageAlloc
52 import Mate.NativeSizes
54 getClassInfo :: B.ByteString -> IO ClassInfo
55 getClassInfo path = do
56 class_map <- getClassMap
57 case M.lookup path class_map of
58 Nothing -> loadAndInitClass path
61 classLoaded :: B.ByteString -> IO Bool
63 class_map <- getClassMap
64 return $ M.member path class_map
66 getClassFile :: B.ByteString -> IO (Class Direct)
67 getClassFile path = do
68 ci <- getClassInfo path
71 getStaticFieldOffset :: B.ByteString -> B.ByteString -> IO CPtrdiff
72 getStaticFieldOffset path field = do
73 ci <- getClassInfo path
74 return $ fromIntegral $ ciStaticMap ci M.! field
76 getFieldOffset :: B.ByteString -> B.ByteString -> IO Int32
77 getFieldOffset path field = do
78 ci <- getClassInfo path
79 return $ ciFieldMap ci M.! field
81 -- method + signature plz!
82 getMethodOffset :: B.ByteString -> B.ByteString -> IO NativeWord
83 getMethodOffset path method = do
84 ci <- getClassInfo path
85 -- (+ ptrSize) one slot for "interface-table-ptr"
86 return $ (+ ptrSize) $ fromIntegral $ ciMethodMap ci M.! method
88 getMethodTable :: B.ByteString -> IO NativeWord
89 getMethodTable path = do
90 ci <- getClassInfo path
91 return $ ciMethodBase ci
93 getObjectSize :: B.ByteString -> IO NativeWord
94 getObjectSize path = do
95 ci <- getClassInfo path
96 -- TODO(bernhard): correct sizes for different types...
97 let fsize = fromIntegral $ M.size $ ciFieldMap ci
98 -- one slot for "method-table-ptr"
99 return $ (1 + fsize) * ptrSize
101 getStaticFieldAddr :: CPtrdiff -> IO CPtrdiff
102 getStaticFieldAddr from = do
103 trapmap <- getTrapMap
104 let w32_from = fromIntegral from
105 let sfi = trapmap M.! w32_from
106 setTrapMap $ M.delete w32_from trapmap
108 (StaticField (StaticFieldInfo cls field)) -> getStaticFieldOffset cls field
109 _ -> error "getFieldAddr: no TrapCause found. abort"
111 -- interface + method + signature plz!
112 getInterfaceMethodOffset :: B.ByteString -> B.ByteString -> B.ByteString -> IO NativeWord
113 getInterfaceMethodOffset ifname meth sig = do
115 ifmmap <- getInterfaceMethodMap
116 let k = ifname `B.append` meth `B.append` sig
117 case M.lookup k ifmmap of
118 Just w32 -> return $ w32 + 4
119 Nothing -> error "getInterfaceMethodOffset: no offset set"
122 readClass :: B.ByteString -> IO ClassInfo
124 class_map' <- getClassMap
125 case M.lookup path class_map' of
128 cfile <- readClassFile $ toString path
132 -- load all interfaces, which are implemented by this class
133 sequence_ [ loadInterface i | i <- interfaces cfile ]
134 superclass <- if path /= "java/lang/Object"
136 sc <- readClass $ superClass cfile
140 (staticmap, fieldmap) <- calculateFields cfile superclass
141 (methodmap, mbase) <- calculateMethodMap cfile superclass
142 immap <- getInterfaceMethodMap
144 -- allocate interface offset table for this class
145 -- TODO(bernhard): we have some duplicates in immap (i.e. some
146 -- entries have the same offset), so we could
147 -- save some memory here.
148 iftable <- mallocClassData ((4*) $ M.size immap)
149 let wn_iftable = fromIntegral $ ptrToIntPtr iftable :: NativeWord
150 -- store interface-table at offset 0 in method-table
151 pokeElemOff (intPtrToPtr $ fromIntegral mbase) 0 wn_iftable
153 let strpath = toString path
154 hexDumpMap ("staticmap @ " ++ strpath) staticmap
155 hexDumpMap ("fieldmap @ " ++ strpath) fieldmap
156 hexDumpMap ("methodmap @ " ++ strpath) methodmap
157 hexDumpMap ("interfacemap @ " ++ strpath) immap
159 printfCp "mbase: 0x%08x\n" mbase
160 printfCp "iftable: 0x%08x\n" wn_iftable
161 virtual_map <- getVirtualMap
162 setVirtualMap $ M.insert mbase path virtual_map
164 class_map <- getClassMap
165 let new_ci = ClassInfo path cfile staticmap fieldmap methodmap mbase False
166 setClassMap $ M.insert path new_ci class_map
170 loadInterface :: B.ByteString -> IO ()
171 loadInterface path = do
172 imap <- getInterfaceMap
173 -- interface already loaded?
174 case M.lookup path imap of
177 printfCp "interface: loading \"%s\"\n" $ toString path
178 cfile <- readClassFile $ toString path
179 -- load "superinterfaces" first
180 sequence_ [ loadInterface i | i <- interfaces cfile ]
181 immap <- getInterfaceMethodMap
183 -- load map again, because there could be new entries now
184 -- due to loading superinterfaces
185 imap' <- getInterfaceMap
186 let max_off = fromIntegral $ M.size immap * 4
187 -- create index of methods by this interface
188 let mm = zipbase max_off (classMethods cfile)
190 -- create for each method from *every* superinterface a entry to,
191 -- but just put in the same offset as it is already in the map
192 let (ifnames, methodnames) = unzip $ concat
193 [ zip (repeat ifname) (classMethods $ imap' M.! ifname)
194 | ifname <- interfaces cfile ]
195 let sm = zipWith (\x y -> (entry y, immap M.! getname x y)) ifnames methodnames
197 -- merge all offset tables
198 setInterfaceMethodMap $ M.fromList sm `M.union` M.fromList mm `M.union` immap
199 setInterfaceMap $ M.insert path cfile imap'
201 zipbase base = zipWith (\x y -> (entry y, x + base)) [0,4..]
203 getname p y = p `B.append` methodName y `B.append` encode (methodSignature y)
206 calculateFields :: Class Direct -> Maybe ClassInfo -> IO (FieldMap, FieldMap)
207 calculateFields cf superclass = do
208 -- TODO(bernhard): correct sizes. int only atm
210 let (sfields, ifields) = partition (S.member ACC_STATIC . fieldAccessFlags) (classFields cf)
212 let sc_sm = getsupermap superclass ciStaticMap
213 staticbase <- mallocClassData $ fromIntegral (length sfields) * 4
214 let sm = zipbase (fromIntegral $ ptrToIntPtr staticbase) sfields
215 -- new fields "overwrite" old ones, if they have the same name
216 let staticmap = sm `M.union` sc_sm
218 let sc_im = getsupermap superclass ciFieldMap
219 -- "+ 4" for the method table pointer
220 let max_off = (4+) $ fromIntegral $ M.size sc_im * 4
221 let im = zipbase max_off ifields
222 -- new fields "overwrite" old ones, if they have the same name
223 let fieldmap = im `M.union` sc_im
225 return (staticmap, fieldmap)
227 zipbase :: Int32 -> [Field Direct] -> FieldMap
228 zipbase base = foldr (\(x,y) -> M.insert (fieldName y) (x + base)) M.empty . zip [0,4..]
231 getsupermap :: Maybe ClassInfo -> (ClassInfo -> FieldMap) -> FieldMap
232 getsupermap superclass getter = case superclass of Just x -> getter x; Nothing -> M.empty
235 calculateMethodMap :: Class Direct -> Maybe ClassInfo -> IO (FieldMap, NativeWord)
236 calculateMethodMap cf superclass = do
238 (\x -> (not . S.member ACC_STATIC . methodAccessFlags) x &&
239 ((/=) "<init>" . methodName) x)
241 let sc_mm = getsupermap superclass ciMethodMap
242 let max_off = fromIntegral $ M.size sc_mm * 4
243 let mm = zipbase max_off methods
244 let methodmap = M.fromList mm `M.union` sc_mm
246 -- (+1): one slot for the interface-table-ptr
247 methodbase <- mallocClassData (((+1) $ fromIntegral $ M.size methodmap) * 4)
248 return (methodmap, fromIntegral $ ptrToIntPtr methodbase)
249 where zipbase base = zipWith (\x y -> (entry y, x + base)) [0,4..]
250 where entry y = methodName y `B.append` encode (methodSignature y)
253 loadAndInitClass :: B.ByteString -> IO ClassInfo
254 loadAndInitClass path = do
255 class_map <- getClassMap
256 ci <- case M.lookup path class_map of
257 Nothing -> readClass path
260 -- first try to execute class initializer of superclass
261 when (path /= "java/lang/Object") (void $ loadAndInitClass $ superClass $ ciFile ci)
263 -- execute class initializer
264 case lookupMethod "<clinit>" (ciFile ci) of
266 rawmethod <- parseMethod (ciFile ci) "<clinit>" $ MethodSignature [] ReturnsVoid
267 let mi = MethodInfo "<clinit>" path (methodSignature m)
268 entry <- compileBB rawmethod mi
269 addMethodRef entry mi [path]
270 printfCp "executing static initializer from %s now\n" (toString path)
272 printfCp "static initializer from %s done\n" (toString path)
275 class_map' <- getClassMap
276 let new_ci = ci { ciInitDone = True }
277 setClassMap $ M.insert path new_ci class_map'
281 readClassFile :: String -> IO (Class Direct)
282 readClassFile path' = readIORef classPaths >>= rcf
284 path = replace "." "/" path'
285 rcf :: [MClassPath] -> IO (Class Direct)
286 rcf [] = error $ "readClassFile: Class \"" ++ show path ++ "\" not found."
287 rcf (Directory pre:xs) = do
288 let cf = pre ++ path ++ ".class"
289 printfCp "rcf: searching @ %s for %s\n" (show pre) (show path)
290 b <- doesFileExist cf
292 then parseClassFile cf
295 printfCp "rcf: searching %s in JAR\n" (show path)
296 entry <- getEntry p path
298 Just (LoadedJAR _ cls) -> return cls
300 _ -> error $ "readClassFile: Class \"" ++ show path ++ "\" in JAR not found. #1"
306 classPaths :: IORef [MClassPath]
307 {-# NOINLINE classPaths #-}
308 classPaths = unsafePerformIO $ newIORef []
310 addClassPath :: String -> IO ()
312 cps <- readIORef classPaths
313 writeIORef classPaths (Directory x:cps)
315 addClassPathJAR :: String -> IO ()
316 addClassPathJAR x = do
317 cps <- readIORef classPaths
318 t <- execClassPath $ addJAR x
319 writeIORef classPaths (JAR t:cps)