From: Bernhard Urban Date: Sun, 20 May 2012 19:50:49 +0000 (+0200) Subject: methodlookup: unique identifier for methods are name+signature X-Git-Url: http://wien.tomnetworks.com/gitweb/?p=mate.git;a=commitdiff_plain;h=1315c607541c6fe37830242dfca042e60a2b6eb0 methodlookup: unique identifier for methods are name+signature in our case always the "first" method with the name was taken (the signature was ignored) --- diff --git a/Mate.hs b/Mate.hs index a4e2a97..1c229e3 100644 --- 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 diff --git a/Mate/BasicBlocks.hs b/Mate/BasicBlocks.hs index b52f22d..8cd919a 100644 --- a/Mate/BasicBlocks.hs +++ b/Mate/BasicBlocks.hs @@ -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) diff --git a/Mate/ClassPool.hs b/Mate/ClassPool.hs index 8b3e8a9..0f18289 100644 --- a/Mate/ClassPool.hs +++ b/Mate/ClassPool.hs @@ -260,7 +260,7 @@ loadAndInitClass path = do -- execute class initializer case lookupMethod "" (ciFile ci) of Just m -> do - hmap <- parseMethod (ciFile ci) "" + hmap <- parseMethod (ciFile ci) "" $ MethodSignature [] ReturnsVoid case hmap of Just hmap' -> do let mi = MethodInfo "" path (methodSignature m) diff --git a/Mate/MethodPool.hs b/Mate/MethodPool.hs index 2f29b40..5a01445 100644 --- a/Mate/MethodPool.hs +++ b/Mate/MethodPool.hs @@ -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 diff --git a/Mate/Utilities.hs b/Mate/Utilities.hs index c76d680..0fd5ddd 100644 --- a/Mate/Utilities.hs +++ b/Mate/Utilities.hs @@ -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 diff --git a/Mate/X86CodeGen.hs b/Mate/X86CodeGen.hs index 724cfc4..42ee662 100644 --- a/Mate/X86CodeGen.hs +++ b/Mate/X86CodeGen.hs @@ -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 index 0000000..8707502 --- /dev/null +++ b/tests/Instance4.java @@ -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); + } +}