boot2

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

051-init-array-list.scm (1267B)


      1 ;; tests/cc-cg/51-init-array-list.scm — global int[3] = {1,2,4};
      2 ;; main returns a[0]+a[1]+a[2]. §E.3.
      3 ;; Runtime: exits 7.
      4 
      5 (let* ((cg  (cg-init))
      6        (elem %t-i32)
      7        (aty (%ctype 'arr 12 4 (cons elem 3)))
      8        (a   (%sym "a" 'var 'static aty #f))
      9        ;; Three 4-byte LE pieces.
     10        (e1 (make-bytevector 4 0)) (_1 (bytevector-u8-set! e1 0 1))
     11        (e2 (make-bytevector 4 0)) (_2 (bytevector-u8-set! e2 0 2))
     12        (e3 (make-bytevector 4 0)) (_3 (bytevector-u8-set! e3 0 4)))
     13   (cg-emit-global cg a (list e1 e2 e3))
     14   (cg-fn-begin cg "main" '() %t-i32)
     15   ;; Sum a[0]+a[1]+a[2]. We avoid array→ptr decay (which is a
     16   ;; parser-side concern) by taking &a explicitly, casting to int*,
     17   ;; then doing ordinary pointer arithmetic.
     18   (let ((emit-elem
     19          (lambda (idx)
     20            (cg-push-sym cg a) (cg-take-addr cg)  ; rval ptr-to-arr
     21            (cg-cast cg (%ctype 'ptr 8 8 elem))   ; relabel as int*
     22            (cg-push-imm cg %t-i32 idx)
     23            (cg-binop cg 'add)                    ; int*  (scaling x 4)
     24            (cg-push-deref cg) (cg-load cg))))    ; rval int
     25     (emit-elem 0)
     26     (emit-elem 1)
     27     (cg-binop cg 'add)
     28     (emit-elem 2)
     29     (cg-binop cg 'add))
     30   (cg-return cg)
     31   (cg-fn-end cg)
     32   (write-bv-fd 1 (cg-finish cg)))