kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 "v8.h" |
| 29 | |
| 30 | #include "ast.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 31 | #include "jump-target-inl.h" |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 32 | #include "parser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "scopes.h" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 34 | #include "string-stream.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 36 | namespace v8 { |
| 37 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 39 | unsigned AstNode::current_id_ = 0; |
| 40 | unsigned AstNode::count_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | VariableProxySentinel VariableProxySentinel::this_proxy_(true); |
| 42 | VariableProxySentinel VariableProxySentinel::identifier_proxy_(false); |
| 43 | ValidLeftHandSideSentinel ValidLeftHandSideSentinel::instance_; |
| 44 | Property Property::this_property_(VariableProxySentinel::this_proxy(), NULL, 0); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 45 | Call Call::sentinel_(NULL, NULL, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | // All the Accept member functions for each syntax tree node type. |
| 50 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 51 | void Slot::Accept(AstVisitor* v) { v->VisitSlot(this); } |
| 52 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 53 | #define DECL_ACCEPT(type) \ |
| 54 | void type::Accept(AstVisitor* v) { v->Visit##type(this); } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 55 | AST_NODE_LIST(DECL_ACCEPT) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | #undef DECL_ACCEPT |
| 57 | |
| 58 | |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | // Implementation of other node functionality. |
| 61 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 62 | Assignment* ExpressionStatement::StatementAsSimpleAssignment() { |
| 63 | return (expression()->AsAssignment() != NULL && |
| 64 | !expression()->AsAssignment()->is_compound()) |
| 65 | ? expression()->AsAssignment() |
| 66 | : NULL; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | CountOperation* ExpressionStatement::StatementAsCountOperation() { |
| 71 | return expression()->AsCountOperation(); |
| 72 | } |
| 73 | |
| 74 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 75 | VariableProxy::VariableProxy(Variable* var) |
| 76 | : name_(var->name()), |
| 77 | var_(NULL), // Will be set by the call to BindTo. |
| 78 | is_this_(var->is_this()), |
| 79 | inside_with_(false), |
| 80 | is_trivial_(false) { |
| 81 | BindTo(var); |
| 82 | } |
| 83 | |
| 84 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | VariableProxy::VariableProxy(Handle<String> name, |
| 86 | bool is_this, |
| 87 | bool inside_with) |
| 88 | : name_(name), |
| 89 | var_(NULL), |
| 90 | is_this_(is_this), |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 91 | inside_with_(inside_with), |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 92 | is_trivial_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | // names must be canonicalized for fast equality checks |
| 94 | ASSERT(name->IsSymbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
| 98 | VariableProxy::VariableProxy(bool is_this) |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 99 | : var_(NULL), |
| 100 | is_this_(is_this), |
| 101 | inside_with_(false), |
| 102 | is_trivial_(false) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | void VariableProxy::BindTo(Variable* var) { |
| 107 | ASSERT(var_ == NULL); // must be bound only once |
| 108 | ASSERT(var != NULL); // must bind |
| 109 | ASSERT((is_this() && var->is_this()) || name_.is_identical_to(var->name())); |
| 110 | // Ideally CONST-ness should match. However, this is very hard to achieve |
| 111 | // because we don't know the exact semantics of conflicting (const and |
| 112 | // non-const) multiple variable declarations, const vars introduced via |
| 113 | // eval() etc. Const-ness and variable declarations are a complete mess |
| 114 | // in JS. Sigh... |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | var_ = var; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 116 | var->set_is_used(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 120 | Assignment::Assignment(Token::Value op, |
| 121 | Expression* target, |
| 122 | Expression* value, |
| 123 | int pos) |
| 124 | : op_(op), |
| 125 | target_(target), |
| 126 | value_(value), |
| 127 | pos_(pos), |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 128 | binary_operation_(NULL), |
| 129 | compound_load_id_(kNoNumber), |
| 130 | assignment_id_(GetNextId()), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 131 | block_start_(false), |
| 132 | block_end_(false), |
| 133 | is_monomorphic_(false), |
| 134 | receiver_types_(NULL) { |
| 135 | ASSERT(Token::IsAssignmentOp(op)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 136 | if (is_compound()) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 137 | binary_operation_ = |
| 138 | new BinaryOperation(binary_op(), target, value, pos + 1); |
| 139 | compound_load_id_ = GetNextId(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | Token::Value Assignment::binary_op() const { |
| 145 | switch (op_) { |
| 146 | case Token::ASSIGN_BIT_OR: return Token::BIT_OR; |
| 147 | case Token::ASSIGN_BIT_XOR: return Token::BIT_XOR; |
| 148 | case Token::ASSIGN_BIT_AND: return Token::BIT_AND; |
| 149 | case Token::ASSIGN_SHL: return Token::SHL; |
| 150 | case Token::ASSIGN_SAR: return Token::SAR; |
| 151 | case Token::ASSIGN_SHR: return Token::SHR; |
| 152 | case Token::ASSIGN_ADD: return Token::ADD; |
| 153 | case Token::ASSIGN_SUB: return Token::SUB; |
| 154 | case Token::ASSIGN_MUL: return Token::MUL; |
| 155 | case Token::ASSIGN_DIV: return Token::DIV; |
| 156 | case Token::ASSIGN_MOD: return Token::MOD; |
| 157 | default: UNREACHABLE(); |
| 158 | } |
| 159 | return Token::ILLEGAL; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | bool FunctionLiteral::AllowsLazyCompilation() { |
| 164 | return scope()->AllowsLazyCompilation(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | ObjectLiteral::Property::Property(Literal* key, Expression* value) { |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 169 | emit_store_ = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | key_ = key; |
| 171 | value_ = value; |
| 172 | Object* k = *key->handle(); |
| 173 | if (k->IsSymbol() && Heap::Proto_symbol()->Equals(String::cast(k))) { |
| 174 | kind_ = PROTOTYPE; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 175 | } else if (value_->AsMaterializedLiteral() != NULL) { |
| 176 | kind_ = MATERIALIZED_LITERAL; |
| 177 | } else if (value_->AsLiteral() != NULL) { |
| 178 | kind_ = CONSTANT; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 180 | kind_ = COMPUTED; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | ObjectLiteral::Property::Property(bool is_getter, FunctionLiteral* value) { |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 186 | emit_store_ = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | key_ = new Literal(value->name()); |
| 188 | value_ = value; |
| 189 | kind_ = is_getter ? GETTER : SETTER; |
| 190 | } |
| 191 | |
| 192 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 193 | bool ObjectLiteral::Property::IsCompileTimeValue() { |
| 194 | return kind_ == CONSTANT || |
| 195 | (kind_ == MATERIALIZED_LITERAL && |
| 196 | CompileTimeValue::IsCompileTimeValue(value_)); |
| 197 | } |
| 198 | |
| 199 | |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 200 | void ObjectLiteral::Property::set_emit_store(bool emit_store) { |
| 201 | emit_store_ = emit_store; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | bool ObjectLiteral::Property::emit_store() { |
| 206 | return emit_store_; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | bool IsEqualString(void* first, void* second) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 211 | ASSERT((*reinterpret_cast<String**>(first))->IsString()); |
| 212 | ASSERT((*reinterpret_cast<String**>(second))->IsString()); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 213 | Handle<String> h1(reinterpret_cast<String**>(first)); |
| 214 | Handle<String> h2(reinterpret_cast<String**>(second)); |
| 215 | return (*h1)->Equals(*h2); |
| 216 | } |
| 217 | |
| 218 | bool IsEqualSmi(void* first, void* second) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 219 | ASSERT((*reinterpret_cast<Smi**>(first))->IsSmi()); |
| 220 | ASSERT((*reinterpret_cast<Smi**>(second))->IsSmi()); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 221 | Handle<Smi> h1(reinterpret_cast<Smi**>(first)); |
| 222 | Handle<Smi> h2(reinterpret_cast<Smi**>(second)); |
| 223 | return (*h1)->value() == (*h2)->value(); |
| 224 | } |
| 225 | |
| 226 | void ObjectLiteral::CalculateEmitStore() { |
| 227 | HashMap properties(&IsEqualString); |
| 228 | HashMap elements(&IsEqualSmi); |
| 229 | for (int i = this->properties()->length() - 1; i >= 0; i--) { |
| 230 | ObjectLiteral::Property* property = this->properties()->at(i); |
| 231 | Literal* literal = property->key(); |
| 232 | Handle<Object> handle = literal->handle(); |
| 233 | |
| 234 | if (handle->IsNull()) { |
| 235 | continue; |
| 236 | } |
| 237 | |
| 238 | uint32_t hash; |
| 239 | HashMap* table; |
| 240 | void* key; |
| 241 | uint32_t index; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame^] | 242 | Smi* smi_key_location; |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 243 | if (handle->IsSymbol()) { |
| 244 | Handle<String> name(String::cast(*handle)); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame^] | 245 | if (name->AsArrayIndex(&index)) { |
| 246 | smi_key_location = Smi::FromInt(index); |
| 247 | key = &smi_key_location; |
| 248 | hash = index; |
| 249 | table = &elements; |
| 250 | } else { |
| 251 | key = name.location(); |
| 252 | hash = name->Hash(); |
| 253 | table = &properties; |
| 254 | } |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 255 | } else if (handle->ToArrayIndex(&index)) { |
| 256 | key = handle.location(); |
| 257 | hash = index; |
| 258 | table = &elements; |
| 259 | } else { |
| 260 | ASSERT(handle->IsNumber()); |
| 261 | double num = handle->Number(); |
| 262 | char arr[100]; |
| 263 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 264 | const char* str = DoubleToCString(num, buffer); |
| 265 | Handle<String> name = Factory::NewStringFromAscii(CStrVector(str)); |
| 266 | key = name.location(); |
| 267 | hash = name->Hash(); |
| 268 | table = &properties; |
| 269 | } |
| 270 | // If the key of a computed property is in the table, do not emit |
| 271 | // a store for the property later. |
| 272 | if (property->kind() == ObjectLiteral::Property::COMPUTED) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 273 | if (table->Lookup(key, hash, false) != NULL) { |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 274 | property->set_emit_store(false); |
| 275 | } |
| 276 | } |
| 277 | // Add key to the table. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 278 | table->Lookup(key, hash, true); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 283 | void TargetCollector::AddTarget(BreakTarget* target) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | // Add the label to the collector, but discard duplicates. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 285 | int length = targets_->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 286 | for (int i = 0; i < length; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 287 | if (targets_->at(i) == target) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 288 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 289 | targets_->Add(target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 293 | bool Expression::GuaranteedSmiResult() { |
| 294 | BinaryOperation* node = AsBinaryOperation(); |
| 295 | if (node == NULL) return false; |
| 296 | Token::Value op = node->op(); |
| 297 | switch (op) { |
| 298 | case Token::COMMA: |
| 299 | case Token::OR: |
| 300 | case Token::AND: |
| 301 | case Token::ADD: |
| 302 | case Token::SUB: |
| 303 | case Token::MUL: |
| 304 | case Token::DIV: |
| 305 | case Token::MOD: |
| 306 | case Token::BIT_XOR: |
| 307 | case Token::SHL: |
| 308 | return false; |
| 309 | break; |
| 310 | case Token::BIT_OR: |
| 311 | case Token::BIT_AND: { |
| 312 | Literal* left = node->left()->AsLiteral(); |
| 313 | Literal* right = node->right()->AsLiteral(); |
| 314 | if (left != NULL && left->handle()->IsSmi()) { |
| 315 | int value = Smi::cast(*left->handle())->value(); |
| 316 | if (op == Token::BIT_OR && ((value & 0xc0000000) == 0xc0000000)) { |
| 317 | // Result of bitwise or is always a negative Smi. |
| 318 | return true; |
| 319 | } |
| 320 | if (op == Token::BIT_AND && ((value & 0xc0000000) == 0)) { |
| 321 | // Result of bitwise and is always a positive Smi. |
| 322 | return true; |
| 323 | } |
| 324 | } |
| 325 | if (right != NULL && right->handle()->IsSmi()) { |
| 326 | int value = Smi::cast(*right->handle())->value(); |
| 327 | if (op == Token::BIT_OR && ((value & 0xc0000000) == 0xc0000000)) { |
| 328 | // Result of bitwise or is always a negative Smi. |
| 329 | return true; |
| 330 | } |
| 331 | if (op == Token::BIT_AND && ((value & 0xc0000000) == 0)) { |
| 332 | // Result of bitwise and is always a positive Smi. |
| 333 | return true; |
| 334 | } |
| 335 | } |
| 336 | return false; |
| 337 | break; |
| 338 | } |
| 339 | case Token::SAR: |
| 340 | case Token::SHR: { |
| 341 | Literal* right = node->right()->AsLiteral(); |
| 342 | if (right != NULL && right->handle()->IsSmi()) { |
| 343 | int value = Smi::cast(*right->handle())->value(); |
| 344 | if ((value & 0x1F) > 1 || |
| 345 | (op == Token::SAR && (value & 0x1F) == 1)) { |
| 346 | return true; |
| 347 | } |
| 348 | } |
| 349 | return false; |
| 350 | break; |
| 351 | } |
| 352 | default: |
| 353 | UNREACHABLE(); |
| 354 | break; |
| 355 | } |
| 356 | return false; |
| 357 | } |
| 358 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 359 | |
| 360 | void Expression::CopyAnalysisResultsFrom(Expression* other) { |
| 361 | bitfields_ = other->bitfields_; |
| 362 | type_ = other->type_; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | bool UnaryOperation::ResultOverwriteAllowed() { |
| 367 | switch (op_) { |
| 368 | case Token::BIT_NOT: |
| 369 | case Token::SUB: |
| 370 | return true; |
| 371 | default: |
| 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | |
| 377 | bool BinaryOperation::ResultOverwriteAllowed() { |
| 378 | switch (op_) { |
| 379 | case Token::COMMA: |
| 380 | case Token::OR: |
| 381 | case Token::AND: |
| 382 | return false; |
| 383 | case Token::BIT_OR: |
| 384 | case Token::BIT_XOR: |
| 385 | case Token::BIT_AND: |
| 386 | case Token::SHL: |
| 387 | case Token::SAR: |
| 388 | case Token::SHR: |
| 389 | case Token::ADD: |
| 390 | case Token::SUB: |
| 391 | case Token::MUL: |
| 392 | case Token::DIV: |
| 393 | case Token::MOD: |
| 394 | return true; |
| 395 | default: |
| 396 | UNREACHABLE(); |
| 397 | } |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | BinaryOperation::BinaryOperation(Assignment* assignment) { |
| 403 | ASSERT(assignment->is_compound()); |
| 404 | op_ = assignment->binary_op(); |
| 405 | left_ = assignment->target(); |
| 406 | right_ = assignment->value(); |
| 407 | pos_ = assignment->position(); |
| 408 | CopyAnalysisResultsFrom(assignment); |
| 409 | } |
| 410 | |
| 411 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 412 | // ---------------------------------------------------------------------------- |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 413 | // Inlining support |
| 414 | |
| 415 | bool Block::IsInlineable() const { |
| 416 | const int count = statements_.length(); |
| 417 | for (int i = 0; i < count; ++i) { |
| 418 | if (!statements_[i]->IsInlineable()) return false; |
| 419 | } |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | bool ExpressionStatement::IsInlineable() const { |
| 425 | return expression()->IsInlineable(); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | bool IfStatement::IsInlineable() const { |
| 430 | return condition()->IsInlineable() && then_statement()->IsInlineable() && |
| 431 | else_statement()->IsInlineable(); |
| 432 | } |
| 433 | |
| 434 | |
| 435 | bool ReturnStatement::IsInlineable() const { |
| 436 | return expression()->IsInlineable(); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | bool Conditional::IsInlineable() const { |
| 441 | return condition()->IsInlineable() && then_expression()->IsInlineable() && |
| 442 | else_expression()->IsInlineable(); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | bool VariableProxy::IsInlineable() const { |
| 447 | return var()->is_global() || var()->IsStackAllocated(); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | bool Assignment::IsInlineable() const { |
| 452 | return target()->IsInlineable() && value()->IsInlineable(); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | bool Property::IsInlineable() const { |
| 457 | return obj()->IsInlineable() && key()->IsInlineable(); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | bool Call::IsInlineable() const { |
| 462 | if (!expression()->IsInlineable()) return false; |
| 463 | const int count = arguments()->length(); |
| 464 | for (int i = 0; i < count; ++i) { |
| 465 | if (!arguments()->at(i)->IsInlineable()) return false; |
| 466 | } |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | bool CallNew::IsInlineable() const { |
| 472 | if (!expression()->IsInlineable()) return false; |
| 473 | const int count = arguments()->length(); |
| 474 | for (int i = 0; i < count; ++i) { |
| 475 | if (!arguments()->at(i)->IsInlineable()) return false; |
| 476 | } |
| 477 | return true; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | bool CallRuntime::IsInlineable() const { |
| 482 | const int count = arguments()->length(); |
| 483 | for (int i = 0; i < count; ++i) { |
| 484 | if (!arguments()->at(i)->IsInlineable()) return false; |
| 485 | } |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | bool UnaryOperation::IsInlineable() const { |
| 491 | return expression()->IsInlineable(); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | bool BinaryOperation::IsInlineable() const { |
| 496 | return left()->IsInlineable() && right()->IsInlineable(); |
| 497 | } |
| 498 | |
| 499 | |
| 500 | bool CompareOperation::IsInlineable() const { |
| 501 | return left()->IsInlineable() && right()->IsInlineable(); |
| 502 | } |
| 503 | |
| 504 | |
| 505 | bool CompareToNull::IsInlineable() const { |
| 506 | return expression()->IsInlineable(); |
| 507 | } |
| 508 | |
| 509 | |
| 510 | bool CountOperation::IsInlineable() const { |
| 511 | return expression()->IsInlineable(); |
| 512 | } |
| 513 | |
| 514 | |
| 515 | // ---------------------------------------------------------------------------- |
| 516 | // Recording of type feedback |
| 517 | |
| 518 | void Property::RecordTypeFeedback(TypeFeedbackOracle* oracle) { |
| 519 | // Record type feedback from the oracle in the AST. |
| 520 | is_monomorphic_ = oracle->LoadIsMonomorphic(this); |
| 521 | if (key()->IsPropertyName()) { |
| 522 | if (oracle->LoadIsBuiltin(this, Builtins::LoadIC_ArrayLength)) { |
| 523 | is_array_length_ = true; |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 524 | } else if (oracle->LoadIsBuiltin(this, |
| 525 | Builtins::LoadIC_FunctionPrototype)) { |
| 526 | is_function_prototype_ = true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 527 | } else { |
| 528 | Literal* lit_key = key()->AsLiteral(); |
| 529 | ASSERT(lit_key != NULL && lit_key->handle()->IsString()); |
| 530 | Handle<String> name = Handle<String>::cast(lit_key->handle()); |
| 531 | ZoneMapList* types = oracle->LoadReceiverTypes(this, name); |
| 532 | receiver_types_ = types; |
| 533 | } |
| 534 | } else if (is_monomorphic_) { |
| 535 | monomorphic_receiver_type_ = oracle->LoadMonomorphicReceiverType(this); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | |
| 540 | void Assignment::RecordTypeFeedback(TypeFeedbackOracle* oracle) { |
| 541 | Property* prop = target()->AsProperty(); |
| 542 | ASSERT(prop != NULL); |
| 543 | is_monomorphic_ = oracle->StoreIsMonomorphic(this); |
| 544 | if (prop->key()->IsPropertyName()) { |
| 545 | Literal* lit_key = prop->key()->AsLiteral(); |
| 546 | ASSERT(lit_key != NULL && lit_key->handle()->IsString()); |
| 547 | Handle<String> name = Handle<String>::cast(lit_key->handle()); |
| 548 | ZoneMapList* types = oracle->StoreReceiverTypes(this, name); |
| 549 | receiver_types_ = types; |
| 550 | } else if (is_monomorphic_) { |
| 551 | // Record receiver type for monomorphic keyed loads. |
| 552 | monomorphic_receiver_type_ = oracle->StoreMonomorphicReceiverType(this); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | |
| 557 | void CaseClause::RecordTypeFeedback(TypeFeedbackOracle* oracle) { |
| 558 | TypeInfo info = oracle->SwitchType(this); |
| 559 | if (info.IsSmi()) { |
| 560 | compare_type_ = SMI_ONLY; |
| 561 | } else if (info.IsNonPrimitive()) { |
| 562 | compare_type_ = OBJECT_ONLY; |
| 563 | } else { |
| 564 | ASSERT(compare_type_ == NONE); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 569 | static bool CanCallWithoutIC(Handle<JSFunction> target, int arity) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 570 | SharedFunctionInfo* info = target->shared(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 571 | // If the number of formal parameters of the target function does |
| 572 | // not match the number of arguments we're passing, we don't want to |
| 573 | // deal with it. Otherwise, we can call it directly. |
| 574 | return !target->NeedsArgumentsAdaption() || |
| 575 | info->formal_parameter_count() == arity; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
| 579 | bool Call::ComputeTarget(Handle<Map> type, Handle<String> name) { |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame^] | 580 | if (check_type_ == RECEIVER_MAP_CHECK) { |
| 581 | // For primitive checks the holder is set up to point to the |
| 582 | // corresponding prototype object, i.e. one step of the algorithm |
| 583 | // below has been already performed. |
| 584 | // For non-primitive checks we clear it to allow computing targets |
| 585 | // for polymorphic calls. |
| 586 | holder_ = Handle<JSObject>::null(); |
| 587 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 588 | while (true) { |
| 589 | LookupResult lookup; |
| 590 | type->LookupInDescriptors(NULL, *name, &lookup); |
| 591 | // If the function wasn't found directly in the map, we start |
| 592 | // looking upwards through the prototype chain. |
| 593 | if (!lookup.IsFound() && type->prototype()->IsJSObject()) { |
| 594 | holder_ = Handle<JSObject>(JSObject::cast(type->prototype())); |
| 595 | type = Handle<Map>(holder()->map()); |
| 596 | } else if (lookup.IsProperty() && lookup.type() == CONSTANT_FUNCTION) { |
| 597 | target_ = Handle<JSFunction>(lookup.GetConstantFunctionFromMap(*type)); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 598 | return CanCallWithoutIC(target_, arguments()->length()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 599 | } else { |
| 600 | return false; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | |
| 606 | bool Call::ComputeGlobalTarget(Handle<GlobalObject> global, |
| 607 | Handle<String> name) { |
| 608 | target_ = Handle<JSFunction>::null(); |
| 609 | cell_ = Handle<JSGlobalPropertyCell>::null(); |
| 610 | LookupResult lookup; |
| 611 | global->Lookup(*name, &lookup); |
| 612 | if (lookup.IsProperty() && lookup.type() == NORMAL) { |
| 613 | cell_ = Handle<JSGlobalPropertyCell>(global->GetPropertyCell(&lookup)); |
| 614 | if (cell_->value()->IsJSFunction()) { |
| 615 | Handle<JSFunction> candidate(JSFunction::cast(cell_->value())); |
| 616 | // If the function is in new space we assume it's more likely to |
| 617 | // change and thus prefer the general IC code. |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 618 | if (!Heap::InNewSpace(*candidate) && |
| 619 | CanCallWithoutIC(candidate, arguments()->length())) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 620 | target_ = candidate; |
| 621 | return true; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | void Call::RecordTypeFeedback(TypeFeedbackOracle* oracle) { |
| 630 | Property* property = expression()->AsProperty(); |
| 631 | ASSERT(property != NULL); |
| 632 | // Specialize for the receiver types seen at runtime. |
| 633 | Literal* key = property->key()->AsLiteral(); |
| 634 | ASSERT(key != NULL && key->handle()->IsString()); |
| 635 | Handle<String> name = Handle<String>::cast(key->handle()); |
| 636 | receiver_types_ = oracle->CallReceiverTypes(this, name); |
| 637 | #ifdef DEBUG |
| 638 | if (FLAG_enable_slow_asserts) { |
| 639 | if (receiver_types_ != NULL) { |
| 640 | int length = receiver_types_->length(); |
| 641 | for (int i = 0; i < length; i++) { |
| 642 | Handle<Map> map = receiver_types_->at(i); |
| 643 | ASSERT(!map.is_null() && *map != NULL); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | #endif |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 648 | is_monomorphic_ = oracle->CallIsMonomorphic(this); |
| 649 | check_type_ = oracle->GetCallCheckType(this); |
| 650 | if (is_monomorphic_) { |
| 651 | Handle<Map> map; |
| 652 | if (receiver_types_ != NULL && receiver_types_->length() > 0) { |
| 653 | ASSERT(check_type_ == RECEIVER_MAP_CHECK); |
| 654 | map = receiver_types_->at(0); |
| 655 | } else { |
| 656 | ASSERT(check_type_ != RECEIVER_MAP_CHECK); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame^] | 657 | holder_ = Handle<JSObject>( |
| 658 | oracle->GetPrototypeForPrimitiveCheck(check_type_)); |
| 659 | map = Handle<Map>(holder_->map()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 660 | } |
| 661 | is_monomorphic_ = ComputeTarget(map, name); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | |
| 666 | void BinaryOperation::RecordTypeFeedback(TypeFeedbackOracle* oracle) { |
| 667 | TypeInfo left = oracle->BinaryType(this, TypeFeedbackOracle::LEFT); |
| 668 | TypeInfo right = oracle->BinaryType(this, TypeFeedbackOracle::RIGHT); |
| 669 | is_smi_only_ = left.IsSmi() && right.IsSmi(); |
| 670 | } |
| 671 | |
| 672 | |
| 673 | void CompareOperation::RecordTypeFeedback(TypeFeedbackOracle* oracle) { |
| 674 | TypeInfo left = oracle->CompareType(this, TypeFeedbackOracle::LEFT); |
| 675 | TypeInfo right = oracle->CompareType(this, TypeFeedbackOracle::RIGHT); |
| 676 | if (left.IsSmi() && right.IsSmi()) { |
| 677 | compare_type_ = SMI_ONLY; |
| 678 | } else if (left.IsNonPrimitive() && right.IsNonPrimitive()) { |
| 679 | compare_type_ = OBJECT_ONLY; |
| 680 | } else { |
| 681 | ASSERT(compare_type_ == NONE); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | |
| 686 | // ---------------------------------------------------------------------------- |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 687 | // Implementation of AstVisitor |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 688 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 689 | bool AstVisitor::CheckStackOverflow() { |
| 690 | if (stack_overflow_) return true; |
| 691 | StackLimitCheck check; |
| 692 | if (!check.HasOverflowed()) return false; |
| 693 | return (stack_overflow_ = true); |
| 694 | } |
| 695 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 696 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 697 | void AstVisitor::VisitDeclarations(ZoneList<Declaration*>* declarations) { |
| 698 | for (int i = 0; i < declarations->length(); i++) { |
| 699 | Visit(declarations->at(i)); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 704 | void AstVisitor::VisitStatements(ZoneList<Statement*>* statements) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 705 | for (int i = 0; i < statements->length(); i++) { |
| 706 | Visit(statements->at(i)); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 711 | void AstVisitor::VisitExpressions(ZoneList<Expression*>* expressions) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 712 | for (int i = 0; i < expressions->length(); i++) { |
| 713 | // The variable statement visiting code may pass NULL expressions |
| 714 | // to this code. Maybe this should be handled by introducing an |
| 715 | // undefined expression or literal? Revisit this code if this |
| 716 | // changes |
| 717 | Expression* expression = expressions->at(i); |
| 718 | if (expression != NULL) Visit(expression); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 723 | // ---------------------------------------------------------------------------- |
| 724 | // Regular expressions |
| 725 | |
| 726 | #define MAKE_ACCEPT(Name) \ |
| 727 | void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \ |
| 728 | return visitor->Visit##Name(this, data); \ |
| 729 | } |
| 730 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT) |
| 731 | #undef MAKE_ACCEPT |
| 732 | |
| 733 | #define MAKE_TYPE_CASE(Name) \ |
| 734 | RegExp##Name* RegExpTree::As##Name() { \ |
| 735 | return NULL; \ |
| 736 | } \ |
| 737 | bool RegExpTree::Is##Name() { return false; } |
| 738 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE) |
| 739 | #undef MAKE_TYPE_CASE |
| 740 | |
| 741 | #define MAKE_TYPE_CASE(Name) \ |
| 742 | RegExp##Name* RegExp##Name::As##Name() { \ |
| 743 | return this; \ |
| 744 | } \ |
| 745 | bool RegExp##Name::Is##Name() { return true; } |
| 746 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE) |
| 747 | #undef MAKE_TYPE_CASE |
| 748 | |
| 749 | RegExpEmpty RegExpEmpty::kInstance; |
| 750 | |
| 751 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 752 | static Interval ListCaptureRegisters(ZoneList<RegExpTree*>* children) { |
| 753 | Interval result = Interval::Empty(); |
| 754 | for (int i = 0; i < children->length(); i++) |
| 755 | result = result.Union(children->at(i)->CaptureRegisters()); |
| 756 | return result; |
| 757 | } |
| 758 | |
| 759 | |
| 760 | Interval RegExpAlternative::CaptureRegisters() { |
| 761 | return ListCaptureRegisters(nodes()); |
| 762 | } |
| 763 | |
| 764 | |
| 765 | Interval RegExpDisjunction::CaptureRegisters() { |
| 766 | return ListCaptureRegisters(alternatives()); |
| 767 | } |
| 768 | |
| 769 | |
| 770 | Interval RegExpLookahead::CaptureRegisters() { |
| 771 | return body()->CaptureRegisters(); |
| 772 | } |
| 773 | |
| 774 | |
| 775 | Interval RegExpCapture::CaptureRegisters() { |
| 776 | Interval self(StartRegister(index()), EndRegister(index())); |
| 777 | return self.Union(body()->CaptureRegisters()); |
| 778 | } |
| 779 | |
| 780 | |
| 781 | Interval RegExpQuantifier::CaptureRegisters() { |
| 782 | return body()->CaptureRegisters(); |
| 783 | } |
| 784 | |
| 785 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 786 | bool RegExpAssertion::IsAnchoredAtStart() { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 787 | return type() == RegExpAssertion::START_OF_INPUT; |
| 788 | } |
| 789 | |
| 790 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 791 | bool RegExpAssertion::IsAnchoredAtEnd() { |
| 792 | return type() == RegExpAssertion::END_OF_INPUT; |
| 793 | } |
| 794 | |
| 795 | |
| 796 | bool RegExpAlternative::IsAnchoredAtStart() { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 797 | ZoneList<RegExpTree*>* nodes = this->nodes(); |
| 798 | for (int i = 0; i < nodes->length(); i++) { |
| 799 | RegExpTree* node = nodes->at(i); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 800 | if (node->IsAnchoredAtStart()) { return true; } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 801 | if (node->max_match() > 0) { return false; } |
| 802 | } |
| 803 | return false; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 807 | bool RegExpAlternative::IsAnchoredAtEnd() { |
| 808 | ZoneList<RegExpTree*>* nodes = this->nodes(); |
| 809 | for (int i = nodes->length() - 1; i >= 0; i--) { |
| 810 | RegExpTree* node = nodes->at(i); |
| 811 | if (node->IsAnchoredAtEnd()) { return true; } |
| 812 | if (node->max_match() > 0) { return false; } |
| 813 | } |
| 814 | return false; |
| 815 | } |
| 816 | |
| 817 | |
| 818 | bool RegExpDisjunction::IsAnchoredAtStart() { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 819 | ZoneList<RegExpTree*>* alternatives = this->alternatives(); |
| 820 | for (int i = 0; i < alternatives->length(); i++) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 821 | if (!alternatives->at(i)->IsAnchoredAtStart()) |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 822 | return false; |
| 823 | } |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 828 | bool RegExpDisjunction::IsAnchoredAtEnd() { |
| 829 | ZoneList<RegExpTree*>* alternatives = this->alternatives(); |
| 830 | for (int i = 0; i < alternatives->length(); i++) { |
| 831 | if (!alternatives->at(i)->IsAnchoredAtEnd()) |
| 832 | return false; |
| 833 | } |
| 834 | return true; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 838 | bool RegExpLookahead::IsAnchoredAtStart() { |
| 839 | return is_positive() && body()->IsAnchoredAtStart(); |
| 840 | } |
| 841 | |
| 842 | |
| 843 | bool RegExpCapture::IsAnchoredAtStart() { |
| 844 | return body()->IsAnchoredAtStart(); |
| 845 | } |
| 846 | |
| 847 | |
| 848 | bool RegExpCapture::IsAnchoredAtEnd() { |
| 849 | return body()->IsAnchoredAtEnd(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 853 | // Convert regular expression trees to a simple sexp representation. |
| 854 | // This representation should be different from the input grammar |
| 855 | // in as many cases as possible, to make it more difficult for incorrect |
| 856 | // parses to look as correct ones which is likely if the input and |
| 857 | // output formats are alike. |
| 858 | class RegExpUnparser: public RegExpVisitor { |
| 859 | public: |
| 860 | RegExpUnparser(); |
| 861 | void VisitCharacterRange(CharacterRange that); |
| 862 | SmartPointer<const char> ToString() { return stream_.ToCString(); } |
| 863 | #define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*, void* data); |
| 864 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE) |
| 865 | #undef MAKE_CASE |
| 866 | private: |
| 867 | StringStream* stream() { return &stream_; } |
| 868 | HeapStringAllocator alloc_; |
| 869 | StringStream stream_; |
| 870 | }; |
| 871 | |
| 872 | |
| 873 | RegExpUnparser::RegExpUnparser() : stream_(&alloc_) { |
| 874 | } |
| 875 | |
| 876 | |
| 877 | void* RegExpUnparser::VisitDisjunction(RegExpDisjunction* that, void* data) { |
| 878 | stream()->Add("(|"); |
| 879 | for (int i = 0; i < that->alternatives()->length(); i++) { |
| 880 | stream()->Add(" "); |
| 881 | that->alternatives()->at(i)->Accept(this, data); |
| 882 | } |
| 883 | stream()->Add(")"); |
| 884 | return NULL; |
| 885 | } |
| 886 | |
| 887 | |
| 888 | void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) { |
| 889 | stream()->Add("(:"); |
| 890 | for (int i = 0; i < that->nodes()->length(); i++) { |
| 891 | stream()->Add(" "); |
| 892 | that->nodes()->at(i)->Accept(this, data); |
| 893 | } |
| 894 | stream()->Add(")"); |
| 895 | return NULL; |
| 896 | } |
| 897 | |
| 898 | |
| 899 | void RegExpUnparser::VisitCharacterRange(CharacterRange that) { |
| 900 | stream()->Add("%k", that.from()); |
| 901 | if (!that.IsSingleton()) { |
| 902 | stream()->Add("-%k", that.to()); |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | |
| 907 | |
| 908 | void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that, |
| 909 | void* data) { |
| 910 | if (that->is_negated()) |
| 911 | stream()->Add("^"); |
| 912 | stream()->Add("["); |
| 913 | for (int i = 0; i < that->ranges()->length(); i++) { |
| 914 | if (i > 0) stream()->Add(" "); |
| 915 | VisitCharacterRange(that->ranges()->at(i)); |
| 916 | } |
| 917 | stream()->Add("]"); |
| 918 | return NULL; |
| 919 | } |
| 920 | |
| 921 | |
| 922 | void* RegExpUnparser::VisitAssertion(RegExpAssertion* that, void* data) { |
| 923 | switch (that->type()) { |
| 924 | case RegExpAssertion::START_OF_INPUT: |
| 925 | stream()->Add("@^i"); |
| 926 | break; |
| 927 | case RegExpAssertion::END_OF_INPUT: |
| 928 | stream()->Add("@$i"); |
| 929 | break; |
| 930 | case RegExpAssertion::START_OF_LINE: |
| 931 | stream()->Add("@^l"); |
| 932 | break; |
| 933 | case RegExpAssertion::END_OF_LINE: |
| 934 | stream()->Add("@$l"); |
| 935 | break; |
| 936 | case RegExpAssertion::BOUNDARY: |
| 937 | stream()->Add("@b"); |
| 938 | break; |
| 939 | case RegExpAssertion::NON_BOUNDARY: |
| 940 | stream()->Add("@B"); |
| 941 | break; |
| 942 | } |
| 943 | return NULL; |
| 944 | } |
| 945 | |
| 946 | |
| 947 | void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) { |
| 948 | stream()->Add("'"); |
| 949 | Vector<const uc16> chardata = that->data(); |
| 950 | for (int i = 0; i < chardata.length(); i++) { |
| 951 | stream()->Add("%k", chardata[i]); |
| 952 | } |
| 953 | stream()->Add("'"); |
| 954 | return NULL; |
| 955 | } |
| 956 | |
| 957 | |
| 958 | void* RegExpUnparser::VisitText(RegExpText* that, void* data) { |
| 959 | if (that->elements()->length() == 1) { |
| 960 | that->elements()->at(0).data.u_atom->Accept(this, data); |
| 961 | } else { |
| 962 | stream()->Add("(!"); |
| 963 | for (int i = 0; i < that->elements()->length(); i++) { |
| 964 | stream()->Add(" "); |
| 965 | that->elements()->at(i).data.u_atom->Accept(this, data); |
| 966 | } |
| 967 | stream()->Add(")"); |
| 968 | } |
| 969 | return NULL; |
| 970 | } |
| 971 | |
| 972 | |
| 973 | void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) { |
| 974 | stream()->Add("(# %i ", that->min()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 975 | if (that->max() == RegExpTree::kInfinity) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 976 | stream()->Add("- "); |
| 977 | } else { |
| 978 | stream()->Add("%i ", that->max()); |
| 979 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 980 | stream()->Add(that->is_greedy() ? "g " : that->is_possessive() ? "p " : "n "); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 981 | that->body()->Accept(this, data); |
| 982 | stream()->Add(")"); |
| 983 | return NULL; |
| 984 | } |
| 985 | |
| 986 | |
| 987 | void* RegExpUnparser::VisitCapture(RegExpCapture* that, void* data) { |
| 988 | stream()->Add("(^ "); |
| 989 | that->body()->Accept(this, data); |
| 990 | stream()->Add(")"); |
| 991 | return NULL; |
| 992 | } |
| 993 | |
| 994 | |
| 995 | void* RegExpUnparser::VisitLookahead(RegExpLookahead* that, void* data) { |
| 996 | stream()->Add("(-> "); |
| 997 | stream()->Add(that->is_positive() ? "+ " : "- "); |
| 998 | that->body()->Accept(this, data); |
| 999 | stream()->Add(")"); |
| 1000 | return NULL; |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | void* RegExpUnparser::VisitBackReference(RegExpBackReference* that, |
| 1005 | void* data) { |
| 1006 | stream()->Add("(<- %i)", that->index()); |
| 1007 | return NULL; |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | void* RegExpUnparser::VisitEmpty(RegExpEmpty* that, void* data) { |
| 1012 | stream()->Put('%'); |
| 1013 | return NULL; |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | SmartPointer<const char> RegExpTree::ToString() { |
| 1018 | RegExpUnparser unparser; |
| 1019 | Accept(&unparser, NULL); |
| 1020 | return unparser.ToString(); |
| 1021 | } |
| 1022 | |
| 1023 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1024 | RegExpDisjunction::RegExpDisjunction(ZoneList<RegExpTree*>* alternatives) |
| 1025 | : alternatives_(alternatives) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1026 | ASSERT(alternatives->length() > 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1027 | RegExpTree* first_alternative = alternatives->at(0); |
| 1028 | min_match_ = first_alternative->min_match(); |
| 1029 | max_match_ = first_alternative->max_match(); |
| 1030 | for (int i = 1; i < alternatives->length(); i++) { |
| 1031 | RegExpTree* alternative = alternatives->at(i); |
| 1032 | min_match_ = Min(min_match_, alternative->min_match()); |
| 1033 | max_match_ = Max(max_match_, alternative->max_match()); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | RegExpAlternative::RegExpAlternative(ZoneList<RegExpTree*>* nodes) |
| 1039 | : nodes_(nodes) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1040 | ASSERT(nodes->length() > 1); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1041 | min_match_ = 0; |
| 1042 | max_match_ = 0; |
| 1043 | for (int i = 0; i < nodes->length(); i++) { |
| 1044 | RegExpTree* node = nodes->at(i); |
| 1045 | min_match_ += node->min_match(); |
| 1046 | int node_max_match = node->max_match(); |
| 1047 | if (kInfinity - max_match_ < node_max_match) { |
| 1048 | max_match_ = kInfinity; |
| 1049 | } else { |
| 1050 | max_match_ += node->max_match(); |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1055 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1056 | CaseClause::CaseClause(Expression* label, |
| 1057 | ZoneList<Statement*>* statements, |
| 1058 | int pos) |
| 1059 | : label_(label), |
| 1060 | statements_(statements), |
| 1061 | position_(pos), |
| 1062 | compare_type_(NONE) {} |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1063 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1064 | } } // namespace v8::internal |