Skip to content

Commit 70559ba

Browse files
committed
Refactor ToIntNode. Get rid of CreateCast
1 parent 3cd92ba commit 70559ba

File tree

5 files changed

+18
-58
lines changed

5 files changed

+18
-58
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,8 +1210,9 @@ protected RubyArray initializeWithSizeAndValue(RubyArray array, int size, Object
12101210

12111211
@Specialization(
12121212
guards = { "wasProvided(size)", "!isImplicitLong(size)", "wasProvided(fillingValue)" })
1213-
protected RubyArray initializeSizeOther(RubyArray array, Object size, Object fillingValue, Nil block) {
1214-
int intSize = toInt(size);
1213+
protected RubyArray initializeSizeOther(RubyArray array, Object size, Object fillingValue, Nil block,
1214+
@Cached @Shared ToIntNode toIntNode) {
1215+
int intSize = toIntNode.execute(size);
12151216
return executeInitialize(array, intSize, fillingValue, block);
12161217
}
12171218

@@ -1255,7 +1256,8 @@ protected RubyArray initializeFromArray(
12551256

12561257
@Specialization(
12571258
guards = { "!isImplicitLong(object)", "wasProvided(object)", "!isRubyArray(object)" })
1258-
protected RubyArray initialize(RubyArray array, Object object, NotProvided unusedValue, Nil block) {
1259+
protected RubyArray initialize(RubyArray array, Object object, NotProvided unusedValue, Nil block,
1260+
@Cached @Shared ToIntNode toIntNode) {
12591261
RubyArray copy = null;
12601262
if (respondToToAry(object)) {
12611263
Object toAryResult = callToAry(object);
@@ -1267,7 +1269,7 @@ protected RubyArray initialize(RubyArray array, Object object, NotProvided unuse
12671269
if (copy != null) {
12681270
return executeInitialize(array, copy, NotProvided.INSTANCE, nil);
12691271
} else {
1270-
int size = toInt(object);
1272+
int size = toIntNode.execute(object);
12711273
return executeInitialize(array, size, NotProvided.INSTANCE, nil);
12721274
}
12731275
}
@@ -1288,14 +1290,6 @@ protected Object callToAry(Object object) {
12881290
return toAryNode.call(object, "to_ary");
12891291
}
12901292

1291-
protected int toInt(Object value) {
1292-
if (toIntNode == null) {
1293-
CompilerDirectives.transferToInterpreterAndInvalidate();
1294-
toIntNode = insert(ToIntNode.create());
1295-
}
1296-
return toIntNode.execute(value);
1297-
}
1298-
12991293
}
13001294

13011295
@CoreMethod(names = "initialize_copy", required = 1, raiseIfFrozenSelf = true)

src/main/java/org/truffleruby/core/cast/ToIntNode.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
import com.oracle.truffle.api.dsl.Cached;
1313
import com.oracle.truffle.api.dsl.Fallback;
1414
import com.oracle.truffle.api.dsl.GenerateUncached;
15-
import com.oracle.truffle.api.dsl.NeverDefault;
16-
import com.oracle.truffle.api.dsl.NodeChild;
1715
import com.oracle.truffle.api.dsl.Specialization;
1816
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
1917
import org.truffleruby.core.CoreLibrary;
2018
import org.truffleruby.core.numeric.IntegerNodes.IntegerLowerNode;
2119
import org.truffleruby.core.numeric.RubyBignum;
2220
import org.truffleruby.language.Nil;
23-
import org.truffleruby.language.RubyBaseNodeWithExecute;
21+
import org.truffleruby.language.RubyBaseNode;
2422
import org.truffleruby.language.control.RaiseException;
2523
import org.truffleruby.language.dispatch.DispatchNode;
2624
import org.truffleruby.utils.Utils;
@@ -48,22 +46,10 @@
4846
* </ul>
4947
*/
5048
@GenerateUncached
51-
@NodeChild(value = "childNode", type = RubyBaseNodeWithExecute.class)
52-
public abstract class ToIntNode extends RubyBaseNodeWithExecute {
53-
54-
@NeverDefault
55-
public static ToIntNode create() {
56-
return ToIntNodeGen.create(null);
57-
}
58-
59-
public static ToIntNode create(RubyBaseNodeWithExecute child) {
60-
return ToIntNodeGen.create(child);
61-
}
49+
public abstract class ToIntNode extends RubyBaseNode {
6250

6351
public abstract int execute(Object object);
6452

65-
abstract RubyBaseNodeWithExecute getChildNode();
66-
6753
@Specialization
6854
protected int coerceInt(int value) {
6955
return value;
@@ -104,7 +90,7 @@ protected int coerceDouble(double value,
10490
}
10591

10692
@Specialization
107-
protected long coerceNil(Nil value) {
93+
protected int coerceNil(Nil value) {
10894
// MRI hardcodes this specific error message, which is slightly different from the one we would get in the
10995
// catch-all case.
11096
throw new RaiseException(
@@ -120,10 +106,4 @@ protected int coerceObject(Object object,
120106
.call(coreLibrary().truffleTypeModule, "rb_to_int_fallback", object);
121107
return fitNode.execute(coerced);
122108
}
123-
124-
@Override
125-
public RubyBaseNodeWithExecute cloneUninitialized() {
126-
return create(getChildNode().cloneUninitialized());
127-
}
128-
129109
}

src/main/java/org/truffleruby/core/format/format/FormatCharacterNode.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.oracle.truffle.api.strings.TruffleString.FromCodePointNode;
1616
import com.oracle.truffle.api.strings.TruffleString.ForceEncodingNode;
1717
import org.truffleruby.core.cast.ToIntNode;
18-
import org.truffleruby.core.cast.ToIntNodeGen;
1918
import org.truffleruby.core.encoding.RubyEncoding;
2019
import org.truffleruby.core.format.FormatNode;
2120
import org.truffleruby.core.format.convert.ToStrNode;
@@ -37,7 +36,6 @@ public abstract class FormatCharacterNode extends FormatNode {
3736

3837
private final RubyEncoding encoding;
3938

40-
@Child private ToIntNode toIntegerNode;
4139
@Child private ToStrNode toStrNode;
4240
@Child private FromCodePointNode fromCodePointNode;
4341
@Child private CodePointLengthNode codePointLengthNode;
@@ -49,13 +47,14 @@ public FormatCharacterNode(RubyEncoding encoding) {
4947

5048
@Specialization
5149
protected RubyString format(Object value,
50+
@Cached ToIntNode toIntNode,
5251
@Cached RubyStringLibrary strings) {
53-
final TruffleString character = getCharacter(value, strings);
52+
final TruffleString character = getCharacter(value, strings, toIntNode);
5453
return createString(character, encoding);
5554
}
5655

5756
@TruffleBoundary
58-
protected TruffleString getCharacter(Object value, RubyStringLibrary strings) {
57+
protected TruffleString getCharacter(Object value, RubyStringLibrary strings, ToIntNode toIntNode) {
5958
final TruffleString character;
6059

6160
Object stringArgument;
@@ -66,7 +65,7 @@ protected TruffleString getCharacter(Object value, RubyStringLibrary strings) {
6665
}
6766

6867
if (stringArgument == null || RubyGuards.isNil(stringArgument)) {
69-
final int codepointArgument = toIntegerNode().execute(value);
68+
final int codepointArgument = toIntNode.execute(value);
7069
character = fromCodePointNode().execute(codepointArgument, encoding.tencoding);
7170

7271
if (character == null) {
@@ -102,15 +101,6 @@ private ToStrNode toStrNode() {
102101
return toStrNode;
103102
}
104103

105-
private ToIntNode toIntegerNode() {
106-
if (toIntegerNode == null) {
107-
CompilerDirectives.transferToInterpreterAndInvalidate();
108-
toIntegerNode = insert(ToIntNodeGen.create(null));
109-
}
110-
111-
return toIntegerNode;
112-
}
113-
114104
private FromCodePointNode fromCodePointNode() {
115105
if (fromCodePointNode == null) {
116106
CompilerDirectives.transferToInterpreterAndInvalidate();

src/main/java/org/truffleruby/core/support/TypeNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ protected long numToLong(Object value) {
424424

425425
@Primitive(name = "rb_num2int")
426426
public abstract static class RbNum2IntPrimitiveNode extends PrimitiveArrayArgumentsNode {
427-
@Child private ToIntNode toIntNode = ToIntNode.create();
428427

429428
@Specialization
430-
protected int numToInt(Object value) {
429+
protected int numToInt(Object value,
430+
@Cached ToIntNode toIntNode) {
431431
return toIntNode.execute(value);
432432
}
433433
}

src/main/java/org/truffleruby/stdlib/readline/ReadlineHistoryNodes.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import org.truffleruby.language.control.RaiseException;
5959

6060
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
61-
import com.oracle.truffle.api.dsl.CreateCast;
6261
import com.oracle.truffle.api.dsl.NodeChild;
6362
import com.oracle.truffle.api.dsl.Specialization;
6463
import org.truffleruby.language.yield.CallBlockNode;
@@ -220,15 +219,12 @@ protected Object getIndex(int index) {
220219
@NodeChild(value = "line", type = RubyNode.class)
221220
public abstract static class SetIndexNode extends CoreMethodNode {
222221

223-
@CreateCast("index")
224-
protected ToIntNode coerceIndexToInt(RubyBaseNodeWithExecute index) {
225-
return ToIntNode.create(index);
226-
}
227-
228222
@TruffleBoundary
229223
@Specialization
230-
protected Object setIndex(int index, Object line,
224+
protected Object setIndex(Object indexObject, Object line,
225+
@Cached ToIntNode toIntNode,
231226
@Cached ToJavaStringNode toJavaStringNode) {
227+
final int index = toIntNode.execute(indexObject);
232228
final var lineAsString = toJavaStringNode.execute(this, line);
233229
final ConsoleHolder consoleHolder = getContext().getConsoleHolder();
234230

0 commit comments

Comments
 (0)