Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyAsmBackend.cpp - WebAssembly Assembler Backend ---------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // 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 Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file implements the WebAssemblyAsmBackend class. |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 14 | #include "MCTargetDesc/WebAssemblyFixupKinds.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAsmBackend.h" |
| 17 | #include "llvm/MC/MCAssembler.h" |
| 18 | #include "llvm/MC/MCDirectives.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 19 | #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 Gohman | d37dc2f | 2017-02-27 22:41:39 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCWasmObjectWriter.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 27 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 31 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 32 | class WebAssemblyAsmBackend final : public MCAsmBackend { |
| 33 | bool Is64Bit; |
| 34 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 35 | public: |
| 36 | explicit WebAssemblyAsmBackend(bool Is64Bit) |
| 37 | : MCAsmBackend(support::little), Is64Bit(Is64Bit) {} |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 38 | ~WebAssemblyAsmBackend() override {} |
| 39 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 40 | unsigned getNumFixupKinds() const override { |
| 41 | return WebAssembly::NumTargetFixupKinds; |
| 42 | } |
| 43 | |
| 44 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override; |
| 45 | |
Derek Schuff | d2c9ec7 | 2017-06-24 01:00:43 +0000 | [diff] [blame] | 46 | void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, |
| 47 | const MCValue &Target, MutableArrayRef<char> Data, |
Ilya Biryukov | 3c9c106 | 2018-06-06 10:57:50 +0000 | [diff] [blame] | 48 | uint64_t Value, bool IsPCRel, |
| 49 | const MCSubtargetInfo *STI) const override; |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 50 | |
Peter Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 51 | std::unique_ptr<MCObjectTargetWriter> |
| 52 | createObjectTargetWriter() const override; |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 53 | |
| 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 Biryukov | 3c9c106 | 2018-06-06 10:57:50 +0000 | [diff] [blame] | 61 | bool mayNeedRelaxation(const MCInst &Inst, |
| 62 | const MCSubtargetInfo &STI) const override { |
| 63 | return false; |
| 64 | } |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 65 | |
Nirav Dave | 8603062 | 2016-07-11 14:23:53 +0000 | [diff] [blame] | 66 | void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, |
| 67 | MCInst &Res) const override {} |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 68 | |
Peter Collingbourne | 571a330 | 2018-05-21 17:57:19 +0000 | [diff] [blame] | 69 | bool writeNopData(raw_ostream &OS, uint64_t Count) const override; |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 72 | const MCFixupKindInfo & |
| 73 | WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { |
| 74 | const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 75 | // 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 Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 82 | }; |
| 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 Collingbourne | 571a330 | 2018-05-21 17:57:19 +0000 | [diff] [blame] | 92 | bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS, |
| 93 | uint64_t Count) const { |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 94 | for (uint64_t i = 0; i < Count; ++i) |
Peter Collingbourne | 571a330 | 2018-05-21 17:57:19 +0000 | [diff] [blame] | 95 | OS << char(WebAssembly::Nop); |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 96 | |
| 97 | return true; |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Derek Schuff | d2c9ec7 | 2017-06-24 01:00:43 +0000 | [diff] [blame] | 100 | void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm, |
| 101 | const MCFixup &Fixup, |
| 102 | const MCValue &Target, |
Reid Kleckner | ef58175 | 2017-06-22 01:07:05 +0000 | [diff] [blame] | 103 | MutableArrayRef<char> Data, |
Ilya Biryukov | 3c9c106 | 2018-06-06 10:57:50 +0000 | [diff] [blame] | 104 | uint64_t Value, bool IsPCRel, |
| 105 | const MCSubtargetInfo *STI) const { |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 106 | const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind()); |
Dan Gohman | a39ca60 | 2016-01-13 19:31:57 +0000 | [diff] [blame] | 107 | assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags"); |
| 108 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 109 | unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8; |
Dan Gohman | 938ff9f | 2016-01-13 19:29:37 +0000 | [diff] [blame] | 110 | if (Value == 0) |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 111 | return; // Doesn't change encoding. |
| 112 | |
| 113 | // Shift the value into position. |
| 114 | Value <<= Info.TargetOffset; |
| 115 | |
| 116 | unsigned Offset = Fixup.getOffset(); |
Reid Kleckner | ef58175 | 2017-06-22 01:07:05 +0000 | [diff] [blame] | 117 | assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!"); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 118 | |
| 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 Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 125 | std::unique_ptr<MCObjectTargetWriter> |
| 126 | WebAssemblyAsmBackend::createObjectTargetWriter() const { |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 127 | return createWebAssemblyWasmObjectWriter(Is64Bit); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 128 | } |
Derek Schuff | 72f1924 | 2018-06-04 22:53:36 +0000 | [diff] [blame] | 129 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 130 | } // end anonymous namespace |
| 131 | |
Dan Gohman | cceedf7 | 2016-01-08 00:43:54 +0000 | [diff] [blame] | 132 | MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) { |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 133 | return new WebAssemblyAsmBackend(TT.isArch64Bit()); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 134 | } |