;; if that might need to return a value
(define impc:ir:compiler:if
(lambda (ast types . hint?)
;; (println 'ast: ast)
(let* ((os (make-string 0))
(elset (if (or (null? (cdddr ast))
(null? (cadddr ast)))
#f #t))
(num (number->string (llvm:count++))))
;; first do compare expression
(emit (impc:ir:compiler (cadr ast) types) os)
;; then break on compare
(emit (string-append "br i1 " (car (impc:ir:gname)) ", label %then" num ", "
"label %else" num "\n") os)
;; do then
(emit "\nthen" num ":\n" os)
(if (not (null? hint?))
(emit (impc:ir:compiler (caddr ast) types (car hint?)) os)
(emit (impc:ir:compiler (caddr ast) types) os))
(if (not (impc:ir:void? (cadr (impc:ir:gname))))
(emit (string-append "store " (cadr (impc:ir:gname)) " " (car (impc:ir:gname))
", " (cadr (impc:ir:gname)) "* %ifptr" num "\n") os))
(emit "br label %ifcont" num "\n" os)
(define a (impc:ir:gname))
;; do else if requried
(if elset
(begin (emit "\nelse" num ":\n" os)
(if (not (null? hint?))
(emit (impc:ir:compiler (cadddr ast) types (car hint?)) os)
(emit (impc:ir:compiler (cadddr ast) types) os))
(if (not (impc:ir:void? (cadr (impc:ir:gname))))
(emit (string-append "store " (cadr (impc:ir:gname)) " " (car (impc:ir:gname))
", " (cadr (impc:ir:gname)) "* %ifptr" num "\n") os))
(emit "br label %ifcont" num "\n" os))
(begin (emit "\nelse" num ":\n" os)
(emit "br label %ifcont" num "\n" os)))