Skip to content

Commit

Permalink
[Truffle] Ruby nodes don't need context as a parameter - they can get…
Browse files Browse the repository at this point in the history
… it from their root node, PolyglotEngine, or a thread local.
  • Loading branch information
chrisseaton committed Jan 2, 2017
1 parent a748e02 commit f933df4
Show file tree
Hide file tree
Showing 253 changed files with 1,379 additions and 1,429 deletions.
130 changes: 71 additions & 59 deletions truffle/src/main/java/org/jruby/truffle/RubyContext.java
Expand Up @@ -111,25 +111,30 @@ public class RubyContext extends ExecutionContext {

private String currentDirectory;

public static ThreadLocal<RubyContext> contextsBeingCreated = new ThreadLocal<>();

public RubyContext(TruffleLanguage.Env env) {
this.env = env;
contextsBeingCreated.set(this);

final OptionsBuilder optionsBuilder = new OptionsBuilder();
optionsBuilder.set(env.getConfig());
optionsBuilder.set(System.getProperties());
options = optionsBuilder.build();
try {
this.env = env;

this.jrubyHome = findJRubyHome();
this.currentDirectory = System.getProperty("user.dir");
this.verbose = options.VERBOSITY.equals(Verbosity.TRUE);
final OptionsBuilder optionsBuilder = new OptionsBuilder();
optionsBuilder.set(env.getConfig());
optionsBuilder.set(System.getProperties());
options = optionsBuilder.build();

if (options.CALL_GRAPH) {
callGraph = new CallGraph();
} else {
callGraph = null;
}
this.jrubyHome = findJRubyHome();
this.currentDirectory = System.getProperty("user.dir");
this.verbose = options.VERBOSITY.equals(Verbosity.TRUE);

// Stuff that needs to be loaded before we load any code
if (options.CALL_GRAPH) {
callGraph = new CallGraph();
} else {
callGraph = null;
}

// Stuff that needs to be loaded before we load any code

/*
* The Graal option TimeThreshold sets how long a method has to become hot after it has started running, in ms.
Expand All @@ -138,9 +143,9 @@ public RubyContext(TruffleLanguage.Env env) {
* produces poor benchmark results. Here we just set it to a very high value, to effectively disable it.
*/

if (compilerOptions.supportsOption("MinTimeThreshold")) {
compilerOptions.setOption("MinTimeThreshold", 100000000);
}
if (compilerOptions.supportsOption("MinTimeThreshold")) {
compilerOptions.setOption("MinTimeThreshold", 100000000);
}

/*
* The Graal option InliningMaxCallerSize sets the maximum size of a method for where we consider to inline
Expand All @@ -149,66 +154,69 @@ public RubyContext(TruffleLanguage.Env env) {
* key methods from the core library from inlining other methods.
*/

if (compilerOptions.supportsOption("MinInliningMaxCallerSize")) {
compilerOptions.setOption("MinInliningMaxCallerSize", 5000);
}
if (compilerOptions.supportsOption("MinInliningMaxCallerSize")) {
compilerOptions.setOption("MinInliningMaxCallerSize", 5000);
}

// Load the core library classes
// Load the core library classes

coreLibrary = new CoreLibrary(this);
coreLibrary.initialize();
coreLibrary = new CoreLibrary(this);
coreLibrary.initialize();

symbolTable = new SymbolTable(coreLibrary.getSymbolFactory());
symbolTable = new SymbolTable(coreLibrary.getSymbolFactory());

// Create objects that need core classes
// Create objects that need core classes

nativePlatform = NativePlatformFactory.createPlatform(this);
rootLexicalScope = new LexicalScope(null, coreLibrary.getObjectClass());
nativePlatform = NativePlatformFactory.createPlatform(this);
rootLexicalScope = new LexicalScope(null, coreLibrary.getObjectClass());

// The encoding manager relies on POSIX having been initialized, so we can't process it during
// normal core library initialization.
coreLibrary.initializeEncodingManager();
// The encoding manager relies on POSIX having been initialized, so we can't process it during
// normal core library initialization.
coreLibrary.initializeEncodingManager();

threadManager = new ThreadManager(this);
threadManager.initialize();
threadManager = new ThreadManager(this);
threadManager.initialize();

// Load the nodes
// Load the nodes

Main.printTruffleTimeMetric("before-load-nodes");
coreLibrary.addCoreMethods(primitiveManager);
Main.printTruffleTimeMetric("after-load-nodes");
Main.printTruffleTimeMetric("before-load-nodes");
coreLibrary.addCoreMethods(primitiveManager);
Main.printTruffleTimeMetric("after-load-nodes");

// Capture known builtin methods
// Capture known builtin methods

final Instrumenter instrumenter = env.lookup(Instrumenter.class);
traceManager = new TraceManager(this, instrumenter);
coreMethods = new CoreMethods(this);
coverageManager = new CoverageManager(this, instrumenter);
final Instrumenter instrumenter = env.lookup(Instrumenter.class);
traceManager = new TraceManager(this, instrumenter);
coreMethods = new CoreMethods(this);
coverageManager = new CoverageManager(this, instrumenter);

// Load the reset of the core library
// Load the reset of the core library

coreLibrary.loadRubyCore();
coreLibrary.loadRubyCore();

// Load other subsystems
// Load other subsystems

final PrintStream configStandardOut = System.out;
debugStandardOut = (configStandardOut == System.out) ? null : configStandardOut;
final PrintStream configStandardOut = System.out;
debugStandardOut = (configStandardOut == System.out) ? null : configStandardOut;

// The instrumentation server can't be run with AOT because com.sun.net.httpserver.spi.HttpServerProvider uses runtime class loading.
if (!TruffleOptions.AOT && options.INSTRUMENTATION_SERVER_PORT != 0) {
instrumentationServerManager = new InstrumentationServerManager(this, options.INSTRUMENTATION_SERVER_PORT);
instrumentationServerManager.start();
} else {
instrumentationServerManager = null;
}
// The instrumentation server can't be run with AOT because com.sun.net.httpserver.spi.HttpServerProvider uses runtime class loading.
if (!TruffleOptions.AOT && options.INSTRUMENTATION_SERVER_PORT != 0) {
instrumentationServerManager = new InstrumentationServerManager(this, options.INSTRUMENTATION_SERVER_PORT);
instrumentationServerManager.start();
} else {
instrumentationServerManager = null;
}

coreLibrary.initializePostBoot();
coreLibrary.initializePostBoot();

consoleHolder = new ConsoleHolder();
consoleHolder = new ConsoleHolder();

// Share once everything is loaded
if (options.SHARED_OBJECTS_ENABLED && options.SHARED_OBJECTS_FORCE) {
sharedObjects.startSharing();
// Share once everything is loaded
if (options.SHARED_OBJECTS_ENABLED && options.SHARED_OBJECTS_FORCE) {
sharedObjects.startSharing();
}
} finally {
contextsBeingCreated.remove();
}
}

Expand Down Expand Up @@ -387,7 +395,11 @@ public CoverageManager getCoverageManager() {
}

public static RubyContext getInstance() {
return RubyLanguage.INSTANCE.unprotectedFindContext(RubyLanguage.INSTANCE.unprotectedCreateFindContextNode());
try {
return RubyLanguage.INSTANCE.unprotectedFindContext(RubyLanguage.INSTANCE.unprotectedCreateFindContextNode());
} catch (IllegalStateException e) {
return contextsBeingCreated.get();
}
}

public SourceLoader getSourceLoader() {
Expand Down
Expand Up @@ -23,8 +23,8 @@ public class CallPrimitiveNode extends RubyNode {

private final ConditionProfile successProfile = ConditionProfile.createBinaryProfile();

public CallPrimitiveNode(RubyContext context, SourceIndexLength sourceSection, RubyNode primitive, RubyNode fallback) {
super(context, sourceSection);
public CallPrimitiveNode(SourceIndexLength sourceSection, RubyNode primitive, RubyNode fallback) {
super(sourceSection);
this.primitive = primitive;
this.fallback = fallback;
}
Expand Down
Expand Up @@ -21,8 +21,8 @@ public abstract class CoreMethodArrayArgumentsNode extends CoreMethodNode {
public CoreMethodArrayArgumentsNode() {
}

public CoreMethodArrayArgumentsNode(RubyContext context, SourceIndexLength sourceSection) {
super(context, sourceSection);
public CoreMethodArrayArgumentsNode(SourceIndexLength sourceSection) {
super(sourceSection);
}

}
Expand Up @@ -21,8 +21,8 @@ public abstract class CoreMethodNode extends RubyNode {
public CoreMethodNode() {
}

public CoreMethodNode(RubyContext context, SourceIndexLength sourceSection) {
super(context, sourceSection);
public CoreMethodNode(SourceIndexLength sourceSection) {
super(sourceSection);
}

}
Expand Up @@ -196,17 +196,17 @@ private static CallTarget makeGenericMethod(RubyContext context, MethodDetails m
AmbiguousOptionalArgumentChecker.verifyNoAmbiguousOptionalArguments(methodDetails);
}

final RubyNode checkArity = Translator.createCheckArityNode(context, sourceSection.getSource(), sourceIndexLength, sharedMethodInfo.getArity());
final RubyNode checkArity = Translator.createCheckArityNode(sourceIndexLength, sharedMethodInfo.getArity());

RubyNode node;
if (!isSafe(context, method.unsafe())) {
node = new UnsafeNode(context, sourceIndexLength);
node = new UnsafeNode(sourceIndexLength);
} else {
node = Translator.sequence(context, sourceSection.getSource(), sourceIndexLength, Arrays.asList(checkArity, methodNode));
node = Translator.sequence(sourceIndexLength, Arrays.asList(checkArity, methodNode));
node = transformResult(method, node);
}

RubyNode bodyNode = new ExceptionTranslatingNode(context, sourceIndexLength, node, method.unsupportedOperationBehavior());
RubyNode bodyNode = new ExceptionTranslatingNode(sourceIndexLength, node, method.unsupportedOperationBehavior());

if (context.getOptions().CHAOS) {
bodyNode = ChaosNodeGen.create(bodyNode);
Expand Down Expand Up @@ -268,30 +268,26 @@ public static <T> T createNodeFromFactory(RubyContext context, Source source, So
final RubyNode[] argumentsArray = argumentsNodes.toArray(new RubyNode[argumentsNodes.size()]);
if (signature.size() == 1 && signature.get(0) == RubyNode[].class) {
methodNode = nodeFactory.createNode(new Object[] { argumentsArray });
} else if (signature.size() >= 3 && signature.get(2) == RubyNode[].class) {
if (signature.get(1) == SourceSection.class) {
methodNode = nodeFactory.createNode(context, sourceSection.toSourceSection(source), argumentsArray);
} else if (signature.get(1) == SourceIndexLength.class) {
methodNode = nodeFactory.createNode(context, sourceSection, argumentsArray);
} else if (signature.size() >= 2 && signature.get(1) == RubyNode[].class) {
if (signature.get(0) == SourceIndexLength.class) {
methodNode = nodeFactory.createNode(sourceSection, argumentsArray);
} else {
throw new UnsupportedOperationException();
}
} else if (signature.get(0) != RubyContext.class) {
} else if (signature.get(0) != SourceIndexLength.class) {
Object[] args = argumentsArray;
methodNode = nodeFactory.createNode(args);
} else {
Object[] args = new Object[2 + argumentsNodes.size()];
Object[] args = new Object[1 + argumentsNodes.size()];
args[0] = context;

if (signature.get(1) == SourceSection.class) {
args[1] = sourceSection.toSourceSection(source);
} else if (signature.get(1) == SourceIndexLength.class) {
args[1] = sourceSection;
if (signature.get(0) == SourceIndexLength.class) {
args[0] = sourceSection;
} else {
throw new UnsupportedOperationException();
}

System.arraycopy(argumentsArray, 0, args, 2, argumentsNodes.size());
System.arraycopy(argumentsArray, 0, args, 1, argumentsNodes.size());
methodNode = nodeFactory.createNode(args);
}
}
Expand All @@ -307,11 +303,11 @@ public static boolean needsSelf(CoreMethod method) {

private static RubyNode transformArgument(RubyContext context, Source source, SourceIndexLength sourceSection, CoreMethod method, RubyNode argument, int n) {
if (ArrayUtils.contains(method.lowerFixnum(), n)) {
argument = FixnumLowerNodeGen.create(null, null, argument);
argument = FixnumLowerNodeGen.create(null, argument);
}

if (n == 0 && method.raiseIfFrozenSelf()) {
argument = new RaiseIfFrozenNode(context, sourceSection, argument);
argument = new RaiseIfFrozenNode(sourceSection, argument);
}

return argument;
Expand Down
Expand Up @@ -22,8 +22,8 @@ public class InvokePrimitiveNode extends RubyNode {

private final ConditionProfile primitiveSucceededCondition = ConditionProfile.createBinaryProfile();

public InvokePrimitiveNode(RubyContext context, SourceIndexLength sourceSection, RubyNode primitive) {
super(context, sourceSection);
public InvokePrimitiveNode(SourceIndexLength sourceSection, RubyNode primitive) {
super(sourceSection);
this.primitive = primitive;
}

Expand Down
Expand Up @@ -23,8 +23,8 @@ public abstract class PrimitiveArrayArgumentsNode extends PrimitiveNode {
public PrimitiveArrayArgumentsNode() {
}

public PrimitiveArrayArgumentsNode(RubyContext context, SourceIndexLength sourceSection) {
super(context, sourceSection);
public PrimitiveArrayArgumentsNode(SourceIndexLength sourceSection) {
super(sourceSection);
}

}
Expand Up @@ -24,8 +24,8 @@ public abstract class PrimitiveNode extends RubyNode {
public PrimitiveNode() {
}

public PrimitiveNode(RubyContext context, SourceIndexLength sourceSection) {
super(context, sourceSection);
public PrimitiveNode(SourceIndexLength sourceSection) {
super(sourceSection);
}

}
Expand Up @@ -53,19 +53,19 @@ public RubyNode createCallPrimitiveNode(RubyContext context, Source source, Sour
}

if (!CoreMethodNodeManager.isSafe(context, annotation.unsafe())) {
return new UnsafeNode(context, sourceSection);
return new UnsafeNode(sourceSection);
}

final RubyNode primitiveNode = CoreMethodNodeManager.createNodeFromFactory(context, source, sourceSection, factory, arguments);

return new CallPrimitiveNode(context, sourceSection, primitiveNode, fallback);
return new CallPrimitiveNode(sourceSection, primitiveNode, fallback);
}

public RubyNode createInvokePrimitiveNode(RubyContext context, Source source, SourceIndexLength sourceSection, RubyNode[] arguments) {
assert arguments.length == getPrimitiveArity();

if (!CoreMethodNodeManager.isSafe(context, annotation.unsafe())) {
return new UnsafeNode(context, sourceSection);
return new UnsafeNode(sourceSection);
}

for (int n = 0; n < arguments.length; n++) {
Expand All @@ -80,20 +80,20 @@ public RubyNode createInvokePrimitiveNode(RubyContext context, Source source, So

final RubyNode primitiveNode;

if (signature.get(0) == RubyContext.class && signature.get(1) == SourceSection.class) {
primitiveNode = factory.createNode(context, sourceSection.toSourceSection(source), arguments);
} else if (signature.get(0) == RubyContext.class && signature.get(1) == SourceIndexLength.class) {
primitiveNode = factory.createNode(context, sourceSection, arguments);
if (signature.get(0) == SourceSection.class) {
primitiveNode = factory.createNode(sourceSection.toSourceSection(source), arguments);
} else if (signature.get(0) == SourceIndexLength.class) {
primitiveNode = factory.createNode(sourceSection, arguments);
} else {
primitiveNode = factory.createNode(new Object[] { arguments });
}

return new InvokePrimitiveNode(context, sourceSection, primitiveNode);
return new InvokePrimitiveNode(sourceSection, primitiveNode);
}

private RubyNode transformArgument(RubyNode argument, int n) {
if (ArrayUtils.contains(annotation.lowerFixnum(), n)) {
return FixnumLowerNodeGen.create(null, null, argument);
return FixnumLowerNodeGen.create(null, argument);
} else {
return argument;
}
Expand Down
Expand Up @@ -21,8 +21,8 @@ public abstract class UnaryCoreMethodNode extends CoreMethodNode {
public UnaryCoreMethodNode() {
}

public UnaryCoreMethodNode(RubyContext context, SourceIndexLength sourceSection) {
super(context, sourceSection);
public UnaryCoreMethodNode(SourceIndexLength sourceSection) {
super(sourceSection);
}

public abstract RubyNode getOperand();
Expand Down
Expand Up @@ -18,8 +18,8 @@

public class UnsafeNode extends RubyNode {

public UnsafeNode(RubyContext context, SourceIndexLength sourceSection) {
super(context, sourceSection);
public UnsafeNode(SourceIndexLength sourceSection) {
super(sourceSection);
}

@Override
Expand Down

0 comments on commit f933df4

Please sign in to comment.