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