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