diff options
Diffstat (limited to 'deps/v8/src/typing-asm.cc')
-rw-r--r-- | deps/v8/src/typing-asm.cc | 823 |
1 files changed, 600 insertions, 223 deletions
diff --git a/deps/v8/src/typing-asm.cc b/deps/v8/src/typing-asm.cc index b267113400..509ba7b125 100644 --- a/deps/v8/src/typing-asm.cc +++ b/deps/v8/src/typing-asm.cc @@ -2,13 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "src/v8.h" - #include "src/typing-asm.h" -#include "src/ast.h" +#include <limits> + +#include "src/v8.h" + +#include "src/ast/ast.h" +#include "src/ast/scopes.h" #include "src/codegen.h" -#include "src/scopes.h" #include "src/type-cache.h" namespace v8 { @@ -38,15 +40,23 @@ namespace internal { AsmTyper::AsmTyper(Isolate* isolate, Zone* zone, Script* script, FunctionLiteral* root) : zone_(zone), + isolate_(isolate), script_(script), root_(root), valid_(true), + allow_simd_(false), + property_info_(NULL), + intish_(0), stdlib_types_(zone), stdlib_heap_types_(zone), stdlib_math_types_(zone), - global_variable_type_(HashMap::PointersMatch, - ZoneHashMap::kDefaultHashMapCapacity, - ZoneAllocationPolicy(zone)), +#define V(NAME, Name, name, lane_count, lane_type) \ + stdlib_simd_##name##_types_(zone), + SIMD128_TYPES(V) +#undef V + global_variable_type_(HashMap::PointersMatch, + ZoneHashMap::kDefaultHashMapCapacity, + ZoneAllocationPolicy(zone)), local_variable_type_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity, ZoneAllocationPolicy(zone)), @@ -68,6 +78,13 @@ void AsmTyper::VisitAsmModule(FunctionLiteral* fun) { Scope* scope = fun->scope(); if (!scope->is_function_scope()) FAIL(fun, "not at function scope"); + ExpressionStatement* use_asm = fun->body()->first()->AsExpressionStatement(); + if (use_asm == NULL) FAIL(fun, "missing \"use asm\""); + Literal* use_asm_literal = use_asm->expression()->AsLiteral(); + if (use_asm_literal == NULL) FAIL(fun, "missing \"use asm\""); + if (!use_asm_literal->raw_value()->AsString()->IsOneByteEqualTo("use asm")) + FAIL(fun, "missing \"use asm\""); + // Module parameters. for (int i = 0; i < scope->num_parameters(); ++i) { Variable* param = scope->parameter(i); @@ -91,7 +108,10 @@ void AsmTyper::VisitAsmModule(FunctionLiteral* fun) { if (decl != NULL) { RECURSE(VisitFunctionAnnotation(decl->fun())); Variable* var = decl->proxy()->var(); - DCHECK(GetType(var) == NULL); + if (property_info_ != NULL) { + SetVariableInfo(var, property_info_); + property_info_ = NULL; + } SetType(var, computed_type_); DCHECK(GetType(var) != NULL); } @@ -116,6 +136,9 @@ void AsmTyper::VisitAsmModule(FunctionLiteral* fun) { // Validate exports. ReturnStatement* stmt = fun->body()->last()->AsReturnStatement(); + if (stmt == nullptr) { + FAIL(fun->body()->last(), "last statement in module is not a return"); + } RECURSE(VisitWithExpectation(stmt->expression(), Type::Object(), "expected object export")); } @@ -139,6 +162,10 @@ void AsmTyper::VisitFunctionDeclaration(FunctionDeclaration* decl) { if (in_function_) { FAIL(decl, "function declared inside another"); } + // Set function type so global references to functions have some type + // (so they can give a more useful error). + Variable* var = decl->proxy()->var(); + SetType(var, Type::Function(zone())); } @@ -149,7 +176,15 @@ void AsmTyper::VisitFunctionAnnotation(FunctionLiteral* fun) { if (body->length() > 0) { ReturnStatement* stmt = body->last()->AsReturnStatement(); if (stmt != NULL) { - RECURSE(VisitExpressionAnnotation(stmt->expression())); + Literal* literal = stmt->expression()->AsLiteral(); + Type* old_expected = expected_type_; + expected_type_ = Type::Any(); + if (literal) { + RECURSE(VisitLiteral(literal, true)); + } else { + RECURSE(VisitExpressionAnnotation(stmt->expression(), NULL, true)); + } + expected_type_ = old_expected; result_type = computed_type_; } } @@ -171,7 +206,11 @@ void AsmTyper::VisitFunctionAnnotation(FunctionLiteral* fun) { Variable* var = proxy->var(); if (var->location() != VariableLocation::PARAMETER || var->index() != i) break; - RECURSE(VisitExpressionAnnotation(expr->value())); + RECURSE(VisitExpressionAnnotation(expr->value(), var, false)); + if (property_info_ != NULL) { + SetVariableInfo(var, property_info_); + property_info_ = NULL; + } SetType(var, computed_type_); type->InitParameter(i, computed_type_); good = true; @@ -182,24 +221,38 @@ void AsmTyper::VisitFunctionAnnotation(FunctionLiteral* fun) { } -void AsmTyper::VisitExpressionAnnotation(Expression* expr) { +void AsmTyper::VisitExpressionAnnotation(Expression* expr, Variable* var, + bool is_return) { // Normal +x or x|0 annotations. BinaryOperation* bin = expr->AsBinaryOperation(); if (bin != NULL) { + if (var != NULL) { + VariableProxy* proxy = bin->left()->AsVariableProxy(); + if (proxy == NULL) { + FAIL(bin->left(), "expected variable for type annotation"); + } + if (proxy->var() != var) { + FAIL(proxy, "annotation source doesn't match destination"); + } + } Literal* right = bin->right()->AsLiteral(); if (right != NULL) { switch (bin->op()) { - case Token::MUL: // We encode +x as 1*x + case Token::MUL: // We encode +x as x*1.0 if (right->raw_value()->ContainsDot() && right->raw_value()->AsNumber() == 1.0) { - SetResult(expr, cache_.kFloat64); + SetResult(expr, cache_.kAsmDouble); return; } break; case Token::BIT_OR: if (!right->raw_value()->ContainsDot() && right->raw_value()->AsNumber() == 0.0) { - SetResult(expr, cache_.kInt32); + if (is_return) { + SetResult(expr, cache_.kAsmSigned); + } else { + SetResult(expr, cache_.kAsmInt); + } return; } break; @@ -218,19 +271,28 @@ void AsmTyper::VisitExpressionAnnotation(Expression* expr) { Call* call = expr->AsCall(); if (call != NULL) { - if (call->expression()->IsVariableProxy()) { - RECURSE(VisitWithExpectation( - call->expression(), Type::Any(zone()), - "only fround allowed on expression annotations")); - if (!computed_type_->Is( - Type::Function(cache_.kFloat32, Type::Number(zone()), zone()))) { - FAIL(call->expression(), - "only fround allowed on expression annotations"); + VariableProxy* proxy = call->expression()->AsVariableProxy(); + if (proxy != NULL) { + VariableInfo* info = GetVariableInfo(proxy->var(), false); + if (!info || + (!info->is_check_function && !info->is_constructor_function)) { + if (allow_simd_) { + FAIL(call->expression(), + "only fround/SIMD.checks allowed on expression annotations"); + } else { + FAIL(call->expression(), + "only fround allowed on expression annotations"); + } + } + Type* type = info->type; + DCHECK(type->IsFunction()); + if (info->is_check_function) { + DCHECK(type->AsFunction()->Arity() == 1); } - if (call->arguments()->length() != 1) { - FAIL(call, "invalid argument count calling fround"); + if (call->arguments()->length() != type->AsFunction()->Arity()) { + FAIL(call, "invalid argument count calling function"); } - SetResult(expr, cache_.kFloat32); + SetResult(expr, type->AsFunction()->Result()); return; } } @@ -274,7 +336,7 @@ void AsmTyper::VisitIfStatement(IfStatement* stmt) { if (!in_function_) { FAIL(stmt, "if statement inside module body"); } - RECURSE(VisitWithExpectation(stmt->condition(), cache_.kInt32, + RECURSE(VisitWithExpectation(stmt->condition(), cache_.kAsmSigned, "if condition expected to be integer")); RECURSE(Visit(stmt->then_statement())); RECURSE(Visit(stmt->else_statement())); @@ -300,9 +362,17 @@ void AsmTyper::VisitReturnStatement(ReturnStatement* stmt) { if (!in_function_) { return; } - RECURSE( - VisitWithExpectation(stmt->expression(), return_type_, - "return expression expected to have return type")); + Literal* literal = stmt->expression()->AsLiteral(); + if (literal) { + VisitLiteral(literal, true); + } else { + RECURSE( + VisitWithExpectation(stmt->expression(), Type::Any(), + "return expression expected to have return type")); + } + if (!computed_type_->Is(return_type_) || !return_type_->Is(computed_type_)) { + FAIL(stmt->expression(), "return type does not match function signature"); + } } @@ -315,23 +385,40 @@ void AsmTyper::VisitSwitchStatement(SwitchStatement* stmt) { if (!in_function_) { FAIL(stmt, "switch statement inside module body"); } - RECURSE(VisitWithExpectation(stmt->tag(), cache_.kInt32, + RECURSE(VisitWithExpectation(stmt->tag(), cache_.kAsmSigned, "switch expression non-integer")); ZoneList<CaseClause*>* clauses = stmt->cases(); + ZoneSet<int32_t> cases(zone()); for (int i = 0; i < clauses->length(); ++i) { CaseClause* clause = clauses->at(i); - if (clause->is_default()) continue; - Expression* label = clause->label(); - RECURSE( - VisitWithExpectation(label, cache_.kInt32, "case label non-integer")); - if (!label->IsLiteral()) FAIL(label, "non-literal case label"); - Handle<Object> value = label->AsLiteral()->value(); - int32_t value32; - if (!value->ToInt32(&value32)) FAIL(label, "illegal case label value"); + if (clause->is_default()) { + if (i != clauses->length() - 1) { + FAIL(clause, "default case out of order"); + } + } else { + Expression* label = clause->label(); + RECURSE(VisitWithExpectation(label, cache_.kAsmSigned, + "case label non-integer")); + if (!label->IsLiteral()) FAIL(label, "non-literal case label"); + Handle<Object> value = label->AsLiteral()->value(); + int32_t value32; + if (!value->ToInt32(&value32)) FAIL(label, "illegal case label value"); + if (cases.find(value32) != cases.end()) { + FAIL(label, "duplicate case value"); + } + cases.insert(value32); + } // TODO(bradnelson): Detect duplicates. ZoneList<Statement*>* stmts = clause->statements(); RECURSE(VisitStatements(stmts)); } + if (cases.size() > 0) { + int64_t min_case = *cases.begin(); + int64_t max_case = *cases.rbegin(); + if (max_case - min_case > std::numeric_limits<int32_t>::max()) { + FAIL(stmt, "case range too large"); + } + } } @@ -343,7 +430,7 @@ void AsmTyper::VisitDoWhileStatement(DoWhileStatement* stmt) { FAIL(stmt, "do statement inside module body"); } RECURSE(Visit(stmt->body())); - RECURSE(VisitWithExpectation(stmt->cond(), cache_.kInt32, + RECURSE(VisitWithExpectation(stmt->cond(), cache_.kAsmSigned, "do condition expected to be integer")); } @@ -352,7 +439,7 @@ void AsmTyper::VisitWhileStatement(WhileStatement* stmt) { if (!in_function_) { FAIL(stmt, "while statement inside module body"); } - RECURSE(VisitWithExpectation(stmt->cond(), cache_.kInt32, + RECURSE(VisitWithExpectation(stmt->cond(), cache_.kAsmSigned, "while condition expected to be integer")); RECURSE(Visit(stmt->body())); } @@ -366,7 +453,7 @@ void AsmTyper::VisitForStatement(ForStatement* stmt) { RECURSE(Visit(stmt->init())); } if (stmt->cond() != NULL) { - RECURSE(VisitWithExpectation(stmt->cond(), cache_.kInt32, + RECURSE(VisitWithExpectation(stmt->cond(), cache_.kAsmSigned, "for condition expected to be integer")); } if (stmt->next() != NULL) { @@ -436,56 +523,81 @@ void AsmTyper::VisitDoExpression(DoExpression* expr) { void AsmTyper::VisitConditional(Conditional* expr) { - RECURSE(VisitWithExpectation(expr->condition(), cache_.kInt32, + RECURSE(VisitWithExpectation(expr->condition(), Type::Number(), "condition expected to be integer")); + if (!computed_type_->Is(cache_.kAsmInt)) { + FAIL(expr->condition(), "condition must be of type int"); + } + RECURSE(VisitWithExpectation( expr->then_expression(), expected_type_, "conditional then branch type mismatch with enclosing expression")); - Type* then_type = computed_type_; + Type* then_type = StorageType(computed_type_); + if (intish_ != 0 || !then_type->Is(cache_.kAsmComparable)) { + FAIL(expr->then_expression(), "invalid type in ? then expression"); + } + RECURSE(VisitWithExpectation( expr->else_expression(), expected_type_, "conditional else branch type mismatch with enclosing expression")); - Type* else_type = computed_type_; - Type* type = Type::Union(then_type, else_type, zone()); - if (!(type->Is(cache_.kInt32) || type->Is(cache_.kUint32) || - type->Is(cache_.kFloat32) || type->Is(cache_.kFloat64))) { - FAIL(expr, "ill-typed conditional"); + Type* else_type = StorageType(computed_type_); + if (intish_ != 0 || !else_type->Is(cache_.kAsmComparable)) { + FAIL(expr->else_expression(), "invalid type in ? else expression"); } - IntersectResult(expr, type); + + if (!then_type->Is(else_type) || !else_type->Is(then_type)) { + FAIL(expr, "then and else expressions in ? must have the same type"); + } + + IntersectResult(expr, then_type); } void AsmTyper::VisitVariableProxy(VariableProxy* expr) { Variable* var = expr->var(); - if (GetType(var) == NULL) { - FAIL(expr, "unbound variable"); + VariableInfo* info = GetVariableInfo(var, false); + if (info == NULL || info->type == NULL) { + if (var->mode() == TEMPORARY) { + SetType(var, Type::Any(zone())); + info = GetVariableInfo(var, false); + } else { + FAIL(expr, "unbound variable"); + } } - Type* type = Type::Intersect(GetType(var), expected_type_, zone()); - if (type->Is(cache_.kInt32)) { - type = cache_.kInt32; + if (property_info_ != NULL) { + SetVariableInfo(var, property_info_); + property_info_ = NULL; } - SetType(var, type); + Type* type = Type::Intersect(info->type, expected_type_, zone()); + if (type->Is(cache_.kAsmInt)) { + type = cache_.kAsmInt; + } + info->type = type; intish_ = 0; IntersectResult(expr, type); } -void AsmTyper::VisitLiteral(Literal* expr) { +void AsmTyper::VisitLiteral(Literal* expr, bool is_return) { intish_ = 0; Handle<Object> value = expr->value(); if (value->IsNumber()) { int32_t i; uint32_t u; if (expr->raw_value()->ContainsDot()) { - IntersectResult(expr, cache_.kFloat64); - } else if (value->ToUint32(&u)) { - IntersectResult(expr, cache_.kInt32); + IntersectResult(expr, cache_.kAsmDouble); + } else if (!is_return && value->ToUint32(&u)) { + if (u <= 0x7fffffff) { + IntersectResult(expr, cache_.kAsmFixnum); + } else { + IntersectResult(expr, cache_.kAsmUnsigned); + } } else if (value->ToInt32(&i)) { - IntersectResult(expr, cache_.kInt32); + IntersectResult(expr, cache_.kAsmSigned); } else { FAIL(expr, "illegal number"); } - } else if (value->IsString()) { + } else if (!is_return && value->IsString()) { IntersectResult(expr, Type::String()); } else if (value->IsUndefined()) { IntersectResult(expr, Type::Undefined()); @@ -495,6 +607,9 @@ void AsmTyper::VisitLiteral(Literal* expr) { } +void AsmTyper::VisitLiteral(Literal* expr) { VisitLiteral(expr, false); } + + void AsmTyper::VisitRegExpLiteral(RegExpLiteral* expr) { FAIL(expr, "regular expression encountered"); } @@ -555,15 +670,23 @@ void AsmTyper::VisitAssignment(Assignment* expr) { Type* type = expected_type_; RECURSE(VisitWithExpectation( expr->value(), type, "assignment value expected to match surrounding")); + Type* target_type = StorageType(computed_type_); if (intish_ != 0) { - FAIL(expr, "value still an intish"); + FAIL(expr, "intish or floatish assignment"); } - RECURSE(VisitWithExpectation(expr->target(), computed_type_, - "assignment target expected to match value")); - if (intish_ != 0) { - FAIL(expr, "value still an intish"); + if (expr->target()->IsVariableProxy()) { + RECURSE(VisitWithExpectation(expr->target(), target_type, + "assignment target expected to match value")); + } else if (expr->target()->IsProperty()) { + Property* property = expr->target()->AsProperty(); + RECURSE(VisitWithExpectation(property->obj(), Type::Any(), + "bad propety object")); + if (!computed_type_->IsArray()) { + FAIL(property->obj(), "array expected"); + } + VisitHeapAccess(property, true, target_type); } - IntersectResult(expr, computed_type_); + IntersectResult(expr, target_type); } @@ -578,137 +701,206 @@ void AsmTyper::VisitThrow(Throw* expr) { int AsmTyper::ElementShiftSize(Type* type) { - if (type->Is(cache_.kInt8) || type->Is(cache_.kUint8)) return 0; - if (type->Is(cache_.kInt16) || type->Is(cache_.kUint16)) return 1; - if (type->Is(cache_.kInt32) || type->Is(cache_.kUint32) || - type->Is(cache_.kFloat32)) - return 2; - if (type->Is(cache_.kFloat64)) return 3; + if (type->Is(cache_.kAsmSize8)) return 0; + if (type->Is(cache_.kAsmSize16)) return 1; + if (type->Is(cache_.kAsmSize32)) return 2; + if (type->Is(cache_.kAsmSize64)) return 3; return -1; } -void AsmTyper::VisitHeapAccess(Property* expr) { +Type* AsmTyper::StorageType(Type* type) { + if (type->Is(cache_.kAsmInt)) { + return cache_.kAsmInt; + } else { + return type; + } +} + + +void AsmTyper::VisitHeapAccess(Property* expr, bool assigning, + Type* assignment_type) { Type::ArrayType* array_type = computed_type_->AsArray(); size_t size = array_size_; Type* type = array_type->AsArray()->Element(); if (type->IsFunction()) { + if (assigning) { + FAIL(expr, "assigning to function table is illegal"); + } BinaryOperation* bin = expr->key()->AsBinaryOperation(); if (bin == NULL || bin->op() != Token::BIT_AND) { FAIL(expr->key(), "expected & in call"); } - RECURSE(VisitWithExpectation(bin->left(), cache_.kInt32, + RECURSE(VisitWithExpectation(bin->left(), cache_.kAsmSigned, "array index expected to be integer")); Literal* right = bin->right()->AsLiteral(); if (right == NULL || right->raw_value()->ContainsDot()) { FAIL(right, "call mask must be integer"); } - RECURSE(VisitWithExpectation(bin->right(), cache_.kInt32, + RECURSE(VisitWithExpectation(bin->right(), cache_.kAsmSigned, "call mask expected to be integer")); if (static_cast<size_t>(right->raw_value()->AsNumber()) != size - 1) { FAIL(right, "call mask must match function table"); } - bin->set_bounds(Bounds(cache_.kInt32)); + bin->set_bounds(Bounds(cache_.kAsmSigned)); + IntersectResult(expr, type); } else { Literal* literal = expr->key()->AsLiteral(); if (literal) { - RECURSE(VisitWithExpectation(literal, cache_.kInt32, + RECURSE(VisitWithExpectation(literal, cache_.kAsmSigned, "array index expected to be integer")); } else { BinaryOperation* bin = expr->key()->AsBinaryOperation(); if (bin == NULL || bin->op() != Token::SAR) { FAIL(expr->key(), "expected >> in heap access"); } - RECURSE(VisitWithExpectation(bin->left(), cache_.kInt32, + RECURSE(VisitWithExpectation(bin->left(), cache_.kAsmSigned, "array index expected to be integer")); Literal* right = bin->right()->AsLiteral(); if (right == NULL || right->raw_value()->ContainsDot()) { FAIL(right, "heap access shift must be integer"); } - RECURSE(VisitWithExpectation(bin->right(), cache_.kInt32, + RECURSE(VisitWithExpectation(bin->right(), cache_.kAsmSigned, "array shift expected to be integer")); int n = static_cast<int>(right->raw_value()->AsNumber()); int expected_shift = ElementShiftSize(type); if (expected_shift < 0 || n != expected_shift) { FAIL(right, "heap access shift must match element size"); } - bin->set_bounds(Bounds(cache_.kInt32)); + bin->set_bounds(Bounds(cache_.kAsmSigned)); + } + Type* result_type; + if (type->Is(cache_.kAsmIntArrayElement)) { + result_type = cache_.kAsmIntQ; + intish_ = kMaxUncombinedAdditiveSteps; + } else if (type->Is(cache_.kAsmFloat)) { + if (assigning) { + result_type = cache_.kAsmFloatDoubleQ; + } else { + result_type = cache_.kAsmFloatQ; + } + intish_ = 0; + } else if (type->Is(cache_.kAsmDouble)) { + if (assigning) { + result_type = cache_.kAsmFloatDoubleQ; + if (intish_ != 0) { + FAIL(expr, "Assignment of floatish to Float64Array"); + } + } else { + result_type = cache_.kAsmDoubleQ; + } + intish_ = 0; + } else { + UNREACHABLE(); + } + if (assigning) { + if (!assignment_type->Is(result_type)) { + FAIL(expr, "illegal type in assignment"); + } + } else { + IntersectResult(expr, expected_type_); + IntersectResult(expr, result_type); } } - IntersectResult(expr, type); } -void AsmTyper::VisitProperty(Property* expr) { - // stdlib.Math.x - Property* inner_prop = expr->obj()->AsProperty(); - if (inner_prop != NULL) { - // Get property name. - Literal* key = expr->key()->AsLiteral(); - if (key == NULL || !key->IsPropertyName()) - FAIL(expr, "invalid type annotation on property 2"); - Handle<String> name = key->AsPropertyName(); - - // Check that inner property name is "Math". - Literal* math_key = inner_prop->key()->AsLiteral(); - if (math_key == NULL || !math_key->IsPropertyName() || - !math_key->AsPropertyName()->IsUtf8EqualTo(CStrVector("Math"))) - FAIL(expr, "invalid type annotation on stdlib (a1)"); - - // Check that object is stdlib. - VariableProxy* proxy = inner_prop->obj()->AsVariableProxy(); - if (proxy == NULL) FAIL(expr, "invalid type annotation on stdlib (a2)"); - Variable* var = proxy->var(); - if (var->location() != VariableLocation::PARAMETER || var->index() != 0) - FAIL(expr, "invalid type annotation on stdlib (a3)"); +bool AsmTyper::IsStdlibObject(Expression* expr) { + VariableProxy* proxy = expr->AsVariableProxy(); + if (proxy == NULL) { + return false; + } + Variable* var = proxy->var(); + VariableInfo* info = GetVariableInfo(var, false); + if (info) { + if (info->standard_member == kStdlib) return true; + } + if (var->location() != VariableLocation::PARAMETER || var->index() != 0) { + return false; + } + info = GetVariableInfo(var, true); + info->type = Type::Object(); + info->standard_member = kStdlib; + return true; +} + + +Expression* AsmTyper::GetReceiverOfPropertyAccess(Expression* expr, + const char* name) { + Property* property = expr->AsProperty(); + if (property == NULL) { + return NULL; + } + Literal* key = property->key()->AsLiteral(); + if (key == NULL || !key->IsPropertyName() || + !key->AsPropertyName()->IsUtf8EqualTo(CStrVector(name))) { + return NULL; + } + return property->obj(); +} + + +bool AsmTyper::IsMathObject(Expression* expr) { + Expression* obj = GetReceiverOfPropertyAccess(expr, "Math"); + return obj && IsStdlibObject(obj); +} + + +bool AsmTyper::IsSIMDObject(Expression* expr) { + Expression* obj = GetReceiverOfPropertyAccess(expr, "SIMD"); + return obj && IsStdlibObject(obj); +} + + +bool AsmTyper::IsSIMDTypeObject(Expression* expr, const char* name) { + Expression* obj = GetReceiverOfPropertyAccess(expr, name); + return obj && IsSIMDObject(obj); +} - // Look up library type. - Type* type = LibType(stdlib_math_types_, name); - if (type == NULL) FAIL(expr, "unknown standard function 3 "); - SetResult(expr, type); + +void AsmTyper::VisitProperty(Property* expr) { + if (IsMathObject(expr->obj())) { + VisitLibraryAccess(&stdlib_math_types_, expr); + return; + } +#define V(NAME, Name, name, lane_count, lane_type) \ + if (IsSIMDTypeObject(expr->obj(), #Name)) { \ + VisitLibraryAccess(&stdlib_simd_##name##_types_, expr); \ + return; \ + } \ + if (IsSIMDTypeObject(expr, #Name)) { \ + VariableInfo* info = stdlib_simd_##name##_constructor_type_; \ + SetResult(expr, info->type); \ + property_info_ = info; \ + return; \ + } + SIMD128_TYPES(V) +#undef V + if (IsStdlibObject(expr->obj())) { + VisitLibraryAccess(&stdlib_types_, expr); return; } + property_info_ = NULL; + // Only recurse at this point so that we avoid needing // stdlib.Math to have a real type. - RECURSE(VisitWithExpectation(expr->obj(), Type::Any(), - "property holder expected to be object")); + RECURSE(VisitWithExpectation(expr->obj(), Type::Any(), "bad propety object")); // For heap view or function table access. if (computed_type_->IsArray()) { - VisitHeapAccess(expr); + VisitHeapAccess(expr, false, NULL); return; } - // Get property name. - Literal* key = expr->key()->AsLiteral(); - if (key == NULL || !key->IsPropertyName()) - FAIL(expr, "invalid type annotation on property 3"); - Handle<String> name = key->AsPropertyName(); - // stdlib.x or foreign.x VariableProxy* proxy = expr->obj()->AsVariableProxy(); if (proxy != NULL) { Variable* var = proxy->var(); - if (var->location() != VariableLocation::PARAMETER) { - FAIL(expr, "invalid type annotation on variable"); - } - switch (var->index()) { - case 0: { - // Object is stdlib, look up library type. - Type* type = LibType(stdlib_types_, name); - if (type == NULL) { - FAIL(expr, "unknown standard function 4"); - } - SetResult(expr, type); - return; - } - case 1: - // Object is foreign lib. - SetResult(expr, expected_type_); - return; - default: - FAIL(expr, "invalid type annotation on parameter"); + if (var->location() == VariableLocation::PARAMETER && var->index() == 1) { + // foreign.x is ok. + SetResult(expr, expected_type_); + return; } } @@ -719,8 +911,20 @@ void AsmTyper::VisitProperty(Property* expr) { void AsmTyper::VisitCall(Call* expr) { RECURSE(VisitWithExpectation(expr->expression(), Type::Any(), "callee expected to be any")); + StandardMember standard_member = kNone; + VariableProxy* proxy = expr->expression()->AsVariableProxy(); + if (proxy) { + standard_member = VariableAsStandardMember(proxy->var()); + } + if (!in_function_ && (proxy == NULL || standard_member != kMathFround)) { + FAIL(expr, "calls forbidden outside function bodies"); + } + if (proxy == NULL && !expr->expression()->IsProperty()) { + FAIL(expr, "calls must be to bound variables or function tables"); + } if (computed_type_->IsFunction()) { Type::FunctionType* fun_type = computed_type_->AsFunction(); + Type* result_type = fun_type->Result(); ZoneList<Expression*>* args = expr->arguments(); if (fun_type->Arity() != args->length()) { FAIL(expr, "call with wrong arity"); @@ -730,8 +934,36 @@ void AsmTyper::VisitCall(Call* expr) { RECURSE(VisitWithExpectation( arg, fun_type->Parameter(i), "call argument expected to match callee parameter")); + if (standard_member != kNone && standard_member != kMathFround && + i == 0) { + result_type = computed_type_; + } } - IntersectResult(expr, fun_type->Result()); + // Handle polymorphic stdlib functions specially. + if (standard_member == kMathCeil || standard_member == kMathFloor || + standard_member == kMathSqrt) { + if (!args->at(0)->bounds().upper->Is(cache_.kAsmFloat) && + !args->at(0)->bounds().upper->Is(cache_.kAsmDouble)) { + FAIL(expr, "illegal function argument type"); + } + } else if (standard_member == kMathAbs || standard_member == kMathMin || + standard_member == kMathMax) { + if (!args->at(0)->bounds().upper->Is(cache_.kAsmFloat) && + !args->at(0)->bounds().upper->Is(cache_.kAsmDouble) && + !args->at(0)->bounds().upper->Is(cache_.kAsmSigned)) { + FAIL(expr, "illegal function argument type"); + } + if (args->length() > 1) { + Type* other = Type::Intersect(args->at(0)->bounds().upper, + args->at(1)->bounds().upper, zone()); + if (!other->Is(cache_.kAsmFloat) && !other->Is(cache_.kAsmDouble) && + !other->Is(cache_.kAsmSigned)) { + FAIL(expr, "function arguments types don't match"); + } + } + } + intish_ = 0; + IntersectResult(expr, result_type); } else if (computed_type_->Is(Type::Any())) { // For foreign calls. ZoneList<Expression*>* args = expr->arguments(); @@ -740,6 +972,7 @@ void AsmTyper::VisitCall(Call* expr) { RECURSE(VisitWithExpectation(arg, Type::Any(), "foreign call argument expected to be any")); } + intish_ = kMaxUncombinedAdditiveSteps; IntersectResult(expr, Type::Number()); } else { FAIL(expr, "invalid callee"); @@ -780,9 +1013,9 @@ void AsmTyper::VisitCallRuntime(CallRuntime* expr) { void AsmTyper::VisitUnaryOperation(UnaryOperation* expr) { switch (expr->op()) { case Token::NOT: // Used to encode != and !== - RECURSE(VisitWithExpectation(expr->expression(), cache_.kInt32, + RECURSE(VisitWithExpectation(expr->expression(), cache_.kAsmInt, "operand expected to be integer")); - IntersectResult(expr, cache_.kInt32); + IntersectResult(expr, cache_.kAsmSigned); return; case Token::DELETE: FAIL(expr, "delete operator encountered"); @@ -805,24 +1038,40 @@ void AsmTyper::VisitIntegerBitwiseOperator(BinaryOperation* expr, Type* left_expected, Type* right_expected, Type* result_type, bool conversion) { - RECURSE(VisitWithExpectation(expr->left(), left_expected, - "left bit operand expected to be integer")); + RECURSE(VisitWithExpectation(expr->left(), Type::Number(), + "left bitwise operand expected to be a number")); int left_intish = intish_; Type* left_type = computed_type_; - RECURSE(VisitWithExpectation(expr->right(), right_expected, - "right bit operand expected to be integer")); + if (!left_type->Is(left_expected)) { + FAIL(expr->left(), "left bitwise operand expected to be an integer"); + } + if (left_intish > kMaxUncombinedAdditiveSteps) { + FAIL(expr->left(), "too many consecutive additive ops"); + } + + RECURSE( + VisitWithExpectation(expr->right(), Type::Number(), + "right bitwise operand expected to be a number")); int right_intish = intish_; Type* right_type = computed_type_; - if (left_intish > kMaxUncombinedAdditiveSteps) { - FAIL(expr, "too many consecutive additive ops"); + if (!right_type->Is(right_expected)) { + FAIL(expr->right(), "right bitwise operand expected to be an integer"); } if (right_intish > kMaxUncombinedAdditiveSteps) { - FAIL(expr, "too many consecutive additive ops"); + FAIL(expr->right(), "too many consecutive additive ops"); } + intish_ = 0; + + if (left_type->Is(cache_.kAsmFixnum) && right_type->Is(cache_.kAsmInt)) { + left_type = right_type; + } + if (right_type->Is(cache_.kAsmFixnum) && left_type->Is(cache_.kAsmInt)) { + right_type = left_type; + } if (!conversion) { if (!left_type->Is(right_type) || !right_type->Is(left_type)) { - FAIL(expr, "ill typed bitwise operation"); + FAIL(expr, "ill-typed bitwise operation"); } } IntersectResult(expr, result_type); @@ -841,29 +1090,42 @@ void AsmTyper::VisitBinaryOperation(BinaryOperation* expr) { } case Token::OR: case Token::AND: - FAIL(expr, "logical operator encountered"); + FAIL(expr, "illegal logical operator"); case Token::BIT_OR: { // BIT_OR allows Any since it is used as a type coercion. - VisitIntegerBitwiseOperator(expr, Type::Any(), cache_.kIntegral32, - cache_.kInt32, true); + VisitIntegerBitwiseOperator(expr, Type::Any(), cache_.kAsmInt, + cache_.kAsmSigned, true); return; } case Token::BIT_XOR: { + // Handle booleans specially to handle de-sugared ! + Literal* left = expr->left()->AsLiteral(); + if (left && left->value()->IsBoolean()) { + if (left->ToBooleanIsTrue()) { + left->set_bounds(Bounds(cache_.kSingletonOne)); + RECURSE(VisitWithExpectation(expr->right(), cache_.kAsmInt, + "not operator expects an integer")); + IntersectResult(expr, cache_.kAsmSigned); + return; + } else { + FAIL(left, "unexpected false"); + } + } // BIT_XOR allows Number since it is used as a type coercion (via ~~). - VisitIntegerBitwiseOperator(expr, Type::Number(), cache_.kIntegral32, - cache_.kInt32, true); + VisitIntegerBitwiseOperator(expr, Type::Number(), cache_.kAsmInt, + cache_.kAsmSigned, true); return; } case Token::SHR: { - VisitIntegerBitwiseOperator(expr, cache_.kIntegral32, cache_.kIntegral32, - cache_.kUint32, false); + VisitIntegerBitwiseOperator(expr, cache_.kAsmInt, cache_.kAsmInt, + cache_.kAsmUnsigned, false); return; } case Token::SHL: case Token::SAR: case Token::BIT_AND: { - VisitIntegerBitwiseOperator(expr, cache_.kIntegral32, cache_.kIntegral32, - cache_.kInt32, false); + VisitIntegerBitwiseOperator(expr, cache_.kAsmInt, cache_.kAsmInt, + cache_.kAsmSigned, false); return; } case Token::ADD: @@ -882,13 +1144,25 @@ void AsmTyper::VisitBinaryOperation(BinaryOperation* expr) { Type* right_type = computed_type_; int right_intish = intish_; Type* type = Type::Union(left_type, right_type, zone()); - if (type->Is(cache_.kInt32) || type->Is(cache_.kUint32)) { + if (type->Is(cache_.kAsmInt)) { if (expr->op() == Token::MUL) { - if (!expr->left()->IsLiteral() && !expr->right()->IsLiteral()) { + Literal* right = expr->right()->AsLiteral(); + if (!right) { FAIL(expr, "direct integer multiply forbidden"); } - intish_ = 0; - IntersectResult(expr, cache_.kInt32); + if (!right->value()->IsNumber()) { + FAIL(expr, "multiply must be by an integer"); + } + int32_t i; + if (!right->value()->ToInt32(&i)) { + FAIL(expr, "multiply must be a signed integer"); + } + i = abs(i); + if (i >= 1 << 20) { + FAIL(expr, "multiply must be by value in -2^20 < n < 2^20"); + } + intish_ = i; + IntersectResult(expr, cache_.kAsmInt); return; } else { intish_ = left_intish + right_intish + 1; @@ -901,20 +1175,23 @@ void AsmTyper::VisitBinaryOperation(BinaryOperation* expr) { FAIL(expr, "too many consecutive multiplicative ops"); } } - IntersectResult(expr, cache_.kInt32); + IntersectResult(expr, cache_.kAsmInt); return; } - } else if (expr->op() == Token::MUL && - left_type->Is(cache_.kIntegral32) && - right_type->Is(cache_.kFloat64)) { + } else if (expr->op() == Token::MUL && expr->right()->IsLiteral() && + right_type->Is(cache_.kAsmDouble)) { // For unary +, expressed as x * 1.0 - IntersectResult(expr, cache_.kFloat64); + IntersectResult(expr, cache_.kAsmDouble); return; - } else if (type->Is(cache_.kFloat32) && expr->op() != Token::MOD) { - IntersectResult(expr, cache_.kFloat32); + } else if (type->Is(cache_.kAsmFloat) && expr->op() != Token::MOD) { + if (left_intish != 0 || right_intish != 0) { + FAIL(expr, "float operation before required fround"); + } + IntersectResult(expr, cache_.kAsmFloat); + intish_ = 1; return; - } else if (type->Is(cache_.kFloat64)) { - IntersectResult(expr, cache_.kFloat64); + } else if (type->Is(cache_.kAsmDouble)) { + IntersectResult(expr, cache_.kAsmDouble); return; } else { FAIL(expr, "ill-typed arithmetic operation"); @@ -927,22 +1204,33 @@ void AsmTyper::VisitBinaryOperation(BinaryOperation* expr) { void AsmTyper::VisitCompareOperation(CompareOperation* expr) { + Token::Value op = expr->op(); + if (op != Token::EQ && op != Token::NE && op != Token::LT && + op != Token::LTE && op != Token::GT && op != Token::GTE) { + FAIL(expr, "illegal comparison operator"); + } + RECURSE( VisitWithExpectation(expr->left(), Type::Number(), "left comparison operand expected to be number")); Type* left_type = computed_type_; + if (!left_type->Is(cache_.kAsmComparable)) { + FAIL(expr->left(), "bad type on left side of comparison"); + } + RECURSE( VisitWithExpectation(expr->right(), Type::Number(), "right comparison operand expected to be number")); Type* right_type = computed_type_; - Type* type = Type::Union(left_type, right_type, zone()); - expr->set_combined_type(type); - if (type->Is(cache_.kInt32) || type->Is(cache_.kUint32) || - type->Is(cache_.kFloat32) || type->Is(cache_.kFloat64)) { - IntersectResult(expr, cache_.kInt32); - } else { - FAIL(expr, "ill-typed comparison operation"); + if (!right_type->Is(cache_.kAsmComparable)) { + FAIL(expr->right(), "bad type on right side of comparison"); + } + + if (!left_type->Is(right_type) && !right_type->Is(left_type)) { + FAIL(expr, "left and right side of comparison must match"); } + + IntersectResult(expr, cache_.kAsmSigned); } @@ -987,64 +1275,115 @@ void AsmTyper::VisitSuperCallReference(SuperCallReference* expr) { } +void AsmTyper::InitializeStdlibSIMD() { +#define V(NAME, Name, name, lane_count, lane_type) \ + { \ + Type* type = Type::Function(Type::Name(isolate_, zone()), Type::Any(), \ + lane_count, zone()); \ + for (int i = 0; i < lane_count; ++i) { \ + type->AsFunction()->InitParameter(i, Type::Number()); \ + } \ + stdlib_simd_##name##_constructor_type_ = new (zone()) VariableInfo(type); \ + stdlib_simd_##name##_constructor_type_->is_constructor_function = true; \ + } + SIMD128_TYPES(V) +#undef V +} + + void AsmTyper::InitializeStdlib() { + if (allow_simd_) { + InitializeStdlibSIMD(); + } Type* number_type = Type::Number(zone()); - Type* double_type = cache_.kFloat64; + Type* double_type = cache_.kAsmDouble; Type* double_fn1_type = Type::Function(double_type, double_type, zone()); Type* double_fn2_type = Type::Function(double_type, double_type, double_type, zone()); - Type* fround_type = Type::Function(cache_.kFloat32, number_type, zone()); + Type* fround_type = Type::Function(cache_.kAsmFloat, number_type, zone()); Type* imul_type = - Type::Function(cache_.kInt32, cache_.kInt32, cache_.kInt32, zone()); + Type::Function(cache_.kAsmSigned, cache_.kAsmInt, cache_.kAsmInt, zone()); // TODO(bradnelson): currently only approximating the proper intersection type // (which we cannot currently represent). - Type* abs_type = Type::Function(number_type, number_type, zone()); + Type* number_fn1_type = Type::Function(number_type, number_type, zone()); + Type* number_fn2_type = + Type::Function(number_type, number_type, number_type, zone()); struct Assignment { const char* name; + StandardMember standard_member; Type* type; }; - const Assignment math[] = { - {"PI", double_type}, {"E", double_type}, - {"LN2", double_type}, {"LN10", double_type}, - {"LOG2E", double_type}, {"LOG10E", double_type}, - {"SQRT2", double_type}, {"SQRT1_2", double_type}, - {"imul", imul_type}, {"abs", abs_type}, - {"ceil", double_fn1_type}, {"floor", double_fn1_type}, - {"fround", fround_type}, {"pow", double_fn2_type}, - {"exp", double_fn1_type}, {"log", double_fn1_type}, - {"min", double_fn2_type}, {"max", double_fn2_type}, - {"sqrt", double_fn1_type}, {"cos", double_fn1_type}, - {"sin", double_fn1_type}, {"tan", double_fn1_type}, - {"acos", double_fn1_type}, {"asin", double_fn1_type}, - {"atan", double_fn1_type}, {"atan2", double_fn2_type}}; + const Assignment math[] = {{"PI", kMathPI, double_type}, + {"E", kMathE, double_type}, + {"LN2", kMathLN2, double_type}, + {"LN10", kMathLN10, double_type}, + {"LOG2E", kMathLOG2E, double_type}, + {"LOG10E", kMathLOG10E, double_type}, + {"SQRT2", kMathSQRT2, double_type}, + {"SQRT1_2", kMathSQRT1_2, double_type}, + {"imul", kMathImul, imul_type}, + {"abs", kMathAbs, number_fn1_type}, + {"ceil", kMathCeil, number_fn1_type}, + {"floor", kMathFloor, number_fn1_type}, + {"fround", kMathFround, fround_type}, + {"pow", kMathPow, double_fn2_type}, + {"exp", kMathExp, double_fn1_type}, + {"log", kMathLog, double_fn1_type}, + {"min", kMathMin, number_fn2_type}, + {"max", kMathMax, number_fn2_type}, + {"sqrt", kMathSqrt, number_fn1_type}, + {"cos", kMathCos, double_fn1_type}, + {"sin", kMathSin, double_fn1_type}, + {"tan", kMathTan, double_fn1_type}, + {"acos", kMathAcos, double_fn1_type}, + {"asin", kMathAsin, double_fn1_type}, + {"atan", kMathAtan, double_fn1_type}, + {"atan2", kMathAtan2, double_fn2_type}}; for (unsigned i = 0; i < arraysize(math); ++i) { - stdlib_math_types_[math[i].name] = math[i].type; + stdlib_math_types_[math[i].name] = new (zone()) VariableInfo(math[i].type); + stdlib_math_types_[math[i].name]->standard_member = math[i].standard_member; } + stdlib_math_types_["fround"]->is_check_function = true; - stdlib_types_["Infinity"] = double_type; - stdlib_types_["NaN"] = double_type; + stdlib_types_["Infinity"] = new (zone()) VariableInfo(double_type); + stdlib_types_["Infinity"]->standard_member = kInfinity; + stdlib_types_["NaN"] = new (zone()) VariableInfo(double_type); + stdlib_types_["NaN"]->standard_member = kNaN; Type* buffer_type = Type::Any(zone()); #define TYPED_ARRAY(TypeName, type_name, TYPE_NAME, ctype, size) \ - stdlib_types_[#TypeName "Array"] = \ - Type::Function(cache_.k##TypeName##Array, buffer_type, zone()); + stdlib_types_[#TypeName "Array"] = new (zone()) VariableInfo( \ + Type::Function(cache_.k##TypeName##Array, buffer_type, zone())); TYPED_ARRAYS(TYPED_ARRAY) #undef TYPED_ARRAY -#define TYPED_ARRAY(TypeName, type_name, TYPE_NAME, ctype, size) \ - stdlib_heap_types_[#TypeName "Array"] = \ - Type::Function(cache_.k##TypeName##Array, buffer_type, zone()); +#define TYPED_ARRAY(TypeName, type_name, TYPE_NAME, ctype, size) \ + stdlib_heap_types_[#TypeName "Array"] = new (zone()) VariableInfo( \ + Type::Function(cache_.k##TypeName##Array, buffer_type, zone())); TYPED_ARRAYS(TYPED_ARRAY) #undef TYPED_ARRAY } -Type* AsmTyper::LibType(ObjectTypeMap map, Handle<String> name) { +void AsmTyper::VisitLibraryAccess(ObjectTypeMap* map, Property* expr) { + Literal* key = expr->key()->AsLiteral(); + if (key == NULL || !key->IsPropertyName()) + FAIL(expr, "invalid key used on stdlib member"); + Handle<String> name = key->AsPropertyName(); + VariableInfo* info = LibType(map, name); + if (info == NULL || info->type == NULL) FAIL(expr, "unknown stdlib function"); + SetResult(expr, info->type); + property_info_ = info; +} + + +AsmTyper::VariableInfo* AsmTyper::LibType(ObjectTypeMap* map, + Handle<String> name) { base::SmartArrayPointer<char> aname = name->ToCString(); - ObjectTypeMap::iterator i = map.find(std::string(aname.get())); - if (i == map.end()) { + ObjectTypeMap::iterator i = map->find(std::string(aname.get())); + if (i == map->end()) { return NULL; } return i->second; @@ -1052,32 +1391,62 @@ Type* AsmTyper::LibType(ObjectTypeMap map, Handle<String> name) { void AsmTyper::SetType(Variable* variable, Type* type) { - ZoneHashMap::Entry* entry; - if (in_function_) { - entry = local_variable_type_.LookupOrInsert( - variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone())); - } else { - entry = global_variable_type_.LookupOrInsert( - variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone())); - } - entry->value = reinterpret_cast<void*>(type); + VariableInfo* info = GetVariableInfo(variable, true); + info->type = type; } Type* AsmTyper::GetType(Variable* variable) { - i::ZoneHashMap::Entry* entry = NULL; + VariableInfo* info = GetVariableInfo(variable, false); + if (!info) return NULL; + return info->type; +} + + +AsmTyper::VariableInfo* AsmTyper::GetVariableInfo(Variable* variable, + bool setting) { + ZoneHashMap::Entry* entry; + ZoneHashMap* map; if (in_function_) { - entry = local_variable_type_.Lookup(variable, ComputePointerHash(variable)); - } - if (entry == NULL) { - entry = - global_variable_type_.Lookup(variable, ComputePointerHash(variable)); + map = &local_variable_type_; + } else { + map = &global_variable_type_; } - if (entry == NULL) { - return NULL; + if (setting) { + entry = map->LookupOrInsert(variable, ComputePointerHash(variable), + ZoneAllocationPolicy(zone())); } else { - return reinterpret_cast<Type*>(entry->value); + entry = map->Lookup(variable, ComputePointerHash(variable)); + if (!entry && in_function_) { + entry = + global_variable_type_.Lookup(variable, ComputePointerHash(variable)); + if (entry && entry->value) { + } + } + } + if (!entry) return NULL; + if (!entry->value) { + if (!setting) return NULL; + entry->value = new (zone()) VariableInfo; } + return reinterpret_cast<VariableInfo*>(entry->value); +} + + +void AsmTyper::SetVariableInfo(Variable* variable, const VariableInfo* info) { + VariableInfo* dest = GetVariableInfo(variable, true); + dest->type = info->type; + dest->is_check_function = info->is_check_function; + dest->is_constructor_function = info->is_constructor_function; + dest->standard_member = info->standard_member; +} + + +AsmTyper::StandardMember AsmTyper::VariableAsStandardMember( + Variable* variable) { + VariableInfo* info = GetVariableInfo(variable, false); + if (!info) return kNone; + return info->standard_member; } @@ -1111,5 +1480,13 @@ void AsmTyper::VisitWithExpectation(Expression* expr, Type* expected_type, } expected_type_ = save; } + + +void AsmTyper::VisitRewritableAssignmentExpression( + RewritableAssignmentExpression* expr) { + RECURSE(Visit(expr->expression())); +} + + } // namespace internal } // namespace v8 |