Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'origin/master' into test-jit
Conflicts:
	core/src/main/java/org/jruby/ir/Compiler.java
  • Loading branch information
headius committed Oct 22, 2014
2 parents 1889a7b + 001d617 commit 9041fe8
Show file tree
Hide file tree
Showing 19 changed files with 189 additions and 168 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/org/jruby/Ruby.java
Expand Up @@ -1206,8 +1206,7 @@ private void init() {

// Create an IR manager and a top-level IR scope and bind it to the top-level static-scope object
irManager = new IRManager();
IRScriptBody topLevelScope = new IRScriptBody(irManager, "", "", tc.getCurrentScope().getStaticScope());


// Initialize the "dummy" class used as a marker
dummyClass = new RubyClass(this, classClass);
dummyClass.freeze(tc);
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/org/jruby/ast/ConstDeclNode.java
Expand Up @@ -74,13 +74,16 @@ public String getName() {
}

/**
* Get the path the name is associated with or null (in Foo::BAR it is Foo).
* Get the full path, including the name of the new constant (in Foo::BAR it is Foo::BAR) or null.
* Your probably want to extract the left part with
* <code>((Colon2Node) node.getConstNode()).getLeftNode()</code>
* if <code>node.getConstNode()</code> is a <code>Colon2ConstNode</code>.
* @return pathNode
*/
public Node getConstNode() {
return (Node) constNode;
}

public List<Node> childNodes() {
return createList(getConstNode(), getValueNode());
}
Expand Down
Expand Up @@ -34,6 +34,10 @@ public class InterpretedIRMethod extends DynamicMethod implements IRMethodArgs,

protected final IRScope method;

// For synthetic methods and for module/class bodies we do not want these added to
// our backtraces.
private boolean isSynthetic;

private static class DynamicMethodBox {
public DynamicMethod actualMethod;
public int callCount = 0;
Expand All @@ -49,6 +53,11 @@ public InterpretedIRMethod(IRScope method, Visibility visibility, RubyModule imp
if (!implementationClass.getRuntime().getInstanceConfig().getCompileMode().shouldJIT()) {
this.box.callCount = -1;
}
isSynthetic = method instanceof IRModuleBody;
}

public boolean isSynthetic() {
return isSynthetic;
}

public IRScope getIRMethod() {
Expand Down Expand Up @@ -94,14 +103,16 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz

if (IRRuntimeHelpers.isDebug()) doDebug();

if (ic.hasExplicitCallProtocol()) return Interpreter.INTERPRET_METHOD(context, this, self, name, args, block);

pre(ic, context, self, name, block);

try {
if (ic.hasExplicitCallProtocol()) {
return Interpreter.INTERPRET_METHOD(context, this, self, name, args, block);
} finally {
post(ic, context);
} else {
try {
pre(ic, context, self, name, block);

return Interpreter.INTERPRET_METHOD(context, this, self, name, args, block);
} finally {
post(ic, context);
}
}
}

Expand Down Expand Up @@ -135,11 +146,10 @@ protected void pre(InterpreterContext ic, ThreadContext context, IRubyObject sel
}

public InterpreterContext ensureInstrsReady() {
InterpreterContext context = method.getInterpreterContext();
if (context == null) {
context = method.prepareForInterpretation();
}
return context;
// Try unsync access first before calling more expensive method for getting IC
InterpreterContext ic = method.getInterpreterContext();

return ic == null ? method.prepareForInterpretation() : ic;
}

public DynamicMethod getMethodForCaching() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ir/Compiler.java
Expand Up @@ -36,7 +36,7 @@ public static Compiler getInstance() {
}

@Override
protected ScriptAndCode execute(final Ruby runtime, final IRScope scope, JRubyClassLoader classLoader) {
protected ScriptAndCode execute(final Ruby runtime, final IRScriptBody scope, JRubyClassLoader classLoader) {
final JVMVisitor visitor = new JVMVisitor();
final byte[] bytecode = visitor.compileToBytecode(scope);
final Class compiled = visitor.defineFromBytecode(scope, bytecode, classLoader);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Expand Up @@ -3237,12 +3237,12 @@ public IREvalScript buildEvalRoot(StaticScope staticScope, IRScope containingSco
return script;
}

public IRScope buildRoot(RootNode rootNode) {
public IRScriptBody buildRoot(RootNode rootNode) {
String file = rootNode.getPosition().getFile();
StaticScope staticScope = rootNode.getStaticScope();

// Top-level script!
IRScriptBody script = new IRScriptBody(manager, "__file__", file, staticScope);
IRScriptBody script = new IRScriptBody(manager, file, staticScope);
addInstr(script, new ReceiveSelfInstr(script.getSelf()));
// Set %current_scope = <current-scope>
// Set %current_module = <current-module>
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/java/org/jruby/ir/IREvalScript.java
Expand Up @@ -3,6 +3,7 @@
import org.jruby.EvalType;
import org.jruby.RubyModule;
import org.jruby.ir.interpreter.Interpreter;
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.ir.operands.ClosureLocalVariable;
import org.jruby.ir.operands.Label;
import org.jruby.ir.operands.LocalVariable;
Expand Down Expand Up @@ -104,8 +105,10 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz
LOG.info("CFG:\n" + cfg().toStringInstrs());
}

InterpreterContext ic = prepareForInterpretation();

// FIXME: Do not push new empty arg array in every time
return Interpreter.INTERPRET_EVAL(context, self, this, clazz, new IRubyObject[] {}, backtraceName, block, null);
return Interpreter.INTERPRET_EVAL(context, self, ic, clazz, new IRubyObject[] {}, backtraceName, block, null);
}

@Override
Expand Down
73 changes: 9 additions & 64 deletions core/src/main/java/org/jruby/ir/IRScriptBody.java
@@ -1,38 +1,30 @@
package org.jruby.ir;

import org.jruby.RubyModule;
import org.jruby.ir.interpreter.Interpreter;
import org.jruby.ir.operands.IRException;
import org.jruby.ir.representations.CFG;
import org.jruby.ir.runtime.IRBreakJump;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.ir.instructions.Instr;
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.ir.interpreter.ScriptBodyInterpreterContext;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

// FIXME: I made this IRModule because any methods placed in top-level script goes
// into something which an IRScript is basically a module that is special in that
// it represents a lexical unit. Fix what now?
public class IRScriptBody extends IRScope {
private static final Logger LOG = LoggerFactory.getLogger("IRScriptBody");

private List<IRClosure> beginBlocks;
private List<IRClosure> endBlocks;

public IRScriptBody(IRManager manager, String className, String sourceName, StaticScope staticScope) {
public IRScriptBody(IRManager manager, String sourceName, StaticScope staticScope) {
super(manager, null, sourceName, sourceName, 0, staticScope);
if (!getManager().isDryRun() && staticScope != null) {
staticScope.setIRScope(this);
staticScope.setScopeType(this.getScopeType());
}
}

@Override
public InterpreterContext allocateInterpreterContext(Instr[] instructionList) {
return new ScriptBodyInterpreterContext(this, instructionList);
}

@Override
public int getNearestModuleReferencingScopeDepth() {
return 0;
Expand Down Expand Up @@ -78,51 +70,4 @@ public List<IRClosure> getEndBlocks() {
public boolean isScriptScope() {
return true;
}

public IRubyObject interpret(ThreadContext context, IRubyObject self) {
prepareForInterpretation();

String name = "(root)";
if (IRRuntimeHelpers.isDebug()) {
LOG.info("Executing '" + name + "'");
CFG cfg = getCFG();
LOG.info("Graph:\n" + cfg.toStringGraph());
LOG.info("CFG:\n" + cfg.toStringInstrs());
}

// We get the live object ball rolling here.
// This give a valid value for the top of this lexical tree.
// All new scopes can then retrieve and set based on lexical parent.
StaticScope scope = getStaticScope();
RubyModule currModule = scope.getModule();
if (currModule == null) {
// SSS FIXME: Looks like this has to do with Kernel#load
// and the wrap parameter. Figure it out and document it here.
currModule = context.getRuntime().getObject();
}

IRubyObject retVal;

scope.setModule(currModule);
if (!this.flags.contains(IRFlags.DYNSCOPE_ELIMINATED)) {
context.preMethodScopeOnly(scope);
}
context.setCurrentVisibility(Visibility.PRIVATE);

try {
Interpreter.runBeginEndBlocks(getBeginBlocks(), context, self, scope, null);
retVal = Interpreter.INTERPRET_ROOT(context, self, this, currModule, name);
Interpreter.runBeginEndBlocks(getEndBlocks(), context, self, scope, null);

Interpreter.dumpStats();
} catch (IRBreakJump bj) {
throw IRException.BREAK_LocalJumpError.getException(context.runtime);
} finally {
if (!this.flags.contains(IRFlags.DYNSCOPE_ELIMINATED)) {
context.popScope();
}
}

return retVal;
}
}
13 changes: 4 additions & 9 deletions core/src/main/java/org/jruby/ir/IRTranslator.java
Expand Up @@ -10,9 +10,6 @@

import java.io.IOException;

//import org.jruby.ir.persistence.persist.string.IRToStringTranslator;
//import org.jruby.ir.persistence.util.FileIO;

/**
* Abstract class that contains general logic for both IR Compiler and IR Interpreter
*
Expand All @@ -21,18 +18,16 @@
*/
public abstract class IRTranslator<R, S> {
public R execute(Ruby runtime, ParseResult result, S specificObject) {
IRScope scope = null;
IRScriptBody scope = null;

if (result instanceof IRScope) { // Already have it (likely from read from persistent store).
scope = (IRScope) result;
if (result instanceof IRScriptBody) { // Already have it (likely from read from persistent store).
scope = (IRScriptBody) result;
} else if (result instanceof RootNode) { // Need to perform create IR from AST
scope = IRBuilder.createIRBuilder(runtime, runtime.getIRManager()).buildRoot((RootNode) result);

if (RubyInstanceConfig.IR_WRITING) {
try {
IRWriter.persist(new IRWriterFile(IRFileExpert.getIRPersistedFile(scope.getFileName())), scope);
// FileIO.writeToFile(IRFileExpert.getIRPersistedFile(scope.getFileName()),
// IRToStringTranslator.translate(scope));
} catch (IOException ex) {
ex.printStackTrace(); // FIXME: Handle errors better
return null;
Expand All @@ -43,5 +38,5 @@ public R execute(Ruby runtime, ParseResult result, S specificObject) {
return execute(runtime, scope, specificObject);
}

protected abstract R execute(Ruby runtime, IRScope producedIrScope, S specificObject);
protected abstract R execute(Ruby runtime, IRScriptBody producedIrScope, S specificObject);
}

0 comments on commit 9041fe8

Please sign in to comment.