aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/jvm/runtime.lux
blob: 55c0aaab11124be3ee1f9364f92c8f2afe439fc0 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
(.module:
  [lux (#- Type)
   [abstract
    [monad (#+ do)]]
   [data
    [text
     ["%" format (#+ format)]]
    [collection
     ["." list ("#@." functor)]]]
   ["." math]
   [target
    [jvm
     ["." type (#+ Type)
      ["." category (#+ Void Value' Value Return' Return Primitive Object Class Array Var Parameter Method)]
      ["." reflection]]]]
   [tool
    [compiler
     [arity (#+ Arity)]
     ["." phase]
     [language
      [lux
       ["." synthesis]
       ["." generation]]]]]]
  [luxc
   [lang
    [host
     ["$" jvm (#+ Label Inst Def Operation)
      ["$d" def]
      ["_" inst]]]]]
  ["." // (#+ ByteCode)])

(def: $Text (type.class "java.lang.String" (list)))
(def: #export $Tag type.int)
(def: #export $Flag (type.class "java.lang.Object" (list)))
(def: #export $Value (type.class "java.lang.Object" (list)))
(def: #export $Index type.int)
(def: #export $Stack (type.array $Value))
(def: $Throwable (type.class "java.lang.Throwable" (list)))

(def: nullary-init-methodT
  (type.method [(list) type.void (list)]))

(def: throw-methodT
  (type.method [(list) type.void (list)]))

(def: #export logI
  Inst
  (let [PrintStream (type.class "java.io.PrintStream" (list))
        outI (_.GETSTATIC (type.class "java.lang.System" (list)) "out" PrintStream)
        printI (function (_ method)
                 (_.INVOKEVIRTUAL PrintStream method (type.method [(list $Value) type.void (list)])))]
    (|>> outI (_.string "LOG: ") (printI "print")
         outI _.SWAP (printI "println"))))

(def: variant-method
  (type.method [(list $Tag $Flag $Value) //.$Variant (list)]))

(def: #export variantI
  Inst
  (_.INVOKESTATIC //.$Runtime "variant_make" variant-method))

(def: #export leftI
  Inst
  (|>> (_.int +0)
       _.NULL
       _.DUP2_X1
       _.POP2
       variantI))

(def: #export rightI
  Inst
  (|>> (_.int +1)
       (_.string "")
       _.DUP2_X1
       _.POP2
       variantI))

(def: #export someI Inst rightI)

(def: #export noneI
  Inst
  (|>> (_.int +0)
       _.NULL
       (_.string synthesis.unit)
       variantI))

(def: (tryI unsafeI)
  (-> Inst Inst)
  (<| _.with-label (function (_ @from))
      _.with-label (function (_ @to))
      _.with-label (function (_ @handler))
      (|>> (_.try @from @to @handler (type.class "java.lang.Exception" (list)))
           (_.label @from)
           unsafeI
           someI
           _.ARETURN
           (_.label @to)
           (_.label @handler)
           noneI
           _.ARETURN)))

(def: #export partials-field Text "partials")
(def: #export apply-method Text "apply")
(def: #export num-apply-variants Nat 8)

(def: #export (apply-signature arity)
  (-> Arity (Type Method))
  (type.method [(list.repeat arity $Value) $Value (list)]))

(def: adt-methods
  Def
  (let [store-tagI (|>> _.DUP (_.int +0) (_.ILOAD 0) (_.wrap type.int) _.AASTORE)
        store-flagI (|>> _.DUP (_.int +1) (_.ALOAD 1) _.AASTORE)
        store-valueI (|>> _.DUP (_.int +2) (_.ALOAD 2) _.AASTORE)]
    (|>> ($d.method #$.Public $.staticM "variant_make"
                    (type.method [(list $Tag $Flag $Value) //.$Variant (list)])
                    (|>> (_.int +3)
                         (_.ANEWARRAY $Value)
                         store-tagI
                         store-flagI
                         store-valueI
                         _.ARETURN)))))

(def: frac-methods
  Def
  (|>> ($d.method #$.Public $.staticM "decode_frac" (type.method [(list $Text) //.$Variant (list)])
                  (tryI
                   (|>> (_.ALOAD 0)
                        (_.INVOKESTATIC (type.class "java.lang.Double" (list)) "parseDouble" (type.method [(list $Text) type.double (list)]))
                        (_.wrap type.double))))
       ))

(def: (illegal-state-exception message)
  (-> Text Inst)
  (let [IllegalStateException (type.class "java.lang.IllegalStateException" (list))]
    (|>> (_.NEW IllegalStateException)
         _.DUP
         (_.string message)
         (_.INVOKESPECIAL IllegalStateException "<init>" (type.method [(list $Text) type.void (list)])))))

(def: pm-methods
  Def
  (let [tuple-sizeI (|>> (_.ALOAD 0) _.ARRAYLENGTH)
        last-rightI (|>> tuple-sizeI (_.int +1) _.ISUB)
        leftsI (_.ILOAD 1)
        left-indexI leftsI
        sub-leftsI (|>> leftsI
                        last-rightI
                        _.ISUB)
        sub-tupleI (|>> (_.ALOAD 0) last-rightI _.AALOAD (_.CHECKCAST //.$Tuple))
        recurI (: (-> Label Inst)
                  (function (_ @loop)
                    (|>> sub-leftsI (_.ISTORE 1)
                         sub-tupleI (_.ASTORE 0)
                         (_.GOTO @loop))))]
    (|>> ($d.method #$.Public $.staticM "pm_fail" throw-methodT
                    (|>> (illegal-state-exception "Invalid expression for pattern-matching.")
                         _.ATHROW))
         ($d.method #$.Public $.staticM "apply_fail" throw-methodT
                    (|>> (illegal-state-exception "Error while applying function.")
                         _.ATHROW))
         ($d.method #$.Public $.staticM "pm_push" (type.method [(list $Stack $Value) $Stack (list)])
                    (|>> (_.int +2)
                         (_.ANEWARRAY $Value)
                         _.DUP
                         (_.int +1)
                         (_.ALOAD 0)
                         _.AASTORE
                         _.DUP
                         (_.int +0)
                         (_.ALOAD 1)
                         _.AASTORE
                         _.ARETURN))
         ($d.method #$.Public $.staticM "pm_variant" (type.method [(list //.$Variant $Tag $Flag) $Value (list)])
                    (<| _.with-label (function (_ @loop))
                        _.with-label (function (_ @perfect-match!))
                        _.with-label (function (_ @tags-match!))
                        _.with-label (function (_ @maybe-nested))
                        _.with-label (function (_ @mismatch!))
                        (let [$variant (_.ALOAD 0)
                              $tag (_.ILOAD 1)
                              $last? (_.ALOAD 2)
                              
                              variant-partI (: (-> Nat Inst)
                                               (function (_ idx)
                                                 (|>> (_.int (.int idx)) _.AALOAD)))
                              ::tag (: Inst
                                       (|>> (variant-partI 0) (_.unwrap type.int)))
                              ::last? (variant-partI 1)
                              ::value (variant-partI 2)

                              super-nested-tag (|>> _.SWAP ## variant::tag, tag
                                                    _.ISUB)
                              super-nested (|>> super-nested-tag ## super-tag
                                                $variant ::last? ## super-tag, super-last
                                                $variant ::value ## super-tag, super-last, super-value
                                                ..variantI)
                              
                              update-$tag _.ISUB
                              update-$variant (|>> $variant ::value
                                                   (_.CHECKCAST //.$Variant)
                                                   (_.ASTORE 0))
                              iterate! (: (-> Label Inst)
                                          (function (_ @loop)
                                            (|>> update-$variant
                                                 update-$tag
                                                 (_.GOTO @loop))))
                              
                              not-found _.NULL])
                        (|>> $tag ## tag
                             (_.label @loop)
                             $variant ::tag ## tag, variant::tag
                             _.DUP2 (_.IF_ICMPEQ @tags-match!) ## tag, variant::tag
                             _.DUP2 (_.IF_ICMPGT @maybe-nested) ## tag, variant::tag
                             $last? (_.IFNULL @mismatch!) ## tag, variant::tag
                             super-nested ## super-variant
                             _.ARETURN
                             (_.label @tags-match!) ## tag, variant::tag
                             $last? ## tag, variant::tag, last?
                             $variant ::last? ## tag, variant::tag, last?, variant::last?
                             (_.IF_ACMPEQ @perfect-match!) ## tag, variant::tag
                             (_.label @maybe-nested) ## tag, variant::tag
                             $variant ::last? ## tag, variant::tag, variant::last?
                             (_.IFNULL @mismatch!) ## tag, variant::tag
                             (iterate! @loop)
                             (_.label @perfect-match!) ## tag, variant::tag
                             ## _.POP2
                             $variant ::value
                             _.ARETURN
                             (_.label @mismatch!) ## tag, variant::tag
                             ## _.POP2
                             not-found
                             _.ARETURN)))
         ($d.method #$.Public $.staticM "tuple_left" (type.method [(list //.$Tuple $Index) $Value (list)])
                    (<| _.with-label (function (_ @loop))
                        _.with-label (function (_ @recursive))
                        (let [left-accessI (|>> (_.ALOAD 0) left-indexI _.AALOAD)])
                        (|>> (_.label @loop)
                             leftsI last-rightI (_.IF_ICMPGE @recursive)
                             left-accessI
                             _.ARETURN
                             (_.label @recursive)
                             ## Recursive
                             (recurI @loop))))
         ($d.method #$.Public $.staticM "tuple_right" (type.method [(list //.$Tuple $Index) $Value (list)])
                    (<| _.with-label (function (_ @loop))
                        _.with-label (function (_ @not-tail))
                        _.with-label (function (_ @slice))
                        (let [right-indexI (|>> leftsI
                                                (_.int +1)
                                                _.IADD)
                              right-accessI (|>> (_.ALOAD 0)
                                                 _.SWAP
                                                 _.AALOAD)
                              sub-rightI (|>> (_.ALOAD 0)
                                              right-indexI
                                              tuple-sizeI
                                              (_.INVOKESTATIC (type.class "java.util.Arrays" (list)) "copyOfRange"
                                                              (type.method [(list //.$Tuple $Index $Index)
                                                                            //.$Tuple
                                                                            (list)])))])
                        (|>> (_.label @loop)
                             last-rightI right-indexI
                             _.DUP2 (_.IF_ICMPNE @not-tail)
                             ## _.POP
                             right-accessI
                             _.ARETURN
                             (_.label @not-tail)
                             (_.IF_ICMPGT @slice)
                             ## Must recurse
                             (recurI @loop)
                             (_.label @slice)
                             sub-rightI
                             _.ARETURN
                             )))
         )))

(def: #export try (type.method [(list //.$Function) //.$Variant (list)]))

(def: io-methods
  Def
  (let [StringWriter (type.class "java.io.StringWriter" (list))
        PrintWriter (type.class "java.io.PrintWriter" (list))
        string-writerI (|>> (_.NEW StringWriter)
                            _.DUP
                            (_.INVOKESPECIAL StringWriter "<init>" nullary-init-methodT))
        print-writerI (|>> (_.NEW PrintWriter)
                           _.SWAP
                           _.DUP2
                           _.POP
                           _.SWAP
                           (_.boolean true)
                           (_.INVOKESPECIAL PrintWriter "<init>" (type.method [(list (type.class "java.io.Writer" (list)) type.boolean) type.void (list)]))
                           )]
    (|>> ($d.method #$.Public $.staticM "try" ..try
                    (<| _.with-label (function (_ @from))
                        _.with-label (function (_ @to))
                        _.with-label (function (_ @handler))
                        (|>> (_.try @from @to @handler $Throwable)
                             (_.label @from)
                             (_.ALOAD 0)
                             _.NULL
                             (_.INVOKEVIRTUAL //.$Function apply-method (apply-signature 1))
                             rightI
                             _.ARETURN
                             (_.label @to)
                             (_.label @handler)
                             string-writerI ## TW
                             _.DUP2 ## TWTW
                             print-writerI ## TWTP
                             (_.INVOKEVIRTUAL $Throwable "printStackTrace" (type.method [(list (type.class "java.io.PrintWriter" (list))) type.void (list)])) ## TW
                             (_.INVOKEVIRTUAL StringWriter "toString" (type.method [(list) $Text (list)])) ## TS
                             _.SWAP _.POP leftI
                             _.ARETURN)))
         )))

(def: reflection
  (All [category]
    (-> (Type (<| Return' Value' category)) Text))
  (|>> type.reflection reflection.reflection))

(def: translate-runtime
  (Operation Any)
  (let [runtime-class (..reflection //.$Runtime)
        bytecode ($d.class #$.V1_6 #$.Public $.finalC runtime-class (list) (type.class "java.lang.Object" (list)) (list)
                           (|>> adt-methods
                                frac-methods
                                pm-methods
                                io-methods))]
    (do phase.monad
      [_ (generation.execute! runtime-class [runtime-class bytecode])]
      (generation.save! false ["" runtime-class] [runtime-class bytecode]))))

(def: translate-function
  (Operation Any)
  (let [applyI (|> (list.n/range 2 num-apply-variants)
                   (list@map (function (_ arity)
                               ($d.method #$.Public $.noneM apply-method (apply-signature arity)
                                          (let [preI (|> (list.n/range 0 (dec arity))
                                                         (list@map _.ALOAD)
                                                         _.fuse)]
                                            (|>> preI
                                                 (_.INVOKEVIRTUAL //.$Function apply-method (apply-signature (dec arity)))
                                                 (_.CHECKCAST //.$Function)
                                                 (_.ALOAD arity)
                                                 (_.INVOKEVIRTUAL //.$Function apply-method (apply-signature 1))
                                                 _.ARETURN)))))
                   (list& ($d.abstract-method #$.Public $.noneM apply-method (apply-signature 1)))
                   $d.fuse)
        $Object (type.class "java.lang.Object" (list))
        function-class (..reflection //.$Function)
        bytecode ($d.abstract #$.V1_6 #$.Public $.noneC function-class (list) $Object (list)
                              (|>> ($d.field #$.Public $.finalF partials-field type.int)
                                   ($d.method #$.Public $.noneM "<init>" (type.method [(list type.int) type.void (list)])
                                              (|>> (_.ALOAD 0)
                                                   (_.INVOKESPECIAL $Object "<init>" nullary-init-methodT)
                                                   (_.ALOAD 0)
                                                   (_.ILOAD 1)
                                                   (_.PUTFIELD //.$Function partials-field type.int)
                                                   _.RETURN))
                                   applyI))]
    (do phase.monad
      [_ (generation.execute! function-class [function-class bytecode])]
      (generation.save! false ["" function-class] [function-class bytecode]))))

(def: #export translate
  (Operation Any)
  (do phase.monad
    [_ translate-runtime]
    translate-function))