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

Side by Side Diff: src/wasm/encoder.cc

Issue 1974933002: [wasm] Remove the use of the "external" bit on OldFunctions section. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Remove unused variable. Created 4 years, 7 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 | « src/wasm/encoder.h ('k') | src/wasm/module-decoder.cc » ('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/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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 97
98 struct WasmFunctionBuilder::Type { 98 struct WasmFunctionBuilder::Type {
99 bool param_; 99 bool param_;
100 LocalType type_; 100 LocalType type_;
101 }; 101 };
102 102
103 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) 103 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone)
104 : return_type_(kAstI32), 104 : return_type_(kAstI32),
105 locals_(zone), 105 locals_(zone),
106 exported_(0), 106 exported_(0),
107 external_(0),
108 body_(zone), 107 body_(zone),
109 local_indices_(zone), 108 local_indices_(zone),
110 name_(zone) {} 109 name_(zone) {}
111 110
112 void WasmFunctionBuilder::EmitVarInt(uint32_t val) { 111 void WasmFunctionBuilder::EmitVarInt(uint32_t val) {
113 byte buffer[8]; 112 byte buffer[8];
114 byte* ptr = buffer; 113 byte* ptr = buffer;
115 LEBHelper::write_u32v(&ptr, val); 114 LEBHelper::write_u32v(&ptr, val);
116 for (byte* p = buffer; p < ptr; p++) { 115 for (byte* p = buffer; p < ptr; p++) {
117 body_.push_back(*p); 116 body_.push_back(*p);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 } 214 }
216 } 215 }
217 } 216 }
218 DCHECK(offset + immediate_size <= body_.size()); 217 DCHECK(offset + immediate_size <= body_.size());
219 byte* p = &body_[offset]; 218 byte* p = &body_[offset];
220 v8::internal::wasm::EmitVarInt(&p, immediate); 219 v8::internal::wasm::EmitVarInt(&p, immediate);
221 } 220 }
222 221
223 void WasmFunctionBuilder::Exported(uint8_t flag) { exported_ = flag; } 222 void WasmFunctionBuilder::Exported(uint8_t flag) { exported_ = flag; }
224 223
225 void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; } 224 void WasmFunctionBuilder::SetName(const char* name, int name_length) {
226
227 void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) {
228 name_.clear(); 225 name_.clear();
229 if (name_length > 0) { 226 if (name_length > 0) {
230 for (int i = 0; i < name_length; i++) { 227 for (int i = 0; i < name_length; i++) {
231 name_.push_back(*(name + i)); 228 name_.push_back(*(name + i));
232 } 229 }
233 } 230 }
234 } 231 }
235 232
236 WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, 233 WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone,
237 WasmModuleBuilder* mb) const { 234 WasmModuleBuilder* mb) const {
238 WasmFunctionEncoder* e = 235 WasmFunctionEncoder* e =
239 new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_); 236 new (zone) WasmFunctionEncoder(zone, return_type_, exported_);
240 uint16_t* var_index = zone->NewArray<uint16_t>(locals_.size()); 237 uint16_t* var_index = zone->NewArray<uint16_t>(locals_.size());
241 IndexVars(e, var_index); 238 IndexVars(e, var_index);
242 if (body_.size() > 0) { 239 if (body_.size() > 0) {
243 // TODO(titzer): iterate over local indexes, not the bytes. 240 // TODO(titzer): iterate over local indexes, not the bytes.
244 const byte* start = &body_[0]; 241 const byte* start = &body_[0];
245 size_t local_index = 0; 242 size_t local_index = 0;
246 for (size_t i = 0; i < body_.size();) { 243 for (size_t i = 0; i < body_.size();) {
247 if (local_index < local_indices_.size() && 244 if (local_index < local_indices_.size() &&
248 i == local_indices_[local_index]) { 245 i == local_indices_[local_index]) {
249 // Read the old index. 246 // Read the old index.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 var_index[i] = i64++; 320 var_index[i] = i64++;
324 } else if (locals_.at(i).type_ == kAstF32) { 321 } else if (locals_.at(i).type_ == kAstF32) {
325 var_index[i] = f32++; 322 var_index[i] = f32++;
326 } else if (locals_.at(i).type_ == kAstF64) { 323 } else if (locals_.at(i).type_ == kAstF64) {
327 var_index[i] = f64++; 324 var_index[i] = f64++;
328 } 325 }
329 } 326 }
330 } 327 }
331 328
332 WasmFunctionEncoder::WasmFunctionEncoder(Zone* zone, LocalType return_type, 329 WasmFunctionEncoder::WasmFunctionEncoder(Zone* zone, LocalType return_type,
333 bool exported, bool external) 330 bool exported)
334 : params_(zone), 331 : params_(zone), exported_(exported), body_(zone), name_(zone) {}
335 exported_(exported),
336 external_(external),
337 body_(zone),
338 name_(zone) {}
339 332
340 uint32_t WasmFunctionEncoder::HeaderSize() const { 333 uint32_t WasmFunctionEncoder::HeaderSize() const {
341 uint32_t size = 3; 334 uint32_t size = 3;
342 if (!external_) size += 2; 335 size += 2;
343 if (HasName()) { 336 if (HasName()) {
344 uint32_t name_size = NameSize(); 337 uint32_t name_size = NameSize();
345 size += 338 size +=
346 static_cast<uint32_t>(LEBHelper::sizeof_u32v(name_size)) + name_size; 339 static_cast<uint32_t>(LEBHelper::sizeof_u32v(name_size)) + name_size;
347 } 340 }
348 return size; 341 return size;
349 } 342 }
350 343
351 uint32_t WasmFunctionEncoder::BodySize(void) const { 344 uint32_t WasmFunctionEncoder::BodySize(void) const {
352 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder 345 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder
353 LocalDeclEncoder local_decl; 346 LocalDeclEncoder local_decl;
354 local_decl.AddLocals(local_i32_count_, kAstI32); 347 local_decl.AddLocals(local_i32_count_, kAstI32);
355 local_decl.AddLocals(local_i64_count_, kAstI64); 348 local_decl.AddLocals(local_i64_count_, kAstI64);
356 local_decl.AddLocals(local_f32_count_, kAstF32); 349 local_decl.AddLocals(local_f32_count_, kAstF32);
357 local_decl.AddLocals(local_f64_count_, kAstF64); 350 local_decl.AddLocals(local_f64_count_, kAstF64);
358 351
359 return external_ ? 0 352 return static_cast<uint32_t>(body_.size() + local_decl.Size());
360 : static_cast<uint32_t>(body_.size() + local_decl.Size());
361 } 353 }
362 354
363 uint32_t WasmFunctionEncoder::NameSize() const { 355 uint32_t WasmFunctionEncoder::NameSize() const {
364 return HasName() ? static_cast<uint32_t>(name_.size()) : 0; 356 return HasName() ? static_cast<uint32_t>(name_.size()) : 0;
365 } 357 }
366 358
367 void WasmFunctionEncoder::Serialize(byte* buffer, byte** header, 359 void WasmFunctionEncoder::Serialize(byte* buffer, byte** header,
368 byte** body) const { 360 byte** body) const {
369 uint8_t decl_bits = (exported_ ? kDeclFunctionExport : 0) | 361 uint8_t decl_bits = (exported_ ? kDeclFunctionExport : 0) |
370 (external_ ? kDeclFunctionImport : 0) |
371 (HasName() ? kDeclFunctionName : 0); 362 (HasName() ? kDeclFunctionName : 0);
372 363
373 EmitUint8(header, decl_bits); 364 EmitUint8(header, decl_bits);
374 EmitUint16(header, signature_index_); 365 EmitUint16(header, signature_index_);
375 366
376 if (HasName()) { 367 if (HasName()) {
377 EmitVarInt(header, NameSize()); 368 EmitVarInt(header, NameSize());
378 for (size_t i = 0; i < name_.size(); ++i) { 369 for (size_t i = 0; i < name_.size(); ++i) {
379 EmitUint8(header, name_[i]); 370 EmitUint8(header, name_[i]);
380 } 371 }
381 } 372 }
382 373
383 if (!external_) { 374 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder
384 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder 375 LocalDeclEncoder local_decl;
385 LocalDeclEncoder local_decl; 376 local_decl.AddLocals(local_i32_count_, kAstI32);
386 local_decl.AddLocals(local_i32_count_, kAstI32); 377 local_decl.AddLocals(local_i64_count_, kAstI64);
387 local_decl.AddLocals(local_i64_count_, kAstI64); 378 local_decl.AddLocals(local_f32_count_, kAstF32);
388 local_decl.AddLocals(local_f32_count_, kAstF32); 379 local_decl.AddLocals(local_f64_count_, kAstF64);
389 local_decl.AddLocals(local_f64_count_, kAstF64);
390 380
391 EmitUint16(header, static_cast<uint16_t>(body_.size() + local_decl.Size())); 381 EmitUint16(header, static_cast<uint16_t>(body_.size() + local_decl.Size()));
392 (*header) += local_decl.Emit(*header); 382 (*header) += local_decl.Emit(*header);
393 if (body_.size() > 0) { 383 if (body_.size() > 0) {
394 std::memcpy(*header, &body_[0], body_.size()); 384 std::memcpy(*header, &body_[0], body_.size());
395 (*header) += body_.size(); 385 (*header) += body_.size();
396 }
397 } 386 }
398 } 387 }
399 388
400 WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data, 389 WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data,
401 uint32_t size, uint32_t dest) 390 uint32_t size, uint32_t dest)
402 : data_(zone), dest_(dest) { 391 : data_(zone), dest_(dest) {
403 for (size_t i = 0; i < size; i++) { 392 for (size_t i = 0; i < size; i++) {
404 data_.push_back(data[i]); 393 data_.push_back(data[i]);
405 } 394 }
406 } 395 }
(...skipping 12 matching lines...) Expand all
419 EmitVarInt(header, dest_); 408 EmitVarInt(header, dest_);
420 EmitVarInt(header, static_cast<uint32_t>(data_.size())); 409 EmitVarInt(header, static_cast<uint32_t>(data_.size()));
421 410
422 std::memcpy(*header, &data_[0], data_.size()); 411 std::memcpy(*header, &data_[0], data_.size());
423 (*header) += data_.size(); 412 (*header) += data_.size();
424 } 413 }
425 414
426 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) 415 WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
427 : zone_(zone), 416 : zone_(zone),
428 signatures_(zone), 417 signatures_(zone),
418 imports_(zone),
429 functions_(zone), 419 functions_(zone),
430 data_segments_(zone), 420 data_segments_(zone),
431 indirect_functions_(zone), 421 indirect_functions_(zone),
432 globals_(zone), 422 globals_(zone),
433 signature_map_(zone), 423 signature_map_(zone),
434 start_function_index_(-1) {} 424 start_function_index_(-1) {}
435 425
436 uint16_t WasmModuleBuilder::AddFunction() { 426 uint32_t WasmModuleBuilder::AddFunction() {
437 functions_.push_back(new (zone_) WasmFunctionBuilder(zone_)); 427 functions_.push_back(new (zone_) WasmFunctionBuilder(zone_));
438 return static_cast<uint16_t>(functions_.size() - 1); 428 return static_cast<uint32_t>(functions_.size() - 1);
439 } 429 }
440 430
441 WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) { 431 WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) {
442 if (functions_.size() > index) { 432 if (functions_.size() > index) {
443 return functions_.at(index); 433 return functions_.at(index);
444 } else { 434 } else {
445 return nullptr; 435 return nullptr;
446 } 436 }
447 } 437 }
448 438
(...skipping 11 matching lines...) Expand all
460 if (a->GetReturn(r) < b->GetReturn(r)) return true; 450 if (a->GetReturn(r) < b->GetReturn(r)) return true;
461 if (a->GetReturn(r) > b->GetReturn(r)) return false; 451 if (a->GetReturn(r) > b->GetReturn(r)) return false;
462 } 452 }
463 for (size_t p = 0; p < a->parameter_count(); p++) { 453 for (size_t p = 0; p < a->parameter_count(); p++) {
464 if (a->GetParam(p) < b->GetParam(p)) return true; 454 if (a->GetParam(p) < b->GetParam(p)) return true;
465 if (a->GetParam(p) > b->GetParam(p)) return false; 455 if (a->GetParam(p) > b->GetParam(p)) return false;
466 } 456 }
467 return false; 457 return false;
468 } 458 }
469 459
470 uint16_t WasmModuleBuilder::AddSignature(FunctionSig* sig) { 460 uint32_t WasmModuleBuilder::AddSignature(FunctionSig* sig) {
471 SignatureMap::iterator pos = signature_map_.find(sig); 461 SignatureMap::iterator pos = signature_map_.find(sig);
472 if (pos != signature_map_.end()) { 462 if (pos != signature_map_.end()) {
473 return pos->second; 463 return pos->second;
474 } else { 464 } else {
475 uint16_t index = static_cast<uint16_t>(signatures_.size()); 465 uint16_t index = static_cast<uint16_t>(signatures_.size());
476 signature_map_[sig] = index; 466 signature_map_[sig] = index;
477 signatures_.push_back(sig); 467 signatures_.push_back(sig);
478 return index; 468 return index;
479 } 469 }
480 } 470 }
481 471
482 void WasmModuleBuilder::AddIndirectFunction(uint16_t index) { 472 void WasmModuleBuilder::AddIndirectFunction(uint16_t index) {
483 indirect_functions_.push_back(index); 473 indirect_functions_.push_back(index);
484 } 474 }
485 475
476 uint32_t WasmModuleBuilder::AddImport(const char* name, int name_length,
477 FunctionSig* sig) {
478 imports_.push_back({AddSignature(sig), name, name_length});
479 return static_cast<uint32_t>(imports_.size() - 1);
480 }
481
486 void WasmModuleBuilder::MarkStartFunction(uint16_t index) { 482 void WasmModuleBuilder::MarkStartFunction(uint16_t index) {
487 start_function_index_ = index; 483 start_function_index_ = index;
488 } 484 }
489 485
490 WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) { 486 WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) {
491 WasmModuleWriter* writer = new (zone) WasmModuleWriter(zone); 487 WasmModuleWriter* writer = new (zone) WasmModuleWriter(zone);
488 for (auto import : imports_) {
489 writer->imports_.push_back(import);
490 }
492 for (auto function : functions_) { 491 for (auto function : functions_) {
493 writer->functions_.push_back(function->Build(zone, this)); 492 writer->functions_.push_back(function->Build(zone, this));
494 } 493 }
495 for (auto segment : data_segments_) { 494 for (auto segment : data_segments_) {
496 writer->data_segments_.push_back(segment); 495 writer->data_segments_.push_back(segment);
497 } 496 }
498 for (auto sig : signatures_) { 497 for (auto sig : signatures_) {
499 writer->signatures_.push_back(sig); 498 writer->signatures_.push_back(sig);
500 } 499 }
501 for (auto index : indirect_functions_) { 500 for (auto index : indirect_functions_) {
502 writer->indirect_functions_.push_back(index); 501 writer->indirect_functions_.push_back(index);
503 } 502 }
504 for (auto global : globals_) { 503 for (auto global : globals_) {
505 writer->globals_.push_back(global); 504 writer->globals_.push_back(global);
506 } 505 }
507 writer->start_function_index_ = start_function_index_; 506 writer->start_function_index_ = start_function_index_;
508 return writer; 507 return writer;
509 } 508 }
510 509
511 uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) { 510 uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) {
512 globals_.push_back(std::make_pair(type, exported)); 511 globals_.push_back(std::make_pair(type, exported));
513 return static_cast<uint32_t>(globals_.size() - 1); 512 return static_cast<uint32_t>(globals_.size() - 1);
514 } 513 }
515 514
516 WasmModuleWriter::WasmModuleWriter(Zone* zone) 515 WasmModuleWriter::WasmModuleWriter(Zone* zone)
517 : functions_(zone), 516 : imports_(zone),
517 functions_(zone),
518 data_segments_(zone), 518 data_segments_(zone),
519 signatures_(zone), 519 signatures_(zone),
520 indirect_functions_(zone), 520 indirect_functions_(zone),
521 globals_(zone) {} 521 globals_(zone) {}
522 522
523 struct Sizes { 523 struct Sizes {
524 size_t header_size; 524 size_t header_size;
525 size_t body_size; 525 size_t body_size;
526 526
527 size_t total() { return header_size + body_size; } 527 size_t total() { return header_size + body_size; }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 if (functions_.size() > 0) { 569 if (functions_.size() > 0) {
570 sizes.AddSection(WasmSection::Code::OldFunctions, functions_.size()); 570 sizes.AddSection(WasmSection::Code::OldFunctions, functions_.size());
571 for (auto function : functions_) { 571 for (auto function : functions_) {
572 sizes.Add(function->HeaderSize() + function->BodySize(), 572 sizes.Add(function->HeaderSize() + function->BodySize(),
573 function->NameSize()); 573 function->NameSize());
574 } 574 }
575 TRACE("Size after functions: %u, %u\n", (unsigned)sizes.header_size, 575 TRACE("Size after functions: %u, %u\n", (unsigned)sizes.header_size,
576 (unsigned)sizes.body_size); 576 (unsigned)sizes.body_size);
577 } 577 }
578 578
579 if (imports_.size() > 0) {
580 sizes.AddSection(WasmSection::Code::ImportTable, imports_.size());
581 for (auto import : imports_) {
582 sizes.Add(LEBHelper::sizeof_u32v(import.sig_index), 0);
583 sizes.Add(LEBHelper::sizeof_u32v(import.name_length), 0);
584 sizes.Add(import.name_length, 0);
585 sizes.Add(1, 0);
586 }
587 TRACE("Size after imports: %u, %u\n", (unsigned)sizes.header_size,
588 (unsigned)sizes.body_size);
589 }
590
579 if (indirect_functions_.size() > 0) { 591 if (indirect_functions_.size() > 0) {
580 sizes.AddSection(WasmSection::Code::FunctionTable, 592 sizes.AddSection(WasmSection::Code::FunctionTable,
581 indirect_functions_.size()); 593 indirect_functions_.size());
582 for (auto function_index : indirect_functions_) { 594 for (auto function_index : indirect_functions_) {
583 sizes.Add(LEBHelper::sizeof_u32v(function_index), 0); 595 sizes.Add(LEBHelper::sizeof_u32v(function_index), 0);
584 } 596 }
585 TRACE("Size after indirect functions: %u, %u\n", 597 TRACE("Size after indirect functions: %u, %u\n",
586 (unsigned)sizes.header_size, (unsigned)sizes.body_size); 598 (unsigned)sizes.header_size, (unsigned)sizes.body_size);
587 } 599 }
588 600
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
648 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j))); 660 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j)));
649 } 661 }
650 EmitVarInt(&header, sig->return_count()); 662 EmitVarInt(&header, sig->return_count());
651 for (size_t j = 0; j < sig->return_count(); j++) { 663 for (size_t j = 0; j < sig->return_count(); j++) {
652 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetReturn(j))); 664 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetReturn(j)));
653 } 665 }
654 } 666 }
655 FixupSection(section, header); 667 FixupSection(section, header);
656 } 668 }
657 669
670 // -- emit imports -----------------------------------------------------------
671 if (imports_.size() > 0) {
672 byte* section = EmitSection(WasmSection::Code::ImportTable, &header);
673 EmitVarInt(&header, imports_.size());
674 for (auto import : imports_) {
675 EmitVarInt(&header, import.sig_index);
676 EmitVarInt(&header, import.name_length);
677 std::memcpy(header, import.name, import.name_length);
678 header += import.name_length;
679 EmitVarInt(&header, 0);
680 }
681 FixupSection(section, header);
682 }
683
658 // -- emit functions --------------------------------------------------------- 684 // -- emit functions ---------------------------------------------------------
659 if (functions_.size() > 0) { 685 if (functions_.size() > 0) {
660 byte* section = EmitSection(WasmSection::Code::OldFunctions, &header); 686 byte* section = EmitSection(WasmSection::Code::OldFunctions, &header);
661 EmitVarInt(&header, functions_.size()); 687 EmitVarInt(&header, functions_.size());
662 688
663 for (auto func : functions_) { 689 for (auto func : functions_) {
664 func->Serialize(buffer, &header, &body); 690 func->Serialize(buffer, &header, &body);
665 } 691 }
666 FixupSection(section, header); 692 FixupSection(section, header);
667 } 693 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 if (sizes.body_size > 0) { 734 if (sizes.body_size > 0) {
709 byte* section = EmitSection(WasmSection::Code::End, &header); 735 byte* section = EmitSection(WasmSection::Code::End, &header);
710 FixupSection(section, header); 736 FixupSection(section, header);
711 } 737 }
712 738
713 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total()); 739 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total());
714 } 740 }
715 } // namespace wasm 741 } // namespace wasm
716 } // namespace internal 742 } // namespace internal
717 } // namespace v8 743 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/encoder.h ('k') | src/wasm/module-decoder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698