1 {-# LANGUAGE OverloadedStrings #-}
8 , TrapMap, MethodMap, ClassMap, FieldMap
9 , StringMap, VirtualMap, InterfaceMap
15 , setTrapMap, getTrapMap
16 , setMethodMap, getMethodMap
17 , setClassMap, getClassMap
18 , setStringMap, getStringMap
19 , setVirtualMap, getVirtualMap
20 , setInterfaceMap, getInterfaceMap
21 , setInterfaceMethodMap, getInterfaceMethodMap
26 import qualified Data.Map as M
27 import qualified Data.ByteString.Lazy as B
30 import System.IO.Unsafe
33 import Foreign.C.Types
38 import Mate.NativeSizes
42 -- Represents a CFG node
43 data BasicBlock = BasicBlock {
44 code :: [Instruction],
47 -- describes (leaving) edges of a CFG node
52 | TwoTarget BlockID BlockID
55 type MapBB = M.Map BlockID BasicBlock
57 data RawMethod = RawMethod {
61 rawArgCount :: NativeWord,
62 rawCodeLength :: NativeWord }
65 -- NativeWord = point of method call in generated code
66 -- MethodInfo = relevant information about callee
67 type TrapMap = M.Map NativeWord TrapCause
69 type TrapPatcher = CPtrdiff -> CodeGen () () CPtrdiff
72 = StaticMethod TrapPatcher -- for static calls
73 | VirtualCall Bool MethodInfo (IO NativeWord) -- for invoke{interface,virtual}
74 | InstanceOf B.ByteString -- class name
75 | NewObject TrapPatcher -- class name
76 | StaticField StaticFieldInfo
77 | ObjectField TrapPatcher
79 data StaticFieldInfo = StaticFieldInfo {
80 sfiClassName :: B.ByteString,
81 sfiFieldName :: B.ByteString } deriving Show
85 -- B.ByteString = name of method
86 -- NativeWord = entrypoint of method
87 type MethodMap = M.Map MethodInfo NativeWord
89 data MethodInfo = MethodInfo {
90 methName :: B.ByteString,
91 methClassName :: B.ByteString,
92 methSignature :: MethodSignature
95 instance Show MethodInfo where
96 show (MethodInfo method c sig) =
97 toString c ++ "." ++ toString method ++ "." ++ show sig
101 -- store information of loaded classes
102 type ClassMap = M.Map B.ByteString ClassInfo
104 data ClassInfo = ClassInfo {
105 ciName :: B.ByteString,
106 ciFile :: Class Direct,
107 ciStaticMap :: FieldMap,
108 ciFieldMap :: FieldMap,
109 ciMethodMap :: FieldMap,
110 ciMethodBase :: NativeWord,
114 -- store field offsets in a map
115 type FieldMap = M.Map B.ByteString Int32
118 -- java strings are allocated only once, therefore we
119 -- use a hashmap to store the address for a String
120 type StringMap = M.Map B.ByteString NativeWord
123 -- map "methodtable addr" to "classname"
124 -- we need that to identify the actual type
125 -- on the invokevirtual insn
126 type VirtualMap = M.Map NativeWord B.ByteString
129 -- store each parsed Interface upon first loading
130 type InterfaceMap = M.Map B.ByteString (Class Direct)
132 -- store offset for each <Interface><Method><Signature> pair
133 type InterfaceMethodMap = M.Map B.ByteString NativeWord
137 toString :: B.ByteString -> String
138 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
142 data MateCtx = MateCtx {
143 ctxMethodMap :: MethodMap,
144 ctxTrapMap :: TrapMap,
145 ctxClassMap :: ClassMap,
146 ctxVirtualMap :: VirtualMap,
147 ctxStringMap :: StringMap,
148 ctxInterfaceMap :: InterfaceMap,
149 ctxInterfaceMethodMap :: InterfaceMethodMap }
151 emptyMateCtx :: MateCtx
152 emptyMateCtx = MateCtx M.empty M.empty M.empty M.empty M.empty M.empty M.empty
154 mateCtx :: IORef MateCtx
155 {-# NOINLINE mateCtx #-}
156 mateCtx = unsafePerformIO $ newIORef emptyMateCtx
158 setMap :: (MateCtx -> MateCtx) -> IO ()
159 setMap recordupdate = recordupdate <$> readIORef mateCtx >>= writeIORef mateCtx
161 setMethodMap :: MethodMap -> IO ()
162 setMethodMap m = setMap (\x -> x {ctxMethodMap = m})
164 getMethodMap :: IO MethodMap
165 getMethodMap = ctxMethodMap <$> readIORef mateCtx
167 setTrapMap :: TrapMap -> IO ()
168 setTrapMap m = setMap (\x -> x {ctxTrapMap = m})
170 getTrapMap :: IO TrapMap
171 getTrapMap = ctxTrapMap <$> readIORef mateCtx
173 setClassMap :: ClassMap -> IO ()
174 setClassMap m = setMap (\x -> x {ctxClassMap = m})
176 getClassMap :: IO ClassMap
177 getClassMap = ctxClassMap <$> readIORef mateCtx
179 setVirtualMap :: VirtualMap -> IO ()
180 setVirtualMap m = setMap (\x -> x {ctxVirtualMap = m})
182 getVirtualMap :: IO VirtualMap
183 getVirtualMap = ctxVirtualMap <$> readIORef mateCtx
185 setStringMap :: StringMap -> IO ()
186 setStringMap m = setMap (\x -> x {ctxStringMap = m})
188 getStringMap :: IO StringMap
189 getStringMap = ctxStringMap <$> readIORef mateCtx
191 setInterfaceMap :: InterfaceMap -> IO ()
192 setInterfaceMap m = setMap (\x -> x {ctxInterfaceMap = m})
194 getInterfaceMap :: IO InterfaceMap
195 getInterfaceMap = ctxInterfaceMap <$> readIORef mateCtx
197 setInterfaceMethodMap :: InterfaceMethodMap -> IO ()
198 setInterfaceMethodMap m = setMap (\x -> x {ctxInterfaceMethodMap = m})
200 getInterfaceMethodMap :: IO InterfaceMethodMap
201 getInterfaceMethodMap = ctxInterfaceMethodMap <$> readIORef mateCtx