aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/generation/jvm/case.lux
blob: 7f33f383bf34bec1bddac858297fa08109063486 (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
(.module:
  [lux (#- Type if let case int)
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." function]]
   [data
    [number
     ["." i32]
     ["n" nat]]]
   [target
    [jvm
     ["_" bytecode (#+ Label Bytecode) ("#@." monad)]
     ["." type (#+ Type)
      [category (#+ Method)]]]]]
  ["." // #_
   ["#." type]
   ["#." runtime (#+ Operation Phase)]
   ["#." value]
   [////
    [reference (#+ Register)]
    ["." synthesis (#+ Path Synthesis)]
    ["." generation]
    ["." phase ("operation@." monad)]]])

(def: equals-name
  "equals")

(def: equals-type
  (type.method [(list //type.value) type.boolean (list)]))

(def: (pop-alt stack-depth)
  (-> Nat (Bytecode Any))
  (.case stack-depth
    0 (_@wrap [])
    1 _.pop
    2 _.pop2
    _ ## (n.> 2)
    ($_ _.compose
        _.pop2
        (pop-alt (n.- 2 stack-depth)))))

(def: int
  (-> (I64 Any) (Bytecode Any))
  (|>> .i64 i32.i32 _.int))

(def: long
  (-> (I64 Any) (Bytecode Any))
  (|>> .int _.long))

(def: double
  (-> Frac (Bytecode Any))
  (|>> _.double))

(def: peek
  (Bytecode Any)
  ($_ _.compose
      _.dup
      (//runtime.get //runtime.stack-head)))

(def: pop
  (Bytecode Any)
  ($_ _.compose
      (//runtime.get //runtime.stack-tail)
      (_.checkcast //type.stack)))

(def: (path' phase stack-depth @else @end path)
  (-> Phase Nat Label Label Path (Operation (Bytecode Any)))
  (.case path
    #synthesis.Pop
    (operation@wrap ..pop)
    
    (#synthesis.Bind register)
    (operation@wrap ($_ _.compose
                        ..peek
                        (_.astore register)))

    (^ (synthesis.path/bit value))
    (operation@wrap (.let [jump (.if value _.ifeq _.ifne)]
                      ($_ _.compose
                          ..peek
                          (//value.unwrap type.boolean)
                          (jump @else))))
    
    (^ (synthesis.path/i64 value))
    (operation@wrap ($_ _.compose
                        ..peek
                        (//value.unwrap type.long)
                        (..long value)
                        _.lcmp
                        (_.ifne @else)))
    
    (^ (synthesis.path/f64 value))
    (operation@wrap ($_ _.compose
                        ..peek
                        (//value.unwrap type.double)
                        (..double value)
                        _.dcmpl
                        (_.ifne @else)))
    
    (^ (synthesis.path/text value))
    (operation@wrap ($_ _.compose
                        ..peek
                        (_.string value)
                        (_.invokevirtual //type.text ..equals-name ..equals-type)
                        (_.ifeq @else)))
    
    (#synthesis.Then bodyS)
    (do phase.monad
      [bodyG (phase bodyS)]
      (wrap ($_ _.compose
                (..pop-alt stack-depth)
                bodyG
                (_.goto @end))))
    
    (^template [<pattern> <flag> <prepare>]
      (^ (<pattern> idx))
      (operation@wrap
       (do _.monad
         [@success _.new-label
          @fail _.new-label]
         ($_ _.compose
             ..peek
             (_.checkcast //type.variant)
             (..int (<prepare> idx))
             <flag>
             //runtime.case
             _.dup
             (_.ifnull @fail)
             (_.goto @success)
             (_.set-label @fail)
             _.pop
             (_.goto @else)
             (_.set-label @success)
             //runtime.push))))
    ([synthesis.side/left  //runtime.left-flag function.identity]
     [synthesis.side/right //runtime.right-flag .inc])

    (^ (synthesis.member/left lefts))
    (operation@wrap (.let [optimized-projection (.case lefts
                                                  0
                                                  _.aaload
                                                  
                                                  lefts
                                                  //runtime.left-projection)]
                      ($_ _.compose
                          ..peek
                          (_.checkcast //type.tuple)
                          (..int lefts)
                          optimized-projection
                          //runtime.push)))

    (^ (synthesis.member/right lefts))
    (operation@wrap ($_ _.compose
                        ..peek
                        (_.checkcast //type.tuple)
                        (..int lefts)
                        //runtime.right-projection
                        //runtime.push))
    
    ## Extra optimization
    (^ (synthesis.path/seq
        (synthesis.member/left 0)
        (synthesis.!bind-top register thenP)))
    (do phase.monad
      [thenG (path' phase stack-depth @else @end thenP)]
      (wrap ($_ _.compose
                ..peek
                (_.checkcast //type.tuple)
                _.iconst-0
                _.aaload
                (_.astore register)
                thenG)))

    ## Extra optimization
    (^template [<pm> <projection>]
      (^ (synthesis.path/seq
          (<pm> lefts)
          (synthesis.!bind-top register thenP)))
      (do phase.monad
        [then! (path' phase stack-depth @else @end thenP)]
        (wrap ($_ _.compose
                  ..peek
                  (_.checkcast //type.tuple)
                  (..int lefts)
                  <projection>
                  (_.astore register)
                  then!))))
    ([synthesis.member/left //runtime.left-projection]
     [synthesis.member/right //runtime.right-projection])

    (#synthesis.Alt leftP rightP)
    (do phase.monad
      [@alt-else //runtime.forge-label
       left! (path' phase (inc stack-depth) @alt-else @end leftP)
       right! (path' phase stack-depth @else @end rightP)]
      (wrap ($_ _.compose
                _.dup
                left!
                (_.set-label @alt-else)
                _.pop
                right!)))
    
    (#synthesis.Seq leftP rightP)
    (do phase.monad
      [left! (path' phase stack-depth @else @end leftP)
       right! (path' phase stack-depth @else @end rightP)]
      (wrap ($_ _.compose
                left!
                right!)))
    ))

(def: (path phase path @end)
  (-> Phase Path Label (Operation (Bytecode Any)))
  (do phase.monad
    [@else //runtime.forge-label
     pathG (..path' phase 1 @else @end path)]
    (wrap ($_ _.compose
              pathG
              (_.set-label @else)
              _.pop
              //runtime.pm-failure
              _.aconst-null
              (_.goto @end)))))

(def: #export (if phase conditionS thenS elseS)
  (-> Phase Synthesis Synthesis Synthesis (Operation (Bytecode Any)))
  (do phase.monad
    [conditionG (phase conditionS)
     thenG (phase thenS)
     elseG (phase elseS)]
    (wrap (do _.monad
            [@else _.new-label
             @end _.new-label]
            ($_ _.compose
                conditionG
                (//value.unwrap type.boolean)
                (_.ifeq @else)
                thenG
                (_.goto @end)
                (_.set-label @else)
                elseG
                (_.set-label @end))))))

(def: #export (let phase inputS register bodyS)
  (-> Phase Synthesis Register Synthesis (Operation (Bytecode Any)))
  (do phase.monad
    [inputG (phase inputS)
     bodyG (phase bodyS)]
    (wrap ($_ _.compose
              inputG
              (_.astore register)
              bodyG))))

(def: #export (case phase valueS path)
  (-> Phase Synthesis Path (Operation (Bytecode Any)))
  (do phase.monad
    [@end //runtime.forge-label
     valueG (phase valueS)
     pathG (..path phase path @end)]
    (wrap ($_ _.compose
              _.aconst-null
              valueG
              //runtime.push
              pathG
              (_.set-label @end)))))