Skip to content

Commit 5fadc20

Browse files
author
Nicolas Laurent
committed
[GR-23357] Refactor method dispatch.
PullRequest: truffleruby/1860
2 parents 678f4de + 0d70209 commit 5fadc20

File tree

116 files changed

+1147
-2464
lines changed

Some content is hidden

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

116 files changed

+1147
-2464
lines changed

src/main/java/org/truffleruby/builtins/EnumeratorSizeNode.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
import org.truffleruby.language.RubyContextSourceNode;
1515
import org.truffleruby.language.RubyNode;
1616
import org.truffleruby.language.arguments.RubyArguments;
17-
import org.truffleruby.language.dispatch.CallDispatchHeadNode;
17+
import org.truffleruby.language.dispatch.DispatchNode;
1818

1919
import com.oracle.truffle.api.CompilerDirectives;
2020
import com.oracle.truffle.api.frame.VirtualFrame;
2121
import com.oracle.truffle.api.profiles.ConditionProfile;
2222

23+
2324
public class EnumeratorSizeNode extends RubyContextSourceNode {
2425

2526
@Child private RubyNode method;
26-
@Child private CallDispatchHeadNode toEnumWithSize;
27+
@Child private DispatchNode toEnumWithSize;
2728

2829
private final ConditionProfile noBlockProfile = ConditionProfile.create();
2930

@@ -43,7 +44,7 @@ public Object execute(VirtualFrame frame) {
4344
if (noBlockProfile.profile(block == null)) {
4445
if (toEnumWithSize == null) {
4546
CompilerDirectives.transferToInterpreterAndInvalidate();
46-
toEnumWithSize = insert(CallDispatchHeadNode.createPrivate());
47+
toEnumWithSize = insert(DispatchNode.create());
4748
}
4849

4950
final Object self = RubyArguments.getSelf(frame);

src/main/java/org/truffleruby/builtins/ReturnEnumeratorIfNoBlockNode.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
import org.truffleruby.language.RubyContextSourceNode;
1616
import org.truffleruby.language.RubyNode;
1717
import org.truffleruby.language.arguments.RubyArguments;
18-
import org.truffleruby.language.dispatch.CallDispatchHeadNode;
18+
import org.truffleruby.language.dispatch.DispatchNode;
1919

2020
import com.oracle.truffle.api.CompilerDirectives;
2121
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
2222
import com.oracle.truffle.api.frame.VirtualFrame;
2323
import com.oracle.truffle.api.profiles.ConditionProfile;
2424

25+
2526
public class ReturnEnumeratorIfNoBlockNode extends RubyContextSourceNode {
2627

2728
private final String methodName;
2829
@Child private RubyNode method;
29-
@Child private CallDispatchHeadNode toEnumNode;
30+
@Child private DispatchNode toEnumNode;
3031
@CompilationFinal private RubySymbol methodSymbol;
3132
private final ConditionProfile noBlockProfile = ConditionProfile.create();
3233

@@ -42,7 +43,7 @@ public Object execute(VirtualFrame frame) {
4243
if (noBlockProfile.profile(block == null)) {
4344
if (toEnumNode == null) {
4445
CompilerDirectives.transferToInterpreterAndInvalidate();
45-
toEnumNode = insert(CallDispatchHeadNode.createPrivate());
46+
toEnumNode = insert(DispatchNode.create());
4647
}
4748

4849
if (methodSymbol == null) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
import org.truffleruby.language.control.BreakException;
8282
import org.truffleruby.language.control.BreakID;
8383
import org.truffleruby.language.control.RaiseException;
84-
import org.truffleruby.language.dispatch.CallDispatchHeadNode;
84+
import org.truffleruby.language.dispatch.DispatchNode;
8585
import org.truffleruby.language.methods.DeclarationContext;
8686
import org.truffleruby.language.methods.InternalMethod;
8787
import org.truffleruby.language.objects.AllocateHelperNode;
@@ -112,6 +112,7 @@
112112
import com.oracle.truffle.api.profiles.ConditionProfile;
113113
import com.oracle.truffle.api.source.SourceSection;
114114

115+
115116
@CoreModule("Truffle::CExt")
116117
public class CExtNodes {
117118

@@ -1092,14 +1093,14 @@ protected int write(RubyString string, int index, int value,
10921093
@CoreMethod(names = "rb_class_new", onSingleton = true, required = 1)
10931094
public abstract static class ClassNewNode extends CoreMethodArrayArgumentsNode {
10941095

1095-
@Child private CallDispatchHeadNode allocateNode;
1096+
@Child private DispatchNode allocateNode;
10961097
@Child private InitializeClassNode initializeClassNode;
10971098

10981099
@Specialization
10991100
protected RubyClass classNew(RubyClass superclass) {
11001101
if (allocateNode == null) {
11011102
CompilerDirectives.transferToInterpreterAndInvalidate();
1102-
allocateNode = insert(CallDispatchHeadNode.createPrivate());
1103+
allocateNode = insert(DispatchNode.create());
11031104
initializeClassNode = insert(InitializeClassNodeGen.create(false));
11041105
}
11051106

@@ -1113,7 +1114,7 @@ protected RubyClass classNew(RubyClass superclass) {
11131114
@CoreMethod(names = "rb_tr_debug", onSingleton = true, rest = true)
11141115
public abstract static class DebugNode extends CoreMethodArrayArgumentsNode {
11151116

1116-
@Child CallDispatchHeadNode toSCall;
1117+
@Child DispatchNode toSCall;
11171118

11181119
@TruffleBoundary
11191120
@Specialization
@@ -1152,7 +1153,7 @@ protected Object debug(Object... objects) {
11521153
private RubyString callToS(Object object) {
11531154
if (toSCall == null) {
11541155
CompilerDirectives.transferToInterpreterAndInvalidate();
1155-
toSCall = insert(CallDispatchHeadNode.createPrivate());
1156+
toSCall = insert(DispatchNode.create());
11561157
}
11571158

11581159
return (RubyString) toSCall.call(object, "to_s");

src/main/java/org/truffleruby/core/VMPrimitiveNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
import org.truffleruby.language.control.RaiseException;
7373
import org.truffleruby.language.control.ThrowException;
7474
import org.truffleruby.language.methods.InternalMethod;
75-
import org.truffleruby.language.methods.LookupMethodNode;
75+
import org.truffleruby.language.methods.LookupMethodOnSelfNode;
7676
import org.truffleruby.language.objects.AllocateHelperNode;
7777
import org.truffleruby.language.objects.MetaClassNode;
7878
import org.truffleruby.language.objects.shared.SharedObjects;
@@ -165,7 +165,7 @@ public static abstract class VMMethodLookupNode extends PrimitiveArrayArgumentsN
165165
@Specialization
166166
protected Object vmMethodLookup(VirtualFrame frame, Object receiver, Object name,
167167
@Cached NameToJavaStringNode nameToJavaStringNode,
168-
@Cached LookupMethodNode lookupMethodNode) {
168+
@Cached LookupMethodOnSelfNode lookupMethodNode) {
169169
// TODO BJF Sep 14, 2016 Handle private
170170
final String normalizedName = nameToJavaStringNode.execute(name);
171171
InternalMethod method = lookupMethodNode.lookupIgnoringVisibility(frame, receiver, normalizedName);

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import static org.truffleruby.core.array.ArrayHelpers.setSize;
1313
import static org.truffleruby.core.array.ArrayHelpers.setStoreAndSize;
14+
import static org.truffleruby.language.dispatch.DispatchNode.PUBLIC;
1415

1516
import java.util.Arrays;
1617

@@ -63,12 +64,13 @@
6364
import org.truffleruby.core.support.TypeNodes;
6465
import org.truffleruby.core.symbol.RubySymbol;
6566
import org.truffleruby.extra.ffi.Pointer;
67+
import org.truffleruby.interop.ToJavaStringNode;
6668
import org.truffleruby.language.Nil;
6769
import org.truffleruby.language.NotProvided;
6870
import org.truffleruby.language.RubyNode;
6971
import org.truffleruby.language.Visibility;
7072
import org.truffleruby.language.control.RaiseException;
71-
import org.truffleruby.language.dispatch.CallDispatchHeadNode;
73+
import org.truffleruby.language.dispatch.DispatchNode;
7274
import org.truffleruby.language.library.RubyLibrary;
7375
import org.truffleruby.language.objects.AllocateHelperNode;
7476
import org.truffleruby.language.objects.PropagateTaintNode;
@@ -967,13 +969,13 @@ protected Object value(Object[] args) {
967969

968970
@Specialization
969971
protected Object fillFallback(VirtualFrame frame, RubyArray array, Object[] args, NotProvided block,
970-
@Cached("createPrivate()") CallDispatchHeadNode callFillInternal) {
972+
@Cached DispatchNode callFillInternal) {
971973
return callFillInternal.call(array, "fill_internal", args);
972974
}
973975

974976
@Specialization
975977
protected Object fillFallback(VirtualFrame frame, RubyArray array, Object[] args, RubyProc block,
976-
@Cached("createPrivate()") CallDispatchHeadNode callFillInternal) {
978+
@Cached DispatchNode callFillInternal) {
977979
return callFillInternal.callWithBlock(array, "fill_internal", block, args);
978980
}
979981

@@ -988,7 +990,7 @@ public abstract static class HashNode extends ArrayCoreMethodNode {
988990
@Specialization(limit = "storageStrategyLimit()")
989991
protected long hash(VirtualFrame frame, RubyArray array,
990992
@CachedLibrary("array.store") ArrayStoreLibrary stores,
991-
@Cached("createPrivate()") CallDispatchHeadNode toHashNode,
993+
@Cached DispatchNode toHashNode,
992994
@Cached ToLongNode toLongNode,
993995
@Cached("createCountingProfile()") LoopConditionProfile loopProfile) {
994996
final int size = array.size;
@@ -1045,7 +1047,7 @@ protected boolean include(RubyArray array, Object value,
10451047
public abstract static class InitializeNode extends YieldingCoreMethodNode {
10461048

10471049
@Child private ToIntNode toIntNode;
1048-
@Child private CallDispatchHeadNode toAryNode;
1050+
@Child private DispatchNode toAryNode;
10491051
@Child private KernelNodes.RespondToNode respondToToAryNode;
10501052

10511053
protected abstract RubyArray executeInitialize(RubyArray array, Object size, Object fillingValue,
@@ -1213,7 +1215,7 @@ public boolean respondToToAry(Object object) {
12131215
protected Object callToAry(Object object) {
12141216
if (toAryNode == null) {
12151217
CompilerDirectives.transferToInterpreterAndInvalidate();
1216-
toAryNode = insert(CallDispatchHeadNode.createPrivate());
1218+
toAryNode = insert(DispatchNode.create());
12171219
}
12181220
return toAryNode.call(object, "to_ary");
12191221
}
@@ -1256,7 +1258,7 @@ protected RubyArray initializeCopy(RubyArray self, RubyArray from,
12561258
@ReportPolymorphism
12571259
public abstract static class InjectNode extends YieldingCoreMethodNode {
12581260

1259-
@Child private CallDispatchHeadNode dispatch = CallDispatchHeadNode.createPublic();
1261+
@Child private DispatchNode dispatch = DispatchNode.create(PUBLIC);
12601262

12611263
// With block
12621264

@@ -1350,9 +1352,18 @@ protected Object injectSymbolWithInitial(
13501352
RubySymbol symbol,
13511353
Nil block,
13521354
@CachedLibrary("array.store") ArrayStoreLibrary stores,
1353-
@Cached("createCountingProfile()") LoopConditionProfile loopProfile) {
1355+
@Cached("createCountingProfile()") LoopConditionProfile loopProfile,
1356+
@Cached ToJavaStringNode toJavaString) {
13541357
final Object store = array.store;
1355-
return injectSymbolHelper(frame, array, symbol, stores, store, initialOrSymbol, 0, loopProfile);
1358+
return injectSymbolHelper(
1359+
frame,
1360+
array,
1361+
toJavaString.executeToJavaString(symbol),
1362+
stores,
1363+
store,
1364+
initialOrSymbol,
1365+
0,
1366+
loopProfile);
13561367
}
13571368

13581369
@Specialization(
@@ -1365,20 +1376,21 @@ protected Object injectSymbolNoInitial(
13651376
NotProvided symbol,
13661377
Nil block,
13671378
@CachedLibrary("array.store") ArrayStoreLibrary stores,
1368-
@Cached("createCountingProfile()") LoopConditionProfile loopProfile) {
1379+
@Cached("createCountingProfile()") LoopConditionProfile loopProfile,
1380+
@Cached ToJavaStringNode toJavaString) {
13691381
final Object store = array.store;
13701382
return injectSymbolHelper(
13711383
frame,
13721384
array,
1373-
initialOrSymbol,
1385+
toJavaString.executeToJavaString(initialOrSymbol),
13741386
stores,
13751387
store,
13761388
stores.read(store, 0),
13771389
1,
13781390
loopProfile);
13791391
}
13801392

1381-
public Object injectSymbolHelper(VirtualFrame frame, RubyArray array, RubySymbol symbol,
1393+
public Object injectSymbolHelper(VirtualFrame frame, RubyArray array, String symbol,
13821394
ArrayStoreLibrary stores, Object store, Object initial, int start,
13831395
LoopConditionProfile loopProfile) {
13841396
Object accumulator = initial;
@@ -2092,7 +2104,7 @@ protected RubyArray sortEmpty(RubyArray array, Object unusedBlock) {
20922104
protected RubyArray sortVeryShort(VirtualFrame frame, RubyArray array, NotProvided block,
20932105
@CachedLibrary("array.store") ArrayStoreLibrary originalStores,
20942106
@CachedLibrary(limit = "1") ArrayStoreLibrary stores,
2095-
@Cached("createPrivate()") CallDispatchHeadNode compareDispatchNode,
2107+
@Cached DispatchNode compareDispatchNode,
20962108
@Cached CmpIntNode cmpIntNode) {
20972109
final Object originalStore = array.store;
20982110
final Object store = originalStores
@@ -2154,13 +2166,13 @@ protected Object sortPrimitiveArrayNoBlock(RubyArray array, NotProvided block,
21542166
limit = "storageStrategyLimit()")
21552167
protected Object sortArrayWithoutBlock(RubyArray array, NotProvided block,
21562168
@CachedLibrary("array.store") ArrayStoreLibrary stores,
2157-
@Cached("createPrivate()") CallDispatchHeadNode fallbackNode) {
2169+
@Cached DispatchNode fallbackNode) {
21582170
return fallbackNode.call(array, "sort_fallback");
21592171
}
21602172

21612173
@Specialization(guards = "!isEmptyArray(array)")
21622174
protected Object sortGenericWithBlock(RubyArray array, RubyProc block,
2163-
@Cached("createPrivate()") CallDispatchHeadNode fallbackNode) {
2175+
@Cached DispatchNode fallbackNode) {
21642176
return fallbackNode.callWithBlock(array, "sort_fallback", block);
21652177
}
21662178

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import org.truffleruby.language.RubyDynamicObject;
1515
import org.truffleruby.language.RubyGuards;
16-
import org.truffleruby.language.dispatch.CallDispatchHeadNode;
16+
import org.truffleruby.language.dispatch.DispatchNode;
1717
import org.truffleruby.language.library.RubyLibrary;
1818
import org.truffleruby.language.objects.ObjectGraph;
1919
import org.truffleruby.language.objects.ObjectGraphNode;
@@ -61,7 +61,7 @@ public long getArraySize() {
6161
@ExportMessage
6262
public Object readArrayElement(long index,
6363
@Cached @Shared("error") BranchProfile errorProfile,
64-
@Cached @Exclusive CallDispatchHeadNode dispatch) throws InvalidArrayIndexException {
64+
@Cached @Exclusive DispatchNode dispatch) throws InvalidArrayIndexException {
6565
if (inBounds(index)) {
6666
// FIXME (pitr 11-Feb-2020): use ArrayReadNormalizedNode
6767
// @Cached ArrayReadNormalizedNode readNode
@@ -76,7 +76,7 @@ public Object readArrayElement(long index,
7676
@ExportMessage
7777
public void writeArrayElement(long index, Object value,
7878
@Cached @Shared("error") BranchProfile errorProfile,
79-
@Cached @Exclusive CallDispatchHeadNode dispatch) throws InvalidArrayIndexException {
79+
@Cached @Exclusive DispatchNode dispatch) throws InvalidArrayIndexException {
8080
if (index >= 0 && RubyGuards.fitsInInteger(index)) {
8181
// FIXME (pitr 11-Feb-2020): use ArrayWriteNormalizedNode
8282
// @Cached ArrayWriteNormalizedNode writeNode
@@ -90,7 +90,7 @@ public void writeArrayElement(long index, Object value,
9090

9191
@ExportMessage
9292
public void removeArrayElement(long index,
93-
@Cached @Exclusive CallDispatchHeadNode dispatch,
93+
@Cached @Exclusive DispatchNode dispatch,
9494
@Cached @Shared("error") BranchProfile errorProfile) throws InvalidArrayIndexException {
9595
if (inBounds(index)) {
9696
// FIXME (pitr 11-Feb-2020): use delete-at node directly

src/main/java/org/truffleruby/core/basicobject/BasicObjectNodes.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.truffleruby.core.basicobject.BasicObjectNodesFactory.InstanceExecNodeFactory;
2020
import org.truffleruby.core.basicobject.BasicObjectNodesFactory.ReferenceEqualNodeFactory;
2121
import org.truffleruby.core.cast.BooleanCastNode;
22+
import org.truffleruby.core.cast.NameToJavaStringNode;
2223
import org.truffleruby.core.exception.ExceptionOperations;
2324
import org.truffleruby.core.exception.RubyException;
2425
import org.truffleruby.core.klass.RubyClass;
@@ -40,13 +41,14 @@
4041
import org.truffleruby.language.arguments.ReadCallerFrameNode;
4142
import org.truffleruby.language.arguments.RubyArguments;
4243
import org.truffleruby.language.control.RaiseException;
43-
import org.truffleruby.language.dispatch.CallDispatchHeadNode;
44+
import org.truffleruby.language.dispatch.DispatchNode;
4445
import org.truffleruby.language.dispatch.RubyCallNode;
4546
import org.truffleruby.language.eval.CreateEvalSourceNode;
4647
import org.truffleruby.language.loader.CodeLoader;
4748
import org.truffleruby.language.methods.DeclarationContext;
4849
import org.truffleruby.language.methods.DeclarationContext.SingletonClassOfSelfDefaultDefinee;
4950
import org.truffleruby.language.methods.InternalMethod;
51+
import org.truffleruby.language.methods.LookupMethodNode;
5052
import org.truffleruby.language.methods.UnsupportedOperationBehavior;
5153
import org.truffleruby.language.objects.AllocateHelperNode;
5254
import org.truffleruby.language.objects.ObjectIDOperations;
@@ -75,6 +77,7 @@
7577
import com.oracle.truffle.api.object.Shape;
7678
import com.oracle.truffle.api.profiles.ConditionProfile;
7779

80+
7881
@CoreModule(value = "BasicObject", isClass = true)
7982
public abstract class BasicObjectNodes {
8083

@@ -92,7 +95,7 @@ protected boolean not(Object value,
9295
@CoreMethod(names = "!=", required = 1)
9396
public abstract static class NotEqualNode extends CoreMethodArrayArgumentsNode {
9497

95-
@Child private CallDispatchHeadNode equalNode = CallDispatchHeadNode.createPrivate();
98+
@Child private DispatchNode equalNode = DispatchNode.create();
9699
@Child private BooleanCastNode booleanCastNode = BooleanCastNode.create();
97100

98101
@Specialization
@@ -557,8 +560,7 @@ private boolean lastCallWasSuper(FrameAndCallNode callerFrame) {
557560
return superCallNode != null;
558561
}
559562

560-
/** See {@link org.truffleruby.language.dispatch.DispatchNode#lookup}. The only way to fail if method is not
561-
* null and not undefined is visibility. */
563+
/** See {@link LookupMethodNode}. The only way to fail if method is not null and not undefined is visibility. */
562564
private Visibility lastCallWasCallingPrivateOrProtectedMethod(Object self, String name,
563565
FrameAndCallNode callerFrame) {
564566
final DeclarationContext declarationContext = RubyArguments.tryGetDeclarationContext(callerFrame.frame);
@@ -581,8 +583,9 @@ private boolean lastCallWasVCall(FrameAndCallNode callerFrame) {
581583
@CoreMethod(names = "__send__", needsBlock = true, rest = true, required = 1)
582584
public abstract static class SendNode extends CoreMethodArrayArgumentsNode {
583585

584-
@Child private CallDispatchHeadNode dispatchNode = CallDispatchHeadNode.createPrivate();
586+
@Child private DispatchNode dispatchNode = DispatchNode.create(DispatchNode.PRIVATE);
585587
@Child private ReadCallerFrameNode readCallerFrame = ReadCallerFrameNode.create();
588+
@Child private NameToJavaStringNode nameToJavaString = NameToJavaStringNode.create();
586589

587590
@Specialization
588591
protected Object send(VirtualFrame frame, Object self, Object name, Object[] args, NotProvided block) {
@@ -594,7 +597,7 @@ protected Object send(VirtualFrame frame, Object self, Object name, Object[] arg
594597
DeclarationContext context = RubyArguments.getDeclarationContext(readCallerFrame.execute(frame));
595598
RubyArguments.setDeclarationContext(frame, context);
596599

597-
return dispatchNode.dispatch(frame, self, name, block, args);
600+
return dispatchNode.dispatch(frame, self, nameToJavaString.execute(name), block, args);
598601
}
599602

600603
}

0 commit comments

Comments
 (0)