updated
authorChristian Urban <urbanc@in.tum.de>
Sun, 13 Oct 2019 00:53:20 +0100
changeset 654 fb6192488b91
parent 653 9d7843934d30
child 655 3d04ee04966d
updated
progs/fun_llvm.scala
--- a/progs/fun_llvm.scala	Sat Oct 12 14:11:10 2019 +0100
+++ b/progs/fun_llvm.scala	Sun Oct 13 00:53:20 2019 +0100
@@ -1,18 +1,13 @@
-// A Small Compiler for a Simple Functional Language
+// A Small LLVM Compiler for a Simple Functional Language
 // (includes an external lexer and parser)
 //
 // call with 
 //
-//     scala fun.scala fact
-//
-//     scala fun.scala defs
+//     scala fun_llvm.scala fact
 //
-// this will generate a .j file and run the jasmin
-// assembler (installed at jvm/jasmin-2.4/jasmin.jar)
-// it runs the resulting JVM file twice for timing 
-// purposes.
-
-
+//     scala fun_llvm.scala defs
+//
+// this will generate a .ll file
 
 
 object Compiler {
@@ -52,8 +47,6 @@
   x ++ "_" ++ counter.toString()
 }
 
-
-
 // Abstract syntax trees for the Fun language
 abstract class KExp
 abstract class KVal
@@ -72,6 +65,9 @@
 }
 case class KReturn(v: KVal) extends KExp
 
+
+// CPS translation from Exp's to KExp's using a
+// continuation k.
 def CPS(e: Exp)(k: KVal => KExp) : KExp = e match {
   case Var(s) => k(KVar(s)) 
   case Num(i) => k(KNum(i))
@@ -104,35 +100,36 @@
 
 def CPSi(e: Exp) = CPS(e)(KReturn)
 
+// some testcases
 val e1 = Aop("*", Var("a"), Num(3))
-CPS(e1)(KReturn)
+CPSi(e1)
 
 val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
-CPS(e2)(KReturn)
+CPSi(e2)
 
 val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
-CPS(e3)(KReturn)
+CPSi(e3)
 
 val e4 = Aop("+", Aop("-", Num(1), Num(2)), Aop("*", Var("a"), Num(3)))
-CPS(e4)(KReturn)
+CPSi(e4)
 
 val e5 = If(Bop("==", Num(1), Num(1)), Num(3), Num(4))
-CPS(e5)(KReturn)
+CPSi(e5)
 
 val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
-CPS(e6)(KReturn)
+CPSi(e6)
 
 val e7 = Call("foo", List(Num(3)))
-CPS(e7)(KReturn)
+CPSi(e7)
 
 val e8 = Call("foo", List(Num(3), Num(4), Aop("+", Num(5), Num(6))))
-CPS(e8)(KReturn)
+CPSi(e8)
 
 val e9 = Sequence(Aop("*", Var("a"), Num(3)), Aop("+", Var("b"), Num(6)))
-CPS(e9)(KReturn)
+CPSi(e9)
 
 val e = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
-CPS(e)(KReturn)
+CPSi(e)
 
 
 
@@ -195,10 +192,6 @@
 // compile function for declarations and main
 def compile_decl(d: Decl) : String = d match {
   case Def(name, args, body) => { 
-    //println(s"DEF\n $name ($args) = \nBODY:")
-    //println(CPSi(body))
-    //println()
-    //counter = -1
     m"define i32 @$name (${args.mkString("i32 %", ", i32 %", "")}) {" ++
     compile_exp(CPSi(body)) ++
     m"}\n"
@@ -228,7 +221,6 @@
 
 def compile(class_name: String) : String = {
   val ast = deserialise[List[Decl]](class_name ++ ".prs").getOrElse(Nil) 
-  //println(ast(0).toString ++ "\n")
   ast.map(compile_decl).mkString
 }