impc:ir:compiler:array-ref   scheme


Defined in:  https://github.com/digego/extempore/tree/v0.8.9/runtime/llvmir.xtm

Implementation

(define impc:ir:compiler:array-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:array? ttype))
          (impc:compiler:print-bad-type-error-with-ast (cadr var) "invalid array type" ast))
      (if (not (impc:ir:fixed-point? (impc:ir:get-type-from-str (cadr idx))))
          (impc:compiler:print-bad-type-error-with-ast (impc:ir:get-type-str t) "array index must be an integer" ast))
      (if (and (integer? (caddr ast)) (> (+ 1 (caddr ast)) (cadr ttype)))
          (impc:compiler:print-index-oob-error 'array ast))
      (if (and (not (impc:ir:pointer? ttype))
               (not (number? (caddr ast))))
          (impc:compiler:print-compiler-error "Value Arrays can only be accessed by literal integer offets.  You could try using an array reference instead?" 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
          ;; (log-error 'Compiler 'Error: 'array-ref 'must 'take 'a 'pointer 'to 'an 'array 'not (cadr var))
          (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" (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)
                 (set! tt (impc:ir:get-type-str (caddr ttype)))
                 (emit (impc:ir:gname "val" tt) " = load " tt ", " tt "* " (car (impc:ir:gname "_val")) "\n" os)))
      (impc:ir:strip-space os))))


Back to Index

Similar Entries