1 //===-- WebAssemblyAsmBackend.cpp - WebAssembly Assembler Backend ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements the WebAssemblyAsmBackend class.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "MCTargetDesc/WebAssemblyFixupKinds.h"
16 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCELFObjectWriter.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCFixupKindInfo.h"
23 #include "llvm/MC/MCObjectWriter.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCWasmObjectWriter.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm;
30 
31 namespace {
32 
33 class WebAssemblyAsmBackend final : public MCAsmBackend {
34   bool Is64Bit;
35   bool IsELF;
36 
37  public:
38   explicit WebAssemblyAsmBackend(bool Is64Bit, bool IsELF)
39       : MCAsmBackend(support::little), Is64Bit(Is64Bit), IsELF(IsELF) {}
40   ~WebAssemblyAsmBackend() override {}
41 
42   unsigned getNumFixupKinds() const override {
43     return WebAssembly::NumTargetFixupKinds;
44   }
45 
46   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
47 
48   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
49                   const MCValue &Target, MutableArrayRef<char> Data,
50                   uint64_t Value, bool IsPCRel) const override;
51 
52   std::unique_ptr<MCObjectTargetWriter>
53   createObjectTargetWriter() const override;
54 
55   // No instruction requires relaxation
56   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
57                             const MCRelaxableFragment *DF,
58                             const MCAsmLayout &Layout) const override {
59     return false;
60   }
61 
62   bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
63 
64   void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
65                         MCInst &Res) const override {}
66 
67   bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
68 };
69 
70 const MCFixupKindInfo &
71 WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
72   const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
73     // This table *must* be in the order that the fixup_* kinds are defined in
74     // WebAssemblyFixupKinds.h.
75     //
76     // Name                     Offset (bits) Size (bits)     Flags
77     { "fixup_code_sleb128_i32", 0,            5*8,            0 },
78     { "fixup_code_sleb128_i64", 0,            10*8,           0 },
79     { "fixup_code_uleb128_i32", 0,            5*8,            0 },
80   };
81 
82   if (Kind < FirstTargetFixupKind)
83     return MCAsmBackend::getFixupKindInfo(Kind);
84 
85   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
86          "Invalid kind!");
87   return Infos[Kind - FirstTargetFixupKind];
88 }
89 
90 bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS,
91                                          uint64_t Count) const {
92   for (uint64_t i = 0; i < Count; ++i)
93     OS << char(WebAssembly::Nop);
94 
95   return true;
96 }
97 
98 void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
99                                        const MCFixup &Fixup,
100                                        const MCValue &Target,
101                                        MutableArrayRef<char> Data,
102                                        uint64_t Value, bool IsPCRel) const {
103   const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
104   assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
105 
106   unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
107   if (Value == 0)
108     return; // Doesn't change encoding.
109 
110   // Shift the value into position.
111   Value <<= Info.TargetOffset;
112 
113   unsigned Offset = Fixup.getOffset();
114   assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
115 
116   // For each byte of the fragment that the fixup touches, mask in the
117   // bits from the fixup value.
118   for (unsigned i = 0; i != NumBytes; ++i)
119     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
120 }
121 
122 std::unique_ptr<MCObjectTargetWriter>
123 WebAssemblyAsmBackend::createObjectTargetWriter() const {
124   return IsELF ? createWebAssemblyELFObjectWriter(Is64Bit, 0)
125                : createWebAssemblyWasmObjectWriter(Is64Bit);
126 }
127 
128 } // end anonymous namespace
129 
130 MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
131   return new WebAssemblyAsmBackend(TT.isArch64Bit(), TT.isOSBinFormatELF());
132 }
133