blob: 9c6c5b5c071bb08326eeab99c6adef8759d0c5e2 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/Assembly/Writer.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Constants.h"
18#include "llvm/Module.h"
Gordon Henriksen1aed5992008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/MachineConstantPool.h"
David Greene066ed6a2009-08-18 19:22:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greenee52fd872009-08-10 16:38:07 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelfe359e72009-01-13 21:44:10 +000025#include "llvm/CodeGen/DwarfWriter.h"
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +000026#include "llvm/Analysis/DebugInfo.h"
Chris Lattner1eb0ad02009-07-27 21:28:04 +000027#include "llvm/MC/MCContext.h"
David Greene4a37a692009-07-16 22:24:20 +000028#include "llvm/MC/MCInst.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000029#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCStreamer.h"
Chris Lattner08c97082009-09-12 23:02:08 +000031#include "llvm/MC/MCSymbol.h"
Evan Cheng42ceb472009-03-25 01:47:28 +000032#include "llvm/Support/CommandLine.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000033#include "llvm/Support/ErrorHandling.h"
David Greene63486122009-07-13 20:25:48 +000034#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/Mangler.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Target/TargetData.h"
38#include "llvm/Target/TargetLowering.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng0eeed442008-07-01 23:18:29 +000040#include "llvm/Target/TargetOptions.h"
Evan Cheng3c0eda52008-03-15 00:03:38 +000041#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng6fb06762007-11-09 01:32:10 +000042#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner89b36582008-08-17 07:19:36 +000043#include "llvm/ADT/SmallString.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000044#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045#include <cerrno>
46using namespace llvm;
47
Evan Cheng42ceb472009-03-25 01:47:28 +000048static cl::opt<cl::boolOrDefault>
49AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
50 cl::init(cl::BOU_UNSET));
51
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052char AsmPrinter::ID = 0;
David Greene302008d2009-07-14 20:18:05 +000053AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +000054 const MCAsmInfo *T, bool VDef)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000055 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattnera5ef4d32009-08-22 21:43:10 +000056 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000057
58 OutContext(*new MCContext()),
Chris Lattnera835afd2009-09-14 03:02:37 +000059 // FIXME: Pass instprinter to streamer.
60 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000061
Chris Lattnerebd055c2009-08-03 23:20:21 +000062 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersonc82bd822009-06-25 16:55:32 +000063 PrevDLT(0, ~0U, ~0U) {
Chris Lattner2424eac2009-06-24 19:09:55 +000064 DW = 0; MMI = 0;
Evan Cheng42ceb472009-03-25 01:47:28 +000065 switch (AsmVerbose) {
66 case cl::BOU_UNSET: VerboseAsm = VDef; break;
67 case cl::BOU_TRUE: VerboseAsm = true; break;
68 case cl::BOU_FALSE: VerboseAsm = false; break;
69 }
70}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000072AsmPrinter::~AsmPrinter() {
73 for (gcp_iterator I = GCMetadataPrinters.begin(),
74 E = GCMetadataPrinters.end(); I != E; ++I)
75 delete I->second;
Chris Lattner1eb0ad02009-07-27 21:28:04 +000076
77 delete &OutStreamer;
78 delete &OutContext;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000079}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
Chris Lattner75bdd292009-08-03 19:12:26 +000081TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerc4c40a92009-07-28 03:13:23 +000082 return TM.getTargetLowering()->getObjFileLowering();
83}
84
Chris Lattnerd69d8ad2009-08-18 06:15:16 +000085/// getCurrentSection() - Return the current section we are emitting to.
86const MCSection *AsmPrinter::getCurrentSection() const {
87 return OutStreamer.getCurrentSection();
88}
89
90
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000091void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +000092 AU.setPreservesAll();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000093 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen1aed5992008-08-17 18:44:35 +000094 AU.addRequired<GCModuleInfo>();
David Greene066ed6a2009-08-18 19:22:55 +000095 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +000096 AU.addRequired<MachineLoopInfo>();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000097}
98
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner01316272009-07-31 17:42:42 +0000100 // Initialize TargetLoweringObjectFile.
101 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
102 .Initialize(OutContext, TM);
103
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000104 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
105 MAI->getLinkerPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000107 if (MAI->doesAllowQuotesInName())
Chris Lattner690b02c2009-06-18 23:41:35 +0000108 Mang->setUseQuotes(true);
Anton Korobeynikov3789f872009-09-18 16:57:42 +0000109
110 if (MAI->doesAllowNameToStartWithDigit())
111 Mang->setSymbolsCanStartWithDigit(true);
Chris Lattner690b02c2009-06-18 23:41:35 +0000112
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000113 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000114 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000115
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000116 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000117 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilson6ed4b622009-09-30 21:26:13 +0000118 debug info. If we don't, this at least helps the user find where
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000119 a function came from. */
120 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
121 }
122
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000123 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
124 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000125 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000126
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 if (!M.getModuleInlineAsm().empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000128 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 << M.getModuleInlineAsm()
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000130 << '\n' << MAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 << " End of file scope inline assembly\n";
132
Chris Lattnerc73e8eb2009-09-24 05:44:53 +0000133 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
134 if (MMI)
135 MMI->AnalyzeModule(M);
136 DW = getAnalysisIfAvailable<DwarfWriter>();
137 if (DW)
138 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel1a454842009-06-19 21:54:26 +0000139
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
Chris Lattnerb6113072009-09-18 20:17:03 +0000208
209 // Allow the target to emit any magic that it wants at the end of the file,
210 // after everything else has gone out.
211 EmitEndOfAsmFile(M);
212
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 delete Mang; Mang = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +0000214 DW = 0; MMI = 0;
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000215
216 OutStreamer.Finish();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 return false;
218}
219
220void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
221 // What's my mangled name?
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000222 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000223 IncrementFunctionNumber();
David Greenee52fd872009-08-10 16:38:07 +0000224
Chris Lattner57c76b52009-09-16 00:35:39 +0000225 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +0000226 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227}
228
Evan Cheng68c18682009-03-13 07:51:59 +0000229namespace {
230 // SectionCPs - Keep track the alignment, constpool entries per Section.
231 struct SectionCPs {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000232 const MCSection *S;
Evan Cheng68c18682009-03-13 07:51:59 +0000233 unsigned Alignment;
234 SmallVector<unsigned, 4> CPEs;
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000235 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng68c18682009-03-13 07:51:59 +0000236 };
237}
238
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239/// EmitConstantPool - Print to the current output stream assembly
240/// representations of the constants in the constant pool MCP. This is
241/// used to print out constants which have been "spilled to memory" by
242/// the code generator.
243///
244void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
245 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
246 if (CP.empty()) return;
247
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000248 // Calculate sections for constant pool entries. We collect entries to go into
249 // the same section together to reduce amount of section switch statements.
Evan Cheng68c18682009-03-13 07:51:59 +0000250 SmallVector<SectionCPs, 4> CPSections;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner680c6f62009-07-22 00:28:43 +0000252 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng68c18682009-03-13 07:51:59 +0000253 unsigned Align = CPE.getAlignment();
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000254
255 SectionKind Kind;
256 switch (CPE.getRelocationInfo()) {
257 default: llvm_unreachable("Unknown section kind");
Chris Lattnera9453412009-08-01 23:57:16 +0000258 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000259 case 1:
Chris Lattnera9453412009-08-01 23:57:16 +0000260 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnered0c6762009-07-26 07:00:12 +0000261 break;
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000262 case 0:
Chris Lattnered0c6762009-07-26 07:00:12 +0000263 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnera9453412009-08-01 23:57:16 +0000264 case 4: Kind = SectionKind::getMergeableConst4(); break;
265 case 8: Kind = SectionKind::getMergeableConst8(); break;
266 case 16: Kind = SectionKind::getMergeableConst16();break;
267 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000268 }
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000269 }
270
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000271 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner680c6f62009-07-22 00:28:43 +0000272
Evan Cheng68c18682009-03-13 07:51:59 +0000273 // The number of sections are small, just do a linear search from the
274 // last section to the first.
275 bool Found = false;
276 unsigned SecIdx = CPSections.size();
277 while (SecIdx != 0) {
278 if (CPSections[--SecIdx].S == S) {
279 Found = true;
280 break;
281 }
282 }
283 if (!Found) {
284 SecIdx = CPSections.size();
285 CPSections.push_back(SectionCPs(S, Align));
286 }
287
288 if (Align > CPSections[SecIdx].Alignment)
289 CPSections[SecIdx].Alignment = Align;
290 CPSections[SecIdx].CPEs.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 }
292
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000293 // Now print stuff into the calculated sections.
Evan Cheng68c18682009-03-13 07:51:59 +0000294 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000295 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng68c18682009-03-13 07:51:59 +0000296 EmitAlignment(Log2_32(CPSections[i].Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297
Evan Cheng68c18682009-03-13 07:51:59 +0000298 unsigned Offset = 0;
299 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
300 unsigned CPI = CPSections[i].CPEs[j];
301 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000302
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 // Emit inter-object padding for alignment.
Evan Cheng68c18682009-03-13 07:51:59 +0000304 unsigned AlignMask = CPE.getAlignment() - 1;
305 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
306 EmitZeros(NewOffset - Offset);
307
308 const Type *Ty = CPE.getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000309 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng68c18682009-03-13 07:51:59 +0000310
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000311 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohman87581eb2009-08-12 18:32:22 +0000312 << CPI << ':';
Evan Cheng68c18682009-03-13 07:51:59 +0000313 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000314 O.PadToColumn(MAI->getCommentColumn());
315 O << MAI->getCommentString() << " constant ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000316 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000317 }
Evan Cheng68c18682009-03-13 07:51:59 +0000318 O << '\n';
319 if (CPE.isMachineConstantPoolEntry())
320 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
321 else
322 EmitGlobalConstant(CPE.Val.ConstVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 }
324 }
325}
326
327/// EmitJumpTableInfo - Print assembly representations of the jump tables used
328/// by the current function to the current output stream.
329///
330void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
331 MachineFunction &MF) {
332 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
333 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000334
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
336
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 // Pick the directive to use to print the jump table entries, and switch to
338 // the appropriate section.
339 TargetLowering *LoweringInfo = TM.getTargetLowering();
340
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000341 const Function *F = MF.getFunction();
Evan Chengccca6f72009-06-18 20:37:15 +0000342 bool JTInDiffSection = false;
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000343 if (F->isWeakForLinker() ||
344 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 // In PIC mode, we need to emit the jump table to the same section as the
346 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000347 // We should also do if the section name is NULL or function is declared in
348 // discardable section.
Chris Lattner73266f92009-08-19 05:49:37 +0000349 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
350 TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 } else {
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000352 // Otherwise, drop it in the readonly section.
353 const MCSection *ReadOnlySection =
Chris Lattnera9453412009-08-01 23:57:16 +0000354 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner73266f92009-08-19 05:49:37 +0000355 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengccca6f72009-06-18 20:37:15 +0000356 JTInDiffSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 }
358
359 EmitAlignment(Log2_32(MJTI->getAlignment()));
360
361 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
362 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
363
364 // If this jump table was deleted, ignore it.
365 if (JTBBs.empty()) continue;
366
367 // For PIC codegen, if possible we want to use the SetDirective to reduce
368 // the number of relocations the assembler will generate for the jump table.
369 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000370 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000371 if (MAI->getSetDirective() && IsPic)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000373 if (EmittedSets.insert(JTBBs[ii]))
374 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375
Chris Lattner149495f2009-09-13 19:02:16 +0000376 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 // before each jump table. The first label is never referenced, but tells
378 // the assembler and linker the extents of the jump table object. The
379 // second label is actually referenced by the code.
Chris Lattner149495f2009-09-13 19:02:16 +0000380 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
381 O << MAI->getLinkerPrivateGlobalPrefix()
382 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengccca6f72009-06-18 20:37:15 +0000383 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000385 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +0000386 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387
388 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000389 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 O << '\n';
391 }
392 }
393}
394
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000395void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
396 const MachineBasicBlock *MBB,
397 unsigned uid) const {
Chris Lattner3fb451e2009-08-11 20:29:57 +0000398 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000399
400 // Use JumpTableDirective otherwise honor the entry size from the jump table
401 // info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000402 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000403 bool HadJTEntryDirective = JTEntryDirective != NULL;
404 if (!HadJTEntryDirective) {
405 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000406 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000407 }
408
409 O << JTEntryDirective << ' ';
410
411 // If we have emitted set directives for the jump table entries, print
412 // them rather than the entries themselves. If we're emitting PIC, then
413 // emit the table entries as differences between two text section labels.
414 // If we're emitting non-PIC code, then emit the entries as direct
415 // references to the target basic blocks.
Chris Lattner3fb451e2009-08-11 20:29:57 +0000416 if (!isPIC) {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000417 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000418 } else if (MAI->getSetDirective()) {
419 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3fb451e2009-08-11 20:29:57 +0000420 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000421 } else {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000422 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner3fb451e2009-08-11 20:29:57 +0000423 // If the arch uses custom Jump Table directives, don't calc relative to
424 // JT
425 if (!HadJTEntryDirective)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000426 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner3fb451e2009-08-11 20:29:57 +0000427 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000428 }
429}
430
431
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
433/// special global used by LLVM. If so, emit it and return true, otherwise
434/// do nothing and return false.
435bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000436 if (GV->getName() == "llvm.used") {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000437 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000438 EmitLLVMUsedList(GV->getInitializer());
439 return true;
440 }
441
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000442 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner68433442009-04-13 05:44:34 +0000443 if (GV->getSection() == "llvm.metadata" ||
444 GV->hasAvailableExternallyLinkage())
445 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446
447 if (!GV->hasAppendingLinkage()) return false;
448
449 assert(GV->hasInitializer() && "Not a special LLVM global!");
450
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 const TargetData *TD = TM.getTargetData();
452 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner0c433e92009-03-09 05:52:15 +0000453 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000454 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000455 EmitAlignment(Align, 0);
456 EmitXXStructorList(GV->getInitializer());
457 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 }
459
Chris Lattner0c433e92009-03-09 05:52:15 +0000460 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000461 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000462 EmitAlignment(Align, 0);
463 EmitXXStructorList(GV->getInitializer());
464 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 }
466
467 return false;
468}
469
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000470/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000471/// global in the specified llvm.used list for which emitUsedDirectiveFor
472/// is true, as being used with this directive.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000474 const char *Directive = MAI->getUsedDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475
Dan Gohman9e1657f2009-06-14 23:30:43 +0000476 // Should be an array of 'i8*'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
478 if (InitList == 0) return;
479
480 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner3f621b32009-07-17 22:00:23 +0000481 const GlobalValue *GV =
482 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner84362ac2009-07-31 20:52:39 +0000483 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000484 O << Directive;
485 EmitConstantValueOnly(InitList->getOperand(i));
486 O << '\n';
487 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 }
489}
490
491/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
492/// function pointers, ignoring the init priority.
493void AsmPrinter::EmitXXStructorList(Constant *List) {
494 // Should be an array of '{ int, void ()* }' structs. The first value is the
495 // init priority, which we ignore.
496 if (!isa<ConstantArray>(List)) return;
497 ConstantArray *InitList = cast<ConstantArray>(List);
498 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
499 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
500 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
501
502 if (CS->getOperand(1)->isNullValue())
503 return; // Found a null terminator, exit printing.
504 // Emit the function pointer.
505 EmitGlobalConstant(CS->getOperand(1));
506 }
507}
508
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509
510//===----------------------------------------------------------------------===//
511/// LEB 128 number encoding.
512
513/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
514/// representing an unsigned leb128 value.
515void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000516 char Buffer[20];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000518 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 Value >>= 7;
520 if (Value) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000521 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 if (Value) O << ", ";
523 } while (Value);
524}
525
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
527/// representing a signed leb128 value.
528void AsmPrinter::PrintSLEB128(int Value) const {
529 int Sign = Value >> (8 * sizeof(Value) - 1);
530 bool IsMore;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000531 char Buffer[20];
aslc200b112008-08-16 12:57:46 +0000532
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000534 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 Value >>= 7;
536 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
537 if (IsMore) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000538 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 if (IsMore) O << ", ";
540 } while (IsMore);
541}
542
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543//===--------------------------------------------------------------------===//
544// Emission and print routines
545//
546
547/// PrintHex - Print a value as a hexidecimal value.
548///
549void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerda2047e2008-11-10 04:30:26 +0000550 char Buffer[20];
Chris Lattnerda2047e2008-11-10 04:30:26 +0000551 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552}
553
554/// EOL - Print a newline character to asm stream. If a comment is present
555/// then it will be printed first. Comments should not contain '\n'.
556void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000557 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000559
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000561 if (VerboseAsm && !Comment.empty()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000562 O.PadToColumn(MAI->getCommentColumn());
563 O << MAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000564 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 << Comment;
566 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000567 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568}
569
Owen Anderson367bfbb2008-07-01 21:16:27 +0000570void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000571 if (VerboseAsm && *Comment) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000572 O.PadToColumn(MAI->getCommentColumn());
573 O << MAI->getCommentString()
Owen Anderson367bfbb2008-07-01 21:16:27 +0000574 << ' '
575 << Comment;
576 }
577 O << '\n';
578}
579
Bill Wendling946b5212009-08-29 12:17:53 +0000580static const char *DecodeDWARFEncoding(unsigned Encoding) {
581 switch (Encoding) {
582 case dwarf::DW_EH_PE_absptr:
583 return "absptr";
584 case dwarf::DW_EH_PE_omit:
585 return "omit";
586 case dwarf::DW_EH_PE_pcrel:
587 return "pcrel";
Bill Wendlingbe23fd42009-09-09 21:26:19 +0000588 case dwarf::DW_EH_PE_udata4:
589 return "udata4";
590 case dwarf::DW_EH_PE_udata8:
591 return "udata8";
592 case dwarf::DW_EH_PE_sdata4:
593 return "sdata4";
594 case dwarf::DW_EH_PE_sdata8:
595 return "sdata8";
Bill Wendling946b5212009-08-29 12:17:53 +0000596 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
597 return "pcrel udata4";
598 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
599 return "pcrel sdata4";
600 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
601 return "pcrel udata8";
602 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
603 return "pcrel sdata8";
604 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
605 return "indirect pcrel udata4";
606 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
607 return "indirect pcrel sdata4";
608 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
609 return "indirect pcrel udata8";
610 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
611 return "indirect pcrel sdata8";
612 }
613
614 return 0;
615}
616
Bill Wendling946b5212009-08-29 12:17:53 +0000617void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
618 if (VerboseAsm && *Comment) {
619 O.PadToColumn(MAI->getCommentColumn());
620 O << MAI->getCommentString()
621 << ' '
622 << Comment;
623
624 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
625 O << " (" << EncStr << ')';
626 }
627 O << '\n';
628}
629
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
631/// unsigned leb128 value.
632void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000633 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 O << "\t.uleb128\t"
635 << Value;
636 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000637 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 PrintULEB128(Value);
639 }
640}
641
642/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
643/// signed leb128 value.
644void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000645 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 O << "\t.sleb128\t"
647 << Value;
648 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000649 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 PrintSLEB128(Value);
651 }
652}
653
654/// EmitInt8 - Emit a byte directive and value.
655///
656void AsmPrinter::EmitInt8(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000657 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 PrintHex(Value & 0xFF);
659}
660
661/// EmitInt16 - Emit a short directive and value.
662///
663void AsmPrinter::EmitInt16(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000664 O << MAI->getData16bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 PrintHex(Value & 0xFFFF);
666}
667
668/// EmitInt32 - Emit a long directive and value.
669///
670void AsmPrinter::EmitInt32(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000671 O << MAI->getData32bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 PrintHex(Value);
673}
674
675/// EmitInt64 - Emit a long long directive and value.
676///
677void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000678 if (MAI->getData64bitsDirective()) {
679 O << MAI->getData64bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 PrintHex(Value);
681 } else {
682 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000683 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684 EmitInt32(unsigned(Value));
685 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000686 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 EmitInt32(unsigned(Value >> 32));
688 }
689 }
690}
691
692/// toOctal - Convert the low order bits of X into an octal digit.
693///
694static inline char toOctal(int X) {
695 return (X&7)+'0';
696}
697
698/// printStringChar - Print a char, escaped if necessary.
699///
David Greene302008d2009-07-14 20:18:05 +0000700static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 if (C == '"') {
702 O << "\\\"";
703 } else if (C == '\\') {
704 O << "\\\\";
Chris Lattner07ec1462009-01-22 23:38:45 +0000705 } else if (isprint((unsigned char)C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 O << C;
707 } else {
708 switch(C) {
709 case '\b': O << "\\b"; break;
710 case '\f': O << "\\f"; break;
711 case '\n': O << "\\n"; break;
712 case '\r': O << "\\r"; break;
713 case '\t': O << "\\t"; break;
714 default:
715 O << '\\';
716 O << toOctal(C >> 6);
717 O << toOctal(C >> 3);
718 O << toOctal(C >> 0);
719 break;
720 }
721 }
722}
723
724/// EmitString - Emit a string with quotes and a null terminator.
725/// Special characters are emitted properly.
726/// \literal (Eg. '\t') \endliteral
727void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling3f94b412009-04-09 23:51:31 +0000728 EmitString(String.c_str(), String.size());
729}
730
731void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000732 const char* AscizDirective = MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733 if (AscizDirective)
734 O << AscizDirective;
735 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000736 O << MAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000737 O << '\"';
Bill Wendling3f94b412009-04-09 23:51:31 +0000738 for (unsigned i = 0; i < Size; ++i)
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000739 printStringChar(O, String[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000741 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 else
743 O << "\\0\"";
744}
745
746
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000747/// EmitFile - Emit a .file directive.
748void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
749 O << "\t.file\t" << Number << " \"";
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000750 for (unsigned i = 0, N = Name.size(); i < N; ++i)
751 printStringChar(O, Name[i]);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000752 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000753}
754
755
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756//===----------------------------------------------------------------------===//
757
758// EmitAlignment - Emit an alignment directive to the specified power of
759// two boundary. For example, if you pass in 3 here, you will get an 8
760// byte alignment. If a global value is specified, and if that global has
761// an explicit alignment requested, it will unconditionally override the
762// alignment request. However, if ForcedAlignBits is specified, this value
763// has final say: the ultimate alignment will be the max of ForcedAlignBits
764// and the alignment computed with NumBits and the global.
765//
766// The algorithm is:
767// Align = NumBits;
768// if (GV && GV->hasalignment) Align = GV->getalignment();
769// Align = std::max(Align, ForcedAlignBits);
770//
771void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000772 unsigned ForcedAlignBits,
773 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774 if (GV && GV->getAlignment())
775 NumBits = Log2_32(GV->getAlignment());
776 NumBits = std::max(NumBits, ForcedAlignBits);
777
778 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner4891d142009-08-19 06:12:02 +0000779
780 unsigned FillValue = 0;
Chris Lattnerd69d8ad2009-08-18 06:15:16 +0000781 if (getCurrentSection()->getKind().isText())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000782 FillValue = MAI->getTextAlignFillValue();
Chris Lattner4891d142009-08-19 06:12:02 +0000783
784 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785}
David Greenecdc2de32009-08-05 21:00:52 +0000786
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787/// EmitZeros - Emit a block of zeros.
788///
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000789void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790 if (NumZeros) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000791 if (MAI->getZeroDirective()) {
792 O << MAI->getZeroDirective() << NumZeros;
793 if (MAI->getZeroDirectiveSuffix())
794 O << MAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000795 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 } else {
797 for (; NumZeros; --NumZeros)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000798 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 }
800 }
801}
802
803// Print out the specified constant, without a storage class. Only the
804// constants valid in constant expressions can occur here.
805void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
806 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000807 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000809 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
811 // This is a constant address for a global variable or function. Use the
Chris Lattner36d03292009-09-15 23:11:32 +0000812 // name of the variable or function as the address value.
813 O << Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
815 const TargetData *TD = TM.getTargetData();
816 unsigned Opcode = CE->getOpcode();
817 switch (Opcode) {
Chris Lattner34e4cde2009-08-01 22:25:12 +0000818 case Instruction::Trunc:
819 case Instruction::ZExt:
820 case Instruction::SExt:
821 case Instruction::FPTrunc:
822 case Instruction::FPExt:
823 case Instruction::UIToFP:
824 case Instruction::SIToFP:
825 case Instruction::FPToUI:
826 case Instruction::FPToSI:
827 llvm_unreachable("FIXME: Don't support this constant cast expr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 case Instruction::GetElementPtr: {
829 // generate a symbolic expression for the byte address
830 const Constant *ptrVal = CE->getOperand(0);
831 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
832 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
833 idxVec.size())) {
Chris Lattner911129a2009-02-05 06:55:21 +0000834 // Truncate/sext the offset to the pointer size.
835 if (TD->getPointerSizeInBits() != 64) {
836 int SExtAmount = 64-TD->getPointerSizeInBits();
837 Offset = (Offset << SExtAmount) >> SExtAmount;
838 }
839
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000841 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 EmitConstantValueOnly(ptrVal);
843 if (Offset > 0)
844 O << ") + " << Offset;
845 else if (Offset < 0)
846 O << ") - " << -Offset;
847 } else {
848 EmitConstantValueOnly(ptrVal);
849 }
850 break;
851 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 case Instruction::BitCast:
853 return EmitConstantValueOnly(CE->getOperand(0));
854
855 case Instruction::IntToPtr: {
856 // Handle casts to pointers by changing them into casts to the appropriate
857 // integer type. This promotes constant folding and simplifies this code.
858 Constant *Op = CE->getOperand(0);
Owen Anderson35b47072009-08-13 21:58:54 +0000859 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
860 false/*ZExt*/);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 return EmitConstantValueOnly(Op);
862 }
863
864
865 case Instruction::PtrToInt: {
866 // Support only foldable casts to/from pointers that can be eliminated by
867 // changing the pointer to the appropriately sized integer type.
868 Constant *Op = CE->getOperand(0);
869 const Type *Ty = CE->getType();
870
871 // We can emit the pointer value into this slot if the slot is an
872 // integer slot greater or equal to the size of the pointer.
Chris Lattner34e4cde2009-08-01 22:25:12 +0000873 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000875
876 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 EmitConstantValueOnly(Op);
Chris Lattner34e4cde2009-08-01 22:25:12 +0000878 APInt ptrMask =
879 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner89b36582008-08-17 07:19:36 +0000880
881 SmallString<40> S;
882 ptrMask.toStringUnsigned(S);
Daniel Dunbar768e97d2009-08-19 20:07:03 +0000883 O << ") & " << S.str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 break;
885 }
886 case Instruction::Add:
887 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000888 case Instruction::And:
889 case Instruction::Or:
890 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000891 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000893 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000894 switch (Opcode) {
895 case Instruction::Add:
896 O << " + ";
897 break;
898 case Instruction::Sub:
899 O << " - ";
900 break;
901 case Instruction::And:
902 O << " & ";
903 break;
904 case Instruction::Or:
905 O << " | ";
906 break;
907 case Instruction::Xor:
908 O << " ^ ";
909 break;
910 default:
911 break;
912 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000913 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000915 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 break;
917 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000918 llvm_unreachable("Unsupported operator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 }
920 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000921 llvm_unreachable("Unknown constant value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 }
923}
924
925/// printAsCString - Print the specified array as a C compatible string, only if
926/// the predicate isString is true.
927///
David Greene302008d2009-07-14 20:18:05 +0000928static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 unsigned LastElt) {
930 assert(CVA->isString() && "Array is not string compatible!");
931
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000932 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 for (unsigned i = 0; i != LastElt; ++i) {
934 unsigned char C =
935 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
936 printStringChar(O, C);
937 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000938 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000939}
940
941/// EmitString - Emit a zero-byte-terminated string constant.
942///
943void AsmPrinter::EmitString(const ConstantArray *CVA) const {
944 unsigned NumElts = CVA->getNumOperands();
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000945 if (MAI->getAscizDirective() && NumElts &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000947 O << MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 printAsCString(O, CVA, NumElts-1);
949 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000950 O << MAI->getAsciiDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951 printAsCString(O, CVA, NumElts);
952 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000953 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954}
955
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000956void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
957 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000958 if (CVA->isString()) {
959 EmitString(CVA);
960 } else { // Not a string. Print the values in successive locations
961 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000962 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000963 }
964}
965
966void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
967 const VectorType *PTy = CP->getType();
968
969 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
970 EmitGlobalConstant(CP->getOperand(I));
971}
972
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000973void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
974 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000975 // Print the fields in successive locations. Pad to align if needed!
976 const TargetData *TD = TM.getTargetData();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000977 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000978 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
979 uint64_t sizeSoFar = 0;
980 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
981 const Constant* field = CVS->getOperand(i);
982
983 // Check if padding is needed and insert one or more 0s.
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000984 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000985 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
986 - cvsLayout->getElementOffset(i)) - fieldSize;
987 sizeSoFar += fieldSize + padSize;
988
989 // Now print the actual field value.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000990 EmitGlobalConstant(field, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000991
992 // Insert padding - this may include padding to increase the size of the
993 // current field up to the ABI size (if the struct is not packed) as well
994 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000995 EmitZeros(padSize, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000996 }
997 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
998 "Layout of constant struct may be incorrect!");
999}
1000
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001001void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1002 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001003 // FP Constants are printed as integer constants to avoid losing
1004 // precision...
Owen Anderson35b47072009-08-13 21:58:54 +00001005 LLVMContext &Context = CFP->getContext();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001006 const TargetData *TD = TM.getTargetData();
Owen Anderson35b47072009-08-13 21:58:54 +00001007 if (CFP->getType() == Type::getDoubleTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001008 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1009 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001010 if (MAI->getData64bitsDirective(AddrSpace)) {
1011 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohman87581eb2009-08-12 18:32:22 +00001012 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001013 O.PadToColumn(MAI->getCommentColumn());
1014 O << MAI->getCommentString() << " double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001015 }
Evan Cheng11db8142009-03-24 00:17:40 +00001016 O << '\n';
1017 } else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001018 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001019 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001020 O.PadToColumn(MAI->getCommentColumn());
1021 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001022 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001023 }
Evan Cheng11db8142009-03-24 00:17:40 +00001024 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001025 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001026 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001027 O.PadToColumn(MAI->getCommentColumn());
1028 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001029 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001030 }
Evan Cheng11db8142009-03-24 00:17:40 +00001031 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001032 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001033 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001034 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001035 O.PadToColumn(MAI->getCommentColumn());
1036 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001037 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001038 }
Evan Cheng11db8142009-03-24 00:17:40 +00001039 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001040 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001041 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001042 O.PadToColumn(MAI->getCommentColumn());
1043 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001044 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001045 }
Evan Cheng11db8142009-03-24 00:17:40 +00001046 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001047 }
1048 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001049 } else if (CFP->getType() == Type::getFloatTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001050 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001051 O << MAI->getData32bitsDirective(AddrSpace)
Evan Cheng11db8142009-03-24 00:17:40 +00001052 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman87581eb2009-08-12 18:32:22 +00001053 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001054 O.PadToColumn(MAI->getCommentColumn());
1055 O << MAI->getCommentString() << " float " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001056 }
Evan Cheng11db8142009-03-24 00:17:40 +00001057 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001058 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001059 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001060 // all long double variants are printed as hex
1061 // api needed to prevent premature destruction
1062 APInt api = CFP->getValueAPF().bitcastToAPInt();
1063 const uint64_t *p = api.getRawData();
1064 // Convert to double so we can print the approximate val as a comment.
1065 APFloat DoubleVal = CFP->getValueAPF();
1066 bool ignored;
1067 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1068 &ignored);
1069 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001070 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001071 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001072 O.PadToColumn(MAI->getCommentColumn());
1073 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001074 << " most significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001075 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001076 }
Evan Cheng11db8142009-03-24 00:17:40 +00001077 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001078 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001079 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001080 O.PadToColumn(MAI->getCommentColumn());
1081 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001082 }
Evan Cheng11db8142009-03-24 00:17:40 +00001083 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001084 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001085 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001086 O.PadToColumn(MAI->getCommentColumn());
1087 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001088 }
Evan Cheng11db8142009-03-24 00:17:40 +00001089 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001090 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001091 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001092 O.PadToColumn(MAI->getCommentColumn());
1093 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001094 }
Evan Cheng11db8142009-03-24 00:17:40 +00001095 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001096 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001097 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001098 O.PadToColumn(MAI->getCommentColumn());
1099 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001100 << " least significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001101 }
Evan Cheng11db8142009-03-24 00:17:40 +00001102 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001103 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001104 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001105 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001106 O.PadToColumn(MAI->getCommentColumn());
1107 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001108 << " least significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001109 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001110 }
Evan Cheng11db8142009-03-24 00:17:40 +00001111 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001112 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001113 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001114 O.PadToColumn(MAI->getCommentColumn());
1115 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001116 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001117 }
Evan Cheng11db8142009-03-24 00:17:40 +00001118 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001119 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001120 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001121 O.PadToColumn(MAI->getCommentColumn());
1122 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001123 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001124 }
Evan Cheng11db8142009-03-24 00:17:40 +00001125 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001126 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001127 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001128 O.PadToColumn(MAI->getCommentColumn());
1129 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001130 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001131 }
Evan Cheng11db8142009-03-24 00:17:40 +00001132 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001133 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001134 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001135 O.PadToColumn(MAI->getCommentColumn());
1136 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001137 << " most significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001138 }
Evan Cheng11db8142009-03-24 00:17:40 +00001139 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001140 }
Owen Anderson35b47072009-08-13 21:58:54 +00001141 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1142 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001143 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001144 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001145 // all long double variants are printed as hex
1146 // api needed to prevent premature destruction
1147 APInt api = CFP->getValueAPF().bitcastToAPInt();
1148 const uint64_t *p = api.getRawData();
1149 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001150 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001151 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001152 O.PadToColumn(MAI->getCommentColumn());
1153 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001154 << " most significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001155 }
Evan Cheng11db8142009-03-24 00:17:40 +00001156 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001157 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001158 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001159 O.PadToColumn(MAI->getCommentColumn());
1160 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001161 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001162 }
Evan Cheng11db8142009-03-24 00:17:40 +00001163 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001164 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001165 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001166 O.PadToColumn(MAI->getCommentColumn());
1167 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001168 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001169 }
Evan Cheng11db8142009-03-24 00:17:40 +00001170 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001171 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001172 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001173 O.PadToColumn(MAI->getCommentColumn());
1174 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001175 << " least significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001176 }
Evan Cheng11db8142009-03-24 00:17:40 +00001177 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001178 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001179 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001180 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001181 O.PadToColumn(MAI->getCommentColumn());
1182 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001183 << " least significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001184 }
Evan Cheng11db8142009-03-24 00:17:40 +00001185 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001186 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001187 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001188 O.PadToColumn(MAI->getCommentColumn());
1189 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001190 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001191 }
Evan Cheng11db8142009-03-24 00:17:40 +00001192 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001193 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001194 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001195 O.PadToColumn(MAI->getCommentColumn());
1196 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001197 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001198 }
Evan Cheng11db8142009-03-24 00:17:40 +00001199 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001200 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001201 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001202 O.PadToColumn(MAI->getCommentColumn());
1203 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001204 << " most significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001205 }
Evan Cheng11db8142009-03-24 00:17:40 +00001206 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001207 }
1208 return;
Edwin Törökbd448e32009-07-14 16:55:14 +00001209 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001210}
1211
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001212void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1213 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001214 const TargetData *TD = TM.getTargetData();
1215 unsigned BitWidth = CI->getBitWidth();
1216 assert(isPowerOf2_32(BitWidth) &&
1217 "Non-power-of-2-sized integers not handled!");
1218
1219 // We don't expect assemblers to support integer data directives
1220 // for more than 64 bits, so we emit the data in at most 64-bit
1221 // quantities at a time.
1222 const uint64_t *RawData = CI->getValue().getRawData();
1223 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1224 uint64_t Val;
1225 if (TD->isBigEndian())
1226 Val = RawData[e - i - 1];
1227 else
1228 Val = RawData[i];
1229
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001230 if (MAI->getData64bitsDirective(AddrSpace))
1231 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001232 else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001233 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001234 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001235 O.PadToColumn(MAI->getCommentColumn());
1236 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001237 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001238 }
Evan Cheng11db8142009-03-24 00:17:40 +00001239 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001240 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001241 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001242 O.PadToColumn(MAI->getCommentColumn());
1243 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001244 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001245 }
Evan Cheng11db8142009-03-24 00:17:40 +00001246 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001247 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001248 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001249 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001250 O.PadToColumn(MAI->getCommentColumn());
1251 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001252 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001253 }
Evan Cheng11db8142009-03-24 00:17:40 +00001254 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001255 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001256 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001257 O.PadToColumn(MAI->getCommentColumn());
1258 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001259 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001260 }
Evan Cheng11db8142009-03-24 00:17:40 +00001261 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001262 }
1263 }
1264}
1265
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001267void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 const TargetData *TD = TM.getTargetData();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001269 const Type *type = CV->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001270 unsigned Size = TD->getTypeAllocSize(type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271
1272 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001273 EmitZeros(Size, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 return;
1275 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +00001276 EmitGlobalConstantArray(CVA , AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277 return;
1278 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001279 EmitGlobalConstantStruct(CVS, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 return;
1281 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001282 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001283 return;
1284 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1285 // Small integers are handled below; large integers are handled here.
1286 if (Size > 4) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001287 EmitGlobalConstantLargeInt(CI, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 return;
1289 }
1290 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001291 EmitGlobalConstantVector(CP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 return;
1293 }
1294
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001295 printDataDirective(type, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 EmitConstantValueOnly(CV);
Evan Cheng11db8142009-03-24 00:17:40 +00001297 if (VerboseAsm) {
1298 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1299 SmallString<40> S;
1300 CI->getValue().toStringUnsigned(S, 16);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001301 O.PadToColumn(MAI->getCommentColumn());
1302 O << MAI->getCommentString() << " 0x" << S.str();
Evan Cheng11db8142009-03-24 00:17:40 +00001303 }
Scott Michele067c3c2008-06-03 15:39:51 +00001304 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001305 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306}
1307
Chris Lattner89b36582008-08-17 07:19:36 +00001308void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001309 // Target doesn't support this yet!
Edwin Törökbd448e32009-07-14 16:55:14 +00001310 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311}
1312
1313/// PrintSpecial - Print information related to the specified machine instr
1314/// that is independent of the operand, and may be independent of the instr
1315/// itself. This can be useful for portably encoding the comment character
1316/// or other bits of target-specific knowledge into the asmstrings. The
1317/// syntax used is ${:comment}. Targets can override this to add support
1318/// for their own strange codes.
Chris Lattner6af48032009-03-10 05:37:13 +00001319void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 if (!strcmp(Code, "private")) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001321 O << MAI->getPrivateGlobalPrefix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 } else if (!strcmp(Code, "comment")) {
Evan Cheng11db8142009-03-24 00:17:40 +00001323 if (VerboseAsm)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001324 O << MAI->getCommentString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 } else if (!strcmp(Code, "uid")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326 // Comparing the address of MI isn't sufficient, because machineinstrs may
1327 // be allocated to the same address across functions.
1328 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1329
Owen Andersonda3388b2009-06-24 22:28:12 +00001330 // If this is a new LastFn instruction, bump the counter.
1331 if (LastMI != MI || LastFn != ThisF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332 ++Counter;
1333 LastMI = MI;
Owen Andersonda3388b2009-06-24 22:28:12 +00001334 LastFn = ThisF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 }
1336 O << Counter;
1337 } else {
Edwin Törökced9ff82009-07-11 13:10:19 +00001338 std::string msg;
1339 raw_string_ostream Msg(msg);
1340 Msg << "Unknown special formatter '" << Code
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 << "' for machine instr: " << *MI;
Edwin Törökced9ff82009-07-11 13:10:19 +00001342 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 }
1344}
1345
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001346/// processDebugLoc - Processes the debug information of each machine
1347/// instruction's DebugLoc.
1348void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001349 if (!MAI || !DW)
Chris Lattner693cfcc2009-08-05 04:09:18 +00001350 return;
1351
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001352 if (MAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001353 if (!DL.isUnknown()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001354 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1355
Chris Lattner32d4cc72009-09-09 23:14:36 +00001356 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001357 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1358 DICompileUnit(CurDLT.CompileUnit)));
Chris Lattner32d4cc72009-09-09 23:14:36 +00001359 O << '\n';
1360 }
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001361
1362 PrevDLT = CurDLT;
1363 }
1364 }
1365}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366
1367/// printInlineAsm - This method formats and prints the specified machine
1368/// instruction that is an inline asm.
1369void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1370 unsigned NumOperands = MI->getNumOperands();
1371
1372 // Count the number of register definitions.
1373 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001374 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375 ++NumDefs)
1376 assert(NumDefs != NumOperands-1 && "No asm string?");
1377
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001378 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379
1380 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1381 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1382
Dale Johannesene99fc902008-01-29 02:21:21 +00001383 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1384 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 if (AsmStr[0] == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001386 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1387 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 return;
1389 }
1390
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001391 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392
1393 // The variant of the current asmprinter.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001394 int AsmPrinterVariant = MAI->getAssemblerDialect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395
1396 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1397 const char *LastEmitted = AsmStr; // One past the last character emitted.
1398
1399 while (*LastEmitted) {
1400 switch (*LastEmitted) {
1401 default: {
1402 // Not a special case, emit the string section literally.
1403 const char *LiteralEnd = LastEmitted+1;
1404 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1405 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1406 ++LiteralEnd;
1407 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1408 O.write(LastEmitted, LiteralEnd-LastEmitted);
1409 LastEmitted = LiteralEnd;
1410 break;
1411 }
1412 case '\n':
1413 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001414 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 break;
1416 case '$': {
1417 ++LastEmitted; // Consume '$' character.
1418 bool Done = true;
1419
1420 // Handle escapes.
1421 switch (*LastEmitted) {
1422 default: Done = false; break;
1423 case '$': // $$ -> $
1424 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1425 O << '$';
1426 ++LastEmitted; // Consume second '$' character.
1427 break;
1428 case '(': // $( -> same as GCC's { character.
1429 ++LastEmitted; // Consume '(' character.
1430 if (CurVariant != -1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001431 llvm_report_error("Nested variants found in inline asm string: '"
1432 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 }
1434 CurVariant = 0; // We're in the first variant now.
1435 break;
1436 case '|':
1437 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001438 if (CurVariant == -1)
1439 O << '|'; // this is gcc's behavior for | outside a variant
1440 else
1441 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 break;
1443 case ')': // $) -> same as GCC's } char.
1444 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001445 if (CurVariant == -1)
1446 O << '}'; // this is gcc's behavior for } outside a variant
1447 else
1448 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001449 break;
1450 }
1451 if (Done) break;
1452
1453 bool HasCurlyBraces = false;
1454 if (*LastEmitted == '{') { // ${variable}
1455 ++LastEmitted; // Consume '{' character.
1456 HasCurlyBraces = true;
1457 }
1458
Chris Lattner6af48032009-03-10 05:37:13 +00001459 // If we have ${:foo}, then this is not a real operand reference, it is a
1460 // "magic" string reference, just like in .td files. Arrange to call
1461 // PrintSpecial.
1462 if (HasCurlyBraces && *LastEmitted == ':') {
1463 ++LastEmitted;
1464 const char *StrStart = LastEmitted;
1465 const char *StrEnd = strchr(StrStart, '}');
1466 if (StrEnd == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001467 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1468 + std::string(AsmStr) + "'");
Chris Lattner6af48032009-03-10 05:37:13 +00001469 }
1470
1471 std::string Val(StrStart, StrEnd);
1472 PrintSpecial(MI, Val.c_str());
1473 LastEmitted = StrEnd+1;
1474 break;
1475 }
1476
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477 const char *IDStart = LastEmitted;
1478 char *IDEnd;
1479 errno = 0;
1480 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1481 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001482 llvm_report_error("Bad $ operand number in inline asm string: '"
1483 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001484 }
1485 LastEmitted = IDEnd;
1486
1487 char Modifier[2] = { 0, 0 };
1488
1489 if (HasCurlyBraces) {
1490 // If we have curly braces, check for a modifier character. This
1491 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1492 if (*LastEmitted == ':') {
1493 ++LastEmitted; // Consume ':' character.
1494 if (*LastEmitted == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001495 llvm_report_error("Bad ${:} expression in inline asm string: '"
1496 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 }
1498
1499 Modifier[0] = *LastEmitted;
1500 ++LastEmitted; // Consume modifier character.
1501 }
1502
1503 if (*LastEmitted != '}') {
Edwin Törökced9ff82009-07-11 13:10:19 +00001504 llvm_report_error("Bad ${} expression in inline asm string: '"
1505 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506 }
1507 ++LastEmitted; // Consume '}' character.
1508 }
1509
1510 if ((unsigned)Val >= NumOperands-1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001511 llvm_report_error("Invalid $ operand number in inline asm string: '"
1512 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001513 }
1514
1515 // Okay, we finally have a value number. Ask the target to print this
1516 // operand!
1517 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1518 unsigned OpNo = 1;
1519
1520 bool Error = false;
1521
1522 // Scan to find the machine operand number for the operand.
1523 for (; Val; --Val) {
1524 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001525 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng92167402009-03-20 18:03:34 +00001526 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 }
1528
1529 if (OpNo >= MI->getNumOperands()) {
1530 Error = true;
1531 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001532 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001533 ++OpNo; // Skip over the ID number.
1534
Dale Johannesencfb19e62007-11-05 21:20:28 +00001535 if (Modifier[0]=='l') // labels are target independent
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001536 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1537 ->getNumber())->print(O, MAI);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001538 else {
1539 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001540 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001541 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1542 Modifier[0] ? Modifier : 0);
1543 } else {
1544 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1545 Modifier[0] ? Modifier : 0);
1546 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 }
1548 }
1549 if (Error) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001550 std::string msg;
1551 raw_string_ostream Msg(msg);
1552 Msg << "Invalid operand found in inline asm: '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553 << AsmStr << "'\n";
Edwin Törökced9ff82009-07-11 13:10:19 +00001554 MI->print(Msg);
1555 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 }
1557 }
1558 break;
1559 }
1560 }
1561 }
Chris Lattner32d4cc72009-09-09 23:14:36 +00001562 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563}
1564
Evan Cheng3c0eda52008-03-15 00:03:38 +00001565/// printImplicitDef - This method prints the specified machine instruction
1566/// that is an implicit def.
1567void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001568 if (!VerboseAsm) return;
1569 O.PadToColumn(MAI->getCommentColumn());
1570 O << MAI->getCommentString() << " implicit-def: "
Chris Lattner28f7e352009-09-13 19:48:37 +00001571 << TRI->getName(MI->getOperand(0).getReg());
Evan Cheng3c0eda52008-03-15 00:03:38 +00001572}
1573
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574/// printLabel - This method prints a local label used by debug and
1575/// exception handling tables.
1576void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001577 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001578}
1579
Evan Chenga53c40a2008-02-01 09:10:45 +00001580void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001581 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Chenga53c40a2008-02-01 09:10:45 +00001582}
1583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1585/// instruction, using the specified assembler variant. Targets should
1586/// overried this to format as appropriate.
1587bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1588 unsigned AsmVariant, const char *ExtraCode) {
1589 // Target doesn't support this yet!
1590 return true;
1591}
1592
1593bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1594 unsigned AsmVariant,
1595 const char *ExtraCode) {
1596 // Target doesn't support this yet!
1597 return true;
1598}
1599
Chris Lattner08c97082009-09-12 23:02:08 +00001600MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1601 SmallString<60> Name;
1602 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1603 << getFunctionNumber() << '_' << MBBID;
1604
1605 return OutContext.GetOrCreateSymbol(Name.str());
1606}
1607
1608
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001609/// EmitBasicBlockStart - This method prints the label for the specified
1610/// MachineBasicBlock, an alignment (if present) and a comment describing
1611/// it if appropriate.
Chris Lattnerda5fb6d2009-09-14 03:15:54 +00001612void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001613 if (unsigned Align = MBB->getAlignment())
1614 EmitAlignment(Log2_32(Align));
Evan Cheng45c1edb2008-02-28 00:43:03 +00001615
Chris Lattner08c97082009-09-12 23:02:08 +00001616 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerda5fb6d2009-09-14 03:15:54 +00001617 O << ':';
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001618
1619 if (VerboseAsm) {
Dan Gohman495af972009-08-12 18:47:05 +00001620 if (const BasicBlock *BB = MBB->getBasicBlock())
1621 if (BB->hasName()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001622 O.PadToColumn(MAI->getCommentColumn());
1623 O << MAI->getCommentString() << ' ';
Dan Gohman5ee8d032009-08-12 20:56:56 +00001624 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohman495af972009-08-12 18:47:05 +00001625 }
David Greenee52fd872009-08-10 16:38:07 +00001626
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001627 EmitComments(*MBB);
David Greenee52fd872009-08-10 16:38:07 +00001628 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001629}
1630
Evan Cheng6fb06762007-11-09 01:32:10 +00001631/// printPICJumpTableSetLabel - This method prints a set label for the
1632/// specified MachineBasicBlock for a jumptable entry.
1633void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1634 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001635 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636 return;
1637
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001638 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001639 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001640 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001641 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001642 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001643}
1644
Evan Cheng6fb06762007-11-09 01:32:10 +00001645void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1646 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001647 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001648 return;
1649
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001650 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001651 << getFunctionNumber() << '_' << uid << '_' << uid2
1652 << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001653 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001654 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001655 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001656}
1657
1658/// printDataDirective - This method prints the asm directive for the
1659/// specified type.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001660void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661 const TargetData *TD = TM.getTargetData();
1662 switch (type->getTypeID()) {
Chris Lattner1ad1c1d2009-07-15 04:42:49 +00001663 case Type::FloatTyID: case Type::DoubleTyID:
1664 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1665 assert(0 && "Should have already output floating point constant.");
1666 default:
1667 assert(0 && "Can't handle printing this type of thing");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001668 case Type::IntegerTyID: {
1669 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1670 if (BitWidth <= 8)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001671 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001672 else if (BitWidth <= 16)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001673 O << MAI->getData16bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001674 else if (BitWidth <= 32)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001675 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676 else if (BitWidth <= 64) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001677 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001678 "Target cannot handle 64-bit constant exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001679 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman07a91ea2008-09-08 16:40:13 +00001680 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00001681 llvm_unreachable("Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682 }
1683 break;
1684 }
1685 case Type::PointerTyID:
1686 if (TD->getPointerSize() == 8) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001687 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001688 "Target cannot handle 64-bit pointer exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001689 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001690 } else if (TD->getPointerSize() == 2) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001691 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001692 } else if (TD->getPointerSize() == 1) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001693 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001695 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001696 }
1697 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001698 }
1699}
1700
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001701void AsmPrinter::printVisibility(const std::string& Name,
1702 unsigned Visibility) const {
1703 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001704 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001705 O << Directive << Name << '\n';
1706 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001707 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001708 O << Directive << Name << '\n';
1709 }
1710}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001711
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001712void AsmPrinter::printOffset(int64_t Offset) const {
1713 if (Offset > 0)
1714 O << '+' << Offset;
1715 else if (Offset < 0)
1716 O << Offset;
1717}
1718
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001719GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1720 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001721 return 0;
1722
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001723 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001724 if (GCPI != GCMetadataPrinters.end())
1725 return GCPI->second;
1726
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001727 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001728
1729 for (GCMetadataPrinterRegistry::iterator
1730 I = GCMetadataPrinterRegistry::begin(),
1731 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1732 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001733 GCMetadataPrinter *GMP = I->instantiate();
1734 GMP->S = S;
1735 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1736 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001737 }
1738
Chris Lattnerbf9d76d2009-08-23 07:19:13 +00001739 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001740 llvm_unreachable(0);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001741}
David Greene63486122009-07-13 20:25:48 +00001742
1743/// EmitComments - Pretty-print comments for instructions
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001744void AsmPrinter::EmitComments(const MachineInstr &MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001745 assert(VerboseAsm && !MI.getDebugLoc().isUnknown());
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001746
1747 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001748
Chris Lattner46148952009-08-17 15:48:08 +00001749 // Print source line info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001750 O.PadToColumn(MAI->getCommentColumn());
1751 O << MAI->getCommentString() << " SrcLine ";
Devang Patel15e723d2009-08-28 23:24:31 +00001752 if (DLT.CompileUnit) {
Devang Patel15e723d2009-08-28 23:24:31 +00001753 DICompileUnit CU(DLT.CompileUnit);
Devang Patelaaf012e2009-09-29 18:40:58 +00001754 O << CU.getFilename() << " ";
David Greene4a37a692009-07-16 22:24:20 +00001755 }
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001756 O << DLT.Line;
1757 if (DLT.Col != 0)
1758 O << ":" << DLT.Col;
David Greene63486122009-07-13 20:25:48 +00001759}
1760
David Greene066ed6a2009-08-18 19:22:55 +00001761/// PrintChildLoopComment - Print comments about child loops within
1762/// the loop for this basic block, with nesting.
1763///
1764static void PrintChildLoopComment(formatted_raw_ostream &O,
1765 const MachineLoop *loop,
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001766 const MCAsmInfo *MAI,
David Greene066ed6a2009-08-18 19:22:55 +00001767 int FunctionNumber) {
1768 // Add child loop information
1769 for(MachineLoop::iterator cl = loop->begin(),
1770 clend = loop->end();
1771 cl != clend;
1772 ++cl) {
1773 MachineBasicBlock *Header = (*cl)->getHeader();
1774 assert(Header && "No header for loop");
1775
1776 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001777 O.PadToColumn(MAI->getCommentColumn());
David Greene066ed6a2009-08-18 19:22:55 +00001778
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001779 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001780 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001781 << " Child Loop BB" << FunctionNumber << "_"
1782 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1783
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001784 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greene066ed6a2009-08-18 19:22:55 +00001785 }
1786}
1787
David Greenee52fd872009-08-10 16:38:07 +00001788/// EmitComments - Pretty-print comments for basic blocks
1789void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1790{
David Greene066ed6a2009-08-18 19:22:55 +00001791 if (VerboseAsm) {
David Greenee52fd872009-08-10 16:38:07 +00001792 // Add loop depth information
1793 const MachineLoop *loop = LI->getLoopFor(&MBB);
1794
1795 if (loop) {
1796 // Print a newline after bb# annotation.
1797 O << "\n";
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001798 O.PadToColumn(MAI->getCommentColumn());
1799 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greenee52fd872009-08-10 16:38:07 +00001800 << '\n';
1801
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001802 O.PadToColumn(MAI->getCommentColumn());
David Greenee52fd872009-08-10 16:38:07 +00001803
1804 MachineBasicBlock *Header = loop->getHeader();
1805 assert(Header && "No header for loop");
1806
1807 if (Header == &MBB) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001808 O << MAI->getCommentString() << " Loop Header";
1809 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greenee52fd872009-08-10 16:38:07 +00001810 }
1811 else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001812 O << MAI->getCommentString() << " Loop Header is BB"
David Greenee52fd872009-08-10 16:38:07 +00001813 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1814 }
1815
1816 if (loop->empty()) {
1817 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001818 O.PadToColumn(MAI->getCommentColumn());
1819 O << MAI->getCommentString() << " Inner Loop";
David Greenee52fd872009-08-10 16:38:07 +00001820 }
1821
1822 // Add parent loop information
1823 for (const MachineLoop *CurLoop = loop->getParentLoop();
1824 CurLoop;
1825 CurLoop = CurLoop->getParentLoop()) {
1826 MachineBasicBlock *Header = CurLoop->getHeader();
1827 assert(Header && "No header for loop");
1828
1829 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001830 O.PadToColumn(MAI->getCommentColumn());
1831 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001832 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001833 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greenee52fd872009-08-10 16:38:07 +00001834 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1835 }
1836 }
1837 }
1838}