Chromium Code Reviews| 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/signature.h" | 5 #include "src/signature.h" |
| 6 | 6 |
| 7 #include "src/handles.h" | 7 #include "src/handles.h" |
| 8 #include "src/v8.h" | 8 #include "src/v8.h" |
| 9 #include "src/zone-containers.h" | 9 #include "src/zone-containers.h" |
| 10 | 10 |
| 11 #include "src/wasm/ast-decoder.h" | 11 #include "src/wasm/ast-decoder.h" |
| 12 #include "src/wasm/encoder.h" | 12 #include "src/wasm/encoder.h" |
| 13 #include "src/wasm/wasm-macro-gen.h" | 13 #include "src/wasm/wasm-macro-gen.h" |
| 14 #include "src/wasm/wasm-module.h" | 14 #include "src/wasm/wasm-module.h" |
| 15 #include "src/wasm/wasm-opcodes.h" | 15 #include "src/wasm/wasm-opcodes.h" |
| 16 | 16 |
| 17 #include "src/v8memory.h" | 17 #include "src/v8memory.h" |
| 18 | 18 |
| 19 #if DEBUG | |
| 20 #define TRACE(...) \ | |
| 21 do { \ | |
| 22 if (FLAG_trace_wasm_decoder) PrintF(__VA_ARGS__); \ | |
| 23 } while (false) | |
| 24 #else | |
| 25 #define TRACE(...) | |
| 26 #endif | |
| 27 | |
| 19 namespace v8 { | 28 namespace v8 { |
| 20 namespace internal { | 29 namespace internal { |
| 21 namespace wasm { | 30 namespace wasm { |
| 22 | 31 |
| 23 /*TODO: add error cases for adding too many locals, too many functions and bad | 32 /*TODO: add error cases for adding too many locals, too many functions and bad |
| 24 indices in body */ | 33 indices in body */ |
| 25 | 34 |
| 26 namespace { | 35 namespace { |
| 27 void EmitUint8(byte** b, uint8_t x) { | 36 void EmitUint8(byte** b, uint8_t x) { |
| 28 Memory::uint8_at(*b) = x; | 37 Memory::uint8_at(*b) = x; |
| 29 *b += 1; | 38 *b += 1; |
| 30 } | 39 } |
| 31 | 40 |
| 32 | 41 |
| 33 void EmitUint16(byte** b, uint16_t x) { | 42 void EmitUint16(byte** b, uint16_t x) { |
| 34 WriteUnalignedUInt16(*b, x); | 43 WriteUnalignedUInt16(*b, x); |
| 35 *b += 2; | 44 *b += 2; |
| 36 } | 45 } |
| 37 | 46 |
| 38 | 47 |
| 39 void EmitUint32(byte** b, uint32_t x) { | 48 void EmitUint32(byte** b, uint32_t x) { |
| 40 WriteUnalignedUInt32(*b, x); | 49 WriteUnalignedUInt32(*b, x); |
| 41 *b += 4; | 50 *b += 4; |
| 42 } | 51 } |
| 43 | 52 |
| 53 // Sections all start with a size, but it's unknown at the start. | |
| 54 // We generate a large varint which we then fixup later when the size is known. | |
| 55 // TODO This isn't strictly necessary since sizes are calculated ahead of time. | |
| 56 const size_t padded_varint = 4; | |
|
binji
2016/03/10 02:20:34
should be 5 right?
JF
2016/03/10 03:20:08
Done.
| |
| 44 | 57 |
| 45 void EmitVarInt(byte** b, size_t val) { | 58 void EmitVarInt(byte** b, size_t val) { |
| 46 while (true) { | 59 while (true) { |
| 47 size_t next = val >> 7; | 60 size_t next = val >> 7; |
| 48 byte out = static_cast<byte>(val & 0x7f); | 61 byte out = static_cast<byte>(val & 0x7f); |
| 49 if (next) { | 62 if (next) { |
| 50 *((*b)++) = 0x80 | out; | 63 *((*b)++) = 0x80 | out; |
| 51 val = next; | 64 val = next; |
| 52 } else { | 65 } else { |
| 53 *((*b)++) = out; | 66 *((*b)++) = out; |
| 54 break; | 67 break; |
| 55 } | 68 } |
| 56 } | 69 } |
| 57 } | 70 } |
| 58 | 71 |
| 59 size_t SizeOfVarInt(size_t value) { | 72 size_t SizeOfVarInt(size_t value) { |
| 60 size_t size = 0; | 73 size_t size = 0; |
| 61 do { | 74 do { |
| 62 size++; | 75 size++; |
| 63 value = value >> 7; | 76 value = value >> 7; |
| 64 } while (value > 0); | 77 } while (value > 0); |
| 65 return size; | 78 return size; |
| 66 } | 79 } |
| 67 | 80 |
| 81 void FixupSection(byte* start, byte* end) { | |
| 82 // Same as EmitVarInt, but fixed-width with zeroes in the MSBs. | |
| 83 size_t val = end - start - padded_varint; | |
| 84 TRACE(" fixup %u\n", (unsigned)val); | |
| 85 for (size_t pos = 0; pos != padded_varint; ++pos) { | |
| 86 size_t next = val >> 7; | |
| 87 byte out = static_cast<byte>(val & 0x7f); | |
| 88 if (pos != padded_varint - 1) { | |
| 89 *(start++) = 0x80 | out; | |
| 90 val = next; | |
| 91 } else { | |
| 92 *(start++) = out; | |
| 93 // TODO check that the pre-allocated fixup size isn't overflowed. | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 // Returns the start of the section, where the section VarInt size is. | |
| 99 byte* EmitSection(WasmSection::Code code, byte** b) { | |
| 100 byte* start = *b; | |
| 101 const char* name = WasmSection::getName(code); | |
| 102 size_t length = WasmSection::getNameLength(code); | |
| 103 TRACE("emit section: %s\n", name); | |
| 104 for (size_t padding = 0; padding != padded_varint; ++padding) { | |
| 105 EmitUint8(b, 0xff); // Will get fixed up later. | |
| 106 } | |
| 107 EmitVarInt(b, length); // Section name string size. | |
| 108 for (size_t i = 0; i != length; ++i) EmitUint8(b, name[i]); | |
| 109 return start; | |
| 110 } | |
| 68 } // namespace | 111 } // namespace |
| 69 | 112 |
| 70 | |
| 71 struct WasmFunctionBuilder::Type { | 113 struct WasmFunctionBuilder::Type { |
| 72 bool param_; | 114 bool param_; |
| 73 LocalType type_; | 115 LocalType type_; |
| 74 }; | 116 }; |
| 75 | 117 |
| 76 | 118 |
| 77 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) | 119 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) |
| 78 : return_type_(kAstI32), | 120 : return_type_(kAstI32), |
| 79 locals_(zone), | 121 locals_(zone), |
| 80 exported_(0), | 122 exported_(0), |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 491 size_t header_size; | 533 size_t header_size; |
| 492 size_t body_size; | 534 size_t body_size; |
| 493 | 535 |
| 494 size_t total() { return header_size + body_size; } | 536 size_t total() { return header_size + body_size; } |
| 495 | 537 |
| 496 void Add(size_t header, size_t body) { | 538 void Add(size_t header, size_t body) { |
| 497 header_size += header; | 539 header_size += header; |
| 498 body_size += body; | 540 body_size += body; |
| 499 } | 541 } |
| 500 | 542 |
| 501 void AddSection(size_t size) { | 543 void AddSection(WasmSection::Code code, size_t other_size) { |
| 502 if (size > 0) { | 544 Add(padded_varint + SizeOfVarInt(WasmSection::getNameLength(code)) + |
| 503 Add(1, 0); | 545 WasmSection::getNameLength(code), |
| 504 Add(SizeOfVarInt(size), 0); | 546 0); |
| 505 } | 547 if (other_size) Add(SizeOfVarInt(other_size), 0); |
| 506 } | 548 } |
| 507 }; | 549 }; |
| 508 | 550 |
| 509 | |
| 510 WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const { | 551 WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const { |
| 511 Sizes sizes = {0, 0}; | 552 Sizes sizes = {0, 0}; |
| 512 | 553 |
| 513 sizes.Add(2 * sizeof(uint32_t), 0); // header | 554 sizes.Add(2 * sizeof(uint32_t), 0); // header |
| 514 | 555 |
| 515 sizes.Add(1, 0); | 556 sizes.AddSection(WasmSection::Code::Memory, 0); |
| 516 sizes.Add(kDeclMemorySize, 0); | 557 sizes.Add(kDeclMemorySize, 0); |
| 558 TRACE("Size after memory: %u, %u\n", (unsigned)sizes.header_size, | |
| 559 (unsigned)sizes.body_size); | |
| 517 | 560 |
| 518 sizes.AddSection(signatures_.size()); | 561 if (globals_.size() > 0) { |
| 519 for (auto sig : signatures_) { | 562 sizes.AddSection(WasmSection::Code::Globals, globals_.size()); |
| 520 sizes.Add(1 + SizeOfVarInt(sig->parameter_count()) + sig->parameter_count(), | 563 /* These globals never have names, so are always 3 bytes. */ |
| 521 0); | 564 sizes.Add(3 * globals_.size(), 0); |
| 565 TRACE("Size after globals: %u, %u\n", (unsigned)sizes.header_size, | |
| 566 (unsigned)sizes.body_size); | |
| 522 } | 567 } |
| 523 | 568 |
| 524 sizes.AddSection(globals_.size()); | 569 if (signatures_.size() > 0) { |
| 525 if (globals_.size() > 0) { | 570 sizes.AddSection(WasmSection::Code::Signatures, signatures_.size()); |
| 526 /* These globals never have names, so are always 3 bytes. */ | 571 for (auto sig : signatures_) { |
| 527 sizes.Add(3 * globals_.size(), 0); | 572 sizes.Add( |
| 573 1 + SizeOfVarInt(sig->parameter_count()) + sig->parameter_count(), 0); | |
| 574 } | |
| 575 TRACE("Size after signatures: %u, %u\n", (unsigned)sizes.header_size, | |
| 576 (unsigned)sizes.body_size); | |
| 528 } | 577 } |
| 529 | 578 |
| 530 sizes.AddSection(functions_.size()); | 579 if (functions_.size() > 0) { |
| 531 for (auto function : functions_) { | 580 sizes.AddSection(WasmSection::Code::Functions, functions_.size()); |
| 532 sizes.Add(function->HeaderSize() + function->BodySize(), | 581 for (auto function : functions_) { |
| 533 function->NameSize()); | 582 sizes.Add(function->HeaderSize() + function->BodySize(), |
| 583 function->NameSize()); | |
| 584 } | |
| 585 TRACE("Size after functions: %u, %u\n", (unsigned)sizes.header_size, | |
| 586 (unsigned)sizes.body_size); | |
| 534 } | 587 } |
| 535 | 588 |
| 536 if (start_function_index_ >= 0) { | 589 if (start_function_index_ >= 0) { |
| 537 sizes.Add(1, 0); | 590 sizes.AddSection(WasmSection::Code::StartFunction, 0); |
| 538 sizes.Add(SizeOfVarInt(start_function_index_), 0); | 591 sizes.Add(SizeOfVarInt(start_function_index_), 0); |
| 592 TRACE("Size after start: %u, %u\n", (unsigned)sizes.header_size, | |
| 593 (unsigned)sizes.body_size); | |
| 539 } | 594 } |
| 540 | 595 |
| 541 sizes.AddSection(data_segments_.size()); | 596 if (data_segments_.size() > 0) { |
| 542 for (auto segment : data_segments_) { | 597 sizes.AddSection(WasmSection::Code::DataSegments, data_segments_.size()); |
| 543 sizes.Add(segment->HeaderSize(), segment->BodySize()); | 598 for (auto segment : data_segments_) { |
| 599 sizes.Add(segment->HeaderSize(), segment->BodySize()); | |
| 600 } | |
| 601 TRACE("Size after data segments: %u, %u\n", (unsigned)sizes.header_size, | |
| 602 (unsigned)sizes.body_size); | |
| 544 } | 603 } |
| 545 | 604 |
| 546 sizes.AddSection(indirect_functions_.size()); | 605 if (indirect_functions_.size() > 0) { |
| 547 for (auto function_index : indirect_functions_) { | 606 sizes.AddSection(WasmSection::Code::FunctionTable, |
| 548 sizes.Add(SizeOfVarInt(function_index), 0); | 607 indirect_functions_.size()); |
| 608 for (auto function_index : indirect_functions_) { | |
| 609 sizes.Add(SizeOfVarInt(function_index), 0); | |
| 610 } | |
| 611 TRACE("Size after indirect functions: %u, %u\n", | |
| 612 (unsigned)sizes.header_size, (unsigned)sizes.body_size); | |
| 549 } | 613 } |
| 550 | 614 |
| 551 if (sizes.body_size > 0) sizes.Add(1, 0); | 615 if (sizes.body_size > 0) { |
| 616 sizes.AddSection(WasmSection::Code::End, 0); | |
| 617 TRACE("Size after end: %u, %u\n", (unsigned)sizes.header_size, | |
| 618 (unsigned)sizes.body_size); | |
| 619 } | |
| 552 | 620 |
| 553 ZoneVector<uint8_t> buffer_vector(sizes.total(), zone); | 621 ZoneVector<uint8_t> buffer_vector(sizes.total(), zone); |
| 554 byte* buffer = &buffer_vector[0]; | 622 byte* buffer = &buffer_vector[0]; |
| 555 byte* header = buffer; | 623 byte* header = buffer; |
| 556 byte* body = buffer + sizes.header_size; | 624 byte* body = buffer + sizes.header_size; |
| 557 | 625 |
| 558 // -- emit magic ------------------------------------------------------------- | 626 // -- emit magic ------------------------------------------------------------- |
| 627 TRACE("emit magic\n"); | |
| 559 EmitUint32(&header, kWasmMagic); | 628 EmitUint32(&header, kWasmMagic); |
| 560 EmitUint32(&header, kWasmVersion); | 629 EmitUint32(&header, kWasmVersion); |
| 561 | 630 |
| 562 // -- emit memory declaration ------------------------------------------------ | 631 // -- emit memory declaration ------------------------------------------------ |
| 563 EmitUint8(&header, kDeclMemory); | 632 { |
| 564 EmitVarInt(&header, 16); // min memory size | 633 byte* section = EmitSection(WasmSection::Code::Memory, &header); |
| 565 EmitVarInt(&header, 16); // max memory size | 634 EmitVarInt(&header, 16); // min memory size |
| 566 EmitUint8(&header, 0); // memory export | 635 EmitVarInt(&header, 16); // max memory size |
| 636 EmitUint8(&header, 0); // memory export | |
| 637 static_assert(kDeclMemorySize == 3, "memory size must match emit above"); | |
| 638 FixupSection(section, header); | |
| 639 } | |
| 567 | 640 |
| 568 // -- emit globals ----------------------------------------------------------- | 641 // -- emit globals ----------------------------------------------------------- |
| 569 if (globals_.size() > 0) { | 642 if (globals_.size() > 0) { |
| 570 EmitUint8(&header, kDeclGlobals); | 643 byte* section = EmitSection(WasmSection::Code::Globals, &header); |
| 571 EmitVarInt(&header, globals_.size()); | 644 EmitVarInt(&header, globals_.size()); |
| 572 | 645 |
| 573 for (auto global : globals_) { | 646 for (auto global : globals_) { |
| 574 EmitVarInt(&header, 0); // Length of the global name. | 647 EmitVarInt(&header, 0); // Length of the global name. |
| 575 EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first)); | 648 EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first)); |
| 576 EmitUint8(&header, global.second); | 649 EmitUint8(&header, global.second); |
| 577 } | 650 } |
| 651 FixupSection(section, header); | |
| 578 } | 652 } |
| 579 | 653 |
| 580 // -- emit signatures -------------------------------------------------------- | 654 // -- emit signatures -------------------------------------------------------- |
| 581 if (signatures_.size() > 0) { | 655 if (signatures_.size() > 0) { |
| 582 EmitUint8(&header, kDeclSignatures); | 656 byte* section = EmitSection(WasmSection::Code::Signatures, &header); |
| 583 EmitVarInt(&header, signatures_.size()); | 657 EmitVarInt(&header, signatures_.size()); |
| 584 | 658 |
| 585 for (FunctionSig* sig : signatures_) { | 659 for (FunctionSig* sig : signatures_) { |
| 586 EmitVarInt(&header, sig->parameter_count()); | 660 EmitVarInt(&header, sig->parameter_count()); |
| 587 if (sig->return_count() > 0) { | 661 if (sig->return_count() > 0) { |
| 588 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetReturn())); | 662 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetReturn())); |
| 589 } else { | 663 } else { |
| 590 EmitUint8(&header, kLocalVoid); | 664 EmitUint8(&header, kLocalVoid); |
| 591 } | 665 } |
| 592 for (size_t j = 0; j < sig->parameter_count(); j++) { | 666 for (size_t j = 0; j < sig->parameter_count(); j++) { |
| 593 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j))); | 667 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j))); |
| 594 } | 668 } |
| 595 } | 669 } |
| 670 FixupSection(section, header); | |
| 596 } | 671 } |
| 597 | 672 |
| 598 // -- emit functions --------------------------------------------------------- | 673 // -- emit functions --------------------------------------------------------- |
| 599 if (functions_.size() > 0) { | 674 if (functions_.size() > 0) { |
| 600 EmitUint8(&header, kDeclFunctions); | 675 byte* section = EmitSection(WasmSection::Code::Functions, &header); |
| 601 EmitVarInt(&header, functions_.size()); | 676 EmitVarInt(&header, functions_.size()); |
| 602 | 677 |
| 603 for (auto func : functions_) { | 678 for (auto func : functions_) { |
| 604 func->Serialize(buffer, &header, &body); | 679 func->Serialize(buffer, &header, &body); |
| 605 } | 680 } |
| 681 FixupSection(section, header); | |
| 606 } | 682 } |
| 607 | 683 |
| 608 // -- emit start function index ---------------------------------------------- | 684 // -- emit start function index ---------------------------------------------- |
| 609 if (start_function_index_ >= 0) { | 685 if (start_function_index_ >= 0) { |
| 610 EmitUint8(&header, kDeclStartFunction); | 686 byte* section = EmitSection(WasmSection::Code::StartFunction, &header); |
| 611 EmitVarInt(&header, start_function_index_); | 687 EmitVarInt(&header, start_function_index_); |
| 688 FixupSection(section, header); | |
| 612 } | 689 } |
| 613 | 690 |
| 614 // -- emit data segments ----------------------------------------------------- | 691 // -- emit data segments ----------------------------------------------------- |
| 615 if (data_segments_.size() > 0) { | 692 if (data_segments_.size() > 0) { |
| 616 EmitUint8(&header, kDeclDataSegments); | 693 byte* section = EmitSection(WasmSection::Code::DataSegments, &header); |
| 617 EmitVarInt(&header, data_segments_.size()); | 694 EmitVarInt(&header, data_segments_.size()); |
| 618 | 695 |
| 619 for (auto segment : data_segments_) { | 696 for (auto segment : data_segments_) { |
| 620 segment->Serialize(buffer, &header, &body); | 697 segment->Serialize(buffer, &header, &body); |
| 621 } | 698 } |
| 699 FixupSection(section, header); | |
| 622 } | 700 } |
| 623 | 701 |
| 624 // -- emit function table ---------------------------------------------------- | 702 // -- emit function table ---------------------------------------------------- |
| 625 if (indirect_functions_.size() > 0) { | 703 if (indirect_functions_.size() > 0) { |
| 626 EmitUint8(&header, kDeclFunctionTable); | 704 byte* section = EmitSection(WasmSection::Code::FunctionTable, &header); |
| 627 EmitVarInt(&header, indirect_functions_.size()); | 705 EmitVarInt(&header, indirect_functions_.size()); |
| 628 | 706 |
| 629 for (auto index : indirect_functions_) { | 707 for (auto index : indirect_functions_) { |
| 630 EmitVarInt(&header, index); | 708 EmitVarInt(&header, index); |
| 631 } | 709 } |
| 710 FixupSection(section, header); | |
| 632 } | 711 } |
| 633 | 712 |
| 634 if (sizes.body_size > 0) EmitUint8(&header, kDeclEnd); | 713 if (sizes.body_size > 0) { |
| 714 byte* section = EmitSection(WasmSection::Code::End, &header); | |
| 715 FixupSection(section, header); | |
| 716 } | |
| 635 | 717 |
| 636 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total()); | 718 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total()); |
| 637 } | 719 } |
| 638 | 720 |
| 639 | 721 |
| 640 std::vector<uint8_t> UnsignedLEB128From(uint32_t result) { | 722 std::vector<uint8_t> UnsignedLEB128From(uint32_t result) { |
| 641 std::vector<uint8_t> output; | 723 std::vector<uint8_t> output; |
| 642 uint8_t next = 0; | 724 uint8_t next = 0; |
| 643 int shift = 0; | 725 int shift = 0; |
| 644 do { | 726 do { |
| 645 next = static_cast<uint8_t>(result >> shift); | 727 next = static_cast<uint8_t>(result >> shift); |
| 646 if (((result >> shift) & 0xFFFFFF80) != 0) { | 728 if (((result >> shift) & 0xFFFFFF80) != 0) { |
| 647 next = next | 0x80; | 729 next = next | 0x80; |
| 648 } | 730 } |
| 649 output.push_back(next); | 731 output.push_back(next); |
| 650 shift += 7; | 732 shift += 7; |
| 651 } while ((next & 0x80) != 0); | 733 } while ((next & 0x80) != 0); |
| 652 return output; | 734 return output; |
| 653 } | 735 } |
| 654 } // namespace wasm | 736 } // namespace wasm |
| 655 } // namespace internal | 737 } // namespace internal |
| 656 } // namespace v8 | 738 } // namespace v8 |
| OLD | NEW |