blob: 32ca72ae3adcd16264aae37e78f92cb7eaf213b9 (
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
|
(ns lux.compiler.jvm.case
(:require (clojure [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]
[host :as &host]
[optimizer :as &o])
[lux.analyser.case :as &a-case]
[lux.compiler.jvm.base :as &&])
(:import (org.objectweb.asm Opcodes
Label
ClassWriter
MethodVisitor)))
;; [Utils]
(defn ^:private pop-alt-stack [^MethodVisitor writer stack-depth]
(cond (= 0 stack-depth)
writer
(= 1 stack-depth)
(doto writer
(.visitInsn Opcodes/POP))
(= 2 stack-depth)
(doto writer
(.visitInsn Opcodes/POP2))
:else ;; > 2
(doto writer
(.visitInsn Opcodes/POP2)
(pop-alt-stack (- stack-depth 2)))))
(defn ^:private stack-peek [^MethodVisitor writer]
(doto writer
(.visitInsn Opcodes/DUP)
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "pm_stack_peek" "([Ljava/lang/Object;)Ljava/lang/Object;")))
(defn ^:private compile-pattern* [^MethodVisitor writer bodies stack-depth $else pm]
"(-> MethodVisitor Case-Pattern (List Label) Int Label MethodVisitor)"
(|case pm
(&o/$ExecPM _body-idx)
(|case (&/|at _body-idx bodies)
(&/$Some $body)
(doto writer
(pop-alt-stack stack-depth)
(.visitJumpInsn Opcodes/GOTO $body))
(&/$None)
(assert false))
(&o/$PopPM)
(doto writer
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "pm_stack_pop" "([Ljava/lang/Object;)[Ljava/lang/Object;"))
(&o/$BindPM _var-id)
(doto writer
stack-peek
(.visitVarInsn Opcodes/ASTORE _var-id)
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "pm_stack_pop" "([Ljava/lang/Object;)[Ljava/lang/Object;"))
(&o/$BitPM _value)
(doto writer
stack-peek
&&/unwrap-boolean
(.visitJumpInsn (if _value Opcodes/IFEQ Opcodes/IFNE) $else))
(&o/$NatPM _value)
(doto writer
stack-peek
&&/unwrap-long
(.visitLdcInsn (long _value))
(.visitInsn Opcodes/LCMP)
(.visitJumpInsn Opcodes/IFNE $else))
(&o/$IntPM _value)
(doto writer
stack-peek
&&/unwrap-long
(.visitLdcInsn (long _value))
(.visitInsn Opcodes/LCMP)
(.visitJumpInsn Opcodes/IFNE $else))
(&o/$RevPM _value)
(doto writer
stack-peek
&&/unwrap-long
(.visitLdcInsn (long _value))
(.visitInsn Opcodes/LCMP)
(.visitJumpInsn Opcodes/IFNE $else))
(&o/$FracPM _value)
(doto writer
stack-peek
&&/unwrap-double
(.visitLdcInsn (double _value))
(.visitInsn Opcodes/DCMPL)
(.visitJumpInsn Opcodes/IFNE $else))
(&o/$TextPM _value)
(doto writer
stack-peek
(.visitLdcInsn _value)
(.visitMethodInsn Opcodes/INVOKEVIRTUAL "java/lang/Object" "equals" "(Ljava/lang/Object;)Z")
(.visitJumpInsn Opcodes/IFEQ $else))
(&o/$TuplePM _idx+)
(|let [[_idx is-tail?] (|case _idx+
(&/$Left _idx)
(&/T [_idx false])
(&/$Right _idx)
(&/T [_idx true]))]
(if (= 0 _idx)
(doto writer
stack-peek
(.visitTypeInsn Opcodes/CHECKCAST "[Ljava/lang/Object;")
(.visitLdcInsn (int 0))
(.visitInsn Opcodes/AALOAD)
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "pm_stack_push" "([Ljava/lang/Object;Ljava/lang/Object;)[Ljava/lang/Object;"))
(doto writer
stack-peek
(.visitTypeInsn Opcodes/CHECKCAST "[Ljava/lang/Object;")
(.visitLdcInsn (int _idx))
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" (if is-tail? "product_getRight" "product_getLeft") "([Ljava/lang/Object;I)Ljava/lang/Object;")
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "pm_stack_push" "([Ljava/lang/Object;Ljava/lang/Object;)[Ljava/lang/Object;")
)))
(&o/$VariantPM _idx+)
(|let [$success (new Label)
$fail (new Label)
[_idx is-last] (|case _idx+
(&/$Left _idx)
(&/T [_idx false])
(&/$Right _idx)
(&/T [_idx true]))
_ (doto writer
stack-peek
(.visitTypeInsn Opcodes/CHECKCAST "[Ljava/lang/Object;")
(.visitLdcInsn (int _idx)))
_ (if is-last
(.visitLdcInsn writer "")
(.visitInsn writer Opcodes/ACONST_NULL))]
(doto writer
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "sum_get" "([Ljava/lang/Object;ILjava/lang/Object;)Ljava/lang/Object;")
(.visitInsn Opcodes/DUP)
(.visitJumpInsn Opcodes/IFNULL $fail)
(.visitJumpInsn Opcodes/GOTO $success)
(.visitLabel $fail)
(.visitInsn Opcodes/POP)
(.visitJumpInsn Opcodes/GOTO $else)
(.visitLabel $success)
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "pm_stack_push" "([Ljava/lang/Object;Ljava/lang/Object;)[Ljava/lang/Object;")))
(&o/$SeqPM _left-pm _right-pm)
(doto writer
(compile-pattern* bodies stack-depth $else _left-pm)
(compile-pattern* bodies stack-depth $else _right-pm))
(&o/$AltPM _left-pm _right-pm)
(|let [$alt-else (new Label)]
(doto writer
(.visitInsn Opcodes/DUP)
(compile-pattern* bodies (inc stack-depth) $alt-else _left-pm)
(.visitLabel $alt-else)
(.visitInsn Opcodes/POP)
(compile-pattern* bodies stack-depth $else _right-pm)))
))
(defn ^:private compile-pattern [^MethodVisitor writer bodies pm $end]
(|let [$else (new Label)]
(doto writer
(compile-pattern* bodies 1 $else pm)
(.visitLabel $else)
(.visitInsn Opcodes/POP)
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "pm_fail" "()V")
(.visitInsn Opcodes/ACONST_NULL)
(.visitJumpInsn Opcodes/GOTO $end))))
(defn ^:private compile-bodies [^MethodVisitor writer compile bodies-labels ?bodies $end]
(&/map% (fn [label+body]
(|let [[_label _body] label+body]
(|do [:let [_ (.visitLabel writer _label)]
_ (compile _body)
:let [_ (.visitJumpInsn writer Opcodes/GOTO $end)]]
(return nil))))
(&/zip2 bodies-labels ?bodies)))
;; [Resources]
(defn compile-case [compile ?value ?pm ?bodies]
(|do [^MethodVisitor *writer* &/get-writer
:let [$end (new Label)
bodies-labels (&/|map (fn [_] (new Label)) ?bodies)]
:let [_ (doto *writer*
(.visitInsn Opcodes/ACONST_NULL))]
_ (compile ?value)
:let [_ (doto *writer*
(.visitMethodInsn Opcodes/INVOKESTATIC "lux/LuxRT" "pm_stack_push" "([Ljava/lang/Object;Ljava/lang/Object;)[Ljava/lang/Object;"))
_ (compile-pattern *writer* bodies-labels ?pm $end)]
_ (compile-bodies *writer* compile bodies-labels ?bodies $end)
:let [_ (.visitLabel *writer* $end)]]
(return nil)))
|