invokevirtual: get the actual class at run-time
[mate.git] / Mate / Types.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ForeignFunctionInterface #-}
3 module Mate.Types where
4
5 import Data.Char
6 import Data.Word
7 import Data.Int
8 import qualified Data.Map as M
9 import qualified Data.ByteString.Lazy as B
10 import Codec.Binary.UTF8.String hiding (encode,decode)
11
12 import Foreign.Ptr
13 import Foreign.StablePtr
14
15 import JVM.ClassFile
16 import JVM.Assembler
17
18
19 type BlockID = Int
20 -- Represents a CFG node
21 data BasicBlock = BasicBlock {
22                      code    :: [Instruction],
23                      successor :: BBEnd }
24
25 -- describes (leaving) edges of a CFG node
26 data BBEnd = Return | FallThrough BlockID | OneTarget BlockID | TwoTarget BlockID BlockID deriving Show
27
28 type MapBB = M.Map BlockID BasicBlock
29
30
31 -- Word32 = point of method call in generated code
32 -- MethodInfo = relevant information about callee
33 type TMap = M.Map Word32 TrapInfo
34
35 data TrapInfo =
36   MI MethodInfo |
37   VI MethodInfo | -- for virtual calls
38   SFI StaticFieldInfo
39
40 data StaticFieldInfo = StaticFieldInfo {
41   sfiClassName :: B.ByteString,
42   sfiFieldName :: B.ByteString }
43
44 -- B.ByteString = name of method
45 -- Word32 = entrypoint of method
46 type MMap = M.Map MethodInfo Word32
47
48 type ClassMap = M.Map B.ByteString ClassInfo
49
50 type FieldMap = M.Map B.ByteString Int32
51
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
56
57 data ClassInfo = ClassInfo {
58   clName :: B.ByteString,
59   clFile :: Class Resolved,
60   clStaticMap  :: FieldMap,
61   clFieldMap :: FieldMap,
62   clMethodMap :: FieldMap,
63   clMethodBase :: Word32,
64   clInitDone :: Bool }
65
66 data MethodInfo = MethodInfo {
67   methName :: B.ByteString,
68   cName :: B.ByteString,
69   mSignature :: MethodSignature}
70
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)
74
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)
80     where
81     cmp_args = (show args_a) `compare` (show args_b)
82
83 instance Ord MethodInfo where
84   compare (MethodInfo m_a c_a s_a) (MethodInfo m_b c_b s_b)
85     | cmp_m /= EQ = cmp_m
86     | cmp_c /= EQ = cmp_c
87     | otherwise = s_a `compare` s_b
88     where
89     cmp_m = m_a `compare` m_b
90     cmp_c = c_a `compare` c_b
91
92 instance Show MethodInfo where
93   show (MethodInfo method c sig) =
94     (toString c) ++ "." ++ (toString method) ++ "." ++ (show sig)
95
96
97 toString :: B.ByteString -> String
98 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
99
100
101 -- global map hax
102 foreign import ccall "get_trapmap"
103   get_trapmap :: IO (Ptr ())
104
105 foreign import ccall "set_trapmap"
106   set_trapmap :: Ptr () -> IO ()
107
108 foreign import ccall "get_methodmap"
109   get_methodmap :: IO (Ptr ())
110
111 foreign import ccall "set_methodmap"
112   set_methodmap :: Ptr () -> IO ()
113
114 foreign import ccall "get_classmap"
115   get_classmap :: IO (Ptr ())
116
117 foreign import ccall "set_classmap"
118   set_classmap :: Ptr () -> IO ()
119
120 foreign import ccall "get_virtualmap"
121   get_virtualmap :: IO (Ptr ())
122
123 foreign import ccall "set_virtualmap"
124   set_virtualmap :: Ptr () -> IO ()
125
126 -- TODO(bernhard): make some typeclass magic 'n stuff
127 mmap2ptr :: MMap -> IO (Ptr ())
128 mmap2ptr mmap = do
129   ptr_mmap <- newStablePtr mmap
130   return $ castStablePtrToPtr ptr_mmap
131
132 ptr2mmap :: Ptr () -> IO MMap
133 ptr2mmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr MMap)
134
135 tmap2ptr :: TMap -> IO (Ptr ())
136 tmap2ptr tmap = do
137   ptr_tmap <- newStablePtr tmap
138   return $ castStablePtrToPtr ptr_tmap
139
140 ptr2tmap :: Ptr () -> IO TMap
141 ptr2tmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr tmap)
142
143 classmap2ptr :: ClassMap -> IO (Ptr ())
144 classmap2ptr cmap = do
145   ptr_cmap <- newStablePtr cmap
146   return $ castStablePtrToPtr ptr_cmap
147
148 ptr2classmap :: Ptr () -> IO ClassMap
149 ptr2classmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)
150
151 virtualmap2ptr :: VirtualMap -> IO (Ptr ())
152 virtualmap2ptr cmap = do
153   ptr_cmap <- newStablePtr cmap
154   return $ castStablePtrToPtr ptr_cmap
155
156 ptr2virtualmap :: Ptr () -> IO VirtualMap
157 ptr2virtualmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)