/
FuncBuilder.kt
1288 lines (1206 loc) · 60.3 KB
/
FuncBuilder.kt
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package asmble.compile.jvm
import asmble.ast.Node
import asmble.io.AstToSExpr
import asmble.io.SExprToStr
import asmble.util.Either
import asmble.util.add
import org.objectweb.asm.Handle
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import org.objectweb.asm.tree.*
import java.lang.invoke.MethodHandle
// TODO: modularize
open class FuncBuilder {
fun fromFunc(ctx: ClsContext, f: Node.Func, index: Int): Func {
ctx.debug { "Building function ${ctx.funcName(index)}" }
ctx.trace { "Function ast:\n${SExprToStr.fromSExpr(AstToSExpr.fromFunc(f))}" }
var func = Func(
access = Opcodes.ACC_PRIVATE,
name = ctx.funcName(index),
params = f.type.params.map(Node.Type.Value::typeRef),
ret = f.type.ret?.let(Node.Type.Value::typeRef) ?: Void::class.ref
)
// Rework the instructions
val reworkedInsns = ctx.reworker.rework(ctx, f)
// Start the implicit block
func = func.pushBlock(Node.Instr.Block(f.type.ret), f.type.ret, f.type.ret)
// Create the context
val funcCtx = FuncContext(
cls = ctx,
node = f,
insns = reworkedInsns,
memIsLocalVar =
ctx.reworker.nonAdjacentMemAccesses(reworkedInsns) >= ctx.nonAdjacentMemAccessesRequiringLocalVar
)
// Add the mem as a local variable if necessary
if (funcCtx.memIsLocalVar) func = func.addInsns(
VarInsnNode(Opcodes.ALOAD, 0),
FieldInsnNode(Opcodes.GETFIELD, ctx.thisRef.asmName, "memory", ctx.mem.memType.asmDesc),
VarInsnNode(Opcodes.ASTORE, funcCtx.actualLocalIndex(funcCtx.node.localsSize))
)
// Add all instructions
ctx.debug { "Applying insns for function ${ctx.funcName(index)}" }
// All functions have an implicit block
func = funcCtx.insns.foldIndexed(func) { index, func, insn ->
ctx.debug { "Applying insn $insn" }
val ret = applyInsn(funcCtx, func, insn, index)
ctx.trace { "Resulting stack: ${ret.stack}"}
ret
}
// End the implicit block
val implicitBlock = func.currentBlock
func = applyEnd(funcCtx, func)
f.type.ret?.typeRef?.also { func = func.popExpecting(it, implicitBlock) }
// If the last instruction does not terminate, add the expected return
if (func.insns.isEmpty() || !func.insns.last().isTerminating) {
func = func.addInsns(InsnNode(when (f.type.ret) {
null -> Opcodes.RETURN
Node.Type.Value.I32 -> Opcodes.IRETURN
Node.Type.Value.I64 -> Opcodes.LRETURN
Node.Type.Value.F32 -> Opcodes.FRETURN
Node.Type.Value.F64 -> Opcodes.DRETURN
}))
}
return func
}
fun applyInsn(ctx: FuncContext, fn: Func, i: Insn, index: Int) = when (i) {
is Insn.Node ->
applyNodeInsn(ctx, fn, i.insn, index)
is Insn.ImportFuncRefNeededOnStack ->
// Func refs are method handle fields
fn.addInsns(
VarInsnNode(Opcodes.ALOAD, 0),
FieldInsnNode(Opcodes.GETFIELD, ctx.cls.thisRef.asmName,
ctx.cls.funcName(i.index), MethodHandle::class.ref.asmDesc)
).push(MethodHandle::class.ref)
is Insn.ImportGlobalSetRefNeededOnStack ->
// Import setters are method handle fields
fn.addInsns(
VarInsnNode(Opcodes.ALOAD, 0),
FieldInsnNode(Opcodes.GETFIELD, ctx.cls.thisRef.asmName,
ctx.cls.importGlobalSetterFieldName(i.index), MethodHandle::class.ref.asmDesc)
).push(MethodHandle::class.ref)
is Insn.ThisNeededOnStack ->
fn.addInsns(VarInsnNode(Opcodes.ALOAD, 0)).push(ctx.cls.thisRef)
is Insn.MemNeededOnStack ->
putMemoryOnStack(ctx, fn)
}
fun applyNodeInsn(ctx: FuncContext, fn: Func, i: Node.Instr, index: Int) = when (i) {
is Node.Instr.Unreachable ->
fn.addInsns(UnsupportedOperationException::class.athrow("Unreachable")).markUnreachable()
is Node.Instr.Nop ->
fn.addInsns(InsnNode(Opcodes.NOP))
is Node.Instr.Block ->
fn.pushBlock(i, i.type, i.type)
is Node.Instr.Loop ->
fn.pushBlock(i, null, i.type)
is Node.Instr.If ->
// The label is set in else or end
fn.popExpecting(Int::class.ref).pushBlock(i, i.type, i.type).pushIf().
addInsns(JumpInsnNode(Opcodes.IFEQ, null))
is Node.Instr.Else ->
applyElse(ctx, fn)
is Node.Instr.End ->
applyEnd(ctx, fn)
is Node.Instr.Br ->
applyBr(ctx, fn, i)
is Node.Instr.BrIf ->
applyBrIf(ctx, fn, i)
is Node.Instr.BrTable ->
applyBrTable(ctx, fn, i)
is Node.Instr.Return ->
applyReturnInsn(ctx, fn)
is Node.Instr.Call ->
applyCallInsn(ctx, fn, i.index)
is Node.Instr.CallIndirect ->
applyCallIndirectInsn(ctx, fn, i.index)
is Node.Instr.Drop ->
fn.pop().let { (fn, popped) ->
fn.addInsns(InsnNode(if (popped.stackSize == 2) Opcodes.POP2 else Opcodes.POP))
}
is Node.Instr.Select ->
applySelectInsn(ctx, fn)
is Node.Instr.GetLocal ->
applyGetLocal(ctx, fn, i.index)
is Node.Instr.SetLocal ->
applySetLocal(ctx, fn, i.index)
is Node.Instr.TeeLocal ->
applyTeeLocal(ctx, fn, i.index)
is Node.Instr.GetGlobal ->
applyGetGlobal(ctx, fn, i.index)
is Node.Instr.SetGlobal ->
applySetGlobal(ctx, fn, i.index)
is Node.Instr.I32Load, is Node.Instr.I64Load, is Node.Instr.F32Load, is Node.Instr.F64Load,
is Node.Instr.I32Load8S, is Node.Instr.I32Load8U, is Node.Instr.I32Load16U, is Node.Instr.I32Load16S,
is Node.Instr.I64Load8S, is Node.Instr.I64Load8U, is Node.Instr.I64Load16U, is Node.Instr.I64Load16S,
is Node.Instr.I64Load32S, is Node.Instr.I64Load32U ->
applyLoadOp(ctx, fn, i as Node.Instr.Args.AlignOffset)
is Node.Instr.I32Store, is Node.Instr.I64Store, is Node.Instr.F32Store, is Node.Instr.F64Store,
is Node.Instr.I32Store8, is Node.Instr.I32Store16, is Node.Instr.I64Store8, is Node.Instr.I64Store16,
is Node.Instr.I64Store32 ->
applyStoreOp(ctx, fn, i as Node.Instr.Args.AlignOffset, index)
is Node.Instr.CurrentMemory ->
applyCurrentMemory(ctx, fn)
is Node.Instr.GrowMemory ->
applyGrowMemory(ctx, fn)
is Node.Instr.I32Const ->
fn.addInsns(i.value.const).push(Int::class.ref)
is Node.Instr.I64Const ->
fn.addInsns(i.value.const).push(Long::class.ref)
is Node.Instr.F32Const ->
fn.addInsns(i.value.const).push(Float::class.ref)
is Node.Instr.F64Const ->
fn.addInsns(i.value.const).push(Double::class.ref)
is Node.Instr.I32Eqz ->
applyI32UnaryCmp(ctx, fn, Opcodes.IFEQ)
is Node.Instr.I32Eq ->
applyI32CmpS(ctx, fn, Opcodes.IF_ICMPEQ)
is Node.Instr.I32Ne ->
applyI32CmpS(ctx, fn, Opcodes.IF_ICMPNE)
is Node.Instr.I32LtS ->
applyI32CmpS(ctx, fn, Opcodes.IF_ICMPLT)
is Node.Instr.I32LtU ->
applyI32CmpU(ctx, fn, Opcodes.IFLT)
is Node.Instr.I32GtS ->
applyI32CmpS(ctx, fn, Opcodes.IF_ICMPGT)
is Node.Instr.I32GtU ->
applyI32CmpU(ctx, fn, Opcodes.IFGT)
is Node.Instr.I32LeS ->
applyI32CmpS(ctx, fn, Opcodes.IF_ICMPLE)
is Node.Instr.I32LeU ->
applyI32CmpU(ctx, fn, Opcodes.IFLE)
is Node.Instr.I32GeS ->
applyI32CmpS(ctx, fn, Opcodes.IF_ICMPGE)
is Node.Instr.I32GeU ->
applyI32CmpU(ctx, fn, Opcodes.IFGE)
is Node.Instr.I64Eqz ->
fn.addInsns(0L.const).push(Long::class.ref).let { fn -> applyI64CmpS(ctx, fn, Opcodes.IFEQ) }
is Node.Instr.I64Eq ->
applyI64CmpS(ctx, fn, Opcodes.IFEQ)
is Node.Instr.I64Ne ->
applyI64CmpS(ctx, fn, Opcodes.IFNE)
is Node.Instr.I64LtS ->
applyI64CmpS(ctx, fn, Opcodes.IFLT)
is Node.Instr.I64LtU ->
applyI64CmpU(ctx, fn, Opcodes.IFLT)
is Node.Instr.I64GtS ->
applyI64CmpS(ctx, fn, Opcodes.IFGT)
is Node.Instr.I64GtU ->
applyI64CmpU(ctx, fn, Opcodes.IFGT)
is Node.Instr.I64LeS ->
applyI64CmpS(ctx, fn, Opcodes.IFLE)
is Node.Instr.I64LeU ->
applyI64CmpU(ctx, fn, Opcodes.IFLE)
is Node.Instr.I64GeS ->
applyI64CmpS(ctx, fn, Opcodes.IFGE)
is Node.Instr.I64GeU ->
applyI64CmpU(ctx, fn, Opcodes.IFGE)
is Node.Instr.F32Eq ->
applyF32Cmp(ctx, fn, Opcodes.IFEQ)
is Node.Instr.F32Ne ->
applyF32Cmp(ctx, fn, Opcodes.IFNE)
is Node.Instr.F32Lt ->
applyF32Cmp(ctx, fn, Opcodes.IFLT)
is Node.Instr.F32Gt ->
applyF32Cmp(ctx, fn, Opcodes.IFGT, nanIsOne = false)
is Node.Instr.F32Le ->
applyF32Cmp(ctx, fn, Opcodes.IFLE)
is Node.Instr.F32Ge ->
applyF32Cmp(ctx, fn, Opcodes.IFGE, nanIsOne = false)
is Node.Instr.F64Eq ->
applyF64Cmp(ctx, fn, Opcodes.IFEQ)
is Node.Instr.F64Ne ->
applyF64Cmp(ctx, fn, Opcodes.IFNE)
is Node.Instr.F64Lt ->
applyF64Cmp(ctx, fn, Opcodes.IFLT)
is Node.Instr.F64Gt ->
applyF64Cmp(ctx, fn, Opcodes.IFGT, nanIsOne = false)
is Node.Instr.F64Le ->
applyF64Cmp(ctx, fn, Opcodes.IFLE)
is Node.Instr.F64Ge ->
applyF64Cmp(ctx, fn, Opcodes.IFGE, nanIsOne = false)
is Node.Instr.I32Clz ->
applyI32Unary(ctx, fn, Integer::class.invokeStatic("numberOfLeadingZeros", Int::class, Int::class))
is Node.Instr.I32Ctz ->
applyI32Unary(ctx, fn, Integer::class.invokeStatic("numberOfTrailingZeros", Int::class, Int::class))
is Node.Instr.I32Popcnt ->
applyI32Unary(ctx, fn, Integer::class.invokeStatic("bitCount", Int::class, Int::class))
is Node.Instr.I32Add ->
applyI32Binary(ctx, fn, Opcodes.IADD)
is Node.Instr.I32Sub ->
applyI32Binary(ctx, fn, Opcodes.ISUB)
is Node.Instr.I32Mul ->
applyI32Binary(ctx, fn, Opcodes.IMUL)
is Node.Instr.I32DivS ->
assertSignedIntegerDiv(ctx, fn, Int::class.ref).let { fn ->
applyI32Binary(ctx, fn, Opcodes.IDIV)
}
is Node.Instr.I32DivU ->
applyI32Binary(ctx, fn, Integer::class.invokeStatic("divideUnsigned", Int::class, Int::class, Int::class))
is Node.Instr.I32RemS ->
applyI32Binary(ctx, fn, Opcodes.IREM)
is Node.Instr.I32RemU ->
applyI32Binary(ctx, fn, Integer::class.invokeStatic("remainderUnsigned", Int::class, Int::class, Int::class))
is Node.Instr.I32And ->
applyI32Binary(ctx, fn, Opcodes.IAND)
is Node.Instr.I32Or ->
applyI32Binary(ctx, fn, Opcodes.IOR)
is Node.Instr.I32Xor ->
applyI32Binary(ctx, fn, Opcodes.IXOR)
is Node.Instr.I32Shl ->
applyI32Binary(ctx, fn, Opcodes.ISHL)
is Node.Instr.I32ShrS ->
applyI32Binary(ctx, fn, Opcodes.ISHR)
is Node.Instr.I32ShrU ->
applyI32Binary(ctx, fn, Opcodes.IUSHR)
is Node.Instr.I32Rotl ->
applyI32Binary(ctx, fn, Integer::class.invokeStatic("rotateLeft", Int::class, Int::class, Int::class))
is Node.Instr.I32Rotr ->
applyI32Binary(ctx, fn, Integer::class.invokeStatic("rotateRight", Int::class, Int::class, Int::class))
is Node.Instr.I64Clz ->
applyI64Unary(ctx, fn,
java.lang.Long::class.invokeStatic("numberOfLeadingZeros", Int::class, Long::class)).
addInsns(InsnNode(Opcodes.I2L))
is Node.Instr.I64Ctz ->
applyI64Unary(ctx, fn,
java.lang.Long::class.invokeStatic("numberOfTrailingZeros", Int::class, Long::class)).
addInsns(InsnNode(Opcodes.I2L))
is Node.Instr.I64Popcnt ->
applyI64Unary(ctx, fn, java.lang.Long::class.invokeStatic("bitCount", Int::class, Long::class)).
addInsns(InsnNode(Opcodes.I2L))
is Node.Instr.I64Add ->
applyI64Binary(ctx, fn, Opcodes.LADD)
is Node.Instr.I64Sub ->
applyI64Binary(ctx, fn, Opcodes.LSUB)
is Node.Instr.I64Mul ->
applyI64Binary(ctx, fn, Opcodes.LMUL)
is Node.Instr.I64DivS ->
assertSignedIntegerDiv(ctx, fn, Long::class.ref).let { fn ->
applyI64Binary(ctx, fn, Opcodes.LDIV)
}
is Node.Instr.I64DivU ->
applyI64Binary(ctx, fn, java.lang.Long::class.invokeStatic("divideUnsigned",
Long::class, Long::class, Long::class))
is Node.Instr.I64RemS ->
applyI64Binary(ctx, fn, Opcodes.LREM)
is Node.Instr.I64RemU ->
applyI64Binary(ctx, fn, java.lang.Long::class.invokeStatic("remainderUnsigned",
Long::class, Long::class, Long::class))
is Node.Instr.I64And ->
applyI64Binary(ctx, fn, Opcodes.LAND)
is Node.Instr.I64Or ->
applyI64Binary(ctx, fn, Opcodes.LOR)
is Node.Instr.I64Xor ->
applyI64Binary(ctx, fn, Opcodes.LXOR)
is Node.Instr.I64Shl ->
applyI64BinarySecondOpI32(ctx, fn, Opcodes.LSHL)
is Node.Instr.I64ShrS ->
applyI64BinarySecondOpI32(ctx, fn, Opcodes.LSHR)
is Node.Instr.I64ShrU ->
applyI64BinarySecondOpI32(ctx, fn, Opcodes.LUSHR)
is Node.Instr.I64Rotl ->
applyI64BinarySecondOpI32(ctx, fn, java.lang.Long::class.invokeStatic("rotateLeft",
Long::class, Long::class, Int::class))
is Node.Instr.I64Rotr ->
applyI64BinarySecondOpI32(ctx, fn, java.lang.Long::class.invokeStatic("rotateRight",
Long::class, Long::class, Int::class))
is Node.Instr.F32Abs ->
applyF32UnaryNanReturnPositive(ctx, fn) { fn ->
applyF32Unary(ctx, fn, forceFnType<(Float) -> Float>(Math::abs).invokeStatic())
}
is Node.Instr.F32Neg ->
applyF32Unary(ctx, fn, InsnNode(Opcodes.FNEG))
is Node.Instr.F32Ceil ->
applyWithF32To64AndBack(ctx, fn) { fn -> applyF64Unary(ctx, fn, Math::ceil.invokeStatic()) }
is Node.Instr.F32Floor ->
applyWithF32To64AndBack(ctx, fn) { fn -> applyF64Unary(ctx, fn, Math::floor.invokeStatic()) }
is Node.Instr.F32Trunc ->
applyF32Trunc(ctx, fn)
is Node.Instr.F32Nearest ->
applyF32UnaryNanReturnSame(ctx, fn) { fn ->
applyWithF32To64AndBack(ctx, fn) { fn -> applyF64Unary(ctx, fn, Math::rint.invokeStatic()) }
}
is Node.Instr.F32Sqrt ->
applyWithF32To64AndBack(ctx, fn) { fn -> applyF64Unary(ctx, fn, Math::sqrt.invokeStatic()) }
is Node.Instr.F32Add ->
applyF32Binary(ctx, fn, Opcodes.FADD)
is Node.Instr.F32Sub ->
applyF32Binary(ctx, fn, Opcodes.FSUB)
is Node.Instr.F32Mul ->
applyF32Binary(ctx, fn, Opcodes.FMUL)
is Node.Instr.F32Div ->
applyF32Binary(ctx, fn, Opcodes.FDIV)
is Node.Instr.F32Min ->
applyF32Binary(ctx, fn, forceFnType<(Float, Float) -> Float>(Math::min).invokeStatic())
is Node.Instr.F32Max ->
applyF32Binary(ctx, fn, forceFnType<(Float, Float) -> Float>(Math::max).invokeStatic())
is Node.Instr.F32CopySign ->
applyF32Binary(ctx, fn, forceFnType<(Float, Float) -> Float>(Math::copySign).invokeStatic())
is Node.Instr.F64Abs ->
applyF64UnaryNanReturnPositive(ctx, fn) { fn ->
applyF64Unary(ctx, fn, forceFnType<(Double) -> Double>(Math::abs).invokeStatic())
}
is Node.Instr.F64Neg ->
applyF64Unary(ctx, fn, InsnNode(Opcodes.DNEG))
is Node.Instr.F64Ceil ->
applyF64Unary(ctx, fn, Math::ceil.invokeStatic())
is Node.Instr.F64Floor ->
applyF64Unary(ctx, fn, Math::floor.invokeStatic())
is Node.Instr.F64Trunc ->
applyF64Trunc(ctx, fn)
is Node.Instr.F64Nearest ->
applyF64UnaryNanReturnSame(ctx, fn) { fn ->
applyF64Unary(ctx, fn, Math::rint.invokeStatic())
}
is Node.Instr.F64Sqrt ->
applyF64Unary(ctx, fn, Math::sqrt.invokeStatic())
is Node.Instr.F64Add ->
applyF64Binary(ctx, fn, Opcodes.DADD)
is Node.Instr.F64Sub ->
applyF64Binary(ctx, fn, Opcodes.DSUB)
is Node.Instr.F64Mul ->
applyF64Binary(ctx, fn, Opcodes.DMUL)
is Node.Instr.F64Div ->
applyF64Binary(ctx, fn, Opcodes.DDIV)
is Node.Instr.F64Min ->
applyF64Binary(ctx, fn, forceFnType<(Double, Double) -> Double>(Math::min).invokeStatic())
is Node.Instr.F64Max ->
applyF64Binary(ctx, fn, forceFnType<(Double, Double) -> Double>(Math::max).invokeStatic())
is Node.Instr.F64CopySign ->
applyF64Binary(ctx, fn, forceFnType<(Double, Double) -> Double>(Math::copySign).invokeStatic())
is Node.Instr.I32WrapI64 ->
applyConv(ctx, fn, Long::class.ref, Int::class.ref, Opcodes.L2I)
is Node.Instr.I32TruncSF32 ->
assertTruncConv(ctx, fn, Float::class.ref, Int::class.ref, signed = true).let { fn ->
applyConv(ctx, fn, Float::class.ref, Int::class.ref, Opcodes.F2I)
}
is Node.Instr.I32TruncUF32 ->
assertTruncConv(ctx, fn, Float::class.ref, Int::class.ref, signed = false).let { fn ->
applyConv(ctx, fn, Float::class.ref, Long::class.ref, Opcodes.F2L).let { fn ->
applyConv(ctx, fn, Long::class.ref, Int::class.ref, Opcodes.L2I)
}
}
is Node.Instr.I32TruncSF64 ->
assertTruncConv(ctx, fn, Double::class.ref, Int::class.ref, signed = true).let { fn ->
applyConv(ctx, fn, Double::class.ref, Int::class.ref, Opcodes.D2I)
}
is Node.Instr.I32TruncUF64 ->
assertTruncConv(ctx, fn, Double::class.ref, Int::class.ref, signed = false).let { fn ->
applyConv(ctx, fn, Double::class.ref, Long::class.ref, Opcodes.D2L).let { fn ->
applyConv(ctx, fn, Long::class.ref, Int::class.ref, Opcodes.L2I)
}
}
is Node.Instr.I64ExtendSI32 ->
applyConv(ctx, fn, Int::class.ref, Long::class.ref, Opcodes.I2L)
is Node.Instr.I64ExtendUI32 ->
applyConv(ctx, fn, Int::class.ref, Long::class.ref,
Integer::class.invokeStatic("toUnsignedLong", Long::class, Int::class))
is Node.Instr.I64TruncSF32 ->
assertTruncConv(ctx, fn, Float::class.ref, Long::class.ref, signed = true).let { fn ->
applyConv(ctx, fn, Float::class.ref, Long::class.ref, Opcodes.F2L)
}
is Node.Instr.I64TruncUF32 ->
assertTruncConv(ctx, fn, Float::class.ref, Long::class.ref, signed = false).let { fn ->
applyI64TruncUF32(ctx, fn)
}
is Node.Instr.I64TruncSF64 ->
assertTruncConv(ctx, fn, Double::class.ref, Long::class.ref, signed = true).let { fn ->
applyConv(ctx, fn, Double::class.ref, Long::class.ref, Opcodes.D2L)
}
is Node.Instr.I64TruncUF64 ->
assertTruncConv(ctx, fn, Double::class.ref, Long::class.ref, signed = false).let { fn ->
applyI64TruncUF64(ctx, fn)
}
is Node.Instr.F32ConvertSI32 ->
applyConv(ctx, fn, Int::class.ref, Float::class.ref, Opcodes.I2F)
is Node.Instr.F32ConvertUI32 ->
fn.addInsns(Integer::class.invokeStatic("toUnsignedLong", Long::class, Int::class)).
let { fn -> applyConv(ctx, fn, Int::class.ref, Float::class.ref, Opcodes.L2F) }
is Node.Instr.F32ConvertSI64 ->
applyConv(ctx, fn, Long::class.ref, Float::class.ref, Opcodes.L2F)
is Node.Instr.F32ConvertUI64 ->
applyF32ConvertUI64(ctx, fn)
is Node.Instr.F32DemoteF64 ->
applyConv(ctx, fn, Double::class.ref, Float::class.ref, Opcodes.D2F)
is Node.Instr.F64ConvertSI32 ->
applyConv(ctx, fn, Int::class.ref, Double::class.ref, Opcodes.I2D)
is Node.Instr.F64ConvertUI32 ->
fn.addInsns(Integer::class.invokeStatic("toUnsignedLong", Long::class, Int::class)).
let { fn -> applyConv(ctx, fn, Int::class.ref, Double::class.ref, Opcodes.L2D) }
is Node.Instr.F64ConvertSI64 ->
applyConv(ctx, fn, Long::class.ref, Double::class.ref, Opcodes.L2D)
is Node.Instr.F64ConvertUI64 ->
applyF64ConvertUI64(ctx, fn)
is Node.Instr.F64PromoteF32 ->
applyConv(ctx, fn, Float::class.ref, Double::class.ref, Opcodes.F2D)
is Node.Instr.I32ReinterpretF32 ->
applyConv(ctx, fn, Float::class.ref, Int::class.ref,
java.lang.Float::class.invokeStatic("floatToRawIntBits", Int::class, Float::class))
is Node.Instr.I64ReinterpretF64 ->
applyConv(ctx, fn, Double::class.ref, Long::class.ref,
java.lang.Double::class.invokeStatic("doubleToRawLongBits", Long::class, Double::class))
is Node.Instr.F32ReinterpretI32 ->
applyConv(ctx, fn, Int::class.ref, Float::class.ref,
java.lang.Float::class.invokeStatic("intBitsToFloat", Float::class, Int::class))
is Node.Instr.F64ReinterpretI64 ->
applyConv(ctx, fn, Long::class.ref, Double::class.ref,
java.lang.Double::class.invokeStatic("longBitsToDouble", Double::class, Long::class))
}
fun popForBlockEscape(ctx: FuncContext, fn: Func, block: Func.Block) =
popUntilStackSize(ctx, fn, block, block.origStack.size + block.labelTypes.size, block.labelTypes.isNotEmpty())
fun popUntilStackSize(
ctx: FuncContext,
fn: Func,
block: Func.Block,
untilStackSize: Int,
keepLast: Boolean
): Func {
ctx.debug { "For block ${block.insn}, popping until stack size $untilStackSize, keeping last? $keepLast" }
// Just get the latest, don't actually pop...
val type = if (keepLast) fn.pop().second else null
return (0 until Math.max(0, fn.stack.size - untilStackSize)).fold(fn) { fn, _ ->
// Essentially swap and pop if they want to keep the latest
(if (type != null && fn.stack.size > 1) fn.stackSwap(block) else fn).let { fn ->
fn.pop(block).let { (fn, poppedType) ->
fn.addInsns(InsnNode(if (poppedType.stackSize == 2) Opcodes.POP2 else Opcodes.POP))
}
}
}
}
fun applyBr(ctx: FuncContext, fn: Func, i: Node.Instr.Br) =
fn.blockAtDepth(i.relativeDepth).let { block ->
ctx.debug { "Unconditional branch on ${block.insn}, curr stack ${fn.stack}, orig stack ${block.origStack}" }
popForBlockEscape(ctx, fn, block).
popExpectingMulti(block.labelTypes, block).
addInsns(JumpInsnNode(Opcodes.GOTO, block.requiredLabel)).
markUnreachable()
}
fun applyBrIf(ctx: FuncContext, fn: Func, i: Node.Instr.BrIf) =
fn.blockAtDepth(i.relativeDepth).let { block ->
fn.popExpecting(Int::class.ref).let { fn ->
// Must at least have the item on the stack that the block expects if it expects something
val needsPopBeforeJump = needsToPopBeforeJumping(ctx, fn, block)
val toLabel = if (needsPopBeforeJump) LabelNode() else block.requiredLabel
fn.addInsns(JumpInsnNode(Opcodes.IFNE, toLabel)).let { fn ->
block.endTypes.firstOrNull()?.let { fn.peekExpecting(it) }
if (needsPopBeforeJump) buildPopBeforeJump(ctx, fn, block, toLabel)
else fn
}
}
}
// Can compile quite cleanly as a table switch on the JVM
fun applyBrTable(ctx: FuncContext, fn: Func, insn: Node.Instr.BrTable) =
fn.blockAtDepth(insn.default).let { defaultBlock ->
insn.targetTable.fold(fn to emptyList<Func.Block>()) { (fn, blocks), targetDepth ->
// All of the target label types have to match the default one
val targetBlock = fn.blockAtDepth(targetDepth)
if (targetBlock.labelTypes != defaultBlock.labelTypes)
throw CompileErr.TableTargetMismatch(defaultBlock.labelTypes, targetBlock.labelTypes)
fn to (blocks + targetBlock)
}.let { (fn, targetBlocks) ->
val fn = fn.popExpecting(Int::class.ref)
// We might have to pop before some jumps sadly
var tempLabels = emptyList<Pair<LabelNode, Func.Block>>()
fun blockLabel(block: Func.Block) =
if (!needsToPopBeforeJumping(ctx, fn, block)) block.requiredLabel
else LabelNode().also {
tempLabels += it to block
}
val defaultLabel = blockLabel(defaultBlock)
val targetLabels = targetBlocks.map(::blockLabel)
// If it's large, we need to handle it differently
if (insn.targetTable.size > ctx.cls.jumpTableChunkSize) {
require(tempLabels.isEmpty()) {
"Leftover conditional jump stack popping is not yet supported for large tables"
}
applyLargeBrTable(ctx, fn, insn, defaultLabel, targetLabels)
} else {
// In some cases, the target labels is empty. We need to make 0 goto
// the default as well.
val targetLabelsArr =
if (targetLabels.isNotEmpty()) targetLabels.toTypedArray()
else arrayOf(defaultLabel)
fn.addInsns(TableSwitchInsnNode(0, targetLabelsArr.size - 1, defaultLabel, *targetLabelsArr)).
let { fn ->
tempLabels.fold(fn) { fn, (label, block) -> buildPopBeforeJump(ctx, fn, block, label) }
}.
popExpectingMulti(defaultBlock.labelTypes).
markUnreachable()
}
}
}
// This already has the index int type popped
fun applyLargeBrTable(
ctx: FuncContext,
fn: Func,
insn: Node.Instr.BrTable,
defaultLabel: LabelNode,
targetLabels: List<LabelNode>
): Func {
// We build a method call to get our set of depths, then we do a table switch
// on the depths. There may be holes in the depths, which we'll fill in w/ the
// default label. And we'll make the default label unreachable.
val depthToLabel = mutableListOf<LabelNode?>()
fun addLabel(depth: Int, label: LabelNode) {
if (depthToLabel.getOrNull(depth) == null) {
for (i in depthToLabel.size..depth) depthToLabel.add(null)
depthToLabel[depth] = label
}
}
insn.targetTable.forEachIndexed { index, targetDepth -> addLabel(targetDepth, targetLabels[index]) }
addLabel(insn.default, defaultLabel)
val unreachableLabel = LabelNode()
return fn.addInsns(
ctx.cls.largeTableJumpCall(insn),
TableSwitchInsnNode(0, depthToLabel.size - 1, unreachableLabel,
*depthToLabel.map { it ?: unreachableLabel }.toTypedArray()),
unreachableLabel
).addInsns(UnsupportedOperationException::class.athrow("Unreachable")).markUnreachable()
}
fun needsToPopBeforeJumping(ctx: FuncContext, fn: Func, block: Func.Block): Boolean {
val requiredStackCount = if (block.endTypes.isEmpty()) block.origStack.size else block.origStack.size + 1
return fn.stack.size > requiredStackCount
}
fun buildPopBeforeJump(ctx: FuncContext, fn: Func, block: Func.Block, tempLabel: LabelNode): Func {
// This is sad that we have to do this because we can't trust the wasm stack on nested breaks
// Steps:
// 1. Build a label, do a GOTO to it for the regular path
// 2. Start the given temp label, do the pop, then goto the block label
// 3. Resume code from the label at #1
// NOTE: We have chosen to do it this way instead of negating the br_if conditional
// or whatever because this should not happen in practice but in many tests there
// are leftover stack items when doing nested breaks
// TODO: make this better by moving this "pad" to the block end so we don't have
// to make the running code path jump also. Also consider a better approach than
// the constant swap-and-pop we do here.
val requiredStackCount = if (block.endTypes.isEmpty()) block.origStack.size else block.origStack.size + 1
ctx.debug {
"Jumping to block requiring stack size $requiredStackCount but we " +
"have ${fn.stack.size} so we are popping all unnecessary stack items before jumping"
}
// We actually have to pop the second to last, keeping the latest (unless it's empty)...and we do
// this over and over, sadly, if there are more to discard
val resumeLabel = LabelNode()
return fn.addInsns(JumpInsnNode(Opcodes.GOTO, resumeLabel), tempLabel).withoutAffectingStack { fn ->
(requiredStackCount until fn.stack.size).fold(fn) { fn, index ->
if (fn.stack.size == 1) {
fn.addInsns(InsnNode(if (fn.stack.last().stackSize == 2) Opcodes.POP2 else Opcodes.POP)).
pop(block).first
} else fn.stackSwap(block).let { fn ->
fn.addInsns(InsnNode(if (fn.stack.last().stackSize == 2) Opcodes.POP2 else Opcodes.POP)).
pop(block).first
}
}
}.addInsns(
JumpInsnNode(Opcodes.GOTO, block.requiredLabel),
resumeLabel
)
}
fun applyElse(ctx: FuncContext, fn: Func) = fn.blockAtDepth(0).let { block ->
// Do a goto the end, and then add a fresh label to the initial "if" that jumps here
// Also, put the stack back at what it was pre-if and ask end to check the else stack
val label = LabelNode()
fn.peekIf().label = label
ctx.debug { "Else block for ${block.insn}, orig stack ${block.origStack}" }
block.hasElse = true
block.thenStackOnIf = fn.stack
fn.addInsns(JumpInsnNode(Opcodes.GOTO, block.requiredLabel), label).copy(stack = block.origStack)
}
fun applyEnd(ctx: FuncContext, fn: Func) = fn.popBlock().let { (fn, block) ->
ctx.debug { "End of block ${block.insn}, orig stack ${block.origStack}, unreachable? " + block.unreachable }
// "If" block checks
if (block.insn is Node.Instr.If) {
// If the block was an typed if w/ no else, it is wrong
if (block.endTypes.isNotEmpty() && !block.hasElse)
throw CompileErr.IfThenValueWithoutElse()
// If the block was an if/then w/ a stack but the else doesn't match it
if (block.hasElse && !block.unreachableInIf && !block.unreachableInElse && block.thenStackOnIf != fn.stack)
throw CompileErr.BlockEndMismatch(block.thenStackOnIf, fn.stack)
}
// Put the stack where it should be
fn.popExpectingMulti(block.endTypes, block).let { fn ->
// Do normal block-end validation
assertValidBlockEnd(ctx, fn, block)
fn.push(block.endTypes).let { fn ->
when (block.insn) {
is Node.Instr.Block ->
// Add label to end of block if it's there
block.label?.let { fn.addInsns(it) } ?: fn
is Node.Instr.Loop ->
// Add label to beginning of loop if it's there
block.label?.let { fn.copy(insns = fn.insns.add(block.startIndex, it)) } ?: fn
is Node.Instr.If -> fn.popIf().let { (fn, jumpNode) ->
when (block.label) {
// If there is no existing break label, add one to initial
// "if" only if it isn't there from an "else"
null -> if (jumpNode.label != null) fn else {
jumpNode.label = LabelNode()
fn.addInsns(jumpNode.label)
}
// If there is one, add it to the initial "if"
// if the "else" didn't set one on there...then push it
else -> {
if (jumpNode.label == null) jumpNode.label = block.label
fn.addInsns(block.label!!)
}
}
}
else -> error("Unrecognized end for ${block.insn}")
}
}
}
}
fun assertValidBlockEnd(ctx: FuncContext, fn: Func, block: Func.Block) {
if (fn.stack != block.origStack) {
throw CompileErr.BlockEndMismatch(block.origStack, fn.stack)
}
}
fun applyF32ConvertUI64(ctx: FuncContext, fn: Func): Func {
// l >= 0 ? (float) l : ((float) ((l >> 1) * 2.0f))
val notPositive = LabelNode()
val allDone = LabelNode()
return fn.popExpecting(Long::class.ref).addInsns(
InsnNode(Opcodes.DUP2),
0L.const,
InsnNode(Opcodes.LCMP),
JumpInsnNode(Opcodes.IFLT, notPositive),
InsnNode(Opcodes.L2F),
JumpInsnNode(Opcodes.GOTO, allDone),
notPositive,
1.const,
InsnNode(Opcodes.LUSHR),
InsnNode(Opcodes.L2F),
2f.const,
InsnNode(Opcodes.FMUL),
allDone
).push(Float::class.ref)
}
fun applyF64ConvertUI64(ctx: FuncContext, fn: Func): Func {
// l >= 0 ? (double) l : (((l >>> 1) | (l & 1)) * 2.0f)
val notPositive = LabelNode()
val allDone = LabelNode()
return fn.popExpecting(Long::class.ref).addInsns(
InsnNode(Opcodes.DUP2),
0L.const,
InsnNode(Opcodes.LCMP),
JumpInsnNode(Opcodes.IFLT, notPositive),
InsnNode(Opcodes.L2D),
JumpInsnNode(Opcodes.GOTO, allDone),
notPositive,
InsnNode(Opcodes.DUP2),
1.const,
InsnNode(Opcodes.LUSHR),
// Swap the shift result and the long on the stack
InsnNode(Opcodes.DUP2_X2), InsnNode(Opcodes.POP2),
1L.const,
InsnNode(Opcodes.LAND),
InsnNode(Opcodes.LOR),
InsnNode(Opcodes.L2D),
2.0.const,
InsnNode(Opcodes.DMUL),
allDone
).push(Double::class.ref)
}
fun applyI64TruncUF32(ctx: FuncContext, fn: Func) = LabelNode().let { underMax ->
LabelNode().let { allDone ->
// If over max long, subtract and negate
// (Really, it's (long) (-9223372036854775808f + (f - 9223372036854775807f))
fn.popExpecting(Float::class.ref).addInsns(
InsnNode(Opcodes.DUP), // [f, f]
9223372036854775807f.const, // [f, f, c]
InsnNode(Opcodes.FCMPL), // [f, z]
JumpInsnNode(Opcodes.IFLT, underMax), // [f]
9223372036854775807f.const, // [f, c]
InsnNode(Opcodes.FSUB),
(-9223372036854775808f).const,
InsnNode(Opcodes.FADD),
InsnNode(Opcodes.F2L),
JumpInsnNode(Opcodes.GOTO, allDone),
underMax,
InsnNode(Opcodes.F2L),
allDone
).push(Long::class.ref)
}
}
fun applyI64TruncUF64(ctx: FuncContext, fn: Func) = LabelNode().let { underMax ->
LabelNode().let { allDone ->
// If over max long, subtract and negate
fn.popExpecting(Double::class.ref).addInsns(
InsnNode(Opcodes.DUP2), // [f, f]
9223372036854775807.0.const, // [f, f, c]
InsnNode(Opcodes.DCMPL), // [f, z]
JumpInsnNode(Opcodes.IFLT, underMax), // [f]
9223372036854775807.0.const, // [f, c]
InsnNode(Opcodes.DSUB),
(-9223372036854775808.0).const,
InsnNode(Opcodes.DADD),
InsnNode(Opcodes.D2L),
JumpInsnNode(Opcodes.GOTO, allDone),
underMax,
InsnNode(Opcodes.D2L),
allDone
).push(Long::class.ref)
}
}
fun assertSignedIntegerDiv(ctx: FuncContext, fn: Func, type: TypeRef) =
if (!ctx.cls.checkSignedDivIntegerOverflow) fn
else if (type == Int::class.ref) fn.addInsns(InsnNode(Opcodes.DUP2), ctx.cls.divAssertI)
else fn.addInsns(
// Duping longs...ug
// TODO: is it really this worth it to avoid a local and make one tiny assertion call?
InsnNode(Opcodes.DUP2_X2),
InsnNode(Opcodes.POP2),
InsnNode(Opcodes.DUP2_X2),
InsnNode(Opcodes.DUP2_X2),
InsnNode(Opcodes.POP2),
InsnNode(Opcodes.DUP2_X2),
ctx.cls.divAssertL
)
fun assertTruncConv(ctx: FuncContext, fn: Func, from: TypeRef, to: TypeRef, signed: Boolean): Func {
if (!ctx.cls.checkTruncOverflow) return fn
if (from == Float::class.ref) {
if (to == Int::class.ref) return fn.addInsns(
InsnNode(Opcodes.DUP),
if (signed) ctx.cls.truncAssertF2SI else ctx.cls.truncAssertF2UI
) else if (to == Long::class.ref) return fn.addInsns(
InsnNode(Opcodes.DUP),
if (signed) ctx.cls.truncAssertF2SL else ctx.cls.truncAssertF2UL
)
} else if (from == Double::class.ref) {
if (to == Int::class.ref) return fn.addInsns(
InsnNode(Opcodes.DUP2),
if (signed) ctx.cls.truncAssertD2SI else ctx.cls.truncAssertD2UI
) else if (to == Long::class.ref) return fn.addInsns(
InsnNode(Opcodes.DUP2),
if (signed) ctx.cls.truncAssertD2SL else ctx.cls.truncAssertD2UL
)
}
return fn
}
fun applyConv(ctx: FuncContext, fn: Func, from: TypeRef, to: TypeRef, op: Int) =
applyConv(ctx, fn, from, to, InsnNode(op))
fun applyConv(ctx: FuncContext, fn: Func, from: TypeRef, to: TypeRef, insn: AbstractInsnNode) =
fn.popExpecting(from).addInsns(insn).push(to)
fun applyF64Trunc(ctx: FuncContext, fn: Func): Func {
// The best way for now is a comparison and jump to ceil or floor sadly
// So with it on the stack:
// dup2
// dconst 0
// dcmpg
// ifge label1
// Math::ceil
// goto label2
// label1: Math::floor
// label2
val label1 = LabelNode()
val label2 = LabelNode()
return fn.popExpecting(Double::class.ref).addInsns(
InsnNode(Opcodes.DUP2),
0.0.const,
InsnNode(Opcodes.DCMPG),
JumpInsnNode(Opcodes.IFGE, label1),
Math::ceil.invokeStatic(),
JumpInsnNode(Opcodes.GOTO, label2),
label1,
Math::floor.invokeStatic(),
label2
).push(Double::class.ref)
}
fun applyF32Trunc(ctx: FuncContext, fn: Func): Func {
// Do the same as applyF64Trunc but convert where needed
val label1 = LabelNode()
val label2 = LabelNode()
return fn.popExpecting(Float::class.ref).addInsns(
InsnNode(Opcodes.DUP),
0.0F.const,
InsnNode(Opcodes.FCMPG),
JumpInsnNode(Opcodes.IFGE, label1),
InsnNode(Opcodes.F2D),
Math::ceil.invokeStatic(),
JumpInsnNode(Opcodes.GOTO, label2),
label1,
InsnNode(Opcodes.F2D),
Math::floor.invokeStatic(),
label2,
InsnNode(Opcodes.D2F)
).push(Float::class.ref)
}
fun applyF64UnaryNanReturnPositive(ctx: FuncContext, fn: Func, cb: (Func) -> Func): Func {
if (!ctx.cls.accurateNanBits) return cb(fn)
val notNan = LabelNode()
val allDone = LabelNode()
return fn.addInsns(
InsnNode(Opcodes.DUP2), // [d, d]
InsnNode(Opcodes.DUP2), // [d, d, d]
// Equals compare to check nan
InsnNode(Opcodes.DCMPL), // [d, z]
JumpInsnNode(Opcodes.IFEQ, notNan), // [d]
InsnNode(Opcodes.DUP2), // [d, d]
Double::class.invokeStatic("doubleToRawLongBits", Long::class, Double::class), // [d, l]
0x7ff0000000000000.const, // [d, l, l]
InsnNode(Opcodes.LCMP), // [d, i]
JumpInsnNode(Opcodes.IFGE, allDone), // [d]
InsnNode(Opcodes.DNEG),
JumpInsnNode(Opcodes.GOTO, allDone),
notNan
).let(cb).addInsns(allDone)
}
fun applyF32UnaryNanReturnPositive(ctx: FuncContext, fn: Func, cb: (Func) -> Func): Func {
if (!ctx.cls.accurateNanBits) return cb(fn)
val notNan = LabelNode()
val allDone = LabelNode()
return fn.addInsns(
InsnNode(Opcodes.DUP), // [f, f]
InsnNode(Opcodes.DUP), // [f, f, f]
// Equals compare to check nan
InsnNode(Opcodes.FCMPL), // [f, z]
JumpInsnNode(Opcodes.IFEQ, notNan), // [f]
InsnNode(Opcodes.DUP), // [f, f]
Float::class.invokeStatic("floatToRawIntBits", Int::class, Float::class), // [f, i]
0x7f800000.const, // [f, i, i]
JumpInsnNode(Opcodes.IF_ICMPGE, allDone), // [f]
InsnNode(Opcodes.FNEG),
JumpInsnNode(Opcodes.GOTO, allDone),
notNan
).let(cb).addInsns(allDone)
}
fun applyF64UnaryNanReturnSame(ctx: FuncContext, fn: Func, cb: (Func) -> Func): Func {
if (!ctx.cls.accurateNanBits) return cb(fn)
val allDone = LabelNode()
return fn.addInsns(
InsnNode(Opcodes.DUP2), // [d, d]
InsnNode(Opcodes.DUP2), // [d, d, d]
// Equals compare to check nan
InsnNode(Opcodes.DCMPL), // [d, z]
JumpInsnNode(Opcodes.IFNE, allDone) // [d]
).let(cb).addInsns(allDone)
}
fun applyF32UnaryNanReturnSame(ctx: FuncContext, fn: Func, cb: (Func) -> Func): Func {
if (!ctx.cls.accurateNanBits) return cb(fn)
// Extra work for NaN, ref:
// http://stackoverflow.com/questions/43129365/javas-math-rint-not-behaving-as-expected-when-using-nan
val allDone = LabelNode()
return fn.addInsns(
InsnNode(Opcodes.DUP), // [f, f]
InsnNode(Opcodes.DUP), // [f, f, f]
// Equals compare to check nan
InsnNode(Opcodes.FCMPL), // [f, z]
JumpInsnNode(Opcodes.IFNE, allDone) // [f]
).let(cb).addInsns(allDone)
}
fun applyWithF32To64AndBack(ctx: FuncContext, fn: Func, f: (Func) -> Func) =
fn.popExpecting(Float::class.ref).
addInsns(InsnNode(Opcodes.F2D)).
push(Double::class.ref).let(f).
popExpecting(Double::class.ref).
addInsns(InsnNode(Opcodes.D2F)).
push(Float::class.ref)
fun applyF64Binary(ctx: FuncContext, fn: Func, op: Int) =
applyF64Binary(ctx, fn, InsnNode(op))
fun applyF64Binary(ctx: FuncContext, fn: Func, insn: AbstractInsnNode) =
applyBinary(ctx, fn, Double::class.ref, insn)
fun applyF32Binary(ctx: FuncContext, fn: Func, op: Int) =
applyF32Binary(ctx, fn, InsnNode(op))
fun applyF32Binary(ctx: FuncContext, fn: Func, insn: AbstractInsnNode) =
applyBinary(ctx, fn, Float::class.ref, insn)
fun applyI64BinarySecondOpI32(ctx: FuncContext, fn: Func, op: Int) =
applyI64BinarySecondOpI32(ctx, fn, InsnNode(op))
fun applyI64BinarySecondOpI32(ctx: FuncContext, fn: Func, insn: AbstractInsnNode) =
fn.popExpectingMulti(Long::class.ref, Long::class.ref).
addInsns(InsnNode(Opcodes.L2I), insn).push(Long::class.ref)
fun applyI64Binary(ctx: FuncContext, fn: Func, op: Int) =
applyI64Binary(ctx, fn, InsnNode(op))
fun applyI64Binary(ctx: FuncContext, fn: Func, insn: AbstractInsnNode) =
applyBinary(ctx, fn, Long::class.ref, insn)
fun applyI32Binary(ctx: FuncContext, fn: Func, op: Int) =
applyI32Binary(ctx, fn, InsnNode(op))
fun applyI32Binary(ctx: FuncContext, fn: Func, insn: AbstractInsnNode) =
applyBinary(ctx, fn, Int::class.ref, insn)
fun applyBinary(ctx: FuncContext, fn: Func, type: TypeRef, insn: AbstractInsnNode) =
fn.popExpectingMulti(type, type).addInsns(insn).push(type)
fun applyF64Unary(ctx: FuncContext, fn: Func, insn: AbstractInsnNode) =
applyUnary(ctx, fn, Double::class.ref, insn)
fun applyF32Unary(ctx: FuncContext, fn: Func, insn: AbstractInsnNode) =
applyUnary(ctx, fn, Float::class.ref, insn)
fun applyI64Unary(ctx: FuncContext, fn: Func, insn: AbstractInsnNode) =
applyUnary(ctx, fn, Long::class.ref, insn)
fun applyI32Unary(ctx: FuncContext, fn: Func, insn: AbstractInsnNode) =
applyUnary(ctx, fn, Int::class.ref, insn)
fun applyUnary(ctx: FuncContext, fn: Func, type: TypeRef, insn: AbstractInsnNode) =
fn.popExpecting(type).addInsns(insn).push(type)
fun applyF32Cmp(ctx: FuncContext, fn: Func, op: Int, nanIsOne: Boolean = true) =
// TODO: Can we shorten this and use the direct cmp result instead of IF<OP>?
fn.popExpecting(Float::class.ref).
popExpecting(Float::class.ref).
addInsns(InsnNode(if (nanIsOne) Opcodes.FCMPG else Opcodes.FCMPL)).
push(Int::class.ref).
let { fn -> applyI32UnaryCmp(ctx, fn, op) }
fun applyF64Cmp(ctx: FuncContext, fn: Func, op: Int, nanIsOne: Boolean = true) =
fn.popExpecting(Double::class.ref).
popExpecting(Double::class.ref).
addInsns(InsnNode(if (nanIsOne) Opcodes.DCMPG else Opcodes.DCMPL)).
push(Int::class.ref).
let { fn -> applyI32UnaryCmp(ctx, fn, op) }