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