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