cproc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/cproc
Log | Files | Refs | Submodules | README | LICENSE

commit d875bf74986b87378f80d7be4cd08e8eb8e91c65
parent 94ff44d9f3df0ce081bd66c5ebebee660608342b
Author: Michael Forney <mforney@mforney.org>
Date:   Wed,  3 Jul 2019 00:16:16 -0700

Convert controlling expression of loops to bool

Diffstat:
Mstmt.c | 6+++---
Mtest/do-loop.qbe | 9+++++----
Mtest/for-loop.qbe | 1+
Mtest/varargs.qbe | 15++++++++-------
Atest/while-condition.c | 7+++++++
Atest/while-condition.qbe | 23+++++++++++++++++++++++
6 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/stmt.c b/stmt.c @@ -153,7 +153,7 @@ stmt(struct func *f, struct scope *s) next(); s = mkscope(s); expect(TLPAREN, "after 'while'"); - e = expr(s); + e = exprconvert(expr(s), &typebool); expect(TRPAREN, "after expression"); label[0] = mkblock("while_cond"); @@ -191,7 +191,7 @@ stmt(struct func *f, struct scope *s) expect(TWHILE, "after 'do' statement"); expect(TLPAREN, "after 'while'"); funclabel(f, label[1]); - e = expr(s); + e = exprconvert(expr(s), &typebool); expect(TRPAREN, "after expression"); v = funcexpr(f, e); @@ -226,7 +226,7 @@ stmt(struct func *f, struct scope *s) delexpr(e); } expect(TSEMICOLON, NULL); - e = tok.kind == TRPAREN ? NULL : expr(s); + e = tok.kind == TRPAREN ? NULL : exprconvert(expr(s), &typebool); expect(TRPAREN, NULL); funclabel(f, label[1]); diff --git a/test/do-loop.qbe b/test/do-loop.qbe @@ -23,9 +23,10 @@ function w $main() { %.10 =w loadsw %.1 %.11 =w sub %.10, 1 storew %.11, %.1 - jnz %.10, @do_body.3, @do_join.5 + %.12 =w cnew %.10, 0 + jnz %.12, @do_body.3, @do_join.5 @do_join.5 - %.12 =w loadsw %.3 - %.13 =w cnew %.12, 2 - ret %.13 + %.13 =w loadsw %.3 + %.14 =w cnew %.13, 2 + ret %.14 } diff --git a/test/for-loop.qbe b/test/for-loop.qbe @@ -15,6 +15,7 @@ function $f() { %.5 =w loadsw %.1 %.6 =w add %.5, 1 storew %.6, %.1 + %.7 =w cnew %.6, 0 jmp @for_cond.3 @for_join.6 ret diff --git a/test/varargs.qbe b/test/varargs.qbe @@ -8,14 +8,15 @@ function $f(w %.1, ...) { vastart %.3 @while_cond.3 %.4 =w loadsw %.2 - jnz %.4, @while_body.4, @while_join.5 + %.5 =w cnew %.4, 0 + jnz %.5, @while_body.4, @while_join.5 @while_body.4 - %.5 =w vaarg %.3 - %.6 =s vaarg %.3 - %.7 =l vaarg %.3 - %.8 =w loadsw %.2 - %.9 =w sub %.8, 1 - storew %.9, %.2 + %.6 =w vaarg %.3 + %.7 =s vaarg %.3 + %.8 =l vaarg %.3 + %.9 =w loadsw %.2 + %.10 =w sub %.9, 1 + storew %.10, %.2 jmp @while_cond.3 @while_join.5 ret diff --git a/test/while-condition.c b/test/while-condition.c @@ -0,0 +1,7 @@ +int main(void) { + double x = 1; + + while (x) + x /= 2; + return x; +} diff --git a/test/while-condition.qbe b/test/while-condition.qbe @@ -0,0 +1,23 @@ +export +function w $main() { +@start.1 + %.1 =l alloc8 8 +@body.2 + %.2 =l add %.1, 0 + %.3 =d swtof 1 + stored %.3, %.2 +@while_cond.3 + %.4 =d loadd %.1 + %.5 =w cned %.4, d_0 + jnz %.5, @while_body.4, @while_join.5 +@while_body.4 + %.6 =d loadd %.1 + %.7 =d swtof 2 + %.8 =d div %.6, %.7 + stored %.8, %.1 + jmp @while_cond.3 +@while_join.5 + %.9 =d loadd %.1 + %.10 =w dtosi %.9 + ret %.10 +}