blob: f51edc50773f38392458030b86a0e3c620ab9ad0 (
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
;; Copyright (c) Eduardo Julian. All rights reserved.
;; This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
;; If a copy of the MPL was not distributed with this file,
;; You can obtain one at http://mozilla.org/MPL/2.0/.
(ns lux.compiler.lambda
(:require (clojure [string :as string]
[set :as set]
[template :refer [do-template]])
clojure.core.match
clojure.core.match.array
(lux [base :as & :refer [|do return* return fail fail* |case |let]]
[type :as &type]
[lexer :as &lexer]
[parser :as &parser]
[analyser :as &analyser]
[host :as &host]
[optimizer :as &o])
[lux.host.generics :as &host-generics]
[lux.analyser.base :as &a]
(lux.compiler [base :as &&]))
(:import (org.objectweb.asm Opcodes
Label
ClassWriter
MethodVisitor)))
;; [Utils]
(def ^:private field-sig (&host-generics/->type-signature "java.lang.Object"))
(def ^:private lambda-return-sig (&host-generics/->type-signature "java.lang.Object"))
(def ^:private <init>-return "V")
(defn ^:private ^String reset-signature [function-class]
(str "()" (&host-generics/->type-signature function-class)))
(defn ^:private ^MethodVisitor get-num-partials! [^MethodVisitor method-writer]
(doto method-writer
(.visitVarInsn Opcodes/ALOAD 0)
(.visitFieldInsn Opcodes/GETFIELD &&/function-class &&/partials-field "I")))
(defn ^:private ^MethodVisitor inc-int! [^MethodVisitor method-writer by]
(doto method-writer
(.visitLdcInsn (int by))
(.visitInsn Opcodes/IADD)))
(defn ^:private ^MethodVisitor get-field! [^MethodVisitor method-writer class-name field-name]
(doto method-writer
(.visitVarInsn Opcodes/ALOAD 0)
(.visitFieldInsn Opcodes/GETFIELD class-name field-name field-sig)))
(defn ^:private ^MethodVisitor put-field! [^MethodVisitor method-writer class-name field-name field-sig value-thunk]
(doto method-writer
(.visitVarInsn Opcodes/ALOAD 0)
value-thunk
(.visitFieldInsn Opcodes/PUTFIELD class-name field-name field-sig)))
(defn ^:private ^MethodVisitor fill-nulls! [^MethodVisitor method-writer amount]
(doto method-writer
(-> (.visitInsn Opcodes/ACONST_NULL)
(->> (dotimes [_ amount])))))
(defn ^:private ^MethodVisitor consecutive-args [^MethodVisitor method-writer start amount]
(doto method-writer
(-> (.visitVarInsn Opcodes/ALOAD (+ start idx))
(->> (dotimes [idx amount])))))
(defn ^:private ^MethodVisitor consecutive-applys [^MethodVisitor method-writer start amount]
(let [max-args-num (min amount &&/num-apply-variants)]
(doto method-writer
(.visitTypeInsn Opcodes/CHECKCAST &&/function-class)
(consecutive-args start max-args-num)
(.visitMethodInsn Opcodes/INVOKEVIRTUAL &&/function-class &&/apply-method (&&/apply-signature max-args-num))
(-> (consecutive-applys (+ start &&/num-apply-variants) (- amount &&/num-apply-variants))
(->> (when (> amount &&/num-apply-variants)))))))
(defn ^:private lambda-impl-signature [arity]
(str "(" (&/fold str "" (&/|repeat arity field-sig)) ")" lambda-return-sig))
(defn ^:private lambda-<init>-signature [env arity]
(if (> arity 1)
(str "(" (&/fold str "" (&/|repeat (&/|length env) field-sig)) "I" (&/fold str "" (&/|repeat (dec arity) field-sig)) ")"
<init>-return)
(str "(" (&/fold str "" (&/|repeat (&/|length env) field-sig)) ")"
<init>-return)))
(defn ^:private init-function [method-writer arity closure-length]
(if (= 1 arity)
(doto method-writer
(.visitLdcInsn (int 0))
(.visitMethodInsn Opcodes/INVOKESPECIAL &&/function-class "<init>" "(I)V"))
(doto method-writer
(.visitVarInsn Opcodes/ILOAD (inc closure-length))
(.visitMethodInsn Opcodes/INVOKESPECIAL &&/function-class "<init>" "(I)V"))))
(defn ^:private add-lambda-<init> [class class-name arity env]
(let [closure-length (&/|length env)]
(doto (.visitMethod ^ClassWriter class Opcodes/ACC_PUBLIC "<init>" (lambda-<init>-signature env arity) nil nil)
(.visitCode)
;; Do normal object initialization
(.visitVarInsn Opcodes/ALOAD 0)
(init-function arity closure-length)
;; Add all of the closure variables
(-> (put-field! class-name (str &&/closure-prefix ?captured-id) field-sig #(.visitVarInsn % Opcodes/ALOAD (inc ?captured-id)))
(->> (|let [[?name [_ (&o/$captured _ ?captured-id ?source)]] ?name+?captured])
(doseq [?name+?captured (&/->seq env)])))
;; Add all the partial arguments
(-> (put-field! class-name (str &&/partial-prefix idx*) field-sig #(.visitVarInsn % Opcodes/ALOAD partial-register))
(->> (|let [partial-register (+ (inc idx*) (inc closure-length))])
(dotimes [idx* (dec arity)])))
;; Finish
(.visitInsn Opcodes/RETURN)
(.visitMaxs 0 0)
(.visitEnd))))
(let [impl-flags (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL)]
(defn ^:private add-lambda-impl [class class-name compile arity impl-body]
(let [$begin (new Label)]
(&/with-writer (doto (.visitMethod ^ClassWriter class impl-flags "impl" (lambda-impl-signature arity) nil nil)
(.visitCode)
(.visitLabel $begin))
(|do [^MethodVisitor *writer* &/get-writer
ret (compile $begin impl-body)
:let [_ (doto *writer*
(.visitInsn Opcodes/ARETURN)
(.visitMaxs 0 0)
(.visitEnd))]]
(return ret))))))
(defn ^:private instance-closure [compile lambda-class arity closed-over]
(|do [^MethodVisitor *writer* &/get-writer
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/NEW lambda-class)
(.visitInsn Opcodes/DUP))]
_ (&/map% (fn [?name+?captured]
(|case ?name+?captured
[?name [_ (&a/$captured _ _ ?source)]]
(compile nil ?source)))
closed-over)
:let [_ (when (> arity 1)
(doto *writer*
(.visitLdcInsn (int 0))
(fill-nulls! (dec arity))))]
:let [_ (.visitMethodInsn *writer* Opcodes/INVOKESPECIAL lambda-class "<init>" (lambda-<init>-signature closed-over arity))]]
(return nil)))
(defn ^:private add-lambda-reset [class-writer class-name arity env]
(if (> arity 1)
(doto (.visitMethod ^ClassWriter class-writer Opcodes/ACC_PUBLIC "reset" (reset-signature class-name) nil nil)
(.visitCode)
(.visitTypeInsn Opcodes/NEW class-name)
(.visitInsn Opcodes/DUP)
(-> (get-field! class-name (str &&/closure-prefix cidx))
(->> (dotimes [cidx (&/|length env)])))
(.visitLdcInsn (int 0))
(fill-nulls! (dec arity))
(.visitMethodInsn Opcodes/INVOKESPECIAL class-name "<init>" (lambda-<init>-signature env arity))
(.visitInsn Opcodes/ARETURN)
(.visitMaxs 0 0)
(.visitEnd))
(doto (.visitMethod ^ClassWriter class-writer Opcodes/ACC_PUBLIC "reset" (reset-signature class-name) nil nil)
(.visitCode)
(.visitVarInsn Opcodes/ALOAD 0)
(.visitInsn Opcodes/ARETURN)
(.visitMaxs 0 0)
(.visitEnd))))
(defn ^:private add-lambda-apply-n [class-writer +degree+ class-name arity env compile impl-body]
(if (> arity 1)
(let [num-partials (dec arity)
$default (new Label)
$labels* (map (fn [_] (new Label)) (repeat num-partials nil))
$labels (vec (concat $labels* (list $default)))
$end (new Label)
method-writer (.visitMethod ^ClassWriter class-writer Opcodes/ACC_PUBLIC &&/apply-method (&&/apply-signature +degree+) nil nil)
frame-locals (to-array (list class-name "java/lang/Object" "java/lang/Object"))
frame-stack (to-array [Opcodes/INTEGER])
arity-over-extent (- arity +degree+)]
(do (doto method-writer
(.visitCode)
get-num-partials!
(.visitTableSwitchInsn 0 (dec num-partials) $default (into-array Label $labels*))
;; (< stage (- arity +degree+))
(-> (doto (.visitLabel $label)
(.visitTypeInsn Opcodes/NEW class-name)
(.visitInsn Opcodes/DUP)
(-> (get-field! class-name (str &&/closure-prefix cidx))
(->> (dotimes [cidx (&/|length env)])))
get-num-partials!
(inc-int! +degree+)
(-> (get-field! class-name (str &&/partial-prefix idx))
(->> (dotimes [idx stage])))
(consecutive-args 1 +degree+)
(fill-nulls! (- (- num-partials +degree+) stage))
(.visitMethodInsn Opcodes/INVOKESPECIAL class-name "<init>" (lambda-<init>-signature env arity))
(.visitJumpInsn Opcodes/GOTO $end))
(->> (cond (= stage arity-over-extent)
(doto method-writer
(.visitLabel $label)
(.visitVarInsn Opcodes/ALOAD 0)
(-> (.visitMethodInsn Opcodes/INVOKEVIRTUAL class-name "reset" (reset-signature class-name))
(->> (when (not= 0 stage))))
(-> (get-field! class-name (str &&/partial-prefix idx))
(->> (dotimes [idx stage])))
(consecutive-args 1 +degree+)
(.visitMethodInsn Opcodes/INVOKEVIRTUAL class-name "impl" (lambda-impl-signature arity))
(.visitJumpInsn Opcodes/GOTO $end))
(> stage arity-over-extent)
(let [args-to-completion (- arity stage)
args-left (- +degree+ args-to-completion)]
(doto method-writer
(.visitLabel $label)
(.visitVarInsn Opcodes/ALOAD 0)
(.visitMethodInsn Opcodes/INVOKEVIRTUAL class-name "reset" (reset-signature class-name))
(-> (get-field! class-name (str &&/partial-prefix idx))
(->> (dotimes [idx stage])))
(consecutive-args 1 args-to-completion)
(.visitMethodInsn Opcodes/INVOKEVIRTUAL class-name "impl" (lambda-impl-signature arity))
(consecutive-applys (+ 1 args-to-completion) args-left)
(.visitJumpInsn Opcodes/GOTO $end)))
:else)
(doseq [[stage $label] (map vector (range arity) $labels)])))
(.visitLabel $end)
(.visitInsn Opcodes/ARETURN)
(.visitMaxs 0 0)
(.visitEnd))
(return nil)))
(let [$begin (new Label)]
(&/with-writer (doto (.visitMethod ^ClassWriter class-writer Opcodes/ACC_PUBLIC &&/apply-method (&&/apply-signature 1) nil nil)
(.visitCode)
(.visitLabel $begin))
(|do [^MethodVisitor *writer* &/get-writer
ret (compile $begin impl-body)
:let [_ (doto *writer*
(.visitInsn Opcodes/ARETURN)
(.visitMaxs 0 0)
(.visitEnd))]]
(return ret))))
))
;; [Exports]
(let [lambda-flags (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_SUPER)
datum-flags (+ Opcodes/ACC_PRIVATE Opcodes/ACC_FINAL)]
(defn compile-function [compile ?prev-writer arity ?scope ?env ?body]
(|do [[file-name _ _] &/cursor
:let [name (&host/location (&/|tail ?scope))
class-name (str (&host/->module-class (&/|head ?scope)) "/" name)
[=class save?] (|case ?prev-writer
(&/$Some _writer)
(&/T [_writer false])
(&/$None)
(&/T [(doto (new ClassWriter ClassWriter/COMPUTE_MAXS)
(.visit &host/bytecode-version lambda-flags
class-name nil &&/function-class (into-array String [])))
true]))
_ (doto =class
(-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_STATIC Opcodes/ACC_FINAL) &&/arity-field "I" nil (int arity))
(doto (.visitEnd)))
(-> (doto (.visitField datum-flags captured-name field-sig nil nil)
(.visitEnd))
(->> (let [captured-name (str &&/closure-prefix ?captured-id)])
(|case ?name+?captured
[?name [_ (&o/$captured _ ?captured-id ?source)]])
(doseq [?name+?captured (&/->seq ?env)])))
(-> (.visitField datum-flags (str &&/partial-prefix idx) field-sig nil nil)
(doto (.visitEnd))
(->> (dotimes [idx (dec arity)])))
(-> (.visitSource file-name nil)
(when save?))
(add-lambda-<init> class-name arity ?env)
(add-lambda-reset class-name arity ?env)
)]
_ (if (> arity 1)
(add-lambda-impl =class class-name compile arity ?body)
(return nil))
_ (&/map% #(add-lambda-apply-n =class % class-name arity ?env compile ?body)
(&/|range* 1 (min arity &&/num-apply-variants)))
:let [_ (.visitEnd =class)]
_ (if save?
(&&/save-class! name (.toByteArray =class))
(return nil))]
(if save?
(instance-closure compile class-name arity ?env)
(return (instance-closure compile class-name arity ?env))))))
|