blob: 90f7b6bcf23cd79185ec95e5c4646122a26132f0 (
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
(ns lux.compiler.jvm.proc.common
(: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 |let |case]]
[type :as &type]
[lexer :as &lexer]
[parser :as &parser]
[analyser :as &analyser]
[optimizer :as &o]
[host :as &host])
[lux.type.host :as &host-type]
[lux.host.generics :as &host-generics]
[lux.analyser.base :as &a]
[lux.compiler.jvm.base :as &&])
(:import (org.objectweb.asm Opcodes
Label
ClassWriter
MethodVisitor
AnnotationVisitor)))
;; [Resources]
(do-template [<name> <op>]
(defn <name> [compile ?values special-args]
(|do [:let [(&/$Cons ?input (&/$Cons ?mask (&/$Nil))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?input)
:let [_ (&&/unwrap-long *writer*)]
_ (compile ?mask)
:let [_ (&&/unwrap-long *writer*)]
:let [_ (doto *writer*
(.visitInsn <op>)
&&/wrap-long)]]
(return nil)))
^:private compile-i64-and Opcodes/LAND
^:private compile-i64-or Opcodes/LOR
^:private compile-i64-xor Opcodes/LXOR
)
(do-template [<name> <op>]
(defn <name> [compile ?values special-args]
(|do [:let [(&/$Cons ?input (&/$Cons ?shift (&/$Nil))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?input)
:let [_ (&&/unwrap-long *writer*)]
_ (compile ?shift)
:let [_ (doto *writer*
&&/unwrap-long
(.visitInsn Opcodes/L2I))]
:let [_ (doto *writer*
(.visitInsn <op>)
&&/wrap-long)]]
(return nil)))
^:private compile-i64-left-shift Opcodes/LSHL
^:private compile-i64-arithmetic-right-shift Opcodes/LSHR
^:private compile-i64-logical-right-shift Opcodes/LUSHR
)
(defn ^:private compile-lux-is [compile ?values special-args]
(|do [:let [(&/$Cons ?left (&/$Cons ?right (&/$Nil))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?left)
_ (compile ?right)
:let [$then (new Label)
$end (new Label)
_ (doto *writer*
(.visitJumpInsn Opcodes/IF_ACMPEQ $then)
;; else
(.visitFieldInsn Opcodes/GETSTATIC "java/lang/Boolean" "FALSE" "Ljava/lang/Boolean;")
(.visitJumpInsn Opcodes/GOTO $end)
(.visitLabel $then)
(.visitFieldInsn Opcodes/GETSTATIC "java/lang/Boolean" "TRUE" "Ljava/lang/Boolean;")
(.visitLabel $end))]]
(return nil)))
(defn ^:private compile-lux-try [compile ?values special-args]
(|do [:let [(&/$Cons ?op (&/$Nil)) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?op)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "lux/Function")
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "runTry" "(Llux/Function;)[Ljava/lang/Object;"))]]
(return nil)))
(do-template [<name> <opcode> <unwrap> <wrap>]
(defn <name> [compile ?values special-args]
(|do [:let [(&/$Cons ?x (&/$Cons ?y (&/$Nil))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?x)
:let [_ (doto *writer*
<unwrap>)]
_ (compile ?y)
:let [_ (doto *writer*
<unwrap>)
_ (doto *writer*
(.visitInsn <opcode>)
<wrap>)]]
(return nil)))
^:private compile-i64-add Opcodes/LADD &&/unwrap-long &&/wrap-long
^:private compile-i64-sub Opcodes/LSUB &&/unwrap-long &&/wrap-long
^:private compile-int-mul Opcodes/LMUL &&/unwrap-long &&/wrap-long
^:private compile-int-div Opcodes/LDIV &&/unwrap-long &&/wrap-long
^:private compile-int-rem Opcodes/LREM &&/unwrap-long &&/wrap-long
^:private compile-frac-add Opcodes/DADD &&/unwrap-double &&/wrap-double
^:private compile-frac-sub Opcodes/DSUB &&/unwrap-double &&/wrap-double
^:private compile-frac-mul Opcodes/DMUL &&/unwrap-double &&/wrap-double
^:private compile-frac-div Opcodes/DDIV &&/unwrap-double &&/wrap-double
^:private compile-frac-rem Opcodes/DREM &&/unwrap-double &&/wrap-double
)
(do-template [<name> <cmpcode> <cmp-output> <unwrap>]
(defn <name> [compile ?values special-args]
(|do [:let [(&/$Cons ?x (&/$Cons ?y (&/$Nil))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?x)
:let [_ (doto *writer*
<unwrap>)]
_ (compile ?y)
:let [_ (doto *writer*
<unwrap>)
$then (new Label)
$end (new Label)
_ (doto *writer*
(.visitInsn <cmpcode>)
(.visitLdcInsn (int <cmp-output>))
(.visitJumpInsn Opcodes/IF_ICMPEQ $then)
(.visitFieldInsn Opcodes/GETSTATIC (&host-generics/->bytecode-class-name "java.lang.Boolean") "FALSE" (&host-generics/->type-signature "java.lang.Boolean"))
(.visitJumpInsn Opcodes/GOTO $end)
(.visitLabel $then)
(.visitFieldInsn Opcodes/GETSTATIC (&host-generics/->bytecode-class-name "java.lang.Boolean") "TRUE" (&host-generics/->type-signature "java.lang.Boolean"))
(.visitLabel $end))]]
(return nil)))
^:private compile-i64-eq Opcodes/LCMP 0 &&/unwrap-long
^:private compile-int-lt Opcodes/LCMP -1 &&/unwrap-long
^:private compile-frac-eq Opcodes/DCMPG 0 &&/unwrap-double
^:private compile-frac-lt Opcodes/DCMPG -1 &&/unwrap-double
)
(do-template [<name> <instr>]
(defn <name> [compile ?values special-args]
(|do [:let [(&/$Nil) ?values]
^MethodVisitor *writer* &/get-writer
:let [_ (doto *writer*
(.visitLdcInsn <instr>)
&&/wrap-double)]]
(return nil)))
^:private compile-frac-smallest Double/MIN_VALUE
^:private compile-frac-min (* -1.0 Double/MAX_VALUE)
^:private compile-frac-max Double/MAX_VALUE
)
(defn ^:private compile-frac-encode [compile ?values special-args]
(|do [:let [(&/$Cons ?input (&/$Nil)) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?input)
:let [_ (doto *writer*
&&/unwrap-double
(.visitMethodInsn Opcodes/INVOKESTATIC "java/lang/Double" "toString" "(D)Ljava/lang/String;"))]]
(return nil)))
(defn ^:private compile-frac-decode [compile ?values special-args]
(|do [:let [(&/$Cons ?input (&/$Nil)) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?input)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String")
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "decode_frac" "(Ljava/lang/String;)[Ljava/lang/Object;"))]]
(return nil)))
(defn ^:private compile-int-char [compile ?values special-args]
(|do [:let [(&/$Cons ?x (&/$Nil)) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?x)
:let [_ (doto *writer*
&&/unwrap-long
(.visitInsn Opcodes/L2I)
(.visitInsn Opcodes/I2C)
(.visitMethodInsn Opcodes/INVOKESTATIC "java/lang/String" "valueOf" "(C)Ljava/lang/String;"))]]
(return nil)))
(do-template [<name> <unwrap> <op> <wrap>]
(defn <name> [compile ?values special-args]
(|do [:let [(&/$Cons ?input (&/$Nil)) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?input)
:let [_ (doto *writer*
<unwrap>
(.visitInsn <op>)
<wrap>)]]
(return nil)))
^:private compile-frac-int &&/unwrap-double Opcodes/D2L &&/wrap-long
^:private compile-int-frac &&/unwrap-long Opcodes/L2D &&/wrap-double
)
(defn ^:private compile-text-eq [compile ?values special-args]
(|do [:let [(&/$Cons ?x (&/$Cons ?y (&/$Nil))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?x)
_ (compile ?y)
:let [_ (doto *writer*
(.visitMethodInsn Opcodes/INVOKEVIRTUAL "java/lang/Object" "equals" "(Ljava/lang/Object;)Z")
(&&/wrap-boolean))]]
(return nil)))
(defn ^:private compile-text-lt [compile ?values special-args]
(|do [:let [(&/$Cons ?x (&/$Cons ?y (&/$Nil))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?x)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String"))]
_ (compile ?y)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String"))]
:let [$then (new Label)
$end (new Label)
_ (doto *writer*
(.visitMethodInsn Opcodes/INVOKEVIRTUAL "java/lang/String" "compareTo" "(Ljava/lang/String;)I")
(.visitJumpInsn Opcodes/IFLT $then)
(.visitFieldInsn Opcodes/GETSTATIC (&host-generics/->bytecode-class-name "java.lang.Boolean") "FALSE" (&host-generics/->type-signature "java.lang.Boolean"))
(.visitJumpInsn Opcodes/GOTO $end)
(.visitLabel $then)
(.visitFieldInsn Opcodes/GETSTATIC (&host-generics/->bytecode-class-name "java.lang.Boolean") "TRUE" (&host-generics/->type-signature "java.lang.Boolean"))
(.visitLabel $end))]]
(return nil)))
(defn compile-text-concat [compile ?values special-args]
(|do [:let [(&/$Cons ?x (&/$Cons ?y (&/$Nil))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?x)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String"))]
_ (compile ?y)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String"))]
:let [_ (doto *writer*
(.visitMethodInsn Opcodes/INVOKEVIRTUAL "java/lang/String" "concat" "(Ljava/lang/String;)Ljava/lang/String;"))]]
(return nil)))
(defn compile-text-clip [compile ?values special-args]
(|do [:let [(&/$Cons ?text (&/$Cons ?from (&/$Cons ?to (&/$Nil)))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?text)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String"))]
_ (compile ?from)
:let [_ (doto *writer*
&&/unwrap-long
(.visitInsn Opcodes/L2I))]
_ (compile ?to)
:let [_ (doto *writer*
&&/unwrap-long
(.visitInsn Opcodes/L2I))]
:let [_ (doto *writer*
(.visitMethodInsn Opcodes/INVOKEVIRTUAL "java/lang/String" "substring" "(II)Ljava/lang/String;"))]]
(return nil)))
(defn ^:private compile-text-index [compile ?values special-args]
(|do [:let [(&/$Cons ?text (&/$Cons ?part (&/$Cons ?start (&/$Nil)))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?text)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String"))]
_ (compile ?part)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String"))]
_ (compile ?start)
:let [_ (doto *writer*
&&/unwrap-long
(.visitInsn Opcodes/L2I))]
:let [_ (doto *writer*
(.visitMethodInsn Opcodes/INVOKEVIRTUAL "java/lang/String" "indexOf" "(Ljava/lang/String;I)I"))]
:let [$not-found (new Label)
$end (new Label)
_ (doto *writer*
(.visitInsn Opcodes/DUP)
(.visitLdcInsn (int -1))
(.visitJumpInsn Opcodes/IF_ICMPEQ $not-found)
(.visitInsn Opcodes/I2L)
&&/wrap-long
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "make_some" "(Ljava/lang/Object;)[Ljava/lang/Object;")
(.visitJumpInsn Opcodes/GOTO $end)
(.visitLabel $not-found)
(.visitInsn Opcodes/POP)
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "make_none" "()[Ljava/lang/Object;")
(.visitLabel $end))]]
(return nil)))
(do-template [<name> <class> <method>]
(defn <name> [compile ?values special-args]
(|do [:let [(&/$Cons ?text (&/$Nil)) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?text)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String")
(.visitMethodInsn Opcodes/INVOKEVIRTUAL <class> <method> "()I")
(.visitInsn Opcodes/I2L)
&&/wrap-long)]]
(return nil)))
^:private compile-text-size "java/lang/String" "length"
)
(defn ^:private compile-text-char [compile ?values special-args]
(|do [:let [(&/$Cons ?text (&/$Cons ?idx (&/$Nil))) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?text)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String"))]
_ (compile ?idx)
:let [_ (doto *writer*
&&/unwrap-long
(.visitInsn Opcodes/L2I)
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "text_char" "(Ljava/lang/String;I)[Ljava/lang/Object;"))]]
(return nil)))
(defn ^:private compile-io-log [compile ?values special-args]
(|do [:let [(&/$Cons ?x (&/$Nil)) ?values]
^MethodVisitor *writer* &/get-writer
:let [_ (doto *writer*
(.visitFieldInsn Opcodes/GETSTATIC "java/lang/System" "out" "Ljava/io/PrintStream;"))]
_ (compile ?x)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String")
(.visitMethodInsn Opcodes/INVOKEVIRTUAL "java/io/PrintStream" "println" "(Ljava/lang/String;)V")
(.visitLdcInsn &/unit-tag))]]
(return nil)))
(defn ^:private compile-io-error [compile ?values special-args]
(|do [:let [(&/$Cons ?message (&/$Nil)) ?values]
^MethodVisitor *writer* &/get-writer
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/NEW "java/lang/Error")
(.visitInsn Opcodes/DUP))]
_ (compile ?message)
:let [_ (doto *writer*
(.visitTypeInsn Opcodes/CHECKCAST "java/lang/String")
(.visitMethodInsn Opcodes/INVOKESPECIAL "java/lang/Error" "<init>" "(Ljava/lang/String;)V")
(.visitInsn Opcodes/ATHROW))]]
(return nil)))
(defn ^:private compile-io-exit [compile ?values special-args]
(|do [:let [(&/$Cons ?code (&/$Nil)) ?values]
^MethodVisitor *writer* &/get-writer
_ (compile ?code)
:let [_ (doto *writer*
&&/unwrap-long
(.visitInsn Opcodes/L2I)
(.visitMethodInsn Opcodes/INVOKESTATIC "java/lang/System" "exit" "(I)V")
(.visitInsn Opcodes/ACONST_NULL))]]
(return nil)))
(defn ^:private compile-io-current-time [compile ?values special-args]
(|do [:let [(&/$Nil) ?values]
^MethodVisitor *writer* &/get-writer
:let [_ (doto *writer*
(.visitMethodInsn Opcodes/INVOKESTATIC "java/lang/System" "currentTimeMillis" "()J")
&&/wrap-long)]]
(return nil)))
(defn compile-proc [compile category proc ?values special-args]
(case category
"lux"
(case proc
"is" (compile-lux-is compile ?values special-args)
"try" (compile-lux-try compile ?values special-args))
"io"
(case proc
"log" (compile-io-log compile ?values special-args)
"error" (compile-io-error compile ?values special-args)
"exit" (compile-io-exit compile ?values special-args)
"current-time" (compile-io-current-time compile ?values special-args)
)
"text"
(case proc
"=" (compile-text-eq compile ?values special-args)
"<" (compile-text-lt compile ?values special-args)
"concat" (compile-text-concat compile ?values special-args)
"clip" (compile-text-clip compile ?values special-args)
"index" (compile-text-index compile ?values special-args)
"size" (compile-text-size compile ?values special-args)
"char" (compile-text-char compile ?values special-args)
)
"i64"
(case proc
"and" (compile-i64-and compile ?values special-args)
"or" (compile-i64-or compile ?values special-args)
"xor" (compile-i64-xor compile ?values special-args)
"left-shift" (compile-i64-left-shift compile ?values special-args)
"arithmetic-right-shift" (compile-i64-arithmetic-right-shift compile ?values special-args)
"logical-right-shift" (compile-i64-logical-right-shift compile ?values special-args)
"=" (compile-i64-eq compile ?values special-args)
"+" (compile-i64-add compile ?values special-args)
"-" (compile-i64-sub compile ?values special-args))
"int"
(case proc
"*" (compile-int-mul compile ?values special-args)
"/" (compile-int-div compile ?values special-args)
"%" (compile-int-rem compile ?values special-args)
"<" (compile-int-lt compile ?values special-args)
"frac" (compile-int-frac compile ?values special-args)
"char" (compile-int-char compile ?values special-args)
)
"frac"
(case proc
"+" (compile-frac-add compile ?values special-args)
"-" (compile-frac-sub compile ?values special-args)
"*" (compile-frac-mul compile ?values special-args)
"/" (compile-frac-div compile ?values special-args)
"%" (compile-frac-rem compile ?values special-args)
"=" (compile-frac-eq compile ?values special-args)
"<" (compile-frac-lt compile ?values special-args)
"smallest" (compile-frac-smallest compile ?values special-args)
"max" (compile-frac-max compile ?values special-args)
"min" (compile-frac-min compile ?values special-args)
"int" (compile-frac-int compile ?values special-args)
"encode" (compile-frac-encode compile ?values special-args)
"decode" (compile-frac-decode compile ?values special-args)
)
;; else
(&/fail-with-loc (str "[Compiler Error] Unknown procedure: " [category proc]))))
|