author | Christian Urban <urbanc@in.tum.de> |
Wed, 02 Oct 2019 02:09:48 +0100 | |
changeset 644 | b4f5714485e1 |
parent 628 | 8067d0a8ba04 |
child 645 | 30943d5491b6 |
permissions | -rw-r--r-- |
625 | 1 |
// A Small Compiler for a Simple Functional Language |
644 | 2 |
// (includes an external lexer and parser) |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
644 | 4 |
import java.io._ |
625 | 5 |
|
644 | 6 |
object Compiler { |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
|
644 | 8 |
// Abstract syntax trees for the Fun language |
9 |
abstract class Exp extends Serializable |
|
10 |
abstract class BExp extends Serializable |
|
11 |
abstract class Decl extends Serializable |
|
626 | 12 |
|
13 |
case class Def(name: String, args: List[String], body: Exp) extends Decl |
|
14 |
case class Main(e: Exp) extends Decl |
|
15 |
||
16 |
case class Call(name: String, args: List[Exp]) extends Exp |
|
17 |
case class If(a: BExp, e1: Exp, e2: Exp) extends Exp |
|
18 |
case class Write(e: Exp) extends Exp |
|
19 |
case class Var(s: String) extends Exp |
|
20 |
case class Num(i: Int) extends Exp |
|
21 |
case class Aop(o: String, a1: Exp, a2: Exp) extends Exp |
|
22 |
case class Sequence(e1: Exp, e2: Exp) extends Exp |
|
23 |
case class Bop(o: String, a1: Exp, a2: Exp) extends BExp |
|
24 |
||
25 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
// compiler - built-in functions |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
// 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
|
28 |
// |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
30 |
val library = """ |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
.class public XXX.XXX |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
.super java/lang/Object |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
.method public <init>()V |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
aload_0 |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
invokenonvirtual java/lang/Object/<init>()V |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
return |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
.end method |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
.method public static write(I)V |
380
1e88390e81aa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
323
diff
changeset
|
41 |
.limit locals 1 |
1e88390e81aa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
323
diff
changeset
|
42 |
.limit stack 2 |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
380
1e88390e81aa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
323
diff
changeset
|
44 |
iload 0 |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
invokevirtual java/io/PrintStream/println(I)V |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
return |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
.end method |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
|
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 |
|
626 | 51 |
// calculating the maximal needed stack size |
52 |
def max_stack_exp(e: Exp): Int = e match { |
|
53 |
case Call(_, args) => args.map(max_stack_exp).sum |
|
54 |
case If(a, e1, e2) => max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) |
|
55 |
case Write(e) => max_stack_exp(e) + 1 |
|
56 |
case Var(_) => 1 |
|
57 |
case Num(_) => 1 |
|
58 |
case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
|
59 |
case Sequence(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max |
|
60 |
} |
|
644 | 61 |
|
626 | 62 |
def max_stack_bexp(e: BExp): Int = e match { |
63 |
case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
|
64 |
} |
|
65 |
||
66 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
// for generating new labels |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
var counter = -1 |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
def Fresh(x: String) = { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
counter += 1 |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
x ++ "_" ++ counter.toString() |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
|
625 | 75 |
// convenient string interpolations |
76 |
// for instructions, labels and methods |
|
77 |
import scala.language.implicitConversions |
|
78 |
import scala.language.reflectiveCalls |
|
79 |
||
80 |
implicit def sring_inters(sc: StringContext) = new { |
|
81 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" |
|
82 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" |
|
83 |
def m(args: Any*): String = sc.s(args:_*) ++ "\n" |
|
84 |
} |
|
85 |
||
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
|
625 | 87 |
type Env = Map[String, Int] |
88 |
||
89 |
// compile expressions |
|
90 |
def compile_exp(a: Exp, env : Env) : String = a match { |
|
91 |
case Num(i) => i"ldc $i" |
|
92 |
case Var(s) => i"iload ${env(s)}" |
|
93 |
case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"iadd" |
|
94 |
case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"isub" |
|
95 |
case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"imul" |
|
96 |
case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"idiv" |
|
97 |
case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ i"irem" |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
case If(b, a1, a2) => { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
val if_else = Fresh("If_else") |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
val if_end = Fresh("If_end") |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
compile_bexp(b, env, if_else) ++ |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
compile_exp(a1, env) ++ |
625 | 103 |
i"goto $if_end" ++ |
104 |
l"$if_else" ++ |
|
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
compile_exp(a2, env) ++ |
625 | 106 |
l"$if_end" |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
} |
625 | 108 |
case Call(name, args) => { |
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
109 |
val is = "I" * args.length |
625 | 110 |
args.map(a => compile_exp(a, env)).mkString ++ |
111 |
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
|
112 |
} |
625 | 113 |
case Sequence(a1, a2) => { |
114 |
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
|
115 |
} |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
116 |
case Write(a1) => { |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
117 |
compile_exp(a1, env) ++ |
625 | 118 |
i"dup" ++ |
119 |
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
|
120 |
} |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
|
625 | 123 |
// compile boolean expressions |
124 |
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
|
125 |
case Bop("==", a1, a2) => |
625 | 126 |
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
|
127 |
case Bop("!=", a1, a2) => |
625 | 128 |
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
|
129 |
case Bop("<", a1, a2) => |
625 | 130 |
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
|
131 |
case Bop("<=", a1, a2) => |
625 | 132 |
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
|
133 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
|
625 | 135 |
// compile function for declarations and main |
136 |
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
|
137 |
case Def(name, args, a) => { |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
138 |
val env = args.zipWithIndex.toMap |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
139 |
val is = "I" * args.length |
625 | 140 |
m".method public static $name($is)I" ++ |
141 |
m".limit locals ${args.length.toString}" ++ |
|
142 |
m".limit stack ${1 + max_stack_exp(a)}" ++ |
|
143 |
l"${name}_Start" ++ |
|
223
e4b29b57f6a3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
221
diff
changeset
|
144 |
compile_exp(a, env) ++ |
625 | 145 |
i"ireturn" ++ |
146 |
m".end method\n" |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
147 |
} |
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
148 |
case Main(a) => { |
625 | 149 |
m".method public static main([Ljava/lang/String;)V" ++ |
150 |
m".limit locals 200" ++ |
|
151 |
m".limit stack 200" ++ |
|
223
e4b29b57f6a3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
221
diff
changeset
|
152 |
compile_exp(a, Map()) ++ |
625 | 153 |
i"invokestatic XXX/XXX/write(I)V" ++ |
154 |
i"return" ++ |
|
155 |
m".end method\n" |
|
221
824ffbf66ab4
added fun tail
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
220
diff
changeset
|
156 |
} |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
157 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
158 |
|
626 | 159 |
// main compiler functions |
220
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
def time_needed[T](i: Int, code: => T) = { |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
val start = System.nanoTime() |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
for (j <- 1 to i) code |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
val end = System.nanoTime() |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
(end - start)/(i * 1.0e9) |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |
|
644 | 168 |
def deserialise[T](fname: String) : T = { |
169 |
val in = new ObjectInputStream(new FileInputStream(fname)) |
|
170 |
val data = in.readObject.asInstanceOf[T] |
|
171 |
in.close |
|
172 |
data |
|
173 |
} |
|
174 |
||
175 |
||
176 |
def compile(class_name: String) : String = { |
|
177 |
val ast = deserialise[List[Decl]](class_name ++ ".prs") |
|
626 | 178 |
val instructions = ast.map(compile_decl).mkString |
179 |
(library + instructions).replaceAllLiterally("XXX", class_name) |
|
180 |
} |
|
181 |
||
644 | 182 |
def compile_to_file(class_name: String) = { |
183 |
val output = compile(class_name) |
|
626 | 184 |
scala.tools.nsc.io.File(s"${class_name}.j").writeAll(output) |
185 |
} |
|
186 |
||
187 |
import scala.sys.process._ |
|
188 |
||
189 |
def compile_run(class_name: String) : Unit = { |
|
644 | 190 |
compile_to_file(class_name) |
626 | 191 |
(s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!! |
625 | 192 |
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
|
193 |
} |
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
194 |
|
141041fc76b5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
195 |
|
626 | 196 |
// some examples of .fun files |
197 |
//compile_file("fact") |
|
198 |
//compile_run("defs") |
|
644 | 199 |
//compile_run("fact") |
200 |
||
201 |
def main(args: Array[String]) = |
|
202 |
compile_run(args(0)) |
|
203 |
||
204 |
||
205 |
} |