Skip to content

Commit 5b21221

Browse files
committed
Moving the inlinable nodes to their respective classes for the readability purposes
1 parent 87763e9 commit 5b21221

File tree

11 files changed

+171
-253
lines changed

11 files changed

+171
-253
lines changed

src/main/java/org/truffleruby/core/binding/BindingNodes.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import com.oracle.truffle.api.frame.MaterializedFrame;
5858
import com.oracle.truffle.api.frame.VirtualFrame;
5959
import com.oracle.truffle.api.source.SourceSection;
60+
import org.truffleruby.language.locals.WriteFrameSlotNode;
61+
import org.truffleruby.language.locals.WriteFrameSlotNodeGen;
6062
import org.truffleruby.parser.ParentFrameDescriptor;
6163
import org.truffleruby.parser.TranslatorEnvironment;
6264

@@ -309,6 +311,34 @@ public RubyNode cloneUninitialized() {
309311
}
310312
}
311313

314+
@GenerateUncached
315+
@ImportStatic(BindingNodes.class)
316+
public abstract static class LocalVariableGetNode extends RubyBaseNode {
317+
318+
public abstract Object execute(RubyBinding binding, String name);
319+
320+
@Specialization(guards = "!isHiddenVariable(name)")
321+
protected Object localVariableGet(RubyBinding binding, String name,
322+
@Cached FindDeclarationVariableNodes.FindAndReadDeclarationVariableNode readNode) {
323+
MaterializedFrame frame = binding.getFrame();
324+
Object result = readNode.execute(frame, name, null);
325+
if (result == null) {
326+
throw new RaiseException(
327+
getContext(),
328+
coreExceptions().nameErrorLocalVariableNotDefined(name, binding, this));
329+
}
330+
return result;
331+
}
332+
333+
@TruffleBoundary
334+
@Specialization(guards = "isHiddenVariable(name)")
335+
protected Object localVariableGetLastLine(RubyBinding binding, String name) {
336+
throw new RaiseException(
337+
getContext(),
338+
coreExceptions().nameError("Bad local variable name", binding, name, this));
339+
}
340+
}
341+
312342
@ReportPolymorphism
313343
@GenerateNodeFactory
314344
@CoreMethod(names = "local_variable_set", required = 2)
@@ -347,6 +377,88 @@ public RubyNode cloneUninitialized() {
347377
}
348378
}
349379

380+
381+
@GenerateUncached
382+
@ImportStatic({ BindingNodes.class, FindDeclarationVariableNodes.class })
383+
public abstract static class LocalVariableSetNode extends RubyBaseNode {
384+
385+
public abstract Object execute(RubyBinding binding, String name, Object value);
386+
387+
@Specialization(
388+
guards = {
389+
"name == cachedName",
390+
"!isHiddenVariable(cachedName)",
391+
"getFrameDescriptor(binding) == cachedFrameDescriptor",
392+
"cachedFrameSlot != null" },
393+
limit = "getCacheLimit()")
394+
protected Object localVariableSetCached(RubyBinding binding, String name, Object value,
395+
@Cached("name") String cachedName,
396+
@Cached("getFrameDescriptor(binding)") FrameDescriptor cachedFrameDescriptor,
397+
@Cached("findFrameSlotOrNull(name, binding.getFrame())") FindDeclarationVariableNodes.FrameSlotAndDepth cachedFrameSlot,
398+
@Cached("createWriteNode(cachedFrameSlot.slot)") WriteFrameSlotNode writeLocalVariableNode) {
399+
final MaterializedFrame frame = RubyArguments
400+
.getDeclarationFrame(binding.getFrame(), cachedFrameSlot.depth);
401+
writeLocalVariableNode.executeWrite(frame, value);
402+
return value;
403+
}
404+
405+
@Specialization(
406+
guards = {
407+
"name == cachedName",
408+
"!isHiddenVariable(cachedName)",
409+
"getFrameDescriptor(binding) == cachedFrameDescriptor",
410+
"cachedFrameSlot == null" },
411+
limit = "getCacheLimit()")
412+
protected Object localVariableSetNewCached(RubyBinding binding, String name, Object value,
413+
@Cached("name") String cachedName,
414+
@Cached("getFrameDescriptor(binding)") FrameDescriptor cachedFrameDescriptor,
415+
@Cached("findFrameSlotOrNull(name, binding.getFrame())") FindDeclarationVariableNodes.FrameSlotAndDepth cachedFrameSlot,
416+
@Cached("newFrameDescriptor(cachedFrameDescriptor, name)") FrameDescriptor newDescriptor,
417+
@Cached("createWriteNode(NEW_VAR_INDEX)") WriteFrameSlotNode writeLocalVariableNode) {
418+
final MaterializedFrame frame = newFrame(binding, newDescriptor);
419+
writeLocalVariableNode.executeWrite(frame, value);
420+
return value;
421+
}
422+
423+
@TruffleBoundary
424+
@Specialization(
425+
guards = "!isHiddenVariable(name)",
426+
replaces = { "localVariableSetCached", "localVariableSetNewCached" })
427+
protected Object localVariableSetUncached(RubyBinding binding, String name, Object value) {
428+
MaterializedFrame frame = binding.getFrame();
429+
final FindDeclarationVariableNodes.FrameSlotAndDepth frameSlot = FindDeclarationVariableNodes
430+
.findFrameSlotOrNull(name, frame);
431+
final int slot;
432+
if (frameSlot != null) {
433+
frame = RubyArguments.getDeclarationFrame(frame, frameSlot.depth);
434+
slot = frameSlot.slot;
435+
} else {
436+
var newDescriptor = newFrameDescriptor(getFrameDescriptor(binding), name);
437+
frame = newFrame(binding, newDescriptor);
438+
assert newDescriptor.getSlotName(NEW_VAR_INDEX) == name;
439+
slot = NEW_VAR_INDEX;
440+
}
441+
frame.setObject(slot, value);
442+
return value;
443+
}
444+
445+
@TruffleBoundary
446+
@Specialization(guards = "isHiddenVariable(name)")
447+
protected Object localVariableSetLastLine(RubyBinding binding, String name, Object value) {
448+
throw new RaiseException(
449+
getContext(),
450+
coreExceptions().nameError("Bad local variable name", binding, name, this));
451+
}
452+
453+
protected WriteFrameSlotNode createWriteNode(int frameSlot) {
454+
return WriteFrameSlotNodeGen.create(frameSlot);
455+
}
456+
457+
protected int getCacheLimit() {
458+
return getLanguage().options.BINDING_LOCAL_VARIABLE_CACHE;
459+
}
460+
}
461+
350462
@Primitive(name = "local_variable_names")
351463
@ImportStatic(BindingNodes.class)
352464
public abstract static class LocalVariablesNode extends PrimitiveArrayArgumentsNode {

src/main/java/org/truffleruby/core/binding/LocalVariableGetNode.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/main/java/org/truffleruby/core/binding/LocalVariableSetNode.java

Lines changed: 0 additions & 110 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@
8282
import org.truffleruby.core.proc.RubyProc;
8383
import org.truffleruby.core.range.RangeNodes;
8484
import org.truffleruby.core.range.RubyIntOrLongRange;
85-
import org.truffleruby.core.string.AllocateNode;
8685
import org.truffleruby.core.string.RubyString;
8786
import org.truffleruby.core.string.StringHelperNodes;
87+
import org.truffleruby.core.string.StringNodes;
8888
import org.truffleruby.core.support.TypeNodes;
8989
import org.truffleruby.core.support.TypeNodes.CheckFrozenNode;
9090
import org.truffleruby.core.support.TypeNodes.ObjectInstanceVariablesNode;
@@ -539,7 +539,7 @@ protected RubyDynamicObject copyRubyClass(RubyClass self,
539539

540540
@Specialization
541541
protected RubyDynamicObject copy(ImmutableRubyString string,
542-
@Cached AllocateNode allocateStringNode) {
542+
@Cached StringNodes.AllocateNode allocateStringNode) {
543543
return allocateStringNode.execute(coreLibrary().stringClass);
544544
}
545545

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import com.oracle.truffle.api.dsl.Cached.Exclusive;
7777
import com.oracle.truffle.api.dsl.Cached.Shared;
7878
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
79+
import com.oracle.truffle.api.dsl.GenerateUncached;
7980
import com.oracle.truffle.api.dsl.NeverDefault;
8081
import com.oracle.truffle.api.nodes.Node;
8182
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
@@ -145,6 +146,7 @@
145146
import org.truffleruby.interop.ToJavaStringNode;
146147
import org.truffleruby.language.Nil;
147148
import org.truffleruby.language.NotProvided;
149+
import org.truffleruby.language.RubyBaseNode;
148150
import org.truffleruby.language.RubyBaseNodeWithExecute;
149151
import org.truffleruby.language.RubyGuards;
150152
import org.truffleruby.language.RubyNode;
@@ -155,6 +157,7 @@
155157
import org.truffleruby.language.control.RaiseException;
156158
import org.truffleruby.language.dispatch.DispatchNode;
157159
import org.truffleruby.language.library.RubyStringLibrary;
160+
import org.truffleruby.language.objects.AllocationTracing;
158161
import org.truffleruby.language.objects.WriteObjectFieldNode;
159162
import org.truffleruby.language.threadlocal.SpecialVariableStorage;
160163
import org.truffleruby.language.yield.CallBlockNode;
@@ -202,6 +205,24 @@ public RubyNode cloneUninitialized() {
202205

203206
}
204207

208+
@GenerateUncached
209+
public abstract static class AllocateNode extends RubyBaseNode {
210+
211+
public abstract RubyString execute(RubyClass rubyClass);
212+
213+
@Specialization
214+
protected RubyString allocate(RubyClass rubyClass) {
215+
final RubyString string = new RubyString(
216+
rubyClass,
217+
getLanguage().stringShape,
218+
false,
219+
EMPTY_BINARY,
220+
Encodings.BINARY);
221+
AllocationTracing.trace(string, this);
222+
return string;
223+
}
224+
}
225+
205226
@CoreMethod(names = "encoding")
206227
public abstract static class EncodingNode extends CoreMethodArrayArgumentsNode {
207228
@Specialization

0 commit comments

Comments
 (0)