| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Sat, 03 Dec 2022 21:58:47 +0000 | |
| changeset 902 | d516888e5878 | 
| parent 870 | 1ea379515c6d | 
| child 905 | d8f870aad77d | 
| 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 | // | 
| 813 | 4 | // | 
| 645 | 5 | // call with | 
| 6 | // | |
| 813 | 7 | // amm fun_llvm.sc main fact.fun | 
| 8 | // amm fun_llvm.sc main defs.fun | |
| 9 | // | |
| 10 | // or | |
| 11 | // | |
| 789 | 12 | // amm fun_llvm.sc write fact.fun | 
| 13 | // amm fun_llvm.sc write defs.fun | |
| 645 | 14 | // | 
| 813 | 15 | // this will generate an .ll file. | 
| 16 | // | |
| 17 | // or | |
| 654 | 18 | // | 
| 813 | 19 | // amm fun_llvm.sc run fact.fun | 
| 20 | // amm fun_llvm.sc run defs.fun | |
| 21 | // | |
| 22 | // | |
| 23 | // You can interpret an .ll file using lli, for example | |
| 24 | // | |
| 25 | // lli fact.ll | |
| 655 | 26 | // | 
| 27 | // The optimiser can be invoked as | |
| 28 | // | |
| 29 | // opt -O1 -S in_file.ll > out_file.ll | |
| 30 | // opt -O3 -S in_file.ll > out_file.ll | |
| 31 | // | |
| 813 | 32 | // The code produced for the various architectures can be obtain with | 
| 655 | 33 | // | 
| 34 | // llc -march=x86 -filetype=asm in_file.ll -o - | |
| 35 | // llc -march=arm -filetype=asm in_file.ll -o - | |
| 36 | // | |
| 37 | // Producing an executable can be achieved by | |
| 38 | // | |
| 39 | // llc -filetype=obj in_file.ll | |
| 40 | // gcc in_file.o -o a.out | |
| 41 | // ./a.out | |
| 42 | ||
| 645 | 43 | |
| 789 | 44 | import $file.fun_tokens, fun_tokens._ | 
| 45 | import $file.fun_parser, fun_parser._ | |
| 626 | 46 | |
| 47 | ||
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 48 | // for generating new labels | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 49 | var counter = -1 | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 50 | |
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 51 | def Fresh(x: String) = {
 | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 52 | counter += 1 | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 53 | x ++ "_" ++ counter.toString() | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 54 | } | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 55 | |
| 678 | 56 | // Internal CPS language for FUN | 
| 648 | 57 | abstract class KExp | 
| 653 | 58 | abstract class KVal | 
| 648 | 59 | |
| 653 | 60 | case class KVar(s: String) extends KVal | 
| 61 | case class KNum(i: Int) extends KVal | |
| 656 | 62 | case class Kop(o: String, v1: KVal, v2: KVal) extends KVal | 
| 653 | 63 | case class KCall(o: String, vrs: List[KVal]) extends KVal | 
| 655 | 64 | case class KWrite(v: KVal) extends KVal | 
| 649 | 65 | |
| 819 | 66 | case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
 | 
| 67 | override def toString = s"LET $x = $e1 in \n$e2" | |
| 649 | 68 | } | 
| 819 | 69 | case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
 | 
| 70 |   def pad(e: KExp) = e.toString.replaceAll("(?m)^", "  ")
 | |
| 71 | ||
| 72 | override def toString = | |
| 73 |      s"IF $x1\nTHEN\n${pad(e1)}\nELSE\n${pad(e2)}"
 | |
| 648 | 74 | } | 
| 653 | 75 | case class KReturn(v: KVal) extends KExp | 
| 648 | 76 | |
| 654 | 77 | |
| 655 | 78 | // CPS translation from Exps to KExps using a | 
| 654 | 79 | // continuation k. | 
| 653 | 80 | def CPS(e: Exp)(k: KVal => KExp) : KExp = e match {
 | 
| 81 | case Var(s) => k(KVar(s)) | |
| 82 | case Num(i) => k(KNum(i)) | |
| 83 |   case Aop(o, e1, e2) => {
 | |
| 84 |     val z = Fresh("tmp")
 | |
| 85 | CPS(e1)(y1 => | |
| 656 | 86 | CPS(e2)(y2 => KLet(z, Kop(o, y1, y2), k(KVar(z))))) | 
| 653 | 87 | } | 
| 88 |   case If(Bop(o, b1, b2), e1, e2) => {
 | |
| 89 |     val z = Fresh("tmp")
 | |
| 90 | CPS(b1)(y1 => | |
| 655 | 91 | CPS(b2)(y2 => | 
| 656 | 92 | KLet(z, Kop(o, y1, y2), KIf(z, CPS(e1)(k), CPS(e2)(k))))) | 
| 653 | 93 | } | 
| 94 |   case Call(name, args) => {
 | |
| 95 |     def aux(args: List[Exp], vs: List[KVal]) : KExp = args match {
 | |
| 96 |       case Nil => {
 | |
| 97 |           val z = Fresh("tmp")
 | |
| 98 | KLet(z, KCall(name, vs), k(KVar(z))) | |
| 99 | } | |
| 100 | case e::es => CPS(e)(y => aux(es, vs ::: List(y))) | |
| 648 | 101 | } | 
| 653 | 102 | aux(args, Nil) | 
| 103 | } | |
| 656 | 104 | case Sequence(e1, e2) => | 
| 679 | 105 | CPS(e1)(_ => CPS(e2)(y2 => k(y2))) | 
| 655 | 106 |   case Write(e) => {
 | 
| 107 |     val z = Fresh("tmp")
 | |
| 108 | CPS(e)(y => KLet(z, KWrite(y), k(KVar(z)))) | |
| 109 | } | |
| 653 | 110 | } | 
| 111 | ||
| 679 | 112 | //initial continuation | 
| 653 | 113 | def CPSi(e: Exp) = CPS(e)(KReturn) | 
| 114 | ||
| 819 | 115 | //some testcases: | 
| 116 | // numbers and vars | |
| 117 | println(CPSi(Num(1)).toString) | |
| 118 | println(CPSi(Var("z")).toString)
 | |
| 119 | ||
| 120 | // a * 3 | |
| 653 | 121 | val e1 = Aop("*", Var("a"), Num(3))
 | 
| 819 | 122 | println(CPSi(e1).toString) | 
| 653 | 123 | |
| 819 | 124 | // (a * 3) + 4 | 
| 653 | 125 | val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
 | 
| 819 | 126 | println(CPSi(e2).toString) | 
| 653 | 127 | |
| 819 | 128 | // 2 + (a * 3) | 
| 653 | 129 | val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
 | 
| 819 | 130 | println(CPSi(e3).toString) | 
| 648 | 131 | |
| 819 | 132 | //(1 - 2) + (a * 3) | 
| 653 | 133 | val e4 = Aop("+", Aop("-", Num(1), Num(2)), Aop("*", Var("a"), Num(3)))
 | 
| 819 | 134 | println(CPSi(e4).toString) | 
| 653 | 135 | |
| 819 | 136 | // 3 + 4 ; 1 * 7 | 
| 137 | val es = Sequence(Aop("+", Num(3), Num(4)),
 | |
| 138 |                   Aop("*", Num(1), Num(7)))
 | |
| 139 | println(CPSi(es).toString) | |
| 140 | ||
| 141 | // if (1 == 1) then 3 else 4 | |
| 653 | 142 | val e5 = If(Bop("==", Num(1), Num(1)), Num(3), Num(4))
 | 
| 819 | 143 | println(CPSi(e5).toString) | 
| 653 | 144 | |
| 819 | 145 | // if (1 == 1) then 3 + 7 else 4 * 2 | 
| 146 | val ei = If(Bop("==", Num(1), Num(1)), 
 | |
| 147 |                 Aop("+", Num(3), Num(7)),
 | |
| 148 |                 Aop("*", Num(4), Num(2)))
 | |
| 149 | println(CPSi(ei).toString) | |
| 150 | ||
| 151 | ||
| 152 | // if (10 != 10) then e5 else 40 | |
| 653 | 153 | val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
 | 
| 819 | 154 | println(CPSi(e6).toString) | 
| 648 | 155 | |
| 653 | 156 | |
| 819 | 157 | // foo(3) | 
| 158 | val e7 = Call("foo", List(Num(3)))
 | |
| 159 | println(CPSi(e7).toString) | |
| 160 | ||
| 161 | // foo(3 * 1, 4, 5 + 6) | |
| 162 | val e8 = Call("foo", List(Aop("*", Num(3), Num(1)), 
 | |
| 163 | Num(4), | |
| 164 |                           Aop("+", Num(5), Num(6))))
 | |
| 165 | println(CPSi(e8).toString) | |
| 653 | 166 | |
| 819 | 167 | // a * 3 ; b + 6 | 
| 168 | val e9 = Sequence(Aop("*", Var("a"), Num(3)), 
 | |
| 169 |                   Aop("+", Var("b"), Num(6)))
 | |
| 170 | println(CPSi(e9).toString) | |
| 649 | 171 | |
| 819 | 172 | |
| 173 | val e10 = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
 | |
| 174 | println(CPSi(e10).toString) | |
| 175 | ||
| 653 | 176 | |
| 648 | 177 | |
| 178 | ||
| 179 | ||
| 625 | 180 | // convenient string interpolations | 
| 181 | // for instructions, labels and methods | |
| 182 | import scala.language.implicitConversions | |
| 183 | import scala.language.reflectiveCalls | |
| 184 | ||
| 185 | implicit def sring_inters(sc: StringContext) = new {
 | |
| 186 | def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" | |
| 187 | def l(args: Any*): String = sc.s(args:_*) ++ ":\n" | |
| 188 | def m(args: Any*): String = sc.s(args:_*) ++ "\n" | |
| 189 | } | |
| 190 | ||
| 656 | 191 | // mathematical and boolean operations | 
| 653 | 192 | def compile_op(op: String) = op match {
 | 
| 193 | case "+" => "add i32 " | |
| 194 | case "*" => "mul i32 " | |
| 195 | case "-" => "sub i32 " | |
| 656 | 196 | case "/" => "sdiv i32 " | 
| 197 | case "%" => "srem i32 " | |
| 653 | 198 | case "==" => "icmp eq i32 " | 
| 813 | 199 | case "<=" => "icmp sle i32 " // signed less or equal | 
| 200 | case "<" => "icmp slt i32 " // signed less than | |
| 653 | 201 | } | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 202 | |
| 813 | 203 | // compile K values | 
| 653 | 204 | def compile_val(v: KVal) : String = v match {
 | 
| 205 | case KNum(i) => s"$i" | |
| 206 | case KVar(s) => s"%$s" | |
| 656 | 207 | case Kop(op, x1, x2) => | 
| 653 | 208 |     s"${compile_op(op)} ${compile_val(x1)}, ${compile_val(x2)}"
 | 
| 209 | case KCall(x1, args) => | |
| 210 |     s"call i32 @$x1 (${args.map(compile_val).mkString("i32 ", ", i32 ", "")})"
 | |
| 655 | 211 | case KWrite(x1) => | 
| 212 |     s"call i32 @printInt (i32 ${compile_val(x1)})"
 | |
| 653 | 213 | } | 
| 648 | 214 | |
| 649 | 215 | // compile K expressions | 
| 216 | def compile_exp(a: KExp) : String = a match {
 | |
| 653 | 217 | case KReturn(v) => | 
| 218 |     i"ret i32 ${compile_val(v)}"
 | |
| 219 | case KLet(x: String, v: KVal, e: KExp) => | |
| 220 |     i"%$x = ${compile_val(v)}" ++ compile_exp(e)
 | |
| 221 |   case KIf(x, e1, e2) => {
 | |
| 679 | 222 |     val if_br = Fresh("if_branch")
 | 
| 223 |     val else_br = Fresh("else_branch")
 | |
| 649 | 224 | i"br i1 %$x, label %$if_br, label %$else_br" ++ | 
| 225 | l"\n$if_br" ++ | |
| 653 | 226 | compile_exp(e1) ++ | 
| 649 | 227 | l"\n$else_br" ++ | 
| 653 | 228 | compile_exp(e2) | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 229 | } | 
| 653 | 230 | } | 
| 231 | ||
| 655 | 232 | |
| 233 | val prelude = """ | |
| 234 | @.str = private constant [4 x i8] c"%d\0A\00" | |
| 235 | ||
| 236 | declare i32 @printf(i8*, ...) | |
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 237 | |
| 655 | 238 | define i32 @printInt(i32 %x) {
 | 
| 239 | %t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0 | |
| 679 | 240 | call i32 (i8*, ...) @printf(i8* %t0, i32 %x) | 
| 655 | 241 | ret i32 %x | 
| 242 | } | |
| 243 | ||
| 244 | """ | |
| 653 | 245 | |
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 246 | |
| 625 | 247 | // compile function for declarations and main | 
| 248 | def compile_decl(d: Decl) : String = d match {
 | |
| 649 | 249 |   case Def(name, args, body) => { 
 | 
| 250 |     m"define i32 @$name (${args.mkString("i32 %", ", i32 %", "")}) {" ++
 | |
| 653 | 251 | compile_exp(CPSi(body)) ++ | 
| 649 | 252 | m"}\n" | 
| 221 
824ffbf66ab4
added fun tail
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
220diff
changeset | 253 | } | 
| 649 | 254 |   case Main(body) => {
 | 
| 255 |     m"define i32 @main() {" ++
 | |
| 789 | 256 | compile_exp(CPS(body)(_ => KReturn(KNum(0)))) ++ | 
| 649 | 257 | m"}\n" | 
| 221 
824ffbf66ab4
added fun tail
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
220diff
changeset | 258 | } | 
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 259 | } | 
| 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 260 | |
| 813 | 261 | |
| 626 | 262 | // main compiler functions | 
| 813 | 263 | def compile(prog: List[Decl]) : String = | 
| 789 | 264 | prelude ++ (prog.map(compile_decl).mkString) | 
| 265 | ||
| 220 
141041fc76b5
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 266 | |
| 869 
16247acc4b0e
changed os-lib as a replacement for ammonite-ops
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
819diff
changeset | 267 | // pre-2.5.0 ammonite | 
| 
16247acc4b0e
changed os-lib as a replacement for ammonite-ops
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
819diff
changeset | 268 | // import ammonite.ops._ | 
| 
16247acc4b0e
changed os-lib as a replacement for ammonite-ops
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
819diff
changeset | 269 | |
| 
16247acc4b0e
changed os-lib as a replacement for ammonite-ops
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
819diff
changeset | 270 | // post 2.5.0 ammonite | 
| 870 
1ea379515c6d
ivy import not needed
 Christian Urban <christian.urban@kcl.ac.uk> parents: 
869diff
changeset | 271 | // import os._ | 
| 813 | 272 | |
| 273 | ||
| 789 | 274 | @main | 
| 813 | 275 | def main(fname: String) = {
 | 
| 789 | 276 | val path = os.pwd / fname | 
| 277 |     val file = fname.stripSuffix("." ++ path.ext)
 | |
| 278 | val tks = tokenise(os.read(path)) | |
| 279 | val ast = parse_tks(tks) | |
| 813 | 280 | println(compile(ast)) | 
| 644 | 281 | } | 
| 282 | ||
| 789 | 283 | @main | 
| 284 | def write(fname: String) = {
 | |
| 285 | val path = os.pwd / fname | |
| 286 |     val file = fname.stripSuffix("." ++ path.ext)
 | |
| 287 | val tks = tokenise(os.read(path)) | |
| 288 | val ast = parse_tks(tks) | |
| 813 | 289 | val code = compile(ast) | 
| 789 | 290 | os.write.over(os.pwd / (file ++ ".ll"), code) | 
| 626 | 291 | } | 
| 292 | ||
| 789 | 293 | @main | 
| 294 | def run(fname: String) = {
 | |
| 295 | val path = os.pwd / fname | |
| 296 |     val file = fname.stripSuffix("." ++ path.ext)
 | |
| 813 | 297 | write(fname) | 
| 789 | 298 |     os.proc("llc", "-filetype=obj", file ++ ".ll").call()
 | 
| 813 | 299 |     os.proc("gcc", file ++ ".o", "-o", file ++ ".bin").call()
 | 
| 300 | os.proc(os.pwd / (file ++ ".bin")).call(stdout = os.Inherit) | |
| 301 | println(s"done.") | |
| 657 | 302 | } | 
| 644 | 303 | |
| 657 | 304 | |
| 305 | ||
| 306 | ||
| 819 | 307 | // CPS functions | 
| 308 | /* | |
| 657 | 309 | |
| 819 | 310 | def fact(n: Int) : Int = | 
| 311 | if (n == 0) 1 else n * fact(n - 1) | |
| 312 | ||
| 313 | fact(6) | |
| 314 | ||
| 315 | def factT(n: Int, acc: Int) : Int = | |
| 316 | if (n == 0) acc else factT(n - 1, acc * n) | |
| 317 | ||
| 318 | factT(6, 1) | |
| 319 | ||
| 320 | def factC(n: Int, ret: Int => Int) : Int = {
 | |
| 321 | if (n == 0) ret(1) | |
| 322 | else factC(n - 1, x => ret(x * n)) | |
| 323 | } | |
| 324 | ||
| 325 | factC(6, x => x) | |
| 326 | factC(6, x => {println(s"The final Result is $x") ; 0})
 | |
| 327 | factC(6, _ + 1) | |
| 328 | ||
| 329 | def fibC(n: Int, ret: Int => Int) : Int = {
 | |
| 330 | if (n == 0 || n == 1) ret(1) | |
| 331 | else fibC(n - 1, x => fibC(n - 2, y => ret(x + y))) | |
| 332 | } | |
| 333 | ||
| 334 | fibC(10, x => {println(s"Result: $x") ; 1})
 | |
| 335 | ||
| 336 | ||
| 337 | */ |