1 {-# LANGUAGE OverloadedStrings #-}
9 , TrapMap, MethodMap, ClassMap, FieldMap
10 , StringMap, VirtualMap, InterfaceMap
16 , setTrapMap, getTrapMap
17 , setMethodMap, getMethodMap
18 , setClassMap, getClassMap
19 , setStringMap, getStringMap
20 , setVirtualMap, getVirtualMap
21 , setInterfaceMap, getInterfaceMap
22 , setInterfaceMethodMap, getInterfaceMethodMap
28 import qualified Data.Map as M
29 import qualified Data.ByteString.Lazy as B
32 import System.IO.Unsafe
35 import Foreign.C.Types
40 import Mate.NativeSizes
44 -- Represents a CFG node
45 data BasicBlock = BasicBlock {
46 code :: [Instruction],
50 -- describes (leaving) edges of a CFG node
55 | TwoTarget BlockID BlockID
58 type MapBB = M.Map BlockID BasicBlock
59 type ExceptionMap = M.Map (Word16, Word16) [(B.ByteString, Word16)]
61 data RawMethod = RawMethod {
63 rawExcpMap :: ExceptionMap,
66 rawArgCount :: NativeWord,
67 rawCodeLength :: NativeWord }
70 -- NativeWord = point of method call in generated code
71 -- MethodInfo = relevant information about callee
72 type TrapMap = M.Map NativeWord TrapCause
74 type TrapPatcher = CPtrdiff -> CodeGen () () CPtrdiff
75 type TrapPatcherEax = CPtrdiff -> CPtrdiff -> CodeGen () () CPtrdiff
76 type TrapPatcherEsp = TrapPatcherEax
79 = StaticMethod TrapPatcher -- for static calls
80 | VirtualCall Bool MethodInfo (IO NativeWord) -- for invoke{interface,virtual}
81 | InstanceOf TrapPatcherEax
82 | ThrowException TrapPatcherEsp
83 | NewObject TrapPatcher
84 | StaticField StaticFieldInfo
85 | ObjectField TrapPatcher
87 data StaticFieldInfo = StaticFieldInfo {
88 sfiClassName :: B.ByteString,
89 sfiFieldName :: B.ByteString } deriving Show
93 -- B.ByteString = name of method
94 -- NativeWord = entrypoint of method
95 type MethodMap = M.Map MethodInfo NativeWord
97 data MethodInfo = MethodInfo {
98 methName :: B.ByteString,
99 methClassName :: B.ByteString,
100 methSignature :: MethodSignature
103 instance Show MethodInfo where
104 show (MethodInfo method c sig) =
105 toString c ++ "." ++ toString method ++ "." ++ show sig
109 -- store information of loaded classes
110 type ClassMap = M.Map B.ByteString ClassInfo
112 data ClassInfo = ClassInfo {
113 ciName :: B.ByteString,
114 ciFile :: Class Direct,
115 ciStaticMap :: FieldMap,
116 ciFieldMap :: FieldMap,
117 ciMethodMap :: FieldMap,
118 ciMethodBase :: NativeWord,
122 -- store field offsets in a map
123 type FieldMap = M.Map B.ByteString Int32
126 -- java strings are allocated only once, therefore we
127 -- use a hashmap to store the address for a String
128 type StringMap = M.Map B.ByteString NativeWord
131 -- map "methodtable addr" to "classname"
132 -- we need that to identify the actual type
133 -- on the invokevirtual insn
134 type VirtualMap = M.Map NativeWord B.ByteString
137 -- store each parsed Interface upon first loading
138 type InterfaceMap = M.Map B.ByteString (Class Direct)
140 -- store offset for each <Interface><Method><Signature> pair
141 type InterfaceMethodMap = M.Map B.ByteString NativeWord
145 toString :: B.ByteString -> String
146 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
150 data MateCtx = MateCtx {
151 ctxMethodMap :: MethodMap,
152 ctxTrapMap :: TrapMap,
153 ctxClassMap :: ClassMap,
154 ctxVirtualMap :: VirtualMap,
155 ctxStringMap :: StringMap,
156 ctxInterfaceMap :: InterfaceMap,
157 ctxInterfaceMethodMap :: InterfaceMethodMap }
159 emptyMateCtx :: MateCtx
160 emptyMateCtx = MateCtx M.empty M.empty M.empty M.empty M.empty M.empty M.empty
162 mateCtx :: IORef MateCtx
163 {-# NOINLINE mateCtx #-}
164 mateCtx = unsafePerformIO $ newIORef emptyMateCtx
166 setMap :: (MateCtx -> MateCtx) -> IO ()
167 setMap recordupdate = recordupdate <$> readIORef mateCtx >>= writeIORef mateCtx
169 setMethodMap :: MethodMap -> IO ()
170 setMethodMap m = setMap (\x -> x {ctxMethodMap = m})
172 getMethodMap :: IO MethodMap
173 getMethodMap = ctxMethodMap <$> readIORef mateCtx
175 setTrapMap :: TrapMap -> IO ()
176 setTrapMap m = setMap (\x -> x {ctxTrapMap = m})
178 getTrapMap :: IO TrapMap
179 getTrapMap = ctxTrapMap <$> readIORef mateCtx
181 setClassMap :: ClassMap -> IO ()
182 setClassMap m = setMap (\x -> x {ctxClassMap = m})
184 getClassMap :: IO ClassMap
185 getClassMap = ctxClassMap <$> readIORef mateCtx
187 setVirtualMap :: VirtualMap -> IO ()
188 setVirtualMap m = setMap (\x -> x {ctxVirtualMap = m})
190 getVirtualMap :: IO VirtualMap
191 getVirtualMap = ctxVirtualMap <$> readIORef mateCtx
193 setStringMap :: StringMap -> IO ()
194 setStringMap m = setMap (\x -> x {ctxStringMap = m})
196 getStringMap :: IO StringMap
197 getStringMap = ctxStringMap <$> readIORef mateCtx
199 setInterfaceMap :: InterfaceMap -> IO ()
200 setInterfaceMap m = setMap (\x -> x {ctxInterfaceMap = m})
202 getInterfaceMap :: IO InterfaceMap
203 getInterfaceMap = ctxInterfaceMap <$> readIORef mateCtx
205 setInterfaceMethodMap :: InterfaceMethodMap -> IO ()
206 setInterfaceMethodMap m = setMap (\x -> x {ctxInterfaceMethodMap = m})
208 getInterfaceMethodMap :: IO InterfaceMethodMap
209 getInterfaceMethodMap = ctxInterfaceMethodMap <$> readIORef mateCtx