author | Christian Urban <christian.urban@kcl.ac.uk> |
Sun, 25 Oct 2020 01:25:01 +0000 | |
changeset 789 | f0696713177b |
parent 734 | progs/fun/funt.scala@5d860ff01938 |
child 813 | 059f970287d1 |
permissions | -rw-r--r-- |
626 | 1 |
// A Small Compiler for a Simple Functional Language |
2 |
// (includes a lexer and a parser) |
|
3 |
||
789 | 4 |
import $file.fun_tokens, fun_tokens._ |
5 |
import $file.fun_parser, fun_parser._ |
|
626 | 6 |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
// compiler - built-in functions |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
// 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
|
9 |
// |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
val library = """ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
.class public XXX.XXX |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
.super java/lang/Object |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
.method public static write(I)V |
626 | 16 |
.limit locals 1 |
17 |
.limit stack 2 |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
626 | 19 |
iload 0 |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
invokevirtual java/io/PrintStream/println(I)V |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
return |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
.end method |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
|
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 |
|
626 | 26 |
// calculating the maximal needed stack size |
27 |
def max_stack_exp(e: Exp): Int = e match { |
|
28 |
case Call(_, args) => args.map(max_stack_exp).sum |
|
29 |
case If(a, e1, e2) => max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max) |
|
30 |
case Write(e) => max_stack_exp(e) + 1 |
|
31 |
case Var(_) => 1 |
|
32 |
case Num(_) => 1 |
|
33 |
case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
|
34 |
case Sequence(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max |
|
35 |
} |
|
789 | 36 |
|
626 | 37 |
def max_stack_bexp(e: BExp): Int = e match { |
38 |
case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2) |
|
39 |
} |
|
40 |
||
41 |
||
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
// for generating new labels |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
var counter = -1 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
def Fresh(x: String) = { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
counter += 1 |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
x ++ "_" ++ counter.toString() |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
|
626 | 50 |
// convenient string interpolations |
51 |
// for instructions, labels and methods |
|
52 |
import scala.language.implicitConversions |
|
53 |
import scala.language.reflectiveCalls |
|
54 |
||
55 |
implicit def sring_inters(sc: StringContext) = new { |
|
56 |
def i(args: Any*): String = " " ++ sc.s(args:_*) ++ "\n" |
|
57 |
def l(args: Any*): String = sc.s(args:_*) ++ ":\n" |
|
58 |
def m(args: Any*): String = sc.s(args:_*) ++ "\n" |
|
59 |
} |
|
60 |
||
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
|
626 | 62 |
type Env = Map[String, Int] |
63 |
||
64 |
||
65 |
def compile_expT(a: Exp, env : Env, name: String) : String = a match { |
|
66 |
case Num(i) => i"ldc $i" |
|
67 |
case Var(s) => i"iload ${env(s)}" |
|
68 |
case Aop("+", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"iadd" |
|
69 |
case Aop("-", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"isub" |
|
70 |
case Aop("*", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"imul" |
|
71 |
case Aop("/", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"idiv" |
|
72 |
case Aop("%", a1, a2) => compile_expT(a1, env, "") ++ compile_expT(a2, env, "") ++ i"irem" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
case If(b, a1, a2) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
val if_else = Fresh("If_else") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
val if_end = Fresh("If_end") |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
compile_bexpT(b, env, if_else) ++ |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
compile_expT(a1, env, name) ++ |
626 | 78 |
i"goto $if_end" ++ |
79 |
l"$if_else" ++ |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
compile_expT(a2, env, name) ++ |
626 | 81 |
l"$if_end" |
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
case Call(n, args) => if (name == n) { |
626 | 84 |
val stores = args.zipWithIndex.map { case (x, y) => i"istore $y" } |
85 |
args.map(a => compile_expT(a, env, "")).mkString ++ |
|
86 |
stores.reverse.mkString ++ |
|
87 |
i"goto ${n}_Start" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
} else { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
val is = "I" * args.length |
626 | 90 |
args.map(a => compile_expT(a, env, "")).mkString ++ |
91 |
i"invokestatic XXX/XXX/${n}(${is})I" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
} |
626 | 93 |
case Sequence(a1, a2) => { |
94 |
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
|
95 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
case Write(a1) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
compile_expT(a1, env, "") ++ |
626 | 98 |
i"dup" ++ |
99 |
i"invokestatic XXX/XXX/write(I)V" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
|
626 | 103 |
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
|
104 |
case Bop("==", a1, a2) => |
626 | 105 |
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
|
106 |
case Bop("!=", a1, a2) => |
626 | 107 |
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
|
108 |
case Bop("<", a1, a2) => |
626 | 109 |
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
|
110 |
case Bop("<=", a1, a2) => |
626 | 111 |
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
|
112 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
|
626 | 115 |
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
|
116 |
case Def(name, args, a) => { |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
val env = args.zipWithIndex.toMap |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
val is = "I" * args.length |
626 | 119 |
m".method public static $name($is)I" ++ |
120 |
m".limit locals ${args.length}" ++ |
|
121 |
m".limit stack ${1 + max_stack_exp(a)}" ++ |
|
122 |
l"${name}_Start" ++ |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
compile_expT(a, env, name) ++ |
626 | 124 |
i"ireturn" ++ |
125 |
m".end method\n" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
case Main(a) => { |
626 | 128 |
m".method public static main([Ljava/lang/String;)V" ++ |
129 |
m".limit locals 200" ++ |
|
130 |
m".limit stack 200" ++ |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
compile_expT(a, Map(), "") ++ |
626 | 132 |
i"invokestatic XXX/XXX/write(I)V" ++ |
789 | 133 |
i"return" ++ |
134 |
m".end method" |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
135 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
136 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
|
626 | 138 |
// main compiler functions |
789 | 139 |
def compile(prog: List[Decl], class_name: String) : String = { |
140 |
val instructions = prog.map(compile_decl).mkString |
|
626 | 141 |
(library + instructions).replaceAllLiterally("XXX", class_name) |
142 |
} |
|
143 |
||
789 | 144 |
|
145 |
@main |
|
146 |
def main(fname: String) = { |
|
147 |
val path = os.pwd / fname |
|
148 |
val class_name = fname.stripSuffix("." ++ path.ext) |
|
149 |
val tks = tokenise(os.read(path)) |
|
150 |
val ast = parse_tks(tks) |
|
151 |
println(compile(ast, class_name)) |
|
626 | 152 |
} |
153 |
||
789 | 154 |
/* |
155 |
||
626 | 156 |
import scala.sys.process._ |
157 |
||
158 |
def compile_run(class_name: String) : Unit = { |
|
159 |
compile_file(class_name) |
|
160 |
(s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!! |
|
161 |
println("Time: " + time_needed(2, (s"java ${class_name}/${class_name}").!)) |
|
541
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
} |
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
|
885cf83ebce3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
//examples |
626 | 166 |
compile_run("defs") |
167 |
compile_run("fact") |
|
789 | 168 |
*/ |