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 21 matching lines...) Expand all Loading... |
32 | 32 |
33 /*TODO: add error cases for adding too many locals, too many functions and bad | 33 /*TODO: add error cases for adding too many locals, too many functions and bad |
34 indices in body */ | 34 indices in body */ |
35 | 35 |
36 namespace { | 36 namespace { |
37 void EmitUint8(byte** b, uint8_t x) { | 37 void EmitUint8(byte** b, uint8_t x) { |
38 Memory::uint8_at(*b) = x; | 38 Memory::uint8_at(*b) = x; |
39 *b += 1; | 39 *b += 1; |
40 } | 40 } |
41 | 41 |
42 | |
43 void EmitUint16(byte** b, uint16_t x) { | 42 void EmitUint16(byte** b, uint16_t x) { |
44 WriteUnalignedUInt16(*b, x); | 43 WriteUnalignedUInt16(*b, x); |
45 *b += 2; | 44 *b += 2; |
46 } | 45 } |
47 | 46 |
48 | |
49 void EmitUint32(byte** b, uint32_t x) { | 47 void EmitUint32(byte** b, uint32_t x) { |
50 WriteUnalignedUInt32(*b, x); | 48 WriteUnalignedUInt32(*b, x); |
51 *b += 4; | 49 *b += 4; |
52 } | 50 } |
53 | 51 |
54 void EmitVarInt(byte** b, size_t val) { | 52 void EmitVarInt(byte** b, size_t val) { |
55 LEBHelper::write_u32v(b, static_cast<uint32_t>(val)); | 53 LEBHelper::write_u32v(b, static_cast<uint32_t>(val)); |
56 } | 54 } |
57 | 55 |
58 // Sections all start with a size, but it's unknown at the start. | 56 // Sections all start with a size, but it's unknown at the start. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 93 |
96 return start; | 94 return start; |
97 } | 95 } |
98 } // namespace | 96 } // namespace |
99 | 97 |
100 struct WasmFunctionBuilder::Type { | 98 struct WasmFunctionBuilder::Type { |
101 bool param_; | 99 bool param_; |
102 LocalType type_; | 100 LocalType type_; |
103 }; | 101 }; |
104 | 102 |
105 | |
106 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) | 103 WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) |
107 : return_type_(kAstI32), | 104 : return_type_(kAstI32), |
108 locals_(zone), | 105 locals_(zone), |
109 exported_(0), | 106 exported_(0), |
110 external_(0), | 107 external_(0), |
111 body_(zone), | 108 body_(zone), |
112 local_indices_(zone), | 109 local_indices_(zone), |
113 name_(zone) {} | 110 name_(zone) {} |
114 | 111 |
115 void WasmFunctionBuilder::EmitVarInt(uint32_t val) { | 112 void WasmFunctionBuilder::EmitVarInt(uint32_t val) { |
116 byte buffer[8]; | 113 byte buffer[8]; |
117 byte* ptr = buffer; | 114 byte* ptr = buffer; |
118 LEBHelper::write_u32v(&ptr, val); | 115 LEBHelper::write_u32v(&ptr, val); |
119 for (byte* p = buffer; p < ptr; p++) { | 116 for (byte* p = buffer; p < ptr; p++) { |
120 body_.push_back(*p); | 117 body_.push_back(*p); |
121 } | 118 } |
122 } | 119 } |
123 | 120 |
124 uint16_t WasmFunctionBuilder::AddParam(LocalType type) { | 121 uint16_t WasmFunctionBuilder::AddParam(LocalType type) { |
125 return AddVar(type, true); | 122 return AddVar(type, true); |
126 } | 123 } |
127 | 124 |
128 | |
129 uint16_t WasmFunctionBuilder::AddLocal(LocalType type) { | 125 uint16_t WasmFunctionBuilder::AddLocal(LocalType type) { |
130 return AddVar(type, false); | 126 return AddVar(type, false); |
131 } | 127 } |
132 | 128 |
133 | |
134 uint16_t WasmFunctionBuilder::AddVar(LocalType type, bool param) { | 129 uint16_t WasmFunctionBuilder::AddVar(LocalType type, bool param) { |
135 locals_.push_back({param, type}); | 130 locals_.push_back({param, type}); |
136 return static_cast<uint16_t>(locals_.size() - 1); | 131 return static_cast<uint16_t>(locals_.size() - 1); |
137 } | 132 } |
138 | 133 |
139 | |
140 void WasmFunctionBuilder::ReturnType(LocalType type) { return_type_ = type; } | 134 void WasmFunctionBuilder::ReturnType(LocalType type) { return_type_ = type; } |
141 | 135 |
142 | |
143 void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size) { | 136 void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size) { |
144 EmitCode(code, code_size, nullptr, 0); | 137 EmitCode(code, code_size, nullptr, 0); |
145 } | 138 } |
146 | 139 |
147 void WasmFunctionBuilder::EmitGetLocal(uint32_t local_index) { | 140 void WasmFunctionBuilder::EmitGetLocal(uint32_t local_index) { |
148 local_indices_.push_back(static_cast<uint32_t>(body_.size() + 1)); | 141 local_indices_.push_back(static_cast<uint32_t>(body_.size() + 1)); |
149 EmitWithVarInt(kExprGetLocal, local_index); | 142 EmitWithVarInt(kExprGetLocal, local_index); |
150 } | 143 } |
151 | 144 |
152 void WasmFunctionBuilder::EmitSetLocal(uint32_t local_index) { | 145 void WasmFunctionBuilder::EmitSetLocal(uint32_t local_index) { |
153 local_indices_.push_back(static_cast<uint32_t>(body_.size() + 1)); | 146 local_indices_.push_back(static_cast<uint32_t>(body_.size() + 1)); |
154 EmitWithVarInt(kExprSetLocal, local_index); | 147 EmitWithVarInt(kExprSetLocal, local_index); |
155 } | 148 } |
156 | 149 |
157 void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size, | 150 void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size, |
158 const uint32_t* local_indices, | 151 const uint32_t* local_indices, |
159 uint32_t indices_size) { | 152 uint32_t indices_size) { |
160 size_t size = body_.size(); | 153 size_t size = body_.size(); |
161 for (size_t i = 0; i < code_size; i++) { | 154 for (size_t i = 0; i < code_size; i++) { |
162 body_.push_back(code[i]); | 155 body_.push_back(code[i]); |
163 } | 156 } |
164 for (size_t i = 0; i < indices_size; i++) { | 157 for (size_t i = 0; i < indices_size; i++) { |
165 local_indices_.push_back(local_indices[i] + static_cast<uint32_t>(size)); | 158 local_indices_.push_back(local_indices[i] + static_cast<uint32_t>(size)); |
166 } | 159 } |
167 } | 160 } |
168 | 161 |
169 | |
170 void WasmFunctionBuilder::Emit(WasmOpcode opcode) { | 162 void WasmFunctionBuilder::Emit(WasmOpcode opcode) { |
171 body_.push_back(static_cast<byte>(opcode)); | 163 body_.push_back(static_cast<byte>(opcode)); |
172 } | 164 } |
173 | 165 |
174 | |
175 void WasmFunctionBuilder::EmitWithU8(WasmOpcode opcode, const byte immediate) { | 166 void WasmFunctionBuilder::EmitWithU8(WasmOpcode opcode, const byte immediate) { |
176 body_.push_back(static_cast<byte>(opcode)); | 167 body_.push_back(static_cast<byte>(opcode)); |
177 body_.push_back(immediate); | 168 body_.push_back(immediate); |
178 } | 169 } |
179 | 170 |
180 void WasmFunctionBuilder::EmitWithU8U8(WasmOpcode opcode, const byte imm1, | 171 void WasmFunctionBuilder::EmitWithU8U8(WasmOpcode opcode, const byte imm1, |
181 const byte imm2) { | 172 const byte imm2) { |
182 body_.push_back(static_cast<byte>(opcode)); | 173 body_.push_back(static_cast<byte>(opcode)); |
183 body_.push_back(imm1); | 174 body_.push_back(imm1); |
184 body_.push_back(imm2); | 175 body_.push_back(imm2); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 if (local_indices_[i] >= offset) { | 213 if (local_indices_[i] >= offset) { |
223 local_indices_[i] += diff; | 214 local_indices_[i] += diff; |
224 } | 215 } |
225 } | 216 } |
226 } | 217 } |
227 DCHECK(offset + immediate_size <= body_.size()); | 218 DCHECK(offset + immediate_size <= body_.size()); |
228 byte* p = &body_[offset]; | 219 byte* p = &body_[offset]; |
229 v8::internal::wasm::EmitVarInt(&p, immediate); | 220 v8::internal::wasm::EmitVarInt(&p, immediate); |
230 } | 221 } |
231 | 222 |
232 | |
233 void WasmFunctionBuilder::Exported(uint8_t flag) { exported_ = flag; } | 223 void WasmFunctionBuilder::Exported(uint8_t flag) { exported_ = flag; } |
234 | 224 |
235 | |
236 void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; } | 225 void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; } |
237 | 226 |
238 void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) { | 227 void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) { |
239 name_.clear(); | 228 name_.clear(); |
240 if (name_length > 0) { | 229 if (name_length > 0) { |
241 for (int i = 0; i < name_length; i++) { | 230 for (int i = 0; i < name_length; i++) { |
242 name_.push_back(*(name + i)); | 231 name_.push_back(*(name + i)); |
243 } | 232 } |
244 } | 233 } |
245 } | 234 } |
246 | 235 |
247 | |
248 WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, | 236 WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, |
249 WasmModuleBuilder* mb) const { | 237 WasmModuleBuilder* mb) const { |
250 WasmFunctionEncoder* e = | 238 WasmFunctionEncoder* e = |
251 new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_); | 239 new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_); |
252 uint16_t* var_index = zone->NewArray<uint16_t>(locals_.size()); | 240 uint16_t* var_index = zone->NewArray<uint16_t>(locals_.size()); |
253 IndexVars(e, var_index); | 241 IndexVars(e, var_index); |
254 if (body_.size() > 0) { | 242 if (body_.size() > 0) { |
255 // TODO(titzer): iterate over local indexes, not the bytes. | 243 // TODO(titzer): iterate over local indexes, not the bytes. |
256 const byte* start = &body_[0]; | 244 const byte* start = &body_[0]; |
257 size_t local_index = 0; | 245 size_t local_index = 0; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 sig.AddReturn(static_cast<LocalType>(return_type_)); | 277 sig.AddReturn(static_cast<LocalType>(return_type_)); |
290 } | 278 } |
291 for (size_t i = 0; i < e->params_.size(); i++) { | 279 for (size_t i = 0; i < e->params_.size(); i++) { |
292 sig.AddParam(static_cast<LocalType>(e->params_[i])); | 280 sig.AddParam(static_cast<LocalType>(e->params_[i])); |
293 } | 281 } |
294 e->signature_index_ = mb->AddSignature(sig.Build()); | 282 e->signature_index_ = mb->AddSignature(sig.Build()); |
295 e->name_.insert(e->name_.begin(), name_.begin(), name_.end()); | 283 e->name_.insert(e->name_.begin(), name_.begin(), name_.end()); |
296 return e; | 284 return e; |
297 } | 285 } |
298 | 286 |
299 | |
300 void WasmFunctionBuilder::IndexVars(WasmFunctionEncoder* e, | 287 void WasmFunctionBuilder::IndexVars(WasmFunctionEncoder* e, |
301 uint16_t* var_index) const { | 288 uint16_t* var_index) const { |
302 uint16_t param = 0; | 289 uint16_t param = 0; |
303 uint16_t i32 = 0; | 290 uint16_t i32 = 0; |
304 uint16_t i64 = 0; | 291 uint16_t i64 = 0; |
305 uint16_t f32 = 0; | 292 uint16_t f32 = 0; |
306 uint16_t f64 = 0; | 293 uint16_t f64 = 0; |
307 for (size_t i = 0; i < locals_.size(); i++) { | 294 for (size_t i = 0; i < locals_.size(); i++) { |
308 if (locals_.at(i).param_) { | 295 if (locals_.at(i).param_) { |
309 param++; | 296 param++; |
(...skipping 25 matching lines...) Expand all Loading... |
335 } else if (locals_.at(i).type_ == kAstI64) { | 322 } else if (locals_.at(i).type_ == kAstI64) { |
336 var_index[i] = i64++; | 323 var_index[i] = i64++; |
337 } else if (locals_.at(i).type_ == kAstF32) { | 324 } else if (locals_.at(i).type_ == kAstF32) { |
338 var_index[i] = f32++; | 325 var_index[i] = f32++; |
339 } else if (locals_.at(i).type_ == kAstF64) { | 326 } else if (locals_.at(i).type_ == kAstF64) { |
340 var_index[i] = f64++; | 327 var_index[i] = f64++; |
341 } | 328 } |
342 } | 329 } |
343 } | 330 } |
344 | 331 |
345 | |
346 WasmFunctionEncoder::WasmFunctionEncoder(Zone* zone, LocalType return_type, | 332 WasmFunctionEncoder::WasmFunctionEncoder(Zone* zone, LocalType return_type, |
347 bool exported, bool external) | 333 bool exported, bool external) |
348 : params_(zone), | 334 : params_(zone), |
349 exported_(exported), | 335 exported_(exported), |
350 external_(external), | 336 external_(external), |
351 body_(zone), | 337 body_(zone), |
352 name_(zone) {} | 338 name_(zone) {} |
353 | 339 |
354 | |
355 uint32_t WasmFunctionEncoder::HeaderSize() const { | 340 uint32_t WasmFunctionEncoder::HeaderSize() const { |
356 uint32_t size = 3; | 341 uint32_t size = 3; |
357 if (!external_) size += 2; | 342 if (!external_) size += 2; |
358 if (HasName()) { | 343 if (HasName()) { |
359 uint32_t name_size = NameSize(); | 344 uint32_t name_size = NameSize(); |
360 size += | 345 size += |
361 static_cast<uint32_t>(LEBHelper::sizeof_u32v(name_size)) + name_size; | 346 static_cast<uint32_t>(LEBHelper::sizeof_u32v(name_size)) + name_size; |
362 } | 347 } |
363 return size; | 348 return size; |
364 } | 349 } |
365 | 350 |
366 | |
367 uint32_t WasmFunctionEncoder::BodySize(void) const { | 351 uint32_t WasmFunctionEncoder::BodySize(void) const { |
368 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder | 352 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder |
369 LocalDeclEncoder local_decl; | 353 LocalDeclEncoder local_decl; |
370 local_decl.AddLocals(local_i32_count_, kAstI32); | 354 local_decl.AddLocals(local_i32_count_, kAstI32); |
371 local_decl.AddLocals(local_i64_count_, kAstI64); | 355 local_decl.AddLocals(local_i64_count_, kAstI64); |
372 local_decl.AddLocals(local_f32_count_, kAstF32); | 356 local_decl.AddLocals(local_f32_count_, kAstF32); |
373 local_decl.AddLocals(local_f64_count_, kAstF64); | 357 local_decl.AddLocals(local_f64_count_, kAstF64); |
374 | 358 |
375 return external_ ? 0 | 359 return external_ ? 0 |
376 : static_cast<uint32_t>(body_.size() + local_decl.Size()); | 360 : static_cast<uint32_t>(body_.size() + local_decl.Size()); |
377 } | 361 } |
378 | 362 |
379 | |
380 uint32_t WasmFunctionEncoder::NameSize() const { | 363 uint32_t WasmFunctionEncoder::NameSize() const { |
381 return HasName() ? static_cast<uint32_t>(name_.size()) : 0; | 364 return HasName() ? static_cast<uint32_t>(name_.size()) : 0; |
382 } | 365 } |
383 | 366 |
384 | |
385 void WasmFunctionEncoder::Serialize(byte* buffer, byte** header, | 367 void WasmFunctionEncoder::Serialize(byte* buffer, byte** header, |
386 byte** body) const { | 368 byte** body) const { |
387 uint8_t decl_bits = (exported_ ? kDeclFunctionExport : 0) | | 369 uint8_t decl_bits = (exported_ ? kDeclFunctionExport : 0) | |
388 (external_ ? kDeclFunctionImport : 0) | | 370 (external_ ? kDeclFunctionImport : 0) | |
389 (HasName() ? kDeclFunctionName : 0); | 371 (HasName() ? kDeclFunctionName : 0); |
390 | 372 |
391 EmitUint8(header, decl_bits); | 373 EmitUint8(header, decl_bits); |
392 EmitUint16(header, signature_index_); | 374 EmitUint16(header, signature_index_); |
393 | 375 |
394 if (HasName()) { | 376 if (HasName()) { |
395 EmitVarInt(header, NameSize()); | 377 EmitVarInt(header, NameSize()); |
396 for (size_t i = 0; i < name_.size(); ++i) { | 378 for (size_t i = 0; i < name_.size(); ++i) { |
397 EmitUint8(header, name_[i]); | 379 EmitUint8(header, name_[i]); |
398 } | 380 } |
399 } | 381 } |
400 | 382 |
401 | |
402 if (!external_) { | 383 if (!external_) { |
403 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder | 384 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder |
404 LocalDeclEncoder local_decl; | 385 LocalDeclEncoder local_decl; |
405 local_decl.AddLocals(local_i32_count_, kAstI32); | 386 local_decl.AddLocals(local_i32_count_, kAstI32); |
406 local_decl.AddLocals(local_i64_count_, kAstI64); | 387 local_decl.AddLocals(local_i64_count_, kAstI64); |
407 local_decl.AddLocals(local_f32_count_, kAstF32); | 388 local_decl.AddLocals(local_f32_count_, kAstF32); |
408 local_decl.AddLocals(local_f64_count_, kAstF64); | 389 local_decl.AddLocals(local_f64_count_, kAstF64); |
409 | 390 |
410 EmitUint16(header, static_cast<uint16_t>(body_.size() + local_decl.Size())); | 391 EmitUint16(header, static_cast<uint16_t>(body_.size() + local_decl.Size())); |
411 (*header) += local_decl.Emit(*header); | 392 (*header) += local_decl.Emit(*header); |
412 if (body_.size() > 0) { | 393 if (body_.size() > 0) { |
413 std::memcpy(*header, &body_[0], body_.size()); | 394 std::memcpy(*header, &body_[0], body_.size()); |
414 (*header) += body_.size(); | 395 (*header) += body_.size(); |
415 } | 396 } |
416 } | 397 } |
417 } | 398 } |
418 | 399 |
419 | |
420 WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data, | 400 WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data, |
421 uint32_t size, uint32_t dest) | 401 uint32_t size, uint32_t dest) |
422 : data_(zone), dest_(dest) { | 402 : data_(zone), dest_(dest) { |
423 for (size_t i = 0; i < size; i++) { | 403 for (size_t i = 0; i < size; i++) { |
424 data_.push_back(data[i]); | 404 data_.push_back(data[i]); |
425 } | 405 } |
426 } | 406 } |
427 | 407 |
428 | |
429 uint32_t WasmDataSegmentEncoder::HeaderSize() const { | 408 uint32_t WasmDataSegmentEncoder::HeaderSize() const { |
430 static const int kDataSegmentSize = 13; | 409 static const int kDataSegmentSize = 13; |
431 return kDataSegmentSize; | 410 return kDataSegmentSize; |
432 } | 411 } |
433 | 412 |
434 | |
435 uint32_t WasmDataSegmentEncoder::BodySize() const { | 413 uint32_t WasmDataSegmentEncoder::BodySize() const { |
436 return static_cast<uint32_t>(data_.size()); | 414 return static_cast<uint32_t>(data_.size()); |
437 } | 415 } |
438 | 416 |
439 | |
440 void WasmDataSegmentEncoder::Serialize(byte* buffer, byte** header, | 417 void WasmDataSegmentEncoder::Serialize(byte* buffer, byte** header, |
441 byte** body) const { | 418 byte** body) const { |
442 EmitVarInt(header, dest_); | 419 EmitVarInt(header, dest_); |
443 EmitVarInt(header, static_cast<uint32_t>(data_.size())); | 420 EmitVarInt(header, static_cast<uint32_t>(data_.size())); |
444 | 421 |
445 std::memcpy(*header, &data_[0], data_.size()); | 422 std::memcpy(*header, &data_[0], data_.size()); |
446 (*header) += data_.size(); | 423 (*header) += data_.size(); |
447 } | 424 } |
448 | 425 |
449 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) | 426 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) |
450 : zone_(zone), | 427 : zone_(zone), |
451 signatures_(zone), | 428 signatures_(zone), |
452 functions_(zone), | 429 functions_(zone), |
453 data_segments_(zone), | 430 data_segments_(zone), |
454 indirect_functions_(zone), | 431 indirect_functions_(zone), |
455 globals_(zone), | 432 globals_(zone), |
456 signature_map_(zone), | 433 signature_map_(zone), |
457 start_function_index_(-1) {} | 434 start_function_index_(-1) {} |
458 | 435 |
459 uint16_t WasmModuleBuilder::AddFunction() { | 436 uint16_t WasmModuleBuilder::AddFunction() { |
460 functions_.push_back(new (zone_) WasmFunctionBuilder(zone_)); | 437 functions_.push_back(new (zone_) WasmFunctionBuilder(zone_)); |
461 return static_cast<uint16_t>(functions_.size() - 1); | 438 return static_cast<uint16_t>(functions_.size() - 1); |
462 } | 439 } |
463 | 440 |
464 | |
465 WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) { | 441 WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) { |
466 if (functions_.size() > index) { | 442 if (functions_.size() > index) { |
467 return functions_.at(index); | 443 return functions_.at(index); |
468 } else { | 444 } else { |
469 return nullptr; | 445 return nullptr; |
470 } | 446 } |
471 } | 447 } |
472 | 448 |
473 | |
474 void WasmModuleBuilder::AddDataSegment(WasmDataSegmentEncoder* data) { | 449 void WasmModuleBuilder::AddDataSegment(WasmDataSegmentEncoder* data) { |
475 data_segments_.push_back(data); | 450 data_segments_.push_back(data); |
476 } | 451 } |
477 | 452 |
478 | |
479 bool WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a, | 453 bool WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a, |
480 FunctionSig* b) const { | 454 FunctionSig* b) const { |
481 if (a->return_count() < b->return_count()) return true; | 455 if (a->return_count() < b->return_count()) return true; |
482 if (a->return_count() > b->return_count()) return false; | 456 if (a->return_count() > b->return_count()) return false; |
483 if (a->parameter_count() < b->parameter_count()) return true; | 457 if (a->parameter_count() < b->parameter_count()) return true; |
484 if (a->parameter_count() > b->parameter_count()) return false; | 458 if (a->parameter_count() > b->parameter_count()) return false; |
485 for (size_t r = 0; r < a->return_count(); r++) { | 459 for (size_t r = 0; r < a->return_count(); r++) { |
486 if (a->GetReturn(r) < b->GetReturn(r)) return true; | 460 if (a->GetReturn(r) < b->GetReturn(r)) return true; |
487 if (a->GetReturn(r) > b->GetReturn(r)) return false; | 461 if (a->GetReturn(r) > b->GetReturn(r)) return false; |
488 } | 462 } |
489 for (size_t p = 0; p < a->parameter_count(); p++) { | 463 for (size_t p = 0; p < a->parameter_count(); p++) { |
490 if (a->GetParam(p) < b->GetParam(p)) return true; | 464 if (a->GetParam(p) < b->GetParam(p)) return true; |
491 if (a->GetParam(p) > b->GetParam(p)) return false; | 465 if (a->GetParam(p) > b->GetParam(p)) return false; |
492 } | 466 } |
493 return false; | 467 return false; |
494 } | 468 } |
495 | 469 |
496 | |
497 uint16_t WasmModuleBuilder::AddSignature(FunctionSig* sig) { | 470 uint16_t WasmModuleBuilder::AddSignature(FunctionSig* sig) { |
498 SignatureMap::iterator pos = signature_map_.find(sig); | 471 SignatureMap::iterator pos = signature_map_.find(sig); |
499 if (pos != signature_map_.end()) { | 472 if (pos != signature_map_.end()) { |
500 return pos->second; | 473 return pos->second; |
501 } else { | 474 } else { |
502 uint16_t index = static_cast<uint16_t>(signatures_.size()); | 475 uint16_t index = static_cast<uint16_t>(signatures_.size()); |
503 signature_map_[sig] = index; | 476 signature_map_[sig] = index; |
504 signatures_.push_back(sig); | 477 signatures_.push_back(sig); |
505 return index; | 478 return index; |
506 } | 479 } |
507 } | 480 } |
508 | 481 |
509 | |
510 void WasmModuleBuilder::AddIndirectFunction(uint16_t index) { | 482 void WasmModuleBuilder::AddIndirectFunction(uint16_t index) { |
511 indirect_functions_.push_back(index); | 483 indirect_functions_.push_back(index); |
512 } | 484 } |
513 | 485 |
514 void WasmModuleBuilder::MarkStartFunction(uint16_t index) { | 486 void WasmModuleBuilder::MarkStartFunction(uint16_t index) { |
515 start_function_index_ = index; | 487 start_function_index_ = index; |
516 } | 488 } |
517 | 489 |
518 WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) { | 490 WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) { |
519 WasmModuleWriter* writer = new (zone) WasmModuleWriter(zone); | 491 WasmModuleWriter* writer = new (zone) WasmModuleWriter(zone); |
520 for (auto function : functions_) { | 492 for (auto function : functions_) { |
521 writer->functions_.push_back(function->Build(zone, this)); | 493 writer->functions_.push_back(function->Build(zone, this)); |
522 } | 494 } |
523 for (auto segment : data_segments_) { | 495 for (auto segment : data_segments_) { |
524 writer->data_segments_.push_back(segment); | 496 writer->data_segments_.push_back(segment); |
525 } | 497 } |
526 for (auto sig : signatures_) { | 498 for (auto sig : signatures_) { |
527 writer->signatures_.push_back(sig); | 499 writer->signatures_.push_back(sig); |
528 } | 500 } |
529 for (auto index : indirect_functions_) { | 501 for (auto index : indirect_functions_) { |
530 writer->indirect_functions_.push_back(index); | 502 writer->indirect_functions_.push_back(index); |
531 } | 503 } |
532 for (auto global : globals_) { | 504 for (auto global : globals_) { |
533 writer->globals_.push_back(global); | 505 writer->globals_.push_back(global); |
534 } | 506 } |
535 writer->start_function_index_ = start_function_index_; | 507 writer->start_function_index_ = start_function_index_; |
536 return writer; | 508 return writer; |
537 } | 509 } |
538 | 510 |
539 | |
540 uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) { | 511 uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) { |
541 globals_.push_back(std::make_pair(type, exported)); | 512 globals_.push_back(std::make_pair(type, exported)); |
542 return static_cast<uint32_t>(globals_.size() - 1); | 513 return static_cast<uint32_t>(globals_.size() - 1); |
543 } | 514 } |
544 | 515 |
545 | |
546 WasmModuleWriter::WasmModuleWriter(Zone* zone) | 516 WasmModuleWriter::WasmModuleWriter(Zone* zone) |
547 : functions_(zone), | 517 : functions_(zone), |
548 data_segments_(zone), | 518 data_segments_(zone), |
549 signatures_(zone), | 519 signatures_(zone), |
550 indirect_functions_(zone), | 520 indirect_functions_(zone), |
551 globals_(zone) {} | 521 globals_(zone) {} |
552 | 522 |
553 struct Sizes { | 523 struct Sizes { |
554 size_t header_size; | 524 size_t header_size; |
555 size_t body_size; | 525 size_t body_size; |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
738 if (sizes.body_size > 0) { | 708 if (sizes.body_size > 0) { |
739 byte* section = EmitSection(WasmSection::Code::End, &header); | 709 byte* section = EmitSection(WasmSection::Code::End, &header); |
740 FixupSection(section, header); | 710 FixupSection(section, header); |
741 } | 711 } |
742 | 712 |
743 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total()); | 713 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total()); |
744 } | 714 } |
745 } // namespace wasm | 715 } // namespace wasm |
746 } // namespace internal | 716 } // namespace internal |
747 } // namespace v8 | 717 } // namespace v8 |
OLD | NEW |