getMethodOffset,
getFieldOffset,
getStaticFieldAddr,
- getInterfaceMethodOffset
+ getInterfaceMethodOffset,
+ addClassPath,
+ addClassPathJAR
) where
import Data.Int
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.ByteString.Lazy as B
+import Data.String.Utils
import Control.Monad
#ifdef DEBUG
import Foreign.C.Types
import Foreign.Storable
+import Data.IORef
+import System.IO.Unsafe
+import System.Directory
+
import JVM.ClassFile
import JVM.Converter
+import Java.ClassPath hiding (Directory)
+import Java.JAR
import Mate.BasicBlocks
import {-# SOURCE #-} Mate.MethodPool
import Mate.Types
-import Mate.Utilities
import Mate.Debug
import Mate.GarbageAlloc
getClassInfo :: B.ByteString -> IO ClassInfo
getClassInfo path = do
- class_map <- get_classmap >>= ptr2classmap
+ class_map <- getClassMap
case M.lookup path class_map of
Nothing -> loadAndInitClass path
Just ci -> return ci
-getClassFile :: B.ByteString -> IO (Class Resolved)
+getClassFile :: B.ByteString -> IO (Class Direct)
getClassFile path = do
ci <- getClassInfo path
return $ ciFile ci
-- one slot for "method-table-ptr"
return $ (1 + fsize) * 4
-foreign export ccall getStaticFieldAddr :: CUInt -> Ptr () -> IO CUInt
-getStaticFieldAddr :: CUInt -> Ptr () -> IO CUInt
-getStaticFieldAddr from ptr_trapmap = do
- trapmap <- ptr2trapmap ptr_trapmap
+getStaticFieldAddr :: CUInt -> IO CUInt
+getStaticFieldAddr from = do
+ trapmap <- getTrapMap
let w32_from = fromIntegral from
let sfi = trapmap M.! w32_from
case sfi of
getInterfaceMethodOffset :: B.ByteString -> B.ByteString -> B.ByteString -> IO Word32
getInterfaceMethodOffset ifname meth sig = do
loadInterface ifname
- ifmmap <- get_interfacemethodmap >>= ptr2interfacemethodmap
+ 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"
-loadClass :: B.ByteString -> IO ClassInfo
-loadClass path = do
- let rpath = toString $ path `B.append` ".class"
- cfile <- parseClassFile rpath
+readClass :: B.ByteString -> IO ClassInfo
+readClass path = do
+ cfile <- readClassFile $ toString path
#ifdef DBG_CLASS
dumpClass cfile
#endif
sequence_ [ loadInterface i | i <- interfaces cfile ]
superclass <- if path /= "java/lang/Object"
then do
- sc <- loadClass $ superClass cfile
+ sc <- readClass $ superClass cfile
return $ Just sc
else return Nothing
(staticmap, fieldmap) <- calculateFields cfile superclass
(methodmap, mbase) <- calculateMethodMap cfile superclass
- immap <- get_interfacemethodmap >>= ptr2interfacemethodmap
+ immap <- getInterfaceMethodMap
-- allocate interface offset table for this class
-- TODO(bernhard): we have some duplicates in immap (i.e. some
printfCp "mbase: 0x%08x\n" mbase
printfCp "interfacemethod: %s @ %s\n" (show immap) (toString path)
printfCp "iftable: 0x%08x\n" w32_iftable
- virtual_map <- get_virtualmap >>= ptr2virtualmap
- let virtual_map' = M.insert mbase path virtual_map
- virtualmap2ptr virtual_map' >>= set_virtualmap
+ virtual_map <- getVirtualMap
+ setVirtualMap $ M.insert mbase path virtual_map
- class_map <- get_classmap >>= ptr2classmap
+ class_map <- getClassMap
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
+ setClassMap $ M.insert path new_ci class_map
return new_ci
loadInterface :: B.ByteString -> IO ()
loadInterface path = do
- imap <- get_interfacesmap >>= ptr2interfacesmap
+ imap <- getInterfaceMap
-- interface already loaded?
case M.lookup path imap of
Just _ -> return ()
Nothing -> do
printfCp "interface: loading \"%s\"\n" $ toString path
- let ifpath = toString $ path `B.append` ".class"
- cfile <- parseClassFile ifpath
+ cfile <- readClassFile $ toString path
-- load "superinterfaces" first
sequence_ [ loadInterface i | i <- interfaces cfile ]
- immap <- get_interfacemethodmap >>= ptr2interfacemethodmap
+ immap <- getInterfaceMethodMap
-- load map again, because there could be new entries now
-- due to loading superinterfaces
- imap' <- get_interfacesmap >>= ptr2interfacesmap
+ imap' <- getInterfaceMap
let max_off = fromIntegral $ M.size immap * 4
-- create index of methods by this interface
let mm = zipbase max_off (classMethods cfile)
let sm = zipWith (\x y -> (entry y, immap M.! getname x y)) ifnames methodnames
-- merge all offset tables
- let methodmap = M.fromList sm `M.union` M.fromList mm `M.union` immap
- interfacemethodmap2ptr methodmap >>= set_interfacemethodmap
-
- interfacesmap2ptr (M.insert path cfile imap') >>= set_interfacesmap
+ 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)
-calculateFields :: Class Resolved -> Maybe ClassInfo -> IO (FieldMap, FieldMap)
+calculateFields :: Class Direct -> Maybe ClassInfo -> IO (FieldMap, FieldMap)
calculateFields cf superclass = do
-- TODO(bernhard): correct sizes. int only atm
getsupermap superclass getter = case superclass of Just x -> getter x; Nothing -> M.empty
-calculateMethodMap :: Class Resolved -> Maybe ClassInfo -> IO (FieldMap, Word32)
+calculateMethodMap :: Class Direct -> Maybe ClassInfo -> IO (FieldMap, Word32)
calculateMethodMap cf superclass = do
let methods = filter
(\x -> (not . S.member ACC_STATIC . methodAccessFlags) x &&
loadAndInitClass :: B.ByteString -> IO ClassInfo
loadAndInitClass path = do
- class_map <- get_classmap >>= ptr2classmap
+ class_map <- getClassMap
ci <- case M.lookup path class_map of
- Nothing -> loadClass path
+ Nothing -> readClass path
Just x -> return x
-- first try to execute class initializer of superclass
printfCp "executing static initializer from %s now\n" (toString path)
executeFuncPtr entry
printfCp "static initializer from %s done\n" (toString path)
- Nothing -> error "loadClass: static initializer not found (WTF?). abort"
+ Nothing -> error "readClass: static initializer not found (WTF?). abort"
Nothing -> return ()
- class_map' <- get_classmap >>= ptr2classmap
+ class_map' <- getClassMap
let new_ci = ci { ciInitDone = True }
- let class_map'' = M.insert path new_ci class_map'
- classmap2ptr class_map'' >>= set_classmap
+ setClassMap $ M.insert path new_ci class_map'
return new_ci
+
+
+readClassFile :: String -> IO (Class Direct)
+readClassFile path' = readIORef classPaths >>= rcf
+ where
+ path = replace "." "/" path'
+ rcf :: [MClassPath] -> IO (Class Direct)
+ rcf [] = error $ "readClassFile: Class \"" ++ (show path) ++ "\" not found."
+ rcf ((Directory pre):xs) = do
+ let cf = pre ++ path ++ ".class"
+ printfCp "rcf: searching @ %s for %s\n" (show pre) (show path)
+ b <- doesFileExist cf
+ if b
+ then parseClassFile cf
+ else rcf xs
+ rcf ((JAR p):xs) = do
+ printfCp "rcf: searching %s in JAR\n" (show path)
+ entry <- getEntry p path
+ case entry of
+ Just (LoadedJAR _ cls) -> return cls
+ Nothing -> rcf xs
+ _ -> error $ "readClassFile: Class \"" ++ show path ++ "\" in JAR not found. #1"
+
+data MClassPath =
+ Directory String |
+ JAR [Tree CPEntry]
+
+classPaths :: IORef [MClassPath]
+{-# NOINLINE classPaths #-}
+classPaths = unsafePerformIO $ newIORef []
+
+addClassPath :: String -> IO ()
+addClassPath x = do
+ cps <- readIORef classPaths
+ writeIORef classPaths (Directory x:cps)
+
+addClassPathJAR :: String -> IO ()
+addClassPathJAR x = do
+ cps <- readIORef classPaths
+ t <- execClassPath $ addJAR x
+ writeIORef classPaths (JAR t:cps)