progs/compile.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 28 Jul 2019 16:15:03 +0100
changeset 625 6709fa87410b
parent 624 8d0af38389bc
child 668 9ce78065f68d
permissions -rw-r--r--
updated to 2.13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
     1
// A Small Compiler for the WHILE Language
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
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
     4
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     5
// the abstract syntax trees
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     6
abstract class Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     7
abstract class AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     8
abstract class BExp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
     9
type Block = List[Stmt]
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    10
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    11
// statements
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    12
case object Skip extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    13
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
    14
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    15
case class Assign(s: String, a: AExp) extends Stmt
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    16
case class Write(s: String) extends Stmt
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    17
case class Read(s: String) extends Stmt
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
    18
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    19
// arithmetic expressions
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    20
case class Var(s: String) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    21
case class Num(i: Int) extends AExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    22
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 69
diff changeset
    23
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    24
// boolean expressions
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    25
case object True extends BExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    26
case object False extends BExp
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    27
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
    28
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    30
// compiler headers needed for the JVM
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    31
// (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
    32
val beginning = """
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
    33
.class public XXX.XXX
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    34
.super java/lang/Object
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    35
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    36
.method public <init>()V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    37
   aload_0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    38
   invokenonvirtual java/lang/Object/<init>()V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    39
   return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    40
.end method
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 static write(I)V 
373
b018234c9126 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
    43
    .limit locals 1 
b018234c9126 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 369
diff changeset
    44
    .limit stack 2 
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    45
    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
    46
    iload 0
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    47
    invokevirtual java/io/PrintStream/println(I)V 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    48
    return 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    49
.end method
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
    50
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    51
.method public static read()I 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    52
    .limit locals 10 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    53
    .limit stack 10
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    54
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    55
    ldc 0 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    56
    istore 1  ; this will hold our final integer 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    57
Label1: 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    58
    getstatic java/lang/System/in Ljava/io/InputStream; 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    59
    invokevirtual java/io/InputStream/read()I 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    60
    istore 2 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    61
    iload 2 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    62
    ldc 10   ; the newline delimiter 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    63
    isub 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    64
    ifeq Label2 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    65
    iload 2 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    66
    ldc 32   ; the space delimiter 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    67
    isub 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    68
    ifeq Label2
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    69
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    70
    iload 2 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    71
    ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    72
    isub 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    73
    ldc 10 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    74
    iload 1 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    75
    imul 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    76
    iadd 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    77
    istore 1 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    78
    goto Label1 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    79
Label2: 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    80
    ;when we come here we have our integer computed in local variable 1 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    81
    iload 1 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    82
    ireturn 
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    83
.end method
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    84
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    85
.method public static main([Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    86
   .limit locals 200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    87
   .limit stack 200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    88
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    89
; COMPILED CODE STARTS
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    90
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    91
"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    92
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    93
val ending = """
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
    94
; COMPILED CODE ENDS
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    95
   return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    96
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    97
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    98
"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    99
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   100
// Compiler functions
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   101
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   102
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   103
// for generating new labels
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   104
var counter = -1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   106
def Fresh(x: String) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   107
  counter += 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   108
  x ++ "_" ++ counter.toString()
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
}
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   110
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   111
// convenient string interpolations 
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   112
// for instructions and labels
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   113
import scala.language.implicitConversions
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   114
import scala.language.reflectiveCalls
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   115
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   116
implicit def sring_inters(sc: StringContext) = new {
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   117
    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
   118
    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
   119
}
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   120
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   121
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   122
// environments 
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   123
type Env = Map[String, String]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   124
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   125
// arithmetic expression compilation
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   126
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
   127
  case Num(i) => i"ldc $i"
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   128
  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
   129
  case Aop("+", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   130
    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
   131
  case Aop("-", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   132
    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
   133
  case Aop("*", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   134
    compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ i"imul"
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   135
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   136
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   137
// boolean expression compilation
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   138
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
   139
  case True => ""
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   140
  case False => i"goto $jmp"
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   141
  case Bop("=", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   142
    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
   143
  case Bop("!=", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   144
    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
   145
  case Bop("<", a1, a2) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   146
    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
   147
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   148
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   149
// statement compilation
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   150
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
   151
  case Skip => ("", env)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   152
  case Assign(x, a) => {
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 323
diff changeset
   153
    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
   154
                    env.keys.size.toString
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   155
    (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
   156
  } 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   157
  case If(b, bl1, bl2) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   158
    val if_else = Fresh("If_else")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   159
    val if_end = Fresh("If_end")
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   160
    val (instrs1, env1) = compile_block(bl1, env)
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   161
    val (instrs2, env2) = compile_block(bl2, env1)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   162
    (compile_bexp(b, env, if_else) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   163
     instrs1 ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   164
     i"goto $if_end" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   165
     l"$if_else" ++
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   166
     instrs2 ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   167
     l"$if_end", env2)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   168
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   169
  case While(b, bl) => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   170
    val loop_begin = Fresh("Loop_begin")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   171
    val loop_end = Fresh("Loop_end")
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   172
    val (instrs1, env1) = compile_block(bl, env)
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   173
    (l"$loop_begin" ++
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   174
     compile_bexp(b, env, loop_end) ++
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   175
     instrs1 ++
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   176
     i"goto $loop_begin" ++
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   177
     l"$loop_end", env1)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   178
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   179
  case Write(x) => 
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   180
    (i"iload ${env(x)}" ++ 
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   181
     i"invokestatic XXX/XXX/write(I)V", env)
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   182
  case Read(x) => {
369
43c0ed473720 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 323
diff changeset
   183
    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
   184
                    env.keys.size.toString
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   185
    (i"invokestatic XXX/XXX/read()I" ++ 
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   186
     i"istore $index", env + (x -> index))
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   187
  }
69
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   188
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 68
diff changeset
   189
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   190
// 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
   191
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
   192
  case Nil => ("", env)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   193
  case s::bl => {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   194
    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
   195
    val (instrs2, env2) = compile_block(bl, env1)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   196
    (instrs1 ++ instrs2, env2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   197
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   198
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   199
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   200
// main compilation function for blocks
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   201
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
   202
  val instructions = compile_block(bl, Map.empty)._1
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
   203
  (beginning ++ instructions.mkString ++ ending).replaceAllLiterally("XXX", class_name)
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   204
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   205
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   206
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   207
// compiling and running files
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   208
//
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   209
// JVM files can be assembled with 
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   210
//
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   211
//    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
   212
//
323
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   213
// and started with
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   214
//
4ce07c4abdb4 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   215
//    java fib/fib
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   216
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   217
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   218
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   219
import scala.util._
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   220
import scala.sys.process._
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   221
import scala.io
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   222
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   223
def compile_tofile(bl: Block, class_name: String) = {
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   224
  val output = compile(bl, class_name)
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   225
  val fw = new java.io.FileWriter(class_name + ".j") 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   226
  fw.write(output) 
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   227
  fw.close()
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   228
}
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   229
535
095be64c03c1 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   230
def compile_all(bl: Block, class_name: String) : Unit = {
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   231
  compile_tofile(bl, class_name)
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   232
  println("compiled ")
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   233
  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
   234
  println("assembled ")
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   235
}
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   236
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   237
def time_needed[T](i: Int, code: => T) = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   238
  val start = System.nanoTime()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   239
  for (j <- 1 to i) code
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   240
  val end = System.nanoTime()
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   241
  (end - start)/(i * 1.0e9)
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   242
}
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   243
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   244
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   245
def compile_run(bl: Block, class_name: String) : Unit = {
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   246
  println("Start compilation")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   247
  compile_all(bl, class_name)
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 609
diff changeset
   248
  println("running")
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   249
  println("Time: " + time_needed(1, ("java " + class_name + "/" + class_name).!))
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   250
}
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   251
76
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
   252
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   253
// Fibonacci numbers as a test-case
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   254
val fib_test = 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   255
  List(Assign("n", Num(10)),            //  n := 10;                     
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   256
       Assign("minus1",Num(0)),         //  minus1 := 0;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   257
       Assign("minus2",Num(1)),         //  minus2 := 1;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   258
       Assign("temp",Num(0)),           //  temp := 0;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   259
       While(Bop("<",Num(0),Var("n")),  //  while n > 0 do  {
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   260
          List(Assign("temp",Var("minus2")), //  temp := minus2;
609
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   261
               Assign("minus2",Aop("+",Var("minus1"),Var("minus2"))), 
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   262
                                        //  minus2 := minus1 + minus2;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   263
               Assign("minus1",Var("temp")), //  minus1 := temp;
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   264
               Assign("n",Aop("-",Var("n"),Num(1))))), //  n := n - 1 };
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   265
       Write("minus1"))                 //  write minus1
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   266
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   267
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   268
compile_run(fib_test, "fib")
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   269
e33545bb2eba updated
Christian Urban <urbanc@in.tum.de>
parents: 600
diff changeset
   270