068-ptr-cmp.scm (1191B)
1 ;; tests/cc-cg/68-ptr-cmp.scm — pointer comparison via unsigned ltu (§K.6). 2 ;; Models: 3 ;; int main(void) { 4 ;; int x = 0; int y = 0; 5 ;; int *a = &x; int *b = &y; 6 ;; /* The two slots' relative order in the frame is fixed by 7 ;; * cg-alloc-slot, but we only care that the comparison is 8 ;; * unsigned. */ 9 ;; return (a < b) || (b < a); /* one of them must be true */ 10 ;; } 11 ;; Returns 1 — locks in that cg-binop dispatches to the unsigned 12 ;; variant when either operand is ptr/arr. 13 14 (let* ((cg (cg-init)) 15 (params (cg-fn-begin cg "main" '() %t-i32)) 16 (x-sl (cg-alloc-slot cg 4 4)) 17 (y-sl (cg-alloc-slot cg 4 4)) 18 (x-sym (%sym "x" 'var 'auto %t-i32 x-sl)) 19 (y-sym (%sym "y" 'var 'auto %t-i32 y-sl))) 20 ;; (a < b) || (b < a) — without short-circuit. Take addresses 21 ;; explicitly and feed cg-binop 'lt twice. 22 (cg-push-sym cg x-sym) (cg-take-addr cg) 23 (cg-push-sym cg y-sym) (cg-take-addr cg) 24 (cg-binop cg 'lt) 25 (cg-push-sym cg y-sym) (cg-take-addr cg) 26 (cg-push-sym cg x-sym) (cg-take-addr cg) 27 (cg-binop cg 'lt) 28 (cg-binop cg 'or) 29 (cg-return cg) 30 (cg-fn-end cg) 31 (write-bv-fd 1 (cg-finish cg)))