global map hack: refactor
authorBernhard Urban <lewurm@gmail.com>
Mon, 23 Apr 2012 14:10:21 +0000 (16:10 +0200)
committerBernhard Urban <lewurm@gmail.com>
Mon, 23 Apr 2012 14:11:07 +0000 (16:11 +0200)
TODO: do some haskell magic for conversion functions.
  type class? template haskell?

Mate/ClassPool.hs
Mate/MethodPool.hs
Mate/Types.hs
Mate/X86CodeGen.hs
ffi/trap.c
tests/Static1.java

index d14c2f30c61a5eda67982866fc3cf136f69a767f..ccfe1b3afc7b4bad72e817e09479582cfacc0bc3 100644 (file)
@@ -30,5 +30,13 @@ import Mate.Utilities
 
 getClassFile :: B.ByteString -> IO (Class Resolved)
 getClassFile path = do
-  let rpath = toString $ path `B.append` ".class"
-  parseClassFile rpath
+  ptr_classmap <- get_classmap
+  class_map <- ptr2classmap ptr_classmap
+  case M.lookup path class_map of
+    Nothing -> do
+      let rpath = toString $ path `B.append` ".class"
+      cfile <- parseClassFile rpath
+      let class_map' = M.insert path (ClassInfo path cfile) class_map
+      classmap2ptr class_map' >>= set_classmap
+      return cfile
+    Just (ClassInfo name cfs) -> return cfs
index f2ead78bfb2ccc995c42c7b2a21cca9a2a2fbdfc..cba2d174da877a5c94c9ab5841a29f6661ada29c 100644 (file)
@@ -29,12 +29,6 @@ import Mate.Utilities
 import Mate.ClassPool
 
 
-foreign import ccall "get_mmap"
-  get_mmap :: IO (Ptr ())
-
-foreign import ccall "set_mmap"
-  set_mmap :: Ptr () -> IO ()
-
 foreign import ccall "dynamic"
    code_void :: FunPtr (IO ()) -> (IO ())
 
@@ -71,7 +65,7 @@ getMethodEntry signal_from ptr_mmap ptr_cmap = do
                 nf <- loadNativeFunction symbol
                 let w32_nf = fromIntegral nf
                 let mmap' = M.insert mi w32_nf mmap
-                mmap2ptr mmap' >>= set_mmap
+                mmap2ptr mmap' >>= set_methodmap
                 return nf
         Nothing -> error $ (show method) ++ " not found. abort"
     Just w32 -> return (fromIntegral w32)
@@ -101,13 +95,14 @@ loadNativeFunction sym = do
 
 initMethodPool :: IO ()
 initMethodPool = do
-  mmap2ptr M.empty >>= set_mmap
-  cmap2ptr M.empty >>= set_cmap
+  mmap2ptr M.empty >>= set_methodmap
+  cmap2ptr M.empty >>= set_callermap
+  classmap2ptr M.empty >>= set_classmap
 
 compileBB :: MapBB -> MethodInfo -> IO (Ptr Word8)
 compileBB hmap methodinfo = do
-  mmap <- get_mmap >>= ptr2mmap
-  cmap <- get_cmap >>= ptr2cmap
+  mmap <- get_methodmap >>= ptr2mmap
+  cmap <- get_callermap >>= ptr2cmap
 
   -- TODO(bernhard): replace parsing with some kind of classpool
   cls <- getClassFile (cName methodinfo)
@@ -117,8 +112,8 @@ compileBB hmap methodinfo = do
 
   let mmap' = M.insert methodinfo w32_entry mmap
   let cmap' = M.union cmap new_cmap -- prefers elements in cmap
-  mmap2ptr mmap' >>= set_mmap
-  cmap2ptr cmap' >>= set_cmap
+  mmap2ptr mmap' >>= set_methodmap
+  cmap2ptr cmap' >>= set_callermap
 
   printf "disasm:\n"
   mapM_ (putStrLn . showAtt) disasm
@@ -133,20 +128,3 @@ compileBB hmap methodinfo = do
 
 executeFuncPtr :: Ptr Word8 -> IO ()
 executeFuncPtr entry = code_void $ ((castPtrToFunPtr entry) :: FunPtr (IO ()))
-
--- TODO(bernhard): make some typeclass magic 'n stuff
-mmap2ptr :: MMap -> IO (Ptr ())
-mmap2ptr mmap = do
-  ptr_mmap <- newStablePtr mmap
-  return $ castStablePtrToPtr ptr_mmap
-
-ptr2mmap :: Ptr () -> IO MMap
-ptr2mmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr MMap)
-
-cmap2ptr :: CMap -> IO (Ptr ())
-cmap2ptr cmap = do
-  ptr_cmap <- newStablePtr cmap
-  return $ castStablePtrToPtr ptr_cmap
-
-ptr2cmap :: Ptr () -> IO CMap
-ptr2cmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)
index cf4bc7834a51c8c296ac58ccc4db77c175238b19..8432e9d800c10269ff61d61e8fbbd3c3c55b0478 100644 (file)
@@ -1,3 +1,5 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE ForeignFunctionInterface #-}
 module Mate.Types where
 
 import Data.Char
@@ -6,6 +8,11 @@ import qualified Data.Map as M
 import qualified Data.ByteString.Lazy as B
 import Codec.Binary.UTF8.String hiding (encode,decode)
 
+import Foreign.Ptr
+import Foreign.C.Types
+import Foreign.C.String
+import Foreign.StablePtr
+
 import JVM.ClassFile
 import JVM.Assembler
 
@@ -30,6 +37,11 @@ type CMap = M.Map Word32 MethodInfo
 -- Word32 = entrypoint of method
 type MMap = M.Map MethodInfo Word32
 
+type ClassMap = M.Map B.ByteString ClassInfo
+
+data ClassInfo = ClassInfo {
+  clName :: B.ByteString,
+  clFile :: Class Resolved }
 
 
 data MethodInfo = MethodInfo {
@@ -65,3 +77,48 @@ instance Show MethodInfo where
 
 toString :: B.ByteString -> String
 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
+
+
+-- global map hax
+foreign import ccall "get_callermap"
+  get_callermap :: IO (Ptr ())
+
+foreign import ccall "set_callermap"
+  set_callermap :: Ptr () -> IO ()
+
+foreign import ccall "get_methodmap"
+  get_methodmap :: IO (Ptr ())
+
+foreign import ccall "set_methodmap"
+  set_methodmap :: Ptr () -> IO ()
+
+foreign import ccall "get_classmap"
+  get_classmap :: IO (Ptr ())
+
+foreign import ccall "set_classmap"
+  set_classmap :: Ptr () -> IO ()
+
+-- TODO(bernhard): make some typeclass magic 'n stuff
+mmap2ptr :: MMap -> IO (Ptr ())
+mmap2ptr mmap = do
+  ptr_mmap <- newStablePtr mmap
+  return $ castStablePtrToPtr ptr_mmap
+
+ptr2mmap :: Ptr () -> IO MMap
+ptr2mmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr MMap)
+
+cmap2ptr :: CMap -> IO (Ptr ())
+cmap2ptr cmap = do
+  ptr_cmap <- newStablePtr cmap
+  return $ castStablePtrToPtr ptr_cmap
+
+ptr2cmap :: Ptr () -> IO CMap
+ptr2cmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)
+
+classmap2ptr :: ClassMap -> IO (Ptr ())
+classmap2ptr cmap = do
+  ptr_cmap <- newStablePtr cmap
+  return $ castStablePtrToPtr ptr_cmap
+
+ptr2classmap :: Ptr () -> IO ClassMap
+ptr2classmap vmap = deRefStablePtr $ ((castPtrToStablePtr vmap) :: StablePtr cmap)
index a28e4bb50d72b1fa67d6caff9639e8e478a54ba1..82ec9d8680768b3cf2c823174c62062353b04937 100644 (file)
@@ -39,12 +39,6 @@ foreign import ccall "callertrap"
 foreign import ccall "register_signal"
   register_signal :: IO ()
 
-foreign import ccall "get_cmap"
-  get_cmap :: IO (Ptr ())
-
-foreign import ccall "set_cmap"
-  set_cmap :: Ptr () -> IO ()
-
 test_01, test_02, test_03 :: IO ()
 test_01 = do
   register_signal
index 0e1c8735c9d9a640924177925ad1a153bd68dea5..4440cd7675802b26a89867732a6048427d01f30f 100644 (file)
 
 unsigned int getMethodEntry(unsigned int, void *, void *);
 
-void *method_map = NULL;
-void *caller_map = NULL;
-
-void set_mmap(void *mmap)
-{
-       printf("set_mmap: 0x%08x\n", (unsigned int) mmap);
-       method_map = mmap;
-}
-
-void *get_mmap()
-{
-       printf("get_mmap: 0x%08x\n", (unsigned int) method_map);
-       return method_map;
-}
-
-void set_cmap(void *cmap)
-{
-       printf("set_cmap: 0x%08x\n", (unsigned int) cmap);
-       caller_map = cmap;
-}
+#define NEW_MAP(prefix) \
+       void* prefix ## _map = NULL; \
+       void set_ ## prefix ## map(void *map) \
+       { \
+               printf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \
+               prefix ## _map = map; \
+       } \
+       void *get_ ## prefix ## map() \
+       { \
+               printf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \
+               return prefix ## _map; \
+       }
 
-void *get_cmap()
-{
-       printf("get_cmap: 0x%08x\n", (unsigned int) caller_map);
-       return caller_map;
-}
+NEW_MAP(method)
+NEW_MAP(caller)
+NEW_MAP(class)
 
 
 void mainresult(unsigned int a)
index 5bd85894b53ce13bb1a5aa70a8639ac250b79dfb..56bd3938a69e033b32f6cca7a2a997d7f18e126a 100644 (file)
@@ -4,6 +4,10 @@ public class Static1 {
        public static int a;
        public static int b;
 
+       static {
+               Static1.a = 0x1337;
+       }
+
        public static void main(String []args) {
                Static1.a = 0x11;
                Static1.b = 0x22;