blob: 484ce94ffcda5e898b076714a7bb8d81925879a3 [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
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000113 // Allow the target to emit any magic that it wants at the start of the file.
114 EmitStartOfAsmFile(M);
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
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000123 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
124 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000125 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
126 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000127 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 if (!M.getModuleInlineAsm().empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000130 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 << M.getModuleInlineAsm()
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000132 << '\n' << MAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 << " End of file scope inline assembly\n";
134
Chris Lattnerc73e8eb2009-09-24 05:44:53 +0000135 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
136 if (MMI)
137 MMI->AnalyzeModule(M);
138 DW = getAnalysisIfAvailable<DwarfWriter>();
139 if (DW)
140 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel1a454842009-06-19 21:54:26 +0000141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 return false;
143}
144
145bool AsmPrinter::doFinalization(Module &M) {
Chris Lattnerae982212009-07-21 18:38:57 +0000146 // Emit global variables.
147 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
148 I != E; ++I)
149 PrintGlobalVariable(I);
150
Chris Lattnere1225cc2009-06-24 18:54:37 +0000151 // Emit final debug information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000152 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattnere1225cc2009-06-24 18:54:37 +0000153 DW->EndModule();
154
Chris Lattnerdb191f02009-06-24 18:52:01 +0000155 // If the target wants to know about weak references, print them all.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000156 if (MAI->getWeakRefDirective()) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000157 // FIXME: This is not lazy, it would be nice to only print weak references
158 // to stuff that is actually used. Note that doing so would require targets
159 // to notice uses in operands (due to constant exprs etc). This should
160 // happen with the MC stuff eventually.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161
Chris Lattnerdb191f02009-06-24 18:52:01 +0000162 // Print out module-level global variables here.
163 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
164 I != E; ++I) {
165 if (I->hasExternalWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000166 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000167 }
168
Chris Lattner0f98c332009-08-03 23:10:34 +0000169 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000170 if (I->hasExternalWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000171 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000172 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 }
174
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000175 if (MAI->getSetDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000176 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattnerdb191f02009-06-24 18:52:01 +0000178 I != E; ++I) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000179 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000180
181 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000182 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000183
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000184 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000185 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 else if (I->hasWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000187 O << MAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000188 else if (!I->hasLocalLinkage())
Edwin Törökbd448e32009-07-14 16:55:14 +0000189 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000190
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000191 printVisibility(Name, I->getVisibility());
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000192
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000193 O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 }
195 }
196
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000197 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000198 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
199 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
200 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000201 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000202
Dan Gohmana65530a2008-05-05 00:28:39 +0000203 // If we don't have any trampolines, then we don't require stack memory
204 // to be executable. Some targets have a directive to declare this.
Chris Lattnerdb191f02009-06-24 18:52:01 +0000205 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana65530a2008-05-05 00:28:39 +0000206 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000207 if (MAI->getNonexecutableStackDirective())
208 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000209
Chris Lattnerb6113072009-09-18 20:17:03 +0000210
211 // Allow the target to emit any magic that it wants at the end of the file,
212 // after everything else has gone out.
213 EmitEndOfAsmFile(M);
214
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 delete Mang; Mang = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +0000216 DW = 0; MMI = 0;
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000217
218 OutStreamer.Finish();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 return false;
220}
221
222void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
223 // What's my mangled name?
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000224 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000225 IncrementFunctionNumber();
David Greenee52fd872009-08-10 16:38:07 +0000226
Chris Lattner57c76b52009-09-16 00:35:39 +0000227 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +0000228 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229}
230
Evan Cheng68c18682009-03-13 07:51:59 +0000231namespace {
232 // SectionCPs - Keep track the alignment, constpool entries per Section.
233 struct SectionCPs {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000234 const MCSection *S;
Evan Cheng68c18682009-03-13 07:51:59 +0000235 unsigned Alignment;
236 SmallVector<unsigned, 4> CPEs;
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000237 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng68c18682009-03-13 07:51:59 +0000238 };
239}
240
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241/// EmitConstantPool - Print to the current output stream assembly
242/// representations of the constants in the constant pool MCP. This is
243/// used to print out constants which have been "spilled to memory" by
244/// the code generator.
245///
246void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
247 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
248 if (CP.empty()) return;
249
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000250 // Calculate sections for constant pool entries. We collect entries to go into
251 // the same section together to reduce amount of section switch statements.
Evan Cheng68c18682009-03-13 07:51:59 +0000252 SmallVector<SectionCPs, 4> CPSections;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner680c6f62009-07-22 00:28:43 +0000254 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng68c18682009-03-13 07:51:59 +0000255 unsigned Align = CPE.getAlignment();
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000256
257 SectionKind Kind;
258 switch (CPE.getRelocationInfo()) {
259 default: llvm_unreachable("Unknown section kind");
Chris Lattnera9453412009-08-01 23:57:16 +0000260 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000261 case 1:
Chris Lattnera9453412009-08-01 23:57:16 +0000262 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnered0c6762009-07-26 07:00:12 +0000263 break;
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000264 case 0:
Chris Lattnered0c6762009-07-26 07:00:12 +0000265 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnera9453412009-08-01 23:57:16 +0000266 case 4: Kind = SectionKind::getMergeableConst4(); break;
267 case 8: Kind = SectionKind::getMergeableConst8(); break;
268 case 16: Kind = SectionKind::getMergeableConst16();break;
269 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000270 }
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000271 }
272
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000273 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner680c6f62009-07-22 00:28:43 +0000274
Evan Cheng68c18682009-03-13 07:51:59 +0000275 // The number of sections are small, just do a linear search from the
276 // last section to the first.
277 bool Found = false;
278 unsigned SecIdx = CPSections.size();
279 while (SecIdx != 0) {
280 if (CPSections[--SecIdx].S == S) {
281 Found = true;
282 break;
283 }
284 }
285 if (!Found) {
286 SecIdx = CPSections.size();
287 CPSections.push_back(SectionCPs(S, Align));
288 }
289
290 if (Align > CPSections[SecIdx].Alignment)
291 CPSections[SecIdx].Alignment = Align;
292 CPSections[SecIdx].CPEs.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 }
294
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000295 // Now print stuff into the calculated sections.
Evan Cheng68c18682009-03-13 07:51:59 +0000296 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000297 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng68c18682009-03-13 07:51:59 +0000298 EmitAlignment(Log2_32(CPSections[i].Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299
Evan Cheng68c18682009-03-13 07:51:59 +0000300 unsigned Offset = 0;
301 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
302 unsigned CPI = CPSections[i].CPEs[j];
303 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000304
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 // Emit inter-object padding for alignment.
Evan Cheng68c18682009-03-13 07:51:59 +0000306 unsigned AlignMask = CPE.getAlignment() - 1;
307 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
308 EmitZeros(NewOffset - Offset);
309
310 const Type *Ty = CPE.getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000311 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng68c18682009-03-13 07:51:59 +0000312
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000313 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohman87581eb2009-08-12 18:32:22 +0000314 << CPI << ':';
Evan Cheng68c18682009-03-13 07:51:59 +0000315 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000316 O.PadToColumn(MAI->getCommentColumn());
317 O << MAI->getCommentString() << " constant ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000318 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000319 }
Evan Cheng68c18682009-03-13 07:51:59 +0000320 O << '\n';
321 if (CPE.isMachineConstantPoolEntry())
322 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
323 else
324 EmitGlobalConstant(CPE.Val.ConstVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 }
326 }
327}
328
329/// EmitJumpTableInfo - Print assembly representations of the jump tables used
330/// by the current function to the current output stream.
331///
332void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
333 MachineFunction &MF) {
334 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
335 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000336
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 // Pick the directive to use to print the jump table entries, and switch to
340 // the appropriate section.
341 TargetLowering *LoweringInfo = TM.getTargetLowering();
342
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000343 const Function *F = MF.getFunction();
Evan Chengccca6f72009-06-18 20:37:15 +0000344 bool JTInDiffSection = false;
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000345 if (F->isWeakForLinker() ||
346 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 // In PIC mode, we need to emit the jump table to the same section as the
348 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000349 // We should also do if the section name is NULL or function is declared in
350 // discardable section.
Chris Lattner73266f92009-08-19 05:49:37 +0000351 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
352 TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 } else {
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000354 // Otherwise, drop it in the readonly section.
355 const MCSection *ReadOnlySection =
Chris Lattnera9453412009-08-01 23:57:16 +0000356 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner73266f92009-08-19 05:49:37 +0000357 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengccca6f72009-06-18 20:37:15 +0000358 JTInDiffSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 }
360
361 EmitAlignment(Log2_32(MJTI->getAlignment()));
362
363 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
364 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
365
366 // If this jump table was deleted, ignore it.
367 if (JTBBs.empty()) continue;
368
369 // For PIC codegen, if possible we want to use the SetDirective to reduce
370 // the number of relocations the assembler will generate for the jump table.
371 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000372 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000373 if (MAI->getSetDirective() && IsPic)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000375 if (EmittedSets.insert(JTBBs[ii]))
376 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377
Chris Lattner149495f2009-09-13 19:02:16 +0000378 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 // before each jump table. The first label is never referenced, but tells
380 // the assembler and linker the extents of the jump table object. The
381 // second label is actually referenced by the code.
Chris Lattner149495f2009-09-13 19:02:16 +0000382 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
383 O << MAI->getLinkerPrivateGlobalPrefix()
384 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengccca6f72009-06-18 20:37:15 +0000385 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000387 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +0000388 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389
390 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000391 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 O << '\n';
393 }
394 }
395}
396
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000397void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
398 const MachineBasicBlock *MBB,
399 unsigned uid) const {
Chris Lattner3fb451e2009-08-11 20:29:57 +0000400 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000401
402 // Use JumpTableDirective otherwise honor the entry size from the jump table
403 // info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000404 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000405 bool HadJTEntryDirective = JTEntryDirective != NULL;
406 if (!HadJTEntryDirective) {
407 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000408 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000409 }
410
411 O << JTEntryDirective << ' ';
412
413 // If we have emitted set directives for the jump table entries, print
414 // them rather than the entries themselves. If we're emitting PIC, then
415 // emit the table entries as differences between two text section labels.
416 // If we're emitting non-PIC code, then emit the entries as direct
417 // references to the target basic blocks.
Chris Lattner3fb451e2009-08-11 20:29:57 +0000418 if (!isPIC) {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000419 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000420 } else if (MAI->getSetDirective()) {
421 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3fb451e2009-08-11 20:29:57 +0000422 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000423 } else {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000424 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner3fb451e2009-08-11 20:29:57 +0000425 // If the arch uses custom Jump Table directives, don't calc relative to
426 // JT
427 if (!HadJTEntryDirective)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000428 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner3fb451e2009-08-11 20:29:57 +0000429 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000430 }
431}
432
433
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
435/// special global used by LLVM. If so, emit it and return true, otherwise
436/// do nothing and return false.
437bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000438 if (GV->getName() == "llvm.used") {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000439 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000440 EmitLLVMUsedList(GV->getInitializer());
441 return true;
442 }
443
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000444 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner68433442009-04-13 05:44:34 +0000445 if (GV->getSection() == "llvm.metadata" ||
446 GV->hasAvailableExternallyLinkage())
447 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448
449 if (!GV->hasAppendingLinkage()) return false;
450
451 assert(GV->hasInitializer() && "Not a special LLVM global!");
452
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 const TargetData *TD = TM.getTargetData();
454 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner0c433e92009-03-09 05:52:15 +0000455 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000456 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000457 EmitAlignment(Align, 0);
458 EmitXXStructorList(GV->getInitializer());
459 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 }
461
Chris Lattner0c433e92009-03-09 05:52:15 +0000462 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000463 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000464 EmitAlignment(Align, 0);
465 EmitXXStructorList(GV->getInitializer());
466 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 }
468
469 return false;
470}
471
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000472/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000473/// global in the specified llvm.used list for which emitUsedDirectiveFor
474/// is true, as being used with this directive.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000476 const char *Directive = MAI->getUsedDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477
Dan Gohman9e1657f2009-06-14 23:30:43 +0000478 // Should be an array of 'i8*'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
480 if (InitList == 0) return;
481
482 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner3f621b32009-07-17 22:00:23 +0000483 const GlobalValue *GV =
484 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner84362ac2009-07-31 20:52:39 +0000485 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000486 O << Directive;
487 EmitConstantValueOnly(InitList->getOperand(i));
488 O << '\n';
489 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 }
491}
492
493/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
494/// function pointers, ignoring the init priority.
495void AsmPrinter::EmitXXStructorList(Constant *List) {
496 // Should be an array of '{ int, void ()* }' structs. The first value is the
497 // init priority, which we ignore.
498 if (!isa<ConstantArray>(List)) return;
499 ConstantArray *InitList = cast<ConstantArray>(List);
500 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
501 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
502 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
503
504 if (CS->getOperand(1)->isNullValue())
505 return; // Found a null terminator, exit printing.
506 // Emit the function pointer.
507 EmitGlobalConstant(CS->getOperand(1));
508 }
509}
510
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511
512//===----------------------------------------------------------------------===//
513/// LEB 128 number encoding.
514
515/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
516/// representing an unsigned leb128 value.
517void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000518 char Buffer[20];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000520 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 Value >>= 7;
522 if (Value) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000523 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 if (Value) O << ", ";
525 } while (Value);
526}
527
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
529/// representing a signed leb128 value.
530void AsmPrinter::PrintSLEB128(int Value) const {
531 int Sign = Value >> (8 * sizeof(Value) - 1);
532 bool IsMore;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000533 char Buffer[20];
aslc200b112008-08-16 12:57:46 +0000534
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000536 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 Value >>= 7;
538 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
539 if (IsMore) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000540 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 if (IsMore) O << ", ";
542 } while (IsMore);
543}
544
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545//===--------------------------------------------------------------------===//
546// Emission and print routines
547//
548
549/// PrintHex - Print a value as a hexidecimal value.
550///
551void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerda2047e2008-11-10 04:30:26 +0000552 char Buffer[20];
Chris Lattnerda2047e2008-11-10 04:30:26 +0000553 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554}
555
556/// EOL - Print a newline character to asm stream. If a comment is present
557/// then it will be printed first. Comments should not contain '\n'.
558void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000559 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000561
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000563 if (VerboseAsm && !Comment.empty()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000564 O.PadToColumn(MAI->getCommentColumn());
565 O << MAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000566 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 << Comment;
568 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000569 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570}
571
Owen Anderson367bfbb2008-07-01 21:16:27 +0000572void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000573 if (VerboseAsm && *Comment) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000574 O.PadToColumn(MAI->getCommentColumn());
575 O << MAI->getCommentString()
Owen Anderson367bfbb2008-07-01 21:16:27 +0000576 << ' '
577 << Comment;
578 }
579 O << '\n';
580}
581
Bill Wendling946b5212009-08-29 12:17:53 +0000582static const char *DecodeDWARFEncoding(unsigned Encoding) {
583 switch (Encoding) {
584 case dwarf::DW_EH_PE_absptr:
585 return "absptr";
586 case dwarf::DW_EH_PE_omit:
587 return "omit";
588 case dwarf::DW_EH_PE_pcrel:
589 return "pcrel";
Bill Wendlingbe23fd42009-09-09 21:26:19 +0000590 case dwarf::DW_EH_PE_udata4:
591 return "udata4";
592 case dwarf::DW_EH_PE_udata8:
593 return "udata8";
594 case dwarf::DW_EH_PE_sdata4:
595 return "sdata4";
596 case dwarf::DW_EH_PE_sdata8:
597 return "sdata8";
Bill Wendling946b5212009-08-29 12:17:53 +0000598 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
599 return "pcrel udata4";
600 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
601 return "pcrel sdata4";
602 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
603 return "pcrel udata8";
604 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
605 return "pcrel sdata8";
606 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
607 return "indirect pcrel udata4";
608 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
609 return "indirect pcrel sdata4";
610 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
611 return "indirect pcrel udata8";
612 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
613 return "indirect pcrel sdata8";
614 }
615
616 return 0;
617}
618
Bill Wendling946b5212009-08-29 12:17:53 +0000619void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
620 if (VerboseAsm && *Comment) {
621 O.PadToColumn(MAI->getCommentColumn());
622 O << MAI->getCommentString()
623 << ' '
624 << Comment;
625
626 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
627 O << " (" << EncStr << ')';
628 }
629 O << '\n';
630}
631
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
633/// unsigned leb128 value.
634void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000635 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 O << "\t.uleb128\t"
637 << Value;
638 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000639 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 PrintULEB128(Value);
641 }
642}
643
644/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
645/// signed leb128 value.
646void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000647 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 O << "\t.sleb128\t"
649 << Value;
650 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000651 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 PrintSLEB128(Value);
653 }
654}
655
656/// EmitInt8 - Emit a byte directive and value.
657///
658void AsmPrinter::EmitInt8(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000659 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 PrintHex(Value & 0xFF);
661}
662
663/// EmitInt16 - Emit a short directive and value.
664///
665void AsmPrinter::EmitInt16(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000666 O << MAI->getData16bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 PrintHex(Value & 0xFFFF);
668}
669
670/// EmitInt32 - Emit a long directive and value.
671///
672void AsmPrinter::EmitInt32(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000673 O << MAI->getData32bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 PrintHex(Value);
675}
676
677/// EmitInt64 - Emit a long long directive and value.
678///
679void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000680 if (MAI->getData64bitsDirective()) {
681 O << MAI->getData64bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 PrintHex(Value);
683 } else {
684 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000685 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 EmitInt32(unsigned(Value));
687 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000688 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 EmitInt32(unsigned(Value >> 32));
690 }
691 }
692}
693
694/// toOctal - Convert the low order bits of X into an octal digit.
695///
696static inline char toOctal(int X) {
697 return (X&7)+'0';
698}
699
700/// printStringChar - Print a char, escaped if necessary.
701///
David Greene302008d2009-07-14 20:18:05 +0000702static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 if (C == '"') {
704 O << "\\\"";
705 } else if (C == '\\') {
706 O << "\\\\";
Chris Lattner07ec1462009-01-22 23:38:45 +0000707 } else if (isprint((unsigned char)C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 O << C;
709 } else {
710 switch(C) {
711 case '\b': O << "\\b"; break;
712 case '\f': O << "\\f"; break;
713 case '\n': O << "\\n"; break;
714 case '\r': O << "\\r"; break;
715 case '\t': O << "\\t"; break;
716 default:
717 O << '\\';
718 O << toOctal(C >> 6);
719 O << toOctal(C >> 3);
720 O << toOctal(C >> 0);
721 break;
722 }
723 }
724}
725
726/// EmitString - Emit a string with quotes and a null terminator.
727/// Special characters are emitted properly.
728/// \literal (Eg. '\t') \endliteral
729void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling3f94b412009-04-09 23:51:31 +0000730 EmitString(String.c_str(), String.size());
731}
732
733void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000734 const char* AscizDirective = MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 if (AscizDirective)
736 O << AscizDirective;
737 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000738 O << MAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000739 O << '\"';
Bill Wendling3f94b412009-04-09 23:51:31 +0000740 for (unsigned i = 0; i < Size; ++i)
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000741 printStringChar(O, String[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000743 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 else
745 O << "\\0\"";
746}
747
748
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000749/// EmitFile - Emit a .file directive.
750void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
751 O << "\t.file\t" << Number << " \"";
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000752 for (unsigned i = 0, N = Name.size(); i < N; ++i)
753 printStringChar(O, Name[i]);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000754 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000755}
756
757
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758//===----------------------------------------------------------------------===//
759
760// EmitAlignment - Emit an alignment directive to the specified power of
761// two boundary. For example, if you pass in 3 here, you will get an 8
762// byte alignment. If a global value is specified, and if that global has
763// an explicit alignment requested, it will unconditionally override the
764// alignment request. However, if ForcedAlignBits is specified, this value
765// has final say: the ultimate alignment will be the max of ForcedAlignBits
766// and the alignment computed with NumBits and the global.
767//
768// The algorithm is:
769// Align = NumBits;
770// if (GV && GV->hasalignment) Align = GV->getalignment();
771// Align = std::max(Align, ForcedAlignBits);
772//
773void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000774 unsigned ForcedAlignBits,
775 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 if (GV && GV->getAlignment())
777 NumBits = Log2_32(GV->getAlignment());
778 NumBits = std::max(NumBits, ForcedAlignBits);
779
780 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner4891d142009-08-19 06:12:02 +0000781
782 unsigned FillValue = 0;
Chris Lattnerd69d8ad2009-08-18 06:15:16 +0000783 if (getCurrentSection()->getKind().isText())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000784 FillValue = MAI->getTextAlignFillValue();
Chris Lattner4891d142009-08-19 06:12:02 +0000785
786 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787}
David Greenecdc2de32009-08-05 21:00:52 +0000788
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789/// EmitZeros - Emit a block of zeros.
790///
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000791void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 if (NumZeros) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000793 if (MAI->getZeroDirective()) {
794 O << MAI->getZeroDirective() << NumZeros;
795 if (MAI->getZeroDirectiveSuffix())
796 O << MAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000797 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 } else {
799 for (; NumZeros; --NumZeros)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000800 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 }
802 }
803}
804
805// Print out the specified constant, without a storage class. Only the
806// constants valid in constant expressions can occur here.
807void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
808 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000809 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000811 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
813 // This is a constant address for a global variable or function. Use the
Chris Lattner36d03292009-09-15 23:11:32 +0000814 // name of the variable or function as the address value.
815 O << Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
817 const TargetData *TD = TM.getTargetData();
818 unsigned Opcode = CE->getOpcode();
819 switch (Opcode) {
Chris Lattner34e4cde2009-08-01 22:25:12 +0000820 case Instruction::Trunc:
821 case Instruction::ZExt:
822 case Instruction::SExt:
823 case Instruction::FPTrunc:
824 case Instruction::FPExt:
825 case Instruction::UIToFP:
826 case Instruction::SIToFP:
827 case Instruction::FPToUI:
828 case Instruction::FPToSI:
829 llvm_unreachable("FIXME: Don't support this constant cast expr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 case Instruction::GetElementPtr: {
831 // generate a symbolic expression for the byte address
832 const Constant *ptrVal = CE->getOperand(0);
833 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
834 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
835 idxVec.size())) {
Chris Lattner911129a2009-02-05 06:55:21 +0000836 // Truncate/sext the offset to the pointer size.
837 if (TD->getPointerSizeInBits() != 64) {
838 int SExtAmount = 64-TD->getPointerSizeInBits();
839 Offset = (Offset << SExtAmount) >> SExtAmount;
840 }
841
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000843 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 EmitConstantValueOnly(ptrVal);
845 if (Offset > 0)
846 O << ") + " << Offset;
847 else if (Offset < 0)
848 O << ") - " << -Offset;
849 } else {
850 EmitConstantValueOnly(ptrVal);
851 }
852 break;
853 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 case Instruction::BitCast:
855 return EmitConstantValueOnly(CE->getOperand(0));
856
857 case Instruction::IntToPtr: {
858 // Handle casts to pointers by changing them into casts to the appropriate
859 // integer type. This promotes constant folding and simplifies this code.
860 Constant *Op = CE->getOperand(0);
Owen Anderson35b47072009-08-13 21:58:54 +0000861 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
862 false/*ZExt*/);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 return EmitConstantValueOnly(Op);
864 }
865
866
867 case Instruction::PtrToInt: {
868 // Support only foldable casts to/from pointers that can be eliminated by
869 // changing the pointer to the appropriately sized integer type.
870 Constant *Op = CE->getOperand(0);
871 const Type *Ty = CE->getType();
872
873 // We can emit the pointer value into this slot if the slot is an
874 // integer slot greater or equal to the size of the pointer.
Chris Lattner34e4cde2009-08-01 22:25:12 +0000875 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000877
878 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 EmitConstantValueOnly(Op);
Chris Lattner34e4cde2009-08-01 22:25:12 +0000880 APInt ptrMask =
881 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner89b36582008-08-17 07:19:36 +0000882
883 SmallString<40> S;
884 ptrMask.toStringUnsigned(S);
Daniel Dunbar768e97d2009-08-19 20:07:03 +0000885 O << ") & " << S.str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 break;
887 }
888 case Instruction::Add:
889 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000890 case Instruction::And:
891 case Instruction::Or:
892 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000893 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000894 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000895 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000896 switch (Opcode) {
897 case Instruction::Add:
898 O << " + ";
899 break;
900 case Instruction::Sub:
901 O << " - ";
902 break;
903 case Instruction::And:
904 O << " & ";
905 break;
906 case Instruction::Or:
907 O << " | ";
908 break;
909 case Instruction::Xor:
910 O << " ^ ";
911 break;
912 default:
913 break;
914 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000915 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000917 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 break;
919 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000920 llvm_unreachable("Unsupported operator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 }
922 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000923 llvm_unreachable("Unknown constant value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 }
925}
926
927/// printAsCString - Print the specified array as a C compatible string, only if
928/// the predicate isString is true.
929///
David Greene302008d2009-07-14 20:18:05 +0000930static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931 unsigned LastElt) {
932 assert(CVA->isString() && "Array is not string compatible!");
933
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000934 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 for (unsigned i = 0; i != LastElt; ++i) {
936 unsigned char C =
937 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
938 printStringChar(O, C);
939 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000940 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941}
942
943/// EmitString - Emit a zero-byte-terminated string constant.
944///
945void AsmPrinter::EmitString(const ConstantArray *CVA) const {
946 unsigned NumElts = CVA->getNumOperands();
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000947 if (MAI->getAscizDirective() && NumElts &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000949 O << MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950 printAsCString(O, CVA, NumElts-1);
951 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000952 O << MAI->getAsciiDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953 printAsCString(O, CVA, NumElts);
954 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000955 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956}
957
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000958void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
959 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000960 if (CVA->isString()) {
961 EmitString(CVA);
962 } else { // Not a string. Print the values in successive locations
963 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000964 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000965 }
966}
967
968void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
969 const VectorType *PTy = CP->getType();
970
971 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
972 EmitGlobalConstant(CP->getOperand(I));
973}
974
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000975void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
976 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000977 // Print the fields in successive locations. Pad to align if needed!
978 const TargetData *TD = TM.getTargetData();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000979 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000980 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
981 uint64_t sizeSoFar = 0;
982 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
983 const Constant* field = CVS->getOperand(i);
984
985 // Check if padding is needed and insert one or more 0s.
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000986 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000987 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
988 - cvsLayout->getElementOffset(i)) - fieldSize;
989 sizeSoFar += fieldSize + padSize;
990
991 // Now print the actual field value.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000992 EmitGlobalConstant(field, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000993
994 // Insert padding - this may include padding to increase the size of the
995 // current field up to the ABI size (if the struct is not packed) as well
996 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000997 EmitZeros(padSize, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000998 }
999 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1000 "Layout of constant struct may be incorrect!");
1001}
1002
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001003void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1004 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001005 // FP Constants are printed as integer constants to avoid losing
1006 // precision...
Owen Anderson35b47072009-08-13 21:58:54 +00001007 LLVMContext &Context = CFP->getContext();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001008 const TargetData *TD = TM.getTargetData();
Owen Anderson35b47072009-08-13 21:58:54 +00001009 if (CFP->getType() == Type::getDoubleTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001010 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1011 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001012 if (MAI->getData64bitsDirective(AddrSpace)) {
1013 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohman87581eb2009-08-12 18:32:22 +00001014 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001015 O.PadToColumn(MAI->getCommentColumn());
1016 O << MAI->getCommentString() << " double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001017 }
Evan Cheng11db8142009-03-24 00:17:40 +00001018 O << '\n';
1019 } else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001020 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001021 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001022 O.PadToColumn(MAI->getCommentColumn());
1023 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001024 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001025 }
Evan Cheng11db8142009-03-24 00:17:40 +00001026 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001027 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001028 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001029 O.PadToColumn(MAI->getCommentColumn());
1030 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001031 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001032 }
Evan Cheng11db8142009-03-24 00:17:40 +00001033 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001034 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001035 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001036 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001037 O.PadToColumn(MAI->getCommentColumn());
1038 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001039 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001040 }
Evan Cheng11db8142009-03-24 00:17:40 +00001041 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001042 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001043 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001044 O.PadToColumn(MAI->getCommentColumn());
1045 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001046 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001047 }
Evan Cheng11db8142009-03-24 00:17:40 +00001048 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001049 }
1050 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001051 } else if (CFP->getType() == Type::getFloatTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001052 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001053 O << MAI->getData32bitsDirective(AddrSpace)
Evan Cheng11db8142009-03-24 00:17:40 +00001054 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman87581eb2009-08-12 18:32:22 +00001055 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001056 O.PadToColumn(MAI->getCommentColumn());
1057 O << MAI->getCommentString() << " float " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001058 }
Evan Cheng11db8142009-03-24 00:17:40 +00001059 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001060 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001061 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001062 // all long double variants are printed as hex
1063 // api needed to prevent premature destruction
1064 APInt api = CFP->getValueAPF().bitcastToAPInt();
1065 const uint64_t *p = api.getRawData();
1066 // Convert to double so we can print the approximate val as a comment.
1067 APFloat DoubleVal = CFP->getValueAPF();
1068 bool ignored;
1069 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1070 &ignored);
1071 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001072 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001073 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001074 O.PadToColumn(MAI->getCommentColumn());
1075 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001076 << " most significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001077 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001078 }
Evan Cheng11db8142009-03-24 00:17:40 +00001079 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001080 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001081 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001082 O.PadToColumn(MAI->getCommentColumn());
1083 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001084 }
Evan Cheng11db8142009-03-24 00:17:40 +00001085 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001086 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001087 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001088 O.PadToColumn(MAI->getCommentColumn());
1089 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001090 }
Evan Cheng11db8142009-03-24 00:17:40 +00001091 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001092 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001093 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001094 O.PadToColumn(MAI->getCommentColumn());
1095 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001096 }
Evan Cheng11db8142009-03-24 00:17:40 +00001097 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001098 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001099 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001100 O.PadToColumn(MAI->getCommentColumn());
1101 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001102 << " least significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001103 }
Evan Cheng11db8142009-03-24 00:17:40 +00001104 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001105 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001106 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001107 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001108 O.PadToColumn(MAI->getCommentColumn());
1109 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001110 << " least significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001111 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001112 }
Evan Cheng11db8142009-03-24 00:17:40 +00001113 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001114 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001115 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001116 O.PadToColumn(MAI->getCommentColumn());
1117 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001118 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001119 }
Evan Cheng11db8142009-03-24 00:17:40 +00001120 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001121 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001122 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001123 O.PadToColumn(MAI->getCommentColumn());
1124 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001125 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001126 }
Evan Cheng11db8142009-03-24 00:17:40 +00001127 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001128 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001129 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001130 O.PadToColumn(MAI->getCommentColumn());
1131 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001132 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001133 }
Evan Cheng11db8142009-03-24 00:17:40 +00001134 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001135 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001136 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001137 O.PadToColumn(MAI->getCommentColumn());
1138 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001139 << " most significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001140 }
Evan Cheng11db8142009-03-24 00:17:40 +00001141 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001142 }
Owen Anderson35b47072009-08-13 21:58:54 +00001143 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1144 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001145 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001146 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001147 // all long double variants are printed as hex
1148 // api needed to prevent premature destruction
1149 APInt api = CFP->getValueAPF().bitcastToAPInt();
1150 const uint64_t *p = api.getRawData();
1151 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001152 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001153 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001154 O.PadToColumn(MAI->getCommentColumn());
1155 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001156 << " most significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001157 }
Evan Cheng11db8142009-03-24 00:17:40 +00001158 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001159 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001160 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001161 O.PadToColumn(MAI->getCommentColumn());
1162 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001163 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001164 }
Evan Cheng11db8142009-03-24 00:17:40 +00001165 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001166 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001167 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001168 O.PadToColumn(MAI->getCommentColumn());
1169 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001170 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001171 }
Evan Cheng11db8142009-03-24 00:17:40 +00001172 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001173 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001174 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001175 O.PadToColumn(MAI->getCommentColumn());
1176 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001177 << " least significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001178 }
Evan Cheng11db8142009-03-24 00:17:40 +00001179 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001180 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001181 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001182 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001183 O.PadToColumn(MAI->getCommentColumn());
1184 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001185 << " least significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001186 }
Evan Cheng11db8142009-03-24 00:17:40 +00001187 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001188 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001189 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001190 O.PadToColumn(MAI->getCommentColumn());
1191 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001192 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001193 }
Evan Cheng11db8142009-03-24 00:17:40 +00001194 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001195 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001196 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001197 O.PadToColumn(MAI->getCommentColumn());
1198 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001199 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001200 }
Evan Cheng11db8142009-03-24 00:17:40 +00001201 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001202 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001203 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001204 O.PadToColumn(MAI->getCommentColumn());
1205 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001206 << " most significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001207 }
Evan Cheng11db8142009-03-24 00:17:40 +00001208 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001209 }
1210 return;
Edwin Törökbd448e32009-07-14 16:55:14 +00001211 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001212}
1213
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001214void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1215 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001216 const TargetData *TD = TM.getTargetData();
1217 unsigned BitWidth = CI->getBitWidth();
1218 assert(isPowerOf2_32(BitWidth) &&
1219 "Non-power-of-2-sized integers not handled!");
1220
1221 // We don't expect assemblers to support integer data directives
1222 // for more than 64 bits, so we emit the data in at most 64-bit
1223 // quantities at a time.
1224 const uint64_t *RawData = CI->getValue().getRawData();
1225 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1226 uint64_t Val;
1227 if (TD->isBigEndian())
1228 Val = RawData[e - i - 1];
1229 else
1230 Val = RawData[i];
1231
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001232 if (MAI->getData64bitsDirective(AddrSpace))
1233 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001234 else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001235 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001236 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001237 O.PadToColumn(MAI->getCommentColumn());
1238 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001239 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001240 }
Evan Cheng11db8142009-03-24 00:17:40 +00001241 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001242 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001243 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001244 O.PadToColumn(MAI->getCommentColumn());
1245 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001246 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001247 }
Evan Cheng11db8142009-03-24 00:17:40 +00001248 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001249 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001250 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001251 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001252 O.PadToColumn(MAI->getCommentColumn());
1253 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001254 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001255 }
Evan Cheng11db8142009-03-24 00:17:40 +00001256 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001257 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001258 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001259 O.PadToColumn(MAI->getCommentColumn());
1260 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001261 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001262 }
Evan Cheng11db8142009-03-24 00:17:40 +00001263 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001264 }
1265 }
1266}
1267
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001269void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 const TargetData *TD = TM.getTargetData();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001271 const Type *type = CV->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001272 unsigned Size = TD->getTypeAllocSize(type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273
1274 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001275 EmitZeros(Size, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 return;
1277 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +00001278 EmitGlobalConstantArray(CVA , AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 return;
1280 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001281 EmitGlobalConstantStruct(CVS, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 return;
1283 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001284 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001285 return;
1286 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1287 // Small integers are handled below; large integers are handled here.
1288 if (Size > 4) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001289 EmitGlobalConstantLargeInt(CI, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 return;
1291 }
1292 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001293 EmitGlobalConstantVector(CP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001294 return;
1295 }
1296
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001297 printDataDirective(type, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 EmitConstantValueOnly(CV);
Evan Cheng11db8142009-03-24 00:17:40 +00001299 if (VerboseAsm) {
1300 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1301 SmallString<40> S;
1302 CI->getValue().toStringUnsigned(S, 16);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001303 O.PadToColumn(MAI->getCommentColumn());
1304 O << MAI->getCommentString() << " 0x" << S.str();
Evan Cheng11db8142009-03-24 00:17:40 +00001305 }
Scott Michele067c3c2008-06-03 15:39:51 +00001306 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001307 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308}
1309
Chris Lattner89b36582008-08-17 07:19:36 +00001310void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311 // Target doesn't support this yet!
Edwin Törökbd448e32009-07-14 16:55:14 +00001312 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313}
1314
1315/// PrintSpecial - Print information related to the specified machine instr
1316/// that is independent of the operand, and may be independent of the instr
1317/// itself. This can be useful for portably encoding the comment character
1318/// or other bits of target-specific knowledge into the asmstrings. The
1319/// syntax used is ${:comment}. Targets can override this to add support
1320/// for their own strange codes.
Chris Lattner6af48032009-03-10 05:37:13 +00001321void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 if (!strcmp(Code, "private")) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001323 O << MAI->getPrivateGlobalPrefix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 } else if (!strcmp(Code, "comment")) {
Evan Cheng11db8142009-03-24 00:17:40 +00001325 if (VerboseAsm)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001326 O << MAI->getCommentString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 } else if (!strcmp(Code, "uid")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 // Comparing the address of MI isn't sufficient, because machineinstrs may
1329 // be allocated to the same address across functions.
1330 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1331
Owen Andersonda3388b2009-06-24 22:28:12 +00001332 // If this is a new LastFn instruction, bump the counter.
1333 if (LastMI != MI || LastFn != ThisF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334 ++Counter;
1335 LastMI = MI;
Owen Andersonda3388b2009-06-24 22:28:12 +00001336 LastFn = ThisF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 }
1338 O << Counter;
1339 } else {
Edwin Törökced9ff82009-07-11 13:10:19 +00001340 std::string msg;
1341 raw_string_ostream Msg(msg);
1342 Msg << "Unknown special formatter '" << Code
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 << "' for machine instr: " << *MI;
Edwin Törökced9ff82009-07-11 13:10:19 +00001344 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001345 }
1346}
1347
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001348/// processDebugLoc - Processes the debug information of each machine
1349/// instruction's DebugLoc.
Devang Patel86049dc2009-09-30 23:12:50 +00001350void AsmPrinter::processDebugLoc(const MachineInstr *MI) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001351 if (!MAI || !DW)
Chris Lattner693cfcc2009-08-05 04:09:18 +00001352 return;
Devang Patel86049dc2009-09-30 23:12:50 +00001353 DebugLoc DL = MI->getDebugLoc();
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001354 if (MAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001355 if (!DL.isUnknown()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001356 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1357
Chris Lattner32d4cc72009-09-09 23:14:36 +00001358 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) {
Devang Patel4d23d842009-09-30 22:51:28 +00001359 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1360 CurDLT.CompileUnit));
Chris Lattner32d4cc72009-09-09 23:14:36 +00001361 O << '\n';
1362 }
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001363
1364 PrevDLT = CurDLT;
1365 }
1366 }
1367}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368
1369/// printInlineAsm - This method formats and prints the specified machine
1370/// instruction that is an inline asm.
1371void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1372 unsigned NumOperands = MI->getNumOperands();
1373
1374 // Count the number of register definitions.
1375 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001376 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 ++NumDefs)
1378 assert(NumDefs != NumOperands-1 && "No asm string?");
1379
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001380 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381
1382 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1383 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1384
Dale Johannesene99fc902008-01-29 02:21:21 +00001385 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1386 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387 if (AsmStr[0] == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001388 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1389 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 return;
1391 }
1392
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001393 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001394
1395 // The variant of the current asmprinter.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001396 int AsmPrinterVariant = MAI->getAssemblerDialect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397
1398 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1399 const char *LastEmitted = AsmStr; // One past the last character emitted.
1400
1401 while (*LastEmitted) {
1402 switch (*LastEmitted) {
1403 default: {
1404 // Not a special case, emit the string section literally.
1405 const char *LiteralEnd = LastEmitted+1;
1406 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1407 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1408 ++LiteralEnd;
1409 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1410 O.write(LastEmitted, LiteralEnd-LastEmitted);
1411 LastEmitted = LiteralEnd;
1412 break;
1413 }
1414 case '\n':
1415 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001416 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 break;
1418 case '$': {
1419 ++LastEmitted; // Consume '$' character.
1420 bool Done = true;
1421
1422 // Handle escapes.
1423 switch (*LastEmitted) {
1424 default: Done = false; break;
1425 case '$': // $$ -> $
1426 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1427 O << '$';
1428 ++LastEmitted; // Consume second '$' character.
1429 break;
1430 case '(': // $( -> same as GCC's { character.
1431 ++LastEmitted; // Consume '(' character.
1432 if (CurVariant != -1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001433 llvm_report_error("Nested variants found in inline asm string: '"
1434 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435 }
1436 CurVariant = 0; // We're in the first variant now.
1437 break;
1438 case '|':
1439 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001440 if (CurVariant == -1)
1441 O << '|'; // this is gcc's behavior for | outside a variant
1442 else
1443 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 break;
1445 case ')': // $) -> same as GCC's } char.
1446 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001447 if (CurVariant == -1)
1448 O << '}'; // this is gcc's behavior for } outside a variant
1449 else
1450 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 break;
1452 }
1453 if (Done) break;
1454
1455 bool HasCurlyBraces = false;
1456 if (*LastEmitted == '{') { // ${variable}
1457 ++LastEmitted; // Consume '{' character.
1458 HasCurlyBraces = true;
1459 }
1460
Chris Lattner6af48032009-03-10 05:37:13 +00001461 // If we have ${:foo}, then this is not a real operand reference, it is a
1462 // "magic" string reference, just like in .td files. Arrange to call
1463 // PrintSpecial.
1464 if (HasCurlyBraces && *LastEmitted == ':') {
1465 ++LastEmitted;
1466 const char *StrStart = LastEmitted;
1467 const char *StrEnd = strchr(StrStart, '}');
1468 if (StrEnd == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001469 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1470 + std::string(AsmStr) + "'");
Chris Lattner6af48032009-03-10 05:37:13 +00001471 }
1472
1473 std::string Val(StrStart, StrEnd);
1474 PrintSpecial(MI, Val.c_str());
1475 LastEmitted = StrEnd+1;
1476 break;
1477 }
1478
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479 const char *IDStart = LastEmitted;
1480 char *IDEnd;
1481 errno = 0;
1482 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1483 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001484 llvm_report_error("Bad $ operand number in inline asm string: '"
1485 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001486 }
1487 LastEmitted = IDEnd;
1488
1489 char Modifier[2] = { 0, 0 };
1490
1491 if (HasCurlyBraces) {
1492 // If we have curly braces, check for a modifier character. This
1493 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1494 if (*LastEmitted == ':') {
1495 ++LastEmitted; // Consume ':' character.
1496 if (*LastEmitted == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001497 llvm_report_error("Bad ${:} expression in inline asm string: '"
1498 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499 }
1500
1501 Modifier[0] = *LastEmitted;
1502 ++LastEmitted; // Consume modifier character.
1503 }
1504
1505 if (*LastEmitted != '}') {
Edwin Törökced9ff82009-07-11 13:10:19 +00001506 llvm_report_error("Bad ${} expression in inline asm string: '"
1507 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 }
1509 ++LastEmitted; // Consume '}' character.
1510 }
1511
1512 if ((unsigned)Val >= NumOperands-1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001513 llvm_report_error("Invalid $ operand number in inline asm string: '"
1514 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 }
1516
1517 // Okay, we finally have a value number. Ask the target to print this
1518 // operand!
1519 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1520 unsigned OpNo = 1;
1521
1522 bool Error = false;
1523
1524 // Scan to find the machine operand number for the operand.
1525 for (; Val; --Val) {
1526 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001527 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng92167402009-03-20 18:03:34 +00001528 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 }
1530
1531 if (OpNo >= MI->getNumOperands()) {
1532 Error = true;
1533 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001534 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001535 ++OpNo; // Skip over the ID number.
1536
Dale Johannesencfb19e62007-11-05 21:20:28 +00001537 if (Modifier[0]=='l') // labels are target independent
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001538 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1539 ->getNumber())->print(O, MAI);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001540 else {
1541 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001542 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001543 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1544 Modifier[0] ? Modifier : 0);
1545 } else {
1546 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1547 Modifier[0] ? Modifier : 0);
1548 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549 }
1550 }
1551 if (Error) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001552 std::string msg;
1553 raw_string_ostream Msg(msg);
1554 Msg << "Invalid operand found in inline asm: '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555 << AsmStr << "'\n";
Edwin Törökced9ff82009-07-11 13:10:19 +00001556 MI->print(Msg);
1557 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 }
1559 }
1560 break;
1561 }
1562 }
1563 }
Chris Lattner32d4cc72009-09-09 23:14:36 +00001564 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001565}
1566
Evan Cheng3c0eda52008-03-15 00:03:38 +00001567/// printImplicitDef - This method prints the specified machine instruction
1568/// that is an implicit def.
1569void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001570 if (!VerboseAsm) return;
1571 O.PadToColumn(MAI->getCommentColumn());
1572 O << MAI->getCommentString() << " implicit-def: "
Chris Lattner28f7e352009-09-13 19:48:37 +00001573 << TRI->getName(MI->getOperand(0).getReg());
Evan Cheng3c0eda52008-03-15 00:03:38 +00001574}
1575
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576/// printLabel - This method prints a local label used by debug and
1577/// exception handling tables.
1578void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001579 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001580}
1581
Evan Chenga53c40a2008-02-01 09:10:45 +00001582void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001583 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Chenga53c40a2008-02-01 09:10:45 +00001584}
1585
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001586/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1587/// instruction, using the specified assembler variant. Targets should
1588/// overried this to format as appropriate.
1589bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1590 unsigned AsmVariant, const char *ExtraCode) {
1591 // Target doesn't support this yet!
1592 return true;
1593}
1594
1595bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1596 unsigned AsmVariant,
1597 const char *ExtraCode) {
1598 // Target doesn't support this yet!
1599 return true;
1600}
1601
Chris Lattner08c97082009-09-12 23:02:08 +00001602MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1603 SmallString<60> Name;
1604 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1605 << getFunctionNumber() << '_' << MBBID;
1606
1607 return OutContext.GetOrCreateSymbol(Name.str());
1608}
1609
1610
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001611/// EmitBasicBlockStart - This method prints the label for the specified
1612/// MachineBasicBlock, an alignment (if present) and a comment describing
1613/// it if appropriate.
Chris Lattnerda5fb6d2009-09-14 03:15:54 +00001614void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001615 if (unsigned Align = MBB->getAlignment())
1616 EmitAlignment(Log2_32(Align));
Evan Cheng45c1edb2008-02-28 00:43:03 +00001617
Chris Lattner08c97082009-09-12 23:02:08 +00001618 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerda5fb6d2009-09-14 03:15:54 +00001619 O << ':';
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001620
1621 if (VerboseAsm) {
Dan Gohman495af972009-08-12 18:47:05 +00001622 if (const BasicBlock *BB = MBB->getBasicBlock())
1623 if (BB->hasName()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001624 O.PadToColumn(MAI->getCommentColumn());
1625 O << MAI->getCommentString() << ' ';
Dan Gohman5ee8d032009-08-12 20:56:56 +00001626 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohman495af972009-08-12 18:47:05 +00001627 }
David Greenee52fd872009-08-10 16:38:07 +00001628
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001629 EmitComments(*MBB);
David Greenee52fd872009-08-10 16:38:07 +00001630 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001631}
1632
Evan Cheng6fb06762007-11-09 01:32:10 +00001633/// printPICJumpTableSetLabel - This method prints a set label for the
1634/// specified MachineBasicBlock for a jumptable entry.
1635void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1636 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001637 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638 return;
1639
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001640 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001641 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001642 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001643 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001644 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001645}
1646
Evan Cheng6fb06762007-11-09 01:32:10 +00001647void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1648 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001649 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001650 return;
1651
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001652 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001653 << getFunctionNumber() << '_' << uid << '_' << uid2
1654 << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001655 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001656 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001657 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658}
1659
1660/// printDataDirective - This method prints the asm directive for the
1661/// specified type.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001662void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001663 const TargetData *TD = TM.getTargetData();
1664 switch (type->getTypeID()) {
Chris Lattner1ad1c1d2009-07-15 04:42:49 +00001665 case Type::FloatTyID: case Type::DoubleTyID:
1666 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1667 assert(0 && "Should have already output floating point constant.");
1668 default:
1669 assert(0 && "Can't handle printing this type of thing");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001670 case Type::IntegerTyID: {
1671 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1672 if (BitWidth <= 8)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001673 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001674 else if (BitWidth <= 16)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001675 O << MAI->getData16bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676 else if (BitWidth <= 32)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001677 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001678 else if (BitWidth <= 64) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001679 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001680 "Target cannot handle 64-bit constant exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001681 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman07a91ea2008-09-08 16:40:13 +00001682 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00001683 llvm_unreachable("Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001684 }
1685 break;
1686 }
1687 case Type::PointerTyID:
1688 if (TD->getPointerSize() == 8) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001689 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 "Target cannot handle 64-bit pointer exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001691 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001692 } else if (TD->getPointerSize() == 2) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001693 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001694 } else if (TD->getPointerSize() == 1) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001695 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001696 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001697 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001698 }
1699 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001700 }
1701}
1702
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001703void AsmPrinter::printVisibility(const std::string& Name,
1704 unsigned Visibility) const {
1705 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001706 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001707 O << Directive << Name << '\n';
1708 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001709 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001710 O << Directive << Name << '\n';
1711 }
1712}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001713
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001714void AsmPrinter::printOffset(int64_t Offset) const {
1715 if (Offset > 0)
1716 O << '+' << Offset;
1717 else if (Offset < 0)
1718 O << Offset;
1719}
1720
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001721GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1722 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001723 return 0;
1724
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001725 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001726 if (GCPI != GCMetadataPrinters.end())
1727 return GCPI->second;
1728
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001729 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001730
1731 for (GCMetadataPrinterRegistry::iterator
1732 I = GCMetadataPrinterRegistry::begin(),
1733 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1734 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001735 GCMetadataPrinter *GMP = I->instantiate();
1736 GMP->S = S;
1737 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1738 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001739 }
1740
Chris Lattnerbf9d76d2009-08-23 07:19:13 +00001741 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001742 llvm_unreachable(0);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001743}
David Greene63486122009-07-13 20:25:48 +00001744
1745/// EmitComments - Pretty-print comments for instructions
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001746void AsmPrinter::EmitComments(const MachineInstr &MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001747 assert(VerboseAsm && !MI.getDebugLoc().isUnknown());
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001748
1749 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001750
Chris Lattner46148952009-08-17 15:48:08 +00001751 // Print source line info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001752 O.PadToColumn(MAI->getCommentColumn());
1753 O << MAI->getCommentString() << " SrcLine ";
Devang Patel15e723d2009-08-28 23:24:31 +00001754 if (DLT.CompileUnit) {
Devang Patel15e723d2009-08-28 23:24:31 +00001755 DICompileUnit CU(DLT.CompileUnit);
Devang Patelaaf012e2009-09-29 18:40:58 +00001756 O << CU.getFilename() << " ";
David Greene4a37a692009-07-16 22:24:20 +00001757 }
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001758 O << DLT.Line;
1759 if (DLT.Col != 0)
1760 O << ":" << DLT.Col;
David Greene63486122009-07-13 20:25:48 +00001761}
1762
David Greene066ed6a2009-08-18 19:22:55 +00001763/// PrintChildLoopComment - Print comments about child loops within
1764/// the loop for this basic block, with nesting.
1765///
1766static void PrintChildLoopComment(formatted_raw_ostream &O,
1767 const MachineLoop *loop,
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001768 const MCAsmInfo *MAI,
David Greene066ed6a2009-08-18 19:22:55 +00001769 int FunctionNumber) {
1770 // Add child loop information
1771 for(MachineLoop::iterator cl = loop->begin(),
1772 clend = loop->end();
1773 cl != clend;
1774 ++cl) {
1775 MachineBasicBlock *Header = (*cl)->getHeader();
1776 assert(Header && "No header for loop");
1777
1778 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001779 O.PadToColumn(MAI->getCommentColumn());
David Greene066ed6a2009-08-18 19:22:55 +00001780
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001781 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001782 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001783 << " Child Loop BB" << FunctionNumber << "_"
1784 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1785
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001786 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greene066ed6a2009-08-18 19:22:55 +00001787 }
1788}
1789
David Greenee52fd872009-08-10 16:38:07 +00001790/// EmitComments - Pretty-print comments for basic blocks
1791void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1792{
David Greene066ed6a2009-08-18 19:22:55 +00001793 if (VerboseAsm) {
David Greenee52fd872009-08-10 16:38:07 +00001794 // Add loop depth information
1795 const MachineLoop *loop = LI->getLoopFor(&MBB);
1796
1797 if (loop) {
1798 // Print a newline after bb# annotation.
1799 O << "\n";
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001800 O.PadToColumn(MAI->getCommentColumn());
1801 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greenee52fd872009-08-10 16:38:07 +00001802 << '\n';
1803
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001804 O.PadToColumn(MAI->getCommentColumn());
David Greenee52fd872009-08-10 16:38:07 +00001805
1806 MachineBasicBlock *Header = loop->getHeader();
1807 assert(Header && "No header for loop");
1808
1809 if (Header == &MBB) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001810 O << MAI->getCommentString() << " Loop Header";
1811 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greenee52fd872009-08-10 16:38:07 +00001812 }
1813 else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001814 O << MAI->getCommentString() << " Loop Header is BB"
David Greenee52fd872009-08-10 16:38:07 +00001815 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1816 }
1817
1818 if (loop->empty()) {
1819 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001820 O.PadToColumn(MAI->getCommentColumn());
1821 O << MAI->getCommentString() << " Inner Loop";
David Greenee52fd872009-08-10 16:38:07 +00001822 }
1823
1824 // Add parent loop information
1825 for (const MachineLoop *CurLoop = loop->getParentLoop();
1826 CurLoop;
1827 CurLoop = CurLoop->getParentLoop()) {
1828 MachineBasicBlock *Header = CurLoop->getHeader();
1829 assert(Header && "No header for loop");
1830
1831 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001832 O.PadToColumn(MAI->getCommentColumn());
1833 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001834 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001835 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greenee52fd872009-08-10 16:38:07 +00001836 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1837 }
1838 }
1839 }
1840}