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