wasm-constants.h 5.63 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8
#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif  // !V8_ENABLE_WEBASSEMBLY

9 10
#ifndef V8_WASM_WASM_CONSTANTS_H_
#define V8_WASM_WASM_CONSTANTS_H_
11

12 13 14
#include <cstddef>
#include <cstdint>

15
#include "src/common/globals.h"
16

17 18 19 20 21 22 23 24
namespace v8 {
namespace internal {
namespace wasm {

// Binary encoding of the module header.
constexpr uint32_t kWasmMagic = 0x6d736100;
constexpr uint32_t kWasmVersion = 0x01;

25
// Binary encoding of value and heap types.
26
enum ValueTypeCode : uint8_t {
27
  // Current wasm types
28 29 30 31 32
  kVoidCode = 0x40,
  kI32Code = 0x7f,
  kI64Code = 0x7e,
  kF32Code = 0x7d,
  kF64Code = 0x7c,
33
  // Simd proposal
34
  kS128Code = 0x7b,
35
  // reftypes, typed-funcref, and GC proposals
36 37 38 39
  kI8Code = 0x7a,
  kI16Code = 0x79,
  kFuncRefCode = 0x70,
  kExternRefCode = 0x6f,
40
  kAnyRefCode = 0x6e,
41 42 43 44
  kEqRefCode = 0x6d,
  kOptRefCode = 0x6c,
  kRefCode = 0x6b,
  kI31RefCode = 0x6a,
45 46
  kRttWithDepthCode = 0x69,
  kRttCode = 0x68,
47
  kDataRefCode = 0x67,
48 49 50
};
// Binary encoding of other types.
constexpr uint8_t kWasmFunctionTypeCode = 0x60;
51 52
constexpr uint8_t kWasmStructTypeCode = 0x5f;
constexpr uint8_t kWasmArrayTypeCode = 0x5e;
53 54 55 56 57 58

// Binary encoding of import/export kinds.
enum ImportExportKindCode : uint8_t {
  kExternalFunction = 0,
  kExternalTable = 1,
  kExternalMemory = 2,
59 60
  kExternalGlobal = 3,
  kExternalException = 4
61 62
};

63 64 65 66 67 68 69
enum LimitsFlags : uint8_t {
  kNoMaximum = 0x00,           // Also valid for table limits.
  kWithMaximum = 0x01,         // Also valid for table limits.
  kSharedNoMaximum = 0x02,     // Only valid for memory limits.
  kSharedWithMaximum = 0x03,   // Only valid for memory limits.
  kMemory64NoMaximum = 0x04,   // Only valid for memory limits.
  kMemory64WithMaximum = 0x05  // Only valid for memory limits.
70 71
};

72 73 74 75 76 77 78
// Flags for data and element segments.
enum SegmentFlags : uint8_t {
  kActiveNoIndex = 0,    // Active segment with a memory/table index of zero.
  kPassive = 1,          // Passive segment.
  kActiveWithIndex = 2,  // Active segment with a given memory/table index.
};

79 80 81 82 83 84
// Binary encoding of sections identifiers.
enum SectionCode : int8_t {
  kUnknownSectionCode = 0,     // code for unknown sections
  kTypeSectionCode = 1,        // Function signature declarations
  kImportSectionCode = 2,      // Import declarations
  kFunctionSectionCode = 3,    // Function declarations
85
  kTableSectionCode = 4,       // Indirect function table and others
86 87 88 89 90 91 92
  kMemorySectionCode = 5,      // Memory attributes
  kGlobalSectionCode = 6,      // Global declarations
  kExportSectionCode = 7,      // Exports
  kStartSectionCode = 8,       // Start function declaration
  kElementSectionCode = 9,     // Elements section
  kCodeSectionCode = 10,       // Function code
  kDataSectionCode = 11,       // Data segments
93 94
  kDataCountSectionCode = 12,  // Number of data segments
  kExceptionSectionCode = 13,  // Exception section
95 96 97 98

  // The following sections are custom sections, and are identified using a
  // string rather than an integer. Their enumeration values are not guaranteed
  // to be consistent.
99 100 101 102 103
  kNameSectionCode,               // Name section (encoded as a string)
  kSourceMappingURLSectionCode,   // Source Map URL section
  kDebugInfoSectionCode,          // DWARF section .debug_info
  kExternalDebugInfoSectionCode,  // Section encoding the external symbol path
  kCompilationHintsSectionCode,   // Compilation hints section
104 105 106

  // Helper values
  kFirstSectionInModule = kTypeSectionCode,
107
  kLastKnownModuleSection = kCompilationHintsSectionCode,
108
  kFirstUnorderedSection = kDataCountSectionCode,
109 110
};

111 112 113 114
// Binary encoding of compilation hints.
constexpr uint8_t kDefaultCompilationHint = 0x0;
constexpr uint8_t kNoCompilationHint = kMaxUInt8;

115
// Binary encoding of name section kinds.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
enum NameSectionKindCode : uint8_t {
  kModule = 0,
  kFunction = 1,
  kLocal = 2,
  // https://github.com/WebAssembly/extended-name-section/
  kLabel = 3,
  kType = 4,
  kTable = 5,
  kMemory = 6,
  kGlobal = 7,
  kElementSegment = 8,
  kDataSegment = 9,
  // https://github.com/WebAssembly/gc/issues/193
  kField = 10
};
131

132 133
constexpr size_t kWasmPageSize = 0x10000;
constexpr uint32_t kWasmPageSizeLog2 = 16;
134
static_assert(kWasmPageSize == size_t{1} << kWasmPageSizeLog2, "consistency");
135 136 137 138 139

// TODO(wasm): Wrap WasmCodePosition in a struct.
using WasmCodePosition = int;
constexpr WasmCodePosition kNoCodePosition = -1;

140 141
constexpr uint32_t kExceptionAttribute = 0;

142
constexpr int kAnonymousFuncIndex = -1;
143

144 145
// The number of calls to an exported Wasm function that will be handled
// by the generic wrapper. Once the budget is exhausted, a specific wrapper
146
// is to be compiled for the function's signature.
147 148 149 150 151 152 153 154 155 156 157
// The abstract goal of the tiering strategy for the js-to-wasm wrappers is to
// use the generic wrapper as much as possible (less space, no need to compile),
// but fall back to compiling a specific wrapper for any function (signature)
// that is used often enough for the generic wrapper's small execution penalty
// to start adding up.
// So, when choosing a value for the initial budget, we are interested in a
// value that skips on tiering up functions that are called only a few times and
// the tier-up only wastes resources, but triggers compilation of specific
// wrappers early on for those functions that have the potential to be called
// often enough.
constexpr uint32_t kGenericWrapperBudget = 1000;
158

159 160 161 162
#if V8_TARGET_ARCH_X64
constexpr int32_t kOSRTargetOffset = 3 * kSystemPointerSize;
#endif

163 164 165 166
}  // namespace wasm
}  // namespace internal
}  // namespace v8

167
#endif  // V8_WASM_WASM_CONSTANTS_H_