1 // Copyright 2015 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 "src/compiler/pipeline.h" 8 #include "src/handles.h" 9 #include "src/interpreter/bytecode-generator.h" 10 #include "src/interpreter/interpreter.h" 11 #include "src/isolate.h" 12 #include "src/parsing/parser.h" 13 #include "test/cctest/cctest.h" 14 #include "test/cctest/interpreter/source-position-matcher.h" 15 16 namespace v8 { 17 namespace internal { 18 namespace interpreter { 19 20 // Flags enabling optimizations that change generated bytecode array. 21 // Format is <command-line flag> <flag name> <bit index> 22 #define OPTIMIZATION_FLAGS(V) \ 23 V(FLAG_ignition_reo, kUseReo, 0) \ 24 V(FLAG_ignition_peephole, kUsePeephole, 1) \ 25 V(FLAG_ignition_filter_expression_positions, kUseFilterExpressionPositions, \ 26 2) \ 27 V(FLAG_ignition_deadcode, kUseDeadCode, 3) 28 29 #define DECLARE_BIT(_, Name, BitIndex) static const int Name = 1 << BitIndex; 30 OPTIMIZATION_FLAGS(DECLARE_BIT) 31 #undef DECLARE_BIT 32 33 // Test cases source positions are checked for. Please ensure all 34 // combinations of flags are present here. This is done manually 35 // because it provides easier to comprehend failure case for humans. 36 #define TEST_CASES(V) \ 37 V(UsingReo, kUseReo) \ 38 V(UsingPeephole, kUsePeephole) \ 39 V(UsingDeadCode, kUseDeadCode) \ 40 V(UsingFilterExpressionPositions, kUseFilterExpressionPositions) \ 41 V(UsingReoAndPeephole, kUseReo | kUsePeephole) \ 42 V(UsingReoAndFilterExpressionPositions, \ 43 kUseReo | kUseFilterExpressionPositions) \ 44 V(UsingReoAndDeadCode, kUseReo | kUseDeadCode) \ 45 V(UsingPeepholeAndFilterExpressionPositions, \ 46 kUsePeephole | kUseFilterExpressionPositions) \ 47 V(UsingPeepholeAndDeadCode, kUsePeephole | kUseDeadCode) \ 48 V(UsingFilterExpressionPositionsAndDeadCode, \ 49 kUseFilterExpressionPositions | kUseDeadCode) \ 50 V(UsingAllOptimizations, \ 51 kUseReo | kUsePeephole | kUseFilterExpressionPositions | kUseDeadCode) 52 53 struct TestCaseData { 54 TestCaseData(const char* const script, 55 const char* const declaration_parameters = "", 56 const char* const arguments = "") 57 : script_(script), 58 declaration_parameters_(declaration_parameters), 59 arguments_(arguments) {} 60 61 const char* const script() const { return script_; } 62 const char* const declaration_parameters() const { 63 return declaration_parameters_; 64 } 65 const char* const arguments() const { return arguments_; } 66 67 private: 68 TestCaseData(); 69 70 const char* const script_; 71 const char* const declaration_parameters_; 72 const char* const arguments_; 73 }; 74 75 static const TestCaseData kTestCaseData[] = { 76 {"var x = (y = 3) + (x = y); return x + y;"}, 77 {"var x = 55;\n" 78 "var y = x + (x = 1) + (x = 2) + (x = 3);\n" 79 "return y;"}, 80 {"var x = 10; return x >>> 3;\n"}, 81 {"var x = 0; return x || (1, 2, 3);\n"}, 82 {"return a || (a, b, a, b, c = 5, 3);\n"}, 83 {"var a = 3; var b = 4; a = b; b = a; a = b; return a;\n"}, 84 {"var a = 1; return [[a, 2], [a + 2]];\n"}, 85 {"var a = 1; if (a || a < 0) { return 1; }\n"}, 86 {"var b;" 87 "b = a.name;" 88 "b = a.name;" 89 "a.name = a;" 90 "b = a.name;" 91 "a.name = a;" 92 "return b;"}, 93 {"var sum = 0;\n" 94 "outer: {\n" 95 " for (var x = 0; x < 10; ++x) {\n" 96 " for (var y = 0; y < 3; ++y) {\n" 97 " ++sum;\n" 98 " if (x + y == 12) { break outer; }\n" 99 " }\n" 100 " }\n" 101 "}\n" 102 "return sum;\n"}, 103 {"var a = 1;" 104 "switch (a) {" 105 " case 1: return a * a + 1;" 106 " case 1: break;" 107 " case 2: return (a = 3) * a + (a = 4);" 108 " case 3:" 109 "}" 110 "return a;"}, 111 {"for (var p of [0, 1, 2]) {}"}, 112 {"var x = { 'a': 1, 'b': 2 };" 113 "for (x['a'] of [1,2,3]) { return x['a']; }"}, 114 {"while (x == 4) {\n" 115 " var y = x + 1;\n" 116 " if (y == 2) break;\n" 117 " for (z['a'] of [0]) {\n" 118 " x += (x *= 3) + y;" 119 " }\n" 120 "}\n"}, 121 {"function g(a, b) { return a.func(b + b, b); }\n" 122 "g(new (function Obj() { this.func = function() { return; }})(), 1)\n"}, 123 {"return some_global[name];", "name", "'a'"}}; 124 125 class OptimizedBytecodeSourcePositionTester final { 126 public: 127 explicit OptimizedBytecodeSourcePositionTester(Isolate* isolate) 128 : isolate_(isolate) { 129 SaveOptimizationFlags(); 130 saved_flag_ignition_ = FLAG_ignition; 131 FLAG_ignition = true; 132 saved_flag_always_opt_ = FLAG_always_opt; 133 FLAG_always_opt = false; 134 } 135 136 ~OptimizedBytecodeSourcePositionTester() { 137 RestoreOptimizationFlags(); 138 FLAG_ignition = saved_flag_ignition_; 139 FLAG_always_opt = saved_flag_always_opt_; 140 } 141 142 bool SourcePositionsMatch(int optimization_bitmap, const char* function_body, 143 const char* function_decl_params, 144 const char* function_args); 145 146 private: 147 Handle<BytecodeArray> MakeBytecode(int optimization_bitmap, 148 const char* function_body, 149 const char* function_decl_params, 150 const char* function_args); 151 static std::string MakeScript(const char* function_body, 152 const char* function_decl_params, 153 const char* function_args); 154 155 void SetOptimizationFlags(int optimization_bitmap); 156 void SaveOptimizationFlags(); 157 void RestoreOptimizationFlags(); 158 159 Isolate* isolate() const { return isolate_; } 160 161 Isolate* isolate_; 162 int saved_optimization_bitmap_; 163 bool saved_flag_ignition_; 164 bool saved_flag_always_opt_; 165 }; 166 167 // static 168 std::string OptimizedBytecodeSourcePositionTester::MakeScript( 169 const char* function_body, const char* function_decl_params, 170 const char* function_args) { 171 std::ostringstream os; 172 os << "function test_function" 173 << "(" << function_decl_params << ") {"; 174 os << function_body; 175 os << "}"; 176 os << "test_function(" << function_args << ");"; 177 return os.str(); 178 } 179 180 Handle<BytecodeArray> OptimizedBytecodeSourcePositionTester::MakeBytecode( 181 int optimization_bitmap, const char* function_body, 182 const char* function_decl_params, const char* function_args) { 183 std::string script = 184 MakeScript(function_body, function_decl_params, function_args); 185 SetOptimizationFlags(optimization_bitmap); 186 CompileRun(script.c_str()); 187 188 Local<Function> api_function = Local<Function>::Cast( 189 CcTest::global() 190 ->Get(CcTest::isolate()->GetCurrentContext(), v8_str("test_function")) 191 .ToLocalChecked()); 192 Handle<JSFunction> function = 193 Handle<JSFunction>::cast(v8::Utils::OpenHandle(*api_function)); 194 return handle(function->shared()->bytecode_array()); 195 } 196 197 void OptimizedBytecodeSourcePositionTester::SetOptimizationFlags( 198 int optimization_bitmap) { 199 #define SET_FLAG(V8Flag, BitName, _) \ 200 V8Flag = (optimization_bitmap & BitName) ? true : false; 201 OPTIMIZATION_FLAGS(SET_FLAG) 202 #undef SET_FLAG 203 } 204 205 void OptimizedBytecodeSourcePositionTester::SaveOptimizationFlags() { 206 saved_optimization_bitmap_ = 0; 207 #define SAVE_FLAG(V8Flag, BitName, _) \ 208 if (V8Flag) saved_optimization_bitmap_ |= BitName; 209 #undef SET_FLAG 210 } 211 212 void OptimizedBytecodeSourcePositionTester::RestoreOptimizationFlags() { 213 SetOptimizationFlags(saved_optimization_bitmap_); 214 } 215 216 bool OptimizedBytecodeSourcePositionTester::SourcePositionsMatch( 217 int optimization_bitmap, const char* function_body, 218 const char* function_decl_params, const char* function_args) { 219 Handle<BytecodeArray> unoptimized_bytecode = 220 MakeBytecode(0, function_body, function_decl_params, function_args); 221 Handle<BytecodeArray> optimized_bytecode = MakeBytecode( 222 optimization_bitmap, function_body, function_decl_params, function_args); 223 SourcePositionMatcher matcher; 224 if (!matcher.Match(unoptimized_bytecode, optimized_bytecode)) { 225 return false; 226 } 227 return true; 228 } 229 230 void TestSourcePositionsEquivalent(int optimization_bitmap) { 231 HandleAndZoneScope handles; 232 // Ensure handler table is generated. 233 handles.main_isolate()->interpreter()->Initialize(); 234 235 OptimizedBytecodeSourcePositionTester tester(handles.main_isolate()); 236 for (auto test_case_data : kTestCaseData) { 237 CHECK(tester.SourcePositionsMatch( 238 optimization_bitmap, test_case_data.script(), 239 test_case_data.declaration_parameters(), test_case_data.arguments())); 240 } 241 } 242 243 #define MAKE_TEST(Name, Bitmap) \ 244 TEST(TestSourcePositionsEquivalent##Name) { \ 245 TestSourcePositionsEquivalent(Bitmap); \ 246 } 247 TEST_CASES(MAKE_TEST) 248 #undef MAKE_TEST 249 250 } // namespace interpreter 251 } // namespace internal 252 } // namespace v8 253