blob: 899489e25002d38ca6580586699b86eb03b3d13d [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002// 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
Ben Murdoch589d6972011-11-30 16:04:58 +000028#ifdef _MSC_VER
29#define V8_WIN32_LEAN_AND_MEAN
30#include "win32-headers.h"
31#endif
32
Ben Murdochb0fe1622011-05-05 13:52:32 +010033#include "../include/v8-preparser.h"
Steve Block44f0eee2011-05-26 01:26:41 +010034
Ben Murdochb0fe1622011-05-05 13:52:32 +010035#include "globals.h"
36#include "checks.h"
37#include "allocation.h"
38#include "utils.h"
39#include "list.h"
Ben Murdoch589d6972011-11-30 16:04:58 +000040#include "hashmap.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000041#include "preparse-data-format.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010042#include "preparse-data.h"
43#include "preparser.h"
44
45namespace v8 {
46namespace internal {
47
48// UTF16Buffer based on a v8::UnicodeInputStream.
Ben Murdoch85b71792012-04-11 18:30:58 +010049class InputStreamUTF16Buffer : public UC16CharacterStream {
Ben Murdochb0fe1622011-05-05 13:52:32 +010050 public:
Ben Murdoch85b71792012-04-11 18:30:58 +010051 /* The InputStreamUTF16Buffer maintains an internal buffer
52 * that is filled in chunks from the UC16CharacterStream.
Ben Murdochb0fe1622011-05-05 13:52:32 +010053 * It also maintains unlimited pushback capability, but optimized
54 * for small pushbacks.
55 * The pushback_buffer_ pointer points to the limit of pushbacks
56 * in the current buffer. There is room for a few pushback'ed chars before
57 * the buffer containing the most recently read chunk. If this is overflowed,
58 * an external buffer is allocated/reused to hold further pushbacks, and
59 * pushback_buffer_ and buffer_cursor_/buffer_end_ now points to the
60 * new buffer. When this buffer is read to the end again, the cursor is
61 * switched back to the internal buffer
62 */
Ben Murdoch85b71792012-04-11 18:30:58 +010063 explicit InputStreamUTF16Buffer(v8::UnicodeInputStream* stream)
64 : UC16CharacterStream(),
Ben Murdochb0fe1622011-05-05 13:52:32 +010065 stream_(stream),
66 pushback_buffer_(buffer_),
67 pushback_buffer_end_cache_(NULL),
68 pushback_buffer_backing_(NULL),
69 pushback_buffer_backing_size_(0) {
70 buffer_cursor_ = buffer_end_ = buffer_ + kPushBackSize;
71 }
72
Ben Murdoch85b71792012-04-11 18:30:58 +010073 virtual ~InputStreamUTF16Buffer() {
Ben Murdochb0fe1622011-05-05 13:52:32 +010074 if (pushback_buffer_backing_ != NULL) {
75 DeleteArray(pushback_buffer_backing_);
76 }
77 }
78
Ben Murdochb8e0da22011-05-16 14:20:40 +010079 virtual void PushBack(uc32 ch) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010080 ASSERT(pos_ > 0);
Ben Murdochb8e0da22011-05-16 14:20:40 +010081 if (ch == kEndOfInput) {
82 pos_--;
83 return;
84 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010085 if (buffer_cursor_ <= pushback_buffer_) {
86 // No more room in the current buffer to do pushbacks.
87 if (pushback_buffer_end_cache_ == NULL) {
88 // We have overflowed the pushback space at the beginning of buffer_.
89 // Switch to using a separate allocated pushback buffer.
90 if (pushback_buffer_backing_ == NULL) {
91 // Allocate a buffer the first time we need it.
92 pushback_buffer_backing_ = NewArray<uc16>(kPushBackSize);
93 pushback_buffer_backing_size_ = kPushBackSize;
94 }
95 pushback_buffer_ = pushback_buffer_backing_;
96 pushback_buffer_end_cache_ = buffer_end_;
97 buffer_end_ = pushback_buffer_backing_ + pushback_buffer_backing_size_;
98 buffer_cursor_ = buffer_end_ - 1;
99 } else {
100 // Hit the bottom of the allocated pushback buffer.
101 // Double the buffer and continue.
102 uc16* new_buffer = NewArray<uc16>(pushback_buffer_backing_size_ * 2);
103 memcpy(new_buffer + pushback_buffer_backing_size_,
104 pushback_buffer_backing_,
105 pushback_buffer_backing_size_);
106 DeleteArray(pushback_buffer_backing_);
107 buffer_cursor_ = new_buffer + pushback_buffer_backing_size_;
108 pushback_buffer_backing_ = pushback_buffer_ = new_buffer;
109 buffer_end_ = pushback_buffer_backing_ + pushback_buffer_backing_size_;
110 }
111 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100112 pushback_buffer_[buffer_cursor_ - pushback_buffer_- 1] =
113 static_cast<uc16>(ch);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100114 pos_--;
115 }
116
117 protected:
118 virtual bool ReadBlock() {
119 if (pushback_buffer_end_cache_ != NULL) {
120 buffer_cursor_ = buffer_;
121 buffer_end_ = pushback_buffer_end_cache_;
122 pushback_buffer_end_cache_ = NULL;
123 return buffer_end_ > buffer_cursor_;
124 }
125 // Copy the top of the buffer into the pushback area.
126 int32_t value;
127 uc16* buffer_start = buffer_ + kPushBackSize;
128 buffer_cursor_ = buffer_end_ = buffer_start;
129 while ((value = stream_->Next()) >= 0) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100130 if (value > static_cast<int32_t>(unibrow::Utf8::kMaxThreeByteChar)) {
131 value = unibrow::Utf8::kBadChar;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100132 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100133 // buffer_end_ is a const pointer, but buffer_ is writable.
134 buffer_start[buffer_end_++ - buffer_start] = static_cast<uc16>(value);
135 if (buffer_end_ == buffer_ + kPushBackSize + kBufferSize) break;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100136 }
137 return buffer_end_ > buffer_start;
138 }
139
140 virtual unsigned SlowSeekForward(unsigned pos) {
141 // Seeking in the input is not used by preparsing.
142 // It's only used by the real parser based on preparser data.
143 UNIMPLEMENTED();
144 return 0;
145 }
146
147 private:
148 static const unsigned kBufferSize = 512;
149 static const unsigned kPushBackSize = 16;
150 v8::UnicodeInputStream* const stream_;
151 // Buffer holding first kPushBackSize characters of pushback buffer,
152 // then kBufferSize chars of read-ahead.
153 // The pushback buffer is only used if pushing back characters past
154 // the start of a block.
155 uc16 buffer_[kPushBackSize + kBufferSize];
156 // Limit of pushbacks before new allocation is necessary.
157 uc16* pushback_buffer_;
158 // Only if that pushback buffer at the start of buffer_ isn't sufficient
159 // is the following used.
160 const uc16* pushback_buffer_end_cache_;
161 uc16* pushback_buffer_backing_;
162 unsigned pushback_buffer_backing_size_;
163};
164
165
Steve Block44f0eee2011-05-26 01:26:41 +0100166// Functions declared by allocation.h and implemented in both api.cc (for v8)
167// or here (for a stand-alone preparser).
Ben Murdochb0fe1622011-05-05 13:52:32 +0100168
169void FatalProcessOutOfMemory(const char* reason) {
170 V8_Fatal(__FILE__, __LINE__, reason);
171}
172
173bool EnableSlowAsserts() { return true; }
174
175} // namespace internal.
176
177
178UnicodeInputStream::~UnicodeInputStream() { }
179
180
181PreParserData Preparse(UnicodeInputStream* input, size_t max_stack) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100182 internal::InputStreamUTF16Buffer buffer(input);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100183 uintptr_t stack_limit = reinterpret_cast<uintptr_t>(&buffer) - max_stack;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100184 internal::UnicodeCache unicode_cache;
Ben Murdoch85b71792012-04-11 18:30:58 +0100185 internal::JavaScriptScanner scanner(&unicode_cache);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100186 scanner.Initialize(&buffer);
187 internal::CompleteParserRecorder recorder;
188 preparser::PreParser::PreParseResult result =
189 preparser::PreParser::PreParseProgram(&scanner,
190 &recorder,
Ben Murdoch85b71792012-04-11 18:30:58 +0100191 true,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100192 stack_limit);
193 if (result == preparser::PreParser::kPreParseStackOverflow) {
194 return PreParserData::StackOverflow();
195 }
196 internal::Vector<unsigned> pre_data = recorder.ExtractData();
197 size_t size = pre_data.length() * sizeof(pre_data[0]);
198 unsigned char* data = reinterpret_cast<unsigned char*>(pre_data.start());
199 return PreParserData(size, data);
200}
201
202} // namespace v8.
203
204
205// Used by ASSERT macros and other immediate exits.
206extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
207 exit(EXIT_FAILURE);
208}