OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/v8.h" |
| 6 |
| 7 #include "test/cctest/compiler/function-tester.h" |
| 8 |
| 9 #if V8_TURBOFAN_TARGET |
| 10 |
| 11 using namespace v8::internal; |
| 12 using namespace v8::internal::compiler; |
| 13 |
| 14 // TODO(sigurds) At the moment we do not write optimization frames when |
| 15 // inlining, thus the reported stack depth changes depending on inlining. |
| 16 // AssertStackDepth checks the stack depth at a simple way to ensure that |
| 17 // inlining actually occurs. |
| 18 // Once inlining creates optimization frames, all these unit tests need to |
| 19 // check that the optimization frame is there. |
| 20 |
| 21 |
| 22 static void AssertStackDepth(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 23 v8::HandleScope scope(args.GetIsolate()); |
| 24 v8::Handle<v8::StackTrace> stackTrace = v8::StackTrace::CurrentStackTrace( |
| 25 args.GetIsolate(), 10, v8::StackTrace::kDetailed); |
| 26 CHECK_EQ(args[0]->ToInt32()->Value(), stackTrace->GetFrameCount()); |
| 27 } |
| 28 |
| 29 |
| 30 static void InstallAssertStackDepthHelper(v8::Isolate* isolate) { |
| 31 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 32 v8::Local<v8::FunctionTemplate> t = |
| 33 v8::FunctionTemplate::New(isolate, AssertStackDepth); |
| 34 context->Global()->Set(v8_str("AssertStackDepth"), t->GetFunction()); |
| 35 } |
| 36 |
| 37 |
| 38 TEST(SimpleInlining) { |
| 39 FLAG_turbo_inlining = true; |
| 40 FunctionTester T( |
| 41 "(function(){" |
| 42 "function foo(s) { AssertStackDepth(1); return s; };" |
| 43 "function bar(s, t) { return foo(s); };" |
| 44 "return bar;})();"); |
| 45 |
| 46 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 47 T.CheckCall(T.Val(1), T.Val(1), T.Val(2)); |
| 48 } |
| 49 |
| 50 |
| 51 TEST(SimpleInliningContext) { |
| 52 FLAG_turbo_inlining = true; |
| 53 FunctionTester T( |
| 54 "(function () {" |
| 55 "function foo(s) { AssertStackDepth(1); var x = 12; return s + x; };" |
| 56 "function bar(s, t) { return foo(s); };" |
| 57 "return bar;" |
| 58 "})();"); |
| 59 |
| 60 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 61 T.CheckCall(T.Val(13), T.Val(1), T.Val(2)); |
| 62 } |
| 63 |
| 64 |
| 65 TEST(CaptureContext) { |
| 66 FLAG_turbo_inlining = true; |
| 67 FunctionTester T( |
| 68 "var f = (function () {" |
| 69 "var x = 42;" |
| 70 "function bar(s) { return x + s; };" |
| 71 "return (function (s) { return bar(s); });" |
| 72 "})();" |
| 73 "(function (s) { return f(s)})"); |
| 74 |
| 75 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 76 T.CheckCall(T.Val(42 + 12), T.Val(12), T.undefined()); |
| 77 } |
| 78 |
| 79 |
| 80 // TODO(sigurds) For now we do not inline any native functions. If we do at |
| 81 // some point, change this test. |
| 82 TEST(DontInlineEval) { |
| 83 FLAG_turbo_inlining = true; |
| 84 FunctionTester T( |
| 85 "var x = 42;" |
| 86 "(function () {" |
| 87 "function bar(s, t) { return eval(\"AssertStackDepth(2); x\") };" |
| 88 "return bar;" |
| 89 "})();"); |
| 90 |
| 91 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 92 T.CheckCall(T.Val(42), T.Val("x"), T.undefined()); |
| 93 } |
| 94 |
| 95 |
| 96 TEST(InlineOmitArguments) { |
| 97 FLAG_turbo_inlining = true; |
| 98 FunctionTester T( |
| 99 "(function () {" |
| 100 "var x = 42;" |
| 101 "function bar(s, t, u, v) { AssertStackDepth(1); return x + s; };" |
| 102 "return (function (s,t) { return bar(s); });" |
| 103 "})();"); |
| 104 |
| 105 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 106 T.CheckCall(T.Val(42 + 12), T.Val(12), T.undefined()); |
| 107 } |
| 108 |
| 109 |
| 110 TEST(InlineSurplusArguments) { |
| 111 FLAG_turbo_inlining = true; |
| 112 FunctionTester T( |
| 113 "(function () {" |
| 114 "var x = 42;" |
| 115 "function foo(s) { AssertStackDepth(1); return x + s; };" |
| 116 "function bar(s,t) { return foo(s,t,13); };" |
| 117 "return bar;" |
| 118 "})();"); |
| 119 |
| 120 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 121 T.CheckCall(T.Val(42 + 12), T.Val(12), T.undefined()); |
| 122 } |
| 123 |
| 124 |
| 125 TEST(InlineTwice) { |
| 126 FLAG_turbo_inlining = true; |
| 127 FunctionTester T( |
| 128 "(function () {" |
| 129 "var x = 42;" |
| 130 "function bar(s) { AssertStackDepth(1); return x + s; };" |
| 131 "return (function (s,t) { return bar(s) + bar(t); });" |
| 132 "})();"); |
| 133 |
| 134 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 135 T.CheckCall(T.Val(2 * 42 + 12 + 4), T.Val(12), T.Val(4)); |
| 136 } |
| 137 |
| 138 |
| 139 TEST(InlineTwiceDependent) { |
| 140 FLAG_turbo_inlining = true; |
| 141 FunctionTester T( |
| 142 "(function () {" |
| 143 "var x = 42;" |
| 144 "function foo(s) { AssertStackDepth(1); return x + s; };" |
| 145 "function bar(s,t) { return foo(foo(s)); };" |
| 146 "return bar;" |
| 147 "})();"); |
| 148 |
| 149 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 150 T.CheckCall(T.Val(42 + 42 + 12), T.Val(12), T.Val(4)); |
| 151 } |
| 152 |
| 153 |
| 154 TEST(InlineTwiceDependentDiamond) { |
| 155 FLAG_turbo_inlining = true; |
| 156 FunctionTester T( |
| 157 "(function () {" |
| 158 "function foo(s) { if (true) {" |
| 159 " return 12 } else { return 13; } };" |
| 160 "function bar(s,t) { return foo(foo(1)); };" |
| 161 "return bar;" |
| 162 "})();"); |
| 163 |
| 164 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 165 T.CheckCall(T.Val(12), T.undefined(), T.undefined()); |
| 166 } |
| 167 |
| 168 |
| 169 TEST(InlineTwiceDependentDiamondReal) { |
| 170 FLAG_turbo_inlining = true; |
| 171 FunctionTester T( |
| 172 "(function () {" |
| 173 "var x = 41;" |
| 174 "function foo(s) { AssertStackDepth(1); if (s % 2 == 0) {" |
| 175 " return x - s } else { return x + s; } };" |
| 176 "function bar(s,t) { return foo(foo(s)); };" |
| 177 "return bar;" |
| 178 "})();"); |
| 179 |
| 180 InstallAssertStackDepthHelper(CcTest::isolate()); |
| 181 T.CheckCall(T.Val(-11), T.Val(11), T.Val(4)); |
| 182 } |
| 183 |
| 184 #endif // V8_TURBOFAN_TARGET |
OLD | NEW |