blob: e11187787223f17964416668b843e20add70f7c5 (
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
|
(.module:
[lux (#- if let case)
[control
[monad (#+ do)]
["ex" exception (#+ exception:)]]
[data
[text
format]]
[compiler
[default
["." phase ("operation/." Monad<Operation>)
["." synthesis (#+ Path Synthesis)]]]]]
[luxc
[lang
[host
["$" jvm (#+ Label Inst Operation Phase)
["$t" type]
["_" inst]]]]]
["." // (#+ $Object)
["." runtime]])
(def: (pop-altI stack-depth)
(-> Nat Inst)
(.case stack-depth
0 id
1 _.POP
2 _.POP2
_ ## (n/> 2)
(|>> _.POP2
(pop-altI (n/- 2 stack-depth)))))
(def: peekI
Inst
(|>> _.DUP
(_.INVOKESTATIC //.runtime-class
"pm_peek"
($t.method (list runtime.$Stack)
(#.Some $Object)
(list))
#0)))
(def: popI
Inst
(|>> (_.INVOKESTATIC //.runtime-class
"pm_pop"
($t.method (list runtime.$Stack)
(#.Some runtime.$Stack)
(list))
#0)))
(def: pushI
Inst
(|>> (_.INVOKESTATIC //.runtime-class
"pm_push"
($t.method (list runtime.$Stack $Object)
(#.Some runtime.$Stack)
(list))
#0)))
(def: (path' translate stack-depth @else @end path)
(-> (-> Synthesis (Operation Inst))
Nat Label Label Path (Operation Inst))
(.case path
#synthesis.Pop
(operation/wrap popI)
(#synthesis.Bind register)
(operation/wrap (|>> peekI
(_.ASTORE register)))
(^ (synthesis.path/bit value))
(operation/wrap (.let [jumpI (.if value _.IFEQ _.IFNE)]
(|>> peekI
(_.unwrap #$.Boolean)
(jumpI @else))))
(^ (synthesis.path/i64 value))
(operation/wrap (|>> peekI
(_.unwrap #$.Long)
(_.long (.int value))
_.LCMP
(_.IFNE @else)))
(^ (synthesis.path/f64 value))
(operation/wrap (|>> peekI
(_.unwrap #$.Double)
(_.double value)
_.DCMPL
(_.IFNE @else)))
(^ (synthesis.path/text value))
(operation/wrap (|>> peekI
(_.string value)
(_.INVOKEVIRTUAL "java.lang.Object"
"equals"
($t.method (list $Object)
(#.Some $t.boolean)
(list))
#0)
(_.IFEQ @else)))
(#synthesis.Then bodyS)
(do phase.Monad<Operation>
[bodyI (translate bodyS)]
(wrap (|>> (pop-altI stack-depth)
bodyI
(_.GOTO @end))))
(^template [<pattern> <method>]
(^ (<pattern> idx))
(operation/wrap (.case idx
0
(|>> peekI
(_.CHECKCAST ($t.descriptor runtime.$Tuple))
(_.int +0)
_.AALOAD
pushI)
idx
(|>> peekI
(_.CHECKCAST ($t.descriptor runtime.$Tuple))
(_.int (.int idx))
(_.INVOKESTATIC //.runtime-class
<method>
($t.method (list runtime.$Tuple $t.int)
(#.Some $Object)
(list))
#0)
pushI))))
([synthesis.member/left "pm_left"]
[synthesis.member/right "pm_right"])
(^template [<pattern> <flag> <mod>]
(^ (<pattern> idx))
(.let [idx (<mod> idx)]
(operation/wrap (<| _.with-label (function (_ @success))
_.with-label (function (_ @fail))
(|>> peekI
(_.CHECKCAST ($t.descriptor runtime.$Variant))
(_.int (.int idx))
<flag>
(_.INVOKESTATIC //.runtime-class "pm_variant"
($t.method (list runtime.$Variant runtime.$Tag runtime.$Flag)
(#.Some runtime.$Datum)
(list))
#0)
_.DUP
(_.IFNULL @fail)
(_.GOTO @success)
(_.label @fail)
_.POP
(_.GOTO @else)
(_.label @success)
pushI)))))
([synthesis.side/left _.NULL .id]
[synthesis.side/right (_.string "") .inc])
(#synthesis.Alt leftP rightP)
(do phase.Monad<Operation>
[@alt-else _.make-label
leftI (path' translate (inc stack-depth) @alt-else @end leftP)
rightI (path' translate stack-depth @else @end rightP)]
(wrap (|>> _.DUP
leftI
(_.label @alt-else)
_.POP
rightI)))
(#synthesis.Seq leftP rightP)
(do phase.Monad<Operation>
[leftI (path' translate stack-depth @else @end leftP)
rightI (path' translate stack-depth @else @end rightP)]
(wrap (|>> leftI
rightI)))
))
(def: (path translate path @end)
(-> Phase Path Label (Operation Inst))
(do phase.Monad<Operation>
[@else _.make-label
pathI (..path' translate 1 @else @end path)]
(wrap (|>> pathI
(_.label @else)
_.POP
(_.INVOKESTATIC //.runtime-class
"pm_fail"
($t.method (list) #.None (list))
#0)
_.NULL
(_.GOTO @end)))))
(def: #export (if translate testS thenS elseS)
(-> Phase Synthesis Synthesis Synthesis (Operation Inst))
(do phase.Monad<Operation>
[testI (translate testS)
thenI (translate thenS)
elseI (translate elseS)]
(wrap (<| _.with-label (function (_ @else))
_.with-label (function (_ @end))
(|>> testI
(_.unwrap #$.Boolean)
(_.IFEQ @else)
thenI
(_.GOTO @end)
(_.label @else)
elseI
(_.label @end))))))
(def: #export (let translate inputS register exprS)
(-> Phase Synthesis Nat Synthesis (Operation Inst))
(do phase.Monad<Operation>
[inputI (translate inputS)
exprI (translate exprS)]
(wrap (|>> inputI
(_.ASTORE register)
exprI))))
(def: #export (case translate valueS path)
(-> Phase Synthesis Path (Operation Inst))
(do phase.Monad<Operation>
[@end _.make-label
valueI (translate valueS)
pathI (..path translate path @end)]
(wrap (|>> _.NULL
valueI
pushI
pathI
(_.label @end)))))
|