ClassPool: JAR and ClassPath support
[mate.git] / Mate.hs
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 #include "debug.h"
4 module Main where
5
6 import System.Environment
7 import Data.Char
8 import Data.List
9 import Data.String.Utils
10 import qualified Data.ByteString.Lazy as B
11
12 #ifdef DEBUG
13 import Text.Printf
14 #endif
15 import JVM.ClassFile
16 import Java.JAR
17
18 import Mate.BasicBlocks
19 import Mate.MethodPool
20 import Mate.Types
21 import Mate.ClassPool
22 import Mate.X86TrapHandling
23
24 main ::  IO ()
25 main = do
26   args <- getArgs
27   register_signal
28   addClassPath "./"
29   case args of
30     [clspath] -> do
31       let bclspath = B.pack $ map (fromIntegral . ord) clspath
32       cls <- getClassFile bclspath
33       executeMain bclspath cls
34     ["-jar", jarpath] -> do
35       addClassPathJAR jarpath
36       res <- readMainClass jarpath
37       case res of
38         Nothing -> error "JAR: no MainClass entry found. Try to pass the jar file via -cp instead."
39         Just mc -> do
40           let mc' = replace "." "/" mc
41           let bclspath = B.pack $ map (fromIntegral . ord) mc'
42           cls <- getClassFile bclspath
43           executeMain bclspath cls
44     _ -> error "Usage: mate [<class-file> | -jar <jar-file>]"
45
46 executeMain :: B.ByteString -> Class Direct -> IO ()
47 executeMain bclspath cls = do
48   hmap <- parseMethod cls "main"
49   case hmap of
50     Just hmap' -> do
51       let methods = classMethods cls; methods :: [Method Direct]
52       let method = find (\x -> methodName x == "main") methods
53       case method of
54         Just m -> do
55           let mi = MethodInfo "main" bclspath $ methodSignature m
56           entry <- compileBB hmap' mi
57           addMethodRef entry mi [bclspath]
58 #ifdef DEBUG
59           printf "executing `main' now:\n"
60 #endif
61           executeFuncPtr entry
62         Nothing -> error "main not found"
63     Nothing -> error "main not found"