progs/compile_arrays.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 28 Jul 2019 21:10:39 +0100
changeset 627 f5214da1976e
parent 625 progs/compile.scala@6709fa87410b
child 628 8067d0a8ba04
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
     1
// A Small Compiler for the WHILE Language with Arrays
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
     2
// (it does not use a parser and lexer)
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     3
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
     4
// the new parts are
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
     5
//    - declaring an array
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
     6
//    - references an array cell
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
     7
//    - assigning an array cell
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     8
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     9
// the abstract syntax trees
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    10
abstract class Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    11
abstract class AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    12
abstract class BExp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    13
type Block = List[Stmt]
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    14
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    15
// statements
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    16
case object Skip extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    17
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
    18
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    19
case class Assign(s: String, a: AExp) extends Stmt
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    20
case class AssignA(s: String, a1: AExp, a2: AExp) extends Stmt
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    21
case class Write(s: String) extends Stmt
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    22
case class Array(s: String, n: Int) extends Stmt 
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
    23
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    24
// arithmetic expressions
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    25
case class Var(s: String) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    26
case class Num(i: Int) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    27
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    28
case class Ref(s: String, a1: AExp) extends AExp
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
    29
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    30
// boolean expressions
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    31
case object True extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    32
case object False extends BExp
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    33
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
    34
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    36
// compiler headers needed for the JVM
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    37
// (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
    38
val beginning = """
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
    39
.class public XXX.XXX
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    40
.super java/lang/Object
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    41
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    42
.method public <init>()V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    43
   aload_0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    44
   invokenonvirtual java/lang/Object/<init>()V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    45
   return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    46
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    47
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    48
.method public static write(I)V 
373
b018234c9126 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
    49
    .limit locals 1 
b018234c9126 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
    50
    .limit stack 2 
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    51
    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
    52
    iload 0
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    53
    invokevirtual java/io/PrintStream/println(I)V 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    54
    return 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    55
.end method
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    56
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    57
.method public static main([Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    58
   .limit locals 200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    59
   .limit stack 200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    60
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    61
; COMPILED CODE STARTS
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    62
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    63
"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    64
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    65
val ending = """
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    66
; COMPILED CODE ENDS
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    67
   return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    68
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    69
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    70
"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    71
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    72
// Compiler functions
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
    73
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
    74
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    75
// for generating new labels
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    76
var counter = -1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    77
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    78
def Fresh(x: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    79
  counter += 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    80
  x ++ "_" ++ counter.toString()
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
}
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    82
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    83
// convenient string interpolations 
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    84
// for instructions and labels
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    85
import scala.language.implicitConversions
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    86
import scala.language.reflectiveCalls
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    87
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    88
implicit def sring_inters(sc: StringContext) = new {
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    89
    def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    90
    def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    91
}
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    92
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    93
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    94
// environments 
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    95
type Env = Map[String, String]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    96
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    97
// arithmetic expression compilation
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    98
def compile_aexp(a: AExp, env : Env) : String = a match {
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    99
  case Num(i) => i"ldc $i"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   100
  case Var(s) => i"iload ${env(s)}"
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 323
diff changeset
   101
  case Aop("+", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   102
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"iadd"
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 323
diff changeset
   103
  case Aop("-", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   104
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"isub"
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 323
diff changeset
   105
  case Aop("*", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   106
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"imul"
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   107
  case Ref(s, a1) => 
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   108
    i"aload ${env(s)}" ++ compile_aexp(a1, env) ++ i"iaload"  
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   109
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   110
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   111
// boolean expression compilation
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   112
def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   113
  case True => ""
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   114
  case False => i"goto $jmp"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   115
  case Bop("=", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   116
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpne $jmp"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   117
  case Bop("!=", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   118
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpeq $jmp"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   119
  case Bop("<", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   120
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"if_icmpge $jmp"
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   121
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   122
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   123
// statement compilation
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   124
def compile_stmt(s: Stmt, env: Env) : (String, Env) = s match {
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   125
  case Skip => ("", env)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   126
  case Assign(x, a) => {
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 323
diff changeset
   127
    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
   128
                    env.keys.size.toString
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   129
    (compile_aexp(a, env) ++ i"istore $index", env + (x -> index))
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   130
  } 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   131
  case If(b, bl1, bl2) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   132
    val if_else = Fresh("If_else")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   133
    val if_end = Fresh("If_end")
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   134
    val (instrs1, env1) = compile_block(bl1, env)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   135
    val (instrs2, env2) = compile_block(bl2, env1)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   136
    (compile_bexp(b, env, if_else) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   137
     instrs1 ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   138
     i"goto $if_end" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   139
     l"$if_else" ++
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   140
     instrs2 ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   141
     l"$if_end", env2)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   142
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   143
  case While(b, bl) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   144
    val loop_begin = Fresh("Loop_begin")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   145
    val loop_end = Fresh("Loop_end")
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   146
    val (instrs1, env1) = compile_block(bl, env)
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   147
    (l"$loop_begin" ++
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   148
     compile_bexp(b, env, loop_end) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   149
     instrs1 ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   150
     i"goto $loop_begin" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   151
     l"$loop_end", env1)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   152
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   153
  case Write(x) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   154
    (i"iload ${env(x)}" ++ 
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   155
     i"invokestatic XXX/XXX/write(I)V", env)
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   156
  case Array(s, n) => {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   157
    val index = if (env.isDefinedAt(s)) throw new Exception("Array already defined") else 
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 323
diff changeset
   158
                    env.keys.size.toString
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   159
    (i"ldc $n" ++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   160
     i"newarray int" ++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   161
     i"astore $index", env + (s -> index))
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   162
  } 
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   163
  case AssignA(s, a1, a2) => {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   164
    val index = if (env.isDefinedAt(s)) env(s) else 
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   165
                    throw new Exception("Array not yet defined")
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   166
    (i"aload $index" ++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   167
     compile_aexp(a1, env) ++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   168
     compile_aexp(a2, env) ++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   169
     i"iastore", env)
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   170
  } 
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   171
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   172
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   173
// compilation of a block (i.e. list of instructions)
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   174
def compile_block(bl: Block, env: Env) : (String, Env) = bl match {
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   175
  case Nil => ("", env)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   176
  case s::bl => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   177
    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
   178
    val (instrs2, env2) = compile_block(bl, env1)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   179
    (instrs1 ++ instrs2, env2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   180
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   181
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   182
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   183
// main compilation function for blocks
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   184
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
   185
  val instructions = compile_block(bl, Map.empty)._1
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   186
  (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
76
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
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   189
// compiling and running files
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   190
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   191
// JVM files can be assembled with 
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   192
//
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   193
//    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
   194
//
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   195
// and started with
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   196
//
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   197
//    java fib/fib
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   198
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   199
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   200
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   201
import scala.util._
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   202
import scala.sys.process._
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   203
import scala.io
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   204
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   205
def compile_tofile(bl: Block, class_name: String) = {
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   206
  val output = compile(bl, class_name)
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   207
  val fw = new java.io.FileWriter(class_name + ".j") 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   208
  fw.write(output) 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   209
  fw.close()
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   210
}
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   211
535
095be64c03c1 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   212
def compile_all(bl: Block, class_name: String) : Unit = {
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   213
  compile_tofile(bl, class_name)
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   214
  println("compiled ")
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   215
  val test = ("java -jar jvm/jasmin-2.4/jasmin.jar " + class_name + ".j").!!
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   216
  println("assembled ")
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   217
}
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   218
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   219
def time_needed[T](i: Int, code: => T) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   220
  val start = System.nanoTime()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   221
  for (j <- 1 to i) code
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   222
  val end = System.nanoTime()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   223
  (end - start)/(i * 1.0e9)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   224
}
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   225
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   226
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   227
def compile_run(bl: Block, class_name: String) : Unit = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   228
  println("Start compilation")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   229
  compile_all(bl, class_name)
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   230
  println("running")
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   231
  println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!))
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   232
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   233
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   234
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   235
// BF Part
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   236
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   237
// simple instructions
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   238
def instr(c: Char) : String = c match {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   239
  case '>' => "ptr := ptr + 1;"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   240
  case '<' => "ptr := ptr - 1;"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   241
  case '+' => "field[ptr] := field[ptr] + 1;"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   242
  case '-' => "field[ptr] := field[ptr] - 1;"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   243
  case '.' => "x := field[ptr]; write x;"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   244
  case '['  => "while (field[ptr] != 0) do {"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   245
  case ']'  => "skip};"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   246
  case _ => ""
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   247
}
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   248
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   249
def instrs(prog: String) : String =
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   250
  prog.toList.map(instr).mkString
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   251
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   252
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   253
// compound instructions
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   254
def splice(cs: List[Char], acc: List[(Char, Int)]) : List[(Char, Int)] = (cs, acc) match {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   255
  case (Nil, acc) => acc
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   256
  case (c :: cs, Nil) => splice(cs, List((c, 1)))
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   257
  case (c :: cs, (d, n) :: acc) => 
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   258
    if (c == d) splice(cs, (c, n + 1) :: acc)
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   259
    else splice(cs, (c, 1) :: (d, n) :: acc)
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   260
}
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   261
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   262
def spl(s: String) = splice(s.toList, Nil).reverse
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   263
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   264
def instr2(c: Char, n: Int) : String = c match {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   265
  case '>' => s"ptr := ptr + $n;"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   266
  case '<' => s"ptr := ptr - $n;"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   267
  case '+' => s"field[ptr] := field[ptr] + $n;"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   268
  case '-' => s"field[ptr] := field[ptr] - $n;"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   269
  case '.' => s"x := field[ptr]; write x;" 
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   270
  case '['  => s"while (field[ptr] != 0) do {" * n 
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   271
  case ']'  => s"skip};" * n
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   272
  case _ => ""
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   273
}
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   274
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   275
def instrs2(prog: String) : String =
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   276
  spl(prog).map{ case (c, n) => instr2(c, n) }.mkString
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   277
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   278
627
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   279
def bf_str(prog: String) : String = {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   280
  "\n" ++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   281
  //"new field[30000];\n" ++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   282
  "ptr := 15000;" ++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   283
  instrs2(prog) ++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   284
  "skip"
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   285
}
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   286
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   287
def bf_run(bfprog: String, name: String) = {
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   288
  println("BF processing start")
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   289
  val bf_string = bf_str(bfprog).replaceAll("\\s", "")
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   290
  println(s"BF parsing start (string length ${bf_string.length})")
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   291
  val bf_prog = Stmts.parse_all(bf_string).toList.head
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   292
  println("BF Compile start")
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   293
  compile_run(Array("field", 30000) :: bf_prog, name)
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   294
}
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   295
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   296
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   297
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   298
val bf1 = """++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   299
      ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   300
      ]>.>+[>>]>+]"""
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   301
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   302
bf_run(bf1, "sier")
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   303
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   304
bf_run("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   305
       ..+++.>>.<-.<.+++.------.--------.>>+.>++.""", "hello")
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   306
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   307
bf_run("""+++++++++++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   308
      >+>>>>++++++++++++++++++++++++++++++++++++++++++++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   309
      >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   310
      +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   311
      <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   312
      -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   313
      >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   314
      +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   315
      ++++++++++++++++++++++++++++++++++++++++++++.[-]<<
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   316
      <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
f5214da1976e updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   317
      [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]""", "fibs")