author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 18 Sep 2020 00:33:03 +0100 | |
changeset 757 | ea0be0662be0 |
parent 716 | df7d47a507f8 |
permissions | -rw-r--r-- |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
1 |
// A Small Compiler for the WHILE Language |
674 | 2 |
// (it does not use a parser nor lexer) |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
3 |
|
624 | 4 |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
5 |
// the abstract syntax trees |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
6 |
abstract class Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
7 |
abstract class AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
8 |
abstract class BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
9 |
type Block = List[Stmt] |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
10 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
11 |
// statements |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
12 |
case object Skip extends Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
13 |
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
14 |
case class While(b: BExp, bl: Block) extends Stmt |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
15 |
case class Assign(s: String, a: AExp) extends Stmt |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
16 |
case class Write(s: String) extends Stmt |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
17 |
case class Read(s: String) extends Stmt |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
18 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
19 |
// arithmetic expressions |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
20 |
case class Var(s: String) extends AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
21 |
case class Num(i: Int) extends AExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
22 |
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp |
70
e6868bd2942b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
69
diff
changeset
|
23 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
24 |
// boolean expressions |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
25 |
case object True extends BExp |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
26 |
case object False extends BExp |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
27 |
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
|
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
30 |
// compiler headers needed for the JVM |
695
484b74bc057e
deleted init function from boilerplate code
Christian Urban <urbanc@in.tum.de>
parents:
690
diff
changeset
|
31 |
// (contains methods for read and write) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
32 |
val beginning = """ |
80
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
33 |
.class public XXX.XXX |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
34 |
.super java/lang/Object |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
35 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
36 |
.method public static write(I)V |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
37 |
.limit locals 1 |
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
38 |
.limit stack 2 |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
39 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
373
b018234c9126
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
369
diff
changeset
|
40 |
iload 0 |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
41 |
invokevirtual java/io/PrintStream/println(I)V |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
42 |
return |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
43 |
.end method |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
44 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
45 |
.method public static read()I |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
46 |
.limit locals 10 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
47 |
.limit stack 10 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
48 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
49 |
ldc 0 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
50 |
istore 1 ; this will hold our final integer |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
51 |
Label1: |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
52 |
getstatic java/lang/System/in Ljava/io/InputStream; |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
53 |
invokevirtual java/io/InputStream/read()I |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
54 |
istore 2 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
55 |
iload 2 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
56 |
ldc 10 ; the newline delimiter |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
57 |
isub |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
58 |
ifeq Label2 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
59 |
iload 2 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
60 |
ldc 32 ; the space delimiter |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
61 |
isub |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
62 |
ifeq Label2 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
63 |
|
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
64 |
iload 2 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
65 |
ldc 48 ; we have our digit in ASCII, have to subtract it from 48 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
66 |
isub |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
67 |
ldc 10 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
68 |
iload 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
69 |
imul |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
70 |
iadd |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
71 |
istore 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
72 |
goto Label1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
73 |
Label2: |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
74 |
;when we come here we have our integer computed in local variable 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
75 |
iload 1 |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
76 |
ireturn |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
77 |
.end method |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
78 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
79 |
.method public static main([Ljava/lang/String;)V |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
80 |
.limit locals 200 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
81 |
.limit stack 200 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
82 |
|
624 | 83 |
; COMPILED CODE STARTS |
84 |
||
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
85 |
""" |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
86 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
87 |
val ending = """ |
624 | 88 |
; COMPILED CODE ENDS |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
89 |
return |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
90 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
91 |
.end method |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
92 |
""" |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
93 |
|
624 | 94 |
// Compiler functions |
471 | 95 |
|
96 |
||
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
97 |
// for generating new labels |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
98 |
var counter = -1 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
99 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
100 |
def Fresh(x: String) = { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
101 |
counter += 1 |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
102 |
x ++ "_" ++ counter.toString() |
66
9215b9fb8852
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
} |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
104 |
|
624 | 105 |
// convenient string interpolations |
106 |
// for instructions and labels |
|
107 |
import scala.language.implicitConversions |
|
108 |
import scala.language.reflectiveCalls |
|
109 |
||
110 |
implicit def sring_inters(sc: StringContext) = new { |
|
111 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" |
|
112 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" |
|
113 |
} |
|
114 |
||
716 | 115 |
// this allows us to write things like |
690 | 116 |
// i"add" and l"Label" |
687 | 117 |
|
624 | 118 |
|
119 |
// environments |
|
668 | 120 |
type Env = Map[String, Int] |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
121 |
|
674 | 122 |
|
123 |
def compile_op(op: String) = op match { |
|
124 |
case "+" => i"iadd" |
|
125 |
case "-" => i"isub" |
|
126 |
case "*" => i"imul" |
|
127 |
} |
|
128 |
||
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
129 |
// arithmetic expression compilation |
624 | 130 |
def compile_aexp(a: AExp, env : Env) : String = a match { |
131 |
case Num(i) => i"ldc $i" |
|
687 | 132 |
case Var(s) => i"iload ${env(s)} \t\t; $s" |
674 | 133 |
case Aop(op, a1, a2) => |
134 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op) |
|
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
135 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
136 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
137 |
// boolean expression compilation |
687 | 138 |
// - the jump-label is for where to jump if the condition is not true |
624 | 139 |
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match { |
140 |
case True => "" |
|
141 |
case False => i"goto $jmp" |
|
687 | 142 |
case Bop("==", a1, a2) => |
624 | 143 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpne $jmp" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
144 |
case Bop("!=", a1, a2) => |
624 | 145 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpeq $jmp" |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
146 |
case Bop("<", a1, a2) => |
624 | 147 |
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpge $jmp" |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
148 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
149 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
150 |
// statement compilation |
624 | 151 |
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match { |
152 |
case Skip => ("", env) |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
153 |
case Assign(x, a) => { |
687 | 154 |
val index = env.getOrElse(x, env.keys.size) |
155 |
(compile_aexp(a, env) ++ i"istore $index \t\t; $x", env + (x -> index)) |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
156 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
157 |
case If(b, bl1, bl2) => { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
158 |
val if_else = Fresh("If_else") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
159 |
val if_end = Fresh("If_end") |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
160 |
val (instrs1, env1) = compile_block(bl1, env) |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
161 |
val (instrs2, env2) = compile_block(bl2, env1) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
162 |
(compile_bexp(b, env, if_else) ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
163 |
instrs1 ++ |
624 | 164 |
i"goto $if_end" ++ |
165 |
l"$if_else" ++ |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
166 |
instrs2 ++ |
624 | 167 |
l"$if_end", env2) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
168 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
169 |
case While(b, bl) => { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
170 |
val loop_begin = Fresh("Loop_begin") |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
171 |
val loop_end = Fresh("Loop_end") |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
172 |
val (instrs1, env1) = compile_block(bl, env) |
624 | 173 |
(l"$loop_begin" ++ |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
174 |
compile_bexp(b, env, loop_end) ++ |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
175 |
instrs1 ++ |
624 | 176 |
i"goto $loop_begin" ++ |
177 |
l"$loop_end", env1) |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
178 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
179 |
case Write(x) => |
687 | 180 |
(i"iload ${env(x)} \t\t; $x" ++ |
624 | 181 |
i"invokestatic XXX/XXX/write(I)V", env) |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
182 |
case Read(x) => { |
687 | 183 |
val index = env.getOrElse(x, env.keys.size) |
624 | 184 |
(i"invokestatic XXX/XXX/read()I" ++ |
687 | 185 |
i"istore $index \t\t; $x", env + (x -> index)) |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
186 |
} |
69
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
187 |
} |
cc3f7908b942
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
68
diff
changeset
|
188 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
189 |
// compilation of a block (i.e. list of instructions) |
624 | 190 |
def compile_block(bl: Block, env: Env) : (String, Env) = bl match { |
191 |
case Nil => ("", env) |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
192 |
case s::bl => { |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
193 |
val (instrs1, env1) = compile_stmt(s, env) |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
194 |
val (instrs2, env2) = compile_block(bl, env1) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
195 |
(instrs1 ++ instrs2, env2) |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
196 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
197 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
198 |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
199 |
// main compilation function for blocks |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
200 |
def compile(bl: Block, class_name: String) : String = { |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
201 |
val instructions = compile_block(bl, Map.empty)._1 |
690 | 202 |
(beginning ++ instructions ++ ending).replaceAllLiterally("XXX", class_name) |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
203 |
} |
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
204 |
|
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
205 |
|
609 | 206 |
// compiling and running files |
207 |
// |
|
208 |
// JVM files can be assembled with |
|
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
209 |
// |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
210 |
// java -jar jvm/jasmin-2.4/jasmin.jar fib.j |
80
191daa3ee29e
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
76
diff
changeset
|
211 |
// |
323
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
212 |
// and started with |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
213 |
// |
4ce07c4abdb4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
214 |
// java fib/fib |
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
215 |
|
609 | 216 |
|
217 |
||
471 | 218 |
import scala.util._ |
219 |
import scala.sys.process._ |
|
220 |
import scala.io |
|
221 |
||
222 |
def compile_tofile(bl: Block, class_name: String) = { |
|
223 |
val output = compile(bl, class_name) |
|
224 |
val fw = new java.io.FileWriter(class_name + ".j") |
|
225 |
fw.write(output) |
|
226 |
fw.close() |
|
227 |
} |
|
228 |
||
535 | 229 |
def compile_all(bl: Block, class_name: String) : Unit = { |
471 | 230 |
compile_tofile(bl, class_name) |
231 |
println("compiled ") |
|
232 |
val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!! |
|
233 |
println("assembled ") |
|
234 |
} |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
235 |
|
609 | 236 |
def time_needed[T](i: Int, code: => T) = { |
237 |
val start = System.nanoTime() |
|
238 |
for (j <- 1 to i) code |
|
239 |
val end = System.nanoTime() |
|
240 |
(end - start)/(i * 1.0e9) |
|
241 |
} |
|
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
242 |
|
609 | 243 |
|
244 |
def compile_run(bl: Block, class_name: String) : Unit = { |
|
245 |
println("Start compilation") |
|
246 |
compile_all(bl, class_name) |
|
624 | 247 |
println("running") |
609 | 248 |
println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!)) |
249 |
} |
|
250 |
||
76
373cf55a3ca5
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
75
diff
changeset
|
251 |
|
674 | 252 |
// Fibonacci numbers as a bare-bone test-case |
609 | 253 |
val fib_test = |
690 | 254 |
List(Assign("n", Num(9)), // n := 10; |
609 | 255 |
Assign("minus1",Num(0)), // minus1 := 0; |
256 |
Assign("minus2",Num(1)), // minus2 := 1; |
|
257 |
Assign("temp",Num(0)), // temp := 0; |
|
258 |
While(Bop("<",Num(0),Var("n")), // while n > 0 do { |
|
625 | 259 |
List(Assign("temp",Var("minus2")), // temp := minus2; |
609 | 260 |
Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), |
261 |
// minus2 := minus1 + minus2; |
|
262 |
Assign("minus1",Var("temp")), // minus1 := temp; |
|
263 |
Assign("n",Aop("-",Var("n"),Num(1))))), // n := n - 1 }; |
|
264 |
Write("minus1")) // write minus1 |
|
265 |
||
266 |
||
687 | 267 |
// the compiled file as string |
268 |
// |
|
269 |
//println(compile(fib_test, "fib")) |
|
270 |
||
271 |
||
609 | 272 |
compile_run(fib_test, "fib") |
273 |
||
274 |