From 5a208ce3ea26726d15e814c0498680597cca45fa Mon Sep 17 00:00:00 2001 From: aarne Date: Sat, 25 Sep 2004 08:24:11 +0000 Subject: compiler works on abs and fibonacci --- examples/gfcc/compiler/CleanJVM.hs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'examples/gfcc/compiler/CleanJVM.hs') diff --git a/examples/gfcc/compiler/CleanJVM.hs b/examples/gfcc/compiler/CleanJVM.hs index 4bbf011d8..72a0060c2 100644 --- a/examples/gfcc/compiler/CleanJVM.hs +++ b/examples/gfcc/compiler/CleanJVM.hs @@ -3,6 +3,8 @@ module Main where import Char import System +--- now works for programs with exactly 2 functions, main last + main :: IO () main = do jvm:src:_ <- getArgs @@ -12,30 +14,38 @@ main = do writeFile obj $ boilerplate cls appendFile obj $ mkJVM cls s putStrLn $ "wrote file " ++ obj + system $ "jasmin " ++ obj + return () mkJVM :: String -> String -> String mkJVM cls = unlines . map trans . lines where trans s = case words s of - ".method":p:s:f:ns -> unwords [".method",p,s, unindex f ++ typesig ns] - ".limit":"locals":ns -> ".limit locals " ++ show (length ns - 1) - "invokestatic":t:"runtime/lt":ns -> ".invokestatic " ++ "runtime/" ++ t ++ "lt" ++ typesig ns + ".method":p:s:f:ns + | take 5 f == "main_" -> ".method public static main([Ljava/lang/String;)V" + | otherwise -> unwords [".method",p,s, unindex f ++ typesig ns] + ".limit":"locals":ns -> ".limit locals " ++ show (length ns) + "invokestatic":t:f:ns | take 8 f == "runtime/" -> + "invokestatic " ++ "runtime/" ++ t ++ drop 8 f ++ typesig ns "invokestatic":f:ns -> "invokestatic " ++ cls ++ "/" ++ unindex f ++ typesig ns "alloc":ns -> "; " ++ s + t:('_':instr):[] -> t ++ instr t:('_':instr):x:_ -> t ++ instr ++ " " ++ address x - "goto":ns -> "goto " ++ label ns - "ifeq":ns -> "ifzero " ++ label ns - "label":ns -> label ns + "goto":ns -> "goto " ++ label ns + "ifeq":ns -> "ifeq " ++ label ns + "label":ns -> label ns ++ ":" ";":[] -> "" _ -> s where unindex = reverse . drop 1 . dropWhile (/= '_') . reverse typesig = init . map toUpper . concat - address = reverse . takeWhile (/= '_') . reverse + address x = case (filter isDigit . reverse . takeWhile (/= '_') . reverse) x of + s@(_:_) -> show $ read s - (1 :: Int) + s -> s label = init . concat boilerplate :: String -> String boilerplate cls = unlines [ - ".class public " ++ cls ++ ".j", + ".class public " ++ cls, ".super java/lang/Object", ".method public ()V", "aload_0", -- cgit v1.2.3