| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "test/cctest/interpreter/bytecode-expectations-printer.h" | 5 #include "test/cctest/interpreter/bytecode-expectations-printer.h" |
| 6 | 6 |
| 7 #include <iostream> | 7 #include <iostream> |
| 8 #include <vector> |
| 8 | 9 |
| 9 #include "include/libplatform/libplatform.h" | 10 #include "include/libplatform/libplatform.h" |
| 10 #include "include/v8.h" | 11 #include "include/v8.h" |
| 11 | 12 |
| 12 #include "src/base/logging.h" | 13 #include "src/base/logging.h" |
| 13 #include "src/base/smart-pointers.h" | 14 #include "src/base/smart-pointers.h" |
| 14 #include "src/compiler.h" | 15 #include "src/compiler.h" |
| 15 | 16 |
| 16 #include "src/interpreter/bytecode-array-iterator.h" | 17 #include "src/interpreter/bytecode-array-iterator.h" |
| 17 #include "src/interpreter/bytecode-generator.h" | 18 #include "src/interpreter/bytecode-generator.h" |
| 18 #include "src/interpreter/bytecodes.h" | 19 #include "src/interpreter/bytecodes.h" |
| 19 #include "src/interpreter/interpreter.h" | 20 #include "src/interpreter/interpreter.h" |
| 20 | 21 |
| 21 namespace v8 { | 22 namespace v8 { |
| 22 namespace internal { | 23 namespace internal { |
| 23 namespace interpreter { | 24 namespace interpreter { |
| 24 | 25 |
| 26 // static |
| 27 const char* const BytecodeExpectationsPrinter::kDefaultTopFunctionName = |
| 28 "__genbckexp_wrapper__"; |
| 29 |
| 25 v8::Local<v8::String> BytecodeExpectationsPrinter::V8StringFromUTF8( | 30 v8::Local<v8::String> BytecodeExpectationsPrinter::V8StringFromUTF8( |
| 26 const char* data) const { | 31 const char* data) const { |
| 27 return v8::String::NewFromUtf8(isolate_, data, v8::NewStringType::kNormal) | 32 return v8::String::NewFromUtf8(isolate_, data, v8::NewStringType::kNormal) |
| 28 .ToLocalChecked(); | 33 .ToLocalChecked(); |
| 29 } | 34 } |
| 30 | 35 |
| 31 std::string BytecodeExpectationsPrinter::WrapCodeInFunction( | 36 std::string BytecodeExpectationsPrinter::WrapCodeInFunction( |
| 32 const char* function_name, const std::string& function_body) const { | 37 const char* function_name, const std::string& function_body) const { |
| 33 std::ostringstream program_stream; | 38 std::ostringstream program_stream; |
| 34 program_stream << "function " << function_name << "() {" << function_body | 39 program_stream << "function " << function_name << "() {" << function_body |
| 35 << "}\n" | 40 << "}\n" |
| 36 << function_name << "();"; | 41 << function_name << "();"; |
| 37 | 42 |
| 38 return program_stream.str(); | 43 return program_stream.str(); |
| 39 } | 44 } |
| 40 | 45 |
| 41 v8::Local<v8::Value> BytecodeExpectationsPrinter::CompileAndRun( | 46 v8::Local<v8::Script> BytecodeExpectationsPrinter::Compile( |
| 42 const char* program) const { | 47 const char* program) const { |
| 43 v8::Local<v8::String> source = V8StringFromUTF8(program); | 48 v8::Local<v8::String> source = V8StringFromUTF8(program); |
| 44 v8::Local<v8::Script> script = | 49 return v8::Script::Compile(isolate_->GetCurrentContext(), source) |
| 45 v8::Script::Compile(isolate_->GetCurrentContext(), source) | 50 .ToLocalChecked(); |
| 46 .ToLocalChecked(); | 51 } |
| 47 | 52 |
| 48 v8::Local<v8::Value> result; | 53 void BytecodeExpectationsPrinter::Run(v8::Local<v8::Script> script) const { |
| 49 CHECK(script->Run(isolate_->GetCurrentContext()).ToLocal(&result)); | 54 CHECK(!script->Run(isolate_->GetCurrentContext()).IsEmpty()); |
| 50 | |
| 51 return result; | |
| 52 } | 55 } |
| 53 | 56 |
| 54 i::Handle<v8::internal::BytecodeArray> | 57 i::Handle<v8::internal::BytecodeArray> |
| 55 BytecodeExpectationsPrinter::GetBytecodeArrayForGlobal( | 58 BytecodeExpectationsPrinter::GetBytecodeArrayForGlobal( |
| 56 const char* global_name) const { | 59 const char* global_name) const { |
| 57 const v8::Local<v8::Context>& context = isolate_->GetCurrentContext(); | 60 const v8::Local<v8::Context>& context = isolate_->GetCurrentContext(); |
| 58 v8::Local<v8::String> v8_global_name = V8StringFromUTF8(global_name); | 61 v8::Local<v8::String> v8_global_name = V8StringFromUTF8(global_name); |
| 59 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | 62 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( |
| 60 context->Global()->Get(context, v8_global_name).ToLocalChecked()); | 63 context->Global()->Get(context, v8_global_name).ToLocalChecked()); |
| 61 i::Handle<i::JSFunction> js_function = | 64 i::Handle<i::JSFunction> js_function = |
| (...skipping 17 matching lines...) Expand all Loading... |
| 79 break; | 82 break; |
| 80 default: | 83 default: |
| 81 stream << c; | 84 stream << c; |
| 82 break; | 85 break; |
| 83 } | 86 } |
| 84 } | 87 } |
| 85 } | 88 } |
| 86 | 89 |
| 87 void BytecodeExpectationsPrinter::PrintBytecodeOperand( | 90 void BytecodeExpectationsPrinter::PrintBytecodeOperand( |
| 88 std::ostream& stream, const BytecodeArrayIterator& bytecode_iter, | 91 std::ostream& stream, const BytecodeArrayIterator& bytecode_iter, |
| 89 const Bytecode& bytecode, int op_index) const { | 92 const Bytecode& bytecode, int op_index, int parameter_count) const { |
| 90 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index); | 93 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index); |
| 91 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index); | 94 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index); |
| 92 | 95 |
| 93 const char* size_tag; | 96 const char* size_tag; |
| 94 switch (op_size) { | 97 switch (op_size) { |
| 95 case OperandSize::kByte: | 98 case OperandSize::kByte: |
| 96 size_tag = "8"; | 99 size_tag = "8"; |
| 97 break; | 100 break; |
| 98 case OperandSize::kShort: | 101 case OperandSize::kShort: |
| 99 size_tag = "16"; | 102 size_tag = "16"; |
| 100 break; | 103 break; |
| 101 default: | 104 default: |
| 102 UNREACHABLE(); | 105 UNREACHABLE(); |
| 103 return; | 106 return; |
| 104 } | 107 } |
| 105 | 108 |
| 106 if (Bytecodes::IsRegisterOperandType(op_type)) { | 109 if (Bytecodes::IsRegisterOperandType(op_type)) { |
| 107 Register register_value = bytecode_iter.GetRegisterOperand(op_index); | 110 Register register_value = bytecode_iter.GetRegisterOperand(op_index); |
| 108 stream << 'R'; | 111 stream << 'R'; |
| 109 if (op_size != OperandSize::kByte) stream << size_tag; | 112 if (op_size != OperandSize::kByte) stream << size_tag; |
| 110 stream << '(' << register_value.index() << ')'; | 113 if (register_value.is_new_target()) { |
| 114 stream << "(new_target)"; |
| 115 } else if (register_value.is_current_context()) { |
| 116 stream << "(context)"; |
| 117 } else if (register_value.is_function_closure()) { |
| 118 stream << "(closure)"; |
| 119 } else if (register_value.is_parameter()) { |
| 120 int parameter_index = register_value.ToParameterIndex(parameter_count); |
| 121 if (parameter_index == 0) { |
| 122 stream << "(this)"; |
| 123 } else { |
| 124 stream << "(arg" << (parameter_index - 1) << ')'; |
| 125 } |
| 126 } else { |
| 127 stream << '(' << register_value.index() << ')'; |
| 128 } |
| 111 } else { | 129 } else { |
| 112 stream << 'U' << size_tag << '('; | 130 stream << 'U' << size_tag << '('; |
| 113 | 131 |
| 114 if (Bytecodes::IsImmediateOperandType(op_type)) { | 132 if (Bytecodes::IsImmediateOperandType(op_type)) { |
| 115 // We need a cast, otherwise the result is printed as char. | 133 // We need a cast, otherwise the result is printed as char. |
| 116 stream << static_cast<int>(bytecode_iter.GetImmediateOperand(op_index)); | 134 stream << static_cast<int>(bytecode_iter.GetImmediateOperand(op_index)); |
| 117 } else if (Bytecodes::IsRegisterCountOperandType(op_type)) { | 135 } else if (Bytecodes::IsRegisterCountOperandType(op_type)) { |
| 118 stream << bytecode_iter.GetRegisterCountOperand(op_index); | 136 stream << bytecode_iter.GetRegisterCountOperand(op_index); |
| 119 } else if (Bytecodes::IsIndexOperandType(op_type)) { | 137 } else if (Bytecodes::IsIndexOperandType(op_type)) { |
| 120 stream << bytecode_iter.GetIndexOperand(op_index); | 138 stream << bytecode_iter.GetIndexOperand(op_index); |
| 121 } else { | 139 } else { |
| 122 UNREACHABLE(); | 140 UNREACHABLE(); |
| 123 } | 141 } |
| 124 | 142 |
| 125 stream << ')'; | 143 stream << ')'; |
| 126 } | 144 } |
| 127 } | 145 } |
| 128 | 146 |
| 129 void BytecodeExpectationsPrinter::PrintBytecode( | 147 void BytecodeExpectationsPrinter::PrintBytecode( |
| 130 std::ostream& stream, const BytecodeArrayIterator& bytecode_iter) const { | 148 std::ostream& stream, const BytecodeArrayIterator& bytecode_iter, |
| 149 int parameter_count) const { |
| 131 Bytecode bytecode = bytecode_iter.current_bytecode(); | 150 Bytecode bytecode = bytecode_iter.current_bytecode(); |
| 132 | 151 |
| 133 stream << "B(" << Bytecodes::ToString(bytecode) << ')'; | 152 stream << "B(" << Bytecodes::ToString(bytecode) << ')'; |
| 134 | 153 |
| 135 int operands_count = Bytecodes::NumberOfOperands(bytecode); | 154 int operands_count = Bytecodes::NumberOfOperands(bytecode); |
| 136 for (int op_index = 0; op_index < operands_count; ++op_index) { | 155 for (int op_index = 0; op_index < operands_count; ++op_index) { |
| 137 stream << ", "; | 156 stream << ", "; |
| 138 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index); | 157 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index, |
| 158 parameter_count); |
| 139 } | 159 } |
| 140 } | 160 } |
| 141 | 161 |
| 142 void BytecodeExpectationsPrinter::PrintV8String(std::ostream& stream, | 162 void BytecodeExpectationsPrinter::PrintV8String(std::ostream& stream, |
| 143 i::String* string) const { | 163 i::String* string) const { |
| 144 stream << '"'; | 164 stream << '"'; |
| 145 for (int i = 0, length = string->length(); i < length; ++i) { | 165 for (int i = 0, length = string->length(); i < length; ++i) { |
| 146 stream << i::AsEscapedUC16ForJSON(string->Get(i)); | 166 stream << i::AsEscapedUC16ForJSON(string->Get(i)); |
| 147 } | 167 } |
| 148 stream << '"'; | 168 stream << '"'; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 if (frame_size > 0) stream << " # in multiples of sizeof(void*)"; | 212 if (frame_size > 0) stream << " # in multiples of sizeof(void*)"; |
| 193 stream << "\nparameter count: " << bytecode_array->parameter_count() << '\n'; | 213 stream << "\nparameter count: " << bytecode_array->parameter_count() << '\n'; |
| 194 } | 214 } |
| 195 | 215 |
| 196 void BytecodeExpectationsPrinter::PrintBytecodeSequence( | 216 void BytecodeExpectationsPrinter::PrintBytecodeSequence( |
| 197 std::ostream& stream, i::Handle<i::BytecodeArray> bytecode_array) const { | 217 std::ostream& stream, i::Handle<i::BytecodeArray> bytecode_array) const { |
| 198 stream << "bytecodes: [\n"; | 218 stream << "bytecodes: [\n"; |
| 199 BytecodeArrayIterator bytecode_iter(bytecode_array); | 219 BytecodeArrayIterator bytecode_iter(bytecode_array); |
| 200 for (; !bytecode_iter.done(); bytecode_iter.Advance()) { | 220 for (; !bytecode_iter.done(); bytecode_iter.Advance()) { |
| 201 stream << " "; | 221 stream << " "; |
| 202 PrintBytecode(stream, bytecode_iter); | 222 PrintBytecode(stream, bytecode_iter, bytecode_array->parameter_count()); |
| 203 stream << ",\n"; | 223 stream << ",\n"; |
| 204 } | 224 } |
| 205 stream << "]\n"; | 225 stream << "]\n"; |
| 206 } | 226 } |
| 207 | 227 |
| 208 void BytecodeExpectationsPrinter::PrintConstantPool( | 228 void BytecodeExpectationsPrinter::PrintConstantPool( |
| 209 std::ostream& stream, i::FixedArray* constant_pool) const { | 229 std::ostream& stream, i::FixedArray* constant_pool) const { |
| 210 stream << "constant pool: [\n"; | 230 stream << "constant pool: [\n"; |
| 211 int num_constants = constant_pool->length(); | 231 int num_constants = constant_pool->length(); |
| 212 if (num_constants > 0) { | 232 if (num_constants > 0) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 225 std::stringstream body_stream(body); | 245 std::stringstream body_stream(body); |
| 226 std::string body_line; | 246 std::string body_line; |
| 227 while (std::getline(body_stream, body_line)) { | 247 while (std::getline(body_stream, body_line)) { |
| 228 stream << " "; | 248 stream << " "; |
| 229 PrintEscapedString(stream, body_line); | 249 PrintEscapedString(stream, body_line); |
| 230 stream << '\n'; | 250 stream << '\n'; |
| 231 } | 251 } |
| 232 stream << "\"\n"; | 252 stream << "\"\n"; |
| 233 } | 253 } |
| 234 | 254 |
| 255 void BytecodeExpectationsPrinter::PrintHandlers( |
| 256 std::ostream& stream, i::Handle<i::BytecodeArray> bytecode_array) const { |
| 257 stream << "handlers: [\n"; |
| 258 HandlerTable* table = HandlerTable::cast(bytecode_array->handler_table()); |
| 259 for (int i = 0, num_entries = table->NumberOfRangeEntries(); i < num_entries; |
| 260 ++i) { |
| 261 stream << " [" << table->GetRangeStart(i) << ", " << table->GetRangeEnd(i) |
| 262 << ", " << table->GetRangeHandler(i) << "],\n"; |
| 263 } |
| 264 stream << "]\n"; |
| 265 } |
| 266 |
| 235 void BytecodeExpectationsPrinter::PrintBytecodeArray( | 267 void BytecodeExpectationsPrinter::PrintBytecodeArray( |
| 236 std::ostream& stream, const std::string& body, | 268 std::ostream& stream, i::Handle<i::BytecodeArray> bytecode_array) const { |
| 237 i::Handle<i::BytecodeArray> bytecode_array) const { | |
| 238 stream << "---\n"; | |
| 239 PrintCodeSnippet(stream, body); | |
| 240 PrintFrameSize(stream, bytecode_array); | 269 PrintFrameSize(stream, bytecode_array); |
| 241 PrintBytecodeSequence(stream, bytecode_array); | 270 PrintBytecodeSequence(stream, bytecode_array); |
| 242 PrintConstantPool(stream, bytecode_array->constant_pool()); | 271 PrintConstantPool(stream, bytecode_array->constant_pool()); |
| 243 | 272 PrintHandlers(stream, bytecode_array); |
| 244 // TODO(ssanfilippo) print handlers. | |
| 245 i::HandlerTable* handlers = | |
| 246 i::HandlerTable::cast(bytecode_array->handler_table()); | |
| 247 CHECK_EQ(handlers->NumberOfRangeEntries(), 0); | |
| 248 } | 273 } |
| 249 | 274 |
| 250 void BytecodeExpectationsPrinter::PrintExpectation( | 275 void BytecodeExpectationsPrinter::PrintExpectation( |
| 251 std::ostream& stream, const std::string& snippet) const { | 276 std::ostream& stream, const std::string& snippet) const { |
| 252 const char* wrapper_function_name = "__genbckexp_wrapper__"; | 277 std::string source_code = |
| 278 wrap_ ? WrapCodeInFunction(top_function_name_.c_str(), snippet) : snippet; |
| 253 | 279 |
| 254 std::string source_code = WrapCodeInFunction(wrapper_function_name, snippet); | 280 v8::Local<v8::Script> script = Compile(source_code.c_str()); |
| 255 CompileAndRun(source_code.c_str()); | 281 |
| 282 if (execute_) Run(script); |
| 256 | 283 |
| 257 i::Handle<i::BytecodeArray> bytecode_array = | 284 i::Handle<i::BytecodeArray> bytecode_array = |
| 258 GetBytecodeArrayForGlobal(wrapper_function_name); | 285 GetBytecodeArrayForGlobal(top_function_name_.c_str()); |
| 259 | 286 |
| 260 PrintBytecodeArray(stream, snippet, bytecode_array); | 287 stream << "---\n"; |
| 288 PrintCodeSnippet(stream, snippet); |
| 289 PrintBytecodeArray(stream, bytecode_array); |
| 261 stream << '\n'; | 290 stream << '\n'; |
| 262 } | 291 } |
| 263 | 292 |
| 264 } // namespace interpreter | 293 } // namespace interpreter |
| 265 } // namespace internal | 294 } // namespace internal |
| 266 } // namespace v8 | 295 } // namespace v8 |
| OLD | NEW |