| author | Christian Urban <urbanc@in.tum.de> | 
| Sun, 13 Oct 2019 00:53:20 +0100 | |
| changeset 654 | 718a9913db60 | 
| parent 653 | 0b26a7a0556b | 
| child 655 | 689d1bdd91c0 | 
| permissions | -rw-r--r-- | 
| 654 | 1 | // A Small LLVM Compiler for a Simple Functional Language | 
| 644 | 2 | // (includes an external lexer and parser) | 
| 645 | 3 | // | 
| 4 | // call with | |
| 5 | // | |
| 654 | 6 | // scala fun_llvm.scala fact | 
| 645 | 7 | // | 
| 654 | 8 | // scala fun_llvm.scala defs | 
| 9 | // | |
| 10 | // this will generate a .ll file | |
| 645 | 11 | |
| 625 | 12 | |
| 649 | 13 | object Compiler {
 | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 14 | |
| 645 | 15 | import java.io._ | 
| 16 | import scala.util._ | |
| 17 | import scala.sys.process._ | |
| 18 | ||
| 644 | 19 | // Abstract syntax trees for the Fun language | 
| 20 | abstract class Exp extends Serializable | |
| 21 | abstract class BExp extends Serializable | |
| 22 | abstract class Decl extends Serializable | |
| 626 | 23 | |
| 24 | case class Def(name: String, args: List[String], body: Exp) extends Decl | |
| 25 | case class Main(e: Exp) extends Decl | |
| 26 | ||
| 27 | case class Call(name: String, args: List[Exp]) extends Exp | |
| 28 | case class If(a: BExp, e1: Exp, e2: Exp) extends Exp | |
| 29 | case class Write(e: Exp) extends Exp | |
| 30 | case class Var(s: String) extends Exp | |
| 31 | case class Num(i: Int) extends Exp | |
| 32 | case class Aop(o: String, a1: Exp, a2: Exp) extends Exp | |
| 33 | case class Sequence(e1: Exp, e2: Exp) extends Exp | |
| 34 | case class Bop(o: String, a1: Exp, a2: Exp) extends BExp | |
| 35 | ||
| 36 | ||
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 37 | // compiler - built-in functions | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 38 | // copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 39 | // | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 40 | |
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 41 | |
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 42 | // for generating new labels | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 43 | var counter = -1 | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 44 | |
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 45 | def Fresh(x: String) = {
 | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 46 | counter += 1 | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 47 | x ++ "_" ++ counter.toString() | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 48 | } | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 49 | |
| 648 | 50 | // Abstract syntax trees for the Fun language | 
| 51 | abstract class KExp | |
| 653 | 52 | abstract class KVal | 
| 648 | 53 | |
| 653 | 54 | case class KVar(s: String) extends KVal | 
| 55 | case class KNum(i: Int) extends KVal | |
| 56 | case class KAop(o: String, v1: KVal, v2: KVal) extends KVal | |
| 57 | case class KBop(o: String, v1: KVal, v2: KVal) extends KVal | |
| 58 | case class KCall(o: String, vrs: List[KVal]) extends KVal | |
| 649 | 59 | |
| 653 | 60 | case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
 | 
| 61 | override def toString = s"KIf $x1\nIF\n$e1\nELSE\n$e2" | |
| 649 | 62 | } | 
| 653 | 63 | case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
 | 
| 648 | 64 | override def toString = s"let $x = $e1 in \n$e2" | 
| 65 | } | |
| 653 | 66 | case class KReturn(v: KVal) extends KExp | 
| 648 | 67 | |
| 654 | 68 | |
| 69 | // CPS translation from Exp's to KExp's using a | |
| 70 | // continuation k. | |
| 653 | 71 | def CPS(e: Exp)(k: KVal => KExp) : KExp = e match {
 | 
| 72 | case Var(s) => k(KVar(s)) | |
| 73 | case Num(i) => k(KNum(i)) | |
| 74 |   case Aop(o, e1, e2) => {
 | |
| 75 |     val z = Fresh("tmp")
 | |
| 76 | CPS(e1)(y1 => | |
| 77 | CPS(e2)(y2 => KLet(z, KAop(o, y1, y2), k(KVar(z))))) | |
| 78 | } | |
| 79 |   case If(Bop(o, b1, b2), e1, e2) => {
 | |
| 80 |     val z = Fresh("tmp")
 | |
| 81 | CPS(b1)(y1 => | |
| 82 | CPS(b2)(y2 => KLet(z, KBop(o, y1, y2), KIf(z, CPS(e1)(k), CPS(e2)(k))))) | |
| 83 | } | |
| 84 |   case Call(name, args) => {
 | |
| 85 |     def aux(args: List[Exp], vs: List[KVal]) : KExp = args match {
 | |
| 86 |       case Nil => {
 | |
| 87 |           val z = Fresh("tmp")
 | |
| 88 | KLet(z, KCall(name, vs), k(KVar(z))) | |
| 89 | } | |
| 90 | case e::es => CPS(e)(y => aux(es, vs ::: List(y))) | |
| 648 | 91 | } | 
| 653 | 92 | aux(args, Nil) | 
| 93 | } | |
| 94 |   case Sequence(e1, e2) => {
 | |
| 95 |     val z = Fresh("tmp")
 | |
| 96 | CPS(e1)(y1 => | |
| 97 |       CPS(e2)(y2 => KLet("_", y1, KLet(z, y2, k(KVar(z))))))
 | |
| 648 | 98 | } | 
| 653 | 99 | } | 
| 100 | ||
| 101 | def CPSi(e: Exp) = CPS(e)(KReturn) | |
| 102 | ||
| 654 | 103 | // some testcases | 
| 653 | 104 | val e1 = Aop("*", Var("a"), Num(3))
 | 
| 654 | 105 | CPSi(e1) | 
| 653 | 106 | |
| 107 | val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
 | |
| 654 | 108 | CPSi(e2) | 
| 653 | 109 | |
| 110 | val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
 | |
| 654 | 111 | CPSi(e3) | 
| 648 | 112 | |
| 653 | 113 | val e4 = Aop("+", Aop("-", Num(1), Num(2)), Aop("*", Var("a"), Num(3)))
 | 
| 654 | 114 | CPSi(e4) | 
| 653 | 115 | |
| 116 | val e5 = If(Bop("==", Num(1), Num(1)), Num(3), Num(4))
 | |
| 654 | 117 | CPSi(e5) | 
| 653 | 118 | |
| 119 | val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
 | |
| 654 | 120 | CPSi(e6) | 
| 648 | 121 | |
| 653 | 122 | val e7 = Call("foo", List(Num(3)))
 | 
| 654 | 123 | CPSi(e7) | 
| 653 | 124 | |
| 125 | val e8 = Call("foo", List(Num(3), Num(4), Aop("+", Num(5), Num(6))))
 | |
| 654 | 126 | CPSi(e8) | 
| 653 | 127 | |
| 128 | val e9 = Sequence(Aop("*", Var("a"), Num(3)), Aop("+", Var("b"), Num(6)))
 | |
| 654 | 129 | CPSi(e9) | 
| 649 | 130 | |
| 131 | val e = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
 | |
| 654 | 132 | CPSi(e) | 
| 653 | 133 | |
| 648 | 134 | |
| 135 | ||
| 136 | ||
| 625 | 137 | // convenient string interpolations | 
| 138 | // for instructions, labels and methods | |
| 139 | import scala.language.implicitConversions | |
| 140 | import scala.language.reflectiveCalls | |
| 141 | ||
| 142 | implicit def sring_inters(sc: StringContext) = new {
 | |
| 143 | def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" | |
| 144 | def l(args: Any*): String = sc.s(args:_*) ++ ":\n" | |
| 145 | def m(args: Any*): String = sc.s(args:_*) ++ "\n" | |
| 146 | } | |
| 147 | ||
| 653 | 148 | def compile_op(op: String) = op match {
 | 
| 149 | case "+" => "add i32 " | |
| 150 | case "*" => "mul i32 " | |
| 151 | case "-" => "sub i32 " | |
| 152 | case "==" => "icmp eq i32 " | |
| 153 | } | |
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 154 | |
| 653 | 155 | def compile_val(v: KVal) : String = v match {
 | 
| 156 | case KNum(i) => s"$i" | |
| 157 | case KVar(s) => s"%$s" | |
| 158 | case KAop(op, x1, x2) => | |
| 159 |     s"${compile_op(op)} ${compile_val(x1)}, ${compile_val(x2)}"
 | |
| 160 | case KBop(op, x1, x2) => | |
| 161 |     s"${compile_op(op)} ${compile_val(x1)}, ${compile_val(x2)}"
 | |
| 162 | case KCall(x1, args) => | |
| 163 |     s"call i32 @$x1 (${args.map(compile_val).mkString("i32 ", ", i32 ", "")})"
 | |
| 164 | } | |
| 648 | 165 | |
| 649 | 166 | // compile K expressions | 
| 167 | def compile_exp(a: KExp) : String = a match {
 | |
| 653 | 168 | case KReturn(v) => | 
| 169 |     i"ret i32 ${compile_val(v)}"
 | |
| 170 | case KLet(x: String, v: KVal, e: KExp) => | |
| 171 |     i"%$x = ${compile_val(v)}" ++ compile_exp(e)
 | |
| 172 |   case KIf(x, e1, e2) => {
 | |
| 649 | 173 |     val if_br = Fresh("if_br")
 | 
| 174 |     val else_br = Fresh("else_br")
 | |
| 175 | i"br i1 %$x, label %$if_br, label %$else_br" ++ | |
| 176 | l"\n$if_br" ++ | |
| 653 | 177 | compile_exp(e1) ++ | 
| 649 | 178 | l"\n$else_br" ++ | 
| 653 | 179 | compile_exp(e2) | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 180 | } | 
| 653 | 181 | } | 
| 182 | ||
| 183 | /*  case Write(a1) => {
 | |
| 221 
824ffbf66ab4
added fun tail
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
220diff
changeset | 184 | compile_exp(a1, env) ++ | 
| 625 | 185 | i"dup" ++ | 
| 186 | i"invokestatic XXX/XXX/write(I)V" | |
| 221 
824ffbf66ab4
added fun tail
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
220diff
changeset | 187 | } | 
| 653 | 188 | */ | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 189 | |
| 653 | 190 | |
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 191 | |
| 625 | 192 | // compile function for declarations and main | 
| 193 | def compile_decl(d: Decl) : String = d match {
 | |
| 649 | 194 |   case Def(name, args, body) => { 
 | 
| 195 |     m"define i32 @$name (${args.mkString("i32 %", ", i32 %", "")}) {" ++
 | |
| 653 | 196 | compile_exp(CPSi(body)) ++ | 
| 649 | 197 | m"}\n" | 
| 221 
824ffbf66ab4
added fun tail
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
220diff
changeset | 198 | } | 
| 649 | 199 |   case Main(body) => {
 | 
| 200 |     m"define i32 @main() {" ++
 | |
| 653 | 201 | compile_exp(CPSi(body)) ++ | 
| 649 | 202 | m"}\n" | 
| 221 
824ffbf66ab4
added fun tail
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
220diff
changeset | 203 | } | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 204 | } | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 205 | |
| 626 | 206 | // main compiler functions | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 207 | |
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 208 | def time_needed[T](i: Int, code: => T) = {
 | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 209 | val start = System.nanoTime() | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 210 | for (j <- 1 to i) code | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 211 | val end = System.nanoTime() | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 212 | (end - start)/(i * 1.0e9) | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 213 | } | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 214 | |
| 645 | 215 | def deserialise[T](fname: String) : Try[T] = {
 | 
| 216 | import scala.util.Using | |
| 217 |   Using(new ObjectInputStream(new FileInputStream(fname))) {
 | |
| 218 | in => in.readObject.asInstanceOf[T] | |
| 219 | } | |
| 644 | 220 | } | 
| 221 | ||
| 222 | def compile(class_name: String) : String = {
 | |
| 645 | 223 | val ast = deserialise[List[Decl]](class_name ++ ".prs").getOrElse(Nil) | 
| 653 | 224 | ast.map(compile_decl).mkString | 
| 626 | 225 | } | 
| 226 | ||
| 649 | 227 | /* | 
| 644 | 228 | def compile_to_file(class_name: String) = {
 | 
| 229 | val output = compile(class_name) | |
| 626 | 230 |   scala.tools.nsc.io.File(s"${class_name}.j").writeAll(output)
 | 
| 231 | } | |
| 232 | ||
| 645 | 233 | def compile_and_run(class_name: String) : Unit = {
 | 
| 644 | 234 | compile_to_file(class_name) | 
| 626 | 235 |   (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!!
 | 
| 625 | 236 |   println("Time: " + time_needed(2, (s"java ${class_name}/${class_name}").!))
 | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 237 | } | 
| 649 | 238 | */ | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 239 | |
| 626 | 240 | // some examples of .fun files | 
| 645 | 241 | //compile_to_file("fact")
 | 
| 242 | //compile_and_run("fact")
 | |
| 243 | //compile_and_run("defs")
 | |
| 244 | ||
| 644 | 245 | |
| 649 | 246 | def main(args: Array[String]) : Unit = | 
| 247 | println(compile(args(0))) | |
| 644 | 248 | |
| 249 | ||
| 250 | } |