blob: 7cb561ab6e4c9eb88949819674db6628d233dd6a [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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file implements the WebAssemblyAsmBackend class.
Dan Gohman05ac43f2015-12-17 01:39:00 +000012///
13//===----------------------------------------------------------------------===//
14
Dan Gohmand934cb82017-02-24 23:18:00 +000015#include "MCTargetDesc/WebAssemblyFixupKinds.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000017#include "llvm/MC/MCAsmBackend.h"
18#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCDirectives.h"
20#include "llvm/MC/MCELFObjectWriter.h"
21#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"
Dan Gohmand37dc2f2017-02-27 22:41:39 +000026#include "llvm/MC/MCWasmObjectWriter.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000027#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
29using namespace llvm;
30
31namespace {
Dan Gohman18eafb62017-02-22 01:23:18 +000032
Dan Gohman05ac43f2015-12-17 01:39:00 +000033class WebAssemblyAsmBackend final : public MCAsmBackend {
34 bool Is64Bit;
Derek Schuff72f19242018-06-04 22:53:36 +000035 bool IsELF;
Dan Gohman05ac43f2015-12-17 01:39:00 +000036
Derek Schuff72f19242018-06-04 22:53:36 +000037 public:
38 explicit WebAssemblyAsmBackend(bool Is64Bit, bool IsELF)
39 : MCAsmBackend(support::little), Is64Bit(Is64Bit), IsELF(IsELF) {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000040 ~WebAssemblyAsmBackend() override {}
41
Dan Gohmand934cb82017-02-24 23:18:00 +000042 unsigned getNumFixupKinds() const override {
43 return WebAssembly::NumTargetFixupKinds;
44 }
45
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
47
Derek Schuffd2c9ec72017-06-24 01:00:43 +000048 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
49 const MCValue &Target, MutableArrayRef<char> Data,
Ilya Biryukov3c9c1062018-06-06 10:57:50 +000050 uint64_t Value, bool IsPCRel,
51 const MCSubtargetInfo *STI) const override;
Dan Gohman05ac43f2015-12-17 01:39:00 +000052
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000053 std::unique_ptr<MCObjectTargetWriter>
54 createObjectTargetWriter() const override;
Dan Gohman05ac43f2015-12-17 01:39:00 +000055
56 // No instruction requires relaxation
57 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
58 const MCRelaxableFragment *DF,
59 const MCAsmLayout &Layout) const override {
60 return false;
61 }
62
Ilya Biryukov3c9c1062018-06-06 10:57:50 +000063 bool mayNeedRelaxation(const MCInst &Inst,
64 const MCSubtargetInfo &STI) const override {
65 return false;
66 }
Dan Gohman05ac43f2015-12-17 01:39:00 +000067
Nirav Dave86030622016-07-11 14:23:53 +000068 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
69 MCInst &Res) const override {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000070
Peter Collingbourne571a3302018-05-21 17:57:19 +000071 bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
Dan Gohman05ac43f2015-12-17 01:39:00 +000072};
73
Dan Gohmand934cb82017-02-24 23:18:00 +000074const MCFixupKindInfo &
75WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
76 const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
77 // This table *must* be in the order that the fixup_* kinds are defined in
78 // WebAssemblyFixupKinds.h.
79 //
80 // Name Offset (bits) Size (bits) Flags
81 { "fixup_code_sleb128_i32", 0, 5*8, 0 },
82 { "fixup_code_sleb128_i64", 0, 10*8, 0 },
83 { "fixup_code_uleb128_i32", 0, 5*8, 0 },
84 };
85
86 if (Kind < FirstTargetFixupKind)
87 return MCAsmBackend::getFixupKindInfo(Kind);
88
89 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
90 "Invalid kind!");
91 return Infos[Kind - FirstTargetFixupKind];
92}
93
Peter Collingbourne571a3302018-05-21 17:57:19 +000094bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS,
95 uint64_t Count) const {
Dan Gohman3acb1872016-10-24 23:27:49 +000096 for (uint64_t i = 0; i < Count; ++i)
Peter Collingbourne571a3302018-05-21 17:57:19 +000097 OS << char(WebAssembly::Nop);
Dan Gohman3acb1872016-10-24 23:27:49 +000098
99 return true;
Dan Gohman05ac43f2015-12-17 01:39:00 +0000100}
101
Derek Schuffd2c9ec72017-06-24 01:00:43 +0000102void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
103 const MCFixup &Fixup,
104 const MCValue &Target,
Reid Kleckneref581752017-06-22 01:07:05 +0000105 MutableArrayRef<char> Data,
Ilya Biryukov3c9c1062018-06-06 10:57:50 +0000106 uint64_t Value, bool IsPCRel,
107 const MCSubtargetInfo *STI) const {
Dan Gohman05ac43f2015-12-17 01:39:00 +0000108 const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
Dan Gohmana39ca602016-01-13 19:31:57 +0000109 assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
110
Dan Gohman18eafb62017-02-22 01:23:18 +0000111 unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
Dan Gohman938ff9f2016-01-13 19:29:37 +0000112 if (Value == 0)
Dan Gohman05ac43f2015-12-17 01:39:00 +0000113 return; // Doesn't change encoding.
114
115 // Shift the value into position.
116 Value <<= Info.TargetOffset;
117
118 unsigned Offset = Fixup.getOffset();
Reid Kleckneref581752017-06-22 01:07:05 +0000119 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
Dan Gohman05ac43f2015-12-17 01:39:00 +0000120
121 // For each byte of the fragment that the fixup touches, mask in the
122 // bits from the fixup value.
123 for (unsigned i = 0; i != NumBytes; ++i)
124 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
125}
126
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000127std::unique_ptr<MCObjectTargetWriter>
128WebAssemblyAsmBackend::createObjectTargetWriter() const {
Derek Schuff72f19242018-06-04 22:53:36 +0000129 return IsELF ? createWebAssemblyELFObjectWriter(Is64Bit, 0)
130 : createWebAssemblyWasmObjectWriter(Is64Bit);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000131}
Derek Schuff72f19242018-06-04 22:53:36 +0000132
Dan Gohman05ac43f2015-12-17 01:39:00 +0000133} // end anonymous namespace
134
Dan Gohmancceedf72016-01-08 00:43:54 +0000135MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
Derek Schuff72f19242018-06-04 22:53:36 +0000136 return new WebAssemblyAsmBackend(TT.isArch64Bit(), TT.isOSBinFormatELF());
Dan Gohman05ac43f2015-12-17 01:39:00 +0000137}