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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 173 |
174 | 174 |
175 void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; } | 175 void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; } |
176 | 176 |
177 void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) { | 177 void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) { |
178 name_.clear(); | 178 name_.clear(); |
179 if (name_length > 0) { | 179 if (name_length > 0) { |
180 for (int i = 0; i < name_length; i++) { | 180 for (int i = 0; i < name_length; i++) { |
181 name_.push_back(*(name + i)); | 181 name_.push_back(*(name + i)); |
182 } | 182 } |
183 name_.push_back('\0'); | |
184 } | 183 } |
185 } | 184 } |
186 | 185 |
187 | 186 |
188 WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, | 187 WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, |
189 WasmModuleBuilder* mb) const { | 188 WasmModuleBuilder* mb) const { |
190 WasmFunctionEncoder* e = | 189 WasmFunctionEncoder* e = |
191 new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_); | 190 new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_); |
192 uint16_t* var_index = zone->NewArray<uint16_t>(locals_.size()); | 191 uint16_t* var_index = zone->NewArray<uint16_t>(locals_.size()); |
193 IndexVars(e, var_index); | 192 IndexVars(e, var_index); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 : params_(zone), | 279 : params_(zone), |
281 exported_(exported), | 280 exported_(exported), |
282 external_(external), | 281 external_(external), |
283 body_(zone), | 282 body_(zone), |
284 name_(zone) {} | 283 name_(zone) {} |
285 | 284 |
286 | 285 |
287 uint32_t WasmFunctionEncoder::HeaderSize() const { | 286 uint32_t WasmFunctionEncoder::HeaderSize() const { |
288 uint32_t size = 3; | 287 uint32_t size = 3; |
289 if (!external_) size += 2; | 288 if (!external_) size += 2; |
290 if (HasName()) size += 4; | 289 if (HasName()) { |
| 290 uint32_t name_size = NameSize(); |
| 291 size += static_cast<uint32_t>(SizeOfVarInt(name_size)) + name_size; |
| 292 } |
291 return size; | 293 return size; |
292 } | 294 } |
293 | 295 |
294 | 296 |
295 uint32_t WasmFunctionEncoder::BodySize(void) const { | 297 uint32_t WasmFunctionEncoder::BodySize(void) const { |
296 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder | 298 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder |
297 LocalDeclEncoder local_decl; | 299 LocalDeclEncoder local_decl; |
298 local_decl.AddLocals(local_i32_count_, kAstI32); | 300 local_decl.AddLocals(local_i32_count_, kAstI32); |
299 local_decl.AddLocals(local_i64_count_, kAstI64); | 301 local_decl.AddLocals(local_i64_count_, kAstI64); |
300 local_decl.AddLocals(local_f32_count_, kAstF32); | 302 local_decl.AddLocals(local_f32_count_, kAstF32); |
(...skipping 12 matching lines...) Expand all Loading... |
313 void WasmFunctionEncoder::Serialize(byte* buffer, byte** header, | 315 void WasmFunctionEncoder::Serialize(byte* buffer, byte** header, |
314 byte** body) const { | 316 byte** body) const { |
315 uint8_t decl_bits = (exported_ ? kDeclFunctionExport : 0) | | 317 uint8_t decl_bits = (exported_ ? kDeclFunctionExport : 0) | |
316 (external_ ? kDeclFunctionImport : 0) | | 318 (external_ ? kDeclFunctionImport : 0) | |
317 (HasName() ? kDeclFunctionName : 0); | 319 (HasName() ? kDeclFunctionName : 0); |
318 | 320 |
319 EmitUint8(header, decl_bits); | 321 EmitUint8(header, decl_bits); |
320 EmitUint16(header, signature_index_); | 322 EmitUint16(header, signature_index_); |
321 | 323 |
322 if (HasName()) { | 324 if (HasName()) { |
323 uint32_t name_offset = static_cast<uint32_t>(*body - buffer); | 325 EmitVarInt(header, NameSize()); |
324 EmitUint32(header, name_offset); | 326 for (size_t i = 0; i < name_.size(); ++i) { |
325 std::memcpy(*body, &name_[0], name_.size()); | 327 EmitUint8(header, name_[i]); |
326 (*body) += name_.size(); | 328 } |
327 } | 329 } |
328 | 330 |
329 | 331 |
330 if (!external_) { | 332 if (!external_) { |
331 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder | 333 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder |
332 LocalDeclEncoder local_decl; | 334 LocalDeclEncoder local_decl; |
333 local_decl.AddLocals(local_i32_count_, kAstI32); | 335 local_decl.AddLocals(local_i32_count_, kAstI32); |
334 local_decl.AddLocals(local_i64_count_, kAstI64); | 336 local_decl.AddLocals(local_i64_count_, kAstI64); |
335 local_decl.AddLocals(local_f32_count_, kAstF32); | 337 local_decl.AddLocals(local_f32_count_, kAstF32); |
336 local_decl.AddLocals(local_f64_count_, kAstF64); | 338 local_decl.AddLocals(local_f64_count_, kAstF64); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 sizes.Add(kDeclMemorySize, 0); | 512 sizes.Add(kDeclMemorySize, 0); |
511 | 513 |
512 sizes.AddSection(signatures_.size()); | 514 sizes.AddSection(signatures_.size()); |
513 for (auto sig : signatures_) { | 515 for (auto sig : signatures_) { |
514 sizes.Add(1 + SizeOfVarInt(sig->parameter_count()) + sig->parameter_count(), | 516 sizes.Add(1 + SizeOfVarInt(sig->parameter_count()) + sig->parameter_count(), |
515 0); | 517 0); |
516 } | 518 } |
517 | 519 |
518 sizes.AddSection(globals_.size()); | 520 sizes.AddSection(globals_.size()); |
519 if (globals_.size() > 0) { | 521 if (globals_.size() > 0) { |
520 sizes.Add(kDeclGlobalSize * globals_.size(), 0); | 522 /* These globals never have names, so are always 3 bytes. */ |
| 523 sizes.Add(3 * globals_.size(), 0); |
521 } | 524 } |
522 | 525 |
523 sizes.AddSection(functions_.size()); | 526 sizes.AddSection(functions_.size()); |
524 for (auto function : functions_) { | 527 for (auto function : functions_) { |
525 sizes.Add(function->HeaderSize() + function->BodySize(), | 528 sizes.Add(function->HeaderSize() + function->BodySize(), |
526 function->NameSize()); | 529 function->NameSize()); |
527 } | 530 } |
528 | 531 |
529 if (start_function_index_ >= 0) { | 532 if (start_function_index_ >= 0) { |
530 sizes.Add(1, 0); | 533 sizes.Add(1, 0); |
(...skipping 26 matching lines...) Expand all Loading... |
557 EmitVarInt(&header, 16); // min memory size | 560 EmitVarInt(&header, 16); // min memory size |
558 EmitVarInt(&header, 16); // max memory size | 561 EmitVarInt(&header, 16); // max memory size |
559 EmitUint8(&header, 0); // memory export | 562 EmitUint8(&header, 0); // memory export |
560 | 563 |
561 // -- emit globals ----------------------------------------------------------- | 564 // -- emit globals ----------------------------------------------------------- |
562 if (globals_.size() > 0) { | 565 if (globals_.size() > 0) { |
563 EmitUint8(&header, kDeclGlobals); | 566 EmitUint8(&header, kDeclGlobals); |
564 EmitVarInt(&header, globals_.size()); | 567 EmitVarInt(&header, globals_.size()); |
565 | 568 |
566 for (auto global : globals_) { | 569 for (auto global : globals_) { |
567 EmitUint32(&header, 0); | 570 EmitVarInt(&header, 0); // Length of the global name. |
568 EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first)); | 571 EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first)); |
569 EmitUint8(&header, global.second); | 572 EmitUint8(&header, global.second); |
570 } | 573 } |
571 } | 574 } |
572 | 575 |
573 // -- emit signatures -------------------------------------------------------- | 576 // -- emit signatures -------------------------------------------------------- |
574 if (signatures_.size() > 0) { | 577 if (signatures_.size() > 0) { |
575 EmitUint8(&header, kDeclSignatures); | 578 EmitUint8(&header, kDeclSignatures); |
576 EmitVarInt(&header, signatures_.size()); | 579 EmitVarInt(&header, signatures_.size()); |
577 | 580 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 next = next | 0x80; | 643 next = next | 0x80; |
641 } | 644 } |
642 output.push_back(next); | 645 output.push_back(next); |
643 shift += 7; | 646 shift += 7; |
644 } while ((next & 0x80) != 0); | 647 } while ((next & 0x80) != 0); |
645 return output; | 648 return output; |
646 } | 649 } |
647 } // namespace wasm | 650 } // namespace wasm |
648 } // namespace internal | 651 } // namespace internal |
649 } // namespace v8 | 652 } // namespace v8 |
OLD | NEW |