LLVM  9.0.0svn
WebAssemblyAsmBackend.cpp
Go to the documentation of this file.
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 
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAssembler.h"
18 #include "llvm/MC/MCDirectives.h"
19 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCObjectWriter.h"
23 #include "llvm/MC/MCSymbol.h"
27 
28 using namespace llvm;
29 
30 namespace {
31 
32 class WebAssemblyAsmBackend final : public MCAsmBackend {
33  bool Is64Bit;
34 
35 public:
36  explicit WebAssemblyAsmBackend(bool Is64Bit)
37  : MCAsmBackend(support::little), Is64Bit(Is64Bit) {}
38 
39  unsigned getNumFixupKinds() const override {
41  }
42 
43  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
44 
45  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
47  uint64_t Value, bool IsPCRel,
48  const MCSubtargetInfo *STI) const override;
49 
50  std::unique_ptr<MCObjectTargetWriter>
51  createObjectTargetWriter() const override;
52 
53  // No instruction requires relaxation
54  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
55  const MCRelaxableFragment *DF,
56  const MCAsmLayout &Layout) const override {
57  return false;
58  }
59 
60  bool mayNeedRelaxation(const MCInst &Inst,
61  const MCSubtargetInfo &STI) const override {
62  return false;
63  }
64 
65  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
66  MCInst &Res) const override {}
67 
68  bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
69 };
70 
71 const MCFixupKindInfo &
72 WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
74  // This table *must* be in the order that the fixup_* kinds are defined in
75  // WebAssemblyFixupKinds.h.
76  //
77  // Name Offset (bits) Size (bits) Flags
78  {"fixup_sleb128_i32", 0, 5 * 8, 0},
79  {"fixup_sleb128_i64", 0, 10 * 8, 0},
80  {"fixup_uleb128_i32", 0, 5 * 8, 0},
81  };
82 
83  if (Kind < FirstTargetFixupKind)
84  return MCAsmBackend::getFixupKindInfo(Kind);
85 
86  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
87  "Invalid kind!");
88  return Infos[Kind - FirstTargetFixupKind];
89 }
90 
91 bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS,
92  uint64_t Count) const {
93  for (uint64_t I = 0; I < Count; ++I)
94  OS << char(WebAssembly::Nop);
95 
96  return true;
97 }
98 
99 void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
100  const MCFixup &Fixup,
101  const MCValue &Target,
103  uint64_t Value, bool IsPCRel,
104  const MCSubtargetInfo *STI) const {
105  const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
106  assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
107 
108  unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
109  if (Value == 0)
110  return; // Doesn't change encoding.
111 
112  // Shift the value into position.
113  Value <<= Info.TargetOffset;
114 
115  unsigned Offset = Fixup.getOffset();
116  assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
117 
118  // For each byte of the fragment that the fixup touches, mask in the
119  // bits from the fixup value.
120  for (unsigned I = 0; I != NumBytes; ++I)
121  Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
122 }
123 
124 std::unique_ptr<MCObjectTargetWriter>
125 WebAssemblyAsmBackend::createObjectTargetWriter() const {
126  return createWebAssemblyWasmObjectWriter(Is64Bit);
127 }
128 
129 } // end anonymous namespace
130 
132  return new WebAssemblyAsmBackend(TT.isArch64Bit());
133 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:39
std::unique_ptr< MCObjectTargetWriter > createWebAssemblyWasmObjectWriter(bool Is64Bit)
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:684
unsigned TargetOffset
The bit offset to write the relocation into.
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:73
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:270
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:290
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
This file provides WebAssembly-specific target descriptions.
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:22
uint32_t getOffset() const
Definition: MCFixup.h:124
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
unsigned TargetSize
The number of bits written by this fixup.
MCAsmBackend * createWebAssemblyAsmBackend(const Triple &TT)
Target - Wrapper for Target specific information.
bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1276
#define I(x, y, z)
Definition: MD5.cpp:58
Generic base class for all target subtargets.
Target independent information on a fixup kind.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:41
static const unsigned Nop
Instruction opcodes emitted via means other than CodeGen.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
unsigned Flags
Flags describing additional information on this fixup kind.
MCFixupKind getKind() const
Definition: MCFixup.h:122