1// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#if !V8_ENABLE_WEBASSEMBLY
6#error This header should only be included if WebAssembly is enabled.
7#endif  // !V8_ENABLE_WEBASSEMBLY
8
9#ifndef V8_WASM_WASM_CONSTANTS_H_
10#define V8_WASM_WASM_CONSTANTS_H_
11
12#include <cstddef>
13#include <cstdint>
14
15#include "src/common/globals.h"
16
17namespace v8 {
18namespace internal {
19namespace wasm {
20
21// Binary encoding of the module header.
22constexpr uint32_t kWasmMagic = 0x6d736100;
23constexpr uint32_t kWasmVersion = 0x01;
24
25// Binary encoding of value and heap types.
26enum ValueTypeCode : uint8_t {
27  // Current value types
28  kVoidCode = 0x40,
29  kI32Code = 0x7f,
30  kI64Code = 0x7e,
31  kF32Code = 0x7d,
32  kF64Code = 0x7c,
33  // Simd proposal
34  kS128Code = 0x7b,
35  // GC proposal packed types
36  kI8Code = 0x7a,
37  kI16Code = 0x79,
38  // Current reference types
39  kFuncRefCode = 0x70,
40  kAnyRefCode = 0x6f,  // aka externref
41  // typed-funcref and GC proposal types
42  // TODO(7748): For backwards compatibility only, remove when able.
43  kAnyRefCodeAlias = 0x6e,
44  kEqRefCode = 0x6d,
45  kOptRefCode = 0x6c,
46  kRefCode = 0x6b,
47  kI31RefCode = 0x6a,
48  // TODO(7748): Only here for backwards compatibility, remove when able.
49  kRttWithDepthCode = 0x69,
50  kRttCode = 0x68,
51  kDataRefCode = 0x67,
52  kArrayRefCode = 0x66
53};
54
55// Binary encoding of type definitions.
56constexpr uint8_t kWasmFunctionTypeCode = 0x60;
57constexpr uint8_t kWasmStructTypeCode = 0x5f;
58constexpr uint8_t kWasmArrayTypeCode = 0x5e;
59constexpr uint8_t kWasmFunctionNominalCode = 0x5d;
60constexpr uint8_t kWasmStructNominalCode = 0x5c;
61constexpr uint8_t kWasmArrayNominalCode = 0x5b;
62constexpr uint8_t kWasmSubtypeCode = 0x50;
63constexpr uint8_t kWasmRecursiveTypeGroupCode = 0x4f;
64
65// Binary encoding of import/export kinds.
66enum ImportExportKindCode : uint8_t {
67  kExternalFunction = 0,
68  kExternalTable = 1,
69  kExternalMemory = 2,
70  kExternalGlobal = 3,
71  kExternalTag = 4
72};
73
74enum LimitsFlags : uint8_t {
75  kNoMaximum = 0x00,           // Also valid for table limits.
76  kWithMaximum = 0x01,         // Also valid for table limits.
77  kSharedNoMaximum = 0x02,     // Only valid for memory limits.
78  kSharedWithMaximum = 0x03,   // Only valid for memory limits.
79  kMemory64NoMaximum = 0x04,   // Only valid for memory limits.
80  kMemory64WithMaximum = 0x05  // Only valid for memory limits.
81};
82
83// Flags for data and element segments.
84enum SegmentFlags : uint8_t {
85  kActiveNoIndex = 0,    // Active segment with a memory/table index of zero.
86  kPassive = 1,          // Passive segment.
87  kActiveWithIndex = 2,  // Active segment with a given memory/table index.
88};
89
90// Binary encoding of sections identifiers.
91enum SectionCode : int8_t {
92  kUnknownSectionCode = 0,     // code for unknown sections
93  kTypeSectionCode = 1,        // Function signature declarations
94  kImportSectionCode = 2,      // Import declarations
95  kFunctionSectionCode = 3,    // Function declarations
96  kTableSectionCode = 4,       // Indirect function table and others
97  kMemorySectionCode = 5,      // Memory attributes
98  kGlobalSectionCode = 6,      // Global declarations
99  kExportSectionCode = 7,      // Exports
100  kStartSectionCode = 8,       // Start function declaration
101  kElementSectionCode = 9,     // Elements section
102  kCodeSectionCode = 10,       // Function code
103  kDataSectionCode = 11,       // Data segments
104  kDataCountSectionCode = 12,  // Number of data segments
105  kTagSectionCode = 13,        // Tag section
106
107  // The following sections are custom sections, and are identified using a
108  // string rather than an integer. Their enumeration values are not guaranteed
109  // to be consistent.
110  kNameSectionCode,               // Name section (encoded as a string)
111  kSourceMappingURLSectionCode,   // Source Map URL section
112  kDebugInfoSectionCode,          // DWARF section .debug_info
113  kExternalDebugInfoSectionCode,  // Section encoding the external symbol path
114  kCompilationHintsSectionCode,   // Compilation hints section
115  kBranchHintsSectionCode,        // Branch hints section
116
117  // Helper values
118  kFirstSectionInModule = kTypeSectionCode,
119  kLastKnownModuleSection = kBranchHintsSectionCode,
120  kFirstUnorderedSection = kDataCountSectionCode,
121};
122
123// Binary encoding of compilation hints.
124constexpr uint8_t kDefaultCompilationHint = 0x0;
125constexpr uint8_t kNoCompilationHint = kMaxUInt8;
126
127// Binary encoding of name section kinds.
128enum NameSectionKindCode : uint8_t {
129  kModuleCode = 0,
130  kFunctionCode = 1,
131  kLocalCode = 2,
132  // https://github.com/WebAssembly/extended-name-section/
133  kLabelCode = 3,
134  kTypeCode = 4,
135  kTableCode = 5,
136  kMemoryCode = 6,
137  kGlobalCode = 7,
138  kElementSegmentCode = 8,
139  kDataSegmentCode = 9,
140  // https://github.com/WebAssembly/gc/issues/193
141  kFieldCode = 10
142};
143
144constexpr size_t kWasmPageSize = 0x10000;
145constexpr uint32_t kWasmPageSizeLog2 = 16;
146static_assert(kWasmPageSize == size_t{1} << kWasmPageSizeLog2, "consistency");
147
148// TODO(wasm): Wrap WasmCodePosition in a struct.
149using WasmCodePosition = int;
150constexpr WasmCodePosition kNoCodePosition = -1;
151
152constexpr uint32_t kExceptionAttribute = 0;
153
154constexpr int kAnonymousFuncIndex = -1;
155
156// The number of calls to an exported Wasm function that will be handled
157// by the generic wrapper. Once the budget is exhausted, a specific wrapper
158// is to be compiled for the function's signature.
159// The abstract goal of the tiering strategy for the js-to-wasm wrappers is to
160// use the generic wrapper as much as possible (less space, no need to compile),
161// but fall back to compiling a specific wrapper for any function (signature)
162// that is used often enough for the generic wrapper's small execution penalty
163// to start adding up.
164// So, when choosing a value for the initial budget, we are interested in a
165// value that skips on tiering up functions that are called only a few times and
166// the tier-up only wastes resources, but triggers compilation of specific
167// wrappers early on for those functions that have the potential to be called
168// often enough.
169constexpr uint32_t kGenericWrapperBudget = 1000;
170
171// The minimum length of supertype arrays for wasm-gc types. Having a size > 0
172// gives up some module size for faster access to the supertypes.
173constexpr uint32_t kMinimumSupertypeArraySize = 3;
174
175#if V8_TARGET_ARCH_X64
176constexpr int32_t kOSRTargetOffset = 5 * kSystemPointerSize;
177#endif
178
179constexpr Tagged_t kArrayInitFromDataArrayTooLargeErrorCode = 0;
180constexpr Tagged_t kArrayInitFromDataSegmentOutOfBoundsErrorCode = 1;
181
182}  // namespace wasm
183}  // namespace internal
184}  // namespace v8
185
186#endif  // V8_WASM_WASM_CONSTANTS_H_
187