blob: 92e6b1145cba4670e204046bb209674a8bca51f5 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
3// found in the LICENSE file.
4
5#include "src/signature.h"
6
7#include "src/handles.h"
8#include "src/v8.h"
9#include "src/zone-containers.h"
10
11#include "src/wasm/ast-decoder.h"
12#include "src/wasm/encoder.h"
Ben Murdochda12d292016-06-02 14:46:10 +010013#include "src/wasm/wasm-macro-gen.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014#include "src/wasm/wasm-module.h"
15#include "src/wasm/wasm-opcodes.h"
16
17#include "src/v8memory.h"
18
Ben Murdochda12d292016-06-02 14:46:10 +010019#if DEBUG
20#define TRACE(...) \
21 do { \
22 if (FLAG_trace_wasm_encoder) PrintF(__VA_ARGS__); \
23 } while (false)
24#else
25#define TRACE(...)
26#endif
27
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028namespace v8 {
29namespace internal {
30namespace wasm {
31
32/*TODO: add error cases for adding too many locals, too many functions and bad
33 indices in body */
34
35namespace {
36void EmitUint8(byte** b, uint8_t x) {
37 Memory::uint8_at(*b) = x;
38 *b += 1;
39}
40
41
42void EmitUint16(byte** b, uint16_t x) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010043 WriteUnalignedUInt16(*b, x);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 *b += 2;
45}
46
47
48void EmitUint32(byte** b, uint32_t x) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010049 WriteUnalignedUInt32(*b, x);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050 *b += 4;
51}
52
Ben Murdochda12d292016-06-02 14:46:10 +010053// Sections all start with a size, but it's unknown at the start.
54// We generate a large varint which we then fixup later when the size is known.
55//
56// TODO(jfb) Not strictly necessary since sizes are calculated ahead of time.
57const size_t padded_varint = 5;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058
59void EmitVarInt(byte** b, size_t val) {
60 while (true) {
61 size_t next = val >> 7;
62 byte out = static_cast<byte>(val & 0x7f);
63 if (next) {
64 *((*b)++) = 0x80 | out;
65 val = next;
66 } else {
67 *((*b)++) = out;
68 break;
69 }
70 }
71}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072
Ben Murdochda12d292016-06-02 14:46:10 +010073size_t SizeOfVarInt(size_t value) {
74 size_t size = 0;
75 do {
76 size++;
77 value = value >> 7;
78 } while (value > 0);
79 return size;
80}
81
82void FixupSection(byte* start, byte* end) {
83 // Same as EmitVarInt, but fixed-width with zeroes in the MSBs.
84 size_t val = end - start - padded_varint;
85 TRACE(" fixup %u\n", (unsigned)val);
86 for (size_t pos = 0; pos != padded_varint; ++pos) {
87 size_t next = val >> 7;
88 byte out = static_cast<byte>(val & 0x7f);
89 if (pos != padded_varint - 1) {
90 *(start++) = 0x80 | out;
91 val = next;
92 } else {
93 *(start++) = out;
94 // TODO(jfb) check that the pre-allocated fixup size isn't overflowed.
95 }
96 }
97}
98
99// Returns the start of the section, where the section VarInt size is.
100byte* EmitSection(WasmSection::Code code, byte** b) {
101 byte* start = *b;
102 const char* name = WasmSection::getName(code);
103 size_t length = WasmSection::getNameLength(code);
104 TRACE("emit section: %s\n", name);
105 for (size_t padding = 0; padding != padded_varint; ++padding) {
106 EmitUint8(b, 0xff); // Will get fixed up later.
107 }
108 EmitVarInt(b, length); // Section name string size.
109 for (size_t i = 0; i != length; ++i) EmitUint8(b, name[i]);
110 return start;
111}
112} // namespace
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113
114struct WasmFunctionBuilder::Type {
115 bool param_;
116 LocalType type_;
117};
118
119
120WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone)
121 : return_type_(kAstI32),
122 locals_(zone),
123 exported_(0),
124 external_(0),
125 body_(zone),
126 local_indices_(zone),
127 name_(zone) {}
128
129
130uint16_t WasmFunctionBuilder::AddParam(LocalType type) {
131 return AddVar(type, true);
132}
133
134
135uint16_t WasmFunctionBuilder::AddLocal(LocalType type) {
136 return AddVar(type, false);
137}
138
139
140uint16_t WasmFunctionBuilder::AddVar(LocalType type, bool param) {
141 locals_.push_back({param, type});
142 return static_cast<uint16_t>(locals_.size() - 1);
143}
144
145
146void WasmFunctionBuilder::ReturnType(LocalType type) { return_type_ = type; }
147
148
149void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size) {
150 EmitCode(code, code_size, nullptr, 0);
151}
152
153
154void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size,
155 const uint32_t* local_indices,
156 uint32_t indices_size) {
157 size_t size = body_.size();
158 for (size_t i = 0; i < code_size; i++) {
159 body_.push_back(code[i]);
160 }
161 for (size_t i = 0; i < indices_size; i++) {
162 local_indices_.push_back(local_indices[i] + static_cast<uint32_t>(size));
163 }
164}
165
166
167void WasmFunctionBuilder::Emit(WasmOpcode opcode) {
168 body_.push_back(static_cast<byte>(opcode));
169}
170
171
172void WasmFunctionBuilder::EmitWithU8(WasmOpcode opcode, const byte immediate) {
173 body_.push_back(static_cast<byte>(opcode));
174 body_.push_back(immediate);
175}
176
Ben Murdochda12d292016-06-02 14:46:10 +0100177void WasmFunctionBuilder::EmitWithU8U8(WasmOpcode opcode, const byte imm1,
178 const byte imm2) {
179 body_.push_back(static_cast<byte>(opcode));
180 body_.push_back(imm1);
181 body_.push_back(imm2);
182}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000183
Ben Murdochda12d292016-06-02 14:46:10 +0100184void WasmFunctionBuilder::EmitWithVarInt(WasmOpcode opcode,
185 uint32_t immediate) {
186 body_.push_back(static_cast<byte>(opcode));
187 size_t immediate_size = SizeOfVarInt(immediate);
188 body_.insert(body_.end(), immediate_size, 0);
189 byte* p = &body_[body_.size() - immediate_size];
190 EmitVarInt(&p, immediate);
191}
192
193uint32_t WasmFunctionBuilder::EmitEditableVarIntImmediate() {
194 // Guess that the immediate will be 1 byte. If it is more, we'll have to
195 // shift everything down.
196 body_.push_back(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197 return static_cast<uint32_t>(body_.size()) - 1;
198}
199
Ben Murdochda12d292016-06-02 14:46:10 +0100200void WasmFunctionBuilder::EditVarIntImmediate(uint32_t offset,
201 const uint32_t immediate) {
202 uint32_t immediate_size = static_cast<uint32_t>(SizeOfVarInt(immediate));
203 // In EmitEditableVarIntImmediate, we guessed that we'd only need one byte.
204 // If we need more, shift everything down to make room for the larger
205 // immediate.
206 if (immediate_size > 1) {
207 uint32_t diff = immediate_size - 1;
208 body_.insert(body_.begin() + offset, diff, 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000209
Ben Murdochda12d292016-06-02 14:46:10 +0100210 for (size_t i = 0; i < local_indices_.size(); ++i) {
211 if (local_indices_[i] >= offset) {
212 local_indices_[i] += diff;
213 }
214 }
215 }
216 DCHECK(offset + immediate_size <= body_.size());
217 byte* p = &body_[offset];
218 EmitVarInt(&p, immediate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000219}
220
221
222void WasmFunctionBuilder::Exported(uint8_t flag) { exported_ = flag; }
223
224
225void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; }
226
227void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) {
228 name_.clear();
229 if (name_length > 0) {
230 for (int i = 0; i < name_length; i++) {
231 name_.push_back(*(name + i));
232 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000233 }
234}
235
236
237WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone,
238 WasmModuleBuilder* mb) const {
239 WasmFunctionEncoder* e =
240 new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_);
241 uint16_t* var_index = zone->NewArray<uint16_t>(locals_.size());
242 IndexVars(e, var_index);
243 if (body_.size() > 0) {
244 // TODO(titzer): iterate over local indexes, not the bytes.
245 const byte* start = &body_[0];
246 const byte* end = start + body_.size();
247 size_t local_index = 0;
248 for (size_t i = 0; i < body_.size();) {
249 if (local_index < local_indices_.size() &&
250 i == local_indices_[local_index]) {
251 int length = 0;
252 uint32_t index;
253 ReadUnsignedLEB128Operand(start + i, end, &length, &index);
254 uint16_t new_index = var_index[index];
255 const std::vector<uint8_t>& index_vec = UnsignedLEB128From(new_index);
256 for (size_t j = 0; j < index_vec.size(); j++) {
257 e->body_.push_back(index_vec.at(j));
258 }
259 i += length;
260 local_index++;
261 } else {
262 e->body_.push_back(*(start + i));
263 i++;
264 }
265 }
266 }
267 FunctionSig::Builder sig(zone, return_type_ == kAstStmt ? 0 : 1,
268 e->params_.size());
269 if (return_type_ != kAstStmt) {
270 sig.AddReturn(static_cast<LocalType>(return_type_));
271 }
272 for (size_t i = 0; i < e->params_.size(); i++) {
273 sig.AddParam(static_cast<LocalType>(e->params_[i]));
274 }
275 e->signature_index_ = mb->AddSignature(sig.Build());
276 e->name_.insert(e->name_.begin(), name_.begin(), name_.end());
277 return e;
278}
279
280
281void WasmFunctionBuilder::IndexVars(WasmFunctionEncoder* e,
282 uint16_t* var_index) const {
283 uint16_t param = 0;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100284 uint16_t i32 = 0;
285 uint16_t i64 = 0;
286 uint16_t f32 = 0;
287 uint16_t f64 = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000288 for (size_t i = 0; i < locals_.size(); i++) {
289 if (locals_.at(i).param_) {
290 param++;
291 } else if (locals_.at(i).type_ == kAstI32) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100292 i32++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293 } else if (locals_.at(i).type_ == kAstI64) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100294 i64++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000295 } else if (locals_.at(i).type_ == kAstF32) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100296 f32++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000297 } else if (locals_.at(i).type_ == kAstF64) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100298 f64++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000299 }
300 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100301 e->local_i32_count_ = i32;
302 e->local_i64_count_ = i64;
303 e->local_f32_count_ = f32;
304 e->local_f64_count_ = f64;
305 f64 = param + i32 + i64 + f32;
306 f32 = param + i32 + i64;
307 i64 = param + i32;
308 i32 = param;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000309 param = 0;
310 for (size_t i = 0; i < locals_.size(); i++) {
311 if (locals_.at(i).param_) {
312 e->params_.push_back(locals_.at(i).type_);
313 var_index[i] = param++;
314 } else if (locals_.at(i).type_ == kAstI32) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100315 var_index[i] = i32++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000316 } else if (locals_.at(i).type_ == kAstI64) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100317 var_index[i] = i64++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000318 } else if (locals_.at(i).type_ == kAstF32) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100319 var_index[i] = f32++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320 } else if (locals_.at(i).type_ == kAstF64) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100321 var_index[i] = f64++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000322 }
323 }
324}
325
326
327WasmFunctionEncoder::WasmFunctionEncoder(Zone* zone, LocalType return_type,
328 bool exported, bool external)
329 : params_(zone),
330 exported_(exported),
331 external_(external),
332 body_(zone),
333 name_(zone) {}
334
335
336uint32_t WasmFunctionEncoder::HeaderSize() const {
337 uint32_t size = 3;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000338 if (!external_) size += 2;
Ben Murdochda12d292016-06-02 14:46:10 +0100339 if (HasName()) {
340 uint32_t name_size = NameSize();
341 size += static_cast<uint32_t>(SizeOfVarInt(name_size)) + name_size;
342 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343 return size;
344}
345
346
347uint32_t WasmFunctionEncoder::BodySize(void) const {
Ben Murdochda12d292016-06-02 14:46:10 +0100348 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder
349 LocalDeclEncoder local_decl;
350 local_decl.AddLocals(local_i32_count_, kAstI32);
351 local_decl.AddLocals(local_i64_count_, kAstI64);
352 local_decl.AddLocals(local_f32_count_, kAstF32);
353 local_decl.AddLocals(local_f64_count_, kAstF64);
354
355 return external_ ? 0
356 : static_cast<uint32_t>(body_.size() + local_decl.Size());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000357}
358
359
360uint32_t WasmFunctionEncoder::NameSize() const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100361 return HasName() ? static_cast<uint32_t>(name_.size()) : 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000362}
363
364
365void WasmFunctionEncoder::Serialize(byte* buffer, byte** header,
366 byte** body) const {
367 uint8_t decl_bits = (exported_ ? kDeclFunctionExport : 0) |
368 (external_ ? kDeclFunctionImport : 0) |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000369 (HasName() ? kDeclFunctionName : 0);
370
371 EmitUint8(header, decl_bits);
372 EmitUint16(header, signature_index_);
373
374 if (HasName()) {
Ben Murdochda12d292016-06-02 14:46:10 +0100375 EmitVarInt(header, NameSize());
376 for (size_t i = 0; i < name_.size(); ++i) {
377 EmitUint8(header, name_[i]);
378 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000379 }
380
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000381
382 if (!external_) {
Ben Murdochda12d292016-06-02 14:46:10 +0100383 // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder
384 LocalDeclEncoder local_decl;
385 local_decl.AddLocals(local_i32_count_, kAstI32);
386 local_decl.AddLocals(local_i64_count_, kAstI64);
387 local_decl.AddLocals(local_f32_count_, kAstF32);
388 local_decl.AddLocals(local_f64_count_, kAstF64);
389
390 EmitUint16(header, static_cast<uint16_t>(body_.size() + local_decl.Size()));
391 (*header) += local_decl.Emit(*header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000392 if (body_.size() > 0) {
393 std::memcpy(*header, &body_[0], body_.size());
394 (*header) += body_.size();
395 }
396 }
397}
398
399
400WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data,
401 uint32_t size, uint32_t dest)
402 : data_(zone), dest_(dest) {
403 for (size_t i = 0; i < size; i++) {
404 data_.push_back(data[i]);
405 }
406}
407
408
409uint32_t WasmDataSegmentEncoder::HeaderSize() const {
410 static const int kDataSegmentSize = 13;
411 return kDataSegmentSize;
412}
413
414
415uint32_t WasmDataSegmentEncoder::BodySize() const {
416 return static_cast<uint32_t>(data_.size());
417}
418
419
420void WasmDataSegmentEncoder::Serialize(byte* buffer, byte** header,
421 byte** body) const {
Ben Murdochda12d292016-06-02 14:46:10 +0100422 EmitVarInt(header, dest_);
423 EmitVarInt(header, static_cast<uint32_t>(data_.size()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000424
Ben Murdochda12d292016-06-02 14:46:10 +0100425 std::memcpy(*header, &data_[0], data_.size());
426 (*header) += data_.size();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000427}
428
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000429WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
430 : zone_(zone),
431 signatures_(zone),
432 functions_(zone),
433 data_segments_(zone),
434 indirect_functions_(zone),
435 globals_(zone),
Ben Murdochda12d292016-06-02 14:46:10 +0100436 signature_map_(zone),
437 start_function_index_(-1) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000438
439uint16_t WasmModuleBuilder::AddFunction() {
440 functions_.push_back(new (zone_) WasmFunctionBuilder(zone_));
441 return static_cast<uint16_t>(functions_.size() - 1);
442}
443
444
445WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) {
446 if (functions_.size() > index) {
447 return functions_.at(index);
448 } else {
449 return nullptr;
450 }
451}
452
453
454void WasmModuleBuilder::AddDataSegment(WasmDataSegmentEncoder* data) {
455 data_segments_.push_back(data);
456}
457
458
Ben Murdoch097c5b22016-05-18 11:27:45 +0100459bool WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a,
460 FunctionSig* b) const {
461 if (a->return_count() < b->return_count()) return true;
462 if (a->return_count() > b->return_count()) return false;
463 if (a->parameter_count() < b->parameter_count()) return true;
464 if (a->parameter_count() > b->parameter_count()) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000465 for (size_t r = 0; r < a->return_count(); r++) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100466 if (a->GetReturn(r) < b->GetReturn(r)) return true;
467 if (a->GetReturn(r) > b->GetReturn(r)) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000468 }
469 for (size_t p = 0; p < a->parameter_count(); p++) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100470 if (a->GetParam(p) < b->GetParam(p)) return true;
471 if (a->GetParam(p) > b->GetParam(p)) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000472 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100473 return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000474}
475
476
477uint16_t WasmModuleBuilder::AddSignature(FunctionSig* sig) {
478 SignatureMap::iterator pos = signature_map_.find(sig);
479 if (pos != signature_map_.end()) {
480 return pos->second;
481 } else {
482 uint16_t index = static_cast<uint16_t>(signatures_.size());
483 signature_map_[sig] = index;
484 signatures_.push_back(sig);
485 return index;
486 }
487}
488
489
490void WasmModuleBuilder::AddIndirectFunction(uint16_t index) {
491 indirect_functions_.push_back(index);
492}
493
Ben Murdochda12d292016-06-02 14:46:10 +0100494void WasmModuleBuilder::MarkStartFunction(uint16_t index) {
495 start_function_index_ = index;
496}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000497
498WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) {
499 WasmModuleWriter* writer = new (zone) WasmModuleWriter(zone);
500 for (auto function : functions_) {
501 writer->functions_.push_back(function->Build(zone, this));
502 }
503 for (auto segment : data_segments_) {
504 writer->data_segments_.push_back(segment);
505 }
506 for (auto sig : signatures_) {
507 writer->signatures_.push_back(sig);
508 }
509 for (auto index : indirect_functions_) {
510 writer->indirect_functions_.push_back(index);
511 }
512 for (auto global : globals_) {
513 writer->globals_.push_back(global);
514 }
Ben Murdochda12d292016-06-02 14:46:10 +0100515 writer->start_function_index_ = start_function_index_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000516 return writer;
517}
518
519
520uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) {
521 globals_.push_back(std::make_pair(type, exported));
522 return static_cast<uint32_t>(globals_.size() - 1);
523}
524
525
526WasmModuleWriter::WasmModuleWriter(Zone* zone)
527 : functions_(zone),
528 data_segments_(zone),
529 signatures_(zone),
530 indirect_functions_(zone),
531 globals_(zone) {}
532
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000533struct Sizes {
534 size_t header_size;
535 size_t body_size;
536
537 size_t total() { return header_size + body_size; }
538
539 void Add(size_t header, size_t body) {
540 header_size += header;
541 body_size += body;
542 }
543
Ben Murdochda12d292016-06-02 14:46:10 +0100544 void AddSection(WasmSection::Code code, size_t other_size) {
545 Add(padded_varint + SizeOfVarInt(WasmSection::getNameLength(code)) +
546 WasmSection::getNameLength(code),
547 0);
548 if (other_size) Add(SizeOfVarInt(other_size), 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549 }
550};
551
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000552WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
553 Sizes sizes = {0, 0};
554
Ben Murdochda12d292016-06-02 14:46:10 +0100555 sizes.Add(2 * sizeof(uint32_t), 0); // header
556
557 sizes.AddSection(WasmSection::Code::Memory, 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000558 sizes.Add(kDeclMemorySize, 0);
Ben Murdochda12d292016-06-02 14:46:10 +0100559 TRACE("Size after memory: %u, %u\n", (unsigned)sizes.header_size,
560 (unsigned)sizes.body_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000561
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000562 if (globals_.size() > 0) {
Ben Murdochda12d292016-06-02 14:46:10 +0100563 sizes.AddSection(WasmSection::Code::Globals, globals_.size());
564 /* These globals never have names, so are always 3 bytes. */
565 sizes.Add(3 * globals_.size(), 0);
566 TRACE("Size after globals: %u, %u\n", (unsigned)sizes.header_size,
567 (unsigned)sizes.body_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000568 }
569
Ben Murdochda12d292016-06-02 14:46:10 +0100570 if (signatures_.size() > 0) {
571 sizes.AddSection(WasmSection::Code::Signatures, signatures_.size());
572 for (auto sig : signatures_) {
573 sizes.Add(
574 1 + SizeOfVarInt(sig->parameter_count()) + sig->parameter_count(), 0);
575 }
576 TRACE("Size after signatures: %u, %u\n", (unsigned)sizes.header_size,
577 (unsigned)sizes.body_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000578 }
579
Ben Murdochda12d292016-06-02 14:46:10 +0100580 if (functions_.size() > 0) {
581 sizes.AddSection(WasmSection::Code::Functions, functions_.size());
582 for (auto function : functions_) {
583 sizes.Add(function->HeaderSize() + function->BodySize(),
584 function->NameSize());
585 }
586 TRACE("Size after functions: %u, %u\n", (unsigned)sizes.header_size,
587 (unsigned)sizes.body_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000588 }
589
Ben Murdochda12d292016-06-02 14:46:10 +0100590 if (start_function_index_ >= 0) {
591 sizes.AddSection(WasmSection::Code::StartFunction, 0);
592 sizes.Add(SizeOfVarInt(start_function_index_), 0);
593 TRACE("Size after start: %u, %u\n", (unsigned)sizes.header_size,
594 (unsigned)sizes.body_size);
595 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000596
Ben Murdochda12d292016-06-02 14:46:10 +0100597 if (data_segments_.size() > 0) {
598 sizes.AddSection(WasmSection::Code::DataSegments, data_segments_.size());
599 for (auto segment : data_segments_) {
600 sizes.Add(segment->HeaderSize(), segment->BodySize());
601 }
602 TRACE("Size after data segments: %u, %u\n", (unsigned)sizes.header_size,
603 (unsigned)sizes.body_size);
604 }
605
606 if (indirect_functions_.size() > 0) {
607 sizes.AddSection(WasmSection::Code::FunctionTable,
608 indirect_functions_.size());
609 for (auto function_index : indirect_functions_) {
610 sizes.Add(SizeOfVarInt(function_index), 0);
611 }
612 TRACE("Size after indirect functions: %u, %u\n",
613 (unsigned)sizes.header_size, (unsigned)sizes.body_size);
614 }
615
616 if (sizes.body_size > 0) {
617 sizes.AddSection(WasmSection::Code::End, 0);
618 TRACE("Size after end: %u, %u\n", (unsigned)sizes.header_size,
619 (unsigned)sizes.body_size);
620 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000621
622 ZoneVector<uint8_t> buffer_vector(sizes.total(), zone);
623 byte* buffer = &buffer_vector[0];
624 byte* header = buffer;
625 byte* body = buffer + sizes.header_size;
626
Ben Murdochda12d292016-06-02 14:46:10 +0100627 // -- emit magic -------------------------------------------------------------
628 TRACE("emit magic\n");
629 EmitUint32(&header, kWasmMagic);
630 EmitUint32(&header, kWasmVersion);
631
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000632 // -- emit memory declaration ------------------------------------------------
Ben Murdochda12d292016-06-02 14:46:10 +0100633 {
634 byte* section = EmitSection(WasmSection::Code::Memory, &header);
635 EmitVarInt(&header, 16); // min memory size
636 EmitVarInt(&header, 16); // max memory size
637 EmitUint8(&header, 0); // memory export
638 static_assert(kDeclMemorySize == 3, "memory size must match emit above");
639 FixupSection(section, header);
640 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000641
642 // -- emit globals -----------------------------------------------------------
643 if (globals_.size() > 0) {
Ben Murdochda12d292016-06-02 14:46:10 +0100644 byte* section = EmitSection(WasmSection::Code::Globals, &header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000645 EmitVarInt(&header, globals_.size());
646
647 for (auto global : globals_) {
Ben Murdochda12d292016-06-02 14:46:10 +0100648 EmitVarInt(&header, 0); // Length of the global name.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000649 EmitUint8(&header, WasmOpcodes::MemTypeCodeFor(global.first));
650 EmitUint8(&header, global.second);
651 }
Ben Murdochda12d292016-06-02 14:46:10 +0100652 FixupSection(section, header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000653 }
654
655 // -- emit signatures --------------------------------------------------------
656 if (signatures_.size() > 0) {
Ben Murdochda12d292016-06-02 14:46:10 +0100657 byte* section = EmitSection(WasmSection::Code::Signatures, &header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000658 EmitVarInt(&header, signatures_.size());
659
660 for (FunctionSig* sig : signatures_) {
Ben Murdochda12d292016-06-02 14:46:10 +0100661 EmitVarInt(&header, sig->parameter_count());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000662 if (sig->return_count() > 0) {
663 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetReturn()));
664 } else {
665 EmitUint8(&header, kLocalVoid);
666 }
667 for (size_t j = 0; j < sig->parameter_count(); j++) {
668 EmitUint8(&header, WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j)));
669 }
670 }
Ben Murdochda12d292016-06-02 14:46:10 +0100671 FixupSection(section, header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000672 }
673
674 // -- emit functions ---------------------------------------------------------
675 if (functions_.size() > 0) {
Ben Murdochda12d292016-06-02 14:46:10 +0100676 byte* section = EmitSection(WasmSection::Code::Functions, &header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000677 EmitVarInt(&header, functions_.size());
678
679 for (auto func : functions_) {
680 func->Serialize(buffer, &header, &body);
681 }
Ben Murdochda12d292016-06-02 14:46:10 +0100682 FixupSection(section, header);
683 }
684
685 // -- emit start function index ----------------------------------------------
686 if (start_function_index_ >= 0) {
687 byte* section = EmitSection(WasmSection::Code::StartFunction, &header);
688 EmitVarInt(&header, start_function_index_);
689 FixupSection(section, header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000690 }
691
692 // -- emit data segments -----------------------------------------------------
693 if (data_segments_.size() > 0) {
Ben Murdochda12d292016-06-02 14:46:10 +0100694 byte* section = EmitSection(WasmSection::Code::DataSegments, &header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000695 EmitVarInt(&header, data_segments_.size());
696
697 for (auto segment : data_segments_) {
698 segment->Serialize(buffer, &header, &body);
699 }
Ben Murdochda12d292016-06-02 14:46:10 +0100700 FixupSection(section, header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000701 }
702
703 // -- emit function table ----------------------------------------------------
704 if (indirect_functions_.size() > 0) {
Ben Murdochda12d292016-06-02 14:46:10 +0100705 byte* section = EmitSection(WasmSection::Code::FunctionTable, &header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000706 EmitVarInt(&header, indirect_functions_.size());
707
708 for (auto index : indirect_functions_) {
Ben Murdochda12d292016-06-02 14:46:10 +0100709 EmitVarInt(&header, index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000710 }
Ben Murdochda12d292016-06-02 14:46:10 +0100711 FixupSection(section, header);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000712 }
713
Ben Murdochda12d292016-06-02 14:46:10 +0100714 if (sizes.body_size > 0) {
715 byte* section = EmitSection(WasmSection::Code::End, &header);
716 FixupSection(section, header);
717 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000718
719 return new (zone) WasmModuleIndex(buffer, buffer + sizes.total());
720}
721
722
723std::vector<uint8_t> UnsignedLEB128From(uint32_t result) {
724 std::vector<uint8_t> output;
725 uint8_t next = 0;
726 int shift = 0;
727 do {
728 next = static_cast<uint8_t>(result >> shift);
729 if (((result >> shift) & 0xFFFFFF80) != 0) {
730 next = next | 0x80;
731 }
732 output.push_back(next);
733 shift += 7;
734 } while ((next & 0x80) != 0);
735 return output;
736}
737} // namespace wasm
738} // namespace internal
739} // namespace v8