(define impc:ir:compiler:array-ref-ptr
(lambda (ast types)
(let* ((os (make-string 0))
(index-str (impc:ir:compiler (caddr ast) types))
(idx (impc:ir:gname))
(var-str (impc:ir:compiler (cadr ast) types))
(var (impc:ir:gname))
(ttype (impc:ir:get-type-from-str (cadr var))))
;; type tests
(if (not (impc:ir:array? ttype))
(impc:compiler:print-bad-type-error-with-ast (cadr var) "invalid array type" ast))
(if (> (impc:ir:get-ptr-depth ttype) 1)
(impc:compiler:print-compiler-error "pointer depth too great for array-set!" ast))
(if (not (impc:ir:fixed-point? (impc:ir:get-type-from-str (cadr idx))))
(impc:compiler:print-bad-type-error-with-ast (cadr idx) "index must be an integer" ast))
(if (and (integer? (caddr ast)) (> (+ 1 (caddr ast)) (cadr ttype)))
(impc:compiler:print-index-oob-error 'array ast))
(emit index-str os)
(emit var-str os)
(emit "; array ref\n" os)
(if (not (impc:ir:pointer? ttype)) ;; must be an array if we're not a pointer
(impc:compiler:print-bad-type-error-with-ast (cadr ttype) "array-ref-ptr must take a pointer to an array" ast)
(emit (string-append (impc:ir:gname "val" (string-append (impc:ir:get-type-str (caddr ttype)) "*"))
" = getelementptr " (impc:ir:pointer-- (cadr var)) ", " (cadr var) " " (car var)
", i32 0, " (cadr idx) " " (car idx) "\n") os))
(impc:ir:strip-space os))))