progs/fun_llvm.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 01 Nov 2019 13:21:51 +0000
changeset 679 8fc109f36b78
parent 678 ff3b48da282c
child 701 681c36b2af27
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
//
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
     6
//     scala fun_llvm.scala fact
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     7
//
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
     8
//     scala fun_llvm.scala defs
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
     9
//
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    10
// this will generate a .ll file. You can interpret this file
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    11
// using lli.
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    12
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    13
// The optimiser can be invoked as
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    14
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    15
//      opt -O1 -S in_file.ll > out_file.ll
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    16
//      opt -O3 -S in_file.ll > out_file.ll
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    17
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    18
// The code produced for the various architectures can be obtains with
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    19
//   
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    20
//   llc -march=x86 -filetype=asm in_file.ll -o -
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    21
//   llc -march=arm -filetype=asm in_file.ll -o -  
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    22
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    23
// Producing an executable can be achieved by
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    24
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    25
//    llc -filetype=obj in_file.ll
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    26
//    gcc in_file.o -o a.out
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    27
//    ./a.out
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    28
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    29
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
    30
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    31
object Compiler {
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    33
import java.io._  
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    34
import scala.util._
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    35
import scala.sys.process._
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    36
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    37
// Abstract syntax trees for the Fun language
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    38
abstract class Exp extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    39
abstract class BExp extends Serializable 
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    40
abstract class Decl extends Serializable 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    41
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    42
case class Def(name: String, args: List[String], body: Exp) extends Decl
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    43
case class Main(e: Exp) extends Decl
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    44
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    45
case class Call(name: String, args: List[Exp]) extends Exp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    46
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    47
case class Write(e: Exp) extends Exp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    48
case class Var(s: String) extends Exp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    49
case class Num(i: Int) extends Exp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    50
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    51
case class Sequence(e1: Exp, e2: Exp) extends Exp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    52
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    53
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    54
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
// for generating new labels
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
var counter = -1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
def Fresh(x: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  counter += 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  x ++ "_" ++ counter.toString()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 657
diff changeset
    63
// Internal CPS language for FUN
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    64
abstract class KExp
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    65
abstract class KVal
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    66
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    67
case class KVar(s: String) extends KVal
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    68
case class KNum(i: Int) extends KVal
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
    69
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
    70
case class KCall(o: String, vrs: List[KVal]) extends KVal
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    71
case class KWrite(v: KVal) extends KVal
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    72
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    73
case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    74
  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
    75
}
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    76
case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    77
  override def toString = s"let $x = $e1 in \n$e2" 
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    78
}
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    79
case class KReturn(v: KVal) extends KExp
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    80
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
    81
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    82
// CPS translation from Exps to KExps using a
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
    83
// continuation k.
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    84
def CPS(e: Exp)(k: KVal => KExp) : KExp = e match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    85
  case Var(s) => k(KVar(s)) 
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    86
  case Num(i) => k(KNum(i))
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    87
  case Aop(o, e1, e2) => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    88
    val z = Fresh("tmp")
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    89
    CPS(e1)(y1 => 
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
    90
      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
    91
  }
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    92
  case If(Bop(o, b1, b2), e1, e2) => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    93
    val z = Fresh("tmp")
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    94
    CPS(b1)(y1 => 
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    95
      CPS(b2)(y2 => 
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
    96
        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
    97
  }
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    98
  case Call(name, args) => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    99
    def aux(args: List[Exp], vs: List[KVal]) : KExp = args match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   100
      case Nil => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   101
          val z = Fresh("tmp")
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   102
          KLet(z, KCall(name, vs), k(KVar(z)))
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
      case e::es => CPS(e)(y => aux(es, vs ::: List(y)))
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   105
    }
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   106
    aux(args, Nil)
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   107
  }
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   108
  case Sequence(e1, e2) => 
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   109
    CPS(e1)(_ => CPS(e2)(y2 => k(y2)))
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   110
  case Write(e) => {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   111
    val z = Fresh("tmp")
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   112
    CPS(e)(y => KLet(z, KWrite(y), k(KVar(z))))
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   113
  }
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   114
}   
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   115
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   116
//initial continuation
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   117
def CPSi(e: Exp) = CPS(e)(KReturn)
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   118
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   119
// some testcases
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   120
val e1 = Aop("*", Var("a"), Num(3))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   121
CPSi(e1)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   122
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   123
val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   124
CPSi(e2)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   125
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   126
val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   127
CPSi(e3)
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   128
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   129
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
   130
CPSi(e4)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   131
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   132
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
   133
CPSi(e5)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   134
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   135
val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   136
CPSi(e6)
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   137
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   138
val e7 = Call("foo", List(Num(3)))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   139
CPSi(e7)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   140
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   141
val e8 = Call("foo", List(Num(3), Num(4), Aop("+", Num(5), Num(6))))
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   142
CPSi(e8)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   143
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   144
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
   145
CPSi(e9)
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   146
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   147
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
   148
CPSi(e)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   149
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   150
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   151
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   152
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   153
// convenient string interpolations 
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   154
// for instructions, labels and methods
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   155
import scala.language.implicitConversions
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   156
import scala.language.reflectiveCalls
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   157
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   158
implicit def sring_inters(sc: StringContext) = new {
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   159
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   160
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   161
    def m(args: Any*): String = sc.s(args:_*) ++ "\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   162
}
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   163
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   164
// mathematical and boolean operations
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   165
def compile_op(op: String) = op match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   166
  case "+" => "add i32 "
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   167
  case "*" => "mul i32 "
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   168
  case "-" => "sub i32 "
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   169
  case "/" => "sdiv i32 "
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   170
  case "%" => "srem i32 "
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   171
  case "==" => "icmp eq i32 "
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   172
  case "<=" => "icmp sle i32 "    // signed less or equal
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   173
  case "<" => "icmp slt i32 "     // signed less than
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   174
}
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   176
def compile_val(v: KVal) : String = v match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   177
  case KNum(i) => s"$i"
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   178
  case KVar(s) => s"%$s"
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   179
  case Kop(op, x1, x2) => 
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   180
    s"${compile_op(op)} ${compile_val(x1)}, ${compile_val(x2)}"
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   181
  case KCall(x1, args) => 
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   182
    s"call i32 @$x1 (${args.map(compile_val).mkString("i32 ", ", i32 ", "")})"
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   183
  case KWrite(x1) =>
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   184
    s"call i32 @printInt (i32 ${compile_val(x1)})"
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   185
}
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   186
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   187
// compile K expressions
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   188
def compile_exp(a: KExp) : String = a match {
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   189
  case KReturn(v) =>
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   190
    i"ret i32 ${compile_val(v)}"
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   191
  case KLet(x: String, v: KVal, e: KExp) => 
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   192
    i"%$x = ${compile_val(v)}" ++ compile_exp(e)
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   193
  case KIf(x, e1, e2) => {
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   194
    val if_br = Fresh("if_branch")
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   195
    val else_br = Fresh("else_branch")
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   196
    i"br i1 %$x, label %$if_br, label %$else_br" ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   197
    l"\n$if_br" ++
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   198
    compile_exp(e1) ++
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   199
    l"\n$else_br" ++ 
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   200
    compile_exp(e2)
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
  }
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   202
}
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   203
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   204
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   205
val prelude = """
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   206
@.str = private constant [4 x i8] c"%d\0A\00"
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   207
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   208
declare i32 @printf(i8*, ...)
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   210
define i32 @printInt(i32 %x) {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   211
   %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
   212
   call i32 (i8*, ...) @printf(i8* %t0, i32 %x) 
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   213
   ret i32 %x
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   214
}
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   215
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   216
"""
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   217
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   219
// compile function for declarations and main
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   220
def compile_decl(d: Decl) : String = d match {
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   221
  case Def(name, args, body) => { 
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   222
    m"define i32 @$name (${args.mkString("i32 %", ", i32 %", "")}) {" ++
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   223
    compile_exp(CPSi(body)) ++
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   224
    m"}\n"
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   225
  }
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   226
  case Main(body) => {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   227
    m"define i32 @main() {" ++
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   228
    compile_exp(CPSi(body)) ++
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   229
    m"}\n"
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   230
  }
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   233
// main compiler functions
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
def time_needed[T](i: Int, code: => T) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
  val start = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
  for (j <- 1 to i) code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
  val end = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
  (end - start)/(i * 1.0e9)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   242
def deserialise[T](fname: String) : Try[T] = {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   243
  import scala.util.Using
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   244
  Using(new ObjectInputStream(new FileInputStream(fname))) {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   245
    in => in.readObject.asInstanceOf[T]
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   246
  }
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   247
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   248
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   249
def compile(fname: String) : String = {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   250
  val ast = deserialise[List[Decl]](fname ++ ".prs").getOrElse(Nil) 
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   251
  prelude ++ (ast.map(compile_decl).mkString)
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   252
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   253
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   254
def compile_to_file(fname: String) = {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   255
  val output = compile(fname)
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   256
  scala.tools.nsc.io.File(s"${fname}.ll").writeAll(output)
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   257
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   258
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   259
def compile_and_run(fname: String) : Unit = {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   260
  compile_to_file(fname)
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   261
  (s"llc -filetype=obj ${fname}.ll").!!
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   262
  (s"gcc ${fname}.o -o a.out").!!
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   263
  println("Time: " + time_needed(2, (s"./a.out").!))
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   266
// some examples of .fun files
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   267
//compile_to_file("fact")
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   268
//compile_and_run("fact")
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   269
//compile_and_run("defs")
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   270
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   271
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   272
def main(args: Array[String]) : Unit = 
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   273
   //println(compile(args(0)))
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   274
   compile_and_run(args(0))
657
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   275
}
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   276
657
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   277
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   278
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   279
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   280
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   281
/*
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   282
LLVM notes
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   283
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   284
Registers are places for data inside the CPU.
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   285
+ up to 10 times faster access than to main memory 
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   286
- expensive; typically just 32 of them in a 32-bit CPU
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   287
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   288
High-level view of x86
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   289
• Not a stack machine; no direct correspondence to operand stacks
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   290
• Arithmetics, etc. is done with values in registers
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   291
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   292
• Started as academic project at University of Illinois in 2002
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   293
• Now a large open source project with many contributors and a growing user base
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   294
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   295
Single Static Assignment (SSA) form
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   296
• Only one assignment in the program text to each variable
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   297
• But dynamically, this assignment can be executed many times
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   298
• Many stores to a memory location are allowed
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   299
• Also, Φ (phi) instructions can be used, in the beginning of a basic block
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   300
• Value is one of the arguments, depending on from which block control came to this block
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   301
• Register allocation tries to keep these variables in same real register
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   302
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   303
Why SSA form?
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   304
Many code optimizations can be done more efficiently
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   305
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   306
Function definition form
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   307
 define t @name(t1 x1, t2 x2, ..., tn xn) {
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   308
 l1: block1
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   309
 l2: block2
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   310
 ... 
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   311
 lm : blockm
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   312
 }
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   313
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   314
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   315
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   316
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   317
*/