boot2

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

028-ternary.scm (1009B)


      1 ;; tests/cc-cg/28-ternary.scm — ternary leaves exactly one rval (§H.1).
      2 ;;
      3 ;; Models: int c = 1; int x = c ? 7 : 9; return x; → exit 7.
      4 ;;
      5 ;; cg-ifelse-merge consumes the cond, runs both thunks, merges each
      6 ;; branch's top rval into a single result slot, and leaves one frame
      7 ;; rval on the vstack.
      8 
      9 (let ((cg (cg-init)))
     10   (cg-fn-begin cg "main" '() %t-i32)
     11   (let* ((off-c (cg-alloc-slot cg 4 4))
     12          (sym-c (%sym "c" 'var 'auto %t-i32 off-c))
     13          (off-x (cg-alloc-slot cg 4 4))
     14          (sym-x (%sym "x" 'var 'auto %t-i32 off-x)))
     15     ;; c = 1
     16     (cg-push-sym cg sym-c)
     17     (cg-push-imm cg %t-i32 1)
     18     (cg-assign cg) (cg-pop cg)
     19     ;; x = c ? 7 : 9
     20     (cg-push-sym cg sym-x)
     21     (cg-push-sym cg sym-c) (cg-load cg)
     22     (cg-ifelse-merge cg
     23       (lambda () (cg-push-imm cg %t-i32 7))
     24       (lambda () (cg-push-imm cg %t-i32 9)))
     25     (cg-assign cg) (cg-pop cg)
     26     ;; return x
     27     (cg-push-sym cg sym-x) (cg-load cg)
     28     (cg-return cg))
     29   (cg-fn-end cg)
     30   (write-bv-fd 1 (cg-finish cg)))