equal
deleted
inserted
replaced
4 // WHILE programs |
4 // WHILE programs |
5 // |
5 // |
6 // - transpiles BF programs into WHILE programs |
6 // - transpiles BF programs into WHILE programs |
7 // and compiles and runs them |
7 // and compiles and runs them |
8 // |
8 // |
|
9 // - makes array access safe by explicit bound checks |
|
10 // and perform defaults when bounds are exceeded |
|
11 // |
|
12 // - uses goto_w for jumps in while-loops |
|
13 // |
9 // Call with |
14 // Call with |
10 // |
15 // |
11 // scala compile_arr.scala |
16 // scala compile_arr3.scala |
12 |
17 |
13 // Mandelbrot |
18 // Mandelbrot |
14 // mand.j size 232266 |
19 // mand.j size 605196 |
15 // mand.class size 21787 |
20 // mand.class size 56812 |
16 // running time 16 secs |
21 // running time 26 secs |
17 |
22 |
18 // the abstract syntax trees |
23 // the abstract syntax trees |
19 abstract class Stmt |
24 abstract class Stmt |
20 abstract class AExp |
25 abstract class AExp |
21 abstract class BExp |
26 abstract class BExp |
177 l"$if_end", env2) |
182 l"$if_end", env2) |
178 } |
183 } |
179 case While(b, bl) => { |
184 case While(b, bl) => { |
180 val loop_begin = Fresh("Loop_begin") |
185 val loop_begin = Fresh("Loop_begin") |
181 val loop_end = Fresh("Loop_end") |
186 val loop_end = Fresh("Loop_end") |
|
187 val loop_false = Fresh("Loop_false") |
|
188 val loop_true = Fresh("Loop_true") |
182 val (instrs1, env1) = compile_block(bl, env) |
189 val (instrs1, env1) = compile_block(bl, env) |
183 (l"$loop_begin" ++ |
190 (l"$loop_begin" ++ |
184 compile_bexp(b, env, loop_end) ++ |
191 compile_bexp(b, env, loop_false) ++ |
|
192 i"goto $loop_true" ++ |
|
193 l"$loop_false" ++ |
|
194 i"goto_w $loop_end" ++ |
|
195 l"$loop_true" ++ |
185 instrs1 ++ |
196 instrs1 ++ |
186 i"goto $loop_begin" ++ |
197 i"goto_w $loop_begin" ++ |
187 l"$loop_end", env1) |
198 l"$loop_end", env1) |
188 } |
199 } |
189 case Write(x) => |
200 case Write(x) => |
190 (compile_iload(env(x)) ++ |
201 (compile_iload(env(x)) ++ |
191 i"invokestatic XXX/XXX/write(I)V", env) |
202 i"invokestatic XXX/XXX/write(I)V", env) |
210 compile_aload(env(s)) ++ |
221 compile_aload(env(s)) ++ |
211 i"dup2" ++ |
222 i"dup2" ++ |
212 i"arraylength" ++ |
223 i"arraylength" ++ |
213 i"if_icmple $arr_safe" ++ |
224 i"if_icmple $arr_safe" ++ |
214 i"pop2" ++ |
225 i"pop2" ++ |
215 i"goto $arr_end" ++ |
226 i"goto_w $arr_end" ++ |
216 l"$arr_safe" ++ |
227 l"$arr_safe" ++ |
217 i"swap" ++ |
228 i"swap" ++ |
218 compile_aexp(a2, env) ++ |
229 compile_aexp(a2, env) ++ |
219 i"iastore" ++ |
230 i"iastore" ++ |
220 l"$arr_end", env) |
231 l"$arr_end", env) |