blob: ddb608fc2cf3154e12282598bfbfd15f83e67867 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
16namespace v8 {
17namespace 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 Murdoch4a90d5f2016-03-22 12:00:34 +000039AsmTyper::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 Murdoch097c5b22016-05-18 11:27:45 +010064 visiting_exports_(false),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 cache_(TypeCache::Get()) {
66 InitializeAstVisitor(isolate);
67 InitializeStdlib();
68}
69
70
71bool AsmTyper::Validate() {
72 VisitAsmModule(root_);
73 return valid_ && !HasStackOverflow();
74}
75
76
77void 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 Murdoch097c5b22016-05-18 11:27:45 +010092 SetType(param, Type::None());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093 }
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 Murdoch097c5b22016-05-18 11:27:45 +0100129 RECURSE(VisitWithExpectation(decl->fun(), Type::Any(), "UNREACHABLE"));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 if (!computed_type_->IsFunction()) {
131 FAIL(decl->fun(), "function literal expected to be a function");
132 }
133 }
134 }
135
136 // Validate exports.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100137 visiting_exports_ = true;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138 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
147void AsmTyper::VisitVariableDeclaration(VariableDeclaration* decl) {
148 Variable* var = decl->proxy()->var();
149 if (var->location() != VariableLocation::PARAMETER) {
150 if (GetType(var) == NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100151 SetType(var, Type::Any());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000152 } else {
153 DCHECK(!GetType(var)->IsFunction());
154 }
155 }
156 DCHECK(GetType(var) != NULL);
157 intish_ = 0;
158}
159
160
161void 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 Murdoch097c5b22016-05-18 11:27:45 +0100168 SetType(var, Type::Function());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000169}
170
171
172void AsmTyper::VisitFunctionAnnotation(FunctionLiteral* fun) {
173 // Extract result type.
174 ZoneList<Statement*>* body = fun->body();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100175 Type* result_type = Type::Undefined();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 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 Murdoch097c5b22016-05-18 11:27:45 +0100191 Type* type =
192 Type::Function(result_type, Type::Any(), fun->parameter_count(), zone());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193
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 Murdoch097c5b22016-05-18 11:27:45 +0100214 type->AsFunction()->InitParameter(i, computed_type_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000215 good = true;
216 }
217 if (!good) FAIL(fun, "missing parameter type annotations");
218
219 SetResult(fun, type);
220}
221
222
223void 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
303void 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
311void AsmTyper::VisitBlock(Block* stmt) {
312 RECURSE(VisitStatements(stmt->statements()));
313}
314
315
316void AsmTyper::VisitExpressionStatement(ExpressionStatement* stmt) {
317 RECURSE(VisitWithExpectation(stmt->expression(), Type::Any(),
318 "expression statement expected to be any"));
319}
320
321
322void AsmTyper::VisitEmptyStatement(EmptyStatement* stmt) {}
323
324
325void AsmTyper::VisitSloppyBlockFunctionStatement(
326 SloppyBlockFunctionStatement* stmt) {
327 Visit(stmt->statement());
328}
329
330
331void AsmTyper::VisitEmptyParentheses(EmptyParentheses* expr) { UNREACHABLE(); }
332
333
334void 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
345void AsmTyper::VisitContinueStatement(ContinueStatement* stmt) {
346 if (!in_function_) {
347 FAIL(stmt, "continue statement inside module body");
348 }
349}
350
351
352void AsmTyper::VisitBreakStatement(BreakStatement* stmt) {
353 if (!in_function_) {
354 FAIL(stmt, "continue statement inside module body");
355 }
356}
357
358
359void 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
378void AsmTyper::VisitWithStatement(WithStatement* stmt) {
379 FAIL(stmt, "bad with statement");
380}
381
382
383void 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
424void AsmTyper::VisitCaseClause(CaseClause* clause) { UNREACHABLE(); }
425
426
427void 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
437void 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
447void 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
465void AsmTyper::VisitForInStatement(ForInStatement* stmt) {
466 FAIL(stmt, "for-in statement encountered");
467}
468
469
470void AsmTyper::VisitForOfStatement(ForOfStatement* stmt) {
471 FAIL(stmt, "for-of statement encountered");
472}
473
474
475void AsmTyper::VisitTryCatchStatement(TryCatchStatement* stmt) {
476 FAIL(stmt, "try statement encountered");
477}
478
479
480void AsmTyper::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
481 FAIL(stmt, "try statement encountered");
482}
483
484
485void AsmTyper::VisitDebuggerStatement(DebuggerStatement* stmt) {
486 FAIL(stmt, "debugger statement encountered");
487}
488
489
490void AsmTyper::VisitFunctionLiteral(FunctionLiteral* expr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000491 if (in_function_) {
492 FAIL(expr, "invalid nested function");
493 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100494 Scope* scope = expr->scope();
495 DCHECK(scope->is_function_scope());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000496
497 if (!expr->bounds().upper->IsFunction()) {
498 FAIL(expr, "invalid function literal");
499 }
500
Ben Murdoch097c5b22016-05-18 11:27:45 +0100501 Type* type = expr->bounds().upper;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502 Type* save_return_type = return_type_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100503 return_type_ = type->AsFunction()->Result();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000504 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
514void AsmTyper::VisitNativeFunctionLiteral(NativeFunctionLiteral* expr) {
515 FAIL(expr, "function info literal encountered");
516}
517
518
519void AsmTyper::VisitDoExpression(DoExpression* expr) {
520 FAIL(expr, "do-expression encountered");
521}
522
523
524void AsmTyper::VisitConditional(Conditional* expr) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100525 if (!in_function_) {
526 FAIL(expr, "ternary operator inside module body");
527 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000528 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
558void AsmTyper::VisitVariableProxy(VariableProxy* expr) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100559 VisitVariableProxy(expr, false);
560}
561
562void AsmTyper::VisitVariableProxy(VariableProxy* expr, bool assignment) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000563 Variable* var = expr->var();
564 VariableInfo* info = GetVariableInfo(var, false);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100565 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000571 if (info == NULL || info->type == NULL) {
572 if (var->mode() == TEMPORARY) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100573 SetType(var, Type::Any());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000574 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
593void 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
622void AsmTyper::VisitLiteral(Literal* expr) { VisitLiteral(expr, false); }
623
624
625void AsmTyper::VisitRegExpLiteral(RegExpLiteral* expr) {
626 FAIL(expr, "regular expression encountered");
627}
628
629
630void 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 Murdoch097c5b22016-05-18 11:27:45 +0100638 RECURSE(VisitWithExpectation(prop->value(), Type::Any(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000639 "object property expected to be a function"));
640 if (!computed_type_->IsFunction()) {
641 FAIL(prop->value(), "non-function in function table");
642 }
643 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100644 IntersectResult(expr, Type::Object());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000645}
646
647
648void 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 Murdoch097c5b22016-05-18 11:27:45 +0100654 Type* elem_type = Type::None();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000655 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
668void 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000686 if (expr->target()->IsVariableProxy()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100687 if (intish_ != 0) {
688 FAIL(expr, "intish or floatish assignment");
689 }
690 expected_type_ = target_type;
691 VisitVariableProxy(expr->target()->AsVariableProxy(), true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000692 } else if (expr->target()->IsProperty()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100693 int value_intish = intish_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000694 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 Murdoch097c5b22016-05-18 11:27:45 +0100700 if (value_intish != 0 && computed_type_->Is(cache_.kFloat64Array)) {
701 FAIL(expr, "floatish assignment to double array");
702 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000703 VisitHeapAccess(property, true, target_type);
704 }
705 IntersectResult(expr, target_type);
706}
707
708
709void AsmTyper::VisitYield(Yield* expr) {
710 FAIL(expr, "yield expression encountered");
711}
712
713
714void AsmTyper::VisitThrow(Throw* expr) {
715 FAIL(expr, "throw statement encountered");
716}
717
718
719int 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
728Type* AsmTyper::StorageType(Type* type) {
729 if (type->Is(cache_.kAsmInt)) {
730 return cache_.kAsmInt;
731 } else {
732 return type;
733 }
734}
735
736
737void AsmTyper::VisitHeapAccess(Property* expr, bool assigning,
738 Type* assignment_type) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100739 ArrayType* array_type = computed_type_->AsArray();
740 // size_t size = array_size_;
741 Type* type = array_type->Element();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000742 if (type->IsFunction()) {
743 if (assigning) {
744 FAIL(expr, "assigning to function table is illegal");
745 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100746 // 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000765 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000772 int expected_shift = ElementShiftSize(type);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100773 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()) {
784 FAIL(right, "heap access shift must be integer");
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 Murdoch4a90d5f2016-03-22 12:00:34 +0000792 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100793 expr->key()->set_bounds(Bounds(cache_.kAsmSigned));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000794 }
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
831bool 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
851Expression* 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
866bool AsmTyper::IsMathObject(Expression* expr) {
867 Expression* obj = GetReceiverOfPropertyAccess(expr, "Math");
868 return obj && IsStdlibObject(obj);
869}
870
871
872bool AsmTyper::IsSIMDObject(Expression* expr) {
873 Expression* obj = GetReceiverOfPropertyAccess(expr, "SIMD");
874 return obj && IsStdlibObject(obj);
875}
876
877
878bool AsmTyper::IsSIMDTypeObject(Expression* expr, const char* name) {
879 Expression* obj = GetReceiverOfPropertyAccess(expr, name);
880 return obj && IsSIMDObject(obj);
881}
882
883
884void 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 Murdoch097c5b22016-05-18 11:27:45 +0100911 RECURSE(
912 VisitWithExpectation(expr->obj(), Type::Any(), "bad property object"));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000913
914 // For heap view or function table access.
915 if (computed_type_->IsArray()) {
916 VisitHeapAccess(expr, false, NULL);
917 return;
918 }
919
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000920 VariableProxy* proxy = expr->obj()->AsVariableProxy();
921 if (proxy != NULL) {
922 Variable* var = proxy->var();
923 if (var->location() == VariableLocation::PARAMETER && var->index() == 1) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100924 // 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000930 return;
931 }
932 }
933
934 FAIL(expr, "invalid property access");
935}
936
937
938void AsmTyper::VisitCall(Call* expr) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100939 Type* expected_type = expected_type_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000940 RECURSE(VisitWithExpectation(expr->expression(), Type::Any(),
941 "callee expected to be any"));
942 StandardMember standard_member = kNone;
943 VariableProxy* proxy = expr->expression()->AsVariableProxy();
944 if (proxy) {
945 standard_member = VariableAsStandardMember(proxy->var());
946 }
947 if (!in_function_ && (proxy == NULL || standard_member != kMathFround)) {
948 FAIL(expr, "calls forbidden outside function bodies");
949 }
950 if (proxy == NULL && !expr->expression()->IsProperty()) {
951 FAIL(expr, "calls must be to bound variables or function tables");
952 }
953 if (computed_type_->IsFunction()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100954 FunctionType* fun_type = computed_type_->AsFunction();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000955 Type* result_type = fun_type->Result();
956 ZoneList<Expression*>* args = expr->arguments();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100957 if (Type::Any()->Is(result_type)) {
958 // For foreign calls.
959 ZoneList<Expression*>* args = expr->arguments();
960 for (int i = 0; i < args->length(); ++i) {
961 Expression* arg = args->at(i);
962 RECURSE(VisitWithExpectation(
963 arg, Type::Any(), "foreign call argument expected to be any"));
964 // Checking for asm extern types explicitly, as the type system
965 // doesn't correctly check their inheritance relationship.
966 if (!computed_type_->Is(cache_.kAsmSigned) &&
967 !computed_type_->Is(cache_.kAsmFixnum) &&
968 !computed_type_->Is(cache_.kAsmDouble)) {
969 FAIL(arg,
970 "foreign call argument expected to be int, double, or fixnum");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000971 }
972 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100973 intish_ = 0;
974 expr->expression()->set_bounds(
975 Bounds(Type::Function(Type::Any(), zone())));
976 IntersectResult(expr, expected_type);
977 } else {
978 if (fun_type->Arity() != args->length()) {
979 FAIL(expr, "call with wrong arity");
980 }
981 for (int i = 0; i < args->length(); ++i) {
982 Expression* arg = args->at(i);
983 RECURSE(VisitWithExpectation(
984 arg, fun_type->Parameter(i),
985 "call argument expected to match callee parameter"));
986 if (standard_member != kNone && standard_member != kMathFround &&
987 i == 0) {
988 result_type = computed_type_;
989 }
990 }
991 // Handle polymorphic stdlib functions specially.
992 if (standard_member == kMathCeil || standard_member == kMathFloor ||
993 standard_member == kMathSqrt) {
994 if (!args->at(0)->bounds().upper->Is(cache_.kAsmFloat) &&
995 !args->at(0)->bounds().upper->Is(cache_.kAsmDouble)) {
996 FAIL(expr, "illegal function argument type");
997 }
998 } else if (standard_member == kMathAbs || standard_member == kMathMin ||
999 standard_member == kMathMax) {
1000 if (!args->at(0)->bounds().upper->Is(cache_.kAsmFloat) &&
1001 !args->at(0)->bounds().upper->Is(cache_.kAsmDouble) &&
1002 !args->at(0)->bounds().upper->Is(cache_.kAsmSigned)) {
1003 FAIL(expr, "illegal function argument type");
1004 }
1005 if (args->length() > 1) {
1006 Type* other = Type::Intersect(args->at(0)->bounds().upper,
1007 args->at(1)->bounds().upper, zone());
1008 if (!other->Is(cache_.kAsmFloat) && !other->Is(cache_.kAsmDouble) &&
1009 !other->Is(cache_.kAsmSigned)) {
1010 FAIL(expr, "function arguments types don't match");
1011 }
1012 }
1013 }
1014 intish_ = 0;
1015 IntersectResult(expr, result_type);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001016 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001017 } else {
1018 FAIL(expr, "invalid callee");
1019 }
1020}
1021
1022
1023void AsmTyper::VisitCallNew(CallNew* expr) {
1024 if (in_function_) {
1025 FAIL(expr, "new not allowed in module function");
1026 }
1027 RECURSE(VisitWithExpectation(expr->expression(), Type::Any(),
1028 "expected stdlib function"));
1029 if (computed_type_->IsFunction()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001030 FunctionType* fun_type = computed_type_->AsFunction();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001031 ZoneList<Expression*>* args = expr->arguments();
1032 if (fun_type->Arity() != args->length())
1033 FAIL(expr, "call with wrong arity");
1034 for (int i = 0; i < args->length(); ++i) {
1035 Expression* arg = args->at(i);
1036 RECURSE(VisitWithExpectation(
1037 arg, fun_type->Parameter(i),
1038 "constructor argument expected to match callee parameter"));
1039 }
1040 IntersectResult(expr, fun_type->Result());
1041 return;
1042 }
1043
1044 FAIL(expr, "ill-typed new operator");
1045}
1046
1047
1048void AsmTyper::VisitCallRuntime(CallRuntime* expr) {
1049 // Allow runtime calls for now.
1050}
1051
1052
1053void AsmTyper::VisitUnaryOperation(UnaryOperation* expr) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001054 if (!in_function_) {
1055 FAIL(expr, "unary operator inside module body");
1056 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001057 switch (expr->op()) {
1058 case Token::NOT: // Used to encode != and !==
1059 RECURSE(VisitWithExpectation(expr->expression(), cache_.kAsmInt,
1060 "operand expected to be integer"));
1061 IntersectResult(expr, cache_.kAsmSigned);
1062 return;
1063 case Token::DELETE:
1064 FAIL(expr, "delete operator encountered");
1065 case Token::VOID:
1066 FAIL(expr, "void operator encountered");
1067 case Token::TYPEOF:
1068 FAIL(expr, "typeof operator encountered");
1069 default:
1070 UNREACHABLE();
1071 }
1072}
1073
1074
1075void AsmTyper::VisitCountOperation(CountOperation* expr) {
1076 FAIL(expr, "increment or decrement operator encountered");
1077}
1078
1079
1080void AsmTyper::VisitIntegerBitwiseOperator(BinaryOperation* expr,
1081 Type* left_expected,
1082 Type* right_expected,
1083 Type* result_type, bool conversion) {
1084 RECURSE(VisitWithExpectation(expr->left(), Type::Number(),
1085 "left bitwise operand expected to be a number"));
1086 int left_intish = intish_;
1087 Type* left_type = computed_type_;
1088 if (!left_type->Is(left_expected)) {
1089 FAIL(expr->left(), "left bitwise operand expected to be an integer");
1090 }
1091 if (left_intish > kMaxUncombinedAdditiveSteps) {
1092 FAIL(expr->left(), "too many consecutive additive ops");
1093 }
1094
1095 RECURSE(
1096 VisitWithExpectation(expr->right(), Type::Number(),
1097 "right bitwise operand expected to be a number"));
1098 int right_intish = intish_;
1099 Type* right_type = computed_type_;
1100 if (!right_type->Is(right_expected)) {
1101 FAIL(expr->right(), "right bitwise operand expected to be an integer");
1102 }
1103 if (right_intish > kMaxUncombinedAdditiveSteps) {
1104 FAIL(expr->right(), "too many consecutive additive ops");
1105 }
1106
1107 intish_ = 0;
1108
1109 if (left_type->Is(cache_.kAsmFixnum) && right_type->Is(cache_.kAsmInt)) {
1110 left_type = right_type;
1111 }
1112 if (right_type->Is(cache_.kAsmFixnum) && left_type->Is(cache_.kAsmInt)) {
1113 right_type = left_type;
1114 }
1115 if (!conversion) {
1116 if (!left_type->Is(right_type) || !right_type->Is(left_type)) {
1117 FAIL(expr, "ill-typed bitwise operation");
1118 }
1119 }
1120 IntersectResult(expr, result_type);
1121}
1122
1123
1124void AsmTyper::VisitBinaryOperation(BinaryOperation* expr) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001125 if (!in_function_) {
1126 if (expr->op() != Token::BIT_OR && expr->op() != Token::MUL) {
1127 FAIL(expr, "illegal binary operator inside module body");
1128 }
1129 if (!(expr->left()->IsProperty() || expr->left()->IsVariableProxy()) ||
1130 !expr->right()->IsLiteral()) {
1131 FAIL(expr, "illegal computation inside module body");
1132 }
1133 DCHECK(expr->right()->AsLiteral() != nullptr);
1134 const AstValue* right_value = expr->right()->AsLiteral()->raw_value();
1135 if (expr->op() == Token::BIT_OR) {
1136 if (right_value->AsNumber() != 0.0 || right_value->ContainsDot()) {
1137 FAIL(expr, "illegal integer annotation value");
1138 }
1139 }
1140 if (expr->op() == Token::MUL) {
1141 if (right_value->AsNumber() != 1.0 && right_value->ContainsDot()) {
1142 FAIL(expr, "illegal double annotation value");
1143 }
1144 }
1145 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001146 switch (expr->op()) {
1147 case Token::COMMA: {
1148 RECURSE(VisitWithExpectation(expr->left(), Type::Any(),
1149 "left comma operand expected to be any"));
1150 RECURSE(VisitWithExpectation(expr->right(), Type::Any(),
1151 "right comma operand expected to be any"));
1152 IntersectResult(expr, computed_type_);
1153 return;
1154 }
1155 case Token::OR:
1156 case Token::AND:
1157 FAIL(expr, "illegal logical operator");
1158 case Token::BIT_OR: {
1159 // BIT_OR allows Any since it is used as a type coercion.
1160 VisitIntegerBitwiseOperator(expr, Type::Any(), cache_.kAsmInt,
1161 cache_.kAsmSigned, true);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001162 if (expr->left()->IsCall() && expr->op() == Token::BIT_OR) {
1163 expr->left()->set_bounds(Bounds(cache_.kAsmSigned));
1164 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001165 return;
1166 }
1167 case Token::BIT_XOR: {
1168 // Handle booleans specially to handle de-sugared !
1169 Literal* left = expr->left()->AsLiteral();
1170 if (left && left->value()->IsBoolean()) {
1171 if (left->ToBooleanIsTrue()) {
1172 left->set_bounds(Bounds(cache_.kSingletonOne));
1173 RECURSE(VisitWithExpectation(expr->right(), cache_.kAsmInt,
1174 "not operator expects an integer"));
1175 IntersectResult(expr, cache_.kAsmSigned);
1176 return;
1177 } else {
1178 FAIL(left, "unexpected false");
1179 }
1180 }
1181 // BIT_XOR allows Number since it is used as a type coercion (via ~~).
1182 VisitIntegerBitwiseOperator(expr, Type::Number(), cache_.kAsmInt,
1183 cache_.kAsmSigned, true);
1184 return;
1185 }
1186 case Token::SHR: {
1187 VisitIntegerBitwiseOperator(expr, cache_.kAsmInt, cache_.kAsmInt,
1188 cache_.kAsmUnsigned, false);
1189 return;
1190 }
1191 case Token::SHL:
1192 case Token::SAR:
1193 case Token::BIT_AND: {
1194 VisitIntegerBitwiseOperator(expr, cache_.kAsmInt, cache_.kAsmInt,
1195 cache_.kAsmSigned, false);
1196 return;
1197 }
1198 case Token::ADD:
1199 case Token::SUB:
1200 case Token::MUL:
1201 case Token::DIV:
1202 case Token::MOD: {
1203 RECURSE(VisitWithExpectation(
1204 expr->left(), Type::Number(),
1205 "left arithmetic operand expected to be number"));
1206 Type* left_type = computed_type_;
1207 int left_intish = intish_;
1208 RECURSE(VisitWithExpectation(
1209 expr->right(), Type::Number(),
1210 "right arithmetic operand expected to be number"));
1211 Type* right_type = computed_type_;
1212 int right_intish = intish_;
1213 Type* type = Type::Union(left_type, right_type, zone());
1214 if (type->Is(cache_.kAsmInt)) {
1215 if (expr->op() == Token::MUL) {
1216 Literal* right = expr->right()->AsLiteral();
1217 if (!right) {
1218 FAIL(expr, "direct integer multiply forbidden");
1219 }
1220 if (!right->value()->IsNumber()) {
1221 FAIL(expr, "multiply must be by an integer");
1222 }
1223 int32_t i;
1224 if (!right->value()->ToInt32(&i)) {
1225 FAIL(expr, "multiply must be a signed integer");
1226 }
1227 i = abs(i);
1228 if (i >= 1 << 20) {
1229 FAIL(expr, "multiply must be by value in -2^20 < n < 2^20");
1230 }
1231 intish_ = i;
1232 IntersectResult(expr, cache_.kAsmInt);
1233 return;
1234 } else {
1235 intish_ = left_intish + right_intish + 1;
1236 if (expr->op() == Token::ADD || expr->op() == Token::SUB) {
1237 if (intish_ > kMaxUncombinedAdditiveSteps) {
1238 FAIL(expr, "too many consecutive additive ops");
1239 }
1240 } else {
1241 if (intish_ > kMaxUncombinedMultiplicativeSteps) {
1242 FAIL(expr, "too many consecutive multiplicative ops");
1243 }
1244 }
1245 IntersectResult(expr, cache_.kAsmInt);
1246 return;
1247 }
1248 } else if (expr->op() == Token::MUL && expr->right()->IsLiteral() &&
1249 right_type->Is(cache_.kAsmDouble)) {
1250 // For unary +, expressed as x * 1.0
Ben Murdoch097c5b22016-05-18 11:27:45 +01001251 if (expr->left()->IsCall() && expr->op() == Token::MUL) {
1252 expr->left()->set_bounds(Bounds(cache_.kAsmDouble));
1253 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001254 IntersectResult(expr, cache_.kAsmDouble);
1255 return;
1256 } else if (type->Is(cache_.kAsmFloat) && expr->op() != Token::MOD) {
1257 if (left_intish != 0 || right_intish != 0) {
1258 FAIL(expr, "float operation before required fround");
1259 }
1260 IntersectResult(expr, cache_.kAsmFloat);
1261 intish_ = 1;
1262 return;
1263 } else if (type->Is(cache_.kAsmDouble)) {
1264 IntersectResult(expr, cache_.kAsmDouble);
1265 return;
1266 } else {
1267 FAIL(expr, "ill-typed arithmetic operation");
1268 }
1269 }
1270 default:
1271 UNREACHABLE();
1272 }
1273}
1274
1275
1276void AsmTyper::VisitCompareOperation(CompareOperation* expr) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001277 if (!in_function_) {
1278 FAIL(expr, "comparison inside module body");
1279 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001280 Token::Value op = expr->op();
1281 if (op != Token::EQ && op != Token::NE && op != Token::LT &&
1282 op != Token::LTE && op != Token::GT && op != Token::GTE) {
1283 FAIL(expr, "illegal comparison operator");
1284 }
1285
1286 RECURSE(
1287 VisitWithExpectation(expr->left(), Type::Number(),
1288 "left comparison operand expected to be number"));
1289 Type* left_type = computed_type_;
1290 if (!left_type->Is(cache_.kAsmComparable)) {
1291 FAIL(expr->left(), "bad type on left side of comparison");
1292 }
1293
1294 RECURSE(
1295 VisitWithExpectation(expr->right(), Type::Number(),
1296 "right comparison operand expected to be number"));
1297 Type* right_type = computed_type_;
1298 if (!right_type->Is(cache_.kAsmComparable)) {
1299 FAIL(expr->right(), "bad type on right side of comparison");
1300 }
1301
1302 if (!left_type->Is(right_type) && !right_type->Is(left_type)) {
1303 FAIL(expr, "left and right side of comparison must match");
1304 }
1305
1306 IntersectResult(expr, cache_.kAsmSigned);
1307}
1308
1309
1310void AsmTyper::VisitThisFunction(ThisFunction* expr) {
1311 FAIL(expr, "this function not allowed");
1312}
1313
1314
1315void AsmTyper::VisitDeclarations(ZoneList<Declaration*>* decls) {
1316 for (int i = 0; i < decls->length(); ++i) {
1317 Declaration* decl = decls->at(i);
1318 RECURSE(Visit(decl));
1319 }
1320}
1321
1322
1323void AsmTyper::VisitImportDeclaration(ImportDeclaration* decl) {
1324 FAIL(decl, "import declaration encountered");
1325}
1326
1327
1328void AsmTyper::VisitExportDeclaration(ExportDeclaration* decl) {
1329 FAIL(decl, "export declaration encountered");
1330}
1331
1332
1333void AsmTyper::VisitClassLiteral(ClassLiteral* expr) {
1334 FAIL(expr, "class literal not allowed");
1335}
1336
1337
1338void AsmTyper::VisitSpread(Spread* expr) { FAIL(expr, "spread not allowed"); }
1339
1340
1341void AsmTyper::VisitSuperPropertyReference(SuperPropertyReference* expr) {
1342 FAIL(expr, "super property reference not allowed");
1343}
1344
1345
1346void AsmTyper::VisitSuperCallReference(SuperCallReference* expr) {
1347 FAIL(expr, "call reference not allowed");
1348}
1349
1350
1351void AsmTyper::InitializeStdlibSIMD() {
1352#define V(NAME, Name, name, lane_count, lane_type) \
1353 { \
1354 Type* type = Type::Function(Type::Name(isolate_, zone()), Type::Any(), \
1355 lane_count, zone()); \
1356 for (int i = 0; i < lane_count; ++i) { \
1357 type->AsFunction()->InitParameter(i, Type::Number()); \
1358 } \
1359 stdlib_simd_##name##_constructor_type_ = new (zone()) VariableInfo(type); \
1360 stdlib_simd_##name##_constructor_type_->is_constructor_function = true; \
1361 }
1362 SIMD128_TYPES(V)
1363#undef V
1364}
1365
1366
1367void AsmTyper::InitializeStdlib() {
1368 if (allow_simd_) {
1369 InitializeStdlibSIMD();
1370 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001371 Type* number_type = Type::Number();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001372 Type* double_type = cache_.kAsmDouble;
1373 Type* double_fn1_type = Type::Function(double_type, double_type, zone());
1374 Type* double_fn2_type =
1375 Type::Function(double_type, double_type, double_type, zone());
1376
1377 Type* fround_type = Type::Function(cache_.kAsmFloat, number_type, zone());
1378 Type* imul_type =
1379 Type::Function(cache_.kAsmSigned, cache_.kAsmInt, cache_.kAsmInt, zone());
1380 // TODO(bradnelson): currently only approximating the proper intersection type
1381 // (which we cannot currently represent).
1382 Type* number_fn1_type = Type::Function(number_type, number_type, zone());
1383 Type* number_fn2_type =
1384 Type::Function(number_type, number_type, number_type, zone());
1385
1386 struct Assignment {
1387 const char* name;
1388 StandardMember standard_member;
1389 Type* type;
1390 };
1391
1392 const Assignment math[] = {{"PI", kMathPI, double_type},
1393 {"E", kMathE, double_type},
1394 {"LN2", kMathLN2, double_type},
1395 {"LN10", kMathLN10, double_type},
1396 {"LOG2E", kMathLOG2E, double_type},
1397 {"LOG10E", kMathLOG10E, double_type},
1398 {"SQRT2", kMathSQRT2, double_type},
1399 {"SQRT1_2", kMathSQRT1_2, double_type},
1400 {"imul", kMathImul, imul_type},
1401 {"abs", kMathAbs, number_fn1_type},
1402 {"ceil", kMathCeil, number_fn1_type},
1403 {"floor", kMathFloor, number_fn1_type},
1404 {"fround", kMathFround, fround_type},
1405 {"pow", kMathPow, double_fn2_type},
1406 {"exp", kMathExp, double_fn1_type},
1407 {"log", kMathLog, double_fn1_type},
1408 {"min", kMathMin, number_fn2_type},
1409 {"max", kMathMax, number_fn2_type},
1410 {"sqrt", kMathSqrt, number_fn1_type},
1411 {"cos", kMathCos, double_fn1_type},
1412 {"sin", kMathSin, double_fn1_type},
1413 {"tan", kMathTan, double_fn1_type},
1414 {"acos", kMathAcos, double_fn1_type},
1415 {"asin", kMathAsin, double_fn1_type},
1416 {"atan", kMathAtan, double_fn1_type},
1417 {"atan2", kMathAtan2, double_fn2_type}};
1418 for (unsigned i = 0; i < arraysize(math); ++i) {
1419 stdlib_math_types_[math[i].name] = new (zone()) VariableInfo(math[i].type);
1420 stdlib_math_types_[math[i].name]->standard_member = math[i].standard_member;
1421 }
1422 stdlib_math_types_["fround"]->is_check_function = true;
1423
1424 stdlib_types_["Infinity"] = new (zone()) VariableInfo(double_type);
1425 stdlib_types_["Infinity"]->standard_member = kInfinity;
1426 stdlib_types_["NaN"] = new (zone()) VariableInfo(double_type);
1427 stdlib_types_["NaN"]->standard_member = kNaN;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001428 Type* buffer_type = Type::Any();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001429#define TYPED_ARRAY(TypeName, type_name, TYPE_NAME, ctype, size) \
1430 stdlib_types_[#TypeName "Array"] = new (zone()) VariableInfo( \
1431 Type::Function(cache_.k##TypeName##Array, buffer_type, zone()));
1432 TYPED_ARRAYS(TYPED_ARRAY)
1433#undef TYPED_ARRAY
1434
1435#define TYPED_ARRAY(TypeName, type_name, TYPE_NAME, ctype, size) \
1436 stdlib_heap_types_[#TypeName "Array"] = new (zone()) VariableInfo( \
1437 Type::Function(cache_.k##TypeName##Array, buffer_type, zone()));
1438 TYPED_ARRAYS(TYPED_ARRAY)
1439#undef TYPED_ARRAY
1440}
1441
1442
1443void AsmTyper::VisitLibraryAccess(ObjectTypeMap* map, Property* expr) {
1444 Literal* key = expr->key()->AsLiteral();
1445 if (key == NULL || !key->IsPropertyName())
1446 FAIL(expr, "invalid key used on stdlib member");
1447 Handle<String> name = key->AsPropertyName();
1448 VariableInfo* info = LibType(map, name);
1449 if (info == NULL || info->type == NULL) FAIL(expr, "unknown stdlib function");
1450 SetResult(expr, info->type);
1451 property_info_ = info;
1452}
1453
1454
1455AsmTyper::VariableInfo* AsmTyper::LibType(ObjectTypeMap* map,
1456 Handle<String> name) {
1457 base::SmartArrayPointer<char> aname = name->ToCString();
1458 ObjectTypeMap::iterator i = map->find(std::string(aname.get()));
1459 if (i == map->end()) {
1460 return NULL;
1461 }
1462 return i->second;
1463}
1464
1465
1466void AsmTyper::SetType(Variable* variable, Type* type) {
1467 VariableInfo* info = GetVariableInfo(variable, true);
1468 info->type = type;
1469}
1470
1471
1472Type* AsmTyper::GetType(Variable* variable) {
1473 VariableInfo* info = GetVariableInfo(variable, false);
1474 if (!info) return NULL;
1475 return info->type;
1476}
1477
1478
1479AsmTyper::VariableInfo* AsmTyper::GetVariableInfo(Variable* variable,
1480 bool setting) {
1481 ZoneHashMap::Entry* entry;
1482 ZoneHashMap* map;
1483 if (in_function_) {
1484 map = &local_variable_type_;
1485 } else {
1486 map = &global_variable_type_;
1487 }
1488 if (setting) {
1489 entry = map->LookupOrInsert(variable, ComputePointerHash(variable),
1490 ZoneAllocationPolicy(zone()));
1491 } else {
1492 entry = map->Lookup(variable, ComputePointerHash(variable));
1493 if (!entry && in_function_) {
1494 entry =
1495 global_variable_type_.Lookup(variable, ComputePointerHash(variable));
1496 if (entry && entry->value) {
1497 }
1498 }
1499 }
1500 if (!entry) return NULL;
1501 if (!entry->value) {
1502 if (!setting) return NULL;
1503 entry->value = new (zone()) VariableInfo;
1504 }
1505 return reinterpret_cast<VariableInfo*>(entry->value);
1506}
1507
1508
1509void AsmTyper::SetVariableInfo(Variable* variable, const VariableInfo* info) {
1510 VariableInfo* dest = GetVariableInfo(variable, true);
1511 dest->type = info->type;
1512 dest->is_check_function = info->is_check_function;
1513 dest->is_constructor_function = info->is_constructor_function;
1514 dest->standard_member = info->standard_member;
1515}
1516
1517
1518AsmTyper::StandardMember AsmTyper::VariableAsStandardMember(
1519 Variable* variable) {
1520 VariableInfo* info = GetVariableInfo(variable, false);
1521 if (!info) return kNone;
1522 return info->standard_member;
1523}
1524
1525
1526void AsmTyper::SetResult(Expression* expr, Type* type) {
1527 computed_type_ = type;
1528 expr->set_bounds(Bounds(computed_type_));
1529}
1530
1531
1532void AsmTyper::IntersectResult(Expression* expr, Type* type) {
1533 computed_type_ = type;
1534 Type* bounded_type = Type::Intersect(computed_type_, expected_type_, zone());
1535 expr->set_bounds(Bounds(bounded_type));
1536}
1537
1538
1539void AsmTyper::VisitWithExpectation(Expression* expr, Type* expected_type,
1540 const char* msg) {
1541 Type* save = expected_type_;
1542 expected_type_ = expected_type;
1543 RECURSE(Visit(expr));
1544 Type* bounded_type = Type::Intersect(computed_type_, expected_type_, zone());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001545 if (bounded_type->Is(Type::None())) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001546#ifdef DEBUG
1547 PrintF("Computed type: ");
1548 computed_type_->Print();
1549 PrintF("Expected type: ");
1550 expected_type_->Print();
1551#endif
1552 FAIL(expr, msg);
1553 }
1554 expected_type_ = save;
1555}
1556
1557
Ben Murdoch097c5b22016-05-18 11:27:45 +01001558void AsmTyper::VisitRewritableExpression(RewritableExpression* expr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001559 RECURSE(Visit(expr->expression()));
1560}
1561
1562
1563} // namespace internal
1564} // namespace v8