• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /// \brief This file implements the WebAssemblyAsmBackend class.
12 ///
13 //===----------------------------------------------------------------------===//
14 
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"
27 using namespace llvm;
28 
29 namespace {
30 class WebAssemblyAsmBackend final : public MCAsmBackend {
31   bool Is64Bit;
32 
33 public:
WebAssemblyAsmBackend(bool Is64Bit)34   explicit WebAssemblyAsmBackend(bool Is64Bit)
35       : MCAsmBackend(), Is64Bit(Is64Bit) {}
~WebAssemblyAsmBackend()36   ~WebAssemblyAsmBackend() override {}
37 
38   void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
39                   uint64_t Value, bool IsPCRel) const override;
40 
41   MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
42 
43   // No instruction requires relaxation
fixupNeedsRelaxation(const MCFixup & Fixup,uint64_t Value,const MCRelaxableFragment * DF,const MCAsmLayout & Layout) const44   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
45                             const MCRelaxableFragment *DF,
46                             const MCAsmLayout &Layout) const override {
47     return false;
48   }
49 
getNumFixupKinds() const50   unsigned getNumFixupKinds() const override {
51     // We currently just use the generic fixups in MCFixup.h and don't have any
52     // target-specific fixups.
53     return 0;
54   }
55 
mayNeedRelaxation(const MCInst & Inst) const56   bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
57 
relaxInstruction(const MCInst & Inst,const MCSubtargetInfo & STI,MCInst & Res) const58   void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
59                         MCInst &Res) const override {}
60 
61   bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
62 };
63 
writeNopData(uint64_t Count,MCObjectWriter * OW) const64 bool WebAssemblyAsmBackend::writeNopData(uint64_t Count,
65                                          MCObjectWriter *OW) const {
66   if (Count == 0)
67     return true;
68 
69   // FIXME: Do something.
70   return false;
71 }
72 
applyFixup(const MCFixup & Fixup,char * Data,unsigned DataSize,uint64_t Value,bool IsPCRel) const73 void WebAssemblyAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
74                                        unsigned DataSize, uint64_t Value,
75                                        bool IsPCRel) const {
76   const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
77   assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
78 
79   unsigned NumBytes = (Info.TargetSize + 7) / 8;
80   if (Value == 0)
81     return; // Doesn't change encoding.
82 
83   // Shift the value into position.
84   Value <<= Info.TargetOffset;
85 
86   unsigned Offset = Fixup.getOffset();
87   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
88 
89   // For each byte of the fragment that the fixup touches, mask in the
90   // bits from the fixup value.
91   for (unsigned i = 0; i != NumBytes; ++i)
92     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
93 }
94 
95 MCObjectWriter *
createObjectWriter(raw_pwrite_stream & OS) const96 WebAssemblyAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
97   return createWebAssemblyELFObjectWriter(OS, Is64Bit, 0);
98 }
99 } // end anonymous namespace
100 
createWebAssemblyAsmBackend(const Triple & TT)101 MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
102   return new WebAssemblyAsmBackend(TT.isArch64Bit());
103 }
104