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 const WasmInitExpr& init) { | 259 globals_.push_back({type, exported, mutability}); |
260 globals_.push_back({type, exported, mutability, init}); | |
261 return static_cast<uint32_t>(globals_.size() - 1); | 260 return static_cast<uint32_t>(globals_.size() - 1); |
262 } | 261 } |
263 | 262 |
264 void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const { | 263 void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const { |
265 uint32_t exports = 0; | 264 uint32_t exports = 0; |
266 | 265 |
267 // == Emit magic ============================================================= | 266 // == Emit magic ============================================================= |
268 TRACE("emit magic\n"); | 267 TRACE("emit magic\n"); |
269 buffer.write_u32(kWasmMagic); | 268 buffer.write_u32(kWasmMagic); |
270 buffer.write_u32(kWasmVersion); | 269 buffer.write_u32(kWasmVersion); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 } | 337 } |
339 | 338 |
340 // == Emit globals =========================================================== | 339 // == Emit globals =========================================================== |
341 if (globals_.size() > 0) { | 340 if (globals_.size() > 0) { |
342 size_t start = EmitSection(kGlobalSectionCode, buffer); | 341 size_t start = EmitSection(kGlobalSectionCode, buffer); |
343 buffer.write_size(globals_.size()); | 342 buffer.write_size(globals_.size()); |
344 | 343 |
345 for (auto global : globals_) { | 344 for (auto global : globals_) { |
346 buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(global.type)); | 345 buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(global.type)); |
347 buffer.write_u8(global.mutability ? 1 : 0); | 346 buffer.write_u8(global.mutability ? 1 : 0); |
348 switch (global.init.kind) { | 347 switch (global.type) { |
349 case WasmInitExpr::kI32Const: { | 348 case kAstI32: { |
350 DCHECK_EQ(kAstI32, global.type); | 349 static const byte code[] = {WASM_I32V_1(0)}; |
351 const byte code[] = {WASM_I32V_5(global.init.val.i32_const)}; | |
352 buffer.write(code, sizeof(code)); | 350 buffer.write(code, sizeof(code)); |
353 break; | 351 break; |
354 } | 352 } |
355 case WasmInitExpr::kI64Const: { | 353 case kAstF32: { |
356 DCHECK_EQ(kAstI64, global.type); | 354 static const byte code[] = {WASM_F32(0)}; |
357 const byte code[] = {WASM_I64V_10(global.init.val.i64_const)}; | |
358 buffer.write(code, sizeof(code)); | 355 buffer.write(code, sizeof(code)); |
359 break; | 356 break; |
360 } | 357 } |
361 case WasmInitExpr::kF32Const: { | 358 case kAstI64: { |
362 DCHECK_EQ(kAstF32, global.type); | 359 static const byte code[] = {WASM_I64V_1(0)}; |
363 const byte code[] = {WASM_F32(global.init.val.f32_const)}; | |
364 buffer.write(code, sizeof(code)); | 360 buffer.write(code, sizeof(code)); |
365 break; | 361 break; |
366 } | 362 } |
367 case WasmInitExpr::kF64Const: { | 363 case kAstF64: { |
368 DCHECK_EQ(kAstF64, global.type); | 364 static const byte code[] = {WASM_F64(0.0)}; |
369 const byte code[] = {WASM_F64(global.init.val.f64_const)}; | |
370 buffer.write(code, sizeof(code)); | 365 buffer.write(code, sizeof(code)); |
371 break; | 366 break; |
372 } | 367 } |
373 case WasmInitExpr::kGlobalIndex: { | 368 default: |
374 const byte code[] = {kExprGetGlobal, | 369 UNREACHABLE(); |
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 } | |
406 } | 370 } |
407 buffer.write_u8(kExprEnd); | 371 buffer.write_u8(kExprEnd); |
408 } | 372 } |
409 FixupSection(buffer, start); | 373 FixupSection(buffer, start); |
410 } | 374 } |
411 | 375 |
412 // == emit exports =========================================================== | 376 // == emit exports =========================================================== |
413 if (exports > 0) { | 377 if (exports > 0) { |
414 size_t start = EmitSection(kExportSectionCode, buffer); | 378 size_t start = EmitSection(kExportSectionCode, buffer); |
415 buffer.write_u32v(exports); | 379 buffer.write_u32v(exports); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 function->name_.size()); | 450 function->name_.size()); |
487 } | 451 } |
488 buffer.write_u8(0); | 452 buffer.write_u8(0); |
489 } | 453 } |
490 FixupSection(buffer, start); | 454 FixupSection(buffer, start); |
491 } | 455 } |
492 } | 456 } |
493 } // namespace wasm | 457 } // namespace wasm |
494 } // namespace internal | 458 } // namespace internal |
495 } // namespace v8 | 459 } // namespace v8 |
OLD | NEW |