2 {-# LANGUAGE OverloadedStrings #-}
6 import System.Environment
10 import qualified Data.ByteString.Lazy as B
18 import Mate.BasicBlocks
19 import Mate.MethodPool
22 import Mate.X86TrapHandling
30 parseArgs :: [String] -> Bool -> IO ()
31 parseArgs ("-jar":jarpath:_) stdcp = do
32 if not stdcp then addClassPath "./" else return ()
33 addClassPathJAR jarpath
34 res <- readMainClass jarpath
36 Nothing -> error "JAR: no MainClass entry found. Try to pass the jar file via -cp instead."
38 let bclspath = B.pack $ map (fromIntegral . ord) mc
39 cls <- getClassFile bclspath
40 executeMain bclspath cls
41 parseArgs ("-cp":cps) cpset = parseArgs ("-classpath":cps) cpset
42 parseArgs ("-classpath":cps:xs) False = do
43 let paths = splitOn ":" cps
47 addStuff :: String -> IO ()
49 | ".jar" `isSuffixOf` x = addClassPathJAR x
50 | otherwise = addClassPath $ x ++ "/"
51 parseArgs ("-classpath":xs) _ = parseArgs ("-":xs) True -- usage
52 parseArgs (('-':_):_) _ = error "Usage: mate [-cp|-classpath <cp1:cp2:..>] [<class-file> | -jar <jar-file>]"
53 -- first argument which isn't prefixed by '-' should be a class file
54 parseArgs (clspath:_) stdcp = do
55 if not stdcp then addClassPath "./" else return ()
56 let bclspath = B.pack $ map (fromIntegral . ord) clspath
57 cls <- getClassFile bclspath
58 executeMain bclspath cls
59 parseArgs _ _ = parseArgs ["-"] False
62 executeMain :: B.ByteString -> Class Direct -> IO ()
63 executeMain bclspath cls = do
64 let methods = classMethods cls; methods :: [Method Direct]
65 case find (\x -> methodName x == "main") methods of
67 let mi = MethodInfo "main" bclspath $ methodSignature m
68 hmap <- parseMethod cls "main" $ methodSignature m
71 entry <- compileBB hmap' mi
72 addMethodRef entry mi [bclspath]
74 printf "executing `main' now:\n"
77 Nothing -> error "main not found"
78 Nothing -> error "main not found"