public IRScope buildRoot(RootNode rootNode) { String file = rootNode.getPosition().getFile(); StaticScope staticScope = rootNode.getStaticScope(); // Top-level script! IRScriptBody script = new IRScriptBody(manager, "__file__", file, staticScope); script.addInstr(new ReceiveSelfInstr(script.getSelf())); // Set %current_scope = <current-scope> // Set %current_module = <current-module> script.addInstr(new CopyInstr(script.getCurrentScopeVariable(), new CurrentScope(script))); script.addInstr(new CopyInstr(script.getCurrentModuleVariable(), new ScopeModule(script))); // Build IR for the tree and return the result of the expression tree script.addInstr(new ReturnInstr(build(rootNode.getBodyNode(), script))); return script; }
MethodHandle _compiledHandle; if ((scope.getBeginBlocks() != null && scope.getBeginBlocks().size() > 0) || (scope.getEndBlocks() != null && scope.getBeginBlocks().size() > 0)) { throw new NotCompilableException("target script has BEGIN or END"); throw nce; } catch (Throwable t) { throw new NotCompilableException("failed to compile script " + scope.getId(), t); final StaticScope staticScope = scope.getStaticScope();
public void emit(IRScriptBody script) { String clsName = jvm.scriptToClass(script.getName()); jvm.pushscript(clsName, script.getFileName()); emitScope(script, "__script__", 0); jvm.cls().visitEnd(); jvm.popclass(); }
public static IRubyObject interpret(Ruby runtime, Node rootNode, IRubyObject self) { if (runtime.is1_9()) IRBuilder.setRubyVersion("1.9"); IRScriptBody root = (IRScriptBody) IRBuilder.createIRBuilder(runtime, runtime.getIRManager()).buildRoot((RootNode) rootNode); // We get the live object ball rolling here. This give a valid value for the top // of this lexical tree. All new scope can then retrieve and set based on lexical parent. if (root.getStaticScope().getModule() == null) { // If an eval this may already be setup. root.getStaticScope().setModule(runtime.getObject()); } RubyModule currModule = root.getStaticScope().getModule(); // Scope state for root? IRStaticScopeFactory.newIRLocalScope(null).setModule(currModule); ThreadContext context = runtime.getCurrentContext(); try { runBeginEndBlocks(root.getBeginBlocks(), context, self, null); // FIXME: No temp vars yet...not needed? InterpretedIRMethod method = new InterpretedIRMethod(root, currModule); IRubyObject rv = method.call(context, self, currModule, "(root)", IRubyObject.NULL_ARRAY); runBeginEndBlocks(root.getEndBlocks(), context, self, null); // FIXME: No temp vars yet...not needed? if ((IRRuntimeHelpers.isDebug() || IRRuntimeHelpers.inProfileMode()) && interpInstrsCount > 10000) { LOG.info("-- Interpreted instructions: {}", interpInstrsCount); /* for (Operation o: opStats.keySet()) { System.out.println(o + " = " + opStats.get(o).count); } */ } return rv; } catch (IRBreakJump bj) { throw IRException.BREAK_LocalJumpError.getException(context.runtime); } }
public static InterpreterContext buildRoot(IRManager manager, RootNode rootNode) { // FIXME: This filename should switch to ByteList String file = rootNode.getFile(); if (file == null) file = "(anon)"; IRScriptBody script = new IRScriptBody(manager, manager.runtime.newSymbol(file), rootNode.getStaticScope()); return topIRBuilder(manager, script).buildRootInner(rootNode); }
@JRubyMethod(module = true, name = "compile", required = 1, optional = 3) public static IRubyObject compile(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) { // def compile(content = nil, filename = DEFAULT_FILENAME, extra_position_info = false, &block) final Ruby runtime = context.runtime; final RubyString content = args[0].convertToString(); args[0] = content; final RubyString filename = args.length > 1 ? args[1].convertToString() : RubyString.newEmptyString(runtime); IRScriptBody scope = compileIR(context, args, block); JVMVisitor visitor = new JVMVisitor(runtime); JVMVisitorMethodContext methodContext = new JVMVisitorMethodContext(); byte[] bytes = visitor.compileToBytecode(scope, methodContext); scope.getStaticScope().setModule( runtime.getTopSelf().getMetaClass() ); RubyClass CompiledScript = (RubyClass) runtime.getModule("JRuby").getConstantAt("CompiledScript"); // JRuby::CompiledScript#initialize(filename, class_name, content, bytes) return CompiledScript.newInstance(context, new IRubyObject[] { filename, scope.getName(), content, Java.getInstance(runtime, bytes) }, Block.NULL_BLOCK); }
@Override public void setFileName(String fileName) { this.fileName = getManager().runtime.newSymbol(fileName); }
public R execute(Ruby runtime, ParseResult result, S specificObject) { IRScriptBody scope = null; 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 // FIXME: In terms of writing and reading we should emit enough to rebuild IC + minimal IRScope state InterpreterContext ic = IRBuilder.buildRoot(runtime.getIRManager(), (RootNode) result); scope = (IRScriptBody) ic.getScope(); scope.setScriptDynamicScope(((RootNode) result).getScope()); if (RubyInstanceConfig.IR_WRITING) { try { IRWriter.persist(new IRWriterStream(IRFileExpert.getIRPersistedFile(scope.getFileName())), scope); } catch (IOException ex) { ex.printStackTrace(); // FIXME: Handle errors better return null; } } } return execute(runtime, scope, specificObject); }
@Override public String toString() { return "Script: file: " + getFileName() + super.toString(); }
public static IRubyObject interpret(Ruby runtime, Node rootNode, IRubyObject self) { if (runtime.is1_9()) IRBuilder.setRubyVersion("1.9"); IRScriptBody root = (IRScriptBody) IRBuilder.createIRBuilder(runtime, runtime.getIRManager()).buildRoot((RootNode) rootNode); // We get the live object ball rolling here. This give a valid value for the top // of this lexical tree. All new scope can then retrieve and set based on lexical parent. if (root.getStaticScope().getModule() == null) { // If an eval this may already be setup. root.getStaticScope().setModule(runtime.getObject()); } RubyModule currModule = root.getStaticScope().getModule(); // Scope state for root? IRStaticScopeFactory.newIRLocalScope(null).setModule(currModule); ThreadContext context = runtime.getCurrentContext(); try { runBeginEndBlocks(root.getBeginBlocks(), context, self, null); // FIXME: No temp vars yet...not needed? InterpretedIRMethod method = new InterpretedIRMethod(root, currModule); IRubyObject rv = method.call(context, self, currModule, "(root)", IRubyObject.NULL_ARRAY); runBeginEndBlocks(root.getEndBlocks(), context, self, null); // FIXME: No temp vars yet...not needed? if ((IRRuntimeHelpers.isDebug() || IRRuntimeHelpers.inProfileMode()) && interpInstrsCount > 10000) { LOG.info("-- Interpreted instructions: {}", interpInstrsCount); /* for (Operation o: opStats.keySet()) { System.out.println(o + " = " + opStats.get(o).count); } */ } return rv; } catch (IRBreakJump bj) { throw IRException.BREAK_LocalJumpError.getException(context.runtime); } }
public static InterpreterContext buildRoot(IRManager manager, RootNode rootNode) { // FIXME: This filename should switch to ByteList String file = rootNode.getFile(); if (file == null) file = "(anon)"; IRScriptBody script = new IRScriptBody(manager, manager.runtime.newSymbol(file), rootNode.getStaticScope()); return topIRBuilder(manager, script).buildRootInner(rootNode); }
@JRubyMethod(module = true, name = "compile", required = 1, optional = 3) public static IRubyObject compile(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) { // def compile(content = nil, filename = DEFAULT_FILENAME, extra_position_info = false, &block) final Ruby runtime = context.runtime; final RubyString content = args[0].convertToString(); args[0] = content; final RubyString filename = args.length > 1 ? args[1].convertToString() : RubyString.newEmptyString(runtime); IRScriptBody scope = compileIR(context, args, block); JVMVisitor visitor = new JVMVisitor(runtime); JVMVisitorMethodContext methodContext = new JVMVisitorMethodContext(); byte[] bytes = visitor.compileToBytecode(scope, methodContext); scope.getStaticScope().setModule( runtime.getTopSelf().getMetaClass() ); RubyClass CompiledScript = (RubyClass) runtime.getModule("JRuby").getConstantAt("CompiledScript"); // JRuby::CompiledScript#initialize(filename, class_name, content, bytes) return CompiledScript.newInstance(context, new IRubyObject[] { filename, scope.getName(), content, Java.getInstance(runtime, bytes) }, Block.NULL_BLOCK); }
@Override public void setFileName(String fileName) { this.fileName = getManager().runtime.newSymbol(fileName); }
public R execute(Ruby runtime, ParseResult result, S specificObject) { IRScriptBody scope = null; 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 // FIXME: In terms of writing and reading we should emit enough to rebuild IC + minimal IRScope state InterpreterContext ic = IRBuilder.buildRoot(runtime.getIRManager(), (RootNode) result); scope = (IRScriptBody) ic.getScope(); scope.setScriptDynamicScope(((RootNode) result).getScope()); if (RubyInstanceConfig.IR_WRITING) { try { IRWriter.persist(new IRWriterStream(IRFileExpert.getIRPersistedFile(scope.getFileName())), scope); } catch (IOException ex) { ex.printStackTrace(); // FIXME: Handle errors better return null; } } } return execute(runtime, scope, specificObject); }
@Override public String toString() { return "Script: file: " + getFileName() + super.toString(); }
MethodHandle _compiledHandle; if ((scope.getBeginBlocks() != null && scope.getBeginBlocks().size() > 0) || (scope.getEndBlocks() != null && scope.getBeginBlocks().size() > 0)) { throw new NotCompilableException("target script has BEGIN or END"); throw nce; } catch (Throwable t) { throw new NotCompilableException("failed to compile script " + scope.getId(), t); final StaticScope staticScope = scope.getStaticScope();
public IRScope buildRoot(RootNode rootNode) { String file = rootNode.getPosition().getFile(); StaticScope staticScope = rootNode.getStaticScope(); // Top-level script! IRScriptBody script = new IRScriptBody(manager, "__file__", file, staticScope); script.addInstr(new ReceiveSelfInstr(script.getSelf())); // Set %current_scope = <current-scope> // Set %current_module = <current-module> script.addInstr(new CopyInstr(script.getCurrentScopeVariable(), new CurrentScope(script))); script.addInstr(new CopyInstr(script.getCurrentModuleVariable(), new ScopeModule(script))); // Build IR for the tree and return the result of the expression tree script.addInstr(new ReturnInstr(build(rootNode.getBodyNode(), script))); return script; }
public static IRScope createScope(IRManager manager, IRScopeType type, RubySymbol name, int line, IRScope lexicalParent, Signature signature, StaticScope staticScope) { switch (type) { case CLASS_BODY: return new IRClassBody(manager, lexicalParent, name, line, staticScope); case METACLASS_BODY: return new IRMetaClassBody(manager, lexicalParent, manager.getMetaClassName(), line, staticScope); case INSTANCE_METHOD: return new IRMethod(manager, lexicalParent, null, name, true, line, staticScope, false); case CLASS_METHOD: return new IRMethod(manager, lexicalParent, null, name, false, line, staticScope, false); case MODULE_BODY: return new IRModuleBody(manager, lexicalParent, name, line, staticScope); case SCRIPT_BODY: return new IRScriptBody(manager, name, staticScope); case FOR: return new IRFor(manager, lexicalParent, line, staticScope, signature); case CLOSURE: return new IRClosure(manager, lexicalParent, line, staticScope, signature); case EVAL_SCRIPT: // SSS FIXME: This is broken right now -- the isModuleEval arg has to be persisted and then read back. return new IREvalScript(manager, lexicalParent, lexicalParent.getFileName(), line, staticScope, EvalType.NONE); } throw new RuntimeException("No such scope type: " + type); } }
public void emit(IRScriptBody script) { String clsName = jvm.scriptToClass(script.getName()); jvm.pushscript(clsName, script.getFileName()); emitScope(script, "__script__", 0); jvm.cls().visitEnd(); jvm.popclass(); }
public IRScriptBody(IRManager manager, RubySymbol sourceName, StaticScope staticScope) { super(manager, null, sourceName, 0, staticScope); this.toplevelScope = null; this.fileName = sourceName; if (!getManager().isDryRun() && staticScope != null) { staticScope.setIRScope(this); } }