progs/fun/fun_llvm.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:59:32 +0000
changeset 976 e9eac62928f5
parent 961 c0600f8b6427
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
     1
// A Small LLVM Compiler for a Simple Functional Language
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
     2
// (includes an external lexer and parser)
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     3
//
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     4
//
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     5
// call with 
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
     6
//
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     7
//     amm fun_llvm.sc main fact.fun
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     8
//     amm fun_llvm.sc main defs.fun
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
     9
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    10
// or
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    11
//
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    12
//     amm fun_llvm.sc write fact.fun
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
    13
//     amm fun_llvm.sc write defs.fun
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    14
//
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    15
//       this will generate an .ll file. 
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    16
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    17
// or 
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
    18
//
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    19
//     amm fun_llvm.sc run fact.fun
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    20
//     amm fun_llvm.sc run defs.fun
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    21
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    22
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    23
// You can interpret an .ll file using lli, for example
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    24
//
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    25
//      lli fact.ll
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    26
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    27
// The optimiser can be invoked as
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    28
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    29
//      opt -O1 -S in_file.ll > out_file.ll
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    30
//      opt -O3 -S in_file.ll > out_file.ll
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    31
//
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
    32
// The code produced for the various architectures can be obtain with
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    33
//   
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    34
//   llc -march=x86 -filetype=asm in_file.ll -o -
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    35
//   llc -march=arm -filetype=asm in_file.ll -o -  
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    36
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    37
// Producing an executable can be achieved by
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    38
//
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    39
//    llc -filetype=obj in_file.ll
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    40
//    gcc in_file.o -o a.out
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    41
//    ./a.out
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    42
645
30943d5491b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 644
diff changeset
    43
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    44
//> using toolkit 0.5.0
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    45
// > using file fun_tokens.scala
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    46
// > using file fun_parser.scala
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    47
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    48
//import $file.fun_tokens, fun_tokens._
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    49
//import $file.fun_parser, fun_parser._ 
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    50
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    51
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
// for generating new labels
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
var counter = -1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
def Fresh(x: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  counter += 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  x ++ "_" ++ counter.toString()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents: 657
diff changeset
    60
// Internal CPS language for FUN
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    61
abstract class KExp
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    62
abstract class KVal
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    63
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    64
case class KVar(s: String) extends KVal
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    65
case class KNum(i: Int) extends KVal
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
    66
case class Kop(o: String, v1: KVal, v2: KVal) extends KVal
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    67
case class KCall(o: String, vrs: List[KVal]) extends KVal
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
    68
case class KWrite(v: KVal) extends KVal
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    69
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    70
case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    71
  override def toString = s"LET $x = $e1 in \n$e2" 
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
    72
}
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    73
case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    74
  def pad(e: KExp) = e.toString.replaceAll("(?m)^", "  ")
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    75
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    76
  override def toString = 
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
    77
     s"IF $x1\nTHEN\n${pad(e1)}\nELSE\n${pad(e2)}"
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    78
}
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    79
case class KReturn(v: KVal) extends KExp
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
    80
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    81
// some functions for drawing KVal-trees 
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    82
// inspired by William Bradford Larcombe
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    83
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    84
def draw_vals(vs: List[KVal], prefix: String) : String = {
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    85
  val vsi = vs.iterator
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    86
  vsi.map(v => draw_val(v, prefix, vsi.hasNext)).mkString 
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    87
}
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    88
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    89
def draw_val(k: KVal, prefix: String, more: Boolean) : String = {
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    90
  val full_prefix = s"$prefix${if more then "├" else "└"}"
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    91
  val childPrefix = s"$prefix${if more then "│" else ""}  "
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    92
  s"\n${full_prefix}" ++ 
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    93
  (k match {
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    94
    case KVar(x) => x
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    95
    case KNum(n) => n.toString
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    96
    case Kop(op, v1 , v2) => s"KOp($op) ${draw_vals(List(v1, v2), childPrefix)}" 
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    97
    case KCall(nme, as) => s"KCall($nme) ${draw_vals(as, childPrefix)}" 
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    98
    case KWrite(v) => s"KWrite ${draw_val(v, childPrefix, false)}" 
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
    99
  })
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   100
}
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   101
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   102
def draw(k: KVal) = "│" ++ draw_val(k, "", false)
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   103
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   104
// val k1 = KVar("foo")
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   105
// val k2 = KNum(1)
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   106
// val k3 = Kop("-", Kop("+", k1, k2), KNum(2))
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   107
// println(draw(k3).mkString)
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   108
// println(draw(KCall("bar", List(k1,k2,k3,k2,k1))).mkString)
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   109
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   110
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   111
// CPS translation from Exps to KExps using a
654
fb6192488b91 updated
Christian Urban <urbanc@in.tum.de>
parents: 653
diff changeset
   112
// continuation k.
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   113
def CPS(e: Exp)(k: KVal => KExp) : KExp = e match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   114
  case Var(s) => k(KVar(s)) 
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   115
  case Num(i) => k(KNum(i))
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   116
  case Aop(o, e1, e2) => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   117
    val z = Fresh("tmp")
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   118
    CPS(e1)(y1 => 
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   119
      CPS(e2)(y2 => KLet(z, Kop(o, y1, y2), k(KVar(z)))))
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   120
  }
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   121
  case If(Bop(o, b1, b2), e1, e2) => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   122
    val z = Fresh("tmp")
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   123
    CPS(b1)(y1 => 
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   124
      CPS(b2)(y2 => 
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   125
        KLet(z, Kop(o, y1, y2), KIf(z, CPS(e1)(k), CPS(e2)(k)))))
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   126
  }
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   127
  case Call(name, args) => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   128
    def aux(args: List[Exp], vs: List[KVal]) : KExp = args match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   129
      case Nil => {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   130
          val z = Fresh("tmp")
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   131
          KLet(z, KCall(name, vs), k(KVar(z)))
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   132
      }
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   133
      case e::es => CPS(e)(y => aux(es, vs ::: List(y)))
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   134
    }
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   135
    aux(args, Nil)
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   136
  }
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   137
  case Sequence(e1, e2) => 
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   138
    CPS(e1)(_ => CPS(e2)(y2 => k(y2)))
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   139
  case Write(e) => {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   140
    val z = Fresh("tmp")
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   141
    CPS(e)(y => KLet(z, KWrite(y), k(KVar(z))))
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   142
  }
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   143
}   
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   144
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   145
//initial continuation
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   146
def CPSi(e: Exp) = CPS(e)(KReturn)
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   147
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   148
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   149
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   150
//some testcases:
905
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   151
// (1 + 2) * 3
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   152
println(CPSi(Aop("*", Aop("+", Num(1), Num(2)), Num(3))).toString)
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   153
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   154
// 3 * (1 + 2)
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   155
println(CPSi(Aop("*", Num(3), Aop("+", Num(1), Num(2)))).toString)
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   156
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   157
//some testcases:
15973df32613 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   158
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   159
// numbers and vars   
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   160
println(CPSi(Num(1)).toString)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   161
println(CPSi(Var("z")).toString)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   162
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   163
//  a * 3
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   164
val e1 = Aop("*", Var("a"), Num(3))
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   165
println(CPSi(e1).toString)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   166
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   167
// (a * 3) + 4
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   168
val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   169
println(CPSi(e2).toString)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   170
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   171
// 2 + (a * 3)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   172
val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   173
println(CPSi(e3).toString)
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   174
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   175
//(1 - 2) + (a * 3)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   176
val e4 = Aop("+", Aop("-", Num(1), Num(2)), Aop("*", Var("a"), Num(3)))
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   177
println(CPSi(e4).toString)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   178
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   179
// 3 + 4 ; 1 * 7
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   180
val es = Sequence(Aop("+", Num(3), Num(4)),
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   181
                  Aop("*", Num(1), Num(7)))
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   182
println(CPSi(es).toString)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   183
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   184
// if (1 == 1) then 3 else 4
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   185
val e5 = If(Bop("==", Num(1), Num(1)), Num(3), Num(4))
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   186
println(CPSi(e5).toString)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   187
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   188
// if (1 == 1) then 3 + 7 else 4 * 2
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   189
val ei = If(Bop("==", Num(1), Num(1)), 
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   190
                Aop("+", Num(3), Num(7)),
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   191
                Aop("*", Num(4), Num(2)))
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   192
println(CPSi(ei).toString)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   193
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   194
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   195
// if (10 != 10) then e5 else 40
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   196
val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   197
println(CPSi(e6).toString)
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   198
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   199
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   200
// foo(3)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   201
val e7 = Call("foo", List(Num(3)))
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   202
println(CPSi(e7).toString)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   203
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   204
// foo(3 * 1, 4, 5 + 6)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   205
val e8 = Call("foo", List(Aop("*", Num(3), Num(1)), 
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   206
                          Num(4), 
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   207
                          Aop("+", Num(5), Num(6))))
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   208
println(CPSi(e8).toString)
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   209
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   210
// a * 3 ; b + 6
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   211
val e9 = Sequence(Aop("*", Var("a"), Num(3)), 
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   212
                  Aop("+", Var("b"), Num(6)))
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   213
println(CPSi(e9).toString)
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   214
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   215
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   216
val e10 = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   217
println(CPSi(e10).toString)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   218
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   219
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   220
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   221
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   222
// convenient string interpolations 
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   223
// for instructions, labels and methods
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   224
import scala.language.implicitConversions
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   225
import scala.language.reflectiveCalls
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   226
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 905
diff changeset
   227
extension (sc: StringContext) {
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   228
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   229
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   230
    def m(args: Any*): String = sc.s(args:_*) ++ "\n"
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   231
}
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   232
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   233
// mathematical and boolean operations
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   234
def compile_op(op: String) = op match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   235
  case "+" => "add i32 "
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   236
  case "*" => "mul i32 "
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   237
  case "-" => "sub i32 "
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   238
  case "/" => "sdiv i32 "
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   239
  case "%" => "srem i32 "
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   240
  case "==" => "icmp eq i32 "
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   241
  case "<=" => "icmp sle i32 "     // signed less or equal
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   242
  case "<"  => "icmp slt i32 "     // signed less than
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   243
}
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   245
// compile K values
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   246
def compile_val(v: KVal) : String = v match {
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   247
  case KNum(i) => s"$i"
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   248
  case KVar(s) => s"%$s"
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
   249
  case Kop(op, x1, x2) => 
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   250
    s"${compile_op(op)} ${compile_val(x1)}, ${compile_val(x2)}"
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   251
  case KCall(x1, args) => 
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   252
    s"call i32 @$x1 (${args.map(compile_val).mkString("i32 ", ", i32 ", "")})"
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   253
  case KWrite(x1) =>
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   254
    s"call i32 @printInt (i32 ${compile_val(x1)})"
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   255
}
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 646
diff changeset
   256
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   257
// compile K expressions
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   258
def compile_exp(a: KExp) : String = a match {
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   259
  case KReturn(v) =>
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   260
    i"ret i32 ${compile_val(v)}"
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   261
  case KLet(x: String, v: KVal, e: KExp) => 
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   262
    i"%$x = ${compile_val(v)}" ++ compile_exp(e)
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   263
  case KIf(x, e1, e2) => {
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   264
    val if_br = Fresh("if_branch")
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   265
    val else_br = Fresh("else_branch")
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   266
    i"br i1 %$x, label %$if_br, label %$else_br" ++
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   267
    l"\n$if_br" ++
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   268
    compile_exp(e1) ++
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   269
    l"\n$else_br" ++ 
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   270
    compile_exp(e2)
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
  }
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   272
}
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   273
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   274
val prelude = """
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   275
@.str = private constant [4 x i8] c"%d\0A\00"
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   276
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   277
declare i32 @printf(i8*, ...)
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   279
define i32 @printInt(i32 %x) {
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   280
   %t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0
679
8fc109f36b78 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
   281
   call i32 (i8*, ...) @printf(i8* %t0, i32 %x) 
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   282
   ret i32 %x
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   283
}
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   284
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 654
diff changeset
   285
"""
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   286
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   288
// compile function for declarations and main
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   289
def compile_decl(d: Decl) : String = d match {
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   290
  case Def(name, args, body) => { 
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   291
    m"define i32 @$name (${args.mkString("i32 %", ", i32 %", "")}) {" ++
653
9d7843934d30 updated llvm
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
   292
    compile_exp(CPSi(body)) ++
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   293
    m"}\n"
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   294
  }
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   295
  case Main(body) => {
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   296
    m"define i32 @main() {" ++
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   297
    compile_exp(CPS(body)(_ => KReturn(KNum(0)))) ++
649
e83afb44f276 updated
Christian Urban <urbanc@in.tum.de>
parents: 648
diff changeset
   298
    m"}\n"
221
824ffbf66ab4 added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 220
diff changeset
   299
  }
220
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   300
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   301
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   302
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   303
// main compiler functions
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   304
def compile(prog: List[Decl]) : String = 
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   305
  prelude ++ (prog.map(compile_decl).mkString)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   306
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   307
@main
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   308
def main(fname: String) = {
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   309
    val path = os.pwd / fname
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   310
    val file = fname.stripSuffix("." ++ path.ext)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   311
    val tks = tokenise(os.read(path))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   312
    val ast = parse_tks(tks)
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   313
    println(compile(ast))
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   314
}
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   315
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   316
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   317
def write(fname: String) = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   318
    val path = os.pwd / fname
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   319
    val file = fname.stripSuffix("." ++ path.ext)
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   320
    val tks = tokenise(os.read(path))
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   321
    val ast = parse_tks(tks)
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   322
    val code = compile(ast)
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   323
    os.write.over(os.pwd / (file ++ ".ll"), code)
626
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   324
}
2d91b2107656 updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   325
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   326
@main
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   327
def run(fname: String) = {
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   328
    val path = os.pwd / fname
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   329
    val file = fname.stripSuffix("." ++ path.ext)
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   330
    write(fname)  
789
f0696713177b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 734
diff changeset
   331
    os.proc("llc", "-filetype=obj", file ++ ".ll").call()
813
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   332
    os.proc("gcc", file ++ ".o", "-o", file ++ ".bin").call()
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   333
    os.proc(os.pwd / (file ++ ".bin")).call(stdout = os.Inherit)
059f970287d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 789
diff changeset
   334
    println(s"done.")
657
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   335
}
644
b4f5714485e1 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
   336
657
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   337
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   338
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   339
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   340
// CPS functions
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   341
/*
657
732cb155d806 updated
Christian Urban <urbanc@in.tum.de>
parents: 656
diff changeset
   342
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   343
def fact(n: Int) : Int = 
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   344
  if (n == 0) 1 else n * fact(n - 1)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   345
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   346
fact(6)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   347
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   348
def factT(n: Int, acc: Int) : Int =
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   349
  if (n == 0) acc else factT(n - 1, acc * n)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   350
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   351
factT(6, 1)
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   352
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   353
def factC(n: Int, ret: Int => Int) : Int = {
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   354
  if (n == 0) ret(1) 
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   355
  else factC(n - 1, x => ret(x * n))
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   356
}
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   357
959
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   358
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   359
959
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   360
819
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   361
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   362
fibC(10, x => {println(s"Result: $x") ; 1})
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   363
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   364
fd88a0656164 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 813
diff changeset
   365
*/
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   366
/*
959
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   367
factC(6, x => x)
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   368
factC(6, x => {println(s"The final Result is $x") ; 0})
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   369
factC(6, _ + 1)
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   370
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   371
def fib(n: Int) : Int = {
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   372
  if (n == 0 || n == 1) 1
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   373
  else fib(n - 1) + fib(n - 2)
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   374
}
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   375
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   376
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   377
def fibC(n: Int, ret: Int => Int) : Int = {
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   378
  if (n == 0 || n == 1) ret(1)
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 958
diff changeset
   379
  else fibC(n - 1, x => fibC(n - 2, y => ret(x + y)))
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   380
}
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
   381
*/