| OLD | NEW |
| (Empty) |
| 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/zone-containers.h" | |
| 10 | |
| 11 #include "src/wasm/ast-decoder.h" | |
| 12 #include "src/wasm/encoder.h" | |
| 13 #include "src/wasm/leb-helper.h" | |
| 14 #include "src/wasm/wasm-macro-gen.h" | |
| 15 #include "src/wasm/wasm-module.h" | |
| 16 #include "src/wasm/wasm-opcodes.h" | |
| 17 | |
| 18 #include "src/v8memory.h" | |
| 19 | |
| 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 | |
| 29 namespace v8 { | |
| 30 namespace internal { | |
| 31 namespace wasm { | |
| 32 | |
| 33 // Emit a section code and the size as a padded varint that can be patched | |
| 34 // later. | |
| 35 size_t EmitSection(WasmSectionCode code, ZoneBuffer& buffer) { | |
| 36 // Emit the section code. | |
| 37 buffer.write_u8(code); | |
| 38 | |
| 39 // Emit a placeholder for the length. | |
| 40 return buffer.reserve_u32v(); | |
| 41 } | |
| 42 | |
| 43 // Patch the size of a section after it's finished. | |
| 44 void FixupSection(ZoneBuffer& buffer, size_t start) { | |
| 45 buffer.patch_u32v(start, static_cast<uint32_t>(buffer.offset() - start - | |
| 46 kPaddedVarInt32Size)); | |
| 47 } | |
| 48 | |
| 49 WasmFunctionBuilder::WasmFunctionBuilder(WasmModuleBuilder* builder) | |
| 50 : builder_(builder), | |
| 51 locals_(builder->zone()), | |
| 52 signature_index_(0), | |
| 53 exported_(0), | |
| 54 func_index_(static_cast<uint32_t>(builder->imports_.size() + | |
| 55 builder->functions_.size())), | |
| 56 body_(builder->zone()), | |
| 57 name_(builder->zone()), | |
| 58 i32_temps_(builder->zone()), | |
| 59 i64_temps_(builder->zone()), | |
| 60 f32_temps_(builder->zone()), | |
| 61 f64_temps_(builder->zone()) {} | |
| 62 | |
| 63 void WasmFunctionBuilder::EmitVarInt(uint32_t val) { | |
| 64 byte buffer[8]; | |
| 65 byte* ptr = buffer; | |
| 66 LEBHelper::write_u32v(&ptr, val); | |
| 67 for (byte* p = buffer; p < ptr; p++) { | |
| 68 body_.push_back(*p); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void WasmFunctionBuilder::SetSignature(FunctionSig* sig) { | |
| 73 DCHECK(!locals_.has_sig()); | |
| 74 locals_.set_sig(sig); | |
| 75 signature_index_ = builder_->AddSignature(sig); | |
| 76 } | |
| 77 | |
| 78 uint32_t WasmFunctionBuilder::AddLocal(LocalType type) { | |
| 79 DCHECK(locals_.has_sig()); | |
| 80 return locals_.AddLocals(1, type); | |
| 81 } | |
| 82 | |
| 83 void WasmFunctionBuilder::EmitGetLocal(uint32_t local_index) { | |
| 84 EmitWithVarInt(kExprGetLocal, local_index); | |
| 85 } | |
| 86 | |
| 87 void WasmFunctionBuilder::EmitSetLocal(uint32_t local_index) { | |
| 88 EmitWithVarInt(kExprSetLocal, local_index); | |
| 89 } | |
| 90 | |
| 91 void WasmFunctionBuilder::EmitTeeLocal(uint32_t local_index) { | |
| 92 EmitWithVarInt(kExprTeeLocal, local_index); | |
| 93 } | |
| 94 | |
| 95 void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size) { | |
| 96 for (size_t i = 0; i < code_size; ++i) { | |
| 97 body_.push_back(code[i]); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 void WasmFunctionBuilder::Emit(WasmOpcode opcode) { | |
| 102 body_.push_back(static_cast<byte>(opcode)); | |
| 103 } | |
| 104 | |
| 105 void WasmFunctionBuilder::EmitWithU8(WasmOpcode opcode, const byte immediate) { | |
| 106 body_.push_back(static_cast<byte>(opcode)); | |
| 107 body_.push_back(immediate); | |
| 108 } | |
| 109 | |
| 110 void WasmFunctionBuilder::EmitWithU8U8(WasmOpcode opcode, const byte imm1, | |
| 111 const byte imm2) { | |
| 112 body_.push_back(static_cast<byte>(opcode)); | |
| 113 body_.push_back(imm1); | |
| 114 body_.push_back(imm2); | |
| 115 } | |
| 116 | |
| 117 void WasmFunctionBuilder::EmitWithVarInt(WasmOpcode opcode, | |
| 118 uint32_t immediate) { | |
| 119 body_.push_back(static_cast<byte>(opcode)); | |
| 120 EmitVarInt(immediate); | |
| 121 } | |
| 122 | |
| 123 void WasmFunctionBuilder::EmitI32Const(int32_t value) { | |
| 124 // TODO(titzer): variable-length signed and unsigned i32 constants. | |
| 125 if (-128 <= value && value <= 127) { | |
| 126 EmitWithU8(kExprI8Const, static_cast<byte>(value)); | |
| 127 } else { | |
| 128 byte code[] = {WASM_I32V_5(value)}; | |
| 129 EmitCode(code, sizeof(code)); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 void WasmFunctionBuilder::SetExported() { exported_ = true; } | |
| 134 | |
| 135 void WasmFunctionBuilder::SetName(const char* name, int name_length) { | |
| 136 name_.clear(); | |
| 137 if (name_length > 0) { | |
| 138 for (int i = 0; i < name_length; ++i) { | |
| 139 name_.push_back(*(name + i)); | |
| 140 } | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const { | |
| 145 buffer.write_u32v(signature_index_); | |
| 146 } | |
| 147 | |
| 148 void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer) const { | |
| 149 if (exported_) { | |
| 150 buffer.write_size(name_.size()); | |
| 151 if (name_.size() > 0) { | |
| 152 buffer.write(reinterpret_cast<const byte*>(&name_[0]), name_.size()); | |
| 153 } | |
| 154 buffer.write_u8(kExternalFunction); | |
| 155 buffer.write_u32v(func_index_); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 void WasmFunctionBuilder::WriteBody(ZoneBuffer& buffer) const { | |
| 160 size_t locals_size = locals_.Size(); | |
| 161 buffer.write_size(locals_size + body_.size()); | |
| 162 buffer.EnsureSpace(locals_size); | |
| 163 byte** ptr = buffer.pos_ptr(); | |
| 164 locals_.Emit(*ptr); | |
| 165 (*ptr) += locals_size; // UGLY: manual bump of position pointer | |
| 166 if (body_.size() > 0) { | |
| 167 buffer.write(&body_[0], body_.size()); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data, | |
| 172 uint32_t size, uint32_t dest) | |
| 173 : data_(zone), dest_(dest) { | |
| 174 for (size_t i = 0; i < size; ++i) { | |
| 175 data_.push_back(data[i]); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 void WasmDataSegmentEncoder::Write(ZoneBuffer& buffer) const { | |
| 180 buffer.write_u8(0); // linear memory zero | |
| 181 buffer.write_u8(kExprI32Const); | |
| 182 buffer.write_u32v(dest_); | |
| 183 buffer.write_u8(kExprEnd); | |
| 184 buffer.write_u32v(static_cast<uint32_t>(data_.size())); | |
| 185 buffer.write(&data_[0], data_.size()); | |
| 186 } | |
| 187 | |
| 188 WasmModuleBuilder::WasmModuleBuilder(Zone* zone) | |
| 189 : zone_(zone), | |
| 190 signatures_(zone), | |
| 191 imports_(zone), | |
| 192 functions_(zone), | |
| 193 data_segments_(zone), | |
| 194 indirect_functions_(zone), | |
| 195 globals_(zone), | |
| 196 signature_map_(zone), | |
| 197 start_function_index_(-1) {} | |
| 198 | |
| 199 WasmFunctionBuilder* WasmModuleBuilder::AddFunction(FunctionSig* sig) { | |
| 200 functions_.push_back(new (zone_) WasmFunctionBuilder(this)); | |
| 201 // Add the signature if one was provided here. | |
| 202 if (sig) functions_.back()->SetSignature(sig); | |
| 203 return functions_.back(); | |
| 204 } | |
| 205 | |
| 206 void WasmModuleBuilder::AddDataSegment(WasmDataSegmentEncoder* data) { | |
| 207 data_segments_.push_back(data); | |
| 208 } | |
| 209 | |
| 210 bool WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a, | |
| 211 FunctionSig* b) const { | |
| 212 if (a->return_count() < b->return_count()) return true; | |
| 213 if (a->return_count() > b->return_count()) return false; | |
| 214 if (a->parameter_count() < b->parameter_count()) return true; | |
| 215 if (a->parameter_count() > b->parameter_count()) return false; | |
| 216 for (size_t r = 0; r < a->return_count(); r++) { | |
| 217 if (a->GetReturn(r) < b->GetReturn(r)) return true; | |
| 218 if (a->GetReturn(r) > b->GetReturn(r)) return false; | |
| 219 } | |
| 220 for (size_t p = 0; p < a->parameter_count(); p++) { | |
| 221 if (a->GetParam(p) < b->GetParam(p)) return true; | |
| 222 if (a->GetParam(p) > b->GetParam(p)) return false; | |
| 223 } | |
| 224 return false; | |
| 225 } | |
| 226 | |
| 227 uint32_t WasmModuleBuilder::AddSignature(FunctionSig* sig) { | |
| 228 SignatureMap::iterator pos = signature_map_.find(sig); | |
| 229 if (pos != signature_map_.end()) { | |
| 230 return pos->second; | |
| 231 } else { | |
| 232 uint32_t index = static_cast<uint32_t>(signatures_.size()); | |
| 233 signature_map_[sig] = index; | |
| 234 signatures_.push_back(sig); | |
| 235 return index; | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 void WasmModuleBuilder::AddIndirectFunction(uint32_t index) { | |
| 240 indirect_functions_.push_back(index); | |
| 241 } | |
| 242 | |
| 243 uint32_t WasmModuleBuilder::AddImport(const char* name, int name_length, | |
| 244 FunctionSig* sig) { | |
| 245 DCHECK_EQ(0, functions_.size()); // imports must be added before functions! | |
| 246 imports_.push_back({AddSignature(sig), name, name_length}); | |
| 247 return static_cast<uint32_t>(imports_.size() - 1); | |
| 248 } | |
| 249 | |
| 250 void WasmModuleBuilder::MarkStartFunction(WasmFunctionBuilder* function) { | |
| 251 start_function_index_ = function->func_index(); | |
| 252 } | |
| 253 | |
| 254 uint32_t WasmModuleBuilder::AddGlobal(LocalType type, bool exported, | |
| 255 bool mutability) { | |
| 256 globals_.push_back(std::make_tuple(type, exported, mutability)); | |
| 257 return static_cast<uint32_t>(globals_.size() - 1); | |
| 258 } | |
| 259 | |
| 260 void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const { | |
| 261 uint32_t exports = 0; | |
| 262 | |
| 263 // == Emit magic ============================================================= | |
| 264 TRACE("emit magic\n"); | |
| 265 buffer.write_u32(kWasmMagic); | |
| 266 buffer.write_u32(kWasmVersion); | |
| 267 | |
| 268 // == Emit signatures ======================================================== | |
| 269 if (signatures_.size() > 0) { | |
| 270 size_t start = EmitSection(kTypeSectionCode, buffer); | |
| 271 buffer.write_size(signatures_.size()); | |
| 272 | |
| 273 for (FunctionSig* sig : signatures_) { | |
| 274 buffer.write_u8(kWasmFunctionTypeForm); | |
| 275 buffer.write_size(sig->parameter_count()); | |
| 276 for (size_t j = 0; j < sig->parameter_count(); j++) { | |
| 277 buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j))); | |
| 278 } | |
| 279 buffer.write_size(sig->return_count()); | |
| 280 for (size_t j = 0; j < sig->return_count(); j++) { | |
| 281 buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(sig->GetReturn(j))); | |
| 282 } | |
| 283 } | |
| 284 FixupSection(buffer, start); | |
| 285 } | |
| 286 | |
| 287 // == Emit imports =========================================================== | |
| 288 if (imports_.size() > 0) { | |
| 289 size_t start = EmitSection(kImportSectionCode, buffer); | |
| 290 buffer.write_size(imports_.size()); | |
| 291 for (auto import : imports_) { | |
| 292 buffer.write_u32v(import.name_length); // module name length | |
| 293 buffer.write(reinterpret_cast<const byte*>(import.name), // module name | |
| 294 import.name_length); | |
| 295 buffer.write_u32v(0); // field name length | |
| 296 buffer.write_u8(kExternalFunction); | |
| 297 buffer.write_u32v(import.sig_index); | |
| 298 } | |
| 299 FixupSection(buffer, start); | |
| 300 } | |
| 301 | |
| 302 // == Emit function signatures =============================================== | |
| 303 bool has_names = false; | |
| 304 if (functions_.size() > 0) { | |
| 305 size_t start = EmitSection(kFunctionSectionCode, buffer); | |
| 306 buffer.write_size(functions_.size()); | |
| 307 for (auto function : functions_) { | |
| 308 function->WriteSignature(buffer); | |
| 309 if (function->exported()) exports++; | |
| 310 if (function->name_.size() > 0) has_names = true; | |
| 311 } | |
| 312 FixupSection(buffer, start); | |
| 313 } | |
| 314 | |
| 315 // == emit function table ==================================================== | |
| 316 if (indirect_functions_.size() > 0) { | |
| 317 size_t start = EmitSection(kTableSectionCode, buffer); | |
| 318 buffer.write_u8(1); // table count | |
| 319 buffer.write_u8(kWasmAnyFunctionTypeForm); | |
| 320 buffer.write_u8(kResizableMaximumFlag); | |
| 321 buffer.write_size(indirect_functions_.size()); | |
| 322 buffer.write_size(indirect_functions_.size()); | |
| 323 FixupSection(buffer, start); | |
| 324 } | |
| 325 | |
| 326 // == emit memory declaration ================================================ | |
| 327 { | |
| 328 size_t start = EmitSection(kMemorySectionCode, buffer); | |
| 329 buffer.write_u8(1); // memory count | |
| 330 buffer.write_u32v(kResizableMaximumFlag); | |
| 331 buffer.write_u32v(16); // min memory size | |
| 332 buffer.write_u32v(16); // max memory size | |
| 333 FixupSection(buffer, start); | |
| 334 } | |
| 335 | |
| 336 // == Emit globals =========================================================== | |
| 337 if (globals_.size() > 0) { | |
| 338 size_t start = EmitSection(kGlobalSectionCode, buffer); | |
| 339 buffer.write_size(globals_.size()); | |
| 340 | |
| 341 for (auto global : globals_) { | |
| 342 static const int kLocalTypeIndex = 0; | |
| 343 static const int kMutabilityIndex = 2; | |
| 344 buffer.write_u8( | |
| 345 WasmOpcodes::LocalTypeCodeFor(std::get<kLocalTypeIndex>(global))); | |
| 346 buffer.write_u8(std::get<kMutabilityIndex>(global)); | |
| 347 switch (std::get<kLocalTypeIndex>(global)) { | |
| 348 case kAstI32: { | |
| 349 static const byte code[] = {WASM_I32V_1(0)}; | |
| 350 buffer.write(code, sizeof(code)); | |
| 351 break; | |
| 352 } | |
| 353 case kAstF32: { | |
| 354 static const byte code[] = {WASM_F32(0)}; | |
| 355 buffer.write(code, sizeof(code)); | |
| 356 break; | |
| 357 } | |
| 358 case kAstI64: { | |
| 359 static const byte code[] = {WASM_I64V_1(0)}; | |
| 360 buffer.write(code, sizeof(code)); | |
| 361 break; | |
| 362 } | |
| 363 case kAstF64: { | |
| 364 static const byte code[] = {WASM_F64(0.0)}; | |
| 365 buffer.write(code, sizeof(code)); | |
| 366 break; | |
| 367 } | |
| 368 default: | |
| 369 UNREACHABLE(); | |
| 370 } | |
| 371 buffer.write_u8(kExprEnd); | |
| 372 } | |
| 373 FixupSection(buffer, start); | |
| 374 } | |
| 375 | |
| 376 // == emit exports =========================================================== | |
| 377 if (exports > 0) { | |
| 378 size_t start = EmitSection(kExportSectionCode, buffer); | |
| 379 buffer.write_u32v(exports); | |
| 380 for (auto function : functions_) function->WriteExport(buffer); | |
| 381 FixupSection(buffer, start); | |
| 382 } | |
| 383 | |
| 384 // == emit start function index ============================================== | |
| 385 if (start_function_index_ >= 0) { | |
| 386 size_t start = EmitSection(kStartSectionCode, buffer); | |
| 387 buffer.write_u32v(start_function_index_); | |
| 388 FixupSection(buffer, start); | |
| 389 } | |
| 390 | |
| 391 // == emit function table elements =========================================== | |
| 392 if (indirect_functions_.size() > 0) { | |
| 393 size_t start = EmitSection(kElementSectionCode, buffer); | |
| 394 buffer.write_u8(1); // count of entries | |
| 395 buffer.write_u8(0); // table index | |
| 396 buffer.write_u8(kExprI32Const); // offset | |
| 397 buffer.write_u32v(0); | |
| 398 buffer.write_u8(kExprEnd); | |
| 399 buffer.write_size(indirect_functions_.size()); // element count | |
| 400 | |
| 401 for (auto index : indirect_functions_) { | |
| 402 buffer.write_u32v(index); | |
| 403 } | |
| 404 | |
| 405 FixupSection(buffer, start); | |
| 406 } | |
| 407 | |
| 408 // == emit code ============================================================== | |
| 409 if (functions_.size() > 0) { | |
| 410 size_t start = EmitSection(kCodeSectionCode, buffer); | |
| 411 buffer.write_size(functions_.size()); | |
| 412 for (auto function : functions_) { | |
| 413 function->WriteBody(buffer); | |
| 414 } | |
| 415 FixupSection(buffer, start); | |
| 416 } | |
| 417 | |
| 418 // == emit data segments ===================================================== | |
| 419 if (data_segments_.size() > 0) { | |
| 420 size_t start = EmitSection(kDataSectionCode, buffer); | |
| 421 buffer.write_size(data_segments_.size()); | |
| 422 | |
| 423 for (auto segment : data_segments_) { | |
| 424 segment->Write(buffer); | |
| 425 } | |
| 426 FixupSection(buffer, start); | |
| 427 } | |
| 428 | |
| 429 // == Emit names ============================================================= | |
| 430 if (has_names) { | |
| 431 // Emit the section code. | |
| 432 buffer.write_u8(kUnknownSectionCode); | |
| 433 // Emit a placeholder for the length. | |
| 434 size_t start = buffer.reserve_u32v(); | |
| 435 // Emit the section string. | |
| 436 buffer.write_size(4); | |
| 437 buffer.write(reinterpret_cast<const byte*>("name"), 4); | |
| 438 // Emit the names. | |
| 439 buffer.write_size(functions_.size()); | |
| 440 for (auto function : functions_) { | |
| 441 buffer.write_size(function->name_.size()); | |
| 442 if (function->name_.size() > 0) { | |
| 443 buffer.write(reinterpret_cast<const byte*>(&function->name_[0]), | |
| 444 function->name_.size()); | |
| 445 } | |
| 446 buffer.write_u8(0); | |
| 447 } | |
| 448 FixupSection(buffer, start); | |
| 449 } | |
| 450 } | |
| 451 } // namespace wasm | |
| 452 } // namespace internal | |
| 453 } // namespace v8 | |
| OLD | NEW |