codegen: handle exceptions of a method
[mate.git] / Mate / Types.hs
index 060ffb7403e3d1e036bbec9cedbc97eab3659288..d3bc626300fbad81f692e75de8f9e78b8e470fd4 100644 (file)
@@ -4,7 +4,10 @@ module Mate.Types
   , BasicBlock(..)
   , BBEnd(..)
   , MapBB
+  , ExceptionMap
+  , JpcNpcMap
   , RawMethod(..)
+  , TrapPatcher, TrapPatcherEax, TrapPatcherEaxEsp
   , TrapMap, MethodMap, ClassMap, FieldMap
   , StringMap, VirtualMap, InterfaceMap
   , InterfaceMethodMap
@@ -23,7 +26,9 @@ module Mate.Types
 
 import Data.Int
 import Data.Functor
+import Data.Word
 import qualified Data.Map as M
+import qualified Data.Bimap as BI
 import qualified Data.ByteString.Lazy as B
 
 import Data.IORef
@@ -41,7 +46,8 @@ import Mate.NativeSizes
 type BlockID = Int
 -- Represents a CFG node
 data BasicBlock = BasicBlock {
-  code :: [Instruction],
+  code :: [(Int, Instruction)],
+  bblength :: Int,
   successor :: BBEnd }
 
 -- describes (leaving) edges of a CFG node
@@ -53,9 +59,14 @@ data BBEnd
   deriving Show
 
 type MapBB = M.Map BlockID BasicBlock
+type ExceptionMap = M.Map (Word16, Word16) [(B.ByteString, Word16)]
+
+-- java byte code PC -> native PC
+type JpcNpcMap = BI.Bimap Int Word32
 
 data RawMethod = RawMethod {
   rawMapBB :: MapBB,
+  rawExcpMap :: ExceptionMap,
   rawLocals :: Int,
   rawStackSize :: Int,
   rawArgCount :: NativeWord,
@@ -67,12 +78,15 @@ data RawMethod = RawMethod {
 type TrapMap = M.Map NativeWord TrapCause
 
 type TrapPatcher = CPtrdiff -> CodeGen () () CPtrdiff
+type TrapPatcherEax = CPtrdiff -> TrapPatcher
+type TrapPatcherEaxEsp =  CPtrdiff -> TrapPatcherEax
 
 data TrapCause
-  = StaticMethod MethodInfo -- for static calls
+  = StaticMethod TrapPatcher -- for static calls
   | VirtualCall Bool MethodInfo (IO NativeWord) -- for invoke{interface,virtual}
-  | InstanceOf B.ByteString -- class name
-  | NewObject B.ByteString -- class name
+  | InstanceOf TrapPatcherEax
+  | ThrowException TrapPatcherEaxEsp
+  | NewObject TrapPatcher
   | StaticField StaticFieldInfo
   | ObjectField TrapPatcher
 
@@ -84,7 +98,7 @@ data StaticFieldInfo = StaticFieldInfo {
 
 -- B.ByteString = name of method
 -- NativeWord = entrypoint of method
-type MethodMap = M.Map MethodInfo NativeWord
+type MethodMap = M.Map MethodInfo (NativeWord, JpcNpcMap)
 
 data MethodInfo = MethodInfo {
   methName :: B.ByteString,