aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/jvm/case.lux
blob: 43d11c71ed4bf3a461768d916633069c00f75790 (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
(.module:
  [lux (#- if let case)
   [abstract
    [monad (#+ do)]]
   [control
    ["." function]
    ["ex" exception (#+ exception:)]]
   [data
    [text
     format]]
   [tool
    [compiler
     ["." synthesis (#+ Path Synthesis)]
     ["." phase ("operation/." monad)]]]]
  [luxc
   [lang
    [host
     ["$" jvm (#+ Label Inst Operation Phase)
      ["$t" type]
      ["_" inst]]]]]
  ["." // (#+ $Object)
   ["." runtime]])

(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
       (_.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' phase stack-depth @else @end path)
  (-> Phase 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
      [bodyI (phase bodyS)]
      (wrap (|>> (pop-altI stack-depth)
                 bodyI
                 (_.GOTO @end))))
    
    
    (^template [<pattern> <flag> <prepare>]
      (^ (<pattern> idx))
      (operation/wrap (<| _.with-label (function (_ @success))
                          _.with-label (function (_ @fail))
                          (|>> peekI
                               (_.CHECKCAST ($t.descriptor runtime.$Variant))
                               (_.int (.int (<prepare> 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        function.identity]
     [synthesis.side/right (_.string "") .inc])

    (^ (synthesis.member/left lefts))
    (operation/wrap (.let [accessI (.case lefts
                                     0
                                     _.AALOAD
                                     
                                     lefts
                                     (_.INVOKESTATIC //.runtime-class
                                                     "tuple_left"
                                                     ($t.method (list runtime.$Tuple $t.int)
                                                                (#.Some $Object)
                                                                (list))
                                                     #0))]
                      (|>> peekI
                           (_.CHECKCAST ($t.descriptor runtime.$Tuple))
                           (_.int (.int lefts))
                           accessI
                           pushI)))

    (^ (synthesis.member/right lefts))
    (operation/wrap (|>> peekI
                         (_.CHECKCAST ($t.descriptor runtime.$Tuple))
                         (_.int (.int lefts))
                         (_.INVOKESTATIC //.runtime-class
                                         "tuple_right"
                                         ($t.method (list runtime.$Tuple $t.int)
                                                    (#.Some $Object)
                                                    (list))
                                         #0)
                         pushI))

    (#synthesis.Alt leftP rightP)
    (do phase.monad
      [@alt-else _.make-label
       leftI (path' phase (inc stack-depth) @alt-else @end leftP)
       rightI (path' phase stack-depth @else @end rightP)]
      (wrap (|>> _.DUP
                 leftI
                 (_.label @alt-else)
                 _.POP
                 rightI)))
    
    (#synthesis.Seq leftP rightP)
    (do phase.monad
      [leftI (path' phase stack-depth @else @end leftP)
       rightI (path' phase stack-depth @else @end rightP)]
      (wrap (|>> leftI
                 rightI)))
    ))

(def: (path phase path @end)
  (-> Phase Path Label (Operation Inst))
  (do phase.monad
    [@else _.make-label
     pathI (..path' phase 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 phase testS thenS elseS)
  (-> Phase Synthesis Synthesis Synthesis (Operation Inst))
  (do phase.monad
    [testI (phase testS)
     thenI (phase thenS)
     elseI (phase 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 phase inputS register exprS)
  (-> Phase Synthesis Nat Synthesis (Operation Inst))
  (do phase.monad
    [inputI (phase inputS)
     exprI (phase exprS)]
    (wrap (|>> inputI
               (_.ASTORE register)
               exprI))))

(def: #export (case phase valueS path)
  (-> Phase Synthesis Path (Operation Inst))
  (do phase.monad
    [@end _.make-label
     valueI (phase valueS)
     pathI (..path phase path @end)]
    (wrap (|>> _.NULL
               valueI
               pushI
               pathI
               (_.label @end)))))