/* **************************************************************************** * * The generic body's half of a resume in the interpreter (IR_JIT_RESUME_INTERP, Runtime/Jit/JitResumeInterp.cs): each * test defines methods with the switch on, calls them once as they are, then runs the rest of a call from the start of * one of their statements with the locals given - what a typed body that gave up there will hand over - and compares * what that prints and answers with what the code from that statement on does with those locals. * * ***************************************************************************/ using System; using System.Collections.Generic; using IronRuby.Builtins; using IronRuby.Compiler.Ast; using IronRuby.Runtime; using IronRuby.Runtime.Calls; using IronRuby.Runtime.Jit; namespace IronRuby.Tests { public partial class Tests { private static RubyMethodInfo ResumeMethod(RubyModule/*!*/ module, string/*!*/ name) { return (RubyMethodInfo)module.ResolveMethod(name, VisibilityContext.AllVisible).Info; } /// The resume point of in the method's generic body, or null. private static ResumePoint ResumePointOf(RubyMethodInfo/*!*/ method, Node/*!*/ statement) { var table = JitResumeInterp.FindTable(method.GetSyntaxTree()); return (table != null) ? table.Find(statement) : null; } /// Runs the rest of a call of from ; locals as name, value pairs. private static object ResumeAt(RubyMethodInfo/*!*/ method, Node/*!*/ statement, object self, object[]/*!*/ arguments, params object[] locals) { var point = ResumePointOf(method, statement); if (point == null || !JitResumeInterp.CanResume(point)) { throw new InvalidOperationException("no resume point at the statement" + (point != null ? ": " + point.Target : "")); } var names = point.Table.SlotNames; var values = new object[names.Length]; for (int i = 0; i < locals.Length; i += 2) { values[Array.IndexOf(names, (string)locals[i])] = locals[i + 1]; } var all = new object[2 + arguments.Length]; all[0] = self; Array.Copy(arguments, 0, all, 2, arguments.Length); return JitResumeInterp.Resume(point, all, values); } private static Statements ResumeBody(RubyMethodInfo/*!*/ method) { return method.GetSyntaxTree().Body.Statements; } /// /// Whether method bodies have an interpreted form here: a run that compiles them to IL at once (/debug, /noadaptive) /// has nothing to resume in, and the tests below have nothing to test. /// private bool ResumesHere() { bool enabled = JitResumeInterp.Enabled; JitResumeInterp.Enabled = true; try { var probe = (RubyModule)Engine.Execute("class RIProbe; def m; 1; end; self; end"); var method = ResumeMethod(probe, "m"); method.GetDelegate(); var table = JitResumeInterp.FindTable(method.GetSyntaxTree()); return table != null && table.Generic.Target is Microsoft.Scripting.Interpreter.LightLambda; } finally { JitResumeInterp.Enabled = enabled; } } private void WithResumeInterp(Action/*!*/ test) { bool enabled = JitResumeInterp.Enabled; JitResumeInterp.Enabled = true; try { test(); } finally { JitResumeInterp.Enabled = enabled; } } /// /// Statements of the method body, of a while loop's body, of a begin block's body and of an if branch, in a light frame: /// the statements before the point do not run, the condition of the if is not evaluated again, the loop goes on through /// its head, a rescue clause takes what the resumed statement raises and the ensure clause runs once. /// public void ResumeInterp_Statements1() { if (!ResumesHere()) { return; } var results = new List(); WithResumeInterp(() => AssertOutput(() => { var c = (RubyClass)Engine.Execute(@" class RIa def m(a) puts ""s0 #{a}"" b = a * 2 puts ""s2 #{a} #{b}"" b + 1 end def w(n) i = 0 s = 0 while i < n s += i puts ""i=#{i} s=#{s}"" i += 1 end s end def g(x) begin puts 'a' y = 10 / x puts ""y=#{y}"" rescue ZeroDivisionError puts 'rescued' ensure puts 'ensure' end :done end def f(c) if c puts 'then' else puts 'e1' puts 'e2' end puts 'after' end self end "); var self = Engine.Execute("RIa.new"); Engine.Execute("o = RIa.new; p o.m(5); p o.w(4); p o.g(2); o.f(false)"); Engine.Execute("puts '--'"); var m = ResumeMethod(c, "m"); results.Add(Context.Inspect(ResumeAt(m, ResumeBody(m)[2], self, new object[] { 5 }, "a", 5, "b", 7)).ToString()); var w = ResumeMethod(c, "w"); var loop = (WhileLoopExpression)ResumeBody(w)[2]; results.Add(Context.Inspect(ResumeAt(w, loop.Statements[1], self, new object[] { 4 }, "n", 4, "i", 2, "s", 10)).ToString()); var g = ResumeMethod(c, "g"); var begin = (Body)ResumeBody(g)[0]; results.Add(Context.Inspect(ResumeAt(g, begin.Statements[1], self, new object[] { 0 }, "x", 0)).ToString()); results.Add(Context.Inspect(ResumeAt(g, begin.Statements[2], self, new object[] { 2 }, "x", 2, "y", 3)).ToString()); var f = ResumeMethod(c, "f"); var branch = (IfExpression)ResumeBody(f)[0]; ResumeAt(f, branch.ElseIfClauses[0].Statements[1], self, new object[] { true }, "c", true); }, @" s0 5 s2 5 10 11 i=0 s=0 i=1 s=1 i=2 s=3 i=3 s=6 6 a y=5 ensure :done e1 e2 after -- s2 5 7 i=2 s=10 i=3 s=13 rescued ensure y=3 ensure e2 after ")); AreEqual(String.Join(" ", results), "8 13 :done :done"); } /// /// A full frame (the method has a block): the locals are in the scope's tuple, which the block sees, and a while loop is /// entered through its entry code (EnterLoop marks the scope). /// public void ResumeInterp_FullFrame1() { if (!ResumesHere()) { return; } var results = new List(); WithResumeInterp(() => AssertOutput(() => { var c = (RubyClass)Engine.Execute(@" class RIb def fb(a) x = a [1].each { |q| x += q } puts x x end def wf(n) i = 0 while i < n [1].each { } puts i i += 1 end :out end self end "); var self = Engine.Execute("RIb.new"); Engine.Execute("o = RIb.new; o.fb(1); p o.wf(3)"); Engine.Execute("puts '--'"); var fb = ResumeMethod(c, "fb"); results.Add(Context.Inspect(ResumeAt(fb, ResumeBody(fb)[1], self, new object[] { 1 }, "a", 1, "x", 40)).ToString()); var wf = ResumeMethod(c, "wf"); var loop = (WhileLoopExpression)ResumeBody(wf)[1]; results.Add(Context.Inspect(ResumeAt(wf, loop.Statements[1], self, new object[] { 3 }, "n", 3, "i", 1)).ToString()); }, @" 2 0 1 2 :out -- 41 1 2 ")); AreEqual(String.Join(" ", results), "41 :out"); } /// /// A jump into a begin block runs the block's entry code: $! as the block found it, which the block restores once its /// rescue clause is done (a method called from a rescue clause of its caller). /// public void ResumeInterp_EntryException1() { if (!ResumesHere()) { return; } var results = new List(); WithResumeInterp(() => AssertOutput(() => { var c = (RubyClass)Engine.Execute(@" class RId def gx(x) begin y = 10 / x rescue ZeroDivisionError y = 0 end p $! y end self end "); var self = Engine.Execute("RId.new"); Engine.Execute("begin; raise 'outer'; rescue; p RId.new.gx(0); end"); var gx = ResumeMethod(c, "gx"); var begin = (Body)ResumeBody(gx)[0]; Context.CurrentException = new RuntimeError("outer"); results.Add(Context.Inspect(ResumeAt(gx, begin.Statements[0], self, new object[] { 0 }, "x", 0)).ToString()); }, @" # 0 # ")); AreEqual(String.Join(" ", results), "0"); } /// /// The typed side: a method the old rule declines (an attribute write on another object, then an operation that can /// deopt) is compiled to resume, run hot, then made to deopt after its effect - in the body, in a while loop and in a /// begin block - and to come back with a value its Integer type cannot hold. Each output is CRuby's; the effects run /// once. /// public void ResumeInterp_TypedBody1() { int resumes = JitResumeInterp.Resumes; WithResumeInterp(() => TestOutput(@" class RIe attr_accessor :v end class RIf def initialize(o); @o = o; @a = [1, 2, 3, 4]; @n = 0; end attr_accessor :a, :n def put(x) @o.v = x x + @a[0] end def sum(k) i = 0 s = 0 while i < k @n += 1 s = s + @a[i] i += 1 end s end def guarded(i) @n += 1 begin @n += 10 @y = @a[i] + 1 rescue NoMethodError @n += 100 end @n end end o = RIe.new f = RIf.new(o) # what printing these loads first: a method defined after the warm-up takes the typed entries down p 0, 2**62, 2.5, :a 2000.times { |i| f.put(i); f.sum(4); f.guarded(1) } f.n = 0 f.a = [2**62, 2.5, 3, nil] p f.put(2**62), o.v p f.sum(3), f.n p f.guarded(3), f.guarded(1) ", @" 0 4611686018427387904 2.5 :a 9223372036854775808 4611686018427387904 4.611686018427388e+18 3 114 125 ")); Assert(JitResumeInterp.Resumes > resumes, "no call resumed"); } /// /// A body that resumes on every call, called only directly by another typed body: its resumes count as its deopts, it /// retires, and its caller profiles again (JitStub.CallersLeave) rather than call a body its stub has given up on. /// Without that every one of the calls below resumed. /// public void ResumeInterp_Retires1() { int resumes = JitResumeInterp.Resumes; WithResumeInterp(() => TestOutput(@" class RIg; attr_accessor :v; end class RIh def initialize; @peer = RIg.new; @a = [1, 2, 3]; @hits = 0; end attr_accessor :a def find(k) @peer.v = k @a[k] + 1 end def get(k) @hits += 1 find(k) @hits end end t = RIh.new p 0, 2.5 20000.times { t.get(1) } t.a = [1, 2.5, 3] s = 0 50000.times { s += t.get(1) } p s ", @" 0 2.5 2250025000 ")); int resumed = JitResumeInterp.Resumes - resumes; Assert(resumed > 0 && resumed < 10000, "resumed " + resumed + " times"); } /// /// Statements that are no resume points: in a rescue clause, in an else clause, in an ensure clause, in a block; and /// points that are not of the method's own code (a block's body) are not the method's. /// public void ResumeInterp_NoPoints1() { WithResumeInterp(() => { var c = (RubyClass)Engine.Execute(@" class RIc def r(x) begin x += 1 rescue x = 0 else x = 2 ensure x = 3 end [1].each { |q| x += q } x end self end "); Engine.Execute("RIc.new.r(1)"); var r = ResumeMethod(c, "r"); var begin = (Body)ResumeBody(r)[0]; Assert(ResumePointOf(r, begin) != null && ResumePointOf(r, begin.Statements[0]) != null); Assert(ResumePointOf(r, begin.RescueClauses[0].Statements[0]) == null); Assert(ResumePointOf(r, begin.ElseStatements[0]) == null); Assert(ResumePointOf(r, begin.EnsureStatements[0]) == null); var each = (MethodCall)ResumeBody(r)[1]; Assert(ResumePointOf(r, each) != null && ResumePointOf(r, ((BlockDefinition)each.Block).Body[0]) == null); }); } } }