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

Side by Side Diff: test/cctest/interpreter/generate-bytecode-expectations.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 help message. 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 <cstring>
5 #include <fstream> 6 #include <fstream>
6 #include <iostream> 7
8 #include "test/cctest/interpreter/bytecode-expectations.h"
7 9
8 #include "include/libplatform/libplatform.h" 10 #include "include/libplatform/libplatform.h"
9 #include "include/v8.h" 11 #include "include/v8.h"
10 12
11 #include "src/base/logging.h" 13 #include "src/base/logging.h"
12 #include "src/base/smart-pointers.h" 14 #include "src/base/smart-pointers.h"
13 #include "src/compiler.h" 15 #include "src/compiler.h"
14 16
rmcilroy 2016/02/12 15:29:05 rm extra newline
Stefano Sanfilippo 2016/02/12 18:29:40 Done.
15 #include "src/interpreter/bytecode-array-iterator.h"
16 #include "src/interpreter/bytecode-generator.h"
17 #include "src/interpreter/bytecodes.h"
18 #include "src/interpreter/interpreter.h" 17 #include "src/interpreter/interpreter.h"
19 18
20 using namespace i::interpreter; 19 using namespace v8::internal::interpreter;
21 20
22 namespace { 21 namespace {
23 22
24 const char* kIndent = " "; 23 struct CommandLineOptions {
rmcilroy 2016/02/12 15:29:05 Make this a class if it has non-neglagible functio
Stefano Sanfilippo 2016/02/12 18:29:40 Done.
24 bool print_help = false;
25 bool read_raw_snippet = false;
rmcilroy 2016/02/12 15:29:05 read_raw_js_snippet
Stefano Sanfilippo 2016/02/12 18:29:41 Done.
26 ConstantPoolType const_pool_type = kConstantPoolTypeMixed;
27 bool read_from_stdin = false;
rmcilroy 2016/02/12 15:29:05 nit - move up with other bools
Stefano Sanfilippo 2016/02/12 18:29:40 Done.
28 std::vector<std::string> filename_list;
25 29
26 enum ConstantPoolType { 30 bool Validate() const {
27 kConstantPoolTypeUnknown, 31 if (print_help) return true;
28 kConstantPoolTypeString, 32
29 kConstantPoolTypeInteger, 33 if (!read_from_stdin && filename_list.empty()) {
30 kConstantPoolTypeDouble, 34 std::cerr << "ERROR: No input file specified.\n";
31 kConstantPoolTypeMixed, 35 return false;
36 }
37
38 if (read_from_stdin && !filename_list.empty()) {
39 std::cerr << "ERROR: Reading from stdin, but input files supplied.\n";
40 return false;
41 }
42
43 return true;
44 }
32 }; 45 };
33 46
34 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { 47 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator {
35 public: 48 public:
36 void* Allocate(size_t length) override { 49 void* Allocate(size_t length) override {
37 void* data = AllocateUninitialized(length); 50 void* data = AllocateUninitialized(length);
38 if (data != nullptr) memset(data, 0, length); 51 if (data != nullptr) memset(data, 0, length);
39 return data; 52 return data;
40 } 53 }
41 void* AllocateUninitialized(size_t length) override { return malloc(length); } 54 void* AllocateUninitialized(size_t length) override { return malloc(length); }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 isolate_ = v8::Isolate::New(create_params); 92 isolate_ = v8::Isolate::New(create_params);
80 GetInternalIsolate(isolate_)->interpreter()->Initialize(); 93 GetInternalIsolate(isolate_)->interpreter()->Initialize();
81 } 94 }
82 95
83 V8InitializationScope::~V8InitializationScope() { 96 V8InitializationScope::~V8InitializationScope() {
84 isolate_->Dispose(); 97 isolate_->Dispose();
85 v8::V8::Dispose(); 98 v8::V8::Dispose();
86 v8::V8::ShutdownPlatform(); 99 v8::V8::ShutdownPlatform();
87 } 100 }
88 101
89 v8::Local<v8::String> V8StringFromUTF8(v8::Isolate* isolate, const char* data) { 102 bool ReadAll(std::string* body, std::istream& stream) {
rmcilroy 2016/02/12 15:29:05 ReadRawJsSnippet
Stefano Sanfilippo 2016/02/12 18:29:40 Done.
90 return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal) 103 std::stringstream body_buffer;
91 .ToLocalChecked(); 104 bool success = body_buffer << stream.rdbuf();
105 *body = body_buffer.str();
106 return success;
92 } 107 }
93 108
94 std::string WrapCodeInFunction(const char* function_name, 109 bool ReadNextSnippet(std::string* string, std::istream& stream) {
rmcilroy 2016/02/12 15:29:05 Just return the string instead of having it as a o
Stefano Sanfilippo 2016/02/12 18:29:40 The fact is that EOF might test true after that fu
rmcilroy 2016/02/15 11:15:19 Ok, this is fine as is
95 const std::string& function_body) { 110 std::string line;
96 std::ostringstream program_stream; 111 bool found_begin_snippet = false;
97 program_stream << "function " << function_name << "() {" << function_body 112 string->clear();
98 << "}\n" 113 while (std::getline(stream, line)) {
99 << function_name << "();"; 114 if (line == "snippet: \"") {
100 115 found_begin_snippet = true;
101 return program_stream.str(); 116 continue;
117 }
118 if (!found_begin_snippet) continue;
119 if (line == "\"") return true;
120 // Skip two leading spaces we added, if the line is not shorter
121 if (line.size() >= 2) {
122 string->append(line.begin() + 2, line.end());
123 } else {
124 *string += line;
125 }
126 *string += '\n';
127 }
128 return false;
102 } 129 }
103 130
104 v8::Local<v8::Value> CompileAndRun(v8::Isolate* isolate, 131 bool ExtractSnippets(std::vector<std::string>* snippet_list,
rmcilroy 2016/02/12 15:29:05 const std::vector<std::string>*
Stefano Sanfilippo 2016/02/12 18:29:40 We are modifying the vector in the function, const
105 const v8::Local<v8::Context>& context, 132 const CommandLineOptions& options) {
106 const char* program) { 133 std::string snippet;
107 v8::Local<v8::String> source = V8StringFromUTF8(isolate, program);
108 v8::Local<v8::Script> script =
109 v8::Script::Compile(context, source).ToLocalChecked();
110 134
111 v8::Local<v8::Value> result; 135 if (options.read_from_stdin) {
112 CHECK(script->Run(context).ToLocal(&result)); 136 if (!ReadAll(&snippet, std::cin)) {
137 std::cerr << "ERROR: Could not read from stanard input.\n";
138 return false;
139 }
140 snippet_list->push_back(snippet);
141 return true;
142 }
113 143
114 return result; 144 for (const std::string& body_filename : options.filename_list) {
145 std::ifstream body_file{body_filename};
146 if (!body_file.is_open()) {
147 std::cerr << "ERROR: Could not open '" << body_filename << "'.\n";
148 return false;
149 }
150 if (options.read_raw_snippet) {
151 if (!ReadAll(&snippet, body_file)) {
152 std::cerr << "ERROR: Could read from '" << body_filename << "'.\n";
153 return false;
154 }
155 snippet_list->push_back(snippet);
156 } else {
157 while (ReadNextSnippet(&snippet, body_file)) {
158 snippet_list->push_back(snippet);
159 }
160 }
161 }
162
163 return true;
115 } 164 }
116 165
117 i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForGlobal( 166 void GenerateExpectationsFile(std::ostream& stream,
118 v8::Isolate* isolate, const v8::Local<v8::Context>& context, 167 const std::vector<std::string>& snippet_list,
119 const char* global_name) { 168 ConstantPoolType const_pool_type,
120 v8::Local<v8::String> v8_global_name = V8StringFromUTF8(isolate, global_name); 169 const char* exec_path) {
121 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( 170 V8InitializationScope platform(exec_path);
122 context->Global()->Get(context, v8_global_name).ToLocalChecked()); 171 {
123 i::Handle<i::JSFunction> js_function = 172 v8::Isolate::Scope isolate_scope{platform.isolate()};
124 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function)); 173 v8::HandleScope handle_scope{platform.isolate()};
174 v8::Local<v8::Context> context = v8::Context::New(platform.isolate());
175 v8::Context::Scope context_scope{context};
125 176
126 i::Handle<i::BytecodeArray> bytecodes = i::handle( 177 stream << "#\n# Autogenerated by generate-bytecode-expectations\n#\n\n";
127 js_function->shared()->bytecode_array(), GetInternalIsolate(isolate));
128 178
129 return bytecodes; 179 ExpectationPrinter printer{platform.isolate(), const_pool_type};
rmcilroy 2016/02/12 15:29:05 replace "{" with "("
Stefano Sanfilippo 2016/02/12 18:29:40 Done.
130 } 180 for (const std::string& snippet : snippet_list) {
131 181 printer.PrintExpectation(stream, snippet);
132 std::string QuoteCString(const std::string& source) {
133 std::string quoted_buffer;
134 for (char c : source) {
135 switch (c) {
136 case '"':
137 quoted_buffer += "\\\"";
138 break;
139 case '\n':
140 quoted_buffer += "\\n";
141 break;
142 case '\t':
143 quoted_buffer += "\\t";
144 break;
145 case '\\':
146 quoted_buffer += "\\\\";
147 break;
148 default:
149 quoted_buffer += c;
150 break;
151 } 182 }
152 } 183 }
153 return quoted_buffer;
154 }
155
156 void PrintBytecodeOperand(std::ostream& stream,
157 const BytecodeArrayIterator& bytecode_iter,
158 const Bytecode& bytecode, int op_index) {
159 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index);
160 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index);
161
162 const char* size_tag;
163 switch (op_size) {
164 case OperandSize::kByte:
165 size_tag = "8";
166 break;
167 case OperandSize::kShort:
168 size_tag = "16";
169 break;
170 default:
171 UNREACHABLE();
172 return;
173 }
174
175 if (Bytecodes::IsRegisterOperandType(op_type)) {
176 Register register_value = bytecode_iter.GetRegisterOperand(op_index);
177 stream << 'R';
178 if (op_size != OperandSize::kByte) stream << size_tag;
179 stream << '(' << register_value.index() << ')';
180 } else {
181 stream << 'U' << size_tag << '(';
182
183 if (Bytecodes::IsImmediateOperandType(op_type)) {
184 // We need a cast, otherwise the result is printed as char.
185 stream << static_cast<int>(bytecode_iter.GetImmediateOperand(op_index));
186 } else if (Bytecodes::IsRegisterCountOperandType(op_type)) {
187 stream << bytecode_iter.GetRegisterCountOperand(op_index);
188 } else if (Bytecodes::IsIndexOperandType(op_type)) {
189 stream << bytecode_iter.GetIndexOperand(op_index);
190 } else {
191 UNREACHABLE();
192 }
193
194 stream << ')';
195 }
196 } 184 }
197 185
198 void PrintBytecode(std::ostream& stream,
199 const BytecodeArrayIterator& bytecode_iter) {
200 Bytecode bytecode = bytecode_iter.current_bytecode();
201
202 stream << "B(" << Bytecodes::ToString(bytecode) << ')';
203
204 int operands_count = Bytecodes::NumberOfOperands(bytecode);
205 for (int op_index = 0; op_index < operands_count; ++op_index) {
206 stream << ", ";
207 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index);
208 }
209 }
210
211 void PrintV8String(std::ostream& stream, i::String* string) {
212 stream << '"';
213 for (int i = 0, length = string->length(); i < length; ++i) {
214 stream << i::AsEscapedUC16ForJSON(string->Get(i));
215 }
216 stream << '"';
217 }
218
219 void PrintConstant(std::ostream& stream,
220 ConstantPoolType expected_constant_type,
221 i::Handle<i::Object> constant) {
222 switch (expected_constant_type) {
223 case kConstantPoolTypeString:
224 CHECK(constant->IsString());
225 PrintV8String(stream, i::String::cast(*constant));
226 break;
227 case kConstantPoolTypeInteger:
228 if (constant->IsSmi()) {
229 i::Smi::cast(*constant)->SmiPrint(stream);
230 } else if (constant->IsHeapNumber()) {
231 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream);
232 } else {
233 UNREACHABLE();
234 }
235 break;
236 case kConstantPoolTypeDouble:
237 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream);
238 break;
239 case kConstantPoolTypeMixed:
240 if (constant->IsSmi()) {
241 stream << "kInstanceTypeDontCare";
242 } else {
243 stream << "InstanceType::"
244 << i::HeapObject::cast(*constant)->map()->instance_type();
245 }
246 break;
247 default:
248 UNREACHABLE();
249 return;
250 }
251 }
252
253 void PrintFrameSize(std::ostream& stream,
254 i::Handle<i::BytecodeArray> bytecode_array) {
255 const int kPointerSize = sizeof(void*);
256 int frame_size = bytecode_array->frame_size();
257
258 stream << kIndent;
259
260 DCHECK(frame_size % kPointerSize == 0);
261 if (frame_size > kPointerSize) {
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 }
272
273 void PrintBytecodeSequence(std::ostream& stream,
274 i::Handle<i::BytecodeArray> bytecode_array) {
275 stream << kIndent << ' ' << bytecode_array->length() << ",\n"
276 << kIndent << " {\n"
277 << kIndent << " ";
278
279 BytecodeArrayIterator bytecode_iter{bytecode_array};
280 for (; !bytecode_iter.done(); bytecode_iter.Advance()) {
281 // Print separator before each instruction, except the first one.
282 if (bytecode_iter.current_offset() > 0) {
283 stream << ",\n" << kIndent << " ";
284 }
285 PrintBytecode(stream, bytecode_iter);
286 }
287 }
288
289 void PrintConstantPool(std::ostream& stream, i::FixedArray* constant_pool,
290 ConstantPoolType expected_constant_type,
291 v8::Isolate* isolate) {
292 int num_constants = constant_pool->length();
293 stream << "\n" << kIndent << " },\n" << kIndent << ' ' << num_constants;
294 if (num_constants > 0) {
295 stream << ",\n" << kIndent << " {";
296 for (int i = 0; i < num_constants; ++i) {
297 // Print separator before each constant, except the first one
298 if (i != 0) stream << ", ";
299 PrintConstant(
300 stream, expected_constant_type,
301 i::FixedArray::get(constant_pool, i, GetInternalIsolate(isolate)));
302 }
303 stream << '}';
304 }
305 stream << '\n';
306 }
307
308 void PrintBytecodeArray(std::ostream& stream,
309 i::Handle<i::BytecodeArray> bytecode_array,
310 const std::string& body, v8::Isolate* isolate,
311 ConstantPoolType constant_pool_type,
312 bool print_banner = true) {
313 if (print_banner) {
314 stream << kIndent << "// === ExpectedSnippet generated by "
315 "generate-bytecode-expectations. ===\n";
316 }
317
318 // Print the code snippet as a quoted C string.
319 stream << kIndent << "{" << '"' << QuoteCString(body) << "\",\n";
320
321 PrintFrameSize(stream, bytecode_array);
322 PrintBytecodeSequence(stream, bytecode_array);
323 PrintConstantPool(stream, bytecode_array->constant_pool(), constant_pool_type,
324 isolate);
325
326 // TODO(ssanfilippo) print handlers.
327 i::HandlerTable* handlers =
328 i::HandlerTable::cast(bytecode_array->handler_table());
329 CHECK_EQ(handlers->NumberOfRangeEntries(), 0);
330
331 stream << kIndent << "}\n";
332 }
333
334 void PrintExpectedSnippet(ConstantPoolType constant_pool_type, char* exec_path,
335 std::string body) {
336 const char* wrapper_function_name = "__genbckexp_wrapper__";
337
338 V8InitializationScope platform(exec_path);
339 {
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
345 std::string source_code = WrapCodeInFunction(wrapper_function_name, body);
346 CompileAndRun(platform.isolate(), context, source_code.c_str());
347
348 i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal(
349 platform.isolate(), context, wrapper_function_name);
350
351 PrintBytecodeArray(std::cout, bytecode_array, body, platform.isolate(),
352 constant_pool_type);
353 }
354 }
355
356 bool ReadFromFileOrStdin(std::string* body, const char* body_filename) {
357 std::stringstream body_buffer;
358 if (strcmp(body_filename, "-") == 0) {
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) { 186 ConstantPoolType ParseConstantPoolType(const char* type_string) {
370 if (strcmp(type_string, "int") == 0) { 187 if (strcmp(type_string, "int") == 0) {
371 return kConstantPoolTypeInteger; 188 return kConstantPoolTypeInteger;
372 } else if (strcmp(type_string, "double") == 0) { 189 } else if (strcmp(type_string, "double") == 0) {
373 return kConstantPoolTypeDouble; 190 return kConstantPoolTypeDouble;
374 } else if (strcmp(type_string, "string") == 0) { 191 } else if (strcmp(type_string, "string") == 0) {
375 return kConstantPoolTypeString; 192 return kConstantPoolTypeString;
376 } else if (strcmp(type_string, "mixed") == 0) { 193 } else if (strcmp(type_string, "mixed") == 0) {
377 return kConstantPoolTypeMixed; 194 return kConstantPoolTypeMixed;
378 } 195 }
379 return kConstantPoolTypeUnknown; 196 return kConstantPoolTypeUnknown;
380 } 197 }
381 198
199 CommandLineOptions ParseOptionsFromCommandLine(int argc, char** argv) {
rmcilroy 2016/02/12 15:29:05 This should probably be a constructor for CommandL
Stefano Sanfilippo 2016/02/12 18:29:41 Done.
200 CommandLineOptions options;
201
202 if (argc <= 1) return options;
203
204 bool expecting_const_pool_type = false;
205 for (int i = 1; i < argc; ++i) {
206 if (expecting_const_pool_type) {
207 options.const_pool_type = ParseConstantPoolType(argv[i]);
208 if (options.const_pool_type == kConstantPoolTypeUnknown) {
rmcilroy 2016/02/12 15:29:05 Move this logic into CommandLineArguments::Validat
Stefano Sanfilippo 2016/02/12 18:29:40 Done.
209 std::cerr << "ERROR: Unknown constant pool type " << argv[i] << ".\n";
210 return options;
211 }
212 expecting_const_pool_type = false;
213 } else if (strcmp(argv[i], "--help") == 0) {
214 options.print_help = true;
215 } else if (strcmp(argv[i], "--js") == 0) {
216 options.read_raw_snippet = true;
217 } else if (strcmp(argv[i], "--pool-type") == 0) {
218 expecting_const_pool_type = true;
219 } else if (strcmp(argv[i], "--stdin") == 0) {
220 options.read_from_stdin = true;
221 } else if (strncmp(argv[i], "--", 2) != 0) { // If it doesn't start with --
222 options.filename_list.push_back(argv[i]);
223 } else {
224 std::cerr << "ERROR: Unknonwn option " << argv[i] << "\n";
225 return options;
226 }
227 }
228
229 return options;
230 }
231
382 void PrintUsage(const char* exec_path) { 232 void PrintUsage(const char* exec_path) {
383 std::cerr << "Usage: " << exec_path 233 std::cerr
384 << " (int|double|string|mixed) [filename.js|-]\n\n" 234 << "\nUsage: " << exec_path
385 "First argument is the type of objects in the constant pool.\n\n" 235 << " [OPTIONS]... [INPUT FILES]...\n\n"
rmcilroy 2016/02/12 15:29:05 Let's make this tool only take a single file - the
Stefano Sanfilippo 2016/02/12 18:29:40 Done.
386 "Omitting the second argument or - reads from standard input.\n" 236 "Options:\n"
387 "Anything else is interpreted as a filename.\n\n" 237 " --help Print this help message.\n"
388 "This tool is intended as a help in writing tests.\n" 238 " --js Read raw JavaScript, instead of the output format.\n"
rmcilroy 2016/02/12 15:29:05 nit / --raw-js
Stefano Sanfilippo 2016/02/12 18:29:40 Done.
389 "Please, DO NOT blindly copy and paste the output " 239 " --stdin Read from standard input instead of file.\n"
390 "into the test suite.\n"; 240 " --pool-type (int|double|string|mixed)\n"
241 " specify the type of the entries in the constant pool "
242 "(default: mixed).\n"
243 "\n"
244 "Each raw JavaScript file is interpreted as a single snippet.\n\n"
245 "This tool is intended as a help in writing tests.\n"
246 "Please, DO NOT blindly copy and paste the output "
247 "into the test suite.\n";
391 } 248 }
392 249
393 } // namespace 250 } // namespace
394 251
395 int main(int argc, char** argv) { 252 int main(int argc, char** argv) {
396 if (argc < 2) { 253 CommandLineOptions options = ParseOptionsFromCommandLine(argc, argv);
254
255 if (!options.Validate() || options.print_help) {
397 PrintUsage(argv[0]); 256 PrintUsage(argv[0]);
398 return 1; 257 return options.print_help ? 0 : 1;
399 } 258 }
400 259
401 if (argc > 1 && strcmp(argv[1], "--help") == 0) { 260 std::vector<std::string> snippet_list;
402 PrintUsage(argv[0]); 261 if (!ExtractSnippets(&snippet_list, options)) {
403 return 0; 262 return 2;
404 } 263 }
405 264
406 const char* body_filename = (argc > 2 ? argv[2] : "-"); 265 GenerateExpectationsFile(std::cout, snippet_list, options.const_pool_type,
407 const char* const_pool_type_string = argv[1]; 266 argv[0]);
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 } 267 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698