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 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 uint32_t body_offset = static_cast<uint32_t>(*body - buffer); | 326 uint32_t body_offset = static_cast<uint32_t>(*body - buffer); |
327 EmitUint32(header, dest_); | 327 EmitUint32(header, dest_); |
328 EmitUint32(header, body_offset); | 328 EmitUint32(header, body_offset); |
329 EmitUint32(header, static_cast<uint32_t>(data_.size())); | 329 EmitUint32(header, static_cast<uint32_t>(data_.size())); |
330 EmitUint8(header, 1); // init | 330 EmitUint8(header, 1); // init |
331 | 331 |
332 std::memcpy(*body, &data_[0], data_.size()); | 332 std::memcpy(*body, &data_[0], data_.size()); |
333 (*body) += data_.size(); | 333 (*body) += data_.size(); |
334 } | 334 } |
335 | 335 |
336 | |
337 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) | 336 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) |
338 : zone_(zone), | 337 : zone_(zone), |
339 signatures_(zone), | 338 signatures_(zone), |
340 functions_(zone), | 339 functions_(zone), |
341 data_segments_(zone), | 340 data_segments_(zone), |
342 indirect_functions_(zone), | 341 indirect_functions_(zone), |
343 globals_(zone), | 342 globals_(zone), |
344 signature_map_(zone) {} | 343 signature_map_(zone), |
345 | 344 start_function_index_(-1) {} |
346 | 345 |
347 uint16_t WasmModuleBuilder::AddFunction() { | 346 uint16_t WasmModuleBuilder::AddFunction() { |
348 functions_.push_back(new (zone_) WasmFunctionBuilder(zone_)); | 347 functions_.push_back(new (zone_) WasmFunctionBuilder(zone_)); |
349 return static_cast<uint16_t>(functions_.size() - 1); | 348 return static_cast<uint16_t>(functions_.size() - 1); |
350 } | 349 } |
351 | 350 |
352 | 351 |
353 WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) { | 352 WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) { |
354 if (functions_.size() > index) { | 353 if (functions_.size() > index) { |
355 return functions_.at(index); | 354 return functions_.at(index); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 signatures_.push_back(sig); | 391 signatures_.push_back(sig); |
393 return index; | 392 return index; |
394 } | 393 } |
395 } | 394 } |
396 | 395 |
397 | 396 |
398 void WasmModuleBuilder::AddIndirectFunction(uint16_t index) { | 397 void WasmModuleBuilder::AddIndirectFunction(uint16_t index) { |
399 indirect_functions_.push_back(index); | 398 indirect_functions_.push_back(index); |
400 } | 399 } |
401 | 400 |
| 401 void WasmModuleBuilder::MarkStartFunction(uint16_t index) { |
| 402 start_function_index_ = index; |
| 403 } |
402 | 404 |
403 WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) { | 405 WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) { |
404 WasmModuleWriter* writer = new (zone) WasmModuleWriter(zone); | 406 WasmModuleWriter* writer = new (zone) WasmModuleWriter(zone); |
405 for (auto function : functions_) { | 407 for (auto function : functions_) { |
406 writer->functions_.push_back(function->Build(zone, this)); | 408 writer->functions_.push_back(function->Build(zone, this)); |
407 } | 409 } |
408 for (auto segment : data_segments_) { | 410 for (auto segment : data_segments_) { |
409 writer->data_segments_.push_back(segment); | 411 writer->data_segments_.push_back(segment); |
410 } | 412 } |
411 for (auto sig : signatures_) { | 413 for (auto sig : signatures_) { |
412 writer->signatures_.push_back(sig); | 414 writer->signatures_.push_back(sig); |
413 } | 415 } |
414 for (auto index : indirect_functions_) { | 416 for (auto index : indirect_functions_) { |
415 writer->indirect_functions_.push_back(index); | 417 writer->indirect_functions_.push_back(index); |
416 } | 418 } |
417 for (auto global : globals_) { | 419 for (auto global : globals_) { |
418 writer->globals_.push_back(global); | 420 writer->globals_.push_back(global); |
419 } | 421 } |
| 422 writer->start_function_index_ = start_function_index_; |
420 return writer; | 423 return writer; |
421 } | 424 } |
422 | 425 |
423 | 426 |
424 uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) { | 427 uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) { |
425 globals_.push_back(std::make_pair(type, exported)); | 428 globals_.push_back(std::make_pair(type, exported)); |
426 return static_cast<uint32_t>(globals_.size() - 1); | 429 return static_cast<uint32_t>(globals_.size() - 1); |
427 } | 430 } |
428 | 431 |
429 | 432 |
430 WasmModuleWriter::WasmModuleWriter(Zone* zone) | 433 WasmModuleWriter::WasmModuleWriter(Zone* zone) |
431 : functions_(zone), | 434 : functions_(zone), |
432 data_segments_(zone), | 435 data_segments_(zone), |
433 signatures_(zone), | 436 signatures_(zone), |
434 indirect_functions_(zone), | 437 indirect_functions_(zone), |
435 globals_(zone) {} | 438 globals_(zone) {} |
436 | 439 |
| 440 size_t SizeOfVarInt(size_t value) { |
| 441 size_t size = 0; |
| 442 do { |
| 443 size++; |
| 444 value = value >> 7; |
| 445 } while (value > 0); |
| 446 return size; |
| 447 } |
437 | 448 |
438 struct Sizes { | 449 struct Sizes { |
439 size_t header_size; | 450 size_t header_size; |
440 size_t body_size; | 451 size_t body_size; |
441 | 452 |
442 size_t total() { return header_size + body_size; } | 453 size_t total() { return header_size + body_size; } |
443 | 454 |
444 void Add(size_t header, size_t body) { | 455 void Add(size_t header, size_t body) { |
445 header_size += header; | 456 header_size += header; |
446 body_size += body; | 457 body_size += body; |
447 } | 458 } |
448 | 459 |
449 void AddSection(size_t size) { | 460 void AddSection(size_t size) { |
450 if (size > 0) { | 461 if (size > 0) { |
451 Add(1, 0); | 462 Add(1, 0); |
452 while (size > 0) { | 463 Add(SizeOfVarInt(size), 0); |
453 Add(1, 0); | |
454 size = size >> 7; | |
455 } | |
456 } | 464 } |
457 } | 465 } |
458 }; | 466 }; |
459 | 467 |
460 | 468 |
461 WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const { | 469 WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const { |
462 Sizes sizes = {0, 0}; | 470 Sizes sizes = {0, 0}; |
463 | 471 |
464 sizes.Add(2 * sizeof(uint32_t), 0); // header | 472 sizes.Add(2 * sizeof(uint32_t), 0); // header |
465 | 473 |
466 sizes.Add(1, 0); | 474 sizes.Add(1, 0); |
467 sizes.Add(kDeclMemorySize, 0); | 475 sizes.Add(kDeclMemorySize, 0); |
468 | 476 |
469 sizes.AddSection(signatures_.size()); | 477 sizes.AddSection(signatures_.size()); |
470 for (auto sig : signatures_) { | 478 for (auto sig : signatures_) { |
471 sizes.Add(2 + sig->parameter_count(), 0); | 479 sizes.Add(2 + sig->parameter_count(), 0); |
472 } | 480 } |
473 | 481 |
474 sizes.AddSection(globals_.size()); | 482 sizes.AddSection(globals_.size()); |
475 if (globals_.size() > 0) { | 483 if (globals_.size() > 0) { |
476 sizes.Add(kDeclGlobalSize * globals_.size(), 0); | 484 sizes.Add(kDeclGlobalSize * globals_.size(), 0); |
477 } | 485 } |
478 | 486 |
479 sizes.AddSection(functions_.size()); | 487 sizes.AddSection(functions_.size()); |
480 for (auto function : functions_) { | 488 for (auto function : functions_) { |
481 sizes.Add(function->HeaderSize() + function->BodySize(), | 489 sizes.Add(function->HeaderSize() + function->BodySize(), |
482 function->NameSize()); | 490 function->NameSize()); |
483 } | 491 } |
484 | 492 |
| 493 if (start_function_index_ >= 0) { |
| 494 sizes.Add(1, 0); |
| 495 sizes.Add(SizeOfVarInt(start_function_index_), 0); |
| 496 } |
| 497 |
485 sizes.AddSection(data_segments_.size()); | 498 sizes.AddSection(data_segments_.size()); |
486 for (auto segment : data_segments_) { | 499 for (auto segment : data_segments_) { |
487 sizes.Add(segment->HeaderSize(), segment->BodySize()); | 500 sizes.Add(segment->HeaderSize(), segment->BodySize()); |
488 } | 501 } |
489 | 502 |
490 sizes.AddSection(indirect_functions_.size()); | 503 sizes.AddSection(indirect_functions_.size()); |
491 sizes.Add(2 * static_cast<uint32_t>(indirect_functions_.size()), 0); | 504 sizes.Add(2 * static_cast<uint32_t>(indirect_functions_.size()), 0); |
492 | 505 |
493 if (sizes.body_size > 0) sizes.Add(1, 0); | 506 if (sizes.body_size > 0) sizes.Add(1, 0); |
494 | 507 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 // -- emit functions --------------------------------------------------------- | 553 // -- emit functions --------------------------------------------------------- |
541 if (functions_.size() > 0) { | 554 if (functions_.size() > 0) { |
542 EmitUint8(&header, kDeclFunctions); | 555 EmitUint8(&header, kDeclFunctions); |
543 EmitVarInt(&header, functions_.size()); | 556 EmitVarInt(&header, functions_.size()); |
544 | 557 |
545 for (auto func : functions_) { | 558 for (auto func : functions_) { |
546 func->Serialize(buffer, &header, &body); | 559 func->Serialize(buffer, &header, &body); |
547 } | 560 } |
548 } | 561 } |
549 | 562 |
| 563 // -- emit start function index ---------------------------------------------- |
| 564 if (start_function_index_ >= 0) { |
| 565 EmitUint8(&header, kDeclStartFunction); |
| 566 EmitVarInt(&header, start_function_index_); |
| 567 } |
| 568 |
550 // -- emit data segments ----------------------------------------------------- | 569 // -- emit data segments ----------------------------------------------------- |
551 if (data_segments_.size() > 0) { | 570 if (data_segments_.size() > 0) { |
552 EmitUint8(&header, kDeclDataSegments); | 571 EmitUint8(&header, kDeclDataSegments); |
553 EmitVarInt(&header, data_segments_.size()); | 572 EmitVarInt(&header, data_segments_.size()); |
554 | 573 |
555 for (auto segment : data_segments_) { | 574 for (auto segment : data_segments_) { |
556 segment->Serialize(buffer, &header, &body); | 575 segment->Serialize(buffer, &header, &body); |
557 } | 576 } |
558 } | 577 } |
559 | 578 |
(...skipping 23 matching lines...) Expand all Loading... |
583 next = next | 0x80; | 602 next = next | 0x80; |
584 } | 603 } |
585 output.push_back(next); | 604 output.push_back(next); |
586 shift += 7; | 605 shift += 7; |
587 } while ((next & 0x80) != 0); | 606 } while ((next & 0x80) != 0); |
588 return output; | 607 return output; |
589 } | 608 } |
590 } // namespace wasm | 609 } // namespace wasm |
591 } // namespace internal | 610 } // namespace internal |
592 } // namespace v8 | 611 } // namespace v8 |
OLD | NEW |