annotate llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCELFStreamer.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //=== HexagonMCELFStreamer.cpp - Hexagon subclass of MCELFStreamer -------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file is a stub that parses a MCInst bundle and passes the
anatofuz
parents:
diff changeset
10 // instructions on to the real streamer.
anatofuz
parents:
diff changeset
11 //
anatofuz
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 #include "MCTargetDesc/HexagonMCELFStreamer.h"
anatofuz
parents:
diff changeset
15 #include "MCTargetDesc/HexagonMCInstrInfo.h"
anatofuz
parents:
diff changeset
16 #include "MCTargetDesc/HexagonMCShuffler.h"
anatofuz
parents:
diff changeset
17 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
18 #include "llvm/BinaryFormat/ELF.h"
anatofuz
parents:
diff changeset
19 #include "llvm/MC/MCAsmBackend.h"
anatofuz
parents:
diff changeset
20 #include "llvm/MC/MCAssembler.h"
anatofuz
parents:
diff changeset
21 #include "llvm/MC/MCCodeEmitter.h"
anatofuz
parents:
diff changeset
22 #include "llvm/MC/MCContext.h"
anatofuz
parents:
diff changeset
23 #include "llvm/MC/MCExpr.h"
anatofuz
parents:
diff changeset
24 #include "llvm/MC/MCInst.h"
anatofuz
parents:
diff changeset
25 #include "llvm/MC/MCObjectStreamer.h"
anatofuz
parents:
diff changeset
26 #include "llvm/MC/MCObjectWriter.h"
anatofuz
parents:
diff changeset
27 #include "llvm/MC/MCSection.h"
anatofuz
parents:
diff changeset
28 #include "llvm/MC/MCSectionELF.h"
anatofuz
parents:
diff changeset
29 #include "llvm/MC/MCStreamer.h"
anatofuz
parents:
diff changeset
30 #include "llvm/MC/MCSymbol.h"
anatofuz
parents:
diff changeset
31 #include "llvm/MC/MCSymbolELF.h"
anatofuz
parents:
diff changeset
32 #include "llvm/Support/Casting.h"
anatofuz
parents:
diff changeset
33 #include "llvm/Support/CommandLine.h"
anatofuz
parents:
diff changeset
34 #include "llvm/Support/ErrorHandling.h"
anatofuz
parents:
diff changeset
35 #include "llvm/Support/MathExtras.h"
anatofuz
parents:
diff changeset
36 #include <cassert>
anatofuz
parents:
diff changeset
37 #include <cstdint>
anatofuz
parents:
diff changeset
38
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
39 #define DEBUG_TYPE "hexagonmcelfstreamer"
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
40
150
anatofuz
parents:
diff changeset
41 using namespace llvm;
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 static cl::opt<unsigned> GPSize
anatofuz
parents:
diff changeset
44 ("gpsize", cl::NotHidden,
anatofuz
parents:
diff changeset
45 cl::desc("Global Pointer Addressing Size. The default size is 8."),
anatofuz
parents:
diff changeset
46 cl::Prefix,
anatofuz
parents:
diff changeset
47 cl::init(8));
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 HexagonMCELFStreamer::HexagonMCELFStreamer(
anatofuz
parents:
diff changeset
50 MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
anatofuz
parents:
diff changeset
51 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter)
anatofuz
parents:
diff changeset
52 : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
anatofuz
parents:
diff changeset
53 MCII(createHexagonMCInstrInfo()) {}
anatofuz
parents:
diff changeset
54
anatofuz
parents:
diff changeset
55 HexagonMCELFStreamer::HexagonMCELFStreamer(
anatofuz
parents:
diff changeset
56 MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
anatofuz
parents:
diff changeset
57 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
anatofuz
parents:
diff changeset
58 MCAssembler *Assembler)
anatofuz
parents:
diff changeset
59 : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
anatofuz
parents:
diff changeset
60 MCII(createHexagonMCInstrInfo()) {}
anatofuz
parents:
diff changeset
61
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
62 void HexagonMCELFStreamer::emitInstruction(const MCInst &MCB,
150
anatofuz
parents:
diff changeset
63 const MCSubtargetInfo &STI) {
anatofuz
parents:
diff changeset
64 assert(MCB.getOpcode() == Hexagon::BUNDLE);
anatofuz
parents:
diff changeset
65 assert(HexagonMCInstrInfo::bundleSize(MCB) <= HEXAGON_PACKET_SIZE);
anatofuz
parents:
diff changeset
66 assert(HexagonMCInstrInfo::bundleSize(MCB) > 0);
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 // At this point, MCB is a bundle
anatofuz
parents:
diff changeset
69 // Iterate through the bundle and assign addends for the instructions
anatofuz
parents:
diff changeset
70 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
anatofuz
parents:
diff changeset
71 MCInst *MCI = const_cast<MCInst *>(I.getInst());
anatofuz
parents:
diff changeset
72 EmitSymbol(*MCI);
anatofuz
parents:
diff changeset
73 }
anatofuz
parents:
diff changeset
74
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
75 MCObjectStreamer::emitInstruction(MCB, STI);
150
anatofuz
parents:
diff changeset
76 }
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 void HexagonMCELFStreamer::EmitSymbol(const MCInst &Inst) {
anatofuz
parents:
diff changeset
79 // Scan for values.
anatofuz
parents:
diff changeset
80 for (unsigned i = Inst.getNumOperands(); i--;)
anatofuz
parents:
diff changeset
81 if (Inst.getOperand(i).isExpr())
anatofuz
parents:
diff changeset
82 visitUsedExpr(*Inst.getOperand(i).getExpr());
anatofuz
parents:
diff changeset
83 }
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 // EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
anatofuz
parents:
diff changeset
86 // functions found in MCELFStreamer.cpp taking AccessSize as an additional
anatofuz
parents:
diff changeset
87 // parameter.
anatofuz
parents:
diff changeset
88 void HexagonMCELFStreamer::HexagonMCEmitCommonSymbol(MCSymbol *Symbol,
anatofuz
parents:
diff changeset
89 uint64_t Size,
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
90 Align ByteAlignment,
150
anatofuz
parents:
diff changeset
91 unsigned AccessSize) {
anatofuz
parents:
diff changeset
92 getAssembler().registerSymbol(*Symbol);
anatofuz
parents:
diff changeset
93 StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
anatofuz
parents:
diff changeset
94
anatofuz
parents:
diff changeset
95 auto ELFSymbol = cast<MCSymbolELF>(Symbol);
anatofuz
parents:
diff changeset
96 if (!ELFSymbol->isBindingSet()) {
anatofuz
parents:
diff changeset
97 ELFSymbol->setBinding(ELF::STB_GLOBAL);
anatofuz
parents:
diff changeset
98 ELFSymbol->setExternal(true);
anatofuz
parents:
diff changeset
99 }
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 ELFSymbol->setType(ELF::STT_OBJECT);
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
anatofuz
parents:
diff changeset
104 StringRef SectionName =
anatofuz
parents:
diff changeset
105 ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
anatofuz
parents:
diff changeset
106 ? ".bss"
anatofuz
parents:
diff changeset
107 : sbss[(Log2_64(AccessSize))];
anatofuz
parents:
diff changeset
108 MCSection &Section = *getAssembler().getContext().getELFSection(
anatofuz
parents:
diff changeset
109 SectionName, ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
anatofuz
parents:
diff changeset
110 MCSectionSubPair P = getCurrentSection();
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
111 switchSection(&Section);
150
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 if (ELFSymbol->isUndefined()) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
114 emitValueToAlignment(ByteAlignment, 0, 1, 0);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
115 emitLabel(Symbol);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
116 emitZeros(Size);
150
anatofuz
parents:
diff changeset
117 }
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 // Update the maximum alignment of the section if necessary.
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
120 Section.ensureMinAlignment(ByteAlignment);
150
anatofuz
parents:
diff changeset
121
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
122 switchSection(P.first, P.second);
150
anatofuz
parents:
diff changeset
123 } else {
anatofuz
parents:
diff changeset
124 if (ELFSymbol->declareCommon(Size, ByteAlignment))
anatofuz
parents:
diff changeset
125 report_fatal_error("Symbol: " + Symbol->getName() +
anatofuz
parents:
diff changeset
126 " redeclared as different type");
anatofuz
parents:
diff changeset
127 if ((AccessSize) && (Size <= GPSize)) {
anatofuz
parents:
diff changeset
128 uint64_t SectionIndex =
anatofuz
parents:
diff changeset
129 (AccessSize <= GPSize)
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
130 ? ELF::SHN_HEXAGON_SCOMMON + llvm::bit_width(AccessSize)
150
anatofuz
parents:
diff changeset
131 : (unsigned)ELF::SHN_HEXAGON_SCOMMON;
anatofuz
parents:
diff changeset
132 ELFSymbol->setIndex(SectionIndex);
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134 }
anatofuz
parents:
diff changeset
135
anatofuz
parents:
diff changeset
136 ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
anatofuz
parents:
diff changeset
137 }
anatofuz
parents:
diff changeset
138
anatofuz
parents:
diff changeset
139 void HexagonMCELFStreamer::HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol,
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
140 uint64_t Size,
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
141 Align ByteAlignment,
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
142 unsigned AccessSize) {
150
anatofuz
parents:
diff changeset
143 getAssembler().registerSymbol(*Symbol);
anatofuz
parents:
diff changeset
144 auto ELFSymbol = cast<MCSymbolELF>(Symbol);
anatofuz
parents:
diff changeset
145 ELFSymbol->setBinding(ELF::STB_LOCAL);
anatofuz
parents:
diff changeset
146 ELFSymbol->setExternal(false);
anatofuz
parents:
diff changeset
147 HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
anatofuz
parents:
diff changeset
148 }
anatofuz
parents:
diff changeset
149
anatofuz
parents:
diff changeset
150 namespace llvm {
anatofuz
parents:
diff changeset
151 MCStreamer *createHexagonELFStreamer(Triple const &TT, MCContext &Context,
anatofuz
parents:
diff changeset
152 std::unique_ptr<MCAsmBackend> MAB,
anatofuz
parents:
diff changeset
153 std::unique_ptr<MCObjectWriter> OW,
anatofuz
parents:
diff changeset
154 std::unique_ptr<MCCodeEmitter> CE) {
anatofuz
parents:
diff changeset
155 return new HexagonMCELFStreamer(Context, std::move(MAB), std::move(OW),
anatofuz
parents:
diff changeset
156 std::move(CE));
anatofuz
parents:
diff changeset
157 }
anatofuz
parents:
diff changeset
158
anatofuz
parents:
diff changeset
159 } // end namespace llvm