aboutsummaryrefslogtreecommitdiff
path: root/lux-jvm/source/luxc/lang/directive/jvm.lux
blob: bcd40a1f8f3018dff64933fafc4fd5c07a24cef4 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
(.module:
  [lux #*
   [ffi (#+ import:)]
   [type (#+ :share)]
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." try (#+ Try)]]
   [data
    [identity (#+ Identity)]
    ["." product]
    [text
     ["%" format (#+ format)]]
    [collection
     ["." list ("#@." fold)]
     ["." dictionary (#+ Dictionary)]
     ["." row (#+ Row) ("#@." functor fold)]]]
   [math
    [number
     ["." nat]]]
   [target
    ["/" jvm]]
   [tool
    [compiler
     ["." phase]
     [language
      [lux
       [synthesis (#+ Synthesis)]
       ["." generation]
       ["." directive]
       [phase
        ["." extension
         ["." bundle]
         [directive
          ["./" lux]]]]]]]]]
  [///
   [host
    ["." jvm (#+ Inst)
     ["_" inst]]]])

(import: org/objectweb/asm/Label
  ["#::."
   (new [])])

(def: (literal literal)
  (-> /.Literal Inst)
  (case literal
    (#/.Boolean value) (_.boolean value)
    (#/.Int value) (_.int value)
    (#/.Long value) (_.long value)
    (#/.Double value) (_.double value)
    (#/.Char value) (_.char value)
    (#/.String value) (_.string value)))

(def: (constant instruction)
  (-> /.Constant Inst)
  (case instruction
    (#/.BIPUSH constant) (_.BIPUSH constant)
    
    (#/.SIPUSH constant) (_.SIPUSH constant)

    #/.ICONST_M1 _.ICONST_M1
    #/.ICONST_0 _.ICONST_0
    #/.ICONST_1 _.ICONST_1
    #/.ICONST_2 _.ICONST_2
    #/.ICONST_3 _.ICONST_3
    #/.ICONST_4 _.ICONST_4
    #/.ICONST_5 _.ICONST_5

    #/.LCONST_0 _.LCONST_0
    #/.LCONST_1 _.LCONST_1
    
    #/.FCONST_0 _.FCONST_0
    #/.FCONST_1 _.FCONST_1
    #/.FCONST_2 _.FCONST_2
    
    #/.DCONST_0 _.DCONST_0
    #/.DCONST_1 _.DCONST_1
    
    #/.ACONST_NULL _.NULL

    (#/.LDC literal)
    (..literal literal)
    ))

(def: (int_arithmetic instruction)
  (-> /.Int_Arithmetic Inst)
  (case instruction
    #/.IADD _.IADD
    #/.ISUB _.ISUB
    #/.IMUL _.IMUL
    #/.IDIV _.IDIV
    #/.IREM _.IREM
    #/.INEG _.INEG))

(def: (long_arithmetic instruction)
  (-> /.Long_Arithmetic Inst)
  (case instruction
    #/.LADD _.LADD
    #/.LSUB _.LSUB
    #/.LMUL _.LMUL
    #/.LDIV _.LDIV
    #/.LREM _.LREM
    #/.LNEG _.LNEG))

(def: (float_arithmetic instruction)
  (-> /.Float_Arithmetic Inst)
  (case instruction
    #/.FADD _.FADD
    #/.FSUB _.FSUB
    #/.FMUL _.FMUL
    #/.FDIV _.FDIV
    #/.FREM _.FREM
    #/.FNEG _.FNEG))

(def: (double_arithmetic instruction)
  (-> /.Double_Arithmetic Inst)
  (case instruction
    #/.DADD _.DADD
    #/.DSUB _.DSUB
    #/.DMUL _.DMUL
    #/.DDIV _.DDIV
    #/.DREM _.DREM
    #/.DNEG _.DNEG))

(def: (arithmetic instruction)
  (-> /.Arithmetic Inst)
  (case instruction
    (#/.Int_Arithmetic int_arithmetic)
    (..int_arithmetic int_arithmetic)
    
    (#/.Long_Arithmetic long_arithmetic)
    (..long_arithmetic long_arithmetic)
    
    (#/.Float_Arithmetic float_arithmetic)
    (..float_arithmetic float_arithmetic)
    
    (#/.Double_Arithmetic double_arithmetic)
    (..double_arithmetic double_arithmetic)))

(def: (int_bitwise instruction)
  (-> /.Int_Bitwise Inst)
  (case instruction
    #/.IOR _.IOR
    #/.IXOR _.IXOR
    #/.IAND _.IAND
    #/.ISHL _.ISHL
    #/.ISHR _.ISHR
    #/.IUSHR _.IUSHR))

(def: (long_bitwise instruction)
  (-> /.Long_Bitwise Inst)
  (case instruction
    #/.LOR _.LOR
    #/.LXOR _.LXOR
    #/.LAND _.LAND
    #/.LSHL _.LSHL
    #/.LSHR _.LSHR
    #/.LUSHR _.LUSHR))

(def: (bitwise instruction)
  (-> /.Bitwise Inst)
  (case instruction
    (#/.Int_Bitwise int_bitwise)
    (..int_bitwise int_bitwise)
    
    (#/.Long_Bitwise long_bitwise)
    (..long_bitwise long_bitwise)))

(def: (conversion instruction)
  (-> /.Conversion Inst)
  (case instruction
    #/.I2B _.I2B
    #/.I2S _.I2S
    #/.I2L _.I2L
    #/.I2F _.I2F
    #/.I2D _.I2D
    #/.I2C _.I2C

    #/.L2I _.L2I
    #/.L2F _.L2F
    #/.L2D _.L2D

    #/.F2I _.F2I
    #/.F2L _.F2L
    #/.F2D _.F2D
    
    #/.D2I _.D2I
    #/.D2L _.D2L
    #/.D2F _.D2F))

(def: (array instruction)
  (-> /.Array Inst)
  (case instruction
    #/.ARRAYLENGTH _.ARRAYLENGTH

    (#/.NEWARRAY type) (_.NEWARRAY type)
    (#/.ANEWARRAY type) (_.ANEWARRAY type)

    #/.BALOAD _.BALOAD
    #/.BASTORE _.BASTORE

    #/.SALOAD _.SALOAD
    #/.SASTORE _.SASTORE

    #/.IALOAD _.IALOAD
    #/.IASTORE _.IASTORE

    #/.LALOAD _.LALOAD
    #/.LASTORE _.LASTORE

    #/.FALOAD _.FALOAD
    #/.FASTORE _.FASTORE

    #/.DALOAD _.DALOAD
    #/.DASTORE _.DASTORE
    
    #/.CALOAD _.CALOAD
    #/.CASTORE _.CASTORE

    #/.AALOAD _.AALOAD
    #/.AASTORE _.AASTORE))

(def: (object instruction)
  (-> /.Object Inst)
  (case instruction
    (^template [<tag> <inst>]
      [(<tag> class field_name field_type)
       (<inst> class field_name field_type)])
    ([#/.GETSTATIC _.GETSTATIC]
     [#/.PUTSTATIC _.PUTSTATIC]
     [#/.GETFIELD _.GETFIELD]
     [#/.PUTFIELD _.PUTFIELD])
    
    (#/.NEW type) (_.NEW type)
    
    (#/.INSTANCEOF type) (_.INSTANCEOF type)
    (#/.CHECKCAST type) (_.CHECKCAST type)

    (^template [<tag> <inst>]
      [(<tag> class method_name method_type)
       (<inst> class method_name method_type)])
    ([#/.INVOKEINTERFACE _.INVOKEINTERFACE]
     [#/.INVOKESPECIAL _.INVOKESPECIAL]
     [#/.INVOKESTATIC _.INVOKESTATIC]
     [#/.INVOKEVIRTUAL _.INVOKEVIRTUAL])
    ))

(def: (local_int instruction)
  (-> /.Local_Int Inst)
  (case instruction
    (#/.ILOAD register) (_.ILOAD register)
    (#/.ISTORE register) (_.ISTORE register)))

(def: (local_long instruction)
  (-> /.Local_Long Inst)
  (case instruction
    (#/.LLOAD register) (_.LLOAD register)
    (#/.LSTORE register) (_.LSTORE register)))

(def: (local_float instruction)
  (-> /.Local_Float Inst)
  (case instruction
    (#/.FLOAD register) (_.FLOAD register)
    (#/.FSTORE register) (_.FSTORE register)))

(def: (local_double instruction)
  (-> /.Local_Double Inst)
  (case instruction
    (#/.DLOAD register) (_.DLOAD register)
    (#/.DSTORE register) (_.DSTORE register)))

(def: (local_object instruction)
  (-> /.Local_Object Inst)
  (case instruction
    (#/.ALOAD register) (_.ALOAD register)
    (#/.ASTORE register) (_.ASTORE register)))

(def: (local instruction)
  (-> /.Local Inst)
  (case instruction
    (#/.Local_Int instruction) (..local_int instruction)
    (#/.IINC register) (_.IINC register)
    (#/.Local_Long instruction) (..local_long instruction)
    (#/.Local_Float instruction) (..local_float instruction)
    (#/.Local_Double instruction) (..local_double instruction)
    (#/.Local_Object instruction) (..local_object instruction)))

(def: (stack instruction)
  (-> /.Stack Inst)
  (case instruction
    #/.DUP _.DUP
    #/.DUP_X1 _.DUP_X1
    #/.DUP_X2 _.DUP_X2
    #/.DUP2 _.DUP2
    #/.DUP2_X1 _.DUP2_X1
    #/.DUP2_X2 _.DUP2_X2
    #/.SWAP _.SWAP
    #/.POP _.POP
    #/.POP2 _.POP2))

(def: (comparison instruction)
  (-> /.Comparison Inst)
  (case instruction
    #/.LCMP _.LCMP
    
    #/.FCMPG _.FCMPG
    #/.FCMPL _.FCMPL

    #/.DCMPG _.DCMPG
    #/.DCMPL _.DCMPL))

(def: (branching instruction)
  (-> (/.Branching org/objectweb/asm/Label) Inst)
  (case instruction
    (#/.IF_ICMPEQ label) (_.IF_ICMPEQ label)
    (#/.IF_ICMPGE label) (_.IF_ICMPGE label)
    (#/.IF_ICMPGT label) (_.IF_ICMPGT label)
    (#/.IF_ICMPLE label) (_.IF_ICMPLE label)
    (#/.IF_ICMPLT label) (_.IF_ICMPLT label)
    (#/.IF_ICMPNE label) (_.IF_ICMPNE label)
    (#/.IFEQ label) (_.IFEQ label)
    (#/.IFGE label) (_.IFGE label)
    (#/.IFGT label) (_.IFGT label)
    (#/.IFLE label) (_.IFLE label)
    (#/.IFLT label) (_.IFLT label)
    (#/.IFNE label) (_.IFNE label)

    (#/.TABLESWITCH min max default labels)
    (_.TABLESWITCH min max default labels)
    
    (#/.LOOKUPSWITCH default keys+labels)
    (_.LOOKUPSWITCH default keys+labels)

    (#/.IF_ACMPEQ label) (_.IF_ACMPEQ label)
    (#/.IF_ACMPNE label) (_.IF_ACMPNE label)
    (#/.IFNONNULL label) (_.IFNONNULL label)
    (#/.IFNULL label) (_.IFNULL label)))

(def: (exception instruction)
  (-> (/.Exception org/objectweb/asm/Label) Inst)
  (case instruction
    (#/.Try start end handler exception) (_.try start end handler exception)
    #/.ATHROW _.ATHROW))

(def: (concurrency instruction)
  (-> /.Concurrency Inst)
  (case instruction
    #/.MONITORENTER _.MONITORENTER
    #/.MONITOREXIT _.MONITOREXIT))

(def: (return instruction)
  (-> /.Return Inst)
  (case instruction
    #/.RETURN _.RETURN
    #/.IRETURN _.IRETURN
    #/.LRETURN _.LRETURN
    #/.FRETURN _.FRETURN
    #/.DRETURN _.DRETURN
    #/.ARETURN _.ARETURN))

(def: (control instruction)
  (-> (/.Control org/objectweb/asm/Label) Inst)
  (case instruction
    (#/.GOTO label) (_.GOTO label)
    (#/.Branching instruction) (..branching instruction)
    (#/.Exception instruction) (..exception instruction)
    (#/.Concurrency instruction) (..concurrency instruction)
    (#/.Return instruction) (..return instruction)))

(def: (instruction instruction)
  (-> (/.Instruction Inst org/objectweb/asm/Label) Inst)
  (case instruction
    #/.NOP _.NOP
    (#/.Constant instruction) (..constant instruction)
    (#/.Arithmetic instruction) (..arithmetic instruction)
    (#/.Bitwise instruction) (..bitwise instruction)
    (#/.Conversion instruction) (..conversion instruction)
    (#/.Array instruction) (..array instruction)
    (#/.Object instruction) (..object instruction)
    (#/.Local instruction) (..local instruction)
    (#/.Stack instruction) (..stack instruction)
    (#/.Comparison instruction) (..comparison instruction)
    (#/.Control instruction) (..control instruction)
    (#/.Embedded embedded) embedded))

(type: Mapping
  (Dictionary /.Label org/objectweb/asm/Label))

(type: (Re_labeler context)
  (-> [Mapping (context /.Label)]
      [Mapping (context org/objectweb/asm/Label)]))

(def: (relabel [mapping label])
  (Re_labeler Identity)
  (case (dictionary.get label mapping)
    (#.Some label)
    [mapping label]

    #.None
    (let [label' (org/objectweb/asm/Label::new)]
      [(dictionary.put label label' mapping) label'])))

(def: (relabel_branching [mapping instruction])
  (Re_labeler /.Branching)
  (case instruction
    (^template [<tag>]
      [(<tag> label)
       (let [[mapping label] (..relabel [mapping label])]
         [mapping (<tag> label)])])
    ([#/.IF_ICMPEQ] [#/.IF_ICMPGE] [#/.IF_ICMPGT] [#/.IF_ICMPLE] [#/.IF_ICMPLT] [#/.IF_ICMPNE]
     [#/.IFEQ] [#/.IFNE] [#/.IFGE] [#/.IFGT] [#/.IFLE] [#/.IFLT]

     [#/.IF_ACMPEQ] [#/.IF_ACMPNE] [#/.IFNONNULL] [#/.IFNULL])

    (#/.TABLESWITCH min max default labels)
    (let [[mapping default] (..relabel [mapping default])
          [mapping labels] (list@fold (function (_ input [mapping output])
                                        (let [[mapping input] (..relabel [mapping input])]
                                          [mapping (list& input output)]))
                                      [mapping (list)] labels)]
      [mapping (#/.TABLESWITCH min max default (list.reverse labels))])
    
    (#/.LOOKUPSWITCH default keys+labels)
    (let [[mapping default] (..relabel [mapping default])
          [mapping keys+labels] (list@fold (function (_ [expected input] [mapping output])
                                             (let [[mapping input] (..relabel [mapping input])]
                                               [mapping (list& [expected input] output)]))
                                           [mapping (list)] keys+labels)]
      [mapping (#/.LOOKUPSWITCH default (list.reverse keys+labels))])
    ))

(def: (relabel_exception [mapping instruction])
  (Re_labeler /.Exception)
  (case instruction
    (#/.Try start end handler exception)
    (let [[mapping start] (..relabel [mapping start])
          [mapping end] (..relabel [mapping end])
          [mapping handler] (..relabel [mapping handler])]
      [mapping (#/.Try start end handler exception)])
    
    #/.ATHROW
    [mapping #/.ATHROW]
    ))

(def: (relabel_control [mapping instruction])
  (Re_labeler /.Control)
  (case instruction
    (^template [<tag> <relabel>]
      [(<tag> instruction)
       (let [[mapping instruction] (<relabel> [mapping instruction])]
         [mapping (<tag> instruction)])])
    ([#/.GOTO ..relabel]
     [#/.Branching ..relabel_branching]
     [#/.Exception ..relabel_exception])

    (^template [<tag>]
      [(<tag> instruction)
       [mapping (<tag> instruction)]])
    ([#/.Concurrency] [#/.Return])
    ))

(def: (relabel_instruction [mapping instruction])
  (Re_labeler (/.Instruction Inst))
  (case instruction
    (#/.Embedded embedded)
    [mapping (#/.Embedded embedded)]

    #/.NOP
    [mapping #/.NOP]

    (^template [<tag>]
      [(<tag> instruction)
       [mapping (<tag> instruction)]])
    ([#/.Constant]
     [#/.Arithmetic]
     [#/.Bitwise]
     [#/.Conversion]
     [#/.Array]
     [#/.Object]
     [#/.Local]
     [#/.Stack]
     [#/.Comparison])
    
    (#/.Control instruction)
    (let [[mapping instruction] (..relabel_control [mapping instruction])]
      [mapping (#/.Control instruction)])))

(def: (relabel_bytecode [mapping bytecode])
  (Re_labeler (/.Bytecode Inst))
  (row@fold (function (_ input [mapping output])
              (let [[mapping input'] (..relabel_instruction [mapping input])]
                [mapping (row.add input' output)]))
            [mapping (row.row)]
            bytecode))

(def: fresh
  Mapping
  (dictionary.new nat.hash))

(def: bytecode
  (-> (/.Bytecode Inst /.Label) jvm.Inst)
  (|>> [..fresh]
       ..relabel_bytecode
       product.right
       (row@map ..instruction)
       row.to_list
       _.fuse))

(with_expansions [<anchor> (as_is jvm.Anchor)
                  <expression> (as_is Inst)
                  <directive> (as_is jvm.Definition)
                  <type_vars> (as_is <anchor> <expression> <directive>)]
  (type: Handler
    ## (generation.Handler jvm.Anchor (/.Bytecode Inst /.Label) jvm.Definition)
    (-> extension.Name
        (phase.Phase [(extension.Bundle <type_vars>)
                      (generation.State <type_vars>)]
                     Synthesis
                     <expression>)
        (phase.Phase [(extension.Bundle <type_vars>)
                      (generation.State <type_vars>)]
                     (List Synthesis)
                     (/.Bytecode Inst /.Label)))))

(def: (true_handler extender pseudo)
  (-> jvm.Extender Any jvm.Handler)
  (function (_ extension_name phase archive inputs)
    (\ phase.monad map
       (|>> (:as (/.Bytecode Inst /.Label)) ..bytecode)
       ((extender pseudo) extension_name phase archive inputs))))

(def: (def::generation extender)
  (-> jvm.Extender
      (directive.Handler jvm.Anchor jvm.Inst jvm.Definition))
  (function (handler extension_name phase archive inputsC+)
    (case inputsC+
      (^ (list nameC valueC))
      (do phase.monad
        [[_ _ name] (lux/.evaluate! archive Text nameC)
         [_ handlerV] (lux/.generator archive (:as Text name) ..Handler valueC)
         _ (|> handlerV
               (..true_handler extender)
               (extension.install extender (:as Text name))
               directive.lift_generation)
         _ (directive.lift_generation
            (generation.log! (format "Generation " (%.text (:as Text name)))))]
        (wrap directive.no_requirements))

      _
      (phase.throw extension.invalid_syntax [extension_name %.code inputsC+]))))

(def: #export (bundle extender)
  (-> jvm.Extender
      (directive.Bundle jvm.Anchor jvm.Inst jvm.Definition))
  (|> bundle.empty
      (dictionary.put "lux def generation" (..def::generation extender))))