blob: bd09b603fc4020ee8ac81e9f7f5466630a50f307 (
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
|
(ns lux.compiler.lux
(:require (clojure [string :as string]
[set :as set]
[template :refer [do-template]])
[clojure.core.match :refer [match]]
(lux [base :as & :refer [exec return* return fail fail*
repeat-m exhaust-m try-m try-all-m map-m reduce-m
apply-m
normalize-ident]]
[type :as &type]
[lexer :as &lexer]
[parser :as &parser]
[analyser :as &analyser]
[host :as &host])
[lux.analyser.base :as &a]
(lux.compiler [base :as &&]
[lambda :as &&lambda])
:reload)
(:import (org.objectweb.asm Opcodes
Label
ClassWriter
MethodVisitor)))
;; [Utils]
(defn ^:private compile-field [compile ?name body]
(exec [*writer* &/get-writer
module-name &/get-module-name
:let [outer-class (&host/->class module-name)
datum-sig (&host/->type-signature "java.lang.Object")
current-class (&host/location (list outer-class ?name))
_ (.visitInnerClass *writer* current-class outer-class nil (+ Opcodes/ACC_STATIC Opcodes/ACC_SYNTHETIC))
=class (doto (new ClassWriter ClassWriter/COMPUTE_MAXS)
(.visit Opcodes/V1_5 (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_SUPER)
current-class nil "java/lang/Object" (into-array [(&host/->class &host/function-class)]))
(-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC) "_datum" datum-sig nil nil)
(doto (.visitEnd))))]
_ (&/with-writer (.visitMethod =class Opcodes/ACC_PUBLIC "<clinit>" "()V" nil nil)
(exec [*writer* &/get-writer
:let [_ (.visitCode *writer*)]
_ (compile body)
:let [_ (doto *writer*
(.visitFieldInsn Opcodes/PUTSTATIC current-class "_datum" datum-sig)
(.visitInsn Opcodes/RETURN)
(.visitMaxs 0 0)
(.visitEnd))]]
(return nil)))
:let [_ (.visitEnd *writer*)]
_ (&&/save-class! current-class (.toByteArray =class))]
(return nil)))
;; [Resources]
(let [+class+ (&host/->class "java.lang.Boolean")
+sig+ (&host/->type-signature "java.lang.Boolean")]
(defn compile-bool [compile *type* ?value]
(exec [*writer* &/get-writer
:let [_ (.visitFieldInsn *writer* Opcodes/GETSTATIC (&host/->class "java.lang.Boolean") (if ?value "TRUE" "FALSE") (&host/->type-signature "java.lang.Boolean"))]]
(return nil))))
(do-template [<name> <class> <sig> <caster>]
(let [+class+ (&host/->class <class>)]
(defn <name> [compile *type* value]
(exec [*writer* &/get-writer
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/NEW +class+)
(.visitInsn Opcodes/DUP)
(.visitLdcInsn (<caster> value))
(.visitMethodInsn Opcodes/INVOKESPECIAL +class+ "<init>" <sig>))]]
(return nil))))
compile-int "java.lang.Long" "(J)V" long
compile-real "java.lang.Double" "(D)V" double
compile-char "java.lang.Character" "(C)V" char
)
(defn compile-text [compile *type* ?value]
(exec [*writer* &/get-writer
:let [_ (.visitLdcInsn *writer* ?value)]]
(return nil)))
(defn compile-tuple [compile *type* ?elems]
(exec [*writer* &/get-writer
:let [num-elems (count ?elems)
tuple-class (&host/->class (str &host/tuple-class num-elems))
_ (doto *writer*
(.visitTypeInsn Opcodes/NEW tuple-class)
(.visitInsn Opcodes/DUP)
(.visitMethodInsn Opcodes/INVOKESPECIAL tuple-class "<init>" "()V"))]
_ (map-m (fn [idx]
(exec [:let [_ (.visitInsn *writer* Opcodes/DUP)]
ret (compile (nth ?elems idx))
:let [_ (.visitFieldInsn *writer* Opcodes/PUTFIELD tuple-class (str &&/tuple-field-prefix idx) "Ljava/lang/Object;")]]
(return ret)))
(range num-elems))]
(return nil)))
(defn compile-variant [compile *type* ?tag ?value]
(exec [*writer* &/get-writer
:let [variant-class* (&host/->class &host/variant-class)
_ (doto *writer*
(.visitTypeInsn Opcodes/NEW variant-class*)
(.visitInsn Opcodes/DUP)
(.visitMethodInsn Opcodes/INVOKESPECIAL variant-class* "<init>" "()V")
(.visitInsn Opcodes/DUP)
(.visitLdcInsn ?tag)
(.visitFieldInsn Opcodes/PUTFIELD variant-class* "tag" (&host/->type-signature "java.lang.String"))
(.visitInsn Opcodes/DUP))]
_ (compile ?value)
:let [_ (.visitFieldInsn *writer* Opcodes/PUTFIELD variant-class* "value" (&host/->type-signature "java.lang.Object"))]]
(return nil)))
(defn compile-local [compile *type* ?idx]
(exec [*writer* &/get-writer
:let [_ (.visitVarInsn *writer* Opcodes/ALOAD (int ?idx))]]
(return nil)))
(defn compile-captured [compile *type* ?scope ?captured-id ?source]
(exec [*writer* &/get-writer
:let [_ (doto *writer*
(.visitVarInsn Opcodes/ALOAD 0)
(.visitFieldInsn Opcodes/GETFIELD
(normalize-ident ?scope)
(str &&/closure-prefix ?captured-id)
"Ljava/lang/Object;"))]]
(return nil)))
(defn compile-global [compile *type* ?owner-class ?name]
(exec [*writer* &/get-writer
:let [_ (.visitFieldInsn *writer* Opcodes/GETSTATIC (&host/->class (&host/location (list ?owner-class ?name))) "_datum" "Ljava/lang/Object;")]]
(return nil)))
(defn compile-call [compile *type* ?fn ?args]
(exec [*writer* &/get-writer
_ (compile ?fn)
_ (map-m (fn [arg]
(exec [ret (compile arg)
:let [_ (.visitMethodInsn *writer* Opcodes/INVOKEINTERFACE (&host/->class &host/function-class) "apply" &&/apply-signature)]]
(return ret)))
?args)]
(return nil)))
(defn compile-static-call [compile *type* ?needs-num ?fn ?args]
(assert false (pr-str 'compile-static-call))
(exec [*writer* &/get-writer
:let [_ (match (:form ?fn)
[::&a/global ?owner-class ?fn-name]
(let [arg-sig (&host/->type-signature "java.lang.Object")
call-class (&host/location (list ?owner-class ?fn-name))
provides-num (count ?args)]
(if (>= provides-num ?needs-num)
(let [impl-sig (str "(" (reduce str "" (repeat ?needs-num arg-sig)) ")" arg-sig)]
(doto *writer*
(-> (do (compile arg))
(->> (doseq [arg (take ?needs-num ?args)])))
(.visitMethodInsn Opcodes/INVOKESTATIC call-class "impl" impl-sig)
(-> (doto (do (compile arg))
(.visitMethodInsn Opcodes/INVOKEINTERFACE (&host/->class &host/function-class) "apply" &&/apply-signature))
(->> (doseq [arg (drop ?needs-num ?args)])))))
(let [counter-sig "I"
init-signature (str "(" (apply str counter-sig (repeat (dec ?needs-num) arg-sig)) ")" "V")]
(doto *writer*
(.visitTypeInsn Opcodes/NEW call-class)
(.visitInsn Opcodes/DUP)
(.visitLdcInsn (int provides-num))
(-> (do (compile arg))
(->> (doseq [arg ?args])))
(&&/add-nulls (dec (- ?needs-num provides-num)))
(.visitMethodInsn Opcodes/INVOKESPECIAL call-class "<init>" init-signature)))
))
)]]
(return nil)))
(defn compile-def [compile name value]
(exec [value-type (&a/expr-type value)]
(match value
[::&a/Expression ?form _]
(match ?form
[::&a/lambda ?scope ?captured ?args ?body]
(&&lambda/compile-lambda compile value-type ?scope ?captured ?args ?body true false)
_
(compile-field compile name value))
_
(fail "Can only define expressions."))))
(defn compile-self-call [compile ?scope ?assumed-args]
(prn 'compile-self-call ?scope ?assumed-args)
(exec [*writer* &/get-writer
:let [lambda-class (&host/location ?scope)]
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/NEW lambda-class)
(.visitInsn Opcodes/DUP))]
:let [num-args (if (= '("lux" "fold") ?scope)
3
(count ?assumed-args))
init-signature (str "(" (if (> num-args 1)
(reduce str "I" (repeat (dec num-args) (&host/->type-signature "java.lang.Object"))))
")"
"V")
_ (do (when (> num-args 1)
(.visitInsn *writer* Opcodes/ICONST_0)
(&&/add-nulls *writer* (dec num-args)))
(.visitMethodInsn *writer* Opcodes/INVOKESPECIAL lambda-class "<init>" init-signature))]
_ (map-m (fn [arg]
(exec [ret (compile arg)
:let [_ (.visitMethodInsn *writer* Opcodes/INVOKEINTERFACE (&host/->class &host/function-class) "apply" &&/apply-signature)]]
(return ret)))
?assumed-args)]
(return nil)))
|