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/MCDirectives.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCFixupKindInfo.h"
21 #include "llvm/MC/MCObjectWriter.h"
22 #include "llvm/MC/MCSubtargetInfo.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/MC/MCWasmObjectWriter.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
32 class WebAssemblyAsmBackend final
: public MCAsmBackend
{
37 explicit WebAssemblyAsmBackend(bool Is64Bit
, bool IsEmscripten
)
38 : MCAsmBackend(support::little
), Is64Bit(Is64Bit
),
39 IsEmscripten(IsEmscripten
) {}
41 unsigned getNumFixupKinds() const override
{
42 return WebAssembly::NumTargetFixupKinds
;
45 const MCFixupKindInfo
&getFixupKindInfo(MCFixupKind Kind
) const override
;
47 void applyFixup(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
48 const MCValue
&Target
, MutableArrayRef
<char> Data
,
49 uint64_t Value
, bool IsPCRel
,
50 const MCSubtargetInfo
*STI
) const override
;
52 std::unique_ptr
<MCObjectTargetWriter
>
53 createObjectTargetWriter() const override
;
55 // No instruction requires relaxation
56 bool fixupNeedsRelaxation(const MCFixup
&Fixup
, uint64_t Value
,
57 const MCRelaxableFragment
*DF
,
58 const MCAsmLayout
&Layout
) const override
{
62 bool writeNopData(raw_ostream
&OS
, uint64_t Count
,
63 const MCSubtargetInfo
*STI
) const override
;
66 const MCFixupKindInfo
&
67 WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind
) const {
68 const static MCFixupKindInfo Infos
[WebAssembly::NumTargetFixupKinds
] = {
69 // This table *must* be in the order that the fixup_* kinds are defined in
70 // WebAssemblyFixupKinds.h.
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},
79 if (Kind
< FirstTargetFixupKind
)
80 return MCAsmBackend::getFixupKindInfo(Kind
);
82 assert(unsigned(Kind
- FirstTargetFixupKind
) < getNumFixupKinds() &&
84 return Infos
[Kind
- FirstTargetFixupKind
];
87 bool 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
);
95 void WebAssemblyAsmBackend::applyFixup(const MCAssembler
&Asm
,
97 const MCValue
&Target
,
98 MutableArrayRef
<char> Data
,
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");
104 unsigned NumBytes
= alignTo(Info
.TargetSize
, 8) / 8;
106 return; // Doesn't change encoding.
108 // Shift the value into position.
109 Value
<<= Info
.TargetOffset
;
111 unsigned Offset
= Fixup
.getOffset();
112 assert(Offset
+ NumBytes
<= Data
.size() && "Invalid fixup offset!");
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);
120 std::unique_ptr
<MCObjectTargetWriter
>
121 WebAssemblyAsmBackend::createObjectTargetWriter() const {
122 return createWebAssemblyWasmObjectWriter(Is64Bit
, IsEmscripten
);
125 } // end anonymous namespace
127 MCAsmBackend
*llvm::createWebAssemblyAsmBackend(const Triple
&TT
) {
128 return new WebAssemblyAsmBackend(TT
.isArch64Bit(), TT
.isOSEmscripten());