refactor: use `unsafePerformIO hack' for global var
[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 qualified Data.ByteString.Lazy as B
10
11 #ifdef DEBUG
12 import Text.Printf
13 #endif
14 import JVM.ClassFile
15
16 import Mate.BasicBlocks
17 import Mate.X86CodeGen
18 import Mate.MethodPool
19 import Mate.Types
20 import Mate.ClassPool
21
22 main ::  IO ()
23 main = do
24   args <- getArgs
25   register_signal
26   case args of
27     [clspath] -> do
28       let bclspath = B.pack $ map (fromIntegral . ord) clspath
29       cls <- getClassFile bclspath
30       hmap <- parseMethod cls "main"
31       case hmap of
32         Just hmap' -> do
33           let methods = classMethods cls; methods :: [Method Resolved]
34           let method = find (\x -> methodName x == "main") methods
35           case method of
36             Just m -> do
37               let mi = MethodInfo "main" bclspath $ methodSignature m
38               entry <- compileBB hmap' mi
39               addMethodRef entry mi [bclspath]
40 #ifdef DEBUG
41               printf "executing `main' now:\n"
42 #endif
43               executeFuncPtr entry
44             Nothing -> error "main not found"
45         Nothing -> error "main not found"
46     _ -> error "Usage: mate <class-file>"