1 //===-- RISCVAsmBackend.cpp - RISCV Assembler Backend ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/RISCVMCTargetDesc.h"
11 #include "llvm/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCDirectives.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFixupKindInfo.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSubtargetInfo.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
26 class RISCVAsmBackend : public MCAsmBackend {
31 RISCVAsmBackend(uint8_t OSABI, bool Is64Bit)
32 : MCAsmBackend(), OSABI(OSABI), Is64Bit(Is64Bit) {}
33 ~RISCVAsmBackend() override {}
35 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
36 const MCValue &Target, MutableArrayRef<char> Data,
37 uint64_t Value, bool IsPCRel) const override;
39 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
41 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
42 const MCRelaxableFragment *DF,
43 const MCAsmLayout &Layout) const override {
47 unsigned getNumFixupKinds() const override { return 1; }
49 bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
51 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
52 MCInst &Res) const override {
54 llvm_unreachable("RISCVAsmBackend::relaxInstruction() unimplemented");
57 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
60 bool RISCVAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
61 // Once support for the compressed instruction set is added, we will be able
62 // to conditionally support 16-bit NOPs
66 // The canonical nop on RISC-V is addi x0, x0, 0
67 for (uint64_t i = 0; i < Count; i += 4)
73 void RISCVAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
74 const MCValue &Target,
75 MutableArrayRef<char> Data, uint64_t Value,
81 RISCVAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
82 return createRISCVELFObjectWriter(OS, OSABI, Is64Bit);
85 } // end anonymous namespace
87 MCAsmBackend *llvm::createRISCVAsmBackend(const Target &T,
88 const MCRegisterInfo &MRI,
89 const Triple &TT, StringRef CPU,
90 const MCTargetOptions &Options) {
91 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
92 return new RISCVAsmBackend(OSABI, TT.isArch64Bit());