| 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 30 matching lines...) Expand all Loading... |
| 41 #include "scanner-character-streams.h" | 41 #include "scanner-character-streams.h" |
| 42 #include "scopeinfo.h" | 42 #include "scopeinfo.h" |
| 43 #include "string-stream.h" | 43 #include "string-stream.h" |
| 44 #include "scanner.h" | 44 #include "scanner.h" |
| 45 | 45 |
| 46 #include "experimental-scanner.h" | 46 #include "experimental-scanner.h" |
| 47 #include "lexer.h" | 47 #include "lexer.h" |
| 48 | 48 |
| 49 using namespace v8::internal; | 49 using namespace v8::internal; |
| 50 | 50 |
| 51 enum Encoding { |
| 52 ASCII, |
| 53 LATIN1, |
| 54 UTF8, |
| 55 UTF16 |
| 56 }; |
| 57 |
| 58 |
| 51 const byte* ReadFile(const char* name, Isolate* isolate, int* size) { | 59 const byte* ReadFile(const char* name, Isolate* isolate, int* size) { |
| 52 FILE* file = fopen(name, "rb"); | 60 FILE* file = fopen(name, "rb"); |
| 53 *size = 0; | 61 *size = 0; |
| 54 if (file == NULL) return NULL; | 62 if (file == NULL) return NULL; |
| 55 | 63 |
| 56 fseek(file, 0, SEEK_END); | 64 fseek(file, 0, SEEK_END); |
| 57 *size = ftell(file); | 65 *size = ftell(file); |
| 58 rewind(file); | 66 rewind(file); |
| 59 | 67 |
| 60 byte* chars = new byte[*size + 1]; | 68 byte* chars = new byte[*size + 1]; |
| 61 chars[*size] = 0; | 69 chars[*size] = 0; |
| 62 for (int i = 0; i < *size;) { | 70 for (int i = 0; i < *size;) { |
| 63 int read = static_cast<int>(fread(&chars[i], 1, *size - i, file)); | 71 int read = static_cast<int>(fread(&chars[i], 1, *size - i, file)); |
| 64 i += read; | 72 i += read; |
| 65 } | 73 } |
| 66 fclose(file); | 74 fclose(file); |
| 67 return chars; | 75 return chars; |
| 68 } | 76 } |
| 69 | 77 |
| 70 | |
| 71 class BaselineScanner { | 78 class BaselineScanner { |
| 72 public: | 79 public: |
| 73 BaselineScanner(const char* fname, Isolate* isolate) { | 80 BaselineScanner(const char* fname, Isolate* isolate, Encoding encoding) { |
| 74 int length = 0; | 81 int length = 0; |
| 75 source_ = ReadFile(fname, isolate, &length); | 82 source_ = ReadFile(fname, isolate, &length); |
| 76 unicode_cache_ = new UnicodeCache(); | 83 unicode_cache_ = new UnicodeCache(); |
| 77 scanner_ = new Scanner(unicode_cache_); | 84 scanner_ = new Scanner(unicode_cache_); |
| 78 stream_ = new Utf8ToUtf16CharacterStream(source_, length); | 85 switch (encoding) { |
| 86 case ASCII: |
| 87 case UTF8: |
| 88 stream_ = new Utf8ToUtf16CharacterStream(source_, length); |
| 89 break; |
| 90 case UTF16: { |
| 91 Handle<String> result = isolate->factory()->NewStringFromTwoByte( |
| 92 Vector<const uint16_t>( |
| 93 reinterpret_cast<const uint16_t*>(source_), |
| 94 length / 2)); |
| 95 stream_ = |
| 96 new GenericStringUtf16CharacterStream(result, 0, result->length()); |
| 97 break; |
| 98 } |
| 99 case LATIN1: { |
| 100 Handle<String> result = isolate->factory()->NewStringFromOneByte( |
| 101 Vector<const uint8_t>(source_, length)); |
| 102 stream_ = |
| 103 new GenericStringUtf16CharacterStream(result, 0, result->length()); |
| 104 break; |
| 105 } |
| 106 default: |
| 107 break; |
| 108 } |
| 79 scanner_->Initialize(stream_); | 109 scanner_->Initialize(stream_); |
| 80 } | 110 } |
| 81 | 111 |
| 82 ~BaselineScanner() { | 112 ~BaselineScanner() { |
| 83 delete scanner_; | 113 delete scanner_; |
| 84 delete stream_; | 114 delete stream_; |
| 85 delete unicode_cache_; | 115 delete unicode_cache_; |
| 86 delete[] source_; | 116 delete[] source_; |
| 87 } | 117 } |
| 88 | 118 |
| 89 Token::Value Next(int* beg_pos, int* end_pos) { | 119 Token::Value Next(int* beg_pos, int* end_pos) { |
| 90 Token::Value res = scanner_->Next(); | 120 Token::Value res = scanner_->Next(); |
| 91 *beg_pos = scanner_->location().beg_pos; | 121 *beg_pos = scanner_->location().beg_pos; |
| 92 *end_pos = scanner_->location().end_pos; | 122 *end_pos = scanner_->location().end_pos; |
| 93 return res; | 123 return res; |
| 94 } | 124 } |
| 95 | 125 |
| 96 private: | 126 private: |
| 97 UnicodeCache* unicode_cache_; | 127 UnicodeCache* unicode_cache_; |
| 98 Scanner* scanner_; | 128 Scanner* scanner_; |
| 99 const byte* source_; | 129 const byte* source_; |
| 100 Utf8ToUtf16CharacterStream* stream_; | 130 BufferedUtf16CharacterStream* stream_; |
| 101 }; | 131 }; |
| 102 | 132 |
| 103 | 133 |
| 104 int main(int argc, char* argv[]) { | 134 int main(int argc, char* argv[]) { |
| 105 v8::V8::InitializeICU(); | 135 v8::V8::InitializeICU(); |
| 106 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 136 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 137 Encoding encoding = ASCII; |
| 138 bool print_baseline = false; |
| 139 for (int i = 0; i < argc; ++i) { |
| 140 if (strcmp(argv[i], "--latin1") == 0) { |
| 141 encoding = LATIN1; |
| 142 } else if (strcmp(argv[i], "--utf8") == 0) { |
| 143 encoding = UTF8; |
| 144 } else if (strcmp(argv[i], "--utf16") == 0) { |
| 145 encoding = UTF16; |
| 146 } else if (strcmp(argv[i], "--ascii") == 0) { |
| 147 encoding = ASCII; |
| 148 } else if (strcmp(argv[i], "--print-baseline") == 0) { |
| 149 print_baseline = true; |
| 150 } |
| 151 } |
| 107 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 152 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 108 { | 153 { |
| 109 v8::HandleScope handle_scope(isolate); | 154 v8::HandleScope handle_scope(isolate); |
| 110 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 155 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 111 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); | 156 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 112 ASSERT(!context.IsEmpty()); | 157 ASSERT(!context.IsEmpty()); |
| 113 { | 158 { |
| 114 v8::Context::Scope scope(context); | 159 v8::Context::Scope scope(context); |
| 115 Isolate* isolate = Isolate::Current(); | 160 Isolate* isolate = Isolate::Current(); |
| 116 HandleScope handle_scope(isolate); | 161 HandleScope handle_scope(isolate); |
| 117 BaselineScanner baseline(argv[1], isolate); | 162 BaselineScanner baseline(argv[1], isolate, encoding); |
| 118 ExperimentalScanner experimental(argv[1], true, isolate); | 163 ExperimentalScanner experimental(argv[1], true, isolate); |
| 119 | 164 |
| 120 std::vector<Token::Value> baseline_tokens, experimental_tokens; | 165 std::vector<Token::Value> baseline_tokens, experimental_tokens; |
| 121 std::vector<size_t> baseline_beg, baseline_end, experimental_beg, | 166 std::vector<size_t> baseline_beg, baseline_end, experimental_beg, |
| 122 experimental_end; | 167 experimental_end; |
| 123 Token::Value token; | 168 Token::Value token; |
| 124 int beg, end; | 169 int beg, end; |
| 125 | 170 |
| 126 TimeDelta baseline_time, experimental_time; | 171 TimeDelta baseline_time, experimental_time; |
| 127 ElapsedTimer timer; | 172 ElapsedTimer timer; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 141 do { | 186 do { |
| 142 token = experimental.Next(); | 187 token = experimental.Next(); |
| 143 experimental_tokens.push_back(token); | 188 experimental_tokens.push_back(token); |
| 144 ExperimentalScanner::Location location = experimental.location(); | 189 ExperimentalScanner::Location location = experimental.location(); |
| 145 experimental_beg.push_back(location.beg_pos); | 190 experimental_beg.push_back(location.beg_pos); |
| 146 experimental_end.push_back(location.end_pos); | 191 experimental_end.push_back(location.end_pos); |
| 147 } while (token != Token::EOS); | 192 } while (token != Token::EOS); |
| 148 experimental_time = timer.Elapsed(); | 193 experimental_time = timer.Elapsed(); |
| 149 } | 194 } |
| 150 | 195 |
| 196 if (print_baseline) { |
| 197 printf("Baseline:\n"); |
| 198 for (size_t i = 0; i < baseline_tokens.size(); ++i) { |
| 199 printf("=> %11s at (%d, %d)\n", |
| 200 Token::Name(baseline_tokens[i]), |
| 201 static_cast<int>(baseline_beg[i]), |
| 202 static_cast<int>(baseline_end[i])); |
| 203 } |
| 204 printf("(Mis)matches:\n"); |
| 205 } |
| 206 |
| 151 for (size_t i = 0; i < experimental_tokens.size(); ++i) { | 207 for (size_t i = 0; i < experimental_tokens.size(); ++i) { |
| 152 printf("=> %11s at (%d, %d)\n", | 208 printf("=> %11s at (%d, %d)\n", |
| 153 Token::Name(experimental_tokens[i]), | 209 Token::Name(experimental_tokens[i]), |
| 154 static_cast<int>(experimental_beg[i]), | 210 static_cast<int>(experimental_beg[i]), |
| 155 static_cast<int>(experimental_end[i])); | 211 static_cast<int>(experimental_end[i])); |
| 156 if (experimental_tokens[i] != baseline_tokens[i] || | 212 if (experimental_tokens[i] != baseline_tokens[i] || |
| 157 experimental_beg[i] != baseline_beg[i] || | 213 experimental_beg[i] != baseline_beg[i] || |
| 158 experimental_end[i] != baseline_end[i]) { | 214 experimental_end[i] != baseline_end[i]) { |
| 159 printf("MISMATCH:\n"); | 215 printf("MISMATCH:\n"); |
| 160 printf("Expected: %s at (%d, %d)\n", | 216 printf("Expected: %s at (%d, %d)\n", |
| (...skipping 10 matching lines...) Expand all Loading... |
| 171 printf("No of tokens: %d\n", | 227 printf("No of tokens: %d\n", |
| 172 static_cast<int>(experimental_tokens.size())); | 228 static_cast<int>(experimental_tokens.size())); |
| 173 printf("Baseline: %f ms\nExperimental %f ms\n", | 229 printf("Baseline: %f ms\nExperimental %f ms\n", |
| 174 baseline_time.InMillisecondsF(), | 230 baseline_time.InMillisecondsF(), |
| 175 experimental_time.InMillisecondsF()); | 231 experimental_time.InMillisecondsF()); |
| 176 } | 232 } |
| 177 } | 233 } |
| 178 v8::V8::Dispose(); | 234 v8::V8::Dispose(); |
| 179 return 0; | 235 return 0; |
| 180 } | 236 } |
| OLD | NEW |