--- a/progs/compile.scala Thu Apr 09 07:42:23 2015 +0100
+++ b/progs/compile.scala Fri Apr 10 18:02:04 2015 +0100
@@ -1,134 +1,33 @@
-// A Compiler for the WHILE language
+// A Small Compiler for the WHILE Language
//
-import matcher._
-import parser._
-
-// some regular expressions
-val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
-val DIGIT = RANGE("0123456789")
-val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT)))
-val NUM = PLUS(DIGIT)
-val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "true", "false", "write")
-val SEMI: Rexp = ";"
-val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!=", "<", ">")
-val WHITESPACE = PLUS(RANGE(" \n"))
-val RPAREN: Rexp = ")"
-val LPAREN: Rexp = "("
-val BEGIN: Rexp = "{"
-val END: Rexp = "}"
-val COMMENT = SEQS("/*", NOT(SEQS(STAR(ALLC), "*/", STAR(ALLC))), "*/")
-
-// tokens for classifying the strings that have been recognised
-abstract class Token
-case object T_WHITESPACE extends Token
-case object T_COMMENT extends Token
-case object T_SEMI extends Token
-case object T_LPAREN extends Token
-case object T_RPAREN extends Token
-case object T_BEGIN extends Token
-case object T_END extends Token
-case class T_ID(s: String) extends Token
-case class T_OP(s: String) extends Token
-case class T_NUM(s: String) extends Token
-case class T_KWD(s: String) extends Token
-
-val lexing_rules: List[(Rexp, List[Char] => Token)] =
- List((KEYWORD, (s) => T_KWD(s.mkString)),
- (ID, (s) => T_ID(s.mkString)),
- (OP, (s) => T_OP(s.mkString)),
- (NUM, (s) => T_NUM(s.mkString)),
- (SEMI, (s) => T_SEMI),
- (LPAREN, (s) => T_LPAREN),
- (RPAREN, (s) => T_RPAREN),
- (BEGIN, (s) => T_BEGIN),
- (END, (s) => T_END),
- (WHITESPACE, (s) => T_WHITESPACE),
- (COMMENT, (s) => T_COMMENT))
-
-// the tokenizer
-val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE, T_COMMENT))
// the abstract syntax trees
abstract class Stmt
abstract class AExp
abstract class BExp
type Block = List[Stmt]
+
+// statements
case object Skip extends Stmt
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
case class While(b: BExp, bl: Block) extends Stmt
case class Assign(s: String, a: AExp) extends Stmt
case class Write(s: String) extends Stmt
+case class Read(s: String) extends Stmt
+// arithmetic expressions
case class Var(s: String) extends AExp
case class Num(i: Int) extends AExp
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
+// boolean expressions
case object True extends BExp
case object False extends BExp
-case class Relop(o: String, a1: AExp, a2: AExp) extends BExp
-
-// atomic parsers
-case class TokParser(tok: Token) extends Parser[List[Token], Token] {
- def parse(ts: List[Token]) = ts match {
- case t::ts if (t == tok) => Set((t, ts))
- case _ => Set ()
- }
-}
-implicit def token2tparser(t: Token) = TokParser(t)
-
-case object NumParser extends Parser[List[Token], Int] {
- def parse(ts: List[Token]) = ts match {
- case T_NUM(s)::ts => Set((s.toInt, ts))
- case _ => Set ()
- }
-}
-
-case object IdParser extends Parser[List[Token], String] {
- def parse(ts: List[Token]) = ts match {
- case T_ID(s)::ts => Set((s, ts))
- case _ => Set ()
- }
-}
+case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
-// arithmetic expressions
-lazy val AExp: Parser[List[Token], AExp] =
- (T ~ T_OP("+") ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } ||
- (T ~ T_OP("-") ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || T
-lazy val T: Parser[List[Token], AExp] =
- (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || F
-lazy val F: Parser[List[Token], AExp] =
- (T_LPAREN ~> AExp <~ T_RPAREN) ||
- IdParser ==> Var ||
- NumParser ==> Num
-
-// boolean expressions
-lazy val BExp: Parser[List[Token], BExp] =
- (T_KWD("true") ==> ((_) => True: BExp)) ||
- (T_KWD("false") ==> ((_) => False: BExp)) ||
- (T_LPAREN ~> BExp <~ T_RPAREN) ||
- (AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Relop("=", x, z): BExp } ||
- (AExp ~ T_OP("!=") ~ AExp) ==> { case ((x, y), z) => Relop("!=", x, z): BExp } ||
- (AExp ~ T_OP("<") ~ AExp) ==> { case ((x, y), z) => Relop("<", x, z): BExp } ||
- (AExp ~ T_OP(">") ~ AExp) ==> { case ((x, y), z) => Relop("<", z, x): BExp }
-
-lazy val Stmt: Parser[List[Token], Stmt] =
- (T_KWD("skip") ==> ((_) => Skip: Stmt)) ||
- (IdParser ~ T_OP(":=") ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } ||
- (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==>
- { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } ||
- (T_KWD("while") ~ BExp ~ T_KWD("do") ~ Block) ==> { case (((x, y), z), w) => While(y, w) } ||
- (T_KWD("write") ~ IdParser) ==> { case (x, y) => Write(y) }
-
-lazy val Stmts: Parser[List[Token], Block] =
- (Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
- (Stmt ==> ((s) => List(s) : Block))
-
-lazy val Block: Parser[List[Token], Block] =
- (T_BEGIN ~> Stmts <~ T_END) ||
- (Stmt ==> ((s) => List(s)))
-
-// compiler
+// compiler headers needed for the JVM
+// (contains an init method, as well as methods for read and write)
val beginning = """
.class public XXX.XXX
.super java/lang/Object
@@ -149,6 +48,39 @@
return
.end method
+.method public static read()I
+ .limit locals 10
+ .limit stack 10
+
+ ldc 0
+ istore 1 ; this will hold our final integer
+Label1:
+ getstatic java/lang/System/in Ljava/io/InputStream;
+ invokevirtual java/io/InputStream/read()I
+ istore 2
+ iload 2
+ ldc 10 ; the newline delimiter
+ isub
+ ifeq Label2
+ iload 2
+ ldc 32 ; the space delimiter
+ isub
+ ifeq Label2
+
+ iload 2
+ ldc 48 ; we have our digit in ASCII, have to subtract it from 48
+ isub
+ ldc 10
+ iload 1
+ imul
+ iadd
+ istore 1
+ goto Label1
+Label2:
+ ;when we come here we have our integer computed in local variable 1
+ iload 1
+ ireturn
+.end method
.method public static main([Ljava/lang/String;)V
.limit locals 200
@@ -171,9 +103,11 @@
x ++ "_" ++ counter.toString()
}
+// environments and instructions
type Env = Map[String, String]
type Instrs = List[String]
+// arithmetic expression compilation
def compile_aexp(a: AExp, env : Env) : Instrs = a match {
case Num(i) => List("ldc " + i.toString + "\n")
case Var(s) => List("iload " + env(s) + "\n")
@@ -182,18 +116,19 @@
case Aop("*", a1, a2) => compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("imul\n")
}
+// boolean expression compilation
def compile_bexp(b: BExp, env : Env, jmp: String) : Instrs = b match {
case True => Nil
case False => List("goto " + jmp + "\n")
- case Relop("=", a1, a2) =>
+ case Bop("=", a1, a2) =>
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpne " + jmp + "\n")
- case Relop("!=", a1, a2) =>
+ case Bop("!=", a1, a2) =>
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpeq " + jmp + "\n")
- case Relop("<", a1, a2) =>
+ case Bop("<", a1, a2) =>
compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("if_icmpge " + jmp + "\n")
}
-
+// statement compilation
def compile_stmt(s: Stmt, env: Env) : (Instrs, Env) = s match {
case Skip => (Nil, env)
case Assign(x, a) => {
@@ -204,8 +139,8 @@
case If(b, bl1, bl2) => {
val if_else = Fresh("If_else")
val if_end = Fresh("If_end")
- val (instrs1, env1) = compile_bl(bl1, env)
- val (instrs2, env2) = compile_bl(bl2, env1)
+ val (instrs1, env1) = compile_block(bl1, env)
+ val (instrs2, env2) = compile_block(bl2, env1)
(compile_bexp(b, env, if_else) ++
instrs1 ++
List("goto " + if_end + "\n") ++
@@ -216,7 +151,7 @@
case While(b, bl) => {
val loop_begin = Fresh("Loop_begin")
val loop_end = Fresh("Loop_end")
- val (instrs1, env1) = compile_bl(bl, env)
+ val (instrs1, env1) = compile_block(bl, env)
(List("\n" + loop_begin + ":\n\n") ++
compile_bexp(b, env, loop_end) ++
instrs1 ++
@@ -225,102 +160,58 @@
}
case Write(x) =>
(List("iload " + env(x) + "\n" + "invokestatic XXX/XXX/write(I)V\n"), env)
+ case Read(x) => {
+ val index = if (env.isDefinedAt(x)) env(x) else env.keys.size.toString
+ (List("invokestatic XXX/XXX/read()I\n" +
+ "istore " + index + "\n"), env + (x -> index))
+ }
}
-def compile_bl(bl: Block, env: Env) : (Instrs, Env) = bl match {
+// compilation of a block (i.e. list of instructions)
+def compile_block(bl: Block, env: Env) : (Instrs, Env) = bl match {
case Nil => (Nil, env)
case s::bl => {
val (instrs1, env1) = compile_stmt(s, env)
- val (instrs2, env2) = compile_bl(bl, env1)
+ val (instrs2, env2) = compile_block(bl, env1)
(instrs1 ++ instrs2, env2)
}
}
-def compile(input: String) : String = {
- val class_name = input.split('.')(0)
- val tks = Tok.fromFile(input)
- val ast = Stmts.parse_single(tks)
- val instructions = compile_bl(ast, Map.empty)._1
+// main compilation function for blocks
+def compile(bl: Block, class_name: String) : String = {
+ val instructions = compile_block(bl, Map.empty)._1
(beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
}
-def compile_to(input: String, output: String) = {
- val fw = new java.io.FileWriter(output)
- fw.write(compile(input))
- fw.close()
-}
-
-//
-val tks = Tok.fromString("x := x + 1")
-val ast = Stmt.parse_single(tks)
-println(compile_stmt(ast, Map("x" -> "n"))._1.mkString)
+// Fibonacci numbers as a test-case
+val fib_test =
+ List(Read("n"), // read n;
+ Assign("minus1",Num(0)), // minus1 := 0;
+ Assign("minus2",Num(1)), // minus2 := 1;
+ Assign("temp",Num(0)), // temp := 0;
+ While(Bop("<",Num(0),Var("n")), // while n > 0 do {
+ List(Assign("temp",Var("minus2")), // temp := minus2;
+ Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), // minus2 := minus1 + minus2;
+ Assign("minus1",Var("temp")), // minus1 := temp;
+ Assign("n",Aop("-",Var("n"),Num(1))))), // n := n - 1 };
+ Write("minus1")) // write minus1
-//examples
-
-compile_to("loops.while", "loops.j")
-//compile_to("fib.while", "fib.j")
-
-
-// testing cases for time measurements
+// prints out the JVM-assembly program
-def time_needed[T](i: Int, code: => T) = {
- val start = System.nanoTime()
- for (j <- 1 to i) code
- val end = System.nanoTime()
- (end - start)/(i * 1.0e9)
-}
-
-// for testing
-import scala.sys.process._
+println(compile(fib_test, "fib"))
-val test_prog = """
-start := XXX;
-x := start;
-y := start;
-z := start;
-while 0 < x do {
- while 0 < y do {
- while 0 < z do {
- z := z - 1
- };
- z := start;
- y := y - 1
- };
- y := start;
- x := x - 1
-};
-write x;
-write y;
-write z
-"""
-
-
-def compile_test(n: Int) : Unit = {
- val class_name = "LOOP"
- val tks = Tok.fromString(test_prog.replaceAllLiterally("XXX", n.toString))
- val ast = Stmts.parse_single(tks)
- val instructions = compile_bl(ast, Map.empty)._1
- val assembly = (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
- val fw = new java.io.FileWriter(class_name + ".j")
- fw.write(assembly)
- fw.close()
- val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!!
- println(n + " " + time_needed(2, ("java " + class_name + "/" + class_name).!!))
-}
-
-List(1, 5000, 10000, 50000, 100000, 250000, 500000, 750000, 1000000).map(compile_test(_))
-
-
-
-// Javabyte code assmbler
+// can be assembled with
+//
+// java -jar jvm/jasmin-2.4/jasmin.jar fib.j
//
-// java -jar jvm/jasmin-2.4/jasmin.jar loops.j
+// and started with
+//
+// java fib/fib
-
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/fun-bare.scala Fri Apr 10 18:02:04 2015 +0100
@@ -0,0 +1,174 @@
+// A Small Compiler for a simple functional language
+
+// Abstract syntax trees
+abstract class Exp
+abstract class BExp
+abstract class Decl
+
+// function declarations
+case class Def(name: String, args: List[String], body: Exp) extends Decl
+case class Main(e: Exp) extends Decl
+
+// expressions
+case class Call(name: String, args: List[Exp]) extends Exp
+case class If(a: BExp, e1: Exp, e2: Exp) extends Exp
+case class Write(e: Exp) extends Exp
+case class Var(s: String) extends Exp
+case class Num(i: Int) extends Exp
+case class Aop(o: String, a1: Exp, a2: Exp) extends Exp
+case class Sequ(e1: Exp, e2: Exp) extends Exp
+
+// boolean expressions
+case class Bop(o: String, a1: Exp, a2: Exp) extends BExp
+
+// calculating the maximal needed stack size
+def max_stack_exp(e: Exp): Int = e match {
+ case Call(_, args) => args.map(max_stack_exp).sum
+ case If(a, e1, e2) => max_stack_bexp(a) + (List(max_stack_exp(e1), max_stack_exp(e2)).max)
+ case Write(e) => max_stack_exp(e) + 1
+ case Var(_) => 1
+ case Num(_) => 1
+ case Aop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2)
+ case Sequ(e1, e2) => List(max_stack_exp(e1), max_stack_exp(e2)).max
+}
+def max_stack_bexp(e: BExp): Int = e match {
+ case Bop(_, a1, a2) => max_stack_exp(a1) + max_stack_exp(a2)
+}
+
+// compiler - built-in functions
+// copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html
+//
+
+val library = """
+.class public XXX.XXX
+.super java/lang/Object
+
+.method public <init>()V
+ aload_0
+ invokenonvirtual java/lang/Object/<init>()V
+ return
+.end method
+
+.method public static write(I)V
+ .limit locals 5
+ .limit stack 5
+ iload 0
+ getstatic java/lang/System/out Ljava/io/PrintStream;
+ swap
+ invokevirtual java/io/PrintStream/println(I)V
+ return
+.end method
+
+"""
+
+// for generating new labels
+var counter = -1
+
+def Fresh(x: String) = {
+ counter += 1
+ x ++ "_" ++ counter.toString()
+}
+
+
+type Env = Map[String, Int]
+type Instrs = List[String]
+
+// compile expressions
+def compile_exp(a: Exp, env : Env) : Instrs = a match {
+ case Num(i) => List("ldc " + i.toString + "\n")
+ case Var(s) => List("iload " + env(s).toString + "\n")
+ case Aop("+", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("iadd\n")
+ case Aop("-", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("isub\n")
+ case Aop("*", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("imul\n")
+ case Aop("/", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("idiv\n")
+ case Aop("%", a1, a2) => compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("irem\n")
+ case If(b, a1, a2) => {
+ val if_else = Fresh("If_else")
+ val if_end = Fresh("If_end")
+ compile_bexp(b, env, if_else) ++
+ compile_exp(a1, env) ++
+ List("goto " + if_end + "\n") ++
+ List("\n" + if_else + ":\n\n") ++
+ compile_exp(a2, env) ++
+ List("\n" + if_end + ":\n\n")
+ }
+ case Call(n, args) => {
+ val is = "I" * args.length
+ args.flatMap(a => compile_exp(a, env)) ++
+ List ("invokestatic XXX/XXX/" + n + "(" + is + ")I\n")
+ }
+ case Sequ(a1, a2) => {
+ compile_exp(a1, env) ++ List("pop\n") ++ compile_exp(a2, env)
+ }
+ case Write(a1) => {
+ compile_exp(a1, env) ++
+ List("dup\n",
+ "invokestatic XXX/XXX/write(I)V\n")
+ }
+}
+
+// compile boolean expressions
+def compile_bexp(b: BExp, env : Env, jmp: String) : Instrs = b match {
+ case Bop("==", a1, a2) =>
+ compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpne " + jmp + "\n")
+ case Bop("!=", a1, a2) =>
+ compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpeq " + jmp + "\n")
+ case Bop("<", a1, a2) =>
+ compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpge " + jmp + "\n")
+ case Bop("<=", a1, a2) =>
+ compile_exp(a1, env) ++ compile_exp(a2, env) ++ List("if_icmpgt " + jmp + "\n")
+}
+
+// compile function for declarations and main
+def compile_decl(d: Decl) : Instrs = d match {
+ case Def(name, args, a) => {
+ val env = args.zipWithIndex.toMap
+ val is = "I" * args.length
+ List(".method public static " + name + "(" + is + ")I \n",
+ ".limit locals " + args.length.toString + "\n",
+ ".limit stack " + (1 + max_stack_exp(a)).toString + "\n",
+ name + "_Start:\n") ++
+ compile_exp(a, env) ++
+ List("ireturn\n",
+ ".end method \n\n")
+ }
+ case Main(a) => {
+ List(".method public static main([Ljava/lang/String;)V\n",
+ ".limit locals 200\n",
+ ".limit stack 200\n") ++
+ compile_exp(a, Map()) ++
+ List("invokestatic XXX/XXX/write(I)V\n",
+ "return\n",
+ ".end method\n")
+ }
+}
+
+// main compilation function
+def compile(prog: List[Decl], class_name: String) : String = {
+ val instructions = prog.flatMap(compile_decl).mkString
+ (library + instructions).replaceAllLiterally("XXX", class_name)
+}
+
+
+
+
+// example program (factorials)
+
+val test_prog =
+ List(Def("fact", List("n"),
+ If(Bop("==",Var("n"),Num(0)),
+ Num(1),
+ Aop("*",Var("n"),
+ Call("fact",List(Aop("-",Var("n"),Num(1))))))),
+
+ Def("facT",List("n", "acc"),
+ If(Bop("==",Var("n"),Num(0)),
+ Var("acc"),
+ Call("facT",List(Aop("-",Var("n"),Num(1)),
+ Aop("*",Var("n"),Var("acc")))))),
+
+ Main(Sequ(Write(Call("fact",List(Num(10)))),
+ Write(Call("facT",List(Num(10), Num(1)))))))
+
+// prints out the JVM instructions
+println(compile(test, "fact"))