063-do-while.scm (1417B)
1 ;; tests/cc-cg/63-do-while.scm — `do { x = x + 1; } while (x < 3);` (§F.1) 2 ;; Models: int main(void) { int x = 0; do x = x + 1; while (x < 3); return x; } 3 ;; Exercises composing cg-loop + cg-if + cg-break for do-while. 4 ;; Expected exit: 3. 5 6 (let* ((cg (cg-init)) 7 (params (cg-fn-begin cg "main" '() %t-i32)) 8 (x-slot (cg-alloc-slot cg 4 4)) 9 (x-sym (%sym "x" 'var 'auto %t-i32 x-slot))) 10 ;; x = 0 11 (cg-push-sym cg x-sym) 12 (cg-push-imm cg %t-i32 0) 13 (cg-assign cg) (cg-pop cg) 14 ;; do { x = x + 1; } while (x < 3); 15 ;; 16 ;; Encoding (per parse-do-stmt): cg-loop with empty head-thunk; body 17 ;; runs the user body, then evaluates the while-cond and if zero 18 ;; (lnot then if), break. 19 (cg-loop cg 20 (lambda () #t) ; do-while: head is empty (cond at bottom) 21 (lambda (tag) 22 ;; x = x + 1 23 (cg-push-sym cg x-sym) 24 (cg-push-sym cg x-sym) (cg-load cg) 25 (cg-push-imm cg %t-i32 1) 26 (cg-binop cg 'add) 27 (cg-assign cg) (cg-pop cg) 28 ;; while cond: x < 3 ; if !cond, break 29 (cg-push-sym cg x-sym) (cg-load cg) 30 (cg-push-imm cg %t-i32 3) 31 (cg-binop cg 'lt) 32 (cg-unop cg 'lnot) 33 (cg-if cg (lambda () (cg-break cg tag))))) 34 (cg-push-sym cg x-sym) (cg-load cg) 35 (cg-return cg) 36 (cg-fn-end cg) 37 (write-bv-fd 1 (cg-finish cg)))