author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 24 Jan 2022 00:14:02 +0000 | |
changeset 870 | 739039774cee |
parent 869 | 81ee93b87258 |
child 901 | 33cff35bdc1a |
permissions | -rw-r--r-- |
626 | 1 |
// A Small Compiler for a Simple Functional Language |
813 | 2 |
// - includes a lexer and a parser |
3 |
// - performs tail-call optimisations |
|
4 |
// |
|
5 |
// call with |
|
6 |
// |
|
7 |
// amm fun.sc main defs.fun |
|
8 |
// amm fun.sc main fact.fun |
|
9 |
// |
|
10 |
// or |
|
11 |
// |
|
12 |
// amm fun.sc run defs.fun |
|
13 |
// amm fun.sc run fact.fun |
|
14 |
// |
|
15 |
// the first prints out the JVM instructions |
|
16 |
// the second runs the generated class files |
|
17 |
||
626 | 18 |
|
789 | 19 |
import $file.fun_tokens, fun_tokens._ |
20 |
import $file.fun_parser, fun_parser._ |
|
626 | 21 |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
// compiler - built-in functions |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
// |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
val library = """ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
.class public XXX.XXX |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
.super java/lang/Object |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
.method public static write(I)V |
626 | 31 |
.limit locals 1 |
32 |
.limit stack 2 |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
626 | 34 |
iload 0 |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
invokevirtual java/io/PrintStream/println(I)V |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
return |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
.end method |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
""" |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
|
626 | 41 |
// calculating the maximal needed stack size |
42 |
def max_stack_exp(e: Exp): Int = e match { |
|
43 |
case Call(_, args) => args.map(max_stack_exp).sum |
|
813 | 44 |
case If(a, e1, e2) => |
45 |
max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) |
|
626 | 46 |
case Write(e) => max_stack_exp(e) + 1 |
47 |
case Var(_) => 1 |
|
48 |
case Num(_) => 1 |
|
49 |
case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
|
50 |
case Sequence(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max |
|
51 |
} |
|
789 | 52 |
|
626 | 53 |
def max_stack_bexp(e: BExp): Int = e match { |
54 |
case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
|
55 |
} |
|
56 |
||
57 |
||
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
// for generating new labels |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
var counter = -1 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
def Fresh(x: String) = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
counter += 1 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
x ++ "_" ++ counter.toString() |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
626 | 66 |
// convenient string interpolations |
67 |
// for instructions, labels and methods |
|
68 |
import scala.language.implicitConversions |
|
69 |
import scala.language.reflectiveCalls |
|
70 |
||
71 |
implicit def sring_inters(sc: StringContext) = new { |
|
72 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" |
|
73 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" |
|
74 |
def m(args: Any*): String = sc.s(args:_*) ++ "\n" |
|
75 |
} |
|
76 |
||
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
|
626 | 78 |
type Env = Map[String, Int] |
79 |
||
813 | 80 |
def compile_op(op: String) = op match { |
81 |
case "+" => i"iadd" |
|
82 |
case "-" => i"isub" |
|
83 |
case "*" => i"imul" |
|
84 |
case "/" => i"idiv" |
|
85 |
case "%" => i"irem" |
|
86 |
} |
|
626 | 87 |
|
88 |
def compile_expT(a: Exp, env : Env, name: String) : String = a match { |
|
89 |
case Num(i) => i"ldc $i" |
|
90 |
case Var(s) => i"iload ${env(s)}" |
|
813 | 91 |
case Aop(op, a1, a2) => |
92 |
compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ compile_op(op) |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
case If(b, a1, a2) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
val if_else = Fresh("If_else") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
val if_end = Fresh("If_end") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
compile_bexpT(b, env, if_else) ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
compile_expT(a1, env, name) ++ |
626 | 98 |
i"goto $if_end" ++ |
99 |
l"$if_else" ++ |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
compile_expT(a2, env, name) ++ |
626 | 101 |
l"$if_end" |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
case Call(n, args) => if (name == n) { |
626 | 104 |
val stores = args.zipWithIndex.map { case (x, y) => i"istore $y" } |
105 |
args.map(a => compile_expT(a, env, "")).mkString ++ |
|
106 |
stores.reverse.mkString ++ |
|
107 |
i"goto ${n}_Start" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
} else { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
val is = "I" * args.length |
626 | 110 |
args.map(a => compile_expT(a, env, "")).mkString ++ |
111 |
i"invokestatic XXX/XXX/${n}(${is})I" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
} |
626 | 113 |
case Sequence(a1, a2) => { |
114 |
compile_expT(a1, env, "") ++ i"pop" ++ compile_expT(a2, env, name) |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
case Write(a1) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
compile_expT(a1, env, "") ++ |
626 | 118 |
i"dup" ++ |
119 |
i"invokestatic XXX/XXX/write(I)V" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
|
626 | 123 |
def compile_bexpT(b: BExp, env : Env, jmp: String) : String = b match { |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
case Bop("==", a1, a2) => |
626 | 125 |
compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpne $jmp" |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
case Bop("!=", a1, a2) => |
626 | 127 |
compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpeq $jmp" |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
case Bop("<", a1, a2) => |
626 | 129 |
compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpge $jmp" |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
case Bop("<=", a1, a2) => |
626 | 131 |
compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"if_icmpgt $jmp" |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
133 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
|
626 | 135 |
def compile_decl(d: Decl) : String = d match { |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
136 |
case Def(name, args, a) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
val env = args.zipWithIndex.toMap |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
138 |
val is = "I" * args.length |
626 | 139 |
m".method public static $name($is)I" ++ |
140 |
m".limit locals ${args.length}" ++ |
|
141 |
m".limit stack ${1 + max_stack_exp(a)}" ++ |
|
142 |
l"${name}_Start" ++ |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
143 |
compile_expT(a, env, name) ++ |
626 | 144 |
i"ireturn" ++ |
145 |
m".end method\n" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
146 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
147 |
case Main(a) => { |
626 | 148 |
m".method public static main([Ljava/lang/String;)V" ++ |
149 |
m".limit locals 200" ++ |
|
150 |
m".limit stack 200" ++ |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
151 |
compile_expT(a, Map(), "") ++ |
626 | 152 |
i"invokestatic XXX/XXX/write(I)V" ++ |
789 | 153 |
i"return" ++ |
154 |
m".end method" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
155 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
156 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
157 |
|
626 | 158 |
// main compiler functions |
789 | 159 |
def compile(prog: List[Decl], class_name: String) : String = { |
160 |
val instructions = prog.map(compile_decl).mkString |
|
626 | 161 |
(library + instructions).replaceAllLiterally("XXX", class_name) |
162 |
} |
|
163 |
||
869
81ee93b87258
changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents:
813
diff
changeset
|
164 |
// pre-2.5.0 ammonite |
81ee93b87258
changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents:
813
diff
changeset
|
165 |
// import ammonite.ops._ |
81ee93b87258
changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents:
813
diff
changeset
|
166 |
|
81ee93b87258
changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents:
813
diff
changeset
|
167 |
// post 2.5.0 ammonite |
870
739039774cee
ivy import not needed
Christian Urban <christian.urban@kcl.ac.uk>
parents:
869
diff
changeset
|
168 |
// import os._ |
813 | 169 |
|
170 |
def compile_to_file(prog: List[Decl], class_name: String) : Unit = |
|
171 |
write.over(pwd / s"$class_name.j", compile(prog, class_name)) |
|
172 |
||
173 |
def compile_and_run(prog: List[Decl], class_name: String) : Unit = { |
|
174 |
println(s"Start of compilation") |
|
175 |
compile_to_file(prog, class_name) |
|
176 |
println(s"generated $class_name.j file") |
|
177 |
os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call() |
|
178 |
println(s"generated $class_name.class file") |
|
179 |
println(s"Run program") |
|
180 |
os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit) |
|
181 |
println(s"done.") |
|
182 |
} |
|
183 |
||
789 | 184 |
|
185 |
@main |
|
186 |
def main(fname: String) = { |
|
187 |
val path = os.pwd / fname |
|
188 |
val class_name = fname.stripSuffix("." ++ path.ext) |
|
189 |
val tks = tokenise(os.read(path)) |
|
190 |
val ast = parse_tks(tks) |
|
191 |
println(compile(ast, class_name)) |
|
626 | 192 |
} |
193 |
||
813 | 194 |
@main |
195 |
def run(fname: String) = { |
|
196 |
val path = os.pwd / fname |
|
197 |
val class_name = fname.stripSuffix("." ++ path.ext) |
|
198 |
val tks = tokenise(os.read(path)) |
|
199 |
val ast = parse_tks(tks) |
|
200 |
compile_and_run(ast, class_name) |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
201 |
} |