progs/compile_arr2.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 27 Jan 2020 10:11:44 +0000
changeset 708 4980f421b3b0
parent 707 progs/compile_arr.scala@2fcd7c2da729
child 710 183663740fb7
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
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     2
//
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
     3
//  - includes arrays and a small parser for
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     4
//    WHILE programs
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     5
//
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     6
//  - transpiles BF programs into WHILE programs
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     7
//    and compiles and runs them
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     8
//
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
     9
// Call with
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    10
//
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    11
// scala compiler_arr.scala
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    12
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    13
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
// the abstract syntax trees
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
abstract class Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
abstract class AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
abstract class BExp 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
type Block = List[Stmt]
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
// statements
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case object Skip extends Stmt
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    23
case class ArrayDef(s: String, n: Int) extends Stmt
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case class While(b: BExp, bl: Block) extends Stmt
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    26
case class Assign(s: String, a: AExp) extends Stmt             // var := exp
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
    27
case class AssignA(s: String, a1: AExp, a2: AExp) extends Stmt // arr[exp1] := exp2
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    28
case class Write(s: String) extends Stmt
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    29
case class Read(s: String) extends Stmt
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
// arithmetic expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
case class Var(s: String) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
case class Num(i: Int) extends AExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    35
case class Ref(s: String, a: AExp) extends AExp
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
// boolean expressions
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
case object True extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
case object False extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
// compiler headers needed for the JVM
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// (contains an init method, as well as methods for read and write)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
val beginning = """
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
.class public XXX.XXX
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
.super java/lang/Object
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
.method public static write(I)V 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    .limit locals 1 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    .limit stack 2 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
    getstatic java/lang/System/out Ljava/io/PrintStream; 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
    iload 0
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    54
    i2c       ; Int => Char
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    55
    invokevirtual java/io/PrintStream/print(C)V   ; println(I)V => print(C)V    
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
    return 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
.method public static main([Ljava/lang/String;)V
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
   .limit locals 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
   .limit stack 200
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
    63
; COMPILED CODE STARTS   
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
    64
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
val ending = """
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
    68
; COMPILED CODE ENDS
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
   return
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
.end method
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
"""
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    74
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
// for generating new labels
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
var counter = -1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
def Fresh(x: String) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  counter += 1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  x ++ "_" ++ counter.toString()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
// environments and instructions
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    86
type Env = Map[String, Int]
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    87
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    88
// convenient string interpolations 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    89
// for instructions and labels
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    90
import scala.language.implicitConversions
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    91
import scala.language.reflectiveCalls
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    92
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    93
implicit def sring_inters(sc: StringContext) = new {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    94
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    95
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    96
}
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    97
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    98
def compile_op(op: String) = op match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
    99
  case "+" => i"iadd"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   100
  case "-" => i"isub"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   101
  case "*" => i"imul"
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   102
}
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
// arithmetic expression compilation
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   105
def compile_aexp(a: AExp, env : Env) : String = a match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   106
  case Num(i) => i"ldc $i"
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   107
  case Var(s) => i"iload ${env(s)}"
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   108
  case Aop(op, a1, a2) => 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   109
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   110
  case Ref(s, a) =>
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   111
    i"aload ${env(s)}" ++ compile_aexp(a, env) ++  i"iaload"
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   114
def compile_bop(op: String, jmp: String) = op match {
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   115
  case "==" => i"if_icmpne $jmp"
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   116
  case "!=" => i"if_icmpeq $jmp"
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   117
  case "<"  => i"if_icmpge $jmp"
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   118
}
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   119
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
// boolean expression compilation
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   121
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   122
  case True => ""
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   123
  case False => i"goto $jmp"
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   124
  case Bop(op, a1, a2) => 
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   125
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_bop(op, jmp)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
// statement compilation
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   129
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   130
  case Skip => ("", env)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
  case Assign(x, a) => {
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   132
     val index = env.getOrElse(x, env.keys.size)
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   133
    (compile_aexp(a, env) ++ i"istore $index \t\t; $x", env + (x -> index)) 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
  } 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  case If(b, bl1, bl2) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
    val if_else = Fresh("If_else")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
    val if_end = Fresh("If_end")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
    val (instrs1, env1) = compile_block(bl1, env)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
    val (instrs2, env2) = compile_block(bl2, env1)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
    (compile_bexp(b, env, if_else) ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
     instrs1 ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   142
     i"goto $if_end" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   143
     l"$if_else" ++
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
     instrs2 ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   145
     l"$if_end", env2)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
  case While(b, bl) => {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
    val loop_begin = Fresh("Loop_begin")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
    val loop_end = Fresh("Loop_end")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
    val (instrs1, env1) = compile_block(bl, env)
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   151
    (l"$loop_begin" ++
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
     compile_bexp(b, env, loop_end) ++
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
     instrs1 ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   154
     i"goto $loop_begin" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   155
     l"$loop_end", env1)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
  }
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
  case Write(x) => 
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   158
    (i"iload ${env(x)} \t\t; $x" ++ 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   159
     i"invokestatic XXX/XXX/write(I)V", env)
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   160
  case Read(x) => {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   161
    val index = env.getOrElse(x, env.keys.size) 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   162
    (i"invokestatic XXX/XXX/read()I" ++ 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   163
     i"istore $index \t\t; $x", env + (x -> index))
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   164
  }
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   165
  case ArrayDef(s: String, n: Int) => {
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   166
    val index = if (env.isDefinedAt(s)) throw new Exception("array def error") else 
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   167
                    env.keys.size
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   168
    (i"ldc $n" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   169
     i"newarray int" ++
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   170
     i"astore $index", env + (s -> index))
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   171
  }
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   172
  case AssignA(s, a1, a2) => {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   173
    val index = if (env.isDefinedAt(s)) env(s) else 
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   174
                    throw new Exception("array not defined")
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   175
    (i"aload ${env(s)}" ++
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   176
     compile_aexp(a1, env) ++
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   177
     compile_aexp(a2, env) ++
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   178
     i"iastore", env)
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 611
diff changeset
   179
  } 
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   182
// compilation of a block (i.e. list of statements)
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   183
def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   184
  case Nil => ("", env)
609
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
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   192
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
// main compilation function for blocks
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
def compile(bl: Block, class_name: String) : String = {
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   195
  val instructions = compile_block(bl, Map())._1
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   196
  (beginning ++ instructions ++ ending).replaceAllLiterally("XXX", class_name)
609
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
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
import scala.util._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
import scala.sys.process._
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
def time_needed[T](i: Int, code: => T) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
  val start = System.nanoTime()
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   205
  for (j <- 2 to i) code
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   206
  val result = code
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
  val end = System.nanoTime()
707
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   208
  ((end - start) / (i * 1.0e9), result)
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   209
}
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   210
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   211
def compile_to_file(bl: Block, class_name: String) : Unit = 
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   212
  Using(new java.io.FileWriter(class_name + ".j")) {
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   213
    _.write(compile(bl, class_name))
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   214
  }
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   215
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   216
def compile_and_run(bl: Block, class_name: String) : Unit = {
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   217
  println(s"Start compilation of $class_name")
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   218
  compile_to_file(bl, class_name)
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   219
  println("generated .j file")
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   220
  (s"java -jar jvm/jasmin-2.4/jasmin.jar ${class_name}.j").!!
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   221
  println("generated .class file ")
2fcd7c2da729 updated
Christian Urban <urbanc@in.tum.de>
parents: 696
diff changeset
   222
  println("Time: " + time_needed(1, (s"java ${class_name}/${class_name}").!)._1)
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   226
val arr_test = 
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   227
  List(ArrayDef("a", 10),               // new(a[10])
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   228
       ArrayDef("b", 2),                // new(b[2])
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   229
       AssignA("a", Num(0), Num(10)),   // a[0] := 10
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   230
       Assign("x", Ref("a", Num(0))),   // x := a[0]
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   231
       Write("x"),                      // write x
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   232
       AssignA("b", Num(2), Num(5)),    // b[2] := 5
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   233
       Assign("x", Ref("b", Num(1))),   // x := b[1]
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   234
       Write("x"))                      // write x
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   235
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
   236
708
4980f421b3b0 updated
Christian Urban <urbanc@in.tum.de>
parents: 707
diff changeset
   237
compile_and_run(arr_test, "a")
694
b6ed836ce59b updated
Christian Urban <urbanc@in.tum.de>
parents: 616
diff changeset
   238