debug: use #ifdef guards
[mate.git] / Mate.hs
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 module Main where
4
5 import System.Environment
6 import Data.Char
7 import Data.List
8 import qualified Data.ByteString.Lazy as B
9
10 #ifdef DEBUG
11 import Text.Printf
12
13 import JVM.Dump
14 #endif
15 import JVM.ClassFile
16
17 import Mate.BasicBlocks
18 import Mate.X86CodeGen
19 import Mate.MethodPool
20 import Mate.Types
21 import Mate.ClassPool
22
23 main ::  IO ()
24 main = do
25   args <- getArgs
26   register_signal
27   initMethodPool
28   case args of
29     [clspath] -> do
30       let bclspath = B.pack $ map (fromIntegral . ord) clspath
31       cls <- getClassFile bclspath
32 #ifdef DEBUG
33       dumpClass cls
34 #endif
35       hmap <- parseMethod cls "main"
36       case hmap of
37         Just hmap' -> do
38           let methods = classMethods cls; methods :: [Method Resolved]
39           let method = find (\x -> (methodName x) == "main") methods
40           case method of
41             Just m -> do
42               let mi = (MethodInfo "main" bclspath (methodSignature m))
43               entry <- compileBB hmap' mi
44               addMethodRef entry mi [bclspath]
45 #ifdef DEBUG
46               printf "executing `main' now:\n"
47 #endif
48               executeFuncPtr entry
49             Nothing -> error "main not found"
50         Nothing -> error "main not found"
51     _ -> error "Usage: mate <class-file>"