wasm-compile.cc 73.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

#include <algorithm>

#include "include/v8.h"
12
#include "src/execution/isolate.h"
13 14
#include "src/objects/objects-inl.h"
#include "src/objects/objects.h"
15
#include "src/utils/ostreams.h"
16 17
#include "src/wasm/wasm-module-builder.h"
#include "src/wasm/wasm-module.h"
18
#include "src/wasm/wasm-opcodes-inl.h"
19
#include "test/common/wasm/flag-utils.h"
20 21 22
#include "test/common/wasm/test-signatures.h"
#include "test/common/wasm/wasm-module-runner.h"
#include "test/fuzzer/fuzzer-support.h"
23
#include "test/fuzzer/wasm-fuzzer-common.h"
24

25 26 27 28
namespace v8 {
namespace internal {
namespace wasm {
namespace fuzzer {
29

30 31
namespace {

32
constexpr int kMaxFunctions = 4;
33
constexpr int kMaxGlobals = 64;
34 35
constexpr int kMaxParameters = 15;
constexpr int kMaxReturns = 15;
36

37
class DataRange {
38
  Vector<const uint8_t> data_;
39 40

 public:
41
  explicit DataRange(Vector<const uint8_t> data) : data_(data) {}
42

43 44 45 46
  // Don't accidentally pass DataRange by value. This will reuse bytes and might
  // lead to OOM because the end might not be reached.
  // Define move constructor and move assignment, disallow copy constructor and
  // copy assignment (below).
47 48 49 50
  DataRange(DataRange&& other) V8_NOEXCEPT : DataRange(other.data_) {
    other.data_ = {};
  }
  DataRange& operator=(DataRange&& other) V8_NOEXCEPT {
51
    data_ = other.data_;
52
    other.data_ = {};
53
    return *this;
54 55
  }

56
  size_t size() const { return data_.size(); }
57 58

  DataRange split() {
59 60
    uint16_t num_bytes = get<uint16_t>() % std::max(size_t{1}, data_.size());
    DataRange split(data_.SubVector(0, num_bytes));
61 62
    data_ += num_bytes;
    return split;
63 64
  }

65
  template <typename T, size_t max_bytes = sizeof(T)>
66
  T get() {
67 68 69 70 71 72 73
    // DISABLE FOR BOOL
    // The -O3 on release will break the result. This creates a different
    // observable side effect when invoking get<bool> between debug and release
    // version, which eventually makes the code output different as well as
    // raising various unrecoverable errors on runtime. It is caused by
    // undefined behavior of assigning boolean via memcpy from randomized bytes.
    STATIC_ASSERT(!(std::is_same<T, bool>::value));
74
    STATIC_ASSERT(max_bytes <= sizeof(T));
75 76 77 78 79
    // We want to support the case where we have less than sizeof(T) bytes
    // remaining in the slice. For example, if we emit an i32 constant, it's
    // okay if we don't have a full four bytes available, we'll just use what
    // we have. We aren't concerned about endianness because we are generating
    // arbitrary expressions.
80
    const size_t num_bytes = std::min(max_bytes, data_.size());
81
    T result = T();
82
    memcpy(&result, data_.begin(), num_bytes);
83 84
    data_ += num_bytes;
    return result;
85
  }
86 87

  DISALLOW_COPY_AND_ASSIGN(DataRange);
88 89
};

90
ValueType GetValueType(DataRange* data) {
91 92 93
  // TODO(v8:8460): We do not add kWasmS128 here yet because this method is used
  // to generate globals, and since we do not have v128.const yet, there is no
  // way to specify an initial value a global of this type.
94
  switch (data->get<uint8_t>() % 4) {
95 96 97 98 99 100 101 102 103 104 105 106
    case 0:
      return kWasmI32;
    case 1:
      return kWasmI64;
    case 2:
      return kWasmF32;
    case 3:
      return kWasmF64;
  }
  UNREACHABLE();
}

107
class WasmGenerator {
108
  template <WasmOpcode Op, ValueType::Kind... Args>
109
  void op(DataRange* data) {
110 111
    Generate<Args...>(data);
    builder_->Emit(Op);
112 113
  }

114 115
  class BlockScope {
   public:
116
    BlockScope(WasmGenerator* gen, WasmOpcode block_type,
117 118 119
               Vector<const ValueType> param_types,
               Vector<const ValueType> result_types,
               Vector<const ValueType> br_types)
120
        : gen_(gen) {
121
      gen->blocks_.emplace_back(br_types.begin(), br_types.end());
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      if (param_types.size() == 0 && result_types.size() == 0) {
        gen->builder_->EmitWithU8(block_type, kWasmStmt.value_type_code());
        return;
      }
      if (param_types.size() == 0 && result_types.size() == 1) {
        gen->builder_->EmitWithU8(block_type,
                                  result_types[0].value_type_code());
        return;
      }
      // Multi-value block.
      Zone* zone = gen->builder_->builder()->zone();
      FunctionSig::Builder builder(zone, result_types.size(),
                                   param_types.size());
      for (auto& type : param_types) {
        DCHECK_NE(type, kWasmStmt);
        builder.AddParam(type);
      }
      for (auto& type : result_types) {
        DCHECK_NE(type, kWasmStmt);
        builder.AddReturn(type);
      }
      FunctionSig* sig = builder.Build();
      int sig_id = gen->builder_->builder()->AddSignature(sig);
      gen->builder_->EmitWithI32V(block_type, sig_id);
146 147 148 149 150 151 152 153 154 155 156
    }

    ~BlockScope() {
      gen_->builder_->Emit(kExprEnd);
      gen_->blocks_.pop_back();
    }

   private:
    WasmGenerator* const gen_;
  };

157 158
  void block(Vector<const ValueType> param_types,
             Vector<const ValueType> return_types, DataRange* data) {
159 160 161 162 163
    BlockScope block_scope(this, kExprBlock, param_types, return_types,
                           return_types);
    ConsumeAndGenerate(param_types, return_types, data);
  }

164
  template <ValueType::Kind T>
165
  void block(DataRange* data) {
166
    block({}, VectorOf({ValueType::Primitive(T)}), data);
167 168
  }

169 170
  void loop(Vector<const ValueType> param_types,
            Vector<const ValueType> return_types, DataRange* data) {
171 172 173 174 175
    BlockScope block_scope(this, kExprLoop, param_types, return_types,
                           param_types);
    ConsumeAndGenerate(param_types, return_types, data);
  }

176
  template <ValueType::Kind T>
177
  void loop(DataRange* data) {
178
    loop({}, VectorOf({ValueType::Primitive(T)}), data);
179
  }
180

181 182
  enum IfType { kIf, kIfElse };

183 184
  void if_(Vector<const ValueType> param_types,
           Vector<const ValueType> return_types, IfType type, DataRange* data) {
185 186 187 188 189 190 191 192 193 194 195 196
    // One-armed "if" are only valid if the input and output types are the same.
    DCHECK_IMPLIES(type == kIf, param_types == return_types);
    Generate(kWasmI32, data);
    BlockScope block_scope(this, kExprIf, param_types, return_types,
                           return_types);
    ConsumeAndGenerate(param_types, return_types, data);
    if (type == kIfElse) {
      builder_->Emit(kExprElse);
      ConsumeAndGenerate(param_types, return_types, data);
    }
  }

197
  template <ValueType::Kind T, IfType type>
198
  void if_(DataRange* data) {
199
    static_assert(T == ValueType::kStmt || type == kIfElse,
200
                  "if without else cannot produce a value");
201 202 203 204
    if_({},
        T == ValueType::kStmt ? Vector<ValueType>{}
                              : VectorOf({ValueType::Primitive(T)}),
        type, data);
205 206
  }

207 208
  void any_block(Vector<const ValueType> param_types,
                 Vector<const ValueType> return_types, DataRange* data) {
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    uint8_t block_type = data->get<uint8_t>() % 4;
    switch (block_type) {
      case 0:
        block(param_types, return_types, data);
        return;
      case 1:
        loop(param_types, return_types, data);
        return;
      case 2:
        if (param_types == return_types) {
          if_({}, {}, kIf, data);
          return;
        }
        V8_FALLTHROUGH;
      case 3:
        if_(param_types, return_types, kIfElse, data);
        return;
226 227 228
    }
  }

229
  void br(DataRange* data) {
230 231
    // There is always at least the block representing the function body.
    DCHECK(!blocks_.empty());
232
    const uint32_t target_block = data->get<uint32_t>() % blocks_.size();
233
    const auto break_types = blocks_[target_block];
234

235
    Generate(VectorOf(break_types), data);
236 237
    builder_->EmitWithI32V(
        kExprBr, static_cast<uint32_t>(blocks_.size()) - 1 - target_block);
238 239
  }

240
  template <ValueType::Kind wanted_type>
241
  void br_if(DataRange* data) {
242 243
    // There is always at least the block representing the function body.
    DCHECK(!blocks_.empty());
244
    const uint32_t target_block = data->get<uint32_t>() % blocks_.size();
245
    const auto break_types = VectorOf(blocks_[target_block]);
246

247
    Generate(break_types, data);
248 249 250
    Generate(kWasmI32, data);
    builder_->EmitWithI32V(
        kExprBrIf, static_cast<uint32_t>(blocks_.size()) - 1 - target_block);
251 252 253 254 255
    ConsumeAndGenerate(break_types,
                       wanted_type == ValueType::kStmt
                           ? Vector<ValueType>{}
                           : VectorOf({ValueType::Primitive(wanted_type)}),
                       data);
256 257
  }

258 259 260
  // TODO(eholk): make this function constexpr once gcc supports it
  static uint8_t max_alignment(WasmOpcode memop) {
    switch (memop) {
261 262 263
      case kExprS128LoadMem:
      case kExprS128StoreMem:
        return 4;
264 265 266 267
      case kExprI64LoadMem:
      case kExprF64LoadMem:
      case kExprI64StoreMem:
      case kExprF64StoreMem:
268 269
      case kExprI64AtomicStore:
      case kExprI64AtomicLoad:
270 271 272 273 274
      case kExprI64AtomicAdd:
      case kExprI64AtomicSub:
      case kExprI64AtomicAnd:
      case kExprI64AtomicOr:
      case kExprI64AtomicXor:
275 276
      case kExprI64AtomicExchange:
      case kExprI64AtomicCompareExchange:
277 278 279 280 281 282 283
      case kExprI16x8Load8x8S:
      case kExprI16x8Load8x8U:
      case kExprI32x4Load16x4S:
      case kExprI32x4Load16x4U:
      case kExprI64x2Load32x2S:
      case kExprI64x2Load32x2U:
      case kExprS64x2LoadSplat:
284 285 286 287 288 289 290 291
        return 3;
      case kExprI32LoadMem:
      case kExprI64LoadMem32S:
      case kExprI64LoadMem32U:
      case kExprF32LoadMem:
      case kExprI32StoreMem:
      case kExprI64StoreMem32:
      case kExprF32StoreMem:
292 293 294 295
      case kExprI32AtomicStore:
      case kExprI64AtomicStore32U:
      case kExprI32AtomicLoad:
      case kExprI64AtomicLoad32U:
296 297 298 299 300
      case kExprI32AtomicAdd:
      case kExprI32AtomicSub:
      case kExprI32AtomicAnd:
      case kExprI32AtomicOr:
      case kExprI32AtomicXor:
301 302
      case kExprI32AtomicExchange:
      case kExprI32AtomicCompareExchange:
303 304 305 306 307
      case kExprI64AtomicAdd32U:
      case kExprI64AtomicSub32U:
      case kExprI64AtomicAnd32U:
      case kExprI64AtomicOr32U:
      case kExprI64AtomicXor32U:
308 309
      case kExprI64AtomicExchange32U:
      case kExprI64AtomicCompareExchange32U:
310
      case kExprS32x4LoadSplat:
311 312 313 314 315 316 317
        return 2;
      case kExprI32LoadMem16S:
      case kExprI32LoadMem16U:
      case kExprI64LoadMem16S:
      case kExprI64LoadMem16U:
      case kExprI32StoreMem16:
      case kExprI64StoreMem16:
318 319 320 321
      case kExprI32AtomicStore16U:
      case kExprI64AtomicStore16U:
      case kExprI32AtomicLoad16U:
      case kExprI64AtomicLoad16U:
322 323 324 325 326
      case kExprI32AtomicAdd16U:
      case kExprI32AtomicSub16U:
      case kExprI32AtomicAnd16U:
      case kExprI32AtomicOr16U:
      case kExprI32AtomicXor16U:
327 328
      case kExprI32AtomicExchange16U:
      case kExprI32AtomicCompareExchange16U:
329 330 331 332 333
      case kExprI64AtomicAdd16U:
      case kExprI64AtomicSub16U:
      case kExprI64AtomicAnd16U:
      case kExprI64AtomicOr16U:
      case kExprI64AtomicXor16U:
334 335
      case kExprI64AtomicExchange16U:
      case kExprI64AtomicCompareExchange16U:
336
      case kExprS16x8LoadSplat:
337 338 339 340 341 342 343
        return 1;
      case kExprI32LoadMem8S:
      case kExprI32LoadMem8U:
      case kExprI64LoadMem8S:
      case kExprI64LoadMem8U:
      case kExprI32StoreMem8:
      case kExprI64StoreMem8:
344 345 346 347
      case kExprI32AtomicStore8U:
      case kExprI64AtomicStore8U:
      case kExprI32AtomicLoad8U:
      case kExprI64AtomicLoad8U:
348 349 350 351 352
      case kExprI32AtomicAdd8U:
      case kExprI32AtomicSub8U:
      case kExprI32AtomicAnd8U:
      case kExprI32AtomicOr8U:
      case kExprI32AtomicXor8U:
353 354
      case kExprI32AtomicExchange8U:
      case kExprI32AtomicCompareExchange8U:
355 356 357 358 359
      case kExprI64AtomicAdd8U:
      case kExprI64AtomicSub8U:
      case kExprI64AtomicAnd8U:
      case kExprI64AtomicOr8U:
      case kExprI64AtomicXor8U:
360 361
      case kExprI64AtomicExchange8U:
      case kExprI64AtomicCompareExchange8U:
362
      case kExprS8x16LoadSplat:
363 364 365 366 367 368
        return 0;
      default:
        return 0;
    }
  }

369
  template <WasmOpcode memory_op, ValueType::Kind... arg_types>
370 371 372
  void memop(DataRange* data) {
    const uint8_t align = data->get<uint8_t>() % (max_alignment(memory_op) + 1);
    const uint32_t offset = data->get<uint32_t>();
373

374
    // Generate the index and the arguments, if any.
375
    Generate<ValueType::kI32, arg_types...>(data);
376

377
    if (WasmOpcodes::IsPrefixOpcode(static_cast<WasmOpcode>(memory_op >> 8))) {
378
      DCHECK(memory_op >> 8 == kAtomicPrefix || memory_op >> 8 == kSimdPrefix);
379 380 381 382
      builder_->EmitWithPrefix(memory_op);
    } else {
      builder_->Emit(memory_op);
    }
383 384 385 386
    builder_->EmitU32V(align);
    builder_->EmitU32V(offset);
  }

387
  template <WasmOpcode Op, ValueType::Kind... Args>
388 389 390 391 392 393 394 395 396 397 398
  void atomic_op(DataRange* data) {
    const uint8_t align = data->get<uint8_t>() % (max_alignment(Op) + 1);
    const uint32_t offset = data->get<uint32_t>();

    Generate<Args...>(data);
    builder_->EmitWithPrefix(Op);

    builder_->EmitU32V(align);
    builder_->EmitU32V(offset);
  }

399
  template <WasmOpcode Op, ValueType::Kind... Args>
400
  void op_with_prefix(DataRange* data) {
401 402 403 404
    Generate<Args...>(data);
    builder_->EmitWithPrefix(Op);
  }

405 406 407 408 409 410 411
  void simd_const(DataRange* data) {
    builder_->EmitWithPrefix(kExprS128Const);
    for (int i = 0; i < kSimd128Size; i++) {
      builder_->EmitByte(data->get<byte>());
    }
  }

412 413 414 415 416 417 418
  template <WasmOpcode Op, int lanes, ValueType::Kind... Args>
  void simd_lane_op(DataRange* data) {
    Generate<Args...>(data);
    builder_->EmitWithPrefix(Op);
    builder_->EmitByte(data->get<byte>() % lanes);
  }

419 420 421 422 423 424 425 426
  void simd_shuffle(DataRange* data) {
    Generate<ValueType::kS128, ValueType::kS128>(data);
    builder_->EmitWithPrefix(kExprS8x16Shuffle);
    for (int i = 0; i < kSimd128Size; i++) {
      builder_->EmitByte(static_cast<uint8_t>(data->get<byte>() % 32));
    }
  }

427
  void drop(DataRange* data) {
428 429 430 431
    Generate(GetValueType(data), data);
    builder_->Emit(kExprDrop);
  }

432 433
  enum CallDirect : bool { kCallDirect = true, kCallIndirect = false };

434
  template <ValueType::Kind wanted_type>
435
  void call(DataRange* data) {
436 437 438 439 440 441
    call(data, ValueType::Primitive(wanted_type), kCallDirect);
  }

  template <ValueType::Kind wanted_type>
  void call_indirect(DataRange* data) {
    call(data, ValueType::Primitive(wanted_type), kCallIndirect);
442 443 444 445
  }

  void Convert(ValueType src, ValueType dst) {
    auto idx = [](ValueType t) -> int {
446 447
      switch (t.kind()) {
        case ValueType::kI32:
448
          return 0;
449
        case ValueType::kI64:
450
          return 1;
451
        case ValueType::kF32:
452
          return 2;
453
        case ValueType::kF64:
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
          return 3;
        default:
          UNREACHABLE();
      }
    };
    static constexpr WasmOpcode kConvertOpcodes[] = {
        // {i32, i64, f32, f64} -> i32
        kExprNop, kExprI32ConvertI64, kExprI32SConvertF32, kExprI32SConvertF64,
        // {i32, i64, f32, f64} -> i64
        kExprI64SConvertI32, kExprNop, kExprI64SConvertF32, kExprI64SConvertF64,
        // {i32, i64, f32, f64} -> f32
        kExprF32SConvertI32, kExprF32SConvertI64, kExprNop, kExprF32ConvertF64,
        // {i32, i64, f32, f64} -> f64
        kExprF64SConvertI32, kExprF64SConvertI64, kExprF64ConvertF32, kExprNop};
    int arr_idx = idx(dst) << 2 | idx(src);
    builder_->Emit(kConvertOpcodes[arr_idx]);
  }

472
  void ConvertOrGenerate(ValueType src, ValueType dst, DataRange* data) {
473 474 475 476 477 478 479 480 481 482
    if (src == dst) return;
    if (src == kWasmStmt && dst != kWasmStmt) {
      Generate(dst, data);
    } else if (dst == kWasmStmt && src != kWasmStmt) {
      builder_->Emit(kExprDrop);
    } else {
      Convert(src, dst);
    }
  }

483
  void call(DataRange* data, ValueType wanted_type, CallDirect call_direct) {
484 485
    uint8_t random_byte = data->get<uint8_t>();
    int func_index = random_byte % functions_.size();
486 487
    uint32_t sig_index = functions_[func_index];
    FunctionSig* sig = builder_->builder()->GetSignature(sig_index);
488 489 490 491 492
    // Generate arguments.
    for (size_t i = 0; i < sig->parameter_count(); ++i) {
      Generate(sig->GetParam(i), data);
    }
    // Emit call.
493 494 495 496 497 498 499
    // If the return types of the callee happen to match the return types of the
    // caller, generate a tail call.
    bool use_return_call = random_byte > 127;
    if (use_return_call &&
        std::equal(sig->returns().begin(), sig->returns().end(),
                   builder_->signature()->returns().begin(),
                   builder_->signature()->returns().end())) {
500 501 502 503 504 505 506
      if (call_direct) {
        builder_->EmitWithU32V(kExprReturnCall, func_index);
      } else {
        builder_->EmitI32Const(func_index);
        builder_->EmitWithU32V(kExprReturnCallIndirect, sig_index);
        builder_->EmitByte(0);  // Table index.
      }
507 508
      return;
    } else {
509 510 511 512 513 514 515
      if (call_direct) {
        builder_->EmitWithU32V(kExprCallFunction, func_index);
      } else {
        builder_->EmitI32Const(func_index);
        builder_->EmitWithU32V(kExprCallIndirect, sig_index);
        builder_->EmitByte(0);  // Table index.
      }
516
    }
517
    if (sig->return_count() == 0 && wanted_type != kWasmStmt) {
518 519
      // The call did not generate a value. Thus just generate it here.
      Generate(wanted_type, data);
520 521 522 523 524 525 526 527 528
      return;
    }
    if (wanted_type == kWasmStmt) {
      // The call did generate values, but we did not want one.
      for (size_t i = 0; i < sig->return_count(); ++i) {
        builder_->Emit(kExprDrop);
      }
      return;
    }
529 530 531
    auto return_types = VectorOf(sig->returns().begin(), sig->return_count());
    auto wanted_types =
        VectorOf(&wanted_type, wanted_type == kWasmStmt ? 0 : 1);
532
    ConsumeAndGenerate(return_types, wanted_types, data);
533 534
  }

535
  struct Var {
536 537
    uint32_t index;
    ValueType type = kWasmStmt;
538 539
    Var() = default;
    Var(uint32_t index, ValueType type) : index(index), type(type) {}
540 541 542
    bool is_valid() const { return type != kWasmStmt; }
  };

543
  Var GetRandomLocal(DataRange* data) {
544 545 546 547
    uint32_t num_params =
        static_cast<uint32_t>(builder_->signature()->parameter_count());
    uint32_t num_locals = static_cast<uint32_t>(locals_.size());
    if (num_params + num_locals == 0) return {};
548
    uint32_t index = data->get<uint8_t>() % (num_params + num_locals);
549 550 551 552 553
    ValueType type = index < num_params ? builder_->signature()->GetParam(index)
                                        : locals_[index - num_params];
    return {index, type};
  }

554
  template <ValueType::Kind wanted_type>
555
  void local_op(DataRange* data, WasmOpcode opcode) {
556
    Var local = GetRandomLocal(data);
557 558 559
    // If there are no locals and no parameters, just generate any value (if a
    // value is needed), or do nothing.
    if (!local.is_valid()) {
560
      if (wanted_type == ValueType::kStmt) return;
561 562
      return Generate<wanted_type>(data);
    }
563

564
    if (opcode != kExprLocalGet) Generate(local.type, data);
565
    builder_->EmitWithU32V(opcode, local.index);
566
    if (wanted_type != ValueType::kStmt && local.type.kind() != wanted_type) {
567
      Convert(local.type, ValueType::Primitive(wanted_type));
568
    }
569 570
  }

571
  template <ValueType::Kind wanted_type>
572
  void get_local(DataRange* data) {
573
    static_assert(wanted_type != ValueType::kStmt, "illegal type");
574
    local_op<wanted_type>(data, kExprLocalGet);
575 576
  }

577 578 579
  void set_local(DataRange* data) {
    local_op<ValueType::kStmt>(data, kExprLocalSet);
  }
580

581
  template <ValueType::Kind wanted_type>
582
  void tee_local(DataRange* data) {
583
    local_op<wanted_type>(data, kExprLocalTee);
584 585
  }

586
  template <size_t num_bytes>
587 588
  void i32_const(DataRange* data) {
    builder_->EmitI32Const(data->get<int32_t, num_bytes>());
589 590 591
  }

  template <size_t num_bytes>
592 593
  void i64_const(DataRange* data) {
    builder_->EmitI64Const(data->get<int64_t, num_bytes>());
594 595
  }

596
  Var GetRandomGlobal(DataRange* data, bool ensure_mutable) {
597 598 599
    uint32_t index;
    if (ensure_mutable) {
      if (mutable_globals_.empty()) return {};
600
      index = mutable_globals_[data->get<uint8_t>() % mutable_globals_.size()];
601 602
    } else {
      if (globals_.empty()) return {};
603
      index = data->get<uint8_t>() % globals_.size();
604 605 606 607 608
    }
    ValueType type = globals_[index];
    return {index, type};
  }

609
  template <ValueType::Kind wanted_type>
610
  void global_op(DataRange* data) {
611
    constexpr bool is_set = wanted_type == ValueType::kStmt;
612 613 614 615
    Var global = GetRandomGlobal(data, is_set);
    // If there are no globals, just generate any value (if a value is needed),
    // or do nothing.
    if (!global.is_valid()) {
616
      if (wanted_type == ValueType::kStmt) return;
617 618 619 620
      return Generate<wanted_type>(data);
    }

    if (is_set) Generate(global.type, data);
621
    builder_->EmitWithU32V(is_set ? kExprGlobalSet : kExprGlobalGet,
622
                           global.index);
623
    if (!is_set && global.type.kind() != wanted_type) {
624
      Convert(global.type, ValueType::Primitive(wanted_type));
625 626 627
    }
  }

628
  template <ValueType::Kind wanted_type>
629
  void get_global(DataRange* data) {
630
    static_assert(wanted_type != ValueType::kStmt, "illegal type");
631 632 633
    global_op<wanted_type>(data);
  }

634
  template <ValueType::Kind select_type>
635
  void select_with_type(DataRange* data) {
636 637
    static_assert(select_type != ValueType::kStmt, "illegal type for select");
    Generate<select_type, select_type, ValueType::kI32>(data);
638 639 640
    // num_types is always 1.
    uint8_t num_types = 1;
    builder_->EmitWithU8U8(kExprSelectWithType, num_types,
641
                           ValueType::Primitive(select_type).value_type_code());
642 643
  }

644
  void set_global(DataRange* data) { global_op<ValueType::kStmt>(data); }
645

646
  template <ValueType::Kind... Types>
647
  void sequence(DataRange* data) {
648
    Generate<Types...>(data);
649 650
  }

651
  void current_memory(DataRange* data) {
652 653 654
    builder_->EmitWithU8(kExprMemorySize, 0);
  }

655
  void grow_memory(DataRange* data);
656

657
  using GenerateFn = void (WasmGenerator::*const)(DataRange*);
658 659

  template <size_t N>
660
  void GenerateOneOf(GenerateFn (&alternatives)[N], DataRange* data) {
661
    static_assert(N < std::numeric_limits<uint8_t>::max(),
662
                  "Too many alternatives. Use a bigger type if needed.");
663
    const auto which = data->get<uint8_t>();
664

665
    GenerateFn alternate = alternatives[which % N];
666
    (this->*alternate)(data);
667 668
  }

669 670 671
  struct GeneratorRecursionScope {
    explicit GeneratorRecursionScope(WasmGenerator* gen) : gen(gen) {
      ++gen->recursion_depth;
672
      DCHECK_LE(gen->recursion_depth, kMaxRecursionDepth);
673 674 675 676 677 678 679 680
    }
    ~GeneratorRecursionScope() {
      DCHECK_GT(gen->recursion_depth, 0);
      --gen->recursion_depth;
    }
    WasmGenerator* gen;
  };

681
 public:
682
  WasmGenerator(WasmFunctionBuilder* fn, const std::vector<uint32_t>& functions,
683
                const std::vector<ValueType>& globals,
684
                const std::vector<uint8_t>& mutable_globals, DataRange* data)
685 686 687 688
      : builder_(fn),
        functions_(functions),
        globals_(globals),
        mutable_globals_(mutable_globals) {
689
    FunctionSig* sig = fn->signature();
690 691 692
    blocks_.emplace_back();
    for (size_t i = 0; i < sig->return_count(); ++i) {
      blocks_.back().push_back(sig->GetReturn(i));
693
    }
694 695

    constexpr uint32_t kMaxLocals = 32;
696
    locals_.resize(data->get<uint8_t>() % kMaxLocals);
697 698 699 700
    for (ValueType& local : locals_) {
      local = GetValueType(data);
      fn->AddLocal(local);
    }
701
  }
702

703
  void Generate(ValueType type, DataRange* data);
704

705
  template <ValueType::Kind T>
706
  void Generate(DataRange* data);
707

708
  template <ValueType::Kind T1, ValueType::Kind T2, ValueType::Kind... Ts>
709
  void Generate(DataRange* data) {
710
    // TODO(clemensb): Implement a more even split.
711 712
    auto first_data = data->split();
    Generate<T1>(&first_data);
713
    Generate<T2, Ts...>(data);
714 715
  }

716 717
  std::vector<ValueType> GenerateTypes(DataRange* data);
  void Generate(Vector<const ValueType> types, DataRange* data);
718 719
  void ConsumeAndGenerate(Vector<const ValueType> parameter_types,
                          Vector<const ValueType> return_types,
720 721
                          DataRange* data);

722
 private:
723
  WasmFunctionBuilder* builder_;
724
  std::vector<std::vector<ValueType>> blocks_;
725
  const std::vector<uint32_t>& functions_;
726
  std::vector<ValueType> locals_;
727 728
  std::vector<ValueType> globals_;
  std::vector<uint8_t> mutable_globals_;  // indexes into {globals_}.
729 730 731 732 733 734 735
  uint32_t recursion_depth = 0;

  static constexpr uint32_t kMaxRecursionDepth = 64;

  bool recursion_limit_reached() {
    return recursion_depth >= kMaxRecursionDepth;
  }
736 737
};

738 739 740 741 742
template <>
void WasmGenerator::block<ValueType::kStmt>(DataRange* data) {
  block({}, {}, data);
}

743 744 745 746 747
template <>
void WasmGenerator::loop<ValueType::kStmt>(DataRange* data) {
  loop({}, {}, data);
}

748
template <>
749
void WasmGenerator::Generate<ValueType::kStmt>(DataRange* data) {
750
  GeneratorRecursionScope rec_scope(this);
751
  if (recursion_limit_reached() || data->size() == 0) return;
752

753
  constexpr GenerateFn alternatives[] = {
754 755 756 757 758 759 760 761 762 763 764
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kStmt>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kStmt,
                               ValueType::kStmt, ValueType::kStmt>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kStmt,
                               ValueType::kStmt, ValueType::kStmt,
                               ValueType::kStmt, ValueType::kStmt,
                               ValueType::kStmt, ValueType::kStmt>,
      &WasmGenerator::block<ValueType::kStmt>,
      &WasmGenerator::loop<ValueType::kStmt>,
      &WasmGenerator::if_<ValueType::kStmt, kIf>,
      &WasmGenerator::if_<ValueType::kStmt, kIfElse>,
765
      &WasmGenerator::br,
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
      &WasmGenerator::br_if<ValueType::kStmt>,

      &WasmGenerator::memop<kExprI32StoreMem, ValueType::kI32>,
      &WasmGenerator::memop<kExprI32StoreMem8, ValueType::kI32>,
      &WasmGenerator::memop<kExprI32StoreMem16, ValueType::kI32>,
      &WasmGenerator::memop<kExprI64StoreMem, ValueType::kI64>,
      &WasmGenerator::memop<kExprI64StoreMem8, ValueType::kI64>,
      &WasmGenerator::memop<kExprI64StoreMem16, ValueType::kI64>,
      &WasmGenerator::memop<kExprI64StoreMem32, ValueType::kI64>,
      &WasmGenerator::memop<kExprF32StoreMem, ValueType::kF32>,
      &WasmGenerator::memop<kExprF64StoreMem, ValueType::kF64>,
      &WasmGenerator::memop<kExprI32AtomicStore, ValueType::kI32>,
      &WasmGenerator::memop<kExprI32AtomicStore8U, ValueType::kI32>,
      &WasmGenerator::memop<kExprI32AtomicStore16U, ValueType::kI32>,
      &WasmGenerator::memop<kExprI64AtomicStore, ValueType::kI64>,
      &WasmGenerator::memop<kExprI64AtomicStore8U, ValueType::kI64>,
      &WasmGenerator::memop<kExprI64AtomicStore16U, ValueType::kI64>,
      &WasmGenerator::memop<kExprI64AtomicStore32U, ValueType::kI64>,
784
      &WasmGenerator::memop<kExprS128StoreMem, ValueType::kS128>,
785

786 787
      &WasmGenerator::drop,

788
      &WasmGenerator::call<ValueType::kStmt>,
789
      &WasmGenerator::call_indirect<ValueType::kStmt>,
790

791 792
      &WasmGenerator::set_local,
      &WasmGenerator::set_global};
793

794
  GenerateOneOf(alternatives, data);
795 796
}

797
template <>
798
void WasmGenerator::Generate<ValueType::kI32>(DataRange* data) {
799
  GeneratorRecursionScope rec_scope(this);
800 801
  if (recursion_limit_reached() || data->size() <= 1) {
    builder_->EmitI32Const(data->get<uint32_t>());
802
    return;
803
  }
804

805
  constexpr GenerateFn alternatives[] = {
806 807 808 809 810
      &WasmGenerator::i32_const<1>,
      &WasmGenerator::i32_const<2>,
      &WasmGenerator::i32_const<3>,
      &WasmGenerator::i32_const<4>,

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
      &WasmGenerator::sequence<ValueType::kI32, ValueType::kStmt>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kI32>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kI32,
                               ValueType::kStmt>,

      &WasmGenerator::op<kExprI32Eqz, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Eq, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Ne, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32LtS, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32LtU, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32GeS, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32GeU, ValueType::kI32, ValueType::kI32>,

      &WasmGenerator::op<kExprI64Eqz, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Eq, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Ne, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64LtS, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64LtU, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64GeS, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64GeU, ValueType::kI64, ValueType::kI64>,

      &WasmGenerator::op<kExprF32Eq, ValueType::kF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Ne, ValueType::kF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Lt, ValueType::kF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Ge, ValueType::kF32, ValueType::kF32>,

      &WasmGenerator::op<kExprF64Eq, ValueType::kF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Ne, ValueType::kF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Lt, ValueType::kF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Ge, ValueType::kF64, ValueType::kF64>,

      &WasmGenerator::op<kExprI32Add, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Sub, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Mul, ValueType::kI32, ValueType::kI32>,

      &WasmGenerator::op<kExprI32DivS, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32DivU, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32RemS, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32RemU, ValueType::kI32, ValueType::kI32>,

      &WasmGenerator::op<kExprI32And, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Ior, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Xor, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Shl, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32ShrU, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32ShrS, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Ror, ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Rol, ValueType::kI32, ValueType::kI32>,

      &WasmGenerator::op<kExprI32Clz, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Ctz, ValueType::kI32>,
      &WasmGenerator::op<kExprI32Popcnt, ValueType::kI32>,

      &WasmGenerator::op<kExprI32ConvertI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI32SConvertF32, ValueType::kF32>,
      &WasmGenerator::op<kExprI32UConvertF32, ValueType::kF32>,
      &WasmGenerator::op<kExprI32SConvertF64, ValueType::kF64>,
      &WasmGenerator::op<kExprI32UConvertF64, ValueType::kF64>,
      &WasmGenerator::op<kExprI32ReinterpretF32, ValueType::kF32>,

871 872 873 874
      &WasmGenerator::op_with_prefix<kExprI32SConvertSatF32, ValueType::kF32>,
      &WasmGenerator::op_with_prefix<kExprI32UConvertSatF32, ValueType::kF32>,
      &WasmGenerator::op_with_prefix<kExprI32SConvertSatF64, ValueType::kF64>,
      &WasmGenerator::op_with_prefix<kExprI32UConvertSatF64, ValueType::kF64>,
875

876 877 878 879
      &WasmGenerator::block<ValueType::kI32>,
      &WasmGenerator::loop<ValueType::kI32>,
      &WasmGenerator::if_<ValueType::kI32, kIfElse>,
      &WasmGenerator::br_if<ValueType::kI32>,
880 881 882 883 884 885

      &WasmGenerator::memop<kExprI32LoadMem>,
      &WasmGenerator::memop<kExprI32LoadMem8S>,
      &WasmGenerator::memop<kExprI32LoadMem8U>,
      &WasmGenerator::memop<kExprI32LoadMem16S>,
      &WasmGenerator::memop<kExprI32LoadMem16U>,
886 887 888
      &WasmGenerator::memop<kExprI32AtomicLoad>,
      &WasmGenerator::memop<kExprI32AtomicLoad8U>,
      &WasmGenerator::memop<kExprI32AtomicLoad16U>,
889

890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
      &WasmGenerator::atomic_op<kExprI32AtomicAdd, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicSub, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicAnd, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicOr, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicXor, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicExchange, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicCompareExchange, ValueType::kI32,
                                ValueType::kI32, ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicAdd8U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicSub8U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicAnd8U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicOr8U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicXor8U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicExchange8U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicCompareExchange8U,
                                ValueType::kI32, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicAdd16U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicSub16U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicAnd16U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicOr16U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicXor16U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicExchange16U, ValueType::kI32,
                                ValueType::kI32>,
      &WasmGenerator::atomic_op<kExprI32AtomicCompareExchange16U,
                                ValueType::kI32, ValueType::kI32,
                                ValueType::kI32>,

935
      &WasmGenerator::op_with_prefix<kExprV8x16AnyTrue, ValueType::kS128>,
936
      &WasmGenerator::op_with_prefix<kExprV8x16AllTrue, ValueType::kS128>,
937
      &WasmGenerator::op_with_prefix<kExprI8x16BitMask, ValueType::kS128>,
938
      &WasmGenerator::op_with_prefix<kExprV16x8AnyTrue, ValueType::kS128>,
939
      &WasmGenerator::op_with_prefix<kExprV16x8AllTrue, ValueType::kS128>,
940
      &WasmGenerator::op_with_prefix<kExprI16x8BitMask, ValueType::kS128>,
941
      &WasmGenerator::op_with_prefix<kExprV32x4AnyTrue, ValueType::kS128>,
942
      &WasmGenerator::op_with_prefix<kExprV32x4AllTrue, ValueType::kS128>,
943
      &WasmGenerator::op_with_prefix<kExprI32x4BitMask, ValueType::kS128>,
944 945 946 947 948 949 950
      &WasmGenerator::simd_lane_op<kExprI8x16ExtractLaneS, 16,
                                   ValueType::kS128>,
      &WasmGenerator::simd_lane_op<kExprI8x16ExtractLaneU, 16,
                                   ValueType::kS128>,
      &WasmGenerator::simd_lane_op<kExprI16x8ExtractLaneS, 8, ValueType::kS128>,
      &WasmGenerator::simd_lane_op<kExprI16x8ExtractLaneU, 8, ValueType::kS128>,
      &WasmGenerator::simd_lane_op<kExprI32x4ExtractLane, 4, ValueType::kS128>,
951

952
      &WasmGenerator::current_memory,
953 954
      &WasmGenerator::grow_memory,

955 956 957 958 959 960
      &WasmGenerator::get_local<ValueType::kI32>,
      &WasmGenerator::tee_local<ValueType::kI32>,
      &WasmGenerator::get_global<ValueType::kI32>,
      &WasmGenerator::op<kExprSelect, ValueType::kI32, ValueType::kI32,
                         ValueType::kI32>,
      &WasmGenerator::select_with_type<ValueType::kI32>,
961

962 963
      &WasmGenerator::call<ValueType::kI32>,
      &WasmGenerator::call_indirect<ValueType::kI32>};
964

965
  GenerateOneOf(alternatives, data);
966 967 968
}

template <>
969
void WasmGenerator::Generate<ValueType::kI64>(DataRange* data) {
970
  GeneratorRecursionScope rec_scope(this);
971 972
  if (recursion_limit_reached() || data->size() <= 1) {
    builder_->EmitI64Const(data->get<int64_t>());
973
    return;
974
  }
975

976
  constexpr GenerateFn alternatives[] = {
977 978 979 980 981 982 983 984 985
      &WasmGenerator::i64_const<1>,
      &WasmGenerator::i64_const<2>,
      &WasmGenerator::i64_const<3>,
      &WasmGenerator::i64_const<4>,
      &WasmGenerator::i64_const<5>,
      &WasmGenerator::i64_const<6>,
      &WasmGenerator::i64_const<7>,
      &WasmGenerator::i64_const<8>,

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
      &WasmGenerator::sequence<ValueType::kI64, ValueType::kStmt>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kI64>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kI64,
                               ValueType::kStmt>,

      &WasmGenerator::op<kExprI64Add, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Sub, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Mul, ValueType::kI64, ValueType::kI64>,

      &WasmGenerator::op<kExprI64DivS, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64DivU, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64RemS, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64RemU, ValueType::kI64, ValueType::kI64>,

      &WasmGenerator::op<kExprI64And, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Ior, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Xor, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Shl, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64ShrU, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64ShrS, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Ror, ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Rol, ValueType::kI64, ValueType::kI64>,

      &WasmGenerator::op<kExprI64Clz, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Ctz, ValueType::kI64>,
      &WasmGenerator::op<kExprI64Popcnt, ValueType::kI64>,

1013 1014 1015 1016 1017
      &WasmGenerator::op_with_prefix<kExprI64SConvertSatF32, ValueType::kF32>,
      &WasmGenerator::op_with_prefix<kExprI64UConvertSatF32, ValueType::kF32>,
      &WasmGenerator::op_with_prefix<kExprI64SConvertSatF64, ValueType::kF64>,
      &WasmGenerator::op_with_prefix<kExprI64UConvertSatF64, ValueType::kF64>,

1018 1019 1020 1021
      &WasmGenerator::block<ValueType::kI64>,
      &WasmGenerator::loop<ValueType::kI64>,
      &WasmGenerator::if_<ValueType::kI64, kIfElse>,
      &WasmGenerator::br_if<ValueType::kI64>,
1022 1023 1024 1025 1026 1027 1028

      &WasmGenerator::memop<kExprI64LoadMem>,
      &WasmGenerator::memop<kExprI64LoadMem8S>,
      &WasmGenerator::memop<kExprI64LoadMem8U>,
      &WasmGenerator::memop<kExprI64LoadMem16S>,
      &WasmGenerator::memop<kExprI64LoadMem16U>,
      &WasmGenerator::memop<kExprI64LoadMem32S>,
1029
      &WasmGenerator::memop<kExprI64LoadMem32U>,
1030 1031 1032 1033
      &WasmGenerator::memop<kExprI64AtomicLoad>,
      &WasmGenerator::memop<kExprI64AtomicLoad8U>,
      &WasmGenerator::memop<kExprI64AtomicLoad16U>,
      &WasmGenerator::memop<kExprI64AtomicLoad32U>,
1034

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
      &WasmGenerator::atomic_op<kExprI64AtomicAdd, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicSub, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicAnd, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicOr, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicXor, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicExchange, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicCompareExchange, ValueType::kI32,
                                ValueType::kI64, ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicAdd8U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicSub8U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicAnd8U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicOr8U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicXor8U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicExchange8U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicCompareExchange8U,
                                ValueType::kI32, ValueType::kI64,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicAdd16U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicSub16U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicAnd16U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicOr16U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicXor16U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicExchange16U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicCompareExchange16U,
                                ValueType::kI32, ValueType::kI64,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicAdd32U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicSub32U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicAnd32U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicOr32U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicXor32U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicExchange32U, ValueType::kI32,
                                ValueType::kI64>,
      &WasmGenerator::atomic_op<kExprI64AtomicCompareExchange32U,
                                ValueType::kI32, ValueType::kI64,
                                ValueType::kI64>,

1095 1096
      &WasmGenerator::simd_lane_op<kExprI64x2ExtractLane, 2, ValueType::kS128>,

1097 1098 1099 1100 1101 1102 1103
      &WasmGenerator::get_local<ValueType::kI64>,
      &WasmGenerator::tee_local<ValueType::kI64>,
      &WasmGenerator::get_global<ValueType::kI64>,
      &WasmGenerator::op<kExprSelect, ValueType::kI64, ValueType::kI64,
                         ValueType::kI32>,
      &WasmGenerator::select_with_type<ValueType::kI64>,

1104 1105
      &WasmGenerator::call<ValueType::kI64>,
      &WasmGenerator::call_indirect<ValueType::kI64>};
1106

1107
  GenerateOneOf(alternatives, data);
1108 1109 1110
}

template <>
1111
void WasmGenerator::Generate<ValueType::kF32>(DataRange* data) {
1112
  GeneratorRecursionScope rec_scope(this);
1113 1114
  if (recursion_limit_reached() || data->size() <= sizeof(float)) {
    builder_->EmitF32Const(data->get<float>());
1115 1116
    return;
  }
1117

1118
  constexpr GenerateFn alternatives[] = {
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
      &WasmGenerator::sequence<ValueType::kF32, ValueType::kStmt>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kF32>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kF32,
                               ValueType::kStmt>,

      &WasmGenerator::op<kExprF32Abs, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Neg, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Ceil, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Floor, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Trunc, ValueType::kF32>,
      &WasmGenerator::op<kExprF32NearestInt, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Sqrt, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Add, ValueType::kF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Sub, ValueType::kF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Mul, ValueType::kF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Div, ValueType::kF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Min, ValueType::kF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF32Max, ValueType::kF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF32CopySign, ValueType::kF32, ValueType::kF32>,

      &WasmGenerator::op<kExprF32SConvertI32, ValueType::kI32>,
      &WasmGenerator::op<kExprF32UConvertI32, ValueType::kI32>,
      &WasmGenerator::op<kExprF32SConvertI64, ValueType::kI64>,
      &WasmGenerator::op<kExprF32UConvertI64, ValueType::kI64>,
      &WasmGenerator::op<kExprF32ConvertF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF32ReinterpretI32, ValueType::kI32>,

      &WasmGenerator::block<ValueType::kF32>,
      &WasmGenerator::loop<ValueType::kF32>,
      &WasmGenerator::if_<ValueType::kF32, kIfElse>,
      &WasmGenerator::br_if<ValueType::kF32>,
1150

1151 1152
      &WasmGenerator::memop<kExprF32LoadMem>,

1153 1154
      &WasmGenerator::simd_lane_op<kExprF32x4ExtractLane, 4, ValueType::kS128>,

1155 1156 1157 1158 1159 1160
      &WasmGenerator::get_local<ValueType::kF32>,
      &WasmGenerator::tee_local<ValueType::kF32>,
      &WasmGenerator::get_global<ValueType::kF32>,
      &WasmGenerator::op<kExprSelect, ValueType::kF32, ValueType::kF32,
                         ValueType::kI32>,
      &WasmGenerator::select_with_type<ValueType::kF32>,
1161

1162 1163
      &WasmGenerator::call<ValueType::kF32>,
      &WasmGenerator::call_indirect<ValueType::kF32>};
1164

1165
  GenerateOneOf(alternatives, data);
1166 1167 1168
}

template <>
1169
void WasmGenerator::Generate<ValueType::kF64>(DataRange* data) {
1170
  GeneratorRecursionScope rec_scope(this);
1171 1172
  if (recursion_limit_reached() || data->size() <= sizeof(double)) {
    builder_->EmitF64Const(data->get<double>());
1173 1174
    return;
  }
1175

1176
  constexpr GenerateFn alternatives[] = {
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
      &WasmGenerator::sequence<ValueType::kF64, ValueType::kStmt>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kF64>,
      &WasmGenerator::sequence<ValueType::kStmt, ValueType::kF64,
                               ValueType::kStmt>,

      &WasmGenerator::op<kExprF64Abs, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Neg, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Ceil, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Floor, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Trunc, ValueType::kF64>,
      &WasmGenerator::op<kExprF64NearestInt, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Sqrt, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Add, ValueType::kF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Sub, ValueType::kF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Mul, ValueType::kF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Div, ValueType::kF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Min, ValueType::kF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF64Max, ValueType::kF64, ValueType::kF64>,
      &WasmGenerator::op<kExprF64CopySign, ValueType::kF64, ValueType::kF64>,

      &WasmGenerator::op<kExprF64SConvertI32, ValueType::kI32>,
      &WasmGenerator::op<kExprF64UConvertI32, ValueType::kI32>,
      &WasmGenerator::op<kExprF64SConvertI64, ValueType::kI64>,
      &WasmGenerator::op<kExprF64UConvertI64, ValueType::kI64>,
      &WasmGenerator::op<kExprF64ConvertF32, ValueType::kF32>,
      &WasmGenerator::op<kExprF64ReinterpretI64, ValueType::kI64>,

      &WasmGenerator::block<ValueType::kF64>,
      &WasmGenerator::loop<ValueType::kF64>,
      &WasmGenerator::if_<ValueType::kF64, kIfElse>,
      &WasmGenerator::br_if<ValueType::kF64>,
1208

1209 1210
      &WasmGenerator::memop<kExprF64LoadMem>,

1211 1212
      &WasmGenerator::simd_lane_op<kExprF64x2ExtractLane, 2, ValueType::kS128>,

1213 1214 1215 1216 1217 1218
      &WasmGenerator::get_local<ValueType::kF64>,
      &WasmGenerator::tee_local<ValueType::kF64>,
      &WasmGenerator::get_global<ValueType::kF64>,
      &WasmGenerator::op<kExprSelect, ValueType::kF64, ValueType::kF64,
                         ValueType::kI32>,
      &WasmGenerator::select_with_type<ValueType::kF64>,
1219

1220 1221
      &WasmGenerator::call<ValueType::kF64>,
      &WasmGenerator::call_indirect<ValueType::kF64>};
1222

1223
  GenerateOneOf(alternatives, data);
1224 1225
}

1226
template <>
1227
void WasmGenerator::Generate<ValueType::kS128>(DataRange* data) {
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
  GeneratorRecursionScope rec_scope(this);
  if (recursion_limit_reached() || data->size() <= sizeof(int32_t)) {
    // TODO(v8:8460): v128.const is not implemented yet, and we need a way to
    // "bottom-out", so use a splat to generate this.
    builder_->EmitI32Const(data->get<int32_t>());
    builder_->EmitWithPrefix(kExprI8x16Splat);
    return;
  }

  constexpr GenerateFn alternatives[] = {
1238
      &WasmGenerator::simd_const,
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
      &WasmGenerator::simd_lane_op<kExprI8x16ReplaceLane, 16, ValueType::kS128,
                                   ValueType::kI32>,
      &WasmGenerator::simd_lane_op<kExprI16x8ReplaceLane, 8, ValueType::kS128,
                                   ValueType::kI32>,
      &WasmGenerator::simd_lane_op<kExprI32x4ReplaceLane, 4, ValueType::kS128,
                                   ValueType::kI32>,
      &WasmGenerator::simd_lane_op<kExprI64x2ReplaceLane, 2, ValueType::kS128,
                                   ValueType::kI64>,
      &WasmGenerator::simd_lane_op<kExprF32x4ReplaceLane, 4, ValueType::kS128,
                                   ValueType::kF32>,
      &WasmGenerator::simd_lane_op<kExprF64x2ReplaceLane, 2, ValueType::kS128,
                                   ValueType::kF64>,

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
      &WasmGenerator::op_with_prefix<kExprI8x16Splat, ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI8x16Eq, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16Ne, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16LtS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16LtU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16GtS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16GtU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16LeS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16LeU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16GeS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16GeU, ValueType::kS128,
                                     ValueType::kS128>,
1273
      &WasmGenerator::op_with_prefix<kExprI8x16Abs, ValueType::kS128>,
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
      &WasmGenerator::op_with_prefix<kExprI8x16Neg, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16Shl, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI8x16ShrS, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI8x16ShrU, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI8x16Add, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16AddSaturateS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16AddSaturateU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16Sub, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16SubSaturateS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16SubSaturateU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16MinS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16MinU, ValueType::kS128,
                                     ValueType::kS128>,
1297
      // I8x16Mul is prototyped but not in the proposal, thus omitted here.
1298 1299 1300 1301
      &WasmGenerator::op_with_prefix<kExprI8x16MaxS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16MaxU, ValueType::kS128,
                                     ValueType::kS128>,
1302 1303
      &WasmGenerator::op_with_prefix<kExprI8x16RoundingAverageU,
                                     ValueType::kS128, ValueType::kS128>,
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325

      &WasmGenerator::op_with_prefix<kExprI16x8Splat, ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI16x8Eq, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8Ne, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8LtS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8LtU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8GtS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8GtU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8LeS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8LeU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8GeS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8GeU, ValueType::kS128,
                                     ValueType::kS128>,
1326
      &WasmGenerator::op_with_prefix<kExprI16x8Abs, ValueType::kS128>,
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
      &WasmGenerator::op_with_prefix<kExprI16x8Neg, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8Shl, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI16x8ShrS, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI16x8ShrU, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI16x8Add, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8AddSaturateS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8AddSaturateU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8Sub, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8SubSaturateS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8SubSaturateU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8Mul, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8MinS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8MinU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8MaxS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8MaxU, ValueType::kS128,
                                     ValueType::kS128>,
1356 1357
      &WasmGenerator::op_with_prefix<kExprI16x8RoundingAverageU,
                                     ValueType::kS128, ValueType::kS128>,
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379

      &WasmGenerator::op_with_prefix<kExprI32x4Splat, ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI32x4Eq, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4Ne, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4LtS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4LtU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4GtS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4GtU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4LeS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4LeU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4GeS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4GeU, ValueType::kS128,
                                     ValueType::kS128>,
1380
      &WasmGenerator::op_with_prefix<kExprI32x4Abs, ValueType::kS128>,
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
      &WasmGenerator::op_with_prefix<kExprI32x4Neg, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4Shl, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI32x4ShrS, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI32x4ShrU, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI32x4Add, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4Sub, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4Mul, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4MinS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4MinU, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4MaxS, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4MaxU, ValueType::kS128,
                                     ValueType::kS128>,

      &WasmGenerator::op_with_prefix<kExprI64x2Splat, ValueType::kI64>,
      &WasmGenerator::op_with_prefix<kExprI64x2Neg, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI64x2Shl, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI64x2ShrS, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI64x2ShrU, ValueType::kS128,
                                     ValueType::kI32>,
      &WasmGenerator::op_with_prefix<kExprI64x2Add, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI64x2Sub, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI64x2Mul, ValueType::kS128,
                                     ValueType::kS128>,

      &WasmGenerator::op_with_prefix<kExprF32x4Splat, ValueType::kF32>,
      &WasmGenerator::op_with_prefix<kExprF32x4Eq, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Ne, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Lt, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Gt, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Le, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Ge, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Abs, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Neg, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Sqrt, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Add, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Sub, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Mul, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Div, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Min, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Max, ValueType::kS128,
                                     ValueType::kS128>,
1446 1447 1448 1449
      &WasmGenerator::op_with_prefix<kExprF32x4Pmin, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Pmax, ValueType::kS128,
                                     ValueType::kS128>,
1450 1451 1452 1453
      &WasmGenerator::op_with_prefix<kExprF32x4Ceil, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Floor, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4Trunc, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4NearestInt, ValueType::kS128>,
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482

      &WasmGenerator::op_with_prefix<kExprF64x2Splat, ValueType::kF64>,
      &WasmGenerator::op_with_prefix<kExprF64x2Eq, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Ne, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Lt, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Gt, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Le, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Ge, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Abs, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Neg, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Sqrt, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Add, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Sub, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Mul, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Div, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Min, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Max, ValueType::kS128,
                                     ValueType::kS128>,
1483 1484 1485 1486
      &WasmGenerator::op_with_prefix<kExprF64x2Pmin, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Pmax, ValueType::kS128,
                                     ValueType::kS128>,
1487 1488 1489 1490
      &WasmGenerator::op_with_prefix<kExprF64x2Ceil, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Floor, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2Trunc, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF64x2NearestInt, ValueType::kS128>,
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521

      &WasmGenerator::op_with_prefix<kExprI32x4SConvertF32x4, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4UConvertF32x4, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4SConvertI32x4, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprF32x4UConvertI32x4, ValueType::kS128>,

      &WasmGenerator::op_with_prefix<kExprI8x16SConvertI16x8, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI8x16UConvertI16x8, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8SConvertI32x4, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8UConvertI32x4, ValueType::kS128,
                                     ValueType::kS128>,

      &WasmGenerator::op_with_prefix<kExprI16x8SConvertI8x16Low,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8SConvertI8x16High,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8UConvertI8x16Low,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI16x8UConvertI8x16High,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4SConvertI16x8Low,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4SConvertI16x8High,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4UConvertI16x8Low,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprI32x4UConvertI16x8High,
                                     ValueType::kS128>,
1522

1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
      &WasmGenerator::op_with_prefix<kExprS128Not, ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprS128And, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprS128AndNot, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprS128Or, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprS128Xor, ValueType::kS128,
                                     ValueType::kS128>,
      &WasmGenerator::op_with_prefix<kExprS128Select, ValueType::kS128,
                                     ValueType::kS128, ValueType::kS128>,

1535
      &WasmGenerator::simd_shuffle,
1536 1537
      &WasmGenerator::op_with_prefix<kExprS8x16Swizzle, ValueType::kS128,
                                     ValueType::kS128>,
1538

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
      &WasmGenerator::memop<kExprS128LoadMem>,
      &WasmGenerator::memop<kExprI16x8Load8x8S>,
      &WasmGenerator::memop<kExprI16x8Load8x8U>,
      &WasmGenerator::memop<kExprI32x4Load16x4S>,
      &WasmGenerator::memop<kExprI32x4Load16x4U>,
      &WasmGenerator::memop<kExprI64x2Load32x2S>,
      &WasmGenerator::memop<kExprI64x2Load32x2U>,
      &WasmGenerator::memop<kExprS8x16LoadSplat>,
      &WasmGenerator::memop<kExprS16x8LoadSplat>,
      &WasmGenerator::memop<kExprS32x4LoadSplat>,
      &WasmGenerator::memop<kExprS64x2LoadSplat>};
1550 1551 1552 1553

  GenerateOneOf(alternatives, data);
}

1554
void WasmGenerator::grow_memory(DataRange* data) {
1555
  Generate<ValueType::kI32>(data);
1556
  builder_->EmitWithU8(kExprMemoryGrow, 0);
1557 1558
}

1559
void WasmGenerator::Generate(ValueType type, DataRange* data) {
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
  switch (type.kind()) {
    case ValueType::kStmt:
      return Generate<ValueType::kStmt>(data);
    case ValueType::kI32:
      return Generate<ValueType::kI32>(data);
    case ValueType::kI64:
      return Generate<ValueType::kI64>(data);
    case ValueType::kF32:
      return Generate<ValueType::kF32>(data);
    case ValueType::kF64:
      return Generate<ValueType::kF64>(data);
    case ValueType::kS128:
      return Generate<ValueType::kS128>(data);
1573 1574 1575 1576
    default:
      UNREACHABLE();
  }
}
1577

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
std::vector<ValueType> WasmGenerator::GenerateTypes(DataRange* data) {
  std::vector<ValueType> types;
  int num_params = int{data->get<uint8_t>()} % (kMaxParameters + 1);
  for (int i = 0; i < num_params; ++i) {
    types.push_back(GetValueType(data));
  }
  return types;
}

void WasmGenerator::Generate(Vector<const ValueType> types, DataRange* data) {
  // Maybe emit a multi-value block with the expected return type. Use a
  // non-default value to indicate block generation to avoid recursion when we
  // reach the end of the data.
  bool generate_block = data->get<uint8_t>() % 32 == 1;
  if (generate_block) {
    GeneratorRecursionScope rec_scope(this);
    if (!recursion_limit_reached()) {
      const auto param_types = GenerateTypes(data);
      Generate(VectorOf(param_types), data);
1597
      any_block(VectorOf(param_types), types, data);
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
      return;
    }
  }

  if (types.size() == 0) {
    Generate(kWasmStmt, data);
    return;
  }
  if (types.size() == 1) {
    Generate(types[0], data);
    return;
  }

  // Split the types in two halves and recursively generate each half.
  // Each half is non empty to ensure termination.
  size_t split_index = data->get<uint8_t>() % (types.size() - 1) + 1;
1614 1615
  Vector<const ValueType> lower_half = types.SubVector(0, split_index);
  Vector<const ValueType> upper_half =
1616
      types.SubVector(split_index, types.size());
1617 1618 1619 1620 1621 1622
  DataRange first_range = data->split();
  Generate(lower_half, &first_range);
  Generate(upper_half, data);
}

// Emit code to match an arbitrary signature.
1623 1624 1625
void WasmGenerator::ConsumeAndGenerate(Vector<const ValueType> param_types,
                                       Vector<const ValueType> return_types,
                                       DataRange* data) {
1626
  if (param_types.size() == 0) {
1627
    Generate(return_types, data);
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
    return;
  }
  // Keep exactly one of the parameters on the stack with a combination of drops
  // and selects, convert this value to the first return type, and generate the
  // remaining types.
  // TODO(thibaudm): Improve this strategy to potentially generate any sequence
  // of instructions matching the given signature.
  size_t return_index = data->get<uint8_t>() % param_types.size();
  for (size_t i = param_types.size() - 1; i > return_index; --i) {
    builder_->Emit(kExprDrop);
  }
  for (size_t i = return_index; i > 0; --i) {
    Convert(param_types[i], param_types[i - 1]);
    builder_->EmitI32Const(0);
    builder_->Emit(kExprSelect);
  }
  if (return_types.empty()) {
    builder_->Emit(kExprDrop);
  } else {
    Convert(param_types[0], return_types[0]);
1648
    Generate(return_types + 1, data);
1649 1650 1651
  }
}

1652
FunctionSig* GenerateSig(Zone* zone, DataRange* data) {
1653
  // Generate enough parameters to spill some to the stack.
1654
  int num_params = int{data->get<uint8_t>()} % (kMaxParameters + 1);
1655
  int num_returns = int{data->get<uint8_t>()} % (kMaxReturns + 1);
1656

1657 1658
  FunctionSig::Builder builder(zone, num_returns, num_params);
  for (int i = 0; i < num_returns; ++i) builder.AddReturn(GetValueType(data));
1659 1660 1661 1662
  for (int i = 0; i < num_params; ++i) builder.AddParam(GetValueType(data));
  return builder.Build();
}

1663
}  // namespace
1664

1665
class WasmCompileFuzzer : public WasmExecutionFuzzer {
1666
  bool GenerateModule(
1667
      Isolate* isolate, Zone* zone, Vector<const uint8_t> data,
1668 1669 1670
      ZoneBuffer* buffer, int32_t* num_args,
      std::unique_ptr<WasmValue[]>* interpreter_args,
      std::unique_ptr<Handle<Object>[]>* compiler_args) override {
1671
    TestSignatures sigs;
1672

1673
    WasmModuleBuilder builder(zone);
1674

1675
    DataRange range(data);
1676 1677
    std::vector<uint32_t> function_signatures;
    function_signatures.push_back(builder.AddSignature(sigs.i_iii()));
1678

1679 1680 1681 1682
    static_assert(kMaxFunctions >= 1, "need min. 1 function");
    int num_functions = 1 + (range.get<uint8_t>() % kMaxFunctions);

    for (int i = 1; i < num_functions; ++i) {
1683 1684 1685
      FunctionSig* sig = GenerateSig(zone, &range);
      uint32_t signature_index = builder.AddSignature(sig);
      function_signatures.push_back(signature_index);
1686 1687
    }

1688 1689 1690 1691 1692 1693 1694
    int num_globals = range.get<uint8_t>() % (kMaxGlobals + 1);
    std::vector<ValueType> globals;
    std::vector<uint8_t> mutable_globals;
    globals.reserve(num_globals);
    mutable_globals.reserve(num_globals);

    for (int i = 0; i < num_globals; ++i) {
1695
      ValueType type = GetValueType(&range);
1696 1697
      // 1/8 of globals are immutable.
      const bool mutability = (range.get<uint8_t>() % 8) != 0;
1698
      builder.AddGlobal(type, mutability, WasmInitExpr());
1699 1700 1701 1702
      globals.push_back(type);
      if (mutability) mutable_globals.push_back(static_cast<uint8_t>(i));
    }

1703 1704 1705 1706
    for (int i = 0; i < num_functions; ++i) {
      DataRange function_range =
          i == num_functions - 1 ? std::move(range) : range.split();

1707
      FunctionSig* sig = builder.GetSignature(function_signatures[i]);
1708 1709
      WasmFunctionBuilder* f = builder.AddFunction(sig);

1710
      WasmGenerator gen(f, function_signatures, globals, mutable_globals,
1711
                        &function_range);
1712 1713 1714
      Vector<const ValueType> return_types(sig->returns().begin(),
                                           sig->return_count());
      gen.Generate(return_types, &function_range);
1715 1716 1717 1718

      f->Emit(kExprEnd);
      if (i == 0) builder.AddExport(CStrVector("main"), f);
    }
1719

1720 1721 1722 1723 1724
    builder.AllocateIndirectFunctions(num_functions);
    for (int i = 0; i < num_functions; ++i) {
      builder.SetIndirectFunction(i, i);
    }

1725
    builder.SetMaxMemorySize(32);
1726 1727
    // We enable shared memory to be able to test atomics.
    builder.SetHasSharedMemory();
1728
    builder.WriteTo(buffer);
1729

1730 1731
    *num_args = 3;
    interpreter_args->reset(
1732
        new WasmValue[3]{WasmValue(1), WasmValue(2), WasmValue(3)});
1733

1734 1735 1736 1737
    compiler_args->reset(new Handle<Object>[3] {
      handle(Smi::FromInt(1), isolate), handle(Smi::FromInt(2), isolate),
          handle(Smi::FromInt(3), isolate)
    });
1738
    return true;
1739
  }
1740
};
1741

1742
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
1743
  constexpr bool require_valid = true;
1744
  EXPERIMENTAL_FLAG_SCOPE(reftypes);
1745 1746
  WasmCompileFuzzer().FuzzWasmModule({data, size}, require_valid);
  return 0;
1747
}
1748 1749 1750 1751 1752

}  // namespace fuzzer
}  // namespace wasm
}  // namespace internal
}  // namespace v8