LLVM 20.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
17#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCSymbol.h"
27
28using namespace llvm;
29
30namespace {
31
32class WebAssemblyAsmBackend final : public MCAsmBackend {
33 bool Is64Bit;
34 bool IsEmscripten;
35
36public:
37 explicit WebAssemblyAsmBackend(bool Is64Bit, bool IsEmscripten)
38 : MCAsmBackend(llvm::endianness::little), Is64Bit(Is64Bit),
39 IsEmscripten(IsEmscripten) {}
40
41 unsigned getNumFixupKinds() const override {
43 }
44
45 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
46
47 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
49 uint64_t Value, bool IsPCRel,
50 const MCSubtargetInfo *STI) const override;
51
52 std::unique_ptr<MCObjectTargetWriter>
53 createObjectTargetWriter() const override;
54
56 const MCSubtargetInfo *STI) const override;
57};
58
59const MCFixupKindInfo &
60WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
62 // This table *must* be in the order that the fixup_* kinds are defined in
63 // WebAssemblyFixupKinds.h.
64 //
65 // Name Offset (bits) Size (bits) Flags
66 {"fixup_sleb128_i32", 0, 5 * 8, 0},
67 {"fixup_sleb128_i64", 0, 10 * 8, 0},
68 {"fixup_uleb128_i32", 0, 5 * 8, 0},
69 {"fixup_uleb128_i64", 0, 10 * 8, 0},
70 };
71
72 if (Kind < FirstTargetFixupKind)
74
75 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
76 "Invalid kind!");
77 return Infos[Kind - FirstTargetFixupKind];
78}
79
80bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
81 const MCSubtargetInfo *STI) const {
82 for (uint64_t I = 0; I < Count; ++I)
83 OS << char(WebAssembly::Nop);
84
85 return true;
86}
87
88void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
89 const MCFixup &Fixup,
90 const MCValue &Target,
92 uint64_t Value, bool IsPCRel,
93 const MCSubtargetInfo *STI) const {
94 const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
95 assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
96
97 unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
98 if (Value == 0)
99 return; // Doesn't change encoding.
100
101 // Shift the value into position.
102 Value <<= Info.TargetOffset;
103
104 unsigned Offset = Fixup.getOffset();
105 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
106
107 // For each byte of the fragment that the fixup touches, mask in the
108 // bits from the fixup value.
109 for (unsigned I = 0; I != NumBytes; ++I)
110 Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
111}
112
113std::unique_ptr<MCObjectTargetWriter>
114WebAssemblyAsmBackend::createObjectTargetWriter() const {
115 return createWebAssemblyWasmObjectWriter(Is64Bit, IsEmscripten);
116}
117
118} // end anonymous namespace
119
121 return new WebAssemblyAsmBackend(TT.isArch64Bit(), TT.isOSEmscripten());
122}
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define I(x, y, z)
Definition: MD5.cpp:58
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file provides WebAssembly-specific target descriptions.
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
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 std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Generic base class for all target subtargets.
This represents an "assembler immediate".
Definition: MCValue.h:36
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
static const unsigned Nop
Instruction opcodes emitted via means other than CodeGen.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
MCAsmBackend * createWebAssemblyAsmBackend(const Triple &TT)
std::unique_ptr< MCObjectTargetWriter > createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten)
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ FirstTargetFixupKind
Definition: MCFixup.h:45
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:70
Target independent information on a fixup kind.