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
70 type TrapPatcherEax = CPtrdiff -> CPtrdiff -> CodeGen () () CPtrdiff
71 type TrapPatcherEsp = TrapPatcherEax
74 = StaticMethod TrapPatcher -- for static calls
75 | VirtualCall Bool MethodInfo (IO NativeWord) -- for invoke{interface,virtual}
76 | InstanceOf TrapPatcherEax
77 | ThrowException TrapPatcherEsp
78 | NewObject TrapPatcher
79 | StaticField StaticFieldInfo
80 | ObjectField TrapPatcher
82 data StaticFieldInfo = StaticFieldInfo {
83 sfiClassName :: B.ByteString,
84 sfiFieldName :: B.ByteString } deriving Show
88 -- B.ByteString = name of method
89 -- NativeWord = entrypoint of method
90 type MethodMap = M.Map MethodInfo NativeWord
92 data MethodInfo = MethodInfo {
93 methName :: B.ByteString,
94 methClassName :: B.ByteString,
95 methSignature :: MethodSignature
98 instance Show MethodInfo where
99 show (MethodInfo method c sig) =
100 toString c ++ "." ++ toString method ++ "." ++ show sig
104 -- store information of loaded classes
105 type ClassMap = M.Map B.ByteString ClassInfo
107 data ClassInfo = ClassInfo {
108 ciName :: B.ByteString,
109 ciFile :: Class Direct,
110 ciStaticMap :: FieldMap,
111 ciFieldMap :: FieldMap,
112 ciMethodMap :: FieldMap,
113 ciMethodBase :: NativeWord,
117 -- store field offsets in a map
118 type FieldMap = M.Map B.ByteString Int32
121 -- java strings are allocated only once, therefore we
122 -- use a hashmap to store the address for a String
123 type StringMap = M.Map B.ByteString NativeWord
126 -- map "methodtable addr" to "classname"
127 -- we need that to identify the actual type
128 -- on the invokevirtual insn
129 type VirtualMap = M.Map NativeWord B.ByteString
132 -- store each parsed Interface upon first loading
133 type InterfaceMap = M.Map B.ByteString (Class Direct)
135 -- store offset for each <Interface><Method><Signature> pair
136 type InterfaceMethodMap = M.Map B.ByteString NativeWord
140 toString :: B.ByteString -> String
141 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
145 data MateCtx = MateCtx {
146 ctxMethodMap :: MethodMap,
147 ctxTrapMap :: TrapMap,
148 ctxClassMap :: ClassMap,
149 ctxVirtualMap :: VirtualMap,
150 ctxStringMap :: StringMap,
151 ctxInterfaceMap :: InterfaceMap,
152 ctxInterfaceMethodMap :: InterfaceMethodMap }
154 emptyMateCtx :: MateCtx
155 emptyMateCtx = MateCtx M.empty M.empty M.empty M.empty M.empty M.empty M.empty
157 mateCtx :: IORef MateCtx
158 {-# NOINLINE mateCtx #-}
159 mateCtx = unsafePerformIO $ newIORef emptyMateCtx
161 setMap :: (MateCtx -> MateCtx) -> IO ()
162 setMap recordupdate = recordupdate <$> readIORef mateCtx >>= writeIORef mateCtx
164 setMethodMap :: MethodMap -> IO ()
165 setMethodMap m = setMap (\x -> x {ctxMethodMap = m})
167 getMethodMap :: IO MethodMap
168 getMethodMap = ctxMethodMap <$> readIORef mateCtx
170 setTrapMap :: TrapMap -> IO ()
171 setTrapMap m = setMap (\x -> x {ctxTrapMap = m})
173 getTrapMap :: IO TrapMap
174 getTrapMap = ctxTrapMap <$> readIORef mateCtx
176 setClassMap :: ClassMap -> IO ()
177 setClassMap m = setMap (\x -> x {ctxClassMap = m})
179 getClassMap :: IO ClassMap
180 getClassMap = ctxClassMap <$> readIORef mateCtx
182 setVirtualMap :: VirtualMap -> IO ()
183 setVirtualMap m = setMap (\x -> x {ctxVirtualMap = m})
185 getVirtualMap :: IO VirtualMap
186 getVirtualMap = ctxVirtualMap <$> readIORef mateCtx
188 setStringMap :: StringMap -> IO ()
189 setStringMap m = setMap (\x -> x {ctxStringMap = m})
191 getStringMap :: IO StringMap
192 getStringMap = ctxStringMap <$> readIORef mateCtx
194 setInterfaceMap :: InterfaceMap -> IO ()
195 setInterfaceMap m = setMap (\x -> x {ctxInterfaceMap = m})
197 getInterfaceMap :: IO InterfaceMap
198 getInterfaceMap = ctxInterfaceMap <$> readIORef mateCtx
200 setInterfaceMethodMap :: InterfaceMethodMap -> IO ()
201 setInterfaceMethodMap m = setMap (\x -> x {ctxInterfaceMethodMap = m})
203 getInterfaceMethodMap :: IO InterfaceMethodMap
204 getInterfaceMethodMap = ctxInterfaceMethodMap <$> readIORef mateCtx