LLVM  8.0.1
WebAssemblyAsmBackend.cpp
Go to the documentation of this file.
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 
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCObjectWriter.h"
24 #include "llvm/MC/MCSymbol.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  : MCAsmBackend(support::little), Is64Bit(Is64Bit) {}
39  ~WebAssemblyAsmBackend() override {}
40 
41  unsigned getNumFixupKinds() const override {
43  }
44 
45  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
46 
47  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
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  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,
63  const MCSubtargetInfo &STI) const override {
64  return false;
65  }
66 
67  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
68  MCInst &Res) const override {}
69 
70  bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
71 };
72 
73 const MCFixupKindInfo &
74 WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
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  if (Kind < FirstTargetFixupKind)
86  return MCAsmBackend::getFixupKindInfo(Kind);
87 
88  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
89  "Invalid kind!");
90  return Infos[Kind - FirstTargetFixupKind];
91 }
92 
93 bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS,
94  uint64_t Count) const {
95  for (uint64_t i = 0; i < Count; ++i)
96  OS << char(WebAssembly::Nop);
97 
98  return true;
99 }
100 
101 void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
102  const MCFixup &Fixup,
103  const MCValue &Target,
105  uint64_t Value, bool IsPCRel,
106  const MCSubtargetInfo *STI) const {
107  const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
108  assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
109 
110  unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
111  if (Value == 0)
112  return; // Doesn't change encoding.
113 
114  // Shift the value into position.
115  Value <<= Info.TargetOffset;
116 
117  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  for (unsigned i = 0; i != NumBytes; ++i)
123  Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
124 }
125 
126 std::unique_ptr<MCObjectTargetWriter>
127 WebAssemblyAsmBackend::createObjectTargetWriter() const {
128  return createWebAssemblyWasmObjectWriter(Is64Bit);
129 }
130 
131 } // end anonymous namespace
132 
134  return new WebAssemblyAsmBackend(TT.isArch64Bit());
135 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
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:685
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:74
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
Analysis containing CSE Info
Definition: CSEInfo.cpp:21
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:271
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
This file provides WebAssembly-specific target descriptions.
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
uint32_t getOffset() const
Definition: MCFixup.h:125
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
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:1269
Generic base class for all target subtargets.
Target independent information on a fixup kind.
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
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:46
unsigned Flags
Flags describing additional information on this fixup kind.
MCFixupKind getKind() const
Definition: MCFixup.h:123