Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/typing-asm.h" |
| 6 | |
| 7 | #include <limits> |
| 8 | |
| 9 | #include "src/v8.h" |
| 10 | |
| 11 | #include "src/ast/ast.h" |
| 12 | #include "src/ast/scopes.h" |
| 13 | #include "src/codegen.h" |
| 14 | #include "src/type-cache.h" |
| 15 | |
| 16 | namespace v8 { |
| 17 | namespace internal { |
| 18 | |
| 19 | #define FAIL(node, msg) \ |
| 20 | do { \ |
| 21 | valid_ = false; \ |
| 22 | int line = node->position() == RelocInfo::kNoPosition \ |
| 23 | ? -1 \ |
| 24 | : script_->GetLineNumber(node->position()); \ |
| 25 | base::OS::SNPrintF(error_message_, sizeof(error_message_), \ |
| 26 | "asm: line %d: %s\n", line + 1, msg); \ |
| 27 | return; \ |
| 28 | } while (false) |
| 29 | |
| 30 | |
| 31 | #define RECURSE(call) \ |
| 32 | do { \ |
| 33 | DCHECK(!HasStackOverflow()); \ |
| 34 | call; \ |
| 35 | if (HasStackOverflow()) return; \ |
| 36 | if (!valid_) return; \ |
| 37 | } while (false) |
| 38 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 39 | AsmTyper::AsmTyper(Isolate* isolate, Zone* zone, Script* script, |
| 40 | FunctionLiteral* root) |
| 41 | : zone_(zone), |
| 42 | isolate_(isolate), |
| 43 | script_(script), |
| 44 | root_(root), |
| 45 | valid_(true), |
| 46 | allow_simd_(false), |
| 47 | property_info_(NULL), |
| 48 | intish_(0), |
| 49 | stdlib_types_(zone), |
| 50 | stdlib_heap_types_(zone), |
| 51 | stdlib_math_types_(zone), |
| 52 | #define V(NAME, Name, name, lane_count, lane_type) \ |
| 53 | stdlib_simd_##name##_types_(zone), |
| 54 | SIMD128_TYPES(V) |
| 55 | #undef V |
| 56 | global_variable_type_(HashMap::PointersMatch, |
| 57 | ZoneHashMap::kDefaultHashMapCapacity, |
| 58 | ZoneAllocationPolicy(zone)), |
| 59 | local_variable_type_(HashMap::PointersMatch, |
| 60 | ZoneHashMap::kDefaultHashMapCapacity, |
| 61 | ZoneAllocationPolicy(zone)), |
| 62 | in_function_(false), |
| 63 | building_function_tables_(false), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 64 | visiting_exports_(false), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | cache_(TypeCache::Get()) { |
| 66 | InitializeAstVisitor(isolate); |
| 67 | InitializeStdlib(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | bool AsmTyper::Validate() { |
| 72 | VisitAsmModule(root_); |
| 73 | return valid_ && !HasStackOverflow(); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void AsmTyper::VisitAsmModule(FunctionLiteral* fun) { |
| 78 | Scope* scope = fun->scope(); |
| 79 | if (!scope->is_function_scope()) FAIL(fun, "not at function scope"); |
| 80 | |
| 81 | ExpressionStatement* use_asm = fun->body()->first()->AsExpressionStatement(); |
| 82 | if (use_asm == NULL) FAIL(fun, "missing \"use asm\""); |
| 83 | Literal* use_asm_literal = use_asm->expression()->AsLiteral(); |
| 84 | if (use_asm_literal == NULL) FAIL(fun, "missing \"use asm\""); |
| 85 | if (!use_asm_literal->raw_value()->AsString()->IsOneByteEqualTo("use asm")) |
| 86 | FAIL(fun, "missing \"use asm\""); |
| 87 | |
| 88 | // Module parameters. |
| 89 | for (int i = 0; i < scope->num_parameters(); ++i) { |
| 90 | Variable* param = scope->parameter(i); |
| 91 | DCHECK(GetType(param) == NULL); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 92 | SetType(param, Type::None()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | ZoneList<Declaration*>* decls = scope->declarations(); |
| 96 | |
| 97 | // Set all globals to type Any. |
| 98 | VariableDeclaration* decl = scope->function(); |
| 99 | if (decl != NULL) SetType(decl->proxy()->var(), Type::None()); |
| 100 | RECURSE(VisitDeclarations(scope->declarations())); |
| 101 | |
| 102 | // Validate global variables. |
| 103 | RECURSE(VisitStatements(fun->body())); |
| 104 | |
| 105 | // Validate function annotations. |
| 106 | for (int i = 0; i < decls->length(); ++i) { |
| 107 | FunctionDeclaration* decl = decls->at(i)->AsFunctionDeclaration(); |
| 108 | if (decl != NULL) { |
| 109 | RECURSE(VisitFunctionAnnotation(decl->fun())); |
| 110 | Variable* var = decl->proxy()->var(); |
| 111 | if (property_info_ != NULL) { |
| 112 | SetVariableInfo(var, property_info_); |
| 113 | property_info_ = NULL; |
| 114 | } |
| 115 | SetType(var, computed_type_); |
| 116 | DCHECK(GetType(var) != NULL); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Build function tables. |
| 121 | building_function_tables_ = true; |
| 122 | RECURSE(VisitStatements(fun->body())); |
| 123 | building_function_tables_ = false; |
| 124 | |
| 125 | // Validate function bodies. |
| 126 | for (int i = 0; i < decls->length(); ++i) { |
| 127 | FunctionDeclaration* decl = decls->at(i)->AsFunctionDeclaration(); |
| 128 | if (decl != NULL) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 129 | RECURSE(VisitWithExpectation(decl->fun(), Type::Any(), "UNREACHABLE")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | if (!computed_type_->IsFunction()) { |
| 131 | FAIL(decl->fun(), "function literal expected to be a function"); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Validate exports. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 137 | visiting_exports_ = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | ReturnStatement* stmt = fun->body()->last()->AsReturnStatement(); |
| 139 | if (stmt == nullptr) { |
| 140 | FAIL(fun->body()->last(), "last statement in module is not a return"); |
| 141 | } |
| 142 | RECURSE(VisitWithExpectation(stmt->expression(), Type::Object(), |
| 143 | "expected object export")); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void AsmTyper::VisitVariableDeclaration(VariableDeclaration* decl) { |
| 148 | Variable* var = decl->proxy()->var(); |
| 149 | if (var->location() != VariableLocation::PARAMETER) { |
| 150 | if (GetType(var) == NULL) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 151 | SetType(var, Type::Any()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 152 | } else { |
| 153 | DCHECK(!GetType(var)->IsFunction()); |
| 154 | } |
| 155 | } |
| 156 | DCHECK(GetType(var) != NULL); |
| 157 | intish_ = 0; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | void AsmTyper::VisitFunctionDeclaration(FunctionDeclaration* decl) { |
| 162 | if (in_function_) { |
| 163 | FAIL(decl, "function declared inside another"); |
| 164 | } |
| 165 | // Set function type so global references to functions have some type |
| 166 | // (so they can give a more useful error). |
| 167 | Variable* var = decl->proxy()->var(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 168 | SetType(var, Type::Function()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| 172 | void AsmTyper::VisitFunctionAnnotation(FunctionLiteral* fun) { |
| 173 | // Extract result type. |
| 174 | ZoneList<Statement*>* body = fun->body(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 175 | Type* result_type = Type::Undefined(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 176 | if (body->length() > 0) { |
| 177 | ReturnStatement* stmt = body->last()->AsReturnStatement(); |
| 178 | if (stmt != NULL) { |
| 179 | Literal* literal = stmt->expression()->AsLiteral(); |
| 180 | Type* old_expected = expected_type_; |
| 181 | expected_type_ = Type::Any(); |
| 182 | if (literal) { |
| 183 | RECURSE(VisitLiteral(literal, true)); |
| 184 | } else { |
| 185 | RECURSE(VisitExpressionAnnotation(stmt->expression(), NULL, true)); |
| 186 | } |
| 187 | expected_type_ = old_expected; |
| 188 | result_type = computed_type_; |
| 189 | } |
| 190 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 191 | Type* type = |
| 192 | Type::Function(result_type, Type::Any(), fun->parameter_count(), zone()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 193 | |
| 194 | // Extract parameter types. |
| 195 | bool good = true; |
| 196 | for (int i = 0; i < fun->parameter_count(); ++i) { |
| 197 | good = false; |
| 198 | if (i >= body->length()) break; |
| 199 | ExpressionStatement* stmt = body->at(i)->AsExpressionStatement(); |
| 200 | if (stmt == NULL) break; |
| 201 | Assignment* expr = stmt->expression()->AsAssignment(); |
| 202 | if (expr == NULL || expr->is_compound()) break; |
| 203 | VariableProxy* proxy = expr->target()->AsVariableProxy(); |
| 204 | if (proxy == NULL) break; |
| 205 | Variable* var = proxy->var(); |
| 206 | if (var->location() != VariableLocation::PARAMETER || var->index() != i) |
| 207 | break; |
| 208 | RECURSE(VisitExpressionAnnotation(expr->value(), var, false)); |
| 209 | if (property_info_ != NULL) { |
| 210 | SetVariableInfo(var, property_info_); |
| 211 | property_info_ = NULL; |
| 212 | } |
| 213 | SetType(var, computed_type_); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 214 | type->AsFunction()->InitParameter(i, computed_type_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 215 | good = true; |
| 216 | } |
| 217 | if (!good) FAIL(fun, "missing parameter type annotations"); |
| 218 | |
| 219 | SetResult(fun, type); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | void AsmTyper::VisitExpressionAnnotation(Expression* expr, Variable* var, |
| 224 | bool is_return) { |
| 225 | // Normal +x or x|0 annotations. |
| 226 | BinaryOperation* bin = expr->AsBinaryOperation(); |
| 227 | if (bin != NULL) { |
| 228 | if (var != NULL) { |
| 229 | VariableProxy* proxy = bin->left()->AsVariableProxy(); |
| 230 | if (proxy == NULL) { |
| 231 | FAIL(bin->left(), "expected variable for type annotation"); |
| 232 | } |
| 233 | if (proxy->var() != var) { |
| 234 | FAIL(proxy, "annotation source doesn't match destination"); |
| 235 | } |
| 236 | } |
| 237 | Literal* right = bin->right()->AsLiteral(); |
| 238 | if (right != NULL) { |
| 239 | switch (bin->op()) { |
| 240 | case Token::MUL: // We encode +x as x*1.0 |
| 241 | if (right->raw_value()->ContainsDot() && |
| 242 | right->raw_value()->AsNumber() == 1.0) { |
| 243 | SetResult(expr, cache_.kAsmDouble); |
| 244 | return; |
| 245 | } |
| 246 | break; |
| 247 | case Token::BIT_OR: |
| 248 | if (!right->raw_value()->ContainsDot() && |
| 249 | right->raw_value()->AsNumber() == 0.0) { |
| 250 | if (is_return) { |
| 251 | SetResult(expr, cache_.kAsmSigned); |
| 252 | } else { |
| 253 | SetResult(expr, cache_.kAsmInt); |
| 254 | } |
| 255 | return; |
| 256 | } |
| 257 | break; |
| 258 | default: |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | FAIL(expr, "invalid type annotation on binary op"); |
| 263 | } |
| 264 | |
| 265 | // Numbers or the undefined literal (for empty returns). |
| 266 | if (expr->IsLiteral()) { |
| 267 | RECURSE(VisitWithExpectation(expr, Type::Any(), "invalid literal")); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | Call* call = expr->AsCall(); |
| 272 | if (call != NULL) { |
| 273 | VariableProxy* proxy = call->expression()->AsVariableProxy(); |
| 274 | if (proxy != NULL) { |
| 275 | VariableInfo* info = GetVariableInfo(proxy->var(), false); |
| 276 | if (!info || |
| 277 | (!info->is_check_function && !info->is_constructor_function)) { |
| 278 | if (allow_simd_) { |
| 279 | FAIL(call->expression(), |
| 280 | "only fround/SIMD.checks allowed on expression annotations"); |
| 281 | } else { |
| 282 | FAIL(call->expression(), |
| 283 | "only fround allowed on expression annotations"); |
| 284 | } |
| 285 | } |
| 286 | Type* type = info->type; |
| 287 | DCHECK(type->IsFunction()); |
| 288 | if (info->is_check_function) { |
| 289 | DCHECK(type->AsFunction()->Arity() == 1); |
| 290 | } |
| 291 | if (call->arguments()->length() != type->AsFunction()->Arity()) { |
| 292 | FAIL(call, "invalid argument count calling function"); |
| 293 | } |
| 294 | SetResult(expr, type->AsFunction()->Result()); |
| 295 | return; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | FAIL(expr, "invalid type annotation"); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | void AsmTyper::VisitStatements(ZoneList<Statement*>* stmts) { |
| 304 | for (int i = 0; i < stmts->length(); ++i) { |
| 305 | Statement* stmt = stmts->at(i); |
| 306 | RECURSE(Visit(stmt)); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | |
| 311 | void AsmTyper::VisitBlock(Block* stmt) { |
| 312 | RECURSE(VisitStatements(stmt->statements())); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | void AsmTyper::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 317 | RECURSE(VisitWithExpectation(stmt->expression(), Type::Any(), |
| 318 | "expression statement expected to be any")); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | void AsmTyper::VisitEmptyStatement(EmptyStatement* stmt) {} |
| 323 | |
| 324 | |
| 325 | void AsmTyper::VisitSloppyBlockFunctionStatement( |
| 326 | SloppyBlockFunctionStatement* stmt) { |
| 327 | Visit(stmt->statement()); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | void AsmTyper::VisitEmptyParentheses(EmptyParentheses* expr) { UNREACHABLE(); } |
| 332 | |
| 333 | |
| 334 | void AsmTyper::VisitIfStatement(IfStatement* stmt) { |
| 335 | if (!in_function_) { |
| 336 | FAIL(stmt, "if statement inside module body"); |
| 337 | } |
| 338 | RECURSE(VisitWithExpectation(stmt->condition(), cache_.kAsmSigned, |
| 339 | "if condition expected to be integer")); |
| 340 | RECURSE(Visit(stmt->then_statement())); |
| 341 | RECURSE(Visit(stmt->else_statement())); |
| 342 | } |
| 343 | |
| 344 | |
| 345 | void AsmTyper::VisitContinueStatement(ContinueStatement* stmt) { |
| 346 | if (!in_function_) { |
| 347 | FAIL(stmt, "continue statement inside module body"); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | |
| 352 | void AsmTyper::VisitBreakStatement(BreakStatement* stmt) { |
| 353 | if (!in_function_) { |
| 354 | FAIL(stmt, "continue statement inside module body"); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | |
| 359 | void AsmTyper::VisitReturnStatement(ReturnStatement* stmt) { |
| 360 | // Handle module return statement in VisitAsmModule. |
| 361 | if (!in_function_) { |
| 362 | return; |
| 363 | } |
| 364 | Literal* literal = stmt->expression()->AsLiteral(); |
| 365 | if (literal) { |
| 366 | VisitLiteral(literal, true); |
| 367 | } else { |
| 368 | RECURSE( |
| 369 | VisitWithExpectation(stmt->expression(), Type::Any(), |
| 370 | "return expression expected to have return type")); |
| 371 | } |
| 372 | if (!computed_type_->Is(return_type_) || !return_type_->Is(computed_type_)) { |
| 373 | FAIL(stmt->expression(), "return type does not match function signature"); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | |
| 378 | void AsmTyper::VisitWithStatement(WithStatement* stmt) { |
| 379 | FAIL(stmt, "bad with statement"); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | void AsmTyper::VisitSwitchStatement(SwitchStatement* stmt) { |
| 384 | if (!in_function_) { |
| 385 | FAIL(stmt, "switch statement inside module body"); |
| 386 | } |
| 387 | RECURSE(VisitWithExpectation(stmt->tag(), cache_.kAsmSigned, |
| 388 | "switch expression non-integer")); |
| 389 | ZoneList<CaseClause*>* clauses = stmt->cases(); |
| 390 | ZoneSet<int32_t> cases(zone()); |
| 391 | for (int i = 0; i < clauses->length(); ++i) { |
| 392 | CaseClause* clause = clauses->at(i); |
| 393 | if (clause->is_default()) { |
| 394 | if (i != clauses->length() - 1) { |
| 395 | FAIL(clause, "default case out of order"); |
| 396 | } |
| 397 | } else { |
| 398 | Expression* label = clause->label(); |
| 399 | RECURSE(VisitWithExpectation(label, cache_.kAsmSigned, |
| 400 | "case label non-integer")); |
| 401 | if (!label->IsLiteral()) FAIL(label, "non-literal case label"); |
| 402 | Handle<Object> value = label->AsLiteral()->value(); |
| 403 | int32_t value32; |
| 404 | if (!value->ToInt32(&value32)) FAIL(label, "illegal case label value"); |
| 405 | if (cases.find(value32) != cases.end()) { |
| 406 | FAIL(label, "duplicate case value"); |
| 407 | } |
| 408 | cases.insert(value32); |
| 409 | } |
| 410 | // TODO(bradnelson): Detect duplicates. |
| 411 | ZoneList<Statement*>* stmts = clause->statements(); |
| 412 | RECURSE(VisitStatements(stmts)); |
| 413 | } |
| 414 | if (cases.size() > 0) { |
| 415 | int64_t min_case = *cases.begin(); |
| 416 | int64_t max_case = *cases.rbegin(); |
| 417 | if (max_case - min_case > std::numeric_limits<int32_t>::max()) { |
| 418 | FAIL(stmt, "case range too large"); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | |
| 424 | void AsmTyper::VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } |
| 425 | |
| 426 | |
| 427 | void AsmTyper::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 428 | if (!in_function_) { |
| 429 | FAIL(stmt, "do statement inside module body"); |
| 430 | } |
| 431 | RECURSE(Visit(stmt->body())); |
| 432 | RECURSE(VisitWithExpectation(stmt->cond(), cache_.kAsmSigned, |
| 433 | "do condition expected to be integer")); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | void AsmTyper::VisitWhileStatement(WhileStatement* stmt) { |
| 438 | if (!in_function_) { |
| 439 | FAIL(stmt, "while statement inside module body"); |
| 440 | } |
| 441 | RECURSE(VisitWithExpectation(stmt->cond(), cache_.kAsmSigned, |
| 442 | "while condition expected to be integer")); |
| 443 | RECURSE(Visit(stmt->body())); |
| 444 | } |
| 445 | |
| 446 | |
| 447 | void AsmTyper::VisitForStatement(ForStatement* stmt) { |
| 448 | if (!in_function_) { |
| 449 | FAIL(stmt, "for statement inside module body"); |
| 450 | } |
| 451 | if (stmt->init() != NULL) { |
| 452 | RECURSE(Visit(stmt->init())); |
| 453 | } |
| 454 | if (stmt->cond() != NULL) { |
| 455 | RECURSE(VisitWithExpectation(stmt->cond(), cache_.kAsmSigned, |
| 456 | "for condition expected to be integer")); |
| 457 | } |
| 458 | if (stmt->next() != NULL) { |
| 459 | RECURSE(Visit(stmt->next())); |
| 460 | } |
| 461 | RECURSE(Visit(stmt->body())); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | void AsmTyper::VisitForInStatement(ForInStatement* stmt) { |
| 466 | FAIL(stmt, "for-in statement encountered"); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | void AsmTyper::VisitForOfStatement(ForOfStatement* stmt) { |
| 471 | FAIL(stmt, "for-of statement encountered"); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | void AsmTyper::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 476 | FAIL(stmt, "try statement encountered"); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | void AsmTyper::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 481 | FAIL(stmt, "try statement encountered"); |
| 482 | } |
| 483 | |
| 484 | |
| 485 | void AsmTyper::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 486 | FAIL(stmt, "debugger statement encountered"); |
| 487 | } |
| 488 | |
| 489 | |
| 490 | void AsmTyper::VisitFunctionLiteral(FunctionLiteral* expr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 491 | if (in_function_) { |
| 492 | FAIL(expr, "invalid nested function"); |
| 493 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 494 | Scope* scope = expr->scope(); |
| 495 | DCHECK(scope->is_function_scope()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 496 | |
| 497 | if (!expr->bounds().upper->IsFunction()) { |
| 498 | FAIL(expr, "invalid function literal"); |
| 499 | } |
| 500 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 501 | Type* type = expr->bounds().upper; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 502 | Type* save_return_type = return_type_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 503 | return_type_ = type->AsFunction()->Result(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 504 | in_function_ = true; |
| 505 | local_variable_type_.Clear(); |
| 506 | RECURSE(VisitDeclarations(scope->declarations())); |
| 507 | RECURSE(VisitStatements(expr->body())); |
| 508 | in_function_ = false; |
| 509 | return_type_ = save_return_type; |
| 510 | IntersectResult(expr, type); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | void AsmTyper::VisitNativeFunctionLiteral(NativeFunctionLiteral* expr) { |
| 515 | FAIL(expr, "function info literal encountered"); |
| 516 | } |
| 517 | |
| 518 | |
| 519 | void AsmTyper::VisitDoExpression(DoExpression* expr) { |
| 520 | FAIL(expr, "do-expression encountered"); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | void AsmTyper::VisitConditional(Conditional* expr) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 525 | if (!in_function_) { |
| 526 | FAIL(expr, "ternary operator inside module body"); |
| 527 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 528 | RECURSE(VisitWithExpectation(expr->condition(), Type::Number(), |
| 529 | "condition expected to be integer")); |
| 530 | if (!computed_type_->Is(cache_.kAsmInt)) { |
| 531 | FAIL(expr->condition(), "condition must be of type int"); |
| 532 | } |
| 533 | |
| 534 | RECURSE(VisitWithExpectation( |
| 535 | expr->then_expression(), expected_type_, |
| 536 | "conditional then branch type mismatch with enclosing expression")); |
| 537 | Type* then_type = StorageType(computed_type_); |
| 538 | if (intish_ != 0 || !then_type->Is(cache_.kAsmComparable)) { |
| 539 | FAIL(expr->then_expression(), "invalid type in ? then expression"); |
| 540 | } |
| 541 | |
| 542 | RECURSE(VisitWithExpectation( |
| 543 | expr->else_expression(), expected_type_, |
| 544 | "conditional else branch type mismatch with enclosing expression")); |
| 545 | Type* else_type = StorageType(computed_type_); |
| 546 | if (intish_ != 0 || !else_type->Is(cache_.kAsmComparable)) { |
| 547 | FAIL(expr->else_expression(), "invalid type in ? else expression"); |
| 548 | } |
| 549 | |
| 550 | if (!then_type->Is(else_type) || !else_type->Is(then_type)) { |
| 551 | FAIL(expr, "then and else expressions in ? must have the same type"); |
| 552 | } |
| 553 | |
| 554 | IntersectResult(expr, then_type); |
| 555 | } |
| 556 | |
| 557 | |
| 558 | void AsmTyper::VisitVariableProxy(VariableProxy* expr) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 559 | VisitVariableProxy(expr, false); |
| 560 | } |
| 561 | |
| 562 | void AsmTyper::VisitVariableProxy(VariableProxy* expr, bool assignment) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 563 | Variable* var = expr->var(); |
| 564 | VariableInfo* info = GetVariableInfo(var, false); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 565 | if (!assignment && !in_function_ && !building_function_tables_ && |
| 566 | !visiting_exports_) { |
| 567 | if (var->location() != VariableLocation::PARAMETER || var->index() >= 3) { |
| 568 | FAIL(expr, "illegal variable reference in module body"); |
| 569 | } |
| 570 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 571 | if (info == NULL || info->type == NULL) { |
| 572 | if (var->mode() == TEMPORARY) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 573 | SetType(var, Type::Any()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 574 | info = GetVariableInfo(var, false); |
| 575 | } else { |
| 576 | FAIL(expr, "unbound variable"); |
| 577 | } |
| 578 | } |
| 579 | if (property_info_ != NULL) { |
| 580 | SetVariableInfo(var, property_info_); |
| 581 | property_info_ = NULL; |
| 582 | } |
| 583 | Type* type = Type::Intersect(info->type, expected_type_, zone()); |
| 584 | if (type->Is(cache_.kAsmInt)) { |
| 585 | type = cache_.kAsmInt; |
| 586 | } |
| 587 | info->type = type; |
| 588 | intish_ = 0; |
| 589 | IntersectResult(expr, type); |
| 590 | } |
| 591 | |
| 592 | |
| 593 | void AsmTyper::VisitLiteral(Literal* expr, bool is_return) { |
| 594 | intish_ = 0; |
| 595 | Handle<Object> value = expr->value(); |
| 596 | if (value->IsNumber()) { |
| 597 | int32_t i; |
| 598 | uint32_t u; |
| 599 | if (expr->raw_value()->ContainsDot()) { |
| 600 | IntersectResult(expr, cache_.kAsmDouble); |
| 601 | } else if (!is_return && value->ToUint32(&u)) { |
| 602 | if (u <= 0x7fffffff) { |
| 603 | IntersectResult(expr, cache_.kAsmFixnum); |
| 604 | } else { |
| 605 | IntersectResult(expr, cache_.kAsmUnsigned); |
| 606 | } |
| 607 | } else if (value->ToInt32(&i)) { |
| 608 | IntersectResult(expr, cache_.kAsmSigned); |
| 609 | } else { |
| 610 | FAIL(expr, "illegal number"); |
| 611 | } |
| 612 | } else if (!is_return && value->IsString()) { |
| 613 | IntersectResult(expr, Type::String()); |
| 614 | } else if (value->IsUndefined()) { |
| 615 | IntersectResult(expr, Type::Undefined()); |
| 616 | } else { |
| 617 | FAIL(expr, "illegal literal"); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | |
| 622 | void AsmTyper::VisitLiteral(Literal* expr) { VisitLiteral(expr, false); } |
| 623 | |
| 624 | |
| 625 | void AsmTyper::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 626 | FAIL(expr, "regular expression encountered"); |
| 627 | } |
| 628 | |
| 629 | |
| 630 | void AsmTyper::VisitObjectLiteral(ObjectLiteral* expr) { |
| 631 | if (in_function_) { |
| 632 | FAIL(expr, "object literal in function"); |
| 633 | } |
| 634 | // Allowed for asm module's export declaration. |
| 635 | ZoneList<ObjectLiteralProperty*>* props = expr->properties(); |
| 636 | for (int i = 0; i < props->length(); ++i) { |
| 637 | ObjectLiteralProperty* prop = props->at(i); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 638 | RECURSE(VisitWithExpectation(prop->value(), Type::Any(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 639 | "object property expected to be a function")); |
| 640 | if (!computed_type_->IsFunction()) { |
| 641 | FAIL(prop->value(), "non-function in function table"); |
| 642 | } |
| 643 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 644 | IntersectResult(expr, Type::Object()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | |
| 648 | void AsmTyper::VisitArrayLiteral(ArrayLiteral* expr) { |
| 649 | if (in_function_) { |
| 650 | FAIL(expr, "array literal inside a function"); |
| 651 | } |
| 652 | // Allowed for function tables. |
| 653 | ZoneList<Expression*>* values = expr->values(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 654 | Type* elem_type = Type::None(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 655 | for (int i = 0; i < values->length(); ++i) { |
| 656 | Expression* value = values->at(i); |
| 657 | RECURSE(VisitWithExpectation(value, Type::Any(), "UNREACHABLE")); |
| 658 | if (!computed_type_->IsFunction()) { |
| 659 | FAIL(value, "array component expected to be a function"); |
| 660 | } |
| 661 | elem_type = Type::Union(elem_type, computed_type_, zone()); |
| 662 | } |
| 663 | array_size_ = values->length(); |
| 664 | IntersectResult(expr, Type::Array(elem_type, zone())); |
| 665 | } |
| 666 | |
| 667 | |
| 668 | void AsmTyper::VisitAssignment(Assignment* expr) { |
| 669 | // Handle function tables and everything else in different passes. |
| 670 | if (!in_function_) { |
| 671 | if (expr->value()->IsArrayLiteral()) { |
| 672 | if (!building_function_tables_) { |
| 673 | return; |
| 674 | } |
| 675 | } else { |
| 676 | if (building_function_tables_) { |
| 677 | return; |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | if (expr->is_compound()) FAIL(expr, "compound assignment encountered"); |
| 682 | Type* type = expected_type_; |
| 683 | RECURSE(VisitWithExpectation( |
| 684 | expr->value(), type, "assignment value expected to match surrounding")); |
| 685 | Type* target_type = StorageType(computed_type_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 686 | if (expr->target()->IsVariableProxy()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 687 | if (intish_ != 0) { |
| 688 | FAIL(expr, "intish or floatish assignment"); |
| 689 | } |
| 690 | expected_type_ = target_type; |
| 691 | VisitVariableProxy(expr->target()->AsVariableProxy(), true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 692 | } else if (expr->target()->IsProperty()) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 693 | int32_t value_intish = intish_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 694 | Property* property = expr->target()->AsProperty(); |
| 695 | RECURSE(VisitWithExpectation(property->obj(), Type::Any(), |
| 696 | "bad propety object")); |
| 697 | if (!computed_type_->IsArray()) { |
| 698 | FAIL(property->obj(), "array expected"); |
| 699 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 700 | if (value_intish != 0 && computed_type_->Is(cache_.kFloat64Array)) { |
| 701 | FAIL(expr, "floatish assignment to double array"); |
| 702 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 703 | VisitHeapAccess(property, true, target_type); |
| 704 | } |
| 705 | IntersectResult(expr, target_type); |
| 706 | } |
| 707 | |
| 708 | |
| 709 | void AsmTyper::VisitYield(Yield* expr) { |
| 710 | FAIL(expr, "yield expression encountered"); |
| 711 | } |
| 712 | |
| 713 | |
| 714 | void AsmTyper::VisitThrow(Throw* expr) { |
| 715 | FAIL(expr, "throw statement encountered"); |
| 716 | } |
| 717 | |
| 718 | |
| 719 | int AsmTyper::ElementShiftSize(Type* type) { |
| 720 | if (type->Is(cache_.kAsmSize8)) return 0; |
| 721 | if (type->Is(cache_.kAsmSize16)) return 1; |
| 722 | if (type->Is(cache_.kAsmSize32)) return 2; |
| 723 | if (type->Is(cache_.kAsmSize64)) return 3; |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | |
| 728 | Type* AsmTyper::StorageType(Type* type) { |
| 729 | if (type->Is(cache_.kAsmInt)) { |
| 730 | return cache_.kAsmInt; |
| 731 | } else { |
| 732 | return type; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | |
| 737 | void AsmTyper::VisitHeapAccess(Property* expr, bool assigning, |
| 738 | Type* assignment_type) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 739 | ArrayType* array_type = computed_type_->AsArray(); |
| 740 | // size_t size = array_size_; |
| 741 | Type* type = array_type->Element(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 742 | if (type->IsFunction()) { |
| 743 | if (assigning) { |
| 744 | FAIL(expr, "assigning to function table is illegal"); |
| 745 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 746 | // TODO(bradnelson): Fix the parser and then un-comment this part |
| 747 | // BinaryOperation* bin = expr->key()->AsBinaryOperation(); |
| 748 | // if (bin == NULL || bin->op() != Token::BIT_AND) { |
| 749 | // FAIL(expr->key(), "expected & in call"); |
| 750 | // } |
| 751 | // RECURSE(VisitWithExpectation(bin->left(), cache_.kAsmSigned, |
| 752 | // "array index expected to be integer")); |
| 753 | // Literal* right = bin->right()->AsLiteral(); |
| 754 | // if (right == NULL || right->raw_value()->ContainsDot()) { |
| 755 | // FAIL(right, "call mask must be integer"); |
| 756 | // } |
| 757 | // RECURSE(VisitWithExpectation(bin->right(), cache_.kAsmSigned, |
| 758 | // "call mask expected to be integer")); |
| 759 | // if (static_cast<size_t>(right->raw_value()->AsNumber()) != size - 1) { |
| 760 | // FAIL(right, "call mask must match function table"); |
| 761 | // } |
| 762 | // bin->set_bounds(Bounds(cache_.kAsmSigned)); |
| 763 | RECURSE(VisitWithExpectation(expr->key(), cache_.kAsmSigned, |
| 764 | "must be integer")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 765 | IntersectResult(expr, type); |
| 766 | } else { |
| 767 | Literal* literal = expr->key()->AsLiteral(); |
| 768 | if (literal) { |
| 769 | RECURSE(VisitWithExpectation(literal, cache_.kAsmSigned, |
| 770 | "array index expected to be integer")); |
| 771 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 772 | int expected_shift = ElementShiftSize(type); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 773 | if (expected_shift == 0) { |
| 774 | RECURSE(Visit(expr->key())); |
| 775 | } else { |
| 776 | BinaryOperation* bin = expr->key()->AsBinaryOperation(); |
| 777 | if (bin == NULL || bin->op() != Token::SAR) { |
| 778 | FAIL(expr->key(), "expected >> in heap access"); |
| 779 | } |
| 780 | RECURSE(VisitWithExpectation(bin->left(), cache_.kAsmSigned, |
| 781 | "array index expected to be integer")); |
| 782 | Literal* right = bin->right()->AsLiteral(); |
| 783 | if (right == NULL || right->raw_value()->ContainsDot()) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 784 | FAIL(bin->right(), "heap access shift must be integer"); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 785 | } |
| 786 | RECURSE(VisitWithExpectation(bin->right(), cache_.kAsmSigned, |
| 787 | "array shift expected to be integer")); |
| 788 | int n = static_cast<int>(right->raw_value()->AsNumber()); |
| 789 | if (expected_shift < 0 || n != expected_shift) { |
| 790 | FAIL(right, "heap access shift must match element size"); |
| 791 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 792 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 793 | expr->key()->set_bounds(Bounds(cache_.kAsmSigned)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 794 | } |
| 795 | Type* result_type; |
| 796 | if (type->Is(cache_.kAsmIntArrayElement)) { |
| 797 | result_type = cache_.kAsmIntQ; |
| 798 | intish_ = kMaxUncombinedAdditiveSteps; |
| 799 | } else if (type->Is(cache_.kAsmFloat)) { |
| 800 | if (assigning) { |
| 801 | result_type = cache_.kAsmFloatDoubleQ; |
| 802 | } else { |
| 803 | result_type = cache_.kAsmFloatQ; |
| 804 | } |
| 805 | intish_ = 0; |
| 806 | } else if (type->Is(cache_.kAsmDouble)) { |
| 807 | if (assigning) { |
| 808 | result_type = cache_.kAsmFloatDoubleQ; |
| 809 | if (intish_ != 0) { |
| 810 | FAIL(expr, "Assignment of floatish to Float64Array"); |
| 811 | } |
| 812 | } else { |
| 813 | result_type = cache_.kAsmDoubleQ; |
| 814 | } |
| 815 | intish_ = 0; |
| 816 | } else { |
| 817 | UNREACHABLE(); |
| 818 | } |
| 819 | if (assigning) { |
| 820 | if (!assignment_type->Is(result_type)) { |
| 821 | FAIL(expr, "illegal type in assignment"); |
| 822 | } |
| 823 | } else { |
| 824 | IntersectResult(expr, expected_type_); |
| 825 | IntersectResult(expr, result_type); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | |
| 831 | bool AsmTyper::IsStdlibObject(Expression* expr) { |
| 832 | VariableProxy* proxy = expr->AsVariableProxy(); |
| 833 | if (proxy == NULL) { |
| 834 | return false; |
| 835 | } |
| 836 | Variable* var = proxy->var(); |
| 837 | VariableInfo* info = GetVariableInfo(var, false); |
| 838 | if (info) { |
| 839 | if (info->standard_member == kStdlib) return true; |
| 840 | } |
| 841 | if (var->location() != VariableLocation::PARAMETER || var->index() != 0) { |
| 842 | return false; |
| 843 | } |
| 844 | info = GetVariableInfo(var, true); |
| 845 | info->type = Type::Object(); |
| 846 | info->standard_member = kStdlib; |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | |
| 851 | Expression* AsmTyper::GetReceiverOfPropertyAccess(Expression* expr, |
| 852 | const char* name) { |
| 853 | Property* property = expr->AsProperty(); |
| 854 | if (property == NULL) { |
| 855 | return NULL; |
| 856 | } |
| 857 | Literal* key = property->key()->AsLiteral(); |
| 858 | if (key == NULL || !key->IsPropertyName() || |
| 859 | !key->AsPropertyName()->IsUtf8EqualTo(CStrVector(name))) { |
| 860 | return NULL; |
| 861 | } |
| 862 | return property->obj(); |
| 863 | } |
| 864 | |
| 865 | |
| 866 | bool AsmTyper::IsMathObject(Expression* expr) { |
| 867 | Expression* obj = GetReceiverOfPropertyAccess(expr, "Math"); |
| 868 | return obj && IsStdlibObject(obj); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | bool AsmTyper::IsSIMDObject(Expression* expr) { |
| 873 | Expression* obj = GetReceiverOfPropertyAccess(expr, "SIMD"); |
| 874 | return obj && IsStdlibObject(obj); |
| 875 | } |
| 876 | |
| 877 | |
| 878 | bool AsmTyper::IsSIMDTypeObject(Expression* expr, const char* name) { |
| 879 | Expression* obj = GetReceiverOfPropertyAccess(expr, name); |
| 880 | return obj && IsSIMDObject(obj); |
| 881 | } |
| 882 | |
| 883 | |
| 884 | void AsmTyper::VisitProperty(Property* expr) { |
| 885 | if (IsMathObject(expr->obj())) { |
| 886 | VisitLibraryAccess(&stdlib_math_types_, expr); |
| 887 | return; |
| 888 | } |
| 889 | #define V(NAME, Name, name, lane_count, lane_type) \ |
| 890 | if (IsSIMDTypeObject(expr->obj(), #Name)) { \ |
| 891 | VisitLibraryAccess(&stdlib_simd_##name##_types_, expr); \ |
| 892 | return; \ |
| 893 | } \ |
| 894 | if (IsSIMDTypeObject(expr, #Name)) { \ |
| 895 | VariableInfo* info = stdlib_simd_##name##_constructor_type_; \ |
| 896 | SetResult(expr, info->type); \ |
| 897 | property_info_ = info; \ |
| 898 | return; \ |
| 899 | } |
| 900 | SIMD128_TYPES(V) |
| 901 | #undef V |
| 902 | if (IsStdlibObject(expr->obj())) { |
| 903 | VisitLibraryAccess(&stdlib_types_, expr); |
| 904 | return; |
| 905 | } |
| 906 | |
| 907 | property_info_ = NULL; |
| 908 | |
| 909 | // Only recurse at this point so that we avoid needing |
| 910 | // stdlib.Math to have a real type. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 911 | RECURSE( |
| 912 | VisitWithExpectation(expr->obj(), Type::Any(), "bad property object")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 913 | |
| 914 | // For heap view or function table access. |
| 915 | if (computed_type_->IsArray()) { |
| 916 | VisitHeapAccess(expr, false, NULL); |
| 917 | return; |
| 918 | } |
| 919 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 920 | VariableProxy* proxy = expr->obj()->AsVariableProxy(); |
| 921 | if (proxy != NULL) { |
| 922 | Variable* var = proxy->var(); |
| 923 | if (var->location() == VariableLocation::PARAMETER && var->index() == 1) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 924 | // foreign.x - Function represent as () -> Any |
| 925 | if (Type::Any()->Is(expected_type_)) { |
| 926 | SetResult(expr, Type::Function(Type::Any(), zone())); |
| 927 | } else { |
| 928 | SetResult(expr, expected_type_); |
| 929 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 930 | return; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | FAIL(expr, "invalid property access"); |
| 935 | } |
| 936 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 937 | void AsmTyper::CheckPolymorphicStdlibArguments( |
| 938 | enum StandardMember standard_member, ZoneList<Expression*>* args) { |
| 939 | if (args->length() == 0) { |
| 940 | return; |
| 941 | } |
| 942 | // Handle polymorphic stdlib functions specially. |
| 943 | Expression* arg0 = args->at(0); |
| 944 | Type* arg0_type = arg0->bounds().upper; |
| 945 | switch (standard_member) { |
| 946 | case kMathFround: { |
| 947 | if (!arg0_type->Is(cache_.kAsmFloat) && |
| 948 | !arg0_type->Is(cache_.kAsmDouble) && |
| 949 | !arg0_type->Is(cache_.kAsmSigned) && |
| 950 | !arg0_type->Is(cache_.kAsmUnsigned)) { |
| 951 | FAIL(arg0, "illegal function argument type"); |
| 952 | } |
| 953 | break; |
| 954 | } |
| 955 | case kMathCeil: |
| 956 | case kMathFloor: |
| 957 | case kMathSqrt: { |
| 958 | if (!arg0_type->Is(cache_.kAsmFloat) && |
| 959 | !arg0_type->Is(cache_.kAsmDouble)) { |
| 960 | FAIL(arg0, "illegal function argument type"); |
| 961 | } |
| 962 | break; |
| 963 | } |
| 964 | case kMathAbs: |
| 965 | case kMathMin: |
| 966 | case kMathMax: { |
| 967 | if (!arg0_type->Is(cache_.kAsmFloat) && |
| 968 | !arg0_type->Is(cache_.kAsmDouble) && |
| 969 | !arg0_type->Is(cache_.kAsmSigned)) { |
| 970 | FAIL(arg0, "illegal function argument type"); |
| 971 | } |
| 972 | if (args->length() > 1) { |
| 973 | Type* other = Type::Intersect(args->at(0)->bounds().upper, |
| 974 | args->at(1)->bounds().upper, zone()); |
| 975 | if (!other->Is(cache_.kAsmFloat) && !other->Is(cache_.kAsmDouble) && |
| 976 | !other->Is(cache_.kAsmSigned)) { |
| 977 | FAIL(arg0, "function arguments types don't match"); |
| 978 | } |
| 979 | } |
| 980 | break; |
| 981 | } |
| 982 | default: { break; } |
| 983 | } |
| 984 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 985 | |
| 986 | void AsmTyper::VisitCall(Call* expr) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 987 | Type* expected_type = expected_type_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 988 | RECURSE(VisitWithExpectation(expr->expression(), Type::Any(), |
| 989 | "callee expected to be any")); |
| 990 | StandardMember standard_member = kNone; |
| 991 | VariableProxy* proxy = expr->expression()->AsVariableProxy(); |
| 992 | if (proxy) { |
| 993 | standard_member = VariableAsStandardMember(proxy->var()); |
| 994 | } |
| 995 | if (!in_function_ && (proxy == NULL || standard_member != kMathFround)) { |
| 996 | FAIL(expr, "calls forbidden outside function bodies"); |
| 997 | } |
| 998 | if (proxy == NULL && !expr->expression()->IsProperty()) { |
| 999 | FAIL(expr, "calls must be to bound variables or function tables"); |
| 1000 | } |
| 1001 | if (computed_type_->IsFunction()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1002 | FunctionType* fun_type = computed_type_->AsFunction(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1003 | Type* result_type = fun_type->Result(); |
| 1004 | ZoneList<Expression*>* args = expr->arguments(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1005 | if (Type::Any()->Is(result_type)) { |
| 1006 | // For foreign calls. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1007 | for (int i = 0; i < args->length(); ++i) { |
| 1008 | Expression* arg = args->at(i); |
| 1009 | RECURSE(VisitWithExpectation( |
| 1010 | arg, Type::Any(), "foreign call argument expected to be any")); |
| 1011 | // Checking for asm extern types explicitly, as the type system |
| 1012 | // doesn't correctly check their inheritance relationship. |
| 1013 | if (!computed_type_->Is(cache_.kAsmSigned) && |
| 1014 | !computed_type_->Is(cache_.kAsmFixnum) && |
| 1015 | !computed_type_->Is(cache_.kAsmDouble)) { |
| 1016 | FAIL(arg, |
| 1017 | "foreign call argument expected to be int, double, or fixnum"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1020 | intish_ = 0; |
| 1021 | expr->expression()->set_bounds( |
| 1022 | Bounds(Type::Function(Type::Any(), zone()))); |
| 1023 | IntersectResult(expr, expected_type); |
| 1024 | } else { |
| 1025 | if (fun_type->Arity() != args->length()) { |
| 1026 | FAIL(expr, "call with wrong arity"); |
| 1027 | } |
| 1028 | for (int i = 0; i < args->length(); ++i) { |
| 1029 | Expression* arg = args->at(i); |
| 1030 | RECURSE(VisitWithExpectation( |
| 1031 | arg, fun_type->Parameter(i), |
| 1032 | "call argument expected to match callee parameter")); |
| 1033 | if (standard_member != kNone && standard_member != kMathFround && |
| 1034 | i == 0) { |
| 1035 | result_type = computed_type_; |
| 1036 | } |
| 1037 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1038 | RECURSE(CheckPolymorphicStdlibArguments(standard_member, args)); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1039 | intish_ = 0; |
| 1040 | IntersectResult(expr, result_type); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1041 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1042 | } else { |
| 1043 | FAIL(expr, "invalid callee"); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | void AsmTyper::VisitCallNew(CallNew* expr) { |
| 1049 | if (in_function_) { |
| 1050 | FAIL(expr, "new not allowed in module function"); |
| 1051 | } |
| 1052 | RECURSE(VisitWithExpectation(expr->expression(), Type::Any(), |
| 1053 | "expected stdlib function")); |
| 1054 | if (computed_type_->IsFunction()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1055 | FunctionType* fun_type = computed_type_->AsFunction(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1056 | ZoneList<Expression*>* args = expr->arguments(); |
| 1057 | if (fun_type->Arity() != args->length()) |
| 1058 | FAIL(expr, "call with wrong arity"); |
| 1059 | for (int i = 0; i < args->length(); ++i) { |
| 1060 | Expression* arg = args->at(i); |
| 1061 | RECURSE(VisitWithExpectation( |
| 1062 | arg, fun_type->Parameter(i), |
| 1063 | "constructor argument expected to match callee parameter")); |
| 1064 | } |
| 1065 | IntersectResult(expr, fun_type->Result()); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | FAIL(expr, "ill-typed new operator"); |
| 1070 | } |
| 1071 | |
| 1072 | |
| 1073 | void AsmTyper::VisitCallRuntime(CallRuntime* expr) { |
| 1074 | // Allow runtime calls for now. |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | void AsmTyper::VisitUnaryOperation(UnaryOperation* expr) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1079 | if (!in_function_) { |
| 1080 | FAIL(expr, "unary operator inside module body"); |
| 1081 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1082 | switch (expr->op()) { |
| 1083 | case Token::NOT: // Used to encode != and !== |
| 1084 | RECURSE(VisitWithExpectation(expr->expression(), cache_.kAsmInt, |
| 1085 | "operand expected to be integer")); |
| 1086 | IntersectResult(expr, cache_.kAsmSigned); |
| 1087 | return; |
| 1088 | case Token::DELETE: |
| 1089 | FAIL(expr, "delete operator encountered"); |
| 1090 | case Token::VOID: |
| 1091 | FAIL(expr, "void operator encountered"); |
| 1092 | case Token::TYPEOF: |
| 1093 | FAIL(expr, "typeof operator encountered"); |
| 1094 | default: |
| 1095 | UNREACHABLE(); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | void AsmTyper::VisitCountOperation(CountOperation* expr) { |
| 1101 | FAIL(expr, "increment or decrement operator encountered"); |
| 1102 | } |
| 1103 | |
| 1104 | |
| 1105 | void AsmTyper::VisitIntegerBitwiseOperator(BinaryOperation* expr, |
| 1106 | Type* left_expected, |
| 1107 | Type* right_expected, |
| 1108 | Type* result_type, bool conversion) { |
| 1109 | RECURSE(VisitWithExpectation(expr->left(), Type::Number(), |
| 1110 | "left bitwise operand expected to be a number")); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1111 | int32_t left_intish = intish_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1112 | Type* left_type = computed_type_; |
| 1113 | if (!left_type->Is(left_expected)) { |
| 1114 | FAIL(expr->left(), "left bitwise operand expected to be an integer"); |
| 1115 | } |
| 1116 | if (left_intish > kMaxUncombinedAdditiveSteps) { |
| 1117 | FAIL(expr->left(), "too many consecutive additive ops"); |
| 1118 | } |
| 1119 | |
| 1120 | RECURSE( |
| 1121 | VisitWithExpectation(expr->right(), Type::Number(), |
| 1122 | "right bitwise operand expected to be a number")); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1123 | int32_t right_intish = intish_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1124 | Type* right_type = computed_type_; |
| 1125 | if (!right_type->Is(right_expected)) { |
| 1126 | FAIL(expr->right(), "right bitwise operand expected to be an integer"); |
| 1127 | } |
| 1128 | if (right_intish > kMaxUncombinedAdditiveSteps) { |
| 1129 | FAIL(expr->right(), "too many consecutive additive ops"); |
| 1130 | } |
| 1131 | |
| 1132 | intish_ = 0; |
| 1133 | |
| 1134 | if (left_type->Is(cache_.kAsmFixnum) && right_type->Is(cache_.kAsmInt)) { |
| 1135 | left_type = right_type; |
| 1136 | } |
| 1137 | if (right_type->Is(cache_.kAsmFixnum) && left_type->Is(cache_.kAsmInt)) { |
| 1138 | right_type = left_type; |
| 1139 | } |
| 1140 | if (!conversion) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1141 | if (!left_type->Is(cache_.kAsmIntQ) || !right_type->Is(cache_.kAsmIntQ)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1142 | FAIL(expr, "ill-typed bitwise operation"); |
| 1143 | } |
| 1144 | } |
| 1145 | IntersectResult(expr, result_type); |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | void AsmTyper::VisitBinaryOperation(BinaryOperation* expr) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1150 | if (!in_function_) { |
| 1151 | if (expr->op() != Token::BIT_OR && expr->op() != Token::MUL) { |
| 1152 | FAIL(expr, "illegal binary operator inside module body"); |
| 1153 | } |
| 1154 | if (!(expr->left()->IsProperty() || expr->left()->IsVariableProxy()) || |
| 1155 | !expr->right()->IsLiteral()) { |
| 1156 | FAIL(expr, "illegal computation inside module body"); |
| 1157 | } |
| 1158 | DCHECK(expr->right()->AsLiteral() != nullptr); |
| 1159 | const AstValue* right_value = expr->right()->AsLiteral()->raw_value(); |
| 1160 | if (expr->op() == Token::BIT_OR) { |
| 1161 | if (right_value->AsNumber() != 0.0 || right_value->ContainsDot()) { |
| 1162 | FAIL(expr, "illegal integer annotation value"); |
| 1163 | } |
| 1164 | } |
| 1165 | if (expr->op() == Token::MUL) { |
| 1166 | if (right_value->AsNumber() != 1.0 && right_value->ContainsDot()) { |
| 1167 | FAIL(expr, "illegal double annotation value"); |
| 1168 | } |
| 1169 | } |
| 1170 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1171 | switch (expr->op()) { |
| 1172 | case Token::COMMA: { |
| 1173 | RECURSE(VisitWithExpectation(expr->left(), Type::Any(), |
| 1174 | "left comma operand expected to be any")); |
| 1175 | RECURSE(VisitWithExpectation(expr->right(), Type::Any(), |
| 1176 | "right comma operand expected to be any")); |
| 1177 | IntersectResult(expr, computed_type_); |
| 1178 | return; |
| 1179 | } |
| 1180 | case Token::OR: |
| 1181 | case Token::AND: |
| 1182 | FAIL(expr, "illegal logical operator"); |
| 1183 | case Token::BIT_OR: { |
| 1184 | // BIT_OR allows Any since it is used as a type coercion. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1185 | RECURSE(VisitIntegerBitwiseOperator(expr, Type::Any(), cache_.kAsmIntQ, |
| 1186 | cache_.kAsmSigned, true)); |
| 1187 | if (expr->left()->IsCall() && expr->op() == Token::BIT_OR && |
| 1188 | Type::Number()->Is(expr->left()->bounds().upper)) { |
| 1189 | // Force the return types of foreign functions. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1190 | expr->left()->set_bounds(Bounds(cache_.kAsmSigned)); |
| 1191 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1192 | if (in_function_ && !expr->left()->bounds().upper->Is(cache_.kAsmIntQ)) { |
| 1193 | FAIL(expr->left(), "intish required"); |
| 1194 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1195 | return; |
| 1196 | } |
| 1197 | case Token::BIT_XOR: { |
| 1198 | // Handle booleans specially to handle de-sugared ! |
| 1199 | Literal* left = expr->left()->AsLiteral(); |
| 1200 | if (left && left->value()->IsBoolean()) { |
| 1201 | if (left->ToBooleanIsTrue()) { |
| 1202 | left->set_bounds(Bounds(cache_.kSingletonOne)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1203 | RECURSE(VisitWithExpectation(expr->right(), cache_.kAsmIntQ, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1204 | "not operator expects an integer")); |
| 1205 | IntersectResult(expr, cache_.kAsmSigned); |
| 1206 | return; |
| 1207 | } else { |
| 1208 | FAIL(left, "unexpected false"); |
| 1209 | } |
| 1210 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1211 | // BIT_XOR allows Any since it is used as a type coercion (via ~~). |
| 1212 | RECURSE(VisitIntegerBitwiseOperator(expr, Type::Any(), cache_.kAsmIntQ, |
| 1213 | cache_.kAsmSigned, true)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1214 | return; |
| 1215 | } |
| 1216 | case Token::SHR: { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1217 | RECURSE(VisitIntegerBitwiseOperator( |
| 1218 | expr, cache_.kAsmIntQ, cache_.kAsmIntQ, cache_.kAsmUnsigned, false)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1219 | return; |
| 1220 | } |
| 1221 | case Token::SHL: |
| 1222 | case Token::SAR: |
| 1223 | case Token::BIT_AND: { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1224 | RECURSE(VisitIntegerBitwiseOperator( |
| 1225 | expr, cache_.kAsmIntQ, cache_.kAsmIntQ, cache_.kAsmSigned, false)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1226 | return; |
| 1227 | } |
| 1228 | case Token::ADD: |
| 1229 | case Token::SUB: |
| 1230 | case Token::MUL: |
| 1231 | case Token::DIV: |
| 1232 | case Token::MOD: { |
| 1233 | RECURSE(VisitWithExpectation( |
| 1234 | expr->left(), Type::Number(), |
| 1235 | "left arithmetic operand expected to be number")); |
| 1236 | Type* left_type = computed_type_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1237 | int32_t left_intish = intish_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1238 | RECURSE(VisitWithExpectation( |
| 1239 | expr->right(), Type::Number(), |
| 1240 | "right arithmetic operand expected to be number")); |
| 1241 | Type* right_type = computed_type_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1242 | int32_t right_intish = intish_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1243 | Type* type = Type::Union(left_type, right_type, zone()); |
| 1244 | if (type->Is(cache_.kAsmInt)) { |
| 1245 | if (expr->op() == Token::MUL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1246 | int32_t i; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1247 | Literal* left = expr->left()->AsLiteral(); |
| 1248 | Literal* right = expr->right()->AsLiteral(); |
| 1249 | if (left != nullptr && left->value()->IsNumber() && |
| 1250 | left->value()->ToInt32(&i)) { |
| 1251 | if (right_intish != 0) { |
| 1252 | FAIL(expr, "intish not allowed in multiply"); |
| 1253 | } |
| 1254 | } else if (right != nullptr && right->value()->IsNumber() && |
| 1255 | right->value()->ToInt32(&i)) { |
| 1256 | if (left_intish != 0) { |
| 1257 | FAIL(expr, "intish not allowed in multiply"); |
| 1258 | } |
| 1259 | } else { |
| 1260 | FAIL(expr, "multiply must be by an integer literal"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1261 | } |
| 1262 | i = abs(i); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1263 | if (i >= (1 << 20)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1264 | FAIL(expr, "multiply must be by value in -2^20 < n < 2^20"); |
| 1265 | } |
| 1266 | intish_ = i; |
| 1267 | IntersectResult(expr, cache_.kAsmInt); |
| 1268 | return; |
| 1269 | } else { |
| 1270 | intish_ = left_intish + right_intish + 1; |
| 1271 | if (expr->op() == Token::ADD || expr->op() == Token::SUB) { |
| 1272 | if (intish_ > kMaxUncombinedAdditiveSteps) { |
| 1273 | FAIL(expr, "too many consecutive additive ops"); |
| 1274 | } |
| 1275 | } else { |
| 1276 | if (intish_ > kMaxUncombinedMultiplicativeSteps) { |
| 1277 | FAIL(expr, "too many consecutive multiplicative ops"); |
| 1278 | } |
| 1279 | } |
| 1280 | IntersectResult(expr, cache_.kAsmInt); |
| 1281 | return; |
| 1282 | } |
| 1283 | } else if (expr->op() == Token::MUL && expr->right()->IsLiteral() && |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1284 | right_type->Is(cache_.kAsmDouble) && |
| 1285 | expr->right()->AsLiteral()->raw_value()->ContainsDot() && |
| 1286 | expr->right()->AsLiteral()->raw_value()->AsNumber() == 1.0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1287 | // For unary +, expressed as x * 1.0 |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1288 | if (expr->left()->IsCall() && |
| 1289 | Type::Number()->Is(expr->left()->bounds().upper)) { |
| 1290 | // Force the return types of foreign functions. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1291 | expr->left()->set_bounds(Bounds(cache_.kAsmDouble)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1292 | left_type = expr->left()->bounds().upper; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1293 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1294 | if (!(expr->left()->IsProperty() && |
| 1295 | Type::Number()->Is(expr->left()->bounds().upper))) { |
| 1296 | if (!left_type->Is(cache_.kAsmSigned) && |
| 1297 | !left_type->Is(cache_.kAsmUnsigned) && |
| 1298 | !left_type->Is(cache_.kAsmFixnum) && |
| 1299 | !left_type->Is(cache_.kAsmFloatQ) && |
| 1300 | !left_type->Is(cache_.kAsmDoubleQ)) { |
| 1301 | FAIL( |
| 1302 | expr->left(), |
| 1303 | "unary + only allowed on signed, unsigned, float?, or double?"); |
| 1304 | } |
| 1305 | } |
| 1306 | IntersectResult(expr, cache_.kAsmDouble); |
| 1307 | return; |
| 1308 | } else if (expr->op() == Token::MUL && left_type->Is(cache_.kAsmDouble) && |
| 1309 | expr->right()->IsLiteral() && |
| 1310 | !expr->right()->AsLiteral()->raw_value()->ContainsDot() && |
| 1311 | expr->right()->AsLiteral()->raw_value()->AsNumber() == -1.0) { |
| 1312 | // For unary -, expressed as x * -1 |
| 1313 | expr->right()->set_bounds(Bounds(cache_.kAsmDouble)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1314 | IntersectResult(expr, cache_.kAsmDouble); |
| 1315 | return; |
| 1316 | } else if (type->Is(cache_.kAsmFloat) && expr->op() != Token::MOD) { |
| 1317 | if (left_intish != 0 || right_intish != 0) { |
| 1318 | FAIL(expr, "float operation before required fround"); |
| 1319 | } |
| 1320 | IntersectResult(expr, cache_.kAsmFloat); |
| 1321 | intish_ = 1; |
| 1322 | return; |
| 1323 | } else if (type->Is(cache_.kAsmDouble)) { |
| 1324 | IntersectResult(expr, cache_.kAsmDouble); |
| 1325 | return; |
| 1326 | } else { |
| 1327 | FAIL(expr, "ill-typed arithmetic operation"); |
| 1328 | } |
| 1329 | } |
| 1330 | default: |
| 1331 | UNREACHABLE(); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | |
| 1336 | void AsmTyper::VisitCompareOperation(CompareOperation* expr) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1337 | if (!in_function_) { |
| 1338 | FAIL(expr, "comparison inside module body"); |
| 1339 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1340 | Token::Value op = expr->op(); |
| 1341 | if (op != Token::EQ && op != Token::NE && op != Token::LT && |
| 1342 | op != Token::LTE && op != Token::GT && op != Token::GTE) { |
| 1343 | FAIL(expr, "illegal comparison operator"); |
| 1344 | } |
| 1345 | |
| 1346 | RECURSE( |
| 1347 | VisitWithExpectation(expr->left(), Type::Number(), |
| 1348 | "left comparison operand expected to be number")); |
| 1349 | Type* left_type = computed_type_; |
| 1350 | if (!left_type->Is(cache_.kAsmComparable)) { |
| 1351 | FAIL(expr->left(), "bad type on left side of comparison"); |
| 1352 | } |
| 1353 | |
| 1354 | RECURSE( |
| 1355 | VisitWithExpectation(expr->right(), Type::Number(), |
| 1356 | "right comparison operand expected to be number")); |
| 1357 | Type* right_type = computed_type_; |
| 1358 | if (!right_type->Is(cache_.kAsmComparable)) { |
| 1359 | FAIL(expr->right(), "bad type on right side of comparison"); |
| 1360 | } |
| 1361 | |
| 1362 | if (!left_type->Is(right_type) && !right_type->Is(left_type)) { |
| 1363 | FAIL(expr, "left and right side of comparison must match"); |
| 1364 | } |
| 1365 | |
| 1366 | IntersectResult(expr, cache_.kAsmSigned); |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | void AsmTyper::VisitThisFunction(ThisFunction* expr) { |
| 1371 | FAIL(expr, "this function not allowed"); |
| 1372 | } |
| 1373 | |
| 1374 | |
| 1375 | void AsmTyper::VisitDeclarations(ZoneList<Declaration*>* decls) { |
| 1376 | for (int i = 0; i < decls->length(); ++i) { |
| 1377 | Declaration* decl = decls->at(i); |
| 1378 | RECURSE(Visit(decl)); |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | |
| 1383 | void AsmTyper::VisitImportDeclaration(ImportDeclaration* decl) { |
| 1384 | FAIL(decl, "import declaration encountered"); |
| 1385 | } |
| 1386 | |
| 1387 | |
| 1388 | void AsmTyper::VisitExportDeclaration(ExportDeclaration* decl) { |
| 1389 | FAIL(decl, "export declaration encountered"); |
| 1390 | } |
| 1391 | |
| 1392 | |
| 1393 | void AsmTyper::VisitClassLiteral(ClassLiteral* expr) { |
| 1394 | FAIL(expr, "class literal not allowed"); |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | void AsmTyper::VisitSpread(Spread* expr) { FAIL(expr, "spread not allowed"); } |
| 1399 | |
| 1400 | |
| 1401 | void AsmTyper::VisitSuperPropertyReference(SuperPropertyReference* expr) { |
| 1402 | FAIL(expr, "super property reference not allowed"); |
| 1403 | } |
| 1404 | |
| 1405 | |
| 1406 | void AsmTyper::VisitSuperCallReference(SuperCallReference* expr) { |
| 1407 | FAIL(expr, "call reference not allowed"); |
| 1408 | } |
| 1409 | |
| 1410 | |
| 1411 | void AsmTyper::InitializeStdlibSIMD() { |
| 1412 | #define V(NAME, Name, name, lane_count, lane_type) \ |
| 1413 | { \ |
| 1414 | Type* type = Type::Function(Type::Name(isolate_, zone()), Type::Any(), \ |
| 1415 | lane_count, zone()); \ |
| 1416 | for (int i = 0; i < lane_count; ++i) { \ |
| 1417 | type->AsFunction()->InitParameter(i, Type::Number()); \ |
| 1418 | } \ |
| 1419 | stdlib_simd_##name##_constructor_type_ = new (zone()) VariableInfo(type); \ |
| 1420 | stdlib_simd_##name##_constructor_type_->is_constructor_function = true; \ |
| 1421 | } |
| 1422 | SIMD128_TYPES(V) |
| 1423 | #undef V |
| 1424 | } |
| 1425 | |
| 1426 | |
| 1427 | void AsmTyper::InitializeStdlib() { |
| 1428 | if (allow_simd_) { |
| 1429 | InitializeStdlibSIMD(); |
| 1430 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1431 | Type* number_type = Type::Number(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1432 | Type* double_type = cache_.kAsmDouble; |
| 1433 | Type* double_fn1_type = Type::Function(double_type, double_type, zone()); |
| 1434 | Type* double_fn2_type = |
| 1435 | Type::Function(double_type, double_type, double_type, zone()); |
| 1436 | |
| 1437 | Type* fround_type = Type::Function(cache_.kAsmFloat, number_type, zone()); |
| 1438 | Type* imul_type = |
| 1439 | Type::Function(cache_.kAsmSigned, cache_.kAsmInt, cache_.kAsmInt, zone()); |
| 1440 | // TODO(bradnelson): currently only approximating the proper intersection type |
| 1441 | // (which we cannot currently represent). |
| 1442 | Type* number_fn1_type = Type::Function(number_type, number_type, zone()); |
| 1443 | Type* number_fn2_type = |
| 1444 | Type::Function(number_type, number_type, number_type, zone()); |
| 1445 | |
| 1446 | struct Assignment { |
| 1447 | const char* name; |
| 1448 | StandardMember standard_member; |
| 1449 | Type* type; |
| 1450 | }; |
| 1451 | |
| 1452 | const Assignment math[] = {{"PI", kMathPI, double_type}, |
| 1453 | {"E", kMathE, double_type}, |
| 1454 | {"LN2", kMathLN2, double_type}, |
| 1455 | {"LN10", kMathLN10, double_type}, |
| 1456 | {"LOG2E", kMathLOG2E, double_type}, |
| 1457 | {"LOG10E", kMathLOG10E, double_type}, |
| 1458 | {"SQRT2", kMathSQRT2, double_type}, |
| 1459 | {"SQRT1_2", kMathSQRT1_2, double_type}, |
| 1460 | {"imul", kMathImul, imul_type}, |
| 1461 | {"abs", kMathAbs, number_fn1_type}, |
| 1462 | {"ceil", kMathCeil, number_fn1_type}, |
| 1463 | {"floor", kMathFloor, number_fn1_type}, |
| 1464 | {"fround", kMathFround, fround_type}, |
| 1465 | {"pow", kMathPow, double_fn2_type}, |
| 1466 | {"exp", kMathExp, double_fn1_type}, |
| 1467 | {"log", kMathLog, double_fn1_type}, |
| 1468 | {"min", kMathMin, number_fn2_type}, |
| 1469 | {"max", kMathMax, number_fn2_type}, |
| 1470 | {"sqrt", kMathSqrt, number_fn1_type}, |
| 1471 | {"cos", kMathCos, double_fn1_type}, |
| 1472 | {"sin", kMathSin, double_fn1_type}, |
| 1473 | {"tan", kMathTan, double_fn1_type}, |
| 1474 | {"acos", kMathAcos, double_fn1_type}, |
| 1475 | {"asin", kMathAsin, double_fn1_type}, |
| 1476 | {"atan", kMathAtan, double_fn1_type}, |
| 1477 | {"atan2", kMathAtan2, double_fn2_type}}; |
| 1478 | for (unsigned i = 0; i < arraysize(math); ++i) { |
| 1479 | stdlib_math_types_[math[i].name] = new (zone()) VariableInfo(math[i].type); |
| 1480 | stdlib_math_types_[math[i].name]->standard_member = math[i].standard_member; |
| 1481 | } |
| 1482 | stdlib_math_types_["fround"]->is_check_function = true; |
| 1483 | |
| 1484 | stdlib_types_["Infinity"] = new (zone()) VariableInfo(double_type); |
| 1485 | stdlib_types_["Infinity"]->standard_member = kInfinity; |
| 1486 | stdlib_types_["NaN"] = new (zone()) VariableInfo(double_type); |
| 1487 | stdlib_types_["NaN"]->standard_member = kNaN; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1488 | Type* buffer_type = Type::Any(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1489 | #define TYPED_ARRAY(TypeName, type_name, TYPE_NAME, ctype, size) \ |
| 1490 | stdlib_types_[#TypeName "Array"] = new (zone()) VariableInfo( \ |
| 1491 | Type::Function(cache_.k##TypeName##Array, buffer_type, zone())); |
| 1492 | TYPED_ARRAYS(TYPED_ARRAY) |
| 1493 | #undef TYPED_ARRAY |
| 1494 | |
| 1495 | #define TYPED_ARRAY(TypeName, type_name, TYPE_NAME, ctype, size) \ |
| 1496 | stdlib_heap_types_[#TypeName "Array"] = new (zone()) VariableInfo( \ |
| 1497 | Type::Function(cache_.k##TypeName##Array, buffer_type, zone())); |
| 1498 | TYPED_ARRAYS(TYPED_ARRAY) |
| 1499 | #undef TYPED_ARRAY |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | void AsmTyper::VisitLibraryAccess(ObjectTypeMap* map, Property* expr) { |
| 1504 | Literal* key = expr->key()->AsLiteral(); |
| 1505 | if (key == NULL || !key->IsPropertyName()) |
| 1506 | FAIL(expr, "invalid key used on stdlib member"); |
| 1507 | Handle<String> name = key->AsPropertyName(); |
| 1508 | VariableInfo* info = LibType(map, name); |
| 1509 | if (info == NULL || info->type == NULL) FAIL(expr, "unknown stdlib function"); |
| 1510 | SetResult(expr, info->type); |
| 1511 | property_info_ = info; |
| 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | AsmTyper::VariableInfo* AsmTyper::LibType(ObjectTypeMap* map, |
| 1516 | Handle<String> name) { |
| 1517 | base::SmartArrayPointer<char> aname = name->ToCString(); |
| 1518 | ObjectTypeMap::iterator i = map->find(std::string(aname.get())); |
| 1519 | if (i == map->end()) { |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | return i->second; |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | void AsmTyper::SetType(Variable* variable, Type* type) { |
| 1527 | VariableInfo* info = GetVariableInfo(variable, true); |
| 1528 | info->type = type; |
| 1529 | } |
| 1530 | |
| 1531 | |
| 1532 | Type* AsmTyper::GetType(Variable* variable) { |
| 1533 | VariableInfo* info = GetVariableInfo(variable, false); |
| 1534 | if (!info) return NULL; |
| 1535 | return info->type; |
| 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | AsmTyper::VariableInfo* AsmTyper::GetVariableInfo(Variable* variable, |
| 1540 | bool setting) { |
| 1541 | ZoneHashMap::Entry* entry; |
| 1542 | ZoneHashMap* map; |
| 1543 | if (in_function_) { |
| 1544 | map = &local_variable_type_; |
| 1545 | } else { |
| 1546 | map = &global_variable_type_; |
| 1547 | } |
| 1548 | if (setting) { |
| 1549 | entry = map->LookupOrInsert(variable, ComputePointerHash(variable), |
| 1550 | ZoneAllocationPolicy(zone())); |
| 1551 | } else { |
| 1552 | entry = map->Lookup(variable, ComputePointerHash(variable)); |
| 1553 | if (!entry && in_function_) { |
| 1554 | entry = |
| 1555 | global_variable_type_.Lookup(variable, ComputePointerHash(variable)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | if (!entry) return NULL; |
| 1559 | if (!entry->value) { |
| 1560 | if (!setting) return NULL; |
| 1561 | entry->value = new (zone()) VariableInfo; |
| 1562 | } |
| 1563 | return reinterpret_cast<VariableInfo*>(entry->value); |
| 1564 | } |
| 1565 | |
| 1566 | |
| 1567 | void AsmTyper::SetVariableInfo(Variable* variable, const VariableInfo* info) { |
| 1568 | VariableInfo* dest = GetVariableInfo(variable, true); |
| 1569 | dest->type = info->type; |
| 1570 | dest->is_check_function = info->is_check_function; |
| 1571 | dest->is_constructor_function = info->is_constructor_function; |
| 1572 | dest->standard_member = info->standard_member; |
| 1573 | } |
| 1574 | |
| 1575 | |
| 1576 | AsmTyper::StandardMember AsmTyper::VariableAsStandardMember( |
| 1577 | Variable* variable) { |
| 1578 | VariableInfo* info = GetVariableInfo(variable, false); |
| 1579 | if (!info) return kNone; |
| 1580 | return info->standard_member; |
| 1581 | } |
| 1582 | |
| 1583 | |
| 1584 | void AsmTyper::SetResult(Expression* expr, Type* type) { |
| 1585 | computed_type_ = type; |
| 1586 | expr->set_bounds(Bounds(computed_type_)); |
| 1587 | } |
| 1588 | |
| 1589 | |
| 1590 | void AsmTyper::IntersectResult(Expression* expr, Type* type) { |
| 1591 | computed_type_ = type; |
| 1592 | Type* bounded_type = Type::Intersect(computed_type_, expected_type_, zone()); |
| 1593 | expr->set_bounds(Bounds(bounded_type)); |
| 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | void AsmTyper::VisitWithExpectation(Expression* expr, Type* expected_type, |
| 1598 | const char* msg) { |
| 1599 | Type* save = expected_type_; |
| 1600 | expected_type_ = expected_type; |
| 1601 | RECURSE(Visit(expr)); |
| 1602 | Type* bounded_type = Type::Intersect(computed_type_, expected_type_, zone()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1603 | if (bounded_type->Is(Type::None())) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1604 | #ifdef DEBUG |
| 1605 | PrintF("Computed type: "); |
| 1606 | computed_type_->Print(); |
| 1607 | PrintF("Expected type: "); |
| 1608 | expected_type_->Print(); |
| 1609 | #endif |
| 1610 | FAIL(expr, msg); |
| 1611 | } |
| 1612 | expected_type_ = save; |
| 1613 | } |
| 1614 | |
| 1615 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1616 | void AsmTyper::VisitRewritableExpression(RewritableExpression* expr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1617 | RECURSE(Visit(expr->expression())); |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | } // namespace internal |
| 1622 | } // namespace v8 |