1f4a2713aSLionel Sambuc //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the AsmPrinter class.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "llvm/CodeGen/AsmPrinter.h"
15f4a2713aSLionel Sambuc #include "DwarfDebug.h"
16f4a2713aSLionel Sambuc #include "DwarfException.h"
17*0a6a1f1dSLionel Sambuc #include "Win64Exception.h"
18*0a6a1f1dSLionel Sambuc #include "WinCodeViewLineTables.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
20f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
21f4a2713aSLionel Sambuc #include "llvm/Analysis/ConstantFolding.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Analysis/JumpInstrTableInfo.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/Analysis.h"
24f4a2713aSLionel Sambuc #include "llvm/CodeGen/GCMetadataPrinter.h"
25f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineConstantPool.h"
26f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFrameInfo.h"
27f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunction.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/CodeGen/MachineInstrBundle.h"
29f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineJumpTableInfo.h"
30f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineLoopInfo.h"
31f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineModuleInfo.h"
32f4a2713aSLionel Sambuc #include "llvm/IR/DataLayout.h"
33*0a6a1f1dSLionel Sambuc #include "llvm/IR/DebugInfo.h"
34*0a6a1f1dSLionel Sambuc #include "llvm/IR/Mangler.h"
35f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
36f4a2713aSLionel Sambuc #include "llvm/IR/Operator.h"
37f4a2713aSLionel Sambuc #include "llvm/MC/MCAsmInfo.h"
38f4a2713aSLionel Sambuc #include "llvm/MC/MCContext.h"
39f4a2713aSLionel Sambuc #include "llvm/MC/MCExpr.h"
40f4a2713aSLionel Sambuc #include "llvm/MC/MCInst.h"
41f4a2713aSLionel Sambuc #include "llvm/MC/MCSection.h"
42f4a2713aSLionel Sambuc #include "llvm/MC/MCStreamer.h"
43f4a2713aSLionel Sambuc #include "llvm/MC/MCSymbol.h"
44f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
45f4a2713aSLionel Sambuc #include "llvm/Support/Format.h"
46f4a2713aSLionel Sambuc #include "llvm/Support/MathExtras.h"
47f4a2713aSLionel Sambuc #include "llvm/Support/Timer.h"
48f4a2713aSLionel Sambuc #include "llvm/Target/TargetFrameLowering.h"
49f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
50f4a2713aSLionel Sambuc #include "llvm/Target/TargetLowering.h"
51f4a2713aSLionel Sambuc #include "llvm/Target/TargetLoweringObjectFile.h"
52f4a2713aSLionel Sambuc #include "llvm/Target/TargetRegisterInfo.h"
53*0a6a1f1dSLionel Sambuc #include "llvm/Target/TargetSubtargetInfo.h"
54f4a2713aSLionel Sambuc using namespace llvm;
55f4a2713aSLionel Sambuc
56*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "asm-printer"
57*0a6a1f1dSLionel Sambuc
58f4a2713aSLionel Sambuc static const char *const DWARFGroupName = "DWARF Emission";
59*0a6a1f1dSLionel Sambuc static const char *const DbgTimerName = "Debug Info Emission";
60f4a2713aSLionel Sambuc static const char *const EHTimerName = "DWARF Exception Writer";
61*0a6a1f1dSLionel Sambuc static const char *const CodeViewLineTablesGroupName = "CodeView Line Tables";
62f4a2713aSLionel Sambuc
63f4a2713aSLionel Sambuc STATISTIC(EmittedInsts, "Number of machine instrs printed");
64f4a2713aSLionel Sambuc
65f4a2713aSLionel Sambuc char AsmPrinter::ID = 0;
66f4a2713aSLionel Sambuc
67*0a6a1f1dSLionel Sambuc typedef DenseMap<GCStrategy*, std::unique_ptr<GCMetadataPrinter>> gcp_map_type;
getGCMap(void * & P)68f4a2713aSLionel Sambuc static gcp_map_type &getGCMap(void *&P) {
69*0a6a1f1dSLionel Sambuc if (!P)
70f4a2713aSLionel Sambuc P = new gcp_map_type();
71f4a2713aSLionel Sambuc return *(gcp_map_type*)P;
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc
75f4a2713aSLionel Sambuc /// getGVAlignmentLog2 - Return the alignment to use for the specified global
76f4a2713aSLionel Sambuc /// value in log2 form. This rounds up to the preferred alignment if possible
77f4a2713aSLionel Sambuc /// and legal.
getGVAlignmentLog2(const GlobalValue * GV,const DataLayout & TD,unsigned InBits=0)78f4a2713aSLionel Sambuc static unsigned getGVAlignmentLog2(const GlobalValue *GV, const DataLayout &TD,
79f4a2713aSLionel Sambuc unsigned InBits = 0) {
80f4a2713aSLionel Sambuc unsigned NumBits = 0;
81f4a2713aSLionel Sambuc if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
82f4a2713aSLionel Sambuc NumBits = TD.getPreferredAlignmentLog(GVar);
83f4a2713aSLionel Sambuc
84f4a2713aSLionel Sambuc // If InBits is specified, round it to it.
85f4a2713aSLionel Sambuc if (InBits > NumBits)
86f4a2713aSLionel Sambuc NumBits = InBits;
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc // If the GV has a specified alignment, take it into account.
89f4a2713aSLionel Sambuc if (GV->getAlignment() == 0)
90f4a2713aSLionel Sambuc return NumBits;
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc unsigned GVAlign = Log2_32(GV->getAlignment());
93f4a2713aSLionel Sambuc
94f4a2713aSLionel Sambuc // If the GVAlign is larger than NumBits, or if we are required to obey
95f4a2713aSLionel Sambuc // NumBits because the GV has an assigned section, obey it.
96f4a2713aSLionel Sambuc if (GVAlign > NumBits || GV->hasSection())
97f4a2713aSLionel Sambuc NumBits = GVAlign;
98f4a2713aSLionel Sambuc return NumBits;
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc
AsmPrinter(TargetMachine & tm,MCStreamer & Streamer)101f4a2713aSLionel Sambuc AsmPrinter::AsmPrinter(TargetMachine &tm, MCStreamer &Streamer)
102*0a6a1f1dSLionel Sambuc : MachineFunctionPass(ID), TM(tm), MAI(tm.getMCAsmInfo()),
103*0a6a1f1dSLionel Sambuc MII(tm.getSubtargetImpl()->getInstrInfo()),
104*0a6a1f1dSLionel Sambuc OutContext(Streamer.getContext()), OutStreamer(Streamer), LastMI(nullptr),
105*0a6a1f1dSLionel Sambuc LastFn(0), Counter(~0U), SetCounter(0) {
106*0a6a1f1dSLionel Sambuc DD = nullptr; MMI = nullptr; LI = nullptr; MF = nullptr;
107*0a6a1f1dSLionel Sambuc CurrentFnSym = CurrentFnSymForSize = nullptr;
108*0a6a1f1dSLionel Sambuc GCMetadataPrinters = nullptr;
109f4a2713aSLionel Sambuc VerboseAsm = Streamer.isVerboseAsm();
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc
~AsmPrinter()112f4a2713aSLionel Sambuc AsmPrinter::~AsmPrinter() {
113*0a6a1f1dSLionel Sambuc assert(!DD && Handlers.empty() && "Debug/EH info didn't get finalized");
114f4a2713aSLionel Sambuc
115*0a6a1f1dSLionel Sambuc if (GCMetadataPrinters) {
116f4a2713aSLionel Sambuc gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
117f4a2713aSLionel Sambuc
118f4a2713aSLionel Sambuc delete &GCMap;
119*0a6a1f1dSLionel Sambuc GCMetadataPrinters = nullptr;
120f4a2713aSLionel Sambuc }
121f4a2713aSLionel Sambuc
122f4a2713aSLionel Sambuc delete &OutStreamer;
123f4a2713aSLionel Sambuc }
124f4a2713aSLionel Sambuc
125f4a2713aSLionel Sambuc /// getFunctionNumber - Return a unique ID for the current function.
126f4a2713aSLionel Sambuc ///
getFunctionNumber() const127f4a2713aSLionel Sambuc unsigned AsmPrinter::getFunctionNumber() const {
128f4a2713aSLionel Sambuc return MF->getFunctionNumber();
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc
getObjFileLowering() const131f4a2713aSLionel Sambuc const TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
132*0a6a1f1dSLionel Sambuc return TM.getSubtargetImpl()->getTargetLowering()->getObjFileLowering();
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc
135f4a2713aSLionel Sambuc /// getDataLayout - Return information about data layout.
getDataLayout() const136f4a2713aSLionel Sambuc const DataLayout &AsmPrinter::getDataLayout() const {
137*0a6a1f1dSLionel Sambuc return *TM.getSubtargetImpl()->getDataLayout();
138*0a6a1f1dSLionel Sambuc }
139*0a6a1f1dSLionel Sambuc
getSubtargetInfo() const140*0a6a1f1dSLionel Sambuc const MCSubtargetInfo &AsmPrinter::getSubtargetInfo() const {
141*0a6a1f1dSLionel Sambuc return TM.getSubtarget<MCSubtargetInfo>();
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc
EmitToStreamer(MCStreamer & S,const MCInst & Inst)144*0a6a1f1dSLionel Sambuc void AsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst) {
145*0a6a1f1dSLionel Sambuc S.EmitInstruction(Inst, getSubtargetInfo());
146f4a2713aSLionel Sambuc }
147f4a2713aSLionel Sambuc
getTargetTriple() const148f4a2713aSLionel Sambuc StringRef AsmPrinter::getTargetTriple() const {
149f4a2713aSLionel Sambuc return TM.getTargetTriple();
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc
152f4a2713aSLionel Sambuc /// getCurrentSection() - Return the current section we are emitting to.
getCurrentSection() const153f4a2713aSLionel Sambuc const MCSection *AsmPrinter::getCurrentSection() const {
154f4a2713aSLionel Sambuc return OutStreamer.getCurrentSection().first;
155f4a2713aSLionel Sambuc }
156f4a2713aSLionel Sambuc
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc
getAnalysisUsage(AnalysisUsage & AU) const159f4a2713aSLionel Sambuc void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
160f4a2713aSLionel Sambuc AU.setPreservesAll();
161f4a2713aSLionel Sambuc MachineFunctionPass::getAnalysisUsage(AU);
162f4a2713aSLionel Sambuc AU.addRequired<MachineModuleInfo>();
163f4a2713aSLionel Sambuc AU.addRequired<GCModuleInfo>();
164f4a2713aSLionel Sambuc if (isVerbose())
165f4a2713aSLionel Sambuc AU.addRequired<MachineLoopInfo>();
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc
doInitialization(Module & M)168f4a2713aSLionel Sambuc bool AsmPrinter::doInitialization(Module &M) {
169f4a2713aSLionel Sambuc MMI = getAnalysisIfAvailable<MachineModuleInfo>();
170f4a2713aSLionel Sambuc MMI->AnalyzeModule(M);
171f4a2713aSLionel Sambuc
172f4a2713aSLionel Sambuc // Initialize TargetLoweringObjectFile.
173f4a2713aSLionel Sambuc const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
174f4a2713aSLionel Sambuc .Initialize(OutContext, TM);
175f4a2713aSLionel Sambuc
176*0a6a1f1dSLionel Sambuc OutStreamer.InitSections(false);
177f4a2713aSLionel Sambuc
178*0a6a1f1dSLionel Sambuc Mang = new Mangler(TM.getSubtargetImpl()->getDataLayout());
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc // Emit the version-min deplyment target directive if needed.
181*0a6a1f1dSLionel Sambuc //
182*0a6a1f1dSLionel Sambuc // FIXME: If we end up with a collection of these sorts of Darwin-specific
183*0a6a1f1dSLionel Sambuc // or ELF-specific things, it may make sense to have a platform helper class
184*0a6a1f1dSLionel Sambuc // that will work with the target helper class. For now keep it here, as the
185*0a6a1f1dSLionel Sambuc // alternative is duplicated code in each of the target asm printers that
186*0a6a1f1dSLionel Sambuc // use the directive, where it would need the same conditionalization
187*0a6a1f1dSLionel Sambuc // anyway.
188*0a6a1f1dSLionel Sambuc Triple TT(getTargetTriple());
189*0a6a1f1dSLionel Sambuc if (TT.isOSDarwin()) {
190*0a6a1f1dSLionel Sambuc unsigned Major, Minor, Update;
191*0a6a1f1dSLionel Sambuc TT.getOSVersion(Major, Minor, Update);
192*0a6a1f1dSLionel Sambuc // If there is a version specified, Major will be non-zero.
193*0a6a1f1dSLionel Sambuc if (Major)
194*0a6a1f1dSLionel Sambuc OutStreamer.EmitVersionMin((TT.isMacOSX() ?
195*0a6a1f1dSLionel Sambuc MCVM_OSXVersionMin : MCVM_IOSVersionMin),
196*0a6a1f1dSLionel Sambuc Major, Minor, Update);
197*0a6a1f1dSLionel Sambuc }
198f4a2713aSLionel Sambuc
199f4a2713aSLionel Sambuc // Allow the target to emit any magic that it wants at the start of the file.
200f4a2713aSLionel Sambuc EmitStartOfAsmFile(M);
201f4a2713aSLionel Sambuc
202f4a2713aSLionel Sambuc // Very minimal debug info. It is ignored if we emit actual debug info. If we
203f4a2713aSLionel Sambuc // don't, this at least helps the user find where a global came from.
204f4a2713aSLionel Sambuc if (MAI->hasSingleParameterDotFile()) {
205f4a2713aSLionel Sambuc // .file "foo.c"
206f4a2713aSLionel Sambuc OutStreamer.EmitFileDirective(M.getModuleIdentifier());
207f4a2713aSLionel Sambuc }
208f4a2713aSLionel Sambuc
209f4a2713aSLionel Sambuc GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
210f4a2713aSLionel Sambuc assert(MI && "AsmPrinter didn't require GCModuleInfo?");
211*0a6a1f1dSLionel Sambuc for (auto &I : *MI)
212f4a2713aSLionel Sambuc if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
213*0a6a1f1dSLionel Sambuc MP->beginAssembly(M, *MI, *this);
214f4a2713aSLionel Sambuc
215f4a2713aSLionel Sambuc // Emit module-level inline asm if it exists.
216f4a2713aSLionel Sambuc if (!M.getModuleInlineAsm().empty()) {
217f4a2713aSLionel Sambuc OutStreamer.AddComment("Start of file scope inline assembly");
218f4a2713aSLionel Sambuc OutStreamer.AddBlankLine();
219f4a2713aSLionel Sambuc EmitInlineAsm(M.getModuleInlineAsm()+"\n");
220f4a2713aSLionel Sambuc OutStreamer.AddComment("End of file scope inline assembly");
221f4a2713aSLionel Sambuc OutStreamer.AddBlankLine();
222f4a2713aSLionel Sambuc }
223f4a2713aSLionel Sambuc
224*0a6a1f1dSLionel Sambuc if (MAI->doesSupportDebugInformation()) {
225*0a6a1f1dSLionel Sambuc bool skip_dwarf = false;
226*0a6a1f1dSLionel Sambuc if (Triple(TM.getTargetTriple()).isKnownWindowsMSVCEnvironment()) {
227*0a6a1f1dSLionel Sambuc Handlers.push_back(HandlerInfo(new WinCodeViewLineTables(this),
228*0a6a1f1dSLionel Sambuc DbgTimerName,
229*0a6a1f1dSLionel Sambuc CodeViewLineTablesGroupName));
230*0a6a1f1dSLionel Sambuc // FIXME: Don't emit DWARF debug info if there's at least one function
231*0a6a1f1dSLionel Sambuc // with AddressSanitizer instrumentation.
232*0a6a1f1dSLionel Sambuc // This is a band-aid fix for PR22032.
233*0a6a1f1dSLionel Sambuc for (auto &F : M.functions()) {
234*0a6a1f1dSLionel Sambuc if (F.hasFnAttribute(Attribute::SanitizeAddress)) {
235*0a6a1f1dSLionel Sambuc skip_dwarf = true;
236*0a6a1f1dSLionel Sambuc break;
237*0a6a1f1dSLionel Sambuc }
238*0a6a1f1dSLionel Sambuc }
239*0a6a1f1dSLionel Sambuc }
240*0a6a1f1dSLionel Sambuc if (!skip_dwarf) {
241f4a2713aSLionel Sambuc DD = new DwarfDebug(this, &M);
242*0a6a1f1dSLionel Sambuc Handlers.push_back(HandlerInfo(DD, DbgTimerName, DWARFGroupName));
243*0a6a1f1dSLionel Sambuc }
244*0a6a1f1dSLionel Sambuc }
245f4a2713aSLionel Sambuc
246*0a6a1f1dSLionel Sambuc EHStreamer *ES = nullptr;
247f4a2713aSLionel Sambuc switch (MAI->getExceptionHandlingType()) {
248f4a2713aSLionel Sambuc case ExceptionHandling::None:
249*0a6a1f1dSLionel Sambuc break;
250f4a2713aSLionel Sambuc case ExceptionHandling::SjLj:
251f4a2713aSLionel Sambuc case ExceptionHandling::DwarfCFI:
252*0a6a1f1dSLionel Sambuc ES = new DwarfCFIException(this);
253*0a6a1f1dSLionel Sambuc break;
254f4a2713aSLionel Sambuc case ExceptionHandling::ARM:
255*0a6a1f1dSLionel Sambuc ES = new ARMException(this);
256*0a6a1f1dSLionel Sambuc break;
257*0a6a1f1dSLionel Sambuc case ExceptionHandling::ItaniumWinEH:
258*0a6a1f1dSLionel Sambuc case ExceptionHandling::MSVC:
259*0a6a1f1dSLionel Sambuc switch (MAI->getWinEHEncodingType()) {
260*0a6a1f1dSLionel Sambuc default: llvm_unreachable("unsupported unwinding information encoding");
261*0a6a1f1dSLionel Sambuc case WinEH::EncodingType::Itanium:
262*0a6a1f1dSLionel Sambuc ES = new Win64Exception(this);
263*0a6a1f1dSLionel Sambuc break;
264*0a6a1f1dSLionel Sambuc }
265*0a6a1f1dSLionel Sambuc break;
266*0a6a1f1dSLionel Sambuc }
267*0a6a1f1dSLionel Sambuc if (ES)
268*0a6a1f1dSLionel Sambuc Handlers.push_back(HandlerInfo(ES, EHTimerName, DWARFGroupName));
269f4a2713aSLionel Sambuc return false;
270f4a2713aSLionel Sambuc }
271f4a2713aSLionel Sambuc
canBeHidden(const GlobalValue * GV,const MCAsmInfo & MAI)272*0a6a1f1dSLionel Sambuc static bool canBeHidden(const GlobalValue *GV, const MCAsmInfo &MAI) {
273*0a6a1f1dSLionel Sambuc if (!MAI.hasWeakDefCanBeHiddenDirective())
274*0a6a1f1dSLionel Sambuc return false;
275*0a6a1f1dSLionel Sambuc
276*0a6a1f1dSLionel Sambuc return canBeOmittedFromSymbolTable(GV);
277f4a2713aSLionel Sambuc }
278f4a2713aSLionel Sambuc
EmitLinkage(const GlobalValue * GV,MCSymbol * GVSym) const279f4a2713aSLionel Sambuc void AsmPrinter::EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const {
280f4a2713aSLionel Sambuc GlobalValue::LinkageTypes Linkage = GV->getLinkage();
281f4a2713aSLionel Sambuc switch (Linkage) {
282f4a2713aSLionel Sambuc case GlobalValue::CommonLinkage:
283f4a2713aSLionel Sambuc case GlobalValue::LinkOnceAnyLinkage:
284f4a2713aSLionel Sambuc case GlobalValue::LinkOnceODRLinkage:
285f4a2713aSLionel Sambuc case GlobalValue::WeakAnyLinkage:
286f4a2713aSLionel Sambuc case GlobalValue::WeakODRLinkage:
287*0a6a1f1dSLionel Sambuc if (MAI->hasWeakDefDirective()) {
288f4a2713aSLionel Sambuc // .globl _foo
289f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
290f4a2713aSLionel Sambuc
291*0a6a1f1dSLionel Sambuc if (!canBeHidden(GV, *MAI))
292f4a2713aSLionel Sambuc // .weak_definition _foo
293f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(GVSym, MCSA_WeakDefinition);
294f4a2713aSLionel Sambuc else
295f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(GVSym, MCSA_WeakDefAutoPrivate);
296*0a6a1f1dSLionel Sambuc } else if (MAI->hasLinkOnceDirective()) {
297f4a2713aSLionel Sambuc // .globl _foo
298f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
299f4a2713aSLionel Sambuc //NOTE: linkonce is handled by the section the symbol was assigned to.
300f4a2713aSLionel Sambuc } else {
301f4a2713aSLionel Sambuc // .weak _foo
302f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Weak);
303f4a2713aSLionel Sambuc }
304f4a2713aSLionel Sambuc return;
305f4a2713aSLionel Sambuc case GlobalValue::AppendingLinkage:
306f4a2713aSLionel Sambuc // FIXME: appending linkage variables should go into a section of
307f4a2713aSLionel Sambuc // their name or something. For now, just emit them as external.
308f4a2713aSLionel Sambuc case GlobalValue::ExternalLinkage:
309f4a2713aSLionel Sambuc // If external or appending, declare as a global symbol.
310f4a2713aSLionel Sambuc // .globl _foo
311f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
312f4a2713aSLionel Sambuc return;
313f4a2713aSLionel Sambuc case GlobalValue::PrivateLinkage:
314f4a2713aSLionel Sambuc case GlobalValue::InternalLinkage:
315f4a2713aSLionel Sambuc return;
316f4a2713aSLionel Sambuc case GlobalValue::AvailableExternallyLinkage:
317f4a2713aSLionel Sambuc llvm_unreachable("Should never emit this");
318f4a2713aSLionel Sambuc case GlobalValue::ExternalWeakLinkage:
319f4a2713aSLionel Sambuc llvm_unreachable("Don't know how to emit these");
320f4a2713aSLionel Sambuc }
321f4a2713aSLionel Sambuc llvm_unreachable("Unknown linkage type!");
322f4a2713aSLionel Sambuc }
323f4a2713aSLionel Sambuc
getNameWithPrefix(SmallVectorImpl<char> & Name,const GlobalValue * GV) const324*0a6a1f1dSLionel Sambuc void AsmPrinter::getNameWithPrefix(SmallVectorImpl<char> &Name,
325*0a6a1f1dSLionel Sambuc const GlobalValue *GV) const {
326*0a6a1f1dSLionel Sambuc TM.getNameWithPrefix(Name, GV, *Mang);
327*0a6a1f1dSLionel Sambuc }
328*0a6a1f1dSLionel Sambuc
getSymbol(const GlobalValue * GV) const329f4a2713aSLionel Sambuc MCSymbol *AsmPrinter::getSymbol(const GlobalValue *GV) const {
330*0a6a1f1dSLionel Sambuc return TM.getSymbol(GV, *Mang);
331f4a2713aSLionel Sambuc }
332f4a2713aSLionel Sambuc
333f4a2713aSLionel Sambuc /// EmitGlobalVariable - Emit the specified global variable to the .s file.
EmitGlobalVariable(const GlobalVariable * GV)334f4a2713aSLionel Sambuc void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
335f4a2713aSLionel Sambuc if (GV->hasInitializer()) {
336f4a2713aSLionel Sambuc // Check to see if this is a special global used by LLVM, if so, emit it.
337f4a2713aSLionel Sambuc if (EmitSpecialLLVMGlobal(GV))
338f4a2713aSLionel Sambuc return;
339f4a2713aSLionel Sambuc
340f4a2713aSLionel Sambuc if (isVerbose()) {
341*0a6a1f1dSLionel Sambuc GV->printAsOperand(OutStreamer.GetCommentOS(),
342f4a2713aSLionel Sambuc /*PrintType=*/false, GV->getParent());
343f4a2713aSLionel Sambuc OutStreamer.GetCommentOS() << '\n';
344f4a2713aSLionel Sambuc }
345f4a2713aSLionel Sambuc }
346f4a2713aSLionel Sambuc
347f4a2713aSLionel Sambuc MCSymbol *GVSym = getSymbol(GV);
348f4a2713aSLionel Sambuc EmitVisibility(GVSym, GV->getVisibility(), !GV->isDeclaration());
349f4a2713aSLionel Sambuc
350f4a2713aSLionel Sambuc if (!GV->hasInitializer()) // External globals require no extra code.
351f4a2713aSLionel Sambuc return;
352f4a2713aSLionel Sambuc
353*0a6a1f1dSLionel Sambuc GVSym->redefineIfPossible();
354*0a6a1f1dSLionel Sambuc if (GVSym->isDefined() || GVSym->isVariable())
355*0a6a1f1dSLionel Sambuc report_fatal_error("symbol '" + Twine(GVSym->getName()) +
356*0a6a1f1dSLionel Sambuc "' is already defined");
357*0a6a1f1dSLionel Sambuc
358f4a2713aSLionel Sambuc if (MAI->hasDotTypeDotSizeDirective())
359f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
362f4a2713aSLionel Sambuc
363*0a6a1f1dSLionel Sambuc const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
364f4a2713aSLionel Sambuc uint64_t Size = DL->getTypeAllocSize(GV->getType()->getElementType());
365f4a2713aSLionel Sambuc
366f4a2713aSLionel Sambuc // If the alignment is specified, we *must* obey it. Overaligning a global
367f4a2713aSLionel Sambuc // with a specified alignment is a prompt way to break globals emitted to
368f4a2713aSLionel Sambuc // sections and expected to be contiguous (e.g. ObjC metadata).
369f4a2713aSLionel Sambuc unsigned AlignLog = getGVAlignmentLog2(GV, *DL);
370f4a2713aSLionel Sambuc
371*0a6a1f1dSLionel Sambuc for (const HandlerInfo &HI : Handlers) {
372*0a6a1f1dSLionel Sambuc NamedRegionTimer T(HI.TimerName, HI.TimerGroupName, TimePassesIsEnabled);
373*0a6a1f1dSLionel Sambuc HI.Handler->setSymbolSize(GVSym, Size);
374*0a6a1f1dSLionel Sambuc }
375f4a2713aSLionel Sambuc
376f4a2713aSLionel Sambuc // Handle common and BSS local symbols (.lcomm).
377f4a2713aSLionel Sambuc if (GVKind.isCommon() || GVKind.isBSSLocal()) {
378f4a2713aSLionel Sambuc if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
379f4a2713aSLionel Sambuc unsigned Align = 1 << AlignLog;
380f4a2713aSLionel Sambuc
381f4a2713aSLionel Sambuc // Handle common symbols.
382f4a2713aSLionel Sambuc if (GVKind.isCommon()) {
383f4a2713aSLionel Sambuc if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
384f4a2713aSLionel Sambuc Align = 0;
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc // .comm _foo, 42, 4
387f4a2713aSLionel Sambuc OutStreamer.EmitCommonSymbol(GVSym, Size, Align);
388f4a2713aSLionel Sambuc return;
389f4a2713aSLionel Sambuc }
390f4a2713aSLionel Sambuc
391f4a2713aSLionel Sambuc // Handle local BSS symbols.
392f4a2713aSLionel Sambuc if (MAI->hasMachoZeroFillDirective()) {
393f4a2713aSLionel Sambuc const MCSection *TheSection =
394*0a6a1f1dSLionel Sambuc getObjFileLowering().SectionForGlobal(GV, GVKind, *Mang, TM);
395f4a2713aSLionel Sambuc // .zerofill __DATA, __bss, _foo, 400, 5
396f4a2713aSLionel Sambuc OutStreamer.EmitZerofill(TheSection, GVSym, Size, Align);
397f4a2713aSLionel Sambuc return;
398f4a2713aSLionel Sambuc }
399f4a2713aSLionel Sambuc
400f4a2713aSLionel Sambuc // Use .lcomm only if it supports user-specified alignment.
401f4a2713aSLionel Sambuc // Otherwise, while it would still be correct to use .lcomm in some
402f4a2713aSLionel Sambuc // cases (e.g. when Align == 1), the external assembler might enfore
403f4a2713aSLionel Sambuc // some -unknown- default alignment behavior, which could cause
404f4a2713aSLionel Sambuc // spurious differences between external and integrated assembler.
405f4a2713aSLionel Sambuc // Prefer to simply fall back to .local / .comm in this case.
406f4a2713aSLionel Sambuc if (MAI->getLCOMMDirectiveAlignmentType() != LCOMM::NoAlignment) {
407f4a2713aSLionel Sambuc // .lcomm _foo, 42
408f4a2713aSLionel Sambuc OutStreamer.EmitLocalCommonSymbol(GVSym, Size, Align);
409f4a2713aSLionel Sambuc return;
410f4a2713aSLionel Sambuc }
411f4a2713aSLionel Sambuc
412f4a2713aSLionel Sambuc if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
413f4a2713aSLionel Sambuc Align = 0;
414f4a2713aSLionel Sambuc
415f4a2713aSLionel Sambuc // .local _foo
416f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Local);
417f4a2713aSLionel Sambuc // .comm _foo, 42, 4
418f4a2713aSLionel Sambuc OutStreamer.EmitCommonSymbol(GVSym, Size, Align);
419f4a2713aSLionel Sambuc return;
420f4a2713aSLionel Sambuc }
421f4a2713aSLionel Sambuc
422f4a2713aSLionel Sambuc const MCSection *TheSection =
423*0a6a1f1dSLionel Sambuc getObjFileLowering().SectionForGlobal(GV, GVKind, *Mang, TM);
424f4a2713aSLionel Sambuc
425f4a2713aSLionel Sambuc // Handle the zerofill directive on darwin, which is a special form of BSS
426f4a2713aSLionel Sambuc // emission.
427f4a2713aSLionel Sambuc if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
428f4a2713aSLionel Sambuc if (Size == 0) Size = 1; // zerofill of 0 bytes is undefined.
429f4a2713aSLionel Sambuc
430f4a2713aSLionel Sambuc // .globl _foo
431f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
432f4a2713aSLionel Sambuc // .zerofill __DATA, __common, _foo, 400, 5
433f4a2713aSLionel Sambuc OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
434f4a2713aSLionel Sambuc return;
435f4a2713aSLionel Sambuc }
436f4a2713aSLionel Sambuc
437f4a2713aSLionel Sambuc // Handle thread local data for mach-o which requires us to output an
438f4a2713aSLionel Sambuc // additional structure of data and mangle the original symbol so that we
439f4a2713aSLionel Sambuc // can reference it later.
440f4a2713aSLionel Sambuc //
441f4a2713aSLionel Sambuc // TODO: This should become an "emit thread local global" method on TLOF.
442f4a2713aSLionel Sambuc // All of this macho specific stuff should be sunk down into TLOFMachO and
443f4a2713aSLionel Sambuc // stuff like "TLSExtraDataSection" should no longer be part of the parent
444f4a2713aSLionel Sambuc // TLOF class. This will also make it more obvious that stuff like
445f4a2713aSLionel Sambuc // MCStreamer::EmitTBSSSymbol is macho specific and only called from macho
446f4a2713aSLionel Sambuc // specific code.
447f4a2713aSLionel Sambuc if (GVKind.isThreadLocal() && MAI->hasMachoTBSSDirective()) {
448f4a2713aSLionel Sambuc // Emit the .tbss symbol
449f4a2713aSLionel Sambuc MCSymbol *MangSym =
450f4a2713aSLionel Sambuc OutContext.GetOrCreateSymbol(GVSym->getName() + Twine("$tlv$init"));
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambuc if (GVKind.isThreadBSS()) {
453f4a2713aSLionel Sambuc TheSection = getObjFileLowering().getTLSBSSSection();
454f4a2713aSLionel Sambuc OutStreamer.EmitTBSSSymbol(TheSection, MangSym, Size, 1 << AlignLog);
455f4a2713aSLionel Sambuc } else if (GVKind.isThreadData()) {
456f4a2713aSLionel Sambuc OutStreamer.SwitchSection(TheSection);
457f4a2713aSLionel Sambuc
458f4a2713aSLionel Sambuc EmitAlignment(AlignLog, GV);
459f4a2713aSLionel Sambuc OutStreamer.EmitLabel(MangSym);
460f4a2713aSLionel Sambuc
461f4a2713aSLionel Sambuc EmitGlobalConstant(GV->getInitializer());
462f4a2713aSLionel Sambuc }
463f4a2713aSLionel Sambuc
464f4a2713aSLionel Sambuc OutStreamer.AddBlankLine();
465f4a2713aSLionel Sambuc
466f4a2713aSLionel Sambuc // Emit the variable struct for the runtime.
467f4a2713aSLionel Sambuc const MCSection *TLVSect
468f4a2713aSLionel Sambuc = getObjFileLowering().getTLSExtraDataSection();
469f4a2713aSLionel Sambuc
470f4a2713aSLionel Sambuc OutStreamer.SwitchSection(TLVSect);
471f4a2713aSLionel Sambuc // Emit the linkage here.
472f4a2713aSLionel Sambuc EmitLinkage(GV, GVSym);
473f4a2713aSLionel Sambuc OutStreamer.EmitLabel(GVSym);
474f4a2713aSLionel Sambuc
475f4a2713aSLionel Sambuc // Three pointers in size:
476f4a2713aSLionel Sambuc // - __tlv_bootstrap - used to make sure support exists
477f4a2713aSLionel Sambuc // - spare pointer, used when mapped by the runtime
478f4a2713aSLionel Sambuc // - pointer to mangled symbol above with initializer
479f4a2713aSLionel Sambuc unsigned PtrSize = DL->getPointerTypeSize(GV->getType());
480f4a2713aSLionel Sambuc OutStreamer.EmitSymbolValue(GetExternalSymbolSymbol("_tlv_bootstrap"),
481f4a2713aSLionel Sambuc PtrSize);
482f4a2713aSLionel Sambuc OutStreamer.EmitIntValue(0, PtrSize);
483f4a2713aSLionel Sambuc OutStreamer.EmitSymbolValue(MangSym, PtrSize);
484f4a2713aSLionel Sambuc
485f4a2713aSLionel Sambuc OutStreamer.AddBlankLine();
486f4a2713aSLionel Sambuc return;
487f4a2713aSLionel Sambuc }
488f4a2713aSLionel Sambuc
489f4a2713aSLionel Sambuc OutStreamer.SwitchSection(TheSection);
490f4a2713aSLionel Sambuc
491f4a2713aSLionel Sambuc EmitLinkage(GV, GVSym);
492f4a2713aSLionel Sambuc EmitAlignment(AlignLog, GV);
493f4a2713aSLionel Sambuc
494f4a2713aSLionel Sambuc OutStreamer.EmitLabel(GVSym);
495f4a2713aSLionel Sambuc
496f4a2713aSLionel Sambuc EmitGlobalConstant(GV->getInitializer());
497f4a2713aSLionel Sambuc
498f4a2713aSLionel Sambuc if (MAI->hasDotTypeDotSizeDirective())
499f4a2713aSLionel Sambuc // .size foo, 42
500f4a2713aSLionel Sambuc OutStreamer.EmitELFSize(GVSym, MCConstantExpr::Create(Size, OutContext));
501f4a2713aSLionel Sambuc
502f4a2713aSLionel Sambuc OutStreamer.AddBlankLine();
503f4a2713aSLionel Sambuc }
504f4a2713aSLionel Sambuc
505f4a2713aSLionel Sambuc /// EmitFunctionHeader - This method emits the header for the current
506f4a2713aSLionel Sambuc /// function.
EmitFunctionHeader()507f4a2713aSLionel Sambuc void AsmPrinter::EmitFunctionHeader() {
508f4a2713aSLionel Sambuc // Print out constants referenced by the function
509f4a2713aSLionel Sambuc EmitConstantPool();
510f4a2713aSLionel Sambuc
511f4a2713aSLionel Sambuc // Print the 'header' of function.
512f4a2713aSLionel Sambuc const Function *F = MF->getFunction();
513f4a2713aSLionel Sambuc
514*0a6a1f1dSLionel Sambuc OutStreamer.SwitchSection(
515*0a6a1f1dSLionel Sambuc getObjFileLowering().SectionForGlobal(F, *Mang, TM));
516f4a2713aSLionel Sambuc EmitVisibility(CurrentFnSym, F->getVisibility());
517f4a2713aSLionel Sambuc
518f4a2713aSLionel Sambuc EmitLinkage(F, CurrentFnSym);
519f4a2713aSLionel Sambuc EmitAlignment(MF->getAlignment(), F);
520f4a2713aSLionel Sambuc
521f4a2713aSLionel Sambuc if (MAI->hasDotTypeDotSizeDirective())
522f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
523f4a2713aSLionel Sambuc
524f4a2713aSLionel Sambuc if (isVerbose()) {
525*0a6a1f1dSLionel Sambuc F->printAsOperand(OutStreamer.GetCommentOS(),
526f4a2713aSLionel Sambuc /*PrintType=*/false, F->getParent());
527f4a2713aSLionel Sambuc OutStreamer.GetCommentOS() << '\n';
528f4a2713aSLionel Sambuc }
529f4a2713aSLionel Sambuc
530*0a6a1f1dSLionel Sambuc // Emit the prefix data.
531*0a6a1f1dSLionel Sambuc if (F->hasPrefixData())
532*0a6a1f1dSLionel Sambuc EmitGlobalConstant(F->getPrefixData());
533*0a6a1f1dSLionel Sambuc
534f4a2713aSLionel Sambuc // Emit the CurrentFnSym. This is a virtual function to allow targets to
535f4a2713aSLionel Sambuc // do their wild and crazy things as required.
536f4a2713aSLionel Sambuc EmitFunctionEntryLabel();
537f4a2713aSLionel Sambuc
538f4a2713aSLionel Sambuc // If the function had address-taken blocks that got deleted, then we have
539f4a2713aSLionel Sambuc // references to the dangling symbols. Emit them at the start of the function
540f4a2713aSLionel Sambuc // so that we don't get references to undefined symbols.
541f4a2713aSLionel Sambuc std::vector<MCSymbol*> DeadBlockSyms;
542f4a2713aSLionel Sambuc MMI->takeDeletedSymbolsForFunction(F, DeadBlockSyms);
543f4a2713aSLionel Sambuc for (unsigned i = 0, e = DeadBlockSyms.size(); i != e; ++i) {
544f4a2713aSLionel Sambuc OutStreamer.AddComment("Address taken block that was later removed");
545f4a2713aSLionel Sambuc OutStreamer.EmitLabel(DeadBlockSyms[i]);
546f4a2713aSLionel Sambuc }
547f4a2713aSLionel Sambuc
548f4a2713aSLionel Sambuc // Emit pre-function debug and/or EH information.
549*0a6a1f1dSLionel Sambuc for (const HandlerInfo &HI : Handlers) {
550*0a6a1f1dSLionel Sambuc NamedRegionTimer T(HI.TimerName, HI.TimerGroupName, TimePassesIsEnabled);
551*0a6a1f1dSLionel Sambuc HI.Handler->beginFunction(MF);
552f4a2713aSLionel Sambuc }
553f4a2713aSLionel Sambuc
554*0a6a1f1dSLionel Sambuc // Emit the prologue data.
555*0a6a1f1dSLionel Sambuc if (F->hasPrologueData())
556*0a6a1f1dSLionel Sambuc EmitGlobalConstant(F->getPrologueData());
557f4a2713aSLionel Sambuc }
558f4a2713aSLionel Sambuc
559f4a2713aSLionel Sambuc /// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the
560f4a2713aSLionel Sambuc /// function. This can be overridden by targets as required to do custom stuff.
EmitFunctionEntryLabel()561f4a2713aSLionel Sambuc void AsmPrinter::EmitFunctionEntryLabel() {
562*0a6a1f1dSLionel Sambuc CurrentFnSym->redefineIfPossible();
563*0a6a1f1dSLionel Sambuc
564f4a2713aSLionel Sambuc // The function label could have already been emitted if two symbols end up
565f4a2713aSLionel Sambuc // conflicting due to asm renaming. Detect this and emit an error.
566*0a6a1f1dSLionel Sambuc if (CurrentFnSym->isVariable())
567*0a6a1f1dSLionel Sambuc report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
568*0a6a1f1dSLionel Sambuc "' is a protected alias");
569*0a6a1f1dSLionel Sambuc if (CurrentFnSym->isDefined())
570f4a2713aSLionel Sambuc report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
571f4a2713aSLionel Sambuc "' label emitted multiple times to assembly file");
572*0a6a1f1dSLionel Sambuc
573*0a6a1f1dSLionel Sambuc return OutStreamer.EmitLabel(CurrentFnSym);
574f4a2713aSLionel Sambuc }
575f4a2713aSLionel Sambuc
576f4a2713aSLionel Sambuc /// emitComments - Pretty-print comments for instructions.
emitComments(const MachineInstr & MI,raw_ostream & CommentOS)577f4a2713aSLionel Sambuc static void emitComments(const MachineInstr &MI, raw_ostream &CommentOS) {
578f4a2713aSLionel Sambuc const MachineFunction *MF = MI.getParent()->getParent();
579f4a2713aSLionel Sambuc const TargetMachine &TM = MF->getTarget();
580f4a2713aSLionel Sambuc
581f4a2713aSLionel Sambuc // Check for spills and reloads
582f4a2713aSLionel Sambuc int FI;
583f4a2713aSLionel Sambuc
584f4a2713aSLionel Sambuc const MachineFrameInfo *FrameInfo = MF->getFrameInfo();
585f4a2713aSLionel Sambuc
586f4a2713aSLionel Sambuc // We assume a single instruction only has a spill or reload, not
587f4a2713aSLionel Sambuc // both.
588f4a2713aSLionel Sambuc const MachineMemOperand *MMO;
589*0a6a1f1dSLionel Sambuc if (TM.getSubtargetImpl()->getInstrInfo()->isLoadFromStackSlotPostFE(&MI,
590*0a6a1f1dSLionel Sambuc FI)) {
591f4a2713aSLionel Sambuc if (FrameInfo->isSpillSlotObjectIndex(FI)) {
592f4a2713aSLionel Sambuc MMO = *MI.memoperands_begin();
593f4a2713aSLionel Sambuc CommentOS << MMO->getSize() << "-byte Reload\n";
594f4a2713aSLionel Sambuc }
595*0a6a1f1dSLionel Sambuc } else if (TM.getSubtargetImpl()->getInstrInfo()->hasLoadFromStackSlot(
596*0a6a1f1dSLionel Sambuc &MI, MMO, FI)) {
597f4a2713aSLionel Sambuc if (FrameInfo->isSpillSlotObjectIndex(FI))
598f4a2713aSLionel Sambuc CommentOS << MMO->getSize() << "-byte Folded Reload\n";
599*0a6a1f1dSLionel Sambuc } else if (TM.getSubtargetImpl()->getInstrInfo()->isStoreToStackSlotPostFE(
600*0a6a1f1dSLionel Sambuc &MI, FI)) {
601f4a2713aSLionel Sambuc if (FrameInfo->isSpillSlotObjectIndex(FI)) {
602f4a2713aSLionel Sambuc MMO = *MI.memoperands_begin();
603f4a2713aSLionel Sambuc CommentOS << MMO->getSize() << "-byte Spill\n";
604f4a2713aSLionel Sambuc }
605*0a6a1f1dSLionel Sambuc } else if (TM.getSubtargetImpl()->getInstrInfo()->hasStoreToStackSlot(
606*0a6a1f1dSLionel Sambuc &MI, MMO, FI)) {
607f4a2713aSLionel Sambuc if (FrameInfo->isSpillSlotObjectIndex(FI))
608f4a2713aSLionel Sambuc CommentOS << MMO->getSize() << "-byte Folded Spill\n";
609f4a2713aSLionel Sambuc }
610f4a2713aSLionel Sambuc
611f4a2713aSLionel Sambuc // Check for spill-induced copies
612f4a2713aSLionel Sambuc if (MI.getAsmPrinterFlag(MachineInstr::ReloadReuse))
613f4a2713aSLionel Sambuc CommentOS << " Reload Reuse\n";
614f4a2713aSLionel Sambuc }
615f4a2713aSLionel Sambuc
616f4a2713aSLionel Sambuc /// emitImplicitDef - This method emits the specified machine instruction
617f4a2713aSLionel Sambuc /// that is an implicit def.
emitImplicitDef(const MachineInstr * MI) const618f4a2713aSLionel Sambuc void AsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
619f4a2713aSLionel Sambuc unsigned RegNo = MI->getOperand(0).getReg();
620*0a6a1f1dSLionel Sambuc OutStreamer.AddComment(
621*0a6a1f1dSLionel Sambuc Twine("implicit-def: ") +
622*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getRegisterInfo()->getName(RegNo));
623f4a2713aSLionel Sambuc OutStreamer.AddBlankLine();
624f4a2713aSLionel Sambuc }
625f4a2713aSLionel Sambuc
emitKill(const MachineInstr * MI,AsmPrinter & AP)626f4a2713aSLionel Sambuc static void emitKill(const MachineInstr *MI, AsmPrinter &AP) {
627f4a2713aSLionel Sambuc std::string Str = "kill:";
628f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
629f4a2713aSLionel Sambuc const MachineOperand &Op = MI->getOperand(i);
630f4a2713aSLionel Sambuc assert(Op.isReg() && "KILL instruction must have only register operands");
631f4a2713aSLionel Sambuc Str += ' ';
632*0a6a1f1dSLionel Sambuc Str += AP.TM.getSubtargetImpl()->getRegisterInfo()->getName(Op.getReg());
633f4a2713aSLionel Sambuc Str += (Op.isDef() ? "<def>" : "<kill>");
634f4a2713aSLionel Sambuc }
635f4a2713aSLionel Sambuc AP.OutStreamer.AddComment(Str);
636f4a2713aSLionel Sambuc AP.OutStreamer.AddBlankLine();
637f4a2713aSLionel Sambuc }
638f4a2713aSLionel Sambuc
639f4a2713aSLionel Sambuc /// emitDebugValueComment - This method handles the target-independent form
640f4a2713aSLionel Sambuc /// of DBG_VALUE, returning true if it was able to do so. A false return
641f4a2713aSLionel Sambuc /// means the target will need to handle MI in EmitInstruction.
emitDebugValueComment(const MachineInstr * MI,AsmPrinter & AP)642f4a2713aSLionel Sambuc static bool emitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) {
643*0a6a1f1dSLionel Sambuc // This code handles only the 4-operand target-independent form.
644*0a6a1f1dSLionel Sambuc if (MI->getNumOperands() != 4)
645f4a2713aSLionel Sambuc return false;
646f4a2713aSLionel Sambuc
647f4a2713aSLionel Sambuc SmallString<128> Str;
648f4a2713aSLionel Sambuc raw_svector_ostream OS(Str);
649*0a6a1f1dSLionel Sambuc OS << "DEBUG_VALUE: ";
650f4a2713aSLionel Sambuc
651*0a6a1f1dSLionel Sambuc DIVariable V = MI->getDebugVariable();
652f4a2713aSLionel Sambuc if (V.getContext().isSubprogram()) {
653f4a2713aSLionel Sambuc StringRef Name = DISubprogram(V.getContext()).getDisplayName();
654f4a2713aSLionel Sambuc if (!Name.empty())
655f4a2713aSLionel Sambuc OS << Name << ":";
656f4a2713aSLionel Sambuc }
657*0a6a1f1dSLionel Sambuc OS << V.getName();
658*0a6a1f1dSLionel Sambuc
659*0a6a1f1dSLionel Sambuc DIExpression Expr = MI->getDebugExpression();
660*0a6a1f1dSLionel Sambuc if (Expr.isVariablePiece())
661*0a6a1f1dSLionel Sambuc OS << " [piece offset=" << Expr.getPieceOffset()
662*0a6a1f1dSLionel Sambuc << " size=" << Expr.getPieceSize() << "]";
663*0a6a1f1dSLionel Sambuc OS << " <- ";
664f4a2713aSLionel Sambuc
665f4a2713aSLionel Sambuc // The second operand is only an offset if it's an immediate.
666f4a2713aSLionel Sambuc bool Deref = MI->getOperand(0).isReg() && MI->getOperand(1).isImm();
667f4a2713aSLionel Sambuc int64_t Offset = Deref ? MI->getOperand(1).getImm() : 0;
668f4a2713aSLionel Sambuc
669f4a2713aSLionel Sambuc // Register or immediate value. Register 0 means undef.
670f4a2713aSLionel Sambuc if (MI->getOperand(0).isFPImm()) {
671f4a2713aSLionel Sambuc APFloat APF = APFloat(MI->getOperand(0).getFPImm()->getValueAPF());
672f4a2713aSLionel Sambuc if (MI->getOperand(0).getFPImm()->getType()->isFloatTy()) {
673f4a2713aSLionel Sambuc OS << (double)APF.convertToFloat();
674f4a2713aSLionel Sambuc } else if (MI->getOperand(0).getFPImm()->getType()->isDoubleTy()) {
675f4a2713aSLionel Sambuc OS << APF.convertToDouble();
676f4a2713aSLionel Sambuc } else {
677f4a2713aSLionel Sambuc // There is no good way to print long double. Convert a copy to
678f4a2713aSLionel Sambuc // double. Ah well, it's only a comment.
679f4a2713aSLionel Sambuc bool ignored;
680f4a2713aSLionel Sambuc APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
681f4a2713aSLionel Sambuc &ignored);
682f4a2713aSLionel Sambuc OS << "(long double) " << APF.convertToDouble();
683f4a2713aSLionel Sambuc }
684f4a2713aSLionel Sambuc } else if (MI->getOperand(0).isImm()) {
685f4a2713aSLionel Sambuc OS << MI->getOperand(0).getImm();
686f4a2713aSLionel Sambuc } else if (MI->getOperand(0).isCImm()) {
687f4a2713aSLionel Sambuc MI->getOperand(0).getCImm()->getValue().print(OS, false /*isSigned*/);
688f4a2713aSLionel Sambuc } else {
689f4a2713aSLionel Sambuc unsigned Reg;
690f4a2713aSLionel Sambuc if (MI->getOperand(0).isReg()) {
691f4a2713aSLionel Sambuc Reg = MI->getOperand(0).getReg();
692f4a2713aSLionel Sambuc } else {
693f4a2713aSLionel Sambuc assert(MI->getOperand(0).isFI() && "Unknown operand type");
694*0a6a1f1dSLionel Sambuc const TargetFrameLowering *TFI =
695*0a6a1f1dSLionel Sambuc AP.TM.getSubtargetImpl()->getFrameLowering();
696f4a2713aSLionel Sambuc Offset += TFI->getFrameIndexReference(*AP.MF,
697f4a2713aSLionel Sambuc MI->getOperand(0).getIndex(), Reg);
698f4a2713aSLionel Sambuc Deref = true;
699f4a2713aSLionel Sambuc }
700f4a2713aSLionel Sambuc if (Reg == 0) {
701f4a2713aSLionel Sambuc // Suppress offset, it is not meaningful here.
702f4a2713aSLionel Sambuc OS << "undef";
703f4a2713aSLionel Sambuc // NOTE: Want this comment at start of line, don't emit with AddComment.
704*0a6a1f1dSLionel Sambuc AP.OutStreamer.emitRawComment(OS.str());
705f4a2713aSLionel Sambuc return true;
706f4a2713aSLionel Sambuc }
707f4a2713aSLionel Sambuc if (Deref)
708f4a2713aSLionel Sambuc OS << '[';
709*0a6a1f1dSLionel Sambuc OS << AP.TM.getSubtargetImpl()->getRegisterInfo()->getName(Reg);
710f4a2713aSLionel Sambuc }
711f4a2713aSLionel Sambuc
712f4a2713aSLionel Sambuc if (Deref)
713f4a2713aSLionel Sambuc OS << '+' << Offset << ']';
714f4a2713aSLionel Sambuc
715f4a2713aSLionel Sambuc // NOTE: Want this comment at start of line, don't emit with AddComment.
716*0a6a1f1dSLionel Sambuc AP.OutStreamer.emitRawComment(OS.str());
717f4a2713aSLionel Sambuc return true;
718f4a2713aSLionel Sambuc }
719f4a2713aSLionel Sambuc
needsCFIMoves()720f4a2713aSLionel Sambuc AsmPrinter::CFIMoveType AsmPrinter::needsCFIMoves() {
721f4a2713aSLionel Sambuc if (MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI &&
722f4a2713aSLionel Sambuc MF->getFunction()->needsUnwindTableEntry())
723f4a2713aSLionel Sambuc return CFI_M_EH;
724f4a2713aSLionel Sambuc
725f4a2713aSLionel Sambuc if (MMI->hasDebugInfo())
726f4a2713aSLionel Sambuc return CFI_M_Debug;
727f4a2713aSLionel Sambuc
728f4a2713aSLionel Sambuc return CFI_M_None;
729f4a2713aSLionel Sambuc }
730f4a2713aSLionel Sambuc
needsSEHMoves()731f4a2713aSLionel Sambuc bool AsmPrinter::needsSEHMoves() {
732*0a6a1f1dSLionel Sambuc return MAI->usesWindowsCFI() && MF->getFunction()->needsUnwindTableEntry();
733f4a2713aSLionel Sambuc }
734f4a2713aSLionel Sambuc
emitCFIInstruction(const MachineInstr & MI)735*0a6a1f1dSLionel Sambuc void AsmPrinter::emitCFIInstruction(const MachineInstr &MI) {
736*0a6a1f1dSLionel Sambuc ExceptionHandling ExceptionHandlingType = MAI->getExceptionHandlingType();
737*0a6a1f1dSLionel Sambuc if (ExceptionHandlingType != ExceptionHandling::DwarfCFI &&
738*0a6a1f1dSLionel Sambuc ExceptionHandlingType != ExceptionHandling::ARM)
739f4a2713aSLionel Sambuc return;
740f4a2713aSLionel Sambuc
741f4a2713aSLionel Sambuc if (needsCFIMoves() == CFI_M_None)
742f4a2713aSLionel Sambuc return;
743f4a2713aSLionel Sambuc
744f4a2713aSLionel Sambuc const MachineModuleInfo &MMI = MF->getMMI();
745f4a2713aSLionel Sambuc const std::vector<MCCFIInstruction> &Instrs = MMI.getFrameInstructions();
746*0a6a1f1dSLionel Sambuc unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
747*0a6a1f1dSLionel Sambuc const MCCFIInstruction &CFI = Instrs[CFIIndex];
748*0a6a1f1dSLionel Sambuc emitCFIInstruction(CFI);
749f4a2713aSLionel Sambuc }
750*0a6a1f1dSLionel Sambuc
emitFrameAlloc(const MachineInstr & MI)751*0a6a1f1dSLionel Sambuc void AsmPrinter::emitFrameAlloc(const MachineInstr &MI) {
752*0a6a1f1dSLionel Sambuc // The operands are the MCSymbol and the frame offset of the allocation.
753*0a6a1f1dSLionel Sambuc MCSymbol *FrameAllocSym = MI.getOperand(0).getMCSymbol();
754*0a6a1f1dSLionel Sambuc int FrameOffset = MI.getOperand(1).getImm();
755*0a6a1f1dSLionel Sambuc
756*0a6a1f1dSLionel Sambuc // Emit a symbol assignment.
757*0a6a1f1dSLionel Sambuc OutStreamer.EmitAssignment(FrameAllocSym,
758*0a6a1f1dSLionel Sambuc MCConstantExpr::Create(FrameOffset, OutContext));
759f4a2713aSLionel Sambuc }
760f4a2713aSLionel Sambuc
761f4a2713aSLionel Sambuc /// EmitFunctionBody - This method emits the body and trailer for a
762f4a2713aSLionel Sambuc /// function.
EmitFunctionBody()763f4a2713aSLionel Sambuc void AsmPrinter::EmitFunctionBody() {
764f4a2713aSLionel Sambuc // Emit target-specific gunk before the function body.
765f4a2713aSLionel Sambuc EmitFunctionBodyStart();
766f4a2713aSLionel Sambuc
767*0a6a1f1dSLionel Sambuc bool ShouldPrintDebugScopes = MMI->hasDebugInfo();
768f4a2713aSLionel Sambuc
769f4a2713aSLionel Sambuc // Print out code for the function.
770f4a2713aSLionel Sambuc bool HasAnyRealCode = false;
771*0a6a1f1dSLionel Sambuc for (auto &MBB : *MF) {
772f4a2713aSLionel Sambuc // Print a label for the basic block.
773*0a6a1f1dSLionel Sambuc EmitBasicBlockStart(MBB);
774*0a6a1f1dSLionel Sambuc for (auto &MI : MBB) {
775f4a2713aSLionel Sambuc
776f4a2713aSLionel Sambuc // Print the assembly for the instruction.
777*0a6a1f1dSLionel Sambuc if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
778*0a6a1f1dSLionel Sambuc !MI.isDebugValue()) {
779f4a2713aSLionel Sambuc HasAnyRealCode = true;
780f4a2713aSLionel Sambuc ++EmittedInsts;
781f4a2713aSLionel Sambuc }
782f4a2713aSLionel Sambuc
783f4a2713aSLionel Sambuc if (ShouldPrintDebugScopes) {
784*0a6a1f1dSLionel Sambuc for (const HandlerInfo &HI : Handlers) {
785*0a6a1f1dSLionel Sambuc NamedRegionTimer T(HI.TimerName, HI.TimerGroupName,
786*0a6a1f1dSLionel Sambuc TimePassesIsEnabled);
787*0a6a1f1dSLionel Sambuc HI.Handler->beginInstruction(&MI);
788*0a6a1f1dSLionel Sambuc }
789f4a2713aSLionel Sambuc }
790f4a2713aSLionel Sambuc
791f4a2713aSLionel Sambuc if (isVerbose())
792*0a6a1f1dSLionel Sambuc emitComments(MI, OutStreamer.GetCommentOS());
793f4a2713aSLionel Sambuc
794*0a6a1f1dSLionel Sambuc switch (MI.getOpcode()) {
795*0a6a1f1dSLionel Sambuc case TargetOpcode::CFI_INSTRUCTION:
796*0a6a1f1dSLionel Sambuc emitCFIInstruction(MI);
797*0a6a1f1dSLionel Sambuc break;
798*0a6a1f1dSLionel Sambuc
799*0a6a1f1dSLionel Sambuc case TargetOpcode::FRAME_ALLOC:
800*0a6a1f1dSLionel Sambuc emitFrameAlloc(MI);
801f4a2713aSLionel Sambuc break;
802f4a2713aSLionel Sambuc
803f4a2713aSLionel Sambuc case TargetOpcode::EH_LABEL:
804f4a2713aSLionel Sambuc case TargetOpcode::GC_LABEL:
805*0a6a1f1dSLionel Sambuc OutStreamer.EmitLabel(MI.getOperand(0).getMCSymbol());
806f4a2713aSLionel Sambuc break;
807f4a2713aSLionel Sambuc case TargetOpcode::INLINEASM:
808*0a6a1f1dSLionel Sambuc EmitInlineAsm(&MI);
809f4a2713aSLionel Sambuc break;
810f4a2713aSLionel Sambuc case TargetOpcode::DBG_VALUE:
811f4a2713aSLionel Sambuc if (isVerbose()) {
812*0a6a1f1dSLionel Sambuc if (!emitDebugValueComment(&MI, *this))
813*0a6a1f1dSLionel Sambuc EmitInstruction(&MI);
814f4a2713aSLionel Sambuc }
815f4a2713aSLionel Sambuc break;
816f4a2713aSLionel Sambuc case TargetOpcode::IMPLICIT_DEF:
817*0a6a1f1dSLionel Sambuc if (isVerbose()) emitImplicitDef(&MI);
818f4a2713aSLionel Sambuc break;
819f4a2713aSLionel Sambuc case TargetOpcode::KILL:
820*0a6a1f1dSLionel Sambuc if (isVerbose()) emitKill(&MI, *this);
821f4a2713aSLionel Sambuc break;
822f4a2713aSLionel Sambuc default:
823*0a6a1f1dSLionel Sambuc EmitInstruction(&MI);
824f4a2713aSLionel Sambuc break;
825f4a2713aSLionel Sambuc }
826f4a2713aSLionel Sambuc
827f4a2713aSLionel Sambuc if (ShouldPrintDebugScopes) {
828*0a6a1f1dSLionel Sambuc for (const HandlerInfo &HI : Handlers) {
829*0a6a1f1dSLionel Sambuc NamedRegionTimer T(HI.TimerName, HI.TimerGroupName,
830*0a6a1f1dSLionel Sambuc TimePassesIsEnabled);
831*0a6a1f1dSLionel Sambuc HI.Handler->endInstruction();
832f4a2713aSLionel Sambuc }
833f4a2713aSLionel Sambuc }
834f4a2713aSLionel Sambuc }
835f4a2713aSLionel Sambuc
836*0a6a1f1dSLionel Sambuc EmitBasicBlockEnd(MBB);
837*0a6a1f1dSLionel Sambuc }
838f4a2713aSLionel Sambuc
839f4a2713aSLionel Sambuc // If the function is empty and the object file uses .subsections_via_symbols,
840f4a2713aSLionel Sambuc // then we need to emit *something* to the function body to prevent the
841f4a2713aSLionel Sambuc // labels from collapsing together. Just emit a noop.
842*0a6a1f1dSLionel Sambuc if ((MAI->hasSubsectionsViaSymbols() && !HasAnyRealCode)) {
843f4a2713aSLionel Sambuc MCInst Noop;
844*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getInstrInfo()->getNoopForMachoTarget(Noop);
845f4a2713aSLionel Sambuc OutStreamer.AddComment("avoids zero-length function");
846*0a6a1f1dSLionel Sambuc
847*0a6a1f1dSLionel Sambuc // Targets can opt-out of emitting the noop here by leaving the opcode
848*0a6a1f1dSLionel Sambuc // unspecified.
849*0a6a1f1dSLionel Sambuc if (Noop.getOpcode())
850*0a6a1f1dSLionel Sambuc OutStreamer.EmitInstruction(Noop, getSubtargetInfo());
851f4a2713aSLionel Sambuc }
852f4a2713aSLionel Sambuc
853f4a2713aSLionel Sambuc const Function *F = MF->getFunction();
854*0a6a1f1dSLionel Sambuc for (const auto &BB : *F) {
855*0a6a1f1dSLionel Sambuc if (!BB.hasAddressTaken())
856f4a2713aSLionel Sambuc continue;
857*0a6a1f1dSLionel Sambuc MCSymbol *Sym = GetBlockAddressSymbol(&BB);
858f4a2713aSLionel Sambuc if (Sym->isDefined())
859f4a2713aSLionel Sambuc continue;
860f4a2713aSLionel Sambuc OutStreamer.AddComment("Address of block that was removed by CodeGen");
861f4a2713aSLionel Sambuc OutStreamer.EmitLabel(Sym);
862f4a2713aSLionel Sambuc }
863f4a2713aSLionel Sambuc
864f4a2713aSLionel Sambuc // Emit target-specific gunk after the function body.
865f4a2713aSLionel Sambuc EmitFunctionBodyEnd();
866f4a2713aSLionel Sambuc
867f4a2713aSLionel Sambuc // If the target wants a .size directive for the size of the function, emit
868f4a2713aSLionel Sambuc // it.
869f4a2713aSLionel Sambuc if (MAI->hasDotTypeDotSizeDirective()) {
870f4a2713aSLionel Sambuc // Create a symbol for the end of function, so we can get the size as
871f4a2713aSLionel Sambuc // difference between the function label and the temp label.
872f4a2713aSLionel Sambuc MCSymbol *FnEndLabel = OutContext.CreateTempSymbol();
873f4a2713aSLionel Sambuc OutStreamer.EmitLabel(FnEndLabel);
874f4a2713aSLionel Sambuc
875f4a2713aSLionel Sambuc const MCExpr *SizeExp =
876f4a2713aSLionel Sambuc MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(FnEndLabel, OutContext),
877f4a2713aSLionel Sambuc MCSymbolRefExpr::Create(CurrentFnSymForSize,
878f4a2713aSLionel Sambuc OutContext),
879f4a2713aSLionel Sambuc OutContext);
880f4a2713aSLionel Sambuc OutStreamer.EmitELFSize(CurrentFnSym, SizeExp);
881f4a2713aSLionel Sambuc }
882f4a2713aSLionel Sambuc
883*0a6a1f1dSLionel Sambuc // Emit post-function debug and/or EH information.
884*0a6a1f1dSLionel Sambuc for (const HandlerInfo &HI : Handlers) {
885*0a6a1f1dSLionel Sambuc NamedRegionTimer T(HI.TimerName, HI.TimerGroupName, TimePassesIsEnabled);
886*0a6a1f1dSLionel Sambuc HI.Handler->endFunction(MF);
887f4a2713aSLionel Sambuc }
888f4a2713aSLionel Sambuc MMI->EndFunction();
889f4a2713aSLionel Sambuc
890f4a2713aSLionel Sambuc // Print out jump tables referenced by the function.
891f4a2713aSLionel Sambuc EmitJumpTableInfo();
892f4a2713aSLionel Sambuc
893f4a2713aSLionel Sambuc OutStreamer.AddBlankLine();
894f4a2713aSLionel Sambuc }
895f4a2713aSLionel Sambuc
doFinalization(Module & M)896f4a2713aSLionel Sambuc bool AsmPrinter::doFinalization(Module &M) {
897f4a2713aSLionel Sambuc // Emit global variables.
898*0a6a1f1dSLionel Sambuc for (const auto &G : M.globals())
899*0a6a1f1dSLionel Sambuc EmitGlobalVariable(&G);
900f4a2713aSLionel Sambuc
901f4a2713aSLionel Sambuc // Emit visibility info for declarations
902*0a6a1f1dSLionel Sambuc for (const Function &F : M) {
903f4a2713aSLionel Sambuc if (!F.isDeclaration())
904f4a2713aSLionel Sambuc continue;
905f4a2713aSLionel Sambuc GlobalValue::VisibilityTypes V = F.getVisibility();
906f4a2713aSLionel Sambuc if (V == GlobalValue::DefaultVisibility)
907f4a2713aSLionel Sambuc continue;
908f4a2713aSLionel Sambuc
909f4a2713aSLionel Sambuc MCSymbol *Name = getSymbol(&F);
910f4a2713aSLionel Sambuc EmitVisibility(Name, V, false);
911f4a2713aSLionel Sambuc }
912f4a2713aSLionel Sambuc
913*0a6a1f1dSLionel Sambuc // Get information about jump-instruction tables to print.
914*0a6a1f1dSLionel Sambuc JumpInstrTableInfo *JITI = getAnalysisIfAvailable<JumpInstrTableInfo>();
915*0a6a1f1dSLionel Sambuc
916*0a6a1f1dSLionel Sambuc if (JITI && !JITI->getTables().empty()) {
917*0a6a1f1dSLionel Sambuc unsigned Arch = Triple(getTargetTriple()).getArch();
918*0a6a1f1dSLionel Sambuc bool IsThumb = (Arch == Triple::thumb || Arch == Triple::thumbeb);
919*0a6a1f1dSLionel Sambuc MCInst TrapInst;
920*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getInstrInfo()->getTrap(TrapInst);
921*0a6a1f1dSLionel Sambuc unsigned LogAlignment = llvm::Log2_64(JITI->entryByteAlignment());
922*0a6a1f1dSLionel Sambuc
923*0a6a1f1dSLionel Sambuc // Emit the right section for these functions.
924*0a6a1f1dSLionel Sambuc OutStreamer.SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
925*0a6a1f1dSLionel Sambuc for (const auto &KV : JITI->getTables()) {
926*0a6a1f1dSLionel Sambuc uint64_t Count = 0;
927*0a6a1f1dSLionel Sambuc for (const auto &FunPair : KV.second) {
928*0a6a1f1dSLionel Sambuc // Emit the function labels to make this be a function entry point.
929*0a6a1f1dSLionel Sambuc MCSymbol *FunSym =
930*0a6a1f1dSLionel Sambuc OutContext.GetOrCreateSymbol(FunPair.second->getName());
931*0a6a1f1dSLionel Sambuc EmitAlignment(LogAlignment);
932*0a6a1f1dSLionel Sambuc if (IsThumb)
933*0a6a1f1dSLionel Sambuc OutStreamer.EmitThumbFunc(FunSym);
934*0a6a1f1dSLionel Sambuc if (MAI->hasDotTypeDotSizeDirective())
935*0a6a1f1dSLionel Sambuc OutStreamer.EmitSymbolAttribute(FunSym, MCSA_ELF_TypeFunction);
936*0a6a1f1dSLionel Sambuc OutStreamer.EmitLabel(FunSym);
937*0a6a1f1dSLionel Sambuc
938*0a6a1f1dSLionel Sambuc // Emit the jump instruction to transfer control to the original
939*0a6a1f1dSLionel Sambuc // function.
940*0a6a1f1dSLionel Sambuc MCInst JumpToFun;
941*0a6a1f1dSLionel Sambuc MCSymbol *TargetSymbol =
942*0a6a1f1dSLionel Sambuc OutContext.GetOrCreateSymbol(FunPair.first->getName());
943*0a6a1f1dSLionel Sambuc const MCSymbolRefExpr *TargetSymRef =
944*0a6a1f1dSLionel Sambuc MCSymbolRefExpr::Create(TargetSymbol, MCSymbolRefExpr::VK_PLT,
945*0a6a1f1dSLionel Sambuc OutContext);
946*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getInstrInfo()->getUnconditionalBranch(
947*0a6a1f1dSLionel Sambuc JumpToFun, TargetSymRef);
948*0a6a1f1dSLionel Sambuc OutStreamer.EmitInstruction(JumpToFun, getSubtargetInfo());
949*0a6a1f1dSLionel Sambuc ++Count;
950*0a6a1f1dSLionel Sambuc }
951*0a6a1f1dSLionel Sambuc
952*0a6a1f1dSLionel Sambuc // Emit enough padding instructions to fill up to the next power of two.
953*0a6a1f1dSLionel Sambuc uint64_t Remaining = NextPowerOf2(Count) - Count;
954*0a6a1f1dSLionel Sambuc for (uint64_t C = 0; C < Remaining; ++C) {
955*0a6a1f1dSLionel Sambuc EmitAlignment(LogAlignment);
956*0a6a1f1dSLionel Sambuc OutStreamer.EmitInstruction(TrapInst, getSubtargetInfo());
957*0a6a1f1dSLionel Sambuc }
958*0a6a1f1dSLionel Sambuc
959*0a6a1f1dSLionel Sambuc }
960*0a6a1f1dSLionel Sambuc }
961*0a6a1f1dSLionel Sambuc
962f4a2713aSLionel Sambuc // Emit module flags.
963f4a2713aSLionel Sambuc SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
964f4a2713aSLionel Sambuc M.getModuleFlagsMetadata(ModuleFlags);
965f4a2713aSLionel Sambuc if (!ModuleFlags.empty())
966*0a6a1f1dSLionel Sambuc getObjFileLowering().emitModuleFlags(OutStreamer, ModuleFlags, *Mang, TM);
967f4a2713aSLionel Sambuc
968f4a2713aSLionel Sambuc // Make sure we wrote out everything we need.
969f4a2713aSLionel Sambuc OutStreamer.Flush();
970f4a2713aSLionel Sambuc
971f4a2713aSLionel Sambuc // Finalize debug and EH information.
972*0a6a1f1dSLionel Sambuc for (const HandlerInfo &HI : Handlers) {
973*0a6a1f1dSLionel Sambuc NamedRegionTimer T(HI.TimerName, HI.TimerGroupName,
974*0a6a1f1dSLionel Sambuc TimePassesIsEnabled);
975*0a6a1f1dSLionel Sambuc HI.Handler->endModule();
976*0a6a1f1dSLionel Sambuc delete HI.Handler;
977f4a2713aSLionel Sambuc }
978*0a6a1f1dSLionel Sambuc Handlers.clear();
979*0a6a1f1dSLionel Sambuc DD = nullptr;
980f4a2713aSLionel Sambuc
981f4a2713aSLionel Sambuc // If the target wants to know about weak references, print them all.
982f4a2713aSLionel Sambuc if (MAI->getWeakRefDirective()) {
983f4a2713aSLionel Sambuc // FIXME: This is not lazy, it would be nice to only print weak references
984f4a2713aSLionel Sambuc // to stuff that is actually used. Note that doing so would require targets
985f4a2713aSLionel Sambuc // to notice uses in operands (due to constant exprs etc). This should
986f4a2713aSLionel Sambuc // happen with the MC stuff eventually.
987f4a2713aSLionel Sambuc
988f4a2713aSLionel Sambuc // Print out module-level global variables here.
989*0a6a1f1dSLionel Sambuc for (const auto &G : M.globals()) {
990*0a6a1f1dSLionel Sambuc if (!G.hasExternalWeakLinkage())
991*0a6a1f1dSLionel Sambuc continue;
992*0a6a1f1dSLionel Sambuc OutStreamer.EmitSymbolAttribute(getSymbol(&G), MCSA_WeakReference);
993f4a2713aSLionel Sambuc }
994f4a2713aSLionel Sambuc
995*0a6a1f1dSLionel Sambuc for (const auto &F : M) {
996*0a6a1f1dSLionel Sambuc if (!F.hasExternalWeakLinkage())
997*0a6a1f1dSLionel Sambuc continue;
998*0a6a1f1dSLionel Sambuc OutStreamer.EmitSymbolAttribute(getSymbol(&F), MCSA_WeakReference);
999f4a2713aSLionel Sambuc }
1000f4a2713aSLionel Sambuc }
1001f4a2713aSLionel Sambuc
1002f4a2713aSLionel Sambuc OutStreamer.AddBlankLine();
1003*0a6a1f1dSLionel Sambuc for (const auto &Alias : M.aliases()) {
1004*0a6a1f1dSLionel Sambuc MCSymbol *Name = getSymbol(&Alias);
1005f4a2713aSLionel Sambuc
1006*0a6a1f1dSLionel Sambuc if (Alias.hasExternalLinkage() || !MAI->getWeakRefDirective())
1007f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(Name, MCSA_Global);
1008*0a6a1f1dSLionel Sambuc else if (Alias.hasWeakLinkage() || Alias.hasLinkOnceLinkage())
1009f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(Name, MCSA_WeakReference);
1010f4a2713aSLionel Sambuc else
1011*0a6a1f1dSLionel Sambuc assert(Alias.hasLocalLinkage() && "Invalid alias linkage");
1012f4a2713aSLionel Sambuc
1013*0a6a1f1dSLionel Sambuc EmitVisibility(Name, Alias.getVisibility());
1014f4a2713aSLionel Sambuc
1015f4a2713aSLionel Sambuc // Emit the directives as assignments aka .set:
1016*0a6a1f1dSLionel Sambuc OutStreamer.EmitAssignment(Name, lowerConstant(Alias.getAliasee()));
1017f4a2713aSLionel Sambuc }
1018f4a2713aSLionel Sambuc
1019f4a2713aSLionel Sambuc GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
1020f4a2713aSLionel Sambuc assert(MI && "AsmPrinter didn't require GCModuleInfo?");
1021f4a2713aSLionel Sambuc for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
1022*0a6a1f1dSLionel Sambuc if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(**--I))
1023*0a6a1f1dSLionel Sambuc MP->finishAssembly(M, *MI, *this);
1024f4a2713aSLionel Sambuc
1025f4a2713aSLionel Sambuc // Emit llvm.ident metadata in an '.ident' directive.
1026f4a2713aSLionel Sambuc EmitModuleIdents(M);
1027f4a2713aSLionel Sambuc
1028*0a6a1f1dSLionel Sambuc // Emit __morestack address if needed for indirect calls.
1029*0a6a1f1dSLionel Sambuc if (MMI->usesMorestackAddr()) {
1030*0a6a1f1dSLionel Sambuc const MCSection *ReadOnlySection =
1031*0a6a1f1dSLionel Sambuc getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly(),
1032*0a6a1f1dSLionel Sambuc /*C=*/nullptr);
1033*0a6a1f1dSLionel Sambuc OutStreamer.SwitchSection(ReadOnlySection);
1034*0a6a1f1dSLionel Sambuc
1035*0a6a1f1dSLionel Sambuc MCSymbol *AddrSymbol =
1036*0a6a1f1dSLionel Sambuc OutContext.GetOrCreateSymbol(StringRef("__morestack_addr"));
1037*0a6a1f1dSLionel Sambuc OutStreamer.EmitLabel(AddrSymbol);
1038*0a6a1f1dSLionel Sambuc
1039*0a6a1f1dSLionel Sambuc const DataLayout &DL = *TM.getSubtargetImpl()->getDataLayout();
1040*0a6a1f1dSLionel Sambuc unsigned PtrSize = DL.getPointerSize(0);
1041*0a6a1f1dSLionel Sambuc OutStreamer.EmitSymbolValue(GetExternalSymbolSymbol("__morestack"),
1042*0a6a1f1dSLionel Sambuc PtrSize);
1043*0a6a1f1dSLionel Sambuc }
1044*0a6a1f1dSLionel Sambuc
1045f4a2713aSLionel Sambuc // If we don't have any trampolines, then we don't require stack memory
1046f4a2713aSLionel Sambuc // to be executable. Some targets have a directive to declare this.
1047f4a2713aSLionel Sambuc Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
1048f4a2713aSLionel Sambuc if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
1049f4a2713aSLionel Sambuc if (const MCSection *S = MAI->getNonexecutableStackSection(OutContext))
1050f4a2713aSLionel Sambuc OutStreamer.SwitchSection(S);
1051f4a2713aSLionel Sambuc
1052f4a2713aSLionel Sambuc // Allow the target to emit any magic that it wants at the end of the file,
1053f4a2713aSLionel Sambuc // after everything else has gone out.
1054f4a2713aSLionel Sambuc EmitEndOfAsmFile(M);
1055f4a2713aSLionel Sambuc
1056*0a6a1f1dSLionel Sambuc delete Mang; Mang = nullptr;
1057*0a6a1f1dSLionel Sambuc MMI = nullptr;
1058f4a2713aSLionel Sambuc
1059f4a2713aSLionel Sambuc OutStreamer.Finish();
1060f4a2713aSLionel Sambuc OutStreamer.reset();
1061f4a2713aSLionel Sambuc
1062f4a2713aSLionel Sambuc return false;
1063f4a2713aSLionel Sambuc }
1064f4a2713aSLionel Sambuc
SetupMachineFunction(MachineFunction & MF)1065f4a2713aSLionel Sambuc void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
1066f4a2713aSLionel Sambuc this->MF = &MF;
1067f4a2713aSLionel Sambuc // Get the function symbol.
1068f4a2713aSLionel Sambuc CurrentFnSym = getSymbol(MF.getFunction());
1069f4a2713aSLionel Sambuc CurrentFnSymForSize = CurrentFnSym;
1070f4a2713aSLionel Sambuc
1071f4a2713aSLionel Sambuc if (isVerbose())
1072f4a2713aSLionel Sambuc LI = &getAnalysis<MachineLoopInfo>();
1073f4a2713aSLionel Sambuc }
1074f4a2713aSLionel Sambuc
1075f4a2713aSLionel Sambuc namespace {
1076f4a2713aSLionel Sambuc // SectionCPs - Keep track the alignment, constpool entries per Section.
1077f4a2713aSLionel Sambuc struct SectionCPs {
1078f4a2713aSLionel Sambuc const MCSection *S;
1079f4a2713aSLionel Sambuc unsigned Alignment;
1080f4a2713aSLionel Sambuc SmallVector<unsigned, 4> CPEs;
SectionCPs__anond5aaca400111::SectionCPs1081f4a2713aSLionel Sambuc SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
1082f4a2713aSLionel Sambuc };
1083f4a2713aSLionel Sambuc }
1084f4a2713aSLionel Sambuc
1085f4a2713aSLionel Sambuc /// EmitConstantPool - Print to the current output stream assembly
1086f4a2713aSLionel Sambuc /// representations of the constants in the constant pool MCP. This is
1087f4a2713aSLionel Sambuc /// used to print out constants which have been "spilled to memory" by
1088f4a2713aSLionel Sambuc /// the code generator.
1089f4a2713aSLionel Sambuc ///
EmitConstantPool()1090f4a2713aSLionel Sambuc void AsmPrinter::EmitConstantPool() {
1091f4a2713aSLionel Sambuc const MachineConstantPool *MCP = MF->getConstantPool();
1092f4a2713aSLionel Sambuc const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
1093f4a2713aSLionel Sambuc if (CP.empty()) return;
1094f4a2713aSLionel Sambuc
1095f4a2713aSLionel Sambuc // Calculate sections for constant pool entries. We collect entries to go into
1096f4a2713aSLionel Sambuc // the same section together to reduce amount of section switch statements.
1097f4a2713aSLionel Sambuc SmallVector<SectionCPs, 4> CPSections;
1098f4a2713aSLionel Sambuc for (unsigned i = 0, e = CP.size(); i != e; ++i) {
1099f4a2713aSLionel Sambuc const MachineConstantPoolEntry &CPE = CP[i];
1100f4a2713aSLionel Sambuc unsigned Align = CPE.getAlignment();
1101f4a2713aSLionel Sambuc
1102*0a6a1f1dSLionel Sambuc SectionKind Kind =
1103*0a6a1f1dSLionel Sambuc CPE.getSectionKind(TM.getSubtargetImpl()->getDataLayout());
1104f4a2713aSLionel Sambuc
1105*0a6a1f1dSLionel Sambuc const Constant *C = nullptr;
1106*0a6a1f1dSLionel Sambuc if (!CPE.isMachineConstantPoolEntry())
1107*0a6a1f1dSLionel Sambuc C = CPE.Val.ConstVal;
1108*0a6a1f1dSLionel Sambuc
1109*0a6a1f1dSLionel Sambuc const MCSection *S = getObjFileLowering().getSectionForConstant(Kind, C);
1110f4a2713aSLionel Sambuc
1111f4a2713aSLionel Sambuc // The number of sections are small, just do a linear search from the
1112f4a2713aSLionel Sambuc // last section to the first.
1113f4a2713aSLionel Sambuc bool Found = false;
1114f4a2713aSLionel Sambuc unsigned SecIdx = CPSections.size();
1115f4a2713aSLionel Sambuc while (SecIdx != 0) {
1116f4a2713aSLionel Sambuc if (CPSections[--SecIdx].S == S) {
1117f4a2713aSLionel Sambuc Found = true;
1118f4a2713aSLionel Sambuc break;
1119f4a2713aSLionel Sambuc }
1120f4a2713aSLionel Sambuc }
1121f4a2713aSLionel Sambuc if (!Found) {
1122f4a2713aSLionel Sambuc SecIdx = CPSections.size();
1123f4a2713aSLionel Sambuc CPSections.push_back(SectionCPs(S, Align));
1124f4a2713aSLionel Sambuc }
1125f4a2713aSLionel Sambuc
1126f4a2713aSLionel Sambuc if (Align > CPSections[SecIdx].Alignment)
1127f4a2713aSLionel Sambuc CPSections[SecIdx].Alignment = Align;
1128f4a2713aSLionel Sambuc CPSections[SecIdx].CPEs.push_back(i);
1129f4a2713aSLionel Sambuc }
1130f4a2713aSLionel Sambuc
1131f4a2713aSLionel Sambuc // Now print stuff into the calculated sections.
1132*0a6a1f1dSLionel Sambuc const MCSection *CurSection = nullptr;
1133f4a2713aSLionel Sambuc unsigned Offset = 0;
1134*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
1135f4a2713aSLionel Sambuc for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
1136f4a2713aSLionel Sambuc unsigned CPI = CPSections[i].CPEs[j];
1137*0a6a1f1dSLionel Sambuc MCSymbol *Sym = GetCPISymbol(CPI);
1138*0a6a1f1dSLionel Sambuc if (!Sym->isUndefined())
1139*0a6a1f1dSLionel Sambuc continue;
1140*0a6a1f1dSLionel Sambuc
1141*0a6a1f1dSLionel Sambuc if (CurSection != CPSections[i].S) {
1142*0a6a1f1dSLionel Sambuc OutStreamer.SwitchSection(CPSections[i].S);
1143*0a6a1f1dSLionel Sambuc EmitAlignment(Log2_32(CPSections[i].Alignment));
1144*0a6a1f1dSLionel Sambuc CurSection = CPSections[i].S;
1145*0a6a1f1dSLionel Sambuc Offset = 0;
1146*0a6a1f1dSLionel Sambuc }
1147*0a6a1f1dSLionel Sambuc
1148f4a2713aSLionel Sambuc MachineConstantPoolEntry CPE = CP[CPI];
1149f4a2713aSLionel Sambuc
1150f4a2713aSLionel Sambuc // Emit inter-object padding for alignment.
1151f4a2713aSLionel Sambuc unsigned AlignMask = CPE.getAlignment() - 1;
1152f4a2713aSLionel Sambuc unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
1153f4a2713aSLionel Sambuc OutStreamer.EmitZeros(NewOffset - Offset);
1154f4a2713aSLionel Sambuc
1155f4a2713aSLionel Sambuc Type *Ty = CPE.getType();
1156*0a6a1f1dSLionel Sambuc Offset = NewOffset +
1157*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(Ty);
1158f4a2713aSLionel Sambuc
1159*0a6a1f1dSLionel Sambuc OutStreamer.EmitLabel(Sym);
1160f4a2713aSLionel Sambuc if (CPE.isMachineConstantPoolEntry())
1161f4a2713aSLionel Sambuc EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
1162f4a2713aSLionel Sambuc else
1163f4a2713aSLionel Sambuc EmitGlobalConstant(CPE.Val.ConstVal);
1164f4a2713aSLionel Sambuc }
1165f4a2713aSLionel Sambuc }
1166f4a2713aSLionel Sambuc }
1167f4a2713aSLionel Sambuc
1168f4a2713aSLionel Sambuc /// EmitJumpTableInfo - Print assembly representations of the jump tables used
1169f4a2713aSLionel Sambuc /// by the current function to the current output stream.
1170f4a2713aSLionel Sambuc ///
EmitJumpTableInfo()1171f4a2713aSLionel Sambuc void AsmPrinter::EmitJumpTableInfo() {
1172*0a6a1f1dSLionel Sambuc const DataLayout *DL = MF->getSubtarget().getDataLayout();
1173f4a2713aSLionel Sambuc const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1174*0a6a1f1dSLionel Sambuc if (!MJTI) return;
1175f4a2713aSLionel Sambuc if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) return;
1176f4a2713aSLionel Sambuc const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1177f4a2713aSLionel Sambuc if (JT.empty()) return;
1178f4a2713aSLionel Sambuc
1179f4a2713aSLionel Sambuc // Pick the directive to use to print the jump table entries, and switch to
1180f4a2713aSLionel Sambuc // the appropriate section.
1181f4a2713aSLionel Sambuc const Function *F = MF->getFunction();
1182f4a2713aSLionel Sambuc bool JTInDiffSection = false;
1183f4a2713aSLionel Sambuc if (// In PIC mode, we need to emit the jump table to the same section as the
1184f4a2713aSLionel Sambuc // function body itself, otherwise the label differences won't make sense.
1185f4a2713aSLionel Sambuc // FIXME: Need a better predicate for this: what about custom entries?
1186f4a2713aSLionel Sambuc MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 ||
1187f4a2713aSLionel Sambuc // We should also do if the section name is NULL or function is declared
1188f4a2713aSLionel Sambuc // in discardable section
1189f4a2713aSLionel Sambuc // FIXME: this isn't the right predicate, should be based on the MCSection
1190f4a2713aSLionel Sambuc // for the function.
1191f4a2713aSLionel Sambuc F->isWeakForLinker()) {
1192*0a6a1f1dSLionel Sambuc OutStreamer.SwitchSection(
1193*0a6a1f1dSLionel Sambuc getObjFileLowering().SectionForGlobal(F, *Mang, TM));
1194f4a2713aSLionel Sambuc } else {
1195f4a2713aSLionel Sambuc // Otherwise, drop it in the readonly section.
1196f4a2713aSLionel Sambuc const MCSection *ReadOnlySection =
1197*0a6a1f1dSLionel Sambuc getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly(),
1198*0a6a1f1dSLionel Sambuc /*C=*/nullptr);
1199f4a2713aSLionel Sambuc OutStreamer.SwitchSection(ReadOnlySection);
1200f4a2713aSLionel Sambuc JTInDiffSection = true;
1201f4a2713aSLionel Sambuc }
1202f4a2713aSLionel Sambuc
1203*0a6a1f1dSLionel Sambuc EmitAlignment(Log2_32(
1204*0a6a1f1dSLionel Sambuc MJTI->getEntryAlignment(*TM.getSubtargetImpl()->getDataLayout())));
1205f4a2713aSLionel Sambuc
1206f4a2713aSLionel Sambuc // Jump tables in code sections are marked with a data_region directive
1207f4a2713aSLionel Sambuc // where that's supported.
1208f4a2713aSLionel Sambuc if (!JTInDiffSection)
1209f4a2713aSLionel Sambuc OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
1210f4a2713aSLionel Sambuc
1211f4a2713aSLionel Sambuc for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) {
1212f4a2713aSLionel Sambuc const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1213f4a2713aSLionel Sambuc
1214f4a2713aSLionel Sambuc // If this jump table was deleted, ignore it.
1215f4a2713aSLionel Sambuc if (JTBBs.empty()) continue;
1216f4a2713aSLionel Sambuc
1217*0a6a1f1dSLionel Sambuc // For the EK_LabelDifference32 entry, if using .set avoids a relocation,
1218*0a6a1f1dSLionel Sambuc /// emit a .set directive for each unique entry.
1219f4a2713aSLionel Sambuc if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 &&
1220*0a6a1f1dSLionel Sambuc MAI->doesSetDirectiveSuppressesReloc()) {
1221f4a2713aSLionel Sambuc SmallPtrSet<const MachineBasicBlock*, 16> EmittedSets;
1222*0a6a1f1dSLionel Sambuc const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
1223f4a2713aSLionel Sambuc const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext);
1224f4a2713aSLionel Sambuc for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
1225f4a2713aSLionel Sambuc const MachineBasicBlock *MBB = JTBBs[ii];
1226*0a6a1f1dSLionel Sambuc if (!EmittedSets.insert(MBB).second)
1227*0a6a1f1dSLionel Sambuc continue;
1228f4a2713aSLionel Sambuc
1229f4a2713aSLionel Sambuc // .set LJTSet, LBB32-base
1230f4a2713aSLionel Sambuc const MCExpr *LHS =
1231f4a2713aSLionel Sambuc MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
1232f4a2713aSLionel Sambuc OutStreamer.EmitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()),
1233f4a2713aSLionel Sambuc MCBinaryExpr::CreateSub(LHS, Base, OutContext));
1234f4a2713aSLionel Sambuc }
1235f4a2713aSLionel Sambuc }
1236f4a2713aSLionel Sambuc
1237f4a2713aSLionel Sambuc // On some targets (e.g. Darwin) we want to emit two consecutive labels
1238f4a2713aSLionel Sambuc // before each jump table. The first label is never referenced, but tells
1239f4a2713aSLionel Sambuc // the assembler and linker the extents of the jump table object. The
1240f4a2713aSLionel Sambuc // second label is actually referenced by the code.
1241*0a6a1f1dSLionel Sambuc if (JTInDiffSection && DL->hasLinkerPrivateGlobalPrefix())
1242f4a2713aSLionel Sambuc // FIXME: This doesn't have to have any specific name, just any randomly
1243f4a2713aSLionel Sambuc // named and numbered 'l' label would work. Simplify GetJTISymbol.
1244f4a2713aSLionel Sambuc OutStreamer.EmitLabel(GetJTISymbol(JTI, true));
1245f4a2713aSLionel Sambuc
1246f4a2713aSLionel Sambuc OutStreamer.EmitLabel(GetJTISymbol(JTI));
1247f4a2713aSLionel Sambuc
1248f4a2713aSLionel Sambuc for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
1249f4a2713aSLionel Sambuc EmitJumpTableEntry(MJTI, JTBBs[ii], JTI);
1250f4a2713aSLionel Sambuc }
1251f4a2713aSLionel Sambuc if (!JTInDiffSection)
1252f4a2713aSLionel Sambuc OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1253f4a2713aSLionel Sambuc }
1254f4a2713aSLionel Sambuc
1255f4a2713aSLionel Sambuc /// EmitJumpTableEntry - Emit a jump table entry for the specified MBB to the
1256f4a2713aSLionel Sambuc /// current stream.
EmitJumpTableEntry(const MachineJumpTableInfo * MJTI,const MachineBasicBlock * MBB,unsigned UID) const1257f4a2713aSLionel Sambuc void AsmPrinter::EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
1258f4a2713aSLionel Sambuc const MachineBasicBlock *MBB,
1259f4a2713aSLionel Sambuc unsigned UID) const {
1260f4a2713aSLionel Sambuc assert(MBB && MBB->getNumber() >= 0 && "Invalid basic block");
1261*0a6a1f1dSLionel Sambuc const MCExpr *Value = nullptr;
1262f4a2713aSLionel Sambuc switch (MJTI->getEntryKind()) {
1263f4a2713aSLionel Sambuc case MachineJumpTableInfo::EK_Inline:
1264f4a2713aSLionel Sambuc llvm_unreachable("Cannot emit EK_Inline jump table entry");
1265f4a2713aSLionel Sambuc case MachineJumpTableInfo::EK_Custom32:
1266*0a6a1f1dSLionel Sambuc Value =
1267*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getTargetLowering()->LowerCustomJumpTableEntry(
1268*0a6a1f1dSLionel Sambuc MJTI, MBB, UID, OutContext);
1269f4a2713aSLionel Sambuc break;
1270f4a2713aSLionel Sambuc case MachineJumpTableInfo::EK_BlockAddress:
1271f4a2713aSLionel Sambuc // EK_BlockAddress - Each entry is a plain address of block, e.g.:
1272f4a2713aSLionel Sambuc // .word LBB123
1273f4a2713aSLionel Sambuc Value = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
1274f4a2713aSLionel Sambuc break;
1275f4a2713aSLionel Sambuc case MachineJumpTableInfo::EK_GPRel32BlockAddress: {
1276f4a2713aSLionel Sambuc // EK_GPRel32BlockAddress - Each entry is an address of block, encoded
1277f4a2713aSLionel Sambuc // with a relocation as gp-relative, e.g.:
1278f4a2713aSLionel Sambuc // .gprel32 LBB123
1279f4a2713aSLionel Sambuc MCSymbol *MBBSym = MBB->getSymbol();
1280f4a2713aSLionel Sambuc OutStreamer.EmitGPRel32Value(MCSymbolRefExpr::Create(MBBSym, OutContext));
1281f4a2713aSLionel Sambuc return;
1282f4a2713aSLionel Sambuc }
1283f4a2713aSLionel Sambuc
1284f4a2713aSLionel Sambuc case MachineJumpTableInfo::EK_GPRel64BlockAddress: {
1285f4a2713aSLionel Sambuc // EK_GPRel64BlockAddress - Each entry is an address of block, encoded
1286f4a2713aSLionel Sambuc // with a relocation as gp-relative, e.g.:
1287f4a2713aSLionel Sambuc // .gpdword LBB123
1288f4a2713aSLionel Sambuc MCSymbol *MBBSym = MBB->getSymbol();
1289f4a2713aSLionel Sambuc OutStreamer.EmitGPRel64Value(MCSymbolRefExpr::Create(MBBSym, OutContext));
1290f4a2713aSLionel Sambuc return;
1291f4a2713aSLionel Sambuc }
1292f4a2713aSLionel Sambuc
1293f4a2713aSLionel Sambuc case MachineJumpTableInfo::EK_LabelDifference32: {
1294*0a6a1f1dSLionel Sambuc // Each entry is the address of the block minus the address of the jump
1295*0a6a1f1dSLionel Sambuc // table. This is used for PIC jump tables where gprel32 is not supported.
1296*0a6a1f1dSLionel Sambuc // e.g.:
1297f4a2713aSLionel Sambuc // .word LBB123 - LJTI1_2
1298*0a6a1f1dSLionel Sambuc // If the .set directive avoids relocations, this is emitted as:
1299f4a2713aSLionel Sambuc // .set L4_5_set_123, LBB123 - LJTI1_2
1300f4a2713aSLionel Sambuc // .word L4_5_set_123
1301*0a6a1f1dSLionel Sambuc if (MAI->doesSetDirectiveSuppressesReloc()) {
1302f4a2713aSLionel Sambuc Value = MCSymbolRefExpr::Create(GetJTSetSymbol(UID, MBB->getNumber()),
1303f4a2713aSLionel Sambuc OutContext);
1304f4a2713aSLionel Sambuc break;
1305f4a2713aSLionel Sambuc }
1306f4a2713aSLionel Sambuc Value = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
1307*0a6a1f1dSLionel Sambuc const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
1308*0a6a1f1dSLionel Sambuc const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF, UID, OutContext);
1309*0a6a1f1dSLionel Sambuc Value = MCBinaryExpr::CreateSub(Value, Base, OutContext);
1310f4a2713aSLionel Sambuc break;
1311f4a2713aSLionel Sambuc }
1312f4a2713aSLionel Sambuc }
1313f4a2713aSLionel Sambuc
1314f4a2713aSLionel Sambuc assert(Value && "Unknown entry kind!");
1315f4a2713aSLionel Sambuc
1316*0a6a1f1dSLionel Sambuc unsigned EntrySize =
1317*0a6a1f1dSLionel Sambuc MJTI->getEntrySize(*TM.getSubtargetImpl()->getDataLayout());
1318f4a2713aSLionel Sambuc OutStreamer.EmitValue(Value, EntrySize);
1319f4a2713aSLionel Sambuc }
1320f4a2713aSLionel Sambuc
1321f4a2713aSLionel Sambuc
1322f4a2713aSLionel Sambuc /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
1323f4a2713aSLionel Sambuc /// special global used by LLVM. If so, emit it and return true, otherwise
1324f4a2713aSLionel Sambuc /// do nothing and return false.
EmitSpecialLLVMGlobal(const GlobalVariable * GV)1325f4a2713aSLionel Sambuc bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
1326f4a2713aSLionel Sambuc if (GV->getName() == "llvm.used") {
1327f4a2713aSLionel Sambuc if (MAI->hasNoDeadStrip()) // No need to emit this at all.
1328f4a2713aSLionel Sambuc EmitLLVMUsedList(cast<ConstantArray>(GV->getInitializer()));
1329f4a2713aSLionel Sambuc return true;
1330f4a2713aSLionel Sambuc }
1331f4a2713aSLionel Sambuc
1332f4a2713aSLionel Sambuc // Ignore debug and non-emitted data. This handles llvm.compiler.used.
1333*0a6a1f1dSLionel Sambuc if (StringRef(GV->getSection()) == "llvm.metadata" ||
1334f4a2713aSLionel Sambuc GV->hasAvailableExternallyLinkage())
1335f4a2713aSLionel Sambuc return true;
1336f4a2713aSLionel Sambuc
1337f4a2713aSLionel Sambuc if (!GV->hasAppendingLinkage()) return false;
1338f4a2713aSLionel Sambuc
1339f4a2713aSLionel Sambuc assert(GV->hasInitializer() && "Not a special LLVM global!");
1340f4a2713aSLionel Sambuc
1341f4a2713aSLionel Sambuc if (GV->getName() == "llvm.global_ctors") {
1342f4a2713aSLionel Sambuc EmitXXStructorList(GV->getInitializer(), /* isCtor */ true);
1343f4a2713aSLionel Sambuc
1344f4a2713aSLionel Sambuc if (TM.getRelocationModel() == Reloc::Static &&
1345f4a2713aSLionel Sambuc MAI->hasStaticCtorDtorReferenceInStaticMode()) {
1346f4a2713aSLionel Sambuc StringRef Sym(".constructors_used");
1347f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(OutContext.GetOrCreateSymbol(Sym),
1348f4a2713aSLionel Sambuc MCSA_Reference);
1349f4a2713aSLionel Sambuc }
1350f4a2713aSLionel Sambuc return true;
1351f4a2713aSLionel Sambuc }
1352f4a2713aSLionel Sambuc
1353f4a2713aSLionel Sambuc if (GV->getName() == "llvm.global_dtors") {
1354f4a2713aSLionel Sambuc EmitXXStructorList(GV->getInitializer(), /* isCtor */ false);
1355f4a2713aSLionel Sambuc
1356f4a2713aSLionel Sambuc if (TM.getRelocationModel() == Reloc::Static &&
1357f4a2713aSLionel Sambuc MAI->hasStaticCtorDtorReferenceInStaticMode()) {
1358f4a2713aSLionel Sambuc StringRef Sym(".destructors_used");
1359f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(OutContext.GetOrCreateSymbol(Sym),
1360f4a2713aSLionel Sambuc MCSA_Reference);
1361f4a2713aSLionel Sambuc }
1362f4a2713aSLionel Sambuc return true;
1363f4a2713aSLionel Sambuc }
1364f4a2713aSLionel Sambuc
1365f4a2713aSLionel Sambuc return false;
1366f4a2713aSLionel Sambuc }
1367f4a2713aSLionel Sambuc
1368f4a2713aSLionel Sambuc /// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
1369f4a2713aSLionel Sambuc /// global in the specified llvm.used list for which emitUsedDirectiveFor
1370f4a2713aSLionel Sambuc /// is true, as being used with this directive.
EmitLLVMUsedList(const ConstantArray * InitList)1371f4a2713aSLionel Sambuc void AsmPrinter::EmitLLVMUsedList(const ConstantArray *InitList) {
1372f4a2713aSLionel Sambuc // Should be an array of 'i8*'.
1373f4a2713aSLionel Sambuc for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1374f4a2713aSLionel Sambuc const GlobalValue *GV =
1375f4a2713aSLionel Sambuc dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
1376*0a6a1f1dSLionel Sambuc if (GV)
1377f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(getSymbol(GV), MCSA_NoDeadStrip);
1378f4a2713aSLionel Sambuc }
1379f4a2713aSLionel Sambuc }
1380f4a2713aSLionel Sambuc
1381*0a6a1f1dSLionel Sambuc namespace {
1382*0a6a1f1dSLionel Sambuc struct Structor {
Structor__anond5aaca400211::Structor1383*0a6a1f1dSLionel Sambuc Structor() : Priority(0), Func(nullptr), ComdatKey(nullptr) {}
1384*0a6a1f1dSLionel Sambuc int Priority;
1385*0a6a1f1dSLionel Sambuc llvm::Constant *Func;
1386*0a6a1f1dSLionel Sambuc llvm::GlobalValue *ComdatKey;
1387*0a6a1f1dSLionel Sambuc };
1388*0a6a1f1dSLionel Sambuc } // end namespace
1389*0a6a1f1dSLionel Sambuc
1390f4a2713aSLionel Sambuc /// EmitXXStructorList - Emit the ctor or dtor list taking into account the init
1391f4a2713aSLionel Sambuc /// priority.
EmitXXStructorList(const Constant * List,bool isCtor)1392f4a2713aSLionel Sambuc void AsmPrinter::EmitXXStructorList(const Constant *List, bool isCtor) {
1393f4a2713aSLionel Sambuc // Should be an array of '{ int, void ()* }' structs. The first value is the
1394f4a2713aSLionel Sambuc // init priority.
1395f4a2713aSLionel Sambuc if (!isa<ConstantArray>(List)) return;
1396f4a2713aSLionel Sambuc
1397f4a2713aSLionel Sambuc // Sanity check the structors list.
1398f4a2713aSLionel Sambuc const ConstantArray *InitList = dyn_cast<ConstantArray>(List);
1399f4a2713aSLionel Sambuc if (!InitList) return; // Not an array!
1400f4a2713aSLionel Sambuc StructType *ETy = dyn_cast<StructType>(InitList->getType()->getElementType());
1401*0a6a1f1dSLionel Sambuc // FIXME: Only allow the 3-field form in LLVM 4.0.
1402*0a6a1f1dSLionel Sambuc if (!ETy || ETy->getNumElements() < 2 || ETy->getNumElements() > 3)
1403*0a6a1f1dSLionel Sambuc return; // Not an array of two or three elements!
1404f4a2713aSLionel Sambuc if (!isa<IntegerType>(ETy->getTypeAtIndex(0U)) ||
1405f4a2713aSLionel Sambuc !isa<PointerType>(ETy->getTypeAtIndex(1U))) return; // Not (int, ptr).
1406*0a6a1f1dSLionel Sambuc if (ETy->getNumElements() == 3 && !isa<PointerType>(ETy->getTypeAtIndex(2U)))
1407*0a6a1f1dSLionel Sambuc return; // Not (int, ptr, ptr).
1408f4a2713aSLionel Sambuc
1409f4a2713aSLionel Sambuc // Gather the structors in a form that's convenient for sorting by priority.
1410f4a2713aSLionel Sambuc SmallVector<Structor, 8> Structors;
1411*0a6a1f1dSLionel Sambuc for (Value *O : InitList->operands()) {
1412*0a6a1f1dSLionel Sambuc ConstantStruct *CS = dyn_cast<ConstantStruct>(O);
1413f4a2713aSLionel Sambuc if (!CS) continue; // Malformed.
1414f4a2713aSLionel Sambuc if (CS->getOperand(1)->isNullValue())
1415f4a2713aSLionel Sambuc break; // Found a null terminator, skip the rest.
1416f4a2713aSLionel Sambuc ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
1417f4a2713aSLionel Sambuc if (!Priority) continue; // Malformed.
1418*0a6a1f1dSLionel Sambuc Structors.push_back(Structor());
1419*0a6a1f1dSLionel Sambuc Structor &S = Structors.back();
1420*0a6a1f1dSLionel Sambuc S.Priority = Priority->getLimitedValue(65535);
1421*0a6a1f1dSLionel Sambuc S.Func = CS->getOperand(1);
1422*0a6a1f1dSLionel Sambuc if (ETy->getNumElements() == 3 && !CS->getOperand(2)->isNullValue())
1423*0a6a1f1dSLionel Sambuc S.ComdatKey = dyn_cast<GlobalValue>(CS->getOperand(2)->stripPointerCasts());
1424f4a2713aSLionel Sambuc }
1425f4a2713aSLionel Sambuc
1426f4a2713aSLionel Sambuc // Emit the function pointers in the target-specific order
1427*0a6a1f1dSLionel Sambuc const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
1428f4a2713aSLionel Sambuc unsigned Align = Log2_32(DL->getPointerPrefAlignment());
1429*0a6a1f1dSLionel Sambuc std::stable_sort(Structors.begin(), Structors.end(),
1430*0a6a1f1dSLionel Sambuc [](const Structor &L,
1431*0a6a1f1dSLionel Sambuc const Structor &R) { return L.Priority < R.Priority; });
1432*0a6a1f1dSLionel Sambuc for (Structor &S : Structors) {
1433*0a6a1f1dSLionel Sambuc const TargetLoweringObjectFile &Obj = getObjFileLowering();
1434*0a6a1f1dSLionel Sambuc const MCSymbol *KeySym = nullptr;
1435*0a6a1f1dSLionel Sambuc if (GlobalValue *GV = S.ComdatKey) {
1436*0a6a1f1dSLionel Sambuc if (GV->hasAvailableExternallyLinkage())
1437*0a6a1f1dSLionel Sambuc // If the associated variable is available_externally, some other TU
1438*0a6a1f1dSLionel Sambuc // will provide its dynamic initializer.
1439*0a6a1f1dSLionel Sambuc continue;
1440*0a6a1f1dSLionel Sambuc
1441*0a6a1f1dSLionel Sambuc KeySym = getSymbol(GV);
1442*0a6a1f1dSLionel Sambuc }
1443f4a2713aSLionel Sambuc const MCSection *OutputSection =
1444*0a6a1f1dSLionel Sambuc (isCtor ? Obj.getStaticCtorSection(S.Priority, KeySym)
1445*0a6a1f1dSLionel Sambuc : Obj.getStaticDtorSection(S.Priority, KeySym));
1446f4a2713aSLionel Sambuc OutStreamer.SwitchSection(OutputSection);
1447f4a2713aSLionel Sambuc if (OutStreamer.getCurrentSection() != OutStreamer.getPreviousSection())
1448f4a2713aSLionel Sambuc EmitAlignment(Align);
1449*0a6a1f1dSLionel Sambuc EmitXXStructor(S.Func);
1450f4a2713aSLionel Sambuc }
1451f4a2713aSLionel Sambuc }
1452f4a2713aSLionel Sambuc
EmitModuleIdents(Module & M)1453f4a2713aSLionel Sambuc void AsmPrinter::EmitModuleIdents(Module &M) {
1454f4a2713aSLionel Sambuc if (!MAI->hasIdentDirective())
1455f4a2713aSLionel Sambuc return;
1456f4a2713aSLionel Sambuc
1457f4a2713aSLionel Sambuc if (const NamedMDNode *NMD = M.getNamedMetadata("llvm.ident")) {
1458f4a2713aSLionel Sambuc for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1459f4a2713aSLionel Sambuc const MDNode *N = NMD->getOperand(i);
1460f4a2713aSLionel Sambuc assert(N->getNumOperands() == 1 &&
1461f4a2713aSLionel Sambuc "llvm.ident metadata entry can have only one operand");
1462f4a2713aSLionel Sambuc const MDString *S = cast<MDString>(N->getOperand(0));
1463f4a2713aSLionel Sambuc OutStreamer.EmitIdent(S->getString());
1464f4a2713aSLionel Sambuc }
1465f4a2713aSLionel Sambuc }
1466f4a2713aSLionel Sambuc }
1467f4a2713aSLionel Sambuc
1468f4a2713aSLionel Sambuc //===--------------------------------------------------------------------===//
1469f4a2713aSLionel Sambuc // Emission and print routines
1470f4a2713aSLionel Sambuc //
1471f4a2713aSLionel Sambuc
1472f4a2713aSLionel Sambuc /// EmitInt8 - Emit a byte directive and value.
1473f4a2713aSLionel Sambuc ///
EmitInt8(int Value) const1474f4a2713aSLionel Sambuc void AsmPrinter::EmitInt8(int Value) const {
1475f4a2713aSLionel Sambuc OutStreamer.EmitIntValue(Value, 1);
1476f4a2713aSLionel Sambuc }
1477f4a2713aSLionel Sambuc
1478f4a2713aSLionel Sambuc /// EmitInt16 - Emit a short directive and value.
1479f4a2713aSLionel Sambuc ///
EmitInt16(int Value) const1480f4a2713aSLionel Sambuc void AsmPrinter::EmitInt16(int Value) const {
1481f4a2713aSLionel Sambuc OutStreamer.EmitIntValue(Value, 2);
1482f4a2713aSLionel Sambuc }
1483f4a2713aSLionel Sambuc
1484f4a2713aSLionel Sambuc /// EmitInt32 - Emit a long directive and value.
1485f4a2713aSLionel Sambuc ///
EmitInt32(int Value) const1486f4a2713aSLionel Sambuc void AsmPrinter::EmitInt32(int Value) const {
1487f4a2713aSLionel Sambuc OutStreamer.EmitIntValue(Value, 4);
1488f4a2713aSLionel Sambuc }
1489f4a2713aSLionel Sambuc
1490*0a6a1f1dSLionel Sambuc /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
1491*0a6a1f1dSLionel Sambuc /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
1492*0a6a1f1dSLionel Sambuc /// .set if it avoids relocations.
EmitLabelDifference(const MCSymbol * Hi,const MCSymbol * Lo,unsigned Size) const1493f4a2713aSLionel Sambuc void AsmPrinter::EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
1494f4a2713aSLionel Sambuc unsigned Size) const {
1495f4a2713aSLionel Sambuc // Get the Hi-Lo expression.
1496f4a2713aSLionel Sambuc const MCExpr *Diff =
1497f4a2713aSLionel Sambuc MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(Hi, OutContext),
1498f4a2713aSLionel Sambuc MCSymbolRefExpr::Create(Lo, OutContext),
1499f4a2713aSLionel Sambuc OutContext);
1500f4a2713aSLionel Sambuc
1501*0a6a1f1dSLionel Sambuc if (!MAI->doesSetDirectiveSuppressesReloc()) {
1502f4a2713aSLionel Sambuc OutStreamer.EmitValue(Diff, Size);
1503f4a2713aSLionel Sambuc return;
1504f4a2713aSLionel Sambuc }
1505f4a2713aSLionel Sambuc
1506f4a2713aSLionel Sambuc // Otherwise, emit with .set (aka assignment).
1507f4a2713aSLionel Sambuc MCSymbol *SetLabel = GetTempSymbol("set", SetCounter++);
1508f4a2713aSLionel Sambuc OutStreamer.EmitAssignment(SetLabel, Diff);
1509f4a2713aSLionel Sambuc OutStreamer.EmitSymbolValue(SetLabel, Size);
1510f4a2713aSLionel Sambuc }
1511f4a2713aSLionel Sambuc
1512f4a2713aSLionel Sambuc /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
1513f4a2713aSLionel Sambuc /// where the size in bytes of the directive is specified by Size and Label
1514f4a2713aSLionel Sambuc /// specifies the label. This implicitly uses .set if it is available.
EmitLabelPlusOffset(const MCSymbol * Label,uint64_t Offset,unsigned Size,bool IsSectionRelative) const1515f4a2713aSLionel Sambuc void AsmPrinter::EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
1516*0a6a1f1dSLionel Sambuc unsigned Size,
1517*0a6a1f1dSLionel Sambuc bool IsSectionRelative) const {
1518f4a2713aSLionel Sambuc if (MAI->needsDwarfSectionOffsetDirective() && IsSectionRelative) {
1519f4a2713aSLionel Sambuc OutStreamer.EmitCOFFSecRel32(Label);
1520f4a2713aSLionel Sambuc return;
1521f4a2713aSLionel Sambuc }
1522f4a2713aSLionel Sambuc
1523f4a2713aSLionel Sambuc // Emit Label+Offset (or just Label if Offset is zero)
1524f4a2713aSLionel Sambuc const MCExpr *Expr = MCSymbolRefExpr::Create(Label, OutContext);
1525f4a2713aSLionel Sambuc if (Offset)
1526*0a6a1f1dSLionel Sambuc Expr = MCBinaryExpr::CreateAdd(
1527*0a6a1f1dSLionel Sambuc Expr, MCConstantExpr::Create(Offset, OutContext), OutContext);
1528f4a2713aSLionel Sambuc
1529f4a2713aSLionel Sambuc OutStreamer.EmitValue(Expr, Size);
1530f4a2713aSLionel Sambuc }
1531f4a2713aSLionel Sambuc
1532f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1533f4a2713aSLionel Sambuc
1534f4a2713aSLionel Sambuc // EmitAlignment - Emit an alignment directive to the specified power of
1535f4a2713aSLionel Sambuc // two boundary. For example, if you pass in 3 here, you will get an 8
1536f4a2713aSLionel Sambuc // byte alignment. If a global value is specified, and if that global has
1537f4a2713aSLionel Sambuc // an explicit alignment requested, it will override the alignment request
1538f4a2713aSLionel Sambuc // if required for correctness.
1539f4a2713aSLionel Sambuc //
EmitAlignment(unsigned NumBits,const GlobalObject * GV) const1540*0a6a1f1dSLionel Sambuc void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalObject *GV) const {
1541*0a6a1f1dSLionel Sambuc if (GV)
1542*0a6a1f1dSLionel Sambuc NumBits = getGVAlignmentLog2(GV, *TM.getSubtargetImpl()->getDataLayout(),
1543*0a6a1f1dSLionel Sambuc NumBits);
1544f4a2713aSLionel Sambuc
1545f4a2713aSLionel Sambuc if (NumBits == 0) return; // 1-byte aligned: no need to emit alignment.
1546f4a2713aSLionel Sambuc
1547*0a6a1f1dSLionel Sambuc assert(NumBits <
1548*0a6a1f1dSLionel Sambuc static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
1549*0a6a1f1dSLionel Sambuc "undefined behavior");
1550f4a2713aSLionel Sambuc if (getCurrentSection()->getKind().isText())
1551*0a6a1f1dSLionel Sambuc OutStreamer.EmitCodeAlignment(1u << NumBits);
1552f4a2713aSLionel Sambuc else
1553*0a6a1f1dSLionel Sambuc OutStreamer.EmitValueToAlignment(1u << NumBits);
1554f4a2713aSLionel Sambuc }
1555f4a2713aSLionel Sambuc
1556f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1557f4a2713aSLionel Sambuc // Constant emission.
1558f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1559f4a2713aSLionel Sambuc
lowerConstant(const Constant * CV)1560*0a6a1f1dSLionel Sambuc const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
1561*0a6a1f1dSLionel Sambuc MCContext &Ctx = OutContext;
1562f4a2713aSLionel Sambuc
1563f4a2713aSLionel Sambuc if (CV->isNullValue() || isa<UndefValue>(CV))
1564f4a2713aSLionel Sambuc return MCConstantExpr::Create(0, Ctx);
1565f4a2713aSLionel Sambuc
1566f4a2713aSLionel Sambuc if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV))
1567f4a2713aSLionel Sambuc return MCConstantExpr::Create(CI->getZExtValue(), Ctx);
1568f4a2713aSLionel Sambuc
1569f4a2713aSLionel Sambuc if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
1570*0a6a1f1dSLionel Sambuc return MCSymbolRefExpr::Create(getSymbol(GV), Ctx);
1571f4a2713aSLionel Sambuc
1572f4a2713aSLionel Sambuc if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV))
1573*0a6a1f1dSLionel Sambuc return MCSymbolRefExpr::Create(GetBlockAddressSymbol(BA), Ctx);
1574f4a2713aSLionel Sambuc
1575f4a2713aSLionel Sambuc const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
1576*0a6a1f1dSLionel Sambuc if (!CE) {
1577f4a2713aSLionel Sambuc llvm_unreachable("Unknown constant value to lower!");
1578f4a2713aSLionel Sambuc }
1579f4a2713aSLionel Sambuc
1580*0a6a1f1dSLionel Sambuc if (const MCExpr *RelocExpr
1581*0a6a1f1dSLionel Sambuc = getObjFileLowering().getExecutableRelativeSymbol(CE, *Mang, TM))
1582*0a6a1f1dSLionel Sambuc return RelocExpr;
1583*0a6a1f1dSLionel Sambuc
1584f4a2713aSLionel Sambuc switch (CE->getOpcode()) {
1585f4a2713aSLionel Sambuc default:
1586f4a2713aSLionel Sambuc // If the code isn't optimized, there may be outstanding folding
1587f4a2713aSLionel Sambuc // opportunities. Attempt to fold the expression using DataLayout as a
1588f4a2713aSLionel Sambuc // last resort before giving up.
1589*0a6a1f1dSLionel Sambuc if (Constant *C = ConstantFoldConstantExpression(
1590*0a6a1f1dSLionel Sambuc CE, TM.getSubtargetImpl()->getDataLayout()))
1591f4a2713aSLionel Sambuc if (C != CE)
1592*0a6a1f1dSLionel Sambuc return lowerConstant(C);
1593f4a2713aSLionel Sambuc
1594f4a2713aSLionel Sambuc // Otherwise report the problem to the user.
1595f4a2713aSLionel Sambuc {
1596f4a2713aSLionel Sambuc std::string S;
1597f4a2713aSLionel Sambuc raw_string_ostream OS(S);
1598f4a2713aSLionel Sambuc OS << "Unsupported expression in static initializer: ";
1599*0a6a1f1dSLionel Sambuc CE->printAsOperand(OS, /*PrintType=*/false,
1600*0a6a1f1dSLionel Sambuc !MF ? nullptr : MF->getFunction()->getParent());
1601f4a2713aSLionel Sambuc report_fatal_error(OS.str());
1602f4a2713aSLionel Sambuc }
1603f4a2713aSLionel Sambuc case Instruction::GetElementPtr: {
1604*0a6a1f1dSLionel Sambuc const DataLayout &DL = *TM.getSubtargetImpl()->getDataLayout();
1605f4a2713aSLionel Sambuc // Generate a symbolic expression for the byte address
1606f4a2713aSLionel Sambuc APInt OffsetAI(DL.getPointerTypeSizeInBits(CE->getType()), 0);
1607f4a2713aSLionel Sambuc cast<GEPOperator>(CE)->accumulateConstantOffset(DL, OffsetAI);
1608f4a2713aSLionel Sambuc
1609*0a6a1f1dSLionel Sambuc const MCExpr *Base = lowerConstant(CE->getOperand(0));
1610f4a2713aSLionel Sambuc if (!OffsetAI)
1611f4a2713aSLionel Sambuc return Base;
1612f4a2713aSLionel Sambuc
1613f4a2713aSLionel Sambuc int64_t Offset = OffsetAI.getSExtValue();
1614f4a2713aSLionel Sambuc return MCBinaryExpr::CreateAdd(Base, MCConstantExpr::Create(Offset, Ctx),
1615f4a2713aSLionel Sambuc Ctx);
1616f4a2713aSLionel Sambuc }
1617f4a2713aSLionel Sambuc
1618f4a2713aSLionel Sambuc case Instruction::Trunc:
1619f4a2713aSLionel Sambuc // We emit the value and depend on the assembler to truncate the generated
1620f4a2713aSLionel Sambuc // expression properly. This is important for differences between
1621f4a2713aSLionel Sambuc // blockaddress labels. Since the two labels are in the same function, it
1622f4a2713aSLionel Sambuc // is reasonable to treat their delta as a 32-bit value.
1623f4a2713aSLionel Sambuc // FALL THROUGH.
1624f4a2713aSLionel Sambuc case Instruction::BitCast:
1625*0a6a1f1dSLionel Sambuc return lowerConstant(CE->getOperand(0));
1626f4a2713aSLionel Sambuc
1627f4a2713aSLionel Sambuc case Instruction::IntToPtr: {
1628*0a6a1f1dSLionel Sambuc const DataLayout &DL = *TM.getSubtargetImpl()->getDataLayout();
1629f4a2713aSLionel Sambuc // Handle casts to pointers by changing them into casts to the appropriate
1630f4a2713aSLionel Sambuc // integer type. This promotes constant folding and simplifies this code.
1631f4a2713aSLionel Sambuc Constant *Op = CE->getOperand(0);
1632f4a2713aSLionel Sambuc Op = ConstantExpr::getIntegerCast(Op, DL.getIntPtrType(CV->getType()),
1633f4a2713aSLionel Sambuc false/*ZExt*/);
1634*0a6a1f1dSLionel Sambuc return lowerConstant(Op);
1635f4a2713aSLionel Sambuc }
1636f4a2713aSLionel Sambuc
1637f4a2713aSLionel Sambuc case Instruction::PtrToInt: {
1638*0a6a1f1dSLionel Sambuc const DataLayout &DL = *TM.getSubtargetImpl()->getDataLayout();
1639f4a2713aSLionel Sambuc // Support only foldable casts to/from pointers that can be eliminated by
1640f4a2713aSLionel Sambuc // changing the pointer to the appropriately sized integer type.
1641f4a2713aSLionel Sambuc Constant *Op = CE->getOperand(0);
1642f4a2713aSLionel Sambuc Type *Ty = CE->getType();
1643f4a2713aSLionel Sambuc
1644*0a6a1f1dSLionel Sambuc const MCExpr *OpExpr = lowerConstant(Op);
1645f4a2713aSLionel Sambuc
1646f4a2713aSLionel Sambuc // We can emit the pointer value into this slot if the slot is an
1647f4a2713aSLionel Sambuc // integer slot equal to the size of the pointer.
1648f4a2713aSLionel Sambuc if (DL.getTypeAllocSize(Ty) == DL.getTypeAllocSize(Op->getType()))
1649f4a2713aSLionel Sambuc return OpExpr;
1650f4a2713aSLionel Sambuc
1651f4a2713aSLionel Sambuc // Otherwise the pointer is smaller than the resultant integer, mask off
1652f4a2713aSLionel Sambuc // the high bits so we are sure to get a proper truncation if the input is
1653f4a2713aSLionel Sambuc // a constant expr.
1654f4a2713aSLionel Sambuc unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType());
1655f4a2713aSLionel Sambuc const MCExpr *MaskExpr = MCConstantExpr::Create(~0ULL >> (64-InBits), Ctx);
1656f4a2713aSLionel Sambuc return MCBinaryExpr::CreateAnd(OpExpr, MaskExpr, Ctx);
1657f4a2713aSLionel Sambuc }
1658f4a2713aSLionel Sambuc
1659f4a2713aSLionel Sambuc // The MC library also has a right-shift operator, but it isn't consistently
1660f4a2713aSLionel Sambuc // signed or unsigned between different targets.
1661f4a2713aSLionel Sambuc case Instruction::Add:
1662f4a2713aSLionel Sambuc case Instruction::Sub:
1663f4a2713aSLionel Sambuc case Instruction::Mul:
1664f4a2713aSLionel Sambuc case Instruction::SDiv:
1665f4a2713aSLionel Sambuc case Instruction::SRem:
1666f4a2713aSLionel Sambuc case Instruction::Shl:
1667f4a2713aSLionel Sambuc case Instruction::And:
1668f4a2713aSLionel Sambuc case Instruction::Or:
1669f4a2713aSLionel Sambuc case Instruction::Xor: {
1670*0a6a1f1dSLionel Sambuc const MCExpr *LHS = lowerConstant(CE->getOperand(0));
1671*0a6a1f1dSLionel Sambuc const MCExpr *RHS = lowerConstant(CE->getOperand(1));
1672f4a2713aSLionel Sambuc switch (CE->getOpcode()) {
1673f4a2713aSLionel Sambuc default: llvm_unreachable("Unknown binary operator constant cast expr");
1674f4a2713aSLionel Sambuc case Instruction::Add: return MCBinaryExpr::CreateAdd(LHS, RHS, Ctx);
1675f4a2713aSLionel Sambuc case Instruction::Sub: return MCBinaryExpr::CreateSub(LHS, RHS, Ctx);
1676f4a2713aSLionel Sambuc case Instruction::Mul: return MCBinaryExpr::CreateMul(LHS, RHS, Ctx);
1677f4a2713aSLionel Sambuc case Instruction::SDiv: return MCBinaryExpr::CreateDiv(LHS, RHS, Ctx);
1678f4a2713aSLionel Sambuc case Instruction::SRem: return MCBinaryExpr::CreateMod(LHS, RHS, Ctx);
1679f4a2713aSLionel Sambuc case Instruction::Shl: return MCBinaryExpr::CreateShl(LHS, RHS, Ctx);
1680f4a2713aSLionel Sambuc case Instruction::And: return MCBinaryExpr::CreateAnd(LHS, RHS, Ctx);
1681f4a2713aSLionel Sambuc case Instruction::Or: return MCBinaryExpr::CreateOr (LHS, RHS, Ctx);
1682f4a2713aSLionel Sambuc case Instruction::Xor: return MCBinaryExpr::CreateXor(LHS, RHS, Ctx);
1683f4a2713aSLionel Sambuc }
1684f4a2713aSLionel Sambuc }
1685f4a2713aSLionel Sambuc }
1686f4a2713aSLionel Sambuc }
1687f4a2713aSLionel Sambuc
1688f4a2713aSLionel Sambuc static void emitGlobalConstantImpl(const Constant *C, AsmPrinter &AP);
1689f4a2713aSLionel Sambuc
1690f4a2713aSLionel Sambuc /// isRepeatedByteSequence - Determine whether the given value is
1691f4a2713aSLionel Sambuc /// composed of a repeated sequence of identical bytes and return the
1692f4a2713aSLionel Sambuc /// byte value. If it is not a repeated sequence, return -1.
isRepeatedByteSequence(const ConstantDataSequential * V)1693f4a2713aSLionel Sambuc static int isRepeatedByteSequence(const ConstantDataSequential *V) {
1694f4a2713aSLionel Sambuc StringRef Data = V->getRawDataValues();
1695f4a2713aSLionel Sambuc assert(!Data.empty() && "Empty aggregates should be CAZ node");
1696f4a2713aSLionel Sambuc char C = Data[0];
1697f4a2713aSLionel Sambuc for (unsigned i = 1, e = Data.size(); i != e; ++i)
1698f4a2713aSLionel Sambuc if (Data[i] != C) return -1;
1699f4a2713aSLionel Sambuc return static_cast<uint8_t>(C); // Ensure 255 is not returned as -1.
1700f4a2713aSLionel Sambuc }
1701f4a2713aSLionel Sambuc
1702f4a2713aSLionel Sambuc
1703f4a2713aSLionel Sambuc /// isRepeatedByteSequence - Determine whether the given value is
1704f4a2713aSLionel Sambuc /// composed of a repeated sequence of identical bytes and return the
1705f4a2713aSLionel Sambuc /// byte value. If it is not a repeated sequence, return -1.
isRepeatedByteSequence(const Value * V,TargetMachine & TM)1706f4a2713aSLionel Sambuc static int isRepeatedByteSequence(const Value *V, TargetMachine &TM) {
1707f4a2713aSLionel Sambuc
1708f4a2713aSLionel Sambuc if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
1709f4a2713aSLionel Sambuc if (CI->getBitWidth() > 64) return -1;
1710f4a2713aSLionel Sambuc
1711*0a6a1f1dSLionel Sambuc uint64_t Size =
1712*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(V->getType());
1713f4a2713aSLionel Sambuc uint64_t Value = CI->getZExtValue();
1714f4a2713aSLionel Sambuc
1715f4a2713aSLionel Sambuc // Make sure the constant is at least 8 bits long and has a power
1716f4a2713aSLionel Sambuc // of 2 bit width. This guarantees the constant bit width is
1717f4a2713aSLionel Sambuc // always a multiple of 8 bits, avoiding issues with padding out
1718f4a2713aSLionel Sambuc // to Size and other such corner cases.
1719f4a2713aSLionel Sambuc if (CI->getBitWidth() < 8 || !isPowerOf2_64(CI->getBitWidth())) return -1;
1720f4a2713aSLionel Sambuc
1721f4a2713aSLionel Sambuc uint8_t Byte = static_cast<uint8_t>(Value);
1722f4a2713aSLionel Sambuc
1723f4a2713aSLionel Sambuc for (unsigned i = 1; i < Size; ++i) {
1724f4a2713aSLionel Sambuc Value >>= 8;
1725f4a2713aSLionel Sambuc if (static_cast<uint8_t>(Value) != Byte) return -1;
1726f4a2713aSLionel Sambuc }
1727f4a2713aSLionel Sambuc return Byte;
1728f4a2713aSLionel Sambuc }
1729f4a2713aSLionel Sambuc if (const ConstantArray *CA = dyn_cast<ConstantArray>(V)) {
1730f4a2713aSLionel Sambuc // Make sure all array elements are sequences of the same repeated
1731f4a2713aSLionel Sambuc // byte.
1732f4a2713aSLionel Sambuc assert(CA->getNumOperands() != 0 && "Should be a CAZ");
1733f4a2713aSLionel Sambuc int Byte = isRepeatedByteSequence(CA->getOperand(0), TM);
1734f4a2713aSLionel Sambuc if (Byte == -1) return -1;
1735f4a2713aSLionel Sambuc
1736f4a2713aSLionel Sambuc for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1737f4a2713aSLionel Sambuc int ThisByte = isRepeatedByteSequence(CA->getOperand(i), TM);
1738f4a2713aSLionel Sambuc if (ThisByte == -1) return -1;
1739f4a2713aSLionel Sambuc if (Byte != ThisByte) return -1;
1740f4a2713aSLionel Sambuc }
1741f4a2713aSLionel Sambuc return Byte;
1742f4a2713aSLionel Sambuc }
1743f4a2713aSLionel Sambuc
1744f4a2713aSLionel Sambuc if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V))
1745f4a2713aSLionel Sambuc return isRepeatedByteSequence(CDS);
1746f4a2713aSLionel Sambuc
1747f4a2713aSLionel Sambuc return -1;
1748f4a2713aSLionel Sambuc }
1749f4a2713aSLionel Sambuc
emitGlobalConstantDataSequential(const ConstantDataSequential * CDS,AsmPrinter & AP)1750f4a2713aSLionel Sambuc static void emitGlobalConstantDataSequential(const ConstantDataSequential *CDS,
1751f4a2713aSLionel Sambuc AsmPrinter &AP){
1752f4a2713aSLionel Sambuc
1753f4a2713aSLionel Sambuc // See if we can aggregate this into a .fill, if so, emit it as such.
1754f4a2713aSLionel Sambuc int Value = isRepeatedByteSequence(CDS, AP.TM);
1755f4a2713aSLionel Sambuc if (Value != -1) {
1756*0a6a1f1dSLionel Sambuc uint64_t Bytes =
1757*0a6a1f1dSLionel Sambuc AP.TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(
1758*0a6a1f1dSLionel Sambuc CDS->getType());
1759f4a2713aSLionel Sambuc // Don't emit a 1-byte object as a .fill.
1760f4a2713aSLionel Sambuc if (Bytes > 1)
1761f4a2713aSLionel Sambuc return AP.OutStreamer.EmitFill(Bytes, Value);
1762f4a2713aSLionel Sambuc }
1763f4a2713aSLionel Sambuc
1764f4a2713aSLionel Sambuc // If this can be emitted with .ascii/.asciz, emit it as such.
1765f4a2713aSLionel Sambuc if (CDS->isString())
1766f4a2713aSLionel Sambuc return AP.OutStreamer.EmitBytes(CDS->getAsString());
1767f4a2713aSLionel Sambuc
1768f4a2713aSLionel Sambuc // Otherwise, emit the values in successive locations.
1769f4a2713aSLionel Sambuc unsigned ElementByteSize = CDS->getElementByteSize();
1770f4a2713aSLionel Sambuc if (isa<IntegerType>(CDS->getElementType())) {
1771f4a2713aSLionel Sambuc for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1772f4a2713aSLionel Sambuc if (AP.isVerbose())
1773f4a2713aSLionel Sambuc AP.OutStreamer.GetCommentOS() << format("0x%" PRIx64 "\n",
1774f4a2713aSLionel Sambuc CDS->getElementAsInteger(i));
1775f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(CDS->getElementAsInteger(i),
1776f4a2713aSLionel Sambuc ElementByteSize);
1777f4a2713aSLionel Sambuc }
1778f4a2713aSLionel Sambuc } else if (ElementByteSize == 4) {
1779f4a2713aSLionel Sambuc // FP Constants are printed as integer constants to avoid losing
1780f4a2713aSLionel Sambuc // precision.
1781f4a2713aSLionel Sambuc assert(CDS->getElementType()->isFloatTy());
1782f4a2713aSLionel Sambuc for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1783f4a2713aSLionel Sambuc union {
1784f4a2713aSLionel Sambuc float F;
1785f4a2713aSLionel Sambuc uint32_t I;
1786f4a2713aSLionel Sambuc };
1787f4a2713aSLionel Sambuc
1788f4a2713aSLionel Sambuc F = CDS->getElementAsFloat(i);
1789f4a2713aSLionel Sambuc if (AP.isVerbose())
1790f4a2713aSLionel Sambuc AP.OutStreamer.GetCommentOS() << "float " << F << '\n';
1791f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(I, 4);
1792f4a2713aSLionel Sambuc }
1793f4a2713aSLionel Sambuc } else {
1794f4a2713aSLionel Sambuc assert(CDS->getElementType()->isDoubleTy());
1795f4a2713aSLionel Sambuc for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1796f4a2713aSLionel Sambuc union {
1797f4a2713aSLionel Sambuc double F;
1798f4a2713aSLionel Sambuc uint64_t I;
1799f4a2713aSLionel Sambuc };
1800f4a2713aSLionel Sambuc
1801f4a2713aSLionel Sambuc F = CDS->getElementAsDouble(i);
1802f4a2713aSLionel Sambuc if (AP.isVerbose())
1803f4a2713aSLionel Sambuc AP.OutStreamer.GetCommentOS() << "double " << F << '\n';
1804f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(I, 8);
1805f4a2713aSLionel Sambuc }
1806f4a2713aSLionel Sambuc }
1807f4a2713aSLionel Sambuc
1808*0a6a1f1dSLionel Sambuc const DataLayout &DL = *AP.TM.getSubtargetImpl()->getDataLayout();
1809f4a2713aSLionel Sambuc unsigned Size = DL.getTypeAllocSize(CDS->getType());
1810f4a2713aSLionel Sambuc unsigned EmittedSize = DL.getTypeAllocSize(CDS->getType()->getElementType()) *
1811f4a2713aSLionel Sambuc CDS->getNumElements();
1812f4a2713aSLionel Sambuc if (unsigned Padding = Size - EmittedSize)
1813f4a2713aSLionel Sambuc AP.OutStreamer.EmitZeros(Padding);
1814f4a2713aSLionel Sambuc
1815f4a2713aSLionel Sambuc }
1816f4a2713aSLionel Sambuc
emitGlobalConstantArray(const ConstantArray * CA,AsmPrinter & AP)1817f4a2713aSLionel Sambuc static void emitGlobalConstantArray(const ConstantArray *CA, AsmPrinter &AP) {
1818f4a2713aSLionel Sambuc // See if we can aggregate some values. Make sure it can be
1819f4a2713aSLionel Sambuc // represented as a series of bytes of the constant value.
1820f4a2713aSLionel Sambuc int Value = isRepeatedByteSequence(CA, AP.TM);
1821f4a2713aSLionel Sambuc
1822f4a2713aSLionel Sambuc if (Value != -1) {
1823*0a6a1f1dSLionel Sambuc uint64_t Bytes =
1824*0a6a1f1dSLionel Sambuc AP.TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(
1825*0a6a1f1dSLionel Sambuc CA->getType());
1826f4a2713aSLionel Sambuc AP.OutStreamer.EmitFill(Bytes, Value);
1827f4a2713aSLionel Sambuc }
1828f4a2713aSLionel Sambuc else {
1829f4a2713aSLionel Sambuc for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1830f4a2713aSLionel Sambuc emitGlobalConstantImpl(CA->getOperand(i), AP);
1831f4a2713aSLionel Sambuc }
1832f4a2713aSLionel Sambuc }
1833f4a2713aSLionel Sambuc
emitGlobalConstantVector(const ConstantVector * CV,AsmPrinter & AP)1834f4a2713aSLionel Sambuc static void emitGlobalConstantVector(const ConstantVector *CV, AsmPrinter &AP) {
1835f4a2713aSLionel Sambuc for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
1836f4a2713aSLionel Sambuc emitGlobalConstantImpl(CV->getOperand(i), AP);
1837f4a2713aSLionel Sambuc
1838*0a6a1f1dSLionel Sambuc const DataLayout &DL = *AP.TM.getSubtargetImpl()->getDataLayout();
1839f4a2713aSLionel Sambuc unsigned Size = DL.getTypeAllocSize(CV->getType());
1840f4a2713aSLionel Sambuc unsigned EmittedSize = DL.getTypeAllocSize(CV->getType()->getElementType()) *
1841f4a2713aSLionel Sambuc CV->getType()->getNumElements();
1842f4a2713aSLionel Sambuc if (unsigned Padding = Size - EmittedSize)
1843f4a2713aSLionel Sambuc AP.OutStreamer.EmitZeros(Padding);
1844f4a2713aSLionel Sambuc }
1845f4a2713aSLionel Sambuc
emitGlobalConstantStruct(const ConstantStruct * CS,AsmPrinter & AP)1846f4a2713aSLionel Sambuc static void emitGlobalConstantStruct(const ConstantStruct *CS, AsmPrinter &AP) {
1847f4a2713aSLionel Sambuc // Print the fields in successive locations. Pad to align if needed!
1848*0a6a1f1dSLionel Sambuc const DataLayout *DL = AP.TM.getSubtargetImpl()->getDataLayout();
1849f4a2713aSLionel Sambuc unsigned Size = DL->getTypeAllocSize(CS->getType());
1850f4a2713aSLionel Sambuc const StructLayout *Layout = DL->getStructLayout(CS->getType());
1851f4a2713aSLionel Sambuc uint64_t SizeSoFar = 0;
1852f4a2713aSLionel Sambuc for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
1853f4a2713aSLionel Sambuc const Constant *Field = CS->getOperand(i);
1854f4a2713aSLionel Sambuc
1855f4a2713aSLionel Sambuc // Check if padding is needed and insert one or more 0s.
1856f4a2713aSLionel Sambuc uint64_t FieldSize = DL->getTypeAllocSize(Field->getType());
1857f4a2713aSLionel Sambuc uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1))
1858f4a2713aSLionel Sambuc - Layout->getElementOffset(i)) - FieldSize;
1859f4a2713aSLionel Sambuc SizeSoFar += FieldSize + PadSize;
1860f4a2713aSLionel Sambuc
1861f4a2713aSLionel Sambuc // Now print the actual field value.
1862f4a2713aSLionel Sambuc emitGlobalConstantImpl(Field, AP);
1863f4a2713aSLionel Sambuc
1864f4a2713aSLionel Sambuc // Insert padding - this may include padding to increase the size of the
1865f4a2713aSLionel Sambuc // current field up to the ABI size (if the struct is not packed) as well
1866f4a2713aSLionel Sambuc // as padding to ensure that the next field starts at the right offset.
1867f4a2713aSLionel Sambuc AP.OutStreamer.EmitZeros(PadSize);
1868f4a2713aSLionel Sambuc }
1869f4a2713aSLionel Sambuc assert(SizeSoFar == Layout->getSizeInBytes() &&
1870f4a2713aSLionel Sambuc "Layout of constant struct may be incorrect!");
1871f4a2713aSLionel Sambuc }
1872f4a2713aSLionel Sambuc
emitGlobalConstantFP(const ConstantFP * CFP,AsmPrinter & AP)1873f4a2713aSLionel Sambuc static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP) {
1874f4a2713aSLionel Sambuc APInt API = CFP->getValueAPF().bitcastToAPInt();
1875f4a2713aSLionel Sambuc
1876f4a2713aSLionel Sambuc // First print a comment with what we think the original floating-point value
1877f4a2713aSLionel Sambuc // should have been.
1878f4a2713aSLionel Sambuc if (AP.isVerbose()) {
1879f4a2713aSLionel Sambuc SmallString<8> StrVal;
1880f4a2713aSLionel Sambuc CFP->getValueAPF().toString(StrVal);
1881f4a2713aSLionel Sambuc
1882*0a6a1f1dSLionel Sambuc if (CFP->getType())
1883f4a2713aSLionel Sambuc CFP->getType()->print(AP.OutStreamer.GetCommentOS());
1884*0a6a1f1dSLionel Sambuc else
1885*0a6a1f1dSLionel Sambuc AP.OutStreamer.GetCommentOS() << "Printing <null> Type";
1886f4a2713aSLionel Sambuc AP.OutStreamer.GetCommentOS() << ' ' << StrVal << '\n';
1887f4a2713aSLionel Sambuc }
1888f4a2713aSLionel Sambuc
1889f4a2713aSLionel Sambuc // Now iterate through the APInt chunks, emitting them in endian-correct
1890f4a2713aSLionel Sambuc // order, possibly with a smaller chunk at beginning/end (e.g. for x87 80-bit
1891f4a2713aSLionel Sambuc // floats).
1892f4a2713aSLionel Sambuc unsigned NumBytes = API.getBitWidth() / 8;
1893f4a2713aSLionel Sambuc unsigned TrailingBytes = NumBytes % sizeof(uint64_t);
1894f4a2713aSLionel Sambuc const uint64_t *p = API.getRawData();
1895f4a2713aSLionel Sambuc
1896f4a2713aSLionel Sambuc // PPC's long double has odd notions of endianness compared to how LLVM
1897f4a2713aSLionel Sambuc // handles it: p[0] goes first for *big* endian on PPC.
1898*0a6a1f1dSLionel Sambuc if (AP.TM.getSubtargetImpl()->getDataLayout()->isBigEndian() &&
1899*0a6a1f1dSLionel Sambuc !CFP->getType()->isPPC_FP128Ty()) {
1900f4a2713aSLionel Sambuc int Chunk = API.getNumWords() - 1;
1901f4a2713aSLionel Sambuc
1902f4a2713aSLionel Sambuc if (TrailingBytes)
1903f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(p[Chunk--], TrailingBytes);
1904f4a2713aSLionel Sambuc
1905f4a2713aSLionel Sambuc for (; Chunk >= 0; --Chunk)
1906f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(p[Chunk], sizeof(uint64_t));
1907f4a2713aSLionel Sambuc } else {
1908f4a2713aSLionel Sambuc unsigned Chunk;
1909f4a2713aSLionel Sambuc for (Chunk = 0; Chunk < NumBytes / sizeof(uint64_t); ++Chunk)
1910f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(p[Chunk], sizeof(uint64_t));
1911f4a2713aSLionel Sambuc
1912f4a2713aSLionel Sambuc if (TrailingBytes)
1913f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(p[Chunk], TrailingBytes);
1914f4a2713aSLionel Sambuc }
1915f4a2713aSLionel Sambuc
1916f4a2713aSLionel Sambuc // Emit the tail padding for the long double.
1917*0a6a1f1dSLionel Sambuc const DataLayout &DL = *AP.TM.getSubtargetImpl()->getDataLayout();
1918f4a2713aSLionel Sambuc AP.OutStreamer.EmitZeros(DL.getTypeAllocSize(CFP->getType()) -
1919f4a2713aSLionel Sambuc DL.getTypeStoreSize(CFP->getType()));
1920f4a2713aSLionel Sambuc }
1921f4a2713aSLionel Sambuc
emitGlobalConstantLargeInt(const ConstantInt * CI,AsmPrinter & AP)1922f4a2713aSLionel Sambuc static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) {
1923*0a6a1f1dSLionel Sambuc const DataLayout *DL = AP.TM.getSubtargetImpl()->getDataLayout();
1924f4a2713aSLionel Sambuc unsigned BitWidth = CI->getBitWidth();
1925f4a2713aSLionel Sambuc
1926f4a2713aSLionel Sambuc // Copy the value as we may massage the layout for constants whose bit width
1927f4a2713aSLionel Sambuc // is not a multiple of 64-bits.
1928f4a2713aSLionel Sambuc APInt Realigned(CI->getValue());
1929f4a2713aSLionel Sambuc uint64_t ExtraBits = 0;
1930f4a2713aSLionel Sambuc unsigned ExtraBitsSize = BitWidth & 63;
1931f4a2713aSLionel Sambuc
1932f4a2713aSLionel Sambuc if (ExtraBitsSize) {
1933f4a2713aSLionel Sambuc // The bit width of the data is not a multiple of 64-bits.
1934f4a2713aSLionel Sambuc // The extra bits are expected to be at the end of the chunk of the memory.
1935f4a2713aSLionel Sambuc // Little endian:
1936f4a2713aSLionel Sambuc // * Nothing to be done, just record the extra bits to emit.
1937f4a2713aSLionel Sambuc // Big endian:
1938f4a2713aSLionel Sambuc // * Record the extra bits to emit.
1939f4a2713aSLionel Sambuc // * Realign the raw data to emit the chunks of 64-bits.
1940f4a2713aSLionel Sambuc if (DL->isBigEndian()) {
1941f4a2713aSLionel Sambuc // Basically the structure of the raw data is a chunk of 64-bits cells:
1942f4a2713aSLionel Sambuc // 0 1 BitWidth / 64
1943f4a2713aSLionel Sambuc // [chunk1][chunk2] ... [chunkN].
1944f4a2713aSLionel Sambuc // The most significant chunk is chunkN and it should be emitted first.
1945f4a2713aSLionel Sambuc // However, due to the alignment issue chunkN contains useless bits.
1946f4a2713aSLionel Sambuc // Realign the chunks so that they contain only useless information:
1947f4a2713aSLionel Sambuc // ExtraBits 0 1 (BitWidth / 64) - 1
1948f4a2713aSLionel Sambuc // chu[nk1 chu][nk2 chu] ... [nkN-1 chunkN]
1949f4a2713aSLionel Sambuc ExtraBits = Realigned.getRawData()[0] &
1950f4a2713aSLionel Sambuc (((uint64_t)-1) >> (64 - ExtraBitsSize));
1951f4a2713aSLionel Sambuc Realigned = Realigned.lshr(ExtraBitsSize);
1952f4a2713aSLionel Sambuc } else
1953f4a2713aSLionel Sambuc ExtraBits = Realigned.getRawData()[BitWidth / 64];
1954f4a2713aSLionel Sambuc }
1955f4a2713aSLionel Sambuc
1956f4a2713aSLionel Sambuc // We don't expect assemblers to support integer data directives
1957f4a2713aSLionel Sambuc // for more than 64 bits, so we emit the data in at most 64-bit
1958f4a2713aSLionel Sambuc // quantities at a time.
1959f4a2713aSLionel Sambuc const uint64_t *RawData = Realigned.getRawData();
1960f4a2713aSLionel Sambuc for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1961f4a2713aSLionel Sambuc uint64_t Val = DL->isBigEndian() ? RawData[e - i - 1] : RawData[i];
1962f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(Val, 8);
1963f4a2713aSLionel Sambuc }
1964f4a2713aSLionel Sambuc
1965f4a2713aSLionel Sambuc if (ExtraBitsSize) {
1966f4a2713aSLionel Sambuc // Emit the extra bits after the 64-bits chunks.
1967f4a2713aSLionel Sambuc
1968f4a2713aSLionel Sambuc // Emit a directive that fills the expected size.
1969*0a6a1f1dSLionel Sambuc uint64_t Size = AP.TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(
1970*0a6a1f1dSLionel Sambuc CI->getType());
1971f4a2713aSLionel Sambuc Size -= (BitWidth / 64) * 8;
1972f4a2713aSLionel Sambuc assert(Size && Size * 8 >= ExtraBitsSize &&
1973f4a2713aSLionel Sambuc (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize)))
1974f4a2713aSLionel Sambuc == ExtraBits && "Directive too small for extra bits.");
1975f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(ExtraBits, Size);
1976f4a2713aSLionel Sambuc }
1977f4a2713aSLionel Sambuc }
1978f4a2713aSLionel Sambuc
emitGlobalConstantImpl(const Constant * CV,AsmPrinter & AP)1979f4a2713aSLionel Sambuc static void emitGlobalConstantImpl(const Constant *CV, AsmPrinter &AP) {
1980*0a6a1f1dSLionel Sambuc const DataLayout *DL = AP.TM.getSubtargetImpl()->getDataLayout();
1981f4a2713aSLionel Sambuc uint64_t Size = DL->getTypeAllocSize(CV->getType());
1982f4a2713aSLionel Sambuc if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
1983f4a2713aSLionel Sambuc return AP.OutStreamer.EmitZeros(Size);
1984f4a2713aSLionel Sambuc
1985f4a2713aSLionel Sambuc if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1986f4a2713aSLionel Sambuc switch (Size) {
1987f4a2713aSLionel Sambuc case 1:
1988f4a2713aSLionel Sambuc case 2:
1989f4a2713aSLionel Sambuc case 4:
1990f4a2713aSLionel Sambuc case 8:
1991f4a2713aSLionel Sambuc if (AP.isVerbose())
1992f4a2713aSLionel Sambuc AP.OutStreamer.GetCommentOS() << format("0x%" PRIx64 "\n",
1993f4a2713aSLionel Sambuc CI->getZExtValue());
1994f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(CI->getZExtValue(), Size);
1995f4a2713aSLionel Sambuc return;
1996f4a2713aSLionel Sambuc default:
1997f4a2713aSLionel Sambuc emitGlobalConstantLargeInt(CI, AP);
1998f4a2713aSLionel Sambuc return;
1999f4a2713aSLionel Sambuc }
2000f4a2713aSLionel Sambuc }
2001f4a2713aSLionel Sambuc
2002f4a2713aSLionel Sambuc if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
2003f4a2713aSLionel Sambuc return emitGlobalConstantFP(CFP, AP);
2004f4a2713aSLionel Sambuc
2005f4a2713aSLionel Sambuc if (isa<ConstantPointerNull>(CV)) {
2006f4a2713aSLionel Sambuc AP.OutStreamer.EmitIntValue(0, Size);
2007f4a2713aSLionel Sambuc return;
2008f4a2713aSLionel Sambuc }
2009f4a2713aSLionel Sambuc
2010f4a2713aSLionel Sambuc if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(CV))
2011f4a2713aSLionel Sambuc return emitGlobalConstantDataSequential(CDS, AP);
2012f4a2713aSLionel Sambuc
2013f4a2713aSLionel Sambuc if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
2014f4a2713aSLionel Sambuc return emitGlobalConstantArray(CVA, AP);
2015f4a2713aSLionel Sambuc
2016f4a2713aSLionel Sambuc if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
2017f4a2713aSLionel Sambuc return emitGlobalConstantStruct(CVS, AP);
2018f4a2713aSLionel Sambuc
2019f4a2713aSLionel Sambuc if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
2020f4a2713aSLionel Sambuc // Look through bitcasts, which might not be able to be MCExpr'ized (e.g. of
2021f4a2713aSLionel Sambuc // vectors).
2022f4a2713aSLionel Sambuc if (CE->getOpcode() == Instruction::BitCast)
2023f4a2713aSLionel Sambuc return emitGlobalConstantImpl(CE->getOperand(0), AP);
2024f4a2713aSLionel Sambuc
2025f4a2713aSLionel Sambuc if (Size > 8) {
2026f4a2713aSLionel Sambuc // If the constant expression's size is greater than 64-bits, then we have
2027f4a2713aSLionel Sambuc // to emit the value in chunks. Try to constant fold the value and emit it
2028f4a2713aSLionel Sambuc // that way.
2029f4a2713aSLionel Sambuc Constant *New = ConstantFoldConstantExpression(CE, DL);
2030f4a2713aSLionel Sambuc if (New && New != CE)
2031f4a2713aSLionel Sambuc return emitGlobalConstantImpl(New, AP);
2032f4a2713aSLionel Sambuc }
2033f4a2713aSLionel Sambuc }
2034f4a2713aSLionel Sambuc
2035f4a2713aSLionel Sambuc if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
2036f4a2713aSLionel Sambuc return emitGlobalConstantVector(V, AP);
2037f4a2713aSLionel Sambuc
2038f4a2713aSLionel Sambuc // Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
2039f4a2713aSLionel Sambuc // thread the streamer with EmitValue.
2040*0a6a1f1dSLionel Sambuc AP.OutStreamer.EmitValue(AP.lowerConstant(CV), Size);
2041f4a2713aSLionel Sambuc }
2042f4a2713aSLionel Sambuc
2043f4a2713aSLionel Sambuc /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
EmitGlobalConstant(const Constant * CV)2044f4a2713aSLionel Sambuc void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
2045*0a6a1f1dSLionel Sambuc uint64_t Size =
2046*0a6a1f1dSLionel Sambuc TM.getSubtargetImpl()->getDataLayout()->getTypeAllocSize(CV->getType());
2047f4a2713aSLionel Sambuc if (Size)
2048f4a2713aSLionel Sambuc emitGlobalConstantImpl(CV, *this);
2049f4a2713aSLionel Sambuc else if (MAI->hasSubsectionsViaSymbols()) {
2050f4a2713aSLionel Sambuc // If the global has zero size, emit a single byte so that two labels don't
2051f4a2713aSLionel Sambuc // look like they are at the same location.
2052f4a2713aSLionel Sambuc OutStreamer.EmitIntValue(0, 1);
2053f4a2713aSLionel Sambuc }
2054f4a2713aSLionel Sambuc }
2055f4a2713aSLionel Sambuc
EmitMachineConstantPoolValue(MachineConstantPoolValue * MCPV)2056f4a2713aSLionel Sambuc void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
2057f4a2713aSLionel Sambuc // Target doesn't support this yet!
2058f4a2713aSLionel Sambuc llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
2059f4a2713aSLionel Sambuc }
2060f4a2713aSLionel Sambuc
printOffset(int64_t Offset,raw_ostream & OS) const2061f4a2713aSLionel Sambuc void AsmPrinter::printOffset(int64_t Offset, raw_ostream &OS) const {
2062f4a2713aSLionel Sambuc if (Offset > 0)
2063f4a2713aSLionel Sambuc OS << '+' << Offset;
2064f4a2713aSLionel Sambuc else if (Offset < 0)
2065f4a2713aSLionel Sambuc OS << Offset;
2066f4a2713aSLionel Sambuc }
2067f4a2713aSLionel Sambuc
2068f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2069f4a2713aSLionel Sambuc // Symbol Lowering Routines.
2070f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2071f4a2713aSLionel Sambuc
2072f4a2713aSLionel Sambuc /// GetTempSymbol - Return the MCSymbol corresponding to the assembler
2073f4a2713aSLionel Sambuc /// temporary label with the specified stem and unique ID.
GetTempSymbol(Twine Name,unsigned ID) const2074*0a6a1f1dSLionel Sambuc MCSymbol *AsmPrinter::GetTempSymbol(Twine Name, unsigned ID) const {
2075*0a6a1f1dSLionel Sambuc const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
2076*0a6a1f1dSLionel Sambuc return OutContext.GetOrCreateSymbol(Twine(DL->getPrivateGlobalPrefix()) +
2077f4a2713aSLionel Sambuc Name + Twine(ID));
2078f4a2713aSLionel Sambuc }
2079f4a2713aSLionel Sambuc
2080f4a2713aSLionel Sambuc /// GetTempSymbol - Return an assembler temporary label with the specified
2081f4a2713aSLionel Sambuc /// stem.
GetTempSymbol(Twine Name) const2082*0a6a1f1dSLionel Sambuc MCSymbol *AsmPrinter::GetTempSymbol(Twine Name) const {
2083*0a6a1f1dSLionel Sambuc const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
2084*0a6a1f1dSLionel Sambuc return OutContext.GetOrCreateSymbol(Twine(DL->getPrivateGlobalPrefix())+
2085f4a2713aSLionel Sambuc Name);
2086f4a2713aSLionel Sambuc }
2087f4a2713aSLionel Sambuc
2088f4a2713aSLionel Sambuc
GetBlockAddressSymbol(const BlockAddress * BA) const2089f4a2713aSLionel Sambuc MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const {
2090f4a2713aSLionel Sambuc return MMI->getAddrLabelSymbol(BA->getBasicBlock());
2091f4a2713aSLionel Sambuc }
2092f4a2713aSLionel Sambuc
GetBlockAddressSymbol(const BasicBlock * BB) const2093f4a2713aSLionel Sambuc MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BasicBlock *BB) const {
2094f4a2713aSLionel Sambuc return MMI->getAddrLabelSymbol(BB);
2095f4a2713aSLionel Sambuc }
2096f4a2713aSLionel Sambuc
2097f4a2713aSLionel Sambuc /// GetCPISymbol - Return the symbol for the specified constant pool entry.
GetCPISymbol(unsigned CPID) const2098f4a2713aSLionel Sambuc MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const {
2099*0a6a1f1dSLionel Sambuc const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
2100f4a2713aSLionel Sambuc return OutContext.GetOrCreateSymbol
2101*0a6a1f1dSLionel Sambuc (Twine(DL->getPrivateGlobalPrefix()) + "CPI" + Twine(getFunctionNumber())
2102f4a2713aSLionel Sambuc + "_" + Twine(CPID));
2103f4a2713aSLionel Sambuc }
2104f4a2713aSLionel Sambuc
2105f4a2713aSLionel Sambuc /// GetJTISymbol - Return the symbol for the specified jump table entry.
GetJTISymbol(unsigned JTID,bool isLinkerPrivate) const2106f4a2713aSLionel Sambuc MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const {
2107f4a2713aSLionel Sambuc return MF->getJTISymbol(JTID, OutContext, isLinkerPrivate);
2108f4a2713aSLionel Sambuc }
2109f4a2713aSLionel Sambuc
2110f4a2713aSLionel Sambuc /// GetJTSetSymbol - Return the symbol for the specified jump table .set
2111f4a2713aSLionel Sambuc /// FIXME: privatize to AsmPrinter.
GetJTSetSymbol(unsigned UID,unsigned MBBID) const2112f4a2713aSLionel Sambuc MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const {
2113*0a6a1f1dSLionel Sambuc const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
2114f4a2713aSLionel Sambuc return OutContext.GetOrCreateSymbol
2115*0a6a1f1dSLionel Sambuc (Twine(DL->getPrivateGlobalPrefix()) + Twine(getFunctionNumber()) + "_" +
2116f4a2713aSLionel Sambuc Twine(UID) + "_set_" + Twine(MBBID));
2117f4a2713aSLionel Sambuc }
2118f4a2713aSLionel Sambuc
getSymbolWithGlobalValueBase(const GlobalValue * GV,StringRef Suffix) const2119*0a6a1f1dSLionel Sambuc MCSymbol *AsmPrinter::getSymbolWithGlobalValueBase(const GlobalValue *GV,
2120*0a6a1f1dSLionel Sambuc StringRef Suffix) const {
2121*0a6a1f1dSLionel Sambuc return getObjFileLowering().getSymbolWithGlobalValueBase(GV, Suffix, *Mang,
2122*0a6a1f1dSLionel Sambuc TM);
2123f4a2713aSLionel Sambuc }
2124f4a2713aSLionel Sambuc
2125f4a2713aSLionel Sambuc /// GetExternalSymbolSymbol - Return the MCSymbol for the specified
2126f4a2713aSLionel Sambuc /// ExternalSymbol.
GetExternalSymbolSymbol(StringRef Sym) const2127f4a2713aSLionel Sambuc MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
2128f4a2713aSLionel Sambuc SmallString<60> NameStr;
2129f4a2713aSLionel Sambuc Mang->getNameWithPrefix(NameStr, Sym);
2130f4a2713aSLionel Sambuc return OutContext.GetOrCreateSymbol(NameStr.str());
2131f4a2713aSLionel Sambuc }
2132f4a2713aSLionel Sambuc
2133f4a2713aSLionel Sambuc
2134f4a2713aSLionel Sambuc
2135f4a2713aSLionel Sambuc /// PrintParentLoopComment - Print comments about parent loops of this one.
PrintParentLoopComment(raw_ostream & OS,const MachineLoop * Loop,unsigned FunctionNumber)2136f4a2713aSLionel Sambuc static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop,
2137f4a2713aSLionel Sambuc unsigned FunctionNumber) {
2138*0a6a1f1dSLionel Sambuc if (!Loop) return;
2139f4a2713aSLionel Sambuc PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber);
2140f4a2713aSLionel Sambuc OS.indent(Loop->getLoopDepth()*2)
2141f4a2713aSLionel Sambuc << "Parent Loop BB" << FunctionNumber << "_"
2142f4a2713aSLionel Sambuc << Loop->getHeader()->getNumber()
2143f4a2713aSLionel Sambuc << " Depth=" << Loop->getLoopDepth() << '\n';
2144f4a2713aSLionel Sambuc }
2145f4a2713aSLionel Sambuc
2146f4a2713aSLionel Sambuc
2147f4a2713aSLionel Sambuc /// PrintChildLoopComment - Print comments about child loops within
2148f4a2713aSLionel Sambuc /// the loop for this basic block, with nesting.
PrintChildLoopComment(raw_ostream & OS,const MachineLoop * Loop,unsigned FunctionNumber)2149f4a2713aSLionel Sambuc static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop,
2150f4a2713aSLionel Sambuc unsigned FunctionNumber) {
2151f4a2713aSLionel Sambuc // Add child loop information
2152*0a6a1f1dSLionel Sambuc for (const MachineLoop *CL : *Loop) {
2153*0a6a1f1dSLionel Sambuc OS.indent(CL->getLoopDepth()*2)
2154f4a2713aSLionel Sambuc << "Child Loop BB" << FunctionNumber << "_"
2155*0a6a1f1dSLionel Sambuc << CL->getHeader()->getNumber() << " Depth " << CL->getLoopDepth()
2156f4a2713aSLionel Sambuc << '\n';
2157*0a6a1f1dSLionel Sambuc PrintChildLoopComment(OS, CL, FunctionNumber);
2158f4a2713aSLionel Sambuc }
2159f4a2713aSLionel Sambuc }
2160f4a2713aSLionel Sambuc
2161f4a2713aSLionel Sambuc /// emitBasicBlockLoopComments - Pretty-print comments for basic blocks.
emitBasicBlockLoopComments(const MachineBasicBlock & MBB,const MachineLoopInfo * LI,const AsmPrinter & AP)2162f4a2713aSLionel Sambuc static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
2163f4a2713aSLionel Sambuc const MachineLoopInfo *LI,
2164f4a2713aSLionel Sambuc const AsmPrinter &AP) {
2165f4a2713aSLionel Sambuc // Add loop depth information
2166f4a2713aSLionel Sambuc const MachineLoop *Loop = LI->getLoopFor(&MBB);
2167*0a6a1f1dSLionel Sambuc if (!Loop) return;
2168f4a2713aSLionel Sambuc
2169f4a2713aSLionel Sambuc MachineBasicBlock *Header = Loop->getHeader();
2170f4a2713aSLionel Sambuc assert(Header && "No header for loop");
2171f4a2713aSLionel Sambuc
2172f4a2713aSLionel Sambuc // If this block is not a loop header, just print out what is the loop header
2173f4a2713aSLionel Sambuc // and return.
2174f4a2713aSLionel Sambuc if (Header != &MBB) {
2175f4a2713aSLionel Sambuc AP.OutStreamer.AddComment(" in Loop: Header=BB" +
2176f4a2713aSLionel Sambuc Twine(AP.getFunctionNumber())+"_" +
2177f4a2713aSLionel Sambuc Twine(Loop->getHeader()->getNumber())+
2178f4a2713aSLionel Sambuc " Depth="+Twine(Loop->getLoopDepth()));
2179f4a2713aSLionel Sambuc return;
2180f4a2713aSLionel Sambuc }
2181f4a2713aSLionel Sambuc
2182f4a2713aSLionel Sambuc // Otherwise, it is a loop header. Print out information about child and
2183f4a2713aSLionel Sambuc // parent loops.
2184f4a2713aSLionel Sambuc raw_ostream &OS = AP.OutStreamer.GetCommentOS();
2185f4a2713aSLionel Sambuc
2186f4a2713aSLionel Sambuc PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber());
2187f4a2713aSLionel Sambuc
2188f4a2713aSLionel Sambuc OS << "=>";
2189f4a2713aSLionel Sambuc OS.indent(Loop->getLoopDepth()*2-2);
2190f4a2713aSLionel Sambuc
2191f4a2713aSLionel Sambuc OS << "This ";
2192f4a2713aSLionel Sambuc if (Loop->empty())
2193f4a2713aSLionel Sambuc OS << "Inner ";
2194f4a2713aSLionel Sambuc OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n';
2195f4a2713aSLionel Sambuc
2196f4a2713aSLionel Sambuc PrintChildLoopComment(OS, Loop, AP.getFunctionNumber());
2197f4a2713aSLionel Sambuc }
2198f4a2713aSLionel Sambuc
2199f4a2713aSLionel Sambuc
2200f4a2713aSLionel Sambuc /// EmitBasicBlockStart - This method prints the label for the specified
2201f4a2713aSLionel Sambuc /// MachineBasicBlock, an alignment (if present) and a comment describing
2202f4a2713aSLionel Sambuc /// it if appropriate.
EmitBasicBlockStart(const MachineBasicBlock & MBB) const2203*0a6a1f1dSLionel Sambuc void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const {
2204f4a2713aSLionel Sambuc // Emit an alignment directive for this block, if needed.
2205*0a6a1f1dSLionel Sambuc if (unsigned Align = MBB.getAlignment())
2206f4a2713aSLionel Sambuc EmitAlignment(Align);
2207f4a2713aSLionel Sambuc
2208f4a2713aSLionel Sambuc // If the block has its address taken, emit any labels that were used to
2209f4a2713aSLionel Sambuc // reference the block. It is possible that there is more than one label
2210f4a2713aSLionel Sambuc // here, because multiple LLVM BB's may have been RAUW'd to this block after
2211f4a2713aSLionel Sambuc // the references were generated.
2212*0a6a1f1dSLionel Sambuc if (MBB.hasAddressTaken()) {
2213*0a6a1f1dSLionel Sambuc const BasicBlock *BB = MBB.getBasicBlock();
2214f4a2713aSLionel Sambuc if (isVerbose())
2215f4a2713aSLionel Sambuc OutStreamer.AddComment("Block address taken");
2216f4a2713aSLionel Sambuc
2217*0a6a1f1dSLionel Sambuc std::vector<MCSymbol*> Symbols = MMI->getAddrLabelSymbolToEmit(BB);
2218*0a6a1f1dSLionel Sambuc for (auto *Sym : Symbols)
2219*0a6a1f1dSLionel Sambuc OutStreamer.EmitLabel(Sym);
2220f4a2713aSLionel Sambuc }
2221f4a2713aSLionel Sambuc
2222f4a2713aSLionel Sambuc // Print some verbose block comments.
2223f4a2713aSLionel Sambuc if (isVerbose()) {
2224*0a6a1f1dSLionel Sambuc if (const BasicBlock *BB = MBB.getBasicBlock())
2225f4a2713aSLionel Sambuc if (BB->hasName())
2226f4a2713aSLionel Sambuc OutStreamer.AddComment("%" + BB->getName());
2227*0a6a1f1dSLionel Sambuc emitBasicBlockLoopComments(MBB, LI, *this);
2228f4a2713aSLionel Sambuc }
2229f4a2713aSLionel Sambuc
2230f4a2713aSLionel Sambuc // Print the main label for the block.
2231*0a6a1f1dSLionel Sambuc if (MBB.pred_empty() || isBlockOnlyReachableByFallthrough(&MBB)) {
2232*0a6a1f1dSLionel Sambuc if (isVerbose()) {
2233f4a2713aSLionel Sambuc // NOTE: Want this comment at start of line, don't emit with AddComment.
2234*0a6a1f1dSLionel Sambuc OutStreamer.emitRawComment(" BB#" + Twine(MBB.getNumber()) + ":", false);
2235f4a2713aSLionel Sambuc }
2236f4a2713aSLionel Sambuc } else {
2237*0a6a1f1dSLionel Sambuc OutStreamer.EmitLabel(MBB.getSymbol());
2238f4a2713aSLionel Sambuc }
2239f4a2713aSLionel Sambuc }
2240f4a2713aSLionel Sambuc
EmitVisibility(MCSymbol * Sym,unsigned Visibility,bool IsDefinition) const2241f4a2713aSLionel Sambuc void AsmPrinter::EmitVisibility(MCSymbol *Sym, unsigned Visibility,
2242f4a2713aSLionel Sambuc bool IsDefinition) const {
2243f4a2713aSLionel Sambuc MCSymbolAttr Attr = MCSA_Invalid;
2244f4a2713aSLionel Sambuc
2245f4a2713aSLionel Sambuc switch (Visibility) {
2246f4a2713aSLionel Sambuc default: break;
2247f4a2713aSLionel Sambuc case GlobalValue::HiddenVisibility:
2248f4a2713aSLionel Sambuc if (IsDefinition)
2249f4a2713aSLionel Sambuc Attr = MAI->getHiddenVisibilityAttr();
2250f4a2713aSLionel Sambuc else
2251f4a2713aSLionel Sambuc Attr = MAI->getHiddenDeclarationVisibilityAttr();
2252f4a2713aSLionel Sambuc break;
2253f4a2713aSLionel Sambuc case GlobalValue::ProtectedVisibility:
2254f4a2713aSLionel Sambuc Attr = MAI->getProtectedVisibilityAttr();
2255f4a2713aSLionel Sambuc break;
2256f4a2713aSLionel Sambuc }
2257f4a2713aSLionel Sambuc
2258f4a2713aSLionel Sambuc if (Attr != MCSA_Invalid)
2259f4a2713aSLionel Sambuc OutStreamer.EmitSymbolAttribute(Sym, Attr);
2260f4a2713aSLionel Sambuc }
2261f4a2713aSLionel Sambuc
2262f4a2713aSLionel Sambuc /// isBlockOnlyReachableByFallthough - Return true if the basic block has
2263f4a2713aSLionel Sambuc /// exactly one predecessor and the control transfer mechanism between
2264f4a2713aSLionel Sambuc /// the predecessor and this block is a fall-through.
2265f4a2713aSLionel Sambuc bool AsmPrinter::
isBlockOnlyReachableByFallthrough(const MachineBasicBlock * MBB) const2266f4a2713aSLionel Sambuc isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
2267f4a2713aSLionel Sambuc // If this is a landing pad, it isn't a fall through. If it has no preds,
2268f4a2713aSLionel Sambuc // then nothing falls through to it.
2269f4a2713aSLionel Sambuc if (MBB->isLandingPad() || MBB->pred_empty())
2270f4a2713aSLionel Sambuc return false;
2271f4a2713aSLionel Sambuc
2272f4a2713aSLionel Sambuc // If there isn't exactly one predecessor, it can't be a fall through.
2273*0a6a1f1dSLionel Sambuc if (MBB->pred_size() > 1)
2274f4a2713aSLionel Sambuc return false;
2275f4a2713aSLionel Sambuc
2276f4a2713aSLionel Sambuc // The predecessor has to be immediately before this block.
2277*0a6a1f1dSLionel Sambuc MachineBasicBlock *Pred = *MBB->pred_begin();
2278f4a2713aSLionel Sambuc if (!Pred->isLayoutSuccessor(MBB))
2279f4a2713aSLionel Sambuc return false;
2280f4a2713aSLionel Sambuc
2281f4a2713aSLionel Sambuc // If the block is completely empty, then it definitely does fall through.
2282f4a2713aSLionel Sambuc if (Pred->empty())
2283f4a2713aSLionel Sambuc return true;
2284f4a2713aSLionel Sambuc
2285f4a2713aSLionel Sambuc // Check the terminators in the previous blocks
2286*0a6a1f1dSLionel Sambuc for (const auto &MI : Pred->terminators()) {
2287f4a2713aSLionel Sambuc // If it is not a simple branch, we are in a table somewhere.
2288f4a2713aSLionel Sambuc if (!MI.isBranch() || MI.isIndirectBranch())
2289f4a2713aSLionel Sambuc return false;
2290f4a2713aSLionel Sambuc
2291*0a6a1f1dSLionel Sambuc // If we are the operands of one of the branches, this is not a fall
2292*0a6a1f1dSLionel Sambuc // through. Note that targets with delay slots will usually bundle
2293*0a6a1f1dSLionel Sambuc // terminators with the delay slot instruction.
2294*0a6a1f1dSLionel Sambuc for (ConstMIBundleOperands OP(&MI); OP.isValid(); ++OP) {
2295*0a6a1f1dSLionel Sambuc if (OP->isJTI())
2296f4a2713aSLionel Sambuc return false;
2297*0a6a1f1dSLionel Sambuc if (OP->isMBB() && OP->getMBB() == MBB)
2298f4a2713aSLionel Sambuc return false;
2299f4a2713aSLionel Sambuc }
2300f4a2713aSLionel Sambuc }
2301f4a2713aSLionel Sambuc
2302f4a2713aSLionel Sambuc return true;
2303f4a2713aSLionel Sambuc }
2304f4a2713aSLionel Sambuc
2305f4a2713aSLionel Sambuc
2306f4a2713aSLionel Sambuc
GetOrCreateGCPrinter(GCStrategy & S)2307*0a6a1f1dSLionel Sambuc GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy &S) {
2308*0a6a1f1dSLionel Sambuc if (!S.usesMetadata())
2309*0a6a1f1dSLionel Sambuc return nullptr;
2310*0a6a1f1dSLionel Sambuc
2311*0a6a1f1dSLionel Sambuc assert(!S.useStatepoints() && "statepoints do not currently support custom"
2312*0a6a1f1dSLionel Sambuc " stackmap formats, please see the documentation for a description of"
2313*0a6a1f1dSLionel Sambuc " the default format. If you really need a custom serialized format,"
2314*0a6a1f1dSLionel Sambuc " please file a bug");
2315f4a2713aSLionel Sambuc
2316f4a2713aSLionel Sambuc gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
2317*0a6a1f1dSLionel Sambuc gcp_map_type::iterator GCPI = GCMap.find(&S);
2318f4a2713aSLionel Sambuc if (GCPI != GCMap.end())
2319*0a6a1f1dSLionel Sambuc return GCPI->second.get();
2320f4a2713aSLionel Sambuc
2321*0a6a1f1dSLionel Sambuc const char *Name = S.getName().c_str();
2322f4a2713aSLionel Sambuc
2323f4a2713aSLionel Sambuc for (GCMetadataPrinterRegistry::iterator
2324f4a2713aSLionel Sambuc I = GCMetadataPrinterRegistry::begin(),
2325f4a2713aSLionel Sambuc E = GCMetadataPrinterRegistry::end(); I != E; ++I)
2326f4a2713aSLionel Sambuc if (strcmp(Name, I->getName()) == 0) {
2327*0a6a1f1dSLionel Sambuc std::unique_ptr<GCMetadataPrinter> GMP = I->instantiate();
2328*0a6a1f1dSLionel Sambuc GMP->S = &S;
2329*0a6a1f1dSLionel Sambuc auto IterBool = GCMap.insert(std::make_pair(&S, std::move(GMP)));
2330*0a6a1f1dSLionel Sambuc return IterBool.first->second.get();
2331f4a2713aSLionel Sambuc }
2332f4a2713aSLionel Sambuc
2333f4a2713aSLionel Sambuc report_fatal_error("no GCMetadataPrinter registered for GC: " + Twine(Name));
2334f4a2713aSLionel Sambuc }
2335*0a6a1f1dSLionel Sambuc
2336*0a6a1f1dSLionel Sambuc /// Pin vtable to this file.
~AsmPrinterHandler()2337*0a6a1f1dSLionel Sambuc AsmPrinterHandler::~AsmPrinterHandler() {}
2338