Skip to content

Commit a569bc7

Browse files
committed
Move translator options to language
1 parent 107a66f commit a569bc7

35 files changed

+299
-174
lines changed

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.concurrent.locks.ReentrantLock;
2121
import java.util.logging.Level;
2222

23+
import com.oracle.truffle.api.CompilerAsserts;
2324
import com.oracle.truffle.api.CompilerDirectives;
2425
import com.oracle.truffle.api.nodes.EncapsulatingNodeReference;
2526
import com.oracle.truffle.api.nodes.Node;
@@ -71,6 +72,7 @@
7172
import org.truffleruby.language.loader.FeatureLoader;
7273
import org.truffleruby.language.methods.InternalMethod;
7374
import org.truffleruby.language.objects.shared.SharedObjects;
75+
import org.truffleruby.options.LanguageOptions;
7476
import org.truffleruby.options.Options;
7577
import org.truffleruby.parser.TranslatorDriver;
7678
import org.truffleruby.platform.NativeConfiguration;
@@ -167,7 +169,7 @@ public RubyContext(RubyLanguage language, TruffleLanguage.Env env) {
167169
this.env = env;
168170
this.hasOtherPublicLanguages = computeHasOtherPublicLanguages(env);
169171

170-
options = createOptions(env);
172+
options = createOptions(env, language.options);
171173

172174
referenceProcessor = new ReferenceProcessor(this);
173175
finalizationService = new FinalizationService(this, referenceProcessor);
@@ -273,7 +275,7 @@ protected boolean patch(Env newEnv) {
273275
this.hasOtherPublicLanguages = computeHasOtherPublicLanguages(newEnv);
274276

275277
final Options oldOptions = this.options;
276-
final Options newOptions = createOptions(newEnv);
278+
final Options newOptions = createOptions(newEnv, language.options);
277279
final String newHome = findRubyHome(newOptions);
278280
if (!compatibleOptions(oldOptions, newOptions, this.hadHome, newHome != null)) {
279281
return false;
@@ -366,10 +368,9 @@ private boolean compatibleOptions(Options oldOptions, Options newOptions, boolea
366368
return true;
367369
}
368370

369-
private Options createOptions(TruffleLanguage.Env env) {
371+
private Options createOptions(TruffleLanguage.Env env, LanguageOptions languageOptions) {
370372
Metrics.printTime("before-options");
371-
final Options options = new Options(env, env.getOptions());
372-
373+
final Options options = new Options(env, env.getOptions(), languageOptions);
373374
if (options.OPTIONS_LOG && RubyLanguage.LOGGER.isLoggable(Level.CONFIG)) {
374375
for (OptionDescriptor descriptor : OptionsCatalog.allDescriptors()) {
375376
assert descriptor.getName().startsWith(TruffleRuby.LANGUAGE_ID);
@@ -511,7 +512,9 @@ public Hashing getHashing() {
511512
return hashing;
512513
}
513514

514-
public RubyLanguage getLanguage() {
515+
public RubyLanguage getLanguageSlow() {
516+
CompilerAsserts.neverPartOfCompilation(
517+
"@CachedLanguage or a final field in the node should be used so the RubyLanguage instance is constant in PE code");
515518
return language;
516519
}
517520

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.truffleruby.core.exception.RubySystemCallError;
3030
import org.truffleruby.core.fiber.RubyFiber;
3131
import org.truffleruby.core.hash.RubyHash;
32+
import org.graalvm.options.OptionValues;
3233
import org.truffleruby.core.kernel.TraceManager;
3334
import org.truffleruby.core.klass.RubyClass;
3435
import org.truffleruby.core.method.RubyMethod;
@@ -71,6 +72,7 @@
7172
import org.truffleruby.language.RubyInlineParsingRequestNode;
7273
import org.truffleruby.language.RubyParsingRequestNode;
7374
import org.truffleruby.language.objects.RubyObjectType;
75+
import org.truffleruby.options.LanguageOptions;
7476
import org.truffleruby.platform.Platform;
7577
import org.truffleruby.shared.Metrics;
7678
import org.truffleruby.shared.TruffleRuby;
@@ -142,6 +144,7 @@ public class RubyLanguage extends TruffleLanguage<RubyContext> {
142144
public final CoreSymbols coreSymbols;
143145
public final RopeCache ropeCache;
144146
public final SymbolTable symbolTable;
147+
@CompilationFinal public LanguageOptions options;
145148

146149
@CompilationFinal private AllocationReporter allocationReporter;
147150

@@ -234,8 +237,13 @@ public RubyContext createContext(Env env) {
234237
// We need to initialize the Metrics class of the language classloader
235238
Metrics.initializeOption();
236239

237-
if (allocationReporter == null) {
238-
allocationReporter = env.lookup(AllocationReporter.class);
240+
synchronized (this) {
241+
if (allocationReporter == null) {
242+
allocationReporter = env.lookup(AllocationReporter.class);
243+
}
244+
if (this.options == null) {
245+
this.options = new LanguageOptions(env, env.getOptions());
246+
}
239247
}
240248

241249
LOGGER.fine("createContext()");
@@ -420,4 +428,9 @@ private static Shape createShape(Class<? extends RubyDynamicObject> layoutClass)
420428
.build();
421429
}
422430

431+
@Override
432+
protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues newOptions) {
433+
return LanguageOptions.areOptionsCompatible(firstOptions, newOptions);
434+
}
435+
423436
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public CoreMethodNodeManager(
6363
SingletonClassNode singletonClassNode,
6464
PrimitiveManager primitiveManager) {
6565
this.context = context;
66-
this.language = context.getLanguage();
66+
this.language = context.getLanguageSlow();
6767
this.singletonClassNode = singletonClassNode;
6868
this.primitiveManager = primitiveManager;
6969
}
@@ -167,7 +167,7 @@ public void addLazyCoreMethod(String nodeFactoryName, String moduleName, boolean
167167
final RubyModule module = getModule(moduleName, isClass);
168168
final Arity arity = createArity(required, optional, rest, keywordAsOptional);
169169

170-
Function<SharedMethodInfo, RubyNode> methodNodeFactory = sharedMethodInfo -> new LazyRubyNode(context, () -> {
170+
Function<SharedMethodInfo, RubyNode> methodNodeFactory = sharedMethodInfo -> new LazyRubyNode(language, () -> {
171171
final NodeFactory<? extends RubyNode> nodeFactory = loadNodeFactory(nodeFactoryName);
172172
final CoreMethod methodAnnotation = nodeFactory.getNodeClass().getAnnotation(CoreMethod.class);
173173
return createCoreMethodNode(nodeFactory, methodAnnotation, sharedMethodInfo);
@@ -266,7 +266,7 @@ public RubyNode createCoreMethodNode(NodeFactory<? extends RubyNode> nodeFactory
266266
final boolean needsSelf = needsSelf(method);
267267

268268
if (needsSelf) {
269-
RubyNode readSelfNode = Translator.profileArgument(context, new ReadSelfNode());
269+
RubyNode readSelfNode = Translator.profileArgument(language, new ReadSelfNode());
270270
argumentsNodes[i++] = transformArgument(method, readSelfNode, 0);
271271
}
272272

@@ -276,7 +276,7 @@ public RubyNode createCoreMethodNode(NodeFactory<? extends RubyNode> nodeFactory
276276

277277
for (int n = 0; n < nArgs; n++) {
278278
RubyNode readArgumentNode = Translator
279-
.profileArgument(context, new ReadPreArgumentNode(n, MissingArgumentBehavior.NOT_PROVIDED));
279+
.profileArgument(language, new ReadPreArgumentNode(n, MissingArgumentBehavior.NOT_PROVIDED));
280280
argumentsNodes[i++] = transformArgument(method, readArgumentNode, n + 1);
281281
}
282282

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public CoreLibrary(RubyContext context) {
322322
this.sourceSection = initCoreSourceSection(context);
323323
this.node = new CoreLibraryNode();
324324

325-
final RubyLanguage language = context.getLanguage();
325+
final RubyLanguage language = context.getLanguageSlow();
326326

327327
// Nothing in this constructor can use RubyContext.getCoreLibrary() as we are building it!
328328
// Therefore, only initialize the core classes and modules here.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.truffleruby.core.basicobject;
1111

12+
import com.oracle.truffle.api.dsl.CachedLanguage;
1213
import org.truffleruby.Layouts;
1314
import org.truffleruby.RubyContext;
1415
import org.truffleruby.RubyLanguage;
@@ -248,11 +249,11 @@ protected RubyBignum objectID(double value) {
248249

249250
@Specialization(guards = "!isNil(object)")
250251
protected long objectIDImmutable(ImmutableRubyObject object,
251-
@CachedContext(RubyLanguage.class) RubyContext context) {
252+
@CachedLanguage RubyLanguage language) {
252253
final long id = object.getObjectId();
253254

254255
if (id == 0) {
255-
final long newId = context.getLanguage().getNextObjectID();
256+
final long newId = language.getNextObjectID();
256257
object.setObjectId(newId);
257258
return newId;
258259
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class FormatRootNode extends RubyBaseRootNode implements InternalRootNode
3535
@CompilationFinal private int expectedLength = 0;
3636

3737
public FormatRootNode(RubyContext context, SourceSection sourceSection, FormatEncoding encoding, FormatNode child) {
38-
super(context.getLanguage(), FormatFrameDescriptor.FRAME_DESCRIPTOR, sourceSection);
38+
super(context.getLanguageSlow(), FormatFrameDescriptor.FRAME_DESCRIPTOR, sourceSection);
3939
this.context = context;
4040
this.encoding = encoding;
4141
this.child = child;

src/main/java/org/truffleruby/core/format/unpack/UnpackRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class UnpackRootNode extends RubyBaseRootNode implements InternalRootNode
3131
@CompilationFinal private int expectedLength;
3232

3333
public UnpackRootNode(RubyContext context, SourceSection sourceSection, FormatNode child) {
34-
super(context.getLanguage(), FormatFrameDescriptor.FRAME_DESCRIPTOR, sourceSection);
34+
super(context.getLanguageSlow(), FormatFrameDescriptor.FRAME_DESCRIPTOR, sourceSection);
3535
this.context = context;
3636
this.child = child;
3737
expectedLength = context.getOptions().ARRAY_UNINITIALIZED_SIZE;

src/main/java/org/truffleruby/core/hash/HashLiteralNode.java

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

12-
import org.truffleruby.RubyContext;
12+
1313
import org.truffleruby.RubyLanguage;
1414
import org.truffleruby.core.cast.BooleanCastNode;
1515
import org.truffleruby.language.RubyContextSourceNode;
@@ -25,19 +25,21 @@
2525
public abstract class HashLiteralNode extends RubyContextSourceNode {
2626

2727
@Children protected final RubyNode[] keyValues;
28+
protected final RubyLanguage language;
2829

29-
protected HashLiteralNode(RubyNode[] keyValues) {
30+
protected HashLiteralNode(RubyLanguage language, RubyNode[] keyValues) {
3031
assert keyValues.length % 2 == 0;
32+
this.language = language;
3133
this.keyValues = keyValues;
3234
}
3335

34-
public static HashLiteralNode create(RubyContext context, RubyNode[] keyValues) {
36+
public static HashLiteralNode create(RubyLanguage language, RubyNode[] keyValues) {
3537
if (keyValues.length == 0) {
36-
return new EmptyHashLiteralNode();
37-
} else if (keyValues.length <= context.getOptions().HASH_PACKED_ARRAY_MAX * 2) {
38-
return new SmallHashLiteralNode(keyValues);
38+
return new EmptyHashLiteralNode(language);
39+
} else if (keyValues.length <= language.options.HASH_PACKED_ARRAY_MAX * 2) {
40+
return new SmallHashLiteralNode(language, keyValues);
3941
} else {
40-
return new GenericHashLiteralNode(keyValues);
42+
return new GenericHashLiteralNode(language, keyValues);
4143
}
4244
}
4345

@@ -51,8 +53,8 @@ public void doExecuteVoid(VirtualFrame frame) {
5153

5254
public static class EmptyHashLiteralNode extends HashLiteralNode {
5355

54-
public EmptyHashLiteralNode() {
55-
super(RubyNode.EMPTY_ARRAY);
56+
public EmptyHashLiteralNode(RubyLanguage language) {
57+
super(language, RubyNode.EMPTY_ARRAY);
5658
}
5759

5860
@Override
@@ -70,14 +72,14 @@ public static class SmallHashLiteralNode extends HashLiteralNode {
7072
@Child private FreezeHashKeyIfNeededNode freezeHashKeyIfNeededNode = FreezeHashKeyIfNeededNodeGen.create();
7173
private final BranchProfile duplicateKeyProfile = BranchProfile.create();
7274

73-
public SmallHashLiteralNode(RubyNode[] keyValues) {
74-
super(keyValues);
75+
public SmallHashLiteralNode(RubyLanguage language, RubyNode[] keyValues) {
76+
super(language, keyValues);
7577
}
7678

7779
@ExplodeLoop
7880
@Override
7981
public Object execute(VirtualFrame frame) {
80-
final Object[] store = PackedArrayStrategy.createStore(getContext());
82+
final Object[] store = PackedArrayStrategy.createStore(language);
8183

8284
int size = 0;
8385

@@ -150,8 +152,8 @@ public static class GenericHashLiteralNode extends HashLiteralNode {
150152
@Child SetNode setNode;
151153
private final int bucketsCount;
152154

153-
public GenericHashLiteralNode(RubyNode[] keyValues) {
154-
super(keyValues);
155+
public GenericHashLiteralNode(RubyLanguage language, RubyNode[] keyValues) {
156+
super(language, keyValues);
155157
bucketsCount = BucketsStrategy.capacityGreaterThan(keyValues.length / 2) *
156158
BucketsStrategy.OVERALLOCATE_FACTOR;
157159
}

0 commit comments

Comments
 (0)