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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 byte out = static_cast<byte>(val & 0x7f); | 48 byte out = static_cast<byte>(val & 0x7f); |
49 if (next) { | 49 if (next) { |
50 *((*b)++) = 0x80 | out; | 50 *((*b)++) = 0x80 | out; |
51 val = next; | 51 val = next; |
52 } else { | 52 } else { |
53 *((*b)++) = out; | 53 *((*b)++) = out; |
54 break; | 54 break; |
55 } | 55 } |
56 } | 56 } |
57 } | 57 } |
| 58 |
| 59 size_t SizeOfVarInt(size_t value) { |
| 60 size_t size = 0; |
| 61 do { |
| 62 size++; |
| 63 value = value >> 7; |
| 64 } while (value > 0); |
| 65 return size; |
| 66 } |
| 67 |
58 } // namespace | 68 } // namespace |
59 | 69 |
60 | 70 |
61 struct WasmFunctionBuilder::Type { | 71 struct WasmFunctionBuilder::Type { |
62 bool param_; | 72 bool param_; |
63 LocalType type_; | 73 LocalType type_; |
64 }; | 74 }; |
65 | 75 |
66 | 76 |
67 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) | 77 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 void WasmFunctionBuilder::Emit(WasmOpcode opcode) { | 124 void WasmFunctionBuilder::Emit(WasmOpcode opcode) { |
115 body_.push_back(static_cast<byte>(opcode)); | 125 body_.push_back(static_cast<byte>(opcode)); |
116 } | 126 } |
117 | 127 |
118 | 128 |
119 void WasmFunctionBuilder::EmitWithU8(WasmOpcode opcode, const byte immediate) { | 129 void WasmFunctionBuilder::EmitWithU8(WasmOpcode opcode, const byte immediate) { |
120 body_.push_back(static_cast<byte>(opcode)); | 130 body_.push_back(static_cast<byte>(opcode)); |
121 body_.push_back(immediate); | 131 body_.push_back(immediate); |
122 } | 132 } |
123 | 133 |
| 134 void WasmFunctionBuilder::EmitWithVarInt(WasmOpcode opcode, |
| 135 uint32_t immediate) { |
| 136 body_.push_back(static_cast<byte>(opcode)); |
| 137 size_t immediate_size = SizeOfVarInt(immediate); |
| 138 body_.insert(body_.end(), immediate_size, 0); |
| 139 byte* p = &body_[body_.size() - immediate_size]; |
| 140 EmitVarInt(&p, immediate); |
| 141 } |
124 | 142 |
125 uint32_t WasmFunctionBuilder::EmitEditableImmediate(const byte immediate) { | 143 uint32_t WasmFunctionBuilder::EmitEditableVarIntImmediate() { |
126 body_.push_back(immediate); | 144 // Guess that the immediate will be 1 byte. If it is more, we'll have to |
| 145 // shift everything down. |
| 146 body_.push_back(0); |
127 return static_cast<uint32_t>(body_.size()) - 1; | 147 return static_cast<uint32_t>(body_.size()) - 1; |
128 } | 148 } |
129 | 149 |
| 150 void WasmFunctionBuilder::EditVarIntImmediate(uint32_t offset, |
| 151 const uint32_t immediate) { |
| 152 uint32_t immediate_size = static_cast<uint32_t>(SizeOfVarInt(immediate)); |
| 153 // In EmitEditableVarIntImmediate, we guessed that we'd only need one byte. |
| 154 // If we need more, shift everything down to make room for the larger |
| 155 // immediate. |
| 156 if (immediate_size > 1) { |
| 157 uint32_t diff = immediate_size - 1; |
| 158 body_.insert(body_.begin() + offset, diff, 0); |
130 | 159 |
131 void WasmFunctionBuilder::EditImmediate(uint32_t offset, const byte immediate) { | 160 for (size_t i = 0; i < local_indices_.size(); ++i) { |
132 DCHECK(offset < body_.size()); | 161 if (local_indices_[i] >= offset) { |
133 body_[offset] = immediate; | 162 local_indices_[i] += diff; |
| 163 } |
| 164 } |
| 165 } |
| 166 DCHECK(offset + immediate_size <= body_.size()); |
| 167 byte* p = &body_[offset]; |
| 168 EmitVarInt(&p, immediate); |
134 } | 169 } |
135 | 170 |
136 | 171 |
137 void WasmFunctionBuilder::Exported(uint8_t flag) { exported_ = flag; } | 172 void WasmFunctionBuilder::Exported(uint8_t flag) { exported_ = flag; } |
138 | 173 |
139 | 174 |
140 void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; } | 175 void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; } |
141 | 176 |
142 void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) { | 177 void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) { |
143 name_.clear(); | 178 name_.clear(); |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 } | 474 } |
440 | 475 |
441 | 476 |
442 WasmModuleWriter::WasmModuleWriter(Zone* zone) | 477 WasmModuleWriter::WasmModuleWriter(Zone* zone) |
443 : functions_(zone), | 478 : functions_(zone), |
444 data_segments_(zone), | 479 data_segments_(zone), |
445 signatures_(zone), | 480 signatures_(zone), |
446 indirect_functions_(zone), | 481 indirect_functions_(zone), |
447 globals_(zone) {} | 482 globals_(zone) {} |
448 | 483 |
449 size_t SizeOfVarInt(size_t value) { | |
450 size_t size = 0; | |
451 do { | |
452 size++; | |
453 value = value >> 7; | |
454 } while (value > 0); | |
455 return size; | |
456 } | |
457 | |
458 struct Sizes { | 484 struct Sizes { |
459 size_t header_size; | 485 size_t header_size; |
460 size_t body_size; | 486 size_t body_size; |
461 | 487 |
462 size_t total() { return header_size + body_size; } | 488 size_t total() { return header_size + body_size; } |
463 | 489 |
464 void Add(size_t header, size_t body) { | 490 void Add(size_t header, size_t body) { |
465 header_size += header; | 491 header_size += header; |
466 body_size += body; | 492 body_size += body; |
467 } | 493 } |
(...skipping 10 matching lines...) Expand all Loading... |
478 WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const { | 504 WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const { |
479 Sizes sizes = {0, 0}; | 505 Sizes sizes = {0, 0}; |
480 | 506 |
481 sizes.Add(2 * sizeof(uint32_t), 0); // header | 507 sizes.Add(2 * sizeof(uint32_t), 0); // header |
482 | 508 |
483 sizes.Add(1, 0); | 509 sizes.Add(1, 0); |
484 sizes.Add(kDeclMemorySize, 0); | 510 sizes.Add(kDeclMemorySize, 0); |
485 | 511 |
486 sizes.AddSection(signatures_.size()); | 512 sizes.AddSection(signatures_.size()); |
487 for (auto sig : signatures_) { | 513 for (auto sig : signatures_) { |
488 sizes.Add(2 + sig->parameter_count(), 0); | 514 sizes.Add(1 + SizeOfVarInt(sig->parameter_count()) + sig->parameter_count(), |
| 515 0); |
489 } | 516 } |
490 | 517 |
491 sizes.AddSection(globals_.size()); | 518 sizes.AddSection(globals_.size()); |
492 if (globals_.size() > 0) { | 519 if (globals_.size() > 0) { |
493 sizes.Add(kDeclGlobalSize * globals_.size(), 0); | 520 sizes.Add(kDeclGlobalSize * globals_.size(), 0); |
494 } | 521 } |
495 | 522 |
496 sizes.AddSection(functions_.size()); | 523 sizes.AddSection(functions_.size()); |
497 for (auto function : functions_) { | 524 for (auto function : functions_) { |
498 sizes.Add(function->HeaderSize() + function->BodySize(), | 525 sizes.Add(function->HeaderSize() + function->BodySize(), |
499 function->NameSize()); | 526 function->NameSize()); |
500 } | 527 } |
501 | 528 |
502 if (start_function_index_ >= 0) { | 529 if (start_function_index_ >= 0) { |
503 sizes.Add(1, 0); | 530 sizes.Add(1, 0); |
504 sizes.Add(SizeOfVarInt(start_function_index_), 0); | 531 sizes.Add(SizeOfVarInt(start_function_index_), 0); |
505 } | 532 } |
506 | 533 |
507 sizes.AddSection(data_segments_.size()); | 534 sizes.AddSection(data_segments_.size()); |
508 for (auto segment : data_segments_) { | 535 for (auto segment : data_segments_) { |
509 sizes.Add(segment->HeaderSize(), segment->BodySize()); | 536 sizes.Add(segment->HeaderSize(), segment->BodySize()); |
510 } | 537 } |
511 | 538 |
512 sizes.AddSection(indirect_functions_.size()); | 539 sizes.AddSection(indirect_functions_.size()); |
513 sizes.Add(2 * static_cast<uint32_t>(indirect_functions_.size()), 0); | 540 for (auto function_index : indirect_functions_) { |
| 541 sizes.Add(SizeOfVarInt(function_index), 0); |
| 542 } |
514 | 543 |
515 if (sizes.body_size > 0) sizes.Add(1, 0); | 544 if (sizes.body_size > 0) sizes.Add(1, 0); |
516 | 545 |
517 ZoneVector<uint8_t> buffer_vector(sizes.total(), zone); | 546 ZoneVector<uint8_t> buffer_vector(sizes.total(), zone); |
518 byte* buffer = &buffer_vector[0]; | 547 byte* buffer = &buffer_vector[0]; |
519 byte* header = buffer; | 548 byte* header = buffer; |
520 byte* body = buffer + sizes.header_size; | 549 byte* body = buffer + sizes.header_size; |
521 | 550 |
522 // -- emit magic ------------------------------------------------------------- | 551 // -- emit magic ------------------------------------------------------------- |
523 EmitUint32(&header, kWasmMagic); | 552 EmitUint32(&header, kWasmMagic); |
524 EmitUint32(&header, kWasmVersion); | 553 EmitUint32(&header, kWasmVersion); |
525 | 554 |
526 // -- emit memory declaration ------------------------------------------------ | 555 // -- emit memory declaration ------------------------------------------------ |
527 EmitUint8(&header, kDeclMemory); | 556 EmitUint8(&header, kDeclMemory); |
528 EmitUint8(&header, 16); // min memory size | 557 EmitVarInt(&header, 16); // min memory size |
529 EmitUint8(&header, 16); // max memory size | 558 EmitVarInt(&header, 16); // max memory size |
530 EmitUint8(&header, 0); // memory export | 559 EmitUint8(&header, 0); // memory export |
531 | 560 |
532 // -- emit globals ----------------------------------------------------------- | 561 // -- emit globals ----------------------------------------------------------- |
533 if (globals_.size() > 0) { | 562 if (globals_.size() > 0) { |
534 EmitUint8(&header, kDeclGlobals); | 563 EmitUint8(&header, kDeclGlobals); |
535 EmitVarInt(&header, globals_.size()); | 564 EmitVarInt(&header, globals_.size()); |
536 | 565 |
537 for (auto global : globals_) { | 566 for (auto global : globals_) { |
538 EmitUint32(&header, 0); | 567 EmitUint32(&header, 0); |
539 EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first)); | 568 EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first)); |
540 EmitUint8(&header, global.second); | 569 EmitUint8(&header, global.second); |
541 } | 570 } |
542 } | 571 } |
543 | 572 |
544 // -- emit signatures -------------------------------------------------------- | 573 // -- emit signatures -------------------------------------------------------- |
545 if (signatures_.size() > 0) { | 574 if (signatures_.size() > 0) { |
546 EmitUint8(&header, kDeclSignatures); | 575 EmitUint8(&header, kDeclSignatures); |
547 EmitVarInt(&header, signatures_.size()); | 576 EmitVarInt(&header, signatures_.size()); |
548 | 577 |
549 for (FunctionSig* sig : signatures_) { | 578 for (FunctionSig* sig : signatures_) { |
550 EmitUint8(&header, static_cast<byte>(sig->parameter_count())); | 579 EmitVarInt(&header, sig->parameter_count()); |
551 if (sig->return_count() > 0) { | 580 if (sig->return_count() > 0) { |
552 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetReturn())); | 581 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetReturn())); |
553 } else { | 582 } else { |
554 EmitUint8(&header, kLocalVoid); | 583 EmitUint8(&header, kLocalVoid); |
555 } | 584 } |
556 for (size_t j = 0; j < sig->parameter_count(); j++) { | 585 for (size_t j = 0; j < sig->parameter_count(); j++) { |
557 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j))); | 586 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j))); |
558 } | 587 } |
559 } | 588 } |
560 } | 589 } |
(...skipping 23 matching lines...) Expand all Loading... |
584 segment->Serialize(buffer, &header, &body); | 613 segment->Serialize(buffer, &header, &body); |
585 } | 614 } |
586 } | 615 } |
587 | 616 |
588 // -- emit function table ---------------------------------------------------- | 617 // -- emit function table ---------------------------------------------------- |
589 if (indirect_functions_.size() > 0) { | 618 if (indirect_functions_.size() > 0) { |
590 EmitUint8(&header, kDeclFunctionTable); | 619 EmitUint8(&header, kDeclFunctionTable); |
591 EmitVarInt(&header, indirect_functions_.size()); | 620 EmitVarInt(&header, indirect_functions_.size()); |
592 | 621 |
593 for (auto index : indirect_functions_) { | 622 for (auto index : indirect_functions_) { |
594 EmitUint16(&header, index); | 623 EmitVarInt(&header, index); |
595 } | 624 } |
596 } | 625 } |
597 | 626 |
598 if (sizes.body_size > 0) EmitUint8(&header, kDeclEnd); | 627 if (sizes.body_size > 0) EmitUint8(&header, kDeclEnd); |
599 | 628 |
600 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total()); | 629 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total()); |
601 } | 630 } |
602 | 631 |
603 | 632 |
604 std::vector<uint8_t> UnsignedLEB128From(uint32_t result) { | 633 std::vector<uint8_t> UnsignedLEB128From(uint32_t result) { |
605 std::vector<uint8_t> output; | 634 std::vector<uint8_t> output; |
606 uint8_t next = 0; | 635 uint8_t next = 0; |
607 int shift = 0; | 636 int shift = 0; |
608 do { | 637 do { |
609 next = static_cast<uint8_t>(result >> shift); | 638 next = static_cast<uint8_t>(result >> shift); |
610 if (((result >> shift) & 0xFFFFFF80) != 0) { | 639 if (((result >> shift) & 0xFFFFFF80) != 0) { |
611 next = next | 0x80; | 640 next = next | 0x80; |
612 } | 641 } |
613 output.push_back(next); | 642 output.push_back(next); |
614 shift += 7; | 643 shift += 7; |
615 } while ((next & 0x80) != 0); | 644 } while ((next & 0x80) != 0); |
616 return output; | 645 return output; |
617 } | 646 } |
618 } // namespace wasm | 647 } // namespace wasm |
619 } // namespace internal | 648 } // namespace internal |
620 } // namespace v8 | 649 } // namespace v8 |
OLD | NEW |