public IRModuleBody(IRManager manager, IRScope lexicalParent, String name, String fileName, int lineNumber, StaticScope scope) { super(manager, lexicalParent, name, fileName, lineNumber, scope); if (!getManager().isDryRun()) { if (scope != null) ((IRStaticScope)scope).setIRScope(this); updateVersion(); } }
@Override public String toString() { return super.toString() + "(" + metaClassBody.getName() + ", " + object + ", " + metaClassBody.getFileName() + ")"; }
@Override public String[] toStringNonOperandArgs() { return new String[] {"name: " + metaClassBody.getId() }; }
public Operand buildModule(ModuleNode moduleNode, IRScope s) { Colon3Node cpath = moduleNode.getCPath(); String moduleName = cpath.getName(); Operand container = getContainerFromCPath(cpath, s); // Build the new module IRModuleBody m = new IRModuleBody(manager, s, moduleName, moduleNode.getPosition().getLine(), moduleNode.getScope()); Variable moduleBody = s.getNewTemporaryVariable(); s.addInstr(new DefineModuleInstr(moduleBody, m, container)); Variable ret = s.getNewTemporaryVariable(); s.addInstr(new ProcessModuleBodyInstr(ret, moduleBody)); m.addInstr(new ReceiveSelfInstr(m.getSelf())); // Set %current_scope = <c> // Set %current_module = module<c> m.addInstr(new CopyInstr(m.getCurrentScopeVariable(), new CurrentScope(m))); m.addInstr(new CopyInstr(m.getCurrentModuleVariable(), new ScopeModule(m))); // Create a new nested builder to ensure this gets its own IR builder state Operand rv = newIRBuilder(manager).build(moduleNode.getBodyNode(), m); if (rv != null) m.addInstr(new ReturnInstr(rv)); return ret; }
@Override public void DefineModuleInstr(DefineModuleInstr definemoduleinstr) { IRModuleBody newIRModuleBody = definemoduleinstr.getNewIRModuleBody(); StaticScope scope = newIRModuleBody.getStaticScope(); if (scope.getRequiredArgs() > 3 || scope.getRestArg() >= 0 || scope.getOptionalArgs() != 0) { throw new RuntimeException("can't compile variable method: " + this); a.ldc(newIRModuleBody.getName()); a.ldc(newIRModuleBody.getFileName()); a.ldc(newIRModuleBody.getLineNumber()); visit(definemoduleinstr.getContainer()); m.invokeHelper("checkIsRubyModule", RubyModule.class, ThreadContext.class, Object.class); a.ldc(newIRModuleBody.getName()); a.invokevirtual(p(RubyModule.class), "defineOrGetModuleUnder", sig(RubyModule.class, String.class));
public Operand buildSClass(SClassNode sclassNode, IRScope s) { // class Foo // ... // class << self // ... // end // ... // end // // Here, the class << self declaration is in Foo's body. // Foo is the class in whose context this is being defined. Operand receiver = build(sclassNode.getReceiverNode(), s); // Create a dummy meta class and record it as being lexically defined in scope s IRModuleBody mc = new IRMetaClassBody(manager, s, manager.getMetaClassName(), sclassNode.getPosition().getLine(), sclassNode.getScope()); Variable classBody = s.getNewTemporaryVariable(); s.addInstr(new DefineMetaClassInstr(classBody, receiver, mc)); Variable ret = s.getNewTemporaryVariable(); s.addInstr(new ProcessModuleBodyInstr(ret, classBody)); mc.addInstr(new ReceiveSelfInstr(mc.getSelf())); // Set %current_scope = <current-scope> // Set %current_module = <current-module> mc.addInstr(new ReceiveClosureInstr(mc.getImplicitBlockArg())); mc.addInstr(new CopyInstr(mc.getCurrentScopeVariable(), new CurrentScope(mc))); mc.addInstr(new CopyInstr(mc.getCurrentModuleVariable(), new ScopeModule(mc))); // Create a new nested builder to ensure this gets its own IR builder state Operand rv = newIRBuilder(manager).build(sclassNode.getBodyNode(), mc); if (rv != null) mc.addInstr(new ReturnInstr(rv)); return ret; }
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 IRModuleBody(IRManager manager, IRScope lexicalParent, RubySymbol name, int lineNumber, StaticScope staticScope) { super(manager, lexicalParent, name, lineNumber, staticScope); if (!getManager().isDryRun()) { if (staticScope != null) staticScope.setIRScope(this); } }
protected Handle emitModuleBodyJIT(IRModuleBody method) { String name = JavaNameMangler.encodeScopeForBacktrace(method) + '$' + methodIndex++; String clsName = jvm.scriptToClass(method.getFile()); jvm.pushscript(clsName, method.getFile()); Signature signature = signatureFor(method, false); emitScope(method, name, signature, false, true); Handle handle = new Handle( Opcodes.H_INVOKESTATIC, jvm.clsData().clsName, name, sig(signature.type().returnType(), signature.type().parameterArray()), false); jvm.cls().visitEnd(); jvm.popclass(); return handle; }
public Operand buildModule(ModuleNode moduleNode, IRScope s) { Colon3Node cpath = moduleNode.getCPath(); String moduleName = cpath.getName(); Operand container = getContainerFromCPath(cpath, s); // Build the new module IRModuleBody m = new IRModuleBody(manager, s, moduleName, moduleNode.getPosition().getLine(), moduleNode.getScope()); Variable moduleBody = s.getNewTemporaryVariable(); s.addInstr(new DefineModuleInstr(moduleBody, m, container)); Variable ret = s.getNewTemporaryVariable(); s.addInstr(new ProcessModuleBodyInstr(ret, moduleBody)); m.addInstr(new ReceiveSelfInstr(m.getSelf())); // Set %current_scope = <c> // Set %current_module = module<c> m.addInstr(new CopyInstr(m.getCurrentScopeVariable(), new CurrentScope(m))); m.addInstr(new CopyInstr(m.getCurrentModuleVariable(), new ScopeModule(m))); // Create a new nested builder to ensure this gets its own IR builder state Operand rv = newIRBuilder(manager).build(moduleNode.getBodyNode(), m); if (rv != null) m.addInstr(new ReturnInstr(rv)); return ret; }
@Override public void DefineModuleInstr(DefineModuleInstr definemoduleinstr) { IRModuleBody newIRModuleBody = definemoduleinstr.getNewIRModuleBody(); StaticScope scope = newIRModuleBody.getStaticScope(); if (scope.getRequiredArgs() > 3 || scope.getRestArg() >= 0 || scope.getOptionalArgs() != 0) { throw new RuntimeException("can't compile variable method: " + this); a.ldc(newIRModuleBody.getName()); a.ldc(newIRModuleBody.getFileName()); a.ldc(newIRModuleBody.getLineNumber()); visit(definemoduleinstr.getContainer()); m.invokeHelper("checkIsRubyModule", RubyModule.class, ThreadContext.class, Object.class); a.ldc(newIRModuleBody.getName()); a.invokevirtual(p(RubyModule.class), "defineOrGetModuleUnder", sig(RubyModule.class, String.class));
public Operand buildSClass(SClassNode sclassNode, IRScope s) { // class Foo // ... // class << self // ... // end // ... // end // // Here, the class << self declaration is in Foo's body. // Foo is the class in whose context this is being defined. Operand receiver = build(sclassNode.getReceiverNode(), s); // Create a dummy meta class and record it as being lexically defined in scope s IRModuleBody mc = new IRMetaClassBody(manager, s, manager.getMetaClassName(), sclassNode.getPosition().getLine(), sclassNode.getScope()); Variable classBody = s.getNewTemporaryVariable(); s.addInstr(new DefineMetaClassInstr(classBody, receiver, mc)); Variable ret = s.getNewTemporaryVariable(); s.addInstr(new ProcessModuleBodyInstr(ret, classBody)); mc.addInstr(new ReceiveSelfInstr(mc.getSelf())); // Set %current_scope = <current-scope> // Set %current_module = <current-module> mc.addInstr(new ReceiveClosureInstr(mc.getImplicitBlockArg())); mc.addInstr(new CopyInstr(mc.getCurrentScopeVariable(), new CurrentScope(mc))); mc.addInstr(new CopyInstr(mc.getCurrentModuleVariable(), new ScopeModule(mc))); // Create a new nested builder to ensure this gets its own IR builder state Operand rv = newIRBuilder(manager).build(sclassNode.getBodyNode(), mc); if (rv != null) mc.addInstr(new ReturnInstr(rv)); return ret; }
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 IRModuleBody(IRManager manager, IRScope lexicalParent, RubySymbol name, int lineNumber, StaticScope staticScope) { super(manager, lexicalParent, name, lineNumber, staticScope); if (!getManager().isDryRun()) { if (staticScope != null) staticScope.setIRScope(this); } }
protected Handle emitModuleBodyJIT(IRModuleBody method) { String name = JavaNameMangler.encodeScopeForBacktrace(method) + '$' + methodIndex++; String clsName = jvm.scriptToClass(method.getFile()); jvm.pushscript(clsName, method.getFile()); Signature signature = signatureFor(method, false); emitScope(method, name, signature, false, true); Handle handle = new Handle( Opcodes.H_INVOKESTATIC, jvm.clsData().clsName, name, sig(signature.type().returnType(), signature.type().parameterArray()), false); jvm.cls().visitEnd(); jvm.popclass(); return handle; }
@Override public void DefineMetaClassInstr(DefineMetaClassInstr definemetaclassinstr) { IRModuleBody metaClassBody = definemetaclassinstr.getMetaClassBody(); StaticScope scope = metaClassBody.getStaticScope(); if (scope.getRequiredArgs() > 3 || scope.getRestArg() >= 0 || scope.getOptionalArgs() != 0) { throw new RuntimeException("can't compile variable method: " + this); a.ldc(metaClassBody.getName()); a.ldc(metaClassBody.getFileName()); a.ldc(metaClassBody.getLineNumber());
public IRMethod loadInternalMethod(ThreadContext context, IRubyObject self, String method) { try { RubyModule type = self.getMetaClass(); String fileName = "classpath:/jruby/ruby_implementations/" + type + "/" + method + ".rb"; FileResource file = JRubyFile.createResourceAsFile(context.runtime, fileName); Node parseResult = parse(context, file, fileName); IScopingNode scopeNode = (IScopingNode) parseResult.childNodes().get(0); scopeNode.getScope().setModule(type); DefNode defNode = (DefNode) scopeNode.getBodyNode(); IRScriptBody script = new IRScriptBody(this, runtime.newSymbol(parseResult.getFile()), ((RootNode) parseResult).getStaticScope()); IRModuleBody containingScope; if (scopeNode instanceof ModuleNode) { containingScope = new IRModuleBody(this, script, scopeNode.getCPath().getName(), 0, scopeNode.getScope()); } else { containingScope = new IRClassBody(this, script, scopeNode.getCPath().getName(), 0, scopeNode.getScope()); } IRMethod newMethod = new IRMethod(this, containingScope, defNode, context.runtime.newSymbol(method), true, 0, defNode.getScope(), false); newMethod.prepareForCompilation(); return newMethod; } catch (IOException e) { e.printStackTrace(); // FIXME: More elegantly handle broken internal implementations return null; } }
public IRModuleBody(IRManager manager, IRScope lexicalParent, String name, String fileName, int lineNumber, StaticScope scope) { super(manager, lexicalParent, name, fileName, lineNumber, scope); if (!getManager().isDryRun()) { if (scope != null) ((IRStaticScope)scope).setIRScope(this); updateVersion(); } }
@Override public String toString() { return super.toString() + "(" + newIRModuleBody.getName() + ", " + container + ", " + newIRModuleBody.getFileName() + ")"; }
@Override public String[] toStringNonOperandArgs() { return new String[] { "name: " + newIRModuleBody.getId() }; }