Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: test/cctest/interpreter/bytecode-expectations-printer.cc

Issue 1688383003: [Interpreter] Change the output format of generate-bytecode-expectations. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix MSVC on Windows compiler error. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <fstream> 5 #include "test/cctest/interpreter/bytecode-expectations-printer.h"
6
6 #include <iostream> 7 #include <iostream>
7 8
8 #include "include/libplatform/libplatform.h" 9 #include "include/libplatform/libplatform.h"
9 #include "include/v8.h" 10 #include "include/v8.h"
10 11
11 #include "src/base/logging.h" 12 #include "src/base/logging.h"
12 #include "src/base/smart-pointers.h" 13 #include "src/base/smart-pointers.h"
13 #include "src/compiler.h" 14 #include "src/compiler.h"
14 15
15 #include "src/interpreter/bytecode-array-iterator.h" 16 #include "src/interpreter/bytecode-array-iterator.h"
16 #include "src/interpreter/bytecode-generator.h" 17 #include "src/interpreter/bytecode-generator.h"
17 #include "src/interpreter/bytecodes.h" 18 #include "src/interpreter/bytecodes.h"
18 #include "src/interpreter/interpreter.h" 19 #include "src/interpreter/interpreter.h"
19 20
20 using namespace i::interpreter; 21 namespace v8 {
22 namespace internal {
23 namespace interpreter {
21 24
22 namespace { 25 v8::Local<v8::String> BytecodeExpectationsPrinter::V8StringFromUTF8(
23 26 const char* data) const {
24 const char* kIndent = " "; 27 return v8::String::NewFromUtf8(isolate_, data, v8::NewStringType::kNormal)
25
26 enum ConstantPoolType {
27 kConstantPoolTypeUnknown,
28 kConstantPoolTypeString,
29 kConstantPoolTypeInteger,
30 kConstantPoolTypeDouble,
31 kConstantPoolTypeMixed,
32 };
33
34 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator {
35 public:
36 void* Allocate(size_t length) override {
37 void* data = AllocateUninitialized(length);
38 if (data != nullptr) memset(data, 0, length);
39 return data;
40 }
41 void* AllocateUninitialized(size_t length) override { return malloc(length); }
42 void Free(void* data, size_t) override { free(data); }
43 };
44
45 class V8InitializationScope final {
46 public:
47 explicit V8InitializationScope(const char* exec_path);
48 ~V8InitializationScope();
49
50 v8::Platform* platform() const { return platform_.get(); }
51 v8::Isolate* isolate() const { return isolate_; }
52
53 private:
54 v8::base::SmartPointer<v8::Platform> platform_;
55 v8::Isolate* isolate_;
56
57 DISALLOW_COPY_AND_ASSIGN(V8InitializationScope);
58 };
59
60 i::Isolate* GetInternalIsolate(v8::Isolate* isolate) {
61 return reinterpret_cast<i::Isolate*>(isolate);
62 }
63
64 V8InitializationScope::V8InitializationScope(const char* exec_path)
65 : platform_(v8::platform::CreateDefaultPlatform()) {
66 i::FLAG_ignition = true;
67 i::FLAG_always_opt = false;
68 i::FLAG_allow_natives_syntax = true;
69
70 v8::V8::InitializeICU();
71 v8::V8::InitializeExternalStartupData(exec_path);
72 v8::V8::InitializePlatform(platform_.get());
73 v8::V8::Initialize();
74
75 ArrayBufferAllocator allocator;
76 v8::Isolate::CreateParams create_params;
77 create_params.array_buffer_allocator = &allocator;
78
79 isolate_ = v8::Isolate::New(create_params);
80 GetInternalIsolate(isolate_)->interpreter()->Initialize();
81 }
82
83 V8InitializationScope::~V8InitializationScope() {
84 isolate_->Dispose();
85 v8::V8::Dispose();
86 v8::V8::ShutdownPlatform();
87 }
88
89 v8::Local<v8::String> V8StringFromUTF8(v8::Isolate* isolate, const char* data) {
90 return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal)
91 .ToLocalChecked(); 28 .ToLocalChecked();
92 } 29 }
93 30
94 std::string WrapCodeInFunction(const char* function_name, 31 std::string BytecodeExpectationsPrinter::WrapCodeInFunction(
95 const std::string& function_body) { 32 const char* function_name, const std::string& function_body) const {
96 std::ostringstream program_stream; 33 std::ostringstream program_stream;
97 program_stream << "function " << function_name << "() {" << function_body 34 program_stream << "function " << function_name << "() {" << function_body
98 << "}\n" 35 << "}\n"
99 << function_name << "();"; 36 << function_name << "();";
100 37
101 return program_stream.str(); 38 return program_stream.str();
102 } 39 }
103 40
104 v8::Local<v8::Value> CompileAndRun(v8::Isolate* isolate, 41 v8::Local<v8::Value> BytecodeExpectationsPrinter::CompileAndRun(
105 const v8::Local<v8::Context>& context, 42 const char* program) const {
106 const char* program) { 43 v8::Local<v8::String> source = V8StringFromUTF8(program);
107 v8::Local<v8::String> source = V8StringFromUTF8(isolate, program);
108 v8::Local<v8::Script> script = 44 v8::Local<v8::Script> script =
109 v8::Script::Compile(context, source).ToLocalChecked(); 45 v8::Script::Compile(isolate_->GetCurrentContext(), source)
46 .ToLocalChecked();
110 47
111 v8::Local<v8::Value> result; 48 v8::Local<v8::Value> result;
112 CHECK(script->Run(context).ToLocal(&result)); 49 CHECK(script->Run(isolate_->GetCurrentContext()).ToLocal(&result));
113 50
114 return result; 51 return result;
115 } 52 }
116 53
117 i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForGlobal( 54 i::Handle<v8::internal::BytecodeArray>
118 v8::Isolate* isolate, const v8::Local<v8::Context>& context, 55 BytecodeExpectationsPrinter::GetBytecodeArrayForGlobal(
119 const char* global_name) { 56 const char* global_name) const {
120 v8::Local<v8::String> v8_global_name = V8StringFromUTF8(isolate, global_name); 57 const v8::Local<v8::Context>& context = isolate_->GetCurrentContext();
58 v8::Local<v8::String> v8_global_name = V8StringFromUTF8(global_name);
121 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( 59 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(
122 context->Global()->Get(context, v8_global_name).ToLocalChecked()); 60 context->Global()->Get(context, v8_global_name).ToLocalChecked());
123 i::Handle<i::JSFunction> js_function = 61 i::Handle<i::JSFunction> js_function =
124 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function)); 62 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function));
125 63
126 i::Handle<i::BytecodeArray> bytecodes = i::handle( 64 i::Handle<i::BytecodeArray> bytecodes =
127 js_function->shared()->bytecode_array(), GetInternalIsolate(isolate)); 65 i::handle(js_function->shared()->bytecode_array(), i_isolate());
128 66
129 return bytecodes; 67 return bytecodes;
130 } 68 }
131 69
132 std::string QuoteCString(const std::string& source) { 70 void BytecodeExpectationsPrinter::PrintEscapedString(
133 std::string quoted_buffer; 71 std::ostream& stream, const std::string& string) const {
134 for (char c : source) { 72 for (char c : string) {
135 switch (c) { 73 switch (c) {
136 case '"': 74 case '"':
137 quoted_buffer += "\\\""; 75 stream << "\\\"";
138 break;
139 case '\n':
140 quoted_buffer += "\\n";
141 break;
142 case '\t':
143 quoted_buffer += "\\t";
144 break; 76 break;
145 case '\\': 77 case '\\':
146 quoted_buffer += "\\\\"; 78 stream << "\\\\";
147 break; 79 break;
148 default: 80 default:
149 quoted_buffer += c; 81 stream << c;
150 break; 82 break;
151 } 83 }
152 } 84 }
153 return quoted_buffer;
154 } 85 }
155 86
156 void PrintBytecodeOperand(std::ostream& stream, 87 void BytecodeExpectationsPrinter::PrintBytecodeOperand(
157 const BytecodeArrayIterator& bytecode_iter, 88 std::ostream& stream, const BytecodeArrayIterator& bytecode_iter,
158 const Bytecode& bytecode, int op_index) { 89 const Bytecode& bytecode, int op_index) const {
159 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index); 90 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index);
160 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index); 91 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index);
161 92
162 const char* size_tag; 93 const char* size_tag;
163 switch (op_size) { 94 switch (op_size) {
164 case OperandSize::kByte: 95 case OperandSize::kByte:
165 size_tag = "8"; 96 size_tag = "8";
166 break; 97 break;
167 case OperandSize::kShort: 98 case OperandSize::kShort:
168 size_tag = "16"; 99 size_tag = "16";
(...skipping 19 matching lines...) Expand all
188 } else if (Bytecodes::IsIndexOperandType(op_type)) { 119 } else if (Bytecodes::IsIndexOperandType(op_type)) {
189 stream << bytecode_iter.GetIndexOperand(op_index); 120 stream << bytecode_iter.GetIndexOperand(op_index);
190 } else { 121 } else {
191 UNREACHABLE(); 122 UNREACHABLE();
192 } 123 }
193 124
194 stream << ')'; 125 stream << ')';
195 } 126 }
196 } 127 }
197 128
198 void PrintBytecode(std::ostream& stream, 129 void BytecodeExpectationsPrinter::PrintBytecode(
199 const BytecodeArrayIterator& bytecode_iter) { 130 std::ostream& stream, const BytecodeArrayIterator& bytecode_iter) const {
200 Bytecode bytecode = bytecode_iter.current_bytecode(); 131 Bytecode bytecode = bytecode_iter.current_bytecode();
201 132
202 stream << "B(" << Bytecodes::ToString(bytecode) << ')'; 133 stream << "B(" << Bytecodes::ToString(bytecode) << ')';
203 134
204 int operands_count = Bytecodes::NumberOfOperands(bytecode); 135 int operands_count = Bytecodes::NumberOfOperands(bytecode);
205 for (int op_index = 0; op_index < operands_count; ++op_index) { 136 for (int op_index = 0; op_index < operands_count; ++op_index) {
206 stream << ", "; 137 stream << ", ";
207 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index); 138 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index);
208 } 139 }
209 } 140 }
210 141
211 void PrintV8String(std::ostream& stream, i::String* string) { 142 void BytecodeExpectationsPrinter::PrintV8String(std::ostream& stream,
143 i::String* string) const {
212 stream << '"'; 144 stream << '"';
213 for (int i = 0, length = string->length(); i < length; ++i) { 145 for (int i = 0, length = string->length(); i < length; ++i) {
214 stream << i::AsEscapedUC16ForJSON(string->Get(i)); 146 stream << i::AsEscapedUC16ForJSON(string->Get(i));
215 } 147 }
216 stream << '"'; 148 stream << '"';
217 } 149 }
218 150
219 void PrintConstant(std::ostream& stream, 151 void BytecodeExpectationsPrinter::PrintConstant(
220 ConstantPoolType expected_constant_type, 152 std::ostream& stream, i::Handle<i::Object> constant) const {
221 i::Handle<i::Object> constant) { 153 switch (const_pool_type_) {
222 switch (expected_constant_type) { 154 case ConstantPoolType::kString:
223 case kConstantPoolTypeString:
224 CHECK(constant->IsString()); 155 CHECK(constant->IsString());
225 PrintV8String(stream, i::String::cast(*constant)); 156 PrintV8String(stream, i::String::cast(*constant));
226 break; 157 break;
227 case kConstantPoolTypeInteger: 158 case ConstantPoolType::kInteger:
228 if (constant->IsSmi()) { 159 if (constant->IsSmi()) {
229 i::Smi::cast(*constant)->SmiPrint(stream); 160 i::Smi::cast(*constant)->SmiPrint(stream);
230 } else if (constant->IsHeapNumber()) { 161 } else if (constant->IsHeapNumber()) {
231 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream); 162 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream);
232 } else { 163 } else {
233 UNREACHABLE(); 164 UNREACHABLE();
234 } 165 }
235 break; 166 break;
236 case kConstantPoolTypeDouble: 167 case ConstantPoolType::kDouble:
237 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream); 168 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream);
238 break; 169 break;
239 case kConstantPoolTypeMixed: 170 case ConstantPoolType::kMixed:
240 if (constant->IsSmi()) { 171 if (constant->IsSmi()) {
241 stream << "kInstanceTypeDontCare"; 172 stream << "kInstanceTypeDontCare";
242 } else { 173 } else {
243 stream << "InstanceType::" 174 stream << "InstanceType::"
244 << i::HeapObject::cast(*constant)->map()->instance_type(); 175 << i::HeapObject::cast(*constant)->map()->instance_type();
245 } 176 }
246 break; 177 break;
178 case ConstantPoolType::kUnknown:
247 default: 179 default:
248 UNREACHABLE(); 180 UNREACHABLE();
249 return; 181 return;
250 } 182 }
251 } 183 }
252 184
253 void PrintFrameSize(std::ostream& stream, 185 void BytecodeExpectationsPrinter::PrintFrameSize(
254 i::Handle<i::BytecodeArray> bytecode_array) { 186 std::ostream& stream, i::Handle<i::BytecodeArray> bytecode_array) const {
255 const int kPointerSize = sizeof(void*); 187 const int kPointerSize = sizeof(void*);
256 int frame_size = bytecode_array->frame_size(); 188 int frame_size = bytecode_array->frame_size();
257 189
258 stream << kIndent; 190 DCHECK_EQ(frame_size % kPointerSize, 0);
259 191 stream << "frame size: " << frame_size / kPointerSize;
260 DCHECK(frame_size % kPointerSize == 0); 192 if (frame_size > 0) stream << " # in multiples of sizeof(void*)";
261 if (frame_size > kPointerSize) { 193 stream << "\nparameter count: " << bytecode_array->parameter_count() << '\n';
262 stream << ' ' << frame_size / kPointerSize << " * kPointerSize,\n"
263 << kIndent;
264 } else if (frame_size == kPointerSize) {
265 stream << " kPointerSize,\n" << kIndent;
266 } else if (frame_size == 0) {
267 stream << " 0,\n" << kIndent;
268 }
269
270 stream << ' ' << bytecode_array->parameter_count() << ",\n";
271 } 194 }
272 195
273 void PrintBytecodeSequence(std::ostream& stream, 196 void BytecodeExpectationsPrinter::PrintBytecodeSequence(
274 i::Handle<i::BytecodeArray> bytecode_array) { 197 std::ostream& stream, i::Handle<i::BytecodeArray> bytecode_array) const {
275 stream << kIndent << ' ' << bytecode_array->length() << ",\n" 198 stream << "bytecodes: [\n";
276 << kIndent << " {\n" 199 BytecodeArrayIterator bytecode_iter(bytecode_array);
277 << kIndent << " ";
278
279 BytecodeArrayIterator bytecode_iter{bytecode_array};
280 for (; !bytecode_iter.done(); bytecode_iter.Advance()) { 200 for (; !bytecode_iter.done(); bytecode_iter.Advance()) {
281 // Print separator before each instruction, except the first one. 201 stream << " ";
282 if (bytecode_iter.current_offset() > 0) {
283 stream << ",\n" << kIndent << " ";
284 }
285 PrintBytecode(stream, bytecode_iter); 202 PrintBytecode(stream, bytecode_iter);
203 stream << ",\n";
286 } 204 }
205 stream << "]\n";
287 } 206 }
288 207
289 void PrintConstantPool(std::ostream& stream, i::FixedArray* constant_pool, 208 void BytecodeExpectationsPrinter::PrintConstantPool(
290 ConstantPoolType expected_constant_type, 209 std::ostream& stream, i::FixedArray* constant_pool) const {
291 v8::Isolate* isolate) { 210 stream << "constant pool: [\n";
292 int num_constants = constant_pool->length(); 211 int num_constants = constant_pool->length();
293 stream << "\n" << kIndent << " },\n" << kIndent << ' ' << num_constants;
294 if (num_constants > 0) { 212 if (num_constants > 0) {
295 stream << ",\n" << kIndent << " {";
296 for (int i = 0; i < num_constants; ++i) { 213 for (int i = 0; i < num_constants; ++i) {
297 // Print separator before each constant, except the first one 214 stream << " ";
298 if (i != 0) stream << ", "; 215 PrintConstant(stream, i::FixedArray::get(constant_pool, i, i_isolate()));
299 PrintConstant( 216 stream << ",\n";
300 stream, expected_constant_type,
301 i::FixedArray::get(constant_pool, i, GetInternalIsolate(isolate)));
302 } 217 }
303 stream << '}';
304 } 218 }
305 stream << '\n'; 219 stream << "]\n";
306 } 220 }
307 221
308 void PrintBytecodeArray(std::ostream& stream, 222 void BytecodeExpectationsPrinter::PrintCodeSnippet(
309 i::Handle<i::BytecodeArray> bytecode_array, 223 std::ostream& stream, const std::string& body) const {
310 const std::string& body, v8::Isolate* isolate, 224 stream << "snippet: \"\n";
311 ConstantPoolType constant_pool_type, 225 std::stringstream body_stream(body);
312 bool print_banner = true) { 226 std::string body_line;
313 if (print_banner) { 227 while (std::getline(body_stream, body_line)) {
314 stream << kIndent << "// === ExpectedSnippet generated by " 228 stream << " ";
315 "generate-bytecode-expectations. ===\n"; 229 PrintEscapedString(stream, body_line);
230 stream << '\n';
316 } 231 }
232 stream << "\"\n";
233 }
317 234
318 // Print the code snippet as a quoted C string. 235 void BytecodeExpectationsPrinter::PrintBytecodeArray(
319 stream << kIndent << "{" << '"' << QuoteCString(body) << "\",\n"; 236 std::ostream& stream, const std::string& body,
320 237 i::Handle<i::BytecodeArray> bytecode_array) const {
238 stream << "---\n";
239 PrintCodeSnippet(stream, body);
321 PrintFrameSize(stream, bytecode_array); 240 PrintFrameSize(stream, bytecode_array);
322 PrintBytecodeSequence(stream, bytecode_array); 241 PrintBytecodeSequence(stream, bytecode_array);
323 PrintConstantPool(stream, bytecode_array->constant_pool(), constant_pool_type, 242 PrintConstantPool(stream, bytecode_array->constant_pool());
324 isolate);
325 243
326 // TODO(ssanfilippo) print handlers. 244 // TODO(ssanfilippo) print handlers.
327 i::HandlerTable* handlers = 245 i::HandlerTable* handlers =
328 i::HandlerTable::cast(bytecode_array->handler_table()); 246 i::HandlerTable::cast(bytecode_array->handler_table());
329 CHECK_EQ(handlers->NumberOfRangeEntries(), 0); 247 CHECK_EQ(handlers->NumberOfRangeEntries(), 0);
330
331 stream << kIndent << "}\n";
332 } 248 }
333 249
334 void PrintExpectedSnippet(ConstantPoolType constant_pool_type, char* exec_path, 250 void BytecodeExpectationsPrinter::PrintExpectation(
335 std::string body) { 251 std::ostream& stream, const std::string& snippet) const {
336 const char* wrapper_function_name = "__genbckexp_wrapper__"; 252 const char* wrapper_function_name = "__genbckexp_wrapper__";
337 253
338 V8InitializationScope platform(exec_path); 254 std::string source_code = WrapCodeInFunction(wrapper_function_name, snippet);
339 { 255 CompileAndRun(source_code.c_str());
340 v8::Isolate::Scope isolate_scope(platform.isolate());
341 v8::HandleScope handle_scope(platform.isolate());
342 v8::Local<v8::Context> context = v8::Context::New(platform.isolate());
343 v8::Context::Scope context_scope(context);
344 256
345 std::string source_code = WrapCodeInFunction(wrapper_function_name, body); 257 i::Handle<i::BytecodeArray> bytecode_array =
346 CompileAndRun(platform.isolate(), context, source_code.c_str()); 258 GetBytecodeArrayForGlobal(wrapper_function_name);
347 259
348 i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal( 260 PrintBytecodeArray(stream, snippet, bytecode_array);
349 platform.isolate(), context, wrapper_function_name); 261 stream << '\n';
350
351 PrintBytecodeArray(std::cout, bytecode_array, body, platform.isolate(),
352 constant_pool_type);
353 }
354 } 262 }
355 263
356 bool ReadFromFileOrStdin(std::string* body, const char* body_filename) { 264 } // namespace interpreter
357 std::stringstream body_buffer; 265 } // namespace internal
358 if (strcmp(body_filename, "-") == 0) { 266 } // namespace v8
359 body_buffer << std::cin.rdbuf();
360 } else {
361 std::ifstream body_file{body_filename};
362 if (!body_file) return false;
363 body_buffer << body_file.rdbuf();
364 }
365 *body = body_buffer.str();
366 return true;
367 }
368
369 ConstantPoolType ParseConstantPoolType(const char* type_string) {
370 if (strcmp(type_string, "int") == 0) {
371 return kConstantPoolTypeInteger;
372 } else if (strcmp(type_string, "double") == 0) {
373 return kConstantPoolTypeDouble;
374 } else if (strcmp(type_string, "string") == 0) {
375 return kConstantPoolTypeString;
376 } else if (strcmp(type_string, "mixed") == 0) {
377 return kConstantPoolTypeMixed;
378 }
379 return kConstantPoolTypeUnknown;
380 }
381
382 void PrintUsage(const char* exec_path) {
383 std::cerr << "Usage: " << exec_path
384 << " (int|double|string|mixed) [filename.js|-]\n\n"
385 "First argument is the type of objects in the constant pool.\n\n"
386 "Omitting the second argument or - reads from standard input.\n"
387 "Anything else is interpreted as a filename.\n\n"
388 "This tool is intended as a help in writing tests.\n"
389 "Please, DO NOT blindly copy and paste the output "
390 "into the test suite.\n";
391 }
392
393 } // namespace
394
395 int main(int argc, char** argv) {
396 if (argc < 2) {
397 PrintUsage(argv[0]);
398 return 1;
399 }
400
401 if (argc > 1 && strcmp(argv[1], "--help") == 0) {
402 PrintUsage(argv[0]);
403 return 0;
404 }
405
406 const char* body_filename = (argc > 2 ? argv[2] : "-");
407 const char* const_pool_type_string = argv[1];
408
409 std::string body;
410 if (!ReadFromFileOrStdin(&body, body_filename)) {
411 std::cerr << "Could not open '" << body_filename << "'.\n\n";
412 PrintUsage(argv[0]);
413 return 1;
414 }
415
416 PrintExpectedSnippet(ParseConstantPoolType(const_pool_type_string), argv[0],
417 body);
418 }
OLDNEW
« no previous file with comments | « test/cctest/interpreter/bytecode-expectations-printer.h ('k') | test/cctest/interpreter/generate-bytecode-expectations.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698