OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/wasm/asm-wasm-builder.h" | 7 #include "src/wasm/asm-wasm-builder.h" |
8 #include "src/wasm/wasm-macro-gen.h" | 8 #include "src/wasm/wasm-macro-gen.h" |
9 #include "src/wasm/wasm-opcodes.h" | 9 #include "src/wasm/wasm-opcodes.h" |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 ZoneAllocationPolicy(zone)), | 33 ZoneAllocationPolicy(zone)), |
34 functions_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity, | 34 functions_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity, |
35 ZoneAllocationPolicy(zone)), | 35 ZoneAllocationPolicy(zone)), |
36 global_variables_(HashMap::PointersMatch, | 36 global_variables_(HashMap::PointersMatch, |
37 ZoneHashMap::kDefaultHashMapCapacity, | 37 ZoneHashMap::kDefaultHashMapCapacity, |
38 ZoneAllocationPolicy(zone)), | 38 ZoneAllocationPolicy(zone)), |
39 in_function_(false), | 39 in_function_(false), |
40 is_set_op_(false), | 40 is_set_op_(false), |
41 marking_exported(false), | 41 marking_exported(false), |
42 builder_(new (zone) WasmModuleBuilder(zone)), | 42 builder_(new (zone) WasmModuleBuilder(zone)), |
43 current_function_builder_(NULL), | 43 current_function_builder_(nullptr), |
44 literal_(literal), | 44 literal_(literal), |
45 isolate_(isolate), | 45 isolate_(isolate), |
46 zone_(zone), | 46 zone_(zone), |
47 cache_(TypeCache::Get()), | 47 cache_(TypeCache::Get()), |
48 breakable_blocks_(zone), | 48 breakable_blocks_(zone), |
49 block_size_(0), | 49 block_size_(0), |
50 init_function_initialized(false), | 50 init_function_initialized(false), |
51 init_function_index(0) { | 51 init_function_index(0) { |
52 InitializeAstVisitor(isolate); | 52 InitializeAstVisitor(isolate); |
53 } | 53 } |
54 | 54 |
55 void Compile() { RECURSE(VisitFunctionLiteral(literal_)); } | 55 void Compile() { RECURSE(VisitFunctionLiteral(literal_)); } |
56 | 56 |
57 void VisitVariableDeclaration(VariableDeclaration* decl) {} | 57 void VisitVariableDeclaration(VariableDeclaration* decl) {} |
58 | 58 |
59 void VisitFunctionDeclaration(FunctionDeclaration* decl) { | 59 void VisitFunctionDeclaration(FunctionDeclaration* decl) { |
60 DCHECK(!in_function_); | 60 DCHECK(!in_function_); |
61 DCHECK(current_function_builder_ == NULL); | 61 DCHECK(current_function_builder_ == nullptr); |
62 uint16_t index = LookupOrInsertFunction(decl->proxy()->var()); | 62 uint16_t index = LookupOrInsertFunction(decl->proxy()->var()); |
63 current_function_builder_ = builder_->FunctionAt(index); | 63 current_function_builder_ = builder_->FunctionAt(index); |
64 in_function_ = true; | 64 in_function_ = true; |
65 RECURSE(Visit(decl->fun())); | 65 RECURSE(Visit(decl->fun())); |
66 in_function_ = false; | 66 in_function_ = false; |
67 current_function_builder_ = NULL; | 67 current_function_builder_ = nullptr; |
68 local_variables_.Clear(); | 68 local_variables_.Clear(); |
69 } | 69 } |
70 | 70 |
71 void VisitImportDeclaration(ImportDeclaration* decl) {} | 71 void VisitImportDeclaration(ImportDeclaration* decl) {} |
72 | 72 |
73 void VisitExportDeclaration(ExportDeclaration* decl) {} | 73 void VisitExportDeclaration(ExportDeclaration* decl) {} |
74 | 74 |
75 void VisitStatements(ZoneList<Statement*>* stmts) { | 75 void VisitStatements(ZoneList<Statement*>* stmts) { |
76 for (int i = 0; i < stmts->length(); ++i) { | 76 for (int i = 0; i < stmts->length(); ++i) { |
77 Statement* stmt = stmts->at(i); | 77 Statement* stmt = stmts->at(i); |
78 RECURSE(Visit(stmt)); | 78 RECURSE(Visit(stmt)); |
79 if (stmt->IsJump()) break; | 79 if (stmt->IsJump()) break; |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 void VisitBlock(Block* stmt) { | 83 void VisitBlock(Block* stmt) { |
84 if (stmt->statements()->length() == 1) { | 84 if (stmt->statements()->length() == 1) { |
85 ExpressionStatement* expr = | 85 ExpressionStatement* expr = |
86 stmt->statements()->at(0)->AsExpressionStatement(); | 86 stmt->statements()->at(0)->AsExpressionStatement(); |
87 if (expr != NULL) { | 87 if (expr != nullptr) { |
88 if (expr->expression()->IsAssignment()) { | 88 if (expr->expression()->IsAssignment()) { |
89 RECURSE(VisitExpressionStatement(expr)); | 89 RECURSE(VisitExpressionStatement(expr)); |
90 return; | 90 return; |
91 } | 91 } |
92 } | 92 } |
93 } | 93 } |
94 DCHECK(in_function_); | 94 DCHECK(in_function_); |
95 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprBlock, false); | 95 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprBlock, false); |
96 block_size_ = static_cast<byte>(stmt->statements()->length()); | 96 block_size_ = static_cast<byte>(stmt->statements()->length()); |
97 RECURSE(VisitStatements(stmt->statements())); | 97 RECURSE(VisitStatements(stmt->statements())); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); | 237 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); |
238 block_size_ = 1; | 238 block_size_ = 1; |
239 current_function_builder_->Emit(kExprIf); | 239 current_function_builder_->Emit(kExprIf); |
240 RECURSE(Visit(stmt->cond())); | 240 RECURSE(Visit(stmt->cond())); |
241 current_function_builder_->EmitWithU8(kExprBr, 0); | 241 current_function_builder_->EmitWithU8(kExprBr, 0); |
242 RECURSE(Visit(stmt->body())); | 242 RECURSE(Visit(stmt->body())); |
243 } | 243 } |
244 | 244 |
245 void VisitForStatement(ForStatement* stmt) { | 245 void VisitForStatement(ForStatement* stmt) { |
246 DCHECK(in_function_); | 246 DCHECK(in_function_); |
247 if (stmt->init() != NULL) { | 247 if (stmt->init() != nullptr) { |
248 block_size_++; | 248 block_size_++; |
249 RECURSE(Visit(stmt->init())); | 249 RECURSE(Visit(stmt->init())); |
250 } | 250 } |
251 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); | 251 BlockVisitor visitor(this, stmt->AsBreakableStatement(), kExprLoop, true); |
252 block_size_ = 0; | 252 block_size_ = 0; |
253 if (stmt->cond() != NULL) { | 253 if (stmt->cond() != nullptr) { |
254 block_size_++; | 254 block_size_++; |
255 current_function_builder_->Emit(kExprIf); | 255 current_function_builder_->Emit(kExprIf); |
256 current_function_builder_->Emit(kExprBoolNot); | 256 current_function_builder_->Emit(kExprBoolNot); |
257 RECURSE(Visit(stmt->cond())); | 257 RECURSE(Visit(stmt->cond())); |
258 current_function_builder_->EmitWithU8(kExprBr, 1); | 258 current_function_builder_->EmitWithU8(kExprBr, 1); |
259 current_function_builder_->Emit(kExprNop); | 259 current_function_builder_->Emit(kExprNop); |
260 } | 260 } |
261 if (stmt->body() != NULL) { | 261 if (stmt->body() != nullptr) { |
262 block_size_++; | 262 block_size_++; |
263 RECURSE(Visit(stmt->body())); | 263 RECURSE(Visit(stmt->body())); |
264 } | 264 } |
265 if (stmt->next() != NULL) { | 265 if (stmt->next() != nullptr) { |
266 block_size_++; | 266 block_size_++; |
267 RECURSE(Visit(stmt->next())); | 267 RECURSE(Visit(stmt->next())); |
268 } | 268 } |
269 block_size_++; | 269 block_size_++; |
270 current_function_builder_->EmitWithU8(kExprBr, 0); | 270 current_function_builder_->EmitWithU8(kExprBr, 0); |
271 current_function_builder_->Emit(kExprNop); | 271 current_function_builder_->Emit(kExprNop); |
272 } | 272 } |
273 | 273 |
274 void VisitForInStatement(ForInStatement* stmt) { UNREACHABLE(); } | 274 void VisitForInStatement(ForInStatement* stmt) { UNREACHABLE(); } |
275 | 275 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 current_function_builder_->Exported(1); | 406 current_function_builder_->Exported(1); |
407 in_function_ = true; | 407 in_function_ = true; |
408 } else { | 408 } else { |
409 current_function_builder_ = builder_->FunctionAt(init_function_index); | 409 current_function_builder_ = builder_->FunctionAt(init_function_index); |
410 in_function_ = true; | 410 in_function_ = true; |
411 } | 411 } |
412 } | 412 } |
413 | 413 |
414 void UnLoadInitFunction() { | 414 void UnLoadInitFunction() { |
415 in_function_ = false; | 415 in_function_ = false; |
416 current_function_builder_ = NULL; | 416 current_function_builder_ = nullptr; |
417 } | 417 } |
418 | 418 |
419 void VisitAssignment(Assignment* expr) { | 419 void VisitAssignment(Assignment* expr) { |
420 bool in_init = false; | 420 bool in_init = false; |
421 if (!in_function_) { | 421 if (!in_function_) { |
422 // TODO(bradnelson): Get rid of this. | 422 // TODO(bradnelson): Get rid of this. |
423 if (TypeOf(expr->value()) == kAstStmt) { | 423 if (TypeOf(expr->value()) == kAstStmt) { |
424 return; | 424 return; |
425 } | 425 } |
426 in_init = true; | 426 in_init = true; |
427 LoadInitFunction(); | 427 LoadInitFunction(); |
428 } | 428 } |
429 BinaryOperation* value_op = expr->value()->AsBinaryOperation(); | 429 BinaryOperation* value_op = expr->value()->AsBinaryOperation(); |
430 if (value_op != NULL && MatchBinaryOperation(value_op) == kAsIs) { | 430 if (value_op != nullptr && MatchBinaryOperation(value_op) == kAsIs) { |
431 VariableProxy* target_var = expr->target()->AsVariableProxy(); | 431 VariableProxy* target_var = expr->target()->AsVariableProxy(); |
432 VariableProxy* effective_value_var = GetLeft(value_op)->AsVariableProxy(); | 432 VariableProxy* effective_value_var = GetLeft(value_op)->AsVariableProxy(); |
433 // TODO(aseemgarg): simplify block_size_ or replace with a kNop | 433 // TODO(aseemgarg): simplify block_size_ or replace with a kNop |
434 if (target_var != NULL && effective_value_var != NULL && | 434 if (target_var != nullptr && effective_value_var != nullptr && |
435 target_var->var() == effective_value_var->var()) { | 435 target_var->var() == effective_value_var->var()) { |
436 block_size_--; | 436 block_size_--; |
437 return; | 437 return; |
438 } | 438 } |
439 } | 439 } |
440 is_set_op_ = true; | 440 is_set_op_ = true; |
441 RECURSE(Visit(expr->target())); | 441 RECURSE(Visit(expr->target())); |
442 DCHECK(!is_set_op_); | 442 DCHECK(!is_set_op_); |
443 RECURSE(Visit(expr->value())); | 443 RECURSE(Visit(expr->value())); |
444 if (in_init) { | 444 if (in_init) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 default: | 550 default: |
551 UNREACHABLE(); | 551 UNREACHABLE(); |
552 } | 552 } |
553 RECURSE(Visit(expr->expression())); | 553 RECURSE(Visit(expr->expression())); |
554 } | 554 } |
555 | 555 |
556 void VisitCountOperation(CountOperation* expr) { UNREACHABLE(); } | 556 void VisitCountOperation(CountOperation* expr) { UNREACHABLE(); } |
557 | 557 |
558 bool MatchIntBinaryOperation(BinaryOperation* expr, Token::Value op, | 558 bool MatchIntBinaryOperation(BinaryOperation* expr, Token::Value op, |
559 int32_t val) { | 559 int32_t val) { |
560 DCHECK(expr->right() != NULL); | 560 DCHECK(expr->right() != nullptr); |
561 if (expr->op() == op && expr->right()->IsLiteral() && | 561 if (expr->op() == op && expr->right()->IsLiteral() && |
562 TypeOf(expr) == kAstI32) { | 562 TypeOf(expr) == kAstI32) { |
563 Literal* right = expr->right()->AsLiteral(); | 563 Literal* right = expr->right()->AsLiteral(); |
564 DCHECK(right->raw_value()->IsNumber()); | 564 DCHECK(right->raw_value()->IsNumber()); |
565 if (static_cast<int32_t>(right->raw_value()->AsNumber()) == val) { | 565 if (static_cast<int32_t>(right->raw_value()->AsNumber()) == val) { |
566 return true; | 566 return true; |
567 } | 567 } |
568 } | 568 } |
569 return false; | 569 return false; |
570 } | 570 } |
571 | 571 |
572 bool MatchDoubleBinaryOperation(BinaryOperation* expr, Token::Value op, | 572 bool MatchDoubleBinaryOperation(BinaryOperation* expr, Token::Value op, |
573 double val) { | 573 double val) { |
574 DCHECK(expr->right() != NULL); | 574 DCHECK(expr->right() != nullptr); |
575 if (expr->op() == op && expr->right()->IsLiteral() && | 575 if (expr->op() == op && expr->right()->IsLiteral() && |
576 TypeOf(expr) == kAstF64) { | 576 TypeOf(expr) == kAstF64) { |
577 Literal* right = expr->right()->AsLiteral(); | 577 Literal* right = expr->right()->AsLiteral(); |
578 DCHECK(right->raw_value()->IsNumber()); | 578 DCHECK(right->raw_value()->IsNumber()); |
579 if (right->raw_value()->AsNumber() == val) { | 579 if (right->raw_value()->AsNumber() == val) { |
580 return true; | 580 return true; |
581 } | 581 } |
582 } | 582 } |
583 return false; | 583 return false; |
584 } | 584 } |
(...skipping 18 matching lines...) Expand all Loading... |
603 } else { | 603 } else { |
604 return kNone; | 604 return kNone; |
605 } | 605 } |
606 } | 606 } |
607 | 607 |
608 ConvertOperation MatchXor(BinaryOperation* expr) { | 608 ConvertOperation MatchXor(BinaryOperation* expr) { |
609 if (MatchIntBinaryOperation(expr, Token::BIT_XOR, 0xffffffff)) { | 609 if (MatchIntBinaryOperation(expr, Token::BIT_XOR, 0xffffffff)) { |
610 DCHECK(TypeOf(expr->left()) == kAstI32); | 610 DCHECK(TypeOf(expr->left()) == kAstI32); |
611 DCHECK(TypeOf(expr->right()) == kAstI32); | 611 DCHECK(TypeOf(expr->right()) == kAstI32); |
612 BinaryOperation* op = expr->left()->AsBinaryOperation(); | 612 BinaryOperation* op = expr->left()->AsBinaryOperation(); |
613 if (op != NULL) { | 613 if (op != nullptr) { |
614 if (MatchIntBinaryOperation(op, Token::BIT_XOR, 0xffffffff)) { | 614 if (MatchIntBinaryOperation(op, Token::BIT_XOR, 0xffffffff)) { |
615 DCHECK(TypeOf(op->right()) == kAstI32); | 615 DCHECK(TypeOf(op->right()) == kAstI32); |
616 if (TypeOf(op->left()) != kAstI32) { | 616 if (TypeOf(op->left()) != kAstI32) { |
617 return kToInt; | 617 return kToInt; |
618 } else { | 618 } else { |
619 return kAsIs; | 619 return kAsIs; |
620 } | 620 } |
621 } | 621 } |
622 } | 622 } |
623 } | 623 } |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
892 void VisitRewritableAssignmentExpression( | 892 void VisitRewritableAssignmentExpression( |
893 RewritableAssignmentExpression* expr) { | 893 RewritableAssignmentExpression* expr) { |
894 UNREACHABLE(); | 894 UNREACHABLE(); |
895 } | 895 } |
896 | 896 |
897 struct IndexContainer : public ZoneObject { | 897 struct IndexContainer : public ZoneObject { |
898 uint16_t index; | 898 uint16_t index; |
899 }; | 899 }; |
900 | 900 |
901 uint16_t LookupOrInsertLocal(Variable* v, LocalType type) { | 901 uint16_t LookupOrInsertLocal(Variable* v, LocalType type) { |
902 DCHECK(current_function_builder_ != NULL); | 902 DCHECK(current_function_builder_ != nullptr); |
903 ZoneHashMap::Entry* entry = | 903 ZoneHashMap::Entry* entry = |
904 local_variables_.Lookup(v, ComputePointerHash(v)); | 904 local_variables_.Lookup(v, ComputePointerHash(v)); |
905 if (entry == NULL) { | 905 if (entry == nullptr) { |
906 uint16_t index; | 906 uint16_t index; |
907 if (v->IsParameter()) { | 907 if (v->IsParameter()) { |
908 index = current_function_builder_->AddParam(type); | 908 index = current_function_builder_->AddParam(type); |
909 } else { | 909 } else { |
910 index = current_function_builder_->AddLocal(type); | 910 index = current_function_builder_->AddLocal(type); |
911 } | 911 } |
912 IndexContainer* container = new (zone()) IndexContainer(); | 912 IndexContainer* container = new (zone()) IndexContainer(); |
913 container->index = index; | 913 container->index = index; |
914 entry = local_variables_.LookupOrInsert(v, ComputePointerHash(v), | 914 entry = local_variables_.LookupOrInsert(v, ComputePointerHash(v), |
915 ZoneAllocationPolicy(zone())); | 915 ZoneAllocationPolicy(zone())); |
916 entry->value = container; | 916 entry->value = container; |
917 } | 917 } |
918 return (reinterpret_cast<IndexContainer*>(entry->value))->index; | 918 return (reinterpret_cast<IndexContainer*>(entry->value))->index; |
919 } | 919 } |
920 | 920 |
921 uint16_t LookupOrInsertGlobal(Variable* v, LocalType type) { | 921 uint16_t LookupOrInsertGlobal(Variable* v, LocalType type) { |
922 ZoneHashMap::Entry* entry = | 922 ZoneHashMap::Entry* entry = |
923 global_variables_.Lookup(v, ComputePointerHash(v)); | 923 global_variables_.Lookup(v, ComputePointerHash(v)); |
924 if (entry == NULL) { | 924 if (entry == nullptr) { |
925 uint16_t index = | 925 uint16_t index = |
926 builder_->AddGlobal(WasmOpcodes::MachineTypeFor(type), 0); | 926 builder_->AddGlobal(WasmOpcodes::MachineTypeFor(type), 0); |
927 IndexContainer* container = new (zone()) IndexContainer(); | 927 IndexContainer* container = new (zone()) IndexContainer(); |
928 container->index = index; | 928 container->index = index; |
929 entry = global_variables_.LookupOrInsert(v, ComputePointerHash(v), | 929 entry = global_variables_.LookupOrInsert(v, ComputePointerHash(v), |
930 ZoneAllocationPolicy(zone())); | 930 ZoneAllocationPolicy(zone())); |
931 entry->value = container; | 931 entry->value = container; |
932 } | 932 } |
933 return (reinterpret_cast<IndexContainer*>(entry->value))->index; | 933 return (reinterpret_cast<IndexContainer*>(entry->value))->index; |
934 } | 934 } |
935 | 935 |
936 uint16_t LookupOrInsertFunction(Variable* v) { | 936 uint16_t LookupOrInsertFunction(Variable* v) { |
937 DCHECK(builder_ != NULL); | 937 DCHECK(builder_ != nullptr); |
938 ZoneHashMap::Entry* entry = functions_.Lookup(v, ComputePointerHash(v)); | 938 ZoneHashMap::Entry* entry = functions_.Lookup(v, ComputePointerHash(v)); |
939 if (entry == NULL) { | 939 if (entry == nullptr) { |
940 uint16_t index = builder_->AddFunction(v->raw_name()->raw_data(), | 940 uint16_t index = builder_->AddFunction(v->raw_name()->raw_data(), |
941 v->raw_name()->length()); | 941 v->raw_name()->length()); |
942 IndexContainer* container = new (zone()) IndexContainer(); | 942 IndexContainer* container = new (zone()) IndexContainer(); |
943 container->index = index; | 943 container->index = index; |
944 entry = functions_.LookupOrInsert(v, ComputePointerHash(v), | 944 entry = functions_.LookupOrInsert(v, ComputePointerHash(v), |
945 ZoneAllocationPolicy(zone())); | 945 ZoneAllocationPolicy(zone())); |
946 entry->value = container; | 946 entry->value = container; |
947 } | 947 } |
948 return (reinterpret_cast<IndexContainer*>(entry->value))->index; | 948 return (reinterpret_cast<IndexContainer*>(entry->value))->index; |
949 } | 949 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
998 // that zone in constructor may be thrown away once wasm module is written. | 998 // that zone in constructor may be thrown away once wasm module is written. |
999 WasmModuleIndex* AsmWasmBuilder::Run() { | 999 WasmModuleIndex* AsmWasmBuilder::Run() { |
1000 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); | 1000 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); |
1001 impl.Compile(); | 1001 impl.Compile(); |
1002 WasmModuleWriter* writer = impl.builder_->Build(zone_); | 1002 WasmModuleWriter* writer = impl.builder_->Build(zone_); |
1003 return writer->WriteTo(zone_); | 1003 return writer->WriteTo(zone_); |
1004 } | 1004 } |
1005 } // namespace wasm | 1005 } // namespace wasm |
1006 } // namespace internal | 1006 } // namespace internal |
1007 } // namespace v8 | 1007 } // namespace v8 |
OLD | NEW |