blob: d3f6a29c194b51f390594828e77ee4080b48db4c (
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
|
(.module:
[library
[lux (#- Type if let case)
[abstract
["." monad (#+ do)]]
[control
["." function]
["ex" exception (#+ exception:)]]
[data
[collection
["." list ("#@." fold)]]]
[math
[number
["n" nat]]]
[target
[jvm
["." type (#+ Type)
["." category (#+ Void Value Return Primitive Object Class Array Var Parameter Method)]
["." descriptor (#+ Descriptor)]
["." signature (#+ Signature)]]]]
[tool
[compiler
["." phase ("operation@." monad)]
[meta
[archive (#+ Archive)]]
[language
[lux
["." synthesis (#+ Path Synthesis)]]]]]]]
[luxc
[lang
[host
["$" jvm (#+ Label Inst Operation Phase Generator)
["_" inst]]]]]
["." //
["." runtime]
["." structure]])
(def: (pop_altI stack_depth)
(-> Nat Inst)
(.case stack_depth
0 function.identity
1 _.POP
2 _.POP2
_ ... (n.> 2)
(|>> _.POP2
(pop_altI (n.- 2 stack_depth)))))
(def: peekI
Inst
(|>> _.DUP
(_.int +0)
_.AALOAD))
(def: pushI
Inst
(_.INVOKESTATIC //.$Runtime "pm_push" (type.method [(list) (list runtime.$Stack //.$Value) runtime.$Stack (list)])))
(def: popI
(|>> (_.int +1)
_.AALOAD
(_.CHECKCAST runtime.$Stack)))
(def: (leftsI value)
(-> Nat Inst)
(.case value
0 _.ICONST_0
1 _.ICONST_1
2 _.ICONST_2
3 _.ICONST_3
4 _.ICONST_4
5 _.ICONST_5
_ (_.int (.int value))))
(def: projectionJT
(type.method [(list) (list //.$Tuple runtime.$Index) //.$Value (list)]))
(def: (left_projection lefts)
(-> Nat Inst)
(.let [[indexI accessI] (.case lefts
0
[_.ICONST_0
_.AALOAD]
lefts
[(leftsI lefts)
(_.INVOKESTATIC //.$Runtime "tuple_left" ..projectionJT)])]
(|>> (_.CHECKCAST //.$Tuple)
indexI
accessI)))
(def: (right_projection lefts)
(-> Nat Inst)
(|>> (_.CHECKCAST //.$Tuple)
(leftsI lefts)
(_.INVOKESTATIC //.$Runtime "tuple_right" ..projectionJT)))
(def: equalsJT
(type.method [(list) (list //.$Value) type.boolean (list)]))
(def: sideJT
(type.method [(list) (list //.$Variant runtime.$Tag runtime.$Flag) runtime.$Value (list)]))
(def: (path' stack_depth @else @end phase archive path)
(-> Nat Label Label Phase Archive Path (Operation Inst))
(.case path
#synthesis.Pop
(operation@in ..popI)
(#synthesis.Bind register)
(operation@in (|>> peekI
(_.ASTORE register)))
(#synthesis.Bit_Fork when thenP elseP)
(do phase.monad
[thenG (path' stack_depth @else @end phase archive thenP)
elseG (.case elseP
(#.Some elseP)
(path' stack_depth @else @end phase archive elseP)
#.None
(in (_.GOTO @else)))
.let [ifI (.if when _.IFEQ _.IFNE)]]
(in (<| _.with_label (function (_ @else))
(|>> peekI
(_.unwrap type.boolean)
(ifI @else)
thenG
(_.label @else)
elseG))))
(^template [<tag> <unwrap> <dup> <pop> <test> <comparison> <if>]
[(<tag> cons)
(do {@ phase.monad}
[forkG (: (Operation Inst)
(monad.fold @ (function (_ [test thenP] elseG)
(do @
[thenG (path' stack_depth @else @end phase archive thenP)]
(in (<| _.with_label (function (_ @else))
(|>> <dup>
(<test> test)
<comparison>
(<if> @else)
<pop>
thenG
(_.label @else)
elseG)))))
(|>> <pop>
(_.GOTO @else))
(#.Item cons)))]
(in (|>> peekI
<unwrap>
forkG)))])
([#synthesis.I64_Fork (_.unwrap type.long) _.DUP2 _.POP2 (|>> .int _.long) _.LCMP _.IFNE]
[#synthesis.F64_Fork (_.unwrap type.double) _.DUP2 _.POP2 _.double _.DCMPL _.IFNE]
[#synthesis.Text_Fork (|>) _.DUP _.POP _.string
(_.INVOKEVIRTUAL (type.class "java.lang.Object" (list)) "equals" ..equalsJT)
_.IFEQ])
(#synthesis.Then bodyS)
(do phase.monad
[bodyI (phase archive bodyS)]
(in (|>> (pop_altI stack_depth)
bodyI
(_.GOTO @end))))
(^template [<pattern> <right?>]
[(^ (<pattern> lefts))
(operation@in (<| _.with_label (function (_ @success))
_.with_label (function (_ @fail))
(|>> peekI
(_.CHECKCAST //.$Variant)
(structure.tagI lefts <right?>)
(structure.flagI <right?>)
(_.INVOKESTATIC //.$Runtime "pm_variant" ..sideJT)
_.DUP
(_.IFNULL @fail)
(_.GOTO @success)
(_.label @fail)
_.POP
(_.GOTO @else)
(_.label @success)
pushI)))])
([synthesis.side/left false]
[synthesis.side/right true])
... Extra optimization
(^template [<path> <projection>]
[(^ (<path> lefts))
(operation@in (|>> peekI
(<projection> lefts)
pushI))
(^ (synthesis.path/seq
(<path> lefts)
(synthesis.!bind_top register thenP)))
(do phase.monad
[then! (path' stack_depth @else @end phase archive thenP)]
(in (|>> peekI
(<projection> lefts)
(_.ASTORE register)
then!)))])
([synthesis.member/left ..left_projection]
[synthesis.member/right ..right_projection])
(#synthesis.Seq leftP rightP)
(do phase.monad
[leftI (path' stack_depth @else @end phase archive leftP)
rightI (path' stack_depth @else @end phase archive rightP)]
(in (|>> leftI
rightI)))
(#synthesis.Alt leftP rightP)
(do phase.monad
[@alt_else _.make_label
leftI (path' (inc stack_depth) @alt_else @end phase archive leftP)
rightI (path' stack_depth @else @end phase archive rightP)]
(in (|>> _.DUP
leftI
(_.label @alt_else)
_.POP
rightI)))
))
(def: failJT
(type.method [(list) (list) type.void (list)]))
(def: (path @end phase archive path)
(-> Label Phase Archive Path (Operation Inst))
(do phase.monad
[@else _.make_label
pathI (..path' 1 @else @end phase archive path)]
(in (|>> pathI
(_.label @else)
_.POP
(_.INVOKESTATIC //.$Runtime "pm_fail" ..failJT)
_.NULL
(_.GOTO @end)))))
(def: .public (if phase archive [testS thenS elseS])
(Generator [Synthesis Synthesis Synthesis])
(do phase.monad
[testI (phase archive testS)
thenI (phase archive thenS)
elseI (phase archive elseS)]
(in (<| _.with_label (function (_ @else))
_.with_label (function (_ @end))
(|>> testI
(_.unwrap type.boolean)
(_.IFEQ @else)
thenI
(_.GOTO @end)
(_.label @else)
elseI
(_.label @end))))))
(def: .public (let phase archive [inputS register exprS])
(Generator [Synthesis Nat Synthesis])
(do phase.monad
[inputI (phase archive inputS)
exprI (phase archive exprS)]
(in (|>> inputI
(_.ASTORE register)
exprI))))
(def: .public (get phase archive [path recordS])
(Generator [(List synthesis.Member) Synthesis])
(do phase.monad
[recordG (phase archive recordS)]
(in (list@fold (function (_ step so_far)
(.let [next (.case step
(#.Left lefts)
(..left_projection lefts)
(#.Right lefts)
(..right_projection lefts))]
(|>> so_far next)))
recordG
(list.reversed path)))))
(def: .public (case phase archive [valueS path])
(Generator [Synthesis Path])
(do phase.monad
[@end _.make_label
valueI (phase archive valueS)
pathI (..path @end phase archive path)]
(in (|>> _.NULL
valueI
pushI
pathI
(_.label @end)))))
|