(define impc:ir:compiler:pointer-ref
(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)))
(tt '()))
;; type tests
(if (not (impc:ir:pointer? ttype))
(impc:compiler:print-bad-type-error-with-ast (cadr var) "array must take a pointer to an array" 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))
(emit index-str os)
(emit var-str os)
(emit "; pointer ref\n" os)
(if (not (impc:ir:pointer? ttype)) ;; must be an array if we're not a pointer
(emit (string-append (impc:ir:gname "val" (impc:ir:get-type-str (caddr ttype))) " = extractvalue "
(cadr var) " " (car var) ", " (car idx) "\n") os)
(begin (emit (string-append (impc:ir:gname "val" (cadr var)) " = getelementptr "
(impc:ir:pointer-- (cadr var)) ", " (cadr var) " " (car var) ", " (cadr idx) " " (car idx) "\n") os)
(set! tt (impc:ir:get-type-str (impc:ir:pointer-- (impc:ir:get-type-from-str (cadr var)))))))
(if (impc:ir:pointer? ttype)
(emit (string-append (impc:ir:gname "val" tt) " = load " tt ", " tt "* "
(car (impc:ir:gname 1)) "\n") os))
(impc:ir:strip-space os))))