equal
deleted
inserted
replaced
104 case e::es => CPS(e)(y => aux(es, vs ::: List(y))) |
104 case e::es => CPS(e)(y => aux(es, vs ::: List(y))) |
105 } |
105 } |
106 aux(args, Nil) |
106 aux(args, Nil) |
107 } |
107 } |
108 case Sequence(e1, e2) => |
108 case Sequence(e1, e2) => |
109 CPS(e1)(y1 => CPS(e2)(y2 => k(y2))) |
109 CPS(e1)(_ => CPS(e2)(y2 => k(y2))) |
110 case Write(e) => { |
110 case Write(e) => { |
111 val z = Fresh("tmp") |
111 val z = Fresh("tmp") |
112 CPS(e)(y => KLet(z, KWrite(y), k(KVar(z)))) |
112 CPS(e)(y => KLet(z, KWrite(y), k(KVar(z)))) |
113 } |
113 } |
114 } |
114 } |
115 |
115 |
|
116 //initial continuation |
116 def CPSi(e: Exp) = CPS(e)(KReturn) |
117 def CPSi(e: Exp) = CPS(e)(KReturn) |
117 |
118 |
118 // some testcases |
119 // some testcases |
119 val e1 = Aop("*", Var("a"), Num(3)) |
120 val e1 = Aop("*", Var("a"), Num(3)) |
120 CPSi(e1) |
121 CPSi(e1) |
188 case KReturn(v) => |
189 case KReturn(v) => |
189 i"ret i32 ${compile_val(v)}" |
190 i"ret i32 ${compile_val(v)}" |
190 case KLet(x: String, v: KVal, e: KExp) => |
191 case KLet(x: String, v: KVal, e: KExp) => |
191 i"%$x = ${compile_val(v)}" ++ compile_exp(e) |
192 i"%$x = ${compile_val(v)}" ++ compile_exp(e) |
192 case KIf(x, e1, e2) => { |
193 case KIf(x, e1, e2) => { |
193 val if_br = Fresh("if_br") |
194 val if_br = Fresh("if_branch") |
194 val else_br = Fresh("else_br") |
195 val else_br = Fresh("else_branch") |
195 i"br i1 %$x, label %$if_br, label %$else_br" ++ |
196 i"br i1 %$x, label %$if_br, label %$else_br" ++ |
196 l"\n$if_br" ++ |
197 l"\n$if_br" ++ |
197 compile_exp(e1) ++ |
198 compile_exp(e1) ++ |
198 l"\n$else_br" ++ |
199 l"\n$else_br" ++ |
199 compile_exp(e2) |
200 compile_exp(e2) |
206 |
207 |
207 declare i32 @printf(i8*, ...) |
208 declare i32 @printf(i8*, ...) |
208 |
209 |
209 define i32 @printInt(i32 %x) { |
210 define i32 @printInt(i32 %x) { |
210 %t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0 |
211 %t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0 |
211 call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %x) |
212 call i32 (i8*, ...) @printf(i8* %t0, i32 %x) |
212 ret i32 %x |
213 ret i32 %x |
213 } |
214 } |
214 |
215 |
215 """ |
216 """ |
216 |
217 |