summaryrefslogtreecommitdiff
path: root/examples/gfcc/compiler
diff options
context:
space:
mode:
authoraarne <unknown>2004-09-25 08:24:11 +0000
committeraarne <unknown>2004-09-25 08:24:11 +0000
commit5a208ce3ea26726d15e814c0498680597cca45fa (patch)
tree89ca12f9b6f9fe16cf4bcd6d165e13f3021ce619 /examples/gfcc/compiler
parentff2a2895c08b667894b565c8d39f0bf63d85492a (diff)
compiler works on abs and fibonacci
Diffstat (limited to 'examples/gfcc/compiler')
-rw-r--r--examples/gfcc/compiler/CleanJVM.hs26
-rw-r--r--examples/gfcc/compiler/abs.c12
-rw-r--r--examples/gfcc/compiler/fibonacci.c18
-rw-r--r--examples/gfcc/compiler/runtime.j34
4 files changed, 78 insertions, 12 deletions
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 <init>()V",
"aload_0",
diff --git a/examples/gfcc/compiler/abs.c b/examples/gfcc/compiler/abs.c
index c93b703b6..90312a2de 100644
--- a/examples/gfcc/compiler/abs.c
+++ b/examples/gfcc/compiler/abs.c
@@ -1,12 +1,20 @@
int abs (int x){
+ int y ;
+ {
if (x < 0){
- return 0 - x ;
+ y = 0 - x ;
}
- else return x ;
+ else {
+ y = x ;
+ }
+ }
+ return y ;
} ;
int main () {
int i ;
i = abs (16);
+ printf (int,i) ;
+ return ;
} ;
diff --git a/examples/gfcc/compiler/fibonacci.c b/examples/gfcc/compiler/fibonacci.c
new file mode 100644
index 000000000..c5a791bdf
--- /dev/null
+++ b/examples/gfcc/compiler/fibonacci.c
@@ -0,0 +1,18 @@
+int mx () {
+ return 5000000 ;
+} ;
+
+int main () {
+ int lo ; int hi ;
+ lo = 1 ;
+ hi = lo ;
+ printf(int,lo) ;
+ {
+ while (hi < mx()) {
+ printf(int,hi) ;
+ hi = lo + hi ;
+ lo = hi - lo ;
+ }
+ }
+ return ;
+} ;
diff --git a/examples/gfcc/compiler/runtime.j b/examples/gfcc/compiler/runtime.j
index c99523cc7..88db0b9b8 100644
--- a/examples/gfcc/compiler/runtime.j
+++ b/examples/gfcc/compiler/runtime.j
@@ -19,7 +19,37 @@
Label0:
iconst_0
ireturn
- Label1:
.end method
-; TODO: flt missing
+.method public static flt(FF)I
+.limit locals 2
+.limit stack 2
+ fload_0
+ fload_1
+ fcmpl
+ ifge Label0
+ iconst_1
+ ireturn
+ Label0:
+ iconst_0
+ ireturn
+.end method
+
+.method public static iprintf(I)V
+.limit locals 1
+.limit stack 1000
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ iload_0
+ invokevirtual java/io/PrintStream/println(I)V
+ return
+.end method
+
+.method public static fprintf(F)V
+.limit locals 1
+.limit stack 1000
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ fload_0
+ invokevirtual java/io/PrintStream/println(F)V
+ return
+.end method
+