Skip to content

Commit

Permalink
[Truffle] Refactor state out of TranslatorDriver.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Apr 17, 2015
1 parent 9fd7727 commit 457a569
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
Expand Up @@ -94,8 +94,6 @@ public RubyContext(Ruby runtime) {
safepointManager = new SafepointManager(this);

this.runtime = runtime;
translator = new TranslatorDriver();

warnings = new Warnings(this);

// Object space manager needs to come early before we create any objects
Expand All @@ -104,8 +102,11 @@ public RubyContext(Ruby runtime) {
emptyShape = RubyBasicObject.LAYOUT.createShape(new RubyOperations(this));

coreLibrary = new CoreLibrary(this);
rootLexicalScope = new LexicalScope(null, coreLibrary.getObjectClass());
coreLibrary.initialize();

translator = new TranslatorDriver(this);

featureManager = new FeatureManager(this);
traceManager = new TraceManager();
atExitManager = new AtExitManager();
Expand All @@ -115,8 +116,6 @@ public RubyContext(Ruby runtime) {
threadManager = new ThreadManager(this);
fiberManager = new FiberManager(this);

rootLexicalScope = new LexicalScope(null, coreLibrary.getObjectClass());

rubiniusPrimitiveManager = RubiniusPrimitiveManager.create();

if (Options.TRUFFLE_INSTRUMENTATION_SERVER_PORT.load() != 0) {
Expand Down
Expand Up @@ -826,8 +826,8 @@ private RubyNode openModule(SourceSection sourceSection, RubyNode defineOrGetNod
try {
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, newLexicalScope, Arity.NO_ARGUMENTS, name, false, bodyNode, false);

final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParser(),
environment.getParser().allocateReturnID(), true, true, sharedMethodInfo, name, false);
final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(context, environment, environment.getParseEnvironment(),
environment.getParseEnvironment().allocateReturnID(), true, true, sharedMethodInfo, name, false);

final ModuleTranslator classTranslator = new ModuleTranslator(currentNode, context, this, newEnvironment, source);

Expand Down Expand Up @@ -1090,7 +1090,7 @@ protected RubyNode translateMethodDefinition(SourceSection sourceSection, RubyNo
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, environment.getLexicalScope(), MethodTranslator.getArity(argsNode), methodName, false, parseTree, false);

final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(
context, environment, environment.getParser(), environment.getParser().allocateReturnID(), true, true, sharedMethodInfo, methodName, false);
context, environment, environment.getParseEnvironment(), environment.getParseEnvironment().allocateReturnID(), true, true, sharedMethodInfo, methodName, false);

// ownScopeForAssignments is the same for the defined method as the current one.

Expand Down Expand Up @@ -1661,7 +1661,7 @@ public RubyNode visitIterNode(org.jruby.ast.IterNode node) {
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, environment.getLexicalScope(), MethodTranslator.getArity(argsNode), currentCallMethodName, true, node, false);

final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(
context, environment, environment.getParser(), environment.getReturnID(), hasOwnScope, false, sharedMethodInfo, environment.getNamedMethodName(), true);
context, environment, environment.getParseEnvironment(), environment.getReturnID(), hasOwnScope, false, sharedMethodInfo, environment.getNamedMethodName(), true);
final MethodTranslator methodCompiler = new MethodTranslator(currentNode, context, this, newEnvironment, true, source);
methodCompiler.translatingForStatement = translatingForStatement;

Expand Down Expand Up @@ -2753,7 +2753,7 @@ public RubyNode visitLambdaNode(org.jruby.ast.LambdaNode node) {
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, environment.getLexicalScope(), MethodTranslator.getArity(argsNode), "(lambda)", true, node, false);

final TranslatorEnvironment newEnvironment = new TranslatorEnvironment(
context, environment, environment.getParser(), environment.getReturnID(), false, false, sharedMethodInfo, sharedMethodInfo.getName(), true);
context, environment, environment.getParseEnvironment(), environment.getReturnID(), false, false, sharedMethodInfo, sharedMethodInfo.getName(), true);
final MethodTranslator methodCompiler = new MethodTranslator(currentNode, context, this, newEnvironment, false, source);

final RubyNode definitionNode = methodCompiler.compileFunctionNode(translate(node.getPosition()), sharedMethodInfo.getName(), argsNode, node.getBodyNode(), sharedMethodInfo);
Expand Down
Expand Up @@ -18,9 +18,11 @@
public class ParseEnvironment {

private LexicalScope lexicalScope;
private long nextReturnID;

public ParseEnvironment(RubyContext context) {
lexicalScope = context.getRootLexicalScope();
nextReturnID = 0;
}

public LexicalScope getLexicalScope() {
Expand All @@ -35,4 +37,14 @@ public void popLexicalScope() {
lexicalScope = lexicalScope.getParent();
}

public long allocateReturnID() {
if (nextReturnID == Long.MAX_VALUE) {
throw new RuntimeException("Return IDs exhausted");
}

final long allocated = nextReturnID;
nextReturnID++;
return allocated;
}

}
Expand Up @@ -47,14 +47,18 @@ public static enum ParserContext {
TOP_LEVEL, SHELL, MODULE, EVAL
}

private long nextReturnID = 0;
private final ParseEnvironment parseEnvironment;

public TranslatorDriver(RubyContext context) {
parseEnvironment = new ParseEnvironment(context);
}

public RubyNode parse(RubyContext context, org.jruby.ast.Node parseTree, org.jruby.ast.ArgsNode argsNode, org.jruby.ast.Node bodyNode, Node currentNode) {
final LexicalScope lexicalScope = context.getRootLexicalScope(); // TODO(eregon): figure out how to get the lexical scope from JRuby
final SharedMethodInfo sharedMethod = new SharedMethodInfo(null, lexicalScope, Arity.NO_ARGUMENTS, "(unknown)", false, parseTree, false);

final TranslatorEnvironment environment = new TranslatorEnvironment(
context, environmentForFrame(context, null), this, allocateReturnID(), true, true, sharedMethod, sharedMethod.getName(), false);
context, environmentForFrame(context, null), parseEnvironment, parseEnvironment.allocateReturnID(), true, true, sharedMethod, sharedMethod.getName(), false);

// Translate to Ruby Truffle nodes

Expand Down Expand Up @@ -127,7 +131,8 @@ public RubyRootNode parse(Node currentNode, RubyContext context, Source source,
// TODO (10 Feb. 2015): name should be "<top (required)> for the require-d/load-ed files.
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, context.getRootLexicalScope(), Arity.NO_ARGUMENTS, "<main>", false, rootNode, false);

final TranslatorEnvironment environment = new TranslatorEnvironment(context, environmentForFrame(context, parentFrame), this, allocateReturnID(), ownScopeForAssignments, false, sharedMethodInfo, sharedMethodInfo.getName(), false);
final TranslatorEnvironment environment = new TranslatorEnvironment(context, environmentForFrame(context, parentFrame),
parseEnvironment, parseEnvironment.allocateReturnID(), ownScopeForAssignments, false, sharedMethodInfo, sharedMethodInfo.getName(), false);

// Get the DATA constant

Expand Down Expand Up @@ -220,16 +225,6 @@ private Object getData(RubyContext context) {
return truffleFile;
}

public long allocateReturnID() {
if (nextReturnID == Long.MAX_VALUE) {
throw new RuntimeException("Return IDs exhausted");
}

final long allocated = nextReturnID;
nextReturnID++;
return allocated;
}

private TranslatorEnvironment environmentForFrame(RubyContext context, MaterializedFrame frame) {
if (frame == null) {
return null;
Expand All @@ -238,7 +233,8 @@ private TranslatorEnvironment environmentForFrame(RubyContext context, Materiali
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, context.getRootLexicalScope(), Arity.NO_ARGUMENTS, "(unknown)", false, null, false);
final MaterializedFrame parent = RubyArguments.getDeclarationFrame(frame.getArguments());
// TODO(CS): how do we know if the frame is a block or not?
return new TranslatorEnvironment(context, environmentForFrame(context, parent), frame.getFrameDescriptor(), this, allocateReturnID(), true, true, sharedMethodInfo, sharedMethodInfo.getName(), false);
return new TranslatorEnvironment(context, environmentForFrame(context, parent), frame.getFrameDescriptor(),
parseEnvironment, parseEnvironment.allocateReturnID(), true, true, sharedMethodInfo, sharedMethodInfo.getName(), false);
}
}

Expand Down
Expand Up @@ -37,7 +37,6 @@ public class TranslatorEnvironment {

private final List<FrameSlot> flipFlopStates = new ArrayList<>();

private TranslatorDriver parser;
private final long returnID;
private final boolean isBlock;

Expand All @@ -55,24 +54,29 @@ public class TranslatorEnvironment {

public boolean hasRestParameter = false;

public TranslatorEnvironment(RubyContext context, TranslatorEnvironment parent, FrameDescriptor frameDescriptor, TranslatorDriver parser, long returnID, boolean ownScopeForAssignments,
public TranslatorEnvironment(RubyContext context, TranslatorEnvironment parent, FrameDescriptor frameDescriptor, ParseEnvironment parseEnvironment, long returnID, boolean ownScopeForAssignments,
boolean neverAssignInParentScope, SharedMethodInfo sharedMethodInfo, String namedMethodName, boolean isBlock) {
this.context = context;
this.parent = parent;
this.frameDescriptor = frameDescriptor;
this.parser = parser;
this.parseEnvironment = parseEnvironment;
this.returnID = returnID;
this.ownScopeForAssignments = ownScopeForAssignments;
this.neverAssignInParentScope = neverAssignInParentScope;
this.sharedMethodInfo = sharedMethodInfo;
this.namedMethodName = namedMethodName;
this.isBlock = isBlock;
this.parseEnvironment = (parent != null ? parent.parseEnvironment : new ParseEnvironment(context));
}

public TranslatorEnvironment(RubyContext context, TranslatorEnvironment parent, TranslatorDriver parser, long returnID, boolean ownScopeForAssignments, boolean neverAssignInParentScope,
public TranslatorEnvironment(RubyContext context, TranslatorEnvironment parent, ParseEnvironment parseEnvironment, long returnID, boolean ownScopeForAssignments, boolean neverAssignInParentScope,
SharedMethodInfo methodIdentifier, String namedMethodName, boolean isBlock) {
this(context, parent, new FrameDescriptor(context.getCoreLibrary().getNilObject()), parser, returnID, ownScopeForAssignments, neverAssignInParentScope, methodIdentifier, namedMethodName, isBlock);
this(context, parent, new FrameDescriptor(context.getCoreLibrary().getNilObject()), parseEnvironment, returnID, ownScopeForAssignments, neverAssignInParentScope, methodIdentifier,
namedMethodName, isBlock);
}

public static TranslatorEnvironment newRootEnvironment(RubyContext context, FrameDescriptor frameDescriptor, ParseEnvironment parseEnvironment, long returnID, boolean ownScopeForAssignments,
boolean neverAssignInParentScope, SharedMethodInfo sharedMethodInfo, String namedMethodName, boolean isBlock) {
return new TranslatorEnvironment(context, null, frameDescriptor, parseEnvironment, returnID, ownScopeForAssignments, neverAssignInParentScope, sharedMethodInfo, namedMethodName, isBlock);
}

public LexicalScope getLexicalScope() {
Expand Down Expand Up @@ -189,8 +193,8 @@ public long getReturnID() {
return returnID;
}

public TranslatorDriver getParser() {
return parser;
public ParseEnvironment getParseEnvironment() {
return parseEnvironment;
}

public boolean hasOwnScopeForAssignments() {
Expand Down

0 comments on commit 457a569

Please sign in to comment.