1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ForeignFunctionInterface #-}
3 module Mate.Types where
8 import qualified Data.Map as M
9 import qualified Data.ByteString.Lazy as B
10 import Codec.Binary.UTF8.String hiding (encode,decode)
13 import Foreign.StablePtr
20 -- Represents a CFG node
21 data BasicBlock = BasicBlock {
22 code :: [Instruction],
25 -- describes (leaving) edges of a CFG node
26 data BBEnd = Return | FallThrough BlockID | OneTarget BlockID | TwoTarget BlockID BlockID deriving Show
28 type MapBB = M.Map BlockID BasicBlock
32 -- Word32 = point of method call in generated code
33 -- MethodInfo = relevant information about callee
34 type TrapMap = M.Map Word32 TrapInfo
37 MI MethodInfo | -- for static calls
38 VI MethodInfo | -- for virtual calls
41 data StaticFieldInfo = StaticFieldInfo {
42 sfiClassName :: B.ByteString,
43 sfiFieldName :: B.ByteString }
47 -- B.ByteString = name of method
48 -- Word32 = entrypoint of method
49 type MethodMap = M.Map MethodInfo Word32
51 data MethodInfo = MethodInfo {
52 methName :: B.ByteString,
53 methClassName :: B.ByteString,
54 methSignature :: MethodSignature
57 -- TODO(bernhard): not really efficient. also, outsource that to hs-java
58 -- deriving should be enough?
59 instance Ord MethodSignature where
60 compare (MethodSignature args_a ret_a) (MethodSignature args_b ret_b)
61 | cmp_args /= EQ = cmp_args
62 | otherwise = (show ret_a) `compare` (show ret_b)
64 cmp_args = (show args_a) `compare` (show args_b)
66 instance Show MethodInfo where
67 show (MethodInfo method c sig) =
68 (toString c) ++ "." ++ (toString method) ++ "." ++ (show sig)
72 -- store information of loaded classes
73 type ClassMap = M.Map B.ByteString ClassInfo
75 data ClassInfo = ClassInfo {
76 ciName :: B.ByteString,
77 ciFile :: Class Resolved,
78 ciStaticMap :: FieldMap,
79 ciFieldMap :: FieldMap,
80 ciMethodMap :: FieldMap,
81 ciMethodBase :: Word32,
85 -- store field offsets in a map
86 type FieldMap = M.Map B.ByteString Int32
89 -- java strings are allocated only once, therefore we
90 -- use a hashmap to store the address for a String
91 type StringsMap = M.Map B.ByteString Word32
94 -- map "methodtable addr" to "classname"
95 -- we need that to identify the actual type
96 -- on the invokevirtual insn
97 type VirtualMap = M.Map Word32 B.ByteString
100 toString :: B.ByteString -> String
101 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
104 -- those functions are for the "global map hax"
105 -- TODO(bernhard): other solution please
106 foreign import ccall "get_trapmap"
107 get_trapmap :: IO (Ptr ())
109 foreign import ccall "set_trapmap"
110 set_trapmap :: Ptr () -> IO ()
112 foreign import ccall "get_methodmap"
113 get_methodmap :: IO (Ptr ())
115 foreign import ccall "set_methodmap"
116 set_methodmap :: Ptr () -> IO ()
118 foreign import ccall "get_classmap"
119 get_classmap :: IO (Ptr ())
121 foreign import ccall "set_classmap"
122 set_classmap :: Ptr () -> IO ()
124 foreign import ccall "get_virtualmap"
125 get_virtualmap :: IO (Ptr ())
127 foreign import ccall "set_virtualmap"
128 set_virtualmap :: Ptr () -> IO ()
130 foreign import ccall "get_stringsmap"
131 get_stringsmap :: IO (Ptr ())
133 foreign import ccall "set_stringsmap"
134 set_stringsmap :: Ptr () -> IO ()
136 -- TODO(bernhard): make some typeclass magic 'n stuff
137 -- or remove that sh**
138 methodmap2ptr :: MethodMap -> IO (Ptr ())
139 methodmap2ptr methodmap = do
140 ptr_methodmap <- newStablePtr methodmap
141 return $ castStablePtrToPtr ptr_methodmap
143 ptr2methodmap :: Ptr () -> IO MethodMap
144 ptr2methodmap methodmap = deRefStablePtr $ ((castPtrToStablePtr methodmap) :: StablePtr MethodMap)
146 trapmap2ptr :: TrapMap -> IO (Ptr ())
147 trapmap2ptr trapmap = do
148 ptr_trapmap <- newStablePtr trapmap
149 return $ castStablePtrToPtr ptr_trapmap
151 ptr2trapmap :: Ptr () -> IO TrapMap
152 ptr2trapmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr trapmap)
154 classmap2ptr :: ClassMap -> IO (Ptr ())
155 classmap2ptr cmap = do
156 ptr_cmap <- newStablePtr cmap
157 return $ castStablePtrToPtr ptr_cmap
159 ptr2classmap :: Ptr () -> IO ClassMap
160 ptr2classmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)
162 virtualmap2ptr :: VirtualMap -> IO (Ptr ())
163 virtualmap2ptr cmap = do
164 ptr_cmap <- newStablePtr cmap
165 return $ castStablePtrToPtr ptr_cmap
167 ptr2virtualmap :: Ptr () -> IO VirtualMap
168 ptr2virtualmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)
171 stringsmap2ptr :: StringsMap -> IO (Ptr ())
172 stringsmap2ptr cmap = do
173 ptr_cmap <- newStablePtr cmap
174 return $ castStablePtrToPtr ptr_cmap
176 ptr2stringsmap :: Ptr () -> IO StringsMap
177 ptr2stringsmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)