| 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 152 | 152 | 
| 153 void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; } | 153 void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; } | 
| 154 | 154 | 
| 155 | 155 | 
| 156 WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, | 156 WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, | 
| 157                                                 WasmModuleBuilder* mb) const { | 157                                                 WasmModuleBuilder* mb) const { | 
| 158   WasmFunctionEncoder* e = | 158   WasmFunctionEncoder* e = | 
| 159       new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_); | 159       new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_); | 
| 160   auto var_index = new uint16_t[locals_.size()]; | 160   auto var_index = new uint16_t[locals_.size()]; | 
| 161   IndexVars(e, var_index); | 161   IndexVars(e, var_index); | 
| 162   const byte* start = body_.data(); | 162   const byte* start = &body_[0]; | 
| 163   const byte* end = start + body_.size(); | 163   const byte* end = start + body_.size(); | 
| 164   size_t local_index = 0; | 164   size_t local_index = 0; | 
| 165   for (size_t i = 0; i < body_.size();) { | 165   for (size_t i = 0; i < body_.size();) { | 
| 166     if (local_index < local_indices_.size() && | 166     if (local_index < local_indices_.size() && | 
| 167         i == local_indices_[local_index]) { | 167         i == local_indices_[local_index]) { | 
| 168       int length = 0; | 168       int length = 0; | 
| 169       uint32_t index; | 169       uint32_t index; | 
| 170       ReadUnsignedLEB128Operand(start + i, end, &length, &index); | 170       ReadUnsignedLEB128Operand(start + i, end, &length, &index); | 
| 171       uint16_t new_index = var_index[index]; | 171       uint16_t new_index = var_index[index]; | 
| 172       const std::vector<uint8_t>& index_vec = UnsignedLEB128From(new_index); | 172       const std::vector<uint8_t>& index_vec = UnsignedLEB128From(new_index); | 
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 275                       (external_ ? kDeclFunctionImport : 0) | | 275                       (external_ ? kDeclFunctionImport : 0) | | 
| 276                       (HasLocals() ? kDeclFunctionLocals : 0) | | 276                       (HasLocals() ? kDeclFunctionLocals : 0) | | 
| 277                       (HasName() ? kDeclFunctionName : 0); | 277                       (HasName() ? kDeclFunctionName : 0); | 
| 278 | 278 | 
| 279   EmitUint8(header, decl_bits); | 279   EmitUint8(header, decl_bits); | 
| 280   EmitUint16(header, signature_index_); | 280   EmitUint16(header, signature_index_); | 
| 281 | 281 | 
| 282   if (HasName()) { | 282   if (HasName()) { | 
| 283     uint32_t name_offset = static_cast<uint32_t>(*body - buffer); | 283     uint32_t name_offset = static_cast<uint32_t>(*body - buffer); | 
| 284     EmitUint32(header, name_offset); | 284     EmitUint32(header, name_offset); | 
| 285     std::memcpy(*body, name_.data(), name_.size()); | 285     std::memcpy(*body, &name_[0], name_.size()); | 
| 286     (*body) += name_.size(); | 286     (*body) += name_.size(); | 
| 287   } | 287   } | 
| 288 | 288 | 
| 289   if (HasLocals()) { | 289   if (HasLocals()) { | 
| 290     EmitUint16(header, local_int32_count_); | 290     EmitUint16(header, local_int32_count_); | 
| 291     EmitUint16(header, local_int64_count_); | 291     EmitUint16(header, local_int64_count_); | 
| 292     EmitUint16(header, local_float32_count_); | 292     EmitUint16(header, local_float32_count_); | 
| 293     EmitUint16(header, local_float64_count_); | 293     EmitUint16(header, local_float64_count_); | 
| 294   } | 294   } | 
| 295 | 295 | 
| 296   if (!external_) { | 296   if (!external_) { | 
| 297     EmitUint16(header, static_cast<uint16_t>(body_.size())); | 297     EmitUint16(header, static_cast<uint16_t>(body_.size())); | 
| 298     std::memcpy(*header, body_.data(), body_.size()); | 298     std::memcpy(*header, &body_[0], body_.size()); | 
| 299     (*header) += body_.size(); | 299     (*header) += body_.size(); | 
| 300   } | 300   } | 
| 301 } | 301 } | 
| 302 | 302 | 
| 303 | 303 | 
| 304 WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data, | 304 WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data, | 
| 305                                                uint32_t size, uint32_t dest) | 305                                                uint32_t size, uint32_t dest) | 
| 306     : data_(zone), dest_(dest) { | 306     : data_(zone), dest_(dest) { | 
| 307   for (size_t i = 0; i < size; i++) { | 307   for (size_t i = 0; i < size; i++) { | 
| 308     data_.push_back(data[i]); | 308     data_.push_back(data[i]); | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 322 | 322 | 
| 323 | 323 | 
| 324 void WasmDataSegmentEncoder::Serialize(byte* buffer, byte** header, | 324 void WasmDataSegmentEncoder::Serialize(byte* buffer, byte** header, | 
| 325                                        byte** body) const { | 325                                        byte** body) const { | 
| 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_.data(), data_.size()); | 332   std::memcpy(*body, &data_[0], data_.size()); | 
| 333   (*body) += data_.size(); | 333   (*body) += data_.size(); | 
| 334 } | 334 } | 
| 335 | 335 | 
| 336 | 336 | 
| 337 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) | 337 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) | 
| 338     : zone_(zone), | 338     : zone_(zone), | 
| 339       signatures_(zone), | 339       signatures_(zone), | 
| 340       functions_(zone), | 340       functions_(zone), | 
| 341       data_segments_(zone), | 341       data_segments_(zone), | 
| 342       indirect_functions_(zone), | 342       indirect_functions_(zone), | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
| 360   } | 360   } | 
| 361 } | 361 } | 
| 362 | 362 | 
| 363 | 363 | 
| 364 void WasmModuleBuilder::AddDataSegment(WasmDataSegmentEncoder* data) { | 364 void WasmModuleBuilder::AddDataSegment(WasmDataSegmentEncoder* data) { | 
| 365   data_segments_.push_back(data); | 365   data_segments_.push_back(data); | 
| 366 } | 366 } | 
| 367 | 367 | 
| 368 | 368 | 
| 369 int WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a, | 369 int WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a, | 
| 370                                                        FunctionSig* b) { | 370                                                        FunctionSig* b) const { | 
| 371   if (a->return_count() < b->return_count()) return -1; | 371   if (a->return_count() < b->return_count()) return -1; | 
| 372   if (a->return_count() > b->return_count()) return 1; | 372   if (a->return_count() > b->return_count()) return 1; | 
| 373   if (a->parameter_count() < b->parameter_count()) return -1; | 373   if (a->parameter_count() < b->parameter_count()) return -1; | 
| 374   if (a->parameter_count() > b->parameter_count()) return 1; | 374   if (a->parameter_count() > b->parameter_count()) return 1; | 
| 375   for (size_t r = 0; r < a->return_count(); r++) { | 375   for (size_t r = 0; r < a->return_count(); r++) { | 
| 376     if (a->GetReturn(r) < b->GetReturn(r)) return -1; | 376     if (a->GetReturn(r) < b->GetReturn(r)) return -1; | 
| 377     if (a->GetReturn(r) > b->GetReturn(r)) return 1; | 377     if (a->GetReturn(r) > b->GetReturn(r)) return 1; | 
| 378   } | 378   } | 
| 379   for (size_t p = 0; p < a->parameter_count(); p++) { | 379   for (size_t p = 0; p < a->parameter_count(); p++) { | 
| 380     if (a->GetParam(p) < b->GetParam(p)) return -1; | 380     if (a->GetParam(p) < b->GetParam(p)) return -1; | 
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 486   for (auto segment : data_segments_) { | 486   for (auto segment : data_segments_) { | 
| 487     sizes.Add(segment->HeaderSize(), segment->BodySize()); | 487     sizes.Add(segment->HeaderSize(), segment->BodySize()); | 
| 488   } | 488   } | 
| 489 | 489 | 
| 490   sizes.AddSection(indirect_functions_.size()); | 490   sizes.AddSection(indirect_functions_.size()); | 
| 491   sizes.Add(2 * static_cast<uint32_t>(indirect_functions_.size()), 0); | 491   sizes.Add(2 * static_cast<uint32_t>(indirect_functions_.size()), 0); | 
| 492 | 492 | 
| 493   if (sizes.body_size > 0) sizes.Add(1, 0); | 493   if (sizes.body_size > 0) sizes.Add(1, 0); | 
| 494 | 494 | 
| 495   ZoneVector<uint8_t> buffer_vector(sizes.total(), zone); | 495   ZoneVector<uint8_t> buffer_vector(sizes.total(), zone); | 
| 496   byte* buffer = buffer_vector.data(); | 496   byte* buffer = &buffer_vector[0]; | 
| 497   byte* header = buffer; | 497   byte* header = buffer; | 
| 498   byte* body = buffer + sizes.header_size; | 498   byte* body = buffer + sizes.header_size; | 
| 499 | 499 | 
| 500   // -- emit memory declaration ------------------------------------------------ | 500   // -- emit memory declaration ------------------------------------------------ | 
| 501   EmitUint8(&header, kDeclMemory); | 501   EmitUint8(&header, kDeclMemory); | 
| 502   EmitUint8(&header, 16);  // min memory size | 502   EmitUint8(&header, 16);  // min memory size | 
| 503   EmitUint8(&header, 16);  // max memory size | 503   EmitUint8(&header, 16);  // max memory size | 
| 504   EmitUint8(&header, 0);   // memory export | 504   EmitUint8(&header, 0);   // memory export | 
| 505 | 505 | 
| 506   // -- emit globals ----------------------------------------------------------- | 506   // -- emit globals ----------------------------------------------------------- | 
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 579       next = next | 0x80; | 579       next = next | 0x80; | 
| 580     } | 580     } | 
| 581     output.push_back(next); | 581     output.push_back(next); | 
| 582     shift += 7; | 582     shift += 7; | 
| 583   } while ((next & 0x80) != 0); | 583   } while ((next & 0x80) != 0); | 
| 584   return output; | 584   return output; | 
| 585 } | 585 } | 
| 586 }  // namespace wasm | 586 }  // namespace wasm | 
| 587 }  // namespace internal | 587 }  // namespace internal | 
| 588 }  // namespace v8 | 588 }  // namespace v8 | 
| OLD | NEW | 
|---|