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
35 data TrapInfo = MI MethodInfo | SFI StaticFieldInfo
37 data StaticFieldInfo = StaticFieldInfo {
38 sfiClassName :: B.ByteString,
39 sfiFieldName :: B.ByteString }
41 -- B.ByteString = name of method
42 -- Word32 = entrypoint of method
43 type MMap = M.Map MethodInfo Word32
45 type ClassMap = M.Map B.ByteString ClassInfo
47 type FieldMap = M.Map B.ByteString Int32
49 data ClassInfo = ClassInfo {
50 clName :: B.ByteString,
51 clFile :: Class Resolved,
52 clStaticMap :: FieldMap,
53 clFieldMap :: FieldMap,
56 data MethodInfo = MethodInfo {
57 methName :: B.ByteString,
58 cName :: B.ByteString,
59 mSignature :: MethodSignature}
61 instance Eq MethodInfo where
62 (MethodInfo m_a c_a s_a) == (MethodInfo m_b c_b s_b) =
63 (m_a == m_b) && (c_a == c_b) && (s_a == s_b)
65 -- TODO(bernhard): not really efficient. also, outsource that to hs-java
66 instance Ord MethodSignature where
67 compare (MethodSignature args_a ret_a) (MethodSignature args_b ret_b)
68 | cmp_args /= EQ = cmp_args
69 | otherwise = (show ret_a) `compare` (show ret_b)
71 cmp_args = (show args_a) `compare` (show args_b)
73 instance Ord MethodInfo where
74 compare (MethodInfo m_a c_a s_a) (MethodInfo m_b c_b s_b)
77 | otherwise = s_a `compare` s_b
79 cmp_m = m_a `compare` m_b
80 cmp_c = c_a `compare` c_b
82 instance Show MethodInfo where
83 show (MethodInfo method c sig) =
84 (toString c) ++ "." ++ (toString method) ++ "." ++ (show sig)
87 toString :: B.ByteString -> String
88 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
92 foreign import ccall "get_trapmap"
93 get_trapmap :: IO (Ptr ())
95 foreign import ccall "set_trapmap"
96 set_trapmap :: Ptr () -> IO ()
98 foreign import ccall "get_methodmap"
99 get_methodmap :: IO (Ptr ())
101 foreign import ccall "set_methodmap"
102 set_methodmap :: Ptr () -> IO ()
104 foreign import ccall "get_classmap"
105 get_classmap :: IO (Ptr ())
107 foreign import ccall "set_classmap"
108 set_classmap :: Ptr () -> IO ()
110 -- TODO(bernhard): make some typeclass magic 'n stuff
111 mmap2ptr :: MMap -> IO (Ptr ())
113 ptr_mmap <- newStablePtr mmap
114 return $ castStablePtrToPtr ptr_mmap
116 ptr2mmap :: Ptr () -> IO MMap
117 ptr2mmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr MMap)
119 tmap2ptr :: TMap -> IO (Ptr ())
121 ptr_tmap <- newStablePtr tmap
122 return $ castStablePtrToPtr ptr_tmap
124 ptr2tmap :: Ptr () -> IO TMap
125 ptr2tmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr tmap)
127 classmap2ptr :: ClassMap -> IO (Ptr ())
128 classmap2ptr cmap = do
129 ptr_cmap <- newStablePtr cmap
130 return $ castStablePtrToPtr ptr_cmap
132 ptr2classmap :: Ptr () -> IO ClassMap
133 ptr2classmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)