summaryrefslogtreecommitdiff
path: root/module/language/tree-il/optimize.scm
blob: b1d8b82942e1c69badf179cd19f628578fdd981d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
;;; Tree-il optimizer

;; Copyright (C) 2009, 2010-2015, 2018-2021 Free Software Foundation, Inc.

;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

;;; Code:

(define-module (language tree-il optimize)
  #:use-module (ice-9 match)
  ;; FIXME: Perhaps allow bootstrap builds to skip fix-letrec, because
  ;; it imports intset, intmap, etc.
  #:use-module (language tree-il fix-letrec)
  #:use-module (system base optimize)
  #:export (optimize
            make-lowerer
            tree-il-optimizations))

(define (make-optimizer opts)
  (define-syntax lookup
    (syntax-rules ()
      ((lookup kw id)
       (lookup kw id id))
      ((lookup kw submodule proc)
       (and (assq-ref opts kw)
            (module-ref (resolve-interface '(language tree-il submodule))
                        'proc)))))
  (let ((verify     (or (lookup #:verify-tree-il? debug verify-tree-il)
                        (lambda (exp) exp)))
        (modulify   (lookup #:resolve-free-vars? resolve-free-vars))
        (resolve    (lookup #:resolve-primitives? primitives resolve-primitives))
        (expand     (lookup #:expand-primitives? primitives expand-primitives))
        (letrectify (lookup #:letrectify? letrectify))
        (seal?      (assq-ref opts #:seal-private-bindings?))
        (xinline?   (assq-ref opts #:cross-module-inlining?))
        (peval      (lookup #:partial-eval? peval))
        (eta-expand (lookup #:eta-expand? eta-expand))
        (inlinables (lookup #:inlinable-exports? inlinable-exports)))
    (define-syntax-rule (run-pass! (proc exp arg ...))
      (when proc (set! exp (verify (proc exp arg ...)))))
    (lambda (exp env)
      (verify exp)
      (run-pass! (modulify exp))
      (run-pass! (resolve exp env))
      (run-pass! (expand exp))
      (run-pass! (letrectify exp #:seal-private-bindings? seal?))
      (run-pass! (fix-letrec exp))
      (run-pass! (peval exp env #:cross-module-inlining? xinline?))
      (run-pass! (eta-expand exp))
      (run-pass! (inlinables exp))
      exp)))

(define (optimize x env opts)
  ((make-optimizer opts) x env))

(define (tree-il-optimizations)
  (available-optimizations 'tree-il))

(define (make-lowerer optimization-level opts)
  (define (kw-arg-ref args kw default)
    (match (memq kw args)
      ((_ val . _) val)
      (_ default)))
  (define (enabled-for-level? level) (<= level optimization-level))
  (make-optimizer
   (let lp ((all-opts (tree-il-optimizations)))
     (match all-opts
       (() '())
       (((kw level) . all-opts)
        (acons kw (kw-arg-ref opts kw (enabled-for-level? level))
               (lp all-opts)))))))