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 |
(...skipping 24 matching lines...) Expand all Loading... |
35 *b += 2; | 35 *b += 2; |
36 } | 36 } |
37 | 37 |
38 | 38 |
39 void EmitUint32(byte** b, uint32_t x) { | 39 void EmitUint32(byte** b, uint32_t x) { |
40 WriteUnalignedUInt32(*b, x); | 40 WriteUnalignedUInt32(*b, x); |
41 *b += 4; | 41 *b += 4; |
42 } | 42 } |
43 | 43 |
44 | 44 |
| 45 // Sections all start with a size, but it's unknown at the start. |
| 46 // We generate a large varint which we then fixup later when the size is known. |
| 47 const size_t padded_varint = 4; |
| 48 |
| 49 |
45 void EmitVarInt(byte** b, size_t val) { | 50 void EmitVarInt(byte** b, size_t val) { |
46 while (true) { | 51 while (true) { |
47 size_t next = val >> 7; | 52 size_t next = val >> 7; |
48 byte out = static_cast<byte>(val & 0x7f); | 53 byte out = static_cast<byte>(val & 0x7f); |
49 if (next) { | 54 if (next) { |
50 *((*b)++) = 0x80 | out; | 55 *((*b)++) = 0x80 | out; |
51 val = next; | 56 val = next; |
52 } else { | 57 } else { |
53 *((*b)++) = out; | 58 *((*b)++) = out; |
54 break; | 59 break; |
55 } | 60 } |
56 } | 61 } |
57 } | 62 } |
| 63 |
| 64 |
| 65 void FixupSection(byte* start, byte* end) { |
| 66 // Same as EmitVarInt, but fixed-width with zeroes in the MSBs. |
| 67 size_t val = end - start - padded_varint; |
| 68 for (size_t pos = 0; pos != padded_varint; ++pos) { |
| 69 size_t next = val >> 7; |
| 70 byte out = static_cast<byte>(val & 0x7f); |
| 71 if (pos != padded_varint - 1) { |
| 72 *(start++) = 0x80 | out; |
| 73 val = next; |
| 74 } else { |
| 75 *(start++) = out; |
| 76 } |
| 77 } |
| 78 } |
| 79 |
| 80 |
| 81 // Returns the start of the section, including its VarInt size. |
| 82 byte* EmitSection(byte** b, WasmSection::Code code) { |
| 83 byte* start = *b; |
| 84 const char* name = WasmSection::getName(code); |
| 85 size_t length = WasmSection::getNameLength(code); |
| 86 for (size_t padding = 0; padding != padded_varint; ++padding) { |
| 87 EmitUint8(b, 0xff); // Will get fixed up later. |
| 88 } |
| 89 EmitVarInt(b, length); // Section name string size. |
| 90 for (size_t i = 0; i != length; ++i) EmitUint8(b, name[i]); |
| 91 return start; |
| 92 } |
58 } // namespace | 93 } // namespace |
59 | 94 |
60 | |
61 struct WasmFunctionBuilder::Type { | 95 struct WasmFunctionBuilder::Type { |
62 bool param_; | 96 bool param_; |
63 LocalType type_; | 97 LocalType type_; |
64 }; | 98 }; |
65 | 99 |
66 | 100 |
67 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) | 101 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) |
68 : return_type_(kAstI32), | 102 : return_type_(kAstI32), |
69 locals_(zone), | 103 locals_(zone), |
70 exported_(0), | 104 exported_(0), |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 size_t header_size; | 493 size_t header_size; |
460 size_t body_size; | 494 size_t body_size; |
461 | 495 |
462 size_t total() { return header_size + body_size; } | 496 size_t total() { return header_size + body_size; } |
463 | 497 |
464 void Add(size_t header, size_t body) { | 498 void Add(size_t header, size_t body) { |
465 header_size += header; | 499 header_size += header; |
466 body_size += body; | 500 body_size += body; |
467 } | 501 } |
468 | 502 |
469 void AddSection(size_t size) { | 503 void AddSection(WasmSection::Code code, size_t size) { |
470 if (size > 0) { | 504 if (size > 0) { |
471 Add(1, 0); | 505 Add(padded_varint, 0); // Section size. |
| 506 Add(SizeOfVarInt(WasmSection::getNameLength(code)), 0); |
| 507 Add(WasmSection::getNameLength(code), 0); |
472 Add(SizeOfVarInt(size), 0); | 508 Add(SizeOfVarInt(size), 0); |
473 } | 509 } |
474 } | 510 } |
475 }; | 511 }; |
476 | 512 |
477 | 513 |
478 WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const { | 514 WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const { |
479 Sizes sizes = {0, 0}; | 515 Sizes sizes = {0, 0}; |
480 | 516 |
481 sizes.Add(2 * sizeof(uint32_t), 0); // header | 517 sizes.Add(2 * sizeof(uint32_t), 0); // header |
482 | 518 |
483 sizes.Add(1, 0); | 519 sizes.Add(1, 0); |
484 sizes.Add(kDeclMemorySize, 0); | 520 sizes.Add(kDeclMemorySize, 0); |
485 | 521 |
486 sizes.AddSection(signatures_.size()); | 522 sizes.AddSection(WasmSection::Code::Signatures, signatures_.size()); |
487 for (auto sig : signatures_) { | 523 for (auto sig : signatures_) { |
488 sizes.Add(2 + sig->parameter_count(), 0); | 524 sizes.Add(2 + sig->parameter_count(), 0); |
489 } | 525 } |
490 | 526 |
491 sizes.AddSection(globals_.size()); | 527 sizes.AddSection(WasmSection::Code::Globals, globals_.size()); |
492 if (globals_.size() > 0) { | 528 if (globals_.size() > 0) { |
493 sizes.Add(kDeclGlobalSize * globals_.size(), 0); | 529 sizes.Add(kDeclGlobalSize * globals_.size(), 0); |
494 } | 530 } |
495 | 531 |
496 sizes.AddSection(functions_.size()); | 532 sizes.AddSection(WasmSection::Code::Functions, functions_.size()); |
497 for (auto function : functions_) { | 533 for (auto function : functions_) { |
498 sizes.Add(function->HeaderSize() + function->BodySize(), | 534 sizes.Add(function->HeaderSize() + function->BodySize(), |
499 function->NameSize()); | 535 function->NameSize()); |
500 } | 536 } |
501 | 537 |
502 if (start_function_index_ >= 0) { | 538 if (start_function_index_ >= 0) { |
503 sizes.Add(1, 0); | 539 sizes.Add(1, 0); |
504 sizes.Add(SizeOfVarInt(start_function_index_), 0); | 540 sizes.Add(SizeOfVarInt(start_function_index_), 0); |
505 } | 541 } |
506 | 542 |
507 sizes.AddSection(data_segments_.size()); | 543 sizes.AddSection(WasmSection::Code::DataSegments, data_segments_.size()); |
508 for (auto segment : data_segments_) { | 544 for (auto segment : data_segments_) { |
509 sizes.Add(segment->HeaderSize(), segment->BodySize()); | 545 sizes.Add(segment->HeaderSize(), segment->BodySize()); |
510 } | 546 } |
511 | 547 |
512 sizes.AddSection(indirect_functions_.size()); | 548 sizes.AddSection(WasmSection::Code::FunctionTable, |
| 549 indirect_functions_.size()); |
513 sizes.Add(2 * static_cast<uint32_t>(indirect_functions_.size()), 0); | 550 sizes.Add(2 * static_cast<uint32_t>(indirect_functions_.size()), 0); |
514 | 551 |
515 if (sizes.body_size > 0) sizes.Add(1, 0); | 552 if (sizes.body_size > 0) sizes.Add(1, 0); |
516 | 553 |
517 ZoneVector<uint8_t> buffer_vector(sizes.total(), zone); | 554 ZoneVector<uint8_t> buffer_vector(sizes.total(), zone); |
518 byte* buffer = &buffer_vector[0]; | 555 byte* buffer = &buffer_vector[0]; |
519 byte* header = buffer; | 556 byte* header = buffer; |
520 byte* body = buffer + sizes.header_size; | 557 byte* body = buffer + sizes.header_size; |
521 | 558 |
522 // -- emit magic ------------------------------------------------------------- | 559 // -- emit magic ------------------------------------------------------------- |
523 EmitUint32(&header, kWasmMagic); | 560 EmitUint32(&header, kWasmMagic); |
524 EmitUint32(&header, kWasmVersion); | 561 EmitUint32(&header, kWasmVersion); |
525 | 562 |
526 // -- emit memory declaration ------------------------------------------------ | 563 // -- emit memory declaration ------------------------------------------------ |
527 EmitUint8(&header, kDeclMemory); | 564 { |
528 EmitUint8(&header, 16); // min memory size | 565 byte* section = EmitSection(&header, WasmSection::Code::Memory); |
529 EmitUint8(&header, 16); // max memory size | 566 EmitUint8(&header, 16); // min memory size |
530 EmitUint8(&header, 0); // memory export | 567 EmitUint8(&header, 16); // max memory size |
| 568 EmitUint8(&header, 0); // memory export |
| 569 FixupSection(section, header); |
| 570 } |
531 | 571 |
532 // -- emit globals ----------------------------------------------------------- | 572 // -- emit globals ----------------------------------------------------------- |
533 if (globals_.size() > 0) { | 573 if (globals_.size() > 0) { |
534 EmitUint8(&header, kDeclGlobals); | 574 byte* section = EmitSection(&header, WasmSection::Code::Globals); |
535 EmitVarInt(&header, globals_.size()); | 575 EmitVarInt(&header, globals_.size()); |
536 | 576 |
537 for (auto global : globals_) { | 577 for (auto global : globals_) { |
538 EmitUint32(&header, 0); | 578 EmitUint32(&header, 0); |
539 EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first)); | 579 EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first)); |
540 EmitUint8(&header, global.second); | 580 EmitUint8(&header, global.second); |
541 } | 581 } |
| 582 FixupSection(section, header); |
542 } | 583 } |
543 | 584 |
544 // -- emit signatures -------------------------------------------------------- | 585 // -- emit signatures -------------------------------------------------------- |
545 if (signatures_.size() > 0) { | 586 if (signatures_.size() > 0) { |
546 EmitUint8(&header, kDeclSignatures); | 587 byte* section = EmitSection(&header, WasmSection::Code::Signatures); |
547 EmitVarInt(&header, signatures_.size()); | 588 EmitVarInt(&header, signatures_.size()); |
548 | 589 |
549 for (FunctionSig* sig : signatures_) { | 590 for (FunctionSig* sig : signatures_) { |
550 EmitUint8(&header, static_cast<byte>(sig->parameter_count())); | 591 EmitUint8(&header, static_cast<byte>(sig->parameter_count())); |
551 if (sig->return_count() > 0) { | 592 if (sig->return_count() > 0) { |
552 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetReturn())); | 593 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetReturn())); |
553 } else { | 594 } else { |
554 EmitUint8(&header, kLocalVoid); | 595 EmitUint8(&header, kLocalVoid); |
555 } | 596 } |
556 for (size_t j = 0; j < sig->parameter_count(); j++) { | 597 for (size_t j = 0; j < sig->parameter_count(); j++) { |
557 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j))); | 598 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j))); |
558 } | 599 } |
559 } | 600 } |
| 601 FixupSection(section, header); |
560 } | 602 } |
561 | 603 |
562 // -- emit functions --------------------------------------------------------- | 604 // -- emit functions --------------------------------------------------------- |
563 if (functions_.size() > 0) { | 605 if (functions_.size() > 0) { |
564 EmitUint8(&header, kDeclFunctions); | 606 byte* section = EmitSection(&header, WasmSection::Code::Functions); |
565 EmitVarInt(&header, functions_.size()); | 607 EmitVarInt(&header, functions_.size()); |
566 | 608 |
567 for (auto func : functions_) { | 609 for (auto func : functions_) { |
568 func->Serialize(buffer, &header, &body); | 610 func->Serialize(buffer, &header, &body); |
569 } | 611 } |
| 612 FixupSection(section, header); |
570 } | 613 } |
571 | 614 |
572 // -- emit start function index ---------------------------------------------- | 615 // -- emit start function index ---------------------------------------------- |
573 if (start_function_index_ >= 0) { | 616 if (start_function_index_ >= 0) { |
574 EmitUint8(&header, kDeclStartFunction); | 617 byte* section = EmitSection(&header, WasmSection::Code::StartFunction); |
575 EmitVarInt(&header, start_function_index_); | 618 EmitVarInt(&header, start_function_index_); |
| 619 FixupSection(section, header); |
576 } | 620 } |
577 | 621 |
578 // -- emit data segments ----------------------------------------------------- | 622 // -- emit data segments ----------------------------------------------------- |
579 if (data_segments_.size() > 0) { | 623 if (data_segments_.size() > 0) { |
580 EmitUint8(&header, kDeclDataSegments); | 624 byte* section = EmitSection(&header, WasmSection::Code::DataSegments); |
581 EmitVarInt(&header, data_segments_.size()); | 625 EmitVarInt(&header, data_segments_.size()); |
582 | 626 |
583 for (auto segment : data_segments_) { | 627 for (auto segment : data_segments_) { |
584 segment->Serialize(buffer, &header, &body); | 628 segment->Serialize(buffer, &header, &body); |
585 } | 629 } |
| 630 FixupSection(section, header); |
586 } | 631 } |
587 | 632 |
588 // -- emit function table ---------------------------------------------------- | 633 // -- emit function table ---------------------------------------------------- |
589 if (indirect_functions_.size() > 0) { | 634 if (indirect_functions_.size() > 0) { |
590 EmitUint8(&header, kDeclFunctionTable); | 635 byte* section = EmitSection(&header, WasmSection::Code::FunctionTable); |
591 EmitVarInt(&header, indirect_functions_.size()); | 636 EmitVarInt(&header, indirect_functions_.size()); |
592 | 637 |
593 for (auto index : indirect_functions_) { | 638 for (auto index : indirect_functions_) { |
594 EmitUint16(&header, index); | 639 EmitUint16(&header, index); |
595 } | 640 } |
| 641 FixupSection(section, header); |
596 } | 642 } |
597 | 643 |
598 if (sizes.body_size > 0) EmitUint8(&header, kDeclEnd); | 644 if (sizes.body_size > 0) { |
| 645 byte* section = EmitSection(&header, WasmSection::Code::End); |
| 646 FixupSection(section, header); |
| 647 } |
599 | 648 |
600 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total()); | 649 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total()); |
601 } | 650 } |
602 | 651 |
603 | 652 |
604 std::vector<uint8_t> UnsignedLEB128From(uint32_t result) { | 653 std::vector<uint8_t> UnsignedLEB128From(uint32_t result) { |
605 std::vector<uint8_t> output; | 654 std::vector<uint8_t> output; |
606 uint8_t next = 0; | 655 uint8_t next = 0; |
607 int shift = 0; | 656 int shift = 0; |
608 do { | 657 do { |
609 next = static_cast<uint8_t>(result >> shift); | 658 next = static_cast<uint8_t>(result >> shift); |
610 if (((result >> shift) & 0xFFFFFF80) != 0) { | 659 if (((result >> shift) & 0xFFFFFF80) != 0) { |
611 next = next | 0x80; | 660 next = next | 0x80; |
612 } | 661 } |
613 output.push_back(next); | 662 output.push_back(next); |
614 shift += 7; | 663 shift += 7; |
615 } while ((next & 0x80) != 0); | 664 } while ((next & 0x80) != 0); |
616 return output; | 665 return output; |
617 } | 666 } |
618 } // namespace wasm | 667 } // namespace wasm |
619 } // namespace internal | 668 } // namespace internal |
620 } // namespace v8 | 669 } // namespace v8 |
OLD | NEW |