boot2

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

36-struct-load.scm (1471B)


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