Skip to content

Instantly share code, notes, and snippets.

@Bike
Created May 9, 2019 19:52
Show Gist options
  • Save Bike/47b5b599cca7f1cbe4724878c37e6e23 to your computer and use it in GitHub Desktop.
Save Bike/47b5b599cca7f1cbe4724878c37e6e23 to your computer and use it in GitHub Desktop.
;;;; in opt.lsp
;;; Given argument forms to an arbitrary arity arithmetic function,
;;; return new arguments with all constants folded together.
;;; FIXME: handler-case, return original arguments
;;; if there's an error (e.g. overflow).
(defun fold-constants (function argforms env)
(loop for form in argforms
when (constantp form env)
collect (ext:constant-form-value form env) into constants
else collect form into variables
finally
(return
(if (null constants)
argforms ; original by eq, in case we want to know later
(cons (apply function constants) variables)))))
;;;; altered defs in inline.lisp
(define-cleavir-compiler-macro + (&whole form &rest numbers &environment env)
(core:expand-associative '+ 'primop:inlined-two-arg-+ (cmp::fold-constants #'+ numbers env) 0))
(define-cleavir-compiler-macro * (&whole form &rest numbers &environment env)
(core:expand-associative '* 'primop:inlined-two-arg-* (cmp::fold-constants #'* numbers env) 1))
;;;; analogously in opt-number.lsp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment