1 //===-- LoongArchELFStreamer.cpp - LoongArch ELF Target Streamer Methods --===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides LoongArch specific target streamer methods.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "LoongArchELFStreamer.h"
14 #include "LoongArchAsmBackend.h"
15 #include "llvm/BinaryFormat/ELF.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCObjectWriter.h"
19
20 using namespace llvm;
21
22 // This part is for ELF object output.
LoongArchTargetELFStreamer(MCStreamer & S,const MCSubtargetInfo & STI)23 LoongArchTargetELFStreamer::LoongArchTargetELFStreamer(
24 MCStreamer &S, const MCSubtargetInfo &STI)
25 : LoongArchTargetStreamer(S) {
26 // FIXME: select appropriate ABI.
27 setTargetABI(STI.getTargetTriple().isArch64Bit() ? LoongArchABI::ABI_LP64D
28 : LoongArchABI::ABI_ILP32D);
29 }
30
getStreamer()31 MCELFStreamer &LoongArchTargetELFStreamer::getStreamer() {
32 return static_cast<MCELFStreamer &>(Streamer);
33 }
34
finish()35 void LoongArchTargetELFStreamer::finish() {
36 LoongArchTargetStreamer::finish();
37 MCAssembler &MCA = getStreamer().getAssembler();
38 LoongArchABI::ABI ABI = getTargetABI();
39
40 // Figure out the e_flags.
41 //
42 // Bitness is already represented with the EI_CLASS byte in the current spec,
43 // so here we only record the base ABI modifier. Also set the object file ABI
44 // version to v1, as upstream LLVM cannot handle the previous stack-machine-
45 // based relocs from day one.
46 //
47 // Refer to LoongArch ELF psABI v2.01 for details.
48 unsigned EFlags = MCA.getELFHeaderEFlags();
49 EFlags |= ELF::EF_LOONGARCH_OBJABI_V1;
50 switch (ABI) {
51 case LoongArchABI::ABI_ILP32S:
52 case LoongArchABI::ABI_LP64S:
53 EFlags |= ELF::EF_LOONGARCH_ABI_SOFT_FLOAT;
54 break;
55 case LoongArchABI::ABI_ILP32F:
56 case LoongArchABI::ABI_LP64F:
57 EFlags |= ELF::EF_LOONGARCH_ABI_SINGLE_FLOAT;
58 break;
59 case LoongArchABI::ABI_ILP32D:
60 case LoongArchABI::ABI_LP64D:
61 EFlags |= ELF::EF_LOONGARCH_ABI_DOUBLE_FLOAT;
62 break;
63 case LoongArchABI::ABI_Unknown:
64 llvm_unreachable("Improperly initialized target ABI");
65 }
66 MCA.setELFHeaderEFlags(EFlags);
67 }
68
69 namespace {
70 class LoongArchELFStreamer : public MCELFStreamer {
71 public:
LoongArchELFStreamer(MCContext & C,std::unique_ptr<MCAsmBackend> MAB,std::unique_ptr<MCObjectWriter> MOW,std::unique_ptr<MCCodeEmitter> MCE)72 LoongArchELFStreamer(MCContext &C, std::unique_ptr<MCAsmBackend> MAB,
73 std::unique_ptr<MCObjectWriter> MOW,
74 std::unique_ptr<MCCodeEmitter> MCE)
75 : MCELFStreamer(C, std::move(MAB), std::move(MOW), std::move(MCE)) {}
76 };
77 } // end namespace
78
79 namespace llvm {
createLoongArchELFStreamer(MCContext & C,std::unique_ptr<MCAsmBackend> MAB,std::unique_ptr<MCObjectWriter> MOW,std::unique_ptr<MCCodeEmitter> MCE,bool RelaxAll)80 MCELFStreamer *createLoongArchELFStreamer(MCContext &C,
81 std::unique_ptr<MCAsmBackend> MAB,
82 std::unique_ptr<MCObjectWriter> MOW,
83 std::unique_ptr<MCCodeEmitter> MCE,
84 bool RelaxAll) {
85 LoongArchELFStreamer *S = new LoongArchELFStreamer(
86 C, std::move(MAB), std::move(MOW), std::move(MCE));
87 S->getAssembler().setRelaxAll(RelaxAll);
88 return S;
89 }
90 } // end namespace llvm
91