Skip to content

Commit 2eb0713

Browse files
committed
Remove FixnumLowerNode usage in CreateCast
1 parent 4c5e1fe commit 2eb0713

File tree

6 files changed

+72
-56
lines changed

6 files changed

+72
-56
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.truffleruby.core.module.ConstantLookupResult;
2828
import org.truffleruby.core.module.ModuleOperations;
2929
import org.truffleruby.core.module.RubyModule;
30-
import org.truffleruby.core.numeric.FixnumLowerNodeGen;
30+
import org.truffleruby.core.numeric.FixnumLowerNodeGen.FixnumLowerASTNodeGen;
3131
import org.truffleruby.core.string.StringUtils;
3232
import org.truffleruby.core.support.TypeNodes;
3333
import org.truffleruby.language.LexicalScope;
@@ -435,7 +435,7 @@ public static boolean needsSelf(CoreMethod method) {
435435

436436
private static RubyNode transformArgument(CoreMethod method, RubyNode argument, int n) {
437437
if (ArrayUtils.contains(method.lowerFixnum(), n)) {
438-
argument = FixnumLowerNodeGen.create(argument);
438+
argument = FixnumLowerASTNodeGen.create(argument);
439439
}
440440

441441
if (n == 0 && method.raiseIfFrozenSelf()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.truffleruby.RubyLanguage;
1313
import org.truffleruby.annotations.Primitive;
1414
import org.truffleruby.core.array.ArrayUtils;
15-
import org.truffleruby.core.numeric.FixnumLowerNodeGen;
15+
import org.truffleruby.core.numeric.FixnumLowerNodeGen.FixnumLowerASTNodeGen;
1616
import org.truffleruby.core.support.TypeNodes;
1717
import org.truffleruby.language.RubyBaseNode;
1818
import org.truffleruby.language.RubyNode;
@@ -49,7 +49,7 @@ public RubyNode createInvokePrimitiveNode(Source source, SourceIndexLength sourc
4949

5050
for (int n = 0; n < arguments.length; n++) {
5151
if (ArrayUtils.contains(annotation.lowerFixnum(), n)) {
52-
arguments[n] = FixnumLowerNodeGen.create(arguments[n]);
52+
arguments[n] = FixnumLowerASTNodeGen.create(arguments[n]);
5353
}
5454
if (ArrayUtils.contains(annotation.raiseIfFrozen(), n)) {
5555
arguments[n] = TypeNodes.TypeCheckFrozenNode.create(arguments[n]);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ protected static Object at(RubyArray array, Object index,
251251
@Cached FixnumLowerNode lowerNode,
252252
@Cached AtNode atNode,
253253
@Bind("this") Node node) {
254-
return atNode.executeAt(array, lowerNode.executeLower(toLongNode.execute(node, index)));
254+
return atNode.executeAt(array, lowerNode.execute(node, toLongNode.execute(node, index)));
255255
}
256256
}
257257

src/main/java/org/truffleruby/core/numeric/FixnumLowerNode.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
*/
1010
package org.truffleruby.core.numeric;
1111

12-
import com.oracle.truffle.api.dsl.NeverDefault;
12+
import com.oracle.truffle.api.dsl.Cached;
13+
import com.oracle.truffle.api.dsl.GenerateCached;
14+
import com.oracle.truffle.api.dsl.GenerateInline;
15+
import com.oracle.truffle.api.nodes.Node;
16+
import org.truffleruby.language.RubyBaseNode;
1317
import org.truffleruby.language.RubyBaseNodeWithExecute;
1418
import org.truffleruby.language.RubyContextSourceNode;
19+
import org.truffleruby.core.numeric.FixnumLowerNodeGen.FixnumLowerASTNodeGen;
1520

1621
import com.oracle.truffle.api.dsl.NodeChild;
1722
import com.oracle.truffle.api.dsl.Specialization;
@@ -24,45 +29,49 @@
2429
*
2530
* <p>
2631
* See {@link org.truffleruby.core.cast.ToIntNode} for a comparison of different integer conversion nodes. */
27-
@NodeChild(value = "valueNode", type = RubyBaseNodeWithExecute.class)
28-
public abstract class FixnumLowerNode extends RubyContextSourceNode {
32+
@GenerateCached(false)
33+
@GenerateInline
34+
public abstract class FixnumLowerNode extends RubyBaseNode {
2935

30-
@NeverDefault
31-
public static FixnumLowerNode create() {
32-
return FixnumLowerNodeGen.create(null);
33-
}
34-
35-
public static FixnumLowerNode create(RubyBaseNodeWithExecute value) {
36-
return FixnumLowerNodeGen.create(value);
37-
}
38-
39-
public abstract Object executeLower(Object value);
40-
41-
abstract RubyBaseNodeWithExecute getValueNode();
36+
public abstract Object execute(Node node, Object value);
4237

4338
@Specialization
44-
protected int lower(int value) {
39+
protected static int lower(int value) {
4540
return value;
4641
}
4742

4843
@Specialization(guards = "fitsInInteger(value)")
49-
protected int lower(long value) {
44+
protected static int lower(long value) {
5045
return (int) value;
5146
}
5247

5348
@Specialization(guards = "!fitsInInteger(value)")
54-
protected long lowerFails(long value) {
49+
protected static long lowerFails(long value) {
5550
return value;
5651
}
5752

5853
@Specialization(guards = "!isImplicitLong(value)")
59-
protected Object passThrough(Object value) {
54+
protected static Object passThrough(Object value) {
6055
return value;
6156
}
6257

63-
public RubyNode cloneUninitialized() {
64-
var copy = create(getValueNode().cloneUninitialized());
65-
return copy.copyFlags(this);
58+
@NodeChild(value = "valueNode", type = RubyBaseNodeWithExecute.class)
59+
public abstract static class FixnumLowerASTNode extends RubyContextSourceNode {
60+
61+
protected abstract RubyBaseNodeWithExecute getValueNode();
62+
63+
@Specialization
64+
protected Object doFixnumLower(Object value,
65+
@Cached FixnumLowerNode fixnumLowerNode) {
66+
return fixnumLowerNode.execute(this, value);
67+
68+
}
69+
70+
@Override
71+
public RubyNode cloneUninitialized() {
72+
var copy = FixnumLowerASTNodeGen.create(getValueNode().cloneUninitialized());
73+
return copy.copyFlags(this);
74+
}
6675
}
6776

6877
}

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
import org.truffleruby.Layouts;
103103
import org.truffleruby.annotations.CoreMethod;
104104
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
105-
import org.truffleruby.builtins.CoreMethodNode;
106105
import org.truffleruby.annotations.CoreModule;
107106
import org.truffleruby.annotations.Primitive;
108107
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
@@ -127,6 +126,7 @@
127126
import org.truffleruby.core.format.unpack.UnpackCompiler;
128127
import org.truffleruby.core.kernel.KernelNodes;
129128
import org.truffleruby.core.klass.RubyClass;
129+
import org.truffleruby.core.numeric.FixnumLowerNode;
130130
import org.truffleruby.core.numeric.FixnumOrBignumNode;
131131
import org.truffleruby.core.proc.RubyProc;
132132
import org.truffleruby.core.range.RangeNodes;
@@ -143,9 +143,7 @@
143143
import org.truffleruby.language.Nil;
144144
import org.truffleruby.language.NotProvided;
145145
import org.truffleruby.language.RubyBaseNode;
146-
import org.truffleruby.language.RubyBaseNodeWithExecute;
147146
import org.truffleruby.language.RubyGuards;
148-
import org.truffleruby.language.RubyNode;
149147
import org.truffleruby.annotations.Visibility;
150148
import org.truffleruby.language.arguments.ReadCallerVariablesNode;
151149
import org.truffleruby.language.control.DeferredRaiseException;
@@ -164,7 +162,6 @@
164162
import com.oracle.truffle.api.dsl.Cached;
165163
import com.oracle.truffle.api.dsl.Fallback;
166164
import com.oracle.truffle.api.dsl.ImportStatic;
167-
import com.oracle.truffle.api.dsl.NodeChild;
168165
import com.oracle.truffle.api.dsl.ReportPolymorphism;
169166
import com.oracle.truffle.api.dsl.Specialization;
170167
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -230,65 +227,74 @@ protected RubyString add(Object string, Object other,
230227
}
231228

232229
@CoreMethod(names = "*", required = 1)
233-
@NodeChild(value = "string", type = RubyNode.class)
234-
@NodeChild(value = "times", type = RubyBaseNodeWithExecute.class)
235230
@ImportStatic(StringGuards.class)
236-
public abstract static class MulNode extends CoreMethodNode {
231+
public abstract static class StringMulNode extends CoreMethodArrayArgumentsNode {
237232

238-
// @CreateCast("times")
239-
// protected RubyBaseNodeWithExecute coerceToInteger(RubyBaseNodeWithExecute times) {
240-
// // Not ToIntNode, because this works with empty strings, and must throw a different error
241-
// // for long values that don't fit in an int.
242-
// return FixnumLowerNode.create(ToLongNode.create(times));
243-
// }
233+
// Not ToIntNode, because this works with empty strings, and must throw a different error
234+
// for long values that don't fit in an int.
235+
@Specialization
236+
protected RubyString doMul(Object string, Object timesObject,
237+
@Cached FixnumLowerNode fixnumLowerNode,
238+
@Cached ToLongNode toLongNode,
239+
@Cached MulNode mulNode) {
240+
var times = fixnumLowerNode.execute(this, toLongNode.execute(this, timesObject));
241+
return mulNode.execute(this, string, times);
242+
}
243+
}
244+
245+
@GenerateCached(false)
246+
@GenerateInline
247+
public abstract static class MulNode extends RubyBaseNode {
248+
249+
public abstract RubyString execute(Node node, Object String, Object times);
244250

245251
@Specialization(guards = "times == 0")
246-
protected RubyString multiplyZero(Object string, int times,
252+
protected static RubyString multiplyZero(Node node, Object string, int times,
247253
@Cached @Shared RubyStringLibrary libString) {
248254
final RubyEncoding encoding = libString.getEncoding(string);
249-
return createString(encoding.tencoding.getEmpty(), encoding);
255+
return createString(node, encoding.tencoding.getEmpty(), encoding);
250256
}
251257

252258
@Specialization(guards = "times < 0")
253-
protected RubyString multiplyTimesNegative(Object string, long times) {
254-
throw new RaiseException(getContext(), coreExceptions().argumentError("negative argument", this));
259+
protected static RubyString multiplyTimesNegative(Node node, Object string, long times) {
260+
throw new RaiseException(getContext(node), coreExceptions(node).argumentError("negative argument", node));
255261
}
256262

257263
@Specialization(guards = { "times > 0", "!libString.getTString(string).isEmpty()" })
258-
protected RubyString multiply(Object string, int times,
264+
protected static RubyString multiply(Node node, Object string, int times,
259265
@Cached InlinedBranchProfile tooBigProfile,
260266
@Cached @Shared RubyStringLibrary libString,
261-
@Cached TruffleString.RepeatNode repeatNode) {
267+
@Cached(inline = false) TruffleString.RepeatNode repeatNode) {
262268
var tstring = libString.getTString(string);
263269
var encoding = libString.getEncoding(string);
264270

265271
long longLength = (long) times * tstring.byteLength(encoding.tencoding);
266272
if (longLength > Integer.MAX_VALUE) {
267-
tooBigProfile.enter(this);
268-
throw tooBig();
273+
tooBigProfile.enter(node);
274+
throw tooBig(node);
269275
}
270276

271-
return createString(repeatNode.execute(tstring, times, encoding.tencoding), encoding);
277+
return createString(node, repeatNode.execute(tstring, times, encoding.tencoding), encoding);
272278
}
273279

274280
@Specialization(guards = { "times > 0", "libString.getTString(string).isEmpty()" })
275-
protected RubyString multiplyEmpty(Object string, long times,
281+
protected static RubyString multiplyEmpty(Node node, Object string, long times,
276282
@Cached @Shared RubyStringLibrary libString) {
277283
var encoding = libString.getEncoding(string);
278-
return createString(encoding.tencoding.getEmpty(), encoding);
284+
return createString(node, encoding.tencoding.getEmpty(), encoding);
279285
}
280286

281287
@Specialization(guards = { "times > 0", "!libString.getTString(string).isEmpty()" })
282-
protected RubyString multiplyNonEmpty(Object string, long times,
288+
protected static RubyString multiplyNonEmpty(Node node, Object string, long times,
283289
@Cached @Shared RubyStringLibrary libString) {
284290
assert !CoreLibrary.fitsIntoInteger(times);
285-
throw tooBig();
291+
throw tooBig(node);
286292
}
287293

288-
private RaiseException tooBig() {
294+
private static RaiseException tooBig(Node node) {
289295
// MRI throws this error whenever the total size of the resulting string would exceed LONG_MAX.
290296
// In TruffleRuby, strings have max length Integer.MAX_VALUE.
291-
return new RaiseException(getContext(), coreExceptions().argumentError("argument too big", this));
297+
return new RaiseException(getContext(node), coreExceptions(node).argumentError("argument too big", node));
292298
}
293299
}
294300

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ protected int genRandInt(RubySecureRandomizer randomizer) {
4848
protected int genRandFallback(RubyCustomRandomizer randomizer,
4949
@Cached DispatchNode randomIntNode,
5050
@Cached FixnumLowerNode fixnumLowerNode) {
51-
return (int) fixnumLowerNode.executeLower(
51+
return (int) fixnumLowerNode.execute(
52+
this,
5253
randomIntNode.call(
5354
getContext().getCoreLibrary().truffleRandomOperationsModule,
5455
"obj_random_int",

0 commit comments

Comments
 (0)