Skip to content

Commit 7fbb0ec

Browse files
committed
Return a CallTarget instead of RubyRootNode for TranslatorDriver#parse
* That way the many callers don't need to be careful about creating only 1 CallTarget for that RubyRootNode, parse() already does it.
1 parent a121d85 commit 7fbb0ec

File tree

12 files changed

+53
-79
lines changed

12 files changed

+53
-79
lines changed

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
import org.truffleruby.language.RubyEvalInteractiveRootNode;
9292
import org.truffleruby.language.RubyInlineParsingRequestNode;
9393
import org.truffleruby.language.RubyParsingRequestNode;
94-
import org.truffleruby.language.RubyRootNode;
9594
import org.truffleruby.language.objects.RubyObjectType;
9695
import org.truffleruby.language.objects.classvariables.ClassVariableStorage;
9796
import org.truffleruby.options.LanguageOptions;
@@ -449,14 +448,13 @@ protected RootCallTarget parse(ParsingRequest request) {
449448
request.getSource(),
450449
parsingParameters.getPath(),
451450
parsingParameters.getRope());
452-
final RubyRootNode rootNode = RubyLanguage.getCurrentContext().getCodeLoader().parse(
451+
return RubyLanguage.getCurrentContext().getCodeLoader().parse(
453452
rubySource,
454453
ParserContext.TOP_LEVEL,
455454
null,
456455
null,
457456
true,
458457
parsingParameters.getCurrentNode());
459-
return Truffle.getRuntime().createCallTarget(rootNode);
460458
}
461459

462460
if (request.getSource().isInteractive()) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.truffleruby.language.RubyDynamicObject;
4646
import org.truffleruby.language.RubyGuards;
4747
import org.truffleruby.language.RubyNode;
48-
import org.truffleruby.language.RubyRootNode;
4948
import org.truffleruby.language.RubySourceNode;
5049
import org.truffleruby.language.Visibility;
5150
import org.truffleruby.language.arguments.ReadCallerFrameNode;
@@ -396,7 +395,7 @@ private Object instanceEvalHelper(MaterializedFrame callerFrame, Object receiver
396395
final RubySource source = createEvalSourceNode
397396
.createEvalSource(stringRope, "instance_eval", fileNameString, line);
398397

399-
final RubyRootNode rootNode = getContext().getCodeLoader().parse(
398+
final RootCallTarget callTarget = getContext().getCodeLoader().parse(
400399
source,
401400
ParserContext.EVAL,
402401
callerFrame,
@@ -410,9 +409,9 @@ private Object instanceEvalHelper(MaterializedFrame callerFrame, Object receiver
410409
DeclarationContext.NO_REFINEMENTS);
411410

412411
final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(
412+
callTarget,
413413
ParserContext.EVAL,
414414
declarationContext,
415-
rootNode,
416415
callerFrame,
417416
receiver);
418417

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
import org.truffleruby.language.methods.DeclarationContext;
122122
import org.truffleruby.language.methods.GetMethodObjectNode;
123123
import org.truffleruby.language.methods.InternalMethod;
124+
import org.truffleruby.language.methods.SharedMethodInfo;
124125
import org.truffleruby.language.objects.AllocationTracing;
125126
import org.truffleruby.language.objects.CheckIVarNameNode;
126127
import org.truffleruby.language.objects.IsANode;
@@ -139,7 +140,6 @@
139140
import com.oracle.truffle.api.CompilerDirectives;
140141
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
141142
import com.oracle.truffle.api.RootCallTarget;
142-
import com.oracle.truffle.api.Truffle;
143143
import com.oracle.truffle.api.dsl.Cached;
144144
import com.oracle.truffle.api.dsl.CreateCast;
145145
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -731,18 +731,6 @@ public abstract static class EvalNode extends PrimitiveArrayArgumentsNode {
731731

732732
@Child private CreateEvalSourceNode createEvalSourceNode = new CreateEvalSourceNode();
733733

734-
protected static class RootNodeWrapper {
735-
private final RubyRootNode rootNode;
736-
737-
public RootNodeWrapper(RubyRootNode rootNode) {
738-
this.rootNode = rootNode;
739-
}
740-
741-
public RubyRootNode getRootNode() {
742-
return rootNode;
743-
}
744-
}
745-
746734
public abstract Object execute(VirtualFrame frame, Object target, Object source, RubyBinding binding,
747735
Object file, int line);
748736

@@ -756,7 +744,7 @@ public abstract Object execute(VirtualFrame frame, Object target, Object source,
756744
"equalNode.execute(libSource.getRope(source), cachedSource)",
757745
"equalNode.execute(libFile.getRope(file), cachedFile)",
758746
"line == cachedLine",
759-
"!assignsNewUserVariables(getDescriptor(cachedRootNode))",
747+
"!assignsNewUserVariables(getDescriptor(cachedCallTarget))",
760748
"bindingDescriptor == getBindingDescriptor(binding)" },
761749
limit = "getCacheLimit()")
762750
protected Object evalBindingNoAddsVarsCached(
@@ -767,12 +755,11 @@ protected Object evalBindingNoAddsVarsCached(
767755
@Cached("libFile.getRope(file)") Rope cachedFile,
768756
@Cached("line") int cachedLine,
769757
@Cached("getBindingDescriptor(binding)") FrameDescriptor bindingDescriptor,
770-
@Cached("compileSource(cachedSource, getBindingFrame(binding), cachedFile, cachedLine)") RootNodeWrapper cachedRootNode,
771-
@Cached("createCallTarget(cachedRootNode)") RootCallTarget cachedCallTarget,
758+
@Cached("compileSource(cachedSource, getBindingFrame(binding), cachedFile, cachedLine)") RootCallTarget cachedCallTarget,
772759
@Cached("create(cachedCallTarget)") DirectCallNode callNode,
773760
@Cached RopeNodes.EqualNode equalNode) {
774761
final MaterializedFrame parentFrame = binding.getFrame();
775-
return eval(target, cachedRootNode, cachedCallTarget, callNode, parentFrame);
762+
return eval(target, cachedCallTarget, callNode, parentFrame);
776763
}
777764

778765
@Specialization(
@@ -782,8 +769,8 @@ protected Object evalBindingNoAddsVarsCached(
782769
"equalNode.execute(libSource.getRope(source), cachedSource)",
783770
"equalNode.execute(libFile.getRope(file), cachedFile)",
784771
"line == cachedLine",
785-
"assignsNewUserVariables(getDescriptor(cachedRootNode))",
786-
"!assignsNewUserVariables(getDescriptor(rootNodeToEval))",
772+
"assignsNewUserVariables(getDescriptor(firstCallTarget))",
773+
"!assignsNewUserVariables(getDescriptor(cachedCallTarget))",
787774
"bindingDescriptor == getBindingDescriptor(binding)" },
788775
limit = "getCacheLimit()")
789776
protected Object evalBindingAddsVarsCached(
@@ -794,14 +781,13 @@ protected Object evalBindingAddsVarsCached(
794781
@Cached("libFile.getRope(file)") Rope cachedFile,
795782
@Cached("line") int cachedLine,
796783
@Cached("getBindingDescriptor(binding)") FrameDescriptor bindingDescriptor,
797-
@Cached("compileSource(cachedSource, getBindingFrame(binding), cachedFile, cachedLine)") RootNodeWrapper cachedRootNode,
798-
@Cached("getDescriptor(cachedRootNode).copy()") FrameDescriptor newBindingDescriptor,
799-
@Cached("compileSource(cachedSource, getBindingFrame(binding), newBindingDescriptor, cachedFile, cachedLine)") RootNodeWrapper rootNodeToEval,
800-
@Cached("createCallTarget(rootNodeToEval)") RootCallTarget cachedCallTarget,
784+
@Cached("compileSource(cachedSource, getBindingFrame(binding), cachedFile, cachedLine)") RootCallTarget firstCallTarget,
785+
@Cached("getDescriptor(firstCallTarget).copy()") FrameDescriptor newBindingDescriptor,
786+
@Cached("compileSource(cachedSource, getBindingFrame(binding), newBindingDescriptor, cachedFile, cachedLine)") RootCallTarget cachedCallTarget,
801787
@Cached("create(cachedCallTarget)") DirectCallNode callNode,
802788
@Cached RopeNodes.EqualNode equalNode) {
803789
final MaterializedFrame parentFrame = BindingNodes.newFrame(binding, newBindingDescriptor);
804-
return eval(target, rootNodeToEval, cachedCallTarget, callNode, parentFrame);
790+
return eval(target, cachedCallTarget, callNode, parentFrame);
805791
}
806792

807793
@Specialization(guards = { "libSource.isRubyString(source)", "libFile.isRubyString(file)" })
@@ -818,14 +804,15 @@ protected Object evalBindingUncached(Object target, Object source, RubyBinding b
818804
return deferredCall.call(callNode);
819805
}
820806

821-
private Object eval(Object target, RootNodeWrapper rootNode, RootCallTarget callTarget, DirectCallNode callNode,
807+
private Object eval(Object target, RootCallTarget callTarget, DirectCallNode callNode,
822808
MaterializedFrame parentFrame) {
809+
final SharedMethodInfo sharedMethodInfo = RubyRootNode.of(callTarget).getSharedMethodInfo();
823810
final InternalMethod method = new InternalMethod(
824811
getContext(),
825-
rootNode.getRootNode().getSharedMethodInfo(),
812+
sharedMethodInfo,
826813
RubyArguments.getMethod(parentFrame).getLexicalScope(),
827814
RubyArguments.getDeclarationContext(parentFrame),
828-
rootNode.getRootNode().getSharedMethodInfo().getMethodNameForNotBlock(),
815+
sharedMethodInfo.getMethodNameForNotBlock(),
829816
RubyArguments.getMethod(parentFrame).getDeclaringModule(),
830817
Visibility.PUBLIC,
831818
callTarget);
@@ -845,19 +832,19 @@ private CodeLoader.DeferredCall doEvalX(Object target, Rope source, RubyBinding
845832
final MaterializedFrame frame = BindingNodes.newFrame(binding.getFrame());
846833
final DeclarationContext declarationContext = RubyArguments.getDeclarationContext(frame);
847834
final FrameDescriptor descriptor = frame.getFrameDescriptor();
848-
RubyRootNode rootNode = buildRootNode(source, frame, file, line, false);
835+
RootCallTarget callTarget = parse(source, frame, file, line, false);
849836
if (assignsNewUserVariables(descriptor)) {
850837
binding.setFrame(frame);
851838
}
852839
return getContext().getCodeLoader().prepareExecute(
840+
callTarget,
853841
ParserContext.EVAL,
854842
declarationContext,
855-
rootNode,
856843
frame,
857844
target);
858845
}
859846

860-
protected RubyRootNode buildRootNode(Rope sourceText, MaterializedFrame parentFrame, Rope file, int line,
847+
protected RootCallTarget parse(Rope sourceText, MaterializedFrame parentFrame, Rope file, int line,
861848
boolean ownScopeForAssignments) {
862849
//intern() to improve footprint
863850
final String sourceFile = RopeOperations.decodeRope(file).intern();
@@ -867,25 +854,21 @@ protected RubyRootNode buildRootNode(Rope sourceText, MaterializedFrame parentFr
867854
.parse(source, ParserContext.EVAL, parentFrame, null, ownScopeForAssignments, this);
868855
}
869856

870-
protected RootNodeWrapper compileSource(Rope sourceText, MaterializedFrame parentFrame, Rope file, int line) {
871-
return new RootNodeWrapper(buildRootNode(sourceText, parentFrame, file, line, true));
857+
protected RootCallTarget compileSource(Rope sourceText, MaterializedFrame parentFrame, Rope file, int line) {
858+
return parse(sourceText, parentFrame, file, line, true);
872859
}
873860

874-
protected RootNodeWrapper compileSource(Rope sourceText, MaterializedFrame parentFrame,
861+
protected RootCallTarget compileSource(Rope sourceText, MaterializedFrame parentFrame,
875862
FrameDescriptor additionalVariables, Rope file, int line) {
876863
return compileSource(sourceText, BindingNodes.newFrame(parentFrame, additionalVariables), file, line);
877864
}
878865

879-
protected RootCallTarget createCallTarget(RootNodeWrapper rootNode) {
880-
return Truffle.getRuntime().createCallTarget(rootNode.rootNode);
881-
}
882-
883866
protected FrameDescriptor getBindingDescriptor(RubyBinding binding) {
884867
return BindingNodes.getFrameDescriptor(binding);
885868
}
886869

887-
protected FrameDescriptor getDescriptor(RootNodeWrapper rootNode) {
888-
return rootNode.getRootNode().getFrameDescriptor();
870+
protected FrameDescriptor getDescriptor(RootCallTarget callTarget) {
871+
return RubyRootNode.of(callTarget).getFrameDescriptor();
889872
}
890873

891874
protected MaterializedFrame getBindingFrame(RubyBinding binding) {

src/main/java/org/truffleruby/core/kernel/TruffleKernelNodes.java

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

1212
import java.io.IOException;
1313

14+
import com.oracle.truffle.api.RootCallTarget;
1415
import com.oracle.truffle.api.frame.Frame;
1516
import com.oracle.truffle.api.library.CachedLibrary;
1617
import com.oracle.truffle.api.source.Source;
@@ -118,7 +119,7 @@ protected boolean load(Object file, boolean wrap,
118119
wrapModule = null;
119120
}
120121

121-
final RubyRootNode rootNode = getContext()
122+
final RootCallTarget callTarget = getContext()
122123
.getCodeLoader()
123124
.parse(source, ParserContext.TOP_LEVEL, null, wrapModule, true, this);
124125

@@ -136,9 +137,9 @@ protected boolean load(Object file, boolean wrap,
136137
}
137138

138139
final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(
140+
callTarget,
139141
ParserContext.TOP_LEVEL,
140142
declarationContext,
141-
rootNode,
142143
null,
143144
self);
144145

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ private CodeLoader.DeferredCall classEvalSourceInternal(RubyModule module, Objec
764764
file,
765765
line);
766766

767-
final RubyRootNode rootNode = getContext().getCodeLoader().parse(
767+
final RootCallTarget callTarget = getContext().getCodeLoader().parse(
768768
source,
769769
ParserContext.MODULE,
770770
callerFrame,
@@ -773,12 +773,12 @@ private CodeLoader.DeferredCall classEvalSourceInternal(RubyModule module, Objec
773773
this);
774774

775775
return getContext().getCodeLoader().prepareExecute(
776+
callTarget,
776777
ParserContext.MODULE,
777778
new DeclarationContext(
778779
Visibility.PUBLIC,
779780
new FixedDefaultDefinee(module),
780781
DeclarationContext.NO_REFINEMENTS),
781-
rootNode,
782782
callerFrame,
783783
module);
784784
}

src/main/java/org/truffleruby/debug/DebugHelpers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
*/
1010
package org.truffleruby.debug;
1111

12+
import com.oracle.truffle.api.RootCallTarget;
1213
import org.truffleruby.RubyContext;
1314
import org.truffleruby.RubyLanguage;
1415
import org.truffleruby.language.Nil;
1516
import org.truffleruby.language.RubyNode;
16-
import org.truffleruby.language.RubyRootNode;
1717
import org.truffleruby.language.arguments.RubyArguments;
1818
import org.truffleruby.language.loader.CodeLoader;
1919
import org.truffleruby.language.methods.DeclarationContext;
@@ -72,14 +72,14 @@ public static Object eval(RubyContext context, String code, Object... arguments)
7272

7373
final Source source = Source.newBuilder(TruffleRuby.LANGUAGE_ID, code, "debug-eval").build();
7474

75-
final RubyRootNode rootNode = context
75+
final RootCallTarget callTarget = context
7676
.getCodeLoader()
7777
.parse(new RubySource(source, "debug-eval"), ParserContext.INLINE, evalFrame, null, true, null);
7878

7979
final CodeLoader.DeferredCall deferredCall = context.getCodeLoader().prepareExecute(
80+
callTarget,
8081
ParserContext.INLINE,
8182
declarationContext,
82-
rootNode,
8383
evalFrame,
8484
RubyArguments.getSelf(evalFrame));
8585

src/main/java/org/truffleruby/language/RubyInlineParsingRequestNode.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public RubyInlineParsingRequestNode(
4646

4747
// We use the current frame as the lexical scope to parse, but then we may run with a new frame in the future
4848
final TranslatorDriver translator = new TranslatorDriver(context, rubySource);
49-
final RubyRootNode rootNode = translator.parse(
49+
final RootCallTarget callTarget = translator.parse(
5050
rubySource,
5151
ParserContext.INLINE,
5252
null,
@@ -55,12 +55,10 @@ public RubyInlineParsingRequestNode(
5555
false,
5656
null);
5757

58-
final RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(rootNode);
59-
6058
callNode = insert(Truffle.getRuntime().createDirectCallNode(callTarget));
6159
callNode.forceInlining();
6260

63-
final SharedMethodInfo sharedMethodInfo = rootNode.getSharedMethodInfo();
61+
final SharedMethodInfo sharedMethodInfo = RubyRootNode.of(callTarget).getSharedMethodInfo();
6462
this.method = new InternalMethod(
6563
context,
6664
sharedMethodInfo,

src/main/java/org/truffleruby/language/RubyParsingRequestNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ public RubyParsingRequestNode(RubyLanguage language, RubyContext context, Source
4444
final RubySource rubySource = new RubySource(source, language.getSourcePath(source));
4545

4646
final TranslatorDriver translator = new TranslatorDriver(context, rubySource);
47-
final RubyRootNode rootNode = translator.parse(
47+
callTarget = translator.parse(
4848
rubySource,
4949
ParserContext.TOP_LEVEL,
5050
argumentNames,
5151
null,
5252
null,
5353
true,
5454
null);
55-
callTarget = Truffle.getRuntime().createCallTarget(rootNode);
5655

5756
callNode = insert(Truffle.getRuntime().createDirectCallNode(callTarget));
5857
callNode.forceInlining();

src/main/java/org/truffleruby/language/TruffleBootNodes.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.List;
1515
import java.util.stream.Collectors;
1616

17+
import com.oracle.truffle.api.RootCallTarget;
1718
import com.oracle.truffle.api.library.CachedLibrary;
1819
import com.oracle.truffle.api.nodes.NodeUtil;
1920
import org.graalvm.options.OptionDescriptor;
@@ -133,7 +134,7 @@ protected int main(Object kind, Object toExecute,
133134
if (getContext().getOptions().SYNTAX_CHECK) {
134135
checkSyntax.call(coreLibrary().truffleBootModule, "check_syntax", source);
135136
} else {
136-
final RubyRootNode rootNode = getContext().getCodeLoader().parse(
137+
final RootCallTarget callTarget = getContext().getCodeLoader().parse(
137138
source,
138139
ParserContext.TOP_LEVEL_FIRST,
139140
null,
@@ -142,9 +143,9 @@ protected int main(Object kind, Object toExecute,
142143
null);
143144

144145
final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(
146+
callTarget,
145147
ParserContext.TOP_LEVEL_FIRST,
146148
DeclarationContext.topLevel(getContext()),
147-
rootNode,
148149
null,
149150
coreLibrary().mainObject);
150151

@@ -292,9 +293,10 @@ public abstract static class InnerCheckSyntaxNode extends CoreMethodArrayArgumen
292293
@Specialization
293294
protected Object innerCheckSyntax(RubySource source) {
294295
RubyContext context = getContext();
295-
RubyRootNode rubyRootNode = context
296+
RootCallTarget callTarget = context
296297
.getCodeLoader()
297298
.parse(source, ParserContext.TOP_LEVEL, null, null, true, null);
299+
RubyRootNode rubyRootNode = RubyRootNode.of(callTarget);
298300
EmitWarningsNode emitWarningsNode = NodeUtil.findFirstNodeInstance(rubyRootNode, EmitWarningsNode.class);
299301
if (emitWarningsNode != null) {
300302
emitWarningsNode.printWarnings(context);

0 commit comments

Comments
 (0)