- (SFI (StaticFieldInfo cls field)) -> do
- getStaticFieldOffset cls field
- _ -> error $ "getFieldAddr: no trapInfo. abort"
-
-loadClass :: B.ByteString -> IO ClassInfo
-loadClass path = do
- printf "loadClass: \"%s\"\n" $ toString path
- let rpath = toString $ path `B.append` ".class"
- cfile <- parseClassFile rpath
- superclass <- case (path /= "java/lang/Object") of
- True -> do
- sc <- loadClass $ superClass cfile
- return $ Just $ sc
- False -> return $ Nothing
-
- (staticmap, fieldmap) <- calculateFields cfile superclass
- printf "staticmap: %s @ %s\n" (show staticmap) (toString path)
- printf "fieldmap: %s @ %s\n" (show fieldmap) (toString path)
- (methodmap, mbase) <- calculateMethodMap cfile superclass
- printf "methodmap: %s @ %s\n" (show methodmap) (toString path)
- printf "mbase: 0x%08x\n" mbase
-
- virtual_map <- get_virtualmap >>= ptr2virtualmap
- let virtual_map' = M.insert mbase path virtual_map
- virtualmap2ptr virtual_map' >>= set_virtualmap
-
- class_map <- get_classmap >>= ptr2classmap
- let new_ci = ClassInfo path cfile staticmap fieldmap methodmap mbase False
- let class_map' = M.insert path new_ci class_map
- classmap2ptr class_map' >>= set_classmap
- return new_ci
+ (StaticField (StaticFieldInfo cls field)) -> getStaticFieldOffset cls field
+ _ -> error "getFieldAddr: no TrapCause found. abort"
+
+-- interface + method + signature plz!
+getInterfaceMethodOffset :: B.ByteString -> B.ByteString -> B.ByteString -> IO Word32
+getInterfaceMethodOffset ifname meth sig = do
+ loadInterface ifname
+ ifmmap <- getInterfaceMethodMap
+ let k = ifname `B.append` meth `B.append` sig
+ case M.lookup k ifmmap of
+ Just w32 -> return $ w32 + 4
+ Nothing -> error "getInterfaceMethodOffset: no offset set"
+
+
+readClass :: B.ByteString -> IO ClassInfo
+readClass path = do
+ class_map' <- getClassMap
+ case M.lookup path class_map' of
+ Just cm -> return cm
+ Nothing -> do
+ cfile <- readClassFile $ toString path
+#ifdef DBG_CLASS
+ dumpClass cfile
+#endif
+ -- load all interfaces, which are implemented by this class
+ sequence_ [ loadInterface i | i <- interfaces cfile ]
+ superclass <- if path /= "java/lang/Object"
+ then do
+ sc <- readClass $ superClass cfile
+ return $ Just sc
+ else return Nothing
+
+ (staticmap, fieldmap) <- calculateFields cfile superclass
+ (methodmap, mbase) <- calculateMethodMap cfile superclass
+ immap <- getInterfaceMethodMap
+
+ -- allocate interface offset table for this class
+ -- TODO(bernhard): we have some duplicates in immap (i.e. some
+ -- entries have the same offset), so we could
+ -- save some memory here.
+ iftable <- mallocClassData ((4*) $ M.size immap)
+ let w32_iftable = fromIntegral $ ptrToIntPtr iftable :: Word32
+ -- store interface-table at offset 0 in method-table
+ pokeElemOff (intPtrToPtr $ fromIntegral mbase) 0 w32_iftable
+ printfCp "staticmap: %s @ %s\n" (show staticmap) (toString path)
+ printfCp "fieldmap: %s @ %s\n" (show fieldmap) (toString path)
+ printfCp "methodmap: %s @ %s\n" (show methodmap) (toString path)
+ printfCp "mbase: 0x%08x\n" mbase
+ printfCp "interfacemethod: %s @ %s\n" (show immap) (toString path)
+ printfCp "iftable: 0x%08x\n" w32_iftable
+ virtual_map <- getVirtualMap
+ setVirtualMap $ M.insert mbase path virtual_map
+
+ class_map <- getClassMap
+ let new_ci = ClassInfo path cfile staticmap fieldmap methodmap mbase False
+ setClassMap $ M.insert path new_ci class_map
+ return new_ci
+
+
+loadInterface :: B.ByteString -> IO ()
+loadInterface path = do
+ imap <- getInterfaceMap
+ -- interface already loaded?
+ case M.lookup path imap of
+ Just _ -> return ()
+ Nothing -> do
+ printfCp "interface: loading \"%s\"\n" $ toString path
+ cfile <- readClassFile $ toString path
+ -- load "superinterfaces" first
+ sequence_ [ loadInterface i | i <- interfaces cfile ]
+ immap <- getInterfaceMethodMap
+
+ -- load map again, because there could be new entries now
+ -- due to loading superinterfaces
+ imap' <- getInterfaceMap
+ let max_off = fromIntegral $ M.size immap * 4
+ -- create index of methods by this interface
+ let mm = zipbase max_off (classMethods cfile)
+
+ -- create for each method from *every* superinterface a entry to,
+ -- but just put in the same offset as it is already in the map
+ let (ifnames, methodnames) = unzip $ concat
+ [ zip (repeat ifname) (classMethods $ imap' M.! ifname)
+ | ifname <- interfaces cfile ]
+ let sm = zipWith (\x y -> (entry y, immap M.! getname x y)) ifnames methodnames
+
+ -- merge all offset tables
+ setInterfaceMethodMap $ M.fromList sm `M.union` M.fromList mm `M.union` immap
+ setInterfaceMap $ M.insert path cfile imap'
+ where
+ zipbase base = zipWith (\x y -> (entry y, x + base)) [0,4..]
+ entry = getname path
+ getname p y = p `B.append` methodName y `B.append` encode (methodSignature y)