| 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/zone-containers.h" | 9 #include "src/zone/zone-containers.h" |
| 10 | 10 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 FunctionSig* sig) { | 248 FunctionSig* sig) { |
| 249 imports_.push_back({AddSignature(sig), name, name_length}); | 249 imports_.push_back({AddSignature(sig), name, name_length}); |
| 250 return static_cast<uint32_t>(imports_.size() - 1); | 250 return static_cast<uint32_t>(imports_.size() - 1); |
| 251 } | 251 } |
| 252 | 252 |
| 253 void WasmModuleBuilder::MarkStartFunction(WasmFunctionBuilder* function) { | 253 void WasmModuleBuilder::MarkStartFunction(WasmFunctionBuilder* function) { |
| 254 start_function_index_ = function->func_index(); | 254 start_function_index_ = function->func_index(); |
| 255 } | 255 } |
| 256 | 256 |
| 257 uint32_t WasmModuleBuilder::AddGlobal(LocalType type, bool exported, | 257 uint32_t WasmModuleBuilder::AddGlobal(LocalType type, bool exported, |
| 258 bool mutability) { | 258 bool mutability, |
| 259 globals_.push_back({type, exported, mutability}); | 259 const WasmInitExpr& init) { |
| 260 globals_.push_back({type, exported, mutability, init}); |
| 260 return static_cast<uint32_t>(globals_.size() - 1); | 261 return static_cast<uint32_t>(globals_.size() - 1); |
| 261 } | 262 } |
| 262 | 263 |
| 263 void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const { | 264 void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const { |
| 264 uint32_t exports = 0; | 265 uint32_t exports = 0; |
| 265 | 266 |
| 266 // == Emit magic ============================================================= | 267 // == Emit magic ============================================================= |
| 267 TRACE("emit magic\n"); | 268 TRACE("emit magic\n"); |
| 268 buffer.write_u32(kWasmMagic); | 269 buffer.write_u32(kWasmMagic); |
| 269 buffer.write_u32(kWasmVersion); | 270 buffer.write_u32(kWasmVersion); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 } | 338 } |
| 338 | 339 |
| 339 // == Emit globals =========================================================== | 340 // == Emit globals =========================================================== |
| 340 if (globals_.size() > 0) { | 341 if (globals_.size() > 0) { |
| 341 size_t start = EmitSection(kGlobalSectionCode, buffer); | 342 size_t start = EmitSection(kGlobalSectionCode, buffer); |
| 342 buffer.write_size(globals_.size()); | 343 buffer.write_size(globals_.size()); |
| 343 | 344 |
| 344 for (auto global : globals_) { | 345 for (auto global : globals_) { |
| 345 buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(global.type)); | 346 buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(global.type)); |
| 346 buffer.write_u8(global.mutability ? 1 : 0); | 347 buffer.write_u8(global.mutability ? 1 : 0); |
| 347 switch (global.type) { | 348 switch (global.init.kind) { |
| 348 case kAstI32: { | 349 case WasmInitExpr::kI32Const: { |
| 349 static const byte code[] = {WASM_I32V_1(0)}; | 350 DCHECK_EQ(kAstI32, global.type); |
| 351 const byte code[] = {WASM_I32V_5(global.init.val.i32_const)}; |
| 350 buffer.write(code, sizeof(code)); | 352 buffer.write(code, sizeof(code)); |
| 351 break; | 353 break; |
| 352 } | 354 } |
| 353 case kAstF32: { | 355 case WasmInitExpr::kI64Const: { |
| 354 static const byte code[] = {WASM_F32(0)}; | 356 DCHECK_EQ(kAstI64, global.type); |
| 357 const byte code[] = {WASM_I64V_10(global.init.val.i64_const)}; |
| 355 buffer.write(code, sizeof(code)); | 358 buffer.write(code, sizeof(code)); |
| 356 break; | 359 break; |
| 357 } | 360 } |
| 358 case kAstI64: { | 361 case WasmInitExpr::kF32Const: { |
| 359 static const byte code[] = {WASM_I64V_1(0)}; | 362 DCHECK_EQ(kAstF32, global.type); |
| 363 const byte code[] = {WASM_F32(global.init.val.f32_const)}; |
| 360 buffer.write(code, sizeof(code)); | 364 buffer.write(code, sizeof(code)); |
| 361 break; | 365 break; |
| 362 } | 366 } |
| 363 case kAstF64: { | 367 case WasmInitExpr::kF64Const: { |
| 364 static const byte code[] = {WASM_F64(0.0)}; | 368 DCHECK_EQ(kAstF64, global.type); |
| 369 const byte code[] = {WASM_F64(global.init.val.f64_const)}; |
| 365 buffer.write(code, sizeof(code)); | 370 buffer.write(code, sizeof(code)); |
| 366 break; | 371 break; |
| 367 } | 372 } |
| 368 default: | 373 case WasmInitExpr::kGlobalIndex: { |
| 369 UNREACHABLE(); | 374 const byte code[] = {kExprGetGlobal, |
| 375 U32V_5(global.init.val.global_index)}; |
| 376 buffer.write(code, sizeof(code)); |
| 377 break; |
| 378 } |
| 379 default: { |
| 380 // No initializer, emit a default value. |
| 381 switch (global.type) { |
| 382 case kAstI32: { |
| 383 const byte code[] = {WASM_I32V_1(0)}; |
| 384 buffer.write(code, sizeof(code)); |
| 385 break; |
| 386 } |
| 387 case kAstI64: { |
| 388 const byte code[] = {WASM_I64V_1(0)}; |
| 389 buffer.write(code, sizeof(code)); |
| 390 break; |
| 391 } |
| 392 case kAstF32: { |
| 393 const byte code[] = {WASM_F32(0.0)}; |
| 394 buffer.write(code, sizeof(code)); |
| 395 break; |
| 396 } |
| 397 case kAstF64: { |
| 398 const byte code[] = {WASM_F64(0.0)}; |
| 399 buffer.write(code, sizeof(code)); |
| 400 break; |
| 401 } |
| 402 default: |
| 403 UNREACHABLE(); |
| 404 } |
| 405 } |
| 370 } | 406 } |
| 371 buffer.write_u8(kExprEnd); | 407 buffer.write_u8(kExprEnd); |
| 372 } | 408 } |
| 373 FixupSection(buffer, start); | 409 FixupSection(buffer, start); |
| 374 } | 410 } |
| 375 | 411 |
| 376 // == emit exports =========================================================== | 412 // == emit exports =========================================================== |
| 377 if (exports > 0) { | 413 if (exports > 0) { |
| 378 size_t start = EmitSection(kExportSectionCode, buffer); | 414 size_t start = EmitSection(kExportSectionCode, buffer); |
| 379 buffer.write_u32v(exports); | 415 buffer.write_u32v(exports); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 function->name_.size()); | 486 function->name_.size()); |
| 451 } | 487 } |
| 452 buffer.write_u8(0); | 488 buffer.write_u8(0); |
| 453 } | 489 } |
| 454 FixupSection(buffer, start); | 490 FixupSection(buffer, start); |
| 455 } | 491 } |
| 456 } | 492 } |
| 457 } // namespace wasm | 493 } // namespace wasm |
| 458 } // namespace internal | 494 } // namespace internal |
| 459 } // namespace v8 | 495 } // namespace v8 |
| OLD | NEW |