--- a/progs/defs.fun Mon Oct 14 00:02:24 2019 +0100
+++ b/progs/defs.fun Mon Oct 14 00:48:01 2019 +0100
@@ -3,19 +3,24 @@
def suc(x) = x + 1;
-def pred(x) = if x == 0 then x else x - 1;
+def pred(x) =
+ if x == 0 then x else x - 1;
-def add(x, y) = if x == 0 then y else suc(add(x - 1, y));
+def add(x, y) =
+ if x == 0 then y else suc(add(x - 1, y));
-def mult(x, y) = if x == 0 then 0 else add(y, mult(x - 1, y));
+def mult(x, y) =
+ if x == 0 then 0 else add(y, mult(x - 1, y));
-def pow(x, y) = if y == 0 then 1 else mult(x, pow(x, y - 1));
+def pow(x, y) =
+ if y == 0 then 1 else mult(x, pow(x, y - 1));
def fib(n) = if n == 0 then 0
else if n == 1 then 1
else fib(n - 1) + fib(n - 2);
-def fact(n) = if n == 0 then 1 else n * fact(n - 1);
+def fact(n) =
+ if n == 0 then 1 else n * fact(n - 1);
def ack(m, n) = if m == 0 then n + 1
else if n == 0 then ack(m - 1, 1)
@@ -27,7 +32,8 @@
def rem(x, y) = x % y; //remainder
-def gcd(a, b) = if b == 0 then a else gcd(b, a % b);
+def gcd(a, b) =
+ if b == 0 then a else gcd(b, a % b);
def is_prime_aux(n, i) =
if n % i == 0 then 0
@@ -53,24 +59,25 @@
def collatz(n) = collatz_aux(n, 1);
-def facT(n, acc) = if n == 0 then acc else facT(n - 1, n * acc);
+def facT(n, acc) =
+ if n == 0 then acc else facT(n - 1, n * acc);
//zero(3)
//suc(8)
//pred(7)
-//add(3, 4)
+//write(add(3, 4))
//mult(4,5)
//pow(2, 3)
//fib(20)
//(write(fact(5)) ; fact(6))
//(write(1) ; 2)
-ack(3, 12) // for tail-rec test
+//write(ack(3, 12)) // for tail-rec test
//stack_test(0)
//(write (div(11, 3)); rem(11, 3))
//gcd(54, 24)
//is_prime(2)
-//primes(1000000)
+primes(1000000)
//collatz(5000)
//facT(6, 1)
--- a/progs/fun_llvm.scala Mon Oct 14 00:02:24 2019 +0100
+++ b/progs/fun_llvm.scala Mon Oct 14 00:48:01 2019 +0100
@@ -66,8 +66,7 @@
case class KVar(s: String) extends KVal
case class KNum(i: Int) extends KVal
-case class KAop(o: String, v1: KVal, v2: KVal) extends KVal
-case class KBop(o: String, v1: KVal, v2: KVal) extends KVal
+case class Kop(o: String, v1: KVal, v2: KVal) extends KVal
case class KCall(o: String, vrs: List[KVal]) extends KVal
case class KWrite(v: KVal) extends KVal
@@ -88,13 +87,13 @@
case Aop(o, e1, e2) => {
val z = Fresh("tmp")
CPS(e1)(y1 =>
- CPS(e2)(y2 => KLet(z, KAop(o, y1, y2), k(KVar(z)))))
+ CPS(e2)(y2 => KLet(z, Kop(o, y1, y2), k(KVar(z)))))
}
case If(Bop(o, b1, b2), e1, e2) => {
val z = Fresh("tmp")
CPS(b1)(y1 =>
CPS(b2)(y2 =>
- KLet(z, KBop(o, y1, y2), KIf(z, CPS(e1)(k), CPS(e2)(k)))))
+ KLet(z, Kop(o, y1, y2), KIf(z, CPS(e1)(k), CPS(e2)(k)))))
}
case Call(name, args) => {
def aux(args: List[Exp], vs: List[KVal]) : KExp = args match {
@@ -106,11 +105,8 @@
}
aux(args, Nil)
}
- case Sequence(e1, e2) => {
- val z = Fresh("tmp")
- CPS(e1)(y1 =>
- CPS(e2)(y2 => KLet("_", y1, KLet(z, y2, k(KVar(z))))))
- }
+ case Sequence(e1, e2) =>
+ CPS(e1)(y1 => CPS(e2)(y2 => k(y2)))
case Write(e) => {
val z = Fresh("tmp")
CPS(e)(y => KLet(z, KWrite(y), k(KVar(z))))
@@ -164,19 +160,22 @@
def m(args: Any*): String = sc.s(args:_*) ++ "\n"
}
+// mathematical and boolean operations
def compile_op(op: String) = op match {
case "+" => "add i32 "
case "*" => "mul i32 "
case "-" => "sub i32 "
+ case "/" => "sdiv i32 "
+ case "%" => "srem i32 "
case "==" => "icmp eq i32 "
+ case "<=" => "icmp sle i32 " // signed less or equal
+ case "<" => "icmp slt i32 " // signed less than
}
def compile_val(v: KVal) : String = v match {
case KNum(i) => s"$i"
case KVar(s) => s"%$s"
- case KAop(op, x1, x2) =>
- s"${compile_op(op)} ${compile_val(x1)}, ${compile_val(x2)}"
- case KBop(op, x1, x2) =>
+ case Kop(op, x1, x2) =>
s"${compile_op(op)} ${compile_val(x1)}, ${compile_val(x2)}"
case KCall(x1, args) =>
s"call i32 @$x1 (${args.map(compile_val).mkString("i32 ", ", i32 ", "")})"