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
MapBB,
#ifdef DBG_BB
printMapBB,
- test_main,
#endif
parseMethod,
testCFG -- added by hs to perform benches from outside
import Mate.Types
import Mate.Debug
+import Mate.Utilities
#ifdef DEBUG
import Text.Printf
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
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
-- 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)
-- 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)
import Mate.X86CodeGen
import Mate.ClassPool
import Mate.Debug
+import Mate.Utilities
foreign import ccall "dynamic"
code_void :: FunPtr (IO ()) -> IO ()
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'
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)
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
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
(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
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
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
--- /dev/null
+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);
+ }
+}