Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / v8 / tools / lexer-shell.cc
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 <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string>
33 #include <vector>
34 #include "v8.h"
35
36 #include "api.h"
37 #include "messages.h"
38 #include "platform.h"
39 #include "runtime.h"
40 #include "scanner-character-streams.h"
41 #include "scopeinfo.h"
42 #include "shell-utils.h"
43 #include "string-stream.h"
44 #include "scanner.h"
45
46
47 using namespace v8::internal;
48
49
50 class BaselineScanner {
51  public:
52   BaselineScanner(const char* fname,
53                   Isolate* isolate,
54                   Encoding encoding,
55                   ElapsedTimer* timer,
56                   int repeat)
57       : stream_(NULL) {
58     int length = 0;
59     source_ = ReadFileAndRepeat(fname, &length, repeat);
60     unicode_cache_ = new UnicodeCache();
61     scanner_ = new Scanner(unicode_cache_);
62     switch (encoding) {
63       case UTF8:
64         stream_ = new Utf8ToUtf16CharacterStream(source_, length);
65         break;
66       case UTF16: {
67         Handle<String> result = isolate->factory()->NewStringFromTwoByte(
68             Vector<const uint16_t>(
69                 reinterpret_cast<const uint16_t*>(source_),
70                 length / 2));
71         CHECK_NOT_EMPTY_HANDLE(isolate, result);
72         stream_ =
73             new GenericStringUtf16CharacterStream(result, 0, result->length());
74         break;
75       }
76       case LATIN1: {
77         Handle<String> result = isolate->factory()->NewStringFromOneByte(
78             Vector<const uint8_t>(source_, length));
79         CHECK_NOT_EMPTY_HANDLE(isolate, result);
80         stream_ =
81             new GenericStringUtf16CharacterStream(result, 0, result->length());
82         break;
83       }
84     }
85     timer->Start();
86     scanner_->Initialize(stream_);
87   }
88
89   ~BaselineScanner() {
90     delete scanner_;
91     delete stream_;
92     delete unicode_cache_;
93     delete[] source_;
94   }
95
96   Token::Value Next(int* beg_pos, int* end_pos) {
97     Token::Value res = scanner_->Next();
98     *beg_pos = scanner_->location().beg_pos;
99     *end_pos = scanner_->location().end_pos;
100     return res;
101   }
102
103  private:
104   UnicodeCache* unicode_cache_;
105   Scanner* scanner_;
106   const byte* source_;
107   BufferedUtf16CharacterStream* stream_;
108 };
109
110
111 struct TokenWithLocation {
112   Token::Value value;
113   size_t beg;
114   size_t end;
115   TokenWithLocation() : value(Token::ILLEGAL), beg(0), end(0) { }
116   TokenWithLocation(Token::Value value, size_t beg, size_t end) :
117       value(value), beg(beg), end(end) { }
118   bool operator==(const TokenWithLocation& other) {
119     return value == other.value && beg == other.beg && end == other.end;
120   }
121   bool operator!=(const TokenWithLocation& other) {
122     return !(*this == other);
123   }
124   void Print(const char* prefix) const {
125     printf("%s %11s at (%d, %d)\n",
126            prefix, Token::Name(value),
127            static_cast<int>(beg), static_cast<int>(end));
128   }
129 };
130
131
132 TimeDelta RunBaselineScanner(const char* fname,
133                              Isolate* isolate,
134                              Encoding encoding,
135                              bool dump_tokens,
136                              std::vector<TokenWithLocation>* tokens,
137                              int repeat) {
138   ElapsedTimer timer;
139   BaselineScanner scanner(fname, isolate, encoding, &timer, repeat);
140   Token::Value token;
141   int beg, end;
142   do {
143     token = scanner.Next(&beg, &end);
144     if (dump_tokens) {
145       tokens->push_back(TokenWithLocation(token, beg, end));
146     }
147   } while (token != Token::EOS);
148   return timer.Elapsed();
149 }
150
151
152 void PrintTokens(const char* name,
153                  const std::vector<TokenWithLocation>& tokens) {
154   printf("No of tokens: %d\n",
155          static_cast<int>(tokens.size()));
156   printf("%s:\n", name);
157   for (size_t i = 0; i < tokens.size(); ++i) {
158     tokens[i].Print("=>");
159   }
160 }
161
162
163 TimeDelta ProcessFile(
164     const char* fname,
165     Encoding encoding,
166     Isolate* isolate,
167     bool print_tokens,
168     int repeat) {
169   if (print_tokens) {
170     printf("Processing file %s\n", fname);
171   }
172   HandleScope handle_scope(isolate);
173   std::vector<TokenWithLocation> baseline_tokens;
174   TimeDelta baseline_time;
175   baseline_time = RunBaselineScanner(
176       fname, isolate, encoding, print_tokens,
177       &baseline_tokens, repeat);
178   if (print_tokens) {
179     PrintTokens("Baseline", baseline_tokens);
180   }
181   return baseline_time;
182 }
183
184
185 int main(int argc, char* argv[]) {
186   v8::V8::InitializeICU();
187   v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
188   Encoding encoding = LATIN1;
189   bool print_tokens = false;
190   std::vector<std::string> fnames;
191   std::string benchmark;
192   int repeat = 1;
193   for (int i = 0; i < argc; ++i) {
194     if (strcmp(argv[i], "--latin1") == 0) {
195       encoding = LATIN1;
196     } else if (strcmp(argv[i], "--utf8") == 0) {
197       encoding = UTF8;
198     } else if (strcmp(argv[i], "--utf16") == 0) {
199       encoding = UTF16;
200     } else if (strcmp(argv[i], "--print-tokens") == 0) {
201       print_tokens = true;
202     } else if (strncmp(argv[i], "--benchmark=", 12) == 0) {
203       benchmark = std::string(argv[i]).substr(12);
204     } else if (strncmp(argv[i], "--repeat=", 9) == 0) {
205       std::string repeat_str = std::string(argv[i]).substr(9);
206       repeat = atoi(repeat_str.c_str());
207     } else if (i > 0 && argv[i][0] != '-') {
208       fnames.push_back(std::string(argv[i]));
209     }
210   }
211   v8::Isolate* isolate = v8::Isolate::GetCurrent();
212   {
213     v8::HandleScope handle_scope(isolate);
214     v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
215     v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
216     ASSERT(!context.IsEmpty());
217     {
218       v8::Context::Scope scope(context);
219       Isolate* isolate = Isolate::Current();
220       double baseline_total = 0;
221       for (size_t i = 0; i < fnames.size(); i++) {
222         TimeDelta time;
223         time = ProcessFile(fnames[i].c_str(), encoding, isolate, print_tokens,
224                            repeat);
225         baseline_total += time.InMillisecondsF();
226       }
227       if (benchmark.empty()) benchmark = "Baseline";
228       printf("%s(RunTime): %.f ms\n", benchmark.c_str(), baseline_total);
229     }
230   }
231   v8::V8::Dispose();
232   return 0;
233 }