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