Skip to content

Commit 0d3058b

Browse files
committed
[GR-17457] Refactor nodes to get rid of CreateCast
PullRequest: truffleruby/3869
2 parents 816500f + 792b24b commit 0d3058b

File tree

53 files changed

+778
-893
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+778
-893
lines changed

spec/truffle/parsing/fixtures/hashes/with_double_splat_operator/with_double_splat.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ subject: "Hash"
22
description: "Double Splat operator/there is only double splat operator (`{**{}}`)"
33
notes: >
44
A value is wrapped up in a HashCastNodeGen node
5-
focused_on_node: "org.truffleruby.core.cast.HashCastNodeGen"
5+
focused_on_node: "org.truffleruby.core.cast.HashCastNodeGen$HashCastASTNodeGen"
66
ruby: |
77
{**foo}
88
ast: |
9-
HashCastNodeGen
9+
HashCastNodeGen$HashCastASTNodeGen
1010
attributes:
1111
flags = 0
1212
children:

spec/truffle/parsing/fixtures/hashes/with_double_splat_operator/with_double_splat_and_key_value_pairs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ast: |
1313
flags = 1
1414
children:
1515
children = [
16-
HashCastNodeGen
16+
HashCastNodeGen$HashCastASTNodeGen
1717
attributes:
1818
flags = 0
1919
children:

spec/truffle/parsing/fixtures/hashes/with_double_splat_operator/with_key_value_pairs_and_double_splat_operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ast: |
2929
flags = 0
3030
value = 1
3131
]
32-
HashCastNodeGen
32+
HashCastNodeGen$HashCastASTNodeGen
3333
attributes:
3434
flags = 0
3535
children:

spec/truffle/parsing/fixtures/hashes/with_double_splat_operator/with_key_value_pairs_double_splat_operator_and_key_value_pairs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ast: |
3030
flags = 0
3131
value = 1
3232
]
33-
HashCastNodeGen
33+
HashCastNodeGen$HashCastASTNodeGen
3434
attributes:
3535
flags = 0
3636
children:

spec/truffle/parsing/fixtures/method_calls/arguments/with_double_splat_operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ast: |
2323
ruby2KeywordsHashProfile = false
2424
children:
2525
arguments = [
26-
HashCastNodeGen
26+
HashCastNodeGen$HashCastASTNodeGen
2727
attributes:
2828
flags = 0
2929
children:

spec/truffle/parsing/fixtures/yield/with_only_double_splat_operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ ast: |
7373
ruby2KeywordsHashProfile = false
7474
children:
7575
arguments = [
76-
HashCastNodeGen
76+
HashCastNodeGen$HashCastASTNodeGen
7777
attributes:
7878
flags = 0
7979
children:

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,7 @@ public abstract static class SetMarkList extends CoreMethodArrayArgumentsNode {
18541854
protected Object setMarkList(RubyDynamicObject structOwner,
18551855
@Cached WriteObjectFieldNode writeMarkedNode) {
18561856
writeMarkedNode.execute(
1857+
this,
18571858
structOwner,
18581859
Layouts.MARKED_OBJECTS_IDENTIFIER,
18591860
getContext().getMarkingService()

src/main/java/org/truffleruby/cext/UnwrapNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ protected static ValueWrapper genericToWrapper(Node node, Object value,
183183
} catch (UnsupportedMessageException e) {
184184
unsupportedProfile.enter(node);
185185
throw new RaiseException(getContext(node),
186-
coreExceptions(node).argumentError(e.getMessage(), getNode(node), e));
186+
coreExceptions(node).argumentError(e.getMessage(), node, e));
187187
}
188188
return nativeToWrapperNode.execute(node, handle);
189189
}
@@ -281,7 +281,7 @@ protected static Object unwrapGeneric(Node node, Object value,
281281
} catch (UnsupportedMessageException e) {
282282
unsupportedProfile.enter(node);
283283
throw new RaiseException(getContext(node),
284-
coreExceptions(node).argumentError(e.getMessage(), getNode(node), e));
284+
coreExceptions(node).argumentError(e.getMessage(), node, e));
285285
}
286286
return unwrapNativeNode.execute(node, handle);
287287
}

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ protected long allocateHandle(ValueWrapper wrapper, RubyContext context, RubyLan
336336
}
337337

338338
if (context.getOptions().BACKTRACE_ON_TO_NATIVE) {
339-
context.getDefaultBacktraceFormatter().printBacktraceOnEnvStderr("ValueWrapper#toNative: ", getNode());
339+
context.getDefaultBacktraceFormatter().printBacktraceOnEnvStderr("ValueWrapper#toNative: ", this);
340340
}
341341

342342
if (block == null || block.isFull()) {

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
import java.util.Arrays;
1717

1818
import com.oracle.truffle.api.TruffleSafepoint;
19+
import com.oracle.truffle.api.dsl.GenerateCached;
20+
import com.oracle.truffle.api.dsl.GenerateInline;
1921
import com.oracle.truffle.api.dsl.NeverDefault;
2022
import com.oracle.truffle.api.nodes.Node;
2123
import com.oracle.truffle.api.object.Shape;
24+
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
25+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
2226
import com.oracle.truffle.api.profiles.InlinedIntValueProfile;
2327
import com.oracle.truffle.api.profiles.InlinedLoopConditionProfile;
2428
import com.oracle.truffle.api.profiles.LoopConditionProfile;
@@ -47,11 +51,9 @@
4751
import org.truffleruby.core.cast.BooleanCastNode;
4852
import org.truffleruby.core.cast.CmpIntNode;
4953
import org.truffleruby.core.cast.ToAryNode;
50-
import org.truffleruby.core.cast.ToAryNodeGen;
5154
import org.truffleruby.core.cast.ToIntNode;
5255
import org.truffleruby.core.cast.ToLongNode;
5356
import org.truffleruby.core.cast.ToStrNode;
54-
import org.truffleruby.core.cast.ToStrNodeGen;
5557
import org.truffleruby.core.encoding.RubyEncoding;
5658
import org.truffleruby.core.format.BytesResult;
5759
import org.truffleruby.core.format.FormatExceptionTranslator;
@@ -137,14 +139,11 @@ protected RubyArray allocate(RubyClass rubyClass) {
137139
@ReportPolymorphism
138140
public abstract static class AddNode extends CoreMethodNode {
139141

140-
@CreateCast("b")
141-
protected RubyBaseNodeWithExecute coerceOtherToAry(RubyBaseNodeWithExecute other) {
142-
return ToAryNodeGen.create(other);
143-
}
144-
145142
@Specialization(
146143
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,
148147
@Bind("a.getStore()") Object aStore,
149148
@Bind("b.getStore()") Object bStore,
150149
@CachedLibrary("aStore") ArrayStoreLibrary as,
@@ -599,7 +598,7 @@ protected RubyArray concatZero(RubyArray array, NotProvided first, Object[] rest
599598
protected RubyArray concatOne(RubyArray array, Object first, Object[] rest,
600599
@Cached @Shared ToAryNode toAryNode,
601600
@Cached @Shared ArrayAppendManyNode appendManyNode) {
602-
appendManyNode.executeAppendMany(array, toAryNode.executeToAry(first));
601+
appendManyNode.executeAppendMany(array, toAryNode.execute(first));
603602
return array;
604603
}
605604

@@ -619,11 +618,11 @@ protected RubyArray concatMany(RubyArray array, Object first, Object[] rest,
619618
@Cached @Shared ConditionProfile selfArgProfile) {
620619
int size = array.size;
621620
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));
623622
for (int i = 0; i < cachedLength; ++i) {
624623
final RubyArray argOrCopy = selfArgProfile.profile(rest[i] == array)
625624
? copy
626-
: toAryNode.executeToAry(rest[i]);
625+
: toAryNode.execute(rest[i]);
627626
result = appendManyNode.executeAppendMany(array, argOrCopy);
628627
}
629628
return result;
@@ -642,15 +641,15 @@ protected RubyArray concatManyGeneral(RubyArray array, Object first, Object[] re
642641
final int size = array.size;
643642
Object store = cowNode.execute(array, 0, size);
644643

645-
RubyArray result = appendManyNode.executeAppendMany(array, toAryNode.executeToAry(first));
644+
RubyArray result = appendManyNode.executeAppendMany(array, toAryNode.execute(first));
646645
int i = 0;
647646
try {
648647
for (; loopProfile.inject(i < rest.length); i++) {
649648
Object arg = rest[i];
650649
if (selfArgProfile.profile(arg == array)) {
651650
result = appendManyNode.executeAppendMany(array, createArray(store, size));
652651
} else {
653-
result = appendManyNode.executeAppendMany(array, toAryNode.executeToAry(arg));
652+
result = appendManyNode.executeAppendMany(array, toAryNode.execute(arg));
654653
}
655654
TruffleSafepoint.poll(this);
656655
}
@@ -1323,14 +1322,11 @@ protected int toInt(Object value) {
13231322
@ImportStatic(ArrayGuards.class)
13241323
public abstract static class InitializeCopyNode extends CoreMethodNode {
13251324

1326-
@CreateCast("from")
1327-
protected RubyBaseNodeWithExecute coerceOtherToAry(RubyBaseNodeWithExecute other) {
1328-
return ToAryNodeGen.create(other);
1329-
}
1330-
13311325
@Specialization
1332-
protected RubyArray initializeCopy(RubyArray self, RubyArray from,
1326+
protected RubyArray initializeCopy(RubyArray self, Object fromObject,
1327+
@Cached ToAryNode toAryNode,
13331328
@Cached ReplaceNode replaceNode) {
1329+
final var from = toAryNode.execute(fromObject);
13341330
if (self == from) {
13351331
return self;
13361332
}
@@ -1546,92 +1542,101 @@ public void accept(CallBlockNode yieldNode, RubyArray array, Object state, Objec
15461542
@NodeChild(value = "format", type = RubyBaseNodeWithExecute.class)
15471543
@CoreMethod(names = "pack", required = 1)
15481544
@ReportPolymorphism
1549-
public abstract static class PackNode extends CoreMethodNode {
1545+
public abstract static class ArrayPackNode extends CoreMethodNode {
15501546

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+
}
15531555

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 {
15561559

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);
15611561

15621562
@Specialization(
15631563
guards = {
15641564
"libFormat.isRubyString(format)",
15651565
"equalNode.execute(libFormat, format, cachedFormat, cachedEncoding)" },
15661566
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,
15681570
@Cached @Shared RubyStringLibrary libFormat,
1571+
@Cached @Shared WriteObjectFieldNode writeAssociatedNode,
1572+
@Cached @Shared TruffleString.FromByteArrayNode fromByteArrayNode,
15691573
@Cached("asTruffleStringUncached(format)") TruffleString cachedFormat,
15701574
@Cached("libFormat.getEncoding(format)") RubyEncoding cachedEncoding,
15711575
@Cached("cachedFormat.byteLength(cachedEncoding.tencoding)") int cachedFormatLength,
1572-
@Cached("create(compileFormat(getJavaString(format)))") DirectCallNode callPackNode,
1576+
@Cached("create(compileFormat(node, getJavaString(format)))") DirectCallNode callPackNode,
15731577
@Cached StringHelperNodes.EqualNode equalNode) {
15741578
final BytesResult result;
15751579
try {
15761580
result = (BytesResult) callPackNode.call(
15771581
new Object[]{ array.getStore(), array.size, false, null });
15781582
} 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);
15811585
}
15821586

1583-
return finishPack(cachedFormatLength, result);
1587+
return finishPack(node, cachedFormatLength, result, resizeProfile, writeAssociatedNode, fromByteArrayNode);
15841588
}
15851589

15861590
@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,
15881594
@Cached @Shared RubyStringLibrary libFormat,
1595+
@Cached @Shared WriteObjectFieldNode writeAssociatedNode,
1596+
@Cached @Shared TruffleString.FromByteArrayNode fromByteArrayNode,
15891597
@Cached ToJavaStringNode toJavaStringNode,
15901598
@Cached IndirectCallNode callPackNode) {
15911599
final String formatString = toJavaStringNode.execute(format);
15921600

15931601
final BytesResult result;
15941602
try {
15951603
result = (BytesResult) callPackNode.call(
1596-
compileFormat(formatString),
1604+
compileFormat(node, formatString),
15971605
new Object[]{ array.getStore(), array.size, false, null });
15981606
} 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);
16011609
}
16021610

16031611
int formatLength = libFormat.getTString(format).byteLength(libFormat.getTEncoding(format));
1604-
return finishPack(formatLength, result);
1612+
return finishPack(node, formatLength, result, resizeProfile, writeAssociatedNode, fromByteArrayNode);
16051613
}
16061614

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) {
16081618
byte[] bytes = result.getOutput();
16091619

1610-
if (resizeProfile.profile(bytes.length != result.getOutputLength())) {
1620+
if (resizeProfile.profile(node, bytes.length != result.getOutputLength())) {
16111621
bytes = Arrays.copyOf(bytes, result.getOutputLength());
16121622
}
16131623

16141624
final RubyEncoding rubyEncoding = result.getEncoding().getEncodingForLength(formatLength);
1615-
final RubyString string = createString(fromByteArrayNode, bytes, rubyEncoding);
1625+
final RubyString string = createString(node, fromByteArrayNode, bytes, rubyEncoding);
16161626

16171627
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());
16241629
}
16251630

16261631
return string;
16271632
}
16281633

16291634
@TruffleBoundary
1630-
protected RootCallTarget compileFormat(String format) {
1635+
protected static RootCallTarget compileFormat(Node node, String format) {
16311636
try {
1632-
return new PackCompiler(getLanguage(), this).compile(format);
1637+
return new PackCompiler(getLanguage(node), node).compile(format);
16331638
} catch (DeferredRaiseException dre) {
1634-
throw dre.getException(getContext());
1639+
throw dre.getException(getContext(node));
16351640
}
16361641
}
16371642

@@ -1825,17 +1830,14 @@ public static ReplaceNode create() {
18251830

18261831
public abstract RubyArray executeReplace(RubyArray array, RubyArray other);
18271832

1828-
@CreateCast("other")
1829-
protected RubyBaseNodeWithExecute coerceOtherToAry(RubyBaseNodeWithExecute index) {
1830-
return ToAryNodeGen.create(index);
1831-
}
1832-
18331833
@Specialization
1834-
protected RubyArray replace(RubyArray array, RubyArray other,
1834+
protected RubyArray replace(RubyArray array, Object otherObject,
1835+
@Cached ToAryNode toAryNode,
18351836
@Cached ArrayCopyOnWriteNode cowNode,
18361837
@Cached IsSharedNode isSharedNode,
18371838
@Cached ConditionProfile sharedProfile,
18381839
@CachedLibrary(limit = "2") ArrayStoreLibrary stores) {
1840+
final var other = toAryNode.execute(otherObject);
18391841
final int size = other.size;
18401842
Object store = cowNode.execute(other, 0, size);
18411843
if (sharedProfile.profile(isSharedNode.executeIsShared(this, array))) {

0 commit comments

Comments
 (0)