Skip to content

Commit 6c37b06

Browse files
committed
TranslatorEnvironment#newFrameDescriptorBuilder separated into two functions
1 parent 09c5141 commit 6c37b06

File tree

5 files changed

+39
-31
lines changed

5 files changed

+39
-31
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ public final class RubyLanguage extends TruffleLanguage<RubyContext> {
189189
public static final TruffleLogger LOGGER = TruffleLogger.getLogger(TruffleRuby.LANGUAGE_ID);
190190

191191
/** This is a truly empty frame descriptor and should only by dummy root nodes which require no variables. Any other
192-
* root nodes should should use
193-
* {@link TranslatorEnvironment#newFrameDescriptorBuilder(BlockFrameDescriptorInfo, boolean)}. */
192+
* root nodes should should use either {@link TranslatorEnvironment#newFrameDescriptorBuilderForMethod()} or
193+
* {@link TranslatorEnvironment#newFrameDescriptorBuilderForBlock(BlockFrameDescriptorInfo)}. */
194194
public static final FrameDescriptor EMPTY_FRAME_DESCRIPTOR = new FrameDescriptor(nil);
195195

196196
private RubyThread getOrCreateForeignThread(RubyContext context, Thread thread) {
@@ -336,7 +336,7 @@ private RubyThread getOrCreateForeignThread(RubyContext context, Thread thread)
336336
* special variable storage. This frame descriptor should be used for those frames to provide a constant frame
337337
* descriptor in those cases. */
338338
public final FrameDescriptor emptyDeclarationDescriptor = TranslatorEnvironment
339-
.newFrameDescriptorBuilder(null, true).build();
339+
.newFrameDescriptorBuilderForMethod().build();
340340

341341
public MaterializedFrame createEmptyDeclarationFrame(Object[] packedArgs, SpecialVariableStorage variables) {
342342
// createVirtualFrame().materialize() compiles better if this is in PE code

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static RubyBinding createBinding(RubyContext context, RubyLanguage langua
8181
public static FrameDescriptor newFrameDescriptor(RubyBinding binding) {
8282
FrameDescriptor parentDescriptor = binding.getFrame().getFrameDescriptor();
8383
var ref = new BlockFrameDescriptorInfo(parentDescriptor);
84-
return TranslatorEnvironment.newFrameDescriptorBuilder(ref, false).build();
84+
return TranslatorEnvironment.newFrameDescriptorBuilderForBlock(ref).build();
8585
}
8686

8787
static final int NEW_VAR_INDEX = 1;
@@ -91,7 +91,7 @@ public static FrameDescriptor newFrameDescriptor(FrameDescriptor parentDescripto
9191
assert name != null && !name.isEmpty();
9292

9393
var ref = new BlockFrameDescriptorInfo(parentDescriptor);
94-
var builder = TranslatorEnvironment.newFrameDescriptorBuilder(ref, false);
94+
var builder = TranslatorEnvironment.newFrameDescriptorBuilderForBlock(ref);
9595
int index = builder.addSlot(FrameSlotKind.Illegal, name, null);
9696
if (index != NEW_VAR_INDEX) {
9797
throw CompilerDirectives.shouldNotReachHere("new binding variable not at index 1");

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ public static Object eval(RubyContext context, String code, Object... arguments)
6767
RubyNode.EMPTY_ARGUMENTS);
6868

6969

70-
var builder = TranslatorEnvironment.newFrameDescriptorBuilder(
71-
new BlockFrameDescriptorInfo(currentFrameDescriptor),
72-
false);
70+
var builder = TranslatorEnvironment
71+
.newFrameDescriptorBuilderForBlock(new BlockFrameDescriptorInfo(currentFrameDescriptor));
7372

7473
for (int i = 0; i < nArgs; i++) {
7574
final Object identifier = arguments[i * 2];

src/main/java/org/truffleruby/parser/TranslatorEnvironment.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ public TranslatorEnvironment(
8181
this.parent = parent;
8282

8383
if (descriptor == null) {
84-
BlockFrameDescriptorInfo parentDescriptor = blockDepth > 0
85-
? Objects.requireNonNull(parent.blockFrameDescriptorInfo)
86-
: null;
87-
this.frameDescriptorBuilder = newFrameDescriptorBuilder(parentDescriptor, blockDepth == 0);
84+
if (blockDepth > 0) {
85+
BlockFrameDescriptorInfo parentBlockDescriptor = Objects
86+
.requireNonNull(parent.blockFrameDescriptorInfo);
87+
this.frameDescriptorBuilder = newFrameDescriptorBuilderForBlock(parentBlockDescriptor);
88+
} else {
89+
this.frameDescriptorBuilder = newFrameDescriptorBuilderForMethod();
90+
}
8891
this.blockFrameDescriptorInfo = new BlockFrameDescriptorInfo();
8992
} else {
9093
this.frameDescriptor = descriptor;
@@ -143,36 +146,42 @@ public boolean isTopLevelObjectScope() {
143146
}
144147

145148
// region frame descriptor
146-
public static FrameDescriptor.Builder newFrameDescriptorBuilder(BlockFrameDescriptorInfo parentDescriptor,
147-
boolean canHaveSpecialVariables) {
148-
if ((parentDescriptor != null) == canHaveSpecialVariables) {
149+
public static FrameDescriptor.Builder newFrameDescriptorBuilderForBlock(BlockFrameDescriptorInfo parentBlockInfo) {
150+
if (parentBlockInfo == null) {
149151
throw CompilerDirectives.shouldNotReachHere(
150-
"A descriptor should either be a method and have special variables, or be a block and have no special variables");
152+
"Frame descriptor for block has to have parent");
151153
}
152154

153155
var builder = FrameDescriptor.newBuilder().defaultValue(Nil.INSTANCE);
156+
builder.info(parentBlockInfo);
154157

155-
if (parentDescriptor != null) {
156-
builder.info(parentDescriptor);
157-
} else if (canHaveSpecialVariables) {
158-
// We need to access this Assumption from the FrameDescriptor,
159-
// and there is no way to get a RootNode from a FrameDescriptor, so we store it in the descriptor info.
160-
// We do not store it as slot info for footprint, to avoid needing an info array per FrameDescriptor.
161-
final Assumption doesNotNeedSpecialVariableStorageAssumption = Assumption
162-
.create(SpecialVariableStorage.ASSUMPTION_NAME);
163-
builder.info(doesNotNeedSpecialVariableStorageAssumption);
158+
int selfIndex = builder.addSlot(FrameSlotKind.Illegal, SelfNode.SELF_IDENTIFIER, null);
159+
if (selfIndex != SelfNode.SELF_INDEX) {
160+
throw CompilerDirectives.shouldNotReachHere("(self) should be at index 0");
164161
}
165162

163+
return builder;
164+
}
165+
166+
public static FrameDescriptor.Builder newFrameDescriptorBuilderForMethod() {
167+
168+
var builder = FrameDescriptor.newBuilder().defaultValue(Nil.INSTANCE);
169+
// We need to access this Assumption from the FrameDescriptor,
170+
// and there is no way to get a RootNode from a FrameDescriptor, so we store it in the descriptor info.
171+
// We do not store it as slot info for footprint, to avoid needing an info array per FrameDescriptor.
172+
final Assumption doesNotNeedSpecialVariableStorageAssumption = Assumption
173+
.create(SpecialVariableStorage.ASSUMPTION_NAME);
174+
builder.info(doesNotNeedSpecialVariableStorageAssumption);
175+
176+
166177
int selfIndex = builder.addSlot(FrameSlotKind.Illegal, SelfNode.SELF_IDENTIFIER, null);
167178
if (selfIndex != SelfNode.SELF_INDEX) {
168179
throw CompilerDirectives.shouldNotReachHere("(self) should be at index 0");
169180
}
170181

171-
if (canHaveSpecialVariables) {
172-
int svarsSlot = builder.addSlot(FrameSlotKind.Illegal, SpecialVariableStorage.SLOT_NAME, null);
173-
if (svarsSlot != SpecialVariableStorage.SLOT_INDEX) {
174-
throw CompilerDirectives.shouldNotReachHere("svars should be at index 1");
175-
}
182+
int svarsSlot = builder.addSlot(FrameSlotKind.Illegal, SpecialVariableStorage.SLOT_NAME, null);
183+
if (svarsSlot != SpecialVariableStorage.SLOT_INDEX) {
184+
throw CompilerDirectives.shouldNotReachHere("svars should be at index 1");
176185
}
177186

178187
return builder;

src/main/java/org/truffleruby/parser/YARPTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public YARPTranslator(
166166

167167
public RubyRootNode translate(Nodes.Node node) {
168168
var body = node.accept(this);
169-
var frameDescriptor = TranslatorEnvironment.newFrameDescriptorBuilder(null, true).build();
169+
var frameDescriptor = TranslatorEnvironment.newFrameDescriptorBuilderForMethod().build();
170170
var sourceSection = CoreLibrary.JAVA_CORE_SOURCE_SECTION;
171171
var sharedMethodInfo = new SharedMethodInfo(sourceSection, null, Arity.NO_ARGUMENTS, "<main>", 0, "<main>",
172172
null, null);

0 commit comments

Comments
 (0)