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