Coverage Report

Created: 2025-09-27 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/include/executor/executor.h
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: 2019-2024 Second State INC
3
4
//===-- wasmedge/executor/executor.h - Executor class definition ----------===//
5
//
6
// Part of the WasmEdge Project.
7
//
8
//===----------------------------------------------------------------------===//
9
///
10
/// \file
11
/// This file contains the declaration of the Executor class, which instantiate
12
/// and run Wasm modules.
13
///
14
//===----------------------------------------------------------------------===//
15
#pragma once
16
17
#include "ast/component/component.h"
18
#include "ast/module.h"
19
#include "common/async.h"
20
#include "common/configure.h"
21
#include "common/defines.h"
22
#include "common/errcode.h"
23
#include "common/statistics.h"
24
#include "common/types.h"
25
#include "runtime/callingframe.h"
26
#include "runtime/instance/component/component.h"
27
#include "runtime/instance/module.h"
28
#include "runtime/stackmgr.h"
29
#include "runtime/storemgr.h"
30
31
#include <atomic>
32
#include <condition_variable>
33
#include <csignal>
34
#include <cstdint>
35
#include <functional>
36
#include <memory>
37
#include <mutex>
38
#include <optional>
39
#include <shared_mutex>
40
#include <string_view>
41
#include <type_traits>
42
#include <utility>
43
#include <vector>
44
45
namespace WasmEdge {
46
namespace Executor {
47
48
namespace {
49
50
// Template return type aliasing
51
/// Accept unsigned integer types. (uint32_t, uint64_t)
52
template <typename T>
53
using TypeU = typename std::enable_if_t<IsWasmUnsignV<T>, Expect<void>>;
54
/// Accept integer types. (uint32_t, int32_t, uint64_t, int64_t)
55
template <typename T>
56
using TypeI = typename std::enable_if_t<IsWasmIntV<T>, Expect<void>>;
57
/// Accept floating types. (float, double)
58
template <typename T>
59
using TypeF = typename std::enable_if_t<IsWasmFloatV<T>, Expect<void>>;
60
/// Accept all num types. (uint32_t, int32_t, uint64_t, int64_t, float, double)
61
template <typename T>
62
using TypeT = typename std::enable_if_t<IsWasmNumV<T>, Expect<void>>;
63
/// Accept Wasm built-in num types. (uint32_t, uint64_t, float, double)
64
template <typename T>
65
using TypeN = typename std::enable_if_t<IsWasmNativeNumV<T>, Expect<void>>;
66
67
/// Accept (unsigned integer types, unsigned integer types).
68
template <typename T1, typename T2>
69
using TypeUU = typename std::enable_if_t<IsWasmUnsignV<T1> && IsWasmUnsignV<T2>,
70
                                         Expect<void>>;
71
/// Accept (integer types, unsigned integer types).
72
template <typename T1, typename T2>
73
using TypeIU = typename std::enable_if_t<IsWasmIntV<T1> && IsWasmUnsignV<T2>,
74
                                         Expect<void>>;
75
/// Accept (floating types, floating types).
76
template <typename T1, typename T2>
77
using TypeFF = typename std::enable_if_t<IsWasmFloatV<T1> && IsWasmFloatV<T2>,
78
                                         Expect<void>>;
79
/// Accept (integer types, floating types).
80
template <typename T1, typename T2>
81
using TypeIF =
82
    typename std::enable_if_t<IsWasmIntV<T1> && IsWasmFloatV<T2>, Expect<void>>;
83
/// Accept (floating types, integer types).
84
template <typename T1, typename T2>
85
using TypeFI =
86
    typename std::enable_if_t<IsWasmFloatV<T1> && IsWasmIntV<T2>, Expect<void>>;
87
/// Accept (Wasm built-in num types, Wasm built-in num types).
88
template <typename T1, typename T2>
89
using TypeNN =
90
    typename std::enable_if_t<IsWasmNativeNumV<T1> && IsWasmNativeNumV<T2> &&
91
                                  sizeof(T1) == sizeof(T2),
92
                              Expect<void>>;
93
94
} // namespace
95
96
/// Helper class for handling the pre- and post- host functions
97
class HostFuncHandler {
98
public:
99
0
  void setPreHost(void *HostData, std::function<void(void *)> HostFunc) {
100
0
    std::unique_lock Lock(Mutex);
101
0
    PreHostData = HostData;
102
0
    PreHostFunc = HostFunc;
103
0
  }
104
0
  void setPostHost(void *HostData, std::function<void(void *)> HostFunc) {
105
0
    std::unique_lock Lock(Mutex);
106
0
    PostHostData = HostData;
107
0
    PostHostFunc = HostFunc;
108
0
  }
109
0
  void invokePreHostFunc() {
110
0
    if (PreHostFunc.operator bool()) {
111
0
      PreHostFunc(PreHostData);
112
0
    }
113
0
  }
114
0
  void invokePostHostFunc() {
115
0
    if (PostHostFunc.operator bool()) {
116
0
      PostHostFunc(PostHostData);
117
0
    }
118
0
  }
119
120
private:
121
  void *PreHostData = nullptr;
122
  void *PostHostData = nullptr;
123
  std::function<void(void *)> PreHostFunc = {};
124
  std::function<void(void *)> PostHostFunc = {};
125
  mutable std::shared_mutex Mutex;
126
};
127
128
/// Executor flow control class.
129
class Executor {
130
public:
131
  Executor(const Configure &Conf, Statistics::Statistics *S = nullptr) noexcept
132
0
      : Conf(Conf) {
133
0
    if (Conf.getStatisticsConfigure().isInstructionCounting() ||
134
0
        Conf.getStatisticsConfigure().isCostMeasuring() ||
135
0
        Conf.getStatisticsConfigure().isTimeMeasuring()) {
136
0
      Stat = S;
137
0
    } else {
138
0
      Stat = nullptr;
139
0
    }
140
0
    if (Stat) {
141
0
      Stat->setCostLimit(Conf.getStatisticsConfigure().getCostLimit());
142
0
    }
143
0
  }
144
145
  /// Getter of Configure
146
0
  const Configure &getConfigure() const { return Conf; }
147
148
  /// Instantiate a WASM Module into an anonymous module instance.
149
  Expect<std::unique_ptr<Runtime::Instance::ModuleInstance>>
150
  instantiateModule(Runtime::StoreManager &StoreMgr, const AST::Module &Mod);
151
152
  /// Instantiate and register a WASM module into a named module instance.
153
  Expect<std::unique_ptr<Runtime::Instance::ModuleInstance>>
154
  registerModule(Runtime::StoreManager &StoreMgr, const AST::Module &Mod,
155
                 std::string_view Name);
156
157
  /// Register an instantiated module into a named module instance.
158
  Expect<void> registerModule(Runtime::StoreManager &StoreMgr,
159
                              const Runtime::Instance::ModuleInstance &ModInst);
160
161
  /// Instantiate a Component into an anonymous component instance.
162
  Expect<std::unique_ptr<Runtime::Instance::ComponentInstance>>
163
  instantiateComponent(Runtime::StoreManager &StoreMgr,
164
                       const AST::Component::Component &Comp);
165
166
  /// Instantiate and register a Component into a named component instance.
167
  Expect<std::unique_ptr<Runtime::Instance::ComponentInstance>>
168
  registerComponent(Runtime::StoreManager &StoreMgr,
169
                    const AST::Component::Component &Comp,
170
                    std::string_view Name);
171
172
  /// Register an instantiated component into a named component instance.
173
  Expect<void>
174
  registerComponent(Runtime::StoreManager &StoreMgr,
175
                    const Runtime::Instance::ComponentInstance &CompInst);
176
177
  /// Register a host function which will be invoked before calling a
178
  /// host function.
179
  Expect<void> registerPreHostFunction(void *HostData,
180
                                       std::function<void(void *)> HostFunc);
181
182
  /// Register a host function which will be invoked after calling a
183
  /// host function.
184
  Expect<void> registerPostHostFunction(void *HostData,
185
                                        std::function<void(void *)> HostFunc);
186
187
  /// Invoke a WASM function by function instance.
188
  Expect<std::vector<std::pair<ValVariant, ValType>>>
189
  invoke(const Runtime::Instance::FunctionInstance *FuncInst,
190
         Span<const ValVariant> Params, Span<const ValType> ParamTypes);
191
192
  /// Invoke a Component function by function instance.
193
  Expect<std::vector<std::pair<ValInterface, ValType>>>
194
  invoke(const Runtime::Instance::Component::FunctionInstance *FuncInst,
195
         Span<const ValInterface> Params, Span<const ValType> ParamTypes);
196
197
  /// Asynchronous invoke a WASM function by function instance.
198
  Async<Expect<std::vector<std::pair<ValVariant, ValType>>>>
199
  asyncInvoke(const Runtime::Instance::FunctionInstance *FuncInst,
200
              Span<const ValVariant> Params, Span<const ValType> ParamTypes);
201
202
  /// Stop execution
203
0
  void stop() noexcept {
204
0
    StopToken.store(1, std::memory_order_relaxed);
205
0
    atomicNotifyAll();
206
0
  }
207
208
private:
209
  /// Run Wasm bytecode expression for initialization.
210
  Expect<void> runExpression(Runtime::StackManager &StackMgr,
211
                             AST::InstrView Instrs);
212
213
  /// Run Wasm function.
214
  Expect<void> runFunction(Runtime::StackManager &StackMgr,
215
                           const Runtime::Instance::FunctionInstance &Func,
216
                           Span<const ValVariant> Params);
217
218
  /// Execute instructions.
219
  Expect<void> execute(Runtime::StackManager &StackMgr,
220
                       const AST::InstrView::iterator Start,
221
                       const AST::InstrView::iterator End);
222
223
  /// \name Functions for instantiation.
224
  /// @{
225
  /// Instantiation of Module Instance.
226
  Expect<std::unique_ptr<Runtime::Instance::ModuleInstance>>
227
  instantiate(Runtime::StoreManager &StoreMgr, const AST::Module &Mod,
228
              std::optional<std::string_view> Name = std::nullopt);
229
230
  /// Instantiation of Imports.
231
  Expect<void> instantiate(
232
      std::function<const Runtime::Instance::ModuleInstance *(std::string_view)>
233
          ModuleFinder,
234
      Runtime::Instance::ModuleInstance &ModInst,
235
      const AST::ImportSection &ImportSec);
236
237
  /// Instantiation of Function Instances.
238
  Expect<void> instantiate(Runtime::Instance::ModuleInstance &ModInst,
239
                           const AST::FunctionSection &FuncSec,
240
                           const AST::CodeSection &CodeSec);
241
242
  /// Instantiation of Table Instances.
243
  Expect<void> instantiate(Runtime::StackManager &StackMgr,
244
                           Runtime::Instance::ModuleInstance &ModInst,
245
                           const AST::TableSection &TabSec);
246
247
  /// Instantiation of Memory Instances.
248
  Expect<void> instantiate(Runtime::Instance::ModuleInstance &ModInst,
249
                           const AST::MemorySection &MemSec);
250
251
  /// Instantiateion of Tag Instances.
252
  Expect<void> instantiate(Runtime::Instance::ModuleInstance &ModInst,
253
                           const AST::TagSection &TagSec);
254
255
  /// Instantiation of Global Instances.
256
  Expect<void> instantiate(Runtime::StackManager &StackMgr,
257
                           Runtime::Instance::ModuleInstance &ModInst,
258
                           const AST::GlobalSection &GlobSec);
259
260
  /// Instantiation of Element Instances.
261
  Expect<void> instantiate(Runtime::StackManager &StackMgr,
262
                           Runtime::Instance::ModuleInstance &ModInst,
263
                           const AST::ElementSection &ElemSec);
264
265
  /// Initialize table with Element Instances.
266
  Expect<void> initTable(Runtime::StackManager &StackMgr,
267
                         const AST::ElementSection &ElemSec);
268
269
  /// Instantiation of Data Instances.
270
  Expect<void> instantiate(Runtime::StackManager &StackMgr,
271
                           Runtime::Instance::ModuleInstance &ModInst,
272
                           const AST::DataSection &DataSec);
273
274
  /// Initialize memory with Data Instances.
275
  Expect<void> initMemory(Runtime::StackManager &StackMgr,
276
                          const AST::DataSection &DataSec);
277
278
  /// Instantiation of Exports.
279
  Expect<void> instantiate(Runtime::Instance::ModuleInstance &ModInst,
280
                           const AST::ExportSection &ExportSec);
281
  /// @}
282
283
  /// \name Functions for instantiation of component model.
284
  /// @{
285
  /// Instantiation of Component Instance.
286
  Expect<std::unique_ptr<Runtime::Instance::ComponentInstance>>
287
  instantiate(Runtime::StoreManager &StoreMgr,
288
              const AST::Component::Component &Comp,
289
              std::optional<std::string_view> Name = std::nullopt);
290
291
  /// Instantiation of Child Component Instance.
292
  Expect<std::unique_ptr<Runtime::Instance::ComponentInstance>>
293
  instantiate(Runtime::Instance::ComponentImportManager &ImportMgr,
294
              const AST::Component::Component &Comp);
295
296
  /// Instantiation of Child Core Module Instance.
297
  Expect<std::unique_ptr<Runtime::Instance::ModuleInstance>>
298
  instantiate(Runtime::Instance::ComponentImportManager &ImportMgr,
299
              const AST::Module &Mod);
300
301
  /// Instantiation of Core Module Section.
302
  Expect<void> instantiate(Runtime::Instance::ComponentInstance &CompInst,
303
                           const AST::Component::CoreModuleSection &CoreModSec);
304
305
  /// Instantiation of Core Instance Section.
306
  Expect<void>
307
  instantiate(Runtime::Instance::ComponentInstance &CompInst,
308
              const AST::Component::CoreInstanceSection &CoreInstSec);
309
310
  /// Instantiation of Core Type Section.
311
  Expect<void> instantiate(Runtime::Instance::ComponentInstance &CompInst,
312
                           const AST::Component::CoreTypeSection &CoreTypeSec);
313
314
  /// Instantiation of Component Section.
315
  Expect<void> instantiate(Runtime::Instance::ComponentInstance &CompInst,
316
                           const AST::Component::ComponentSection &CompSec);
317
318
  /// Instantiation of Instance Section.
319
  Expect<void> instantiate(Runtime::Instance::ComponentInstance &CompInst,
320
                           const AST::Component::InstanceSection &InstSec);
321
322
  /// Instantiation of Alias Section.
323
  Expect<void> instantiate(Runtime::Instance::ComponentInstance &CompInst,
324
                           const AST::Component::AliasSection &AliasSec);
325
326
  /// Instantiation of Type Section.
327
  Expect<void> instantiate(Runtime::Instance::ComponentInstance &CompInst,
328
                           const AST::Component::TypeSection &TypeSec);
329
330
  /// Instantiation of Canonical Section.
331
  Expect<void> instantiate(Runtime::Instance::ComponentInstance &CompInst,
332
                           const AST::Component::CanonSection &CanonSec);
333
334
  /// Instantiation of Start Section.
335
  Expect<void> instantiate(Runtime::Instance::ComponentInstance &CompInst,
336
                           const AST::Component::StartSection &StartSec);
337
338
  /// Instantiation of Import Section.
339
  Expect<void> instantiate(Runtime::StoreManager &StoreMgr,
340
                           Runtime::Instance::ComponentInstance &CompInst,
341
                           const AST::Component::ImportSection &ImportSec);
342
343
  /// Instantiation of Import Section in the child component instance.
344
  Expect<void> instantiate(Runtime::Instance::ComponentImportManager &ImportMgr,
345
                           Runtime::Instance::ComponentInstance &CompInst,
346
                           const AST::Component::ImportSection &ImportSec);
347
348
  /// Instantiation of Export Section.
349
  Expect<void> instantiate(Runtime::Instance::ComponentInstance &CompInst,
350
                           const AST::Component::ExportSection &ExportSec);
351
  /// @}
352
353
  /// \name Helper Functions for canonical ABI
354
  /// @{
355
  std::unique_ptr<Runtime::Instance::Component::FunctionInstance>
356
  lifting(Runtime::Instance::ComponentInstance &Comp,
357
          const WasmEdge::AST::Component::FuncType &FuncType,
358
          Runtime::Instance::FunctionInstance *F,
359
          Runtime::Instance::MemoryInstance *Memory,
360
          Runtime::Instance::FunctionInstance *Realloc);
361
362
  std::unique_ptr<Runtime::Instance::FunctionInstance>
363
  lowering(Runtime::Instance::Component::FunctionInstance *F,
364
           Runtime::Instance::MemoryInstance *Memory,
365
           Runtime::Instance::FunctionInstance *Realloc);
366
  /// @}
367
368
  /// \name Helper Functions for block controls.
369
  /// @{
370
  /// Helper function for calling functions. Return the continuation iterator.
371
  Expect<AST::InstrView::iterator>
372
  enterFunction(Runtime::StackManager &StackMgr,
373
                const Runtime::Instance::FunctionInstance &Func,
374
                const AST::InstrView::iterator RetIt, bool IsTailCall = false);
375
376
  /// Helper function for branching to label.
377
  Expect<void> branchToLabel(Runtime::StackManager &StackMgr,
378
                             const AST::Instruction::JumpDescriptor &JumpDesc,
379
                             AST::InstrView::iterator &PC) noexcept;
380
381
  /// Helper function for throwing an exception.
382
  Expect<void> throwException(Runtime::StackManager &StackMgr,
383
                              Runtime::Instance::TagInstance &TagInst,
384
                              AST::InstrView::iterator &PC) noexcept;
385
  /// @}
386
387
  /// \name Helper Functions for GC instructions.
388
  /// @{
389
  Expect<RefVariant> structNew(Runtime::StackManager &StackMgr,
390
                               const uint32_t TypeIdx,
391
                               Span<const ValVariant> Args = {}) const noexcept;
392
  Expect<ValVariant> structGet(Runtime::StackManager &StackMgr,
393
                               const RefVariant Ref, const uint32_t TypeIdx,
394
                               const uint32_t Off,
395
                               const bool IsSigned = false) const noexcept;
396
  Expect<void> structSet(Runtime::StackManager &StackMgr, const RefVariant Ref,
397
                         const ValVariant Val, const uint32_t TypeIdx,
398
                         const uint32_t Off) const noexcept;
399
  Expect<RefVariant> arrayNew(Runtime::StackManager &StackMgr,
400
                              const uint32_t TypeIdx, const uint32_t Length,
401
                              Span<const ValVariant> Args = {}) const noexcept;
402
  Expect<RefVariant> arrayNewData(Runtime::StackManager &StackMgr,
403
                                  const uint32_t TypeIdx,
404
                                  const uint32_t DataIdx, const uint32_t Start,
405
                                  const uint32_t Length) const noexcept;
406
  Expect<RefVariant> arrayNewElem(Runtime::StackManager &StackMgr,
407
                                  const uint32_t TypeIdx,
408
                                  const uint32_t ElemIdx, const uint32_t Start,
409
                                  const uint32_t Length) const noexcept;
410
  Expect<ValVariant> arrayGet(Runtime::StackManager &StackMgr,
411
                              const RefVariant &Ref, const uint32_t TypeIdx,
412
                              const uint32_t Idx,
413
                              const bool IsSigned = false) const noexcept;
414
  Expect<void> arraySet(Runtime::StackManager &StackMgr, const RefVariant &Ref,
415
                        const ValVariant &Val, const uint32_t TypeIdx,
416
                        const uint32_t Idx) const noexcept;
417
  Expect<void> arrayFill(Runtime::StackManager &StackMgr, const RefVariant &Ref,
418
                         const ValVariant &Val, const uint32_t TypeIdx,
419
                         const uint32_t Idx, const uint32_t Cnt) const noexcept;
420
  Expect<void> arrayInitData(Runtime::StackManager &StackMgr,
421
                             const RefVariant &Ref, const uint32_t TypeIdx,
422
                             const uint32_t DataIdx, const uint32_t DstIdx,
423
                             const uint32_t SrcIdx,
424
                             const uint32_t Cnt) const noexcept;
425
  Expect<void> arrayInitElem(Runtime::StackManager &StackMgr,
426
                             const RefVariant &Ref, const uint32_t TypeIdx,
427
                             const uint32_t ElemIdx, const uint32_t DstIdx,
428
                             const uint32_t SrcIdx,
429
                             const uint32_t Cnt) const noexcept;
430
  Expect<void> arrayCopy(Runtime::StackManager &StackMgr,
431
                         const RefVariant &DstRef, const uint32_t DstTypeIdx,
432
                         const uint32_t DstIdx, const RefVariant &SrcRef,
433
                         const uint32_t SrcTypeIdx, const uint32_t SrcIdx,
434
                         const uint32_t Cnt) const noexcept;
435
  /// @}
436
437
  /// \name Helper Functions for atomic operations.
438
  /// @{
439
  template <typename T>
440
  Expect<uint32_t> atomicWait(Runtime::Instance::MemoryInstance &MemInst,
441
                              uint32_t Address, EndianValue<T> Expected,
442
                              int64_t Timeout) noexcept;
443
  Expect<uint32_t> atomicNotify(Runtime::Instance::MemoryInstance &MemInst,
444
                                uint32_t Address, uint32_t Count) noexcept;
445
  void atomicNotifyAll() noexcept;
446
  /// @}
447
448
  /// \name Helper Functions for getting instances or types.
449
  /// @{
450
  /// Helper function for getting defined type by index.
451
  const AST::SubType *getDefTypeByIdx(Runtime::StackManager &StackMgr,
452
                                      const uint32_t Idx) const;
453
454
  /// Helper function for getting composite type by index. Assuming validated.
455
  const WasmEdge::AST::CompositeType &
456
  getCompositeTypeByIdx(Runtime::StackManager &StackMgr,
457
                        const uint32_t Idx) const noexcept;
458
459
  /// Helper function for getting struct storage type by index.
460
  const ValType &getStructStorageTypeByIdx(Runtime::StackManager &StackMgr,
461
                                           const uint32_t Idx,
462
                                           const uint32_t Off) const noexcept;
463
464
  /// Helper function for getting array storage type by index.
465
  const ValType &getArrayStorageTypeByIdx(Runtime::StackManager &StackMgr,
466
                                          const uint32_t Idx) const noexcept;
467
468
  /// Helper function for getting function instance by index.
469
  Runtime::Instance::FunctionInstance *
470
  getFuncInstByIdx(Runtime::StackManager &StackMgr, const uint32_t Idx) const;
471
472
  /// Helper function for getting table instance by index.
473
  Runtime::Instance::TableInstance *
474
  getTabInstByIdx(Runtime::StackManager &StackMgr, const uint32_t Idx) const;
475
476
  /// Helper function for getting memory instance by index.
477
  Runtime::Instance::MemoryInstance *
478
  getMemInstByIdx(Runtime::StackManager &StackMgr, const uint32_t Idx) const;
479
480
  /// Helper function for getting tag instance by index.
481
  Runtime::Instance::TagInstance *
482
  getTagInstByIdx(Runtime::StackManager &StackMgr, const uint32_t Idx) const;
483
484
  /// Helper function for getting global instance by index.
485
  Runtime::Instance::GlobalInstance *
486
  getGlobInstByIdx(Runtime::StackManager &StackMgr, const uint32_t Idx) const;
487
488
  /// Helper function for getting element instance by index.
489
  Runtime::Instance::ElementInstance *
490
  getElemInstByIdx(Runtime::StackManager &StackMgr, const uint32_t Idx) const;
491
492
  /// Helper function for getting data instance by index.
493
  Runtime::Instance::DataInstance *
494
  getDataInstByIdx(Runtime::StackManager &StackMgr, const uint32_t Idx) const;
495
496
  /// Helper function for converting into bottom abstract heap type.
497
  TypeCode toBottomType(Runtime::StackManager &StackMgr,
498
                        const ValType &Type) const;
499
500
  /// Helper function for cleaning unused bits of numeric values in ValVariant.
501
  void cleanNumericVal(ValVariant &Val, const ValType &Type) const noexcept;
502
503
  /// Helper function for packing ValVariant for packed type.
504
  ValVariant packVal(const ValType &Type, const ValVariant &Val) const noexcept;
505
506
  /// Helper function for packing ValVariant vector for packed type.
507
  std::vector<ValVariant>
508
  packVals(const ValType &Type, std::vector<ValVariant> &&Vals) const noexcept;
509
510
  /// Helper function for unpacking ValVariant for packed type.
511
  ValVariant unpackVal(const ValType &Type, const ValVariant &Val,
512
                       bool IsSigned = false) const noexcept;
513
  /// @}
514
515
  /// \name Interpreter - Run instructions functions
516
  /// @{
517
  /// ======= Control instructions =======
518
  Expect<void> runIfElseOp(Runtime::StackManager &StackMgr,
519
                           const AST::Instruction &Instr,
520
                           AST::InstrView::iterator &PC) noexcept;
521
  Expect<void> runThrowOp(Runtime::StackManager &StackMgr,
522
                          const AST::Instruction &Instr,
523
                          AST::InstrView::iterator &PC) noexcept;
524
  Expect<void> runThrowRefOp(Runtime::StackManager &StackMgr,
525
                             const AST::Instruction &Instr,
526
                             AST::InstrView::iterator &PC) noexcept;
527
  Expect<void> runBrOp(Runtime::StackManager &StackMgr,
528
                       const AST::Instruction &Instr,
529
                       AST::InstrView::iterator &PC) noexcept;
530
  Expect<void> runBrIfOp(Runtime::StackManager &StackMgr,
531
                         const AST::Instruction &Instr,
532
                         AST::InstrView::iterator &PC) noexcept;
533
  Expect<void> runBrOnNullOp(Runtime::StackManager &StackMgr,
534
                             const AST::Instruction &Instr,
535
                             AST::InstrView::iterator &PC) noexcept;
536
  Expect<void> runBrOnNonNullOp(Runtime::StackManager &StackMgr,
537
                                const AST::Instruction &Instr,
538
                                AST::InstrView::iterator &PC) noexcept;
539
  Expect<void> runBrTableOp(Runtime::StackManager &StackMgr,
540
                            const AST::Instruction &Instr,
541
                            AST::InstrView::iterator &PC) noexcept;
542
  Expect<void> runBrOnCastOp(Runtime::StackManager &StackMgr,
543
                             const AST::Instruction &Instr,
544
                             AST::InstrView::iterator &PC,
545
                             bool IsReverse = false) noexcept;
546
  Expect<void> runReturnOp(Runtime::StackManager &StackMgr,
547
                           AST::InstrView::iterator &PC) noexcept;
548
  Expect<void> runCallOp(Runtime::StackManager &StackMgr,
549
                         const AST::Instruction &Instr,
550
                         AST::InstrView::iterator &PC,
551
                         bool IsTailCall = false) noexcept;
552
  Expect<void> runCallRefOp(Runtime::StackManager &StackMgr,
553
                            const AST::Instruction &Instr,
554
                            AST::InstrView::iterator &PC,
555
                            bool IsTailCall = false) noexcept;
556
  Expect<void> runCallIndirectOp(Runtime::StackManager &StackMgr,
557
                                 const AST::Instruction &Instr,
558
                                 AST::InstrView::iterator &PC,
559
                                 bool IsTailCall = false) noexcept;
560
  Expect<void> runTryTableOp(Runtime::StackManager &StackMgr,
561
                             const AST::Instruction &Instr,
562
                             AST::InstrView::iterator &PC) noexcept;
563
  /// ======= Variable instructions =======
564
  Expect<void> runLocalGetOp(Runtime::StackManager &StackMgr,
565
                             uint32_t StackOffset) const noexcept;
566
  Expect<void> runLocalSetOp(Runtime::StackManager &StackMgr,
567
                             uint32_t StackOffset) const noexcept;
568
  Expect<void> runLocalTeeOp(Runtime::StackManager &StackMgr,
569
                             uint32_t StackOffset) const noexcept;
570
  Expect<void> runGlobalGetOp(Runtime::StackManager &StackMgr,
571
                              uint32_t Idx) const noexcept;
572
  Expect<void> runGlobalSetOp(Runtime::StackManager &StackMgr,
573
                              uint32_t Idx) const noexcept;
574
  /// ======= Reference instructions =======
575
  Expect<void> runRefNullOp(Runtime::StackManager &StackMgr,
576
                            const ValType &Type) const noexcept;
577
  Expect<void> runRefIsNullOp(ValVariant &Val) const noexcept;
578
  Expect<void> runRefFuncOp(Runtime::StackManager &StackMgr,
579
                            uint32_t Idx) const noexcept;
580
  Expect<void> runRefEqOp(ValVariant &Val1,
581
                          const ValVariant &Val2) const noexcept;
582
  Expect<void> runRefAsNonNullOp(RefVariant &Val,
583
                                 const AST::Instruction &Instr) const noexcept;
584
  Expect<void> runStructNewOp(Runtime::StackManager &StackMgr,
585
                              const uint32_t TypeIdx,
586
                              const bool IsDefault = false) const noexcept;
587
  Expect<void> runStructGetOp(Runtime::StackManager &StackMgr,
588
                              const uint32_t TypeIdx, const uint32_t Off,
589
                              const AST::Instruction &Instr,
590
                              const bool IsSigned = false) const noexcept;
591
  Expect<void> runStructSetOp(Runtime::StackManager &StackMgr,
592
                              const ValVariant &Val, const uint32_t TypeIdx,
593
                              const uint32_t Off,
594
                              const AST::Instruction &Instr) const noexcept;
595
  Expect<void> runArrayNewOp(Runtime::StackManager &StackMgr,
596
                             const uint32_t TypeIdx, const uint32_t InitCnt,
597
                             uint32_t Length) const noexcept;
598
  Expect<void> runArrayNewDataOp(Runtime::StackManager &StackMgr,
599
                                 const uint32_t TypeIdx, const uint32_t DataIdx,
600
                                 const AST::Instruction &Instr) const noexcept;
601
  Expect<void> runArrayNewElemOp(Runtime::StackManager &StackMgr,
602
                                 const uint32_t TypeIdx, const uint32_t ElemIdx,
603
                                 const AST::Instruction &Instr) const noexcept;
604
  Expect<void> runArrayGetOp(Runtime::StackManager &StackMgr,
605
                             const uint32_t TypeIdx,
606
                             const AST::Instruction &Instr,
607
                             const bool IsSigned = false) const noexcept;
608
  Expect<void> runArraySetOp(Runtime::StackManager &StackMgr,
609
                             const ValVariant &Val, const uint32_t TypeIdx,
610
                             const AST::Instruction &Instr) const noexcept;
611
  Expect<void> runArrayLenOp(ValVariant &Val,
612
                             const AST::Instruction &Instr) const noexcept;
613
  Expect<void> runArrayFillOp(Runtime::StackManager &StackMgr,
614
                              const uint32_t Cnt, const ValVariant &Val,
615
                              const uint32_t TypeIdx,
616
                              const AST::Instruction &Instr) const noexcept;
617
  Expect<void> runArrayCopyOp(Runtime::StackManager &StackMgr,
618
                              const uint32_t Cnt, const uint32_t DstTypeIdx,
619
                              const uint32_t SrcTypeIdx,
620
                              const AST::Instruction &Instr) const noexcept;
621
  Expect<void> runArrayInitDataOp(Runtime::StackManager &StackMgr,
622
                                  const uint32_t Cnt, const uint32_t TypeIdx,
623
                                  const uint32_t DataIdx,
624
                                  const AST::Instruction &Instr) const noexcept;
625
  Expect<void> runArrayInitElemOp(Runtime::StackManager &StackMgr,
626
                                  const uint32_t Cnt, const uint32_t TypeIdx,
627
                                  const uint32_t ElemIdx,
628
                                  const AST::Instruction &Instr) const noexcept;
629
  Expect<void> runRefTestOp(const Runtime::Instance::ModuleInstance *ModInst,
630
                            ValVariant &Val, const AST::Instruction &Instr,
631
                            const bool IsCast = false) const noexcept;
632
  Expect<void> runRefConvOp(RefVariant &Val, TypeCode TCode) const noexcept;
633
  Expect<void> runRefI31Op(ValVariant &Val) const noexcept;
634
  Expect<void> runI31GetOp(ValVariant &Val, const AST::Instruction &Instr,
635
                           const bool IsSigned = false) const noexcept;
636
  /// ======= Table instructions =======
637
  Expect<void> runTableGetOp(Runtime::StackManager &StackMgr,
638
                             Runtime::Instance::TableInstance &TabInst,
639
                             const AST::Instruction &Instr);
640
  Expect<void> runTableSetOp(Runtime::StackManager &StackMgr,
641
                             Runtime::Instance::TableInstance &TabInst,
642
                             const AST::Instruction &Instr);
643
  Expect<void> runTableInitOp(Runtime::StackManager &StackMgr,
644
                              Runtime::Instance::TableInstance &TabInst,
645
                              Runtime::Instance::ElementInstance &ElemInst,
646
                              const AST::Instruction &Instr);
647
  Expect<void> runElemDropOp(Runtime::Instance::ElementInstance &ElemInst);
648
  Expect<void> runTableCopyOp(Runtime::StackManager &StackMgr,
649
                              Runtime::Instance::TableInstance &TabInstDst,
650
                              Runtime::Instance::TableInstance &TabInstSrc,
651
                              const AST::Instruction &Instr);
652
  Expect<void> runTableGrowOp(Runtime::StackManager &StackMgr,
653
                              Runtime::Instance::TableInstance &TabInst);
654
  Expect<void> runTableSizeOp(Runtime::StackManager &StackMgr,
655
                              Runtime::Instance::TableInstance &TabInst);
656
  Expect<void> runTableFillOp(Runtime::StackManager &StackMgr,
657
                              Runtime::Instance::TableInstance &TabInst,
658
                              const AST::Instruction &Instr);
659
  /// ======= Memory instructions =======
660
  template <typename T, uint32_t BitWidth = sizeof(T) * 8>
661
  TypeT<T> runLoadOp(Runtime::StackManager &StackMgr,
662
                     Runtime::Instance::MemoryInstance &MemInst,
663
                     const AST::Instruction &Instr);
664
  template <typename T, uint32_t BitWidth = sizeof(T) * 8>
665
  TypeN<T> runStoreOp(Runtime::StackManager &StackMgr,
666
                      Runtime::Instance::MemoryInstance &MemInst,
667
                      const AST::Instruction &Instr);
668
  Expect<void> runMemorySizeOp(Runtime::StackManager &StackMgr,
669
                               Runtime::Instance::MemoryInstance &MemInst);
670
  Expect<void> runMemoryGrowOp(Runtime::StackManager &StackMgr,
671
                               Runtime::Instance::MemoryInstance &MemInst);
672
  Expect<void> runMemoryInitOp(Runtime::StackManager &StackMgr,
673
                               Runtime::Instance::MemoryInstance &MemInst,
674
                               Runtime::Instance::DataInstance &DataInst,
675
                               const AST::Instruction &Instr);
676
  Expect<void> runDataDropOp(Runtime::Instance::DataInstance &DataInst);
677
  Expect<void> runMemoryCopyOp(Runtime::StackManager &StackMgr,
678
                               Runtime::Instance::MemoryInstance &MemInstDst,
679
                               Runtime::Instance::MemoryInstance &MemInstSrc,
680
                               const AST::Instruction &Instr);
681
  Expect<void> runMemoryFillOp(Runtime::StackManager &StackMgr,
682
                               Runtime::Instance::MemoryInstance &MemInst,
683
                               const AST::Instruction &Instr);
684
  /// ======= Test and Relation Numeric instructions =======
685
  template <typename T> TypeU<T> runEqzOp(ValVariant &Val) const;
686
  template <typename T>
687
  TypeT<T> runEqOp(ValVariant &Val1, const ValVariant &Val2) const;
688
  template <typename T>
689
  TypeT<T> runNeOp(ValVariant &Val1, const ValVariant &Val2) const;
690
  template <typename T>
691
  TypeT<T> runLtOp(ValVariant &Val1, const ValVariant &Val2) const;
692
  template <typename T>
693
  TypeT<T> runGtOp(ValVariant &Val1, const ValVariant &Val2) const;
694
  template <typename T>
695
  TypeT<T> runLeOp(ValVariant &Val1, const ValVariant &Val2) const;
696
  template <typename T>
697
  TypeT<T> runGeOp(ValVariant &Val1, const ValVariant &Val2) const;
698
  /// ======= Unary Numeric instructions =======
699
  template <typename T> TypeU<T> runClzOp(ValVariant &Val) const;
700
  template <typename T> TypeU<T> runCtzOp(ValVariant &Val) const;
701
  template <typename T> TypeU<T> runPopcntOp(ValVariant &Val) const;
702
  template <typename T> TypeF<T> runAbsOp(ValVariant &Val) const;
703
  template <typename T> TypeF<T> runNegOp(ValVariant &Val) const;
704
  template <typename T> TypeF<T> runCeilOp(ValVariant &Val) const;
705
  template <typename T> TypeF<T> runFloorOp(ValVariant &Val) const;
706
  template <typename T> TypeF<T> runTruncOp(ValVariant &Val) const;
707
  template <typename T> TypeF<T> runNearestOp(ValVariant &Val) const;
708
  template <typename T> TypeF<T> runSqrtOp(ValVariant &Val) const;
709
  /// ======= Binary Numeric instructions =======
710
  template <typename T>
711
  TypeN<T> runAddOp(ValVariant &Val1, const ValVariant &Val2) const;
712
  template <typename T>
713
  TypeN<T> runSubOp(ValVariant &Val1, const ValVariant &Val2) const;
714
  template <typename T>
715
  TypeN<T> runMulOp(ValVariant &Val1, const ValVariant &Val2) const;
716
  template <typename T>
717
  TypeT<T> runDivOp(const AST::Instruction &Instr, ValVariant &Val1,
718
                    const ValVariant &Val2) const;
719
  template <typename T>
720
  TypeI<T> runRemOp(const AST::Instruction &Instr, ValVariant &Val1,
721
                    const ValVariant &Val2) const;
722
  template <typename T>
723
  TypeU<T> runAndOp(ValVariant &Val1, const ValVariant &Val2) const;
724
  template <typename T>
725
  TypeU<T> runOrOp(ValVariant &Val1, const ValVariant &Val2) const;
726
  template <typename T>
727
  TypeU<T> runXorOp(ValVariant &Val1, const ValVariant &Val2) const;
728
  template <typename T>
729
  TypeU<T> runShlOp(ValVariant &Val1, const ValVariant &Val2) const;
730
  template <typename T>
731
  TypeI<T> runShrOp(ValVariant &Val1, const ValVariant &Val2) const;
732
  template <typename T>
733
  TypeU<T> runRotlOp(ValVariant &Val1, const ValVariant &Val2) const;
734
  template <typename T>
735
  TypeU<T> runRotrOp(ValVariant &Val1, const ValVariant &Val2) const;
736
  template <typename T>
737
  TypeF<T> runMinOp(ValVariant &Val1, const ValVariant &Val2) const;
738
  template <typename T>
739
  TypeF<T> runMaxOp(ValVariant &Val1, const ValVariant &Val2) const;
740
  template <typename T>
741
  TypeF<T> runCopysignOp(ValVariant &Val1, const ValVariant &Val2) const;
742
  /// ======= Cast Numeric instructions =======
743
  template <typename TIn, typename TOut>
744
  TypeUU<TIn, TOut> runWrapOp(ValVariant &Val) const;
745
  template <typename TIn, typename TOut>
746
  TypeFI<TIn, TOut> runTruncateOp(const AST::Instruction &Instr,
747
                                  ValVariant &Val) const;
748
  template <typename TIn, typename TOut>
749
  TypeFI<TIn, TOut> runTruncateSatOp(ValVariant &Val) const;
750
  template <typename TIn, typename TOut, size_t B = sizeof(TIn) * 8>
751
  TypeIU<TIn, TOut> runExtendOp(ValVariant &Val) const;
752
  template <typename TIn, typename TOut>
753
  TypeIF<TIn, TOut> runConvertOp(ValVariant &Val) const;
754
  template <typename TIn, typename TOut>
755
  TypeFF<TIn, TOut> runDemoteOp(ValVariant &Val) const;
756
  template <typename TIn, typename TOut>
757
  TypeFF<TIn, TOut> runPromoteOp(ValVariant &Val) const;
758
  template <typename TIn, typename TOut>
759
  TypeNN<TIn, TOut> runReinterpretOp(ValVariant &Val) const;
760
  /// ======= SIMD Memory instructions =======
761
  template <typename TIn, typename TOut>
762
  Expect<void> runLoadExpandOp(Runtime::StackManager &StackMgr,
763
                               Runtime::Instance::MemoryInstance &MemInst,
764
                               const AST::Instruction &Instr);
765
  template <typename T>
766
  Expect<void> runLoadSplatOp(Runtime::StackManager &StackMgr,
767
                              Runtime::Instance::MemoryInstance &MemInst,
768
                              const AST::Instruction &Instr);
769
  template <typename T>
770
  Expect<void> runLoadLaneOp(Runtime::StackManager &StackMgr,
771
                             Runtime::Instance::MemoryInstance &MemInst,
772
                             const AST::Instruction &Instr);
773
  template <typename T>
774
  Expect<void> runStoreLaneOp(Runtime::StackManager &StackMgr,
775
                              Runtime::Instance::MemoryInstance &MemInst,
776
                              const AST::Instruction &Instr);
777
  /// ======= SIMD Lane instructions =======
778
  template <typename TIn, typename TOut = TIn>
779
  Expect<void> runExtractLaneOp(ValVariant &Val, const uint8_t Index) const;
780
  template <typename TIn, typename TOut = TIn>
781
  Expect<void> runReplaceLaneOp(ValVariant &Val1, const ValVariant &Val2,
782
                                const uint8_t Index) const;
783
  /// ======= SIMD Numeric instructions =======
784
  template <typename TIn, typename TOut = TIn>
785
  Expect<void> runSplatOp(ValVariant &Val) const;
786
  template <typename T>
787
  Expect<void> runVectorEqOp(ValVariant &Val1, const ValVariant &Val2) const;
788
  template <typename T>
789
  Expect<void> runVectorNeOp(ValVariant &Val1, const ValVariant &Val2) const;
790
  template <typename T>
791
  Expect<void> runVectorLtOp(ValVariant &Val1, const ValVariant &Val2) const;
792
  template <typename T>
793
  Expect<void> runVectorGtOp(ValVariant &Val1, const ValVariant &Val2) const;
794
  template <typename T>
795
  Expect<void> runVectorLeOp(ValVariant &Val1, const ValVariant &Val2) const;
796
  template <typename T>
797
  Expect<void> runVectorGeOp(ValVariant &Val1, const ValVariant &Val2) const;
798
  template <typename T> Expect<void> runVectorAbsOp(ValVariant &Val) const;
799
  template <typename T> Expect<void> runVectorNegOp(ValVariant &Val) const;
800
  inline Expect<void> runVectorPopcntOp(ValVariant &Val) const;
801
  template <typename T> Expect<void> runVectorSqrtOp(ValVariant &Val) const;
802
  template <typename TIn, typename TOut>
803
  Expect<void> runVectorTruncSatOp(ValVariant &Val) const;
804
  template <typename TIn, typename TOut>
805
  Expect<void> runVectorConvertOp(ValVariant &Val) const;
806
  inline Expect<void> runVectorDemoteOp(ValVariant &Val) const;
807
  inline Expect<void> runVectorPromoteOp(ValVariant &Val) const;
808
  inline Expect<void> runVectorAnyTrueOp(ValVariant &Val) const;
809
  template <typename T> Expect<void> runVectorAllTrueOp(ValVariant &Val) const;
810
  template <typename T> Expect<void> runVectorBitMaskOp(ValVariant &Val) const;
811
  template <typename TIn, typename TOut>
812
  Expect<void> runVectorNarrowOp(ValVariant &Val1,
813
                                 const ValVariant &Val2) const;
814
  template <typename TIn, typename TOut>
815
  Expect<void> runVectorExtendLowOp(ValVariant &Val) const;
816
  template <typename TIn, typename TOut>
817
  Expect<void> runVectorExtendHighOp(ValVariant &Val) const;
818
  template <typename TIn, typename TOut>
819
  Expect<void> runVectorExtAddPairwiseOp(ValVariant &Val) const;
820
  template <typename TIn, typename TOut>
821
  Expect<void> runVectorExtMulLowOp(ValVariant &Val1,
822
                                    const ValVariant &Val2) const;
823
  template <typename TIn, typename TOut>
824
  Expect<void> runVectorExtMulHighOp(ValVariant &Val1,
825
                                     const ValVariant &Val2) const;
826
  inline Expect<void> runVectorQ15MulSatOp(ValVariant &Val1,
827
                                           const ValVariant &Val2) const;
828
  template <typename T>
829
  Expect<void> runVectorShlOp(ValVariant &Val1, const ValVariant &Val2) const;
830
  template <typename T>
831
  Expect<void> runVectorShrOp(ValVariant &Val1, const ValVariant &Val2) const;
832
  template <typename T>
833
  Expect<void> runVectorAddOp(ValVariant &Val1, const ValVariant &Val2) const;
834
  template <typename T>
835
  Expect<void> runVectorAddSatOp(ValVariant &Val1,
836
                                 const ValVariant &Val2) const;
837
  template <typename T>
838
  Expect<void> runVectorSubOp(ValVariant &Val1, const ValVariant &Val2) const;
839
  template <typename T>
840
  Expect<void> runVectorSubSatOp(ValVariant &Val1,
841
                                 const ValVariant &Val2) const;
842
  template <typename T>
843
  Expect<void> runVectorMulOp(ValVariant &Val1, const ValVariant &Val2) const;
844
  template <typename T>
845
  Expect<void> runVectorDivOp(ValVariant &Val1, const ValVariant &Val2) const;
846
  template <typename T>
847
  Expect<void> runVectorMinOp(ValVariant &Val1, const ValVariant &Val2) const;
848
  template <typename T>
849
  Expect<void> runVectorMaxOp(ValVariant &Val1, const ValVariant &Val2) const;
850
  template <typename T>
851
  Expect<void> runVectorFMinOp(ValVariant &Val1, const ValVariant &Val2) const;
852
  template <typename T>
853
  Expect<void> runVectorFMaxOp(ValVariant &Val1, const ValVariant &Val2) const;
854
  template <typename T, typename ET>
855
  Expect<void> runVectorAvgrOp(ValVariant &Val1, const ValVariant &Val2) const;
856
  template <typename T> Expect<void> runVectorCeilOp(ValVariant &Val) const;
857
  template <typename T> Expect<void> runVectorFloorOp(ValVariant &Val) const;
858
  template <typename T> Expect<void> runVectorTruncOp(ValVariant &Val) const;
859
  template <typename T> Expect<void> runVectorNearestOp(ValVariant &Val) const;
860
  /// ======= Relaxed SIMD instructions =======
861
  template <typename T>
862
  Expect<void> runVectorRelaxedLaneselectOp(ValVariant &Val1,
863
                                            const ValVariant &Val2,
864
                                            const ValVariant &Mask) const;
865
  inline Expect<void>
866
  runVectorRelaxedIntegerDotProductOp(ValVariant &Val1,
867
                                      const ValVariant &Val2) const;
868
  inline Expect<void> runVectorRelaxedIntegerDotProductOpAdd(
869
      ValVariant &Val1, const ValVariant &Val2, const ValVariant &C) const;
870
  /// ======= Atomic instructions =======
871
  Expect<void> runAtomicNotifyOp(Runtime::StackManager &StackMgr,
872
                                 Runtime::Instance::MemoryInstance &MemInst,
873
                                 const AST::Instruction &Instr);
874
  Expect<void> runMemoryFenceOp();
875
  template <typename T>
876
  TypeT<T> runAtomicWaitOp(Runtime::StackManager &StackMgr,
877
                           Runtime::Instance::MemoryInstance &MemInst,
878
                           const AST::Instruction &Instr);
879
  template <typename T, typename I>
880
  TypeT<T> runAtomicLoadOp(Runtime::StackManager &StackMgr,
881
                           Runtime::Instance::MemoryInstance &MemInst,
882
                           const AST::Instruction &Instr);
883
  template <typename T, typename I>
884
  TypeT<T> runAtomicStoreOp(Runtime::StackManager &StackMgr,
885
                            Runtime::Instance::MemoryInstance &MemInst,
886
                            const AST::Instruction &Instr);
887
  template <typename T, typename I>
888
  TypeT<T> runAtomicAddOp(Runtime::StackManager &StackMgr,
889
                          Runtime::Instance::MemoryInstance &MemInst,
890
                          const AST::Instruction &Instr);
891
  template <typename T, typename I>
892
  TypeT<T> runAtomicSubOp(Runtime::StackManager &StackMgr,
893
                          Runtime::Instance::MemoryInstance &MemInst,
894
                          const AST::Instruction &Instr);
895
  template <typename T, typename I>
896
  TypeT<T> runAtomicOrOp(Runtime::StackManager &StackMgr,
897
                         Runtime::Instance::MemoryInstance &MemInst,
898
                         const AST::Instruction &Instr);
899
  template <typename T, typename I>
900
  TypeT<T> runAtomicAndOp(Runtime::StackManager &StackMgr,
901
                          Runtime::Instance::MemoryInstance &MemInst,
902
                          const AST::Instruction &Instr);
903
  template <typename T, typename I>
904
  TypeT<T> runAtomicXorOp(Runtime::StackManager &StackMgr,
905
                          Runtime::Instance::MemoryInstance &MemInst,
906
                          const AST::Instruction &Instr);
907
  template <typename T, typename I>
908
  TypeT<T> runAtomicExchangeOp(Runtime::StackManager &StackMgr,
909
                               Runtime::Instance::MemoryInstance &MemInst,
910
                               const AST::Instruction &Instr);
911
  template <typename T, typename I>
912
  TypeT<T>
913
  runAtomicCompareExchangeOp(Runtime::StackManager &StackMgr,
914
                             Runtime::Instance::MemoryInstance &MemInst,
915
                             const AST::Instruction &Instr);
916
  /// @}
917
918
public:
919
  /// \name AOT/JIT - Run compiled functions
920
  /// @{
921
  Expect<void> proxyTrap(Runtime::StackManager &StackMgr,
922
                         const uint32_t Code) noexcept;
923
  Expect<void> proxyCall(Runtime::StackManager &StackMgr,
924
                         const uint32_t FuncIdx, const ValVariant *Args,
925
                         ValVariant *Rets) noexcept;
926
  Expect<void> proxyCallIndirect(Runtime::StackManager &StackMgr,
927
                                 const uint32_t TableIdx,
928
                                 const uint32_t FuncTypeIdx,
929
                                 const uint32_t FuncIdx, const ValVariant *Args,
930
                                 ValVariant *Rets) noexcept;
931
  Expect<void> proxyCallRef(Runtime::StackManager &StackMgr,
932
                            const RefVariant Ref, const ValVariant *Args,
933
                            ValVariant *Rets) noexcept;
934
  Expect<RefVariant> proxyRefFunc(Runtime::StackManager &StackMgr,
935
                                  const uint32_t FuncIdx) noexcept;
936
  Expect<RefVariant> proxyStructNew(Runtime::StackManager &StackMgr,
937
                                    const uint32_t TypeIdx,
938
                                    const ValVariant *Args,
939
                                    const uint32_t ArgSize) noexcept;
940
  Expect<void> proxyStructGet(Runtime::StackManager &StackMgr,
941
                              const RefVariant Ref, const uint32_t TypeIdx,
942
                              const uint32_t Off, const bool IsSigned,
943
                              ValVariant *Ret) noexcept;
944
  Expect<void> proxyStructSet(Runtime::StackManager &StackMgr,
945
                              const RefVariant Ref, const uint32_t TypeIdx,
946
                              const uint32_t Off,
947
                              const ValVariant *Val) noexcept;
948
  Expect<RefVariant> proxyArrayNew(Runtime::StackManager &StackMgr,
949
                                   const uint32_t TypeIdx,
950
                                   const uint32_t Length,
951
                                   const ValVariant *Args,
952
                                   const uint32_t ArgSize) noexcept;
953
  Expect<RefVariant> proxyArrayNewData(Runtime::StackManager &StackMgr,
954
                                       const uint32_t TypeIdx,
955
                                       const uint32_t DataIdx,
956
                                       const uint32_t Start,
957
                                       const uint32_t Length) noexcept;
958
  Expect<RefVariant> proxyArrayNewElem(Runtime::StackManager &StackMgr,
959
                                       const uint32_t TypeIdx,
960
                                       const uint32_t ElemIdx,
961
                                       const uint32_t Start,
962
                                       const uint32_t Length) noexcept;
963
  Expect<void> proxyArrayGet(Runtime::StackManager &StackMgr,
964
                             const RefVariant Ref, const uint32_t TypeIdx,
965
                             const uint32_t Idx, const bool IsSigned,
966
                             ValVariant *Ret) noexcept;
967
  Expect<void> proxyArraySet(Runtime::StackManager &StackMgr,
968
                             const RefVariant Ref, const uint32_t TypeIdx,
969
                             const uint32_t Idx,
970
                             const ValVariant *Val) noexcept;
971
  Expect<uint32_t> proxyArrayLen(Runtime::StackManager &StackMgr,
972
                                 const RefVariant Ref) noexcept;
973
  Expect<void> proxyArrayFill(Runtime::StackManager &StackMgr,
974
                              const RefVariant Ref, const uint32_t TypeIdx,
975
                              const uint32_t Idx, const uint32_t Cnt,
976
                              const ValVariant *Val) noexcept;
977
  Expect<void> proxyArrayCopy(Runtime::StackManager &StackMgr,
978
                              const RefVariant DstRef,
979
                              const uint32_t DstTypeIdx, const uint32_t DstIdx,
980
                              const RefVariant SrcRef,
981
                              const uint32_t SrcTypeIdx, const uint32_t SrcIdx,
982
                              const uint32_t Cnt) noexcept;
983
  Expect<void> proxyArrayInitData(Runtime::StackManager &StackMgr,
984
                                  const RefVariant Ref, const uint32_t TypeIdx,
985
                                  const uint32_t DataIdx, const uint32_t DstIdx,
986
                                  const uint32_t SrcIdx,
987
                                  const uint32_t Cnt) noexcept;
988
  Expect<void> proxyArrayInitElem(Runtime::StackManager &StackMgr,
989
                                  const RefVariant Ref, const uint32_t TypeIdx,
990
                                  const uint32_t ElemIdx, const uint32_t DstIdx,
991
                                  const uint32_t SrcIdx,
992
                                  const uint32_t Cnt) noexcept;
993
  Expect<uint32_t> proxyRefTest(Runtime::StackManager &StackMgr,
994
                                const RefVariant Ref, ValType VTTest) noexcept;
995
  Expect<RefVariant> proxyRefCast(Runtime::StackManager &StackMgr,
996
                                  const RefVariant Ref,
997
                                  ValType VTCast) noexcept;
998
  Expect<RefVariant> proxyTableGet(Runtime::StackManager &StackMgr,
999
                                   const uint32_t TableIdx,
1000
                                   const uint32_t Off) noexcept;
1001
  Expect<void> proxyTableSet(Runtime::StackManager &StackMgr,
1002
                             const uint32_t TableIdx, const uint32_t Off,
1003
                             const RefVariant Ref) noexcept;
1004
  Expect<void> proxyTableInit(Runtime::StackManager &StackMgr,
1005
                              const uint32_t TableIdx, const uint32_t ElemIdx,
1006
                              const uint32_t DstOff, const uint32_t SrcOff,
1007
                              const uint32_t Len) noexcept;
1008
  Expect<void> proxyElemDrop(Runtime::StackManager &StackMgr,
1009
                             const uint32_t ElemIdx) noexcept;
1010
  Expect<void> proxyTableCopy(Runtime::StackManager &StackMgr,
1011
                              const uint32_t TableIdxDst,
1012
                              const uint32_t TableIdxSrc, const uint32_t DstOff,
1013
                              const uint32_t SrcOff,
1014
                              const uint32_t Len) noexcept;
1015
  Expect<uint32_t> proxyTableGrow(Runtime::StackManager &StackMgr,
1016
                                  const uint32_t TableIdx, const RefVariant Val,
1017
                                  const uint32_t NewSize) noexcept;
1018
  Expect<uint32_t> proxyTableSize(Runtime::StackManager &StackMgr,
1019
                                  const uint32_t TableIdx) noexcept;
1020
  Expect<void> proxyTableFill(Runtime::StackManager &StackMgr,
1021
                              const uint32_t TableIdx, const uint32_t Off,
1022
                              const RefVariant Ref,
1023
                              const uint32_t Len) noexcept;
1024
  Expect<uint32_t> proxyMemGrow(Runtime::StackManager &StackMgr,
1025
                                const uint32_t MemIdx,
1026
                                const uint32_t NewSize) noexcept;
1027
  Expect<uint32_t> proxyMemSize(Runtime::StackManager &StackMgr,
1028
                                const uint32_t MemIdx) noexcept;
1029
  Expect<void> proxyMemInit(Runtime::StackManager &StackMgr,
1030
                            const uint32_t MemIdx, const uint32_t DataIdx,
1031
                            const uint32_t DstOff, const uint32_t SrcOff,
1032
                            const uint32_t Len) noexcept;
1033
  Expect<void> proxyDataDrop(Runtime::StackManager &StackMgr,
1034
                             const uint32_t DataIdx) noexcept;
1035
  Expect<void> proxyMemCopy(Runtime::StackManager &StackMgr,
1036
                            const uint32_t DstMemIdx, const uint32_t SrcMemIdx,
1037
                            const uint32_t DstOff, const uint32_t SrcOff,
1038
                            const uint32_t Len) noexcept;
1039
  Expect<void> proxyMemFill(Runtime::StackManager &StackMgr,
1040
                            const uint32_t MemIdx, const uint32_t Off,
1041
                            const uint8_t Val, const uint32_t Len) noexcept;
1042
  Expect<uint32_t> proxyMemAtomicNotify(Runtime::StackManager &StackMgr,
1043
                                        const uint32_t MemIdx,
1044
                                        const uint32_t Offset,
1045
                                        const uint32_t Count) noexcept;
1046
  Expect<uint32_t>
1047
  proxyMemAtomicWait(Runtime::StackManager &StackMgr, const uint32_t MemIdx,
1048
                     const uint32_t Offset, const uint64_t Expected,
1049
                     const int64_t Timeout, const uint32_t BitWidth) noexcept;
1050
  Expect<void *> proxyTableGetFuncSymbol(Runtime::StackManager &StackMgr,
1051
                                         const uint32_t TableIdx,
1052
                                         const uint32_t FuncTypeIdx,
1053
                                         const uint32_t FuncIdx) noexcept;
1054
  Expect<void *> proxyRefGetFuncSymbol(Runtime::StackManager &StackMgr,
1055
                                       const RefVariant Ref) noexcept;
1056
  /// @}
1057
1058
  /// Callbacks for compiled modules
1059
  static const Executable::IntrinsicsTable Intrinsics;
1060
  /// Proxy helper template struct
1061
  template <typename FuncPtr> struct ProxyHelper;
1062
1063
private:
1064
  /// Execution context for compiled functions.
1065
  struct ExecutionContextStruct {
1066
#if WASMEDGE_ALLOCATOR_IS_STABLE
1067
    uint8_t *const *Memories;
1068
#else
1069
    uint8_t **const *Memories;
1070
#endif
1071
    ValVariant *const *Globals;
1072
    std::atomic_uint64_t *InstrCount;
1073
    uint64_t *CostTable;
1074
    std::atomic_uint64_t *Gas;
1075
    uint64_t GasLimit;
1076
    std::atomic_uint32_t *StopToken;
1077
  };
1078
1079
  /// Restores thread local VM reference after overwriting it.
1080
  struct SavedThreadLocal {
1081
    SavedThreadLocal(Executor &Ex, Runtime::StackManager &StackMgr,
1082
                     const Runtime::Instance::FunctionInstance &Func) noexcept;
1083
1084
    SavedThreadLocal(const SavedThreadLocal &) = delete;
1085
    SavedThreadLocal(SavedThreadLocal &&) = delete;
1086
1087
    ~SavedThreadLocal() noexcept;
1088
1089
    Executor *SavedThis;
1090
    Runtime::StackManager *SavedCurrentStack;
1091
    ExecutionContextStruct SavedExecutionContext;
1092
  };
1093
1094
  /// Pointer to current object.
1095
  static thread_local Executor *This;
1096
  /// Stack for passing into compiled functions
1097
  static thread_local Runtime::StackManager *CurrentStack;
1098
  /// Execution context for compiled functions
1099
  static thread_local ExecutionContextStruct ExecutionContext;
1100
  /// Record stack track on error
1101
  static thread_local std::array<uint32_t, 256> StackTrace;
1102
  static thread_local size_t StackTraceSize;
1103
1104
  /// Waiter struct for atomic instructions
1105
  struct Waiter {
1106
    std::mutex Mutex;
1107
    std::condition_variable Cond;
1108
    Runtime::Instance::MemoryInstance *MemInst;
1109
0
    Waiter(Runtime::Instance::MemoryInstance *Inst) noexcept : MemInst(Inst) {}
1110
  };
1111
  /// Waiter map mutex
1112
  std::mutex WaiterMapMutex;
1113
  /// Waiter multimap
1114
  std::unordered_multimap<uint32_t, Waiter> WaiterMap;
1115
1116
  /// WasmEdge configuration
1117
  const Configure Conf;
1118
  /// Executor statistics
1119
  Statistics::Statistics *Stat;
1120
  /// Stop Execution
1121
  std::atomic_uint32_t StopToken = 0;
1122
  /// Executor Host Function Handler
1123
  HostFuncHandler HostFuncHelper = {};
1124
};
1125
1126
} // namespace Executor
1127
} // namespace WasmEdge
1128
1129
#include "engine/atomic.ipp"
1130
#include "engine/binary_numeric.ipp"
1131
#include "engine/cast_numeric.ipp"
1132
#include "engine/memory.ipp"
1133
#include "engine/relation_numeric.ipp"
1134
#include "engine/unary_numeric.ipp"