1 //===-- WebAssemblyAsmBackend.cpp - WebAssembly Assembler Backend ---------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements the WebAssemblyAsmBackend class.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/WebAssemblyFixupKinds.h"
15 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAssembler.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCFixupKindInfo.h"
20 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MCWasmObjectWriter.h"
24 #include "llvm/Support/raw_ostream.h"
30 class WebAssemblyAsmBackend final
: public MCAsmBackend
{
35 explicit WebAssemblyAsmBackend(bool Is64Bit
, bool IsEmscripten
)
36 : MCAsmBackend(llvm::endianness::little
), Is64Bit(Is64Bit
),
37 IsEmscripten(IsEmscripten
) {}
39 unsigned getNumFixupKinds() const override
{
40 return WebAssembly::NumTargetFixupKinds
;
43 const MCFixupKindInfo
&getFixupKindInfo(MCFixupKind Kind
) const override
;
45 void applyFixup(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
46 const MCValue
&Target
, MutableArrayRef
<char> Data
,
47 uint64_t Value
, bool IsPCRel
,
48 const MCSubtargetInfo
*STI
) const override
;
50 std::unique_ptr
<MCObjectTargetWriter
>
51 createObjectTargetWriter() const override
;
53 bool writeNopData(raw_ostream
&OS
, uint64_t Count
,
54 const MCSubtargetInfo
*STI
) const override
;
57 const MCFixupKindInfo
&
58 WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind
) const {
59 const static MCFixupKindInfo Infos
[WebAssembly::NumTargetFixupKinds
] = {
60 // This table *must* be in the order that the fixup_* kinds are defined in
61 // WebAssemblyFixupKinds.h.
63 // Name Offset (bits) Size (bits) Flags
64 {"fixup_sleb128_i32", 0, 5 * 8, 0},
65 {"fixup_sleb128_i64", 0, 10 * 8, 0},
66 {"fixup_uleb128_i32", 0, 5 * 8, 0},
67 {"fixup_uleb128_i64", 0, 10 * 8, 0},
70 if (Kind
< FirstTargetFixupKind
)
71 return MCAsmBackend::getFixupKindInfo(Kind
);
73 assert(unsigned(Kind
- FirstTargetFixupKind
) < getNumFixupKinds() &&
75 return Infos
[Kind
- FirstTargetFixupKind
];
78 bool WebAssemblyAsmBackend::writeNopData(raw_ostream
&OS
, uint64_t Count
,
79 const MCSubtargetInfo
*STI
) const {
80 for (uint64_t I
= 0; I
< Count
; ++I
)
81 OS
<< char(WebAssembly::Nop
);
86 void WebAssemblyAsmBackend::applyFixup(const MCAssembler
&Asm
,
88 const MCValue
&Target
,
89 MutableArrayRef
<char> Data
,
90 uint64_t Value
, bool IsPCRel
,
91 const MCSubtargetInfo
*STI
) const {
92 const MCFixupKindInfo
&Info
= getFixupKindInfo(Fixup
.getKind());
93 assert(Info
.Flags
== 0 && "WebAssembly does not use MCFixupKindInfo flags");
95 unsigned NumBytes
= alignTo(Info
.TargetSize
, 8) / 8;
97 return; // Doesn't change encoding.
99 // Shift the value into position.
100 Value
<<= Info
.TargetOffset
;
102 unsigned Offset
= Fixup
.getOffset();
103 assert(Offset
+ NumBytes
<= Data
.size() && "Invalid fixup offset!");
105 // For each byte of the fragment that the fixup touches, mask in the
106 // bits from the fixup value.
107 for (unsigned I
= 0; I
!= NumBytes
; ++I
)
108 Data
[Offset
+ I
] |= uint8_t((Value
>> (I
* 8)) & 0xff);
111 std::unique_ptr
<MCObjectTargetWriter
>
112 WebAssemblyAsmBackend::createObjectTargetWriter() const {
113 return createWebAssemblyWasmObjectWriter(Is64Bit
, IsEmscripten
);
116 } // end anonymous namespace
118 MCAsmBackend
*llvm::createWebAssemblyAsmBackend(const Triple
&TT
) {
119 return new WebAssemblyAsmBackend(TT
.isArch64Bit(), TT
.isOSEmscripten());