021-preinc.scm (928B)
1 ;; tests/cc-cg/21-preinc.scm — pre-increment on a simple lval (§B.1). 2 ;; 3 ;; Models: int x = 5; ++x; return x; → exit 6. 4 ;; 5 ;; The "++x" sequence requires the lhs lval to be preserved across 6 ;; the load (so we can store back). cg-dup duplicates the top vstack 7 ;; entry, giving us two lvals: one we load, one we keep for assign. 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 (sym-x (%sym "x" 'var 'auto %t-i32 off-x))) 13 ;; x = 5 14 (cg-push-sym cg sym-x) 15 (cg-push-imm cg %t-i32 5) 16 (cg-assign cg) (cg-pop cg) 17 ;; ++x: push lval; dup; load; push 1; add; assign; pop result 18 (cg-push-sym cg sym-x) 19 (cg-dup cg) 20 (cg-load cg) 21 (cg-push-imm cg %t-i32 1) 22 (cg-binop cg 'add) 23 (cg-assign cg) (cg-pop cg) 24 ;; return x 25 (cg-push-sym cg sym-x) 26 (cg-load cg) 27 (cg-return cg)) 28 (cg-fn-end cg) 29 (write-bv-fd 1 (cg-finish cg)))