10b57cec5SDimitry Andric //===-- WebAssemblyAsmBackend.cpp - WebAssembly Assembler Backend ---------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file implements the WebAssemblyAsmBackend class.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyFixupKinds.h"
150b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCAsmBackend.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCAssembler.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCDirectives.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCFixupKindInfo.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCObjectWriter.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCWasmObjectWriter.h"
250b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
260b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric namespace {
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric class WebAssemblyAsmBackend final : public MCAsmBackend {
330b57cec5SDimitry Andric bool Is64Bit;
348bcb0991SDimitry Andric bool IsEmscripten;
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric public:
WebAssemblyAsmBackend(bool Is64Bit,bool IsEmscripten)378bcb0991SDimitry Andric explicit WebAssemblyAsmBackend(bool Is64Bit, bool IsEmscripten)
38*5f757f3fSDimitry Andric : MCAsmBackend(llvm::endianness::little), Is64Bit(Is64Bit),
398bcb0991SDimitry Andric IsEmscripten(IsEmscripten) {}
400b57cec5SDimitry Andric
getNumFixupKinds() const410b57cec5SDimitry Andric unsigned getNumFixupKinds() const override {
420b57cec5SDimitry Andric return WebAssembly::NumTargetFixupKinds;
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
480b57cec5SDimitry Andric const MCValue &Target, MutableArrayRef<char> Data,
490b57cec5SDimitry Andric uint64_t Value, bool IsPCRel,
500b57cec5SDimitry Andric const MCSubtargetInfo *STI) const override;
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric std::unique_ptr<MCObjectTargetWriter>
530b57cec5SDimitry Andric createObjectTargetWriter() const override;
540b57cec5SDimitry Andric
55349cc55cSDimitry Andric bool writeNopData(raw_ostream &OS, uint64_t Count,
56349cc55cSDimitry Andric const MCSubtargetInfo *STI) const override;
570b57cec5SDimitry Andric };
580b57cec5SDimitry Andric
590b57cec5SDimitry Andric const MCFixupKindInfo &
getFixupKindInfo(MCFixupKind Kind) const600b57cec5SDimitry Andric WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
610b57cec5SDimitry Andric const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
620b57cec5SDimitry Andric // This table *must* be in the order that the fixup_* kinds are defined in
630b57cec5SDimitry Andric // WebAssemblyFixupKinds.h.
640b57cec5SDimitry Andric //
650b57cec5SDimitry Andric // Name Offset (bits) Size (bits) Flags
660b57cec5SDimitry Andric {"fixup_sleb128_i32", 0, 5 * 8, 0},
670b57cec5SDimitry Andric {"fixup_sleb128_i64", 0, 10 * 8, 0},
680b57cec5SDimitry Andric {"fixup_uleb128_i32", 0, 5 * 8, 0},
695ffd83dbSDimitry Andric {"fixup_uleb128_i64", 0, 10 * 8, 0},
700b57cec5SDimitry Andric };
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric if (Kind < FirstTargetFixupKind)
730b57cec5SDimitry Andric return MCAsmBackend::getFixupKindInfo(Kind);
740b57cec5SDimitry Andric
750b57cec5SDimitry Andric assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
760b57cec5SDimitry Andric "Invalid kind!");
770b57cec5SDimitry Andric return Infos[Kind - FirstTargetFixupKind];
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric
writeNopData(raw_ostream & OS,uint64_t Count,const MCSubtargetInfo * STI) const80349cc55cSDimitry Andric bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
81349cc55cSDimitry Andric const MCSubtargetInfo *STI) const {
820b57cec5SDimitry Andric for (uint64_t I = 0; I < Count; ++I)
830b57cec5SDimitry Andric OS << char(WebAssembly::Nop);
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric return true;
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric
applyFixup(const MCAssembler & Asm,const MCFixup & Fixup,const MCValue & Target,MutableArrayRef<char> Data,uint64_t Value,bool IsPCRel,const MCSubtargetInfo * STI) const880b57cec5SDimitry Andric void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
890b57cec5SDimitry Andric const MCFixup &Fixup,
900b57cec5SDimitry Andric const MCValue &Target,
910b57cec5SDimitry Andric MutableArrayRef<char> Data,
920b57cec5SDimitry Andric uint64_t Value, bool IsPCRel,
930b57cec5SDimitry Andric const MCSubtargetInfo *STI) const {
940b57cec5SDimitry Andric const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
950b57cec5SDimitry Andric assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
980b57cec5SDimitry Andric if (Value == 0)
990b57cec5SDimitry Andric return; // Doesn't change encoding.
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric // Shift the value into position.
1020b57cec5SDimitry Andric Value <<= Info.TargetOffset;
1030b57cec5SDimitry Andric
1040b57cec5SDimitry Andric unsigned Offset = Fixup.getOffset();
1050b57cec5SDimitry Andric assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
1060b57cec5SDimitry Andric
1070b57cec5SDimitry Andric // For each byte of the fragment that the fixup touches, mask in the
1080b57cec5SDimitry Andric // bits from the fixup value.
1090b57cec5SDimitry Andric for (unsigned I = 0; I != NumBytes; ++I)
1100b57cec5SDimitry Andric Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const1140b57cec5SDimitry Andric WebAssemblyAsmBackend::createObjectTargetWriter() const {
1158bcb0991SDimitry Andric return createWebAssemblyWasmObjectWriter(Is64Bit, IsEmscripten);
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric } // end anonymous namespace
1190b57cec5SDimitry Andric
createWebAssemblyAsmBackend(const Triple & TT)1200b57cec5SDimitry Andric MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
1218bcb0991SDimitry Andric return new WebAssemblyAsmBackend(TT.isArch64Bit(), TT.isOSEmscripten());
1220b57cec5SDimitry Andric }
123