diff --git a/module/language/tree-il/compile-glil.scm b/module/language/tree-il/compile-glil.scm index 86b610f..57a46c8 100644 --- a/module/language/tree-il/compile-glil.scm +++ b/module/language/tree-il/compile-glil.scm @@ -22,6 +22,7 @@ #:use-module (system base syntax) #:use-module (system base pmatch) #:use-module (system base message) + #:use-module (srfi srfi-1) #:use-module (ice-9 receive) #:use-module (language glil) #:use-module (system vm instruction) @@ -394,20 +395,28 @@ (cons (primitive-ref-name proc) (length args))) (hash-ref *primcall-ops* (primitive-ref-name proc)))) => (lambda (op) - (for-each comp-push args) - (emit-code src (make-glil-call op (length args))) - (case (instruction-pushes op) - ((0) - (case context - ((tail push vals) (emit-code #f (make-glil-void)))) - (maybe-emit-return)) - ((1) - (case context - ((drop) (emit-code #f (make-glil-call 'drop 1)))) - (maybe-emit-return)) - (else - (error "bad primitive op: too many pushes" - op (instruction-pushes op)))))) + (if (every const? args) + (let* ((proc (module-ref the-scm-module + (primitive-ref-name proc))) + (args (map const-exp args))) + ;; constant folding + (emit-code src + (make-glil-const (apply proc args)))) + (begin + (for-each comp-push args) + (emit-code src (make-glil-call op (length args))) + (case (instruction-pushes op) + ((0) + (case context + ((tail push vals) (emit-code #f (make-glil-void)))) + (maybe-emit-return)) + ((1) + (case context + ((drop) (emit-code #f (make-glil-call 'drop 1)))) + (maybe-emit-return)) + (else + (error "bad primitive op: too many pushes" + op (instruction-pushes op)))))))) ;; da capo al fine ((and (lexical-ref? proc)