022-postinc.scm (1061B)
1 ;; tests/cc-cg/22-postinc.scm — post-increment returns OLD value (§B.2). 2 ;; 3 ;; Models: int x = 5; int y = x++; return x*10 + y; → exit 65. 4 ;; (x is 6 after increment, y captures the pre-increment 5.) 5 ;; 6 ;; cg-postinc operates atomically on a lval at the top of the vstack: 7 ;; loads the old value, emits the +1 store, and pushes the OLD value. 8 9 (let ((cg (cg-init))) 10 (cg-fn-begin cg "main" '() %t-i32) 11 (let* ((off-x (cg-alloc-slot cg 4 4)) 12 (off-y (cg-alloc-slot cg 4 4)) 13 (sym-x (%sym "x" 'var 'auto %t-i32 off-x)) 14 (sym-y (%sym "y" 'var 'auto %t-i32 off-y))) 15 ;; x = 5 16 (cg-push-sym cg sym-x) 17 (cg-push-imm cg %t-i32 5) 18 (cg-assign cg) (cg-pop cg) 19 ;; y = x++ 20 (cg-push-sym cg sym-y) 21 (cg-push-sym cg sym-x) 22 (cg-postinc cg) 23 (cg-assign cg) (cg-pop cg) 24 ;; return x*10 + y 25 (cg-push-sym cg sym-x) (cg-load cg) 26 (cg-push-imm cg %t-i32 10) (cg-binop cg 'mul) 27 (cg-push-sym cg sym-y) (cg-load cg) 28 (cg-binop cg 'add) 29 (cg-return cg)) 30 (cg-fn-end cg) 31 (write-bv-fd 1 (cg-finish cg)))