| author | Christian Urban <urbanc@in.tum.de> | 
| Thu, 14 Nov 2019 13:50:29 +0000 | |
| changeset 688 | 289d4baafeee | 
| parent 679 | 9a4404f65b63 | 
| child 701 | 81377a3eb717 | 
| permissions | -rw-r--r-- | 
| 654 | 1  | 
// A Small LLVM Compiler for a Simple Functional Language  | 
| 644 | 2  | 
// (includes an external lexer and parser)  | 
| 645 | 3  | 
//  | 
4  | 
// call with  | 
|
5  | 
//  | 
|
| 654 | 6  | 
// scala fun_llvm.scala fact  | 
| 645 | 7  | 
//  | 
| 654 | 8  | 
// scala fun_llvm.scala defs  | 
9  | 
//  | 
|
| 655 | 10  | 
// this will generate a .ll file. You can interpret this file  | 
11  | 
// using lli.  | 
|
12  | 
//  | 
|
13  | 
// The optimiser can be invoked as  | 
|
14  | 
//  | 
|
15  | 
// opt -O1 -S in_file.ll > out_file.ll  | 
|
16  | 
// opt -O3 -S in_file.ll > out_file.ll  | 
|
17  | 
//  | 
|
18  | 
// The code produced for the various architectures can be obtains with  | 
|
19  | 
//  | 
|
20  | 
// llc -march=x86 -filetype=asm in_file.ll -o -  | 
|
21  | 
// llc -march=arm -filetype=asm in_file.ll -o -  | 
|
22  | 
//  | 
|
23  | 
// Producing an executable can be achieved by  | 
|
24  | 
//  | 
|
25  | 
// llc -filetype=obj in_file.ll  | 
|
26  | 
// gcc in_file.o -o a.out  | 
|
27  | 
// ./a.out  | 
|
28  | 
||
| 645 | 29  | 
|
| 625 | 30  | 
|
| 649 | 31  | 
object Compiler {
 | 
| 
220
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
32  | 
|
| 645 | 33  | 
import java.io._  | 
34  | 
import scala.util._  | 
|
35  | 
import scala.sys.process._  | 
|
36  | 
||
| 644 | 37  | 
// Abstract syntax trees for the Fun language  | 
38  | 
abstract class Exp extends Serializable  | 
|
39  | 
abstract class BExp extends Serializable  | 
|
40  | 
abstract class Decl extends Serializable  | 
|
| 626 | 41  | 
|
42  | 
case class Def(name: String, args: List[String], body: Exp) extends Decl  | 
|
43  | 
case class Main(e: Exp) extends Decl  | 
|
44  | 
||
45  | 
case class Call(name: String, args: List[Exp]) extends Exp  | 
|
46  | 
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp  | 
|
47  | 
case class Write(e: Exp) extends Exp  | 
|
48  | 
case class Var(s: String) extends Exp  | 
|
49  | 
case class Num(i: Int) extends Exp  | 
|
50  | 
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp  | 
|
51  | 
case class Sequence(e1: Exp, e2: Exp) extends Exp  | 
|
52  | 
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp  | 
|
53  | 
||
54  | 
||
| 
220
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
55  | 
// for generating new labels  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
56  | 
var counter = -1  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
57  | 
|
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
58  | 
def Fresh(x: String) = {
 | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
59  | 
counter += 1  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
60  | 
x ++ "_" ++ counter.toString()  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
61  | 
}  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
62  | 
|
| 678 | 63  | 
// Internal CPS language for FUN  | 
| 648 | 64  | 
abstract class KExp  | 
| 653 | 65  | 
abstract class KVal  | 
| 648 | 66  | 
|
| 653 | 67  | 
case class KVar(s: String) extends KVal  | 
68  | 
case class KNum(i: Int) extends KVal  | 
|
| 656 | 69  | 
case class Kop(o: String, v1: KVal, v2: KVal) extends KVal  | 
| 653 | 70  | 
case class KCall(o: String, vrs: List[KVal]) extends KVal  | 
| 655 | 71  | 
case class KWrite(v: KVal) extends KVal  | 
| 649 | 72  | 
|
| 653 | 73  | 
case class KIf(x1: String, e1: KExp, e2: KExp) extends KExp {
 | 
74  | 
override def toString = s"KIf $x1\nIF\n$e1\nELSE\n$e2"  | 
|
| 649 | 75  | 
}  | 
| 653 | 76  | 
case class KLet(x: String, e1: KVal, e2: KExp) extends KExp {
 | 
| 648 | 77  | 
override def toString = s"let $x = $e1 in \n$e2"  | 
78  | 
}  | 
|
| 653 | 79  | 
case class KReturn(v: KVal) extends KExp  | 
| 648 | 80  | 
|
| 654 | 81  | 
|
| 655 | 82  | 
// CPS translation from Exps to KExps using a  | 
| 654 | 83  | 
// continuation k.  | 
| 653 | 84  | 
def CPS(e: Exp)(k: KVal => KExp) : KExp = e match {
 | 
85  | 
case Var(s) => k(KVar(s))  | 
|
86  | 
case Num(i) => k(KNum(i))  | 
|
87  | 
  case Aop(o, e1, e2) => {
 | 
|
88  | 
    val z = Fresh("tmp")
 | 
|
89  | 
CPS(e1)(y1 =>  | 
|
| 656 | 90  | 
CPS(e2)(y2 => KLet(z, Kop(o, y1, y2), k(KVar(z)))))  | 
| 653 | 91  | 
}  | 
92  | 
  case If(Bop(o, b1, b2), e1, e2) => {
 | 
|
93  | 
    val z = Fresh("tmp")
 | 
|
94  | 
CPS(b1)(y1 =>  | 
|
| 655 | 95  | 
CPS(b2)(y2 =>  | 
| 656 | 96  | 
KLet(z, Kop(o, y1, y2), KIf(z, CPS(e1)(k), CPS(e2)(k)))))  | 
| 653 | 97  | 
}  | 
98  | 
  case Call(name, args) => {
 | 
|
99  | 
    def aux(args: List[Exp], vs: List[KVal]) : KExp = args match {
 | 
|
100  | 
      case Nil => {
 | 
|
101  | 
          val z = Fresh("tmp")
 | 
|
102  | 
KLet(z, KCall(name, vs), k(KVar(z)))  | 
|
103  | 
}  | 
|
104  | 
case e::es => CPS(e)(y => aux(es, vs ::: List(y)))  | 
|
| 648 | 105  | 
}  | 
| 653 | 106  | 
aux(args, Nil)  | 
107  | 
}  | 
|
| 656 | 108  | 
case Sequence(e1, e2) =>  | 
| 679 | 109  | 
CPS(e1)(_ => CPS(e2)(y2 => k(y2)))  | 
| 655 | 110  | 
  case Write(e) => {
 | 
111  | 
    val z = Fresh("tmp")
 | 
|
112  | 
CPS(e)(y => KLet(z, KWrite(y), k(KVar(z))))  | 
|
113  | 
}  | 
|
| 653 | 114  | 
}  | 
115  | 
||
| 679 | 116  | 
//initial continuation  | 
| 653 | 117  | 
def CPSi(e: Exp) = CPS(e)(KReturn)  | 
118  | 
||
| 654 | 119  | 
// some testcases  | 
| 653 | 120  | 
val e1 = Aop("*", Var("a"), Num(3))
 | 
| 654 | 121  | 
CPSi(e1)  | 
| 653 | 122  | 
|
123  | 
val e2 = Aop("+", Aop("*", Var("a"), Num(3)), Num(4))
 | 
|
| 654 | 124  | 
CPSi(e2)  | 
| 653 | 125  | 
|
126  | 
val e3 = Aop("+", Num(2), Aop("*", Var("a"), Num(3)))
 | 
|
| 654 | 127  | 
CPSi(e3)  | 
| 648 | 128  | 
|
| 653 | 129  | 
val e4 = Aop("+", Aop("-", Num(1), Num(2)), Aop("*", Var("a"), Num(3)))
 | 
| 654 | 130  | 
CPSi(e4)  | 
| 653 | 131  | 
|
132  | 
val e5 = If(Bop("==", Num(1), Num(1)), Num(3), Num(4))
 | 
|
| 654 | 133  | 
CPSi(e5)  | 
| 653 | 134  | 
|
135  | 
val e6 = If(Bop("!=", Num(10), Num(10)), e5, Num(40))
 | 
|
| 654 | 136  | 
CPSi(e6)  | 
| 648 | 137  | 
|
| 653 | 138  | 
val e7 = Call("foo", List(Num(3)))
 | 
| 654 | 139  | 
CPSi(e7)  | 
| 653 | 140  | 
|
141  | 
val e8 = Call("foo", List(Num(3), Num(4), Aop("+", Num(5), Num(6))))
 | 
|
| 654 | 142  | 
CPSi(e8)  | 
| 653 | 143  | 
|
144  | 
val e9 = Sequence(Aop("*", Var("a"), Num(3)), Aop("+", Var("b"), Num(6)))
 | 
|
| 654 | 145  | 
CPSi(e9)  | 
| 649 | 146  | 
|
147  | 
val e = Aop("*", Aop("+", Num(1), Call("foo", List(Var("a"), Num(3)))), Num(4))
 | 
|
| 654 | 148  | 
CPSi(e)  | 
| 653 | 149  | 
|
| 648 | 150  | 
|
151  | 
||
152  | 
||
| 625 | 153  | 
// convenient string interpolations  | 
154  | 
// for instructions, labels and methods  | 
|
155  | 
import scala.language.implicitConversions  | 
|
156  | 
import scala.language.reflectiveCalls  | 
|
157  | 
||
158  | 
implicit def sring_inters(sc: StringContext) = new {
 | 
|
159  | 
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n"  | 
|
160  | 
def l(args: Any*): String = sc.s(args:_*) ++ ":\n"  | 
|
161  | 
def m(args: Any*): String = sc.s(args:_*) ++ "\n"  | 
|
162  | 
}  | 
|
163  | 
||
| 656 | 164  | 
// mathematical and boolean operations  | 
| 653 | 165  | 
def compile_op(op: String) = op match {
 | 
166  | 
case "+" => "add i32 "  | 
|
167  | 
case "*" => "mul i32 "  | 
|
168  | 
case "-" => "sub i32 "  | 
|
| 656 | 169  | 
case "/" => "sdiv i32 "  | 
170  | 
case "%" => "srem i32 "  | 
|
| 653 | 171  | 
case "==" => "icmp eq i32 "  | 
| 656 | 172  | 
case "<=" => "icmp sle i32 " // signed less or equal  | 
173  | 
case "<" => "icmp slt i32 " // signed less than  | 
|
| 653 | 174  | 
}  | 
| 
220
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
175  | 
|
| 653 | 176  | 
def compile_val(v: KVal) : String = v match {
 | 
177  | 
case KNum(i) => s"$i"  | 
|
178  | 
case KVar(s) => s"%$s"  | 
|
| 656 | 179  | 
case Kop(op, x1, x2) =>  | 
| 653 | 180  | 
    s"${compile_op(op)} ${compile_val(x1)}, ${compile_val(x2)}"
 | 
181  | 
case KCall(x1, args) =>  | 
|
182  | 
    s"call i32 @$x1 (${args.map(compile_val).mkString("i32 ", ", i32 ", "")})"
 | 
|
| 655 | 183  | 
case KWrite(x1) =>  | 
184  | 
    s"call i32 @printInt (i32 ${compile_val(x1)})"
 | 
|
| 653 | 185  | 
}  | 
| 648 | 186  | 
|
| 649 | 187  | 
// compile K expressions  | 
188  | 
def compile_exp(a: KExp) : String = a match {
 | 
|
| 653 | 189  | 
case KReturn(v) =>  | 
190  | 
    i"ret i32 ${compile_val(v)}"
 | 
|
191  | 
case KLet(x: String, v: KVal, e: KExp) =>  | 
|
192  | 
    i"%$x = ${compile_val(v)}" ++ compile_exp(e)
 | 
|
193  | 
  case KIf(x, e1, e2) => {
 | 
|
| 679 | 194  | 
    val if_br = Fresh("if_branch")
 | 
195  | 
    val else_br = Fresh("else_branch")
 | 
|
| 649 | 196  | 
i"br i1 %$x, label %$if_br, label %$else_br" ++  | 
197  | 
l"\n$if_br" ++  | 
|
| 653 | 198  | 
compile_exp(e1) ++  | 
| 649 | 199  | 
l"\n$else_br" ++  | 
| 653 | 200  | 
compile_exp(e2)  | 
| 
220
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
201  | 
}  | 
| 653 | 202  | 
}  | 
203  | 
||
| 655 | 204  | 
|
205  | 
val prelude = """  | 
|
206  | 
@.str = private constant [4 x i8] c"%d\0A\00"  | 
|
207  | 
||
208  | 
declare i32 @printf(i8*, ...)  | 
|
| 
220
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
209  | 
|
| 655 | 210  | 
define i32 @printInt(i32 %x) {
 | 
211  | 
%t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0  | 
|
| 679 | 212  | 
call i32 (i8*, ...) @printf(i8* %t0, i32 %x)  | 
| 655 | 213  | 
ret i32 %x  | 
214  | 
}  | 
|
215  | 
||
216  | 
"""  | 
|
| 653 | 217  | 
|
| 
220
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
218  | 
|
| 625 | 219  | 
// compile function for declarations and main  | 
220  | 
def compile_decl(d: Decl) : String = d match {
 | 
|
| 649 | 221  | 
  case Def(name, args, body) => { 
 | 
222  | 
    m"define i32 @$name (${args.mkString("i32 %", ", i32 %", "")}) {" ++
 | 
|
| 653 | 223  | 
compile_exp(CPSi(body)) ++  | 
| 649 | 224  | 
m"}\n"  | 
| 
221
 
824ffbf66ab4
added fun tail
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
220 
diff
changeset
 | 
225  | 
}  | 
| 649 | 226  | 
  case Main(body) => {
 | 
227  | 
    m"define i32 @main() {" ++
 | 
|
| 653 | 228  | 
compile_exp(CPSi(body)) ++  | 
| 649 | 229  | 
m"}\n"  | 
| 
221
 
824ffbf66ab4
added fun tail
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
220 
diff
changeset
 | 
230  | 
}  | 
| 
220
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
231  | 
}  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
232  | 
|
| 626 | 233  | 
// main compiler functions  | 
| 
220
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
234  | 
|
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
235  | 
def time_needed[T](i: Int, code: => T) = {
 | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
236  | 
val start = System.nanoTime()  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
237  | 
for (j <- 1 to i) code  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
238  | 
val end = System.nanoTime()  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
239  | 
(end - start)/(i * 1.0e9)  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
240  | 
}  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
241  | 
|
| 645 | 242  | 
def deserialise[T](fname: String) : Try[T] = {
 | 
243  | 
import scala.util.Using  | 
|
244  | 
  Using(new ObjectInputStream(new FileInputStream(fname))) {
 | 
|
245  | 
in => in.readObject.asInstanceOf[T]  | 
|
246  | 
}  | 
|
| 644 | 247  | 
}  | 
248  | 
||
| 655 | 249  | 
def compile(fname: String) : String = {
 | 
250  | 
val ast = deserialise[List[Decl]](fname ++ ".prs").getOrElse(Nil)  | 
|
251  | 
prelude ++ (ast.map(compile_decl).mkString)  | 
|
| 626 | 252  | 
}  | 
253  | 
||
| 655 | 254  | 
def compile_to_file(fname: String) = {
 | 
255  | 
val output = compile(fname)  | 
|
256  | 
  scala.tools.nsc.io.File(s"${fname}.ll").writeAll(output)
 | 
|
| 626 | 257  | 
}  | 
258  | 
||
| 655 | 259  | 
def compile_and_run(fname: String) : Unit = {
 | 
260  | 
compile_to_file(fname)  | 
|
261  | 
  (s"llc -filetype=obj ${fname}.ll").!!
 | 
|
262  | 
  (s"gcc ${fname}.o -o a.out").!!
 | 
|
263  | 
  println("Time: " + time_needed(2, (s"./a.out").!))
 | 
|
| 
220
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
264  | 
}  | 
| 
 
141041fc76b5
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
265  | 
|
| 626 | 266  | 
// some examples of .fun files  | 
| 645 | 267  | 
//compile_to_file("fact")
 | 
268  | 
//compile_and_run("fact")
 | 
|
269  | 
//compile_and_run("defs")
 | 
|
270  | 
||
| 644 | 271  | 
|
| 649 | 272  | 
def main(args: Array[String]) : Unit =  | 
| 655 | 273  | 
//println(compile(args(0)))  | 
274  | 
compile_and_run(args(0))  | 
|
| 657 | 275  | 
}  | 
| 644 | 276  | 
|
| 657 | 277  | 
|
278  | 
||
279  | 
||
280  | 
||
281  | 
/*  | 
|
282  | 
LLVM notes  | 
|
283  | 
||
284  | 
Registers are places for data inside the CPU.  | 
|
285  | 
+ up to 10 times faster access than to main memory  | 
|
286  | 
- expensive; typically just 32 of them in a 32-bit CPU  | 
|
287  | 
||
288  | 
High-level view of x86  | 
|
289  | 
• Not a stack machine; no direct correspondence to operand stacks  | 
|
290  | 
• Arithmetics, etc. is done with values in registers  | 
|
291  | 
||
292  | 
• Started as academic project at University of Illinois in 2002  | 
|
293  | 
• Now a large open source project with many contributors and a growing user base  | 
|
294  | 
||
295  | 
Single Static Assignment (SSA) form  | 
|
296  | 
• Only one assignment in the program text to each variable  | 
|
297  | 
• But dynamically, this assignment can be executed many times  | 
|
298  | 
• Many stores to a memory location are allowed  | 
|
299  | 
• Also, Φ (phi) instructions can be used, in the beginning of a basic block  | 
|
300  | 
• Value is one of the arguments, depending on from which block control came to this block  | 
|
301  | 
• Register allocation tries to keep these variables in same real register  | 
|
302  | 
||
303  | 
Why SSA form?  | 
|
304  | 
Many code optimizations can be done more efficiently  | 
|
305  | 
||
306  | 
Function definition form  | 
|
307  | 
 define t @name(t1 x1, t2 x2, ..., tn xn) {
 | 
|
308  | 
l1: block1  | 
|
309  | 
l2: block2  | 
|
310  | 
...  | 
|
311  | 
lm : blockm  | 
|
312  | 
}  | 
|
313  | 
||
314  | 
||
315  | 
||
316  | 
||
317  | 
*/  |