Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 13 | #include "src/wasm/leb-helper.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 14 | #include "src/wasm/wasm-macro-gen.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | #include "src/wasm/wasm-module.h" |
| 16 | #include "src/wasm/wasm-opcodes.h" |
| 17 | |
| 18 | #include "src/v8memory.h" |
| 19 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 20 | #if DEBUG |
| 21 | #define TRACE(...) \ |
| 22 | do { \ |
| 23 | if (FLAG_trace_wasm_encoder) PrintF(__VA_ARGS__); \ |
| 24 | } while (false) |
| 25 | #else |
| 26 | #define TRACE(...) |
| 27 | #endif |
| 28 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 29 | namespace v8 { |
| 30 | namespace internal { |
| 31 | namespace wasm { |
| 32 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 33 | // Emit a section name and the size as a padded varint that can be patched |
| 34 | // later. |
| 35 | size_t EmitSection(WasmSection::Code code, ZoneBuffer& buffer) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 36 | // Emit the section name. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 37 | const char* name = WasmSection::getName(code); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 38 | TRACE("emit section: %s\n", name); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 39 | size_t length = WasmSection::getNameLength(code); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 40 | buffer.write_size(length); // Section name string size. |
| 41 | buffer.write(reinterpret_cast<const byte*>(name), length); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 42 | |
| 43 | // Emit a placeholder for the length. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 44 | return buffer.reserve_u32v(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 45 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 46 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 47 | // Patch the size of a section after it's finished. |
| 48 | void FixupSection(ZoneBuffer& buffer, size_t start) { |
| 49 | buffer.patch_u32v(start, static_cast<uint32_t>(buffer.offset() - start - |
| 50 | kPaddedVarInt32Size)); |
| 51 | } |
| 52 | |
| 53 | WasmFunctionBuilder::WasmFunctionBuilder(WasmModuleBuilder* builder) |
| 54 | : builder_(builder), |
| 55 | locals_(builder->zone()), |
| 56 | signature_index_(0), |
| 57 | exported_(0), |
| 58 | body_(builder->zone()), |
| 59 | name_(builder->zone()) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 60 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 61 | void WasmFunctionBuilder::EmitVarInt(uint32_t val) { |
| 62 | byte buffer[8]; |
| 63 | byte* ptr = buffer; |
| 64 | LEBHelper::write_u32v(&ptr, val); |
| 65 | for (byte* p = buffer; p < ptr; p++) { |
| 66 | body_.push_back(*p); |
| 67 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 70 | void WasmFunctionBuilder::SetSignature(FunctionSig* sig) { |
| 71 | DCHECK(!locals_.has_sig()); |
| 72 | locals_.set_sig(sig); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 73 | signature_index_ = builder_->AddSignature(sig); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 76 | uint32_t WasmFunctionBuilder::AddLocal(LocalType type) { |
| 77 | DCHECK(locals_.has_sig()); |
| 78 | return locals_.AddLocals(1, type); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 81 | void WasmFunctionBuilder::EmitGetLocal(uint32_t local_index) { |
| 82 | EmitWithVarInt(kExprGetLocal, local_index); |
| 83 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 84 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 85 | void WasmFunctionBuilder::EmitSetLocal(uint32_t local_index) { |
| 86 | EmitWithVarInt(kExprSetLocal, local_index); |
| 87 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | |
| 89 | void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 90 | for (size_t i = 0; i < code_size; ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 91 | body_.push_back(code[i]); |
| 92 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 95 | void WasmFunctionBuilder::Emit(WasmOpcode opcode) { |
| 96 | body_.push_back(static_cast<byte>(opcode)); |
| 97 | } |
| 98 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | void WasmFunctionBuilder::EmitWithU8(WasmOpcode opcode, const byte immediate) { |
| 100 | body_.push_back(static_cast<byte>(opcode)); |
| 101 | body_.push_back(immediate); |
| 102 | } |
| 103 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 104 | void WasmFunctionBuilder::EmitWithU8U8(WasmOpcode opcode, const byte imm1, |
| 105 | const byte imm2) { |
| 106 | body_.push_back(static_cast<byte>(opcode)); |
| 107 | body_.push_back(imm1); |
| 108 | body_.push_back(imm2); |
| 109 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 110 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 111 | void WasmFunctionBuilder::EmitWithVarInt(WasmOpcode opcode, |
| 112 | uint32_t immediate) { |
| 113 | body_.push_back(static_cast<byte>(opcode)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 114 | EmitVarInt(immediate); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 117 | void WasmFunctionBuilder::EmitI32Const(int32_t value) { |
| 118 | // TODO(titzer): variable-length signed and unsigned i32 constants. |
| 119 | if (-128 <= value && value <= 127) { |
| 120 | EmitWithU8(kExprI8Const, static_cast<byte>(value)); |
| 121 | } else { |
| 122 | byte code[] = {WASM_I32V_5(value)}; |
| 123 | EmitCode(code, sizeof(code)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 124 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 127 | void WasmFunctionBuilder::SetExported() { exported_ = true; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 128 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 129 | void WasmFunctionBuilder::SetName(const char* name, int name_length) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | name_.clear(); |
| 131 | if (name_length > 0) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 132 | for (int i = 0; i < name_length; ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 133 | name_.push_back(*(name + i)); |
| 134 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 138 | void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const { |
| 139 | buffer.write_u32v(signature_index_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 142 | void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer, |
| 143 | uint32_t func_index) const { |
| 144 | if (exported_) { |
| 145 | buffer.write_u32v(func_index); |
| 146 | buffer.write_size(name_.size()); |
| 147 | if (name_.size() > 0) { |
| 148 | buffer.write(reinterpret_cast<const byte*>(&name_[0]), name_.size()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 149 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 150 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 151 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 152 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 153 | void WasmFunctionBuilder::WriteBody(ZoneBuffer& buffer) const { |
| 154 | size_t locals_size = locals_.Size(); |
| 155 | buffer.write_size(locals_size + body_.size()); |
| 156 | buffer.EnsureSpace(locals_size); |
| 157 | byte** ptr = buffer.pos_ptr(); |
| 158 | locals_.Emit(*ptr); |
| 159 | (*ptr) += locals_size; // UGLY: manual bump of position pointer |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 160 | if (body_.size() > 0) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 161 | buffer.write(&body_[0], body_.size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 165 | WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data, |
| 166 | uint32_t size, uint32_t dest) |
| 167 | : data_(zone), dest_(dest) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 168 | for (size_t i = 0; i < size; ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 169 | data_.push_back(data[i]); |
| 170 | } |
| 171 | } |
| 172 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 173 | void WasmDataSegmentEncoder::Write(ZoneBuffer& buffer) const { |
| 174 | buffer.write_u32v(dest_); |
| 175 | buffer.write_u32v(static_cast<uint32_t>(data_.size())); |
| 176 | buffer.write(&data_[0], data_.size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 179 | WasmModuleBuilder::WasmModuleBuilder(Zone* zone) |
| 180 | : zone_(zone), |
| 181 | signatures_(zone), |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 182 | imports_(zone), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 183 | functions_(zone), |
| 184 | data_segments_(zone), |
| 185 | indirect_functions_(zone), |
| 186 | globals_(zone), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 187 | signature_map_(zone), |
| 188 | start_function_index_(-1) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 189 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 190 | uint32_t WasmModuleBuilder::AddFunction() { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 191 | functions_.push_back(new (zone_) WasmFunctionBuilder(this)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 192 | return static_cast<uint32_t>(functions_.size() - 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 195 | WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) { |
| 196 | if (functions_.size() > index) { |
| 197 | return functions_.at(index); |
| 198 | } else { |
| 199 | return nullptr; |
| 200 | } |
| 201 | } |
| 202 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 203 | void WasmModuleBuilder::AddDataSegment(WasmDataSegmentEncoder* data) { |
| 204 | data_segments_.push_back(data); |
| 205 | } |
| 206 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 207 | bool WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a, |
| 208 | FunctionSig* b) const { |
| 209 | if (a->return_count() < b->return_count()) return true; |
| 210 | if (a->return_count() > b->return_count()) return false; |
| 211 | if (a->parameter_count() < b->parameter_count()) return true; |
| 212 | if (a->parameter_count() > b->parameter_count()) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 213 | for (size_t r = 0; r < a->return_count(); r++) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 214 | if (a->GetReturn(r) < b->GetReturn(r)) return true; |
| 215 | if (a->GetReturn(r) > b->GetReturn(r)) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 216 | } |
| 217 | for (size_t p = 0; p < a->parameter_count(); p++) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 218 | if (a->GetParam(p) < b->GetParam(p)) return true; |
| 219 | if (a->GetParam(p) > b->GetParam(p)) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 220 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 221 | return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 224 | uint32_t WasmModuleBuilder::AddSignature(FunctionSig* sig) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 225 | SignatureMap::iterator pos = signature_map_.find(sig); |
| 226 | if (pos != signature_map_.end()) { |
| 227 | return pos->second; |
| 228 | } else { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 229 | uint32_t index = static_cast<uint32_t>(signatures_.size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 230 | signature_map_[sig] = index; |
| 231 | signatures_.push_back(sig); |
| 232 | return index; |
| 233 | } |
| 234 | } |
| 235 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 236 | void WasmModuleBuilder::AddIndirectFunction(uint32_t index) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 237 | indirect_functions_.push_back(index); |
| 238 | } |
| 239 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 240 | uint32_t WasmModuleBuilder::AddImport(const char* name, int name_length, |
| 241 | FunctionSig* sig) { |
| 242 | imports_.push_back({AddSignature(sig), name, name_length}); |
| 243 | return static_cast<uint32_t>(imports_.size() - 1); |
| 244 | } |
| 245 | |
| 246 | void WasmModuleBuilder::MarkStartFunction(uint32_t index) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 247 | start_function_index_ = index; |
| 248 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 249 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 250 | uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) { |
| 251 | globals_.push_back(std::make_pair(type, exported)); |
| 252 | return static_cast<uint32_t>(globals_.size() - 1); |
| 253 | } |
| 254 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 255 | void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const { |
| 256 | uint32_t exports = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 257 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 258 | // == Emit magic ============================================================= |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 259 | TRACE("emit magic\n"); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 260 | buffer.write_u32(kWasmMagic); |
| 261 | buffer.write_u32(kWasmVersion); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 262 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 263 | // == Emit signatures ======================================================== |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 264 | if (signatures_.size() > 0) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 265 | size_t start = EmitSection(WasmSection::Code::Signatures, buffer); |
| 266 | buffer.write_size(signatures_.size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 267 | |
| 268 | for (FunctionSig* sig : signatures_) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 269 | buffer.write_u8(kWasmFunctionTypeForm); |
| 270 | buffer.write_size(sig->parameter_count()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 271 | for (size_t j = 0; j < sig->parameter_count(); j++) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 272 | buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j))); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 273 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 274 | buffer.write_size(sig->return_count()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 275 | for (size_t j = 0; j < sig->return_count(); j++) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 276 | buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(sig->GetReturn(j))); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 277 | } |
| 278 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 279 | FixupSection(buffer, start); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 282 | // == Emit globals =========================================================== |
| 283 | if (globals_.size() > 0) { |
| 284 | size_t start = EmitSection(WasmSection::Code::Globals, buffer); |
| 285 | buffer.write_size(globals_.size()); |
| 286 | |
| 287 | for (auto global : globals_) { |
| 288 | buffer.write_u32v(0); // Length of the global name. |
| 289 | buffer.write_u8(WasmOpcodes::MemTypeCodeFor(global.first)); |
| 290 | buffer.write_u8(global.second); |
| 291 | } |
| 292 | FixupSection(buffer, start); |
| 293 | } |
| 294 | |
| 295 | // == Emit imports =========================================================== |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 296 | if (imports_.size() > 0) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 297 | size_t start = EmitSection(WasmSection::Code::ImportTable, buffer); |
| 298 | buffer.write_size(imports_.size()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 299 | for (auto import : imports_) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 300 | buffer.write_u32v(import.sig_index); |
| 301 | buffer.write_u32v(import.name_length); |
| 302 | buffer.write(reinterpret_cast<const byte*>(import.name), |
| 303 | import.name_length); |
| 304 | buffer.write_u32v(0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 305 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 306 | FixupSection(buffer, start); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 309 | // == Emit function signatures =============================================== |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 310 | if (functions_.size() > 0) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 311 | size_t start = EmitSection(WasmSection::Code::FunctionSignatures, buffer); |
| 312 | buffer.write_size(functions_.size()); |
| 313 | for (auto function : functions_) { |
| 314 | function->WriteSignature(buffer); |
| 315 | if (function->exported()) exports++; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 316 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 317 | FixupSection(buffer, start); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 320 | // == emit function table ==================================================== |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 321 | if (indirect_functions_.size() > 0) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 322 | size_t start = EmitSection(WasmSection::Code::FunctionTable, buffer); |
| 323 | buffer.write_size(indirect_functions_.size()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 324 | |
| 325 | for (auto index : indirect_functions_) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 326 | buffer.write_u32v(index); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 327 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 328 | FixupSection(buffer, start); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 331 | // == emit memory declaration ================================================ |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 332 | { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 333 | size_t start = EmitSection(WasmSection::Code::Memory, buffer); |
| 334 | buffer.write_u32v(16); // min memory size |
| 335 | buffer.write_u32v(16); // max memory size |
| 336 | buffer.write_u8(0); // memory export |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 337 | static_assert(kDeclMemorySize == 3, "memory size must match emit above"); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 338 | FixupSection(buffer, start); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 341 | // == emit exports =========================================================== |
| 342 | if (exports > 0) { |
| 343 | size_t start = EmitSection(WasmSection::Code::ExportTable, buffer); |
| 344 | buffer.write_u32v(exports); |
| 345 | uint32_t index = 0; |
| 346 | for (auto function : functions_) { |
| 347 | function->WriteExport(buffer, index++); |
| 348 | } |
| 349 | FixupSection(buffer, start); |
| 350 | } |
| 351 | |
| 352 | // == emit start function index ============================================== |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 353 | if (start_function_index_ >= 0) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 354 | size_t start = EmitSection(WasmSection::Code::StartFunction, buffer); |
| 355 | buffer.write_u32v(start_function_index_); |
| 356 | FixupSection(buffer, start); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 359 | // == emit code ============================================================== |
| 360 | if (functions_.size() > 0) { |
| 361 | size_t start = EmitSection(WasmSection::Code::FunctionBodies, buffer); |
| 362 | buffer.write_size(functions_.size()); |
| 363 | for (auto function : functions_) { |
| 364 | function->WriteBody(buffer); |
| 365 | } |
| 366 | FixupSection(buffer, start); |
| 367 | } |
| 368 | |
| 369 | // == emit data segments ===================================================== |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 370 | if (data_segments_.size() > 0) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 371 | size_t start = EmitSection(WasmSection::Code::DataSegments, buffer); |
| 372 | buffer.write_size(data_segments_.size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 373 | |
| 374 | for (auto segment : data_segments_) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 375 | segment->Write(buffer); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 376 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 377 | FixupSection(buffer, start); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 378 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 379 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 380 | } // namespace wasm |
| 381 | } // namespace internal |
| 382 | } // namespace v8 |