2011-04-07 09:51:25 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "ast.h"
|
2009-10-28 13:25:40 +00:00
|
|
|
#include "parser.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "scopes.h"
|
2008-11-25 11:07:48 +00:00
|
|
|
#include "string-stream.h"
|
2011-04-07 14:42:37 +00:00
|
|
|
#include "type-info.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// All the Accept member functions for each syntax tree node type.
|
|
|
|
|
2010-03-30 12:25:58 +00:00
|
|
|
#define DECL_ACCEPT(type) \
|
|
|
|
void type::Accept(AstVisitor* v) { v->Visit##type(this); }
|
2009-07-30 12:09:05 +00:00
|
|
|
AST_NODE_LIST(DECL_ACCEPT)
|
2008-07-03 15:10:15 +00:00
|
|
|
#undef DECL_ACCEPT
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Implementation of other node functionality.
|
|
|
|
|
2011-10-31 11:11:26 +00:00
|
|
|
|
|
|
|
bool Expression::IsSmiLiteral() {
|
|
|
|
return AsLiteral() != NULL && AsLiteral()->handle()->IsSmi();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Expression::IsStringLiteral() {
|
|
|
|
return AsLiteral() != NULL && AsLiteral()->handle()->IsString();
|
2010-03-11 10:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-31 11:11:26 +00:00
|
|
|
bool Expression::IsNullLiteral() {
|
|
|
|
return AsLiteral() != NULL && AsLiteral()->handle()->IsNull();
|
2010-03-11 10:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-18 17:32:41 +00:00
|
|
|
VariableProxy::VariableProxy(Isolate* isolate, Variable* var)
|
|
|
|
: Expression(isolate),
|
|
|
|
name_(var->name()),
|
2010-09-24 07:53:59 +00:00
|
|
|
var_(NULL), // Will be set by the call to BindTo.
|
|
|
|
is_this_(var->is_this()),
|
2011-04-01 11:54:04 +00:00
|
|
|
is_trivial_(false),
|
|
|
|
position_(RelocInfo::kNoPosition) {
|
2010-09-24 07:53:59 +00:00
|
|
|
BindTo(var);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-18 17:32:41 +00:00
|
|
|
VariableProxy::VariableProxy(Isolate* isolate,
|
|
|
|
Handle<String> name,
|
2008-07-03 15:10:15 +00:00
|
|
|
bool is_this,
|
2011-04-01 11:54:04 +00:00
|
|
|
int position)
|
2011-07-18 17:32:41 +00:00
|
|
|
: Expression(isolate),
|
|
|
|
name_(name),
|
|
|
|
var_(NULL),
|
|
|
|
is_this_(is_this),
|
|
|
|
is_trivial_(false),
|
|
|
|
position_(position) {
|
2011-04-01 11:54:04 +00:00
|
|
|
// Names must be canonicalized for fast equality checks.
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(name->IsSymbol());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VariableProxy::BindTo(Variable* var) {
|
|
|
|
ASSERT(var_ == NULL); // must be bound only once
|
|
|
|
ASSERT(var != NULL); // must bind
|
|
|
|
ASSERT((is_this() && var->is_this()) || name_.is_identical_to(var->name()));
|
|
|
|
// Ideally CONST-ness should match. However, this is very hard to achieve
|
|
|
|
// because we don't know the exact semantics of conflicting (const and
|
|
|
|
// non-const) multiple variable declarations, const vars introduced via
|
|
|
|
// eval() etc. Const-ness and variable declarations are a complete mess
|
|
|
|
// in JS. Sigh...
|
|
|
|
var_ = var;
|
2010-03-08 13:01:24 +00:00
|
|
|
var->set_is_used(true);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-18 17:32:41 +00:00
|
|
|
Assignment::Assignment(Isolate* isolate,
|
|
|
|
Token::Value op,
|
2010-12-07 11:31:57 +00:00
|
|
|
Expression* target,
|
|
|
|
Expression* value,
|
|
|
|
int pos)
|
2011-07-18 17:32:41 +00:00
|
|
|
: Expression(isolate),
|
|
|
|
op_(op),
|
2010-12-07 11:31:57 +00:00
|
|
|
target_(target),
|
|
|
|
value_(value),
|
|
|
|
pos_(pos),
|
2010-12-13 16:29:47 +00:00
|
|
|
binary_operation_(NULL),
|
|
|
|
compound_load_id_(kNoNumber),
|
2011-07-18 17:32:41 +00:00
|
|
|
assignment_id_(GetNextId(isolate)),
|
2010-12-07 11:31:57 +00:00
|
|
|
block_start_(false),
|
|
|
|
block_end_(false),
|
2011-08-22 14:23:37 +00:00
|
|
|
is_monomorphic_(false) {
|
2010-12-07 11:31:57 +00:00
|
|
|
ASSERT(Token::IsAssignmentOp(op));
|
|
|
|
if (is_compound()) {
|
2010-12-13 16:29:47 +00:00
|
|
|
binary_operation_ =
|
2011-07-18 17:32:41 +00:00
|
|
|
new(isolate->zone()) BinaryOperation(isolate,
|
|
|
|
binary_op(),
|
|
|
|
target,
|
|
|
|
value,
|
|
|
|
pos + 1);
|
|
|
|
compound_load_id_ = GetNextId(isolate);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Token::Value Assignment::binary_op() const {
|
|
|
|
switch (op_) {
|
|
|
|
case Token::ASSIGN_BIT_OR: return Token::BIT_OR;
|
|
|
|
case Token::ASSIGN_BIT_XOR: return Token::BIT_XOR;
|
|
|
|
case Token::ASSIGN_BIT_AND: return Token::BIT_AND;
|
|
|
|
case Token::ASSIGN_SHL: return Token::SHL;
|
|
|
|
case Token::ASSIGN_SAR: return Token::SAR;
|
|
|
|
case Token::ASSIGN_SHR: return Token::SHR;
|
|
|
|
case Token::ASSIGN_ADD: return Token::ADD;
|
|
|
|
case Token::ASSIGN_SUB: return Token::SUB;
|
|
|
|
case Token::ASSIGN_MUL: return Token::MUL;
|
|
|
|
case Token::ASSIGN_DIV: return Token::DIV;
|
|
|
|
case Token::ASSIGN_MOD: return Token::MOD;
|
|
|
|
default: UNREACHABLE();
|
|
|
|
}
|
|
|
|
return Token::ILLEGAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FunctionLiteral::AllowsLazyCompilation() {
|
|
|
|
return scope()->AllowsLazyCompilation();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObjectLiteral::Property::Property(Literal* key, Expression* value) {
|
2010-10-27 11:37:59 +00:00
|
|
|
emit_store_ = true;
|
2008-07-03 15:10:15 +00:00
|
|
|
key_ = key;
|
|
|
|
value_ = value;
|
|
|
|
Object* k = *key->handle();
|
2011-03-18 20:35:07 +00:00
|
|
|
if (k->IsSymbol() && HEAP->Proto_symbol()->Equals(String::cast(k))) {
|
2008-07-03 15:10:15 +00:00
|
|
|
kind_ = PROTOTYPE;
|
2009-03-23 07:27:47 +00:00
|
|
|
} else if (value_->AsMaterializedLiteral() != NULL) {
|
|
|
|
kind_ = MATERIALIZED_LITERAL;
|
|
|
|
} else if (value_->AsLiteral() != NULL) {
|
|
|
|
kind_ = CONSTANT;
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2009-03-23 07:27:47 +00:00
|
|
|
kind_ = COMPUTED;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObjectLiteral::Property::Property(bool is_getter, FunctionLiteral* value) {
|
2011-07-18 17:32:41 +00:00
|
|
|
Isolate* isolate = Isolate::Current();
|
2010-10-27 11:37:59 +00:00
|
|
|
emit_store_ = true;
|
2011-07-18 17:32:41 +00:00
|
|
|
key_ = new(isolate->zone()) Literal(isolate, value->name());
|
2008-07-03 15:10:15 +00:00
|
|
|
value_ = value;
|
|
|
|
kind_ = is_getter ? GETTER : SETTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-28 13:25:40 +00:00
|
|
|
bool ObjectLiteral::Property::IsCompileTimeValue() {
|
|
|
|
return kind_ == CONSTANT ||
|
|
|
|
(kind_ == MATERIALIZED_LITERAL &&
|
|
|
|
CompileTimeValue::IsCompileTimeValue(value_));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-27 11:37:59 +00:00
|
|
|
void ObjectLiteral::Property::set_emit_store(bool emit_store) {
|
|
|
|
emit_store_ = emit_store;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ObjectLiteral::Property::emit_store() {
|
|
|
|
return emit_store_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IsEqualString(void* first, void* second) {
|
2011-01-12 09:29:20 +00:00
|
|
|
ASSERT((*reinterpret_cast<String**>(first))->IsString());
|
|
|
|
ASSERT((*reinterpret_cast<String**>(second))->IsString());
|
2010-10-27 11:37:59 +00:00
|
|
|
Handle<String> h1(reinterpret_cast<String**>(first));
|
|
|
|
Handle<String> h2(reinterpret_cast<String**>(second));
|
|
|
|
return (*h1)->Equals(*h2);
|
|
|
|
}
|
|
|
|
|
2011-02-02 14:02:58 +00:00
|
|
|
|
|
|
|
bool IsEqualNumber(void* first, void* second) {
|
|
|
|
ASSERT((*reinterpret_cast<Object**>(first))->IsNumber());
|
|
|
|
ASSERT((*reinterpret_cast<Object**>(second))->IsNumber());
|
|
|
|
|
|
|
|
Handle<Object> h1(reinterpret_cast<Object**>(first));
|
|
|
|
Handle<Object> h2(reinterpret_cast<Object**>(second));
|
|
|
|
if (h1->IsSmi()) {
|
|
|
|
return h2->IsSmi() && *h1 == *h2;
|
|
|
|
}
|
|
|
|
if (h2->IsSmi()) return false;
|
|
|
|
Handle<HeapNumber> n1 = Handle<HeapNumber>::cast(h1);
|
|
|
|
Handle<HeapNumber> n2 = Handle<HeapNumber>::cast(h2);
|
|
|
|
ASSERT(isfinite(n1->value()));
|
|
|
|
ASSERT(isfinite(n2->value()));
|
|
|
|
return n1->value() == n2->value();
|
2010-10-27 11:37:59 +00:00
|
|
|
}
|
|
|
|
|
2011-02-02 14:02:58 +00:00
|
|
|
|
2010-10-27 11:37:59 +00:00
|
|
|
void ObjectLiteral::CalculateEmitStore() {
|
|
|
|
HashMap properties(&IsEqualString);
|
2011-02-02 14:02:58 +00:00
|
|
|
HashMap elements(&IsEqualNumber);
|
2010-10-27 11:37:59 +00:00
|
|
|
for (int i = this->properties()->length() - 1; i >= 0; i--) {
|
|
|
|
ObjectLiteral::Property* property = this->properties()->at(i);
|
|
|
|
Literal* literal = property->key();
|
|
|
|
Handle<Object> handle = literal->handle();
|
|
|
|
|
|
|
|
if (handle->IsNull()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t hash;
|
|
|
|
HashMap* table;
|
|
|
|
void* key;
|
2011-03-18 20:35:07 +00:00
|
|
|
Factory* factory = Isolate::Current()->factory();
|
2010-10-27 11:37:59 +00:00
|
|
|
if (handle->IsSymbol()) {
|
|
|
|
Handle<String> name(String::cast(*handle));
|
2011-02-02 14:02:58 +00:00
|
|
|
if (name->AsArrayIndex(&hash)) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Object> key_handle = factory->NewNumberFromUint(hash);
|
2011-02-02 14:02:58 +00:00
|
|
|
key = key_handle.location();
|
2011-01-25 07:48:19 +00:00
|
|
|
table = &elements;
|
|
|
|
} else {
|
|
|
|
key = name.location();
|
|
|
|
hash = name->Hash();
|
|
|
|
table = &properties;
|
|
|
|
}
|
2011-02-02 14:02:58 +00:00
|
|
|
} else if (handle->ToArrayIndex(&hash)) {
|
2010-10-27 11:37:59 +00:00
|
|
|
key = handle.location();
|
|
|
|
table = &elements;
|
|
|
|
} else {
|
|
|
|
ASSERT(handle->IsNumber());
|
|
|
|
double num = handle->Number();
|
|
|
|
char arr[100];
|
|
|
|
Vector<char> buffer(arr, ARRAY_SIZE(arr));
|
|
|
|
const char* str = DoubleToCString(num, buffer);
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<String> name = factory->NewStringFromAscii(CStrVector(str));
|
2010-10-27 11:37:59 +00:00
|
|
|
key = name.location();
|
|
|
|
hash = name->Hash();
|
|
|
|
table = &properties;
|
|
|
|
}
|
|
|
|
// If the key of a computed property is in the table, do not emit
|
|
|
|
// a store for the property later.
|
|
|
|
if (property->kind() == ObjectLiteral::Property::COMPUTED) {
|
2011-01-12 09:29:20 +00:00
|
|
|
if (table->Lookup(key, hash, false) != NULL) {
|
2010-10-27 11:37:59 +00:00
|
|
|
property->set_emit_store(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add key to the table.
|
2011-01-12 09:29:20 +00:00
|
|
|
table->Lookup(key, hash, true);
|
2010-10-27 11:37:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-07 14:42:37 +00:00
|
|
|
void TargetCollector::AddTarget(Label* target) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Add the label to the collector, but discard duplicates.
|
2011-06-08 13:55:33 +00:00
|
|
|
int length = targets_.length();
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
2011-06-08 13:55:33 +00:00
|
|
|
if (targets_[i] == target) return;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-06-08 13:55:33 +00:00
|
|
|
targets_.Add(target);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-25 11:10:05 +00:00
|
|
|
bool UnaryOperation::ResultOverwriteAllowed() {
|
|
|
|
switch (op_) {
|
|
|
|
case Token::BIT_NOT:
|
|
|
|
case Token::SUB:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BinaryOperation::ResultOverwriteAllowed() {
|
|
|
|
switch (op_) {
|
|
|
|
case Token::COMMA:
|
|
|
|
case Token::OR:
|
|
|
|
case Token::AND:
|
|
|
|
return false;
|
|
|
|
case Token::BIT_OR:
|
|
|
|
case Token::BIT_XOR:
|
|
|
|
case Token::BIT_AND:
|
|
|
|
case Token::SHL:
|
|
|
|
case Token::SAR:
|
|
|
|
case Token::SHR:
|
|
|
|
case Token::ADD:
|
|
|
|
case Token::SUB:
|
|
|
|
case Token::MUL:
|
|
|
|
case Token::DIV:
|
|
|
|
case Token::MOD:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-19 14:50:33 +00:00
|
|
|
static bool IsTypeof(Expression* expr) {
|
|
|
|
UnaryOperation* maybe_unary = expr->AsUnaryOperation();
|
|
|
|
return maybe_unary != NULL && maybe_unary->op() == Token::TYPEOF;
|
|
|
|
}
|
|
|
|
|
2011-06-24 14:30:10 +00:00
|
|
|
|
2011-09-19 14:50:33 +00:00
|
|
|
// Check for the pattern: typeof <expression> equals <string literal>.
|
|
|
|
static bool MatchLiteralCompareTypeof(Expression* left,
|
|
|
|
Token::Value op,
|
|
|
|
Expression* right,
|
|
|
|
Expression** expr,
|
|
|
|
Handle<String>* check) {
|
|
|
|
if (IsTypeof(left) && right->IsStringLiteral() && Token::IsEqualityOp(op)) {
|
|
|
|
*expr = left->AsUnaryOperation()->expression();
|
|
|
|
*check = Handle<String>::cast(right->AsLiteral()->handle());
|
2011-06-24 14:30:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-19 14:50:33 +00:00
|
|
|
bool CompareOperation::IsLiteralCompareTypeof(Expression** expr,
|
|
|
|
Handle<String>* check) {
|
|
|
|
return MatchLiteralCompareTypeof(left_, op_, right_, expr, check) ||
|
|
|
|
MatchLiteralCompareTypeof(right_, op_, left_, expr, check);
|
|
|
|
}
|
2011-06-24 14:30:10 +00:00
|
|
|
|
|
|
|
|
2011-09-19 14:50:33 +00:00
|
|
|
static bool IsVoidOfLiteral(Expression* expr) {
|
|
|
|
UnaryOperation* maybe_unary = expr->AsUnaryOperation();
|
|
|
|
return maybe_unary != NULL &&
|
|
|
|
maybe_unary->op() == Token::VOID &&
|
|
|
|
maybe_unary->expression()->AsLiteral() != NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-24 14:30:10 +00:00
|
|
|
|
2011-09-19 14:50:33 +00:00
|
|
|
// Check for the pattern: void <literal> equals <expression>
|
|
|
|
static bool MatchLiteralCompareUndefined(Expression* left,
|
|
|
|
Token::Value op,
|
|
|
|
Expression* right,
|
|
|
|
Expression** expr) {
|
|
|
|
if (IsVoidOfLiteral(left) && Token::IsEqualityOp(op)) {
|
|
|
|
*expr = right;
|
2011-06-24 14:30:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-19 14:50:33 +00:00
|
|
|
bool CompareOperation::IsLiteralCompareUndefined(Expression** expr) {
|
|
|
|
return MatchLiteralCompareUndefined(left_, op_, right_, expr) ||
|
|
|
|
MatchLiteralCompareUndefined(right_, op_, left_, expr);
|
|
|
|
}
|
2011-09-15 09:09:40 +00:00
|
|
|
|
|
|
|
|
2011-09-19 14:50:33 +00:00
|
|
|
// Check for the pattern: null equals <expression>
|
|
|
|
static bool MatchLiteralCompareNull(Expression* left,
|
|
|
|
Token::Value op,
|
|
|
|
Expression* right,
|
|
|
|
Expression** expr) {
|
|
|
|
if (left->IsNullLiteral() && Token::IsEqualityOp(op)) {
|
|
|
|
*expr = right;
|
2011-09-15 09:09:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-19 14:50:33 +00:00
|
|
|
bool CompareOperation::IsLiteralCompareNull(Expression** expr) {
|
|
|
|
return MatchLiteralCompareNull(left_, op_, right_, expr) ||
|
|
|
|
MatchLiteralCompareNull(right_, op_, left_, expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Inlining support
|
|
|
|
|
2011-04-12 08:37:29 +00:00
|
|
|
bool Declaration::IsInlineable() const {
|
2011-06-01 13:26:30 +00:00
|
|
|
return proxy()->var()->IsStackAllocated() && fun() == NULL;
|
2011-04-12 08:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TargetCollector::IsInlineable() const {
|
|
|
|
UNREACHABLE();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ForInStatement::IsInlineable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-12 10:52:49 +00:00
|
|
|
bool WithStatement::IsInlineable() const {
|
2011-04-12 08:37:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SwitchStatement::IsInlineable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TryStatement::IsInlineable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TryCatchStatement::IsInlineable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TryFinallyStatement::IsInlineable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DebuggerStatement::IsInlineable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Throw::IsInlineable() const {
|
2011-04-20 09:15:52 +00:00
|
|
|
return exception()->IsInlineable();
|
2011-04-12 08:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MaterializedLiteral::IsInlineable() const {
|
|
|
|
// TODO(1322): Allow materialized literals.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FunctionLiteral::IsInlineable() const {
|
|
|
|
// TODO(1322): Allow materialized literals.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ThisFunction::IsInlineable() const {
|
2011-10-19 11:41:22 +00:00
|
|
|
return true;
|
2011-04-12 08:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SharedFunctionInfoLiteral::IsInlineable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 10:27:09 +00:00
|
|
|
bool ForStatement::IsInlineable() const {
|
|
|
|
return (init() == NULL || init()->IsInlineable())
|
|
|
|
&& (cond() == NULL || cond()->IsInlineable())
|
|
|
|
&& (next() == NULL || next()->IsInlineable())
|
|
|
|
&& body()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool WhileStatement::IsInlineable() const {
|
|
|
|
return cond()->IsInlineable()
|
|
|
|
&& body()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DoWhileStatement::IsInlineable() const {
|
|
|
|
return cond()->IsInlineable()
|
|
|
|
&& body()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ContinueStatement::IsInlineable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BreakStatement::IsInlineable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-12 08:37:29 +00:00
|
|
|
bool EmptyStatement::IsInlineable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Literal::IsInlineable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
bool Block::IsInlineable() const {
|
|
|
|
const int count = statements_.length();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (!statements_[i]->IsInlineable()) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ExpressionStatement::IsInlineable() const {
|
|
|
|
return expression()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IfStatement::IsInlineable() const {
|
2011-04-12 08:37:29 +00:00
|
|
|
return condition()->IsInlineable()
|
|
|
|
&& then_statement()->IsInlineable()
|
|
|
|
&& else_statement()->IsInlineable();
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ReturnStatement::IsInlineable() const {
|
|
|
|
return expression()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Conditional::IsInlineable() const {
|
|
|
|
return condition()->IsInlineable() && then_expression()->IsInlineable() &&
|
|
|
|
else_expression()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VariableProxy::IsInlineable() const {
|
2011-09-15 10:57:47 +00:00
|
|
|
return var()->IsUnallocated()
|
|
|
|
|| var()->IsStackAllocated()
|
|
|
|
|| var()->IsContextSlot();
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Assignment::IsInlineable() const {
|
|
|
|
return target()->IsInlineable() && value()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Property::IsInlineable() const {
|
|
|
|
return obj()->IsInlineable() && key()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Call::IsInlineable() const {
|
|
|
|
if (!expression()->IsInlineable()) return false;
|
|
|
|
const int count = arguments()->length();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (!arguments()->at(i)->IsInlineable()) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CallNew::IsInlineable() const {
|
|
|
|
if (!expression()->IsInlineable()) return false;
|
|
|
|
const int count = arguments()->length();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (!arguments()->at(i)->IsInlineable()) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CallRuntime::IsInlineable() const {
|
2011-05-26 10:56:07 +00:00
|
|
|
// Don't try to inline JS runtime calls because we don't (currently) even
|
|
|
|
// optimize them.
|
|
|
|
if (is_jsruntime()) return false;
|
|
|
|
// Don't inline the %_ArgumentsLength or %_Arguments because their
|
|
|
|
// implementation will not work. There is no stack frame to get them
|
|
|
|
// from.
|
|
|
|
if (function()->intrinsic_type == Runtime::INLINE &&
|
|
|
|
(name()->IsEqualTo(CStrVector("_ArgumentsLength")) ||
|
|
|
|
name()->IsEqualTo(CStrVector("_Arguments")))) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
const int count = arguments()->length();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (!arguments()->at(i)->IsInlineable()) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool UnaryOperation::IsInlineable() const {
|
|
|
|
return expression()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BinaryOperation::IsInlineable() const {
|
|
|
|
return left()->IsInlineable() && right()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CompareOperation::IsInlineable() const {
|
|
|
|
return left()->IsInlineable() && right()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CountOperation::IsInlineable() const {
|
|
|
|
return expression()->IsInlineable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Recording of type feedback
|
|
|
|
|
|
|
|
void Property::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
|
|
|
|
// Record type feedback from the oracle in the AST.
|
2011-06-16 06:37:49 +00:00
|
|
|
is_monomorphic_ = oracle->LoadIsMonomorphicNormal(this);
|
2011-08-22 14:23:37 +00:00
|
|
|
receiver_types_.Clear();
|
2010-12-07 11:31:57 +00:00
|
|
|
if (key()->IsPropertyName()) {
|
2011-03-23 13:40:07 +00:00
|
|
|
if (oracle->LoadIsBuiltin(this, Builtins::kLoadIC_ArrayLength)) {
|
2010-12-07 11:31:57 +00:00
|
|
|
is_array_length_ = true;
|
2011-03-23 13:40:07 +00:00
|
|
|
} else if (oracle->LoadIsBuiltin(this, Builtins::kLoadIC_StringLength)) {
|
2011-01-25 15:51:10 +00:00
|
|
|
is_string_length_ = true;
|
2010-12-22 15:43:32 +00:00
|
|
|
} else if (oracle->LoadIsBuiltin(this,
|
2011-03-23 13:40:07 +00:00
|
|
|
Builtins::kLoadIC_FunctionPrototype)) {
|
2010-12-22 15:43:32 +00:00
|
|
|
is_function_prototype_ = true;
|
2010-12-07 11:31:57 +00:00
|
|
|
} else {
|
|
|
|
Literal* lit_key = key()->AsLiteral();
|
|
|
|
ASSERT(lit_key != NULL && lit_key->handle()->IsString());
|
|
|
|
Handle<String> name = Handle<String>::cast(lit_key->handle());
|
2011-08-22 14:23:37 +00:00
|
|
|
oracle->LoadReceiverTypes(this, name, &receiver_types_);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
2011-03-23 13:40:07 +00:00
|
|
|
} else if (oracle->LoadIsBuiltin(this, Builtins::kKeyedLoadIC_String)) {
|
2011-03-14 15:36:00 +00:00
|
|
|
is_string_access_ = true;
|
2010-12-07 11:31:57 +00:00
|
|
|
} else if (is_monomorphic_) {
|
2011-08-22 14:23:37 +00:00
|
|
|
receiver_types_.Add(oracle->LoadMonomorphicReceiverType(this));
|
2011-06-20 10:19:00 +00:00
|
|
|
} else if (oracle->LoadIsMegamorphicWithTypeInfo(this)) {
|
2011-08-22 14:23:37 +00:00
|
|
|
receiver_types_.Reserve(kMaxKeyedPolymorphism);
|
|
|
|
oracle->CollectKeyedReceiverTypes(this->id(), &receiver_types_);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Assignment::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
|
|
|
|
Property* prop = target()->AsProperty();
|
|
|
|
ASSERT(prop != NULL);
|
2011-06-16 06:37:49 +00:00
|
|
|
is_monomorphic_ = oracle->StoreIsMonomorphicNormal(this);
|
2011-08-22 14:23:37 +00:00
|
|
|
receiver_types_.Clear();
|
2010-12-07 11:31:57 +00:00
|
|
|
if (prop->key()->IsPropertyName()) {
|
|
|
|
Literal* lit_key = prop->key()->AsLiteral();
|
|
|
|
ASSERT(lit_key != NULL && lit_key->handle()->IsString());
|
|
|
|
Handle<String> name = Handle<String>::cast(lit_key->handle());
|
2011-08-22 14:23:37 +00:00
|
|
|
oracle->StoreReceiverTypes(this, name, &receiver_types_);
|
2010-12-07 11:31:57 +00:00
|
|
|
} else if (is_monomorphic_) {
|
2011-06-20 10:19:00 +00:00
|
|
|
// Record receiver type for monomorphic keyed stores.
|
2011-08-22 14:23:37 +00:00
|
|
|
receiver_types_.Add(oracle->StoreMonomorphicReceiverType(this));
|
2011-06-20 10:19:00 +00:00
|
|
|
} else if (oracle->StoreIsMegamorphicWithTypeInfo(this)) {
|
2011-08-22 14:23:37 +00:00
|
|
|
receiver_types_.Reserve(kMaxKeyedPolymorphism);
|
|
|
|
oracle->CollectKeyedReceiverTypes(this->id(), &receiver_types_);
|
2011-04-07 09:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CountOperation::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
|
2011-06-16 06:37:49 +00:00
|
|
|
is_monomorphic_ = oracle->StoreIsMonomorphicNormal(this);
|
2011-08-22 14:23:37 +00:00
|
|
|
receiver_types_.Clear();
|
2011-04-07 09:51:25 +00:00
|
|
|
if (is_monomorphic_) {
|
2011-06-20 10:19:00 +00:00
|
|
|
// Record receiver type for monomorphic keyed stores.
|
2011-08-22 14:23:37 +00:00
|
|
|
receiver_types_.Add(oracle->StoreMonomorphicReceiverType(this));
|
2011-06-20 10:19:00 +00:00
|
|
|
} else if (oracle->StoreIsMegamorphicWithTypeInfo(this)) {
|
2011-08-22 14:23:37 +00:00
|
|
|
receiver_types_.Reserve(kMaxKeyedPolymorphism);
|
|
|
|
oracle->CollectKeyedReceiverTypes(this->id(), &receiver_types_);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CaseClause::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
|
|
|
|
TypeInfo info = oracle->SwitchType(this);
|
|
|
|
if (info.IsSmi()) {
|
|
|
|
compare_type_ = SMI_ONLY;
|
|
|
|
} else if (info.IsNonPrimitive()) {
|
|
|
|
compare_type_ = OBJECT_ONLY;
|
|
|
|
} else {
|
|
|
|
ASSERT(compare_type_ == NONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-19 20:05:22 +00:00
|
|
|
static bool CanCallWithoutIC(Handle<JSFunction> target, int arity) {
|
2010-12-14 18:53:48 +00:00
|
|
|
SharedFunctionInfo* info = target->shared();
|
2011-01-19 20:05:22 +00:00
|
|
|
// If the number of formal parameters of the target function does
|
|
|
|
// not match the number of arguments we're passing, we don't want to
|
|
|
|
// deal with it. Otherwise, we can call it directly.
|
|
|
|
return !target->NeedsArgumentsAdaption() ||
|
|
|
|
info->formal_parameter_count() == arity;
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Call::ComputeTarget(Handle<Map> type, Handle<String> name) {
|
2011-01-25 12:21:03 +00:00
|
|
|
if (check_type_ == RECEIVER_MAP_CHECK) {
|
|
|
|
// For primitive checks the holder is set up to point to the
|
|
|
|
// corresponding prototype object, i.e. one step of the algorithm
|
|
|
|
// below has been already performed.
|
|
|
|
// For non-primitive checks we clear it to allow computing targets
|
|
|
|
// for polymorphic calls.
|
|
|
|
holder_ = Handle<JSObject>::null();
|
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
while (true) {
|
2011-10-18 11:18:55 +00:00
|
|
|
LookupResult lookup(type->GetIsolate());
|
2010-12-07 11:31:57 +00:00
|
|
|
type->LookupInDescriptors(NULL, *name, &lookup);
|
|
|
|
// If the function wasn't found directly in the map, we start
|
|
|
|
// looking upwards through the prototype chain.
|
|
|
|
if (!lookup.IsFound() && type->prototype()->IsJSObject()) {
|
|
|
|
holder_ = Handle<JSObject>(JSObject::cast(type->prototype()));
|
|
|
|
type = Handle<Map>(holder()->map());
|
|
|
|
} else if (lookup.IsProperty() && lookup.type() == CONSTANT_FUNCTION) {
|
|
|
|
target_ = Handle<JSFunction>(lookup.GetConstantFunctionFromMap(*type));
|
2011-01-19 20:05:22 +00:00
|
|
|
return CanCallWithoutIC(target_, arguments()->length());
|
2010-12-07 11:31:57 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Call::ComputeGlobalTarget(Handle<GlobalObject> global,
|
2011-04-01 11:54:04 +00:00
|
|
|
LookupResult* lookup) {
|
2010-12-07 11:31:57 +00:00
|
|
|
target_ = Handle<JSFunction>::null();
|
|
|
|
cell_ = Handle<JSGlobalPropertyCell>::null();
|
2011-04-01 11:54:04 +00:00
|
|
|
ASSERT(lookup->IsProperty() &&
|
|
|
|
lookup->type() == NORMAL &&
|
|
|
|
lookup->holder() == *global);
|
|
|
|
cell_ = Handle<JSGlobalPropertyCell>(global->GetPropertyCell(lookup));
|
|
|
|
if (cell_->value()->IsJSFunction()) {
|
|
|
|
Handle<JSFunction> candidate(JSFunction::cast(cell_->value()));
|
|
|
|
// If the function is in new space we assume it's more likely to
|
|
|
|
// change and thus prefer the general IC code.
|
|
|
|
if (!HEAP->InNewSpace(*candidate) &&
|
|
|
|
CanCallWithoutIC(candidate, arguments()->length())) {
|
|
|
|
target_ = candidate;
|
|
|
|
return true;
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-24 14:01:36 +00:00
|
|
|
void Call::RecordTypeFeedback(TypeFeedbackOracle* oracle,
|
|
|
|
CallKind call_kind) {
|
2011-09-27 11:42:02 +00:00
|
|
|
is_monomorphic_ = oracle->CallIsMonomorphic(this);
|
2010-12-07 11:31:57 +00:00
|
|
|
Property* property = expression()->AsProperty();
|
2011-09-27 11:42:02 +00:00
|
|
|
if (property == NULL) {
|
|
|
|
// Function call. Specialize for monomorphic calls.
|
|
|
|
if (is_monomorphic_) target_ = oracle->GetCallTarget(this);
|
|
|
|
} else {
|
|
|
|
// Method call. Specialize for the receiver types seen at runtime.
|
|
|
|
Literal* key = property->key()->AsLiteral();
|
|
|
|
ASSERT(key != NULL && key->handle()->IsString());
|
|
|
|
Handle<String> name = Handle<String>::cast(key->handle());
|
|
|
|
receiver_types_.Clear();
|
|
|
|
oracle->CallReceiverTypes(this, name, call_kind, &receiver_types_);
|
2010-12-07 11:31:57 +00:00
|
|
|
#ifdef DEBUG
|
2011-09-27 11:42:02 +00:00
|
|
|
if (FLAG_enable_slow_asserts) {
|
|
|
|
int length = receiver_types_.length();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Handle<Map> map = receiver_types_.at(i);
|
|
|
|
ASSERT(!map.is_null() && *map != NULL);
|
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-09-27 11:42:02 +00:00
|
|
|
check_type_ = oracle->GetCallCheckType(this);
|
|
|
|
if (is_monomorphic_) {
|
|
|
|
Handle<Map> map;
|
|
|
|
if (receiver_types_.length() > 0) {
|
|
|
|
ASSERT(check_type_ == RECEIVER_MAP_CHECK);
|
|
|
|
map = receiver_types_.at(0);
|
|
|
|
} else {
|
|
|
|
ASSERT(check_type_ != RECEIVER_MAP_CHECK);
|
|
|
|
holder_ = Handle<JSObject>(
|
|
|
|
oracle->GetPrototypeForPrimitiveCheck(check_type_));
|
|
|
|
map = Handle<Map>(holder_->map());
|
|
|
|
}
|
|
|
|
is_monomorphic_ = ComputeTarget(map, name);
|
2011-01-13 14:16:08 +00:00
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CompareOperation::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
|
2011-01-27 14:04:07 +00:00
|
|
|
TypeInfo info = oracle->CompareType(this);
|
|
|
|
if (info.IsSmi()) {
|
2010-12-07 11:31:57 +00:00
|
|
|
compare_type_ = SMI_ONLY;
|
2011-01-27 14:04:07 +00:00
|
|
|
} else if (info.IsNonPrimitive()) {
|
2010-12-07 11:31:57 +00:00
|
|
|
compare_type_ = OBJECT_ONLY;
|
|
|
|
} else {
|
|
|
|
ASSERT(compare_type_ == NONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2008-11-27 13:55:06 +00:00
|
|
|
// Implementation of AstVisitor
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-03-30 12:25:58 +00:00
|
|
|
bool AstVisitor::CheckStackOverflow() {
|
|
|
|
if (stack_overflow_) return true;
|
2011-03-21 11:57:59 +00:00
|
|
|
StackLimitCheck check(isolate_);
|
2010-03-30 12:25:58 +00:00
|
|
|
if (!check.HasOverflowed()) return false;
|
|
|
|
return (stack_overflow_ = true);
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-10-12 15:06:28 +00:00
|
|
|
void AstVisitor::VisitDeclarations(ZoneList<Declaration*>* declarations) {
|
|
|
|
for (int i = 0; i < declarations->length(); i++) {
|
|
|
|
Visit(declarations->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-27 13:55:06 +00:00
|
|
|
void AstVisitor::VisitStatements(ZoneList<Statement*>* statements) {
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < statements->length(); i++) {
|
|
|
|
Visit(statements->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-27 13:55:06 +00:00
|
|
|
void AstVisitor::VisitExpressions(ZoneList<Expression*>* expressions) {
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < expressions->length(); i++) {
|
|
|
|
// The variable statement visiting code may pass NULL expressions
|
|
|
|
// to this code. Maybe this should be handled by introducing an
|
|
|
|
// undefined expression or literal? Revisit this code if this
|
|
|
|
// changes
|
|
|
|
Expression* expression = expressions->at(i);
|
|
|
|
if (expression != NULL) Visit(expression);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Regular expressions
|
|
|
|
|
|
|
|
#define MAKE_ACCEPT(Name) \
|
|
|
|
void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \
|
|
|
|
return visitor->Visit##Name(this, data); \
|
|
|
|
}
|
|
|
|
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT)
|
|
|
|
#undef MAKE_ACCEPT
|
|
|
|
|
|
|
|
#define MAKE_TYPE_CASE(Name) \
|
|
|
|
RegExp##Name* RegExpTree::As##Name() { \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
bool RegExpTree::Is##Name() { return false; }
|
2008-11-26 11:29:26 +00:00
|
|
|
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
|
2008-11-25 11:07:48 +00:00
|
|
|
#undef MAKE_TYPE_CASE
|
|
|
|
|
|
|
|
#define MAKE_TYPE_CASE(Name) \
|
|
|
|
RegExp##Name* RegExp##Name::As##Name() { \
|
|
|
|
return this; \
|
|
|
|
} \
|
|
|
|
bool RegExp##Name::Is##Name() { return true; }
|
|
|
|
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
|
|
|
|
#undef MAKE_TYPE_CASE
|
|
|
|
|
|
|
|
RegExpEmpty RegExpEmpty::kInstance;
|
|
|
|
|
|
|
|
|
2009-01-14 11:32:23 +00:00
|
|
|
static Interval ListCaptureRegisters(ZoneList<RegExpTree*>* children) {
|
|
|
|
Interval result = Interval::Empty();
|
|
|
|
for (int i = 0; i < children->length(); i++)
|
|
|
|
result = result.Union(children->at(i)->CaptureRegisters());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpAlternative::CaptureRegisters() {
|
|
|
|
return ListCaptureRegisters(nodes());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpDisjunction::CaptureRegisters() {
|
|
|
|
return ListCaptureRegisters(alternatives());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpLookahead::CaptureRegisters() {
|
|
|
|
return body()->CaptureRegisters();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpCapture::CaptureRegisters() {
|
|
|
|
Interval self(StartRegister(index()), EndRegister(index()));
|
|
|
|
return self.Union(body()->CaptureRegisters());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpQuantifier::CaptureRegisters() {
|
|
|
|
return body()->CaptureRegisters();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-19 14:00:01 +00:00
|
|
|
bool RegExpAssertion::IsAnchoredAtStart() {
|
2009-01-23 07:46:44 +00:00
|
|
|
return type() == RegExpAssertion::START_OF_INPUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-19 14:00:01 +00:00
|
|
|
bool RegExpAssertion::IsAnchoredAtEnd() {
|
|
|
|
return type() == RegExpAssertion::END_OF_INPUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpAlternative::IsAnchoredAtStart() {
|
2009-02-03 11:43:55 +00:00
|
|
|
ZoneList<RegExpTree*>* nodes = this->nodes();
|
|
|
|
for (int i = 0; i < nodes->length(); i++) {
|
|
|
|
RegExpTree* node = nodes->at(i);
|
2010-10-19 14:00:01 +00:00
|
|
|
if (node->IsAnchoredAtStart()) { return true; }
|
|
|
|
if (node->max_match() > 0) { return false; }
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpAlternative::IsAnchoredAtEnd() {
|
|
|
|
ZoneList<RegExpTree*>* nodes = this->nodes();
|
|
|
|
for (int i = nodes->length() - 1; i >= 0; i--) {
|
|
|
|
RegExpTree* node = nodes->at(i);
|
|
|
|
if (node->IsAnchoredAtEnd()) { return true; }
|
2009-02-03 11:43:55 +00:00
|
|
|
if (node->max_match() > 0) { return false; }
|
|
|
|
}
|
|
|
|
return false;
|
2009-01-23 07:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-19 14:00:01 +00:00
|
|
|
bool RegExpDisjunction::IsAnchoredAtStart() {
|
2009-01-23 07:46:44 +00:00
|
|
|
ZoneList<RegExpTree*>* alternatives = this->alternatives();
|
|
|
|
for (int i = 0; i < alternatives->length(); i++) {
|
2010-10-19 14:00:01 +00:00
|
|
|
if (!alternatives->at(i)->IsAnchoredAtStart())
|
2009-01-23 07:46:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-19 14:00:01 +00:00
|
|
|
bool RegExpDisjunction::IsAnchoredAtEnd() {
|
|
|
|
ZoneList<RegExpTree*>* alternatives = this->alternatives();
|
|
|
|
for (int i = 0; i < alternatives->length(); i++) {
|
|
|
|
if (!alternatives->at(i)->IsAnchoredAtEnd())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpLookahead::IsAnchoredAtStart() {
|
|
|
|
return is_positive() && body()->IsAnchoredAtStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpCapture::IsAnchoredAtStart() {
|
|
|
|
return body()->IsAnchoredAtStart();
|
2009-01-23 07:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-19 14:00:01 +00:00
|
|
|
bool RegExpCapture::IsAnchoredAtEnd() {
|
|
|
|
return body()->IsAnchoredAtEnd();
|
2009-01-23 07:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
// Convert regular expression trees to a simple sexp representation.
|
|
|
|
// This representation should be different from the input grammar
|
|
|
|
// in as many cases as possible, to make it more difficult for incorrect
|
|
|
|
// parses to look as correct ones which is likely if the input and
|
|
|
|
// output formats are alike.
|
|
|
|
class RegExpUnparser: public RegExpVisitor {
|
|
|
|
public:
|
|
|
|
RegExpUnparser();
|
|
|
|
void VisitCharacterRange(CharacterRange that);
|
2011-09-09 22:39:47 +00:00
|
|
|
SmartArrayPointer<const char> ToString() { return stream_.ToCString(); }
|
2008-11-25 11:07:48 +00:00
|
|
|
#define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*, void* data);
|
|
|
|
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
|
|
|
|
#undef MAKE_CASE
|
|
|
|
private:
|
|
|
|
StringStream* stream() { return &stream_; }
|
|
|
|
HeapStringAllocator alloc_;
|
|
|
|
StringStream stream_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
RegExpUnparser::RegExpUnparser() : stream_(&alloc_) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitDisjunction(RegExpDisjunction* that, void* data) {
|
|
|
|
stream()->Add("(|");
|
|
|
|
for (int i = 0; i < that->alternatives()->length(); i++) {
|
|
|
|
stream()->Add(" ");
|
|
|
|
that->alternatives()->at(i)->Accept(this, data);
|
|
|
|
}
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) {
|
|
|
|
stream()->Add("(:");
|
|
|
|
for (int i = 0; i < that->nodes()->length(); i++) {
|
|
|
|
stream()->Add(" ");
|
|
|
|
that->nodes()->at(i)->Accept(this, data);
|
|
|
|
}
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpUnparser::VisitCharacterRange(CharacterRange that) {
|
|
|
|
stream()->Add("%k", that.from());
|
|
|
|
if (!that.IsSingleton()) {
|
|
|
|
stream()->Add("-%k", that.to());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that,
|
|
|
|
void* data) {
|
|
|
|
if (that->is_negated())
|
|
|
|
stream()->Add("^");
|
|
|
|
stream()->Add("[");
|
|
|
|
for (int i = 0; i < that->ranges()->length(); i++) {
|
|
|
|
if (i > 0) stream()->Add(" ");
|
|
|
|
VisitCharacterRange(that->ranges()->at(i));
|
|
|
|
}
|
|
|
|
stream()->Add("]");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitAssertion(RegExpAssertion* that, void* data) {
|
|
|
|
switch (that->type()) {
|
|
|
|
case RegExpAssertion::START_OF_INPUT:
|
|
|
|
stream()->Add("@^i");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::END_OF_INPUT:
|
|
|
|
stream()->Add("@$i");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::START_OF_LINE:
|
|
|
|
stream()->Add("@^l");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::END_OF_LINE:
|
|
|
|
stream()->Add("@$l");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::BOUNDARY:
|
|
|
|
stream()->Add("@b");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::NON_BOUNDARY:
|
|
|
|
stream()->Add("@B");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) {
|
|
|
|
stream()->Add("'");
|
|
|
|
Vector<const uc16> chardata = that->data();
|
|
|
|
for (int i = 0; i < chardata.length(); i++) {
|
|
|
|
stream()->Add("%k", chardata[i]);
|
|
|
|
}
|
|
|
|
stream()->Add("'");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitText(RegExpText* that, void* data) {
|
|
|
|
if (that->elements()->length() == 1) {
|
|
|
|
that->elements()->at(0).data.u_atom->Accept(this, data);
|
|
|
|
} else {
|
|
|
|
stream()->Add("(!");
|
|
|
|
for (int i = 0; i < that->elements()->length(); i++) {
|
|
|
|
stream()->Add(" ");
|
|
|
|
that->elements()->at(i).data.u_atom->Accept(this, data);
|
|
|
|
}
|
|
|
|
stream()->Add(")");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) {
|
|
|
|
stream()->Add("(# %i ", that->min());
|
2008-12-17 10:59:14 +00:00
|
|
|
if (that->max() == RegExpTree::kInfinity) {
|
2008-11-25 11:07:48 +00:00
|
|
|
stream()->Add("- ");
|
|
|
|
} else {
|
|
|
|
stream()->Add("%i ", that->max());
|
|
|
|
}
|
2010-01-07 19:01:23 +00:00
|
|
|
stream()->Add(that->is_greedy() ? "g " : that->is_possessive() ? "p " : "n ");
|
2008-11-25 11:07:48 +00:00
|
|
|
that->body()->Accept(this, data);
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitCapture(RegExpCapture* that, void* data) {
|
|
|
|
stream()->Add("(^ ");
|
|
|
|
that->body()->Accept(this, data);
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitLookahead(RegExpLookahead* that, void* data) {
|
|
|
|
stream()->Add("(-> ");
|
|
|
|
stream()->Add(that->is_positive() ? "+ " : "- ");
|
|
|
|
that->body()->Accept(this, data);
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitBackReference(RegExpBackReference* that,
|
|
|
|
void* data) {
|
|
|
|
stream()->Add("(<- %i)", that->index());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitEmpty(RegExpEmpty* that, void* data) {
|
|
|
|
stream()->Put('%');
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-09 22:39:47 +00:00
|
|
|
SmartArrayPointer<const char> RegExpTree::ToString() {
|
2008-11-25 11:07:48 +00:00
|
|
|
RegExpUnparser unparser;
|
|
|
|
Accept(&unparser, NULL);
|
|
|
|
return unparser.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-17 10:59:14 +00:00
|
|
|
RegExpDisjunction::RegExpDisjunction(ZoneList<RegExpTree*>* alternatives)
|
|
|
|
: alternatives_(alternatives) {
|
2009-01-23 07:46:44 +00:00
|
|
|
ASSERT(alternatives->length() > 1);
|
2008-12-17 10:59:14 +00:00
|
|
|
RegExpTree* first_alternative = alternatives->at(0);
|
|
|
|
min_match_ = first_alternative->min_match();
|
|
|
|
max_match_ = first_alternative->max_match();
|
|
|
|
for (int i = 1; i < alternatives->length(); i++) {
|
|
|
|
RegExpTree* alternative = alternatives->at(i);
|
|
|
|
min_match_ = Min(min_match_, alternative->min_match());
|
|
|
|
max_match_ = Max(max_match_, alternative->max_match());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RegExpAlternative::RegExpAlternative(ZoneList<RegExpTree*>* nodes)
|
|
|
|
: nodes_(nodes) {
|
2009-01-23 07:46:44 +00:00
|
|
|
ASSERT(nodes->length() > 1);
|
2008-12-17 10:59:14 +00:00
|
|
|
min_match_ = 0;
|
|
|
|
max_match_ = 0;
|
|
|
|
for (int i = 0; i < nodes->length(); i++) {
|
|
|
|
RegExpTree* node = nodes->at(i);
|
|
|
|
min_match_ += node->min_match();
|
|
|
|
int node_max_match = node->max_match();
|
|
|
|
if (kInfinity - max_match_ < node_max_match) {
|
|
|
|
max_match_ = kInfinity;
|
|
|
|
} else {
|
|
|
|
max_match_ += node->max_match();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-22 13:21:32 +00:00
|
|
|
|
2011-07-18 17:32:41 +00:00
|
|
|
CaseClause::CaseClause(Isolate* isolate,
|
|
|
|
Expression* label,
|
2010-12-07 11:31:57 +00:00
|
|
|
ZoneList<Statement*>* statements,
|
|
|
|
int pos)
|
|
|
|
: label_(label),
|
|
|
|
statements_(statements),
|
|
|
|
position_(pos),
|
2011-03-09 12:06:54 +00:00
|
|
|
compare_type_(NONE),
|
2011-07-18 17:32:41 +00:00
|
|
|
compare_id_(AstNode::GetNextId(isolate)),
|
|
|
|
entry_id_(AstNode::GetNextId(isolate)) {
|
2011-03-09 12:06:54 +00:00
|
|
|
}
|
2010-05-10 11:32:25 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|