progs/fun/fun_llvm.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 25 Oct 2020 01:25:01 +0000
changeset 789 f0696713177b
parent 734 progs/fun/fun_llvm.scala@5d860ff01938
child 813 059f970287d1
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
     1
// A Small LLVM Compiler for a Simple Functional Language
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
     2
// (includes an external lexer and parser)
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     3
//
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     4
// call with 
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     5
//
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     6
//     amm fun_llvm.sc write fact.fun
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     7
//
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
     8
//     amm fun_llvm.sc write defs.fun
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     9
//
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    10
// this will generate a .ll file. Other options are compile and run.
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
    11
//
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    12
// You can interpret an .ll file using lli.
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    13
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    14
// The optimiser can be invoked as
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    15
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    16
//      opt -O1 -S in_file.ll > out_file.ll
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    17
//      opt -O3 -S in_file.ll > out_file.ll
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    18
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    19
// The code produced for the various architectures can be obtains with
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    20
//   
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    21
//   llc -march=x86 -filetype=asm in_file.ll -o -
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    22
//   llc -march=arm -filetype=asm in_file.ll -o -  
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    23
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    24
// Producing an executable can be achieved by
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    25
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    26
//    llc -filetype=obj in_file.ll
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    27
//    gcc in_file.o -o a.out
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    28
//    ./a.out
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    29
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    30
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    31
import $file.fun_tokens, fun_tokens._
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    32
import $file.fun_parser, fun_parser._ 
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    33
import scala.util._
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    34
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    35
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
// for generating new labels
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
var counter = -1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
def Fresh(x: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  counter += 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  x ++ "_" ++ counter.toString()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 657
diff changeset
    44
// Internal CPS language for FUN
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    45
abstract class KExp
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    46
abstract class KVal
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    47
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    48
case class KVar(s: String) extends KVal
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    49
case class KNum(i: Int) extends KVal
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
    50
case class Kop(o: String, v1: KVal, v2: KVal) extends KVal
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    51
case class KCall(o: String, vrs: List[KVal]) extends KVal
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    52
case class KWrite(v: KVal) extends KVal
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    53
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    54
case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    55
  override def toString = s"KIf $x1\nIF\n$e1\nELSE\n$e2"
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    56
}
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    57
case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    58
  override def toString = s"let $x = $e1 in \n$e2" 
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    59
}
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    60
case class KReturn(v: KVal) extends KExp
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    61
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
    62
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    63
// CPS translation from Exps to KExps using a
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
    64
// continuation k.
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    65
def CPS(e: Exp)(k: KVal => KExp) : KExp = e match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    66
  case Var(s) => k(KVar(s)) 
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    67
  case Num(i) => k(KNum(i))
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    68
  case Aop(o, e1, e2) => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    69
    val z = Fresh("tmp")
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    70
    CPS(e1)(y1 => 
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
    71
      CPS(e2)(y2 => KLet(z, Kop(o, y1, y2), k(KVar(z)))))
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    72
  }
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    73
  case If(Bop(o, b1, b2), e1, e2) => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    74
    val z = Fresh("tmp")
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    75
    CPS(b1)(y1 => 
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    76
      CPS(b2)(y2 => 
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
    77
        KLet(z, Kop(o, y1, y2), KIf(z, CPS(e1)(k), CPS(e2)(k)))))
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    78
  }
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    79
  case Call(name, args) => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    80
    def aux(args: List[Exp], vs: List[KVal]) : KExp = args match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    81
      case Nil => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    82
          val z = Fresh("tmp")
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    83
          KLet(z, KCall(name, vs), k(KVar(z)))
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    84
      }
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    85
      case e::es => CPS(e)(y => aux(es, vs ::: List(y)))
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    86
    }
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    87
    aux(args, Nil)
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    88
  }
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
    89
  case Sequence(e1, e2) => 
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
    90
    CPS(e1)(_ => CPS(e2)(y2 => k(y2)))
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    91
  case Write(e) => {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    92
    val z = Fresh("tmp")
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    93
    CPS(e)(y => KLet(z, KWrite(y), k(KVar(z))))
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    94
  }
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    95
}   
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    96
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
    97
//initial continuation
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    98
def CPSi(e: Exp) = CPS(e)(KReturn)
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    99
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   100
// some testcases
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   101
val e1 = Aop("*", Var("a"), Num(3))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   102
CPSi(e1)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   103
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   104
val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   105
CPSi(e2)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   106
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   107
val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   108
CPSi(e3)
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   109
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   110
val e4 = Aop("+", Aop("-", Num(1), Num(2)), Aop("*", Var("a"), Num(3)))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   111
CPSi(e4)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   112
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   113
val e5 = If(Bop("==", Num(1), Num(1)), Num(3), Num(4))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   114
CPSi(e5)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   115
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   116
val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   117
CPSi(e6)
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   118
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   119
val e7 = Call("foo", List(Num(3)))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   120
CPSi(e7)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   121
705
bfc8703b1527 updated
Christian Urban <urbanc@in.tum.de>
parents: 701
diff changeset
   122
val e8 = Call("foo", List(Aop("*", Num(3), Num(1)), Num(4), Aop("+", Num(5), Num(6))))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   123
CPSi(e8)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   124
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   125
val e9 = Sequence(Aop("*", Var("a"), Num(3)), Aop("+", Var("b"), Num(6)))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   126
CPSi(e9)
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   127
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   128
val e = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   129
CPSi(e)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   130
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   131
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   132
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   133
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   134
// convenient string interpolations 
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   135
// for instructions, labels and methods
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   136
import scala.language.implicitConversions
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   137
import scala.language.reflectiveCalls
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   138
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   139
implicit def sring_inters(sc: StringContext) = new {
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   140
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   141
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   142
    def m(args: Any*): String = sc.s(args:_*) ++ "\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   143
}
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   144
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   145
// mathematical and boolean operations
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   146
def compile_op(op: String) = op match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   147
  case "+" => "add i32 "
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   148
  case "*" => "mul i32 "
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   149
  case "-" => "sub i32 "
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   150
  case "/" => "sdiv i32 "
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   151
  case "%" => "srem i32 "
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   152
  case "==" => "icmp eq i32 "
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   153
  case "<=" => "icmp sle i32 "    // signed less or equal
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   154
  case "<" => "icmp slt i32 "     // signed less than
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   155
}
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   157
def compile_val(v: KVal) : String = v match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   158
  case KNum(i) => s"$i"
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   159
  case KVar(s) => s"%$s"
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   160
  case Kop(op, x1, x2) => 
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   161
    s"${compile_op(op)} ${compile_val(x1)}, ${compile_val(x2)}"
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   162
  case KCall(x1, args) => 
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   163
    s"call i32 @$x1 (${args.map(compile_val).mkString("i32 ", ", i32 ", "")})"
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   164
  case KWrite(x1) =>
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   165
    s"call i32 @printInt (i32 ${compile_val(x1)})"
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   166
}
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   167
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   168
// compile K expressions
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   169
def compile_exp(a: KExp) : String = a match {
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   170
  case KReturn(v) =>
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   171
    i"ret i32 ${compile_val(v)}"
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   172
  case KLet(x: String, v: KVal, e: KExp) => 
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   173
    i"%$x = ${compile_val(v)}" ++ compile_exp(e)
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   174
  case KIf(x, e1, e2) => {
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   175
    val if_br = Fresh("if_branch")
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   176
    val else_br = Fresh("else_branch")
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   177
    i"br i1 %$x, label %$if_br, label %$else_br" ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   178
    l"\n$if_br" ++
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   179
    compile_exp(e1) ++
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   180
    l"\n$else_br" ++ 
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   181
    compile_exp(e2)
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
  }
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   183
}
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   184
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   185
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   186
val prelude = """
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   187
@.str = private constant [4 x i8] c"%d\0A\00"
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   188
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   189
declare i32 @printf(i8*, ...)
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   191
define i32 @printInt(i32 %x) {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   192
   %t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   193
   call i32 (i8*, ...) @printf(i8* %t0, i32 %x) 
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   194
   ret i32 %x
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   195
}
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   196
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   197
"""
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   198
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   200
// compile function for declarations and main
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   201
def compile_decl(d: Decl) : String = d match {
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   202
  case Def(name, args, body) => { 
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   203
    m"define i32 @$name (${args.mkString("i32 %", ", i32 %", "")}) {" ++
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   204
    compile_exp(CPSi(body)) ++
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   205
    m"}\n"
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   206
  }
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   207
  case Main(body) => {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   208
    m"define i32 @main() {" ++
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   209
    compile_exp(CPS(body)(_ => KReturn(KNum(0)))) ++
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   210
    m"}\n"
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   211
  }
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   214
// main compiler functions
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   216
def compile_prog(prog: List[Decl]) : String = 
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   217
  prelude ++ (prog.map(compile_decl).mkString)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   218
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   220
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   221
def compile(fname: String) = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   222
    val path = os.pwd / fname
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   223
    val file = fname.stripSuffix("." ++ path.ext)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   224
    val tks = tokenise(os.read(path))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   225
    val ast = parse_tks(tks)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   226
    println(compile_prog(ast))
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   227
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   228
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   229
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   230
def write(fname: String) = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   231
    val path = os.pwd / fname
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   232
    val file = fname.stripSuffix("." ++ path.ext)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   233
    val tks = tokenise(os.read(path))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   234
    val ast = parse_tks(tks)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   235
    val code = compile_prog(ast)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   236
    os.write.over(os.pwd / (file ++ ".ll"), code)
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   237
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   238
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   239
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   240
def run(fname: String) = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   241
    val path = os.pwd / fname
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   242
    val file = fname.stripSuffix("." ++ path.ext)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   243
    val tks = tokenise(os.read(path))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   244
    val ast = parse_tks(tks)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   245
    val code = compile_prog(ast)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   246
    os.write.over(os.pwd / (file ++ ".ll"), code)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   247
    os.proc("llc", "-filetype=obj", file ++ ".ll").call()
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   248
    os.proc("gcc", file ++ ".o", "-o", file).call()
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   249
    print(os.proc(os.pwd / file).call().out.string)
657
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   250
}
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   251
657
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   252
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   253
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   254
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   255