summaryrefslogtreecommitdiff
path: root/examples/gfcc/JVM.hs
diff options
context:
space:
mode:
authoraarne <unknown>2004-09-19 20:27:01 +0000
committeraarne <unknown>2004-09-19 20:27:01 +0000
commitdf4cbb482f0546b884eb210d825c794d14f82712 (patch)
treecc8ecc187cdd2ce07926308ee1656a1fa3a213b7 /examples/gfcc/JVM.hs
parent3a1f403a0146f4717b210373167640a07f0248dd (diff)
report
Diffstat (limited to 'examples/gfcc/JVM.hs')
-rw-r--r--examples/gfcc/JVM.hs20
1 files changed, 20 insertions, 0 deletions
diff --git a/examples/gfcc/JVM.hs b/examples/gfcc/JVM.hs
new file mode 100644
index 000000000..380570049
--- /dev/null
+++ b/examples/gfcc/JVM.hs
@@ -0,0 +1,20 @@
+module JVM where
+
+mkJVM :: String -> String
+mkJVM = unlines . reverse . fst . foldl trans ([],([],0)) . lines where
+ trans (code,(env,v)) s = case words s of
+ ".method":f:ns -> ((".method " ++ f ++ concat ns):code,([],0))
+ "alloc":t:x:_ -> (code, ((x,v):env, v + size t))
+ ".limit":"locals":ns -> chCode (".limit locals " ++ show (length ns - 1))
+ t:"_load" :x:_ -> chCode (t ++ "load " ++ look x)
+ t:"_store":x:_ -> chCode (t ++ "store " ++ look x)
+ t:"_return":_ -> chCode (t ++ "return")
+ "goto":ns -> chCode ("goto " ++ concat ns)
+ "ifzero":ns -> chCode ("ifzero " ++ concat ns)
+ _ -> chCode s
+ where
+ chCode c = (c:code,(env,v))
+ look x = maybe (x ++ show env) show $ lookup x env
+ size t = case t of
+ "d" -> 2
+ _ -> 1