blob: 80c8db46915166a8f67f7ce88daf75d73e081d0b [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
Dan Gohmand934cb82017-02-24 23:18:00 +000039 unsigned getNumFixupKinds() const override {
40 return WebAssembly::NumTargetFixupKinds;
41 }
42
43 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
44
Derek Schuffd2c9ec72017-06-24 01:00:43 +000045 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
46 const MCValue &Target, MutableArrayRef<char> Data,
Ilya Biryukov3c9c1062018-06-06 10:57:50 +000047 uint64_t Value, bool IsPCRel,
48 const MCSubtargetInfo *STI) const override;
Dan Gohman05ac43f2015-12-17 01:39:00 +000049
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000050 std::unique_ptr<MCObjectTargetWriter>
51 createObjectTargetWriter() const override;
Dan Gohman05ac43f2015-12-17 01:39:00 +000052
53 // No instruction requires relaxation
54 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
55 const MCRelaxableFragment *DF,
56 const MCAsmLayout &Layout) const override {
57 return false;
58 }
59
Ilya Biryukov3c9c1062018-06-06 10:57:50 +000060 bool mayNeedRelaxation(const MCInst &Inst,
61 const MCSubtargetInfo &STI) const override {
62 return false;
63 }
Dan Gohman05ac43f2015-12-17 01:39:00 +000064
Nirav Dave86030622016-07-11 14:23:53 +000065 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
66 MCInst &Res) const override {}
Dan Gohman05ac43f2015-12-17 01:39:00 +000067
Peter Collingbourne571a3302018-05-21 17:57:19 +000068 bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
Dan Gohman05ac43f2015-12-17 01:39:00 +000069};
70
Dan Gohmand934cb82017-02-24 23:18:00 +000071const MCFixupKindInfo &
72WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
73 const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
Heejin Ahnf208f632018-09-05 01:27:38 +000074 // This table *must* be in the order that the fixup_* kinds are defined in
75 // WebAssemblyFixupKinds.h.
76 //
77 // Name Offset (bits) Size (bits) Flags
78 {"fixup_code_sleb128_i32", 0, 5 * 8, 0},
79 {"fixup_code_sleb128_i64", 0, 10 * 8, 0},
80 {"fixup_code_uleb128_i32", 0, 5 * 8, 0},
Dan Gohmand934cb82017-02-24 23:18:00 +000081 };
82
83 if (Kind < FirstTargetFixupKind)
84 return MCAsmBackend::getFixupKindInfo(Kind);
85
86 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
87 "Invalid kind!");
88 return Infos[Kind - FirstTargetFixupKind];
89}
90
Peter Collingbourne571a3302018-05-21 17:57:19 +000091bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS,
92 uint64_t Count) const {
Heejin Ahn18c56a02019-02-04 19:13:39 +000093 for (uint64_t I = 0; I < Count; ++I)
Peter Collingbourne571a3302018-05-21 17:57:19 +000094 OS << char(WebAssembly::Nop);
Dan Gohman3acb1872016-10-24 23:27:49 +000095
96 return true;
Dan Gohman05ac43f2015-12-17 01:39:00 +000097}
98
Derek Schuffd2c9ec72017-06-24 01:00:43 +000099void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
100 const MCFixup &Fixup,
101 const MCValue &Target,
Reid Kleckneref581752017-06-22 01:07:05 +0000102 MutableArrayRef<char> Data,
Ilya Biryukov3c9c1062018-06-06 10:57:50 +0000103 uint64_t Value, bool IsPCRel,
104 const MCSubtargetInfo *STI) const {
Dan Gohman05ac43f2015-12-17 01:39:00 +0000105 const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
Dan Gohmana39ca602016-01-13 19:31:57 +0000106 assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
107
Dan Gohman18eafb62017-02-22 01:23:18 +0000108 unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
Dan Gohman938ff9f2016-01-13 19:29:37 +0000109 if (Value == 0)
Dan Gohman05ac43f2015-12-17 01:39:00 +0000110 return; // Doesn't change encoding.
111
112 // Shift the value into position.
113 Value <<= Info.TargetOffset;
114
115 unsigned Offset = Fixup.getOffset();
Reid Kleckneref581752017-06-22 01:07:05 +0000116 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
Dan Gohman05ac43f2015-12-17 01:39:00 +0000117
118 // For each byte of the fragment that the fixup touches, mask in the
119 // bits from the fixup value.
Heejin Ahn18c56a02019-02-04 19:13:39 +0000120 for (unsigned I = 0; I != NumBytes; ++I)
121 Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000122}
123
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000124std::unique_ptr<MCObjectTargetWriter>
125WebAssemblyAsmBackend::createObjectTargetWriter() const {
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000126 return createWebAssemblyWasmObjectWriter(Is64Bit);
Dan Gohman05ac43f2015-12-17 01:39:00 +0000127}
Derek Schuff72f19242018-06-04 22:53:36 +0000128
Dan Gohman05ac43f2015-12-17 01:39:00 +0000129} // end anonymous namespace
130
Dan Gohmancceedf72016-01-08 00:43:54 +0000131MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000132 return new WebAssemblyAsmBackend(TT.isArch64Bit());
Dan Gohman05ac43f2015-12-17 01:39:00 +0000133}