author | Christian Urban <urbanc@in.tum.de> |
Mon, 07 Oct 2019 20:53:25 +0100 | |
changeset 648 | 36379b038438 |
parent 646 | 2abd285c66d1 |
child 649 | e83afb44f276 |
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 |
|
648 | 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 |
|
59 |
||
60 |
case class KVar(s: String) extends KExp |
|
61 |
case class KNum(i: Int) extends KExp |
|
62 |
case class KAop(o: String, x1: String, x2: String) extends KExp |
|
63 |
case class KIfeq(x1: String, x2: String, e1: KExp, e2: KExp) extends KExp |
|
64 |
case class KCall(o: String, vrs: List[String]) extends KExp |
|
65 |
case class KLet(x: String, e1: KExp, e2: KExp) extends KExp { |
|
66 |
override def toString = s"let $x = $e1 in \n$e2" |
|
67 |
} |
|
68 |
||
69 |
def K(e: Exp) : KExp = e match { |
|
70 |
case Var(s) => KVar(s) |
|
71 |
case Num(i) => KNum(i) |
|
72 |
case Aop(o, a1, a2) => { |
|
73 |
val x1 = Fresh("tmp") |
|
74 |
val x2 = Fresh("tmp") |
|
75 |
KLet(x1, K(a1), KLet(x2, K(a2), KAop(o, x1, x2))) |
|
76 |
} |
|
77 |
case Call(name: String, args: List[Exp]) => { |
|
78 |
val args_new = args.map{a => (Fresh("x"), K(a))} |
|
79 |
def aux(as: List[(String, KExp)]) : KExp = as match { |
|
80 |
case Nil => KCall(name, args_new.map(_._1)) |
|
81 |
case (x, a)::rest => KLet(x, a, aux(rest)) |
|
82 |
} |
|
83 |
aux(args_new) |
|
84 |
} |
|
85 |
||
86 |
} |
|
87 |
||
88 |
def Denest(e: KExp) : KExp = e match { |
|
89 |
case KLet(xt, e1, e2) => { |
|
90 |
def insert(e: KExp) : KExp = e match { |
|
91 |
case KLet(yt, e3, e4) => KLet(yt, e3, insert(e4)) |
|
92 |
case e => KLet(xt, e, Denest(e2)) |
|
93 |
} |
|
94 |
insert(Denest(e1)) |
|
95 |
} |
|
96 |
case _ => e |
|
97 |
} |
|
98 |
||
99 |
val e = Aop("*", Aop("+", Num(1), Call("foo", List(Num(2), Num(3)))), Num(4)) |
|
100 |
println(K(e)) |
|
101 |
println(Denest(K(e))) |
|
102 |
||
103 |
||
104 |
||
625 | 105 |
// convenient string interpolations |
106 |
// for instructions, labels and methods |
|
107 |
import scala.language.implicitConversions |
|
108 |
import scala.language.reflectiveCalls |
|
109 |
||
110 |
implicit def sring_inters(sc: StringContext) = new { |
|
111 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" |
|
112 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" |
|
113 |
def m(args: Any*): String = sc.s(args:_*) ++ "\n" |
|
114 |
} |
|
115 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
|
625 | 117 |
type Env = Map[String, Int] |
118 |
||
648 | 119 |
|
120 |
||
625 | 121 |
// compile expressions |
648 | 122 |
def compile_exp(a: Exp, env : Env, k: Int) : (String, Int) = a match { |
123 |
case Num(i) => (i"%$k = add i32 0, i32 $i", k) |
|
124 |
case Var(s) => (i"%$k = add i32 0, i32 %${env(s)}", k) |
|
125 |
case Aop("+", a1, a2) => { |
|
126 |
val (cs1, k1) = compile_exp(a1, env, k) |
|
127 |
val (cs2, k2) = compile_exp(a2, env, k1 + 1) |
|
128 |
(cs1 ++ cs2 ++ i"%${k2+1} = add i32 %$k1, i32 %$k2", k2 + 1) |
|
129 |
} |
|
130 |
case Aop("-", a1, a2) => { |
|
131 |
val (cs1, k1) = compile_exp(a1, env, k) |
|
132 |
val (cs2, k2) = compile_exp(a2, env, k1 + 1) |
|
133 |
(cs1 ++ cs2 ++ i"%${k2+1} = sub i32 %$k1, i32 %$k2", k2 + 1) |
|
134 |
} |
|
135 |
case Aop("*", a1, a2) => { |
|
136 |
val (cs1, k1) = compile_exp(a1, env, k) |
|
137 |
val (cs2, k2) = compile_exp(a2, env, k1 + 1) |
|
138 |
(cs1 ++ cs2 ++ i"%${k2+1} = mul i32 %$k1, i32 %$k2", k2 + 1) |
|
139 |
} |
|
140 |
/* |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
case If(b, a1, a2) => { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
142 |
val if_else = Fresh("If_else") |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
143 |
val if_end = Fresh("If_end") |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
144 |
compile_bexp(b, env, if_else) ++ |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
145 |
compile_exp(a1, env) ++ |
625 | 146 |
i"goto $if_end" ++ |
147 |
l"$if_else" ++ |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
148 |
compile_exp(a2, env) ++ |
625 | 149 |
l"$if_end" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
150 |
} |
625 | 151 |
case Call(name, args) => { |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
152 |
val is = "I" * args.length |
625 | 153 |
args.map(a => compile_exp(a, env)).mkString ++ |
154 |
i"invokestatic XXX/XXX/$name($is)I" |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
155 |
} |
625 | 156 |
case Sequence(a1, a2) => { |
157 |
compile_exp(a1, env) ++ i"pop" ++ compile_exp(a2, env) |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
158 |
} |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
159 |
case Write(a1) => { |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
160 |
compile_exp(a1, env) ++ |
625 | 161 |
i"dup" ++ |
162 |
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
|
163 |
} |
648 | 164 |
*/ |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
|
648 | 167 |
val e = Aop("*", Aop("+", Num(2), Num(4)), Num(5)) |
168 |
compile_exp(e, Map(), 1)._1.mkString |
|
169 |
||
170 |
||
625 | 171 |
// compile boolean expressions |
172 |
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
173 |
case Bop("==", a1, a2) => |
625 | 174 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpne $jmp" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
case Bop("!=", a1, a2) => |
625 | 176 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpeq $jmp" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
177 |
case Bop("<", a1, a2) => |
625 | 178 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpge $jmp" |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
179 |
case Bop("<=", a1, a2) => |
625 | 180 |
compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"if_icmpgt $jmp" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
181 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
182 |
|
625 | 183 |
// compile function for declarations and main |
184 |
def compile_decl(d: Decl) : String = d match { |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
185 |
case Def(name, args, a) => { |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
186 |
val env = args.zipWithIndex.toMap |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
187 |
val is = "I" * args.length |
625 | 188 |
m".method public static $name($is)I" ++ |
189 |
m".limit locals ${args.length.toString}" ++ |
|
190 |
m".limit stack ${1 + max_stack_exp(a)}" ++ |
|
191 |
l"${name}_Start" ++ |
|
223
e4b29b57f6a3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
221
diff
changeset
|
192 |
compile_exp(a, env) ++ |
625 | 193 |
i"ireturn" ++ |
194 |
m".end method\n" |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
195 |
} |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
196 |
case Main(a) => { |
625 | 197 |
m".method public static main([Ljava/lang/String;)V" ++ |
198 |
m".limit locals 200" ++ |
|
199 |
m".limit stack 200" ++ |
|
223
e4b29b57f6a3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
221
diff
changeset
|
200 |
compile_exp(a, Map()) ++ |
625 | 201 |
i"invokestatic XXX/XXX/write(I)V" ++ |
202 |
i"return" ++ |
|
203 |
m".end method\n" |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
204 |
} |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
205 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
206 |
|
626 | 207 |
// main compiler functions |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
208 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
209 |
def time_needed[T](i: Int, code: => T) = { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
210 |
val start = System.nanoTime() |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
211 |
for (j <- 1 to i) code |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
212 |
val end = System.nanoTime() |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
213 |
(end - start)/(i * 1.0e9) |
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 |
|
645 | 216 |
def deserialise[T](fname: String) : Try[T] = { |
217 |
import scala.util.Using |
|
218 |
Using(new ObjectInputStream(new FileInputStream(fname))) { |
|
219 |
in => in.readObject.asInstanceOf[T] |
|
220 |
} |
|
644 | 221 |
} |
222 |
||
223 |
def compile(class_name: String) : String = { |
|
645 | 224 |
val ast = deserialise[List[Decl]](class_name ++ ".prs").getOrElse(Nil) |
626 | 225 |
val instructions = ast.map(compile_decl).mkString |
226 |
(library + instructions).replaceAllLiterally("XXX", class_name) |
|
227 |
} |
|
228 |
||
644 | 229 |
def compile_to_file(class_name: String) = { |
230 |
val output = compile(class_name) |
|
626 | 231 |
scala.tools.nsc.io.File(s"${class_name}.j").writeAll(output) |
232 |
} |
|
233 |
||
645 | 234 |
def compile_and_run(class_name: String) : Unit = { |
644 | 235 |
compile_to_file(class_name) |
626 | 236 |
(s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!! |
625 | 237 |
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
|
238 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
239 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
240 |
|
626 | 241 |
// some examples of .fun files |
645 | 242 |
//compile_to_file("fact") |
243 |
//compile_and_run("fact") |
|
244 |
//compile_and_run("defs") |
|
245 |
||
644 | 246 |
|
247 |
def main(args: Array[String]) = |
|
645 | 248 |
compile_and_run(args(0)) |
644 | 249 |
|
250 |
||
251 |
} |