1 //===-- WebAssemblyAsmBackend.cpp - WebAssembly Assembler Backend ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements the WebAssemblyAsmBackend class.
13 //===----------------------------------------------------------------------===//
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/MCELFObjectWriter.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCFixupKindInfo.h"
22 #include "llvm/MC/MCObjectWriter.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
30 class WebAssemblyAsmBackend final : public MCAsmBackend {
34 explicit WebAssemblyAsmBackend(bool Is64Bit)
35 : MCAsmBackend(), Is64Bit(Is64Bit) {}
36 ~WebAssemblyAsmBackend() override {}
38 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
39 uint64_t Value, bool IsPCRel) const override;
41 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
43 // No instruction requires relaxation
44 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
45 const MCRelaxableFragment *DF,
46 const MCAsmLayout &Layout) const override {
50 unsigned getNumFixupKinds() const override {
51 // We currently just use the generic fixups in MCFixup.h and don't have any
52 // target-specific fixups.
56 bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
58 void relaxInstruction(const MCInst &Inst, MCInst &Res) const override {}
60 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
63 bool WebAssemblyAsmBackend::writeNopData(uint64_t Count,
64 MCObjectWriter *OW) const {
68 // FIXME: Do something.
72 void WebAssemblyAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
73 unsigned DataSize, uint64_t Value,
75 const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
76 unsigned NumBytes = RoundUpToAlignment(Info.TargetSize, 8);
78 return; // Doesn't change encoding.
80 // Shift the value into position.
81 Value <<= Info.TargetOffset;
83 unsigned Offset = Fixup.getOffset();
84 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
86 // For each byte of the fragment that the fixup touches, mask in the
87 // bits from the fixup value.
88 for (unsigned i = 0; i != NumBytes; ++i)
89 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
93 WebAssemblyAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
94 return createWebAssemblyELFObjectWriter(OS, Is64Bit, 0);
96 } // end anonymous namespace
98 MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Target &T,
99 const MCRegisterInfo &MRI,
102 return new WebAssemblyAsmBackend(TT.isArch64Bit());