| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 #include "scanner.h" | 45 #include "scanner.h" |
| 46 #include "lexer/lexer.h" | 46 #include "lexer/lexer.h" |
| 47 | 47 |
| 48 using namespace v8::internal; | 48 using namespace v8::internal; |
| 49 | 49 |
| 50 | 50 |
| 51 enum Encoding { | 51 enum Encoding { |
| 52 LATIN1, | 52 LATIN1, |
| 53 UTF8, | 53 UTF8, |
| 54 UTF16, | 54 UTF16, |
| 55 UTF8TO16, // Convert stream via scanner input stream | 55 UTF8TO16, |
| 56 UTF8TOLATIN1, // Convert stream via scanner input stream | 56 UTF8TOLATIN1, |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 | 59 |
| 60 struct LexerShellSettings { | 60 struct LexerShellSettings { |
| 61 Encoding encoding; | 61 Encoding encoding; |
| 62 bool print_tokens; | 62 bool print_tokens; |
| 63 bool print_tokens_for_compare; | 63 bool print_tokens_for_compare; |
| 64 bool break_after_illegal; | 64 bool break_after_illegal; |
| 65 bool eos_test; | 65 bool eos_test; |
| 66 int repeat; | 66 int repeat; |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 SmartArrayPointer<uint16_t> literal; | 289 SmartArrayPointer<uint16_t> literal; |
| 290 int literal_length; | 290 int literal_length; |
| 291 // The location of the latest octal position when the token was seen. | 291 // The location of the latest octal position when the token was seen. |
| 292 int octal_beg; | 292 int octal_beg; |
| 293 int octal_end; | 293 int octal_end; |
| 294 | 294 |
| 295 DISALLOW_COPY_AND_ASSIGN(TokenWithLocation); | 295 DISALLOW_COPY_AND_ASSIGN(TokenWithLocation); |
| 296 }; | 296 }; |
| 297 | 297 |
| 298 | 298 |
| 299 typedef std::vector<TokenWithLocation*> TokenVector; |
| 300 |
| 301 |
| 299 static TimeDelta RunLexer(const uint16_t* source, | 302 static TimeDelta RunLexer(const uint16_t* source, |
| 300 const uint8_t* source_end, | 303 const uint8_t* source_end, |
| 301 Isolate* isolate, | 304 Isolate* isolate, |
| 302 Encoding output_encoding, | 305 Encoding output_encoding, |
| 303 const LexerShellSettings& settings) { | 306 const LexerShellSettings& settings, |
| 307 TokenVector* tokens) { |
| 304 SmartPointer<Utf16CharacterStream> stream; | 308 SmartPointer<Utf16CharacterStream> stream; |
| 305 const uint8_t* one_byte_source = reinterpret_cast<const uint8_t*>(source); | 309 const uint8_t* one_byte_source = reinterpret_cast<const uint8_t*>(source); |
| 310 CHECK_GE(source_end - one_byte_source, 0); |
| 306 int bytes = source_end - one_byte_source; | 311 int bytes = source_end - one_byte_source; |
| 307 switch (output_encoding) { | 312 switch (output_encoding) { |
| 308 case UTF8: | 313 case UTF8: |
| 309 stream.Reset(new Utf8ToUtf16CharacterStream(one_byte_source, bytes)); | 314 stream.Reset(new Utf8ToUtf16CharacterStream(one_byte_source, bytes)); |
| 310 break; | 315 break; |
| 311 case UTF16: { | 316 case UTF16: { |
| 312 CHECK_EQ(0, bytes % 2); | 317 CHECK_EQ(0, bytes % 2); |
| 313 Handle<String> result = isolate->factory()->NewStringFromTwoByte( | 318 Handle<String> result = isolate->factory()->NewStringFromTwoByte( |
| 314 Vector<const uint16_t>(source, bytes / 2), false); | 319 Vector<const uint16_t>(source, bytes / 2), false); |
| 315 stream.Reset( | 320 stream.Reset( |
| 316 new GenericStringUtf16CharacterStream(result, 0, result->length())); | 321 new GenericStringUtf16CharacterStream(result, 0, result->length())); |
| 317 break; | 322 break; |
| 318 } | 323 } |
| 319 case LATIN1: { | 324 case LATIN1: { |
| 320 Handle<String> result = isolate->factory()->NewStringFromOneByte( | 325 Handle<String> result = isolate->factory()->NewStringFromOneByte( |
| 321 Vector<const uint8_t>(one_byte_source, bytes)); | 326 Vector<const uint8_t>(one_byte_source, bytes)); |
| 322 stream.Reset( | 327 stream.Reset( |
| 323 new GenericStringUtf16CharacterStream(result, 0, result->length())); | 328 new GenericStringUtf16CharacterStream(result, 0, result->length())); |
| 324 break; | 329 break; |
| 325 } | 330 } |
| 326 case UTF8TO16: | 331 case UTF8TO16: |
| 327 case UTF8TOLATIN1: | 332 case UTF8TOLATIN1: |
| 328 CHECK(false); | 333 CHECK(false); |
| 329 } | 334 } |
| 330 Scanner scanner(isolate->unicode_cache()); | 335 Scanner scanner(isolate->unicode_cache()); |
| 331 scanner.SetHarmonyNumericLiterals(settings.harmony_numeric_literals); | 336 scanner.SetHarmonyNumericLiterals(settings.harmony_numeric_literals); |
| 332 scanner.SetHarmonyModules(settings.harmony_modules); | 337 scanner.SetHarmonyModules(settings.harmony_modules); |
| 333 scanner.SetHarmonyScoping(settings.harmony_scoping); | 338 scanner.SetHarmonyScoping(settings.harmony_scoping); |
| 334 ElapsedTimer timer; | 339 ElapsedTimer timer; |
| 335 std::vector<TokenWithLocation*> tokens; | |
| 336 timer.Start(); | 340 timer.Start(); |
| 337 scanner.Initialize(stream.get()); | 341 scanner.Initialize(stream.get()); |
| 338 Token::Value token; | 342 Token::Value token; |
| 339 do { | 343 do { |
| 340 token = scanner.Next(); | 344 token = scanner.Next(); |
| 341 Handle<String> literal; | 345 Handle<String> literal; |
| 342 if (HasLiteral(token)) { | 346 if (HasLiteral(token)) { |
| 343 literal = scanner.AllocateInternalizedString(isolate); | 347 literal = scanner.AllocateInternalizedString(isolate); |
| 344 } | 348 } |
| 345 if (settings.print_tokens) { | 349 if (settings.print_tokens) { |
| 346 tokens.push_back(new TokenWithLocation(token, &scanner, literal)); | 350 tokens->push_back(new TokenWithLocation(token, &scanner, literal)); |
| 347 } | 351 } |
| 348 if (token == Token::ILLEGAL && settings.break_after_illegal) break; | 352 if (token == Token::ILLEGAL && settings.break_after_illegal) break; |
| 349 } while (token != Token::EOS); | 353 } while (token != Token::EOS); |
| 350 TimeDelta elapsed = timer.Elapsed(); | 354 return timer.Elapsed(); |
| 351 // Dump tokens. | |
| 352 if (settings.print_tokens) { | |
| 353 if (!settings.print_tokens_for_compare) { | |
| 354 printf("No of tokens:\t%d\n", static_cast<int>(tokens.size())); | |
| 355 } | |
| 356 for (size_t i = 0; i < tokens.size(); ++i) { | |
| 357 tokens[i]->Print(settings.print_tokens_for_compare); | |
| 358 } | |
| 359 } | |
| 360 for (size_t i = 0; i < tokens.size(); ++i) { | |
| 361 delete tokens[i]; | |
| 362 } | |
| 363 return elapsed; | |
| 364 } | 355 } |
| 365 | 356 |
| 366 | 357 |
| 367 static TimeDelta ProcessFile( | 358 static void Run(const LexerShellSettings& settings, |
| 368 Isolate* isolate, | 359 const FileData& file_data) { |
| 369 const LexerShellSettings& settings, | 360 Isolate* isolate = Isolate::Current(); |
| 370 const FileData& file_data, | 361 HandleScope handle_scope(isolate); |
| 371 int truncate_by, | 362 v8::Context::Scope scope(v8::Context::New(v8::Isolate::GetCurrent())); |
| 372 bool* can_truncate) { | 363 double total_time = 0; |
| 373 if (settings.print_tokens && !settings.print_tokens_for_compare) { | 364 std::vector<TokenWithLocation*> tokens; |
| 374 printf("Processing file %s, truncating by %d bytes\n", | 365 const uint16_t* const buffer = file_data.data; |
| 375 file_data.file_name, truncate_by); | 366 const uint8_t* const char_data = reinterpret_cast<const uint8_t*>(buffer); |
| 367 for (unsigned truncate_by = 0; |
| 368 truncate_by <= file_data.length_in_bytes; |
| 369 truncate_by += file_data.encoding == UTF16 ? 2 : 1) { |
| 370 if (settings.print_tokens && !settings.print_tokens_for_compare) { |
| 371 printf("Processing file %s, truncating by %d bytes\n", |
| 372 file_data.file_name, truncate_by); |
| 373 } |
| 374 HandleScope handle_scope(isolate); |
| 375 const uint8_t* buffer_end = |
| 376 &char_data[file_data.length_in_bytes] - truncate_by; |
| 377 TimeDelta delta = RunLexer( |
| 378 buffer, buffer_end, isolate, file_data.encoding, settings, &tokens); |
| 379 total_time += delta.InMillisecondsF(); |
| 380 // Dump tokens. |
| 381 if (settings.print_tokens) { |
| 382 if (!settings.print_tokens_for_compare) { |
| 383 printf("No of tokens:\t%d\n", static_cast<int>(tokens.size())); |
| 384 } |
| 385 for (size_t i = 0; i < tokens.size(); ++i) { |
| 386 tokens[i]->Print(settings.print_tokens_for_compare); |
| 387 } |
| 388 } |
| 389 // Destroy tokens. |
| 390 for (size_t i = 0; i < tokens.size(); ++i) { |
| 391 delete tokens[i]; |
| 392 } |
| 393 tokens.clear(); |
| 394 if (!settings.eos_test) break; |
| 376 } | 395 } |
| 377 HandleScope handle_scope(isolate); | 396 if (!settings.print_tokens_for_compare) { |
| 378 const uint16_t* buffer = file_data.data; | 397 printf("RunTime: %.f ms\n", total_time); |
| 379 const uint8_t* char_data = reinterpret_cast<const uint8_t*>(buffer); | |
| 380 const uint8_t* buffer_end = &char_data[file_data.length_in_bytes]; | |
| 381 TimeDelta time; | |
| 382 if (truncate_by > buffer_end - char_data) { | |
| 383 *can_truncate = false; | |
| 384 } else { | |
| 385 buffer_end -= truncate_by; | |
| 386 time = RunLexer(buffer, buffer_end, isolate, file_data.encoding, settings); | |
| 387 } | 398 } |
| 388 return time; | |
| 389 } | 399 } |
| 390 | 400 |
| 391 | 401 |
| 392 int main(int argc, char* argv[]) { | 402 int main(int argc, char* argv[]) { |
| 393 v8::V8::InitializeICU(); | 403 v8::V8::InitializeICU(); |
| 394 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 404 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 395 std::string file_name; | 405 std::string file_name; |
| 396 LexerShellSettings settings; | 406 LexerShellSettings settings; |
| 397 for (int i = 0; i < argc; ++i) { | 407 for (int i = 0; i < argc; ++i) { |
| 398 if (strcmp(argv[i], "--latin1") == 0) { | 408 if (strcmp(argv[i], "--latin1") == 0) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 411 #ifdef V8_USE_GENERATED_LEXER | 421 #ifdef V8_USE_GENERATED_LEXER |
| 412 settings.encoding = UTF8TOLATIN1; | 422 settings.encoding = UTF8TOLATIN1; |
| 413 #else | 423 #else |
| 414 settings.encoding = UTF8; | 424 settings.encoding = UTF8; |
| 415 #endif | 425 #endif |
| 416 } else if (strcmp(argv[i], "--print-tokens") == 0) { | 426 } else if (strcmp(argv[i], "--print-tokens") == 0) { |
| 417 settings.print_tokens = true; | 427 settings.print_tokens = true; |
| 418 } else if (strcmp(argv[i], "--print-tokens-for-compare") == 0) { | 428 } else if (strcmp(argv[i], "--print-tokens-for-compare") == 0) { |
| 419 settings.print_tokens = true; | 429 settings.print_tokens = true; |
| 420 settings.print_tokens_for_compare = true; | 430 settings.print_tokens_for_compare = true; |
| 421 } else if (strcmp(argv[i], "--no-baseline") == 0) { | |
| 422 // Ignore. | |
| 423 } else if (strcmp(argv[i], "--no-experimental") == 0) { | |
| 424 // Ignore. | |
| 425 } else if (strcmp(argv[i], "--no-check") == 0) { | |
| 426 // Ignore. | |
| 427 } else if (strcmp(argv[i], "--break-after-illegal") == 0) { | 431 } else if (strcmp(argv[i], "--break-after-illegal") == 0) { |
| 428 settings.break_after_illegal = true; | 432 settings.break_after_illegal = true; |
| 429 } else if (strcmp(argv[i], "--use-harmony") == 0) { | 433 } else if (strcmp(argv[i], "--use-harmony") == 0) { |
| 430 settings.harmony_numeric_literals = true; | 434 settings.harmony_numeric_literals = true; |
| 431 settings.harmony_modules = true; | 435 settings.harmony_modules = true; |
| 432 settings.harmony_scoping = true; | 436 settings.harmony_scoping = true; |
| 433 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) { | |
| 434 // Ignore. | |
| 435 } else if (strncmp(argv[i], "--repeat=", 9) == 0) { | 437 } else if (strncmp(argv[i], "--repeat=", 9) == 0) { |
| 436 std::string repeat_str = std::string(argv[i]).substr(9); | 438 std::string repeat_str = std::string(argv[i]).substr(9); |
| 437 settings.repeat = atoi(repeat_str.c_str()); | 439 settings.repeat = atoi(repeat_str.c_str()); |
| 438 } else if (strcmp(argv[i], "--eos-test") == 0) { | 440 } else if (strcmp(argv[i], "--eos-test") == 0) { |
| 439 settings.eos_test = true; | 441 settings.eos_test = true; |
| 440 } else if (i > 0 && argv[i][0] != '-') { | 442 } else if (i > 0 && argv[i][0] != '-') { |
| 441 file_name = std::string(argv[i]); | 443 file_name = std::string(argv[i]); |
| 442 } | 444 } |
| 443 } | 445 } |
| 444 CHECK_NE(0, file_name.size()); | 446 CHECK_NE(0, file_name.size()); |
| 445 FileData file_data = ReadFile(file_name.c_str(), settings); | 447 FileData file_data = ReadFile(file_name.c_str(), settings); |
| 446 { | 448 Run(settings, file_data); |
| 447 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 448 v8::HandleScope handle_scope(isolate); | |
| 449 v8::Local<v8::Context> context = v8::Context::New(isolate); | |
| 450 CHECK(!context.IsEmpty()); | |
| 451 v8::Context::Scope scope(context); | |
| 452 Isolate* internal_isolate = Isolate::Current(); | |
| 453 double total_time = 0; | |
| 454 bool can_truncate = settings.eos_test; | |
| 455 int truncate_by = 0; | |
| 456 do { | |
| 457 TimeDelta t = ProcessFile(internal_isolate, | |
| 458 settings, | |
| 459 file_data, | |
| 460 truncate_by, | |
| 461 &can_truncate); | |
| 462 total_time += t.InMillisecondsF(); | |
| 463 ++truncate_by; | |
| 464 } while (can_truncate); | |
| 465 if (!settings.print_tokens_for_compare) { | |
| 466 printf("RunTime: %.f ms\n", total_time); | |
| 467 } | |
| 468 } | |
| 469 delete[] file_data.data; | 449 delete[] file_data.data; |
| 470 v8::V8::Dispose(); | 450 v8::V8::Dispose(); |
| 471 return 0; | 451 return 0; |
| 472 } | 452 } |
| OLD | NEW |