progs/compile_arr.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 27 Nov 2018 07:53:57 +0000
changeset 611 060f33b5661d
parent 609 e33545bb2eba
child 612 7a12053567d4
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// A Small Compiler for the WHILE Language
611
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     2
// (stub for including arrays)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// the abstract syntax trees
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
abstract class Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
abstract class AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
abstract class BExp 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
type Block = List[Stmt]
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// statements
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
case object Skip extends Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
case class While(b: BExp, bl: Block) extends Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
case class Assign(s: String, a: AExp) extends Stmt
611
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    15
case class Write(s: String) extends Stmt  // writes out a variable
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
// arithmetic expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
case class Var(s: String) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
case class Num(i: Int) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
// boolean expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case object True extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case object False extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// compiler headers needed for the JVM
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
// (contains an init method, as well as methods for read and write)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
val beginning = """
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
.class public XXX.XXX
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
.super java/lang/Object
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
.method public <init>()V
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
   aload_0
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
   invokenonvirtual java/lang/Object/<init>()V
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
   return
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
.method public static write(I)V 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
    .limit locals 1 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
    .limit stack 2 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
    getstatic java/lang/System/out Ljava/io/PrintStream; 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    iload 0
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    invokevirtual java/io/PrintStream/println(I)V 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
    return 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
.method public static main([Ljava/lang/String;)V
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
   .limit locals 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
   .limit stack 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
val ending = """
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
   return
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
println("Start compilation")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
// for generating new labels
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
var counter = -1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
def Fresh(x: String) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  counter += 1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  x ++ "_" ++ counter.toString()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// environments and instructions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
type Env = Map[String, String]
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
type Instrs = List[String]
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
// arithmetic expression compilation
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
def compile_aexp(a: AExp, env : Env) : Instrs = a match {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  case Num(i) => List("ldc " + i.toString + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  case Var(s) => List("iload " + env(s) + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  case Aop("+", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
    compile_aexp(a1, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
    compile_aexp(a2, env) ++ List("iadd\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  case Aop("-", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("isub\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  case Aop("*", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ List("imul\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// boolean expression compilation
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
def compile_bexp(b: BExp, env : Env, jmp: String) : Instrs = b match {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  case True => Nil
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  case False => List("goto " + jmp + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  case Bop("=", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
    List("if_icmpne " + jmp + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  case Bop("!=", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
    List("if_icmpeq " + jmp + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  case Bop("<", a1, a2) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
    List("if_icmpge " + jmp + "\n")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
// statement compilation
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
def compile_stmt(s: Stmt, env: Env) : (Instrs, Env) = s match {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  case Skip => (Nil, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  case Assign(x, a) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
    val index = if (env.isDefinedAt(x)) env(x) else 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
                    env.keys.size.toString
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
    (compile_aexp(a, env) ++ 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
     List("istore " + index + "\n"), env + (x -> index))
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  } 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  case If(b, bl1, bl2) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
    val if_else = Fresh("If_else")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
    val if_end = Fresh("If_end")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
    val (instrs1, env1) = compile_block(bl1, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
    val (instrs2, env2) = compile_block(bl2, env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
    (compile_bexp(b, env, if_else) ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
     instrs1 ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
     List("goto " + if_end + "\n") ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
     List("\n" + if_else + ":\n\n") ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
     instrs2 ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
     List("\n" + if_end + ":\n\n"), env2)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  case While(b, bl) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
    val loop_begin = Fresh("Loop_begin")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
    val loop_end = Fresh("Loop_end")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
    val (instrs1, env1) = compile_block(bl, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
    (List("\n" + loop_begin + ":\n\n") ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
     compile_bexp(b, env, loop_end) ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
     instrs1 ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
     List("goto " + loop_begin + "\n") ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
     List("\n" + loop_end + ":\n\n"), env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  case Write(x) => 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
    (List("iload " + env(x) + "\n" + 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
           "invokestatic XXX/XXX/write(I)V\n"), env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
// compilation of a block (i.e. list of instructions)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
def compile_block(bl: Block, env: Env) : (Instrs, Env) = bl match {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  case Nil => (Nil, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
  case s::bl => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
    val (instrs1, env1) = compile_stmt(s, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
    val (instrs2, env2) = compile_block(bl, env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
    (instrs1 ++ instrs2, env2)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
// main compilation function for blocks
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
def compile(bl: Block, class_name: String) : String = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
  val instructions = compile_block(bl, Map.empty)._1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
  (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
// compiling and running files
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
// JVM files can be assembled with 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
//    java -jar jvm/jasmin-2.4/jasmin.jar fib.j
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
// and started with
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
//    java fib/fib
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
import scala.util._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
import scala.sys.process._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
import scala.io
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
def compile_tofile(bl: Block, class_name: String) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
  val output = compile(bl, class_name)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  val fw = new java.io.FileWriter(class_name + ".j") 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
  fw.write(output) 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
  fw.close()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
def compile_all(bl: Block, class_name: String) : Unit = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
  compile_tofile(bl, class_name)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
  println("compiled ")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!!
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
  println("assembled ")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
def time_needed[T](i: Int, code: => T) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
  val start = System.nanoTime()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
  for (j <- 1 to i) code
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
  val end = System.nanoTime()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
  (end - start)/(i * 1.0e9)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
def compile_run(bl: Block, class_name: String) : Unit = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
  println("Start compilation")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
  compile_all(bl, class_name)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
  println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!))
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
// Fibonacci numbers as a test-case
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
val fib_test = 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
  List(Assign("n", Num(10)),            //  n := 10;                     
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
       Assign("minus1",Num(0)),         //  minus1 := 0;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
       Assign("minus2",Num(1)),         //  minus2 := 1;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
       Assign("temp",Num(0)),           //  temp := 0;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
       While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
          List(Assign("temp",Var("minus2")),    //  temp := minus2;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
               Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
                                        //  minus2 := minus1 + minus2;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
               Assign("minus1",Var("temp")), //  minus1 := temp;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
               Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
       Write("minus1"))                 //  write minus1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
compile_run(fib_test, "fib")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
611
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   221
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   222
val arr_test = 
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   223
  List(Array("a", 10),
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   224
       Array("b", 2),
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   225
       AssignA("a", Num(0), Num(10)),
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   226
       Assign("x", Ref("a", Num(0))),
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   227
       Write("x"),
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   228
       AssignA("b", Num(1), Num(5)),
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   229
       Assign("x", Ref("b", Num(1))),
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   230
       Write("x"))       
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   231
060f33b5661d updated
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   232
//compile_run(arr_test, "a")