methodlookup: unique identifier for methods are name+signature
authorBernhard Urban <lewurm@gmail.com>
Sun, 20 May 2012 19:50:49 +0000 (21:50 +0200)
committerBernhard Urban <lewurm@gmail.com>
Sun, 20 May 2012 19:50:49 +0000 (21:50 +0200)
in our case always the "first" method with the name was taken
(the signature was ignored)

Mate.hs
Mate/BasicBlocks.hs
Mate/ClassPool.hs
Mate/MethodPool.hs
Mate/Utilities.hs
Mate/X86CodeGen.hs
tests/Instance4.java [new file with mode: 0644]

diff --git a/Mate.hs b/Mate.hs
index a4e2a97c258b0e8d687db652da39b49fd6b5a9f1..1c229e3cc560e8ddb1d2583febaa18b6eaf8c8c7 100644 (file)
--- a/Mate.hs
+++ b/Mate.hs
@@ -61,14 +61,13 @@ parseArgs _ _ = parseArgs ["-"] False
 
 executeMain :: B.ByteString -> Class Direct -> IO ()
 executeMain bclspath cls = do
-  hmap <- parseMethod cls "main"
-  case hmap of
-    Just hmap' -> do
-      let methods = classMethods cls; methods :: [Method Direct]
-      let method = find (\x -> methodName x == "main") methods
-      case method of
-        Just m -> do
-          let mi = MethodInfo "main" bclspath $ methodSignature m
+  let methods = classMethods cls; methods :: [Method Direct]
+  case find (\x -> methodName x == "main") methods of
+    Just m -> do
+      let mi = MethodInfo "main" bclspath $ methodSignature m
+      hmap <- parseMethod cls "main" $ methodSignature m
+      case hmap of
+        Just hmap' -> do
           entry <- compileBB hmap' mi
           addMethodRef entry mi [bclspath]
 #ifdef DEBUG
index b52f22d2c4a9e4b5f246f408572784613d67cf4e..8cd919a1e4f770152d971295ca4e7e2712477f07 100644 (file)
@@ -8,7 +8,6 @@ module Mate.BasicBlocks(
   MapBB,
 #ifdef DBG_BB
   printMapBB,
-  test_main,
 #endif
   parseMethod,
   testCFG -- added by hs to perform benches from outside
@@ -26,6 +25,7 @@ import JVM.Assembler
 
 import Mate.Types
 import Mate.Debug
+import Mate.Utilities
 
 #ifdef DEBUG
 import Text.Printf
@@ -61,11 +61,12 @@ printMapBB (Just hmap) = do
                   Nothing -> error $ "BlockID " ++ show i ++ " not found."
 #endif
 
+#if 0
 #ifdef DBG_BB
-testInstance :: String -> B.ByteString -> IO ()
-testInstance cf method = do
+testInstance :: String -> B.ByteString -> MethodSignature -> IO ()
+testInstance cf method sig = do
                       cls <- parseClassFile cf
-                      hmap <- parseMethod cls method
+                      hmap <- parseMethod cls method sig
                       printMapBB hmap
 #endif
 
@@ -83,11 +84,12 @@ test_02 = testInstance "./tests/While.class" "f"
 test_03 = testInstance "./tests/While.class" "g"
 test_04 = testInstance "./tests/Fac.class" "fac"
 #endif
+#endif
 
 
-parseMethod :: Class Direct -> B.ByteString -> IO (Maybe MapBB)
-parseMethod cls method = do
-                     let maybe_bb = testCFG $ lookupMethod method cls
+parseMethod :: Class Direct -> B.ByteString -> MethodSignature -> IO (Maybe MapBB)
+parseMethod cls method sig = do
+                     let maybe_bb = testCFG $ lookupMethodSig method sig cls
                      let msig = methodSignature $ classMethods cls !! 1
                      printfBb "BB: analysing \"%s\"\n" $ toString (method `B.append` ": " `B.append` encode msig)
 #ifdef DBG_BB
@@ -96,7 +98,7 @@ parseMethod cls method = do
                      -- small example how to get information about
                      -- exceptions of a method
                      -- TODO: remove ;-)
-                     let (Just m) = lookupMethod method cls
+                     let (Just m) = lookupMethodSig method sig cls
                      case attrByName m "Code" of
                       Nothing -> printfBb "exception: no handler for this method\n"
                       Just exceptionstream -> printfBb "exception: \"%s\"\n" (show $ codeExceptions $ decodeMethod exceptionstream)
index 8b3e8a961fdb71af97418061fe2f23f8ceeb3041..0f1828981f1af5c4c23c4132132ff592656ff011 100644 (file)
@@ -260,7 +260,7 @@ loadAndInitClass path = do
   -- execute class initializer
   case lookupMethod "<clinit>" (ciFile ci) of
     Just m -> do
-      hmap <- parseMethod (ciFile ci) "<clinit>"
+      hmap <- parseMethod (ciFile ci) "<clinit>" $ MethodSignature [] ReturnsVoid
       case hmap of
         Just hmap' -> do
           let mi = MethodInfo "<clinit>" path (methodSignature m)
index 2f29b400583dcb2291d2aa8318518f1bfb46a1e4..5a0144521ad241e626b110726612de27ade683bd 100644 (file)
@@ -29,6 +29,7 @@ import Mate.Types
 import Mate.X86CodeGen
 import Mate.ClassPool
 import Mate.Debug
+import Mate.Utilities
 
 foreign import ccall "dynamic"
    code_void :: FunPtr (IO ()) -> IO ()
@@ -54,7 +55,7 @@ getMethodEntry signal_from methodtable = do
     Nothing -> do
       cls <- getClassFile cm
       printfMp "getMethodEntry(from 0x%08x): no method \"%s\" found. compile it\n" w32_from (show mi')
-      mm <- lookupMethodRecursive method [] cls
+      mm <- lookupMethodRecursive method sig [] cls
       case mm of
         Just (mm', clsnames, cls') -> do
             let flags = methodAccessFlags mm'
@@ -71,7 +72,7 @@ getMethodEntry signal_from methodtable = do
                 setMethodMap $ M.insert mi' w32_nf mmap
                 return nf
               else do
-                hmap <- parseMethod cls' method
+                hmap <- parseMethod cls' method sig
                 case hmap of
                   Just hmap' -> do
                     entry <- compileBB hmap' (MethodInfo method (thisClass cls') sig)
@@ -81,18 +82,18 @@ getMethodEntry signal_from methodtable = do
         Nothing -> error $ show method ++ " not found. abort"
     Just w32 -> return (fromIntegral w32)
 
-lookupMethodRecursive :: B.ByteString -> [B.ByteString] -> Class Direct
+lookupMethodRecursive :: B.ByteString -> MethodSignature -> [B.ByteString] -> Class Direct
                          -> IO (Maybe (Method Direct, [B.ByteString], Class Direct))
-lookupMethodRecursive name clsnames cls =
+lookupMethodRecursive name sig clsnames cls =
   case res of
     Just x -> return $ Just (x, nextclsn, cls)
     Nothing -> if thisname == "java/lang/Object"
       then return Nothing
       else do
         supercl <- getClassFile (superClass cls)
-        lookupMethodRecursive name nextclsn supercl
+        lookupMethodRecursive name sig nextclsn supercl
   where
-  res = lookupMethod name cls
+  res = lookupMethodSig name sig cls
   thisname = thisClass cls
   nextclsn :: [B.ByteString]
   nextclsn = thisname:clsnames
@@ -132,13 +133,13 @@ compileBB hmap methodinfo = do
   tmap <- getTrapMap
 
   cls <- getClassFile (methClassName methodinfo)
-  let ebb = emitFromBB (methName methodinfo) cls hmap
+  let ebb = emitFromBB (methName methodinfo) (methSignature methodinfo) cls hmap
   (_, Right right) <- runCodeGen ebb () ()
 
   let ((entry, _, _, new_tmap), _) = right
   setTrapMap $ tmap `M.union` new_tmap -- prefers elements in tmap
 
-  printfJit "generated code of \"%s\":\n" (toString $ methName methodinfo)
+  printfJit "generated code of \"%s\" from \"%s\":\n" (toString $ methName methodinfo) (toString $ methClassName methodinfo)
   mapM_ (printfJit "%s\n" . showAtt) (snd right)
   printfJit "\n\n"
   -- UNCOMMENT NEXT LINES FOR GDB FUN
index c76d68043c94877ef187389e80929ac175bb2a46..0fd5ddd74ca9f33c5c313c67c102f03e2c9a077c 100644 (file)
@@ -53,3 +53,11 @@ methodHaveReturnValue cls idx = case ret of
     (CIfaceMethod _ nt') -> nt'
     _ -> error "methodHaveReturnValue: something wrong. abort."
   (MethodSignature _ ret) = ntSignature nt
+
+lookupMethodSig :: B.ByteString -> MethodSignature -> Class Direct -> Maybe (Method Direct)
+lookupMethodSig name sig cls = look (classMethods cls)
+  where
+  look [] = Nothing
+  look (f:fs)
+    | methodName f == name && methodSignature f == sig = Just f
+    | otherwise = look fs
index 724cfc4a8ffacc9d6bb6e469bc862da8c62e9ba8..42ee6620232fd4450b81157db8fbf4e0b739906d 100644 (file)
@@ -46,8 +46,8 @@ type BBStarts = M.Map BlockID Int
 type CompileInfo = (EntryPoint, BBStarts, Int, TrapMap)
 
 
-emitFromBB :: B.ByteString -> Class Direct -> MapBB -> CodeGen e s (CompileInfo, [Instruction])
-emitFromBB method cls hmap =  do
+emitFromBB :: B.ByteString -> MethodSignature -> Class Direct -> MapBB -> CodeGen e s (CompileInfo, [Instruction])
+emitFromBB method sig cls hmap =  do
         llmap <- sequence [newNamedLabel ("bb_" ++ show x) | (x,_) <- M.toList hmap]
         let lmap = zip (Prelude.fst $ unzip $ M.toList hmap) llmap
         ep <- getEntryPoint
@@ -336,8 +336,8 @@ emitFromBB method cls hmap =  do
   thisMethodArgCnt :: Word32
   thisMethodArgCnt = isNonStatic + fromIntegral (length args)
     where
-    (Just m) = lookupMethod method cls
-    (MethodSignature args _) = methodSignature m
+    (Just m) = lookupMethodSig method sig cls
+    (MethodSignature args _) = sig
     isNonStatic = if S.member ACC_STATIC (methodAccessFlags m)
         then 0 else 1 -- one argument for the this pointer
 
diff --git a/tests/Instance4.java b/tests/Instance4.java
new file mode 100644 (file)
index 0000000..8707502
--- /dev/null
@@ -0,0 +1,20 @@
+package tests;
+
+public class Instance4 {
+       public int a, b;
+
+       public Instance4() {
+               this(0x666, 0x1337);
+       }
+
+       public Instance4(int x, int y) {
+               this.a = x;
+               this.b = y;
+       }
+
+       public static void main(String []args) {
+               Instance4 x = new Instance4();
+               System.out.printf("result: 0x%08x\n", x.a);
+               System.out.printf("result: 0x%08x\n", x.b);
+       }
+}