@@ -351,11 +351,25 @@ static PrimitiveNativeWrapper doIntegerSmall(@SuppressWarnings("unused") CExtCon
351
351
return PrimitiveNativeWrapper .createInt (i );
352
352
}
353
353
354
+ /**
355
+ * Generic integer conversion.<br/>
356
+ * In the interpreter: we just lookup the cached primitive native wrapper and use this
357
+ * instance. In compiled code, we do the same but create a fresh copy such that the compiler
358
+ * always sees a fresh instance. This avoids a phi and certainly a real allocation. Note: it
359
+ * is important to copy the existing cached wrapper because otherwise pointer equality is
360
+ * not ensured (see also:
361
+ * {@link AsPythonObjectBaseNode#mayUsePrimitive(IsPointerNode, PrimitiveNativeWrapper)}).
362
+ */
354
363
@ Specialization (replaces = "doIntegerSmall" )
355
364
static PrimitiveNativeWrapper doInteger (@ SuppressWarnings ("unused" ) CExtContext cextContext , int i ,
356
365
@ Shared ("contextRef" ) @ CachedContext (PythonLanguage .class ) ContextReference <PythonContext > contextRef ) {
357
- if (CompilerDirectives .inInterpreter () && CApiGuards .isSmallInteger (i )) {
358
- return doIntegerSmall (cextContext , i , contextRef );
366
+ if (CApiGuards .isSmallInteger (i )) {
367
+ if (CompilerDirectives .inInterpreter ()) {
368
+ return doIntegerSmall (cextContext , i , contextRef );
369
+ } else {
370
+ // for explanation: see method doc
371
+ return doIntegerSmall (cextContext , i , contextRef ).copy ();
372
+ }
359
373
}
360
374
return PrimitiveNativeWrapper .createInt (i );
361
375
}
@@ -373,8 +387,13 @@ static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContex
373
387
@ Specialization (replaces = "doLongSmall" )
374
388
static PrimitiveNativeWrapper doLong (@ SuppressWarnings ("unused" ) CExtContext cextContext , long l ,
375
389
@ Shared ("contextRef" ) @ CachedContext (PythonLanguage .class ) ContextReference <PythonContext > contextRef ) {
376
- if (CompilerDirectives .inInterpreter () && CApiGuards .isSmallLong (l )) {
377
- return doLongSmall (cextContext , l , contextRef );
390
+ if (CApiGuards .isSmallLong (l )) {
391
+ // for explanation of this construct: see 'ToSulongNode.doInteger'
392
+ if (CompilerDirectives .inInterpreter ()) {
393
+ return doLongSmall (cextContext , l , contextRef );
394
+ } else {
395
+ return doLongSmall (cextContext , l , contextRef ).copy ();
396
+ }
378
397
}
379
398
return PrimitiveNativeWrapper .createLong (l );
380
399
}
@@ -602,17 +621,27 @@ static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContex
602
621
@ Specialization (replaces = "doIntegerSmall" )
603
622
static PrimitiveNativeWrapper doInteger (CExtContext cextContext , int i ,
604
623
@ Shared ("contextRef" ) @ CachedContext (PythonLanguage .class ) ContextReference <PythonContext > contextRef ) {
605
- if (CompilerDirectives .inInterpreter () && CApiGuards .isSmallInteger (i )) {
606
- return doIntegerSmall (cextContext , i , contextRef );
624
+ if (CApiGuards .isSmallInteger (i )) {
625
+ // for explanation of this construct: see 'ToSulongNode.doInteger'
626
+ if (CompilerDirectives .inInterpreter ()) {
627
+ return doIntegerSmall (cextContext , i , contextRef );
628
+ } else {
629
+ return doIntegerSmall (cextContext , i , contextRef ).copy ();
630
+ }
607
631
}
608
632
return PrimitiveNativeWrapper .createInt (i );
609
633
}
610
634
611
635
@ Specialization (replaces = "doLongSmall" )
612
636
static PrimitiveNativeWrapper doLong (CExtContext cextContext , long l ,
613
637
@ Shared ("contextRef" ) @ CachedContext (PythonLanguage .class ) ContextReference <PythonContext > contextRef ) {
614
- if (CompilerDirectives .inInterpreter () && CApiGuards .isSmallLong (l )) {
615
- return doLongSmall (cextContext , l , contextRef );
638
+ if (CApiGuards .isSmallLong (l )) {
639
+ // for explanation of this construct: see 'ToSulongNode.doInteger'
640
+ if (CompilerDirectives .inInterpreter ()) {
641
+ return doLongSmall (cextContext , l , contextRef );
642
+ } else {
643
+ return doLongSmall (cextContext , l , contextRef ).copy ();
644
+ }
616
645
}
617
646
return PrimitiveNativeWrapper .createLong (l );
618
647
}
0 commit comments