LLVM 22.0.0git
WebAssemblyAsmBackend.cpp
Go to the documentation of this file.
1//===-- WebAssemblyAsmBackend.cpp - WebAssembly Assembler Backend ---------===//
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/// \file
10/// This file implements the WebAssemblyAsmBackend class.
11///
12//===----------------------------------------------------------------------===//
13
18#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCSymbol.h"
23#include "llvm/MC/MCValue.h"
26
27using namespace llvm;
28
29namespace {
30
31class WebAssemblyAsmBackend final : public MCAsmBackend {
32 bool Is64Bit;
33 bool IsEmscripten;
34
35public:
36 explicit WebAssemblyAsmBackend(bool Is64Bit, bool IsEmscripten)
37 : MCAsmBackend(llvm::endianness::little), Is64Bit(Is64Bit),
38 IsEmscripten(IsEmscripten) {}
39
40 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
41 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
42
43 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
44 uint8_t *Data, uint64_t Value, bool) override;
45
46 std::unique_ptr<MCObjectTargetWriter>
47 createObjectTargetWriter() const override;
48
50 const MCSubtargetInfo *STI) const override;
51};
52
53std::optional<MCFixupKind>
54WebAssemblyAsmBackend::getFixupKind(StringRef Name) const {
56#define WASM_RELOC(NAME, ID) .Case(#NAME, ID)
57#include "llvm/BinaryFormat/WasmRelocs.def"
58#undef WASM_RELOC
59 .Default(-1u);
60 if (Type != -1u)
61 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
62 return std::nullopt;
63}
64
66WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
68 // This table *must* be in the order that the fixup_* kinds are defined in
69 // WebAssemblyFixupKinds.h.
70 //
71 // Name Offset (bits) Size (bits) Flags
72 {"fixup_sleb128_i32", 0, 5 * 8, 0},
73 {"fixup_sleb128_i64", 0, 10 * 8, 0},
74 {"fixup_uleb128_i32", 0, 5 * 8, 0},
75 {"fixup_uleb128_i64", 0, 10 * 8, 0},
76 };
77
78 // Fixup kinds from raw relocation types and .reloc directives force
79 // relocations and do not use these fields.
80 if (mc::isRelocation(Kind))
81 return {};
82
83 if (Kind < FirstTargetFixupKind)
85
86 assert(unsigned(Kind - FirstTargetFixupKind) <
88 "Invalid kind!");
89 return Infos[Kind - FirstTargetFixupKind];
90}
91
92bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
93 const MCSubtargetInfo *STI) const {
94 for (uint64_t I = 0; I < Count; ++I)
95 OS << char(WebAssembly::Nop);
96
97 return true;
98}
99
100void WebAssemblyAsmBackend::applyFixup(const MCFragment &F,
101 const MCFixup &Fixup,
102 const MCValue &Target, uint8_t *Data,
103 uint64_t Value, bool IsResolved) {
104 if (!IsResolved)
105 Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
106
107 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
108 assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
109
110 unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
111 if (Value == 0)
112 return; // Doesn't change encoding.
113
114 // Shift the value into position.
115 Value <<= Info.TargetOffset;
116
117 assert(Fixup.getOffset() + NumBytes <= F.getSize() &&
118 "Invalid fixup offset!");
119
120 // For each byte of the fragment that the fixup touches, mask in the
121 // bits from the fixup value.
122 for (unsigned I = 0; I != NumBytes; ++I)
123 Data[I] |= uint8_t((Value >> (I * 8)) & 0xff);
124}
125
126std::unique_ptr<MCObjectTargetWriter>
127WebAssemblyAsmBackend::createObjectTargetWriter() const {
128 return createWebAssemblyWasmObjectWriter(Is64Bit, IsEmscripten);
129}
130
131} // end anonymous namespace
132
134 return new WebAssemblyAsmBackend(TT.isArch64Bit(), TT.isOSEmscripten());
135}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
PowerPC TLS Dynamic Call Fixup
raw_pwrite_stream & OS
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
This file provides WebAssembly-specific target descriptions.
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:55
virtual bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual std::optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
virtual void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved)=0
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:61
Generic base class for all target subtargets.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
R Default(T Value)
Definition: StringSwitch.h:177
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
static const unsigned Nop
Instruction opcodes emitted via means other than CodeGen.
bool isRelocation(MCFixupKind FixupKind)
Definition: MCFixup.h:130
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ FirstTargetFixupKind
Definition: MCFixup.h:44
@ FirstLiteralRelocationKind
Definition: MCFixup.h:29
MCAsmBackend * createWebAssemblyAsmBackend(const Triple &TT)
std::unique_ptr< MCObjectTargetWriter > createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
endianness
Definition: bit.h:71
Target independent information on a fixup kind.
Definition: MCAsmBackend.h:38