boot2

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

036-struct-load.scm (1436B)


      1 ;; tests/cc-cg/36-struct-load.scm — struct member load via cg-push-field.
      2 ;;
      3 ;; Models:
      4 ;;   struct S { int a; int b; };
      5 ;;   struct S s;
      6 ;;   s.a = 5; s.b = 7;     <- using direct cg-push-field + cg-assign
      7 ;;   return s.b - s.a;     <- 2; assert (s.b - s.a == 2) -> exit 1
      8 ;;
      9 ;; If cg-push-field is the broken stub it would access offset 0 for
     10 ;; both fields, so loading s.b would also yield 5 and the equality
     11 ;; check would fail (exit 0). With correct field-offset arithmetic
     12 ;; the result is 2 and exit is 1.
     13 
     14 (let* ((cg     (cg-init))
     15        (st-ty  (%ctype 'struct 8 4
     16                        (list "S" #t
     17                              (list (list "a" %t-i32 0)
     18                                    (list "b" %t-i32 4))))))
     19   (cg-fn-begin cg "main" '() %t-i32)
     20   (let* ((off-s (cg-alloc-slot cg 8 4))
     21          (sym-s (%sym "s" 'var 'auto st-ty off-s)))
     22     ;; s.a = 5
     23     (cg-push-sym cg sym-s)
     24     (cg-push-field cg "a")
     25     (cg-push-imm cg %t-i32 5)
     26     (cg-assign cg) (cg-pop cg)
     27     ;; s.b = 7
     28     (cg-push-sym cg sym-s)
     29     (cg-push-field cg "b")
     30     (cg-push-imm cg %t-i32 7)
     31     (cg-assign cg) (cg-pop cg)
     32     ;; return (s.b - s.a) == 2
     33     (cg-push-sym cg sym-s)
     34     (cg-push-field cg "b")
     35     (cg-load cg)
     36     (cg-push-sym cg sym-s)
     37     (cg-push-field cg "a")
     38     (cg-load cg)
     39     (cg-binop cg 'sub)
     40     (cg-push-imm cg %t-i32 2)
     41     (cg-binop cg 'eq)
     42     (cg-return cg))
     43   (cg-fn-end cg)
     44   (write-bv-fd 1 (cg-finish cg)))