author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 18 Oct 2024 05:59:04 +0100 | |
changeset 970 | 1d4659dd83fe |
parent 959 | 64ec1884d860 |
permissions | -rw-r--r-- |
864 | 1 |
// Compiler for JVM |
910 | 2 |
// |
3 |
// call with |
|
4 |
// |
|
5 |
// amm2 compiler.sc |
|
6 |
// |
|
864 | 7 |
|
8 |
import $file.lexer |
|
9 |
import lexer._ |
|
10 |
||
11 |
import $file.parser |
|
12 |
import parser._ |
|
13 |
||
14 |
||
15 |
val beginning = """ |
|
16 |
.class public XXX.XXX |
|
17 |
.super java/lang/Object |
|
18 |
||
19 |
.method public static write(I)V |
|
20 |
.limit locals 1 |
|
21 |
.limit stack 2 |
|
22 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
|
23 |
iload 0 |
|
24 |
invokevirtual java/io/PrintStream/print(I)V |
|
25 |
return |
|
26 |
.end method |
|
27 |
||
28 |
.method public static writes(Ljava/lang/String;)V |
|
29 |
.limit stack 2 |
|
30 |
.limit locals 1 |
|
31 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
|
32 |
aload 0 |
|
33 |
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V |
|
34 |
return |
|
35 |
.end method |
|
36 |
||
37 |
.method public static read()I |
|
38 |
.limit locals 10 |
|
39 |
.limit stack 10 |
|
40 |
||
41 |
ldc 0 |
|
42 |
istore 1 ; this will hold our final integer |
|
43 |
Label1: |
|
44 |
getstatic java/lang/System/in Ljava/io/InputStream; |
|
45 |
invokevirtual java/io/InputStream/read()I |
|
46 |
istore 2 |
|
47 |
iload 2 |
|
48 |
ldc 10 ; the newline delimiter |
|
49 |
isub |
|
50 |
ifeq Label2 |
|
51 |
iload 2 |
|
52 |
ldc 32 ; the space delimiter |
|
53 |
isub |
|
54 |
ifeq Label2 |
|
55 |
||
56 |
iload 2 |
|
57 |
ldc 48 ; we have our digit in ASCII, have to subtract it from 48 |
|
58 |
isub |
|
59 |
ldc 10 |
|
60 |
iload 1 |
|
61 |
imul |
|
62 |
iadd |
|
63 |
istore 1 |
|
64 |
goto Label1 |
|
65 |
Label2: |
|
66 |
;when we come here we have our integer computed in local variable 1 |
|
67 |
iload 1 |
|
68 |
ireturn |
|
69 |
.end method |
|
70 |
||
71 |
.method public static main([Ljava/lang/String;)V |
|
72 |
.limit locals 200 |
|
73 |
.limit stack 200 |
|
74 |
||
75 |
; COMPILED CODE STARTS |
|
76 |
||
77 |
""" |
|
78 |
||
79 |
val ending = """ |
|
80 |
; COMPILED CODE ENDS |
|
81 |
return |
|
82 |
||
83 |
.end method |
|
84 |
""" |
|
85 |
||
86 |
// Compiler |
|
87 |
||
88 |
var counter = -1 |
|
89 |
||
90 |
def Fresh(x: String) = { |
|
91 |
counter += 1 |
|
92 |
x ++ "_" ++ counter.toString() |
|
93 |
} |
|
94 |
||
920 | 95 |
extension (sc: StringContext) { |
864 | 96 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" |
97 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" |
|
98 |
} |
|
99 |
||
100 |
type Env = Map[String, Int] |
|
101 |
||
102 |
def compile_op(op: String) = op match { |
|
103 |
case "+" => i"iadd" |
|
104 |
case "-" => i"isub" |
|
105 |
case "*" => i"imul" |
|
106 |
case "/" => i"idiv" |
|
107 |
case "%" => i"irem" |
|
108 |
} |
|
109 |
||
110 |
def compile_aexp(a: AExp, env : Env) : String = a match { |
|
111 |
case Num(i) => i"ldc $i" |
|
112 |
case Var(s) => i"iload ${env(s)} \t\t; $s" |
|
113 |
case Aop(op, a1, a2) => |
|
114 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op) |
|
115 |
} |
|
116 |
||
910 | 117 |
def compile_bop(op: String) = op match { |
118 |
case "==" => "if_icmpne" |
|
119 |
case "!=" => "if_icmpeq" |
|
120 |
case "<" => "if_icmpge" |
|
121 |
case ">" => "if_icmple" |
|
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
122 |
case "<=" => "if_icmpgt" |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
123 |
case "=>" => "if_icmplt" |
910 | 124 |
} |
125 |
||
864 | 126 |
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { |
127 |
case True => "" |
|
128 |
case False => i"goto $jmp" |
|
129 |
case And(b1, b2) => compile_bexp(b1, env, jmp) ++ compile_bexp(b2, env, jmp) |
|
130 |
case Or(b1, b2) => { |
|
131 |
val b1_false = Fresh("Or_second"); |
|
132 |
val or_end = Fresh("Or_end"); |
|
133 |
compile_bexp(b1, env, b1_false) ++ |
|
134 |
i"goto $or_end" ++ |
|
135 |
l"$b1_false" ++ |
|
136 |
compile_bexp(b2, env, jmp) ++ |
|
137 |
l"$or_end" |
|
138 |
} |
|
910 | 139 |
case Bop(op, a1, a2) => |
140 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"${compile_bop(op)} $jmp" |
|
864 | 141 |
} |
142 |
||
920 | 143 |
def compile_stmt(s: Stmt, env: Env, break: String) : (String, Env) = s match { |
864 | 144 |
case Skip => ("", env) |
145 |
case Assign(x, a) => { |
|
146 |
val index = env.getOrElse(x, env.keys.size) |
|
147 |
(compile_aexp(a, env) ++ i"istore $index \t\t; $x", env + (x -> index)) |
|
148 |
} |
|
149 |
case If(b, bl1, bl2) => { |
|
150 |
val if_else = Fresh("If_else") |
|
151 |
val if_end = Fresh("If_end") |
|
920 | 152 |
val (instrs1, env1) = compile_block(bl1, env, break) |
153 |
val (instrs2, env2) = compile_block(bl2, env1, break) |
|
864 | 154 |
(compile_bexp(b, env, if_else) ++ |
155 |
instrs1 ++ |
|
156 |
i"goto $if_end" ++ |
|
157 |
l"$if_else" ++ |
|
158 |
instrs2 ++ |
|
159 |
l"$if_end", env2) |
|
160 |
} |
|
161 |
case While(b, bl) => { |
|
162 |
val loop_begin = Fresh("Loop_begin") |
|
163 |
val loop_end = Fresh("Loop_end") |
|
920 | 164 |
val (instrs1, env1) = compile_block(bl, env, break) |
864 | 165 |
(l"$loop_begin" ++ |
166 |
compile_bexp(b, env, loop_end) ++ |
|
167 |
instrs1 ++ |
|
168 |
i"goto $loop_begin" ++ |
|
169 |
l"$loop_end", env1) |
|
170 |
} |
|
171 |
case For(id, lower, upper, code) => { |
|
920 | 172 |
val (assignment_code, env1) = compile_stmt(Assign(id, lower), env, break) // id := lower; |
864 | 173 |
val while_equivalent = While( |
174 |
Or(Bop("<", Var(id), upper), Bop("==", Var(id), upper)), // while id <= upper do { |
|
175 |
code ++ // code |
|
176 |
List( |
|
177 |
Assign(id, Aop("+", Var(id), Num(1))) // id := id + 1 |
|
178 |
)) // }; |
|
920 | 179 |
val new_break = Fresh("for_break") |
180 |
val (while_code, env2) = compile_stmt(while_equivalent, env1, new_break) |
|
181 |
(assignment_code ++ while_code ++ l"$new_break", env2) |
|
864 | 182 |
} |
920 | 183 |
case Break => (i"goto $break", env) |
864 | 184 |
case WriteId(x) => (i"iload ${env(x)} \t\t; $x" ++ |
185 |
i"invokestatic XXX/XXX/write(I)V", env) |
|
186 |
case WriteString(x) => (s" ldc ${x}\n" ++ |
|
187 |
i"invokestatic XXX/XXX/writes(Ljava/lang/String;)V", env) |
|
188 |
case Read(x) => { |
|
189 |
val index = env.getOrElse(x, env.keys.size) |
|
190 |
(i"invokestatic XXX/XXX/read()I" ++ |
|
191 |
i"istore $index \t\t; $x", env + (x -> index)) |
|
192 |
} |
|
193 |
} |
|
194 |
||
920 | 195 |
def compile_block(bl: Block, env: Env, break: String) : (String, Env) = bl match { |
864 | 196 |
case Nil => ("", env) |
197 |
case s::bl => { |
|
920 | 198 |
val (instrs1, env1) = compile_stmt(s, env, break) |
199 |
val (instrs2, env2) = compile_block(bl, env1, break) |
|
864 | 200 |
(instrs1 ++ instrs2, env2) |
201 |
} |
|
202 |
} |
|
203 |
||
204 |
def compile(bl: Block, class_name: String) : String = { |
|
920 | 205 |
val prog_end = Fresh("Prog_end") |
206 |
val instructions = compile_block(bl, Map.empty, prog_end)._1 |
|
207 |
(beginning ++ instructions ++ l"$prog_end" ++ ending).replaceAllLiterally("XXX", class_name) |
|
864 | 208 |
} |
209 |
||
210 |
// Compiling and running |
|
211 |
||
212 |
||
920 | 213 |
def compile_to_file(bl: Block, class_name: String) : Unit = |
214 |
os.write.over(os.pwd / s"$class_name.j", compile(bl, class_name)) |
|
864 | 215 |
|
216 |
def compile_all(bl: Block, class_name: String) : Unit = { |
|
920 | 217 |
println(s"Start of compilation") |
218 |
compile_to_file(bl, class_name) |
|
219 |
println(s"generated $class_name.j file") |
|
220 |
os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call() |
|
221 |
println(s"generated $class_name.class file ") |
|
864 | 222 |
} |
223 |
||
224 |
def time_needed[T](i: Int, code: => T) = { |
|
225 |
val start = System.nanoTime() |
|
226 |
for (j <- 1 to i) code |
|
227 |
val end = System.nanoTime() |
|
228 |
(end - start)/(i * 1.0e9) |
|
229 |
} |
|
230 |
||
231 |
def compile_run(bl: Block, class_name: String) : Unit = { |
|
232 |
compile_all(bl, class_name) |
|
233 |
println("running") |
|
920 | 234 |
os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit) |
235 |
println(s"done.") |
|
864 | 236 |
} |
237 |
||
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
238 |
//compile_run(Stmts.parse_all(tokenise(os.read(os.pwd / "pr.while"))).head, "pr") |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
239 |
|
864 | 240 |
// ---- Q1 |
241 |
||
242 |
// Fibonacci |
|
243 |
||
244 |
val fibonacciProgram = """write "Fib"; |
|
245 |
read n; |
|
246 |
minus1 := 0; |
|
247 |
minus2 := 1; |
|
248 |
while n > 0 do { |
|
249 |
temp := minus2; |
|
250 |
minus2 := minus1 + minus2; |
|
251 |
minus1 := temp; |
|
252 |
n := n - 1 |
|
253 |
}; |
|
254 |
write "Result"; |
|
255 |
write minus2""" |
|
256 |
||
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
257 |
//compile_all(Stmts.parse_all(tokenise(fibonacciProgram)).head, "fib") |
864 | 258 |
|
259 |
val factorialProgram = """write "Factorial"; |
|
260 |
read n; |
|
261 |
fact := 1; |
|
262 |
||
263 |
while n > 0 do { |
|
264 |
fact := n * fact; |
|
265 |
n := n - 1 |
|
266 |
}; |
|
267 |
||
268 |
write "Result"; |
|
269 |
write fact |
|
270 |
""" |
|
271 |
||
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
272 |
//compile_all(Stmts.parse_all(tokenise(factorialProgram)).head, "factorial") |
864 | 273 |
|
274 |
// ---- Q3 |
|
275 |
||
276 |
/* compile_run(Stmts.parse_all(tokenise("""for i := 1 upto 10 do { |
|
277 |
for i := 1 upto 10 do { |
|
278 |
write i |
|
279 |
} |
|
280 |
}""")).head, "nestedloop") */ |
|
281 |
||
282 |
||
920 | 283 |
//compile_run(Stmts.parse_all(tokenise(os.read(os.pwd / "collatz2.while"))).head, "collatz2") |
284 |
||
285 |
||
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
286 |
//println(tokenise(os.read(os.pwd / "forloop.while"))) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
287 |
//compile_all(Stmts.parse_all(tokenise(os.read(os.pwd / "forloop.while"))).head, "forloop") |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
288 |
|
921 | 289 |
|
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
290 |
//compile_all(Stmts.parse_all(tokenise(os.read(os.pwd / "primes.while"))).head, "primes") |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
291 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
292 |
//compile_run(Stmts.parse_all(tokenise(os.read(os.pwd / "pr.while"))).head, "pr") |
921 | 293 |
|
294 |
||
295 |
// for automated testing |
|
296 |
||
297 |
@main |
|
298 |
def main(file: String) = { |
|
299 |
// empty - nothing to run |
|
300 |
} |
|
301 |
||
302 |
@main |
|
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
303 |
def run(file: String) = { |
921 | 304 |
val contents = os.read(os.pwd / file) |
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
305 |
val class_name = file.stripSuffix(".while") |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
306 |
val tks = tokenise(os.read(os.pwd / file)) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
307 |
//println(tks) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
308 |
//println(Stmts.parse(tks)) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
309 |
compile_run(Stmts.parse_all(tks).head, class_name) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
310 |
//tokenise(contents) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
311 |
//println(Stmts.parse_all(tokenise(os.read(os.pwd / file))).head) |
921 | 312 |
} |
313 |
||
314 |
||
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
315 |
//println("TEST") |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
316 |
//println(tokenise(os.read(os.pwd / "pr.while"))) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
317 |
//println(Stmts.parse_all(tokenise(os.read(os.pwd / "pr.while"))).head) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
318 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
319 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
320 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
321 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
322 |
/* For with scopes |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
323 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
324 |
case For(x,a,b,bl) => { |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
325 |
val loop_begin = Fresh("Loop_begin") |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
326 |
val loop_end = Fresh("Loop_end") |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
327 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
328 |
val ind = env.getOrElse(x, -1) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
329 |
val new_ind = Fresh_index() |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
330 |
val (assign,env1) = (compile_aexp(a, env) ++ i"istore $new_ind \t\t; ${x}-new", env + (x -> new_ind)) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
331 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
332 |
val bool = l"$loop_begin" ++ compile_aexp(Var(x), env1) ++ compile_aexp(b, env1) ++ i"if_icmpgt $loop_end" |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
333 |
val (block, env2) = compile_block(bl,env1,loop_end) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
334 |
val inc = compile_stmt(Assign(x,Aop("+", Var(x), Num(1))),env2, "")._1 ++ i"goto $loop_begin" ++ l"$loop_end" |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
335 |
(assign ++ bool ++ block ++ inc, if (ind != -1){env2 + (x -> ind)} else {env2 - x}) |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
336 |
} |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
337 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
338 |
|
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
921
diff
changeset
|
339 |
*/ |