boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

291-do-while-continue.c (1059B)


      1 /* `continue` in a do-while loop must transfer control to the *cond*
      2  * test (C11 §6.8.6.2), not back to the top of the body. parse-do-stmt
      3  * placed both the body and the cond test inside the cg-loop body
      4  * thunk — but cg-loop's underlying %loop_tag macro labels the top of
      5  * its body as `tag_top`, and `%continue(tag)` jumps there. The result
      6  * was that a `continue` re-entered the body from the start, executing
      7  * one extra iteration of the body before the cond was finally tested.
      8  *
      9  * Detect: with the buggy semantics, `continue` followed by a
     10  * normally-terminating cond test bypasses the test and runs the body
     11  * once more. Below, the `continue` fires when n==3 — at that point
     12  * the cond `n < 3` is already false, so a correct compiler exits the
     13  * loop and leaves n=3. The buggy compiler instead restarts the body
     14  * (n becomes 4) before the cond finally fires.
     15  */
     16 
     17 int main(int argc, char **argv) {
     18     int n = 0;
     19     do {
     20         n = n + 1;
     21         if (n == 3) continue;
     22     } while (n < 3);
     23     if (n != 3) return 1;
     24     return 0;
     25 }