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

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

Issue 178193012: Experimental parser: remove lexer-shell from tools/ (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | 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
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28 #include <assert.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string>
34 #include <vector>
35 #include "v8.h"
36
37 #include "api.h"
38 #include "ast.h"
39 #include "char-predicates-inl.h"
40 #include "messages.h"
41 #include "platform.h"
42 #include "runtime.h"
43 #include "scanner-character-streams.h"
44 #include "scopeinfo.h"
45 #include "string-stream.h"
46 #include "scanner.h"
47
48
49 using namespace v8::internal;
50
51 enum Encoding {
52 LATIN1,
53 UTF8,
54 UTF16
55 };
56
57
58 const byte* ReadFile(const char* name, Isolate* isolate,
59 int* size, int repeat) {
60 FILE* file = fopen(name, "rb");
61 *size = 0;
62 if (file == NULL) return NULL;
63
64 fseek(file, 0, SEEK_END);
65 int file_size = ftell(file);
66 rewind(file);
67
68 *size = file_size * repeat;
69
70 byte* chars = new byte[*size + 1];
71 for (int i = 0; i < file_size;) {
72 int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file));
73 i += read;
74 }
75 fclose(file);
76
77 for (int i = file_size; i < *size; i++) {
78 chars[i] = chars[i - file_size];
79 }
80 chars[*size] = 0;
81
82 return chars;
83 }
84
85
86 class BaselineScanner {
87 public:
88 BaselineScanner(const char* fname,
89 Isolate* isolate,
90 Encoding encoding,
91 ElapsedTimer* timer,
92 int repeat)
93 : stream_(NULL) {
94 int length = 0;
95 source_ = ReadFile(fname, isolate, &length, repeat);
96 unicode_cache_ = new UnicodeCache();
97 scanner_ = new Scanner(unicode_cache_);
98 switch (encoding) {
99 case UTF8:
100 stream_ = new Utf8ToUtf16CharacterStream(source_, length);
101 break;
102 case UTF16: {
103 Handle<String> result = isolate->factory()->NewStringFromTwoByte(
104 Vector<const uint16_t>(
105 reinterpret_cast<const uint16_t*>(source_),
106 length / 2));
107 stream_ =
108 new GenericStringUtf16CharacterStream(result, 0, result->length());
109 break;
110 }
111 case LATIN1: {
112 Handle<String> result = isolate->factory()->NewStringFromOneByte(
113 Vector<const uint8_t>(source_, length));
114 stream_ =
115 new GenericStringUtf16CharacterStream(result, 0, result->length());
116 break;
117 }
118 }
119 timer->Start();
120 scanner_->Initialize(stream_);
121 }
122
123 ~BaselineScanner() {
124 delete scanner_;
125 delete stream_;
126 delete unicode_cache_;
127 delete[] source_;
128 }
129
130 Token::Value Next(int* beg_pos, int* end_pos) {
131 Token::Value res = scanner_->Next();
132 *beg_pos = scanner_->location().beg_pos;
133 *end_pos = scanner_->location().end_pos;
134 return res;
135 }
136
137 private:
138 UnicodeCache* unicode_cache_;
139 Scanner* scanner_;
140 const byte* source_;
141 BufferedUtf16CharacterStream* stream_;
142 };
143
144
145 struct TokenWithLocation {
146 Token::Value value;
147 size_t beg;
148 size_t end;
149 TokenWithLocation() : value(Token::ILLEGAL), beg(0), end(0) { }
150 TokenWithLocation(Token::Value value, size_t beg, size_t end) :
151 value(value), beg(beg), end(end) { }
152 bool operator==(const TokenWithLocation& other) {
153 return value == other.value && beg == other.beg && end == other.end;
154 }
155 bool operator!=(const TokenWithLocation& other) {
156 return !(*this == other);
157 }
158 void Print(const char* prefix) const {
159 printf("%s %11s at (%d, %d)\n",
160 prefix, Token::Name(value),
161 static_cast<int>(beg), static_cast<int>(end));
162 }
163 };
164
165
166 TimeDelta RunBaselineScanner(const char* fname,
167 Isolate* isolate,
168 Encoding encoding,
169 bool dump_tokens,
170 std::vector<TokenWithLocation>* tokens,
171 int repeat) {
172 ElapsedTimer timer;
173 BaselineScanner scanner(fname, isolate, encoding, &timer, repeat);
174 Token::Value token;
175 int beg, end;
176 do {
177 token = scanner.Next(&beg, &end);
178 if (dump_tokens) {
179 tokens->push_back(TokenWithLocation(token, beg, end));
180 }
181 } while (token != Token::EOS);
182 return timer.Elapsed();
183 }
184
185
186 void PrintTokens(const char* name,
187 const std::vector<TokenWithLocation>& tokens) {
188 printf("No of tokens: %d\n",
189 static_cast<int>(tokens.size()));
190 printf("%s:\n", name);
191 for (size_t i = 0; i < tokens.size(); ++i) {
192 tokens[i].Print("=>");
193 }
194 }
195
196
197 TimeDelta ProcessFile(
198 const char* fname,
199 Encoding encoding,
200 Isolate* isolate,
201 bool print_tokens,
202 int repeat) {
203 if (print_tokens) {
204 printf("Processing file %s\n", fname);
205 }
206 HandleScope handle_scope(isolate);
207 std::vector<TokenWithLocation> baseline_tokens;
208 TimeDelta baseline_time;
209 baseline_time = RunBaselineScanner(
210 fname, isolate, encoding, print_tokens,
211 &baseline_tokens, repeat);
212 if (print_tokens) {
213 PrintTokens("Baseline", baseline_tokens);
214 }
215 return baseline_time;
216 }
217
218
219 int main(int argc, char* argv[]) {
220 v8::V8::InitializeICU();
221 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
222 Encoding encoding = LATIN1;
223 bool print_tokens = false;
224 std::vector<std::string> fnames;
225 std::string benchmark;
226 int repeat = 1;
227 for (int i = 0; i < argc; ++i) {
228 if (strcmp(argv[i], "--latin1") == 0) {
229 encoding = LATIN1;
230 } else if (strcmp(argv[i], "--utf8") == 0) {
231 encoding = UTF8;
232 } else if (strcmp(argv[i], "--utf16") == 0) {
233 encoding = UTF16;
234 } else if (strcmp(argv[i], "--print-tokens") == 0) {
235 print_tokens = true;
236 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) {
237 benchmark = std::string(argv[i]).substr(12);
238 } else if (strncmp(argv[i], "--repeat=", 9) == 0) {
239 std::string repeat_str = std::string(argv[i]).substr(9);
240 repeat = atoi(repeat_str.c_str());
241 } else if (i > 0 && argv[i][0] != '-') {
242 fnames.push_back(std::string(argv[i]));
243 }
244 }
245 v8::Isolate* isolate = v8::Isolate::GetCurrent();
246 {
247 v8::HandleScope handle_scope(isolate);
248 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
249 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
250 ASSERT(!context.IsEmpty());
251 {
252 v8::Context::Scope scope(context);
253 Isolate* isolate = Isolate::Current();
254 double baseline_total = 0;
255 for (size_t i = 0; i < fnames.size(); i++) {
256 TimeDelta time;
257 time = ProcessFile(fnames[i].c_str(), encoding, isolate, print_tokens,
258 repeat);
259 baseline_total += time.InMillisecondsF();
260 }
261 if (benchmark.empty()) benchmark = "Baseline";
262 printf("%s(RunTime): %.f ms\n", benchmark.c_str(), baseline_total);
263 }
264 }
265 v8::V8::Dispose();
266 return 0;
267 }
OLDNEW
« no previous file with comments | « no previous file | tools/lexer-shell.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698