Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use NotCompilableException more consistently.
  • Loading branch information
headius committed Oct 22, 2014
1 parent 736fb94 commit b58b9f3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
15 changes: 13 additions & 2 deletions core/src/main/java/org/jruby/Ruby.java
Expand Up @@ -40,6 +40,7 @@
package org.jruby;

import org.jruby.compiler.Constantizable;
import org.jruby.compiler.NotCompilableException;
import org.objectweb.asm.util.TraceClassVisitor;
import jnr.constants.Constant;
import jnr.constants.ConstantSet;
Expand Down Expand Up @@ -726,7 +727,7 @@ public IRubyObject runNormally(Node scriptNode) {
// FIXME: restore error once JIT should handle everything
try {
scriptAndCode = tryCompile(scriptNode, new JRubyClassLoader(getJRubyClassLoader()));
if (Options.JIT_LOGGING.load()) {
if (scriptAndCode != null && Options.JIT_LOGGING.load()) {
LOG.info("done compiling target script: " + scriptNode.getPosition().getFile());
}
} catch (Exception e) {
Expand Down Expand Up @@ -782,7 +783,17 @@ private void handeCompileError(Node node, Throwable t) {
}

private ScriptAndCode tryCompile(Node node, JRubyClassLoader classLoader) {
return Compiler.getInstance().execute(this, node, classLoader);
try {
return Compiler.getInstance().execute(this, node, classLoader);
} catch (NotCompilableException e) {
if (Options.JIT_LOGGING.load()) {
LOG.error("failed to compile target script " + node.getPosition().getFile() + ": " + e.getLocalizedMessage());
if (Options.JIT_LOGGING_VERBOSE.load()) {
LOG.error(e);
}
}
return null;
}
}

public IRubyObject runScript(Script script) {
Expand Down
Expand Up @@ -8,4 +8,12 @@ public class NotCompilableException extends RuntimeException {
public NotCompilableException(String message) {
super(message);
}

public NotCompilableException(Throwable t) {
super(t);
}

public NotCompilableException(String message, Throwable t) {
super(message, t);
}
}
28 changes: 20 additions & 8 deletions core/src/main/java/org/jruby/ir/Compiler.java
Expand Up @@ -9,6 +9,7 @@
import org.jruby.ast.executable.AbstractScript;
import org.jruby.ast.executable.Script;
import org.jruby.ast.executable.ScriptAndCode;
import org.jruby.compiler.NotCompilableException;
import org.jruby.exceptions.JumpException;
import org.jruby.ir.targets.JVMVisitor;
import org.jruby.parser.StaticScope;
Expand Down Expand Up @@ -37,21 +38,32 @@ public static Compiler getInstance() {

@Override
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);
final StaticScope staticScope = scope.getStaticScope();
final IRubyObject runtimeTopSelf = runtime.getTopSelf();
staticScope.setModule(runtimeTopSelf.getMetaClass());
JVMVisitor visitor;
byte[] bytecode;
Class compiled;
StaticScope _staticScope;
IRubyObject _runtimeTopSelf;

Method _compiledMethod;
try {
visitor = new JVMVisitor();
bytecode = visitor.compileToBytecode(scope);
compiled = visitor.defineFromBytecode(scope, bytecode, classLoader);
_staticScope = scope.getStaticScope();
_runtimeTopSelf = runtime.getTopSelf();
_staticScope.setModule(_runtimeTopSelf.getMetaClass());

_compiledMethod = compiled.getMethod("__script__", ThreadContext.class,
StaticScope.class, IRubyObject.class, IRubyObject[].class, Block.class, RubyModule.class);
} catch (Exception e) {
throw new RuntimeException(e);
} catch (NotCompilableException nce) {
throw nce;
} catch (Throwable t) {
throw new NotCompilableException("failed to compile script " + scope.getName(), t);
}

final Method compiledMethod = _compiledMethod;
final StaticScope staticScope = _staticScope;
final IRubyObject runtimeTopSelf = _runtimeTopSelf;

Script script = new AbstractScript() {
@Override
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/ir/IRVisitor.java
@@ -1,5 +1,6 @@
package org.jruby.ir;

import org.jruby.compiler.NotCompilableException;
import org.jruby.ir.instructions.*;
import org.jruby.ir.instructions.boxing.*;
import org.jruby.ir.instructions.defined.GetErrorInfoInstr;
Expand All @@ -20,7 +21,7 @@ public void visit(Operand operand) {
}

private void error(Object object) {
throw new RuntimeException("no visitor logic for " + object.getClass().getName() + " in " + getClass().getName());
throw new NotCompilableException("no visitor logic for " + object.getClass().getName() + " in " + getClass().getName());
}

// standard instructions
Expand Down
26 changes: 13 additions & 13 deletions core/src/main/java/org/jruby/ir/targets/JVMVisitor.java
Expand Up @@ -81,7 +81,7 @@ public Class defineFromBytecode(IRScope scope, byte[] code, ClassDefiningClassLo
try {
result.getField(entry.getKey()).set(null, entry.getValue());
} catch (Exception e) {
throw new RuntimeException(e);
throw new NotCompilableException(e);
}
}

Expand All @@ -100,7 +100,7 @@ public void codegenScope(IRScope scope) {
} else if (scope instanceof IRModuleBody) {
emitModuleBodyJIT((IRModuleBody)scope);
} else {
throw new RuntimeException("don't know how to JIT: " + scope);
throw new NotCompilableException("don't know how to JIT: " + scope);
}
}

Expand Down Expand Up @@ -432,7 +432,7 @@ private void loadFloatArg(Operand arg) {
val = (double)((Fixnum)arg).value;
} else {
// Should not happen -- so, forcing an exception.
throw new RuntimeException("Non-float/fixnum in loadFloatArg!" + arg);
throw new NotCompilableException("Non-float/fixnum in loadFloatArg!" + arg);
}
jvmAdapter().ldc(val);
}
Expand All @@ -449,7 +449,7 @@ private void loadFixnumArg(Operand arg) {
val = ((Fixnum)arg).value;
} else {
// Should not happen -- so, forcing an exception.
throw new RuntimeException("Non-float/fixnum in loadFixnumArg!" + arg);
throw new NotCompilableException("Non-float/fixnum in loadFixnumArg!" + arg);
}
jvmAdapter().ldc(val);
}
Expand All @@ -464,7 +464,7 @@ private void loadBooleanArg(Operand arg) {
val = ((UnboxedBoolean)arg).isTrue();
} else {
// Should not happen -- so, forcing an exception.
throw new RuntimeException("Non-float/fixnum in loadFixnumArg!" + arg);
throw new NotCompilableException("Non-float/fixnum in loadFixnumArg!" + arg);
}
jvmAdapter().ldc(val);
}
Expand Down Expand Up @@ -592,7 +592,7 @@ public void AluInstr(AluInstr instr) {
case ISHL: a.lshl(); break;
case ISHR: a.lshr(); break;
case IEQ: m.invokeIRHelper("ilt", sig(boolean.class, long.class, long.class)); break; // annoying to have to do it in a method
default: throw new RuntimeException("UNHANDLED!");
default: throw new NotCompilableException("UNHANDLED!");
}

// Store it
Expand Down Expand Up @@ -965,12 +965,12 @@ public void EQQInstr(EQQInstr eqqinstr) {

@Override
public void ExceptionRegionEndMarkerInstr(ExceptionRegionEndMarkerInstr exceptionregionendmarkerinstr) {
throw new RuntimeException("Marker instructions shouldn't reach compiler: " + exceptionregionendmarkerinstr);
throw new NotCompilableException("Marker instructions shouldn't reach compiler: " + exceptionregionendmarkerinstr);
}

@Override
public void ExceptionRegionStartMarkerInstr(ExceptionRegionStartMarkerInstr exceptionregionstartmarkerinstr) {
throw new RuntimeException("Marker instructions shouldn't reach compiler: " + exceptionregionstartmarkerinstr);
throw new NotCompilableException("Marker instructions shouldn't reach compiler: " + exceptionregionstartmarkerinstr);
}

@Override
Expand Down Expand Up @@ -1166,13 +1166,13 @@ public void MatchInstr(MatchInstr matchinstr) {
@Override
public void MethodLookupInstr(MethodLookupInstr methodlookupinstr) {
// SSS FIXME: Unused at this time
throw new RuntimeException("Unsupported instruction: " + methodlookupinstr);
throw new NotCompilableException("Unsupported instruction: " + methodlookupinstr);
}

@Override
public void ModuleVersionGuardInstr(ModuleVersionGuardInstr moduleversionguardinstr) {
// SSS FIXME: Unused at this time
throw new RuntimeException("Unsupported instruction: " + moduleversionguardinstr);
throw new NotCompilableException("Unsupported instruction: " + moduleversionguardinstr);
}

@Override
Expand Down Expand Up @@ -1533,7 +1533,7 @@ public void RuntimeHelperCall(RuntimeHelperCall runtimehelpercall) {
jvmStoreLocal(runtimehelpercall.getResult());
break;
default:
throw new RuntimeException("Unknown IR runtime helper method: " + runtimehelpercall.getHelperMethod() + "; INSTR: " + this);
throw new NotCompilableException("Unknown IR runtime helper method: " + runtimehelpercall.getHelperMethod() + "; INSTR: " + this);
}
}

Expand Down Expand Up @@ -1912,7 +1912,7 @@ public void MethAddr(MethAddr methaddr) {
@Override
public void MethodHandle(MethodHandle methodhandle) {
// SSS FIXME: Unused at this time
throw new RuntimeException("Unsupported operand: " + methodhandle);
throw new NotCompilableException("Unsupported operand: " + methodhandle);
}

@Override
Expand Down Expand Up @@ -2040,7 +2040,7 @@ public void UndefinedValue(UndefinedValue undefinedvalue) {

@Override
public void UnexecutableNil(UnexecutableNil unexecutablenil) {
throw new RuntimeException(this.getClass().getSimpleName() + " should never be directly executed!");
throw new NotCompilableException(this.getClass().getSimpleName() + " should never be directly executed!");
}

@Override
Expand Down

0 comments on commit b58b9f3

Please sign in to comment.