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
31 -- Word32 = point of method call in generated code
32 -- MethodInfo = relevant information about callee
33 type TMap = M.Map Word32 TrapInfo
37 VI MethodInfo | -- for virtual calls
40 data StaticFieldInfo = StaticFieldInfo {
41 sfiClassName :: B.ByteString,
42 sfiFieldName :: B.ByteString }
44 -- B.ByteString = name of method
45 -- Word32 = entrypoint of method
46 type MMap = M.Map MethodInfo Word32
48 type ClassMap = M.Map B.ByteString ClassInfo
50 type FieldMap = M.Map B.ByteString Int32
52 -- map "methodtable addr" to "classname"
53 -- we need that to identify the actual type
54 -- on the invokevirtual insn
55 type VirtualMap = M.Map Word32 B.ByteString
57 data ClassInfo = ClassInfo {
58 clName :: B.ByteString,
59 clFile :: Class Resolved,
60 clStaticMap :: FieldMap,
61 clFieldMap :: FieldMap,
62 clMethodMap :: FieldMap,
63 clMethodBase :: Word32,
66 data MethodInfo = MethodInfo {
67 methName :: B.ByteString,
68 cName :: B.ByteString,
69 mSignature :: MethodSignature}
71 instance Eq MethodInfo where
72 (MethodInfo m_a c_a s_a) == (MethodInfo m_b c_b s_b) =
73 (m_a == m_b) && (c_a == c_b) && (s_a == s_b)
75 -- TODO(bernhard): not really efficient. also, outsource that to hs-java
76 instance Ord MethodSignature where
77 compare (MethodSignature args_a ret_a) (MethodSignature args_b ret_b)
78 | cmp_args /= EQ = cmp_args
79 | otherwise = (show ret_a) `compare` (show ret_b)
81 cmp_args = (show args_a) `compare` (show args_b)
83 instance Ord MethodInfo where
84 compare (MethodInfo m_a c_a s_a) (MethodInfo m_b c_b s_b)
87 | otherwise = s_a `compare` s_b
89 cmp_m = m_a `compare` m_b
90 cmp_c = c_a `compare` c_b
92 instance Show MethodInfo where
93 show (MethodInfo method c sig) =
94 (toString c) ++ "." ++ (toString method) ++ "." ++ (show sig)
97 toString :: B.ByteString -> String
98 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
102 foreign import ccall "get_trapmap"
103 get_trapmap :: IO (Ptr ())
105 foreign import ccall "set_trapmap"
106 set_trapmap :: Ptr () -> IO ()
108 foreign import ccall "get_methodmap"
109 get_methodmap :: IO (Ptr ())
111 foreign import ccall "set_methodmap"
112 set_methodmap :: Ptr () -> IO ()
114 foreign import ccall "get_classmap"
115 get_classmap :: IO (Ptr ())
117 foreign import ccall "set_classmap"
118 set_classmap :: Ptr () -> IO ()
120 foreign import ccall "get_virtualmap"
121 get_virtualmap :: IO (Ptr ())
123 foreign import ccall "set_virtualmap"
124 set_virtualmap :: Ptr () -> IO ()
126 -- TODO(bernhard): make some typeclass magic 'n stuff
127 mmap2ptr :: MMap -> IO (Ptr ())
129 ptr_mmap <- newStablePtr mmap
130 return $ castStablePtrToPtr ptr_mmap
132 ptr2mmap :: Ptr () -> IO MMap
133 ptr2mmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr MMap)
135 tmap2ptr :: TMap -> IO (Ptr ())
137 ptr_tmap <- newStablePtr tmap
138 return $ castStablePtrToPtr ptr_tmap
140 ptr2tmap :: Ptr () -> IO TMap
141 ptr2tmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr tmap)
143 classmap2ptr :: ClassMap -> IO (Ptr ())
144 classmap2ptr cmap = do
145 ptr_cmap <- newStablePtr cmap
146 return $ castStablePtrToPtr ptr_cmap
148 ptr2classmap :: Ptr () -> IO ClassMap
149 ptr2classmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)
151 virtualmap2ptr :: VirtualMap -> IO (Ptr ())
152 virtualmap2ptr cmap = do
153 ptr_cmap <- newStablePtr cmap
154 return $ castStablePtrToPtr ptr_cmap
156 ptr2virtualmap :: Ptr () -> IO VirtualMap
157 ptr2virtualmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)