2 {-# LANGUAGE OverloadedStrings #-}
6 import System.Environment
10 import qualified Data.ByteString.Lazy as B
19 import Mate.BasicBlocks
20 import Mate.MethodPool
23 import Mate.NativeMachine
33 parseArgs :: [String] -> Bool -> IO ()
34 parseArgs ("-jar":jarpath:_) stdcp = do
35 unless stdcp $ addClassPath "./"
36 addClassPathJAR jarpath
37 res <- readMainClass jarpath
39 Nothing -> error "JAR: no MainClass entry found. Try to pass the jar file via -cp instead."
41 let bclspath = B.pack . map (fromIntegral . ord) $ mc
42 cls <- getClassFile bclspath
43 executeMain bclspath cls
45 parseArgs ("-cp":cps) cpset = parseArgs ("-classpath":cps) cpset
46 parseArgs ("-classpath":cps:xs) False = do
47 mapM_ addStuff $ splitOn ":" cps
50 addStuff :: String -> IO ()
52 | ".jar" `isSuffixOf` x = addClassPathJAR x
53 | otherwise = addClassPath $ x ++ "/"
54 parseArgs ("-classpath":xs) _ = parseArgs ("-":xs) True -- usage
55 parseArgs (('-':_):_) _ = error "Usage: mate [-cp|-classpath <cp1:cp2:..>] [<class-file> | -jar <jar-file>]"
56 -- first argument which isn't prefixed by '-' should be a class file
57 parseArgs (clspath:_) stdcp = do
58 unless stdcp $ addClassPath "./"
59 let bclspath = B.pack . map (fromIntegral . ord) $ clspath
60 cls <- getClassFile bclspath
61 executeMain bclspath cls
62 parseArgs _ _ = parseArgs ["-"] False
65 executeMain :: B.ByteString -> Class Direct -> IO ()
66 executeMain bclspath cls = do
67 initGC --required on some platforms. [todo bernhard: maybe this should be moved somewhere else - maybe at a global place where vm initialization takes place
68 let methods = classMethods cls; methods :: [Method Direct]
69 case find (\x -> methodName x == "main") methods of
71 let mi = MethodInfo "main" bclspath $ methodSignature m
72 rawmethod <- parseMethod cls "main" $ methodSignature m
73 entry <- compileBB rawmethod mi
74 addMethodRef entry mi [bclspath]
76 printf "executing `main' now:\n"
79 Nothing -> error "main not found"