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