blob: 6a9428737fa9b9a4d977567e462c9f2ce892bfaf [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);
109
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000110 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000111 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000112
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000113 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000114 /* Very minimal debug info. It is ignored if we emit actual
115 debug info. If we don't, this at helps the user find where
116 a function came from. */
117 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
118 }
119
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000120 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
121 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000122 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000123
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 if (!M.getModuleInlineAsm().empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000125 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 << M.getModuleInlineAsm()
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000127 << '\n' << MAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 << " End of file scope inline assembly\n";
129
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000130 if (MAI->doesSupportDebugInformation() ||
131 MAI->doesSupportExceptionHandling()) {
Chris Lattner2424eac2009-06-24 19:09:55 +0000132 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
133 if (MMI)
Devang Patel1a454842009-06-19 21:54:26 +0000134 MMI->AnalyzeModule(M);
Chris Lattner2424eac2009-06-24 19:09:55 +0000135 DW = getAnalysisIfAvailable<DwarfWriter>();
136 if (DW)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000137 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel1a454842009-06-19 21:54:26 +0000138 }
139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 return false;
141}
142
143bool AsmPrinter::doFinalization(Module &M) {
Chris Lattnerae982212009-07-21 18:38:57 +0000144 // Emit global variables.
145 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
146 I != E; ++I)
147 PrintGlobalVariable(I);
148
Chris Lattnere1225cc2009-06-24 18:54:37 +0000149 // Emit final debug information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000150 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattnere1225cc2009-06-24 18:54:37 +0000151 DW->EndModule();
152
Chris Lattnerdb191f02009-06-24 18:52:01 +0000153 // If the target wants to know about weak references, print them all.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000154 if (MAI->getWeakRefDirective()) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000155 // FIXME: This is not lazy, it would be nice to only print weak references
156 // to stuff that is actually used. Note that doing so would require targets
157 // to notice uses in operands (due to constant exprs etc). This should
158 // happen with the MC stuff eventually.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Chris Lattnerdb191f02009-06-24 18:52:01 +0000160 // Print out module-level global variables here.
161 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
162 I != E; ++I) {
163 if (I->hasExternalWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000164 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000165 }
166
Chris Lattner0f98c332009-08-03 23:10:34 +0000167 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000168 if (I->hasExternalWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000169 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000170 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 }
172
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000173 if (MAI->getSetDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000174 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattnerdb191f02009-06-24 18:52:01 +0000176 I != E; ++I) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000177 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000178
179 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000180 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000181
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000182 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000183 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 else if (I->hasWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000185 O << MAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000186 else if (!I->hasLocalLinkage())
Edwin Törökbd448e32009-07-14 16:55:14 +0000187 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000188
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000189 printVisibility(Name, I->getVisibility());
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000190
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000191 O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 }
193 }
194
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000195 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000196 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
197 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
198 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000199 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000200
Dan Gohmana65530a2008-05-05 00:28:39 +0000201 // If we don't have any trampolines, then we don't require stack memory
202 // to be executable. Some targets have a directive to declare this.
Chris Lattnerdb191f02009-06-24 18:52:01 +0000203 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana65530a2008-05-05 00:28:39 +0000204 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000205 if (MAI->getNonexecutableStackDirective())
206 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 delete Mang; Mang = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +0000209 DW = 0; MMI = 0;
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000210
211 OutStreamer.Finish();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 return false;
213}
214
215void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
216 // What's my mangled name?
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000217 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000218 IncrementFunctionNumber();
David Greenee52fd872009-08-10 16:38:07 +0000219
Chris Lattner57c76b52009-09-16 00:35:39 +0000220 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +0000221 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222}
223
Evan Cheng68c18682009-03-13 07:51:59 +0000224namespace {
225 // SectionCPs - Keep track the alignment, constpool entries per Section.
226 struct SectionCPs {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000227 const MCSection *S;
Evan Cheng68c18682009-03-13 07:51:59 +0000228 unsigned Alignment;
229 SmallVector<unsigned, 4> CPEs;
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000230 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng68c18682009-03-13 07:51:59 +0000231 };
232}
233
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234/// EmitConstantPool - Print to the current output stream assembly
235/// representations of the constants in the constant pool MCP. This is
236/// used to print out constants which have been "spilled to memory" by
237/// the code generator.
238///
239void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
240 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
241 if (CP.empty()) return;
242
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000243 // Calculate sections for constant pool entries. We collect entries to go into
244 // the same section together to reduce amount of section switch statements.
Evan Cheng68c18682009-03-13 07:51:59 +0000245 SmallVector<SectionCPs, 4> CPSections;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner680c6f62009-07-22 00:28:43 +0000247 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng68c18682009-03-13 07:51:59 +0000248 unsigned Align = CPE.getAlignment();
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000249
250 SectionKind Kind;
251 switch (CPE.getRelocationInfo()) {
252 default: llvm_unreachable("Unknown section kind");
Chris Lattnera9453412009-08-01 23:57:16 +0000253 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000254 case 1:
Chris Lattnera9453412009-08-01 23:57:16 +0000255 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnered0c6762009-07-26 07:00:12 +0000256 break;
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000257 case 0:
Chris Lattnered0c6762009-07-26 07:00:12 +0000258 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnera9453412009-08-01 23:57:16 +0000259 case 4: Kind = SectionKind::getMergeableConst4(); break;
260 case 8: Kind = SectionKind::getMergeableConst8(); break;
261 case 16: Kind = SectionKind::getMergeableConst16();break;
262 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000263 }
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000264 }
265
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000266 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner680c6f62009-07-22 00:28:43 +0000267
Evan Cheng68c18682009-03-13 07:51:59 +0000268 // The number of sections are small, just do a linear search from the
269 // last section to the first.
270 bool Found = false;
271 unsigned SecIdx = CPSections.size();
272 while (SecIdx != 0) {
273 if (CPSections[--SecIdx].S == S) {
274 Found = true;
275 break;
276 }
277 }
278 if (!Found) {
279 SecIdx = CPSections.size();
280 CPSections.push_back(SectionCPs(S, Align));
281 }
282
283 if (Align > CPSections[SecIdx].Alignment)
284 CPSections[SecIdx].Alignment = Align;
285 CPSections[SecIdx].CPEs.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 }
287
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000288 // Now print stuff into the calculated sections.
Evan Cheng68c18682009-03-13 07:51:59 +0000289 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000290 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng68c18682009-03-13 07:51:59 +0000291 EmitAlignment(Log2_32(CPSections[i].Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292
Evan Cheng68c18682009-03-13 07:51:59 +0000293 unsigned Offset = 0;
294 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
295 unsigned CPI = CPSections[i].CPEs[j];
296 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 // Emit inter-object padding for alignment.
Evan Cheng68c18682009-03-13 07:51:59 +0000299 unsigned AlignMask = CPE.getAlignment() - 1;
300 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
301 EmitZeros(NewOffset - Offset);
302
303 const Type *Ty = CPE.getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000304 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng68c18682009-03-13 07:51:59 +0000305
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000306 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohman87581eb2009-08-12 18:32:22 +0000307 << CPI << ':';
Evan Cheng68c18682009-03-13 07:51:59 +0000308 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000309 O.PadToColumn(MAI->getCommentColumn());
310 O << MAI->getCommentString() << " constant ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000311 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000312 }
Evan Cheng68c18682009-03-13 07:51:59 +0000313 O << '\n';
314 if (CPE.isMachineConstantPoolEntry())
315 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
316 else
317 EmitGlobalConstant(CPE.Val.ConstVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 }
319 }
320}
321
322/// EmitJumpTableInfo - Print assembly representations of the jump tables used
323/// by the current function to the current output stream.
324///
325void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
326 MachineFunction &MF) {
327 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
328 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000329
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
331
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 // Pick the directive to use to print the jump table entries, and switch to
333 // the appropriate section.
334 TargetLowering *LoweringInfo = TM.getTargetLowering();
335
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000336 const Function *F = MF.getFunction();
Evan Chengccca6f72009-06-18 20:37:15 +0000337 bool JTInDiffSection = false;
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000338 if (F->isWeakForLinker() ||
339 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 // In PIC mode, we need to emit the jump table to the same section as the
341 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000342 // We should also do if the section name is NULL or function is declared in
343 // discardable section.
Chris Lattner73266f92009-08-19 05:49:37 +0000344 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
345 TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 } else {
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000347 // Otherwise, drop it in the readonly section.
348 const MCSection *ReadOnlySection =
Chris Lattnera9453412009-08-01 23:57:16 +0000349 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner73266f92009-08-19 05:49:37 +0000350 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengccca6f72009-06-18 20:37:15 +0000351 JTInDiffSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 }
353
354 EmitAlignment(Log2_32(MJTI->getAlignment()));
355
356 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
357 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
358
359 // If this jump table was deleted, ignore it.
360 if (JTBBs.empty()) continue;
361
362 // For PIC codegen, if possible we want to use the SetDirective to reduce
363 // the number of relocations the assembler will generate for the jump table.
364 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000365 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000366 if (MAI->getSetDirective() && IsPic)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000368 if (EmittedSets.insert(JTBBs[ii]))
369 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370
Chris Lattner149495f2009-09-13 19:02:16 +0000371 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 // before each jump table. The first label is never referenced, but tells
373 // the assembler and linker the extents of the jump table object. The
374 // second label is actually referenced by the code.
Chris Lattner149495f2009-09-13 19:02:16 +0000375 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
376 O << MAI->getLinkerPrivateGlobalPrefix()
377 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengccca6f72009-06-18 20:37:15 +0000378 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000380 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +0000381 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382
383 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000384 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 O << '\n';
386 }
387 }
388}
389
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000390void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
391 const MachineBasicBlock *MBB,
392 unsigned uid) const {
Chris Lattner3fb451e2009-08-11 20:29:57 +0000393 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000394
395 // Use JumpTableDirective otherwise honor the entry size from the jump table
396 // info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000397 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000398 bool HadJTEntryDirective = JTEntryDirective != NULL;
399 if (!HadJTEntryDirective) {
400 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000401 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000402 }
403
404 O << JTEntryDirective << ' ';
405
406 // If we have emitted set directives for the jump table entries, print
407 // them rather than the entries themselves. If we're emitting PIC, then
408 // emit the table entries as differences between two text section labels.
409 // If we're emitting non-PIC code, then emit the entries as direct
410 // references to the target basic blocks.
Chris Lattner3fb451e2009-08-11 20:29:57 +0000411 if (!isPIC) {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000412 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000413 } else if (MAI->getSetDirective()) {
414 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3fb451e2009-08-11 20:29:57 +0000415 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000416 } else {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000417 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner3fb451e2009-08-11 20:29:57 +0000418 // If the arch uses custom Jump Table directives, don't calc relative to
419 // JT
420 if (!HadJTEntryDirective)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000421 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner3fb451e2009-08-11 20:29:57 +0000422 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000423 }
424}
425
426
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
428/// special global used by LLVM. If so, emit it and return true, otherwise
429/// do nothing and return false.
430bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000431 if (GV->getName() == "llvm.used") {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000432 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000433 EmitLLVMUsedList(GV->getInitializer());
434 return true;
435 }
436
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000437 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner68433442009-04-13 05:44:34 +0000438 if (GV->getSection() == "llvm.metadata" ||
439 GV->hasAvailableExternallyLinkage())
440 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441
442 if (!GV->hasAppendingLinkage()) return false;
443
444 assert(GV->hasInitializer() && "Not a special LLVM global!");
445
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 const TargetData *TD = TM.getTargetData();
447 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner0c433e92009-03-09 05:52:15 +0000448 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000449 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000450 EmitAlignment(Align, 0);
451 EmitXXStructorList(GV->getInitializer());
452 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 }
454
Chris Lattner0c433e92009-03-09 05:52:15 +0000455 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000456 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000457 EmitAlignment(Align, 0);
458 EmitXXStructorList(GV->getInitializer());
459 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 }
461
462 return false;
463}
464
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000465/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000466/// global in the specified llvm.used list for which emitUsedDirectiveFor
467/// is true, as being used with this directive.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000469 const char *Directive = MAI->getUsedDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470
Dan Gohman9e1657f2009-06-14 23:30:43 +0000471 // Should be an array of 'i8*'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
473 if (InitList == 0) return;
474
475 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner3f621b32009-07-17 22:00:23 +0000476 const GlobalValue *GV =
477 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner84362ac2009-07-31 20:52:39 +0000478 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000479 O << Directive;
480 EmitConstantValueOnly(InitList->getOperand(i));
481 O << '\n';
482 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 }
484}
485
486/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
487/// function pointers, ignoring the init priority.
488void AsmPrinter::EmitXXStructorList(Constant *List) {
489 // Should be an array of '{ int, void ()* }' structs. The first value is the
490 // init priority, which we ignore.
491 if (!isa<ConstantArray>(List)) return;
492 ConstantArray *InitList = cast<ConstantArray>(List);
493 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
494 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
495 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
496
497 if (CS->getOperand(1)->isNullValue())
498 return; // Found a null terminator, exit printing.
499 // Emit the function pointer.
500 EmitGlobalConstant(CS->getOperand(1));
501 }
502}
503
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504
505//===----------------------------------------------------------------------===//
506/// LEB 128 number encoding.
507
508/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
509/// representing an unsigned leb128 value.
510void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000511 char Buffer[20];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000513 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 Value >>= 7;
515 if (Value) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000516 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 if (Value) O << ", ";
518 } while (Value);
519}
520
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
522/// representing a signed leb128 value.
523void AsmPrinter::PrintSLEB128(int Value) const {
524 int Sign = Value >> (8 * sizeof(Value) - 1);
525 bool IsMore;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000526 char Buffer[20];
aslc200b112008-08-16 12:57:46 +0000527
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000529 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 Value >>= 7;
531 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
532 if (IsMore) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000533 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 if (IsMore) O << ", ";
535 } while (IsMore);
536}
537
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538//===--------------------------------------------------------------------===//
539// Emission and print routines
540//
541
542/// PrintHex - Print a value as a hexidecimal value.
543///
544void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerda2047e2008-11-10 04:30:26 +0000545 char Buffer[20];
Chris Lattnerda2047e2008-11-10 04:30:26 +0000546 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547}
548
549/// EOL - Print a newline character to asm stream. If a comment is present
550/// then it will be printed first. Comments should not contain '\n'.
551void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000552 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000554
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000556 if (VerboseAsm && !Comment.empty()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000557 O.PadToColumn(MAI->getCommentColumn());
558 O << MAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000559 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 << Comment;
561 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000562 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563}
564
Owen Anderson367bfbb2008-07-01 21:16:27 +0000565void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000566 if (VerboseAsm && *Comment) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000567 O.PadToColumn(MAI->getCommentColumn());
568 O << MAI->getCommentString()
Owen Anderson367bfbb2008-07-01 21:16:27 +0000569 << ' '
570 << Comment;
571 }
572 O << '\n';
573}
574
Bill Wendling946b5212009-08-29 12:17:53 +0000575static const char *DecodeDWARFEncoding(unsigned Encoding) {
576 switch (Encoding) {
577 case dwarf::DW_EH_PE_absptr:
578 return "absptr";
579 case dwarf::DW_EH_PE_omit:
580 return "omit";
581 case dwarf::DW_EH_PE_pcrel:
582 return "pcrel";
Bill Wendlingbe23fd42009-09-09 21:26:19 +0000583 case dwarf::DW_EH_PE_udata4:
584 return "udata4";
585 case dwarf::DW_EH_PE_udata8:
586 return "udata8";
587 case dwarf::DW_EH_PE_sdata4:
588 return "sdata4";
589 case dwarf::DW_EH_PE_sdata8:
590 return "sdata8";
Bill Wendling946b5212009-08-29 12:17:53 +0000591 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
592 return "pcrel udata4";
593 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
594 return "pcrel sdata4";
595 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
596 return "pcrel udata8";
597 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
598 return "pcrel sdata8";
599 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
600 return "indirect pcrel udata4";
601 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
602 return "indirect pcrel sdata4";
603 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
604 return "indirect pcrel udata8";
605 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
606 return "indirect pcrel sdata8";
607 }
608
609 return 0;
610}
611
Bill Wendling946b5212009-08-29 12:17:53 +0000612void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
613 if (VerboseAsm && *Comment) {
614 O.PadToColumn(MAI->getCommentColumn());
615 O << MAI->getCommentString()
616 << ' '
617 << Comment;
618
619 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
620 O << " (" << EncStr << ')';
621 }
622 O << '\n';
623}
624
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
626/// unsigned leb128 value.
627void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000628 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 O << "\t.uleb128\t"
630 << Value;
631 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000632 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 PrintULEB128(Value);
634 }
635}
636
637/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
638/// signed leb128 value.
639void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000640 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 O << "\t.sleb128\t"
642 << Value;
643 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000644 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 PrintSLEB128(Value);
646 }
647}
648
649/// EmitInt8 - Emit a byte directive and value.
650///
651void AsmPrinter::EmitInt8(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000652 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 PrintHex(Value & 0xFF);
654}
655
656/// EmitInt16 - Emit a short directive and value.
657///
658void AsmPrinter::EmitInt16(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000659 O << MAI->getData16bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 PrintHex(Value & 0xFFFF);
661}
662
663/// EmitInt32 - Emit a long directive and value.
664///
665void AsmPrinter::EmitInt32(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000666 O << MAI->getData32bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 PrintHex(Value);
668}
669
670/// EmitInt64 - Emit a long long directive and value.
671///
672void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000673 if (MAI->getData64bitsDirective()) {
674 O << MAI->getData64bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675 PrintHex(Value);
676 } else {
677 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000678 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 EmitInt32(unsigned(Value));
680 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000681 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 EmitInt32(unsigned(Value >> 32));
683 }
684 }
685}
686
687/// toOctal - Convert the low order bits of X into an octal digit.
688///
689static inline char toOctal(int X) {
690 return (X&7)+'0';
691}
692
693/// printStringChar - Print a char, escaped if necessary.
694///
David Greene302008d2009-07-14 20:18:05 +0000695static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 if (C == '"') {
697 O << "\\\"";
698 } else if (C == '\\') {
699 O << "\\\\";
Chris Lattner07ec1462009-01-22 23:38:45 +0000700 } else if (isprint((unsigned char)C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 O << C;
702 } else {
703 switch(C) {
704 case '\b': O << "\\b"; break;
705 case '\f': O << "\\f"; break;
706 case '\n': O << "\\n"; break;
707 case '\r': O << "\\r"; break;
708 case '\t': O << "\\t"; break;
709 default:
710 O << '\\';
711 O << toOctal(C >> 6);
712 O << toOctal(C >> 3);
713 O << toOctal(C >> 0);
714 break;
715 }
716 }
717}
718
719/// EmitString - Emit a string with quotes and a null terminator.
720/// Special characters are emitted properly.
721/// \literal (Eg. '\t') \endliteral
722void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling3f94b412009-04-09 23:51:31 +0000723 EmitString(String.c_str(), String.size());
724}
725
726void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000727 const char* AscizDirective = MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728 if (AscizDirective)
729 O << AscizDirective;
730 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000731 O << MAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000732 O << '\"';
Bill Wendling3f94b412009-04-09 23:51:31 +0000733 for (unsigned i = 0; i < Size; ++i)
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000734 printStringChar(O, String[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000736 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 else
738 O << "\\0\"";
739}
740
741
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000742/// EmitFile - Emit a .file directive.
743void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
744 O << "\t.file\t" << Number << " \"";
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000745 for (unsigned i = 0, N = Name.size(); i < N; ++i)
746 printStringChar(O, Name[i]);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000747 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000748}
749
750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751//===----------------------------------------------------------------------===//
752
753// EmitAlignment - Emit an alignment directive to the specified power of
754// two boundary. For example, if you pass in 3 here, you will get an 8
755// byte alignment. If a global value is specified, and if that global has
756// an explicit alignment requested, it will unconditionally override the
757// alignment request. However, if ForcedAlignBits is specified, this value
758// has final say: the ultimate alignment will be the max of ForcedAlignBits
759// and the alignment computed with NumBits and the global.
760//
761// The algorithm is:
762// Align = NumBits;
763// if (GV && GV->hasalignment) Align = GV->getalignment();
764// Align = std::max(Align, ForcedAlignBits);
765//
766void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000767 unsigned ForcedAlignBits,
768 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769 if (GV && GV->getAlignment())
770 NumBits = Log2_32(GV->getAlignment());
771 NumBits = std::max(NumBits, ForcedAlignBits);
772
773 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner4891d142009-08-19 06:12:02 +0000774
775 unsigned FillValue = 0;
Chris Lattnerd69d8ad2009-08-18 06:15:16 +0000776 if (getCurrentSection()->getKind().isText())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000777 FillValue = MAI->getTextAlignFillValue();
Chris Lattner4891d142009-08-19 06:12:02 +0000778
779 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780}
David Greenecdc2de32009-08-05 21:00:52 +0000781
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782/// EmitZeros - Emit a block of zeros.
783///
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000784void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 if (NumZeros) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000786 if (MAI->getZeroDirective()) {
787 O << MAI->getZeroDirective() << NumZeros;
788 if (MAI->getZeroDirectiveSuffix())
789 O << MAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000790 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 } else {
792 for (; NumZeros; --NumZeros)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000793 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 }
795 }
796}
797
798// Print out the specified constant, without a storage class. Only the
799// constants valid in constant expressions can occur here.
800void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
801 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000802 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000804 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
806 // This is a constant address for a global variable or function. Use the
Chris Lattner36d03292009-09-15 23:11:32 +0000807 // name of the variable or function as the address value.
808 O << Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
810 const TargetData *TD = TM.getTargetData();
811 unsigned Opcode = CE->getOpcode();
812 switch (Opcode) {
Chris Lattner34e4cde2009-08-01 22:25:12 +0000813 case Instruction::Trunc:
814 case Instruction::ZExt:
815 case Instruction::SExt:
816 case Instruction::FPTrunc:
817 case Instruction::FPExt:
818 case Instruction::UIToFP:
819 case Instruction::SIToFP:
820 case Instruction::FPToUI:
821 case Instruction::FPToSI:
822 llvm_unreachable("FIXME: Don't support this constant cast expr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 case Instruction::GetElementPtr: {
824 // generate a symbolic expression for the byte address
825 const Constant *ptrVal = CE->getOperand(0);
826 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
827 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
828 idxVec.size())) {
Chris Lattner911129a2009-02-05 06:55:21 +0000829 // Truncate/sext the offset to the pointer size.
830 if (TD->getPointerSizeInBits() != 64) {
831 int SExtAmount = 64-TD->getPointerSizeInBits();
832 Offset = (Offset << SExtAmount) >> SExtAmount;
833 }
834
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000836 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 EmitConstantValueOnly(ptrVal);
838 if (Offset > 0)
839 O << ") + " << Offset;
840 else if (Offset < 0)
841 O << ") - " << -Offset;
842 } else {
843 EmitConstantValueOnly(ptrVal);
844 }
845 break;
846 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 case Instruction::BitCast:
848 return EmitConstantValueOnly(CE->getOperand(0));
849
850 case Instruction::IntToPtr: {
851 // Handle casts to pointers by changing them into casts to the appropriate
852 // integer type. This promotes constant folding and simplifies this code.
853 Constant *Op = CE->getOperand(0);
Owen Anderson35b47072009-08-13 21:58:54 +0000854 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
855 false/*ZExt*/);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 return EmitConstantValueOnly(Op);
857 }
858
859
860 case Instruction::PtrToInt: {
861 // Support only foldable casts to/from pointers that can be eliminated by
862 // changing the pointer to the appropriately sized integer type.
863 Constant *Op = CE->getOperand(0);
864 const Type *Ty = CE->getType();
865
866 // We can emit the pointer value into this slot if the slot is an
867 // integer slot greater or equal to the size of the pointer.
Chris Lattner34e4cde2009-08-01 22:25:12 +0000868 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000870
871 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 EmitConstantValueOnly(Op);
Chris Lattner34e4cde2009-08-01 22:25:12 +0000873 APInt ptrMask =
874 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner89b36582008-08-17 07:19:36 +0000875
876 SmallString<40> S;
877 ptrMask.toStringUnsigned(S);
Daniel Dunbar768e97d2009-08-19 20:07:03 +0000878 O << ") & " << S.str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 break;
880 }
881 case Instruction::Add:
882 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000883 case Instruction::And:
884 case Instruction::Or:
885 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000886 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000888 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000889 switch (Opcode) {
890 case Instruction::Add:
891 O << " + ";
892 break;
893 case Instruction::Sub:
894 O << " - ";
895 break;
896 case Instruction::And:
897 O << " & ";
898 break;
899 case Instruction::Or:
900 O << " | ";
901 break;
902 case Instruction::Xor:
903 O << " ^ ";
904 break;
905 default:
906 break;
907 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000908 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000910 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 break;
912 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000913 llvm_unreachable("Unsupported operator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 }
915 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000916 llvm_unreachable("Unknown constant value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 }
918}
919
920/// printAsCString - Print the specified array as a C compatible string, only if
921/// the predicate isString is true.
922///
David Greene302008d2009-07-14 20:18:05 +0000923static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 unsigned LastElt) {
925 assert(CVA->isString() && "Array is not string compatible!");
926
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000927 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 for (unsigned i = 0; i != LastElt; ++i) {
929 unsigned char C =
930 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
931 printStringChar(O, C);
932 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000933 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934}
935
936/// EmitString - Emit a zero-byte-terminated string constant.
937///
938void AsmPrinter::EmitString(const ConstantArray *CVA) const {
939 unsigned NumElts = CVA->getNumOperands();
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000940 if (MAI->getAscizDirective() && NumElts &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000942 O << MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943 printAsCString(O, CVA, NumElts-1);
944 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000945 O << MAI->getAsciiDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 printAsCString(O, CVA, NumElts);
947 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000948 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949}
950
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000951void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
952 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000953 if (CVA->isString()) {
954 EmitString(CVA);
955 } else { // Not a string. Print the values in successive locations
956 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000957 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000958 }
959}
960
961void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
962 const VectorType *PTy = CP->getType();
963
964 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
965 EmitGlobalConstant(CP->getOperand(I));
966}
967
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000968void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
969 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000970 // Print the fields in successive locations. Pad to align if needed!
971 const TargetData *TD = TM.getTargetData();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000972 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000973 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
974 uint64_t sizeSoFar = 0;
975 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
976 const Constant* field = CVS->getOperand(i);
977
978 // Check if padding is needed and insert one or more 0s.
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000979 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000980 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
981 - cvsLayout->getElementOffset(i)) - fieldSize;
982 sizeSoFar += fieldSize + padSize;
983
984 // Now print the actual field value.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000985 EmitGlobalConstant(field, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000986
987 // Insert padding - this may include padding to increase the size of the
988 // current field up to the ABI size (if the struct is not packed) as well
989 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000990 EmitZeros(padSize, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000991 }
992 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
993 "Layout of constant struct may be incorrect!");
994}
995
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000996void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
997 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000998 // FP Constants are printed as integer constants to avoid losing
999 // precision...
Owen Anderson35b47072009-08-13 21:58:54 +00001000 LLVMContext &Context = CFP->getContext();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001001 const TargetData *TD = TM.getTargetData();
Owen Anderson35b47072009-08-13 21:58:54 +00001002 if (CFP->getType() == Type::getDoubleTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001003 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1004 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001005 if (MAI->getData64bitsDirective(AddrSpace)) {
1006 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohman87581eb2009-08-12 18:32:22 +00001007 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001008 O.PadToColumn(MAI->getCommentColumn());
1009 O << MAI->getCommentString() << " double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001010 }
Evan Cheng11db8142009-03-24 00:17:40 +00001011 O << '\n';
1012 } else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001013 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001014 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001015 O.PadToColumn(MAI->getCommentColumn());
1016 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001017 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001018 }
Evan Cheng11db8142009-03-24 00:17:40 +00001019 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001020 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001021 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001022 O.PadToColumn(MAI->getCommentColumn());
1023 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001024 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001025 }
Evan Cheng11db8142009-03-24 00:17:40 +00001026 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001027 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001028 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001029 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001030 O.PadToColumn(MAI->getCommentColumn());
1031 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001032 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001033 }
Evan Cheng11db8142009-03-24 00:17:40 +00001034 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001035 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001036 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001037 O.PadToColumn(MAI->getCommentColumn());
1038 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001039 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001040 }
Evan Cheng11db8142009-03-24 00:17:40 +00001041 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001042 }
1043 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001044 } else if (CFP->getType() == Type::getFloatTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001045 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001046 O << MAI->getData32bitsDirective(AddrSpace)
Evan Cheng11db8142009-03-24 00:17:40 +00001047 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman87581eb2009-08-12 18:32:22 +00001048 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001049 O.PadToColumn(MAI->getCommentColumn());
1050 O << MAI->getCommentString() << " float " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001051 }
Evan Cheng11db8142009-03-24 00:17:40 +00001052 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001053 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001054 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001055 // all long double variants are printed as hex
1056 // api needed to prevent premature destruction
1057 APInt api = CFP->getValueAPF().bitcastToAPInt();
1058 const uint64_t *p = api.getRawData();
1059 // Convert to double so we can print the approximate val as a comment.
1060 APFloat DoubleVal = CFP->getValueAPF();
1061 bool ignored;
1062 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1063 &ignored);
1064 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001065 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001066 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001067 O.PadToColumn(MAI->getCommentColumn());
1068 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001069 << " most significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001070 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001071 }
Evan Cheng11db8142009-03-24 00:17:40 +00001072 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001073 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001074 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001075 O.PadToColumn(MAI->getCommentColumn());
1076 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001077 }
Evan Cheng11db8142009-03-24 00:17:40 +00001078 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001079 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001080 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001081 O.PadToColumn(MAI->getCommentColumn());
1082 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001083 }
Evan Cheng11db8142009-03-24 00:17:40 +00001084 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001085 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001086 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001087 O.PadToColumn(MAI->getCommentColumn());
1088 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001089 }
Evan Cheng11db8142009-03-24 00:17:40 +00001090 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001091 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001092 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001093 O.PadToColumn(MAI->getCommentColumn());
1094 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001095 << " least significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001096 }
Evan Cheng11db8142009-03-24 00:17:40 +00001097 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001098 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001099 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001100 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001101 O.PadToColumn(MAI->getCommentColumn());
1102 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001103 << " least significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001104 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001105 }
Evan Cheng11db8142009-03-24 00:17:40 +00001106 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001107 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001108 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001109 O.PadToColumn(MAI->getCommentColumn());
1110 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001111 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001112 }
Evan Cheng11db8142009-03-24 00:17:40 +00001113 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001114 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001115 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001116 O.PadToColumn(MAI->getCommentColumn());
1117 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001118 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001119 }
Evan Cheng11db8142009-03-24 00:17:40 +00001120 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001121 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001122 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001123 O.PadToColumn(MAI->getCommentColumn());
1124 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001125 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001126 }
Evan Cheng11db8142009-03-24 00:17:40 +00001127 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001128 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001129 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001130 O.PadToColumn(MAI->getCommentColumn());
1131 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001132 << " most significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001133 }
Evan Cheng11db8142009-03-24 00:17:40 +00001134 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001135 }
Owen Anderson35b47072009-08-13 21:58:54 +00001136 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1137 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001138 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001139 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001140 // all long double variants are printed as hex
1141 // api needed to prevent premature destruction
1142 APInt api = CFP->getValueAPF().bitcastToAPInt();
1143 const uint64_t *p = api.getRawData();
1144 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001145 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001146 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001147 O.PadToColumn(MAI->getCommentColumn());
1148 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001149 << " most significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001150 }
Evan Cheng11db8142009-03-24 00:17:40 +00001151 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001152 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001153 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001154 O.PadToColumn(MAI->getCommentColumn());
1155 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001156 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001157 }
Evan Cheng11db8142009-03-24 00:17:40 +00001158 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001159 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001160 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001161 O.PadToColumn(MAI->getCommentColumn());
1162 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001163 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001164 }
Evan Cheng11db8142009-03-24 00:17:40 +00001165 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001166 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001167 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001168 O.PadToColumn(MAI->getCommentColumn());
1169 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001170 << " least significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001171 }
Evan Cheng11db8142009-03-24 00:17:40 +00001172 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001173 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001174 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001175 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001176 O.PadToColumn(MAI->getCommentColumn());
1177 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001178 << " least significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001179 }
Evan Cheng11db8142009-03-24 00:17:40 +00001180 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001181 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001182 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001183 O.PadToColumn(MAI->getCommentColumn());
1184 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001185 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001186 }
Evan Cheng11db8142009-03-24 00:17:40 +00001187 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001188 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001189 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001190 O.PadToColumn(MAI->getCommentColumn());
1191 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001192 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001193 }
Evan Cheng11db8142009-03-24 00:17:40 +00001194 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001195 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001196 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001197 O.PadToColumn(MAI->getCommentColumn());
1198 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001199 << " most significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001200 }
Evan Cheng11db8142009-03-24 00:17:40 +00001201 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001202 }
1203 return;
Edwin Törökbd448e32009-07-14 16:55:14 +00001204 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001205}
1206
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001207void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1208 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001209 const TargetData *TD = TM.getTargetData();
1210 unsigned BitWidth = CI->getBitWidth();
1211 assert(isPowerOf2_32(BitWidth) &&
1212 "Non-power-of-2-sized integers not handled!");
1213
1214 // We don't expect assemblers to support integer data directives
1215 // for more than 64 bits, so we emit the data in at most 64-bit
1216 // quantities at a time.
1217 const uint64_t *RawData = CI->getValue().getRawData();
1218 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1219 uint64_t Val;
1220 if (TD->isBigEndian())
1221 Val = RawData[e - i - 1];
1222 else
1223 Val = RawData[i];
1224
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001225 if (MAI->getData64bitsDirective(AddrSpace))
1226 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001227 else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001228 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001229 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001230 O.PadToColumn(MAI->getCommentColumn());
1231 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001232 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001233 }
Evan Cheng11db8142009-03-24 00:17:40 +00001234 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001235 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001236 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001237 O.PadToColumn(MAI->getCommentColumn());
1238 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001239 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001240 }
Evan Cheng11db8142009-03-24 00:17:40 +00001241 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001242 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001243 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001244 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001245 O.PadToColumn(MAI->getCommentColumn());
1246 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001247 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001248 }
Evan Cheng11db8142009-03-24 00:17:40 +00001249 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001250 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001251 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001252 O.PadToColumn(MAI->getCommentColumn());
1253 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001254 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001255 }
Evan Cheng11db8142009-03-24 00:17:40 +00001256 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001257 }
1258 }
1259}
1260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001262void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001263 const TargetData *TD = TM.getTargetData();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001264 const Type *type = CV->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001265 unsigned Size = TD->getTypeAllocSize(type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266
1267 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001268 EmitZeros(Size, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 return;
1270 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +00001271 EmitGlobalConstantArray(CVA , AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272 return;
1273 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001274 EmitGlobalConstantStruct(CVS, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 return;
1276 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001277 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001278 return;
1279 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1280 // Small integers are handled below; large integers are handled here.
1281 if (Size > 4) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001282 EmitGlobalConstantLargeInt(CI, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 return;
1284 }
1285 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001286 EmitGlobalConstantVector(CP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287 return;
1288 }
1289
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001290 printDataDirective(type, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 EmitConstantValueOnly(CV);
Evan Cheng11db8142009-03-24 00:17:40 +00001292 if (VerboseAsm) {
1293 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1294 SmallString<40> S;
1295 CI->getValue().toStringUnsigned(S, 16);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001296 O.PadToColumn(MAI->getCommentColumn());
1297 O << MAI->getCommentString() << " 0x" << S.str();
Evan Cheng11db8142009-03-24 00:17:40 +00001298 }
Scott Michele067c3c2008-06-03 15:39:51 +00001299 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001300 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301}
1302
Chris Lattner89b36582008-08-17 07:19:36 +00001303void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 // Target doesn't support this yet!
Edwin Törökbd448e32009-07-14 16:55:14 +00001305 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306}
1307
1308/// PrintSpecial - Print information related to the specified machine instr
1309/// that is independent of the operand, and may be independent of the instr
1310/// itself. This can be useful for portably encoding the comment character
1311/// or other bits of target-specific knowledge into the asmstrings. The
1312/// syntax used is ${:comment}. Targets can override this to add support
1313/// for their own strange codes.
Chris Lattner6af48032009-03-10 05:37:13 +00001314void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 if (!strcmp(Code, "private")) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001316 O << MAI->getPrivateGlobalPrefix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 } else if (!strcmp(Code, "comment")) {
Evan Cheng11db8142009-03-24 00:17:40 +00001318 if (VerboseAsm)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001319 O << MAI->getCommentString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 } else if (!strcmp(Code, "uid")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001321 // Comparing the address of MI isn't sufficient, because machineinstrs may
1322 // be allocated to the same address across functions.
1323 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1324
Owen Andersonda3388b2009-06-24 22:28:12 +00001325 // If this is a new LastFn instruction, bump the counter.
1326 if (LastMI != MI || LastFn != ThisF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 ++Counter;
1328 LastMI = MI;
Owen Andersonda3388b2009-06-24 22:28:12 +00001329 LastFn = ThisF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 }
1331 O << Counter;
1332 } else {
Edwin Törökced9ff82009-07-11 13:10:19 +00001333 std::string msg;
1334 raw_string_ostream Msg(msg);
1335 Msg << "Unknown special formatter '" << Code
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336 << "' for machine instr: " << *MI;
Edwin Törökced9ff82009-07-11 13:10:19 +00001337 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001338 }
1339}
1340
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001341/// processDebugLoc - Processes the debug information of each machine
1342/// instruction's DebugLoc.
1343void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001344 if (!MAI || !DW)
Chris Lattner693cfcc2009-08-05 04:09:18 +00001345 return;
1346
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001347 if (MAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001348 if (!DL.isUnknown()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001349 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1350
Chris Lattner32d4cc72009-09-09 23:14:36 +00001351 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001352 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1353 DICompileUnit(CurDLT.CompileUnit)));
Chris Lattner32d4cc72009-09-09 23:14:36 +00001354 O << '\n';
1355 }
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001356
1357 PrevDLT = CurDLT;
1358 }
1359 }
1360}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001361
1362/// printInlineAsm - This method formats and prints the specified machine
1363/// instruction that is an inline asm.
1364void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1365 unsigned NumOperands = MI->getNumOperands();
1366
1367 // Count the number of register definitions.
1368 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001369 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 ++NumDefs)
1371 assert(NumDefs != NumOperands-1 && "No asm string?");
1372
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001373 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374
1375 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1376 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1377
Dale Johannesene99fc902008-01-29 02:21:21 +00001378 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1379 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380 if (AsmStr[0] == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001381 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1382 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 return;
1384 }
1385
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001386 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387
1388 // The variant of the current asmprinter.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001389 int AsmPrinterVariant = MAI->getAssemblerDialect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390
1391 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1392 const char *LastEmitted = AsmStr; // One past the last character emitted.
1393
1394 while (*LastEmitted) {
1395 switch (*LastEmitted) {
1396 default: {
1397 // Not a special case, emit the string section literally.
1398 const char *LiteralEnd = LastEmitted+1;
1399 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1400 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1401 ++LiteralEnd;
1402 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1403 O.write(LastEmitted, LiteralEnd-LastEmitted);
1404 LastEmitted = LiteralEnd;
1405 break;
1406 }
1407 case '\n':
1408 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001409 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 break;
1411 case '$': {
1412 ++LastEmitted; // Consume '$' character.
1413 bool Done = true;
1414
1415 // Handle escapes.
1416 switch (*LastEmitted) {
1417 default: Done = false; break;
1418 case '$': // $$ -> $
1419 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1420 O << '$';
1421 ++LastEmitted; // Consume second '$' character.
1422 break;
1423 case '(': // $( -> same as GCC's { character.
1424 ++LastEmitted; // Consume '(' character.
1425 if (CurVariant != -1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001426 llvm_report_error("Nested variants found in inline asm string: '"
1427 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001428 }
1429 CurVariant = 0; // We're in the first variant now.
1430 break;
1431 case '|':
1432 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001433 if (CurVariant == -1)
1434 O << '|'; // this is gcc's behavior for | outside a variant
1435 else
1436 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 break;
1438 case ')': // $) -> same as GCC's } char.
1439 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001440 if (CurVariant == -1)
1441 O << '}'; // this is gcc's behavior for } outside a variant
1442 else
1443 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 break;
1445 }
1446 if (Done) break;
1447
1448 bool HasCurlyBraces = false;
1449 if (*LastEmitted == '{') { // ${variable}
1450 ++LastEmitted; // Consume '{' character.
1451 HasCurlyBraces = true;
1452 }
1453
Chris Lattner6af48032009-03-10 05:37:13 +00001454 // If we have ${:foo}, then this is not a real operand reference, it is a
1455 // "magic" string reference, just like in .td files. Arrange to call
1456 // PrintSpecial.
1457 if (HasCurlyBraces && *LastEmitted == ':') {
1458 ++LastEmitted;
1459 const char *StrStart = LastEmitted;
1460 const char *StrEnd = strchr(StrStart, '}');
1461 if (StrEnd == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001462 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1463 + std::string(AsmStr) + "'");
Chris Lattner6af48032009-03-10 05:37:13 +00001464 }
1465
1466 std::string Val(StrStart, StrEnd);
1467 PrintSpecial(MI, Val.c_str());
1468 LastEmitted = StrEnd+1;
1469 break;
1470 }
1471
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 const char *IDStart = LastEmitted;
1473 char *IDEnd;
1474 errno = 0;
1475 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1476 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001477 llvm_report_error("Bad $ operand number in inline asm string: '"
1478 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479 }
1480 LastEmitted = IDEnd;
1481
1482 char Modifier[2] = { 0, 0 };
1483
1484 if (HasCurlyBraces) {
1485 // If we have curly braces, check for a modifier character. This
1486 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1487 if (*LastEmitted == ':') {
1488 ++LastEmitted; // Consume ':' character.
1489 if (*LastEmitted == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001490 llvm_report_error("Bad ${:} expression in inline asm string: '"
1491 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 }
1493
1494 Modifier[0] = *LastEmitted;
1495 ++LastEmitted; // Consume modifier character.
1496 }
1497
1498 if (*LastEmitted != '}') {
Edwin Törökced9ff82009-07-11 13:10:19 +00001499 llvm_report_error("Bad ${} expression in inline asm string: '"
1500 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 }
1502 ++LastEmitted; // Consume '}' character.
1503 }
1504
1505 if ((unsigned)Val >= NumOperands-1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001506 llvm_report_error("Invalid $ operand number in inline asm string: '"
1507 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 }
1509
1510 // Okay, we finally have a value number. Ask the target to print this
1511 // operand!
1512 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1513 unsigned OpNo = 1;
1514
1515 bool Error = false;
1516
1517 // Scan to find the machine operand number for the operand.
1518 for (; Val; --Val) {
1519 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001520 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng92167402009-03-20 18:03:34 +00001521 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001522 }
1523
1524 if (OpNo >= MI->getNumOperands()) {
1525 Error = true;
1526 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001527 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001528 ++OpNo; // Skip over the ID number.
1529
Dale Johannesencfb19e62007-11-05 21:20:28 +00001530 if (Modifier[0]=='l') // labels are target independent
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001531 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1532 ->getNumber())->print(O, MAI);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001533 else {
1534 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001535 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001536 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1537 Modifier[0] ? Modifier : 0);
1538 } else {
1539 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1540 Modifier[0] ? Modifier : 0);
1541 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001542 }
1543 }
1544 if (Error) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001545 std::string msg;
1546 raw_string_ostream Msg(msg);
1547 Msg << "Invalid operand found in inline asm: '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001548 << AsmStr << "'\n";
Edwin Törökced9ff82009-07-11 13:10:19 +00001549 MI->print(Msg);
1550 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551 }
1552 }
1553 break;
1554 }
1555 }
1556 }
Chris Lattner32d4cc72009-09-09 23:14:36 +00001557 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558}
1559
Evan Cheng3c0eda52008-03-15 00:03:38 +00001560/// printImplicitDef - This method prints the specified machine instruction
1561/// that is an implicit def.
1562void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001563 if (!VerboseAsm) return;
1564 O.PadToColumn(MAI->getCommentColumn());
1565 O << MAI->getCommentString() << " implicit-def: "
Chris Lattner28f7e352009-09-13 19:48:37 +00001566 << TRI->getName(MI->getOperand(0).getReg());
Evan Cheng3c0eda52008-03-15 00:03:38 +00001567}
1568
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569/// printLabel - This method prints a local label used by debug and
1570/// exception handling tables.
1571void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001572 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001573}
1574
Evan Chenga53c40a2008-02-01 09:10:45 +00001575void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001576 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Chenga53c40a2008-02-01 09:10:45 +00001577}
1578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001579/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1580/// instruction, using the specified assembler variant. Targets should
1581/// overried this to format as appropriate.
1582bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1583 unsigned AsmVariant, const char *ExtraCode) {
1584 // Target doesn't support this yet!
1585 return true;
1586}
1587
1588bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1589 unsigned AsmVariant,
1590 const char *ExtraCode) {
1591 // Target doesn't support this yet!
1592 return true;
1593}
1594
Chris Lattner08c97082009-09-12 23:02:08 +00001595MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1596 SmallString<60> Name;
1597 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1598 << getFunctionNumber() << '_' << MBBID;
1599
1600 return OutContext.GetOrCreateSymbol(Name.str());
1601}
1602
1603
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001604/// EmitBasicBlockStart - This method prints the label for the specified
1605/// MachineBasicBlock, an alignment (if present) and a comment describing
1606/// it if appropriate.
Chris Lattnerda5fb6d2009-09-14 03:15:54 +00001607void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001608 if (unsigned Align = MBB->getAlignment())
1609 EmitAlignment(Log2_32(Align));
Evan Cheng45c1edb2008-02-28 00:43:03 +00001610
Chris Lattner08c97082009-09-12 23:02:08 +00001611 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerda5fb6d2009-09-14 03:15:54 +00001612 O << ':';
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001613
1614 if (VerboseAsm) {
Dan Gohman495af972009-08-12 18:47:05 +00001615 if (const BasicBlock *BB = MBB->getBasicBlock())
1616 if (BB->hasName()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001617 O.PadToColumn(MAI->getCommentColumn());
1618 O << MAI->getCommentString() << ' ';
Dan Gohman5ee8d032009-08-12 20:56:56 +00001619 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohman495af972009-08-12 18:47:05 +00001620 }
David Greenee52fd872009-08-10 16:38:07 +00001621
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001622 EmitComments(*MBB);
David Greenee52fd872009-08-10 16:38:07 +00001623 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001624}
1625
Evan Cheng6fb06762007-11-09 01:32:10 +00001626/// printPICJumpTableSetLabel - This method prints a set label for the
1627/// specified MachineBasicBlock for a jumptable entry.
1628void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1629 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001630 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001631 return;
1632
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001633 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001634 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001635 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001636 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001637 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638}
1639
Evan Cheng6fb06762007-11-09 01:32:10 +00001640void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1641 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001642 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001643 return;
1644
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001645 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001646 << getFunctionNumber() << '_' << uid << '_' << uid2
1647 << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001648 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001649 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001650 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001651}
1652
1653/// printDataDirective - This method prints the asm directive for the
1654/// specified type.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001655void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001656 const TargetData *TD = TM.getTargetData();
1657 switch (type->getTypeID()) {
Chris Lattner1ad1c1d2009-07-15 04:42:49 +00001658 case Type::FloatTyID: case Type::DoubleTyID:
1659 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1660 assert(0 && "Should have already output floating point constant.");
1661 default:
1662 assert(0 && "Can't handle printing this type of thing");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001663 case Type::IntegerTyID: {
1664 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1665 if (BitWidth <= 8)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001666 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001667 else if (BitWidth <= 16)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001668 O << MAI->getData16bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001669 else if (BitWidth <= 32)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001670 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001671 else if (BitWidth <= 64) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001672 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001673 "Target cannot handle 64-bit constant exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001674 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman07a91ea2008-09-08 16:40:13 +00001675 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00001676 llvm_unreachable("Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001677 }
1678 break;
1679 }
1680 case Type::PointerTyID:
1681 if (TD->getPointerSize() == 8) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001682 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001683 "Target cannot handle 64-bit pointer exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001684 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001685 } else if (TD->getPointerSize() == 2) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001686 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001687 } else if (TD->getPointerSize() == 1) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001688 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001689 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001690 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001691 }
1692 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 }
1694}
1695
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001696void AsmPrinter::printVisibility(const std::string& Name,
1697 unsigned Visibility) const {
1698 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001699 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001700 O << Directive << Name << '\n';
1701 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001702 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001703 O << Directive << Name << '\n';
1704 }
1705}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001706
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001707void AsmPrinter::printOffset(int64_t Offset) const {
1708 if (Offset > 0)
1709 O << '+' << Offset;
1710 else if (Offset < 0)
1711 O << Offset;
1712}
1713
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001714GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1715 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001716 return 0;
1717
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001718 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001719 if (GCPI != GCMetadataPrinters.end())
1720 return GCPI->second;
1721
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001722 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001723
1724 for (GCMetadataPrinterRegistry::iterator
1725 I = GCMetadataPrinterRegistry::begin(),
1726 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1727 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001728 GCMetadataPrinter *GMP = I->instantiate();
1729 GMP->S = S;
1730 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1731 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001732 }
1733
Chris Lattnerbf9d76d2009-08-23 07:19:13 +00001734 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001735 llvm_unreachable(0);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001736}
David Greene63486122009-07-13 20:25:48 +00001737
1738/// EmitComments - Pretty-print comments for instructions
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001739void AsmPrinter::EmitComments(const MachineInstr &MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001740 assert(VerboseAsm && !MI.getDebugLoc().isUnknown());
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001741
1742 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001743
Chris Lattner46148952009-08-17 15:48:08 +00001744 // Print source line info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001745 O.PadToColumn(MAI->getCommentColumn());
1746 O << MAI->getCommentString() << " SrcLine ";
Devang Patel15e723d2009-08-28 23:24:31 +00001747 if (DLT.CompileUnit) {
1748 std::string Str;
1749 DICompileUnit CU(DLT.CompileUnit);
1750 O << CU.getFilename(Str) << " ";
David Greene4a37a692009-07-16 22:24:20 +00001751 }
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001752 O << DLT.Line;
1753 if (DLT.Col != 0)
1754 O << ":" << DLT.Col;
David Greene63486122009-07-13 20:25:48 +00001755}
1756
David Greene066ed6a2009-08-18 19:22:55 +00001757/// PrintChildLoopComment - Print comments about child loops within
1758/// the loop for this basic block, with nesting.
1759///
1760static void PrintChildLoopComment(formatted_raw_ostream &O,
1761 const MachineLoop *loop,
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001762 const MCAsmInfo *MAI,
David Greene066ed6a2009-08-18 19:22:55 +00001763 int FunctionNumber) {
1764 // Add child loop information
1765 for(MachineLoop::iterator cl = loop->begin(),
1766 clend = loop->end();
1767 cl != clend;
1768 ++cl) {
1769 MachineBasicBlock *Header = (*cl)->getHeader();
1770 assert(Header && "No header for loop");
1771
1772 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001773 O.PadToColumn(MAI->getCommentColumn());
David Greene066ed6a2009-08-18 19:22:55 +00001774
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001775 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001776 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001777 << " Child Loop BB" << FunctionNumber << "_"
1778 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1779
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001780 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greene066ed6a2009-08-18 19:22:55 +00001781 }
1782}
1783
David Greenee52fd872009-08-10 16:38:07 +00001784/// EmitComments - Pretty-print comments for basic blocks
1785void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1786{
David Greene066ed6a2009-08-18 19:22:55 +00001787 if (VerboseAsm) {
David Greenee52fd872009-08-10 16:38:07 +00001788 // Add loop depth information
1789 const MachineLoop *loop = LI->getLoopFor(&MBB);
1790
1791 if (loop) {
1792 // Print a newline after bb# annotation.
1793 O << "\n";
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001794 O.PadToColumn(MAI->getCommentColumn());
1795 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greenee52fd872009-08-10 16:38:07 +00001796 << '\n';
1797
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001798 O.PadToColumn(MAI->getCommentColumn());
David Greenee52fd872009-08-10 16:38:07 +00001799
1800 MachineBasicBlock *Header = loop->getHeader();
1801 assert(Header && "No header for loop");
1802
1803 if (Header == &MBB) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001804 O << MAI->getCommentString() << " Loop Header";
1805 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greenee52fd872009-08-10 16:38:07 +00001806 }
1807 else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001808 O << MAI->getCommentString() << " Loop Header is BB"
David Greenee52fd872009-08-10 16:38:07 +00001809 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1810 }
1811
1812 if (loop->empty()) {
1813 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001814 O.PadToColumn(MAI->getCommentColumn());
1815 O << MAI->getCommentString() << " Inner Loop";
David Greenee52fd872009-08-10 16:38:07 +00001816 }
1817
1818 // Add parent loop information
1819 for (const MachineLoop *CurLoop = loop->getParentLoop();
1820 CurLoop;
1821 CurLoop = CurLoop->getParentLoop()) {
1822 MachineBasicBlock *Header = CurLoop->getHeader();
1823 assert(Header && "No header for loop");
1824
1825 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001826 O.PadToColumn(MAI->getCommentColumn());
1827 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001828 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001829 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greenee52fd872009-08-10 16:38:07 +00001830 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1831 }
1832 }
1833 }
1834}