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
73 = StaticMethod TrapPatcher -- for static calls
74 | VirtualCall Bool MethodInfo (IO NativeWord) -- for invoke{interface,virtual}
75 | InstanceOf TrapPatcherEax
76 | NewObject TrapPatcher
77 | StaticField StaticFieldInfo
78 | ObjectField TrapPatcher
80 data StaticFieldInfo = StaticFieldInfo {
81 sfiClassName :: B.ByteString,
82 sfiFieldName :: B.ByteString } deriving Show
86 -- B.ByteString = name of method
87 -- NativeWord = entrypoint of method
88 type MethodMap = M.Map MethodInfo NativeWord
90 data MethodInfo = MethodInfo {
91 methName :: B.ByteString,
92 methClassName :: B.ByteString,
93 methSignature :: MethodSignature
96 instance Show MethodInfo where
97 show (MethodInfo method c sig) =
98 toString c ++ "." ++ toString method ++ "." ++ show sig
102 -- store information of loaded classes
103 type ClassMap = M.Map B.ByteString ClassInfo
105 data ClassInfo = ClassInfo {
106 ciName :: B.ByteString,
107 ciFile :: Class Direct,
108 ciStaticMap :: FieldMap,
109 ciFieldMap :: FieldMap,
110 ciMethodMap :: FieldMap,
111 ciMethodBase :: NativeWord,
115 -- store field offsets in a map
116 type FieldMap = M.Map B.ByteString Int32
119 -- java strings are allocated only once, therefore we
120 -- use a hashmap to store the address for a String
121 type StringMap = M.Map B.ByteString NativeWord
124 -- map "methodtable addr" to "classname"
125 -- we need that to identify the actual type
126 -- on the invokevirtual insn
127 type VirtualMap = M.Map NativeWord B.ByteString
130 -- store each parsed Interface upon first loading
131 type InterfaceMap = M.Map B.ByteString (Class Direct)
133 -- store offset for each <Interface><Method><Signature> pair
134 type InterfaceMethodMap = M.Map B.ByteString NativeWord
138 toString :: B.ByteString -> String
139 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
143 data MateCtx = MateCtx {
144 ctxMethodMap :: MethodMap,
145 ctxTrapMap :: TrapMap,
146 ctxClassMap :: ClassMap,
147 ctxVirtualMap :: VirtualMap,
148 ctxStringMap :: StringMap,
149 ctxInterfaceMap :: InterfaceMap,
150 ctxInterfaceMethodMap :: InterfaceMethodMap }
152 emptyMateCtx :: MateCtx
153 emptyMateCtx = MateCtx M.empty M.empty M.empty M.empty M.empty M.empty M.empty
155 mateCtx :: IORef MateCtx
156 {-# NOINLINE mateCtx #-}
157 mateCtx = unsafePerformIO $ newIORef emptyMateCtx
159 setMap :: (MateCtx -> MateCtx) -> IO ()
160 setMap recordupdate = recordupdate <$> readIORef mateCtx >>= writeIORef mateCtx
162 setMethodMap :: MethodMap -> IO ()
163 setMethodMap m = setMap (\x -> x {ctxMethodMap = m})
165 getMethodMap :: IO MethodMap
166 getMethodMap = ctxMethodMap <$> readIORef mateCtx
168 setTrapMap :: TrapMap -> IO ()
169 setTrapMap m = setMap (\x -> x {ctxTrapMap = m})
171 getTrapMap :: IO TrapMap
172 getTrapMap = ctxTrapMap <$> readIORef mateCtx
174 setClassMap :: ClassMap -> IO ()
175 setClassMap m = setMap (\x -> x {ctxClassMap = m})
177 getClassMap :: IO ClassMap
178 getClassMap = ctxClassMap <$> readIORef mateCtx
180 setVirtualMap :: VirtualMap -> IO ()
181 setVirtualMap m = setMap (\x -> x {ctxVirtualMap = m})
183 getVirtualMap :: IO VirtualMap
184 getVirtualMap = ctxVirtualMap <$> readIORef mateCtx
186 setStringMap :: StringMap -> IO ()
187 setStringMap m = setMap (\x -> x {ctxStringMap = m})
189 getStringMap :: IO StringMap
190 getStringMap = ctxStringMap <$> readIORef mateCtx
192 setInterfaceMap :: InterfaceMap -> IO ()
193 setInterfaceMap m = setMap (\x -> x {ctxInterfaceMap = m})
195 getInterfaceMap :: IO InterfaceMap
196 getInterfaceMap = ctxInterfaceMap <$> readIORef mateCtx
198 setInterfaceMethodMap :: InterfaceMethodMap -> IO ()
199 setInterfaceMethodMap m = setMap (\x -> x {ctxInterfaceMethodMap = m})
201 getInterfaceMethodMap :: IO InterfaceMethodMap
202 getInterfaceMethodMap = ctxInterfaceMethodMap <$> readIORef mateCtx