progs/while/compile.sc
changeset 809 2b9956d29038
parent 790 31a9f89776a3
child 810 ba25eaf6043c
equal deleted inserted replaced
808:ba3edaf7dc49 809:2b9956d29038
    45     iload 0
    45     iload 0
    46     invokevirtual java/io/PrintStream/println(I)V 
    46     invokevirtual java/io/PrintStream/println(I)V 
    47     return 
    47     return 
    48 .end method
    48 .end method
    49 
    49 
    50 .method public static read()I 
       
    51     .limit locals 10 
       
    52     .limit stack 10
       
    53 
       
    54     ldc 0 
       
    55     istore 1  ; this will hold our final integer 
       
    56 Label1: 
       
    57     getstatic java/lang/System/in Ljava/io/InputStream; 
       
    58     invokevirtual java/io/InputStream/read()I 
       
    59     istore 2 
       
    60     iload 2 
       
    61     ldc 10   ; the newline delimiter 
       
    62     isub 
       
    63     ifeq Label2 
       
    64     iload 2 
       
    65     ldc 32   ; the space delimiter 
       
    66     isub 
       
    67     ifeq Label2
       
    68 
       
    69     iload 2 
       
    70     ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
       
    71     isub 
       
    72     ldc 10 
       
    73     iload 1 
       
    74     imul 
       
    75     iadd 
       
    76     istore 1 
       
    77     goto Label1 
       
    78 Label2: 
       
    79     ;when we come here we have our integer computed in local variable 1 
       
    80     iload 1 
       
    81     ireturn 
       
    82 .end method
       
    83 
       
    84 .method public static main([Ljava/lang/String;)V
    50 .method public static main([Ljava/lang/String;)V
    85    .limit locals 200
    51    .limit locals 200
    86    .limit stack 200
    52    .limit stack 200
    87 
    53 
    88 ; COMPILED CODE STARTS
    54 ; COMPILED CODE STARTS
   116     def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
    82     def i(args: Any*): String = "   " ++ sc.s(args:_*) ++ "\n"
   117     def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
    83     def l(args: Any*): String = sc.s(args:_*) ++ ":\n"
   118 }
    84 }
   119 
    85 
   120 // this allows us to write things like
    86 // this allows us to write things like
   121 // i"add" and l"Label"
    87 // i"iadd" and l"Label"
   122 
    88 
   123 
    89 
   124 // environments 
    90 // environments 
   125 type Env = Map[String, Int]
    91 type Env = Map[String, Int]
   126 
    92 
   136   case Num(i) => i"ldc $i"
   102   case Num(i) => i"ldc $i"
   137   case Var(s) => i"iload ${env(s)} \t\t; $s"
   103   case Var(s) => i"iload ${env(s)} \t\t; $s"
   138   case Aop(op, a1, a2) => 
   104   case Aop(op, a1, a2) => 
   139     compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
   105     compile_aexp(a1, env) ++ compile_aexp(a2, env) ++ compile_op(op)
   140 }
   106 }
       
   107 
   141 
   108 
   142 // boolean expression compilation
   109 // boolean expression compilation
   143 //  - the jump-label is for where to jump if the condition is not true
   110 //  - the jump-label is for where to jump if the condition is not true
   144 def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
   111 def compile_bexp(b: BExp, env : Env, jmp: String) : String = b match {
   145   case True => ""
   112   case True => ""
   254 }
   221 }
   255 
   222 
   256 @main
   223 @main
   257 def test2() =
   224 def test2() =
   258   run(fib_test, "fib")
   225   run(fib_test, "fib")
       
   226 
       
   227 
       
   228 
       
   229 
       
   230 
       
   231 
       
   232 /* Jasmin code for reading an integer
       
   233 
       
   234 .method public static read()I 
       
   235     .limit locals 10 
       
   236     .limit stack 10
       
   237 
       
   238     ldc 0 
       
   239     istore 1  ; this will hold our final integer 
       
   240 Label1: 
       
   241     getstatic java/lang/System/in Ljava/io/InputStream; 
       
   242     invokevirtual java/io/InputStream/read()I 
       
   243     istore 2 
       
   244     iload 2 
       
   245     ldc 10   ; the newline delimiter 
       
   246     isub 
       
   247     ifeq Label2 
       
   248     iload 2 
       
   249     ldc 32   ; the space delimiter 
       
   250     isub 
       
   251     ifeq Label2
       
   252 
       
   253     iload 2 
       
   254     ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
       
   255     isub 
       
   256     ldc 10 
       
   257     iload 1 
       
   258     imul 
       
   259     iadd 
       
   260     istore 1 
       
   261     goto Label1 
       
   262 Label2: 
       
   263     ;when we come here we have our integer computed in local variable 1 
       
   264     iload 1 
       
   265     ireturn 
       
   266 .end method
       
   267 
       
   268 */