blob: 337c18ca57d414915dc6f6944536503a6499c3f4 [file] [log] [blame]
Dan Gohman05ac43f2015-12-17 01:39:00 +00001//===-- 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
15#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16#include "llvm/MC/MCAsmBackend.h"
17#include "llvm/MC/MCAssembler.h"
18#include "llvm/MC/MCDirectives.h"
19#include "llvm/MC/MCELFObjectWriter.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000020#include "llvm/MC/MCWasmObjectWriter.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCFixupKindInfo.h"
23#include "llvm/MC/MCObjectWriter.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
28using namespace llvm;
29
30namespace {
Dan Gohman18eafb62017-02-22 01:23:18 +000031class WebAssemblyAsmBackendELF final : public MCAsmBackend {
32 bool Is64Bit;
33
34public:
35 explicit WebAssemblyAsmBackendELF(bool Is64Bit)
36 : MCAsmBackend(), Is64Bit(Is64Bit) {}
37 ~WebAssemblyAsmBackendELF() override {}
38
39 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
40 uint64_t Value, bool IsPCRel) const override;
41
42 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
43
44 // No instruction requires relaxation
45 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
46 const MCRelaxableFragment *DF,
47 const MCAsmLayout &Layout) const override {
48 return false;
49 }
50
51 unsigned getNumFixupKinds() const override {
52 // We currently just use the generic fixups in MCFixup.h and don't have any
53 // target-specific fixups.
54 return 0;
55 }
56
57 bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
58
59 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
60 MCInst &Res) const override {}
61
62 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
63};
64
Dan Gohman05ac43f2015-12-17 01:39:00 +000065class WebAssemblyAsmBackend final : public MCAsmBackend {
66 bool Is64Bit;
67
68public:
69 explicit WebAssemblyAsmBackend(bool Is64Bit)
70 : MCAsmBackend(), Is64Bit(Is64Bit) {}
71 ~WebAssemblyAsmBackend() override {}
72
73 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
74 uint64_t Value, bool IsPCRel) const override;
75
76 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
77
78 // No instruction requires relaxation
79 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
80 const MCRelaxableFragment *DF,
81 const MCAsmLayout &Layout) const override {
82 return false;
83 }
84
85 unsigned getNumFixupKinds() const override {
86 // We currently just use the generic fixups in MCFixup.h and don't have any
87 // target-specific fixups.
88 return 0;
89 }
90
91 bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
92
Nirav Dave86030622016-07-11 14:23:53 +000093 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
94 MCInst &Res) const override {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000095
96 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
97};
98
Dan Gohman18eafb62017-02-22 01:23:18 +000099bool WebAssemblyAsmBackendELF::writeNopData(uint64_t Count,
100 MCObjectWriter *OW) const {
101 for (uint64_t i = 0; i < Count; ++i)
102 OW->write8(WebAssembly::Nop);
103
104 return true;
105}
106
107void WebAssemblyAsmBackendELF::applyFixup(const MCFixup &Fixup, char *Data,
108 unsigned DataSize, uint64_t Value,
109 bool IsPCRel) const {
110 const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
111 assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
112
113 unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
114 if (Value == 0)
115 return; // Doesn't change encoding.
116
117 // Shift the value into position.
118 Value <<= Info.TargetOffset;
119
120 unsigned Offset = Fixup.getOffset();
121 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
122
123 // For each byte of the fragment that the fixup touches, mask in the
124 // bits from the fixup value.
125 for (unsigned i = 0; i != NumBytes; ++i)
126 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
127}
128
129MCObjectWriter *
130WebAssemblyAsmBackendELF::createObjectWriter(raw_pwrite_stream &OS) const {
131 return createWebAssemblyELFObjectWriter(OS, Is64Bit, 0);
132}
133
Dan Gohman05ac43f2015-12-17 01:39:00 +0000134bool WebAssemblyAsmBackend::writeNopData(uint64_t Count,
135 MCObjectWriter *OW) const {
136 if (Count == 0)
137 return true;
138
Dan Gohman3acb1872016-10-24 23:27:49 +0000139 for (uint64_t i = 0; i < Count; ++i)
140 OW->write8(WebAssembly::Nop);
141
142 return true;
Dan Gohman05ac43f2015-12-17 01:39:00 +0000143}
144
145void WebAssemblyAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
146 unsigned DataSize, uint64_t Value,
147 bool IsPCRel) const {
148 const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
Dan Gohmana39ca602016-01-13 19:31:57 +0000149 assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
150
Dan Gohman18eafb62017-02-22 01:23:18 +0000151 unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
Dan Gohman938ff9f2016-01-13 19:29:37 +0000152 if (Value == 0)
Dan Gohman05ac43f2015-12-17 01:39:00 +0000153 return; // Doesn't change encoding.
154
155 // Shift the value into position.
156 Value <<= Info.TargetOffset;
157
158 unsigned Offset = Fixup.getOffset();
159 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
160
161 // For each byte of the fragment that the fixup touches, mask in the
162 // bits from the fixup value.
163 for (unsigned i = 0; i != NumBytes; ++i)
164 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
165}
166
167MCObjectWriter *
168WebAssemblyAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
Dan Gohman18eafb62017-02-22 01:23:18 +0000169 return createWebAssemblyWasmObjectWriter(OS, Is64Bit);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000170}
171} // end anonymous namespace
172
Dan Gohmancceedf72016-01-08 00:43:54 +0000173MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
Dan Gohman18eafb62017-02-22 01:23:18 +0000174 if (TT.isOSBinFormatELF())
175 return new WebAssemblyAsmBackendELF(TT.isArch64Bit());
Dan Gohman05ac43f2015-12-17 01:39:00 +0000176 return new WebAssemblyAsmBackend(TT.isArch64Bit());
177}