instanceOf: make decision at runtime
[mate.git] / Mate / Types.hs
index 2cdad11970d731afad72661936c44eae36f3171c..7de8493a8523dddf9c8b6084e42e238a26d0fe22 100644 (file)
@@ -1,20 +1,42 @@
 {-# LANGUAGE OverloadedStrings #-}
-{-# LANGUAGE ForeignFunctionInterface #-}
-module Mate.Types where
+module Mate.Types
+  ( BlockID
+  , BasicBlock(..)
+  , BBEnd(..)
+  , MapBB
+  , RawMethod(..)
+  , TrapMap, MethodMap, ClassMap, FieldMap
+  , StringMap, VirtualMap, InterfaceMap
+  , InterfaceMethodMap
+  , TrapCause(..)
+  , StaticFieldInfo(..)
+  , MethodInfo(..)
+  , ClassInfo(..)
+  , setTrapMap, getTrapMap
+  , setMethodMap, getMethodMap
+  , setClassMap, getClassMap
+  , setStringMap, getStringMap
+  , setVirtualMap, getVirtualMap
+  , setInterfaceMap, getInterfaceMap
+  , setInterfaceMethodMap, getInterfaceMethodMap
+  ) where
 
-import Data.Char
-import Data.Word
 import Data.Int
+import Data.Functor
 import qualified Data.Map as M
 import qualified Data.ByteString.Lazy as B
-import Codec.Binary.UTF8.String hiding (encode,decode)
 
-import Foreign.Ptr
-import Foreign.StablePtr
+import Data.IORef
+import System.IO.Unsafe
+
+import Harpy
+import Foreign.C.Types
 
 import JVM.ClassFile
 import JVM.Assembler
 
+import Mate.NativeSizes
+
 
 type BlockID = Int
 -- Represents a CFG node
@@ -23,21 +45,37 @@ data BasicBlock = BasicBlock {
   successor :: BBEnd }
 
 -- describes (leaving) edges of a CFG node
-data BBEnd = Return | FallThrough BlockID | OneTarget BlockID | TwoTarget BlockID BlockID deriving Show
+data BBEnd
+  = Return
+  | FallThrough BlockID
+  | OneTarget BlockID
+  | TwoTarget BlockID BlockID
+  deriving Show
 
 type MapBB = M.Map BlockID BasicBlock
 
+data RawMethod = RawMethod {
+  rawMapBB :: MapBB,
+  rawLocals :: Int,
+  rawStackSize :: Int,
+  rawArgCount :: NativeWord,
+  rawCodeLength :: NativeWord }
 
 
--- Word32 = point of method call in generated code
+-- NativeWord = point of method call in generated code
 -- MethodInfo = relevant information about callee
-type TrapMap = M.Map Word32 TrapInfo
+type TrapMap = M.Map NativeWord TrapCause
 
-data TrapInfo =
-  MI MethodInfo | -- for static calls
-  VI MethodInfo | -- for virtual calls
-  II MethodInfo | -- for interface calls
-  SFI StaticFieldInfo deriving Show
+type TrapPatcher = CPtrdiff -> CodeGen () () CPtrdiff
+type TrapPatcherEax = CPtrdiff -> CPtrdiff -> CodeGen () () CPtrdiff
+
+data TrapCause
+  = StaticMethod TrapPatcher -- for static calls
+  | VirtualCall Bool MethodInfo (IO NativeWord) -- for invoke{interface,virtual}
+  | InstanceOf TrapPatcherEax
+  | NewObject TrapPatcher
+  | StaticField StaticFieldInfo
+  | ObjectField TrapPatcher
 
 data StaticFieldInfo = StaticFieldInfo {
   sfiClassName :: B.ByteString,
@@ -46,8 +84,8 @@ data StaticFieldInfo = StaticFieldInfo {
 
 
 -- B.ByteString = name of method
--- Word32 = entrypoint of method
-type MethodMap = M.Map MethodInfo Word32
+-- NativeWord = entrypoint of method
+type MethodMap = M.Map MethodInfo NativeWord
 
 data MethodInfo = MethodInfo {
   methName :: B.ByteString,
@@ -55,14 +93,6 @@ data MethodInfo = MethodInfo {
   methSignature :: MethodSignature
   } deriving (Eq, Ord)
 
--- TODO(bernhard): not really efficient. also, outsource that to hs-java
---                 deriving should be enough?
-instance Ord MethodSignature where
-  compare (MethodSignature args_a ret_a) (MethodSignature args_b ret_b)
-    | cmp_args /= EQ = cmp_args
-    | otherwise = show ret_a `compare` show ret_b
-    where cmp_args = show args_a `compare` show args_b
-
 instance Show MethodInfo where
   show (MethodInfo method c sig) =
     toString c ++ "." ++ toString method ++ "." ++ show sig
@@ -74,11 +104,11 @@ type ClassMap = M.Map B.ByteString ClassInfo
 
 data ClassInfo = ClassInfo {
   ciName :: B.ByteString,
-  ciFile :: Class Resolved,
+  ciFile :: Class Direct,
   ciStaticMap  :: FieldMap,
   ciFieldMap :: FieldMap,
   ciMethodMap :: FieldMap,
-  ciMethodBase :: Word32,
+  ciMethodBase :: NativeWord,
   ciInitDone :: Bool }
 
 
@@ -88,34 +118,28 @@ type FieldMap = M.Map B.ByteString Int32
 
 -- java strings are allocated only once, therefore we
 -- use a hashmap to store the address for a String
-type StringMap = M.Map B.ByteString Word32
+type StringMap = M.Map B.ByteString NativeWord
 
 
 -- map "methodtable addr" to "classname"
 -- we need that to identify the actual type
 -- on the invokevirtual insn
-type VirtualMap = M.Map Word32 B.ByteString
+type VirtualMap = M.Map NativeWord B.ByteString
 
 
 -- store each parsed Interface upon first loading
-type InterfaceMap = M.Map B.ByteString (Class Resolved)
+type InterfaceMap = M.Map B.ByteString (Class Direct)
 
 -- store offset for each <Interface><Method><Signature> pair
-type InterfaceMethodMap = M.Map B.ByteString Word32
+type InterfaceMethodMap = M.Map B.ByteString NativeWord
 
 
+{-
 toString :: B.ByteString -> String
 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
+-}
 
 
--- those functions are for the "global map hax"
--- TODO(bernhard): other solution please
-foreign import ccall "set_mate_context"
-  set_mate_context :: Ptr () -> IO ()
-
-foreign import ccall "get_mate_context"
-  get_mate_context :: IO (Ptr ())
-
 data MateCtx = MateCtx {
   ctxMethodMap :: MethodMap,
   ctxTrapMap :: TrapMap,
@@ -128,87 +152,51 @@ data MateCtx = MateCtx {
 emptyMateCtx :: MateCtx
 emptyMateCtx = MateCtx M.empty M.empty M.empty M.empty M.empty M.empty M.empty
 
-ctx2ptr :: MateCtx -> IO (Ptr ())
-ctx2ptr ctx = do
-  ptr <- newStablePtr ctx
-  return $ castStablePtrToPtr ptr
-
-ptr2ctx :: Ptr () -> IO MateCtx
-ptr2ctx ptr = deRefStablePtr (castPtrToStablePtr ptr :: StablePtr MateCtx)
+mateCtx :: IORef MateCtx
+{-# NOINLINE mateCtx #-}
+mateCtx = unsafePerformIO $ newIORef emptyMateCtx
 
+setMap :: (MateCtx -> MateCtx) -> IO ()
+setMap recordupdate = recordupdate <$> readIORef mateCtx >>= writeIORef mateCtx
 
 setMethodMap :: MethodMap -> IO ()
-setMethodMap m = do
-  ctx <- get_mate_context >>= ptr2ctx
-  ctx2ptr ctx { ctxMethodMap = m } >>= set_mate_context
+setMethodMap m = setMap (\x -> x {ctxMethodMap = m})
 
 getMethodMap :: IO MethodMap
-getMethodMap = do
-  ctx <- get_mate_context >>= ptr2ctx
-  return $ ctxMethodMap ctx
-
+getMethodMap = ctxMethodMap <$> readIORef mateCtx
 
 setTrapMap :: TrapMap -> IO ()
-setTrapMap m = do
-  ctx <- get_mate_context >>= ptr2ctx
-  ctx2ptr ctx { ctxTrapMap = m } >>= set_mate_context
+setTrapMap m = setMap (\x -> x {ctxTrapMap = m})
 
 getTrapMap :: IO TrapMap
-getTrapMap = do
-  ctx <- get_mate_context >>= ptr2ctx
-  return $ ctxTrapMap ctx
-
+getTrapMap = ctxTrapMap <$> readIORef mateCtx
 
 setClassMap :: ClassMap -> IO ()
-setClassMap m = do
-  ctx <- get_mate_context >>= ptr2ctx
-  ctx2ptr ctx { ctxClassMap = m } >>= set_mate_context
+setClassMap m = setMap (\x -> x {ctxClassMap = m})
 
 getClassMap :: IO ClassMap
-getClassMap = do
-  ctx <- get_mate_context >>= ptr2ctx
-  return $ ctxClassMap ctx
-
+getClassMap = ctxClassMap <$> readIORef mateCtx
 
 setVirtualMap :: VirtualMap -> IO ()
-setVirtualMap m = do
-  ctx <- get_mate_context >>= ptr2ctx
-  ctx2ptr ctx { ctxVirtualMap = m } >>= set_mate_context
+setVirtualMap m = setMap (\x -> x {ctxVirtualMap = m})
 
 getVirtualMap :: IO VirtualMap
-getVirtualMap = do
-  ctx <- get_mate_context >>= ptr2ctx
-  return $ ctxVirtualMap ctx
-
+getVirtualMap = ctxVirtualMap <$> readIORef mateCtx
 
 setStringMap :: StringMap -> IO ()
-setStringMap m = do
-  ctx <- get_mate_context >>= ptr2ctx
-  ctx2ptr ctx { ctxStringMap = m } >>= set_mate_context
+setStringMap m = setMap (\x -> x {ctxStringMap = m})
 
 getStringMap :: IO StringMap
-getStringMap = do
-  ctx <- get_mate_context >>= ptr2ctx
-  return $ ctxStringMap ctx
-
+getStringMap = ctxStringMap <$> readIORef mateCtx
 
 setInterfaceMap :: InterfaceMap -> IO ()
-setInterfaceMap m = do
-  ctx <- get_mate_context >>= ptr2ctx
-  ctx2ptr ctx { ctxInterfaceMap = m } >>= set_mate_context
+setInterfaceMap m = setMap (\x -> x {ctxInterfaceMap = m})
 
 getInterfaceMap :: IO InterfaceMap
-getInterfaceMap = do
-  ctx <- get_mate_context >>= ptr2ctx
-  return $ ctxInterfaceMap ctx
-
+getInterfaceMap = ctxInterfaceMap <$> readIORef mateCtx
 
 setInterfaceMethodMap :: InterfaceMethodMap -> IO ()
-setInterfaceMethodMap m = do
-  ctx <- get_mate_context >>= ptr2ctx
-  ctx2ptr ctx { ctxInterfaceMethodMap = m } >>= set_mate_context
+setInterfaceMethodMap m = setMap (\x -> x {ctxInterfaceMethodMap = m})
 
 getInterfaceMethodMap :: IO InterfaceMethodMap
-getInterfaceMethodMap = do
-  ctx <- get_mate_context >>= ptr2ctx
-  return $ ctxInterfaceMethodMap ctx
+getInterfaceMethodMap = ctxInterfaceMethodMap <$> readIORef mateCtx