Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Side by Side Diff: src/wasm/asm-wasm-builder.cc

Issue 1583603002: Add __init__ function to all modules created in asm-to-wasm (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@refactor
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/wasm/encoder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 29 matching lines...) Expand all
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_(nullptr), 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),
51 init_function_index(0) { 50 init_function_index(0) {
52 InitializeAstVisitor(isolate); 51 InitializeAstVisitor(isolate);
53 } 52 }
54 53
55 void Compile() { RECURSE(VisitFunctionLiteral(literal_)); } 54 void InitializeInitFunction() {
55 unsigned char init[] = "__init__";
56 init_function_index = builder_->AddFunction();
57 current_function_builder_ = builder_->FunctionAt(init_function_index);
58 current_function_builder_->SetName(init, 8);
59 current_function_builder_->ReturnType(kAstStmt);
60 current_function_builder_->Exported(1);
61 current_function_builder_ = nullptr;
62 }
63
64 void Compile() {
65 InitializeInitFunction();
66 RECURSE(VisitFunctionLiteral(literal_));
67 }
56 68
57 void VisitVariableDeclaration(VariableDeclaration* decl) {} 69 void VisitVariableDeclaration(VariableDeclaration* decl) {}
58 70
59 void VisitFunctionDeclaration(FunctionDeclaration* decl) { 71 void VisitFunctionDeclaration(FunctionDeclaration* decl) {
60 DCHECK(!in_function_); 72 DCHECK(!in_function_);
61 DCHECK(current_function_builder_ == nullptr); 73 DCHECK(current_function_builder_ == nullptr);
62 uint16_t index = LookupOrInsertFunction(decl->proxy()->var()); 74 uint16_t index = LookupOrInsertFunction(decl->proxy()->var());
63 current_function_builder_ = builder_->FunctionAt(index); 75 current_function_builder_ = builder_->FunctionAt(index);
64 in_function_ = true; 76 in_function_ = true;
65 RECURSE(Visit(decl->fun())); 77 RECURSE(Visit(decl->fun()));
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 } 385 }
374 } 386 }
375 LocalType var_type = TypeOf(expr); 387 LocalType var_type = TypeOf(expr);
376 DCHECK(var_type != kAstStmt); 388 DCHECK(var_type != kAstStmt);
377 if (var->IsContextSlot()) { 389 if (var->IsContextSlot()) {
378 AddLeb128(LookupOrInsertGlobal(var, var_type), false); 390 AddLeb128(LookupOrInsertGlobal(var, var_type), false);
379 } else { 391 } else {
380 AddLeb128(LookupOrInsertLocal(var, var_type), true); 392 AddLeb128(LookupOrInsertLocal(var, var_type), true);
381 } 393 }
382 } 394 }
383 } else if (marking_exported) {
384 Variable* var = expr->var();
385 if (var->is_function()) {
386 uint16_t index = LookupOrInsertFunction(var);
387 builder_->FunctionAt(index)->Exported(1);
388 }
389 } 395 }
390 } 396 }
391 397
392 void VisitLiteral(Literal* expr) { 398 void VisitLiteral(Literal* expr) {
393 if (in_function_) { 399 if (in_function_) {
394 if (expr->raw_value()->IsNumber()) { 400 if (expr->raw_value()->IsNumber()) {
395 LocalType type = TypeOf(expr); 401 LocalType type = TypeOf(expr);
396 switch (type) { 402 switch (type) {
397 case kAstI32: { 403 case kAstI32: {
398 int val = static_cast<int>(expr->raw_value()->AsNumber()); 404 int val = static_cast<int>(expr->raw_value()->AsNumber());
(...skipping 19 matching lines...) Expand all
418 } 424 }
419 } 425 }
420 } 426 }
421 427
422 void VisitRegExpLiteral(RegExpLiteral* expr) { UNREACHABLE(); } 428 void VisitRegExpLiteral(RegExpLiteral* expr) { UNREACHABLE(); }
423 429
424 void VisitObjectLiteral(ObjectLiteral* expr) { 430 void VisitObjectLiteral(ObjectLiteral* expr) {
425 ZoneList<ObjectLiteralProperty*>* props = expr->properties(); 431 ZoneList<ObjectLiteralProperty*>* props = expr->properties();
426 for (int i = 0; i < props->length(); ++i) { 432 for (int i = 0; i < props->length(); ++i) {
427 ObjectLiteralProperty* prop = props->at(i); 433 ObjectLiteralProperty* prop = props->at(i);
428 RECURSE(Visit(prop->value())); 434 DCHECK(marking_exported);
435 VariableProxy* expr = prop->value()->AsVariableProxy();
436 DCHECK(expr != nullptr);
437 Variable* var = expr->var();
438 Literal* name = prop->key()->AsLiteral();
439 DCHECK(name != nullptr);
440 DCHECK(name->IsPropertyName());
441 const AstRawString* raw_name = name->AsRawPropertyName();
442 if (var->is_function()) {
443 uint16_t index = LookupOrInsertFunction(var);
444 builder_->FunctionAt(index)->Exported(1);
445 builder_->FunctionAt(index)
446 ->SetName(raw_name->raw_data(), raw_name->length());
447 }
429 } 448 }
430 } 449 }
431 450
432 void VisitArrayLiteral(ArrayLiteral* expr) { UNREACHABLE(); } 451 void VisitArrayLiteral(ArrayLiteral* expr) { UNREACHABLE(); }
433 452
434 void LoadInitFunction() { 453 void LoadInitFunction() {
435 if (!init_function_initialized) { 454 current_function_builder_ = builder_->FunctionAt(init_function_index);
436 init_function_initialized = true; 455 in_function_ = true;
437 unsigned char init[] = "__init__";
438 init_function_index = builder_->AddFunction(init, 8);
439 current_function_builder_ = builder_->FunctionAt(init_function_index);
440 current_function_builder_->ReturnType(kAstStmt);
441 current_function_builder_->Exported(1);
442 in_function_ = true;
443 } else {
444 current_function_builder_ = builder_->FunctionAt(init_function_index);
445 in_function_ = true;
446 }
447 } 456 }
448 457
449 void UnLoadInitFunction() { 458 void UnLoadInitFunction() {
450 in_function_ = false; 459 in_function_ = false;
451 current_function_builder_ = nullptr; 460 current_function_builder_ = nullptr;
452 } 461 }
453 462
454 void VisitAssignment(Assignment* expr) { 463 void VisitAssignment(Assignment* expr) {
455 bool in_init = false; 464 bool in_init = false;
456 if (!in_function_) { 465 if (!in_function_) {
457 // TODO(bradnelson): Get rid of this. 466 // TODO(bradnelson): Get rid of this.
458 if (TypeOf(expr->value()) == kAstStmt) { 467 if (TypeOf(expr->value()) == kAstStmt) {
459 return; 468 return;
460 } 469 }
461 in_init = true; 470 in_init = true;
462 LoadInitFunction(); 471 LoadInitFunction();
463 } 472 }
464 BinaryOperation* value_op = expr->value()->AsBinaryOperation(); 473 BinaryOperation* value_op = expr->value()->AsBinaryOperation();
465 if (value_op != nullptr && MatchBinaryOperation(value_op) == kAsIs) { 474 if (value_op != nullptr && MatchBinaryOperation(value_op) == kAsIs) {
466 VariableProxy* target_var = expr->target()->AsVariableProxy(); 475 VariableProxy* target_var = expr->target()->AsVariableProxy();
467 VariableProxy* effective_value_var = GetLeft(value_op)->AsVariableProxy(); 476 VariableProxy* effective_value_var = GetLeft(value_op)->AsVariableProxy();
468 // TODO(aseemgarg): simplify block_size_ or replace with a kNop
469 if (target_var != nullptr && effective_value_var != nullptr && 477 if (target_var != nullptr && effective_value_var != nullptr &&
470 target_var->var() == effective_value_var->var()) { 478 target_var->var() == effective_value_var->var()) {
471 block_size_--; 479 block_size_--;
472 return; 480 return;
473 } 481 }
474 } 482 }
475 is_set_op_ = true; 483 is_set_op_ = true;
476 RECURSE(Visit(expr->target())); 484 RECURSE(Visit(expr->target()));
477 DCHECK(!is_set_op_); 485 DCHECK(!is_set_op_);
478 RECURSE(Visit(expr->value())); 486 RECURSE(Visit(expr->value()));
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 ZoneAllocationPolicy(zone())); 970 ZoneAllocationPolicy(zone()));
963 entry->value = container; 971 entry->value = container;
964 } 972 }
965 return (reinterpret_cast<IndexContainer*>(entry->value))->index; 973 return (reinterpret_cast<IndexContainer*>(entry->value))->index;
966 } 974 }
967 975
968 uint16_t LookupOrInsertFunction(Variable* v) { 976 uint16_t LookupOrInsertFunction(Variable* v) {
969 DCHECK(builder_ != nullptr); 977 DCHECK(builder_ != nullptr);
970 ZoneHashMap::Entry* entry = functions_.Lookup(v, ComputePointerHash(v)); 978 ZoneHashMap::Entry* entry = functions_.Lookup(v, ComputePointerHash(v));
971 if (entry == nullptr) { 979 if (entry == nullptr) {
972 uint16_t index = builder_->AddFunction(v->raw_name()->raw_data(), 980 uint16_t index = builder_->AddFunction();
973 v->raw_name()->length());
974 IndexContainer* container = new (zone()) IndexContainer(); 981 IndexContainer* container = new (zone()) IndexContainer();
975 container->index = index; 982 container->index = index;
976 entry = functions_.LookupOrInsert(v, ComputePointerHash(v), 983 entry = functions_.LookupOrInsert(v, ComputePointerHash(v),
977 ZoneAllocationPolicy(zone())); 984 ZoneAllocationPolicy(zone()));
978 entry->value = container; 985 entry->value = container;
979 } 986 }
980 return (reinterpret_cast<IndexContainer*>(entry->value))->index; 987 return (reinterpret_cast<IndexContainer*>(entry->value))->index;
981 } 988 }
982 989
983 LocalType TypeOf(Expression* expr) { 990 LocalType TypeOf(Expression* expr) {
(...skipping 22 matching lines...) Expand all
1006 bool is_set_op_; 1013 bool is_set_op_;
1007 bool marking_exported; 1014 bool marking_exported;
1008 WasmModuleBuilder* builder_; 1015 WasmModuleBuilder* builder_;
1009 WasmFunctionBuilder* current_function_builder_; 1016 WasmFunctionBuilder* current_function_builder_;
1010 FunctionLiteral* literal_; 1017 FunctionLiteral* literal_;
1011 Isolate* isolate_; 1018 Isolate* isolate_;
1012 Zone* zone_; 1019 Zone* zone_;
1013 TypeCache const& cache_; 1020 TypeCache const& cache_;
1014 ZoneVector<std::pair<BreakableStatement*, bool>> breakable_blocks_; 1021 ZoneVector<std::pair<BreakableStatement*, bool>> breakable_blocks_;
1015 int block_size_; 1022 int block_size_;
1016 bool init_function_initialized;
1017 uint16_t init_function_index; 1023 uint16_t init_function_index;
1018 1024
1019 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 1025 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
1020 1026
1021 private: 1027 private:
1022 DISALLOW_COPY_AND_ASSIGN(AsmWasmBuilderImpl); 1028 DISALLOW_COPY_AND_ASSIGN(AsmWasmBuilderImpl);
1023 }; 1029 };
1024 1030
1025 AsmWasmBuilder::AsmWasmBuilder(Isolate* isolate, Zone* zone, 1031 AsmWasmBuilder::AsmWasmBuilder(Isolate* isolate, Zone* zone,
1026 FunctionLiteral* literal) 1032 FunctionLiteral* literal)
1027 : isolate_(isolate), zone_(zone), literal_(literal) {} 1033 : isolate_(isolate), zone_(zone), literal_(literal) {}
1028 1034
1029 // TODO(aseemgarg): probably should take zone (to write wasm to) as input so 1035 // TODO(aseemgarg): probably should take zone (to write wasm to) as input so
1030 // that zone in constructor may be thrown away once wasm module is written. 1036 // that zone in constructor may be thrown away once wasm module is written.
1031 WasmModuleIndex* AsmWasmBuilder::Run() { 1037 WasmModuleIndex* AsmWasmBuilder::Run() {
1032 AsmWasmBuilderImpl impl(isolate_, zone_, literal_); 1038 AsmWasmBuilderImpl impl(isolate_, zone_, literal_);
1033 impl.Compile(); 1039 impl.Compile();
1034 WasmModuleWriter* writer = impl.builder_->Build(zone_); 1040 WasmModuleWriter* writer = impl.builder_->Build(zone_);
1035 return writer->WriteTo(zone_); 1041 return writer->WriteTo(zone_);
1036 } 1042 }
1037 } // namespace wasm 1043 } // namespace wasm
1038 } // namespace internal 1044 } // namespace internal
1039 } // namespace v8 1045 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | src/wasm/encoder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698