LLDB mainline
WasmAddress.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_WASM_WASMADDRESS_H
10#define LLDB_SOURCE_PLUGINS_OBJECTFILE_WASM_WASMADDRESS_H
11
12#include "lldb/lldb-types.h"
13#include <cstdint>
14
15namespace lldb_private {
16namespace wasm {
17
18/// Each WebAssembly module has separate address spaces for Code and Memory. A
19/// WebAssembly module also has a Data section which, when the module is loaded,
20/// gets mapped into a region in the module Memory.
21///
22/// Globals are not addressable: they live in an index space of their own. The
23/// synthetic Global space stands in for one, holding the index where an address
24/// would hold an offset, so that a global can be named and read.
25///
26/// The tag is two bits wide and these are the only spaces there are, so the
27/// remaining value means an address that belongs to nothing.
28enum WasmAddressType : uint8_t {
29 Memory = 0x00,
30 Object = 0x01,
31 Global = 0x02,
32 Invalid = 0x03,
33};
34
35/// Widths of the fields a 64-bit address is made of, from the low bits up. The
36/// bitfields below are declared with the same constants, so a field and the
37/// mask that extracts it cannot come to disagree.
38static constexpr uint32_t kWasmOffsetBits = 32;
39static constexpr uint32_t kWasmModuleIDBits = 30;
40static constexpr uint32_t kWasmAddressTypeBits = 2;
41
43 "a Wasm address has to account for all 64 bits");
44
45static constexpr uint32_t kWasmModuleIDShift = kWasmOffsetBits;
46static constexpr uint32_t kWasmAddressTypeShift =
48
49static constexpr uint64_t MakeFieldMask(uint32_t bits, uint32_t shift) {
50 return ((uint64_t(1) << bits) - 1) << shift;
51}
52
53static constexpr uint64_t kWasmOffsetMask = MakeFieldMask(kWasmOffsetBits, 0);
54static constexpr uint64_t kWasmModuleIDMask =
56static constexpr uint64_t kWasmAddressTypeMask =
58
59/// For the purpose of debugging, we can represent all these separated 32-bit
60/// address spaces with a single virtual 64-bit address space. The
61/// wasm_addr_t provides this encoding using bitfields.
66
71
74
75 WasmAddressType GetType() const { return static_cast<WasmAddressType>(type); }
76 uint32_t GetModuleID() const { return module_id; }
77 uint32_t GetOffset() const { return offset; }
78
79 operator lldb::addr_t() { return *(uint64_t *)this; }
80};
81
82static_assert(sizeof(wasm_addr_t) == 8, "");
83
84} // namespace wasm
85} // namespace lldb_private
86
87#endif
static constexpr uint32_t kWasmModuleIDShift
Definition WasmAddress.h:45
static constexpr uint64_t MakeFieldMask(uint32_t bits, uint32_t shift)
Definition WasmAddress.h:49
static constexpr uint32_t kWasmModuleIDBits
Definition WasmAddress.h:39
static constexpr uint64_t kWasmAddressTypeMask
Definition WasmAddress.h:56
static constexpr uint32_t kWasmAddressTypeBits
Definition WasmAddress.h:40
static constexpr uint32_t kWasmOffsetBits
Widths of the fields a 64-bit address is made of, from the low bits up.
Definition WasmAddress.h:38
WasmAddressType
Each WebAssembly module has separate address spaces for Code and Memory.
Definition WasmAddress.h:28
static constexpr uint64_t kWasmModuleIDMask
Definition WasmAddress.h:54
static constexpr uint32_t kWasmAddressTypeShift
Definition WasmAddress.h:46
static constexpr uint64_t kWasmOffsetMask
Definition WasmAddress.h:53
A class that represents a running process on the host machine.
static uint32_t bits(const uint32_t val, const uint32_t msbit, const uint32_t lsbit)
Definition ARMUtils.h:265
uint64_t addr_t
Definition lldb-types.h:80
wasm_addr_t(WasmAddressType type, uint32_t module_id, uint32_t offset)
Definition WasmAddress.h:72
wasm_addr_t(lldb::addr_t addr)
Definition WasmAddress.h:67
WasmAddressType GetType() const
Definition WasmAddress.h:75