-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathBCodeBodyBuilder.scala
1844 lines (1599 loc) · 74.3 KB
/
BCodeBodyBuilder.scala
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 dotty.tools
package backend
package jvm
import scala.language.unsafeNulls
import scala.annotation.{switch, tailrec}
import scala.collection.mutable.SortedMap
import scala.tools.asm
import scala.tools.asm.{Handle, Opcodes}
import BCodeHelpers.InvokeStyle
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.CompilationUnit
import dotty.tools.dotc.core.Constants._
import dotty.tools.dotc.core.Flags.{Label => LabelFlag, _}
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.core.StdNames.{nme, str}
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.transform.Erasure
import dotty.tools.dotc.transform.SymUtils._
import dotty.tools.dotc.util.Spans._
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases._
import dotty.tools.dotc.core.Decorators.em
import dotty.tools.dotc.report
/*
*
* @author Miguel Garcia, http://lamp.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/
* @version 1.0
*
*/
trait BCodeBodyBuilder extends BCodeSkelBuilder {
// import global._
// import definitions._
import tpd._
import int.{_, given}
import DottyBackendInterface.symExtensions
import bTypes._
import coreBTypes._
protected val primitives: DottyPrimitives
/*
* Functionality to build the body of ASM MethodNode, except for `synchronized` and `try` expressions.
*/
abstract class PlainBodyBuilder(cunit: CompilationUnit) extends PlainSkelBuilder(cunit) {
import Primitives.TestOp
/* ---------------- helper utils for generating methods and code ---------------- */
def emit(opc: Int): Unit = { mnode.visitInsn(opc) }
def emitZeroOf(tk: BType): Unit = {
tk match {
case BOOL => bc.boolconst(false)
case BYTE |
SHORT |
CHAR |
INT => bc.iconst(0)
case LONG => bc.lconst(0)
case FLOAT => bc.fconst(0)
case DOUBLE => bc.dconst(0)
case UNIT => ()
case _ => emit(asm.Opcodes.ACONST_NULL)
}
}
/*
* Emits code that adds nothing to the operand stack.
* Two main cases: `tree` is an assignment,
* otherwise an `adapt()` to UNIT is performed if needed.
*/
def genStat(tree: Tree): Unit = {
lineNumber(tree)
tree match {
case Assign(lhs @ DesugaredSelect(qual, _), rhs) =>
val savedStackSize = stack.recordSize()
val isStatic = lhs.symbol.isStaticMember
if (!isStatic) {
val qualTK = genLoad(qual)
stack.push(qualTK)
}
genLoad(rhs, symInfoTK(lhs.symbol))
stack.restoreSize(savedStackSize)
lineNumber(tree)
// receiverClass is used in the bytecode to access the field. using sym.owner may lead to IllegalAccessError
val receiverClass = qual.tpe.typeSymbol
fieldStore(lhs.symbol, receiverClass)
case Assign(lhs, rhs) =>
val s = lhs.symbol
val Local(tk, _, idx, _) = locals.getOrMakeLocal(s)
rhs match {
case Apply(Select(larg: Ident, nme.ADD), Literal(x) :: Nil)
if larg.symbol == s && tk.isIntSizedType && x.isShortRange =>
lineNumber(tree)
bc.iinc(idx, x.intValue)
case Apply(Select(larg: Ident, nme.SUB), Literal(x) :: Nil)
if larg.symbol == s && tk.isIntSizedType && Constant(-x.intValue).isShortRange =>
lineNumber(tree)
bc.iinc(idx, -x.intValue)
case _ =>
genLoad(rhs, tk)
lineNumber(tree)
bc.store(idx, tk)
}
case _ =>
genLoad(tree, UNIT)
}
}
/* Generate code for primitive arithmetic operations. */
def genArithmeticOp(tree: Tree, code: Int): BType = tree match{
case Apply(fun @ DesugaredSelect(larg, _), args) =>
var resKind = tpeTK(larg)
assert(resKind.isNumericType || (resKind == BOOL),
s"$resKind is not a numeric or boolean type [operation: ${fun.symbol}]")
import ScalaPrimitivesOps._
args match {
// unary operation
case Nil =>
genLoad(larg, resKind)
code match {
case POS => () // nothing
case NEG => bc.neg(resKind)
case NOT => bc.genPrimitiveArithmetic(Primitives.NOT, resKind)
case _ => abort(s"Unknown unary operation: ${fun.symbol.showFullName} code: $code")
}
// binary operation
case rarg :: Nil =>
val isShift = isShiftOp(code)
resKind = tpeTK(larg).maxType(if (isShift) INT else tpeTK(rarg))
if (isShift || isBitwiseOp(code)) {
assert(resKind.isIntegralType || (resKind == BOOL),
s"$resKind incompatible with arithmetic modulo operation.")
}
genLoad(larg, resKind)
stack.push(resKind)
genLoad(rarg, if (isShift) INT else resKind)
stack.pop()
(code: @switch) match {
case ADD => bc add resKind
case SUB => bc sub resKind
case MUL => bc mul resKind
case DIV => bc div resKind
case MOD => bc rem resKind
case OR | XOR | AND => bc.genPrimitiveLogical(code, resKind)
case LSL | LSR | ASR => bc.genPrimitiveShift(code, resKind)
case _ => abort(s"Unknown primitive: ${fun.symbol}[$code]")
}
case _ =>
abort(s"Too many arguments for primitive function: $tree")
}
lineNumber(tree)
resKind
}
/* Generate primitive array operations. */
def genArrayOp(tree: Tree, code: Int, expectedType: BType): BType = tree match{
case Apply(DesugaredSelect(arrayObj, _), args) =>
import ScalaPrimitivesOps._
val k = tpeTK(arrayObj)
genLoad(arrayObj, k)
val elementType = typeOfArrayOp.getOrElse[bTypes.BType](code, abort(s"Unknown operation on arrays: $tree code: $code"))
var generatedType = expectedType
if (isArrayGet(code)) {
// load argument on stack
assert(args.length == 1, s"Too many arguments for array get operation: $tree");
stack.push(k)
genLoad(args.head, INT)
stack.pop()
generatedType = k.asArrayBType.componentType
bc.aload(elementType)
}
else if (isArraySet(code)) {
val List(a1, a2) = args
stack.push(k)
genLoad(a1, INT)
stack.push(INT)
genLoad(a2)
stack.pop(2)
generatedType = UNIT
bc.astore(elementType)
} else {
generatedType = INT
emit(asm.Opcodes.ARRAYLENGTH)
}
lineNumber(tree)
generatedType
}
def genLoadIfTo(tree: If, expectedType: BType, dest: LoadDestination): BType = tree match{
case If(condp, thenp, elsep) =>
val success = new asm.Label
val failure = new asm.Label
val hasElse = !elsep.isEmpty && (elsep match {
case Literal(value) if value.tag == UnitTag => false
case _ => true
})
genCond(condp, success, failure, targetIfNoJump = success)
markProgramPoint(success)
if dest == LoadDestination.FallThrough then
if hasElse then
val thenKind = tpeTK(thenp)
val elseKind = tpeTK(elsep)
def hasUnitBranch = (thenKind == UNIT || elseKind == UNIT) && expectedType == UNIT
val resKind = if (hasUnitBranch) UNIT else tpeTK(tree)
val postIf = new asm.Label
genLoadTo(thenp, resKind, LoadDestination.Jump(postIf, stack.recordSize()))
markProgramPoint(failure)
genLoadTo(elsep, resKind, LoadDestination.FallThrough)
markProgramPoint(postIf)
resKind
else
genLoad(thenp, UNIT)
markProgramPoint(failure)
UNIT
end if
else
genLoadTo(thenp, expectedType, dest)
markProgramPoint(failure)
if hasElse then
genLoadTo(elsep, expectedType, dest)
else
genAdaptAndSendToDest(UNIT, expectedType, dest)
expectedType
end if
}
def genPrimitiveOp(tree: Apply, expectedType: BType): BType = (tree: @unchecked) match {
case Apply(fun @ DesugaredSelect(receiver, _), _) =>
val sym = tree.symbol
val code = primitives.getPrimitive(tree, receiver.tpe)
import ScalaPrimitivesOps._
if (isArithmeticOp(code)) genArithmeticOp(tree, code)
else if (code == CONCAT) genStringConcat(tree)
else if (code == HASH) genScalaHash(receiver)
else if (isArrayOp(code)) genArrayOp(tree, code, expectedType)
else if (isLogicalOp(code) || isComparisonOp(code)) {
val success, failure, after = new asm.Label
genCond(tree, success, failure, targetIfNoJump = success)
// success block
markProgramPoint(success)
bc boolconst true
bc goTo after
// failure block
markProgramPoint(failure)
bc boolconst false
// after
markProgramPoint(after)
BOOL
}
else if (isCoercion(code)) {
genLoad(receiver)
lineNumber(tree)
genCoercion(code)
coercionTo(code)
}
else abort(
s"Primitive operation not handled yet: ${sym.showFullName}(${fun.symbol.name}) at: ${tree.span}"
)
}
def genLoad(tree: Tree): BType = {
val generatedType = tpeTK(tree)
genLoad(tree, generatedType)
generatedType
}
/* Generate code for trees that produce values on the stack */
def genLoad(tree: Tree, expectedType: BType): Unit =
genLoadTo(tree, expectedType, LoadDestination.FallThrough)
/* Generate code for trees that produce values, sent to a given `LoadDestination`. */
def genLoadTo(tree: Tree, expectedType: BType, dest: LoadDestination): Unit =
var generatedType = expectedType
var generatedDest = LoadDestination.FallThrough
lineNumber(tree)
tree match {
case tree@ValDef(_, _, _) =>
val sym = tree.symbol
/* most of the time, !locals.contains(sym), unless the current activation of genLoad() is being called
while duplicating a finalizer that contains this ValDef. */
val loc = locals.getOrMakeLocal(sym)
val Local(tk, _, idx, isSynth) = loc
if (tree.rhs == tpd.EmptyTree) { emitZeroOf(tk) }
else { genLoad(tree.rhs, tk) }
bc.store(idx, tk)
val localVarStart = currProgramPoint()
if (!isSynth) { // there are case <synthetic> ValDef's emitted by patmat
varsInScope ::= (sym -> localVarStart)
}
generatedType = UNIT
case t @ If(_, _, _) =>
generatedType = genLoadIfTo(t, expectedType, dest)
generatedDest = dest
case t @ Labeled(_, _) =>
generatedType = genLabeledTo(t, expectedType, dest)
generatedDest = dest
case r: Return =>
genReturn(r)
generatedDest = LoadDestination.Return
case t @ WhileDo(_, _) =>
generatedDest = genWhileDo(t)
generatedType = UNIT
case t @ Try(_, _, _) =>
generatedType = genLoadTry(t)
case t: Apply if t.fun.symbol eq defn.throwMethod =>
val thrownExpr = t.args.head
val thrownKind = tpeTK(thrownExpr)
genLoadTo(thrownExpr, thrownKind, LoadDestination.Throw)
generatedDest = LoadDestination.Throw
case New(tpt) =>
abort(s"Unexpected New(${tpt.tpe.showSummary()}/$tpt) reached GenBCode.\n" +
" Call was genLoad" + ((tree, expectedType)))
case t @ Closure(env, call, tpt) =>
val functionalInterface: Symbol =
if !tpt.isEmpty then tpt.tpe.classSymbol
else t.tpe.classSymbol
val (fun, args) = call match {
case Apply(fun, args) => (fun, args)
case t @ DesugaredSelect(_, _) => (t, Nil) // TODO: use Select
case t @ Ident(_) => (t, Nil)
}
val savedStackSize = stack.recordSize()
if (!fun.symbol.isStaticMember) {
// load receiver of non-static implementation of lambda
// darkdimius: I haven't found in spec `this` reference should go
// but I was able to derrive it by reading
// AbstractValidatingLambdaMetafactory.validateMetafactoryArgs
val DesugaredSelect(prefix, _) = fun: @unchecked
val prefixTK = genLoad(prefix)
stack.push(prefixTK)
}
genLoadArguments(env, fun.symbol.info.firstParamTypes map toTypeKind)
stack.restoreSize(savedStackSize)
generatedType = genInvokeDynamicLambda(NoSymbol, fun.symbol, env.size, functionalInterface)
case app @ Apply(_, _) =>
generatedType = genApply(app, expectedType)
case This(qual) =>
val symIsModuleClass = tree.symbol.is(ModuleClass)
assert(tree.symbol == claszSymbol || symIsModuleClass,
s"Trying to access the this of another class: tree.symbol = ${tree.symbol}, class symbol = $claszSymbol compilation unit: $cunit")
if (symIsModuleClass && tree.symbol != claszSymbol) {
generatedType = genLoadModule(tree)
}
else {
mnode.visitVarInsn(asm.Opcodes.ALOAD, 0)
// When compiling Array.scala, the constructor invokes `Array.this.super.<init>`. The expectedType
// is `[Object` (computed by typeToBType, the type of This(Array) is `Array[T]`). If we would set
// the generatedType to `Array` below, the call to adapt at the end would fail. The situation is
// similar for primitives (`I` vs `Int`).
if (tree.symbol != defn.ArrayClass && !tree.symbol.isPrimitiveValueClass) {
generatedType = classBTypeFromSymbol(claszSymbol)
}
}
case DesugaredSelect(Ident(nme.EMPTY_PACKAGE), module) =>
assert(tree.symbol.is(Module), s"Selection of non-module from empty package: $tree sym: ${tree.symbol} at: ${tree.span}")
genLoadModule(tree)
case DesugaredSelect(qualifier, _) =>
val sym = tree.symbol
generatedType = symInfoTK(sym)
val qualSafeToElide = tpd.isIdempotentExpr(qualifier)
def genLoadQualUnlessElidable(): Unit = { if (!qualSafeToElide) { genLoadQualifier(tree) } }
// receiverClass is used in the bytecode to access the field. using sym.owner may lead to IllegalAccessError
def receiverClass = qualifier.tpe.typeSymbol
if (sym.is(Module)) {
genLoadQualUnlessElidable()
genLoadModule(tree)
} else if (sym.isStaticMember) {
genLoadQualUnlessElidable()
fieldLoad(sym, receiverClass)
} else {
genLoadQualifier(tree)
fieldLoad(sym, receiverClass)
}
case t @ Ident(name) =>
val sym = tree.symbol
val tk = symInfoTK(sym)
generatedType = tk
val desugared = cachedDesugarIdent(t)
desugared match {
case None =>
if (!sym.is(Package)) {
if (sym.is(Module)) genLoadModule(sym)
else locals.load(sym)
}
case Some(t) =>
genLoad(t, generatedType)
}
case Literal(value) =>
if (value.tag != UnitTag) (value.tag, expectedType) match {
case (IntTag, LONG ) => bc.lconst(value.longValue); generatedType = LONG
case (FloatTag, DOUBLE) => bc.dconst(value.doubleValue); generatedType = DOUBLE
case (NullTag, _ ) => bc.emit(asm.Opcodes.ACONST_NULL); generatedType = srNullRef
case _ => genConstant(value); generatedType = tpeTK(tree)
}
case blck @ Block(stats, expr) =>
if(stats.isEmpty)
genLoadTo(expr, expectedType, dest)
else
genBlockTo(blck, expectedType, dest)
generatedDest = dest
case Typed(Super(_, _), _) =>
genLoadTo(tpd.This(claszSymbol.asClass), expectedType, dest)
generatedDest = dest
case Typed(expr, _) =>
genLoadTo(expr, expectedType, dest)
generatedDest = dest
case Assign(_, _) =>
generatedType = UNIT
genStat(tree)
case av @ ArrayValue(_, _) =>
generatedType = genArrayValue(av)
case mtch @ Match(_, _) =>
generatedType = genMatchTo(mtch, expectedType, dest)
generatedDest = dest
case tpd.EmptyTree => if (expectedType != UNIT) { emitZeroOf(expectedType) }
case t: TypeApply => // dotty specific
generatedType = genTypeApply(t)
case _ => abort(s"Unexpected tree in genLoad: $tree/${tree.getClass} at: ${tree.span}")
}
// emit conversion and send to the right destination
if generatedDest == LoadDestination.FallThrough then
genAdaptAndSendToDest(generatedType, expectedType, dest)
end genLoadTo
def genAdaptAndSendToDest(generatedType: BType, expectedType: BType, dest: LoadDestination): Unit =
if generatedType != expectedType then
adapt(generatedType, expectedType)
dest match
case LoadDestination.FallThrough =>
()
case LoadDestination.Jump(label, targetStackSize) =>
val stackDiff = stack.heightDiffWrt(targetStackSize)
if stackDiff != 0 then
if expectedType == UNIT then
bc dropMany stackDiff
else
val loc = locals.makeTempLocal(expectedType)
bc.store(loc.idx, expectedType)
bc dropMany stackDiff
bc.load(loc.idx, expectedType)
end if
bc goTo label
case LoadDestination.Return =>
bc emitRETURN returnType
case LoadDestination.Throw =>
val thrownType = expectedType
// `throw null` is valid although scala.Null (as defined in src/libray-aux) isn't a subtype of Throwable.
// Similarly for scala.Nothing (again, as defined in src/libray-aux).
assert(thrownType.isNullType || thrownType.isNothingType || thrownType.asClassBType.isSubtypeOf(jlThrowableRef))
emit(asm.Opcodes.ATHROW)
end genAdaptAndSendToDest
// ---------------- field load and store ----------------
/*
* must-single-thread
*/
def fieldLoad( field: Symbol, hostClass: Symbol = null): Unit = fieldOp(field, isLoad = true, hostClass)
/*
* must-single-thread
*/
def fieldStore(field: Symbol, hostClass: Symbol = null): Unit = fieldOp(field, isLoad = false, hostClass)
/*
* must-single-thread
*/
private def fieldOp(field: Symbol, isLoad: Boolean, specificReceiver: Symbol): Unit = {
val useSpecificReceiver = specificReceiver != null && !field.isScalaStatic
val owner = internalName(if (useSpecificReceiver) specificReceiver else field.owner)
val fieldJName = field.javaSimpleName
val fieldDescr = symInfoTK(field).descriptor
val isStatic = field.isStaticMember
val opc =
if (isLoad) { if (isStatic) asm.Opcodes.GETSTATIC else asm.Opcodes.GETFIELD }
else { if (isStatic) asm.Opcodes.PUTSTATIC else asm.Opcodes.PUTFIELD }
mnode.visitFieldInsn(opc, owner, fieldJName, fieldDescr)
}
// ---------------- emitting constant values ----------------
/*
* For ClazzTag:
* must-single-thread
* Otherwise it's safe to call from multiple threads.
*/
def genConstant(const: Constant): Unit = {
(const.tag/*: @switch*/) match {
case BooleanTag => bc.boolconst(const.booleanValue)
case ByteTag => bc.iconst(const.byteValue)
case ShortTag => bc.iconst(const.shortValue)
case CharTag => bc.iconst(const.charValue)
case IntTag => bc.iconst(const.intValue)
case LongTag => bc.lconst(const.longValue)
case FloatTag => bc.fconst(const.floatValue)
case DoubleTag => bc.dconst(const.doubleValue)
case UnitTag => ()
case StringTag =>
assert(const.value != null, const) // TODO this invariant isn't documented in `case class Constant`
mnode.visitLdcInsn(const.stringValue) // `stringValue` special-cases null, but not for a const with StringTag
case NullTag => emit(asm.Opcodes.ACONST_NULL)
case ClazzTag =>
val tp = toTypeKind(const.typeValue)
if tp.isPrimitive then
val boxedClass = boxedClassOfPrimitive(tp.asPrimitiveBType)
mnode.visitFieldInsn(
asm.Opcodes.GETSTATIC,
boxedClass.internalName,
"TYPE", // field name
jlClassRef.descriptor
)
else
mnode.visitLdcInsn(tp.toASMType)
case _ => abort(s"Unknown constant value: $const")
}
}
private def genLabeledTo(tree: Labeled, expectedType: BType, dest: LoadDestination): BType = tree match {
case Labeled(bind, expr) =>
val labelSym = bind.symbol
if dest == LoadDestination.FallThrough then
val resKind = tpeTK(tree)
val jumpTarget = new asm.Label
registerJumpDest(labelSym, resKind, LoadDestination.Jump(jumpTarget, stack.recordSize()))
genLoad(expr, resKind)
markProgramPoint(jumpTarget)
resKind
else
registerJumpDest(labelSym, expectedType, dest)
genLoadTo(expr, expectedType, dest)
expectedType
end if
}
private def genReturn(r: Return): Unit = {
val expr: Tree = r.expr
val fromSym: Symbol = if (r.from.symbol.is(LabelFlag)) r.from.symbol else NoSymbol
if (NoSymbol == fromSym) {
// return from enclosing method
cleanups match {
case Nil =>
// not an assertion: !shouldEmitCleanup (at least not yet, pendingCleanups() may still have to run, and reset `shouldEmitCleanup`.
genLoadTo(expr, returnType, LoadDestination.Return)
case nextCleanup :: rest =>
genLoad(expr, returnType)
lineNumber(r)
val saveReturnValue = (returnType != UNIT)
if (saveReturnValue) {
// regarding return value, the protocol is: in place of a `return-stmt`, a sequence of `adapt, store, jump` are inserted.
if (earlyReturnVar == null) {
earlyReturnVar = locals.makeLocal(returnType, "earlyReturnVar", expr.tpe, expr.span)
}
locals.store(earlyReturnVar)
}
bc goTo nextCleanup
shouldEmitCleanup = true
}
} else {
// return from labeled
assert(fromSym.is(LabelFlag), fromSym)
assert(!fromSym.is(Method), fromSym)
/* TODO At the moment, we disregard cleanups, because by construction we don't have return-from-labels
* that cross cleanup boundaries. However, in theory such crossings are valid, so we should take care
* of them.
*/
val (exprExpectedType, exprDest) = findJumpDest(fromSym)
genLoadTo(expr, exprExpectedType, exprDest)
}
} // end of genReturn()
def genWhileDo(tree: WhileDo): LoadDestination = tree match{
case WhileDo(cond, body) =>
val isInfinite = cond == tpd.EmptyTree
val loop = new asm.Label
markProgramPoint(loop)
if isInfinite then
val dest = LoadDestination.Jump(loop, stack.recordSize())
genLoadTo(body, UNIT, dest)
dest
else
body match
case Literal(value) if value.tag == UnitTag =>
// this is the shape of do..while loops
val exitLoop = new asm.Label
genCond(cond, loop, exitLoop, targetIfNoJump = exitLoop)
markProgramPoint(exitLoop)
case _ =>
val success = new asm.Label
val failure = new asm.Label
genCond(cond, success, failure, targetIfNoJump = success)
markProgramPoint(success)
genLoadTo(body, UNIT, LoadDestination.Jump(loop, stack.recordSize()))
markProgramPoint(failure)
end match
LoadDestination.FallThrough
}
def genTypeApply(t: TypeApply): BType = (t: @unchecked) match {
case TypeApply(fun@DesugaredSelect(obj, _), targs) =>
val sym = fun.symbol
val cast =
if (sym == defn.Any_isInstanceOf) false
else if (sym == defn.Any_asInstanceOf) true
else abort(s"Unexpected type application $fun[sym: ${sym.showFullName}] in: $t")
val l = tpeTK(obj)
val r = tpeTK(targs.head)
genLoadQualifier(fun)
// TODO @lry make pattern match
if (l.isPrimitive && r.isPrimitive)
genConversion(l, r, cast)
else if (l.isPrimitive) {
bc drop l
if (cast) {
mnode.visitTypeInsn(asm.Opcodes.NEW, jlClassCastExceptionRef.internalName)
bc dup ObjectRef
emit(asm.Opcodes.ATHROW)
} else {
bc boolconst false
}
}
else if (r.isPrimitive && cast) {
abort(s"Erasure should have added an unboxing operation to prevent this cast. Tree: $t")
}
else if (r.isPrimitive) {
bc isInstance boxedClassOfPrimitive(r.asPrimitiveBType)
}
else {
assert(r.isRef, r) // ensure that it's not a method
genCast(r.asRefBType, cast)
}
if (cast) r else BOOL
} // end of genTypeApply()
private def mkArrayConstructorCall(arr: ArrayBType, app: Apply, args: List[Tree]) = {
val dims = arr.dimension
var elemKind = arr.elementType
val argsSize = args.length
if (argsSize > dims) {
report.error(em"too many arguments for array constructor: found ${args.length} but array has only $dims dimension(s)", ctx.source.atSpan(app.span))
}
if (argsSize < dims) {
/* In one step:
* elemKind = new BType(BType.ARRAY, arr.off + argsSize, arr.len - argsSize)
* however the above does not enter a TypeName for each nested arrays in chrs.
*/
for (i <- args.length until dims) elemKind = ArrayBType(elemKind)
}
genLoadArguments(args, List.fill(args.size)(INT))
(argsSize /*: @switch*/) match {
case 1 => bc newarray elemKind
case _ =>
val descr = ("[" * argsSize) + elemKind.descriptor // denotes the same as: arrayN(elemKind, argsSize).descriptor
mnode.visitMultiANewArrayInsn(descr, argsSize)
}
}
private def genApply(app: Apply, expectedType: BType): BType = {
var generatedType = expectedType
lineNumber(app)
app match {
case Apply(_, args) if app.symbol eq defn.newArrayMethod =>
val List(elemClaz, Literal(c: Constant), ArrayValue(_, dims)) = args: @unchecked
generatedType = toTypeKind(c.typeValue)
mkArrayConstructorCall(generatedType.asArrayBType, app, dims)
case Apply(t :TypeApply, _) =>
generatedType =
if (t.symbol ne defn.Object_synchronized) genTypeApply(t)
else genSynchronized(app, expectedType)
case Apply(fun @ DesugaredSelect(Super(superQual, _), _), args) =>
// 'super' call: Note: since constructors are supposed to
// return an instance of what they construct, we have to take
// special care. On JVM they are 'void', and Scala forbids (syntactically)
// to call super constructors explicitly and/or use their 'returned' value.
// therefore, we can ignore this fact, and generate code that leaves nothing
// on the stack (contrary to what the type in the AST says).
// scala/bug#10290: qual can be `this.$outer()` (not just `this`), so we call genLoad (not just ALOAD_0)
val superQualTK = genLoad(superQual)
stack.push(superQualTK)
genLoadArguments(args, paramTKs(app))
stack.pop()
generatedType = genCallMethod(fun.symbol, InvokeStyle.Super, app.span)
// 'new' constructor call: Note: since constructors are
// thought to return an instance of what they construct,
// we have to 'simulate' it by DUPlicating the freshly created
// instance (on JVM, <init> methods return VOID).
case Apply(fun @ DesugaredSelect(New(tpt), nme.CONSTRUCTOR), args) =>
val ctor = fun.symbol
assert(ctor.isClassConstructor, s"'new' call to non-constructor: ${ctor.name}")
generatedType = toTypeKind(tpt.tpe)
assert(generatedType.isRef, s"Non reference type cannot be instantiated: $generatedType")
generatedType match {
case arr: ArrayBType =>
mkArrayConstructorCall(arr, app, args)
case rt: ClassBType =>
assert(classBTypeFromSymbol(ctor.owner) == rt, s"Symbol ${ctor.owner.showFullName} is different from $rt")
mnode.visitTypeInsn(asm.Opcodes.NEW, rt.internalName)
bc dup generatedType
stack.push(rt)
stack.push(rt)
genLoadArguments(args, paramTKs(app))
stack.pop(2)
genCallMethod(ctor, InvokeStyle.Special, app.span)
case _ =>
abort(s"Cannot instantiate $tpt of kind: $generatedType")
}
case Apply(fun, List(expr)) if Erasure.Boxing.isBox(fun.symbol) && fun.symbol.denot.owner != defn.UnitModuleClass =>
val nativeKind = tpeTK(expr)
genLoad(expr, nativeKind)
val MethodNameAndType(mname, methodType) = asmBoxTo(nativeKind)
bc.invokestatic(srBoxesRuntimeRef.internalName, mname, methodType.descriptor, itf = false)
generatedType = boxResultType(fun.symbol) // was toTypeKind(fun.symbol.tpe.resultType)
case Apply(fun, List(expr)) if Erasure.Boxing.isUnbox(fun.symbol) && fun.symbol.denot.owner != defn.UnitModuleClass =>
genLoad(expr)
val boxType = unboxResultType(fun.symbol) // was toTypeKind(fun.symbol.owner.linkedClassOfClass.tpe)
generatedType = boxType
val MethodNameAndType(mname, methodType) = asmUnboxTo(boxType)
bc.invokestatic(srBoxesRuntimeRef.internalName, mname, methodType.descriptor, itf = false)
case app @ Apply(fun, args) =>
val sym = fun.symbol
if (isPrimitive(fun)) { // primitive method call
generatedType = genPrimitiveOp(app, expectedType)
} else { // normal method call
val invokeStyle =
if (sym.isStaticMember) InvokeStyle.Static
else if (sym.is(Private) || sym.isClassConstructor) InvokeStyle.Special
else if (app.hasAttachment(BCodeHelpers.UseInvokeSpecial)) InvokeStyle.Special
else InvokeStyle.Virtual
val savedStackSize = stack.recordSize()
if invokeStyle.hasInstance then
stack.push(genLoadQualifier(fun))
genLoadArguments(args, paramTKs(app))
stack.restoreSize(savedStackSize)
val DesugaredSelect(qual, name) = fun: @unchecked // fun is a Select, also checked in genLoadQualifier
val isArrayClone = name == nme.clone_ && qual.tpe.widen.isInstanceOf[JavaArrayType]
if (isArrayClone) {
// Special-case Array.clone, introduced in 36ef60e. The goal is to generate this call
// as "[I.clone" instead of "java/lang/Object.clone". This is consistent with javac.
// Arrays have a public method `clone` (jls 10.7).
//
// The JVMS is not explicit about this, but that receiver type can be an array type
// descriptor (instead of a class internal name):
// invokevirtual #2; //Method "[I".clone:()Ljava/lang/Object
//
// Note that using `Object.clone()` would work as well, but only because the JVM
// relaxes protected access specifically if the receiver is an array:
// http://hg.openjdk.java.net/jdk8/jdk8/hotspot/file/87ee5ee27509/src/share/vm/interpreter/linkResolver.cpp#l439
// Example: `class C { override def clone(): Object = "hi" }`
// Emitting `def f(c: C) = c.clone()` as `Object.clone()` gives a VerifyError.
val target: String = tpeTK(qual).asRefBType.classOrArrayType
val methodBType = asmMethodType(sym)
bc.invokevirtual(target, sym.javaSimpleName, methodBType.descriptor)
generatedType = methodBType.returnType
} else {
val receiverClass = if (!invokeStyle.isVirtual) null else {
// receiverClass is used in the bytecode to as the method receiver. using sym.owner
// may lead to IllegalAccessErrors, see 9954eaf / aladdin bug 455.
val qualSym = qual.tpe.typeSymbol
if (qualSym == defn.ArrayClass) {
// For invocations like `Array(1).hashCode` or `.wait()`, use Object as receiver
// in the bytecode. Using the array descriptor (like we do for clone above) seems
// to work as well, but it seems safer not to change this. Javac also uses Object.
// Note that array apply/update/length are handled by isPrimitive (above).
assert(sym.owner == defn.ObjectClass, s"unexpected array call: $app")
defn.ObjectClass
} else qualSym
}
generatedType = genCallMethod(sym, invokeStyle, app.span, receiverClass)
}
}
}
generatedType
} // end of genApply()
private def genArrayValue(av: tpd.JavaSeqLiteral): BType = {
val ArrayValue(tpt, elems) = av: @unchecked
lineNumber(av)
genArray(elems, tpt)
}
private def genArray(elems: List[Tree], elemType: Type): BType = {
val elmKind = toTypeKind(elemType)
val generatedType = ArrayBType(elmKind)
bc iconst elems.length
bc newarray elmKind
// during the genLoad below, there is the result, its dup, and the index
stack.push(generatedType)
stack.push(generatedType)
stack.push(INT)
var i = 0
var rest = elems
while (!rest.isEmpty) {
bc dup generatedType
bc iconst i
genLoad(rest.head, elmKind)
bc astore elmKind
rest = rest.tail
i = i + 1
}
stack.pop(3)
generatedType
}
/* A Match node contains one or more case clauses, each case clause lists one or more
* Int/String values to use as keys, and a code block. The exception is the "default" case
* clause which doesn't list any key (there is exactly one of these per match).
*/
private def genMatchTo(tree: Match, expectedType: BType, dest: LoadDestination): BType = tree match {
case Match(selector, cases) =>
lineNumber(tree)
val (generatedType, postMatch, postMatchDest) =
if dest == LoadDestination.FallThrough then
val postMatch = new asm.Label
(tpeTK(tree), postMatch, LoadDestination.Jump(postMatch, stack.recordSize()))
else
(expectedType, null, dest)
// Only two possible selector types exist in `Match` trees at this point: Int and String
if (tpeTK(selector) == INT) {
/* On a first pass over the case clauses, we flatten the keys and their
* targets (the latter represented with asm.Labels). That representation
* allows JCodeMethodV to emit a lookupswitch or a tableswitch.
*
* On a second pass, we emit the switch blocks, one for each different target.
*/
var flatKeys: List[Int] = Nil
var targets: List[asm.Label] = Nil
var default: asm.Label = null
var switchBlocks: List[(asm.Label, Tree)] = Nil
genLoad(selector, INT)
// collect switch blocks and their keys, but don't emit yet any switch-block.
for (caze @ CaseDef(pat, guard, body) <- cases) {
assert(guard == tpd.EmptyTree, guard)
val switchBlockPoint = new asm.Label
switchBlocks ::= (switchBlockPoint, body)
pat match {
case Literal(value) =>
flatKeys ::= value.intValue
targets ::= switchBlockPoint
case Ident(nme.WILDCARD) =>
assert(default == null, s"multiple default targets in a Match node, at ${tree.span}")
default = switchBlockPoint
case Alternative(alts) =>
alts foreach {
case Literal(value) =>
flatKeys ::= value.intValue
targets ::= switchBlockPoint
case _ =>
abort(s"Invalid alternative in alternative pattern in Match node: $tree at: ${tree.span}")
}
case _ =>
abort(s"Invalid pattern in Match node: $tree at: ${tree.span}")
}
}
bc.emitSWITCH(mkArrayReverse(flatKeys), mkArrayL(targets.reverse), default, MIN_SWITCH_DENSITY)
// emit switch-blocks.
for (sb <- switchBlocks.reverse) {
val (caseLabel, caseBody) = sb
markProgramPoint(caseLabel)
genLoadTo(caseBody, generatedType, postMatchDest)
}
} else {
/* Since the JVM doesn't have a way to switch on a string, we switch
* on the `hashCode` of the string then do an `equals` check (with a
* possible second set of jumps if blocks can be reach from multiple
* string alternatives).
*
* This mirrors the way that Java compiles `switch` on Strings.
*/
var default: asm.Label = null
var indirectBlocks: List[(asm.Label, Tree)] = Nil
// Cases grouped by their hashCode
val casesByHash = SortedMap.empty[Int, List[(String, Either[asm.Label, Tree])]]
var caseFallback: Tree = null
for (caze @ CaseDef(pat, guard, body) <- cases) {