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

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: Do not create a static lib target. 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 #include <sstream>
7 8
8 #include "include/libplatform/libplatform.h" 9 #include "test/cctest/interpreter/bytecode-expectations.h"
9 #include "include/v8.h"
10 10
11 #include "src/base/logging.h" 11 using namespace v8::internal::interpreter;
12 #include "src/base/smart-pointers.h"
13 #include "src/compiler.h"
14
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"
19
20 using namespace i::interpreter;
21 12
22 namespace { 13 namespace {
23 14
24 const char* kIndent = " "; 15 struct CommandLineOptions {
16 bool print_help = false;
17 bool read_raw_snippet = false;
18 ConstantPoolType const_pool_type = kConstantPoolTypeMixed;
19 bool read_from_stdin = false;
20 std::vector<std::string> filename_list;
25 21
26 enum ConstantPoolType { 22 bool Validate() const {
27 kConstantPoolTypeUnknown, 23 if (print_help) return true;
28 kConstantPoolTypeString, 24
29 kConstantPoolTypeInteger, 25 if (!read_from_stdin && filename_list.empty()) {
30 kConstantPoolTypeDouble, 26 std::cerr << "ERROR: No input file specified.\n";
31 kConstantPoolTypeMixed, 27 return false;
28 }
29
30 if (read_from_stdin && !filename_list.empty()) {
31 std::cerr << "ERROR: Reading from stdin, but input files supplied.\n";
32 return false;
33 }
34
35 return true;
36 }
32 }; 37 };
33 38
34 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { 39 bool ReadAll(std::string* body, std::istream& stream) {
35 public: 40 std::stringstream body_buffer;
36 void* Allocate(size_t length) override { 41 bool success = body_buffer << stream.rdbuf();
37 void* data = AllocateUninitialized(length); 42 *body = body_buffer.str();
38 if (data != nullptr) memset(data, 0, length); 43 return success;
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 } 44 }
63 45
64 V8InitializationScope::V8InitializationScope(const char* exec_path) 46 bool ReadNextSnippet(std::string* string, std::istream& stream) {
oth 2016/02/12 14:40:14 Optional nit: std::ostringstream might be a better
Stefano Sanfilippo 2016/02/12 14:49:52 The snippets then get pushed into a vector as stri
65 : platform_(v8::platform::CreateDefaultPlatform()) { 47 std::string line;
66 i::FLAG_ignition = true; 48 bool found_begin_snippet = false;
67 i::FLAG_always_opt = false; 49 string->clear();
68 i::FLAG_allow_natives_syntax = true; 50 while (std::getline(stream, line)) {
69 51 if (line == "snippet: \"") {
70 v8::V8::InitializeICU(); 52 found_begin_snippet = true;
71 v8::V8::InitializeExternalStartupData(exec_path); 53 continue;
72 v8::V8::InitializePlatform(platform_.get()); 54 }
73 v8::V8::Initialize(); 55 if (!found_begin_snippet) continue;
74 56 if (line == "\"") return true;
75 ArrayBufferAllocator allocator; 57 // Skip two leading spaces we added, if the line is not shorter
oth 2016/02/12 14:40:14 Consider just skipping whitespace here (string::fi
Stefano Sanfilippo 2016/02/12 14:49:52 The issue is that we want the output to exactly ma
rmcilroy 2016/02/12 15:59:05 I don't think keeping the output matching the inpu
76 v8::Isolate::CreateParams create_params; 58 if (line.size() >= 2) {
77 create_params.array_buffer_allocator = &allocator; 59 string->append(line.begin() + 2, line.end());
78 60 } else {
79 isolate_ = v8::Isolate::New(create_params); 61 *string += line;
80 GetInternalIsolate(isolate_)->interpreter()->Initialize(); 62 }
63 *string += '\n';
64 }
65 return false;
81 } 66 }
82 67
83 V8InitializationScope::~V8InitializationScope() { 68 bool ExtractSnippets(std::vector<TestInput>* snippet_list,
84 isolate_->Dispose(); 69 const CommandLineOptions& options) {
85 v8::V8::Dispose(); 70 std::string snippet;
86 v8::V8::ShutdownPlatform();
87 }
88 71
89 v8::Local<v8::String> V8StringFromUTF8(v8::Isolate* isolate, const char* data) { 72 if (options.read_from_stdin) {
90 return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal) 73 if (!ReadAll(&snippet, std::cin)) {
91 .ToLocalChecked(); 74 std::cerr << "ERROR: Could read from stanard input.\n";
92 } 75 return false;
76 }
77 snippet_list->push_back({snippet, options.const_pool_type});
78 return true;
79 }
93 80
94 std::string WrapCodeInFunction(const char* function_name, 81 for (const std::string& body_filename : options.filename_list) {
95 const std::string& function_body) { 82 std::ifstream body_file{body_filename};
96 std::ostringstream program_stream; 83 if (!body_file.is_open()) {
97 program_stream << "function " << function_name << "() {" << function_body 84 std::cerr << "ERROR: Could not open '" << body_filename << "'.\n";
98 << "}\n" 85 return false;
99 << function_name << "();"; 86 }
100 87 if (options.read_raw_snippet) {
101 return program_stream.str(); 88 if (!ReadAll(&snippet, body_file)) {
102 } 89 std::cerr << "ERROR: Could read from '" << body_filename << "'.\n";
103 90 return false;
104 v8::Local<v8::Value> CompileAndRun(v8::Isolate* isolate, 91 }
105 const v8::Local<v8::Context>& context, 92 snippet_list->push_back({snippet, options.const_pool_type});
106 const char* program) { 93 } else {
107 v8::Local<v8::String> source = V8StringFromUTF8(isolate, program); 94 while (ReadNextSnippet(&snippet, body_file)) {
108 v8::Local<v8::Script> script = 95 snippet_list->push_back({snippet, options.const_pool_type});
109 v8::Script::Compile(context, source).ToLocalChecked(); 96 }
110
111 v8::Local<v8::Value> result;
112 CHECK(script->Run(context).ToLocal(&result));
113
114 return result;
115 }
116
117 i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForGlobal(
118 v8::Isolate* isolate, const v8::Local<v8::Context>& context,
119 const char* global_name) {
120 v8::Local<v8::String> v8_global_name = V8StringFromUTF8(isolate, global_name);
121 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(
122 context->Global()->Get(context, v8_global_name).ToLocalChecked());
123 i::Handle<i::JSFunction> js_function =
124 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function));
125
126 i::Handle<i::BytecodeArray> bytecodes = i::handle(
127 js_function->shared()->bytecode_array(), GetInternalIsolate(isolate));
128
129 return bytecodes;
130 }
131
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 } 97 }
152 } 98 }
153 return quoted_buffer;
154 }
155 99
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 }
197
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; 100 return true;
367 } 101 }
368 102
369 ConstantPoolType ParseConstantPoolType(const char* type_string) { 103 ConstantPoolType ParseConstantPoolType(const char* type_string) {
370 if (strcmp(type_string, "int") == 0) { 104 if (strcmp(type_string, "int") == 0) {
371 return kConstantPoolTypeInteger; 105 return kConstantPoolTypeInteger;
372 } else if (strcmp(type_string, "double") == 0) { 106 } else if (strcmp(type_string, "double") == 0) {
373 return kConstantPoolTypeDouble; 107 return kConstantPoolTypeDouble;
374 } else if (strcmp(type_string, "string") == 0) { 108 } else if (strcmp(type_string, "string") == 0) {
375 return kConstantPoolTypeString; 109 return kConstantPoolTypeString;
376 } else if (strcmp(type_string, "mixed") == 0) { 110 } else if (strcmp(type_string, "mixed") == 0) {
377 return kConstantPoolTypeMixed; 111 return kConstantPoolTypeMixed;
378 } 112 }
379 return kConstantPoolTypeUnknown; 113 return kConstantPoolTypeUnknown;
380 } 114 }
381 115
116 CommandLineOptions ParseOptionsFromCommandLine(int argc, char** argv) {
117 CommandLineOptions options;
118
119 if (argc <= 1) return options;
120
121 bool expecting_const_pool_type = false;
122 for (int i = 1; i < argc; ++i) {
123 if (expecting_const_pool_type) {
124 options.const_pool_type = ParseConstantPoolType(argv[i]);
125 if (options.const_pool_type == kConstantPoolTypeUnknown) {
126 std::cerr << "ERROR: Unknown constant pool type " << argv[i] << ".\n";
127 return options;
128 }
129 expecting_const_pool_type = false;
130 } else if (strcmp(argv[i], "--help") == 0) {
131 options.print_help = true;
132 } else if (strcmp(argv[i], "--js") == 0) {
133 options.read_raw_snippet = true;
134 } else if (strcmp(argv[i], "--pool-type") == 0) {
135 expecting_const_pool_type = true;
136 } else if (strcmp(argv[i], "--stdin") == 0) {
137 options.read_from_stdin = true;
138 } else if (strncmp(argv[i], "--", 2) != 0) { // If it doesn't start with --
139 options.filename_list.push_back(argv[i]);
140 } else {
141 std::cerr << "ERROR: Unknonwn option " << argv[i] << "\n";
142 return options;
143 }
144 }
145
146 return options;
147 }
148
382 void PrintUsage(const char* exec_path) { 149 void PrintUsage(const char* exec_path) {
383 std::cerr << "Usage: " << exec_path 150 std::cerr
384 << " (int|double|string|mixed) [filename.js|-]\n\n" 151 << "\nUsage: " << exec_path
385 "First argument is the type of objects in the constant pool.\n\n" 152 << " [OPTIONS]... [INPUT FILES]...\n\n"
386 "Omitting the second argument or - reads from standard input.\n" 153 "Options:\n"
387 "Anything else is interpreted as a filename.\n\n" 154 " --help Print this help message.\n"
388 "This tool is intended as a help in writing tests.\n" 155 " --js Read raw JavaScript, instead of my output format "
oth 2016/02/12 14:40:13 s/my output/the default/
Stefano Sanfilippo 2016/02/12 14:49:52 Done.
389 "Please, DO NOT blindly copy and paste the output " 156 "(default: no).\n"
oth 2016/02/12 14:40:14 Do not need (default:no) here.
Stefano Sanfilippo 2016/02/12 14:49:52 Done.
390 "into the test suite.\n"; 157 " --stdin Read from standard input instead of file (default: no).\n"
oth 2016/02/12 14:40:14 Do not need (default:no) here.
Stefano Sanfilippo 2016/02/12 14:49:53 Done.
158 " --pool-type (int|double|string|mixed)\n"
159 " specify the type of the entries in the constant pool "
160 "(default: mixed).\n"
161 "\n"
162 "Each raw JavaScript file is interpreted as a single snippet.\n\n"
163 "This tool is intended as a help in writing tests.\n"
164 "Please, DO NOT blindly copy and paste the output "
165 "into the test suite.\n";
391 } 166 }
392 167
393 } // namespace 168 } // namespace
394 169
170 // TODO(ssanfilippo) split library from utilty?
395 int main(int argc, char** argv) { 171 int main(int argc, char** argv) {
396 if (argc < 2) { 172 CommandLineOptions options = ParseOptionsFromCommandLine(argc, argv);
173
174 if (!options.Validate() || options.print_help) {
397 PrintUsage(argv[0]); 175 PrintUsage(argv[0]);
398 return 1; 176 return options.print_help ? 0 : 1;
399 } 177 }
400 178
401 if (argc > 1 && strcmp(argv[1], "--help") == 0) { 179 std::vector<TestInput> snippet_list;
402 PrintUsage(argv[0]); 180 if (!ExtractSnippets(&snippet_list, options)) {
403 return 0; 181 return 2;
404 } 182 }
405 183
406 const char* body_filename = (argc > 2 ? argv[2] : "-"); 184 GenerateExpectationsFile(std::cout, snippet_list, argv[0]);
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 } 185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698