LLVM  4.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 
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAssembler.h"
18 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCObjectWriter.h"
24 #include "llvm/MC/MCSymbol.h"
27 using namespace llvm;
28 
29 namespace {
30 class WebAssemblyAsmBackend final : public MCAsmBackend {
31  bool Is64Bit;
32 
33 public:
34  explicit WebAssemblyAsmBackend(bool Is64Bit)
35  : MCAsmBackend(), Is64Bit(Is64Bit) {}
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
44  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
45  const MCRelaxableFragment *DF,
46  const MCAsmLayout &Layout) const override {
47  return false;
48  }
49 
50  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 
56  bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
57 
58  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 
64 bool WebAssemblyAsmBackend::writeNopData(uint64_t Count,
65  MCObjectWriter *OW) const {
66  if (Count == 0)
67  return true;
68 
69  for (uint64_t i = 0; i < Count; ++i)
71 
72  return true;
73 }
74 
75 void WebAssemblyAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
76  unsigned DataSize, uint64_t Value,
77  bool IsPCRel) const {
78  const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
79  assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
80 
81  unsigned NumBytes = (Info.TargetSize + 7) / 8;
82  if (Value == 0)
83  return; // Doesn't change encoding.
84 
85  // Shift the value into position.
86  Value <<= Info.TargetOffset;
87 
88  unsigned Offset = Fixup.getOffset();
89  assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
90 
91  // For each byte of the fragment that the fixup touches, mask in the
92  // bits from the fixup value.
93  for (unsigned i = 0; i != NumBytes; ++i)
94  Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
95 }
96 
98 WebAssemblyAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
99  return createWebAssemblyELFObjectWriter(OS, Is64Bit, 0);
100 }
101 } // end anonymous namespace
102 
104  return new WebAssemblyAsmBackend(TT.isArch64Bit());
105 }
size_t i
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:1202
uint32_t getOffset() const
Definition: MCFixup.h:95
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:249
This file provides WebAssembly-specific target descriptions.
uint32_t Offset
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)
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
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:71
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
static const unsigned Nop
Instruction opcodes emitted via means other than CodeGen.
unsigned Flags
Flags describing additional information on this fixup kind.