+lookupMethodRecursive :: B.ByteString -> [B.ByteString] -> Class Resolved
+ -> IO (Maybe (Method Resolved, [B.ByteString], Class Resolved))
+lookupMethodRecursive name 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
+ where
+ res = lookupMethod name cls
+ thisname = thisClass cls
+ nextclsn :: [B.ByteString]
+ nextclsn = thisname:clsnames
+
+-- TODO(bernhard): UBERHAX. ghc patch?
+foreign import ccall safe "lookupSymbol"
+ c_lookupSymbol :: CString -> IO (Ptr a)
+
+loadNativeFunction :: String -> IO CUInt
+loadNativeFunction sym = do
+ _ <- loadRawObject "ffi/native.o"
+ -- TODO(bernhard): WTF
+ resolveObjs (return ())
+ ptr <- withCString sym c_lookupSymbol
+ if ptr == nullPtr
+ then error $ "dyn. loading of \"" ++ sym ++ "\" failed."
+ else return $ fromIntegral $ ptrToIntPtr ptr
+
+-- t_01 :: IO ()
+-- t_01 = do
+-- (entry, _) <- testCase "./tests/Fib.class" "fib"
+-- let int_entry = ((fromIntegral $ ptrToIntPtr entry) :: Word32)
+-- let mmap = M.insert ("fib" :: String) int_entry M.empty
+-- mapM_ (\(x,y) -> printf "%s at 0x%08x\n" x y) $ M.toList mmap
+-- mmap2ptr mmap >>= set_mmap
+-- demo_mmap -- access Data.Map from C
+
+addMethodRef :: Word32 -> MethodInfo -> [B.ByteString] -> IO ()
+addMethodRef entry (MethodInfo mmname _ msig) clsnames = do
+ mmap <- getMethodMap
+ let newmap = M.fromList $ map (\x -> (MethodInfo mmname x msig, entry)) clsnames
+ setMethodMap $ mmap `M.union` newmap
+
+
+compileBB :: MapBB -> MethodInfo -> IO Word32
+compileBB hmap methodinfo = do
+ tmap <- getTrapMap
+
+ cls <- getClassFile (methClassName methodinfo)
+ let ebb = emitFromBB (methName 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)
+ mapM_ (printfJit "%s\n" . showAtt) (snd right)
+ printfJit "\n\n"
+ -- UNCOMMENT NEXT LINE FOR GDB FUN
+ -- _ <- getLine
+ -- (1) start it with `gdb ./mate' and then `run <classfile>'
+ -- (2) on getLine, press ctrl+c
+ -- (3) `br *0x<addr>'; obtain the address from the disasm above
+ -- (4) `cont' and press enter
+ return $ fromIntegral $ ptrToIntPtr entry
+
+
+executeFuncPtr :: Word32 -> IO ()
+executeFuncPtr entry =
+ code_void ((castPtrToFunPtr $ intPtrToPtr $ fromIntegral entry) :: FunPtr (IO ()))