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

Side by Side Diff: tools/lexer-shell.cc

Issue 71163006: Merge bleeding_edge r17376:17693. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Fix all.gyp Created 7 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « tools/js2c.py ('k') | tools/lexer-shell.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 13 matching lines...) Expand all
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include <assert.h> 28 #include <assert.h>
29 #include <fcntl.h> 29 #include <fcntl.h>
30 #include <string.h> 30 #include <string.h>
31 #include <stdio.h> 31 #include <stdio.h>
32 #include <stdlib.h> 32 #include <stdlib.h>
33 #include <string> 33 #include <string>
34 #include <vector>
34 #include "v8.h" 35 #include "v8.h"
35 36
36 #include "api.h" 37 #include "api.h"
37 #include "ast.h" 38 #include "ast.h"
38 #include "char-predicates-inl.h" 39 #include "char-predicates-inl.h"
39 #include "messages.h" 40 #include "messages.h"
40 #include "platform.h" 41 #include "platform.h"
41 #include "runtime.h" 42 #include "runtime.h"
42 #include "scanner-character-streams.h" 43 #include "scanner-character-streams.h"
43 #include "scopeinfo.h" 44 #include "scopeinfo.h"
44 #include "string-stream.h" 45 #include "string-stream.h"
45 #include "scanner.h" 46 #include "scanner.h"
46 47
47 #include "experimental-scanner.h"
48 #include "lexer.h"
49 48
50 using namespace v8::internal; 49 using namespace v8::internal;
51 50
52 enum Encoding { 51 enum Encoding {
53 LATIN1, 52 LATIN1,
54 UTF8, 53 UTF8,
55 UTF16 54 UTF16
56 }; 55 };
57 56
58 57
59 const byte* ReadFile(const char* name, Isolate* isolate, int* size) { 58 const byte* ReadFile(const char* name, Isolate* isolate, int* size) {
60 FILE* file = fopen(name, "rb"); 59 FILE* file = fopen(name, "rb");
61 *size = 0; 60 *size = 0;
62 if (file == NULL) return NULL; 61 if (file == NULL) return NULL;
63 62
64 fseek(file, 0, SEEK_END); 63 fseek(file, 0, SEEK_END);
65 *size = ftell(file); 64 *size = ftell(file);
66 rewind(file); 65 rewind(file);
67 66
68 byte* chars = new byte[*size + 1]; 67 byte* chars = new byte[*size + 1];
69 chars[*size] = 0; 68 chars[*size] = 0;
70 for (int i = 0; i < *size;) { 69 for (int i = 0; i < *size;) {
71 int read = static_cast<int>(fread(&chars[i], 1, *size - i, file)); 70 int read = static_cast<int>(fread(&chars[i], 1, *size - i, file));
72 i += read; 71 i += read;
73 } 72 }
74 fclose(file); 73 fclose(file);
75 return chars; 74 return chars;
76 } 75 }
77 76
77
78 class BaselineScanner { 78 class BaselineScanner {
79 public: 79 public:
80 BaselineScanner(const char* fname, 80 BaselineScanner(const char* fname,
81 Isolate* isolate, 81 Isolate* isolate,
82 Encoding encoding, 82 Encoding encoding,
83 ElapsedTimer* timer) 83 ElapsedTimer* timer)
84 : stream_(NULL) { 84 : stream_(NULL) {
85 int length = 0; 85 int length = 0;
86 source_ = ReadFile(fname, isolate, &length); 86 source_ = ReadFile(fname, isolate, &length);
87 unicode_cache_ = new UnicodeCache(); 87 unicode_cache_ = new UnicodeCache();
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 do { 166 do {
167 token = scanner.Next(&beg, &end); 167 token = scanner.Next(&beg, &end);
168 if (dump_tokens) { 168 if (dump_tokens) {
169 tokens->push_back(TokenWithLocation(token, beg, end)); 169 tokens->push_back(TokenWithLocation(token, beg, end));
170 } 170 }
171 } while (token != Token::EOS); 171 } while (token != Token::EOS);
172 return timer.Elapsed(); 172 return timer.Elapsed();
173 } 173 }
174 174
175 175
176 TimeDelta RunExperimentalScanner(const char* fname,
177 Isolate* isolate,
178 Encoding encoding,
179 bool dump_tokens,
180 std::vector<TokenWithLocation>* tokens) {
181 ElapsedTimer timer;
182 timer.Start();
183 ExperimentalScanner scanner(fname, true, isolate);
184 Token::Value token;
185 do {
186 token = scanner.Next();
187 ExperimentalScanner::Location location = scanner.location();
188 if (dump_tokens) {
189 tokens->push_back(
190 TokenWithLocation(token, location.beg_pos, location.end_pos));
191 }
192 } while (token != Token::EOS);
193 return timer.Elapsed();
194 }
195
196
197 void PrintTokens(const char* name, 176 void PrintTokens(const char* name,
198 const std::vector<TokenWithLocation>& tokens) { 177 const std::vector<TokenWithLocation>& tokens) {
199 printf("No of tokens: %d\n", 178 printf("No of tokens: %d\n",
200 static_cast<int>(tokens.size())); 179 static_cast<int>(tokens.size()));
201 printf("%s:\n", name); 180 printf("%s:\n", name);
202 for (size_t i = 0; i < tokens.size(); ++i) { 181 for (size_t i = 0; i < tokens.size(); ++i) {
203 tokens[i].Print("=>"); 182 tokens[i].Print("=>");
204 } 183 }
205 } 184 }
206 185
207 186
208 std::pair<TimeDelta, TimeDelta> ProcessFile( 187 TimeDelta ProcessFile(
209 const char* fname, 188 const char* fname,
210 Encoding encoding, 189 Encoding encoding,
211 Isolate* isolate, 190 Isolate* isolate,
212 bool run_baseline, 191 bool print_tokens) {
213 bool run_experimental,
214 bool print_tokens,
215 bool check_tokens) {
216 if (print_tokens) { 192 if (print_tokens) {
217 printf("Processing file %s\n", fname); 193 printf("Processing file %s\n", fname);
218 } 194 }
219 HandleScope handle_scope(isolate); 195 HandleScope handle_scope(isolate);
220 std::vector<TokenWithLocation> baseline_tokens, experimental_tokens; 196 std::vector<TokenWithLocation> baseline_tokens;
221 TimeDelta baseline_time, experimental_time; 197 TimeDelta baseline_time;
222 if (run_baseline) { 198 baseline_time = RunBaselineScanner(
223 baseline_time = RunBaselineScanner( 199 fname, isolate, encoding, print_tokens,
224 fname, isolate, encoding, print_tokens || check_tokens, 200 &baseline_tokens);
225 &baseline_tokens); 201 if (print_tokens) {
226 }
227 if (run_experimental) {
228 experimental_time = RunExperimentalScanner(
229 fname, isolate, encoding, print_tokens || check_tokens,
230 &experimental_tokens);
231 }
232 if (print_tokens && !run_experimental) {
233 PrintTokens("Baseline", baseline_tokens); 202 PrintTokens("Baseline", baseline_tokens);
234 } 203 }
235 if (print_tokens && !run_baseline) { 204 return baseline_time;
236 PrintTokens("Experimental", experimental_tokens);
237 }
238 if ((print_tokens || check_tokens) && run_baseline && run_experimental) {
239 if (print_tokens) {
240 printf("No of tokens in Baseline: %d\n",
241 static_cast<int>(baseline_tokens.size()));
242 printf("No of tokens in Experimental: %d\n",
243 static_cast<int>(experimental_tokens.size()));
244 printf("Baseline and Experimental:\n");
245 }
246 for (size_t i = 0; i < experimental_tokens.size(); ++i) {
247 if (print_tokens) experimental_tokens[i].Print("=>");
248 if (experimental_tokens[i] != baseline_tokens[i]) {
249 printf("MISMATCH:\n");
250 baseline_tokens[i].Print("Expected: ");
251 experimental_tokens[i].Print("Actual: ");
252 exit(1);
253 }
254 }
255 }
256 return std::make_pair(baseline_time, experimental_time);
257 } 205 }
258 206
259 207
260 int main(int argc, char* argv[]) { 208 int main(int argc, char* argv[]) {
261 v8::V8::InitializeICU(); 209 v8::V8::InitializeICU();
262 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); 210 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
263 Encoding encoding = LATIN1; 211 Encoding encoding = LATIN1;
264 bool print_tokens = false; 212 bool print_tokens = false;
265 bool run_baseline = true;
266 bool run_experimental = true;
267 bool check_tokens = true;
268 std::vector<std::string> fnames; 213 std::vector<std::string> fnames;
269 std::string benchmark; 214 std::string benchmark;
270 for (int i = 0; i < argc; ++i) { 215 for (int i = 0; i < argc; ++i) {
271 if (strcmp(argv[i], "--latin1") == 0) { 216 if (strcmp(argv[i], "--latin1") == 0) {
272 encoding = LATIN1; 217 encoding = LATIN1;
273 } else if (strcmp(argv[i], "--utf8") == 0) { 218 } else if (strcmp(argv[i], "--utf8") == 0) {
274 encoding = UTF8; 219 encoding = UTF8;
275 } else if (strcmp(argv[i], "--utf16") == 0) { 220 } else if (strcmp(argv[i], "--utf16") == 0) {
276 encoding = UTF16; 221 encoding = UTF16;
277 } else if (strcmp(argv[i], "--print-tokens") == 0) { 222 } else if (strcmp(argv[i], "--print-tokens") == 0) {
278 print_tokens = true; 223 print_tokens = true;
279 } else if (strcmp(argv[i], "--no-baseline") == 0) {
280 run_baseline = false;
281 } else if (strcmp(argv[i], "--no-experimental") == 0) {
282 run_experimental = false;
283 } else if (strcmp(argv[i], "--no-check") == 0) {
284 check_tokens = false;
285 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) { 224 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) {
286 benchmark = std::string(argv[i]).substr(12); 225 benchmark = std::string(argv[i]).substr(12);
287 } else if (i > 0 && argv[i][0] != '-') { 226 } else if (i > 0 && argv[i][0] != '-') {
288 fnames.push_back(std::string(argv[i])); 227 fnames.push_back(std::string(argv[i]));
289 } 228 }
290 } 229 }
291 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 230 v8::Isolate* isolate = v8::Isolate::GetCurrent();
292 { 231 {
293 v8::HandleScope handle_scope(isolate); 232 v8::HandleScope handle_scope(isolate);
294 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 233 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
295 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); 234 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
296 ASSERT(!context.IsEmpty()); 235 ASSERT(!context.IsEmpty());
297 { 236 {
298 v8::Context::Scope scope(context); 237 v8::Context::Scope scope(context);
299 Isolate* isolate = Isolate::Current(); 238 Isolate* isolate = Isolate::Current();
300 double baseline_total = 0, experimental_total = 0; 239 double baseline_total = 0;
301 for (size_t i = 0; i < fnames.size(); i++) { 240 for (size_t i = 0; i < fnames.size(); i++) {
302 std::pair<TimeDelta, TimeDelta> times; 241 TimeDelta time;
303 check_tokens = check_tokens && run_baseline && run_experimental; 242 time = ProcessFile(fnames[i].c_str(), encoding, isolate, print_tokens);
304 times = ProcessFile(fnames[i].c_str(), encoding, isolate, run_baseline, 243 baseline_total += time.InMillisecondsF();
305 run_experimental, print_tokens, check_tokens);
306 baseline_total += times.first.InMillisecondsF();
307 experimental_total += times.second.InMillisecondsF();
308 } 244 }
309 if (run_baseline) { 245 if (benchmark.empty()) benchmark = "Baseline";
310 printf("Baseline%s(RunTime): %.f ms\n", benchmark.c_str(), 246 printf("%s(RunTime): %.f ms\n", benchmark.c_str(), baseline_total);
311 baseline_total);
312 }
313 if (run_experimental) {
314 if (benchmark.empty()) benchmark = "Experimental";
315 printf("%s(RunTime): %.f ms\n", benchmark.c_str(),
316 experimental_total);
317 }
318 } 247 }
319 } 248 }
320 v8::V8::Dispose(); 249 v8::V8::Dispose();
321 return 0; 250 return 0;
322 } 251 }
OLDNEW
« no previous file with comments | « tools/js2c.py ('k') | tools/lexer-shell.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698