-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.hs
84 lines (70 loc) · 2.8 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Main where
import GHC.IO.Encoding
import JVM.ClassFile
import Java.JAR
-- import Runtime.Environment
import System.Environment
import Data.Char
import Data.List
import Data.List.Split
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Char8 as C8
import Control.Monad
import LLVMFrontend.CFG
import MateVMRuntime.ClassPool
import MateVMRuntime.MethodPool
import MateVMRuntime.Types
import MateVMRuntime.Debug
import MateVMRuntime.RtsOptions
import MateVMRuntime.Utilities
import Mate.GC.Boehm
import Misc.Logger
bootstrapMethod :: String
bootstrapMethod = "RunMe"
main :: IO ()
main = do
time "main" $ do
args <- getArgs
parseArgs args False
printCompileTime
parseArgs :: [String] -> Bool -> IO ()
parseArgs ("-jar":jarpath:_) stdcp = do
unless stdcp $ addClassPath "./"
addClassPathJAR jarpath
res <- readMainClass jarpath
case res of
Nothing -> error "JAR: no MainClass entry found. Try to pass the jar file via -cp instead."
Just mc -> do
let bclspath = B.pack . map (fromIntegral . ord) $ mc
cls <- getClassFile bclspath
executeMain bclspath cls
parseArgs ("-cp":cps) cpset = parseArgs ("-classpath":cps) cpset
parseArgs ("-classpath":cps:xs) False = do
mapM_ addStuff $ splitOn ":" cps
parseArgs xs True
where
addStuff :: String -> IO ()
addStuff x
| ".jar" `isSuffixOf` x = addClassPathJAR x
| otherwise = addClassPath $ x ++ "/"
parseArgs ("-classpath":xs) _ = parseArgs ("-":xs) True -- usage
parseArgs (('-':_):_) _ = error "Usage: mate [-cp|-classpath <cp1:cp2:..>] [<class-file> | -jar <jar-file>]"
-- first argument which isn't prefixed by '-' should be a class file
parseArgs (clspath:_) stdcp = do
unless stdcp $ addClassPath "./"
let bclspath = B.pack . map (fromIntegral . ord) $ clspath
cls <- getClassFile bclspath
executeMain bclspath cls
parseArgs _ _ = parseArgs ["-"] False
executeMain :: B.ByteString -> Class Direct -> IO ()
executeMain bclspath cls = do
--required on some platforms, initializes boehmgc. [todo bernhard: maybe this should be moved somewhere else - maybe at a global place where vm initialization takes place]
unless usePreciseGC initGC
case find ((==) (C8.pack bootstrapMethod) . methodName) (classMethods cls) of
Just m -> do
let mi = MethodInfo (C8.pack bootstrapMethod) bclspath $ methodSignature m
entry <- lookupMethodEntry mi
printfInfo $ "executing '" ++ bootstrapMethod ++ "' now:\n"
executeFuncPtr (fromIntegral entry)
printfInfo "Well, goodbye Sir!\n"
Nothing -> error $ bootstrapMethod ++ " not found"