|
| 1 | +;; Copyright (c) Nicola Mometto, Rich Hickey & contributors. |
| 2 | +;; The use and distribution terms for this software are covered by the |
| 3 | +;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) |
| 4 | +;; which can be found in the file epl-v10.html at the root of this distribution. |
| 5 | +;; By using this software in any fashion, you are agreeing to be bound by |
| 6 | +;; the terms of this license. |
| 7 | +;; You must not remove this notice, or any other, from this software. |
| 8 | + |
| 9 | +(ns clojure.tools.emitter.passes.jvm.clear-locals |
| 10 | + (:require [clojure.tools.analyzer.ast :refer [update-children]] |
| 11 | + [clojure.tools.analyzer.utils :refer [ctx rseqv]] |
| 12 | + [clojure.tools.analyzer.passes.jvm |
| 13 | + [annotate-branch :refer [annotate-branch]] |
| 14 | + [annotate-loops :refer [annotate-loops]]] |
| 15 | + [clojure.tools.analyzer.passes.collect-closed-overs :refer [collect-closed-overs]])) |
| 16 | + |
| 17 | +(def ^:dynamic *clears*) |
| 18 | + |
| 19 | +(defmulti -clear-locals :op) |
| 20 | +(defmulti should-not-clear :op) |
| 21 | + |
| 22 | +(defmethod should-not-clear :local |
| 23 | + [ast] |
| 24 | + (or (= :letfn (:local ast)) |
| 25 | + (:case-test ast))) |
| 26 | + |
| 27 | +(defmethod should-not-clear :binding |
| 28 | + [ast] |
| 29 | + (:case-test @(:atom ast))) |
| 30 | + |
| 31 | +(defmethod should-not-clear :default [ast] |
| 32 | + false) |
| 33 | + |
| 34 | +(defn maybe-clear-local |
| 35 | + [{:keys [name local env loops] :as ast}] |
| 36 | + (let [{:keys [closed-overs locals loop-closed-overs]} @*clears* |
| 37 | + loop-id (:loop-id env)] |
| 38 | + (if (and (#{:let :loop :catch :arg} local) |
| 39 | + (or (not (get (loop-closed-overs loop-id) name)) ;; if we're in a loop and the local is defined outside the loop |
| 40 | + (not loops) ;; it's only safe to clear it if we're in the loop exit path for this loop |
| 41 | + (and (not (loops loop-id)) ;; and if the local isn't defined outside different loop than this and we're |
| 42 | + (not (some (fn [id] (get (loop-closed-overs id) name)) loops)))) ;; in a recur path for that loop |
| 43 | + (or (not (closed-overs name)) ;; if it's a closed-over var, we can only clear it if we explicitely |
| 44 | + (:once env)) ;; declared the function to be run :once |
| 45 | + (not (locals name)) ;; if the local is in `locals` it means that it's used later in the body and can't be cleared here |
| 46 | + (not (should-not-clear ast))) ;; letfn bindings/case test |
| 47 | + (assoc ast :to-clear? true) |
| 48 | + ast))) |
| 49 | + |
| 50 | +(defn maybe-clear-this |
| 51 | + [{:keys [env] :as ast}] |
| 52 | + (-> (if (and (isa? (:context env) :ctx/return) |
| 53 | + (not (:in-try env))) |
| 54 | + (assoc ast :to-clear? true) |
| 55 | + ast) |
| 56 | + (update-children -clear-locals rseqv))) |
| 57 | + |
| 58 | +(defmethod -clear-locals :invoke |
| 59 | + [ast] |
| 60 | + (maybe-clear-this ast)) |
| 61 | + |
| 62 | +(defmethod -clear-locals :protocol-invoke |
| 63 | + [ast] |
| 64 | + (maybe-clear-this ast)) |
| 65 | + |
| 66 | +(defmethod -clear-locals :prim-invoke |
| 67 | + [ast] |
| 68 | + (maybe-clear-this ast)) |
| 69 | + |
| 70 | +(defmethod -clear-locals :static-call |
| 71 | + [ast] |
| 72 | + (maybe-clear-this ast)) |
| 73 | + |
| 74 | +(defmethod -clear-locals :instance-call |
| 75 | + [ast] |
| 76 | + (maybe-clear-this ast)) |
| 77 | + |
| 78 | +(defmethod -clear-locals :default |
| 79 | + [{:keys [closed-overs op loop-id] :as ast}] |
| 80 | + (if closed-overs |
| 81 | + (let [key (if (= :loop op) :loop-closed-overs ) ;; if we're in a loop those are not actually closed-overs |
| 82 | + [ast body-locals] (binding [*clears* (atom (if (= :loop op) |
| 83 | + (assoc-in @*clears* [:loop-closed-overs loop-id] closed-overs) |
| 84 | + (update-in @*clears* [:closed-overs] merge closed-overs)))] ;; clear locals in the body |
| 85 | + [(update-children ast -clear-locals rseqv) (:locals @*clears*)]) ;; and save encountered locals |
| 86 | + [ks vs] (reduce-kv (fn [[keys vals] k v] |
| 87 | + [(conj keys k) (conj vals v)]) |
| 88 | + [[] []] closed-overs) |
| 89 | + closed-overs (zipmap ks (mapv maybe-clear-local vs))] ;; clear outer closed-overs at the point of the closure creation |
| 90 | + (swap! *clears* #(update-in % [:locals] into body-locals)) ;; merge the locals so that we know not to clear them "before" |
| 91 | + (if (#{:fn :reify} op) |
| 92 | + (assoc ast :closed-overs closed-overs) |
| 93 | + ast)) |
| 94 | + (update-children ast -clear-locals rseqv))) |
| 95 | + |
| 96 | +(defmethod -clear-locals :if |
| 97 | + [{:keys [test then else] :as ast}] |
| 98 | + (let [[then then-clears] (binding [*clears* (atom @*clears*)] ;; push a new locals frame for every path so that |
| 99 | + [(-clear-locals then) @*clears*]) ;; we can clear the same local in different branches |
| 100 | + [else else-clears] (binding [*clears* (atom @*clears*)] ;; this is safe to do since the different paths will |
| 101 | + [(-clear-locals else) @*clears*]) ;; never interfere |
| 102 | + locals (into (:locals then-clears) ;; merge all the locals encountered in the branch paths |
| 103 | + (:locals else-clears))] ;; so that if we encounter them "before" in the body we know |
| 104 | + (swap! *clears* #(update-in % [:locals] into locals)) ;; that we cannot clear them since they are needed later |
| 105 | + (let [test (-clear-locals test)] |
| 106 | + (assoc ast |
| 107 | + :test test |
| 108 | + :then then |
| 109 | + :else else)))) |
| 110 | + |
| 111 | +(defmethod -clear-locals :case |
| 112 | + [{:keys [test default thens] :as ast}] |
| 113 | + (let [[thens thens-locals] |
| 114 | + (reduce (fn [[thens locals] then] |
| 115 | + (let [[t l] (binding [*clears* (atom @*clears*)] |
| 116 | + [(-clear-locals then) (:locals @*clears*)])] |
| 117 | + [(conj thens t) (into locals l)])) |
| 118 | + [[] #{}] thens) |
| 119 | + [default {:keys [locals]}] (binding [*clears* (atom @*clears*)] |
| 120 | + [(-clear-locals default) @*clears*])] |
| 121 | + (swap! *clears* #(update-in % [:locals] into (into thens-locals locals))) |
| 122 | + (assoc ast |
| 123 | + :test test |
| 124 | + :thens thens |
| 125 | + :default default))) |
| 126 | + |
| 127 | +(defmethod -clear-locals :local |
| 128 | + [ast] |
| 129 | + (let [ast (maybe-clear-local ast)] |
| 130 | + (swap! *clears* #(update-in % [:locals] conj (:name ast))) ;; register that the local has been used and potentially cleared |
| 131 | + ast)) |
| 132 | + |
| 133 | +(defmethod -clear-locals :binding |
| 134 | + [ast] |
| 135 | + (let [{:keys [init to-clear?] :as ast} (-> ast (update-children -clear-locals rseqv) |
| 136 | + maybe-clear-local)] |
| 137 | + (if (and init to-clear?) |
| 138 | + (update-in ast [:init :env] ctx :statement) |
| 139 | + ast))) |
| 140 | + |
| 141 | +(defn clear-locals |
| 142 | + "Attached :to-clear? true to all the nodes that the compiler |
| 143 | + can clear, those nodes can be: |
| 144 | + * :local nodes |
| 145 | + * :binding nodes |
| 146 | + * :invoke/protocol-invoke/prim-invoke/static-call/instance-call nodes |
| 147 | + in return position, meaning that the \"this\" local is eligible for |
| 148 | + clearing" |
| 149 | + {:pass-info {:walk :none :depends #{#'collect-closed-overs #'annotate-branch #'annotate-loops}}} |
| 150 | + [ast] |
| 151 | + (if (:disable-locals-clearing *compiler-options*) |
| 152 | + ast |
| 153 | + (binding [*clears* (atom {:closed-overs {} |
| 154 | + :loop-closed-overs {} |
| 155 | + :locals #{}})] |
| 156 | + (-clear-locals ast)))) |
0 commit comments