boot2

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

050-init-addr.scm (896B)


      1 ;; tests/cc-cg/50-init-addr.scm — global pointer initialized to address
      2 ;; of another global. §E.2.
      3 ;;
      4 ;; Models:
      5 ;;   int x = 7;
      6 ;;   int *p = &x;
      7 ;;   int main(void) { return *p; }
      8 ;; Runtime: exits 7.
      9 
     10 (let* ((cg  (cg-init))
     11        ;; int x = 7
     12        (x   (%sym "x" 'var 'static %t-i32 #f))
     13        (bvx (make-bytevector 4 0))
     14        (_x  (bytevector-u8-set! bvx 0 7))
     15        ;; int *p = &x  -> structured init: a single label-ref to cc__x
     16        (p   (%sym "p" 'var 'static (%ctype 'ptr 8 8 %t-i32) #f)))
     17   (cg-emit-global cg x (list bvx))
     18   (cg-emit-global cg p (list (cons 'label-ref "cc__x")))
     19   (cg-fn-begin cg "main" '() %t-i32)
     20   ;; return *p
     21   (cg-push-sym cg p)
     22   (cg-load cg)            ; rval ptr-to-int (the value of p)
     23   (cg-push-deref cg)      ; lval int through pointer
     24   (cg-load cg)            ; rval int
     25   (cg-return cg)
     26   (cg-fn-end cg)
     27   (write-bv-fd 1 (cg-finish cg)))