LLVM 19.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
55 // No instruction requires relaxation
58 const MCAsmLayout &Layout) const override {
59 return false;
60 }
61
63 const MCSubtargetInfo *STI) const override;
64};
65
66const MCFixupKindInfo &
67WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
69 // This table *must* be in the order that the fixup_* kinds are defined in
70 // WebAssemblyFixupKinds.h.
71 //
72 // Name Offset (bits) Size (bits) Flags
73 {"fixup_sleb128_i32", 0, 5 * 8, 0},
74 {"fixup_sleb128_i64", 0, 10 * 8, 0},
75 {"fixup_uleb128_i32", 0, 5 * 8, 0},
76 {"fixup_uleb128_i64", 0, 10 * 8, 0},
77 };
78
79 if (Kind < FirstTargetFixupKind)
81
82 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
83 "Invalid kind!");
84 return Infos[Kind - FirstTargetFixupKind];
85}
86
87bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
88 const MCSubtargetInfo *STI) const {
89 for (uint64_t I = 0; I < Count; ++I)
90 OS << char(WebAssembly::Nop);
91
92 return true;
93}
94
95void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
96 const MCFixup &Fixup,
97 const MCValue &Target,
99 uint64_t Value, bool IsPCRel,
100 const MCSubtargetInfo *STI) const {
101 const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
102 assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
103
104 unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
105 if (Value == 0)
106 return; // Doesn't change encoding.
107
108 // Shift the value into position.
109 Value <<= Info.TargetOffset;
110
111 unsigned Offset = Fixup.getOffset();
112 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
113
114 // For each byte of the fragment that the fixup touches, mask in the
115 // bits from the fixup value.
116 for (unsigned I = 0; I != NumBytes; ++I)
117 Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
118}
119
120std::unique_ptr<MCObjectTargetWriter>
121WebAssemblyAsmBackend::createObjectTargetWriter() const {
122 return createWebAssemblyWasmObjectWriter(Is64Bit, IsEmscripten);
123}
124
125} // end anonymous namespace
126
128 return new WebAssemblyAsmBackend(TT.isArch64Bit(), TT.isOSEmscripten());
129}
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
#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:43
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 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const =0
Simple predicate for targets where !Resolved implies requiring relaxation.
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...
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:274
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:456
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.