(define impc:ir:compiler:vector-ref
(lambda (ast types)
(let* ((os (make-string 0))
(index-str (impc:ir:compiler (caddr ast) types *impc:ir:si32*))
(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:vector? ttype))
(impc:compiler:print-bad-type-error-with-ast (cadr var) "invalid vector type" ast))
(if (not (impc:ir:fixed-point? (impc:ir:get-type-from-str (cadr idx))))
(impc:compiler:print-bad-type-error-with-ast ast 'index 'must 'be 'fixed-point 'not (cadr idx)))
(emit index-str os)
(emit var-str os)
(emit "; vector ref\n" os)
;; (println 'ttype: ttype)
;; (println 'var: var)
;; (println 'idx: idx)
;; (println 'ret: (caddr ttype))
;; (println 'pointer: (impc:ir:pointer? ttype))
(if (impc:ir:pointer? ttype) ;; a pointer?
(emit (string-append (impc:ir:gname "vect" (impc:ir:get-type-str (impc:ir:pointer-- (cadr var)))) " = load " (impc:ir:pointer-- (cadr var)) ", "
(cadr var) " " (car var) "\n"
(impc:ir:gname "val" (impc:ir:get-type-str (caddr ttype))) " = extractelement "
(cadr (impc:ir:gname "vect")) " " (car (impc:ir:gname "vect")) ", i32 " (car idx) "\n") os)
(emit (impc:ir:gname "val" (impc:ir:get-type-str (caddr ttype))) " = extractelement "
(cadr var) " " (car var) ", " (cadr idx) " " (car idx) "\n" os))
(impc:ir:strip-space os))))