progs/fun_llvm.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 08 Oct 2019 21:12:52 +0100
changeset 649 e83afb44f276
parent 648 36379b038438
child 650 3031e3379ea3
permissions -rw-r--r--
updated
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
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    59
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    60
case class KVar(s: String) extends KExp
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    61
case class KNum(i: Int) extends KExp
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    62
case class KAop(o: String, x1: String, x2: String) extends KExp
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    63
case class KIfeq(x1: String, x2: String, e1: KExp, e2: KExp) extends KExp {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    64
  override def toString = s"KIf $x1 == $x2 \nIF\n$e1\nELSE\n$e2"
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    65
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    66
}
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    67
case class KCall(o: String, vrs: List[String]) extends KExp
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    68
case class KLet(x: String, e1: KExp, e2: KExp) extends KExp {
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    69
  override def toString = s"let $x = $e1 in \n$e2" 
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    70
}
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    71
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    72
def K(e: Exp) : KExp = e match {
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    73
  case Var(s) => KVar(s) 
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    74
  case Num(i) => KNum(i)
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    75
  case Aop(o, a1, a2) => {
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    76
    val x1 = Fresh("tmp")
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    77
    val x2 = Fresh("tmp") 
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    78
    KLet(x1, K(a1), KLet(x2, K(a2), KAop(o, x1, x2)))
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    79
  } 
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    80
  case Call(name: String, args: List[Exp]) => {
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    81
    val args_new = args.map{a => (Fresh("tmp"), K(a))}
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    82
    def aux(as: List[(String, KExp)]) : KExp = as match {
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    83
      case Nil => KCall(name, args_new.map(_._1))
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    84
      case (x, a)::rest => KLet(x, a, aux(rest))
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    85
    }
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    86
    aux(args_new)
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    87
  }
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    88
  case If(Bop("==", b1, b2), e1, e2) => {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    89
    val x1 = Fresh("tmp")
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    90
    val x2 = Fresh("tmp") 
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    91
    KLet(x1, K(b1), KLet(x2, K(b2), KIfeq(x1, x2, K(e1), K(e2))))
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    92
  }
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    93
}
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    94
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    95
def Denest(e: KExp) : KExp = e match {
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    96
  case KLet(xt, e1, e2) => {
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    97
    def insert(e: KExp) : KExp = e match {
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    98
      case KLet(yt, e3, e4) => KLet(yt, e3, insert(e4))
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    99
      case e => KLet(xt, e, Denest(e2))
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   100
    }
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   101
    insert(Denest(e1))  
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   102
  }
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   103
  case KIfeq(x1, x2, e1, e2) =>  KIfeq(x1, x2, Denest(e1), Denest(e2))
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   104
  case _ => e
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   105
}
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   106
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   107
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   108
val e = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   109
println(K(e))
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   110
println()
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   111
println(Denest(K(e)))
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   112
println()
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   113
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   114
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   115
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   116
// convenient string interpolations 
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   117
// for instructions, labels and methods
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   118
import scala.language.implicitConversions
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   119
import scala.language.reflectiveCalls
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   120
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   121
implicit def sring_inters(sc: StringContext) = new {
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   122
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   123
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   124
    def m(args: Any*): String = sc.s(args:_*) ++ "\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   125
}
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   126
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   128
type Env = Map[String, Int]
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   129
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   130
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   131
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   132
// compile K expressions
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   133
def compile_exp(a: KExp) : String = a match {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   134
  case KNum(i) => s"?$i?"
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   135
  case KVar(s) => s"?$s?"
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   136
  case KAop("+", x1, x2) => s"add i32 %$x1, i32 %$x2"
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   137
  case KAop("-", x1, x2) => s"sub i32 %$x1, i32 %$x2"
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   138
  case KAop("*", x1, x2) => s"mul i32 %$x1, i32 %$x2"
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   139
  case KLet(x: String, e1: KExp, e2: KExp) => {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   140
    val is1 = compile_exp(e1)
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   141
    val is2 = compile_exp(e2)
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   142
    i"%$x = $is1" ++ is2
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   143
  }
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   144
  case KIfeq(x1, x2, a1, a2) => {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   145
    val if_br = Fresh("if_br")
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   146
    val else_br = Fresh("else_br")
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   147
    val x = Fresh("tmp")
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   148
    i"%$x = icmp eq i32 %$x1, i32 %$x2" ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   149
    i"br i1 %$x, label %$if_br, label %$else_br" ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   150
    l"\n$if_br" ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   151
    compile_exp(a1) ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   152
    l"\n$else_br" ++ 
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   153
    compile_exp(a2)
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   154
  }
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   155
  case KCall(x1, args) => {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   156
    s"Call $x1 ($args)"
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
  }
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   158
/*
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   159
  case Call(name, args) => {
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   160
    val is = "I" * args.length
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   161
    args.map(a => compile_exp(a, env)).mkString ++
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   162
    i"invokestatic XXX/XXX/$name($is)I"
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   163
  }
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   164
  case Sequence(a1, a2) => {
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   165
    compile_exp(a1, env) ++ i"pop" ++ compile_exp(a2, env)
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   166
  }
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   167
  case Write(a1) => {
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   168
    compile_exp(a1, env) ++
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   169
    i"dup" ++
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   170
    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
   171
  }
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   172
  */
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   175
/*
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   176
// compile boolean expressions
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   177
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   178
  case Bop("==", a1, a2) => 
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   179
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpne $jmp"
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
  case Bop("!=", a1, a2) => 
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   181
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpeq $jmp"
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
  case Bop("<", a1, a2) => 
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   183
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpge $jmp"
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   184
  case Bop("<=", a1, a2) => 
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   185
    compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpgt $jmp"
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
}
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   187
*/
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   189
// compile function for declarations and main
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   190
def compile_decl(d: Decl) : String = d match {
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   191
  case Def(name, args, body) => { 
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   192
    println(s"DEF\n $name ($args) = \nBODY:")
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   193
    println(Denest(K(body)))
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   194
    println()
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   195
    counter = -1
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   196
    m"define i32 @$name (${args.mkString("i32 %", ", i32 %", "")}) {" ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   197
    compile_exp(Denest(K(body))) ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   198
    m"}\n"
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   199
  }
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   200
  case Main(body) => {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   201
    m"define i32 @main() {" ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   202
    compile_exp(Denest(K(body))) ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   203
    i"ret i32 0" ++
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
  }
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   208
// main compiler functions
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
def time_needed[T](i: Int, code: => T) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
  val start = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
  for (j <- 1 to i) code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
  val end = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
  (end - start)/(i * 1.0e9)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   217
def deserialise[T](fname: String) : Try[T] = {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   218
  import scala.util.Using
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   219
  Using(new ObjectInputStream(new FileInputStream(fname))) {
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   220
    in => in.readObject.asInstanceOf[T]
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   221
  }
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   222
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   223
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   224
def compile(class_name: String) : String = {
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   225
  val ast = deserialise[List[Decl]](class_name ++ ".prs").getOrElse(Nil) 
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   226
  println(ast(0).toString ++ "\n")
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   227
  val instructions = List(ast(0), ast(2)).map(compile_decl).mkString
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   228
  instructions
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   229
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   230
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   231
/*
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   232
def compile_to_file(class_name: String) = {
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   233
  val output = compile(class_name)
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   234
  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
   235
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   236
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   237
def compile_and_run(class_name: String) : Unit = {
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   238
  compile_to_file(class_name)
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   239
  (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
   240
  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
   241
}
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   242
*/
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   244
// some examples of .fun files
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   245
//compile_to_file("fact")
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   246
//compile_and_run("fact")
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   247
//compile_and_run("defs")
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
   248
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   249
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   250
def main(args: Array[String]) : Unit = 
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   251
   println(compile(args(0)))
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   252
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   253
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   254
}