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

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

Issue 1686963002: [Interpreter] Print constant pool in generate-bytecode-expectations (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Address reviewers' comments. 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <fstream>
6 #include <iostream> 6 #include <iostream>
7 7
8 #include "include/libplatform/libplatform.h" 8 #include "include/libplatform/libplatform.h"
9 #include "include/v8.h" 9 #include "include/v8.h"
10 10
11 #include "src/base/logging.h" 11 #include "src/base/logging.h"
12 #include "src/base/smart-pointers.h" 12 #include "src/base/smart-pointers.h"
13 #include "src/compiler.h" 13 #include "src/compiler.h"
14 14
15 #include "src/interpreter/bytecode-array-iterator.h" 15 #include "src/interpreter/bytecode-array-iterator.h"
16 #include "src/interpreter/bytecode-generator.h" 16 #include "src/interpreter/bytecode-generator.h"
17 #include "src/interpreter/bytecodes.h" 17 #include "src/interpreter/bytecodes.h"
18 #include "src/interpreter/interpreter.h" 18 #include "src/interpreter/interpreter.h"
19 19
20 using namespace i::interpreter; 20 using namespace i::interpreter;
21 21
22 namespace { 22 namespace {
23 23
24 const char* kIndent = " "; 24 const char* kIndent = " ";
25 25
26 enum ConstantPoolType {
27 kConstantPoolTypeUnknown,
28 kConstantPoolTypeString,
29 kConstantPoolTypeInteger,
30 kConstantPoolTypeDouble,
31 kConstantPoolTypeMixed,
32 };
33
26 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { 34 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator {
27 public: 35 public:
28 void* Allocate(size_t length) override { 36 void* Allocate(size_t length) override {
29 void* data = AllocateUninitialized(length); 37 void* data = AllocateUninitialized(length);
30 if (data != nullptr) memset(data, 0, length); 38 if (data != nullptr) memset(data, 0, length);
31 return data; 39 return data;
32 } 40 }
33 void* AllocateUninitialized(size_t length) override { return malloc(length); } 41 void* AllocateUninitialized(size_t length) override { return malloc(length); }
34 void Free(void* data, size_t) override { free(data); } 42 void Free(void* data, size_t) override { free(data); }
35 }; 43 };
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 context->Global()->Get(context, v8_global_name).ToLocalChecked()); 122 context->Global()->Get(context, v8_global_name).ToLocalChecked());
115 i::Handle<i::JSFunction> js_function = 123 i::Handle<i::JSFunction> js_function =
116 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function)); 124 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function));
117 125
118 i::Handle<i::BytecodeArray> bytecodes = i::handle( 126 i::Handle<i::BytecodeArray> bytecodes = i::handle(
119 js_function->shared()->bytecode_array(), GetInternalIsolate(isolate)); 127 js_function->shared()->bytecode_array(), GetInternalIsolate(isolate));
120 128
121 return bytecodes; 129 return bytecodes;
122 } 130 }
123 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 }
152 }
153 return quoted_buffer;
154 }
155
124 void PrintBytecodeOperand(std::ostream& stream, 156 void PrintBytecodeOperand(std::ostream& stream,
125 const BytecodeArrayIterator& bytecode_iter, 157 const BytecodeArrayIterator& bytecode_iter,
126 const Bytecode& bytecode, int op_index) { 158 const Bytecode& bytecode, int op_index) {
127 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index); 159 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index);
128 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index); 160 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index);
129 161
130 const char* size_tag; 162 const char* size_tag;
131 switch (op_size) { 163 switch (op_size) {
132 case OperandSize::kByte: 164 case OperandSize::kByte:
133 size_tag = "8"; 165 size_tag = "8";
(...skipping 23 matching lines...) Expand all
157 189
158 stream << "B(" << Bytecodes::ToString(bytecode) << ')'; 190 stream << "B(" << Bytecodes::ToString(bytecode) << ')';
159 191
160 int operands_count = Bytecodes::NumberOfOperands(bytecode); 192 int operands_count = Bytecodes::NumberOfOperands(bytecode);
161 for (int op_index = 0; op_index < operands_count; ++op_index) { 193 for (int op_index = 0; op_index < operands_count; ++op_index) {
162 stream << ", "; 194 stream << ", ";
163 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index); 195 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index);
164 } 196 }
165 } 197 }
166 198
167 std::string QuoteCString(const std::string& source) { 199 void PrintV8String(std::ostream& stream, i::String* string) {
168 std::string quoted_buffer; 200 stream << '"';
169 for (char c : source) { 201 for (int i = 0, length = string->length(); i < length; ++i) {
170 switch (c) { 202 stream << i::AsEscapedUC16ForJSON(string->Get(i));
171 case '"':
172 quoted_buffer += "\\\"";
173 break;
174 case '\n':
175 quoted_buffer += "\\n";
176 break;
177 case '\t':
178 quoted_buffer += "\\t";
179 break;
180 case '\\':
181 quoted_buffer += "\\\\";
182 break;
183 default:
184 quoted_buffer += c;
185 break;
186 }
187 } 203 }
188 return quoted_buffer; 204 stream << '"';
189 } 205 }
190 206
191 void PrintBytecodeArray(std::ostream& stream, 207 void PrintConstant(std::ostream& stream,
192 i::Handle<i::BytecodeArray> bytecode_array, 208 ConstantPoolType expected_constant_type,
193 const std::string& body, bool print_banner = true) { 209 i::Handle<i::Object> constant) {
210 switch (expected_constant_type) {
211 case kConstantPoolTypeString:
212 CHECK(constant->IsString());
213 PrintV8String(stream, i::String::cast(*constant));
214 break;
215 case kConstantPoolTypeInteger:
216 if (constant->IsSmi()) {
217 i::Smi::cast(*constant)->SmiPrint(stream);
218 } else if (constant->IsHeapNumber()) {
219 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream);
220 } else {
221 UNREACHABLE();
222 }
223 break;
224 case kConstantPoolTypeDouble:
225 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream);
226 break;
227 case kConstantPoolTypeMixed:
228 if (constant->IsSmi()) {
229 stream << "kInstanceTypeDontCare";
230 } else {
231 stream << "InstanceType::"
232 << i::HeapObject::cast(*constant)->map()->instance_type();
233 }
234 break;
235 default:
236 UNREACHABLE();
237 return;
238 }
239 }
240
241 void PrintFrameSize(std::ostream& stream,
242 i::Handle<i::BytecodeArray> bytecode_array) {
194 const int kPointerSize = sizeof(void*); 243 const int kPointerSize = sizeof(void*);
244 int frame_size = bytecode_array->frame_size();
195 245
196 if (print_banner) { 246 stream << kIndent;
197 stream << kIndent << "// === ExpectedSnippet generated by "
198 "generate-bytecode-expectations. ===\n";
199 }
200 247
201 // Print the code snippet as a quoted C string.
202 stream << kIndent << "{" << '"' << QuoteCString(body) << "\",\n" << kIndent;
203
204 // Print the frame size, in multiples of kPointerSize.
205 int frame_size = bytecode_array->frame_size();
206 DCHECK(frame_size % kPointerSize == 0); 248 DCHECK(frame_size % kPointerSize == 0);
207 if (frame_size > kPointerSize) { 249 if (frame_size > kPointerSize) {
208 stream << ' ' << frame_size / kPointerSize << " * kPointerSize,\n" 250 stream << ' ' << frame_size / kPointerSize << " * kPointerSize,\n"
209 << kIndent; 251 << kIndent;
210 } else if (frame_size == kPointerSize) { 252 } else if (frame_size == kPointerSize) {
211 stream << " kPointerSize,\n" << kIndent; 253 stream << " kPointerSize,\n" << kIndent;
212 } else if (frame_size == 0) { 254 } else if (frame_size == 0) {
213 stream << " 0,\n" << kIndent; 255 stream << " 0,\n" << kIndent;
214 } 256 }
215 257
216 // Print parameter count and size of the bytecode array. 258 stream << ' ' << bytecode_array->parameter_count() << ",\n";
217 stream << ' ' << bytecode_array->parameter_count() << ",\n" 259 }
218 << kIndent << ' ' << bytecode_array->length() << ",\n" 260
261 void PrintBytecodeSequence(std::ostream& stream,
262 i::Handle<i::BytecodeArray> bytecode_array) {
263 stream << kIndent << ' ' << bytecode_array->length() << ",\n"
219 << kIndent << " {\n" 264 << kIndent << " {\n"
220 << kIndent << " "; 265 << kIndent << " ";
221 266
222 // Print bytecodes.
223 BytecodeArrayIterator bytecode_iter{bytecode_array}; 267 BytecodeArrayIterator bytecode_iter{bytecode_array};
224 for (; !bytecode_iter.done(); bytecode_iter.Advance()) { 268 for (; !bytecode_iter.done(); bytecode_iter.Advance()) {
225 // Print separator before each instruction, except the first one. 269 // Print separator before each instruction, except the first one.
226 if (bytecode_iter.current_offset() > 0) { 270 if (bytecode_iter.current_offset() > 0) {
227 stream << ",\n" << kIndent << " "; 271 stream << ",\n" << kIndent << " ";
228 } 272 }
229 PrintBytecode(stream, bytecode_iter); 273 PrintBytecode(stream, bytecode_iter);
230 } 274 }
275 }
231 276
232 stream << "\n" << kIndent << " },\n"; 277 void PrintConstantPool(std::ostream& stream, i::FixedArray* constant_pool,
233 // TODO(ssanfilippo) add representation of constant pool and handlers. 278 ConstantPoolType expected_constant_type,
234 stream << kIndent << " // constant pool and handlers here\n"; 279 v8::Isolate* isolate) {
280 int num_constants = constant_pool->length();
281 stream << "\n" << kIndent << " },\n" << kIndent << ' ' << num_constants;
282 if (num_constants > 0) {
283 stream << ",\n" << kIndent << " {";
284 for (int i = 0; i < num_constants; ++i) {
285 // Print separator before each constant, except the first one
286 if (i != 0) stream << ", ";
287 PrintConstant(
288 stream, expected_constant_type,
289 i::FixedArray::get(constant_pool, i, GetInternalIsolate(isolate)));
290 }
291 stream << '}';
292 }
293 stream << '\n';
294 }
295
296 void PrintBytecodeArray(std::ostream& stream,
297 i::Handle<i::BytecodeArray> bytecode_array,
298 const std::string& body, v8::Isolate* isolate,
299 ConstantPoolType constant_pool_type,
300 bool print_banner = true) {
301 if (print_banner) {
302 stream << kIndent << "// === ExpectedSnippet generated by "
303 "generate-bytecode-expectations. ===\n";
304 }
305
306 // Print the code snippet as a quoted C string.
307 stream << kIndent << "{" << '"' << QuoteCString(body) << "\",\n";
308
309 PrintFrameSize(stream, bytecode_array);
310 PrintBytecodeSequence(stream, bytecode_array);
311 PrintConstantPool(stream, bytecode_array->constant_pool(), constant_pool_type,
312 isolate);
313
314 // TODO(ssanfilippo) print handlers.
315 i::HandlerTable* handlers =
316 i::HandlerTable::cast(bytecode_array->handler_table());
317 CHECK_EQ(handlers->NumberOfRangeEntries(), 0);
318
235 stream << kIndent << "}\n"; 319 stream << kIndent << "}\n";
236 } 320 }
237 321
322 void PrintExpectedSnippet(ConstantPoolType constant_pool_type, char* exec_path,
323 std::string body) {
324 const char* wrapper_function_name = "__genbckexp_wrapper__";
325
326 V8InitializationScope platform(exec_path);
327 {
328 v8::Isolate::Scope isolate_scope(platform.isolate());
329 v8::HandleScope handle_scope(platform.isolate());
330 v8::Local<v8::Context> context = v8::Context::New(platform.isolate());
331 v8::Context::Scope context_scope(context);
332
333 std::string source_code = WrapCodeInFunction(wrapper_function_name, body);
334 CompileAndRun(platform.isolate(), context, source_code.c_str());
335
336 i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal(
337 platform.isolate(), context, wrapper_function_name);
338
339 PrintBytecodeArray(std::cout, bytecode_array, body, platform.isolate(),
340 constant_pool_type);
341 }
342 }
343
238 bool ReadFromFileOrStdin(std::string* body, const char* body_filename) { 344 bool ReadFromFileOrStdin(std::string* body, const char* body_filename) {
239 std::stringstream body_buffer; 345 std::stringstream body_buffer;
240 if (strcmp(body_filename, "-") == 0) { 346 if (strcmp(body_filename, "-") == 0) {
241 body_buffer << std::cin.rdbuf(); 347 body_buffer << std::cin.rdbuf();
242 } else { 348 } else {
243 std::ifstream body_file{body_filename}; 349 std::ifstream body_file{body_filename};
244 if (!body_file) return false; 350 if (!body_file) return false;
245 body_buffer << body_file.rdbuf(); 351 body_buffer << body_file.rdbuf();
246 } 352 }
247 *body = body_buffer.str(); 353 *body = body_buffer.str();
248 return true; 354 return true;
249 } 355 }
250 356
357 ConstantPoolType ParseConstantPoolType(const char* type_string) {
358 if (strcmp(type_string, "int") == 0) {
359 return kConstantPoolTypeInteger;
360 } else if (strcmp(type_string, "double") == 0) {
361 return kConstantPoolTypeDouble;
362 } else if (strcmp(type_string, "string") == 0) {
363 return kConstantPoolTypeString;
364 } else if (strcmp(type_string, "mixed") == 0) {
365 return kConstantPoolTypeMixed;
366 }
367 return kConstantPoolTypeUnknown;
368 }
369
251 void PrintUsage(const char* exec_path) { 370 void PrintUsage(const char* exec_path) {
252 std::cerr << "Usage: " << exec_path 371 std::cerr << "Usage: " << exec_path
253 << " [filename.js|-]\n\n" 372 << " (int|double|string|mixed) [filename.js|-]\n\n"
254 "No arguments or - reads from standard input.\n" 373 "First argument is the type of objects in the constant pool.\n\n"
374 "Omitting the second argument or - reads from standard input.\n"
255 "Anything else is interpreted as a filename.\n\n" 375 "Anything else is interpreted as a filename.\n\n"
256 "This tool is intended as a help in writing tests.\n" 376 "This tool is intended as a help in writing tests.\n"
257 "Please, DO NOT blindly copy and paste the output " 377 "Please, DO NOT blindly copy and paste the output "
258 "into the test suite.\n"; 378 "into the test suite.\n";
259 } 379 }
260 380
261 } // namespace 381 } // namespace
262 382
263 int main(int argc, char** argv) { 383 int main(int argc, char** argv) {
384 if (argc < 2) {
385 PrintUsage(argv[0]);
386 return 1;
387 }
388
264 if (argc > 1 && strcmp(argv[1], "--help") == 0) { 389 if (argc > 1 && strcmp(argv[1], "--help") == 0) {
265 PrintUsage(argv[0]); 390 PrintUsage(argv[0]);
266 return 0; 391 return 0;
267 } 392 }
268 393
269 const char* body_filename = (argc > 1 ? argv[1] : "-"); 394 const char* body_filename = (argc > 2 ? argv[2] : "-");
270 const char* wrapper_function_name = "__genbckexp_wrapper__"; 395 const char* const_pool_type_string = argv[1];
271 396
272 std::string body; 397 std::string body;
273 if (!ReadFromFileOrStdin(&body, body_filename)) { 398 if (!ReadFromFileOrStdin(&body, body_filename)) {
274 std::cerr << "Could not open '" << body_filename << "'.\n\n"; 399 std::cerr << "Could not open '" << body_filename << "'.\n\n";
275 PrintUsage(argv[0]); 400 PrintUsage(argv[0]);
276 return 1; 401 return 1;
277 } 402 }
278 403
279 V8InitializationScope platform(argv[0]); 404 PrintExpectedSnippet(ParseConstantPoolType(const_pool_type_string), argv[0],
280 { 405 body);
281 v8::Isolate::Scope isolate_scope(platform.isolate());
282 v8::HandleScope handle_scope(platform.isolate());
283 v8::Local<v8::Context> context = v8::Context::New(platform.isolate());
284 v8::Context::Scope context_scope(context);
285
286 std::string source_code = WrapCodeInFunction(wrapper_function_name, body);
287 CompileAndRun(platform.isolate(), context, source_code.c_str());
288
289 i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal(
290 platform.isolate(), context, wrapper_function_name);
291
292 PrintBytecodeArray(std::cout, bytecode_array, body);
293 }
294 } 406 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698