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