blob: 61f5672f7516e992f0777def9ca360d0e7a24667 [file] [log] [blame]
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
15#include "llvm/CodeGen/AsmPrinter.h"
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -070016#ifndef ANDROID_TARGET_BUILD
17# include "DwarfDebug.h"
18# include "DwarfException.h"
19#endif // ANDROID_TARGET_BUILD
Shih-wei Liaoe264f622010-02-10 11:10:31 -080020#include "llvm/Module.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080021#include "llvm/CodeGen/GCMetadataPrinter.h"
22#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineJumpTableInfo.h"
26#include "llvm/CodeGen/MachineLoopInfo.h"
27#include "llvm/CodeGen/MachineModuleInfo.h"
28#include "llvm/Analysis/ConstantFolding.h"
29#include "llvm/Analysis/DebugInfo.h"
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070030#include "llvm/MC/MCAsmInfo.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080031#include "llvm/MC/MCContext.h"
32#include "llvm/MC/MCExpr.h"
33#include "llvm/MC/MCInst.h"
34#include "llvm/MC/MCSection.h"
35#include "llvm/MC/MCStreamer.h"
36#include "llvm/MC/MCSymbol.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080037#include "llvm/Target/Mangler.h"
38#include "llvm/Target/TargetData.h"
39#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetLowering.h"
41#include "llvm/Target/TargetLoweringObjectFile.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080042#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner9fc5cdf2011-01-02 22:09:33 +000043#include "llvm/Assembly/Writer.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080044#include "llvm/ADT/SmallString.h"
45#include "llvm/ADT/Statistic.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080046#include "llvm/Support/ErrorHandling.h"
47#include "llvm/Support/Format.h"
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070048#include "llvm/Support/Timer.h"
Shih-wei Liaoe4454322010-04-07 12:21:42 -070049#include <ctype.h>
Shih-wei Liaoe264f622010-02-10 11:10:31 -080050using namespace llvm;
51
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070052static const char *DWARFGroupName = "DWARF Emission";
53static const char *DbgTimerName = "DWARF Debug Writer";
54static const char *EHTimerName = "DWARF Exception Writer";
55
Shih-wei Liaoe264f622010-02-10 11:10:31 -080056STATISTIC(EmittedInsts, "Number of machine instrs printed");
57
58char AsmPrinter::ID = 0;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070059
60typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type;
61static gcp_map_type &getGCMap(void *&P) {
62 if (P == 0)
63 P = new gcp_map_type();
64 return *(gcp_map_type*)P;
65}
66
67
Chris Lattner7516e9e2010-04-28 19:58:07 +000068/// getGVAlignmentLog2 - Return the alignment to use for the specified global
69/// value in log2 form. This rounds up to the preferred alignment if possible
70/// and legal.
71static unsigned getGVAlignmentLog2(const GlobalValue *GV, const TargetData &TD,
72 unsigned InBits = 0) {
73 unsigned NumBits = 0;
74 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
75 NumBits = TD.getPreferredAlignmentLog(GVar);
Jim Grosbach83d80832011-03-29 23:20:22 +000076
Chris Lattner7516e9e2010-04-28 19:58:07 +000077 // If InBits is specified, round it to it.
78 if (InBits > NumBits)
79 NumBits = InBits;
Jim Grosbach83d80832011-03-29 23:20:22 +000080
Chris Lattner7516e9e2010-04-28 19:58:07 +000081 // If the GV has a specified alignment, take it into account.
82 if (GV->getAlignment() == 0)
83 return NumBits;
Jim Grosbach83d80832011-03-29 23:20:22 +000084
Chris Lattner7516e9e2010-04-28 19:58:07 +000085 unsigned GVAlign = Log2_32(GV->getAlignment());
Jim Grosbach83d80832011-03-29 23:20:22 +000086
Chris Lattner7516e9e2010-04-28 19:58:07 +000087 // If the GVAlign is larger than NumBits, or if we are required to obey
88 // NumBits because the GV has an assigned section, obey it.
89 if (GVAlign > NumBits || GV->hasSection())
90 NumBits = GVAlign;
91 return NumBits;
92}
93
94
95
96
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070097AsmPrinter::AsmPrinter(TargetMachine &tm, MCStreamer &Streamer)
Owen Anderson75693222010-08-06 18:33:48 +000098 : MachineFunctionPass(ID),
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070099 TM(tm), MAI(tm.getMCAsmInfo()),
100 OutContext(Streamer.getContext()),
101 OutStreamer(Streamer),
102 LastMI(0), LastFn(0), Counter(~0U), SetCounter(0) {
103 DD = 0; DE = 0; MMI = 0; LI = 0;
104 GCMetadataPrinters = 0;
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800105 VerboseAsm = Streamer.isVerboseAsm();
106}
107
108AsmPrinter::~AsmPrinter() {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700109 assert(DD == 0 && DE == 0 && "Debug/EH info didn't get finalized");
Jim Grosbach83d80832011-03-29 23:20:22 +0000110
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700111 if (GCMetadataPrinters != 0) {
112 gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
Jim Grosbach83d80832011-03-29 23:20:22 +0000113
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700114 for (gcp_map_type::iterator I = GCMap.begin(), E = GCMap.end(); I != E; ++I)
115 delete I->second;
116 delete &GCMap;
117 GCMetadataPrinters = 0;
118 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000119
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800120 delete &OutStreamer;
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800121}
122
123/// getFunctionNumber - Return a unique ID for the current function.
124///
125unsigned AsmPrinter::getFunctionNumber() const {
126 return MF->getFunctionNumber();
127}
128
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700129const TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800130 return TM.getTargetLowering()->getObjFileLowering();
131}
132
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700133
134/// getTargetData - Return information about data layout.
135const TargetData &AsmPrinter::getTargetData() const {
136 return *TM.getTargetData();
137}
138
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800139/// getCurrentSection() - Return the current section we are emitting to.
140const MCSection *AsmPrinter::getCurrentSection() const {
141 return OutStreamer.getCurrentSection();
142}
143
144
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700145
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800146void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
147 AU.setPreservesAll();
148 MachineFunctionPass::getAnalysisUsage(AU);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700149 AU.addRequired<MachineModuleInfo>();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800150 AU.addRequired<GCModuleInfo>();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700151 if (isVerbose())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800152 AU.addRequired<MachineLoopInfo>();
153}
154
155bool AsmPrinter::doInitialization(Module &M) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700156 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
157 MMI->AnalyzeModule(M);
158
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800159 // Initialize TargetLoweringObjectFile.
160 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
161 .Initialize(OutContext, TM);
Jim Grosbach83d80832011-03-29 23:20:22 +0000162
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700163 Mang = new Mangler(OutContext, *TM.getTargetData());
Jim Grosbach83d80832011-03-29 23:20:22 +0000164
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800165 // Allow the target to emit any magic that it wants at the start of the file.
166 EmitStartOfAsmFile(M);
167
168 // Very minimal debug info. It is ignored if we emit actual debug info. If we
169 // don't, this at least helps the user find where a global came from.
170 if (MAI->hasSingleParameterDotFile()) {
171 // .file "foo.c"
172 OutStreamer.EmitFileDirective(M.getModuleIdentifier());
173 }
174
175 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
176 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
177 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
178 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700179 MP->beginAssembly(*this);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800180
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700181 // Emit module-level inline asm if it exists.
182 if (!M.getModuleInlineAsm().empty()) {
183 OutStreamer.AddComment("Start of file scope inline assembly");
184 OutStreamer.AddBlankLine();
Chris Lattnera38941d2010-11-17 07:53:40 +0000185 EmitInlineAsm(M.getModuleInlineAsm()+"\n");
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700186 OutStreamer.AddComment("End of file scope inline assembly");
187 OutStreamer.AddBlankLine();
188 }
189
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700190#ifndef ANDROID_TARGET_BUILD
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700191 if (MAI->doesSupportDebugInformation())
192 DD = new DwarfDebug(this, &M);
Anton Korobeynikovd7e8ddc2011-01-14 21:57:45 +0000193
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700194 if (MAI->doesSupportExceptionHandling())
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +0000195 switch (MAI->getExceptionHandlingType()) {
196 default:
197 case ExceptionHandling::DwarfTable:
198 DE = new DwarfTableException(this);
199 break;
200 case ExceptionHandling::DwarfCFI:
201 DE = new DwarfCFIException(this);
202 break;
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000203 case ExceptionHandling::ARM:
204 DE = new ARMException(this);
205 break;
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +0000206 }
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700207#endif // ANDROID_TARGET_BUILD
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800208
209 return false;
210}
211
212void AsmPrinter::EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const {
213 switch ((GlobalValue::LinkageTypes)Linkage) {
214 case GlobalValue::CommonLinkage:
215 case GlobalValue::LinkOnceAnyLinkage:
216 case GlobalValue::LinkOnceODRLinkage:
217 case GlobalValue::WeakAnyLinkage:
218 case GlobalValue::WeakODRLinkage:
Bill Wendlingf8239662010-07-01 21:55:59 +0000219 case GlobalValue::LinkerPrivateWeakLinkage:
Bill Wendling01085e62010-08-20 22:05:50 +0000220 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800221 if (MAI->getWeakDefDirective() != 0) {
222 // .globl _foo
223 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
Bill Wendling01085e62010-08-20 22:05:50 +0000224
225 if ((GlobalValue::LinkageTypes)Linkage !=
226 GlobalValue::LinkerPrivateWeakDefAutoLinkage)
227 // .weak_definition _foo
228 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_WeakDefinition);
229 else
230 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_WeakDefAutoPrivate);
Duncan Sandsf01b62d2010-05-12 07:11:33 +0000231 } else if (MAI->getLinkOnceDirective() != 0) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800232 // .globl _foo
233 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
Duncan Sandsf01b62d2010-05-12 07:11:33 +0000234 //NOTE: linkonce is handled by the section the symbol was assigned to.
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800235 } else {
236 // .weak _foo
237 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Weak);
238 }
239 break;
240 case GlobalValue::DLLExportLinkage:
241 case GlobalValue::AppendingLinkage:
242 // FIXME: appending linkage variables should go into a section of
243 // their name or something. For now, just emit them as external.
244 case GlobalValue::ExternalLinkage:
245 // If external or appending, declare as a global symbol.
246 // .globl _foo
247 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
248 break;
249 case GlobalValue::PrivateLinkage:
250 case GlobalValue::InternalLinkage:
Bill Wendling73b3a722010-07-01 22:38:24 +0000251 case GlobalValue::LinkerPrivateLinkage:
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800252 break;
253 default:
254 llvm_unreachable("Unknown linkage type!");
255 }
256}
257
258
259/// EmitGlobalVariable - Emit the specified global variable to the .s file.
260void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
Rafael Espindola84397472011-04-05 15:51:32 +0000261 if (GV->hasInitializer()) {
262 // Check to see if this is a special global used by LLVM, if so, emit it.
263 if (EmitSpecialLLVMGlobal(GV))
264 return;
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800265
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700266 if (isVerbose()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800267 WriteAsOperand(OutStreamer.GetCommentOS(), GV,
268 /*PrintType=*/false, GV->getParent());
269 OutStreamer.GetCommentOS() << '\n';
270 }
Eric Christopher04386ca2010-05-25 21:49:43 +0000271 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000272
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000273 MCSymbol *GVSym = Mang->getSymbol(GV);
Chris Lattnerbe9dfce2010-01-28 00:05:10 +0000274 EmitVisibility(GVSym, GV->getVisibility());
Chris Lattner74bfe212010-01-19 05:38:33 +0000275
Rafael Espindola84397472011-04-05 15:51:32 +0000276 if (!GV->hasInitializer()) // External globals require no extra code.
277 return;
278
Chris Lattnera800f7c2010-01-25 18:33:40 +0000279 if (MAI->hasDotTypeDotSizeDirective())
280 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
Jim Grosbach83d80832011-03-29 23:20:22 +0000281
Chris Lattner74bfe212010-01-19 05:38:33 +0000282 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
283
284 const TargetData *TD = TM.getTargetData();
Chris Lattnere87f7bb2010-04-28 19:58:07 +0000285 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
Jim Grosbach83d80832011-03-29 23:20:22 +0000286
Chris Lattner567dd1f2010-04-26 18:46:46 +0000287 // If the alignment is specified, we *must* obey it. Overaligning a global
288 // with a specified alignment is a prompt way to break globals emitted to
289 // sections and expected to be contiguous (e.g. ObjC metadata).
Chris Lattnere87f7bb2010-04-28 19:58:07 +0000290 unsigned AlignLog = getGVAlignmentLog2(GV, *TD);
Jim Grosbach83d80832011-03-29 23:20:22 +0000291
Chris Lattner9744d612010-01-19 05:51:42 +0000292 // Handle common and BSS local symbols (.lcomm).
293 if (GVKind.isCommon() || GVKind.isBSSLocal()) {
Chris Lattner74bfe212010-01-19 05:38:33 +0000294 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Jim Grosbach83d80832011-03-29 23:20:22 +0000295
Chris Lattner3f53c832010-04-04 18:52:31 +0000296 if (isVerbose()) {
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000297 WriteAsOperand(OutStreamer.GetCommentOS(), GV,
298 /*PrintType=*/false, GV->getParent());
299 OutStreamer.GetCommentOS() << '\n';
Chris Lattner74bfe212010-01-19 05:38:33 +0000300 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000301
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800302 // Handle common symbols.
303 if (GVKind.isCommon()) {
Chris Lattner82f9a8e2010-09-27 06:44:54 +0000304 unsigned Align = 1 << AlignLog;
305 if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
306 Align = 0;
Jim Grosbach83d80832011-03-29 23:20:22 +0000307
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800308 // .comm _foo, 42, 4
Chris Lattner82f9a8e2010-09-27 06:44:54 +0000309 OutStreamer.EmitCommonSymbol(GVSym, Size, Align);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800310 return;
311 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000312
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800313 // Handle local BSS symbols.
314 if (MAI->hasMachoZeroFillDirective()) {
315 const MCSection *TheSection =
316 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
317 // .zerofill __DATA, __bss, _foo, 400, 5
318 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
319 return;
320 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000321
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800322 if (MAI->hasLCOMMDirective()) {
323 // .lcomm _foo, 42
324 OutStreamer.EmitLocalCommonSymbol(GVSym, Size);
325 return;
326 }
Chris Lattner82f9a8e2010-09-27 06:44:54 +0000327
328 unsigned Align = 1 << AlignLog;
329 if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
330 Align = 0;
Jim Grosbach83d80832011-03-29 23:20:22 +0000331
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800332 // .local _foo
333 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Local);
334 // .comm _foo, 42, 4
Chris Lattner82f9a8e2010-09-27 06:44:54 +0000335 OutStreamer.EmitCommonSymbol(GVSym, Size, Align);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800336 return;
337 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000338
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800339 const MCSection *TheSection =
340 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
341
342 // Handle the zerofill directive on darwin, which is a special form of BSS
343 // emission.
344 if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
Chris Lattner0631a922010-04-27 07:41:44 +0000345 if (Size == 0) Size = 1; // zerofill of 0 bytes is undefined.
Jim Grosbach83d80832011-03-29 23:20:22 +0000346
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800347 // .globl _foo
348 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
349 // .zerofill __DATA, __common, _foo, 400, 5
350 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
351 return;
352 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000353
Eric Christopherfdc794a2010-05-25 21:28:50 +0000354 // Handle thread local data for mach-o which requires us to output an
355 // additional structure of data and mangle the original symbol so that we
356 // can reference it later.
Chris Lattnerc1f3acb2010-09-05 20:33:40 +0000357 //
358 // TODO: This should become an "emit thread local global" method on TLOF.
359 // All of this macho specific stuff should be sunk down into TLOFMachO and
360 // stuff like "TLSExtraDataSection" should no longer be part of the parent
361 // TLOF class. This will also make it more obvious that stuff like
362 // MCStreamer::EmitTBSSSymbol is macho specific and only called from macho
363 // specific code.
Eric Christopherfdc794a2010-05-25 21:28:50 +0000364 if (GVKind.isThreadLocal() && MAI->hasMachoTBSSDirective()) {
Eric Christopher0f986f62010-05-22 00:10:22 +0000365 // Emit the .tbss symbol
Jim Grosbach83d80832011-03-29 23:20:22 +0000366 MCSymbol *MangSym =
Eric Christopher0f986f62010-05-22 00:10:22 +0000367 OutContext.GetOrCreateSymbol(GVSym->getName() + Twine("$tlv$init"));
Jim Grosbach83d80832011-03-29 23:20:22 +0000368
Eric Christopherfdc794a2010-05-25 21:28:50 +0000369 if (GVKind.isThreadBSS())
370 OutStreamer.EmitTBSSSymbol(TheSection, MangSym, Size, 1 << AlignLog);
371 else if (GVKind.isThreadData()) {
372 OutStreamer.SwitchSection(TheSection);
373
Jim Grosbach83d80832011-03-29 23:20:22 +0000374 EmitAlignment(AlignLog, GV);
Eric Christopherfdc794a2010-05-25 21:28:50 +0000375 OutStreamer.EmitLabel(MangSym);
Jim Grosbach83d80832011-03-29 23:20:22 +0000376
Eric Christopherfdc794a2010-05-25 21:28:50 +0000377 EmitGlobalConstant(GV->getInitializer());
378 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000379
Eric Christopher0f986f62010-05-22 00:10:22 +0000380 OutStreamer.AddBlankLine();
Jim Grosbach83d80832011-03-29 23:20:22 +0000381
Eric Christopher0f986f62010-05-22 00:10:22 +0000382 // Emit the variable struct for the runtime.
Jim Grosbach83d80832011-03-29 23:20:22 +0000383 const MCSection *TLVSect
Eric Christopher0f986f62010-05-22 00:10:22 +0000384 = getObjFileLowering().getTLSExtraDataSection();
Jim Grosbach83d80832011-03-29 23:20:22 +0000385
Eric Christopher0f986f62010-05-22 00:10:22 +0000386 OutStreamer.SwitchSection(TLVSect);
387 // Emit the linkage here.
388 EmitLinkage(GV->getLinkage(), GVSym);
389 OutStreamer.EmitLabel(GVSym);
Jim Grosbach83d80832011-03-29 23:20:22 +0000390
Eric Christopher0f986f62010-05-22 00:10:22 +0000391 // Three pointers in size:
392 // - __tlv_bootstrap - used to make sure support exists
393 // - spare pointer, used when mapped by the runtime
394 // - pointer to mangled symbol above with initializer
395 unsigned PtrSize = TD->getPointerSizeInBits()/8;
Eric Christopherdbf4b2d2010-06-03 04:02:59 +0000396 OutStreamer.EmitSymbolValue(GetExternalSymbolSymbol("_tlv_bootstrap"),
Eric Christopher0f986f62010-05-22 00:10:22 +0000397 PtrSize, 0);
398 OutStreamer.EmitIntValue(0, PtrSize, 0);
399 OutStreamer.EmitSymbolValue(MangSym, PtrSize, 0);
Jim Grosbach83d80832011-03-29 23:20:22 +0000400
Eric Christopher0f986f62010-05-22 00:10:22 +0000401 OutStreamer.AddBlankLine();
Eric Christopher2d4ea3e2010-05-20 00:49:07 +0000402 return;
403 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800404
405 OutStreamer.SwitchSection(TheSection);
406
407 EmitLinkage(GV->getLinkage(), GVSym);
408 EmitAlignment(AlignLog, GV);
409
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800410 OutStreamer.EmitLabel(GVSym);
411
412 EmitGlobalConstant(GV->getInitializer());
413
414 if (MAI->hasDotTypeDotSizeDirective())
415 // .size foo, 42
416 OutStreamer.EmitELFSize(GVSym, MCConstantExpr::Create(Size, OutContext));
Jim Grosbach83d80832011-03-29 23:20:22 +0000417
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800418 OutStreamer.AddBlankLine();
419}
420
421/// EmitFunctionHeader - This method emits the header for the current
422/// function.
423void AsmPrinter::EmitFunctionHeader() {
424 // Print out constants referenced by the function
425 EmitConstantPool();
Jim Grosbach83d80832011-03-29 23:20:22 +0000426
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800427 // Print the 'header' of function.
428 const Function *F = MF->getFunction();
429
430 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
431 EmitVisibility(CurrentFnSym, F->getVisibility());
432
433 EmitLinkage(F->getLinkage(), CurrentFnSym);
434 EmitAlignment(MF->getAlignment(), F);
435
436 if (MAI->hasDotTypeDotSizeDirective())
437 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
438
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700439 if (isVerbose()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800440 WriteAsOperand(OutStreamer.GetCommentOS(), F,
441 /*PrintType=*/false, F->getParent());
442 OutStreamer.GetCommentOS() << '\n';
443 }
444
445 // Emit the CurrentFnSym. This is a virtual function to allow targets to
446 // do their wild and crazy things as required.
447 EmitFunctionEntryLabel();
Jim Grosbach83d80832011-03-29 23:20:22 +0000448
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700449 // If the function had address-taken blocks that got deleted, then we have
450 // references to the dangling symbols. Emit them at the start of the function
451 // so that we don't get references to undefined symbols.
452 std::vector<MCSymbol*> DeadBlockSyms;
453 MMI->takeDeletedSymbolsForFunction(F, DeadBlockSyms);
454 for (unsigned i = 0, e = DeadBlockSyms.size(); i != e; ++i) {
455 OutStreamer.AddComment("Address taken block that was later removed");
456 OutStreamer.EmitLabel(DeadBlockSyms[i]);
457 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000458
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800459 // Add some workaround for linkonce linkage on Cygwin\MinGW.
460 if (MAI->getLinkOnceDirective() != 0 &&
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700461 (F->hasLinkOnceLinkage() || F->hasWeakLinkage())) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800462 // FIXME: What is this?
Jim Grosbach83d80832011-03-29 23:20:22 +0000463 MCSymbol *FakeStub =
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700464 OutContext.GetOrCreateSymbol(Twine("Lllvm$workaround$fake$stub$")+
465 CurrentFnSym->getName());
466 OutStreamer.EmitLabel(FakeStub);
467 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000468
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800469 // Emit pre-function debug and/or EH information.
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700470#ifndef ANDROID_TARGET_BUILD
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700471 if (DE) {
Dan Gohmane2a95082010-06-18 15:56:31 +0000472 NamedRegionTimer T(EHTimerName, DWARFGroupName, TimePassesIsEnabled);
473 DE->BeginFunction(MF);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700474 }
475 if (DD) {
Dan Gohmane2a95082010-06-18 15:56:31 +0000476 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
477 DD->beginFunction(MF);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700478 }
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700479#endif // ANDROID_TARGET_BUILD
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800480}
481
482/// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the
483/// function. This can be overridden by targets as required to do custom stuff.
484void AsmPrinter::EmitFunctionEntryLabel() {
Chris Lattnerd095d342010-05-06 00:05:37 +0000485 // The function label could have already been emitted if two symbols end up
486 // conflicting due to asm renaming. Detect this and emit an error.
487 if (CurrentFnSym->isUndefined())
488 return OutStreamer.EmitLabel(CurrentFnSym);
489
490 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
491 "' label emitted multiple times to assembly file");
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800492}
493
494
Jim Grosbach83d80832011-03-29 23:20:22 +0000495static void EmitDebugLoc(DebugLoc DL, const MachineFunction *MF,
Devang Patelabb79d92010-06-29 22:29:15 +0000496 raw_ostream &CommentOS) {
497 const LLVMContext &Ctx = MF->getFunction()->getContext();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700498 if (!DL.isUnknown()) { // Print source line info.
Devang Patelabb79d92010-06-29 22:29:15 +0000499 DIScope Scope(DL.getScope(Ctx));
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800500 // Omit the directory, because it's likely to be long and uninteresting.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700501 if (Scope.Verify())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800502 CommentOS << Scope.getFilename();
503 else
504 CommentOS << "<unknown>";
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700505 CommentOS << ':' << DL.getLine();
506 if (DL.getCol() != 0)
507 CommentOS << ':' << DL.getCol();
Devang Patelabb79d92010-06-29 22:29:15 +0000508 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
509 if (!InlinedAtDL.isUnknown()) {
510 CommentOS << "[ ";
511 EmitDebugLoc(InlinedAtDL, MF, CommentOS);
512 CommentOS << " ]";
513 }
514 }
515}
516
517/// EmitComments - Pretty-print comments for instructions.
518static void EmitComments(const MachineInstr &MI, raw_ostream &CommentOS) {
519 const MachineFunction *MF = MI.getParent()->getParent();
520 const TargetMachine &TM = MF->getTarget();
Jim Grosbach83d80832011-03-29 23:20:22 +0000521
Devang Patelabb79d92010-06-29 22:29:15 +0000522 DebugLoc DL = MI.getDebugLoc();
523 if (!DL.isUnknown()) { // Print source line info.
524 EmitDebugLoc(DL, MF, CommentOS);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800525 CommentOS << '\n';
526 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000527
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800528 // Check for spills and reloads
529 int FI;
Jim Grosbach83d80832011-03-29 23:20:22 +0000530
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800531 const MachineFrameInfo *FrameInfo = MF->getFrameInfo();
Jim Grosbach83d80832011-03-29 23:20:22 +0000532
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800533 // We assume a single instruction only has a spill or reload, not
534 // both.
535 const MachineMemOperand *MMO;
536 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
537 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
538 MMO = *MI.memoperands_begin();
539 CommentOS << MMO->getSize() << "-byte Reload\n";
540 }
541 } else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
542 if (FrameInfo->isSpillSlotObjectIndex(FI))
543 CommentOS << MMO->getSize() << "-byte Folded Reload\n";
544 } else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
545 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
546 MMO = *MI.memoperands_begin();
547 CommentOS << MMO->getSize() << "-byte Spill\n";
548 }
549 } else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
550 if (FrameInfo->isSpillSlotObjectIndex(FI))
551 CommentOS << MMO->getSize() << "-byte Folded Spill\n";
552 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000553
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800554 // Check for spill-induced copies
Jakob Stoklund Olesendef3acb2010-07-16 04:45:42 +0000555 if (MI.getAsmPrinterFlag(MachineInstr::ReloadReuse))
556 CommentOS << " Reload Reuse\n";
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800557}
558
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700559/// EmitImplicitDef - This method emits the specified machine instruction
560/// that is an implicit def.
561static void EmitImplicitDef(const MachineInstr *MI, AsmPrinter &AP) {
562 unsigned RegNo = MI->getOperand(0).getReg();
563 AP.OutStreamer.AddComment(Twine("implicit-def: ") +
564 AP.TM.getRegisterInfo()->getName(RegNo));
565 AP.OutStreamer.AddBlankLine();
566}
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800567
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700568static void EmitKill(const MachineInstr *MI, AsmPrinter &AP) {
569 std::string Str = "kill:";
570 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
571 const MachineOperand &Op = MI->getOperand(i);
572 assert(Op.isReg() && "KILL instruction must have only register operands");
573 Str += ' ';
574 Str += AP.TM.getRegisterInfo()->getName(Op.getReg());
575 Str += (Op.isDef() ? "<def>" : "<kill>");
576 }
577 AP.OutStreamer.AddComment(Str);
578 AP.OutStreamer.AddBlankLine();
579}
580
581/// EmitDebugValueComment - This method handles the target-independent form
582/// of DBG_VALUE, returning true if it was able to do so. A false return
583/// means the target will need to handle MI in EmitInstruction.
584static bool EmitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) {
585 // This code handles only the 3-operand target-independent form.
586 if (MI->getNumOperands() != 3)
587 return false;
588
589 SmallString<128> Str;
590 raw_svector_ostream OS(Str);
591 OS << '\t' << AP.MAI->getCommentString() << "DEBUG_VALUE: ";
592
593 // cast away const; DIetc do not take const operands for some reason.
594 DIVariable V(const_cast<MDNode*>(MI->getOperand(2).getMetadata()));
Devang Patel77defb52010-04-29 18:52:10 +0000595 if (V.getContext().isSubprogram())
Devang Patel19302aa2010-05-07 18:11:54 +0000596 OS << DISubprogram(V.getContext()).getDisplayName() << ":";
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700597 OS << V.getName() << " <- ";
598
599 // Register or immediate value. Register 0 means undef.
600 if (MI->getOperand(0).isFPImm()) {
601 APFloat APF = APFloat(MI->getOperand(0).getFPImm()->getValueAPF());
602 if (MI->getOperand(0).getFPImm()->getType()->isFloatTy()) {
603 OS << (double)APF.convertToFloat();
604 } else if (MI->getOperand(0).getFPImm()->getType()->isDoubleTy()) {
605 OS << APF.convertToDouble();
606 } else {
607 // There is no good way to print long double. Convert a copy to
608 // double. Ah well, it's only a comment.
609 bool ignored;
610 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
611 &ignored);
612 OS << "(long double) " << APF.convertToDouble();
613 }
614 } else if (MI->getOperand(0).isImm()) {
615 OS << MI->getOperand(0).getImm();
616 } else {
617 assert(MI->getOperand(0).isReg() && "Unknown operand type");
618 if (MI->getOperand(0).getReg() == 0) {
619 // Suppress offset, it is not meaningful here.
620 OS << "undef";
621 // NOTE: Want this comment at start of line, don't emit with AddComment.
622 AP.OutStreamer.EmitRawText(OS.str());
623 return true;
624 }
625 OS << AP.TM.getRegisterInfo()->getName(MI->getOperand(0).getReg());
626 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000627
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700628 OS << '+' << MI->getOperand(1).getImm();
629 // NOTE: Want this comment at start of line, don't emit with AddComment.
630 AP.OutStreamer.EmitRawText(OS.str());
631 return true;
632}
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800633
634/// EmitFunctionBody - This method emits the body and trailer for a
635/// function.
636void AsmPrinter::EmitFunctionBody() {
637 // Emit target-specific gunk before the function body.
638 EmitFunctionBodyStart();
Jim Grosbach83d80832011-03-29 23:20:22 +0000639
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700640 bool ShouldPrintDebugScopes = DD && MMI->hasDebugInfo();
Jim Grosbach83d80832011-03-29 23:20:22 +0000641
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800642 // Print out code for the function.
643 bool HasAnyRealCode = false;
Bill Wendling5a1bec12010-07-16 22:51:10 +0000644 const MachineInstr *LastMI = 0;
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800645 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
646 I != E; ++I) {
647 // Print a label for the basic block.
648 EmitBasicBlockStart(I);
649 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
650 II != IE; ++II) {
Bill Wendling5a1bec12010-07-16 22:51:10 +0000651 LastMI = II;
652
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800653 // Print the assembly for the instruction.
Dale Johannesen1ffdb1f2010-05-01 16:41:11 +0000654 if (!II->isLabel() && !II->isImplicitDef() && !II->isKill() &&
655 !II->isDebugValue()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800656 HasAnyRealCode = true;
657
Evan Cheng53a0cbf2010-04-27 19:38:45 +0000658 ++EmittedInsts;
Shih-wei Liaoa95f5892010-09-11 01:42:09 -0700659 }
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700660#ifndef ANDROID_TARGET_BUILD
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700661 if (ShouldPrintDebugScopes) {
Dan Gohmane2a95082010-06-18 15:56:31 +0000662 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Devang Patel2bfdc272010-10-26 17:49:02 +0000663 DD->beginInstruction(II);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700664 }
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700665#endif // ANDROID_TARGET_BUILD
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800666
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700667 if (isVerbose())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800668 EmitComments(*II, OutStreamer.GetCommentOS());
669
670 switch (II->getOpcode()) {
Bill Wendlinga02effc2010-07-16 22:20:36 +0000671 case TargetOpcode::PROLOG_LABEL:
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800672 case TargetOpcode::EH_LABEL:
673 case TargetOpcode::GC_LABEL:
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700674 OutStreamer.EmitLabel(II->getOperand(0).getMCSymbol());
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800675 break;
676 case TargetOpcode::INLINEASM:
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700677 EmitInlineAsm(II);
678 break;
679 case TargetOpcode::DBG_VALUE:
680 if (isVerbose()) {
681 if (!EmitDebugValueComment(II, *this))
682 EmitInstruction(II);
683 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800684 break;
685 case TargetOpcode::IMPLICIT_DEF:
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700686 if (isVerbose()) EmitImplicitDef(II, *this);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800687 break;
688 case TargetOpcode::KILL:
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700689 if (isVerbose()) EmitKill(II, *this);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800690 break;
691 default:
692 EmitInstruction(II);
693 break;
694 }
695
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700696#ifndef ANDROID_TARGET_BUILD
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700697 if (ShouldPrintDebugScopes) {
Dan Gohmane2a95082010-06-18 15:56:31 +0000698 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Devang Patel2bfdc272010-10-26 17:49:02 +0000699 DD->endInstruction(II);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700700 }
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700701#endif // ANDROID_TARGET_BUILD
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800702 }
703 }
Bill Wendling5a1bec12010-07-16 22:51:10 +0000704
705 // If the last instruction was a prolog label, then we have a situation where
706 // we emitted a prolog but no function body. This results in the ending prolog
707 // label equaling the end of function label and an invalid "row" in the
708 // FDE. We need to emit a noop in this situation so that the FDE's rows are
709 // valid.
Bill Wendlingf7e17752010-07-17 19:18:44 +0000710 bool RequiresNoop = LastMI && LastMI->isPrologLabel();
Bill Wendling5a1bec12010-07-16 22:51:10 +0000711
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800712 // If the function is empty and the object file uses .subsections_via_symbols,
713 // then we need to emit *something* to the function body to prevent the
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700714 // labels from collapsing together. Just emit a noop.
Bill Wendling5a1bec12010-07-16 22:51:10 +0000715 if ((MAI->hasSubsectionsViaSymbols() && !HasAnyRealCode) || RequiresNoop) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700716 MCInst Noop;
717 TM.getInstrInfo()->getNoopForMachoTarget(Noop);
718 if (Noop.getOpcode()) {
719 OutStreamer.AddComment("avoids zero-length function");
720 OutStreamer.EmitInstruction(Noop);
721 } else // Target not mc-ized yet.
722 OutStreamer.EmitRawText(StringRef("\tnop\n"));
723 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000724
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800725 // Emit target-specific gunk after the function body.
726 EmitFunctionBodyEnd();
Jim Grosbach83d80832011-03-29 23:20:22 +0000727
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700728 // If the target wants a .size directive for the size of the function, emit
729 // it.
730 if (MAI->hasDotTypeDotSizeDirective()) {
731 // Create a symbol for the end of function, so we can get the size as
732 // difference between the function label and the temp label.
733 MCSymbol *FnEndLabel = OutContext.CreateTempSymbol();
734 OutStreamer.EmitLabel(FnEndLabel);
Jim Grosbach83d80832011-03-29 23:20:22 +0000735
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700736 const MCExpr *SizeExp =
737 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(FnEndLabel, OutContext),
738 MCSymbolRefExpr::Create(CurrentFnSym, OutContext),
739 OutContext);
740 OutStreamer.EmitELFSize(CurrentFnSym, SizeExp);
741 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000742
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800743 // Emit post-function debug information.
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700744#ifndef ANDROID_TARGET_BUILD
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700745 if (DD) {
Dan Gohmane2a95082010-06-18 15:56:31 +0000746 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
747 DD->endFunction(MF);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700748 }
749 if (DE) {
Dan Gohmane2a95082010-06-18 15:56:31 +0000750 NamedRegionTimer T(EHTimerName, DWARFGroupName, TimePassesIsEnabled);
751 DE->EndFunction();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700752 }
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700753#endif // ANDROID_TARGET_BUILD
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700754 MMI->EndFunction();
Jim Grosbach83d80832011-03-29 23:20:22 +0000755
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800756 // Print out jump tables referenced by the function.
757 EmitJumpTableInfo();
Jim Grosbach83d80832011-03-29 23:20:22 +0000758
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800759 OutStreamer.AddBlankLine();
760}
761
Devang Patel7c3efb12010-04-28 01:39:28 +0000762/// getDebugValueLocation - Get location information encoded by DBG_VALUE
763/// operands.
Jim Grosbach83d80832011-03-29 23:20:22 +0000764MachineLocation AsmPrinter::
765getDebugValueLocation(const MachineInstr *MI) const {
Devang Patel7c3efb12010-04-28 01:39:28 +0000766 // Target specific DBG_VALUE instructions are handled by each target.
767 return MachineLocation();
768}
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800769
770bool AsmPrinter::doFinalization(Module &M) {
771 // Emit global variables.
772 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
773 I != E; ++I)
774 EmitGlobalVariable(I);
Rafael Espindola1ffb5332011-01-28 03:20:10 +0000775
776 // Emit visibility info for declarations
777 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
778 const Function &F = *I;
779 if (!F.isDeclaration())
780 continue;
781 GlobalValue::VisibilityTypes V = F.getVisibility();
782 if (V == GlobalValue::DefaultVisibility)
783 continue;
784
785 MCSymbol *Name = Mang->getSymbol(&F);
Stuart Hastings5129bde2011-02-23 02:27:05 +0000786 EmitVisibility(Name, V, false);
Rafael Espindola1ffb5332011-01-28 03:20:10 +0000787 }
788
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700789 // Finalize debug and EH information.
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700790#ifndef ANDROID_TARGET_BUILD
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700791 if (DE) {
Dan Gohmane2a95082010-06-18 15:56:31 +0000792 {
793 NamedRegionTimer T(EHTimerName, DWARFGroupName, TimePassesIsEnabled);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700794 DE->EndModule();
795 }
796 delete DE; DE = 0;
797 }
798 if (DD) {
Dan Gohmane2a95082010-06-18 15:56:31 +0000799 {
800 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700801 DD->endModule();
802 }
803 delete DD; DD = 0;
804 }
Shih-wei Liaoa59a85f2010-04-29 00:24:07 -0700805#endif // ANDROID_TARGET_BUILD
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800806
807 // If the target wants to know about weak references, print them all.
808 if (MAI->getWeakRefDirective()) {
809 // FIXME: This is not lazy, it would be nice to only print weak references
810 // to stuff that is actually used. Note that doing so would require targets
811 // to notice uses in operands (due to constant exprs etc). This should
812 // happen with the MC stuff eventually.
813
814 // Print out module-level global variables here.
815 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
816 I != E; ++I) {
817 if (!I->hasExternalWeakLinkage()) continue;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700818 OutStreamer.EmitSymbolAttribute(Mang->getSymbol(I), MCSA_WeakReference);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800819 }
Jim Grosbach83d80832011-03-29 23:20:22 +0000820
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800821 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
822 if (!I->hasExternalWeakLinkage()) continue;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700823 OutStreamer.EmitSymbolAttribute(Mang->getSymbol(I), MCSA_WeakReference);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800824 }
825 }
826
827 if (MAI->hasSetDirective()) {
828 OutStreamer.AddBlankLine();
829 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
830 I != E; ++I) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700831 MCSymbol *Name = Mang->getSymbol(I);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800832
833 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700834 MCSymbol *Target = Mang->getSymbol(GV);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800835
836 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
837 OutStreamer.EmitSymbolAttribute(Name, MCSA_Global);
838 else if (I->hasWeakLinkage())
839 OutStreamer.EmitSymbolAttribute(Name, MCSA_WeakReference);
840 else
841 assert(I->hasLocalLinkage() && "Invalid alias linkage");
842
843 EmitVisibility(Name, I->getVisibility());
844
845 // Emit the directives as assignments aka .set:
Jim Grosbach83d80832011-03-29 23:20:22 +0000846 OutStreamer.EmitAssignment(Name,
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800847 MCSymbolRefExpr::Create(Target, OutContext));
848 }
849 }
850
851 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
852 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
853 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
854 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700855 MP->finishAssembly(*this);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800856
857 // If we don't have any trampolines, then we don't require stack memory
858 // to be executable. Some targets have a directive to declare this.
859 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
860 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700861 if (const MCSection *S = MAI->getNonexecutableStackSection(OutContext))
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800862 OutStreamer.SwitchSection(S);
Jim Grosbach83d80832011-03-29 23:20:22 +0000863
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800864 // Allow the target to emit any magic that it wants at the end of the file,
865 // after everything else has gone out.
866 EmitEndOfAsmFile(M);
Jim Grosbach83d80832011-03-29 23:20:22 +0000867
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800868 delete Mang; Mang = 0;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700869 MMI = 0;
Jim Grosbach83d80832011-03-29 23:20:22 +0000870
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800871 OutStreamer.Finish();
872 return false;
873}
874
875void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
876 this->MF = &MF;
877 // Get the function symbol.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700878 CurrentFnSym = Mang->getSymbol(MF.getFunction());
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800879
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700880 if (isVerbose())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800881 LI = &getAnalysis<MachineLoopInfo>();
882}
883
884namespace {
885 // SectionCPs - Keep track the alignment, constpool entries per Section.
886 struct SectionCPs {
887 const MCSection *S;
888 unsigned Alignment;
889 SmallVector<unsigned, 4> CPEs;
890 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
891 };
892}
893
894/// EmitConstantPool - Print to the current output stream assembly
895/// representations of the constants in the constant pool MCP. This is
896/// used to print out constants which have been "spilled to memory" by
897/// the code generator.
898///
899void AsmPrinter::EmitConstantPool() {
900 const MachineConstantPool *MCP = MF->getConstantPool();
901 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
902 if (CP.empty()) return;
903
904 // Calculate sections for constant pool entries. We collect entries to go into
905 // the same section together to reduce amount of section switch statements.
906 SmallVector<SectionCPs, 4> CPSections;
907 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
908 const MachineConstantPoolEntry &CPE = CP[i];
909 unsigned Align = CPE.getAlignment();
Jim Grosbach83d80832011-03-29 23:20:22 +0000910
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800911 SectionKind Kind;
912 switch (CPE.getRelocationInfo()) {
913 default: llvm_unreachable("Unknown section kind");
914 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
915 case 1:
916 Kind = SectionKind::getReadOnlyWithRelLocal();
917 break;
918 case 0:
919 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
920 case 4: Kind = SectionKind::getMergeableConst4(); break;
921 case 8: Kind = SectionKind::getMergeableConst8(); break;
922 case 16: Kind = SectionKind::getMergeableConst16();break;
923 default: Kind = SectionKind::getMergeableConst(); break;
924 }
925 }
926
927 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Jim Grosbach83d80832011-03-29 23:20:22 +0000928
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800929 // The number of sections are small, just do a linear search from the
930 // last section to the first.
931 bool Found = false;
932 unsigned SecIdx = CPSections.size();
933 while (SecIdx != 0) {
934 if (CPSections[--SecIdx].S == S) {
935 Found = true;
936 break;
937 }
938 }
939 if (!Found) {
940 SecIdx = CPSections.size();
941 CPSections.push_back(SectionCPs(S, Align));
942 }
943
944 if (Align > CPSections[SecIdx].Alignment)
945 CPSections[SecIdx].Alignment = Align;
946 CPSections[SecIdx].CPEs.push_back(i);
947 }
948
949 // Now print stuff into the calculated sections.
950 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
951 OutStreamer.SwitchSection(CPSections[i].S);
952 EmitAlignment(Log2_32(CPSections[i].Alignment));
953
954 unsigned Offset = 0;
955 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
956 unsigned CPI = CPSections[i].CPEs[j];
957 MachineConstantPoolEntry CPE = CP[CPI];
958
959 // Emit inter-object padding for alignment.
960 unsigned AlignMask = CPE.getAlignment() - 1;
961 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
962 OutStreamer.EmitFill(NewOffset - Offset, 0/*fillval*/, 0/*addrspace*/);
963
964 const Type *Ty = CPE.getType();
965 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800966 OutStreamer.EmitLabel(GetCPISymbol(CPI));
967
968 if (CPE.isMachineConstantPoolEntry())
969 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
970 else
971 EmitGlobalConstant(CPE.Val.ConstVal);
972 }
973 }
974}
975
976/// EmitJumpTableInfo - Print assembly representations of the jump tables used
Jim Grosbach83d80832011-03-29 23:20:22 +0000977/// by the current function to the current output stream.
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800978///
979void AsmPrinter::EmitJumpTableInfo() {
980 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
981 if (MJTI == 0) return;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700982 if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) return;
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800983 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
984 if (JT.empty()) return;
985
Jim Grosbach83d80832011-03-29 23:20:22 +0000986 // Pick the directive to use to print the jump table entries, and switch to
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800987 // the appropriate section.
988 const Function *F = MF->getFunction();
989 bool JTInDiffSection = false;
990 if (// In PIC mode, we need to emit the jump table to the same section as the
991 // function body itself, otherwise the label differences won't make sense.
992 // FIXME: Need a better predicate for this: what about custom entries?
993 MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 ||
994 // We should also do if the section name is NULL or function is declared
995 // in discardable section
996 // FIXME: this isn't the right predicate, should be based on the MCSection
997 // for the function.
998 F->isWeakForLinker()) {
999 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F,Mang,TM));
1000 } else {
1001 // Otherwise, drop it in the readonly section.
Jim Grosbach83d80832011-03-29 23:20:22 +00001002 const MCSection *ReadOnlySection =
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001003 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
1004 OutStreamer.SwitchSection(ReadOnlySection);
1005 JTInDiffSection = true;
1006 }
1007
1008 EmitAlignment(Log2_32(MJTI->getEntryAlignment(*TM.getTargetData())));
Jim Grosbach83d80832011-03-29 23:20:22 +00001009
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001010 for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) {
1011 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach83d80832011-03-29 23:20:22 +00001012
1013 // If this jump table was deleted, ignore it.
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001014 if (JTBBs.empty()) continue;
1015
1016 // For the EK_LabelDifference32 entry, if the target supports .set, emit a
1017 // .set directive for each unique entry. This reduces the number of
1018 // relocations the assembler will generate for the jump table.
1019 if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 &&
1020 MAI->hasSetDirective()) {
1021 SmallPtrSet<const MachineBasicBlock*, 16> EmittedSets;
1022 const TargetLowering *TLI = TM.getTargetLowering();
1023 const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext);
1024 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
1025 const MachineBasicBlock *MBB = JTBBs[ii];
1026 if (!EmittedSets.insert(MBB)) continue;
Jim Grosbach83d80832011-03-29 23:20:22 +00001027
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001028 // .set LJTSet, LBB32-base
1029 const MCExpr *LHS =
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001030 MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001031 OutStreamer.EmitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()),
1032 MCBinaryExpr::CreateSub(LHS, Base, OutContext));
1033 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001034 }
1035
Duncan Sandsab4c3662011-02-15 09:23:02 +00001036 // On some targets (e.g. Darwin) we want to emit two consecutive labels
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001037 // before each jump table. The first label is never referenced, but tells
1038 // the assembler and linker the extents of the jump table object. The
1039 // second label is actually referenced by the code.
1040 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0])
1041 // FIXME: This doesn't have to have any specific name, just any randomly
1042 // named and numbered 'l' label would work. Simplify GetJTISymbol.
1043 OutStreamer.EmitLabel(GetJTISymbol(JTI, true));
1044
1045 OutStreamer.EmitLabel(GetJTISymbol(JTI));
1046
1047 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
1048 EmitJumpTableEntry(MJTI, JTBBs[ii], JTI);
1049 }
1050}
1051
1052/// EmitJumpTableEntry - Emit a jump table entry for the specified MBB to the
1053/// current stream.
1054void AsmPrinter::EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
1055 const MachineBasicBlock *MBB,
1056 unsigned UID) const {
Jakob Stoklund Olesene5005d02011-02-09 21:52:06 +00001057 assert(MBB && MBB->getNumber() >= 0 && "Invalid basic block");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001058 const MCExpr *Value = 0;
1059 switch (MJTI->getEntryKind()) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001060 case MachineJumpTableInfo::EK_Inline:
1061 llvm_unreachable("Cannot emit EK_Inline jump table entry"); break;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001062 case MachineJumpTableInfo::EK_Custom32:
1063 Value = TM.getTargetLowering()->LowerCustomJumpTableEntry(MJTI, MBB, UID,
1064 OutContext);
1065 break;
1066 case MachineJumpTableInfo::EK_BlockAddress:
1067 // EK_BlockAddress - Each entry is a plain address of block, e.g.:
1068 // .word LBB123
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001069 Value = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001070 break;
1071 case MachineJumpTableInfo::EK_GPRel32BlockAddress: {
1072 // EK_GPRel32BlockAddress - Each entry is an address of block, encoded
1073 // with a relocation as gp-relative, e.g.:
1074 // .gprel32 LBB123
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001075 MCSymbol *MBBSym = MBB->getSymbol();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001076 OutStreamer.EmitGPRel32Value(MCSymbolRefExpr::Create(MBBSym, OutContext));
1077 return;
1078 }
1079
1080 case MachineJumpTableInfo::EK_LabelDifference32: {
1081 // EK_LabelDifference32 - Each entry is the address of the block minus
1082 // the address of the jump table. This is used for PIC jump tables where
1083 // gprel32 is not supported. e.g.:
1084 // .word LBB123 - LJTI1_2
1085 // If the .set directive is supported, this is emitted as:
1086 // .set L4_5_set_123, LBB123 - LJTI1_2
1087 // .word L4_5_set_123
Jim Grosbach83d80832011-03-29 23:20:22 +00001088
1089 // If we have emitted set directives for the jump table entries, print
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001090 // them rather than the entries themselves. If we're emitting PIC, then
1091 // emit the table entries as differences between two text section labels.
1092 if (MAI->hasSetDirective()) {
1093 // If we used .set, reference the .set's symbol.
1094 Value = MCSymbolRefExpr::Create(GetJTSetSymbol(UID, MBB->getNumber()),
1095 OutContext);
1096 break;
1097 }
1098 // Otherwise, use the difference as the jump table entry.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001099 Value = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001100 const MCExpr *JTI = MCSymbolRefExpr::Create(GetJTISymbol(UID), OutContext);
1101 Value = MCBinaryExpr::CreateSub(Value, JTI, OutContext);
1102 break;
1103 }
1104 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001105
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001106 assert(Value && "Unknown entry kind!");
Jim Grosbach83d80832011-03-29 23:20:22 +00001107
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001108 unsigned EntrySize = MJTI->getEntrySize(*TM.getTargetData());
1109 OutStreamer.EmitValue(Value, EntrySize, /*addrspace*/0);
1110}
1111
1112
1113/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
1114/// special global used by LLVM. If so, emit it and return true, otherwise
1115/// do nothing and return false.
1116bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
1117 if (GV->getName() == "llvm.used") {
1118 if (MAI->hasNoDeadStrip()) // No need to emit this at all.
1119 EmitLLVMUsedList(GV->getInitializer());
1120 return true;
1121 }
1122
1123 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
1124 if (GV->getSection() == "llvm.metadata" ||
1125 GV->hasAvailableExternallyLinkage())
1126 return true;
Jim Grosbach83d80832011-03-29 23:20:22 +00001127
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001128 if (!GV->hasAppendingLinkage()) return false;
1129
1130 assert(GV->hasInitializer() && "Not a special LLVM global!");
Jim Grosbach83d80832011-03-29 23:20:22 +00001131
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001132 const TargetData *TD = TM.getTargetData();
1133 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
1134 if (GV->getName() == "llvm.global_ctors") {
1135 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner7516e9e2010-04-28 19:58:07 +00001136 EmitAlignment(Align);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001137 EmitXXStructorList(GV->getInitializer());
Jim Grosbach83d80832011-03-29 23:20:22 +00001138
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001139 if (TM.getRelocationModel() == Reloc::Static &&
1140 MAI->hasStaticCtorDtorReferenceInStaticMode()) {
1141 StringRef Sym(".constructors_used");
1142 OutStreamer.EmitSymbolAttribute(OutContext.GetOrCreateSymbol(Sym),
1143 MCSA_Reference);
1144 }
1145 return true;
Jim Grosbach83d80832011-03-29 23:20:22 +00001146 }
1147
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001148 if (GV->getName() == "llvm.global_dtors") {
1149 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner7516e9e2010-04-28 19:58:07 +00001150 EmitAlignment(Align);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001151 EmitXXStructorList(GV->getInitializer());
1152
1153 if (TM.getRelocationModel() == Reloc::Static &&
1154 MAI->hasStaticCtorDtorReferenceInStaticMode()) {
1155 StringRef Sym(".destructors_used");
1156 OutStreamer.EmitSymbolAttribute(OutContext.GetOrCreateSymbol(Sym),
1157 MCSA_Reference);
1158 }
1159 return true;
1160 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001161
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001162 return false;
1163}
1164
1165/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
1166/// global in the specified llvm.used list for which emitUsedDirectiveFor
1167/// is true, as being used with this directive.
1168void AsmPrinter::EmitLLVMUsedList(Constant *List) {
1169 // Should be an array of 'i8*'.
1170 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
1171 if (InitList == 0) return;
Jim Grosbach83d80832011-03-29 23:20:22 +00001172
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001173 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1174 const GlobalValue *GV =
1175 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
1176 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang))
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001177 OutStreamer.EmitSymbolAttribute(Mang->getSymbol(GV), MCSA_NoDeadStrip);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001178 }
1179}
1180
Jim Grosbach83d80832011-03-29 23:20:22 +00001181/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001182/// function pointers, ignoring the init priority.
1183void AsmPrinter::EmitXXStructorList(Constant *List) {
1184 // Should be an array of '{ int, void ()* }' structs. The first value is the
1185 // init priority, which we ignore.
1186 if (!isa<ConstantArray>(List)) return;
1187 ConstantArray *InitList = cast<ConstantArray>(List);
1188 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
1189 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
1190 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
1191
1192 if (CS->getOperand(1)->isNullValue())
1193 return; // Found a null terminator, exit printing.
1194 // Emit the function pointer.
1195 EmitGlobalConstant(CS->getOperand(1));
1196 }
1197}
1198
1199//===--------------------------------------------------------------------===//
1200// Emission and print routines
1201//
1202
1203/// EmitInt8 - Emit a byte directive and value.
1204///
1205void AsmPrinter::EmitInt8(int Value) const {
1206 OutStreamer.EmitIntValue(Value, 1, 0/*addrspace*/);
1207}
1208
1209/// EmitInt16 - Emit a short directive and value.
1210///
1211void AsmPrinter::EmitInt16(int Value) const {
1212 OutStreamer.EmitIntValue(Value, 2, 0/*addrspace*/);
1213}
1214
1215/// EmitInt32 - Emit a long directive and value.
1216///
1217void AsmPrinter::EmitInt32(int Value) const {
1218 OutStreamer.EmitIntValue(Value, 4, 0/*addrspace*/);
1219}
1220
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001221/// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size
1222/// in bytes of the directive is specified by Size and Hi/Lo specify the
1223/// labels. This implicitly uses .set if it is available.
1224void AsmPrinter::EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
1225 unsigned Size) const {
1226 // Get the Hi-Lo expression.
Jim Grosbach83d80832011-03-29 23:20:22 +00001227 const MCExpr *Diff =
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001228 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(Hi, OutContext),
1229 MCSymbolRefExpr::Create(Lo, OutContext),
1230 OutContext);
Jim Grosbach83d80832011-03-29 23:20:22 +00001231
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001232 if (!MAI->hasSetDirective()) {
1233 OutStreamer.EmitValue(Diff, Size, 0/*AddrSpace*/);
1234 return;
1235 }
1236
1237 // Otherwise, emit with .set (aka assignment).
1238 MCSymbol *SetLabel = GetTempSymbol("set", SetCounter++);
1239 OutStreamer.EmitAssignment(SetLabel, Diff);
1240 OutStreamer.EmitSymbolValue(SetLabel, Size, 0/*AddrSpace*/);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001241}
1242
Jim Grosbach83d80832011-03-29 23:20:22 +00001243/// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo"
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001244/// where the size in bytes of the directive is specified by Size and Hi/Lo
1245/// specify the labels. This implicitly uses .set if it is available.
1246void AsmPrinter::EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset,
Jim Grosbach83d80832011-03-29 23:20:22 +00001247 const MCSymbol *Lo, unsigned Size)
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001248 const {
Jim Grosbach83d80832011-03-29 23:20:22 +00001249
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001250 // Emit Hi+Offset - Lo
1251 // Get the Hi+Offset expression.
1252 const MCExpr *Plus =
Jim Grosbach83d80832011-03-29 23:20:22 +00001253 MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Hi, OutContext),
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001254 MCConstantExpr::Create(Offset, OutContext),
1255 OutContext);
Jim Grosbach83d80832011-03-29 23:20:22 +00001256
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001257 // Get the Hi+Offset-Lo expression.
Jim Grosbach83d80832011-03-29 23:20:22 +00001258 const MCExpr *Diff =
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001259 MCBinaryExpr::CreateSub(Plus,
1260 MCSymbolRefExpr::Create(Lo, OutContext),
1261 OutContext);
Jim Grosbach83d80832011-03-29 23:20:22 +00001262
1263 if (!MAI->hasSetDirective())
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001264 OutStreamer.EmitValue(Diff, 4, 0/*AddrSpace*/);
1265 else {
1266 // Otherwise, emit with .set (aka assignment).
1267 MCSymbol *SetLabel = GetTempSymbol("set", SetCounter++);
1268 OutStreamer.EmitAssignment(SetLabel, Diff);
1269 OutStreamer.EmitSymbolValue(SetLabel, 4, 0/*AddrSpace*/);
1270 }
1271}
Devang Patel17e9a622010-09-02 16:43:44 +00001272
Jim Grosbach83d80832011-03-29 23:20:22 +00001273/// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
Devang Patel17e9a622010-09-02 16:43:44 +00001274/// where the size in bytes of the directive is specified by Size and Label
1275/// specifies the label. This implicitly uses .set if it is available.
1276void AsmPrinter::EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
Jim Grosbach83d80832011-03-29 23:20:22 +00001277 unsigned Size)
Devang Patel17e9a622010-09-02 16:43:44 +00001278 const {
Jim Grosbach83d80832011-03-29 23:20:22 +00001279
Devang Patel17e9a622010-09-02 16:43:44 +00001280 // Emit Label+Offset
1281 const MCExpr *Plus =
Jim Grosbach83d80832011-03-29 23:20:22 +00001282 MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Label, OutContext),
Devang Patel17e9a622010-09-02 16:43:44 +00001283 MCConstantExpr::Create(Offset, OutContext),
1284 OutContext);
Jim Grosbach83d80832011-03-29 23:20:22 +00001285
Devang Patele5e909f2010-09-02 23:01:10 +00001286 OutStreamer.EmitValue(Plus, 4, 0/*AddrSpace*/);
Devang Patel17e9a622010-09-02 16:43:44 +00001287}
Jim Grosbach83d80832011-03-29 23:20:22 +00001288
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001289
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001290//===----------------------------------------------------------------------===//
1291
1292// EmitAlignment - Emit an alignment directive to the specified power of
1293// two boundary. For example, if you pass in 3 here, you will get an 8
1294// byte alignment. If a global value is specified, and if that global has
Chris Lattner7516e9e2010-04-28 19:58:07 +00001295// an explicit alignment requested, it will override the alignment request
1296// if required for correctness.
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001297//
Chris Lattner027b9362010-04-28 01:08:40 +00001298void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattner7516e9e2010-04-28 19:58:07 +00001299 if (GV) NumBits = getGVAlignmentLog2(GV, *TM.getTargetData(), NumBits);
Jim Grosbach83d80832011-03-29 23:20:22 +00001300
Chris Lattner7516e9e2010-04-28 19:58:07 +00001301 if (NumBits == 0) return; // 1-byte aligned: no need to emit alignment.
Jim Grosbach83d80832011-03-29 23:20:22 +00001302
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001303 if (getCurrentSection()->getKind().isText())
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001304 OutStreamer.EmitCodeAlignment(1 << NumBits);
1305 else
1306 OutStreamer.EmitValueToAlignment(1 << NumBits, 0, 1, 0);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001307}
1308
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001309//===----------------------------------------------------------------------===//
1310// Constant emission.
1311//===----------------------------------------------------------------------===//
1312
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001313/// LowerConstant - Lower the specified LLVM Constant to an MCExpr.
1314///
1315static const MCExpr *LowerConstant(const Constant *CV, AsmPrinter &AP) {
1316 MCContext &Ctx = AP.OutContext;
Jim Grosbach83d80832011-03-29 23:20:22 +00001317
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001318 if (CV->isNullValue() || isa<UndefValue>(CV))
1319 return MCConstantExpr::Create(0, Ctx);
1320
1321 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV))
1322 return MCConstantExpr::Create(CI->getZExtValue(), Ctx);
Jim Grosbach83d80832011-03-29 23:20:22 +00001323
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001324 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001325 return MCSymbolRefExpr::Create(AP.Mang->getSymbol(GV), Ctx);
Bill Wendling021fd612010-08-18 18:41:13 +00001326
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001327 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV))
1328 return MCSymbolRefExpr::Create(AP.GetBlockAddressSymbol(BA), Ctx);
Jim Grosbach83d80832011-03-29 23:20:22 +00001329
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001330 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
1331 if (CE == 0) {
1332 llvm_unreachable("Unknown constant value to lower!");
1333 return MCConstantExpr::Create(0, Ctx);
1334 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001335
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001336 switch (CE->getOpcode()) {
1337 default:
1338 // If the code isn't optimized, there may be outstanding folding
1339 // opportunities. Attempt to fold the expression using TargetData as a
1340 // last resort before giving up.
1341 if (Constant *C =
1342 ConstantFoldConstantExpression(CE, AP.TM.getTargetData()))
1343 if (C != CE)
1344 return LowerConstant(C, AP);
Dan Gohmana3f4d8f2010-08-04 18:51:09 +00001345
1346 // Otherwise report the problem to the user.
1347 {
1348 std::string S;
1349 raw_string_ostream OS(S);
1350 OS << "Unsupported expression in static initializer: ";
1351 WriteAsOperand(OS, CE, /*PrintType=*/false,
1352 !AP.MF ? 0 : AP.MF->getFunction()->getParent());
1353 report_fatal_error(OS.str());
1354 }
1355 return MCConstantExpr::Create(0, Ctx);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001356 case Instruction::GetElementPtr: {
1357 const TargetData &TD = *AP.TM.getTargetData();
1358 // Generate a symbolic expression for the byte address
1359 const Constant *PtrVal = CE->getOperand(0);
1360 SmallVector<Value*, 8> IdxVec(CE->op_begin()+1, CE->op_end());
1361 int64_t Offset = TD.getIndexedOffset(PtrVal->getType(), &IdxVec[0],
1362 IdxVec.size());
Jim Grosbach83d80832011-03-29 23:20:22 +00001363
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001364 const MCExpr *Base = LowerConstant(CE->getOperand(0), AP);
1365 if (Offset == 0)
1366 return Base;
Jim Grosbach83d80832011-03-29 23:20:22 +00001367
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001368 // Truncate/sext the offset to the pointer size.
1369 if (TD.getPointerSizeInBits() != 64) {
1370 int SExtAmount = 64-TD.getPointerSizeInBits();
1371 Offset = (Offset << SExtAmount) >> SExtAmount;
1372 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001373
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001374 return MCBinaryExpr::CreateAdd(Base, MCConstantExpr::Create(Offset, Ctx),
1375 Ctx);
1376 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001377
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001378 case Instruction::Trunc:
1379 // We emit the value and depend on the assembler to truncate the generated
1380 // expression properly. This is important for differences between
1381 // blockaddress labels. Since the two labels are in the same function, it
1382 // is reasonable to treat their delta as a 32-bit value.
1383 // FALL THROUGH.
1384 case Instruction::BitCast:
1385 return LowerConstant(CE->getOperand(0), AP);
1386
1387 case Instruction::IntToPtr: {
1388 const TargetData &TD = *AP.TM.getTargetData();
1389 // Handle casts to pointers by changing them into casts to the appropriate
1390 // integer type. This promotes constant folding and simplifies this code.
1391 Constant *Op = CE->getOperand(0);
1392 Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CV->getContext()),
1393 false/*ZExt*/);
1394 return LowerConstant(Op, AP);
1395 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001396
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001397 case Instruction::PtrToInt: {
1398 const TargetData &TD = *AP.TM.getTargetData();
1399 // Support only foldable casts to/from pointers that can be eliminated by
1400 // changing the pointer to the appropriately sized integer type.
1401 Constant *Op = CE->getOperand(0);
1402 const Type *Ty = CE->getType();
1403
1404 const MCExpr *OpExpr = LowerConstant(Op, AP);
1405
1406 // We can emit the pointer value into this slot if the slot is an
1407 // integer slot equal to the size of the pointer.
1408 if (TD.getTypeAllocSize(Ty) == TD.getTypeAllocSize(Op->getType()))
1409 return OpExpr;
1410
1411 // Otherwise the pointer is smaller than the resultant integer, mask off
1412 // the high bits so we are sure to get a proper truncation if the input is
1413 // a constant expr.
1414 unsigned InBits = TD.getTypeAllocSizeInBits(Op->getType());
1415 const MCExpr *MaskExpr = MCConstantExpr::Create(~0ULL >> (64-InBits), Ctx);
1416 return MCBinaryExpr::CreateAnd(OpExpr, MaskExpr, Ctx);
1417 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001418
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001419 // The MC library also has a right-shift operator, but it isn't consistently
1420 // signed or unsigned between different targets.
1421 case Instruction::Add:
1422 case Instruction::Sub:
1423 case Instruction::Mul:
1424 case Instruction::SDiv:
1425 case Instruction::SRem:
1426 case Instruction::Shl:
1427 case Instruction::And:
1428 case Instruction::Or:
1429 case Instruction::Xor: {
1430 const MCExpr *LHS = LowerConstant(CE->getOperand(0), AP);
1431 const MCExpr *RHS = LowerConstant(CE->getOperand(1), AP);
1432 switch (CE->getOpcode()) {
1433 default: llvm_unreachable("Unknown binary operator constant cast expr");
1434 case Instruction::Add: return MCBinaryExpr::CreateAdd(LHS, RHS, Ctx);
1435 case Instruction::Sub: return MCBinaryExpr::CreateSub(LHS, RHS, Ctx);
1436 case Instruction::Mul: return MCBinaryExpr::CreateMul(LHS, RHS, Ctx);
1437 case Instruction::SDiv: return MCBinaryExpr::CreateDiv(LHS, RHS, Ctx);
1438 case Instruction::SRem: return MCBinaryExpr::CreateMod(LHS, RHS, Ctx);
1439 case Instruction::Shl: return MCBinaryExpr::CreateShl(LHS, RHS, Ctx);
1440 case Instruction::And: return MCBinaryExpr::CreateAnd(LHS, RHS, Ctx);
1441 case Instruction::Or: return MCBinaryExpr::CreateOr (LHS, RHS, Ctx);
1442 case Instruction::Xor: return MCBinaryExpr::CreateXor(LHS, RHS, Ctx);
1443 }
1444 }
1445 }
1446}
1447
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001448static void EmitGlobalConstantImpl(const Constant *C, unsigned AddrSpace,
1449 AsmPrinter &AP);
1450
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001451static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace,
1452 AsmPrinter &AP) {
1453 if (AddrSpace != 0 || !CA->isString()) {
1454 // Not a string. Print the values in successive locations
1455 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001456 EmitGlobalConstantImpl(CA->getOperand(i), AddrSpace, AP);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001457 return;
1458 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001459
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001460 // Otherwise, it can be emitted as .ascii.
1461 SmallVector<char, 128> TmpVec;
1462 TmpVec.reserve(CA->getNumOperands());
1463 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1464 TmpVec.push_back(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
1465
1466 AP.OutStreamer.EmitBytes(StringRef(TmpVec.data(), TmpVec.size()), AddrSpace);
1467}
1468
1469static void EmitGlobalConstantVector(const ConstantVector *CV,
1470 unsigned AddrSpace, AsmPrinter &AP) {
1471 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001472 EmitGlobalConstantImpl(CV->getOperand(i), AddrSpace, AP);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001473}
1474
1475static void EmitGlobalConstantStruct(const ConstantStruct *CS,
1476 unsigned AddrSpace, AsmPrinter &AP) {
1477 // Print the fields in successive locations. Pad to align if needed!
1478 const TargetData *TD = AP.TM.getTargetData();
1479 unsigned Size = TD->getTypeAllocSize(CS->getType());
1480 const StructLayout *Layout = TD->getStructLayout(CS->getType());
1481 uint64_t SizeSoFar = 0;
1482 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
1483 const Constant *Field = CS->getOperand(i);
1484
1485 // Check if padding is needed and insert one or more 0s.
1486 uint64_t FieldSize = TD->getTypeAllocSize(Field->getType());
1487 uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1))
1488 - Layout->getElementOffset(i)) - FieldSize;
1489 SizeSoFar += FieldSize + PadSize;
1490
1491 // Now print the actual field value.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001492 EmitGlobalConstantImpl(Field, AddrSpace, AP);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001493
1494 // Insert padding - this may include padding to increase the size of the
1495 // current field up to the ABI size (if the struct is not packed) as well
1496 // as padding to ensure that the next field starts at the right offset.
1497 AP.OutStreamer.EmitZeros(PadSize, AddrSpace);
1498 }
1499 assert(SizeSoFar == Layout->getSizeInBytes() &&
1500 "Layout of constant struct may be incorrect!");
1501}
1502
1503static void EmitGlobalConstantFP(const ConstantFP *CFP, unsigned AddrSpace,
1504 AsmPrinter &AP) {
1505 // FP Constants are printed as integer constants to avoid losing
1506 // precision.
1507 if (CFP->getType()->isDoubleTy()) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001508 if (AP.isVerbose()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001509 double Val = CFP->getValueAPF().convertToDouble();
1510 AP.OutStreamer.GetCommentOS() << "double " << Val << '\n';
1511 }
1512
1513 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1514 AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace);
1515 return;
1516 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001517
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001518 if (CFP->getType()->isFloatTy()) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001519 if (AP.isVerbose()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001520 float Val = CFP->getValueAPF().convertToFloat();
1521 AP.OutStreamer.GetCommentOS() << "float " << Val << '\n';
1522 }
1523 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1524 AP.OutStreamer.EmitIntValue(Val, 4, AddrSpace);
1525 return;
1526 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001527
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001528 if (CFP->getType()->isX86_FP80Ty()) {
1529 // all long double variants are printed as hex
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001530 // API needed to prevent premature destruction
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001531 APInt API = CFP->getValueAPF().bitcastToAPInt();
1532 const uint64_t *p = API.getRawData();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001533 if (AP.isVerbose()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001534 // Convert to double so we can print the approximate val as a comment.
1535 APFloat DoubleVal = CFP->getValueAPF();
1536 bool ignored;
1537 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1538 &ignored);
1539 AP.OutStreamer.GetCommentOS() << "x86_fp80 ~= "
1540 << DoubleVal.convertToDouble() << '\n';
1541 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001542
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001543 if (AP.TM.getTargetData()->isBigEndian()) {
1544 AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace);
1545 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1546 } else {
1547 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1548 AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace);
1549 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001550
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001551 // Emit the tail padding for the long double.
1552 const TargetData &TD = *AP.TM.getTargetData();
1553 AP.OutStreamer.EmitZeros(TD.getTypeAllocSize(CFP->getType()) -
1554 TD.getTypeStoreSize(CFP->getType()), AddrSpace);
1555 return;
1556 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001557
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001558 assert(CFP->getType()->isPPC_FP128Ty() &&
1559 "Floating point constant type not handled");
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001560 // All long double variants are printed as hex
1561 // API needed to prevent premature destruction.
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001562 APInt API = CFP->getValueAPF().bitcastToAPInt();
1563 const uint64_t *p = API.getRawData();
1564 if (AP.TM.getTargetData()->isBigEndian()) {
1565 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1566 AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace);
1567 } else {
1568 AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace);
1569 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1570 }
1571}
1572
1573static void EmitGlobalConstantLargeInt(const ConstantInt *CI,
1574 unsigned AddrSpace, AsmPrinter &AP) {
1575 const TargetData *TD = AP.TM.getTargetData();
1576 unsigned BitWidth = CI->getBitWidth();
1577 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
1578
1579 // We don't expect assemblers to support integer data directives
1580 // for more than 64 bits, so we emit the data in at most 64-bit
1581 // quantities at a time.
1582 const uint64_t *RawData = CI->getValue().getRawData();
1583 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1584 uint64_t Val = TD->isBigEndian() ? RawData[e - i - 1] : RawData[i];
1585 AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace);
1586 }
1587}
1588
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001589static void EmitGlobalConstantImpl(const Constant *CV, unsigned AddrSpace,
1590 AsmPrinter &AP) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001591 if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV)) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001592 uint64_t Size = AP.TM.getTargetData()->getTypeAllocSize(CV->getType());
1593 return AP.OutStreamer.EmitZeros(Size, AddrSpace);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001594 }
1595
1596 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001597 unsigned Size = AP.TM.getTargetData()->getTypeAllocSize(CV->getType());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001598 switch (Size) {
1599 case 1:
1600 case 2:
1601 case 4:
1602 case 8:
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001603 if (AP.isVerbose())
1604 AP.OutStreamer.GetCommentOS() << format("0x%llx\n", CI->getZExtValue());
Bill Wendling021fd612010-08-18 18:41:13 +00001605 AP.OutStreamer.EmitIntValue(CI->getZExtValue(), Size, AddrSpace);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001606 return;
1607 default:
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001608 EmitGlobalConstantLargeInt(CI, AddrSpace, AP);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001609 return;
1610 }
1611 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001612
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001613 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001614 return EmitGlobalConstantArray(CVA, AddrSpace, AP);
Jim Grosbach83d80832011-03-29 23:20:22 +00001615
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001616 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001617 return EmitGlobalConstantStruct(CVS, AddrSpace, AP);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001618
1619 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001620 return EmitGlobalConstantFP(CFP, AddrSpace, AP);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001621
1622 if (isa<ConstantPointerNull>(CV)) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001623 unsigned Size = AP.TM.getTargetData()->getTypeAllocSize(CV->getType());
1624 AP.OutStreamer.EmitIntValue(0, Size, AddrSpace);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001625 return;
1626 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001627
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001628 if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
1629 return EmitGlobalConstantVector(V, AddrSpace, AP);
Jim Grosbach83d80832011-03-29 23:20:22 +00001630
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001631 // Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
1632 // thread the streamer with EmitValue.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001633 AP.OutStreamer.EmitValue(LowerConstant(CV, AP),
1634 AP.TM.getTargetData()->getTypeAllocSize(CV->getType()),
1635 AddrSpace);
1636}
1637
1638/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
1639void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
1640 uint64_t Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
1641 if (Size)
1642 EmitGlobalConstantImpl(CV, AddrSpace, *this);
1643 else if (MAI->hasSubsectionsViaSymbols()) {
1644 // If the global has zero size, emit a single byte so that two labels don't
1645 // look like they are at the same location.
1646 OutStreamer.EmitIntValue(0, 1, AddrSpace);
1647 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001648}
1649
1650void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
1651 // Target doesn't support this yet!
1652 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
1653}
1654
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001655void AsmPrinter::printOffset(int64_t Offset, raw_ostream &OS) const {
1656 if (Offset > 0)
1657 OS << '+' << Offset;
1658 else if (Offset < 0)
1659 OS << Offset;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001660}
1661
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001662//===----------------------------------------------------------------------===//
1663// Symbol Lowering Routines.
1664//===----------------------------------------------------------------------===//
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001665
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001666/// GetTempSymbol - Return the MCSymbol corresponding to the assembler
1667/// temporary label with the specified stem and unique ID.
1668MCSymbol *AsmPrinter::GetTempSymbol(StringRef Name, unsigned ID) const {
1669 return OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
1670 Name + Twine(ID));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001671}
1672
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001673/// GetTempSymbol - Return an assembler temporary label with the specified
1674/// stem.
1675MCSymbol *AsmPrinter::GetTempSymbol(StringRef Name) const {
1676 return OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())+
1677 Name);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001678}
1679
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001680
1681MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001682 return MMI->getAddrLabelSymbol(BA->getBasicBlock());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001683}
1684
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001685MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BasicBlock *BB) const {
1686 return MMI->getAddrLabelSymbol(BB);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001687}
1688
1689/// GetCPISymbol - Return the symbol for the specified constant pool entry.
1690MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001691 return OutContext.GetOrCreateSymbol
1692 (Twine(MAI->getPrivateGlobalPrefix()) + "CPI" + Twine(getFunctionNumber())
1693 + "_" + Twine(CPID));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001694}
1695
1696/// GetJTISymbol - Return the symbol for the specified jump table entry.
1697MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const {
1698 return MF->getJTISymbol(JTID, OutContext, isLinkerPrivate);
1699}
1700
1701/// GetJTSetSymbol - Return the symbol for the specified jump table .set
1702/// FIXME: privatize to AsmPrinter.
1703MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001704 return OutContext.GetOrCreateSymbol
1705 (Twine(MAI->getPrivateGlobalPrefix()) + Twine(getFunctionNumber()) + "_" +
1706 Twine(UID) + "_set_" + Twine(MBBID));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001707}
1708
1709/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
1710/// global value name as its base, with the specified suffix, and where the
1711/// symbol is forced to have private linkage if ForcePrivate is true.
1712MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1713 StringRef Suffix,
1714 bool ForcePrivate) const {
1715 SmallString<60> NameStr;
1716 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
1717 NameStr.append(Suffix.begin(), Suffix.end());
1718 return OutContext.GetOrCreateSymbol(NameStr.str());
1719}
1720
1721/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1722/// ExternalSymbol.
1723MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1724 SmallString<60> NameStr;
1725 Mang->getNameWithPrefix(NameStr, Sym);
1726 return OutContext.GetOrCreateSymbol(NameStr.str());
Jim Grosbach83d80832011-03-29 23:20:22 +00001727}
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001728
1729
1730
1731/// PrintParentLoopComment - Print comments about parent loops of this one.
1732static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop,
1733 unsigned FunctionNumber) {
1734 if (Loop == 0) return;
1735 PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber);
1736 OS.indent(Loop->getLoopDepth()*2)
1737 << "Parent Loop BB" << FunctionNumber << "_"
1738 << Loop->getHeader()->getNumber()
1739 << " Depth=" << Loop->getLoopDepth() << '\n';
1740}
1741
1742
1743/// PrintChildLoopComment - Print comments about child loops within
1744/// the loop for this basic block, with nesting.
1745static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop,
1746 unsigned FunctionNumber) {
1747 // Add child loop information
1748 for (MachineLoop::iterator CL = Loop->begin(), E = Loop->end();CL != E; ++CL){
1749 OS.indent((*CL)->getLoopDepth()*2)
1750 << "Child Loop BB" << FunctionNumber << "_"
1751 << (*CL)->getHeader()->getNumber() << " Depth " << (*CL)->getLoopDepth()
1752 << '\n';
1753 PrintChildLoopComment(OS, *CL, FunctionNumber);
1754 }
1755}
1756
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001757/// EmitBasicBlockLoopComments - Pretty-print comments for basic blocks.
1758static void EmitBasicBlockLoopComments(const MachineBasicBlock &MBB,
1759 const MachineLoopInfo *LI,
1760 const AsmPrinter &AP) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001761 // Add loop depth information
1762 const MachineLoop *Loop = LI->getLoopFor(&MBB);
1763 if (Loop == 0) return;
Jim Grosbach83d80832011-03-29 23:20:22 +00001764
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001765 MachineBasicBlock *Header = Loop->getHeader();
1766 assert(Header && "No header for loop");
Jim Grosbach83d80832011-03-29 23:20:22 +00001767
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001768 // If this block is not a loop header, just print out what is the loop header
1769 // and return.
1770 if (Header != &MBB) {
1771 AP.OutStreamer.AddComment(" in Loop: Header=BB" +
1772 Twine(AP.getFunctionNumber())+"_" +
1773 Twine(Loop->getHeader()->getNumber())+
1774 " Depth="+Twine(Loop->getLoopDepth()));
1775 return;
1776 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001777
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001778 // Otherwise, it is a loop header. Print out information about child and
1779 // parent loops.
1780 raw_ostream &OS = AP.OutStreamer.GetCommentOS();
Jim Grosbach83d80832011-03-29 23:20:22 +00001781
1782 PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber());
1783
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001784 OS << "=>";
1785 OS.indent(Loop->getLoopDepth()*2-2);
Jim Grosbach83d80832011-03-29 23:20:22 +00001786
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001787 OS << "This ";
1788 if (Loop->empty())
1789 OS << "Inner ";
1790 OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n';
Jim Grosbach83d80832011-03-29 23:20:22 +00001791
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001792 PrintChildLoopComment(OS, Loop, AP.getFunctionNumber());
1793}
1794
1795
1796/// EmitBasicBlockStart - This method prints the label for the specified
1797/// MachineBasicBlock, an alignment (if present) and a comment describing
1798/// it if appropriate.
1799void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
1800 // Emit an alignment directive for this block, if needed.
1801 if (unsigned Align = MBB->getAlignment())
1802 EmitAlignment(Log2_32(Align));
1803
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001804 // If the block has its address taken, emit any labels that were used to
1805 // reference the block. It is possible that there is more than one label
1806 // here, because multiple LLVM BB's may have been RAUW'd to this block after
1807 // the references were generated.
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001808 if (MBB->hasAddressTaken()) {
1809 const BasicBlock *BB = MBB->getBasicBlock();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001810 if (isVerbose())
1811 OutStreamer.AddComment("Block address taken");
Jim Grosbach83d80832011-03-29 23:20:22 +00001812
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001813 std::vector<MCSymbol*> Syms = MMI->getAddrLabelSymbolToEmit(BB);
1814
1815 for (unsigned i = 0, e = Syms.size(); i != e; ++i)
1816 OutStreamer.EmitLabel(Syms[i]);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001817 }
1818
1819 // Print the main label for the block.
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001820 if (MBB->pred_empty() || isBlockOnlyReachableByFallthrough(MBB)) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001821 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001822 if (const BasicBlock *BB = MBB->getBasicBlock())
1823 if (BB->hasName())
1824 OutStreamer.AddComment("%" + BB->getName());
Jim Grosbach83d80832011-03-29 23:20:22 +00001825
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001826 EmitBasicBlockLoopComments(*MBB, LI, *this);
Jim Grosbach83d80832011-03-29 23:20:22 +00001827
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001828 // NOTE: Want this comment at start of line, don't emit with AddComment.
1829 OutStreamer.EmitRawText(Twine(MAI->getCommentString()) + " BB#" +
1830 Twine(MBB->getNumber()) + ":");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001831 }
1832 } else {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001833 if (isVerbose()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001834 if (const BasicBlock *BB = MBB->getBasicBlock())
1835 if (BB->hasName())
1836 OutStreamer.AddComment("%" + BB->getName());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001837 EmitBasicBlockLoopComments(*MBB, LI, *this);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001838 }
1839
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001840 OutStreamer.EmitLabel(MBB->getSymbol());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001841 }
1842}
1843
Stuart Hastings5129bde2011-02-23 02:27:05 +00001844void AsmPrinter::EmitVisibility(MCSymbol *Sym, unsigned Visibility,
1845 bool IsDefinition) const {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001846 MCSymbolAttr Attr = MCSA_Invalid;
Jim Grosbach83d80832011-03-29 23:20:22 +00001847
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001848 switch (Visibility) {
1849 default: break;
1850 case GlobalValue::HiddenVisibility:
Stuart Hastings5129bde2011-02-23 02:27:05 +00001851 if (IsDefinition)
1852 Attr = MAI->getHiddenVisibilityAttr();
1853 else
1854 Attr = MAI->getHiddenDeclarationVisibilityAttr();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001855 break;
1856 case GlobalValue::ProtectedVisibility:
1857 Attr = MAI->getProtectedVisibilityAttr();
1858 break;
1859 }
1860
1861 if (Attr != MCSA_Invalid)
1862 OutStreamer.EmitSymbolAttribute(Sym, Attr);
1863}
1864
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001865/// isBlockOnlyReachableByFallthough - Return true if the basic block has
1866/// exactly one predecessor and the control transfer mechanism between
1867/// the predecessor and this block is a fall-through.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001868bool AsmPrinter::
1869isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001870 // If this is a landing pad, it isn't a fall through. If it has no preds,
1871 // then nothing falls through to it.
1872 if (MBB->isLandingPad() || MBB->pred_empty())
1873 return false;
Jim Grosbach83d80832011-03-29 23:20:22 +00001874
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001875 // If there isn't exactly one predecessor, it can't be a fall through.
1876 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
1877 ++PI2;
1878 if (PI2 != MBB->pred_end())
1879 return false;
Jim Grosbach83d80832011-03-29 23:20:22 +00001880
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001881 // The predecessor has to be immediately before this block.
1882 const MachineBasicBlock *Pred = *PI;
Jim Grosbach83d80832011-03-29 23:20:22 +00001883
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001884 if (!Pred->isLayoutSuccessor(MBB))
1885 return false;
Jim Grosbach83d80832011-03-29 23:20:22 +00001886
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001887 // If the block is completely empty, then it definitely does fall through.
1888 if (Pred->empty())
1889 return true;
Jim Grosbach83d80832011-03-29 23:20:22 +00001890
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001891 // Otherwise, check the last instruction.
1892 const MachineInstr &LastInst = Pred->back();
1893 return !LastInst.getDesc().isBarrier();
1894}
1895
1896
1897
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001898GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1899 if (!S->usesMetadata())
1900 return 0;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001901
1902 gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
1903 gcp_map_type::iterator GCPI = GCMap.find(S);
1904 if (GCPI != GCMap.end())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001905 return GCPI->second;
Jim Grosbach83d80832011-03-29 23:20:22 +00001906
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001907 const char *Name = S->getName().c_str();
Jim Grosbach83d80832011-03-29 23:20:22 +00001908
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001909 for (GCMetadataPrinterRegistry::iterator
1910 I = GCMetadataPrinterRegistry::begin(),
1911 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1912 if (strcmp(Name, I->getName()) == 0) {
1913 GCMetadataPrinter *GMP = I->instantiate();
1914 GMP->S = S;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001915 GCMap.insert(std::make_pair(S, GMP));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001916 return GMP;
1917 }
Jim Grosbach83d80832011-03-29 23:20:22 +00001918
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001919 report_fatal_error("no GCMetadataPrinter registered for GC: " + Twine(Name));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001920 return 0;
1921}
1922