File: WasmFormat.h

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (852 lines) | stat: -rw-r--r-- 25,511 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
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
/*
 * Copyright (C) 2015-2023 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#if ENABLE(WEBASSEMBLY)

#include "CCallHelpers.h"
#include "CallLinkInfo.h"
#include "CodeLocation.h"
#include "Identifier.h"
#include "JSString.h"
#include "MacroAssemblerCodeRef.h"
#include "MathCommon.h"
#include "PageCount.h"
#include "RegisterAtOffsetList.h"
#include "WasmMemoryInformation.h"
#include "WasmName.h"
#include "WasmNameSection.h"
#include "WasmOps.h"
#include "WasmTypeDefinition.h"
#include <cstdint>
#include <limits>
#include <memory>
#include <wtf/FixedBitVector.h>
#include <wtf/TZoneMalloc.h>
#include <wtf/Vector.h>

WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN

namespace JSC {

class Compilation;

namespace Wasm {

struct CompilationContext;
struct ModuleInformation;
struct UnlinkedHandlerInfo;

struct BlockSignature {
    const FunctionSignature* m_signature;
    RefPtr<TypeDefinition> m_generatedUnderlyingType;
};

enum class TableElementType : uint8_t {
    Externref,
    Funcref
};

constexpr int32_t maxI31ref = 1073741823;
constexpr int32_t minI31ref = -1073741824;

inline bool isValueType(Type type)
{
    switch (type.kind) {
    case TypeKind::I32:
    case TypeKind::I64:
    case TypeKind::F32:
    case TypeKind::F64:
        return true;
    case TypeKind::Exn:
    case TypeKind::Externref:
    case TypeKind::Funcref:
        return false;
    case TypeKind::Ref:
    case TypeKind::RefNull:
        return type.index != TypeDefinition::invalidIndex;
    case TypeKind::V128:
        return Options::useWasmSIMD();
    default:
        break;
    }
    return false;
}

inline bool isRefType(Type type)
{
    return type.isRef() || type.isRefNull();
}

// If this is a type, returns true iff it's a ref type; if it's a packed type, returns false
inline bool isRefType(StorageType type)
{
    if (type.is<Type>())
        return isRefType(type.as<Type>());
    return false;
}

inline bool isExternref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Externref);
}

inline bool isFuncref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Funcref);
}

inline bool isEqref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Eqref);
}

inline bool isAnyref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Anyref);
}

inline bool isNullexnref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Nullexn);
}

inline bool isNullref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Nullref);
}

inline bool isNullfuncref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Nullfuncref);
}

inline bool isNullexternref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Nullexternref);
}

inline bool isInternalref(Type type)
{
    if (!isRefType(type))
        return false;
    if (typeIndexIsType(type.index)) {
        switch (static_cast<TypeKind>(type.index)) {
        case TypeKind::I31ref:
        case TypeKind::Arrayref:
        case TypeKind::Structref:
        case TypeKind::Eqref:
        case TypeKind::Anyref:
        case TypeKind::Nullref:
            return true;
        default:
            return false;
        }
    }
    return !TypeInformation::get(type.index).expand().is<FunctionSignature>();
}

inline bool isI31ref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::I31ref);
}

inline bool isArrayref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Arrayref);
}

inline bool isStructref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Structref);
}

inline bool isExnref(Type type)
{
    return isRefType(type) && type.index == static_cast<TypeIndex>(TypeKind::Exn);
}

inline JSString* typeToJSAPIString(VM& vm, Type type)
{
    switch (type.kind) {
    case TypeKind::I32:
        return jsNontrivialString(vm, "i32"_s);
    case TypeKind::I64:
        return jsNontrivialString(vm, "i64"_s);
    case TypeKind::F32:
        return jsNontrivialString(vm, "f32"_s);
    case TypeKind::F64:
        return jsNontrivialString(vm, "f64"_s);
    case TypeKind::V128:
        return jsNontrivialString(vm, "v128"_s);
    default: {
        if (isFuncref(type) && type.isNullable())
            return jsNontrivialString(vm, "funcref"_s);
        if (isExternref(type) && type.isNullable())
            return jsNontrivialString(vm, "externref"_s);
        // Some Wasm reference types are currently unrepresentable for the JS API.
        return nullptr;
    }
    }
    RELEASE_ASSERT_NOT_REACHED();
}

inline Type nonNullFuncrefType()
{
    return Wasm::Type { Wasm::TypeKind::Ref, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Funcref) };
}

inline Type funcrefType()
{
    return Wasm::Type { Wasm::TypeKind::RefNull, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Funcref) };
}

inline Type externrefType(bool isNullable = true)
{
    return Wasm::Type { isNullable ? Wasm::TypeKind::RefNull : Wasm::TypeKind::Ref, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Externref) };
}

inline Type eqrefType()
{
    return Wasm::Type { Wasm::TypeKind::RefNull, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Eqref) };
}

inline Type anyrefType(bool isNullable = true)
{
    return Wasm::Type { isNullable ? Wasm::TypeKind::RefNull : Wasm::TypeKind::Ref, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Anyref) };
}

inline Type arrayrefType(bool isNullable = true)
{
    // Returns a non-null ref type, since this is used for the return types of array operations
    // that are guaranteed to return a non-null array reference
    return Wasm::Type { isNullable ? Wasm::TypeKind::RefNull : Wasm::TypeKind::Ref, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Arrayref) };
}

inline Type exnrefType()
{
    return Wasm::Type { Wasm::TypeKind::RefNull, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Exn) };
}

inline bool isRefWithTypeIndex(Type type)
{
    return isRefType(type) && !typeIndexIsType(type.index);
}

// Determine if the ref type has a placeholder type index that is used
// for an unresolved recursive reference in a recursion group.
inline bool isRefWithRecursiveReference(Type type)
{
    if (isRefWithTypeIndex(type)) {
        const TypeDefinition& def = TypeInformation::get(type.index);
        if (def.is<Projection>())
            return def.as<Projection>()->isPlaceholder();
    }

    return false;
}

inline bool isRefWithRecursiveReference(StorageType storageType)
{
    if (storageType.is<PackedType>())
        return false;

    return isRefWithRecursiveReference(storageType.as<Type>());
}

inline bool isTypeIndexHeapType(int32_t heapType)
{
    return heapType >= 0;
}

inline bool isSubtypeIndex(TypeIndex sub, TypeIndex parent)
{
    if (sub == parent)
        return true;

    auto subRTT = TypeInformation::tryGetCanonicalRTT(sub);
    auto parentRTT = TypeInformation::tryGetCanonicalRTT(parent);
    ASSERT(subRTT.has_value() && parentRTT.has_value());

    return subRTT.value()->isSubRTT(*parentRTT.value());
}

bool isSubtype(Type, Type);

inline bool isSubtypeSlow(Type sub, Type parent)
{
    // Before the typed funcref proposal there is no non-trivial subtyping.
    if (sub.isNullable() && !parent.isNullable())
        return false;

    if (isRefWithTypeIndex(sub)) {
        if (isRefWithTypeIndex(parent))
            return isSubtypeIndex(sub.index, parent.index);

        if ((isAnyref(parent) || isEqref(parent)))
            return !TypeInformation::get(sub.index).expand().is<FunctionSignature>();

        if (isArrayref(parent))
            return TypeInformation::get(sub.index).expand().is<ArrayType>();

        if (isStructref(parent))
            return TypeInformation::get(sub.index).expand().is<StructType>();

        if (isFuncref(parent))
            return TypeInformation::get(sub.index).expand().is<FunctionSignature>();
    }

    if ((isI31ref(sub) || isStructref(sub) || isArrayref(sub)) && (isAnyref(parent) || isEqref(parent)))
        return true;

    if (isEqref(sub) && isAnyref(parent))
        return true;

    if (isNullref(sub))
        return isInternalref(parent);

    if (isNullfuncref(sub))
        return isSubtype(parent, funcrefType());

    if (isNullexternref(sub) && isExternref(parent))
        return true;

    if (isNullexnref(sub) && isExnref(parent))
        return true;

    if (sub.isRef() && parent.isRefNull())
        return sub.index == parent.index;

    return false;
}

ALWAYS_INLINE bool isSubtype(Type sub, Type parent)
{
    // Fast path.
    if (sub == parent)
        return true;
    return isSubtypeSlow(sub, parent);
}

inline bool isSubtype(StorageType sub, StorageType parent)
{
    if (sub.is<PackedType>() || parent.is<PackedType>())
        return sub == parent;

    ASSERT(sub.is<Type>() && parent.is<Type>());
    return isSubtype(sub.as<Type>(), parent.as<Type>());
}

inline bool isValidHeapTypeKind(intptr_t kind)
{
    switch (kind) {
    case static_cast<intptr_t>(TypeKind::Funcref):
    case static_cast<intptr_t>(TypeKind::Externref):
    case static_cast<intptr_t>(TypeKind::Exn):
    case static_cast<intptr_t>(TypeKind::I31ref):
    case static_cast<intptr_t>(TypeKind::Arrayref):
    case static_cast<intptr_t>(TypeKind::Structref):
    case static_cast<intptr_t>(TypeKind::Eqref):
    case static_cast<intptr_t>(TypeKind::Anyref):
    case static_cast<intptr_t>(TypeKind::Nullexn):
    case static_cast<intptr_t>(TypeKind::Nullref):
    case static_cast<intptr_t>(TypeKind::Nullfuncref):
    case static_cast<intptr_t>(TypeKind::Nullexternref):
        return true;
    default:
        break;
    }
    return false;
}

// FIXME: separating out heap types in wasm.json could be cleaner in the long term.
inline const char* heapTypeKindAsString(TypeKind kind)
{
    ASSERT(isValidHeapTypeKind(static_cast<intptr_t>(kind)));
    switch (kind) {
    case TypeKind::Funcref:
        return "func";
    case TypeKind::Externref:
        return "extern";
    case TypeKind::I31ref:
        return "i31";
    case TypeKind::Arrayref:
        return "array";
    case TypeKind::Structref:
        return "struct";
    case TypeKind::Eqref:
        return "eq";
    case TypeKind::Anyref:
        return "any";
    case TypeKind::Nullref:
        return "none";
    case TypeKind::Nullfuncref:
        return "nofunc";
    case TypeKind::Nullexternref:
        return "noextern";
    case TypeKind::Exn:
        return "exn";
    case TypeKind::Nullexn:
        return "nullexn";
    default:
        RELEASE_ASSERT_NOT_REACHED();
        return "";
    }
}

inline bool isDefaultableType(Type type)
{
    return !type.isRef();
}

inline bool isDefaultableType(StorageType type)
{
    if (type.is<Type>())
        return !type.as<Type>().isRef();
    // All packed types are defaultable.
    return true;
}

inline JSValue internalizeExternref(JSValue value)
{
    if (value.isDouble() && JSC::canBeStrictInt32(value.asDouble())) {
        int32_t int32Value = JSC::toInt32(value.asDouble());
        if (int32Value <= Wasm::maxI31ref && int32Value >= Wasm::minI31ref)
            return jsNumber(int32Value);
    }

    return value;
}

enum class ExternalKind : uint8_t {
    // FIXME auto-generate this. https://bugs.webkit.org/show_bug.cgi?id=165231
    Function = 0,
    Table = 1,
    Memory = 2,
    Global = 3,
    Exception = 4,
};

template<typename Int>
inline bool isValidExternalKind(Int val)
{
    switch (val) {
    case static_cast<Int>(ExternalKind::Function):
    case static_cast<Int>(ExternalKind::Table):
    case static_cast<Int>(ExternalKind::Memory):
    case static_cast<Int>(ExternalKind::Global):
    case static_cast<Int>(ExternalKind::Exception):
        return true;
    }
    return false;
}

static_assert(static_cast<int>(ExternalKind::Function) == 0, "Wasm needs Function to have the value 0");
static_assert(static_cast<int>(ExternalKind::Table)    == 1, "Wasm needs Table to have the value 1");
static_assert(static_cast<int>(ExternalKind::Memory)   == 2, "Wasm needs Memory to have the value 2");
static_assert(static_cast<int>(ExternalKind::Global)   == 3, "Wasm needs Global to have the value 3");
static_assert(static_cast<int>(ExternalKind::Exception)   == 4, "Wasm needs Exception to have the value 4");

inline ASCIILiteral makeString(ExternalKind kind)
{
    switch (kind) {
    case ExternalKind::Function: return "function"_s;
    case ExternalKind::Table: return "table"_s;
    case ExternalKind::Memory: return "memory"_s;
    case ExternalKind::Global: return "global"_s;
    case ExternalKind::Exception: return "tag"_s;
    }
    RELEASE_ASSERT_NOT_REACHED();
    return "?"_s;
}

struct Import {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    const Name module;
    const Name field;
    ExternalKind kind;
    unsigned kindIndex; // Index in the vector of the corresponding kind.
};

struct Export {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    const Name field;
    ExternalKind kind;
    unsigned kindIndex; // Index in the vector of the corresponding kind.
};

String makeString(const Name& characters);

struct GlobalInformation {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;

    enum InitializationType : uint8_t {
        IsImport,
        FromGlobalImport,
        FromRefFunc,
        FromExpression,
        FromVector,
        FromExtendedExpression,
    };

    enum class BindingMode : uint8_t {
        EmbeddedInInstance = 0,
        Portable,
    };

    Mutability mutability;
    Type type;
    InitializationType initializationType { IsImport };
    BindingMode bindingMode { BindingMode::EmbeddedInInstance };
    union {
        uint64_t initialBitsOrImportNumber;
        v128_t initialVector { };
    } initialBits;
};

struct FunctionData {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    size_t start;
    size_t end;
    Vector<uint8_t> data;
    bool usesSIMD : 1 { false };
    bool usesExceptions : 1 { false };
    bool usesAtomics : 1 { false };
    bool finishedValidating : 1 { false };
};

class I32InitExpr {
    WTF_MAKE_TZONE_ALLOCATED(I32InitExpr);
    enum Type : uint8_t {
        Global,
        Const,
        ExtendedExpression
    };

    I32InitExpr(Type type, uint32_t bits)
        : m_bits(bits)
        , m_type(type)
    { }

public:
    I32InitExpr() = delete;

    static I32InitExpr globalImport(uint32_t globalImportNumber) { return I32InitExpr(Global, globalImportNumber); }
    static I32InitExpr constValue(uint32_t constValue) { return I32InitExpr(Const, constValue); }
    static I32InitExpr extendedExpression(uint32_t constantExpressionNumber) { return I32InitExpr(ExtendedExpression, constantExpressionNumber); }

    bool isConst() const { return m_type == Const; }
    bool isGlobalImport() const { return m_type == Global; }
    bool isExtendedExpression() const { return m_type == ExtendedExpression; }
    uint32_t constValue() const
    {
        RELEASE_ASSERT(isConst());
        return m_bits;
    }
    uint32_t globalImportIndex() const
    {
        RELEASE_ASSERT(isGlobalImport());
        return m_bits;
    }
    uint32_t constantExpressionIndex() const
    {
        RELEASE_ASSERT(isExtendedExpression());
        return m_bits;
    }

private:
    uint32_t m_bits;
    Type m_type;
};

struct Segment {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;

    enum class Kind : uint8_t {
        Active,
        Passive,
    };

    Kind kind;
    uint32_t sizeInBytes;
    std::optional<I32InitExpr> offsetIfActive;
    // Bytes are allocated at the end.
    uint8_t& byte(uint32_t pos)
    {
        ASSERT(pos < sizeInBytes);
        return *(reinterpret_cast<uint8_t*>(this) + sizeof(Segment) + pos);
    }

    static void destroy(Segment*);
    typedef std::unique_ptr<Segment, decltype(&Segment::destroy)> Ptr;
    static Segment::Ptr create(std::optional<I32InitExpr>, uint32_t, Kind);

    bool isActive() const { return kind == Kind::Active; }
    bool isPassive() const { return kind == Kind::Passive; }
};

struct Element {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;

    enum class Kind : uint8_t {
        Active,
        Passive,
        Declared,
    };

    enum InitializationType : uint8_t {
        FromGlobal,
        FromRefFunc,
        FromRefNull,
        FromExtendedExpression,
    };

    Element(Element::Kind kind, Type elementType, std::optional<uint32_t> tableIndex, std::optional<I32InitExpr> initExpr)
        : kind(kind)
        , elementType(elementType)
        , tableIndexIfActive(WTFMove(tableIndex))
        , offsetIfActive(WTFMove(initExpr))
    { }

    Element(Element::Kind kind, Type elemType)
        : Element(kind, elemType, std::nullopt, std::nullopt)
    { }

    uint32_t length() const { return initTypes.size(); }

    bool isActive() const { return kind == Kind::Active; }
    bool isPassive() const { return kind == Kind::Passive; }

    Kind kind;
    Type elementType;
    std::optional<uint32_t> tableIndexIfActive;
    std::optional<I32InitExpr> offsetIfActive;

    Vector<InitializationType> initTypes;
    Vector<uint64_t> initialBitsOrIndices;
};

class TableInformation {
    WTF_MAKE_TZONE_ALLOCATED(TableInformation);
public:
    enum InitializationType : uint8_t {
        Default,
        FromGlobalImport,
        FromRefFunc,
        FromRefNull,
        FromExtendedExpression,
    };

    TableInformation()
    {
        ASSERT(!*this);
    }

    TableInformation(uint32_t initial, std::optional<uint32_t> maximum, bool isImport, TableElementType type, Type wasmType, InitializationType initType, uint64_t initialBitsOrImportNumber)
        : m_initial(initial)
        , m_maximum(maximum)
        , m_isImport(isImport)
        , m_isValid(true)
        , m_type(type)
        , m_wasmType(wasmType)
        , m_initType(initType)
        , m_initialBitsOrImportNumber(initialBitsOrImportNumber)
    {
        ASSERT(*this);
    }

    explicit operator bool() const { return m_isValid; }
    bool isImport() const { return m_isImport; }
    uint32_t initial() const { return m_initial; }
    std::optional<uint32_t> maximum() const { return m_maximum; }
    TableElementType type() const { return m_type; }
    Type wasmType() const { return m_wasmType; }
    InitializationType initType() const { return m_initType; }
    uint64_t initialBitsOrImportNumber() const { return m_initialBitsOrImportNumber; }

private:
    uint32_t m_initial;
    std::optional<uint32_t> m_maximum;
    bool m_isImport { false };
    bool m_isValid { false };
    TableElementType m_type;
    Type m_wasmType;
    InitializationType m_initType { Default };
    uint64_t m_initialBitsOrImportNumber;
};
    
struct CustomSection {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    Name name;
    Vector<uint8_t> payload;
};

enum class NameType : uint8_t {
    Module = 0,
    Function = 1,
    Local = 2,
};
    
template<typename Int>
inline bool isValidNameType(Int val)
{
    switch (val) {
    case static_cast<Int>(NameType::Module):
    case static_cast<Int>(NameType::Function):
    case static_cast<Int>(NameType::Local):
        return true;
    }
    return false;
}

// A code index is an index in the code section of the module, thus does not include imports. This is also sometimes called an internal function.
// A space index is an index into the total function space of a module and includes imports. It's broken down into [ imports..., code section... ].
// These are **NOT** convertable without knowing how many imports there are e.g. via ModuleInformation/CalleeGroup's toCodeIndex/toSpaceIndex

class FunctionSpaceIndex;
class TRIVIAL_ABI FunctionCodeIndex {
public:
    FunctionCodeIndex() = default;
    FunctionCodeIndex(FunctionSpaceIndex) = delete;
    explicit constexpr FunctionCodeIndex(uint32_t index)
        : m_index(index)
    { }

    size_t rawIndex() const { return m_index; }
    operator size_t() const { return m_index; }
    void dump(PrintStream& out) const { out.print(m_index); }

private:
    uint32_t m_index { UINT_MAX };
};

class TRIVIAL_ABI FunctionSpaceIndex {
public:
    FunctionSpaceIndex() = default;
    FunctionSpaceIndex(FunctionCodeIndex) = delete;
    explicit constexpr FunctionSpaceIndex(uint32_t index)
        : m_index(index)
    { }

    size_t rawIndex() const { return m_index; }
    operator size_t() const { return m_index; }
    void dump(PrintStream& out) const { out.print(m_index); }

private:
    uint32_t m_index { UINT_MAX };
};

struct UnlinkedWasmToWasmCall {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    CodeLocationNearCall<WasmEntryPtrTag> callLocation;
    FunctionSpaceIndex functionIndexSpace;
    CodeLocationDataLabelPtr<WasmEntryPtrTag> calleeLocation;

};

#if ENABLE(JIT)
struct Entrypoint {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    std::unique_ptr<Compilation> compilation;
    RegisterAtOffsetList calleeSaveRegisters;
};
#endif

class OSREntryValue;
using StackMap = FixedVector<OSREntryValue>;
using StackMaps = UncheckedKeyHashMap<CallSiteIndex, StackMap>;

struct InternalFunction {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
#if ENABLE(WEBASSEMBLY_OMGJIT) || ENABLE(WEBASSEMBLY_BBQJIT)
    StackMaps stackmaps;
#endif
    Vector<UnlinkedHandlerInfo> exceptionHandlers;
#if ENABLE(JIT)
    Vector<CCallHelpers::Label> bbqLoopEntrypoints;
    std::optional<CCallHelpers::Label> bbqSharedLoopEntrypoint;
    Entrypoint entrypoint;
    FixedBitVector outgoingJITDirectCallees;
#endif
    unsigned osrEntryScratchBufferSize { 0 };
};

extern const uintptr_t NullWasmCallee;

struct alignas(8) WasmCallableFunction {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    using LoadLocation = CodePtr<WasmEntryPtrTag>*;
    static constexpr ptrdiff_t offsetOfEntrypointLoadLocation() { return OBJECT_OFFSETOF(WasmCallableFunction, entrypointLoadLocation); }
    static constexpr ptrdiff_t offsetOfBoxedWasmCalleeLoadLocation() { return OBJECT_OFFSETOF(WasmCallableFunction, boxedWasmCalleeLoadLocation); }

    // FIXME: This always points to the interpreter callee anyway so there's no point in having the extra indirection.
    const uintptr_t* boxedWasmCalleeLoadLocation { &NullWasmCallee };
    // Target instance and entrypoint are only set for wasm->wasm calls, and are otherwise nullptr. The js-specific logic occurs through import function.
    WriteBarrier<JSWebAssemblyInstance> targetInstance { };
    LoadLocation entrypointLoadLocation { };
};

// WebAssembly direct calls and call_indirect use indices into "function index space". This space starts
// with all imports, and then all internal functions. WasmToWasmImportableFunction and FunctionIndexSpace are only
// meant as fast lookup tables for these opcodes and do not own code.
struct WasmToWasmImportableFunction : public WasmCallableFunction {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    static constexpr ptrdiff_t offsetOfSignatureIndex() { return OBJECT_OFFSETOF(WasmToWasmImportableFunction, typeIndex); }
    static constexpr ptrdiff_t offsetOfRTT() { return OBJECT_OFFSETOF(WasmToWasmImportableFunction, rtt); }

    // FIXME: Pack type index and code pointer into one 64-bit value. See <https://bugs.webkit.org/show_bug.cgi?id=165511>.
    TypeIndex typeIndex { TypeDefinition::invalidIndex };
    // Used when GC proposal is enabled, otherwise can be null.
    const RTT* rtt;
};
using FunctionIndexSpace = Vector<WasmToWasmImportableFunction>;

struct WasmOrJSImportableFunction : public WasmToWasmImportableFunction {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    using LoadLocation = CodePtr<WasmEntryPtrTag>*;

    CodePtr<WasmEntryPtrTag> importFunctionStub;
    WriteBarrier<JSObject> importFunction { };
    uintptr_t boxedCallee { 0xBEEF };
};

struct WasmOrJSImportableFunctionCallLinkInfo final : public WasmOrJSImportableFunction {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    std::unique_ptr<DataOnlyCallLinkInfo> callLinkInfo { };
    static constexpr ptrdiff_t offsetOfCallLinkInfo() { return OBJECT_OFFSETOF(WasmOrJSImportableFunctionCallLinkInfo, callLinkInfo); }
};

} } // namespace JSC::Wasm

WTF_ALLOW_UNSAFE_BUFFER_USAGE_END

namespace WTF {

void printInternal(PrintStream&, JSC::Wasm::TableElementType);

} // namespace WTF

#endif // ENABLE(WEBASSEMBLY)