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.X86TrapHandling
31 parseArgs :: [String] -> Bool -> IO ()
32 parseArgs ("-jar":jarpath:_) stdcp = do
33 unless stdcp $ addClassPath "./"
34 addClassPathJAR jarpath
35 res <- readMainClass jarpath
37 Nothing -> error "JAR: no MainClass entry found. Try to pass the jar file via -cp instead."
39 let bclspath = B.pack $ map (fromIntegral . ord) mc
40 cls <- getClassFile bclspath
41 executeMain bclspath cls
42 parseArgs ("-cp":cps) cpset = parseArgs ("-classpath":cps) cpset
43 parseArgs ("-classpath":cps:xs) False = do
44 let paths = splitOn ":" cps
48 addStuff :: String -> IO ()
50 | ".jar" `isSuffixOf` x = addClassPathJAR x
51 | otherwise = addClassPath $ x ++ "/"
52 parseArgs ("-classpath":xs) _ = parseArgs ("-":xs) True -- usage
53 parseArgs (('-':_):_) _ = error "Usage: mate [-cp|-classpath <cp1:cp2:..>] [<class-file> | -jar <jar-file>]"
54 -- first argument which isn't prefixed by '-' should be a class file
55 parseArgs (clspath:_) stdcp = do
56 unless stdcp $ addClassPath "./"
57 let bclspath = B.pack $ map (fromIntegral . ord) clspath
58 cls <- getClassFile bclspath
59 executeMain bclspath cls
60 parseArgs _ _ = parseArgs ["-"] False
63 executeMain :: B.ByteString -> Class Direct -> IO ()
64 executeMain bclspath cls = do
65 let methods = classMethods cls; methods :: [Method Direct]
66 case find (\x -> methodName x == "main") methods of
68 let mi = MethodInfo "main" bclspath $ methodSignature m
69 hmap <- parseMethod cls "main" $ methodSignature m
72 entry <- compileBB hmap' mi
73 addMethodRef entry mi [bclspath]
75 printf "executing `main' now:\n"
78 Nothing -> error "main not found"
79 Nothing -> error "main not found"