blob: 890524dd452602db22fc3e793ef533c5b5a77573 [file] [log] [blame]
Dan Gohman05ac43f2015-12-17 01:39:00 +00001//===-- WebAssemblyAsmBackend.cpp - WebAssembly Assembler Backend ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman05ac43f2015-12-17 01:39:00 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file implements the WebAssemblyAsmBackend class.
Dan Gohman05ac43f2015-12-17 01:39:00 +000011///
12//===----------------------------------------------------------------------===//
13
Dan Gohmand934cb82017-02-24 23:18:00 +000014#include "MCTargetDesc/WebAssemblyFixupKinds.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000016#include "llvm/MC/MCAsmBackend.h"
17#include "llvm/MC/MCAssembler.h"
18#include "llvm/MC/MCDirectives.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000019#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCFixupKindInfo.h"
21#include "llvm/MC/MCObjectWriter.h"
22#include "llvm/MC/MCSubtargetInfo.h"
23#include "llvm/MC/MCSymbol.h"
Dan Gohmand37dc2f2017-02-27 22:41:39 +000024#include "llvm/MC/MCWasmObjectWriter.h"
Dan Gohman05ac43f2015-12-17 01:39:00 +000025#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
Sam Cleggcf2a9e22018-07-16 23:09:29 +000027
Dan Gohman05ac43f2015-12-17 01:39:00 +000028using namespace llvm;
29
30namespace {
Dan Gohman18eafb62017-02-22 01:23:18 +000031
Dan Gohman05ac43f2015-12-17 01:39:00 +000032class WebAssemblyAsmBackend final : public MCAsmBackend {
33 bool Is64Bit;
34
Sam Cleggcf2a9e22018-07-16 23:09:29 +000035public:
36 explicit WebAssemblyAsmBackend(bool Is64Bit)
37 : MCAsmBackend(support::little), Is64Bit(Is64Bit) {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000038 ~WebAssemblyAsmBackend() override {}
39
Dan Gohmand934cb82017-02-24 23:18:00 +000040 unsigned getNumFixupKinds() const override {
41 return WebAssembly::NumTargetFixupKinds;
42 }
43
44 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
45
Derek Schuffd2c9ec72017-06-24 01:00:43 +000046 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
47 const MCValue &Target, MutableArrayRef<char> Data,
Ilya Biryukov3c9c1062018-06-06 10:57:50 +000048 uint64_t Value, bool IsPCRel,
49 const MCSubtargetInfo *STI) const override;
Dan Gohman05ac43f2015-12-17 01:39:00 +000050
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000051 std::unique_ptr<MCObjectTargetWriter>
52 createObjectTargetWriter() const override;
Dan Gohman05ac43f2015-12-17 01:39:00 +000053
54 // No instruction requires relaxation
55 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
56 const MCRelaxableFragment *DF,
57 const MCAsmLayout &Layout) const override {
58 return false;
59 }
60
Ilya Biryukov3c9c1062018-06-06 10:57:50 +000061 bool mayNeedRelaxation(const MCInst &Inst,
62 const MCSubtargetInfo &STI) const override {
63 return false;
64 }
Dan Gohman05ac43f2015-12-17 01:39:00 +000065
Nirav Dave86030622016-07-11 14:23:53 +000066 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
67 MCInst &Res) const override {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000068
Peter Collingbourne571a3302018-05-21 17:57:19 +000069 bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
Dan Gohman05ac43f2015-12-17 01:39:00 +000070};
71
Dan Gohmand934cb82017-02-24 23:18:00 +000072const MCFixupKindInfo &
73WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
74 const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
Heejin Ahnf208f632018-09-05 01:27:38 +000075 // This table *must* be in the order that the fixup_* kinds are defined in
76 // WebAssemblyFixupKinds.h.
77 //
78 // Name Offset (bits) Size (bits) Flags
79 {"fixup_code_sleb128_i32", 0, 5 * 8, 0},
80 {"fixup_code_sleb128_i64", 0, 10 * 8, 0},
81 {"fixup_code_uleb128_i32", 0, 5 * 8, 0},
Dan Gohmand934cb82017-02-24 23:18:00 +000082 };
83
84 if (Kind < FirstTargetFixupKind)
85 return MCAsmBackend::getFixupKindInfo(Kind);
86
87 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
88 "Invalid kind!");
89 return Infos[Kind - FirstTargetFixupKind];
90}
91
Peter Collingbourne571a3302018-05-21 17:57:19 +000092bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS,
93 uint64_t Count) const {
Dan Gohman3acb1872016-10-24 23:27:49 +000094 for (uint64_t i = 0; i < Count; ++i)
Peter Collingbourne571a3302018-05-21 17:57:19 +000095 OS << char(WebAssembly::Nop);
Dan Gohman3acb1872016-10-24 23:27:49 +000096
97 return true;
Dan Gohman05ac43f2015-12-17 01:39:00 +000098}
99
Derek Schuffd2c9ec72017-06-24 01:00:43 +0000100void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
101 const MCFixup &Fixup,
102 const MCValue &Target,
Reid Kleckneref581752017-06-22 01:07:05 +0000103 MutableArrayRef<char> Data,
Ilya Biryukov3c9c1062018-06-06 10:57:50 +0000104 uint64_t Value, bool IsPCRel,
105 const MCSubtargetInfo *STI) const {
Dan Gohman05ac43f2015-12-17 01:39:00 +0000106 const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
Dan Gohmana39ca602016-01-13 19:31:57 +0000107 assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
108
Dan Gohman18eafb62017-02-22 01:23:18 +0000109 unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
Dan Gohman938ff9f2016-01-13 19:29:37 +0000110 if (Value == 0)
Dan Gohman05ac43f2015-12-17 01:39:00 +0000111 return; // Doesn't change encoding.
112
113 // Shift the value into position.
114 Value <<= Info.TargetOffset;
115
116 unsigned Offset = Fixup.getOffset();
Reid Kleckneref581752017-06-22 01:07:05 +0000117 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
Dan Gohman05ac43f2015-12-17 01:39:00 +0000118
119 // For each byte of the fragment that the fixup touches, mask in the
120 // bits from the fixup value.
121 for (unsigned i = 0; i != NumBytes; ++i)
122 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
123}
124
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000125std::unique_ptr<MCObjectTargetWriter>
126WebAssemblyAsmBackend::createObjectTargetWriter() const {
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000127 return createWebAssemblyWasmObjectWriter(Is64Bit);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000128}
Derek Schuff72f19242018-06-04 22:53:36 +0000129
Dan Gohman05ac43f2015-12-17 01:39:00 +0000130} // end anonymous namespace
131
Dan Gohmancceedf72016-01-08 00:43:54 +0000132MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000133 return new WebAssemblyAsmBackend(TT.isArch64Bit());
Dan Gohman05ac43f2015-12-17 01:39:00 +0000134}