aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2019-07-03 00:16:16 -0700
committerMichael Forney <mforney@mforney.org>2019-07-03 02:40:53 -0700
commitd875bf74986b87378f80d7be4cd08e8eb8e91c65 (patch)
tree605f5b0b3339a9f9c049d63cb796f2d44f48ef15
parent94ff44d9f3df0ce081bd66c5ebebee660608342b (diff)
Convert controlling expression of loops to bool
-rw-r--r--stmt.c6
-rw-r--r--test/do-loop.qbe9
-rw-r--r--test/for-loop.qbe1
-rw-r--r--test/varargs.qbe15
-rw-r--r--test/while-condition.c7
-rw-r--r--test/while-condition.qbe23
6 files changed, 47 insertions, 14 deletions
diff --git a/stmt.c b/stmt.c
index f7dd0cd..cf5209d 100644
--- 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
index 698e310..d31c80c 100644
--- 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
index 56b38b1..6d16cf3 100644
--- 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
index 2a760d6..cb3542a 100644
--- 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
new file mode 100644
index 0000000..e346212
--- /dev/null
+++ 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
new file mode 100644
index 0000000..23b3b15
--- /dev/null
+++ 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
+}