Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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 | #ifndef V8_PARSER_H_ |
| 29 | #define V8_PARSER_H_ |
| 30 | |
| 31 | #include "scanner.h" |
| 32 | #include "allocation.h" |
| 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
| 37 | |
| 38 | class ParserMessage : public Malloced { |
| 39 | public: |
| 40 | ParserMessage(Scanner::Location loc, const char* message, |
| 41 | Vector<const char*> args) |
| 42 | : loc_(loc), |
| 43 | message_(message), |
| 44 | args_(args) { } |
| 45 | ~ParserMessage(); |
| 46 | Scanner::Location location() { return loc_; } |
| 47 | const char* message() { return message_; } |
| 48 | Vector<const char*> args() { return args_; } |
| 49 | private: |
| 50 | Scanner::Location loc_; |
| 51 | const char* message_; |
| 52 | Vector<const char*> args_; |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | class FunctionEntry BASE_EMBEDDED { |
| 57 | public: |
| 58 | explicit FunctionEntry(Vector<unsigned> backing) : backing_(backing) { } |
| 59 | FunctionEntry() : backing_(Vector<unsigned>::empty()) { } |
| 60 | |
| 61 | int start_pos() { return backing_[kStartPosOffset]; } |
| 62 | void set_start_pos(int value) { backing_[kStartPosOffset] = value; } |
| 63 | |
| 64 | int end_pos() { return backing_[kEndPosOffset]; } |
| 65 | void set_end_pos(int value) { backing_[kEndPosOffset] = value; } |
| 66 | |
| 67 | int literal_count() { return backing_[kLiteralCountOffset]; } |
| 68 | void set_literal_count(int value) { backing_[kLiteralCountOffset] = value; } |
| 69 | |
| 70 | int property_count() { return backing_[kPropertyCountOffset]; } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 71 | void set_property_count(int value) { |
| 72 | backing_[kPropertyCountOffset] = value; |
| 73 | } |
| 74 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 75 | int predata_function_skip() { return backing_[kPredataFunctionSkipOffset]; } |
| 76 | void set_predata_function_skip(int value) { |
| 77 | backing_[kPredataFunctionSkipOffset] = value; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 78 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 80 | int predata_symbol_skip() { return backing_[kPredataSymbolSkipOffset]; } |
| 81 | void set_predata_symbol_skip(int value) { |
| 82 | backing_[kPredataSymbolSkipOffset] = value; |
| 83 | } |
| 84 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | bool is_valid() { return backing_.length() > 0; } |
| 86 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 87 | static const int kSize = 6; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | Vector<unsigned> backing_; |
| 91 | static const int kStartPosOffset = 0; |
| 92 | static const int kEndPosOffset = 1; |
| 93 | static const int kLiteralCountOffset = 2; |
| 94 | static const int kPropertyCountOffset = 3; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 95 | static const int kPredataFunctionSkipOffset = 4; |
| 96 | static const int kPredataSymbolSkipOffset = 5; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | |
| 100 | class ScriptDataImpl : public ScriptData { |
| 101 | public: |
| 102 | explicit ScriptDataImpl(Vector<unsigned> store) |
| 103 | : store_(store), |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 104 | owns_store_(true) { } |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 105 | |
| 106 | // Create an empty ScriptDataImpl that is guaranteed to not satisfy |
| 107 | // a SanityCheck. |
| 108 | ScriptDataImpl() : store_(Vector<unsigned>()), owns_store_(false) { } |
| 109 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | virtual ~ScriptDataImpl(); |
| 111 | virtual int Length(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 112 | virtual const char* Data(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 113 | virtual bool HasError(); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 114 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 115 | void Initialize(); |
| 116 | void ReadNextSymbolPosition(); |
| 117 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 118 | FunctionEntry GetFunctionEntry(int start); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 119 | int GetSymbolIdentifier(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 120 | void SkipFunctionEntry(int start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | bool SanityCheck(); |
| 122 | |
| 123 | Scanner::Location MessageLocation(); |
| 124 | const char* BuildMessage(); |
| 125 | Vector<const char*> BuildArgs(); |
| 126 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 127 | int symbol_count() { |
| 128 | return (store_.length() > kHeaderSize) ? store_[kSymbolCountOffset] : 0; |
| 129 | } |
| 130 | // The following functions should only be called if SanityCheck has |
| 131 | // returned true. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | bool has_error() { return store_[kHasErrorOffset]; } |
| 133 | unsigned magic() { return store_[kMagicOffset]; } |
| 134 | unsigned version() { return store_[kVersionOffset]; } |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 135 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 136 | // Skip forward in the preparser data by the given number |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 137 | // of unsigned ints of function entries and the given number of bytes of |
| 138 | // symbol id encoding. |
| 139 | void Skip(int function_entries, int symbol_entries) { |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 140 | ASSERT(function_entries >= 0); |
| 141 | ASSERT(function_entries |
| 142 | <= (static_cast<int>(store_[kFunctionsSizeOffset]) |
| 143 | - (function_index_ - kHeaderSize))); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 144 | ASSERT(symbol_entries >= 0); |
| 145 | ASSERT(symbol_entries <= symbol_data_end_ - symbol_data_); |
| 146 | |
| 147 | unsigned max_function_skip = store_[kFunctionsSizeOffset] - |
| 148 | static_cast<unsigned>(function_index_ - kHeaderSize); |
| 149 | function_index_ += |
| 150 | Min(static_cast<unsigned>(function_entries), max_function_skip); |
| 151 | symbol_data_ += |
| 152 | Min(static_cast<unsigned>(symbol_entries), |
| 153 | static_cast<unsigned>(symbol_data_end_ - symbol_data_)); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 154 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | |
| 156 | static const unsigned kMagicNumber = 0xBadDead; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 157 | static const unsigned kCurrentVersion = 3; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 159 | static const int kMagicOffset = 0; |
| 160 | static const int kVersionOffset = 1; |
| 161 | static const int kHasErrorOffset = 2; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 162 | static const int kFunctionsSizeOffset = 3; |
| 163 | static const int kSymbolCountOffset = 4; |
| 164 | static const int kSizeOffset = 5; |
| 165 | static const int kHeaderSize = 6; |
| 166 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 167 | // If encoding a message, the following positions are fixed. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 168 | static const int kMessageStartPos = 0; |
| 169 | static const int kMessageEndPos = 1; |
| 170 | static const int kMessageArgCountPos = 2; |
| 171 | static const int kMessageTextPos = 3; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 173 | static const byte kNumberTerminator = 0x80u; |
| 174 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | private: |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 176 | Vector<unsigned> store_; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 177 | unsigned char* symbol_data_; |
| 178 | unsigned char* symbol_data_end_; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 179 | int function_index_; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 180 | bool owns_store_; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 181 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | unsigned Read(int position); |
| 183 | unsigned* ReadAddress(int position); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 184 | // Reads a number from the current symbols |
| 185 | int ReadNumber(byte** source); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 186 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 187 | ScriptDataImpl(const char* backing_store, int length) |
| 188 | : store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)), |
| 189 | length / sizeof(unsigned)), |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 190 | owns_store_(false) { |
| 191 | ASSERT_EQ(0, reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 194 | // Read strings written by ParserRecorder::WriteString. |
| 195 | static const char* ReadString(unsigned* start, int* chars); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 196 | |
| 197 | friend class ScriptData; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | |
| 201 | // The parser: Takes a script and and context information, and builds a |
| 202 | // FunctionLiteral AST node. Returns NULL and deallocates any allocated |
| 203 | // AST nodes if parsing failed. |
| 204 | FunctionLiteral* MakeAST(bool compile_in_global_context, |
| 205 | Handle<Script> script, |
| 206 | v8::Extension* extension, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 207 | ScriptDataImpl* pre_data, |
| 208 | bool is_json = false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 209 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 210 | // Generic preparser generating full preparse data. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | ScriptDataImpl* PreParse(Handle<String> source, |
| 212 | unibrow::CharacterStream* stream, |
| 213 | v8::Extension* extension); |
| 214 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 215 | // Preparser that only does preprocessing that makes sense if only used |
| 216 | // immediately after. |
| 217 | ScriptDataImpl* PartialPreParse(Handle<String> source, |
| 218 | unibrow::CharacterStream* stream, |
| 219 | v8::Extension* extension); |
| 220 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | |
| 222 | bool ParseRegExp(FlatStringReader* input, |
| 223 | bool multiline, |
| 224 | RegExpCompileData* result); |
| 225 | |
| 226 | |
| 227 | // Support for doing lazy compilation. The script is the script containing full |
| 228 | // source of the script where the function is declared. The start_position and |
| 229 | // end_position specifies the part of the script source which has the source |
| 230 | // for the function declaration in the form: |
| 231 | // |
| 232 | // (<formal parameters>) { <function body> } |
| 233 | // |
| 234 | // without any function keyword or name. |
| 235 | // |
| 236 | FunctionLiteral* MakeLazyAST(Handle<Script> script, |
| 237 | Handle<String> name, |
| 238 | int start_position, |
| 239 | int end_position, |
| 240 | bool is_expression); |
| 241 | |
| 242 | |
| 243 | // Support for handling complex values (array and object literals) that |
| 244 | // can be fully handled at compile time. |
| 245 | class CompileTimeValue: public AllStatic { |
| 246 | public: |
| 247 | enum Type { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 248 | OBJECT_LITERAL_FAST_ELEMENTS, |
| 249 | OBJECT_LITERAL_SLOW_ELEMENTS, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | ARRAY_LITERAL |
| 251 | }; |
| 252 | |
| 253 | static bool IsCompileTimeValue(Expression* expression); |
| 254 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 255 | static bool ArrayLiteralElementNeedsInitialization(Expression* value); |
| 256 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 257 | // Get the value as a compile time value. |
| 258 | static Handle<FixedArray> GetValue(Expression* expression); |
| 259 | |
| 260 | // Get the type of a compile time value returned by GetValue(). |
| 261 | static Type GetType(Handle<FixedArray> value); |
| 262 | |
| 263 | // Get the elements array of a compile time value returned by GetValue(). |
| 264 | static Handle<FixedArray> GetElements(Handle<FixedArray> value); |
| 265 | |
| 266 | private: |
| 267 | static const int kTypeSlot = 0; |
| 268 | static const int kElementsSlot = 1; |
| 269 | |
| 270 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
| 271 | }; |
| 272 | |
| 273 | |
| 274 | } } // namespace v8::internal |
| 275 | |
| 276 | #endif // V8_PARSER_H_ |