Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' into update_stdlib
Conflicts:
	core/src/main/java/org/jruby/ir/targets/JVMVisitor.java
  • Loading branch information
headius committed Oct 30, 2014
2 parents 67f9e27 + 18bb4db commit 03a60a4
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 2,251 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/org/jruby/ir/IRBuilder.java
Expand Up @@ -2074,7 +2074,9 @@ public String buildType(Node typeNode) {
}

public Operand buildDot(final DotNode dotNode, IRScope s) {
return copyAndReturnValue(s, new Range(build(dotNode.getBeginNode(), s), build(dotNode.getEndNode(), s), dotNode.isExclusive()));
Variable res = s.createTemporaryVariable();
addInstr(s, new BuildRangeInstr(res, build(dotNode.getBeginNode(), s), build(dotNode.getEndNode(), s), dotNode.isExclusive()));
return res;
}

private Operand dynamicPiece(Node pieceNode, IRScope s) {
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/org/jruby/ir/IRScope.java
Expand Up @@ -700,7 +700,11 @@ public void computeScopeFlags() {
// NOTE: bindingHasEscaped is the crucial flag and it effectively is
// unconditionally true whenever it has a call that receives a closure.
// See CallBase.computeRequiresCallersBindingFlag
if (this instanceof IREvalScript) { // for eval scopes, bindings are considered escaped ...
if (this instanceof IREvalScript || this instanceof IRScriptBody) {
// For eval scopes, bindings are considered escaped.
// For top-level script scopes, bindings are considered escaped as well
// because TOPLEVEL_BINDING can be used in places besides the file
// that is being parsed?
flags.add(BINDING_HAS_ESCAPED);
} else {
flags.remove(BINDING_HAS_ESCAPED);
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/org/jruby/ir/IRScriptBody.java
Expand Up @@ -4,22 +4,33 @@
import org.jruby.ir.interpreter.BeginEndInterpreterContext;
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.DynamicScope;

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

public class IRScriptBody extends IRScope {
private List<IRClosure> beginBlocks;
private List<IRClosure> endBlocks;
private DynamicScope tlbScope;

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

public DynamicScope getTopLevelBindingScope() {
return tlbScope;
}

public void setTopLevelBindingScope(DynamicScope tlbScope) {
this.tlbScope = tlbScope;
}

@Override
public InterpreterContext allocateInterpreterContext(Instr[] instructionList) {
return new BeginEndInterpreterContext(this, instructionList);
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/ir/IRTranslator.java
Expand Up @@ -24,6 +24,7 @@ public R execute(Ruby runtime, ParseResult result, S specificObject) {
scope = (IRScriptBody) result;
} else if (result instanceof RootNode) { // Need to perform create IR from AST
scope = IRBuilder.createIRBuilder(runtime, runtime.getIRManager()).buildRoot((RootNode) result);
scope.setTopLevelBindingScope(((RootNode)result).getScope());

if (RubyInstanceConfig.IR_WRITING) {
try {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ir/IRVisitor.java
Expand Up @@ -40,6 +40,7 @@ private void error(Object object) {
public void BuildCompoundArrayInstr(BuildCompoundArrayInstr instr) { error(instr); }
public void BuildCompoundStringInstr(BuildCompoundStringInstr instr) { error(instr); }
public void BuildDynRegExpInstr(BuildDynRegExpInstr instr) { error(instr); }
public void BuildRangeInstr(BuildRangeInstr instr) { error(instr); }
public void CallInstr(CallInstr callinstr) { error(callinstr); }
public void CheckArgsArrayArityInstr(CheckArgsArrayArityInstr checkargsarrayarityinstr) { error(checkargsarrayarityinstr); }
public void CheckArityInstr(CheckArityInstr checkarityinstr) { error(checkarityinstr); }
Expand Down Expand Up @@ -156,7 +157,6 @@ private void error(Object object) {
public void Nil(Nil nil) { error(nil); }
public void NthRef(NthRef nthref) { error(nthref); }
public void ObjectClass(ObjectClass objectclass) { error(objectclass); }
public void Range(Range range) { error(range); }
public void Rational(Rational rational) { error(rational); }
public void Regexp(Regexp regexp) { error(regexp); }
public void ScopeModule(ScopeModule scopemodule) { error(scopemodule); }
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/ir/Operation.java
Expand Up @@ -143,6 +143,7 @@ public enum Operation {
BUILD_COMPOUND_ARRAY(OpFlags.f_can_raise_exception),
BUILD_COMPOUND_STRING(OpFlags.f_can_raise_exception),
BUILD_DREGEXP(OpFlags.f_can_raise_exception),
BUILD_RANGE(OpFlags.f_can_raise_exception),
BACKTICK_STRING(OpFlags.f_can_raise_exception),
CHECK_ARGS_ARRAY_ARITY(OpFlags.f_can_raise_exception),
CHECK_ARITY(OpFlags.f_is_book_keeping_op | OpFlags.f_can_raise_exception),
Expand Down
@@ -1,32 +1,51 @@
package org.jruby.ir.operands;
package org.jruby.ir.instructions;

import org.jruby.RubyRange;
import org.jruby.ir.IRVisitor;
import org.jruby.ir.Operation;
import org.jruby.ir.operands.Variable;
import org.jruby.ir.operands.Operand;
import org.jruby.ir.transformations.inlining.CloneInfo;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

import java.util.List;
import java.util.Map;

// Represents a range (1..5) or (a..b) in ruby code
//
// NOTE: This operand is only used in the initial stages of optimization
// Further down the line, this range operand could get converted to calls
// that actually build the Range object
public class Range extends Operand {
final private Operand begin;
final private Operand end;
// that actually build the BuildRangeInstr object
public class BuildRangeInstr extends Instr implements ResultInstr {
private Variable result;
private Operand begin;
private Operand end;
private boolean exclusive;

public Range(Operand begin, Operand end, boolean exclusive) {
super(OperandType.RANGE);
public BuildRangeInstr(Variable result, Operand begin, Operand end, boolean exclusive) {
super(Operation.BUILD_RANGE);

this.begin = begin;
this.end = end;
this.exclusive = exclusive;
this.result = result;
}

@Override
public Variable getResult() {
return result;
}

@Override
public void updateResult(Variable v) {
this.result = v;
}

@Override
public Operand[] getOperands() {
return new Operand[] { begin, end };
}

public Operand getBegin() {
Expand All @@ -43,44 +62,30 @@ public boolean isExclusive() {

@Override
public String toString() {
return begin + (exclusive ? ".." : "...") + end;
return result + " = " + begin + (exclusive ? ".." : "...") + end;
}


// ---------- These methods below are used during compile-time optimizations -------
@Override
public boolean hasKnownValue() {
return begin.hasKnownValue() && end.hasKnownValue();
}

@Override
public Operand getSimplifiedOperand(Map<Operand, Operand> valueMap, boolean force) {
Operand newBegin = begin.getSimplifiedOperand(valueMap, force);
Operand newEnd = end.getSimplifiedOperand(valueMap, force);
return (newBegin == begin && newEnd == end) ? this : new Range(newBegin, newEnd, exclusive);
}

/** Append the list of variables used in this operand to the input list */
@Override
public void addUsedVariables(List<Variable> l) {
begin.addUsedVariables(l);
end.addUsedVariables(l);
public void simplifyOperands(Map<Operand, Operand> valueMap, boolean force) {
begin = begin.getSimplifiedOperand(valueMap, force);
end= end.getSimplifiedOperand(valueMap, force);
}

@Override
public Operand cloneForInlining(CloneInfo ii) {
return hasKnownValue() ? this : new Range(begin.cloneForInlining(ii), end.cloneForInlining(ii), exclusive);
public Instr clone(CloneInfo ii) {
return new BuildRangeInstr(ii.getRenamedVariable(result), begin.cloneForInlining(ii), end.cloneForInlining(ii), exclusive);
}

@Override
public Object retrieve(ThreadContext context, IRubyObject self, StaticScope currScope, DynamicScope currDynScope, Object[] temp) {
public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
return RubyRange.newRange(context.runtime, context,
(IRubyObject) begin.retrieve(context, self, currScope, currDynScope, temp),
(IRubyObject) end.retrieve(context, self, currScope, currDynScope, temp), exclusive);
}

@Override
public void visit(IRVisitor visitor) {
visitor.Range(this);
visitor.BuildRangeInstr(this);
}
}
10 changes: 8 additions & 2 deletions core/src/main/java/org/jruby/ir/interpreter/Interpreter.java
Expand Up @@ -161,7 +161,13 @@ protected IRubyObject execute(Ruby runtime, IRScriptBody irScope, IRubyObject se
IRubyObject retVal;

scope.setModule(currModule);
if (!ic.isDynscopeEliminated()) context.preMethodScopeOnly(scope);
DynamicScope tlbScope = irScope.getTopLevelBindingScope();
if (tlbScope == null) {
context.preMethodScopeOnly(scope);
} else {
context.preScopedBody(tlbScope);
tlbScope.growIfNeeded();
}
context.setCurrentVisibility(Visibility.PRIVATE);

try {
Expand All @@ -173,7 +179,7 @@ protected IRubyObject execute(Ruby runtime, IRScriptBody irScope, IRubyObject se
throw IRException.BREAK_LocalJumpError.getException(context.runtime);
} finally {
Interpreter.runEndBlocks(ic.getEndBlocks(), context, self, scope, null);
if (!ic.isDynscopeEliminated()) context.popScope();
context.popScope();
}

return retVal;
Expand Down
Expand Up @@ -53,10 +53,10 @@ public Instr decodeInner(Operation operation) {
case CHECK_ARITY: return new CheckArityInstr(d.decodeInt(), d.decodeInt(), d.decodeInt(), d.decodeBoolean(), d.decodeInt());
case CLASS_VAR_MODULE: return new GetClassVarContainerModuleInstr(d.decodeVariable(), d.decodeOperand(), d.decodeVariable());
case CONST_MISSING: return decodeConstMissingInstr();
// SSS FIXME: Might need fixing
// case BUILD_COMPOUND_INSTR: return decodeBuildCompoundStringInstr();
// SSS FIXME: TODO
// case BUILD_COMPOUND_INSTR: return decodeBuildCompoundStringInstr();
// case BUILD_DREGEXP: return decodeBuildDynRegExpInstr();
// case BUILD_RANGE: return new Range(d.decodeOperand(), d.decodeOperand(), d.decodeBoolean());
case COPY: return decodeCopy();
case DEF_CLASS: return new DefineClassInstr((d.decodeVariable()), (IRClassBody) d.decodeScope(), d.decodeOperand(), d.decodeOperand());
case DEF_CLASS_METH: return new DefineClassMethodInstr(d.decodeOperand(), (IRMethod) d.decodeScope());
Expand Down
Expand Up @@ -47,10 +47,9 @@ public void encode(Instr instr) {
case CHECK_ARGS_ARRAY_ARITY: encodeCheckArgsArrayArityInstr((CheckArgsArrayArityInstr) instr); break;
case CHECK_ARITY: encodeCheckArityInstr((CheckArityInstr) instr); break;
case CLASS_VAR_MODULE: encodeGetClassVarContainerModuleInstr((GetClassVarContainerModuleInstr) instr); break;
// SSS FIXME: Needs fixing
// case BUILD_COMPOUND_STRING: encodeBuildCompoundStringInstr((BuildCompoundStringInstr) instr); break;
// SSS FIXME: TODO
// case BUILD_DREGEXP: return encodeBuildDynRegExpInstr();
// case BUILD_RANGE: return encodeBuildRangeInstr();
case CONST_MISSING: encodeConstMissingInstr((ConstMissingInstr) instr); break;
case COPY: encodeCopyInstr((CopyInstr) instr); break;
case DEF_CLASS: encodeDefineClassInstr((DefineClassInstr) instr); break;
Expand Down
Expand Up @@ -50,7 +50,6 @@ public Operand decode(OperandType type) {
case NIL: return manager.getNil();
case NTH_REF: return new NthRef(d.decodeInt());
case OBJECT_CLASS: return new ObjectClass();
case RANGE: return new Range(d.decodeOperand(), d.decodeOperand(), d.decodeBoolean());
case REGEXP: return decodeRegexp();
case SCOPE_MODULE: return new ScopeModule(d.decodeInt());
case SELF: return Self.SELF;
Expand Down
Expand Up @@ -93,12 +93,6 @@ public void encode(Operand operand) {

@Override public void ObjectClass(ObjectClass objectclass) {} // No data

@Override public void Range(Range range) {
encoder.encode(range.getBegin());
encoder.encode(range.getEnd());
encoder.encode(range.isExclusive());
}

@Override public void Regexp(Regexp regexp) {
encode(regexp.getRegexp());
encoder.encode(regexp.options.isEncodingNone());
Expand Down
21 changes: 11 additions & 10 deletions core/src/main/java/org/jruby/ir/targets/JVMVisitor.java
Expand Up @@ -767,6 +767,17 @@ public void run() {
jvmStoreLocal(instr.getResult());
}

@Override
public void BuildRangeInstr(BuildRangeInstr instr) {
jvmMethod().loadRuntime();
jvmMethod().loadContext();
visit(instr.getBegin());
visit(instr.getEnd());
jvmAdapter().ldc(instr.isExclusive());
jvmAdapter().invokestatic(p(RubyRange.class), "newRange", sig(RubyRange.class, Ruby.class, ThreadContext.class, IRubyObject.class, IRubyObject.class, boolean.class));
jvmStoreLocal(instr.getResult());
}

@Override
public void CallInstr(CallInstr callInstr) {
IRBytecodeAdapter m = jvmMethod();
Expand Down Expand Up @@ -2025,16 +2036,6 @@ public void ObjectClass(ObjectClass objectclass) {
jvmMethod().pushObjectClass();
}

@Override
public void Range(Range range) {
jvmMethod().loadRuntime();
jvmMethod().loadContext();
visit(range.getBegin());
visit(range.getEnd());
jvmAdapter().ldc(range.isExclusive());
jvmAdapter().invokestatic(p(RubyRange.class), "newRange", sig(RubyRange.class, Ruby.class, ThreadContext.class, IRubyObject.class, IRubyObject.class, boolean.class));
}

@Override
public void Rational(Rational rational) {
jvmMethod().loadRuntime();
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/parser/Parser.java
Expand Up @@ -122,6 +122,7 @@ public Node parse(String file, LexerSource lexerSource, DynamicScope blockScope,
// but I am not sure which conditions leads to this...so lame message.
throw runtime.newSyntaxError("Problem reading source: " + e);
} catch (SyntaxException e) {
e.printStackTrace();
switch (e.getPid()) {
case UNKNOWN_ENCODING:
case NOT_ASCII_COMPATIBLE:
Expand Down
41 changes: 1 addition & 40 deletions core/src/main/java/org/jruby/truffle/nodes/RubyNode.java
Expand Up @@ -10,19 +10,14 @@
package org.jruby.truffle.nodes;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.dsl.TypeSystemReference;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.UnexpectedResultException;
import org.jruby.truffle.nodes.dispatch.Dispatch;
import org.jruby.truffle.nodes.yield.YieldDispatchNode;
import org.jruby.truffle.runtime.LexicalScope;
import org.jruby.truffle.runtime.RubyCallStack;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.UndefinedPlaceholder;
import org.jruby.truffle.runtime.backtrace.Backtrace;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.core.*;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyHash;
Expand Down Expand Up @@ -219,38 +214,4 @@ public static void notDesignedForCompilation() {
CompilerAsserts.neverPartOfCompilation();
}

private static void panic(RubyContext context, RubyNode currentNode, String message) {
CompilerDirectives.transferToInterpreter();

System.err.println("PANIC");

if (message != null) {
System.err.println(message);
}

for (String line : Backtrace.PANIC_FORMATTER.format(context, null, RubyCallStack.getBacktrace(currentNode))) {
System.err.println(line);
}

new Exception().printStackTrace();

System.exit(1);
}

public void panic(String format, Object... args) {
panic(String.format(format, args));
}

public void panic(String message) {
panic(getContext(), this, message);
}

public void panic() {
panic(getContext(), this, null);
}

public static void panic(RubyContext context) {
panic(context, null, null);
}

}
Expand Up @@ -116,7 +116,7 @@ public PanicNode(PanicNode prev) {

@Specialization
public RubyNilClass doPanic() {
panic();
DebugOperations.panic(getContext(), this, null);
return getContext().getCoreLibrary().getNilObject();
}

Expand Down

0 comments on commit 03a60a4

Please sign in to comment.