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 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" 25 26 using namespace llvm; 27 28 namespace { 29 30 class WebAssemblyAsmBackend final : public MCAsmBackend { 31 bool Is64Bit; 32 bool IsEmscripten; 33 34 public: 35 explicit WebAssemblyAsmBackend(bool Is64Bit, bool IsEmscripten) 36 : MCAsmBackend(llvm::endianness::little), Is64Bit(Is64Bit), 37 IsEmscripten(IsEmscripten) {} 38 39 unsigned getNumFixupKinds() const override { 40 return WebAssembly::NumTargetFixupKinds; 41 } 42 43 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override; 44 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; 49 50 std::unique_ptr<MCObjectTargetWriter> 51 createObjectTargetWriter() const override; 52 53 bool writeNopData(raw_ostream &OS, uint64_t Count, 54 const MCSubtargetInfo *STI) const override; 55 }; 56 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. 62 // 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}, 68 }; 69 70 if (Kind < FirstTargetFixupKind) 71 return MCAsmBackend::getFixupKindInfo(Kind); 72 73 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && 74 "Invalid kind!"); 75 return Infos[Kind - FirstTargetFixupKind]; 76 } 77 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); 82 83 return true; 84 } 85 86 void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm, 87 const MCFixup &Fixup, 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"); 94 95 unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8; 96 if (Value == 0) 97 return; // Doesn't change encoding. 98 99 // Shift the value into position. 100 Value <<= Info.TargetOffset; 101 102 unsigned Offset = Fixup.getOffset(); 103 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!"); 104 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); 109 } 110 111 std::unique_ptr<MCObjectTargetWriter> 112 WebAssemblyAsmBackend::createObjectTargetWriter() const { 113 return createWebAssemblyWasmObjectWriter(Is64Bit, IsEmscripten); 114 } 115 116 } // end anonymous namespace 117 118 MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) { 119 return new WebAssemblyAsmBackend(TT.isArch64Bit(), TT.isOSEmscripten()); 120 } 121