LCOV - code coverage report
Current view: top level - lib/Target/WebAssembly/MCTargetDesc - WebAssemblyAsmBackend.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 20 29 69.0 %
Date: 2018-10-20 13:21:21 Functions: 6 11 54.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13