progs/while-arrays/compile_arrays2.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:59:32 +0000
changeset 976 e9eac62928f5
parent 949 61797848eede
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// A Small Compiler for the WHILE Language
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
     3
// - there are some small peep-hole optimisations
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
     4
//   implemented
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
     5
//
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// - this compiler contains support for "static" integer 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
//   arrays (they are mutable but cannot be re-sized)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
// Call with 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
// amm compile_arrays.sc
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
// the abstract syntax trees for WHILE
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    16
//> using toolkit latest
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
    17
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
abstract class Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
abstract class AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
abstract class BExp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
type Block = List[Stmt]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
// statements
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
case object Skip extends Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case class ArrayDef(s: String, n: Int) extends Stmt            // array definition
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case class While(b: BExp, bl: Block) extends Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
case class Assign(s: String, a: AExp) extends Stmt             // var := exp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
case class AssignA(s: String, a1: AExp, a2: AExp) extends Stmt // arr[exp1] := exp2
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
case class Write(s: String) extends Stmt
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
// arithmetic expressions
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
case class Var(s: String) extends AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
case class Num(i: Int) extends AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
case class Ref(s: String, a: AExp) extends AExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
// boolean expressions
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
case object True extends BExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
case object False extends BExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
// compiler headers needed for the JVM
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
// - contains a main method and a method for writing out an integer
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
// - the stack and locals are hard-coded
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
val beginning = """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
.class public XXX.XXX
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
.super java/lang/Object
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
.method public static write(I)V 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    .limit locals 1 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
    .limit stack 2 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
    getstatic java/lang/System/out Ljava/io/PrintStream; 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
    iload 0
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
    invokevirtual java/io/PrintStream/print(I)V
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
    return 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
.end method
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
.method public static main([Ljava/lang/String;)V
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
   .limit locals 200
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
   .limit stack 200
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
; COMPILED CODE STARTS   
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
val ending = """
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
; COMPILED CODE ENDS
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
   return
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
.end method
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
"""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
// for generating new labels
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
var counter = -1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
def Fresh(x: String) = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
  counter += 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
  x ++ "_" ++ counter.toString()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
// environments for variables and indices
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
type Env = Map[String, Int]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
// convenient string interpolations 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
// for generating instructions and labels
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
    96
extension (sc: StringContext) {
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
943
5365ef60707e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 870
diff changeset
   101
817
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   102
def compile_num(i: Int) = 
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   103
  if (0 <= i && i <= 5) i"iconst_$i" else 
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   104
  if (-128 <= i && i <= 127) i"bipush $i" else i"ldc $i"
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   105
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   106
def compile_aload(i: Int) = 
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   107
  if (0 <= i && i <= 3) i"aload_$i" else i"aload $i"
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   108
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   109
def compile_astore(i: Int) = 
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   110
  if (0 <= i && i <= 3) i"astore_$i" else i"astore $i"
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   111
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   112
def compile_iload(i: Int) = 
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   113
  if (0 <= i && i <= 3) i"iload_$i" else i"iload $i"
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   114
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   115
def compile_istore(i: Int) = 
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   116
  if (0 <= i && i <= 3) i"istore_$i" else i"istore $i"
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   117
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   118
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
def compile_op(op: String) = op match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  case "+" => i"iadd"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
  case "-" => i"isub"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  case "*" => i"imul"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
// arithmetic expression compilation
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
def compile_aexp(a: AExp, env : Env) : String = a match {
817
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   127
  case Num(i) => compile_num(i)
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   128
  case Var(s) => compile_iload(env(s))
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
  case Aop(op, a1, a2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  case Ref(s, a) =>
817
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   132
    compile_aload(env(s)) ++ compile_aexp(a, env) ++  i"iaload"
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
// boolean expression compilation
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
  case True => ""
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
  case False => i"goto $jmp"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
  case Bop("==", a1, a2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpne $jmp"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
  case Bop("!=", a1, a2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpeq $jmp"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
  case Bop("<", a1, a2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpge $jmp"
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
// statement compilation
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
  case Skip => ("", env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
  case Assign(x, a) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
     val index = env.getOrElse(x, env.keys.size)
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   152
     (compile_aexp(a, env) ++ compile_istore(index), env + (x -> index)) 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
  } 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
  case If(b, bl1, bl2) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
    val if_else = Fresh("If_else")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
    val if_end = Fresh("If_end")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
    val (instrs1, env1) = compile_block(bl1, env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
    val (instrs2, env2) = compile_block(bl2, env1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
    (compile_bexp(b, env, if_else) ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
     instrs1 ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
     i"goto $if_end" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
     l"$if_else" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
     instrs2 ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
     l"$if_end", env2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
  case While(b, bl) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
    val loop_begin = Fresh("Loop_begin")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
    val loop_end = Fresh("Loop_end")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
    val (instrs1, env1) = compile_block(bl, env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
    (l"$loop_begin" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
     compile_bexp(b, env, loop_end) ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
     instrs1 ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
     i"goto $loop_begin" ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
     l"$loop_end", env1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
  case Write(x) => 
817
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   177
    (compile_iload(env(x)) ++ 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
     i"invokestatic XXX/XXX/write(I)V", env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
  case ArrayDef(s: String, n: Int) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
    val index = if (env.isDefinedAt(s)) throw new Exception("array def error") else 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
                    env.keys.size
817
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   182
    (compile_num(n) ++
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
     i"newarray int" ++
817
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   184
     compile_astore(index), env + (s -> index))
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
  case AssignA(s, a1, a2) => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
    val index = if (env.isDefinedAt(s)) env(s) else 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
                    throw new Exception("array not defined")
817
89f9c68fc417 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 791
diff changeset
   189
    (compile_aload(env(s)) ++
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
     compile_aexp(a1, env) ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
     compile_aexp(a2, env) ++
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
     i"iastore", env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
  } 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
// compile a block (i.e. list of statements)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
  case Nil => ("", env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
  case s::bl => {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
    val (instrs1, env1) = compile_stmt(s, env)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
    val (instrs2, env2) = compile_block(bl, env1)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
    (instrs1 ++ instrs2, env2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
// main compile function for blocks (adds headers and proper JVM names)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
def compile(bl: Block, class_name: String) : String = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
  val instructions = compile_block(bl, Map())._1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
  (beginning ++ instructions ++ ending).replace("XXX", class_name)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
// contrived example involving arrays
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
val array_test = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
  List(ArrayDef("a", 10),               // array a[10]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
       ArrayDef("b", 2),                // array b[2]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
       AssignA("a", Num(0), Num(10)),   // a[0] := 10
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
       Assign("x", Ref("a", Num(0))),   // x := a[0]
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
       Write("x"),            
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
       AssignA("b", Num(1), Num(5)),    // b[1] := 5
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
       Assign("x", Ref("b", Num(1))),   // x := b[1] 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
       Write("x"))                     
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
// prints out the JVM-assembly instructions for fib above
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
//    println(compile(array_test, "arr"))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
// can be assembled by hand with 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
//    java -jar jasmin.jar arr.j
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
// and run with
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
//    java arr/arr
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
// automating the above
869
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
   240
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
   241
// pre-2.5.0 ammonite 
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
   242
// import ammonite.ops._
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
   243
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
   244
// post 2.5.0 ammonite
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
   245
import os._
81ee93b87258 changed os-lib as a replacement for ammonite-ops
Christian Urban <christian.urban@kcl.ac.uk>
parents: 829
diff changeset
   246
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
def compile_to_file(bl: Block, class_name: String) : Unit = 
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   249
  os.write.over(os.pwd / s"$class_name.j", compile(bl, class_name))  
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
def compile_and_run(bl: Block, class_name: String) : Unit = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
  println(s"Start of compilation")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
  compile_to_file(bl, class_name)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
  println(s"generated $class_name.j file")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
  os.proc("java", "-jar", "jasmin.jar", s"$class_name.j").call()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
  println(s"generated $class_name.class file ")
747
077fc2e5412c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
   257
  //println(os.proc("java", s"${class_name}/${class_name}").call().out.text())
077fc2e5412c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
   258
  os.proc("java", s"${class_name}/${class_name}").call(stdout = os.Inherit)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
  println(s"done.")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
   
949
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   264
//@main def main() = {
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   265
//  compile_and_run(array_test, "arr")
61797848eede updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 943
diff changeset
   266
//}
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
829
dc3c35673e94 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   269
dc3c35673e94 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 817
diff changeset
   270