boot2

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

053-init-struct-pos.scm (1404B)


      1 ;; tests/cc-cg/53-init-struct-pos.scm — global struct {int a; int b;}
      2 ;; with positional initializer. §E.5.
      3 ;;
      4 ;; Models: struct S { int a; int b; }; struct S s = {3, 4}; return s.a + s.b*10;
      5 ;; main returns 43.
      6 ;;
      7 ;; cc-cg drives the cg API directly; we read a (offset 0) and b (offset 4)
      8 ;; via &s + offset and a cast to int*.
      9 
     10 (let* ((cg  (cg-init))
     11        ;; Build struct ctype: { int a; int b; }
     12        (sty (%ctype 'struct 8 4
     13                     (list "S" #t
     14                           (list (list "a" %t-i32 0)
     15                                 (list "b" %t-i32 4)))))
     16        (s   (%sym "s" 'var 'static sty #f))
     17        ;; Init: 3 (4 bytes LE) | 4 (4 bytes LE)
     18        (bvA (make-bytevector 4 0)) (_a (bytevector-u8-set! bvA 0 3))
     19        (bvB (make-bytevector 4 0)) (_b (bytevector-u8-set! bvB 0 4)))
     20   (cg-emit-global cg s (list bvA bvB))
     21   (cg-fn-begin cg "main" '() %t-i32)
     22   (let ((load-field
     23          (lambda (off)
     24            (cg-push-sym cg s) (cg-take-addr cg)
     25            (cg-cast cg (%ctype 'ptr 8 8 %t-i32))
     26            (cg-push-imm cg %t-i32 off)
     27            (cg-binop cg 'add)
     28            (cg-push-deref cg) (cg-load cg))))
     29     (load-field 0)              ; s.a == 3
     30     (load-field 1)              ; s.b == 4 (4 bytes / 4 = +1 in int*-units)
     31     (cg-push-imm cg %t-i32 10)
     32     (cg-binop cg 'mul)
     33     (cg-binop cg 'add))
     34   (cg-return cg)
     35   (cg-fn-end cg)
     36   (write-bv-fd 1 (cg-finish cg)))