LLVM  5.0.0
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 /// \brief 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"
21 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCObjectWriter.h"
25 #include "llvm/MC/MCSymbol.h"
29 using namespace llvm;
30 
31 namespace {
32 class WebAssemblyAsmBackendELF final : public MCAsmBackend {
33  bool Is64Bit;
34 
35 public:
36  explicit WebAssemblyAsmBackendELF(bool Is64Bit)
37  : MCAsmBackend(), Is64Bit(Is64Bit) {}
38  ~WebAssemblyAsmBackendELF() override {}
39 
40  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
42  uint64_t Value, bool IsPCRel) const override;
43 
44  MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
45 
46  // No instruction requires relaxation
47  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
48  const MCRelaxableFragment *DF,
49  const MCAsmLayout &Layout) const override {
50  return false;
51  }
52 
53  unsigned getNumFixupKinds() const override {
54  // We currently just use the generic fixups in MCFixup.h and don't have any
55  // target-specific fixups.
56  return 0;
57  }
58 
59  bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
60 
61  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
62  MCInst &Res) const override {}
63 
64  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
65 };
66 
67 class WebAssemblyAsmBackend final : public MCAsmBackend {
68  bool Is64Bit;
69 
70 public:
71  explicit WebAssemblyAsmBackend(bool Is64Bit)
72  : MCAsmBackend(), Is64Bit(Is64Bit) {}
73  ~WebAssemblyAsmBackend() override {}
74 
75  unsigned getNumFixupKinds() const override {
77  }
78 
79  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
80 
81  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
83  uint64_t Value, bool IsPCRel) const override;
84 
85  MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
86 
87  // No instruction requires relaxation
88  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
89  const MCRelaxableFragment *DF,
90  const MCAsmLayout &Layout) const override {
91  return false;
92  }
93 
94  bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
95 
96  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
97  MCInst &Res) const override {}
98 
99  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
100 };
101 
102 bool WebAssemblyAsmBackendELF::writeNopData(uint64_t Count,
103  MCObjectWriter *OW) const {
104  for (uint64_t i = 0; i < Count; ++i)
106 
107  return true;
108 }
109 
110 void WebAssemblyAsmBackendELF::applyFixup(const MCAssembler &Asm,
111  const MCFixup &Fixup,
112  const MCValue &Target,
114  uint64_t Value, bool IsPCRel) const {
115  const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
116  assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
117 
118  unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
119  if (Value == 0)
120  return; // Doesn't change encoding.
121 
122  // Shift the value into position.
123  Value <<= Info.TargetOffset;
124 
125  unsigned Offset = Fixup.getOffset();
126  assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
127 
128  // For each byte of the fragment that the fixup touches, mask in the
129  // bits from the fixup value.
130  for (unsigned i = 0; i != NumBytes; ++i)
131  Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
132 }
133 
135 WebAssemblyAsmBackendELF::createObjectWriter(raw_pwrite_stream &OS) const {
136  return createWebAssemblyELFObjectWriter(OS, Is64Bit, 0);
137 }
138 
139 const MCFixupKindInfo &
140 WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
141  const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
142  // This table *must* be in the order that the fixup_* kinds are defined in
143  // WebAssemblyFixupKinds.h.
144  //
145  // Name Offset (bits) Size (bits) Flags
146  { "fixup_code_sleb128_i32", 0, 5*8, 0 },
147  { "fixup_code_sleb128_i64", 0, 10*8, 0 },
148  { "fixup_code_uleb128_i32", 0, 5*8, 0 },
149  };
150 
151  if (Kind < FirstTargetFixupKind)
152  return MCAsmBackend::getFixupKindInfo(Kind);
153 
154  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
155  "Invalid kind!");
156  return Infos[Kind - FirstTargetFixupKind];
157 }
158 
159 bool WebAssemblyAsmBackend::writeNopData(uint64_t Count,
160  MCObjectWriter *OW) const {
161  if (Count == 0)
162  return true;
163 
164  for (uint64_t i = 0; i < Count; ++i)
166 
167  return true;
168 }
169 
170 void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
171  const MCFixup &Fixup,
172  const MCValue &Target,
174  uint64_t Value, bool IsPCRel) const {
175  const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
176  assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
177 
178  unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
179  if (Value == 0)
180  return; // Doesn't change encoding.
181 
182  // Shift the value into position.
183  Value <<= Info.TargetOffset;
184 
185  unsigned Offset = Fixup.getOffset();
186  assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
187 
188  // For each byte of the fragment that the fixup touches, mask in the
189  // bits from the fixup value.
190  for (unsigned i = 0; i != NumBytes; ++i)
191  Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
192 }
193 
195 WebAssemblyAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
196  return createWebAssemblyWasmObjectWriter(OS, Is64Bit);
197 }
198 } // end anonymous namespace
199 
201  if (TT.isOSBinFormatELF())
202  return new WebAssemblyAsmBackendELF(TT.isArch64Bit());
203  return new WebAssemblyAsmBackend(TT.isArch64Bit());
204 }
MCObjectWriter * createWebAssemblyWasmObjectWriter(raw_pwrite_stream &OS, bool Is64Bit)
This represents an "assembler immediate".
Definition: MCValue.h:40
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:677
Defines the object file and target independent interfaces used by the assembler backend to write nati...
void write8(uint8_t Value)
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:66
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1223
uint32_t getOffset() const
Definition: MCFixup.h:95
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:255
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
This file provides WebAssembly-specific target descriptions.
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
MCFixupKind getKind() const
Definition: MCFixup.h:93
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)
MCObjectWriter * createWebAssemblyELFObjectWriter(raw_pwrite_stream &OS, bool Is64Bit, uint8_t OSABI)
Target - Wrapper for Target specific information.
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:577
MCSubtargetInfo - Generic base class for all target subtargets.
Target independent information on a fixup kind.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
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:35
static const unsigned Nop
Instruction opcodes emitted via means other than CodeGen.
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
unsigned Flags
Flags describing additional information on this fixup kind.