16
16
import java .util .Arrays ;
17
17
18
18
import com .oracle .truffle .api .TruffleSafepoint ;
19
+ import com .oracle .truffle .api .dsl .GenerateCached ;
20
+ import com .oracle .truffle .api .dsl .GenerateInline ;
19
21
import com .oracle .truffle .api .dsl .NeverDefault ;
20
22
import com .oracle .truffle .api .nodes .Node ;
21
23
import com .oracle .truffle .api .object .Shape ;
24
+ import com .oracle .truffle .api .profiles .InlinedBranchProfile ;
25
+ import com .oracle .truffle .api .profiles .InlinedConditionProfile ;
22
26
import com .oracle .truffle .api .profiles .InlinedIntValueProfile ;
23
27
import com .oracle .truffle .api .profiles .InlinedLoopConditionProfile ;
24
28
import com .oracle .truffle .api .profiles .LoopConditionProfile ;
47
51
import org .truffleruby .core .cast .BooleanCastNode ;
48
52
import org .truffleruby .core .cast .CmpIntNode ;
49
53
import org .truffleruby .core .cast .ToAryNode ;
50
- import org .truffleruby .core .cast .ToAryNodeGen ;
51
54
import org .truffleruby .core .cast .ToIntNode ;
52
55
import org .truffleruby .core .cast .ToLongNode ;
53
56
import org .truffleruby .core .cast .ToStrNode ;
54
- import org .truffleruby .core .cast .ToStrNodeGen ;
55
57
import org .truffleruby .core .encoding .RubyEncoding ;
56
58
import org .truffleruby .core .format .BytesResult ;
57
59
import org .truffleruby .core .format .FormatExceptionTranslator ;
@@ -137,14 +139,11 @@ protected RubyArray allocate(RubyClass rubyClass) {
137
139
@ ReportPolymorphism
138
140
public abstract static class AddNode extends CoreMethodNode {
139
141
140
- @ CreateCast ("b" )
141
- protected RubyBaseNodeWithExecute coerceOtherToAry (RubyBaseNodeWithExecute other ) {
142
- return ToAryNodeGen .create (other );
143
- }
144
-
145
142
@ Specialization (
146
143
limit = "storageStrategyLimit()" )
147
- protected RubyArray addGeneralize (RubyArray a , RubyArray b ,
144
+ protected RubyArray addGeneralize (RubyArray a , Object bObject ,
145
+ @ Cached ToAryNode toAryNode ,
146
+ @ Bind ("toAryNode.execute(bObject)" ) RubyArray b ,
148
147
@ Bind ("a.getStore()" ) Object aStore ,
149
148
@ Bind ("b.getStore()" ) Object bStore ,
150
149
@ CachedLibrary ("aStore" ) ArrayStoreLibrary as ,
@@ -599,7 +598,7 @@ protected RubyArray concatZero(RubyArray array, NotProvided first, Object[] rest
599
598
protected RubyArray concatOne (RubyArray array , Object first , Object [] rest ,
600
599
@ Cached @ Shared ToAryNode toAryNode ,
601
600
@ Cached @ Shared ArrayAppendManyNode appendManyNode ) {
602
- appendManyNode .executeAppendMany (array , toAryNode .executeToAry (first ));
601
+ appendManyNode .executeAppendMany (array , toAryNode .execute (first ));
603
602
return array ;
604
603
}
605
604
@@ -619,11 +618,11 @@ protected RubyArray concatMany(RubyArray array, Object first, Object[] rest,
619
618
@ Cached @ Shared ConditionProfile selfArgProfile ) {
620
619
int size = array .size ;
621
620
RubyArray copy = createArray (cowNode .execute (array , 0 , size ), size );
622
- RubyArray result = appendManyNode .executeAppendMany (array , toAryNode .executeToAry (first ));
621
+ RubyArray result = appendManyNode .executeAppendMany (array , toAryNode .execute (first ));
623
622
for (int i = 0 ; i < cachedLength ; ++i ) {
624
623
final RubyArray argOrCopy = selfArgProfile .profile (rest [i ] == array )
625
624
? copy
626
- : toAryNode .executeToAry (rest [i ]);
625
+ : toAryNode .execute (rest [i ]);
627
626
result = appendManyNode .executeAppendMany (array , argOrCopy );
628
627
}
629
628
return result ;
@@ -642,15 +641,15 @@ protected RubyArray concatManyGeneral(RubyArray array, Object first, Object[] re
642
641
final int size = array .size ;
643
642
Object store = cowNode .execute (array , 0 , size );
644
643
645
- RubyArray result = appendManyNode .executeAppendMany (array , toAryNode .executeToAry (first ));
644
+ RubyArray result = appendManyNode .executeAppendMany (array , toAryNode .execute (first ));
646
645
int i = 0 ;
647
646
try {
648
647
for (; loopProfile .inject (i < rest .length ); i ++) {
649
648
Object arg = rest [i ];
650
649
if (selfArgProfile .profile (arg == array )) {
651
650
result = appendManyNode .executeAppendMany (array , createArray (store , size ));
652
651
} else {
653
- result = appendManyNode .executeAppendMany (array , toAryNode .executeToAry (arg ));
652
+ result = appendManyNode .executeAppendMany (array , toAryNode .execute (arg ));
654
653
}
655
654
TruffleSafepoint .poll (this );
656
655
}
@@ -1323,14 +1322,11 @@ protected int toInt(Object value) {
1323
1322
@ ImportStatic (ArrayGuards .class )
1324
1323
public abstract static class InitializeCopyNode extends CoreMethodNode {
1325
1324
1326
- @ CreateCast ("from" )
1327
- protected RubyBaseNodeWithExecute coerceOtherToAry (RubyBaseNodeWithExecute other ) {
1328
- return ToAryNodeGen .create (other );
1329
- }
1330
-
1331
1325
@ Specialization
1332
- protected RubyArray initializeCopy (RubyArray self , RubyArray from ,
1326
+ protected RubyArray initializeCopy (RubyArray self , Object fromObject ,
1327
+ @ Cached ToAryNode toAryNode ,
1333
1328
@ Cached ReplaceNode replaceNode ) {
1329
+ final var from = toAryNode .execute (fromObject );
1334
1330
if (self == from ) {
1335
1331
return self ;
1336
1332
}
@@ -1546,92 +1542,101 @@ public void accept(CallBlockNode yieldNode, RubyArray array, Object state, Objec
1546
1542
@ NodeChild (value = "format" , type = RubyBaseNodeWithExecute .class )
1547
1543
@ CoreMethod (names = "pack" , required = 1 )
1548
1544
@ ReportPolymorphism
1549
- public abstract static class PackNode extends CoreMethodNode {
1545
+ public abstract static class ArrayPackNode extends CoreMethodNode {
1550
1546
1551
- @ Child private TruffleString .FromByteArrayNode fromByteArrayNode = TruffleString .FromByteArrayNode .create ();
1552
- @ Child private WriteObjectFieldNode writeAssociatedNode ;
1547
+ @ Specialization
1548
+ protected RubyString pack (RubyArray array , Object format ,
1549
+ @ Cached ToStrNode toStrNode ,
1550
+ @ Cached PackNode packNode ) {
1551
+ final var formatAsString = toStrNode .execute (this , format );
1552
+ return packNode .execute (this , array , formatAsString );
1553
+ }
1554
+ }
1553
1555
1554
- private final BranchProfile exceptionProfile = BranchProfile .create ();
1555
- private final ConditionProfile resizeProfile = ConditionProfile .create ();
1556
+ @ GenerateCached (false )
1557
+ @ GenerateInline
1558
+ public abstract static class PackNode extends RubyBaseNode {
1556
1559
1557
- @ CreateCast ("format" )
1558
- protected ToStrNode coerceFormat (RubyBaseNodeWithExecute format ) {
1559
- return ToStrNodeGen .create (format );
1560
- }
1560
+ public abstract RubyString execute (Node node , RubyArray array , Object format );
1561
1561
1562
1562
@ Specialization (
1563
1563
guards = {
1564
1564
"libFormat.isRubyString(format)" ,
1565
1565
"equalNode.execute(libFormat, format, cachedFormat, cachedEncoding)" },
1566
1566
limit = "getCacheLimit()" )
1567
- protected RubyString packCached (RubyArray array , Object format ,
1567
+ protected static RubyString packCached (Node node , RubyArray array , Object format ,
1568
+ @ Cached @ Shared InlinedBranchProfile exceptionProfile ,
1569
+ @ Cached @ Shared InlinedConditionProfile resizeProfile ,
1568
1570
@ Cached @ Shared RubyStringLibrary libFormat ,
1571
+ @ Cached @ Shared WriteObjectFieldNode writeAssociatedNode ,
1572
+ @ Cached @ Shared TruffleString .FromByteArrayNode fromByteArrayNode ,
1569
1573
@ Cached ("asTruffleStringUncached(format)" ) TruffleString cachedFormat ,
1570
1574
@ Cached ("libFormat.getEncoding(format)" ) RubyEncoding cachedEncoding ,
1571
1575
@ Cached ("cachedFormat.byteLength(cachedEncoding.tencoding)" ) int cachedFormatLength ,
1572
- @ Cached ("create(compileFormat(getJavaString(format)))" ) DirectCallNode callPackNode ,
1576
+ @ Cached ("create(compileFormat(node, getJavaString(format)))" ) DirectCallNode callPackNode ,
1573
1577
@ Cached StringHelperNodes .EqualNode equalNode ) {
1574
1578
final BytesResult result ;
1575
1579
try {
1576
1580
result = (BytesResult ) callPackNode .call (
1577
1581
new Object []{ array .getStore (), array .size , false , null });
1578
1582
} catch (FormatException e ) {
1579
- exceptionProfile .enter ();
1580
- throw FormatExceptionTranslator .translate (getContext (), this , e );
1583
+ exceptionProfile .enter (node );
1584
+ throw FormatExceptionTranslator .translate (getContext (node ), node , e );
1581
1585
}
1582
1586
1583
- return finishPack (cachedFormatLength , result );
1587
+ return finishPack (node , cachedFormatLength , result , resizeProfile , writeAssociatedNode , fromByteArrayNode );
1584
1588
}
1585
1589
1586
1590
@ Specialization (guards = { "libFormat.isRubyString(format)" }, replaces = "packCached" , limit = "1" )
1587
- protected RubyString packUncached (RubyArray array , Object format ,
1591
+ protected static RubyString packUncached (Node node , RubyArray array , Object format ,
1592
+ @ Cached @ Shared InlinedBranchProfile exceptionProfile ,
1593
+ @ Cached @ Shared InlinedConditionProfile resizeProfile ,
1588
1594
@ Cached @ Shared RubyStringLibrary libFormat ,
1595
+ @ Cached @ Shared WriteObjectFieldNode writeAssociatedNode ,
1596
+ @ Cached @ Shared TruffleString .FromByteArrayNode fromByteArrayNode ,
1589
1597
@ Cached ToJavaStringNode toJavaStringNode ,
1590
1598
@ Cached IndirectCallNode callPackNode ) {
1591
1599
final String formatString = toJavaStringNode .execute (format );
1592
1600
1593
1601
final BytesResult result ;
1594
1602
try {
1595
1603
result = (BytesResult ) callPackNode .call (
1596
- compileFormat (formatString ),
1604
+ compileFormat (node , formatString ),
1597
1605
new Object []{ array .getStore (), array .size , false , null });
1598
1606
} catch (FormatException e ) {
1599
- exceptionProfile .enter ();
1600
- throw FormatExceptionTranslator .translate (getContext (), this , e );
1607
+ exceptionProfile .enter (node );
1608
+ throw FormatExceptionTranslator .translate (getContext (node ), node , e );
1601
1609
}
1602
1610
1603
1611
int formatLength = libFormat .getTString (format ).byteLength (libFormat .getTEncoding (format ));
1604
- return finishPack (formatLength , result );
1612
+ return finishPack (node , formatLength , result , resizeProfile , writeAssociatedNode , fromByteArrayNode );
1605
1613
}
1606
1614
1607
- private RubyString finishPack (int formatLength , BytesResult result ) {
1615
+ private static RubyString finishPack (Node node , int formatLength , BytesResult result ,
1616
+ InlinedConditionProfile resizeProfile ,
1617
+ WriteObjectFieldNode writeAssociatedNode , TruffleString .FromByteArrayNode fromByteArrayNode ) {
1608
1618
byte [] bytes = result .getOutput ();
1609
1619
1610
- if (resizeProfile .profile (bytes .length != result .getOutputLength ())) {
1620
+ if (resizeProfile .profile (node , bytes .length != result .getOutputLength ())) {
1611
1621
bytes = Arrays .copyOf (bytes , result .getOutputLength ());
1612
1622
}
1613
1623
1614
1624
final RubyEncoding rubyEncoding = result .getEncoding ().getEncodingForLength (formatLength );
1615
- final RubyString string = createString (fromByteArrayNode , bytes , rubyEncoding );
1625
+ final RubyString string = createString (node , fromByteArrayNode , bytes , rubyEncoding );
1616
1626
1617
1627
if (result .getAssociated () != null ) {
1618
- if (writeAssociatedNode == null ) {
1619
- CompilerDirectives .transferToInterpreterAndInvalidate ();
1620
- writeAssociatedNode = insert (WriteObjectFieldNode .create ());
1621
- }
1622
-
1623
- writeAssociatedNode .execute (string , Layouts .ASSOCIATED_IDENTIFIER , result .getAssociated ());
1628
+ writeAssociatedNode .execute (node , string , Layouts .ASSOCIATED_IDENTIFIER , result .getAssociated ());
1624
1629
}
1625
1630
1626
1631
return string ;
1627
1632
}
1628
1633
1629
1634
@ TruffleBoundary
1630
- protected RootCallTarget compileFormat (String format ) {
1635
+ protected static RootCallTarget compileFormat (Node node , String format ) {
1631
1636
try {
1632
- return new PackCompiler (getLanguage (), this ).compile (format );
1637
+ return new PackCompiler (getLanguage (node ), node ).compile (format );
1633
1638
} catch (DeferredRaiseException dre ) {
1634
- throw dre .getException (getContext ());
1639
+ throw dre .getException (getContext (node ));
1635
1640
}
1636
1641
}
1637
1642
@@ -1825,17 +1830,14 @@ public static ReplaceNode create() {
1825
1830
1826
1831
public abstract RubyArray executeReplace (RubyArray array , RubyArray other );
1827
1832
1828
- @ CreateCast ("other" )
1829
- protected RubyBaseNodeWithExecute coerceOtherToAry (RubyBaseNodeWithExecute index ) {
1830
- return ToAryNodeGen .create (index );
1831
- }
1832
-
1833
1833
@ Specialization
1834
- protected RubyArray replace (RubyArray array , RubyArray other ,
1834
+ protected RubyArray replace (RubyArray array , Object otherObject ,
1835
+ @ Cached ToAryNode toAryNode ,
1835
1836
@ Cached ArrayCopyOnWriteNode cowNode ,
1836
1837
@ Cached IsSharedNode isSharedNode ,
1837
1838
@ Cached ConditionProfile sharedProfile ,
1838
1839
@ CachedLibrary (limit = "2" ) ArrayStoreLibrary stores ) {
1840
+ final var other = toAryNode .execute (otherObject );
1839
1841
final int size = other .size ;
1840
1842
Object store = cowNode .execute (other , 0 , size );
1841
1843
if (sharedProfile .profile (isSharedNode .executeIsShared (this , array ))) {
0 commit comments