blob: e29bff5f66ae2d00bce615b3a91bce9787672f49 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/Assembly/Writer.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Constants.h"
18#include "llvm/Module.h"
Gordon Henriksen1aed5992008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/MachineConstantPool.h"
David Greene066ed6a2009-08-18 19:22:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greenee52fd872009-08-10 16:38:07 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelfe359e72009-01-13 21:44:10 +000025#include "llvm/CodeGen/DwarfWriter.h"
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +000026#include "llvm/Analysis/DebugInfo.h"
Chris Lattner1eb0ad02009-07-27 21:28:04 +000027#include "llvm/MC/MCContext.h"
David Greene4a37a692009-07-16 22:24:20 +000028#include "llvm/MC/MCInst.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000029#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCStreamer.h"
Chris Lattner08c97082009-09-12 23:02:08 +000031#include "llvm/MC/MCSymbol.h"
Evan Cheng42ceb472009-03-25 01:47:28 +000032#include "llvm/Support/CommandLine.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000033#include "llvm/Support/ErrorHandling.h"
David Greene63486122009-07-13 20:25:48 +000034#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/Mangler.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Target/TargetData.h"
38#include "llvm/Target/TargetLowering.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng0eeed442008-07-01 23:18:29 +000040#include "llvm/Target/TargetOptions.h"
Evan Cheng3c0eda52008-03-15 00:03:38 +000041#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng6fb06762007-11-09 01:32:10 +000042#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner89b36582008-08-17 07:19:36 +000043#include "llvm/ADT/SmallString.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000044#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045#include <cerrno>
46using namespace llvm;
47
Evan Cheng42ceb472009-03-25 01:47:28 +000048static cl::opt<cl::boolOrDefault>
49AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
50 cl::init(cl::BOU_UNSET));
51
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052char AsmPrinter::ID = 0;
David Greene302008d2009-07-14 20:18:05 +000053AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +000054 const MCAsmInfo *T, bool VDef)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000055 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattnera5ef4d32009-08-22 21:43:10 +000056 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000057
58 OutContext(*new MCContext()),
Chris Lattnera835afd2009-09-14 03:02:37 +000059 // FIXME: Pass instprinter to streamer.
60 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000061
Chris Lattnerebd055c2009-08-03 23:20:21 +000062 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersonc82bd822009-06-25 16:55:32 +000063 PrevDLT(0, ~0U, ~0U) {
Chris Lattner2424eac2009-06-24 19:09:55 +000064 DW = 0; MMI = 0;
Evan Cheng42ceb472009-03-25 01:47:28 +000065 switch (AsmVerbose) {
66 case cl::BOU_UNSET: VerboseAsm = VDef; break;
67 case cl::BOU_TRUE: VerboseAsm = true; break;
68 case cl::BOU_FALSE: VerboseAsm = false; break;
69 }
70}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000072AsmPrinter::~AsmPrinter() {
73 for (gcp_iterator I = GCMetadataPrinters.begin(),
74 E = GCMetadataPrinters.end(); I != E; ++I)
75 delete I->second;
Chris Lattner1eb0ad02009-07-27 21:28:04 +000076
77 delete &OutStreamer;
78 delete &OutContext;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000079}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
Chris Lattner75bdd292009-08-03 19:12:26 +000081TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerc4c40a92009-07-28 03:13:23 +000082 return TM.getTargetLowering()->getObjFileLowering();
83}
84
Chris Lattnerd69d8ad2009-08-18 06:15:16 +000085/// getCurrentSection() - Return the current section we are emitting to.
86const MCSection *AsmPrinter::getCurrentSection() const {
87 return OutStreamer.getCurrentSection();
88}
89
90
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000091void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +000092 AU.setPreservesAll();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000093 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen1aed5992008-08-17 18:44:35 +000094 AU.addRequired<GCModuleInfo>();
David Greene066ed6a2009-08-18 19:22:55 +000095 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +000096 AU.addRequired<MachineLoopInfo>();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000097}
98
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner01316272009-07-31 17:42:42 +0000100 // Initialize TargetLoweringObjectFile.
101 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
102 .Initialize(OutContext, TM);
103
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000104 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
105 MAI->getLinkerPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000107 if (MAI->doesAllowQuotesInName())
Chris Lattner690b02c2009-06-18 23:41:35 +0000108 Mang->setUseQuotes(true);
Anton Korobeynikov3789f872009-09-18 16:57:42 +0000109
110 if (MAI->doesAllowNameToStartWithDigit())
111 Mang->setSymbolsCanStartWithDigit(true);
Chris Lattner690b02c2009-06-18 23:41:35 +0000112
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000113 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000114 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000115
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000116 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000117 /* Very minimal debug info. It is ignored if we emit actual
118 debug info. If we don't, this at helps the user find where
119 a function came from. */
120 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
121 }
122
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000123 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
124 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000125 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000126
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 if (!M.getModuleInlineAsm().empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000128 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 << M.getModuleInlineAsm()
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000130 << '\n' << MAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 << " End of file scope inline assembly\n";
132
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000133 if (MAI->doesSupportDebugInformation() ||
134 MAI->doesSupportExceptionHandling()) {
Chris Lattner2424eac2009-06-24 19:09:55 +0000135 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
136 if (MMI)
Devang Patel1a454842009-06-19 21:54:26 +0000137 MMI->AnalyzeModule(M);
Chris Lattner2424eac2009-06-24 19:09:55 +0000138 DW = getAnalysisIfAvailable<DwarfWriter>();
139 if (DW)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000140 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel1a454842009-06-19 21:54:26 +0000141 }
142
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 return false;
144}
145
146bool AsmPrinter::doFinalization(Module &M) {
Chris Lattnerae982212009-07-21 18:38:57 +0000147 // Emit global variables.
148 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
149 I != E; ++I)
150 PrintGlobalVariable(I);
151
Chris Lattnere1225cc2009-06-24 18:54:37 +0000152 // Emit final debug information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000153 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattnere1225cc2009-06-24 18:54:37 +0000154 DW->EndModule();
155
Chris Lattnerdb191f02009-06-24 18:52:01 +0000156 // If the target wants to know about weak references, print them all.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000157 if (MAI->getWeakRefDirective()) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000158 // FIXME: This is not lazy, it would be nice to only print weak references
159 // to stuff that is actually used. Note that doing so would require targets
160 // to notice uses in operands (due to constant exprs etc). This should
161 // happen with the MC stuff eventually.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
Chris Lattnerdb191f02009-06-24 18:52:01 +0000163 // Print out module-level global variables here.
164 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
165 I != E; ++I) {
166 if (I->hasExternalWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000167 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000168 }
169
Chris Lattner0f98c332009-08-03 23:10:34 +0000170 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000171 if (I->hasExternalWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000172 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000173 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 }
175
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000176 if (MAI->getSetDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000177 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattnerdb191f02009-06-24 18:52:01 +0000179 I != E; ++I) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000180 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000181
182 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000183 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000184
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000185 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000186 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 else if (I->hasWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000188 O << MAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000189 else if (!I->hasLocalLinkage())
Edwin Törökbd448e32009-07-14 16:55:14 +0000190 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000191
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000192 printVisibility(Name, I->getVisibility());
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000193
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000194 O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
196 }
197
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000198 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000199 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
200 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
201 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000202 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000203
Dan Gohmana65530a2008-05-05 00:28:39 +0000204 // If we don't have any trampolines, then we don't require stack memory
205 // to be executable. Some targets have a directive to declare this.
Chris Lattnerdb191f02009-06-24 18:52:01 +0000206 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana65530a2008-05-05 00:28:39 +0000207 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000208 if (MAI->getNonexecutableStackDirective())
209 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000210
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 delete Mang; Mang = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +0000212 DW = 0; MMI = 0;
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000213
214 OutStreamer.Finish();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 return false;
216}
217
218void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
219 // What's my mangled name?
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000220 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000221 IncrementFunctionNumber();
David Greenee52fd872009-08-10 16:38:07 +0000222
Chris Lattner57c76b52009-09-16 00:35:39 +0000223 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +0000224 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225}
226
Evan Cheng68c18682009-03-13 07:51:59 +0000227namespace {
228 // SectionCPs - Keep track the alignment, constpool entries per Section.
229 struct SectionCPs {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000230 const MCSection *S;
Evan Cheng68c18682009-03-13 07:51:59 +0000231 unsigned Alignment;
232 SmallVector<unsigned, 4> CPEs;
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000233 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng68c18682009-03-13 07:51:59 +0000234 };
235}
236
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237/// EmitConstantPool - Print to the current output stream assembly
238/// representations of the constants in the constant pool MCP. This is
239/// used to print out constants which have been "spilled to memory" by
240/// the code generator.
241///
242void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
243 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
244 if (CP.empty()) return;
245
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000246 // Calculate sections for constant pool entries. We collect entries to go into
247 // the same section together to reduce amount of section switch statements.
Evan Cheng68c18682009-03-13 07:51:59 +0000248 SmallVector<SectionCPs, 4> CPSections;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner680c6f62009-07-22 00:28:43 +0000250 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng68c18682009-03-13 07:51:59 +0000251 unsigned Align = CPE.getAlignment();
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000252
253 SectionKind Kind;
254 switch (CPE.getRelocationInfo()) {
255 default: llvm_unreachable("Unknown section kind");
Chris Lattnera9453412009-08-01 23:57:16 +0000256 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000257 case 1:
Chris Lattnera9453412009-08-01 23:57:16 +0000258 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnered0c6762009-07-26 07:00:12 +0000259 break;
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000260 case 0:
Chris Lattnered0c6762009-07-26 07:00:12 +0000261 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnera9453412009-08-01 23:57:16 +0000262 case 4: Kind = SectionKind::getMergeableConst4(); break;
263 case 8: Kind = SectionKind::getMergeableConst8(); break;
264 case 16: Kind = SectionKind::getMergeableConst16();break;
265 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000266 }
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000267 }
268
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000269 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner680c6f62009-07-22 00:28:43 +0000270
Evan Cheng68c18682009-03-13 07:51:59 +0000271 // The number of sections are small, just do a linear search from the
272 // last section to the first.
273 bool Found = false;
274 unsigned SecIdx = CPSections.size();
275 while (SecIdx != 0) {
276 if (CPSections[--SecIdx].S == S) {
277 Found = true;
278 break;
279 }
280 }
281 if (!Found) {
282 SecIdx = CPSections.size();
283 CPSections.push_back(SectionCPs(S, Align));
284 }
285
286 if (Align > CPSections[SecIdx].Alignment)
287 CPSections[SecIdx].Alignment = Align;
288 CPSections[SecIdx].CPEs.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 }
290
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000291 // Now print stuff into the calculated sections.
Evan Cheng68c18682009-03-13 07:51:59 +0000292 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000293 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng68c18682009-03-13 07:51:59 +0000294 EmitAlignment(Log2_32(CPSections[i].Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295
Evan Cheng68c18682009-03-13 07:51:59 +0000296 unsigned Offset = 0;
297 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
298 unsigned CPI = CPSections[i].CPEs[j];
299 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000300
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 // Emit inter-object padding for alignment.
Evan Cheng68c18682009-03-13 07:51:59 +0000302 unsigned AlignMask = CPE.getAlignment() - 1;
303 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
304 EmitZeros(NewOffset - Offset);
305
306 const Type *Ty = CPE.getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000307 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng68c18682009-03-13 07:51:59 +0000308
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000309 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohman87581eb2009-08-12 18:32:22 +0000310 << CPI << ':';
Evan Cheng68c18682009-03-13 07:51:59 +0000311 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000312 O.PadToColumn(MAI->getCommentColumn());
313 O << MAI->getCommentString() << " constant ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000314 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000315 }
Evan Cheng68c18682009-03-13 07:51:59 +0000316 O << '\n';
317 if (CPE.isMachineConstantPoolEntry())
318 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
319 else
320 EmitGlobalConstant(CPE.Val.ConstVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 }
322 }
323}
324
325/// EmitJumpTableInfo - Print assembly representations of the jump tables used
326/// by the current function to the current output stream.
327///
328void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
329 MachineFunction &MF) {
330 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
331 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
334
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 // Pick the directive to use to print the jump table entries, and switch to
336 // the appropriate section.
337 TargetLowering *LoweringInfo = TM.getTargetLowering();
338
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000339 const Function *F = MF.getFunction();
Evan Chengccca6f72009-06-18 20:37:15 +0000340 bool JTInDiffSection = false;
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000341 if (F->isWeakForLinker() ||
342 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 // In PIC mode, we need to emit the jump table to the same section as the
344 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000345 // We should also do if the section name is NULL or function is declared in
346 // discardable section.
Chris Lattner73266f92009-08-19 05:49:37 +0000347 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
348 TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 } else {
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000350 // Otherwise, drop it in the readonly section.
351 const MCSection *ReadOnlySection =
Chris Lattnera9453412009-08-01 23:57:16 +0000352 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner73266f92009-08-19 05:49:37 +0000353 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengccca6f72009-06-18 20:37:15 +0000354 JTInDiffSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 }
356
357 EmitAlignment(Log2_32(MJTI->getAlignment()));
358
359 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
360 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
361
362 // If this jump table was deleted, ignore it.
363 if (JTBBs.empty()) continue;
364
365 // For PIC codegen, if possible we want to use the SetDirective to reduce
366 // the number of relocations the assembler will generate for the jump table.
367 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000368 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000369 if (MAI->getSetDirective() && IsPic)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000371 if (EmittedSets.insert(JTBBs[ii]))
372 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373
Chris Lattner149495f2009-09-13 19:02:16 +0000374 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 // before each jump table. The first label is never referenced, but tells
376 // the assembler and linker the extents of the jump table object. The
377 // second label is actually referenced by the code.
Chris Lattner149495f2009-09-13 19:02:16 +0000378 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
379 O << MAI->getLinkerPrivateGlobalPrefix()
380 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengccca6f72009-06-18 20:37:15 +0000381 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000383 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +0000384 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385
386 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000387 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 O << '\n';
389 }
390 }
391}
392
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000393void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
394 const MachineBasicBlock *MBB,
395 unsigned uid) const {
Chris Lattner3fb451e2009-08-11 20:29:57 +0000396 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000397
398 // Use JumpTableDirective otherwise honor the entry size from the jump table
399 // info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000400 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000401 bool HadJTEntryDirective = JTEntryDirective != NULL;
402 if (!HadJTEntryDirective) {
403 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000404 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000405 }
406
407 O << JTEntryDirective << ' ';
408
409 // If we have emitted set directives for the jump table entries, print
410 // them rather than the entries themselves. If we're emitting PIC, then
411 // emit the table entries as differences between two text section labels.
412 // If we're emitting non-PIC code, then emit the entries as direct
413 // references to the target basic blocks.
Chris Lattner3fb451e2009-08-11 20:29:57 +0000414 if (!isPIC) {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000415 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000416 } else if (MAI->getSetDirective()) {
417 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3fb451e2009-08-11 20:29:57 +0000418 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000419 } else {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000420 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner3fb451e2009-08-11 20:29:57 +0000421 // If the arch uses custom Jump Table directives, don't calc relative to
422 // JT
423 if (!HadJTEntryDirective)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000424 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner3fb451e2009-08-11 20:29:57 +0000425 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000426 }
427}
428
429
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
431/// special global used by LLVM. If so, emit it and return true, otherwise
432/// do nothing and return false.
433bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000434 if (GV->getName() == "llvm.used") {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000435 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000436 EmitLLVMUsedList(GV->getInitializer());
437 return true;
438 }
439
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000440 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner68433442009-04-13 05:44:34 +0000441 if (GV->getSection() == "llvm.metadata" ||
442 GV->hasAvailableExternallyLinkage())
443 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444
445 if (!GV->hasAppendingLinkage()) return false;
446
447 assert(GV->hasInitializer() && "Not a special LLVM global!");
448
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 const TargetData *TD = TM.getTargetData();
450 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner0c433e92009-03-09 05:52:15 +0000451 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000452 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000453 EmitAlignment(Align, 0);
454 EmitXXStructorList(GV->getInitializer());
455 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 }
457
Chris Lattner0c433e92009-03-09 05:52:15 +0000458 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000459 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000460 EmitAlignment(Align, 0);
461 EmitXXStructorList(GV->getInitializer());
462 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 }
464
465 return false;
466}
467
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000468/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000469/// global in the specified llvm.used list for which emitUsedDirectiveFor
470/// is true, as being used with this directive.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000472 const char *Directive = MAI->getUsedDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473
Dan Gohman9e1657f2009-06-14 23:30:43 +0000474 // Should be an array of 'i8*'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
476 if (InitList == 0) return;
477
478 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner3f621b32009-07-17 22:00:23 +0000479 const GlobalValue *GV =
480 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner84362ac2009-07-31 20:52:39 +0000481 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000482 O << Directive;
483 EmitConstantValueOnly(InitList->getOperand(i));
484 O << '\n';
485 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 }
487}
488
489/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
490/// function pointers, ignoring the init priority.
491void AsmPrinter::EmitXXStructorList(Constant *List) {
492 // Should be an array of '{ int, void ()* }' structs. The first value is the
493 // init priority, which we ignore.
494 if (!isa<ConstantArray>(List)) return;
495 ConstantArray *InitList = cast<ConstantArray>(List);
496 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
497 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
498 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
499
500 if (CS->getOperand(1)->isNullValue())
501 return; // Found a null terminator, exit printing.
502 // Emit the function pointer.
503 EmitGlobalConstant(CS->getOperand(1));
504 }
505}
506
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507
508//===----------------------------------------------------------------------===//
509/// LEB 128 number encoding.
510
511/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
512/// representing an unsigned leb128 value.
513void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000514 char Buffer[20];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000516 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 Value >>= 7;
518 if (Value) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000519 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 if (Value) O << ", ";
521 } while (Value);
522}
523
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
525/// representing a signed leb128 value.
526void AsmPrinter::PrintSLEB128(int Value) const {
527 int Sign = Value >> (8 * sizeof(Value) - 1);
528 bool IsMore;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000529 char Buffer[20];
aslc200b112008-08-16 12:57:46 +0000530
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000532 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 Value >>= 7;
534 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
535 if (IsMore) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000536 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 if (IsMore) O << ", ";
538 } while (IsMore);
539}
540
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541//===--------------------------------------------------------------------===//
542// Emission and print routines
543//
544
545/// PrintHex - Print a value as a hexidecimal value.
546///
547void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerda2047e2008-11-10 04:30:26 +0000548 char Buffer[20];
Chris Lattnerda2047e2008-11-10 04:30:26 +0000549 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550}
551
552/// EOL - Print a newline character to asm stream. If a comment is present
553/// then it will be printed first. Comments should not contain '\n'.
554void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000555 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000557
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000559 if (VerboseAsm && !Comment.empty()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000560 O.PadToColumn(MAI->getCommentColumn());
561 O << MAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000562 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 << Comment;
564 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000565 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566}
567
Owen Anderson367bfbb2008-07-01 21:16:27 +0000568void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000569 if (VerboseAsm && *Comment) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000570 O.PadToColumn(MAI->getCommentColumn());
571 O << MAI->getCommentString()
Owen Anderson367bfbb2008-07-01 21:16:27 +0000572 << ' '
573 << Comment;
574 }
575 O << '\n';
576}
577
Bill Wendling946b5212009-08-29 12:17:53 +0000578static const char *DecodeDWARFEncoding(unsigned Encoding) {
579 switch (Encoding) {
580 case dwarf::DW_EH_PE_absptr:
581 return "absptr";
582 case dwarf::DW_EH_PE_omit:
583 return "omit";
584 case dwarf::DW_EH_PE_pcrel:
585 return "pcrel";
Bill Wendlingbe23fd42009-09-09 21:26:19 +0000586 case dwarf::DW_EH_PE_udata4:
587 return "udata4";
588 case dwarf::DW_EH_PE_udata8:
589 return "udata8";
590 case dwarf::DW_EH_PE_sdata4:
591 return "sdata4";
592 case dwarf::DW_EH_PE_sdata8:
593 return "sdata8";
Bill Wendling946b5212009-08-29 12:17:53 +0000594 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
595 return "pcrel udata4";
596 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
597 return "pcrel sdata4";
598 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
599 return "pcrel udata8";
600 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
601 return "pcrel sdata8";
602 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
603 return "indirect pcrel udata4";
604 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
605 return "indirect pcrel sdata4";
606 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
607 return "indirect pcrel udata8";
608 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
609 return "indirect pcrel sdata8";
610 }
611
612 return 0;
613}
614
Bill Wendling946b5212009-08-29 12:17:53 +0000615void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
616 if (VerboseAsm && *Comment) {
617 O.PadToColumn(MAI->getCommentColumn());
618 O << MAI->getCommentString()
619 << ' '
620 << Comment;
621
622 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
623 O << " (" << EncStr << ')';
624 }
625 O << '\n';
626}
627
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
629/// unsigned leb128 value.
630void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000631 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 O << "\t.uleb128\t"
633 << Value;
634 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000635 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 PrintULEB128(Value);
637 }
638}
639
640/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
641/// signed leb128 value.
642void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000643 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 O << "\t.sleb128\t"
645 << Value;
646 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000647 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 PrintSLEB128(Value);
649 }
650}
651
652/// EmitInt8 - Emit a byte directive and value.
653///
654void AsmPrinter::EmitInt8(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000655 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 PrintHex(Value & 0xFF);
657}
658
659/// EmitInt16 - Emit a short directive and value.
660///
661void AsmPrinter::EmitInt16(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000662 O << MAI->getData16bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 PrintHex(Value & 0xFFFF);
664}
665
666/// EmitInt32 - Emit a long directive and value.
667///
668void AsmPrinter::EmitInt32(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000669 O << MAI->getData32bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 PrintHex(Value);
671}
672
673/// EmitInt64 - Emit a long long directive and value.
674///
675void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000676 if (MAI->getData64bitsDirective()) {
677 O << MAI->getData64bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 PrintHex(Value);
679 } else {
680 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000681 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 EmitInt32(unsigned(Value));
683 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000684 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 EmitInt32(unsigned(Value >> 32));
686 }
687 }
688}
689
690/// toOctal - Convert the low order bits of X into an octal digit.
691///
692static inline char toOctal(int X) {
693 return (X&7)+'0';
694}
695
696/// printStringChar - Print a char, escaped if necessary.
697///
David Greene302008d2009-07-14 20:18:05 +0000698static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 if (C == '"') {
700 O << "\\\"";
701 } else if (C == '\\') {
702 O << "\\\\";
Chris Lattner07ec1462009-01-22 23:38:45 +0000703 } else if (isprint((unsigned char)C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 O << C;
705 } else {
706 switch(C) {
707 case '\b': O << "\\b"; break;
708 case '\f': O << "\\f"; break;
709 case '\n': O << "\\n"; break;
710 case '\r': O << "\\r"; break;
711 case '\t': O << "\\t"; break;
712 default:
713 O << '\\';
714 O << toOctal(C >> 6);
715 O << toOctal(C >> 3);
716 O << toOctal(C >> 0);
717 break;
718 }
719 }
720}
721
722/// EmitString - Emit a string with quotes and a null terminator.
723/// Special characters are emitted properly.
724/// \literal (Eg. '\t') \endliteral
725void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling3f94b412009-04-09 23:51:31 +0000726 EmitString(String.c_str(), String.size());
727}
728
729void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000730 const char* AscizDirective = MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 if (AscizDirective)
732 O << AscizDirective;
733 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000734 O << MAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000735 O << '\"';
Bill Wendling3f94b412009-04-09 23:51:31 +0000736 for (unsigned i = 0; i < Size; ++i)
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000737 printStringChar(O, String[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000739 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 else
741 O << "\\0\"";
742}
743
744
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000745/// EmitFile - Emit a .file directive.
746void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
747 O << "\t.file\t" << Number << " \"";
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000748 for (unsigned i = 0, N = Name.size(); i < N; ++i)
749 printStringChar(O, Name[i]);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000750 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000751}
752
753
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754//===----------------------------------------------------------------------===//
755
756// EmitAlignment - Emit an alignment directive to the specified power of
757// two boundary. For example, if you pass in 3 here, you will get an 8
758// byte alignment. If a global value is specified, and if that global has
759// an explicit alignment requested, it will unconditionally override the
760// alignment request. However, if ForcedAlignBits is specified, this value
761// has final say: the ultimate alignment will be the max of ForcedAlignBits
762// and the alignment computed with NumBits and the global.
763//
764// The algorithm is:
765// Align = NumBits;
766// if (GV && GV->hasalignment) Align = GV->getalignment();
767// Align = std::max(Align, ForcedAlignBits);
768//
769void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000770 unsigned ForcedAlignBits,
771 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 if (GV && GV->getAlignment())
773 NumBits = Log2_32(GV->getAlignment());
774 NumBits = std::max(NumBits, ForcedAlignBits);
775
776 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner4891d142009-08-19 06:12:02 +0000777
778 unsigned FillValue = 0;
Chris Lattnerd69d8ad2009-08-18 06:15:16 +0000779 if (getCurrentSection()->getKind().isText())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000780 FillValue = MAI->getTextAlignFillValue();
Chris Lattner4891d142009-08-19 06:12:02 +0000781
782 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783}
David Greenecdc2de32009-08-05 21:00:52 +0000784
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785/// EmitZeros - Emit a block of zeros.
786///
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000787void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788 if (NumZeros) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000789 if (MAI->getZeroDirective()) {
790 O << MAI->getZeroDirective() << NumZeros;
791 if (MAI->getZeroDirectiveSuffix())
792 O << MAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000793 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 } else {
795 for (; NumZeros; --NumZeros)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000796 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 }
798 }
799}
800
801// Print out the specified constant, without a storage class. Only the
802// constants valid in constant expressions can occur here.
803void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
804 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000805 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000807 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
809 // This is a constant address for a global variable or function. Use the
Chris Lattner36d03292009-09-15 23:11:32 +0000810 // name of the variable or function as the address value.
811 O << Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
813 const TargetData *TD = TM.getTargetData();
814 unsigned Opcode = CE->getOpcode();
815 switch (Opcode) {
Chris Lattner34e4cde2009-08-01 22:25:12 +0000816 case Instruction::Trunc:
817 case Instruction::ZExt:
818 case Instruction::SExt:
819 case Instruction::FPTrunc:
820 case Instruction::FPExt:
821 case Instruction::UIToFP:
822 case Instruction::SIToFP:
823 case Instruction::FPToUI:
824 case Instruction::FPToSI:
825 llvm_unreachable("FIXME: Don't support this constant cast expr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 case Instruction::GetElementPtr: {
827 // generate a symbolic expression for the byte address
828 const Constant *ptrVal = CE->getOperand(0);
829 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
830 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
831 idxVec.size())) {
Chris Lattner911129a2009-02-05 06:55:21 +0000832 // Truncate/sext the offset to the pointer size.
833 if (TD->getPointerSizeInBits() != 64) {
834 int SExtAmount = 64-TD->getPointerSizeInBits();
835 Offset = (Offset << SExtAmount) >> SExtAmount;
836 }
837
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000839 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 EmitConstantValueOnly(ptrVal);
841 if (Offset > 0)
842 O << ") + " << Offset;
843 else if (Offset < 0)
844 O << ") - " << -Offset;
845 } else {
846 EmitConstantValueOnly(ptrVal);
847 }
848 break;
849 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 case Instruction::BitCast:
851 return EmitConstantValueOnly(CE->getOperand(0));
852
853 case Instruction::IntToPtr: {
854 // Handle casts to pointers by changing them into casts to the appropriate
855 // integer type. This promotes constant folding and simplifies this code.
856 Constant *Op = CE->getOperand(0);
Owen Anderson35b47072009-08-13 21:58:54 +0000857 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
858 false/*ZExt*/);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 return EmitConstantValueOnly(Op);
860 }
861
862
863 case Instruction::PtrToInt: {
864 // Support only foldable casts to/from pointers that can be eliminated by
865 // changing the pointer to the appropriately sized integer type.
866 Constant *Op = CE->getOperand(0);
867 const Type *Ty = CE->getType();
868
869 // We can emit the pointer value into this slot if the slot is an
870 // integer slot greater or equal to the size of the pointer.
Chris Lattner34e4cde2009-08-01 22:25:12 +0000871 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000873
874 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 EmitConstantValueOnly(Op);
Chris Lattner34e4cde2009-08-01 22:25:12 +0000876 APInt ptrMask =
877 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner89b36582008-08-17 07:19:36 +0000878
879 SmallString<40> S;
880 ptrMask.toStringUnsigned(S);
Daniel Dunbar768e97d2009-08-19 20:07:03 +0000881 O << ") & " << S.str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 break;
883 }
884 case Instruction::Add:
885 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000886 case Instruction::And:
887 case Instruction::Or:
888 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000889 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000891 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000892 switch (Opcode) {
893 case Instruction::Add:
894 O << " + ";
895 break;
896 case Instruction::Sub:
897 O << " - ";
898 break;
899 case Instruction::And:
900 O << " & ";
901 break;
902 case Instruction::Or:
903 O << " | ";
904 break;
905 case Instruction::Xor:
906 O << " ^ ";
907 break;
908 default:
909 break;
910 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000911 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000913 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 break;
915 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000916 llvm_unreachable("Unsupported operator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 }
918 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000919 llvm_unreachable("Unknown constant value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 }
921}
922
923/// printAsCString - Print the specified array as a C compatible string, only if
924/// the predicate isString is true.
925///
David Greene302008d2009-07-14 20:18:05 +0000926static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927 unsigned LastElt) {
928 assert(CVA->isString() && "Array is not string compatible!");
929
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000930 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931 for (unsigned i = 0; i != LastElt; ++i) {
932 unsigned char C =
933 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
934 printStringChar(O, C);
935 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000936 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937}
938
939/// EmitString - Emit a zero-byte-terminated string constant.
940///
941void AsmPrinter::EmitString(const ConstantArray *CVA) const {
942 unsigned NumElts = CVA->getNumOperands();
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000943 if (MAI->getAscizDirective() && NumElts &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000945 O << MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 printAsCString(O, CVA, NumElts-1);
947 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000948 O << MAI->getAsciiDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 printAsCString(O, CVA, NumElts);
950 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000951 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952}
953
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000954void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
955 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000956 if (CVA->isString()) {
957 EmitString(CVA);
958 } else { // Not a string. Print the values in successive locations
959 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000960 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000961 }
962}
963
964void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
965 const VectorType *PTy = CP->getType();
966
967 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
968 EmitGlobalConstant(CP->getOperand(I));
969}
970
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000971void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
972 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000973 // Print the fields in successive locations. Pad to align if needed!
974 const TargetData *TD = TM.getTargetData();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000975 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000976 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
977 uint64_t sizeSoFar = 0;
978 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
979 const Constant* field = CVS->getOperand(i);
980
981 // Check if padding is needed and insert one or more 0s.
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000982 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000983 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
984 - cvsLayout->getElementOffset(i)) - fieldSize;
985 sizeSoFar += fieldSize + padSize;
986
987 // Now print the actual field value.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000988 EmitGlobalConstant(field, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000989
990 // Insert padding - this may include padding to increase the size of the
991 // current field up to the ABI size (if the struct is not packed) as well
992 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000993 EmitZeros(padSize, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000994 }
995 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
996 "Layout of constant struct may be incorrect!");
997}
998
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000999void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1000 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001001 // FP Constants are printed as integer constants to avoid losing
1002 // precision...
Owen Anderson35b47072009-08-13 21:58:54 +00001003 LLVMContext &Context = CFP->getContext();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001004 const TargetData *TD = TM.getTargetData();
Owen Anderson35b47072009-08-13 21:58:54 +00001005 if (CFP->getType() == Type::getDoubleTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001006 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1007 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001008 if (MAI->getData64bitsDirective(AddrSpace)) {
1009 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohman87581eb2009-08-12 18:32:22 +00001010 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001011 O.PadToColumn(MAI->getCommentColumn());
1012 O << MAI->getCommentString() << " double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001013 }
Evan Cheng11db8142009-03-24 00:17:40 +00001014 O << '\n';
1015 } else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001016 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001017 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001018 O.PadToColumn(MAI->getCommentColumn());
1019 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001020 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001021 }
Evan Cheng11db8142009-03-24 00:17:40 +00001022 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001023 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001024 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001025 O.PadToColumn(MAI->getCommentColumn());
1026 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001027 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001028 }
Evan Cheng11db8142009-03-24 00:17:40 +00001029 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001030 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001031 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001032 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001033 O.PadToColumn(MAI->getCommentColumn());
1034 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001035 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001036 }
Evan Cheng11db8142009-03-24 00:17:40 +00001037 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001038 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001039 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001040 O.PadToColumn(MAI->getCommentColumn());
1041 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001042 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001043 }
Evan Cheng11db8142009-03-24 00:17:40 +00001044 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001045 }
1046 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001047 } else if (CFP->getType() == Type::getFloatTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001048 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001049 O << MAI->getData32bitsDirective(AddrSpace)
Evan Cheng11db8142009-03-24 00:17:40 +00001050 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman87581eb2009-08-12 18:32:22 +00001051 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001052 O.PadToColumn(MAI->getCommentColumn());
1053 O << MAI->getCommentString() << " float " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001054 }
Evan Cheng11db8142009-03-24 00:17:40 +00001055 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001056 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001057 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001058 // all long double variants are printed as hex
1059 // api needed to prevent premature destruction
1060 APInt api = CFP->getValueAPF().bitcastToAPInt();
1061 const uint64_t *p = api.getRawData();
1062 // Convert to double so we can print the approximate val as a comment.
1063 APFloat DoubleVal = CFP->getValueAPF();
1064 bool ignored;
1065 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1066 &ignored);
1067 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001068 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001069 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001070 O.PadToColumn(MAI->getCommentColumn());
1071 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001072 << " most significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001073 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001074 }
Evan Cheng11db8142009-03-24 00:17:40 +00001075 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001076 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001077 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001078 O.PadToColumn(MAI->getCommentColumn());
1079 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001080 }
Evan Cheng11db8142009-03-24 00:17:40 +00001081 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001082 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001083 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001084 O.PadToColumn(MAI->getCommentColumn());
1085 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001086 }
Evan Cheng11db8142009-03-24 00:17:40 +00001087 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001088 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001089 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001090 O.PadToColumn(MAI->getCommentColumn());
1091 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001092 }
Evan Cheng11db8142009-03-24 00:17:40 +00001093 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001094 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001095 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001096 O.PadToColumn(MAI->getCommentColumn());
1097 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001098 << " least significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001099 }
Evan Cheng11db8142009-03-24 00:17:40 +00001100 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001101 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001102 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001103 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001104 O.PadToColumn(MAI->getCommentColumn());
1105 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001106 << " least significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001107 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001108 }
Evan Cheng11db8142009-03-24 00:17:40 +00001109 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001110 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001111 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001112 O.PadToColumn(MAI->getCommentColumn());
1113 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001114 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001115 }
Evan Cheng11db8142009-03-24 00:17:40 +00001116 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001117 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001118 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001119 O.PadToColumn(MAI->getCommentColumn());
1120 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001121 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001122 }
Evan Cheng11db8142009-03-24 00:17:40 +00001123 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001124 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001125 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001126 O.PadToColumn(MAI->getCommentColumn());
1127 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001128 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001129 }
Evan Cheng11db8142009-03-24 00:17:40 +00001130 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001131 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001132 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001133 O.PadToColumn(MAI->getCommentColumn());
1134 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001135 << " most significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001136 }
Evan Cheng11db8142009-03-24 00:17:40 +00001137 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001138 }
Owen Anderson35b47072009-08-13 21:58:54 +00001139 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1140 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001141 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001142 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001143 // all long double variants are printed as hex
1144 // api needed to prevent premature destruction
1145 APInt api = CFP->getValueAPF().bitcastToAPInt();
1146 const uint64_t *p = api.getRawData();
1147 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001148 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001149 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001150 O.PadToColumn(MAI->getCommentColumn());
1151 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001152 << " most significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001153 }
Evan Cheng11db8142009-03-24 00:17:40 +00001154 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001155 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001156 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001157 O.PadToColumn(MAI->getCommentColumn());
1158 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001159 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001160 }
Evan Cheng11db8142009-03-24 00:17:40 +00001161 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001162 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001163 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001164 O.PadToColumn(MAI->getCommentColumn());
1165 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001166 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001167 }
Evan Cheng11db8142009-03-24 00:17:40 +00001168 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001169 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001170 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001171 O.PadToColumn(MAI->getCommentColumn());
1172 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001173 << " least significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001174 }
Evan Cheng11db8142009-03-24 00:17:40 +00001175 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001176 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001177 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001178 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001179 O.PadToColumn(MAI->getCommentColumn());
1180 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001181 << " least significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001182 }
Evan Cheng11db8142009-03-24 00:17:40 +00001183 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001184 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001185 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001186 O.PadToColumn(MAI->getCommentColumn());
1187 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001188 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001189 }
Evan Cheng11db8142009-03-24 00:17:40 +00001190 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001191 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001192 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001193 O.PadToColumn(MAI->getCommentColumn());
1194 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001195 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001196 }
Evan Cheng11db8142009-03-24 00:17:40 +00001197 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001198 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001199 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001200 O.PadToColumn(MAI->getCommentColumn());
1201 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001202 << " most significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001203 }
Evan Cheng11db8142009-03-24 00:17:40 +00001204 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001205 }
1206 return;
Edwin Törökbd448e32009-07-14 16:55:14 +00001207 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001208}
1209
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001210void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1211 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001212 const TargetData *TD = TM.getTargetData();
1213 unsigned BitWidth = CI->getBitWidth();
1214 assert(isPowerOf2_32(BitWidth) &&
1215 "Non-power-of-2-sized integers not handled!");
1216
1217 // We don't expect assemblers to support integer data directives
1218 // for more than 64 bits, so we emit the data in at most 64-bit
1219 // quantities at a time.
1220 const uint64_t *RawData = CI->getValue().getRawData();
1221 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1222 uint64_t Val;
1223 if (TD->isBigEndian())
1224 Val = RawData[e - i - 1];
1225 else
1226 Val = RawData[i];
1227
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001228 if (MAI->getData64bitsDirective(AddrSpace))
1229 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001230 else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001231 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001232 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001233 O.PadToColumn(MAI->getCommentColumn());
1234 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001235 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001236 }
Evan Cheng11db8142009-03-24 00:17:40 +00001237 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001238 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001239 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001240 O.PadToColumn(MAI->getCommentColumn());
1241 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001242 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001243 }
Evan Cheng11db8142009-03-24 00:17:40 +00001244 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001245 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001246 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001247 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001248 O.PadToColumn(MAI->getCommentColumn());
1249 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001250 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001251 }
Evan Cheng11db8142009-03-24 00:17:40 +00001252 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001253 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001254 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001255 O.PadToColumn(MAI->getCommentColumn());
1256 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001257 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001258 }
Evan Cheng11db8142009-03-24 00:17:40 +00001259 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001260 }
1261 }
1262}
1263
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001265void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266 const TargetData *TD = TM.getTargetData();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001267 const Type *type = CV->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001268 unsigned Size = TD->getTypeAllocSize(type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269
1270 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001271 EmitZeros(Size, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272 return;
1273 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +00001274 EmitGlobalConstantArray(CVA , AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 return;
1276 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001277 EmitGlobalConstantStruct(CVS, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278 return;
1279 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001280 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001281 return;
1282 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1283 // Small integers are handled below; large integers are handled here.
1284 if (Size > 4) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001285 EmitGlobalConstantLargeInt(CI, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 return;
1287 }
1288 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001289 EmitGlobalConstantVector(CP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 return;
1291 }
1292
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001293 printDataDirective(type, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001294 EmitConstantValueOnly(CV);
Evan Cheng11db8142009-03-24 00:17:40 +00001295 if (VerboseAsm) {
1296 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1297 SmallString<40> S;
1298 CI->getValue().toStringUnsigned(S, 16);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001299 O.PadToColumn(MAI->getCommentColumn());
1300 O << MAI->getCommentString() << " 0x" << S.str();
Evan Cheng11db8142009-03-24 00:17:40 +00001301 }
Scott Michele067c3c2008-06-03 15:39:51 +00001302 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001303 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304}
1305
Chris Lattner89b36582008-08-17 07:19:36 +00001306void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001307 // Target doesn't support this yet!
Edwin Törökbd448e32009-07-14 16:55:14 +00001308 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001309}
1310
1311/// PrintSpecial - Print information related to the specified machine instr
1312/// that is independent of the operand, and may be independent of the instr
1313/// itself. This can be useful for portably encoding the comment character
1314/// or other bits of target-specific knowledge into the asmstrings. The
1315/// syntax used is ${:comment}. Targets can override this to add support
1316/// for their own strange codes.
Chris Lattner6af48032009-03-10 05:37:13 +00001317void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318 if (!strcmp(Code, "private")) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001319 O << MAI->getPrivateGlobalPrefix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 } else if (!strcmp(Code, "comment")) {
Evan Cheng11db8142009-03-24 00:17:40 +00001321 if (VerboseAsm)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001322 O << MAI->getCommentString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001323 } else if (!strcmp(Code, "uid")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 // Comparing the address of MI isn't sufficient, because machineinstrs may
1325 // be allocated to the same address across functions.
1326 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1327
Owen Andersonda3388b2009-06-24 22:28:12 +00001328 // If this is a new LastFn instruction, bump the counter.
1329 if (LastMI != MI || LastFn != ThisF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 ++Counter;
1331 LastMI = MI;
Owen Andersonda3388b2009-06-24 22:28:12 +00001332 LastFn = ThisF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333 }
1334 O << Counter;
1335 } else {
Edwin Törökced9ff82009-07-11 13:10:19 +00001336 std::string msg;
1337 raw_string_ostream Msg(msg);
1338 Msg << "Unknown special formatter '" << Code
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339 << "' for machine instr: " << *MI;
Edwin Törökced9ff82009-07-11 13:10:19 +00001340 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 }
1342}
1343
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001344/// processDebugLoc - Processes the debug information of each machine
1345/// instruction's DebugLoc.
1346void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001347 if (!MAI || !DW)
Chris Lattner693cfcc2009-08-05 04:09:18 +00001348 return;
1349
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001350 if (MAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001351 if (!DL.isUnknown()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001352 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1353
Chris Lattner32d4cc72009-09-09 23:14:36 +00001354 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001355 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1356 DICompileUnit(CurDLT.CompileUnit)));
Chris Lattner32d4cc72009-09-09 23:14:36 +00001357 O << '\n';
1358 }
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001359
1360 PrevDLT = CurDLT;
1361 }
1362 }
1363}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001364
1365/// printInlineAsm - This method formats and prints the specified machine
1366/// instruction that is an inline asm.
1367void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1368 unsigned NumOperands = MI->getNumOperands();
1369
1370 // Count the number of register definitions.
1371 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001372 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 ++NumDefs)
1374 assert(NumDefs != NumOperands-1 && "No asm string?");
1375
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001376 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377
1378 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1379 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1380
Dale Johannesene99fc902008-01-29 02:21:21 +00001381 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1382 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 if (AsmStr[0] == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001384 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1385 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 return;
1387 }
1388
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001389 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390
1391 // The variant of the current asmprinter.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001392 int AsmPrinterVariant = MAI->getAssemblerDialect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393
1394 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1395 const char *LastEmitted = AsmStr; // One past the last character emitted.
1396
1397 while (*LastEmitted) {
1398 switch (*LastEmitted) {
1399 default: {
1400 // Not a special case, emit the string section literally.
1401 const char *LiteralEnd = LastEmitted+1;
1402 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1403 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1404 ++LiteralEnd;
1405 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1406 O.write(LastEmitted, LiteralEnd-LastEmitted);
1407 LastEmitted = LiteralEnd;
1408 break;
1409 }
1410 case '\n':
1411 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001412 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 break;
1414 case '$': {
1415 ++LastEmitted; // Consume '$' character.
1416 bool Done = true;
1417
1418 // Handle escapes.
1419 switch (*LastEmitted) {
1420 default: Done = false; break;
1421 case '$': // $$ -> $
1422 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1423 O << '$';
1424 ++LastEmitted; // Consume second '$' character.
1425 break;
1426 case '(': // $( -> same as GCC's { character.
1427 ++LastEmitted; // Consume '(' character.
1428 if (CurVariant != -1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001429 llvm_report_error("Nested variants found in inline asm string: '"
1430 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 }
1432 CurVariant = 0; // We're in the first variant now.
1433 break;
1434 case '|':
1435 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001436 if (CurVariant == -1)
1437 O << '|'; // this is gcc's behavior for | outside a variant
1438 else
1439 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001440 break;
1441 case ')': // $) -> same as GCC's } char.
1442 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001443 if (CurVariant == -1)
1444 O << '}'; // this is gcc's behavior for } outside a variant
1445 else
1446 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 break;
1448 }
1449 if (Done) break;
1450
1451 bool HasCurlyBraces = false;
1452 if (*LastEmitted == '{') { // ${variable}
1453 ++LastEmitted; // Consume '{' character.
1454 HasCurlyBraces = true;
1455 }
1456
Chris Lattner6af48032009-03-10 05:37:13 +00001457 // If we have ${:foo}, then this is not a real operand reference, it is a
1458 // "magic" string reference, just like in .td files. Arrange to call
1459 // PrintSpecial.
1460 if (HasCurlyBraces && *LastEmitted == ':') {
1461 ++LastEmitted;
1462 const char *StrStart = LastEmitted;
1463 const char *StrEnd = strchr(StrStart, '}');
1464 if (StrEnd == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001465 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1466 + std::string(AsmStr) + "'");
Chris Lattner6af48032009-03-10 05:37:13 +00001467 }
1468
1469 std::string Val(StrStart, StrEnd);
1470 PrintSpecial(MI, Val.c_str());
1471 LastEmitted = StrEnd+1;
1472 break;
1473 }
1474
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 const char *IDStart = LastEmitted;
1476 char *IDEnd;
1477 errno = 0;
1478 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1479 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001480 llvm_report_error("Bad $ operand number in inline asm string: '"
1481 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482 }
1483 LastEmitted = IDEnd;
1484
1485 char Modifier[2] = { 0, 0 };
1486
1487 if (HasCurlyBraces) {
1488 // If we have curly braces, check for a modifier character. This
1489 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1490 if (*LastEmitted == ':') {
1491 ++LastEmitted; // Consume ':' character.
1492 if (*LastEmitted == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001493 llvm_report_error("Bad ${:} expression in inline asm string: '"
1494 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 }
1496
1497 Modifier[0] = *LastEmitted;
1498 ++LastEmitted; // Consume modifier character.
1499 }
1500
1501 if (*LastEmitted != '}') {
Edwin Törökced9ff82009-07-11 13:10:19 +00001502 llvm_report_error("Bad ${} expression in inline asm string: '"
1503 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 }
1505 ++LastEmitted; // Consume '}' character.
1506 }
1507
1508 if ((unsigned)Val >= NumOperands-1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001509 llvm_report_error("Invalid $ operand number in inline asm string: '"
1510 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511 }
1512
1513 // Okay, we finally have a value number. Ask the target to print this
1514 // operand!
1515 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1516 unsigned OpNo = 1;
1517
1518 bool Error = false;
1519
1520 // Scan to find the machine operand number for the operand.
1521 for (; Val; --Val) {
1522 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001523 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng92167402009-03-20 18:03:34 +00001524 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001525 }
1526
1527 if (OpNo >= MI->getNumOperands()) {
1528 Error = true;
1529 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001530 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531 ++OpNo; // Skip over the ID number.
1532
Dale Johannesencfb19e62007-11-05 21:20:28 +00001533 if (Modifier[0]=='l') // labels are target independent
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001534 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1535 ->getNumber())->print(O, MAI);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001536 else {
1537 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001538 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001539 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1540 Modifier[0] ? Modifier : 0);
1541 } else {
1542 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1543 Modifier[0] ? Modifier : 0);
1544 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 }
1546 }
1547 if (Error) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001548 std::string msg;
1549 raw_string_ostream Msg(msg);
1550 Msg << "Invalid operand found in inline asm: '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551 << AsmStr << "'\n";
Edwin Törökced9ff82009-07-11 13:10:19 +00001552 MI->print(Msg);
1553 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 }
1555 }
1556 break;
1557 }
1558 }
1559 }
Chris Lattner32d4cc72009-09-09 23:14:36 +00001560 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561}
1562
Evan Cheng3c0eda52008-03-15 00:03:38 +00001563/// printImplicitDef - This method prints the specified machine instruction
1564/// that is an implicit def.
1565void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001566 if (!VerboseAsm) return;
1567 O.PadToColumn(MAI->getCommentColumn());
1568 O << MAI->getCommentString() << " implicit-def: "
Chris Lattner28f7e352009-09-13 19:48:37 +00001569 << TRI->getName(MI->getOperand(0).getReg());
Evan Cheng3c0eda52008-03-15 00:03:38 +00001570}
1571
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572/// printLabel - This method prints a local label used by debug and
1573/// exception handling tables.
1574void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001575 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576}
1577
Evan Chenga53c40a2008-02-01 09:10:45 +00001578void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001579 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Chenga53c40a2008-02-01 09:10:45 +00001580}
1581
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001582/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1583/// instruction, using the specified assembler variant. Targets should
1584/// overried this to format as appropriate.
1585bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1586 unsigned AsmVariant, const char *ExtraCode) {
1587 // Target doesn't support this yet!
1588 return true;
1589}
1590
1591bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1592 unsigned AsmVariant,
1593 const char *ExtraCode) {
1594 // Target doesn't support this yet!
1595 return true;
1596}
1597
Chris Lattner08c97082009-09-12 23:02:08 +00001598MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1599 SmallString<60> Name;
1600 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1601 << getFunctionNumber() << '_' << MBBID;
1602
1603 return OutContext.GetOrCreateSymbol(Name.str());
1604}
1605
1606
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001607/// EmitBasicBlockStart - This method prints the label for the specified
1608/// MachineBasicBlock, an alignment (if present) and a comment describing
1609/// it if appropriate.
Chris Lattnerda5fb6d2009-09-14 03:15:54 +00001610void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001611 if (unsigned Align = MBB->getAlignment())
1612 EmitAlignment(Log2_32(Align));
Evan Cheng45c1edb2008-02-28 00:43:03 +00001613
Chris Lattner08c97082009-09-12 23:02:08 +00001614 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerda5fb6d2009-09-14 03:15:54 +00001615 O << ':';
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001616
1617 if (VerboseAsm) {
Dan Gohman495af972009-08-12 18:47:05 +00001618 if (const BasicBlock *BB = MBB->getBasicBlock())
1619 if (BB->hasName()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001620 O.PadToColumn(MAI->getCommentColumn());
1621 O << MAI->getCommentString() << ' ';
Dan Gohman5ee8d032009-08-12 20:56:56 +00001622 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohman495af972009-08-12 18:47:05 +00001623 }
David Greenee52fd872009-08-10 16:38:07 +00001624
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001625 EmitComments(*MBB);
David Greenee52fd872009-08-10 16:38:07 +00001626 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001627}
1628
Evan Cheng6fb06762007-11-09 01:32:10 +00001629/// printPICJumpTableSetLabel - This method prints a set label for the
1630/// specified MachineBasicBlock for a jumptable entry.
1631void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1632 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001633 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 return;
1635
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001636 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001637 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001638 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001639 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001640 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001641}
1642
Evan Cheng6fb06762007-11-09 01:32:10 +00001643void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1644 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001645 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646 return;
1647
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001648 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001649 << getFunctionNumber() << '_' << uid << '_' << uid2
1650 << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001651 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001652 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001653 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001654}
1655
1656/// printDataDirective - This method prints the asm directive for the
1657/// specified type.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001658void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001659 const TargetData *TD = TM.getTargetData();
1660 switch (type->getTypeID()) {
Chris Lattner1ad1c1d2009-07-15 04:42:49 +00001661 case Type::FloatTyID: case Type::DoubleTyID:
1662 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1663 assert(0 && "Should have already output floating point constant.");
1664 default:
1665 assert(0 && "Can't handle printing this type of thing");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001666 case Type::IntegerTyID: {
1667 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1668 if (BitWidth <= 8)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001669 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001670 else if (BitWidth <= 16)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001671 O << MAI->getData16bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001672 else if (BitWidth <= 32)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001673 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001674 else if (BitWidth <= 64) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001675 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676 "Target cannot handle 64-bit constant exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001677 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman07a91ea2008-09-08 16:40:13 +00001678 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00001679 llvm_unreachable("Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001680 }
1681 break;
1682 }
1683 case Type::PointerTyID:
1684 if (TD->getPointerSize() == 8) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001685 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 "Target cannot handle 64-bit pointer exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001687 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001688 } else if (TD->getPointerSize() == 2) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001689 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001690 } else if (TD->getPointerSize() == 1) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001691 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001692 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001693 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 }
1695 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001696 }
1697}
1698
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001699void AsmPrinter::printVisibility(const std::string& Name,
1700 unsigned Visibility) const {
1701 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001702 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001703 O << Directive << Name << '\n';
1704 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001705 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001706 O << Directive << Name << '\n';
1707 }
1708}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001709
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001710void AsmPrinter::printOffset(int64_t Offset) const {
1711 if (Offset > 0)
1712 O << '+' << Offset;
1713 else if (Offset < 0)
1714 O << Offset;
1715}
1716
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001717GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1718 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001719 return 0;
1720
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001721 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001722 if (GCPI != GCMetadataPrinters.end())
1723 return GCPI->second;
1724
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001725 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001726
1727 for (GCMetadataPrinterRegistry::iterator
1728 I = GCMetadataPrinterRegistry::begin(),
1729 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1730 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001731 GCMetadataPrinter *GMP = I->instantiate();
1732 GMP->S = S;
1733 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1734 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001735 }
1736
Chris Lattnerbf9d76d2009-08-23 07:19:13 +00001737 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001738 llvm_unreachable(0);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001739}
David Greene63486122009-07-13 20:25:48 +00001740
1741/// EmitComments - Pretty-print comments for instructions
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001742void AsmPrinter::EmitComments(const MachineInstr &MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001743 assert(VerboseAsm && !MI.getDebugLoc().isUnknown());
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001744
1745 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001746
Chris Lattner46148952009-08-17 15:48:08 +00001747 // Print source line info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001748 O.PadToColumn(MAI->getCommentColumn());
1749 O << MAI->getCommentString() << " SrcLine ";
Devang Patel15e723d2009-08-28 23:24:31 +00001750 if (DLT.CompileUnit) {
1751 std::string Str;
1752 DICompileUnit CU(DLT.CompileUnit);
1753 O << CU.getFilename(Str) << " ";
David Greene4a37a692009-07-16 22:24:20 +00001754 }
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001755 O << DLT.Line;
1756 if (DLT.Col != 0)
1757 O << ":" << DLT.Col;
David Greene63486122009-07-13 20:25:48 +00001758}
1759
David Greene066ed6a2009-08-18 19:22:55 +00001760/// PrintChildLoopComment - Print comments about child loops within
1761/// the loop for this basic block, with nesting.
1762///
1763static void PrintChildLoopComment(formatted_raw_ostream &O,
1764 const MachineLoop *loop,
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001765 const MCAsmInfo *MAI,
David Greene066ed6a2009-08-18 19:22:55 +00001766 int FunctionNumber) {
1767 // Add child loop information
1768 for(MachineLoop::iterator cl = loop->begin(),
1769 clend = loop->end();
1770 cl != clend;
1771 ++cl) {
1772 MachineBasicBlock *Header = (*cl)->getHeader();
1773 assert(Header && "No header for loop");
1774
1775 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001776 O.PadToColumn(MAI->getCommentColumn());
David Greene066ed6a2009-08-18 19:22:55 +00001777
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001778 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001779 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001780 << " Child Loop BB" << FunctionNumber << "_"
1781 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1782
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001783 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greene066ed6a2009-08-18 19:22:55 +00001784 }
1785}
1786
David Greenee52fd872009-08-10 16:38:07 +00001787/// EmitComments - Pretty-print comments for basic blocks
1788void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1789{
David Greene066ed6a2009-08-18 19:22:55 +00001790 if (VerboseAsm) {
David Greenee52fd872009-08-10 16:38:07 +00001791 // Add loop depth information
1792 const MachineLoop *loop = LI->getLoopFor(&MBB);
1793
1794 if (loop) {
1795 // Print a newline after bb# annotation.
1796 O << "\n";
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001797 O.PadToColumn(MAI->getCommentColumn());
1798 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greenee52fd872009-08-10 16:38:07 +00001799 << '\n';
1800
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001801 O.PadToColumn(MAI->getCommentColumn());
David Greenee52fd872009-08-10 16:38:07 +00001802
1803 MachineBasicBlock *Header = loop->getHeader();
1804 assert(Header && "No header for loop");
1805
1806 if (Header == &MBB) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001807 O << MAI->getCommentString() << " Loop Header";
1808 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greenee52fd872009-08-10 16:38:07 +00001809 }
1810 else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001811 O << MAI->getCommentString() << " Loop Header is BB"
David Greenee52fd872009-08-10 16:38:07 +00001812 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1813 }
1814
1815 if (loop->empty()) {
1816 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001817 O.PadToColumn(MAI->getCommentColumn());
1818 O << MAI->getCommentString() << " Inner Loop";
David Greenee52fd872009-08-10 16:38:07 +00001819 }
1820
1821 // Add parent loop information
1822 for (const MachineLoop *CurLoop = loop->getParentLoop();
1823 CurLoop;
1824 CurLoop = CurLoop->getParentLoop()) {
1825 MachineBasicBlock *Header = CurLoop->getHeader();
1826 assert(Header && "No header for loop");
1827
1828 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001829 O.PadToColumn(MAI->getCommentColumn());
1830 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001831 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001832 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greenee52fd872009-08-10 16:38:07 +00001833 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1834 }
1835 }
1836 }
1837}