aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/jvm/case.jvm.lux
blob: 2aa0586aba2920271848f37cbea96c5c38f43b82 (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]]
   [language
    ["." compiler ("operation/" Monad<Operation>)
     ["." synthesis (#+ Path Synthesis)]]]]
  [luxc
   [lang
    [host
     ["$" jvm (#+ Label Inst Operation Compiler)
      ["$t" type]
      ["$i" inst]]]]]
  ["." // (#+ $Object)
   [runtime]])

(def: (pop-altI stack-depth)
  (-> Nat Inst)
  (.case stack-depth
    +0 id
    +1 $i.POP
    +2 $i.POP2
    _ ## (n/> +2)
    (|>> $i.POP2
         (pop-altI (n/- +2 stack-depth)))))

(def: peekI
  Inst
  (|>> $i.DUP
       ($i.INVOKESTATIC //.runtime-class
                        "pm_peek"
                        ($t.method (list runtime.$Stack)
                                   (#.Some $Object)
                                   (list))
                        #0)))

(def: popI
  Inst
  (|>> ($i.INVOKESTATIC //.runtime-class
                        "pm_pop"
                        ($t.method (list runtime.$Stack)
                                   (#.Some runtime.$Stack)
                                   (list))
                        #0)))

(def: pushI
  Inst
  (|>> ($i.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
                         ($i.ASTORE register)))

    (^ (synthesis.path/bit value))
    (operation/wrap (.let [jumpI (.if value $i.IFEQ $i.IFNE)]
                      (|>> peekI
                           ($i.unwrap #$.Boolean)
                           (jumpI @else))))
    
    (^ (synthesis.path/i64 value))
    (operation/wrap (|>> peekI
                         ($i.unwrap #$.Long)
                         ($i.long value)
                         $i.LCMP
                         ($i.IFNE @else)))
    
    (^ (synthesis.path/f64 value))
    (operation/wrap (|>> peekI
                         ($i.unwrap #$.Double)
                         ($i.double value)
                         $i.DCMPL
                         ($i.IFNE @else)))
    
    (^ (synthesis.path/text value))
    (operation/wrap (|>> peekI
                         ($i.string value)
                         ($i.INVOKEVIRTUAL "java.lang.Object"
                                           "equals"
                                           ($t.method (list $Object)
                                                      (#.Some $t.boolean)
                                                      (list))
                                           #0)
                         ($i.IFEQ @else)))
    
    (#synthesis.Then bodyS)
    (do compiler.Monad<Operation>
      [bodyI (translate bodyS)]
      (wrap (|>> (pop-altI stack-depth)
                 bodyI
                 ($i.GOTO @end))))
    
    
    (^template [<pattern> <method> <mod>]
      (^ (<pattern> idx))
      (operation/wrap (.case (<mod> idx)
                        +0
                        (|>> peekI
                             ($i.CHECKCAST ($t.descriptor runtime.$Tuple))
                             ($i.int 0)
                             $i.AALOAD
                             pushI)
                        
                        idx
                        (|>> peekI
                             ($i.CHECKCAST ($t.descriptor runtime.$Tuple))
                             ($i.int (.int idx))
                             ($i.INVOKESTATIC //.runtime-class
                                              <method>
                                              ($t.method (list runtime.$Tuple $t.int)
                                                         (#.Some $Object)
                                                         (list))
                                              #0)
                             pushI))))
    ([synthesis.member/left  "pm_left"  .id]
     [synthesis.member/right "pm_right" .inc])

    (^template [<pattern> <flag> <mod>]
      (^ (<pattern> idx))
      (.let [idx (<mod> idx)]
        (operation/wrap (<| $i.with-label (function (_ @success))
                            $i.with-label (function (_ @fail))
                            (|>> peekI
                                 ($i.CHECKCAST ($t.descriptor runtime.$Variant))
                                 ($i.int (.int idx))
                                 <flag>
                                 ($i.INVOKESTATIC //.runtime-class "pm_variant"
                                                  ($t.method (list runtime.$Variant runtime.$Tag runtime.$Flag)
                                                             (#.Some runtime.$Datum)
                                                             (list))
                                                  #0)
                                 $i.DUP
                                 ($i.IFNULL @fail)
                                 ($i.GOTO @success)
                                 ($i.label @fail)
                                 $i.POP
                                 ($i.GOTO @else)
                                 ($i.label @success)
                                 pushI)))))
    ([synthesis.side/left  $i.NULL        .id]
     [synthesis.side/right ($i.string "") .inc])

    (#synthesis.Alt leftP rightP)
    (do compiler.Monad<Operation>
      [@alt-else $i.make-label
       leftI (path' translate (inc stack-depth) @alt-else @end leftP)
       rightI (path' translate stack-depth @else @end rightP)]
      (wrap (|>> $i.DUP
                 leftI
                 ($i.label @alt-else)
                 $i.POP
                 rightI)))
    
    (#synthesis.Seq leftP rightP)
    (do compiler.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)
  (-> Compiler Path Label (Operation Inst))
  (do compiler.Monad<Operation>
    [@else $i.make-label
     pathI (..path' translate +1 @else @end path)]
    (wrap (|>> pathI
               ($i.label @else)
               $i.POP
               ($i.INVOKESTATIC //.runtime-class
                                "pm_fail"
                                ($t.method (list) #.None (list))
                                #0)
               $i.NULL
               ($i.GOTO @end)))))

(def: #export (if translate testS thenS elseS)
  (-> Compiler Synthesis Synthesis Synthesis (Operation Inst))
  (do compiler.Monad<Operation>
    [testI (translate testS)
     thenI (translate thenS)
     elseI (translate elseS)]
    (wrap (<| $i.with-label (function (_ @else))
              $i.with-label (function (_ @end))
              (|>> testI
                   ($i.unwrap #$.Boolean)
                   ($i.IFEQ @else)
                   thenI
                   ($i.GOTO @end)
                   ($i.label @else)
                   elseI
                   ($i.label @end))))))

(def: #export (let translate inputS register exprS)
  (-> Compiler Synthesis Nat Synthesis (Operation Inst))
  (do compiler.Monad<Operation>
    [inputI (translate inputS)
     exprI (translate exprS)]
    (wrap (|>> inputI
               ($i.ASTORE register)
               exprI))))

(def: #export (case translate valueS path)
  (-> Compiler Synthesis Path (Operation Inst))
  (do compiler.Monad<Operation>
    [@end $i.make-label
     valueI (translate valueS)
     pathI (..path translate path @end)]
    (wrap (|>> valueI
               $i.NULL
               $i.SWAP
               pushI
               pathI
               ($i.label @end)))))