blob: 82c176546e3739886b7d5bd1f43d43276127c198 [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner45111d12010-01-16 21:57:06 +000019#include "llvm/CodeGen/DwarfWriter.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000020#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000021#include "llvm/CodeGen/MachineConstantPool.h"
David Greene1924aab2009-11-13 21:34:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefe37ab32009-08-18 19:22:55 +000023#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000027#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000028#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000029#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000030#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCStreamer.h"
Chris Lattner7cb384d2009-09-12 23:02:08 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000033#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000035#include "llvm/Support/FormattedStream.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattner45111d12010-01-16 21:57:06 +000037#include "llvm/Target/Mangler.h"
Owen Anderson07000c62006-05-12 06:33:49 +000038#include "llvm/Target/TargetData.h"
David Greene1924aab2009-11-13 21:34:57 +000039#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000040#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000042#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000043#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000044#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000045#include "llvm/ADT/SmallString.h"
Chris Lattner66099132006-02-01 22:41:11 +000046#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000047using namespace llvm;
48
Evan Cheng42bf74b2009-03-25 01:47:28 +000049static cl::opt<cl::boolOrDefault>
50AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
51 cl::init(cl::BOU_UNSET));
52
Devang Patel19974732007-05-03 01:11:54 +000053char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000054AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000055 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000056 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000057 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000058
59 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000060 // FIXME: Pass instprinter to streamer.
61 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000062
Devang Patel6b61f582010-01-16 06:09:35 +000063 LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000064 DW = 0; MMI = 0;
Evan Cheng42bf74b2009-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}
Chris Lattnered138932005-12-13 06:32:10 +000071
Gordon Henriksenc317a602008-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 Lattner2b2954f2009-07-27 21:28:04 +000076
77 delete &OutStreamer;
78 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000079}
Chris Lattnered138932005-12-13 06:32:10 +000080
Chris Lattner38c39882009-08-03 19:12:26 +000081TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000082 return TM.getTargetLowering()->getObjFileLowering();
83}
84
Chris Lattnerdabf07c2009-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 Henriksence224772008-01-07 01:30:38 +000091void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000092 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +000093 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000094 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +000095 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +000096 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +000097}
98
Chris Lattnera80ba712004-08-16 23:15:22 +000099bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000100 // Initialize TargetLoweringObjectFile.
101 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
102 .Initialize(OutContext, TM);
103
Chris Lattner33adcfb2009-08-22 21:43:10 +0000104 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
105 MAI->getLinkerPrivateGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +0000106
Bob Wilson812209a2009-09-30 22:06:26 +0000107 // Allow the target to emit any magic that it wants at the start of the file.
108 EmitStartOfAsmFile(M);
Rafael Espindola952b8392008-12-03 11:01:37 +0000109
Chris Lattner33adcfb2009-08-22 21:43:10 +0000110 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000111 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilsonbc9506f2009-09-30 21:26:13 +0000112 debug info. If we don't, this at least helps the user find where
Rafael Espindola952b8392008-12-03 11:01:37 +0000113 a function came from. */
114 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
115 }
116
Bob Wilson812209a2009-09-30 22:06:26 +0000117 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
118 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000119 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
120 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000121 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000122
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000123 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000124 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000125 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000126 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000127 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000128
Chris Lattnerb55e0682009-09-24 05:44:53 +0000129 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
130 if (MMI)
131 MMI->AnalyzeModule(M);
132 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000133 if (DW)
Chris Lattnerb55e0682009-09-24 05:44:53 +0000134 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000135
Chris Lattnera80ba712004-08-16 23:15:22 +0000136 return false;
137}
138
139bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000140 // Emit global variables.
141 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
142 I != E; ++I)
143 PrintGlobalVariable(I);
144
Chris Lattner1f522fe2009-06-24 18:54:37 +0000145 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000146 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000147 DW->EndModule();
148
Chris Lattner0a7befa2009-06-24 18:52:01 +0000149 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000150 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000151 // FIXME: This is not lazy, it would be nice to only print weak references
152 // to stuff that is actually used. Note that doing so would require targets
153 // to notice uses in operands (due to constant exprs etc). This should
154 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000155
Chris Lattner0a7befa2009-06-24 18:52:01 +0000156 // Print out module-level global variables here.
157 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
158 I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000159 if (!I->hasExternalWeakLinkage()) continue;
160 O << MAI->getWeakRefDirective();
161 GetGlobalValueSymbol(I)->print(O, MAI);
162 O << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000163 }
164
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000165 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000166 if (!I->hasExternalWeakLinkage()) continue;
167 O << MAI->getWeakRefDirective();
168 GetGlobalValueSymbol(I)->print(O, MAI);
169 O << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000170 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000171 }
172
Chris Lattner33adcfb2009-08-22 21:43:10 +0000173 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000174 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000175 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000176 I != E; ++I) {
Chris Lattner5c40e692010-01-16 01:17:26 +0000177 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000178
179 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner5c40e692010-01-16 01:17:26 +0000180 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000181
Chris Lattner5c40e692010-01-16 01:17:26 +0000182 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective()) {
183 O << "\t.globl\t";
184 Name->print(O, MAI);
185 O << '\n';
186 } else if (I->hasWeakLinkage()) {
187 O << MAI->getWeakRefDirective();
188 Name->print(O, MAI);
189 O << '\n';
190 } else {
Chris Lattner10595492010-01-16 01:37:14 +0000191 assert(I->hasLocalLinkage() && "Invalid alias linkage");
Chris Lattner5c40e692010-01-16 01:17:26 +0000192 }
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000193
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000194 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000195
Chris Lattner5c40e692010-01-16 01:17:26 +0000196 O << MAI->getSetDirective() << ' ';
197 Name->print(O, MAI);
198 O << ", ";
199 Target->print(O, MAI);
200 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000201 }
202 }
203
Duncan Sands1465d612009-01-28 13:14:17 +0000204 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000205 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
206 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
207 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000208 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000209
Dan Gohmana779a982008-05-05 00:28:39 +0000210 // If we don't have any trampolines, then we don't require stack memory
211 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000212 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000213 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000214 if (MAI->getNonexecutableStackDirective())
215 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000216
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000217
218 // Allow the target to emit any magic that it wants at the end of the file,
219 // after everything else has gone out.
220 EmitEndOfAsmFile(M);
221
Chris Lattnera80ba712004-08-16 23:15:22 +0000222 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000223 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000224
225 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000226 return false;
227}
228
Chris Lattner25045bd2005-11-21 07:51:36 +0000229void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner412c3a52010-01-16 01:24:10 +0000230 // Get the function symbol.
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000231 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000232 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000233
Chris Lattner25d812b2009-09-16 00:35:39 +0000234 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000235 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000236}
237
Evan Cheng1606e8e2009-03-13 07:51:59 +0000238namespace {
239 // SectionCPs - Keep track the alignment, constpool entries per Section.
240 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000241 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000242 unsigned Alignment;
243 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000244 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000245 };
246}
247
Chris Lattner3b4fd322005-11-21 08:25:09 +0000248/// EmitConstantPool - Print to the current output stream assembly
249/// representations of the constants in the constant pool MCP. This is
250/// used to print out constants which have been "spilled to memory" by
251/// the code generator.
252///
253void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000254 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000255 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000256
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000257 // Calculate sections for constant pool entries. We collect entries to go into
258 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000259 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000260 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000261 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000262 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000263
264 SectionKind Kind;
265 switch (CPE.getRelocationInfo()) {
266 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000267 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000268 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000269 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000270 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000271 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000272 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000273 case 4: Kind = SectionKind::getMergeableConst4(); break;
274 case 8: Kind = SectionKind::getMergeableConst8(); break;
275 case 16: Kind = SectionKind::getMergeableConst16();break;
276 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000277 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000278 }
279
Chris Lattner83d77fa2009-08-01 23:46:12 +0000280 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000281
Evan Cheng1606e8e2009-03-13 07:51:59 +0000282 // The number of sections are small, just do a linear search from the
283 // last section to the first.
284 bool Found = false;
285 unsigned SecIdx = CPSections.size();
286 while (SecIdx != 0) {
287 if (CPSections[--SecIdx].S == S) {
288 Found = true;
289 break;
290 }
291 }
292 if (!Found) {
293 SecIdx = CPSections.size();
294 CPSections.push_back(SectionCPs(S, Align));
295 }
296
297 if (Align > CPSections[SecIdx].Alignment)
298 CPSections[SecIdx].Alignment = Align;
299 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000300 }
301
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000302 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000303 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000304 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000305 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000306
Evan Cheng1606e8e2009-03-13 07:51:59 +0000307 unsigned Offset = 0;
308 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
309 unsigned CPI = CPSections[i].CPEs[j];
310 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000311
Chris Lattner3029f922006-02-09 04:46:04 +0000312 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000313 unsigned AlignMask = CPE.getAlignment() - 1;
314 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
315 EmitZeros(NewOffset - Offset);
316
317 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000318 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000319
Chris Lattner33adcfb2009-08-22 21:43:10 +0000320 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000321 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000322 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000323 O.PadToColumn(MAI->getCommentColumn());
324 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000325 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000326 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000327 O << '\n';
328 if (CPE.isMachineConstantPoolEntry())
329 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
330 else
331 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000332 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000333 }
334}
335
Nate Begeman37efe672006-04-22 18:53:45 +0000336/// EmitJumpTableInfo - Print assembly representations of the jump tables used
337/// by the current function to the current output stream.
338///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000339void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
340 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000341 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
342 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000343
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000344 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000345
Nate Begeman2f1ae882006-07-27 01:13:04 +0000346 // Pick the directive to use to print the jump table entries, and switch to
347 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000348 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000349
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000350 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000351 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000352 if (F->isWeakForLinker() ||
353 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000354 // In PIC mode, we need to emit the jump table to the same section as the
355 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000356 // We should also do if the section name is NULL or function is declared in
357 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000358 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
359 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000360 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000361 // Otherwise, drop it in the readonly section.
362 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000363 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000364 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000365 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000366 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000367
368 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000369
Nate Begeman37efe672006-04-22 18:53:45 +0000370 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000371 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000372
373 // If this jump table was deleted, ignore it.
374 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000375
376 // For PIC codegen, if possible we want to use the SetDirective to reduce
377 // the number of relocations the assembler will generate for the jump table.
378 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000379 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000380 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000381 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000382 if (EmittedSets.insert(JTBBs[ii]))
383 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000384
Chris Lattner7c301912009-09-13 19:02:16 +0000385 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000386 // before each jump table. The first label is never referenced, but tells
387 // the assembler and linker the extents of the jump table object. The
388 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000389 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
390 O << MAI->getLinkerPrivateGlobalPrefix()
391 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000392 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000393
Chris Lattner33adcfb2009-08-22 21:43:10 +0000394 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000395 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000396
Nate Begeman37efe672006-04-22 18:53:45 +0000397 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000398 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000399 O << '\n';
400 }
401 }
402}
403
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000404void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
405 const MachineBasicBlock *MBB,
406 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000407 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000408
409 // Use JumpTableDirective otherwise honor the entry size from the jump table
410 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000411 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000412 bool HadJTEntryDirective = JTEntryDirective != NULL;
413 if (!HadJTEntryDirective) {
414 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000415 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000416 }
417
418 O << JTEntryDirective << ' ';
419
420 // If we have emitted set directives for the jump table entries, print
421 // them rather than the entries themselves. If we're emitting PIC, then
422 // emit the table entries as differences between two text section labels.
423 // If we're emitting non-PIC code, then emit the entries as direct
424 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000425 if (!isPIC) {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000426 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000427 } else if (MAI->getSetDirective()) {
428 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000429 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000430 } else {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000431 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000432 // If the arch uses custom Jump Table directives, don't calc relative to
433 // JT
434 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000435 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000436 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000437 }
438}
439
440
Chris Lattnered138932005-12-13 06:32:10 +0000441/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
442/// special global used by LLVM. If so, emit it and return true, otherwise
443/// do nothing and return false.
444bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000445 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000446 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000447 EmitLLVMUsedList(GV->getInitializer());
448 return true;
449 }
450
Chris Lattner401e10c2009-07-20 06:14:25 +0000451 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000452 if (GV->getSection() == "llvm.metadata" ||
453 GV->hasAvailableExternallyLinkage())
454 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000455
456 if (!GV->hasAppendingLinkage()) return false;
457
458 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000459
Evan Cheng916d07c2007-06-04 20:39:18 +0000460 const TargetData *TD = TM.getTargetData();
461 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000462 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000463 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000464 EmitAlignment(Align, 0);
465 EmitXXStructorList(GV->getInitializer());
466 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000467 }
468
Chris Lattnerf231c072009-03-09 05:52:15 +0000469 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000470 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000471 EmitAlignment(Align, 0);
472 EmitXXStructorList(GV->getInitializer());
473 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000474 }
475
476 return false;
477}
478
Chris Lattner33adcfb2009-08-22 21:43:10 +0000479/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000480/// global in the specified llvm.used list for which emitUsedDirectiveFor
481/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000482void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000483 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000484
Dan Gohmana119de82009-06-14 23:30:43 +0000485 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000486 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
487 if (InitList == 0) return;
488
489 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000490 const GlobalValue *GV =
491 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000492 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000493 O << Directive;
494 EmitConstantValueOnly(InitList->getOperand(i));
495 O << '\n';
496 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000497 }
498}
499
Chris Lattnered138932005-12-13 06:32:10 +0000500/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
501/// function pointers, ignoring the init priority.
502void AsmPrinter::EmitXXStructorList(Constant *List) {
503 // Should be an array of '{ int, void ()* }' structs. The first value is the
504 // init priority, which we ignore.
505 if (!isa<ConstantArray>(List)) return;
506 ConstantArray *InitList = cast<ConstantArray>(List);
507 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
508 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
509 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000510
511 if (CS->getOperand(1)->isNullValue())
512 return; // Found a null terminator, exit printing.
513 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000514 EmitGlobalConstant(CS->getOperand(1));
515 }
516}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000517
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000518
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000519//===----------------------------------------------------------------------===//
520/// LEB 128 number encoding.
521
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000522/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000523/// representing an unsigned leb128 value.
524void AsmPrinter::PrintULEB128(unsigned Value) const {
525 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000526 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000527 Value >>= 7;
528 if (Value) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000529 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000530 if (Value) O << ", ";
531 } while (Value);
532}
533
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000534/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000535/// representing a signed leb128 value.
536void AsmPrinter::PrintSLEB128(int Value) const {
537 int Sign = Value >> (8 * sizeof(Value) - 1);
538 bool IsMore;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000539
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000540 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000541 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000542 Value >>= 7;
543 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
544 if (IsMore) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000545 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000546 if (IsMore) O << ", ";
547 } while (IsMore);
548}
549
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000550//===--------------------------------------------------------------------===//
551// Emission and print routines
552//
553
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000554/// PrintHex - Print a value as a hexadecimal value.
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000555///
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000556void AsmPrinter::PrintHex(uint64_t Value) const {
557 O << "0x";
558 O.write_hex(Value);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000559}
560
561/// EOL - Print a newline character to asm stream. If a comment is present
562/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000563void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000564 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000565}
Owen Anderson25995092008-07-01 21:16:27 +0000566
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000567void AsmPrinter::EOL(const Twine &Comment) const {
568 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000569 O.PadToColumn(MAI->getCommentColumn());
570 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000571 << ' '
572 << Comment;
573 }
574 O << '\n';
575}
576
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000577static const char *DecodeDWARFEncoding(unsigned Encoding) {
578 switch (Encoding) {
579 case dwarf::DW_EH_PE_absptr:
580 return "absptr";
581 case dwarf::DW_EH_PE_omit:
582 return "omit";
583 case dwarf::DW_EH_PE_pcrel:
584 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000585 case dwarf::DW_EH_PE_udata4:
586 return "udata4";
587 case dwarf::DW_EH_PE_udata8:
588 return "udata8";
589 case dwarf::DW_EH_PE_sdata4:
590 return "sdata4";
591 case dwarf::DW_EH_PE_sdata8:
592 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000593 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
594 return "pcrel udata4";
595 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
596 return "pcrel sdata4";
597 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
598 return "pcrel udata8";
599 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
600 return "pcrel sdata8";
601 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
602 return "indirect pcrel udata4";
603 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
604 return "indirect pcrel sdata4";
605 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
606 return "indirect pcrel udata8";
607 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
608 return "indirect pcrel sdata8";
609 }
610
611 return 0;
612}
613
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000614void AsmPrinter::EOL(const Twine &Comment, unsigned Encoding) const {
615 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000616 O.PadToColumn(MAI->getCommentColumn());
617 O << MAI->getCommentString()
618 << ' '
619 << Comment;
620
621 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
622 O << " (" << EncStr << ')';
623 }
624 O << '\n';
625}
626
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000627/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
628/// unsigned leb128 value.
629void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000630 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000631 O << "\t.uleb128\t"
632 << Value;
633 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000634 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000635 PrintULEB128(Value);
636 }
637}
638
639/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
640/// signed leb128 value.
641void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000642 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000643 O << "\t.sleb128\t"
644 << Value;
645 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000646 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000647 PrintSLEB128(Value);
648 }
649}
650
651/// EmitInt8 - Emit a byte directive and value.
652///
653void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000654 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000655 PrintHex(Value & 0xFF);
656}
657
658/// EmitInt16 - Emit a short directive and value.
659///
660void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000661 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000662 PrintHex(Value & 0xFFFF);
663}
664
665/// EmitInt32 - Emit a long directive and value.
666///
667void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000668 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000669 PrintHex(Value);
670}
671
672/// EmitInt64 - Emit a long long directive and value.
673///
674void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000675 if (MAI->getData64bitsDirective()) {
676 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000677 PrintHex(Value);
678 } else {
679 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000680 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000681 EmitInt32(unsigned(Value));
682 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000683 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000684 EmitInt32(unsigned(Value >> 32));
685 }
686 }
687}
688
689/// toOctal - Convert the low order bits of X into an octal digit.
690///
691static inline char toOctal(int X) {
692 return (X&7)+'0';
693}
694
695/// printStringChar - Print a char, escaped if necessary.
696///
David Greene71847812009-07-14 20:18:05 +0000697static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000698 if (C == '"') {
699 O << "\\\"";
700 } else if (C == '\\') {
701 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000702 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000703 O << C;
704 } else {
705 switch(C) {
706 case '\b': O << "\\b"; break;
707 case '\f': O << "\\f"; break;
708 case '\n': O << "\\n"; break;
709 case '\r': O << "\\r"; break;
710 case '\t': O << "\\t"; break;
711 default:
712 O << '\\';
713 O << toOctal(C >> 6);
714 O << toOctal(C >> 3);
715 O << toOctal(C >> 0);
716 break;
717 }
718 }
719}
720
721/// EmitString - Emit a string with quotes and a null terminator.
722/// Special characters are emitted properly.
723/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000724void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000725 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000726}
727
728void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000729 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000730 if (AscizDirective)
731 O << AscizDirective;
732 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000733 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000734 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000735 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000736 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000737 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000738 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000739 else
740 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000741}
742
743
Dan Gohman189f80d2007-09-24 20:58:13 +0000744/// EmitFile - Emit a .file directive.
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000745void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
Dan Gohman189f80d2007-09-24 20:58:13 +0000746 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000747 for (unsigned i = 0, N = Name.size(); i < N; ++i)
748 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000749 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000750}
751
752
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000753//===----------------------------------------------------------------------===//
754
Chris Lattner3a420532007-05-31 18:57:45 +0000755// EmitAlignment - Emit an alignment directive to the specified power of
756// two boundary. For example, if you pass in 3 here, you will get an 8
757// byte alignment. If a global value is specified, and if that global has
758// an explicit alignment requested, it will unconditionally override the
759// alignment request. However, if ForcedAlignBits is specified, this value
760// has final say: the ultimate alignment will be the max of ForcedAlignBits
761// and the alignment computed with NumBits and the global.
762//
763// The algorithm is:
764// Align = NumBits;
765// if (GV && GV->hasalignment) Align = GV->getalignment();
766// Align = std::max(Align, ForcedAlignBits);
767//
768void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000769 unsigned ForcedAlignBits,
770 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000771 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000772 NumBits = Log2_32(GV->getAlignment());
773 NumBits = std::max(NumBits, ForcedAlignBits);
774
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000775 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000776
777 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000778 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000779 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000780
781 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000782}
David Greenea5bb59f2009-08-05 21:00:52 +0000783
Chris Lattner25045bd2005-11-21 07:51:36 +0000784/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000785///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000786void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000787 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000788 if (MAI->getZeroDirective()) {
789 O << MAI->getZeroDirective() << NumZeros;
790 if (MAI->getZeroDirectiveSuffix())
791 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000792 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000793 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000794 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000795 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000796 }
797 }
798}
799
Chris Lattnera80ba712004-08-16 23:15:22 +0000800// Print out the specified constant, without a storage class. Only the
801// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000802void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000803 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000804 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000805 return;
806 }
807
808 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000809 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000810 return;
811 }
812
813 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000814 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000815 // name of the variable or function as the address value.
Chris Lattner08ce3b42010-01-16 18:17:26 +0000816 GetGlobalValueSymbol(GV)->print(O, MAI);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000817 return;
818 }
819
820 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Dan Gohman6a55e622009-10-30 01:45:18 +0000821 GetBlockAddressSymbol(BA)->print(O, MAI);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000822 return;
823 }
824
825 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
826 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000827 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000828 O << '0';
829 return;
830 }
831
832 switch (CE->getOpcode()) {
833 case Instruction::ZExt:
834 case Instruction::SExt:
835 case Instruction::FPTrunc:
836 case Instruction::FPExt:
837 case Instruction::UIToFP:
838 case Instruction::SIToFP:
839 case Instruction::FPToUI:
840 case Instruction::FPToSI:
841 default:
842 llvm_unreachable("FIXME: Don't support this constant cast expr");
843 case Instruction::GetElementPtr: {
844 // generate a symbolic expression for the byte address
845 const TargetData *TD = TM.getTargetData();
846 const Constant *ptrVal = CE->getOperand(0);
847 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
848 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
849 idxVec.size());
850 if (Offset == 0)
851 return EmitConstantValueOnly(ptrVal);
852
853 // Truncate/sext the offset to the pointer size.
854 if (TD->getPointerSizeInBits() != 64) {
855 int SExtAmount = 64-TD->getPointerSizeInBits();
856 Offset = (Offset << SExtAmount) >> SExtAmount;
857 }
858
859 if (Offset)
860 O << '(';
861 EmitConstantValueOnly(ptrVal);
862 if (Offset > 0)
863 O << ") + " << Offset;
864 else
865 O << ") - " << -Offset;
866 return;
867 }
868 case Instruction::BitCast:
869 return EmitConstantValueOnly(CE->getOperand(0));
870
871 case Instruction::IntToPtr: {
872 // Handle casts to pointers by changing them into casts to the appropriate
873 // integer type. This promotes constant folding and simplifies this code.
874 const TargetData *TD = TM.getTargetData();
875 Constant *Op = CE->getOperand(0);
876 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
877 false/*ZExt*/);
878 return EmitConstantValueOnly(Op);
879 }
880
881 case Instruction::PtrToInt: {
882 // Support only foldable casts to/from pointers that can be eliminated by
883 // changing the pointer to the appropriately sized integer type.
884 Constant *Op = CE->getOperand(0);
885 const Type *Ty = CE->getType();
886 const TargetData *TD = TM.getTargetData();
887
888 // We can emit the pointer value into this slot if the slot is an
889 // integer slot greater or equal to the size of the pointer.
890 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
891 return EmitConstantValueOnly(Op);
892
893 O << "((";
894 EmitConstantValueOnly(Op);
895 APInt ptrMask =
896 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
897
898 SmallString<40> S;
899 ptrMask.toStringUnsigned(S);
900 O << ") & " << S.str() << ')';
901 return;
902 }
903
904 case Instruction::Trunc:
905 // We emit the value and depend on the assembler to truncate the generated
906 // expression properly. This is important for differences between
907 // blockaddress labels. Since the two labels are in the same function, it
908 // is reasonable to treat their delta as a 32-bit value.
909 return EmitConstantValueOnly(CE->getOperand(0));
910
911 case Instruction::Add:
912 case Instruction::Sub:
913 case Instruction::And:
914 case Instruction::Or:
915 case Instruction::Xor:
916 O << '(';
917 EmitConstantValueOnly(CE->getOperand(0));
918 O << ')';
919 switch (CE->getOpcode()) {
920 case Instruction::Add:
921 O << " + ";
922 break;
923 case Instruction::Sub:
924 O << " - ";
925 break;
926 case Instruction::And:
927 O << " & ";
928 break;
929 case Instruction::Or:
930 O << " | ";
931 break;
932 case Instruction::Xor:
933 O << " ^ ";
934 break;
935 default:
936 break;
937 }
938 O << '(';
939 EmitConstantValueOnly(CE->getOperand(1));
940 O << ')';
941 break;
Chris Lattnera80ba712004-08-16 23:15:22 +0000942 }
943}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000944
Chris Lattner2980cef2005-11-10 18:06:33 +0000945/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000946/// the predicate isString is true.
947///
David Greene71847812009-07-14 20:18:05 +0000948static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000949 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000950 assert(CVA->isString() && "Array is not string compatible!");
951
Dan Gohmand19a53b2008-06-30 22:03:41 +0000952 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000953 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000954 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000955 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000956 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000957 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000958 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000959}
960
Jeff Cohenc884db42006-05-02 01:16:28 +0000961/// EmitString - Emit a zero-byte-terminated string constant.
962///
963void AsmPrinter::EmitString(const ConstantArray *CVA) const {
964 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000965 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000966 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000967 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000968 printAsCString(O, CVA, NumElts-1);
969 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000970 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000971 printAsCString(O, CVA, NumElts);
972 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000973 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000974}
975
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000976void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
977 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000978 if (CVA->isString()) {
979 EmitString(CVA);
980 } else { // Not a string. Print the values in successive locations
981 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000982 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000983 }
984}
985
986void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
987 const VectorType *PTy = CP->getType();
988
989 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
990 EmitGlobalConstant(CP->getOperand(I));
991}
992
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000993void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
994 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000995 // Print the fields in successive locations. Pad to align if needed!
996 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +0000997 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000998 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
999 uint64_t sizeSoFar = 0;
1000 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1001 const Constant* field = CVS->getOperand(i);
1002
1003 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +00001004 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001005 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1006 - cvsLayout->getElementOffset(i)) - fieldSize;
1007 sizeSoFar += fieldSize + padSize;
1008
1009 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001010 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001011
1012 // Insert padding - this may include padding to increase the size of the
1013 // current field up to the ABI size (if the struct is not packed) as well
1014 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001015 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001016 }
1017 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1018 "Layout of constant struct may be incorrect!");
1019}
1020
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001021void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1022 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001023 // FP Constants are printed as integer constants to avoid losing
1024 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001025 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001026 const TargetData *TD = TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001027 if (CFP->getType()->isDoubleTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001028 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1029 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001030 if (MAI->getData64bitsDirective(AddrSpace)) {
1031 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001032 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001033 O.PadToColumn(MAI->getCommentColumn());
1034 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001035 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001036 O << '\n';
1037 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001038 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001039 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001040 O.PadToColumn(MAI->getCommentColumn());
1041 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001042 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001043 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001044 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001045 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001046 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001047 O.PadToColumn(MAI->getCommentColumn());
1048 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001049 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001050 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001051 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001052 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001053 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001054 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001055 O.PadToColumn(MAI->getCommentColumn());
1056 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001057 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001058 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001059 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001060 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001061 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001062 O.PadToColumn(MAI->getCommentColumn());
1063 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001064 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001065 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001066 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001067 }
1068 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001069 }
1070
1071 if (CFP->getType()->isFloatTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001072 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001073 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001074 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001075 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001076 O.PadToColumn(MAI->getCommentColumn());
1077 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001078 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001079 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001080 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001081 }
1082
1083 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001084 // all long double variants are printed as hex
1085 // api needed to prevent premature destruction
1086 APInt api = CFP->getValueAPF().bitcastToAPInt();
1087 const uint64_t *p = api.getRawData();
1088 // Convert to double so we can print the approximate val as a comment.
1089 APFloat DoubleVal = CFP->getValueAPF();
1090 bool ignored;
1091 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1092 &ignored);
1093 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001094 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001095 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001096 O.PadToColumn(MAI->getCommentColumn());
1097 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001098 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001099 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001100 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001101 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001102 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001103 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001104 O.PadToColumn(MAI->getCommentColumn());
1105 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001106 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001107 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001108 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001109 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001110 O.PadToColumn(MAI->getCommentColumn());
1111 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001112 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001113 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001114 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001115 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001116 O.PadToColumn(MAI->getCommentColumn());
1117 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001118 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001119 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001120 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001121 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001122 O.PadToColumn(MAI->getCommentColumn());
1123 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001124 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001125 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001126 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001127 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001128 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001129 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001130 O.PadToColumn(MAI->getCommentColumn());
1131 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001132 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001133 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001134 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001135 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001136 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001137 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001138 O.PadToColumn(MAI->getCommentColumn());
1139 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001140 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001141 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001142 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001143 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001144 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001145 O.PadToColumn(MAI->getCommentColumn());
1146 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001147 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001148 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001149 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001150 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001151 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001152 O.PadToColumn(MAI->getCommentColumn());
1153 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001154 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001155 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001156 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001157 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001158 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001159 O.PadToColumn(MAI->getCommentColumn());
1160 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001161 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001162 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001163 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001164 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001165 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1166 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001167 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001168 }
1169
1170 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001171 // all long double variants are printed as hex
1172 // api needed to prevent premature destruction
1173 APInt api = CFP->getValueAPF().bitcastToAPInt();
1174 const uint64_t *p = api.getRawData();
1175 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001176 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001177 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001178 O.PadToColumn(MAI->getCommentColumn());
1179 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001180 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001181 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001182 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001183 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001184 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001185 O.PadToColumn(MAI->getCommentColumn());
1186 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001187 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001188 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001189 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001190 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001191 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001192 O.PadToColumn(MAI->getCommentColumn());
1193 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001194 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001195 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001196 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001197 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001198 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001199 O.PadToColumn(MAI->getCommentColumn());
1200 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001201 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001202 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001203 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001204 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001205 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001206 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001207 O.PadToColumn(MAI->getCommentColumn());
1208 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001209 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001210 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001211 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001212 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001213 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001214 O.PadToColumn(MAI->getCommentColumn());
1215 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001216 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001217 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001218 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001219 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001220 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001221 O.PadToColumn(MAI->getCommentColumn());
1222 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001223 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001224 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001225 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001226 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001227 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001228 O.PadToColumn(MAI->getCommentColumn());
1229 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001230 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001231 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001232 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001233 }
1234 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001235 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001236}
1237
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001238void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1239 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001240 const TargetData *TD = TM.getTargetData();
1241 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001242 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001243
1244 // We don't expect assemblers to support integer data directives
1245 // for more than 64 bits, so we emit the data in at most 64-bit
1246 // quantities at a time.
1247 const uint64_t *RawData = CI->getValue().getRawData();
1248 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1249 uint64_t Val;
1250 if (TD->isBigEndian())
1251 Val = RawData[e - i - 1];
1252 else
1253 Val = RawData[i];
1254
Chris Lattner5979dff2010-01-13 04:38:16 +00001255 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001256 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Chris Lattner5979dff2010-01-13 04:38:16 +00001257 continue;
Dan Gohman00d448a2008-12-22 21:14:27 +00001258 }
Chris Lattner5979dff2010-01-13 04:38:16 +00001259
1260 // Emit two 32-bit chunks, order depends on endianness.
1261 unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
1262 const char *FirstName = " least", *SecondName = " most";
1263 if (TD->isBigEndian()) {
1264 std::swap(FirstChunk, SecondChunk);
1265 std::swap(FirstName, SecondName);
1266 }
1267
1268 O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
1269 if (VerboseAsm) {
1270 O.PadToColumn(MAI->getCommentColumn());
1271 O << MAI->getCommentString()
1272 << FirstName << " significant half of i64 " << Val;
1273 }
1274 O << '\n';
1275
1276 O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
1277 if (VerboseAsm) {
1278 O.PadToColumn(MAI->getCommentColumn());
1279 O << MAI->getCommentString()
1280 << SecondName << " significant half of i64 " << Val;
1281 }
1282 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001283 }
1284}
1285
Chris Lattner25045bd2005-11-21 07:51:36 +00001286/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001287void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001288 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001289 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001290 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001291
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001292 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001293 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001294 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001295 }
1296
1297 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001298 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001299 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001300 }
1301
1302 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001303 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001304 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001305 }
1306
1307 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001308 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001309 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001310 }
1311
1312 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1313 // If we can directly emit an 8-byte constant, do it.
1314 if (Size == 8)
1315 if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
1316 O << Data64Dir << CI->getZExtValue() << '\n';
1317 return;
1318 }
1319
Dan Gohman00d448a2008-12-22 21:14:27 +00001320 // Small integers are handled below; large integers are handled here.
1321 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001322 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001323 return;
1324 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001325 }
1326
1327 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001328 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001329 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001330 }
1331
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001332 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001333 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001334 if (VerboseAsm) {
1335 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1336 SmallString<40> S;
1337 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001338 O.PadToColumn(MAI->getCommentColumn());
1339 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001340 }
Scott Micheleefc8452008-06-03 15:39:51 +00001341 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001342 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001343}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001344
Chris Lattnerfad86b02008-08-17 07:19:36 +00001345void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001346 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001347 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001348}
1349
Chris Lattner3ce9b672006-09-26 23:59:50 +00001350/// PrintSpecial - Print information related to the specified machine instr
1351/// that is independent of the operand, and may be independent of the instr
1352/// itself. This can be useful for portably encoding the comment character
1353/// or other bits of target-specific knowledge into the asmstrings. The
1354/// syntax used is ${:comment}. Targets can override this to add support
1355/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001356void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001357 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001358 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001359 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001360 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001361 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001362 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001363 // Comparing the address of MI isn't sufficient, because machineinstrs may
1364 // be allocated to the same address across functions.
1365 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1366
Owen Andersonbd58edf2009-06-24 22:28:12 +00001367 // If this is a new LastFn instruction, bump the counter.
1368 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001369 ++Counter;
1370 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001371 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001372 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001373 O << Counter;
1374 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001375 std::string msg;
1376 raw_string_ostream Msg(msg);
1377 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001378 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001379 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001380 }
1381}
1382
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001383/// processDebugLoc - Processes the debug information of each machine
1384/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001385void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1386 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001387 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1388 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001389 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001390 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001391 if (DL.isUnknown())
1392 return;
Devang Patel6b61f582010-01-16 06:09:35 +00001393 DILocation CurDLT = MF->getDILocation(DL);
1394 if (CurDLT.getScope().isNull())
Devang Patel53bb5c92009-11-10 23:06:00 +00001395 return;
1396
1397 if (BeforePrintingInsn) {
Devang Patel6b61f582010-01-16 06:09:35 +00001398 if (CurDLT.getNode() != PrevDLT.getNode()) {
1399 unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(),
1400 CurDLT.getColumnNumber(),
1401 CurDLT.getScope().getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001402 printLabel(L);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001403 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001404 DW->BeginScope(MI, L);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001405 PrevDLT = CurDLT;
1406 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001407 } else {
1408 // After printing instruction
1409 DW->EndScope(MI);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001410 }
1411}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001412
Devang Patel53bb5c92009-11-10 23:06:00 +00001413
Chris Lattner0264d1a2006-01-27 02:10:10 +00001414/// printInlineAsm - This method formats and prints the specified machine
1415/// instruction that is an inline asm.
1416void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001417 unsigned NumOperands = MI->getNumOperands();
1418
1419 // Count the number of register definitions.
1420 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001421 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001422 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001423 assert(NumDefs != NumOperands-1 && "No asm string?");
1424
Dan Gohmand735b802008-10-03 15:45:36 +00001425 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001426
1427 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001428 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001429
Dan Gohman40c57862009-11-06 00:04:54 +00001430 O << '\t';
1431
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001432 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1433 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001434 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001435 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1436 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001437 return;
1438 }
1439
Chris Lattner33adcfb2009-08-22 21:43:10 +00001440 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001441
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001442 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001443 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001444
Chris Lattner66099132006-02-01 22:41:11 +00001445 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1446 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001447
Chris Lattner66099132006-02-01 22:41:11 +00001448 while (*LastEmitted) {
1449 switch (*LastEmitted) {
1450 default: {
1451 // Not a special case, emit the string section literally.
1452 const char *LiteralEnd = LastEmitted+1;
1453 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001454 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001455 ++LiteralEnd;
1456 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1457 O.write(LastEmitted, LiteralEnd-LastEmitted);
1458 LastEmitted = LiteralEnd;
1459 break;
1460 }
Chris Lattner1c059972006-05-05 21:47:05 +00001461 case '\n':
1462 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001463 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001464 break;
Chris Lattner66099132006-02-01 22:41:11 +00001465 case '$': {
1466 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001467 bool Done = true;
1468
1469 // Handle escapes.
1470 switch (*LastEmitted) {
1471 default: Done = false; break;
1472 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001473 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1474 O << '$';
1475 ++LastEmitted; // Consume second '$' character.
1476 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001477 case '(': // $( -> same as GCC's { character.
1478 ++LastEmitted; // Consume '(' character.
1479 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001480 llvm_report_error("Nested variants found in inline asm string: '"
1481 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001482 }
1483 CurVariant = 0; // We're in the first variant now.
1484 break;
1485 case '|':
1486 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001487 if (CurVariant == -1)
1488 O << '|'; // this is gcc's behavior for | outside a variant
1489 else
1490 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001491 break;
1492 case ')': // $) -> same as GCC's } char.
1493 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001494 if (CurVariant == -1)
1495 O << '}'; // this is gcc's behavior for } outside a variant
1496 else
1497 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001498 break;
Chris Lattner66099132006-02-01 22:41:11 +00001499 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001500 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001501
1502 bool HasCurlyBraces = false;
1503 if (*LastEmitted == '{') { // ${variable}
1504 ++LastEmitted; // Consume '{' character.
1505 HasCurlyBraces = true;
1506 }
1507
Chris Lattner3e0cc262009-03-10 05:37:13 +00001508 // If we have ${:foo}, then this is not a real operand reference, it is a
1509 // "magic" string reference, just like in .td files. Arrange to call
1510 // PrintSpecial.
1511 if (HasCurlyBraces && *LastEmitted == ':') {
1512 ++LastEmitted;
1513 const char *StrStart = LastEmitted;
1514 const char *StrEnd = strchr(StrStart, '}');
1515 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001516 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1517 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001518 }
1519
1520 std::string Val(StrStart, StrEnd);
1521 PrintSpecial(MI, Val.c_str());
1522 LastEmitted = StrEnd+1;
1523 break;
1524 }
1525
Chris Lattner66099132006-02-01 22:41:11 +00001526 const char *IDStart = LastEmitted;
1527 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001528 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001529 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1530 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001531 llvm_report_error("Bad $ operand number in inline asm string: '"
1532 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001533 }
1534 LastEmitted = IDEnd;
1535
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001536 char Modifier[2] = { 0, 0 };
1537
Chris Lattner66099132006-02-01 22:41:11 +00001538 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001539 // If we have curly braces, check for a modifier character. This
1540 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1541 if (*LastEmitted == ':') {
1542 ++LastEmitted; // Consume ':' character.
1543 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001544 llvm_report_error("Bad ${:} expression in inline asm string: '"
1545 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001546 }
1547
1548 Modifier[0] = *LastEmitted;
1549 ++LastEmitted; // Consume modifier character.
1550 }
1551
Chris Lattner66099132006-02-01 22:41:11 +00001552 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001553 llvm_report_error("Bad ${} expression in inline asm string: '"
1554 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001555 }
1556 ++LastEmitted; // Consume '}' character.
1557 }
1558
1559 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001560 llvm_report_error("Invalid $ operand number in inline asm string: '"
1561 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001562 }
1563
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001564 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001565 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001566 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1567 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001568
1569 bool Error = false;
1570
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001571 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001572 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001573 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001574 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001575 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001576 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001577
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001578 if (OpNo >= MI->getNumOperands()) {
1579 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001580 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001581 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001582 ++OpNo; // Skip over the ID number.
1583
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001584 if (Modifier[0]=='l') // labels are target independent
Chris Lattner325d3dc2009-09-13 17:14:04 +00001585 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1586 ->getNumber())->print(O, MAI);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001587 else {
1588 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001589 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001590 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1591 Modifier[0] ? Modifier : 0);
1592 } else {
1593 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1594 Modifier[0] ? Modifier : 0);
1595 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001596 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001597 }
1598 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001599 std::string msg;
1600 raw_string_ostream Msg(msg);
1601 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001602 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001603 MI->print(Msg);
1604 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001605 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001606 }
Chris Lattner66099132006-02-01 22:41:11 +00001607 break;
1608 }
Chris Lattner66099132006-02-01 22:41:11 +00001609 }
1610 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001611 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001612}
1613
Evan Chengda47e6e2008-03-15 00:03:38 +00001614/// printImplicitDef - This method prints the specified machine instruction
1615/// that is an implicit def.
1616void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001617 if (!VerboseAsm) return;
1618 O.PadToColumn(MAI->getCommentColumn());
1619 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001620 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001621}
1622
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001623void AsmPrinter::printKill(const MachineInstr *MI) const {
1624 if (!VerboseAsm) return;
1625 O.PadToColumn(MAI->getCommentColumn());
1626 O << MAI->getCommentString() << " kill:";
1627 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1628 const MachineOperand &op = MI->getOperand(n);
1629 assert(op.isReg() && "KILL instruction must have only register operands");
1630 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1631 }
1632}
1633
Jim Laskey1ee29252007-01-26 14:34:52 +00001634/// printLabel - This method prints a local label used by debug and
1635/// exception handling tables.
1636void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001637 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001638}
1639
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001640void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001641 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001642}
1643
Chris Lattner66099132006-02-01 22:41:11 +00001644/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1645/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001646/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001647bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001648 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001649 // Target doesn't support this yet!
1650 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001651}
Chris Lattnerdd260332006-02-24 20:21:58 +00001652
1653bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1654 unsigned AsmVariant,
1655 const char *ExtraCode) {
1656 // Target doesn't support this yet!
1657 return true;
1658}
Nate Begeman37efe672006-04-22 18:53:45 +00001659
Dan Gohman29cbade2009-11-20 23:18:13 +00001660MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1661 const char *Suffix) const {
1662 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001663}
1664
1665MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001666 const BasicBlock *BB,
1667 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001668 assert(BB->hasName() &&
1669 "Address of anonymous basic block not supported yet!");
1670
Dan Gohman568a3be2009-11-05 23:14:35 +00001671 // This code must use the function name itself, and not the function number,
1672 // since it must be possible to generate the label name from within other
1673 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001674 SmallString<60> FnName;
1675 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001676
Chris Lattner2f8cc262010-01-13 07:30:49 +00001677 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001678 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001679 Mang->getNameWithPrefix(NameResult,
1680 StringRef("BA") + Twine((unsigned)FnName.size()) +
1681 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1682 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001683
Chris Lattner48130352010-01-13 06:38:18 +00001684 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001685}
1686
Chris Lattner7cb384d2009-09-12 23:02:08 +00001687MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1688 SmallString<60> Name;
1689 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1690 << getFunctionNumber() << '_' << MBBID;
1691
1692 return OutContext.GetOrCreateSymbol(Name.str());
1693}
1694
Chris Lattner6b04ede2010-01-15 23:18:17 +00001695/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1696/// value.
1697MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1698 SmallString<60> NameStr;
1699 Mang->getNameWithPrefix(NameStr, GV, false);
1700 return OutContext.GetOrCreateSymbol(NameStr.str());
1701}
1702
Chris Lattner7a2ba942010-01-16 18:37:32 +00001703/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
Chris Lattnerd588b972010-01-15 23:25:11 +00001704/// global value name as its base, with the specified suffix, and where the
Chris Lattner7a2ba942010-01-16 18:37:32 +00001705/// symbol is forced to have private linkage if ForcePrivate is true.
1706MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1707 StringRef Suffix,
1708 bool ForcePrivate) const {
Chris Lattnerd588b972010-01-15 23:25:11 +00001709 SmallString<60> NameStr;
Chris Lattner7a2ba942010-01-16 18:37:32 +00001710 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
Chris Lattnerd588b972010-01-15 23:25:11 +00001711 NameStr.append(Suffix.begin(), Suffix.end());
1712 return OutContext.GetOrCreateSymbol(NameStr.str());
1713}
1714
Chris Lattner6b04ede2010-01-15 23:18:17 +00001715/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1716/// ExternalSymbol.
1717MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1718 SmallString<60> NameStr;
1719 Mang->getNameWithPrefix(NameStr, Sym);
1720 return OutContext.GetOrCreateSymbol(NameStr.str());
1721}
1722
Chris Lattner7cb384d2009-09-12 23:02:08 +00001723
Chris Lattner70a54c02009-09-13 18:25:37 +00001724/// EmitBasicBlockStart - This method prints the label for the specified
1725/// MachineBasicBlock, an alignment (if present) and a comment describing
1726/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001727void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001728 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001729 if (unsigned Align = MBB->getAlignment())
1730 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001731
Dan Gohmanb1cac332009-10-30 01:34:35 +00001732 // If the block has its address taken, emit a special label to satisfy
1733 // references to the block. This is done so that we don't need to
1734 // remember the number of this label, and so that we can make
1735 // forward references to labels without knowing what their numbers
1736 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001737 if (MBB->hasAddressTaken()) {
1738 GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1739 MBB->getBasicBlock())->print(O, MAI);
1740 O << ':';
1741 if (VerboseAsm) {
1742 O.PadToColumn(MAI->getCommentColumn());
1743 O << MAI->getCommentString() << " Address Taken";
1744 }
1745 O << '\n';
1746 }
1747
Dan Gohmanb1cac332009-10-30 01:34:35 +00001748 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001749 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1750 if (VerboseAsm)
1751 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1752 } else {
1753 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
1754 O << ':';
1755 if (!VerboseAsm)
1756 O << '\n';
1757 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001758
Dan Gohmanb1cac332009-10-30 01:34:35 +00001759 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001760 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001761 if (const BasicBlock *BB = MBB->getBasicBlock())
1762 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001763 O.PadToColumn(MAI->getCommentColumn());
1764 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001765 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001766 }
David Greeneb71d1b22009-08-10 16:38:07 +00001767
Chris Lattner70a54c02009-09-13 18:25:37 +00001768 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001769 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001770 }
Nate Begeman37efe672006-04-22 18:53:45 +00001771}
Nate Begeman52a51e382006-08-12 21:29:52 +00001772
Evan Chengcc415862007-11-09 01:32:10 +00001773/// printPICJumpTableSetLabel - This method prints a set label for the
1774/// specified MachineBasicBlock for a jumptable entry.
1775void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1776 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001777 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001778 return;
1779
Chris Lattner33adcfb2009-08-22 21:43:10 +00001780 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001781 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001782 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001783 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001784 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001785}
Evan Chengd6594ae2006-09-12 21:00:35 +00001786
Evan Chengcc415862007-11-09 01:32:10 +00001787void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1788 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001789 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001790 return;
1791
Chris Lattner33adcfb2009-08-22 21:43:10 +00001792 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001793 << getFunctionNumber() << '_' << uid << '_' << uid2
1794 << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001795 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001796 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001797 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001798}
1799
Evan Chengd6594ae2006-09-12 21:00:35 +00001800/// printDataDirective - This method prints the asm directive for the
1801/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001802void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001803 const TargetData *TD = TM.getTargetData();
1804 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001805 case Type::FloatTyID: case Type::DoubleTyID:
1806 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1807 assert(0 && "Should have already output floating point constant.");
1808 default:
1809 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001810 case Type::IntegerTyID: {
1811 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1812 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001813 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001814 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001815 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001816 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001817 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001818 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001819 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001820 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001821 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001822 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001823 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001824 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001825 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001826 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001827 case Type::PointerTyID:
1828 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001829 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001830 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001831 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001832 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001833 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001834 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001835 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001836 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001837 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001838 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001839 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001840 }
1841}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001842
Chris Lattner53d4d782010-01-15 23:38:51 +00001843void AsmPrinter::printVisibility(const MCSymbol *Sym,
1844 unsigned Visibility) const {
1845 if (Visibility == GlobalValue::HiddenVisibility) {
1846 if (const char *Directive = MAI->getHiddenDirective()) {
1847 O << Directive;
1848 Sym->print(O, MAI);
1849 O << '\n';
1850 }
1851 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1852 if (const char *Directive = MAI->getProtectedDirective()) {
1853 O << Directive;
1854 Sym->print(O, MAI);
1855 O << '\n';
1856 }
1857 }
1858}
1859
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001860void AsmPrinter::printOffset(int64_t Offset) const {
1861 if (Offset > 0)
1862 O << '+' << Offset;
1863 else if (Offset < 0)
1864 O << Offset;
1865}
1866
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001867GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1868 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001869 return 0;
1870
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001871 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001872 if (GCPI != GCMetadataPrinters.end())
1873 return GCPI->second;
1874
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001875 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001876
1877 for (GCMetadataPrinterRegistry::iterator
1878 I = GCMetadataPrinterRegistry::begin(),
1879 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1880 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001881 GCMetadataPrinter *GMP = I->instantiate();
1882 GMP->S = S;
1883 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1884 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001885 }
1886
Chris Lattner103289e2009-08-23 07:19:13 +00001887 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001888 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001889}
David Greene014700c2009-07-13 20:25:48 +00001890
1891/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001892void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001893 if (!VerboseAsm)
1894 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001895
David Greene1924aab2009-11-13 21:34:57 +00001896 bool Newline = false;
1897
1898 if (!MI.getDebugLoc().isUnknown()) {
Devang Patel6b61f582010-01-16 06:09:35 +00001899 DILocation DLT = MF->getDILocation(MI.getDebugLoc());
David Greene1924aab2009-11-13 21:34:57 +00001900
1901 // Print source line info.
1902 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001903 O << MAI->getCommentString() << ' ';
Devang Patel6b61f582010-01-16 06:09:35 +00001904 DIScope Scope = DLT.getScope();
Dan Gohman3b9bc042009-12-05 00:23:29 +00001905 // Omit the directory, because it's likely to be long and uninteresting.
1906 if (!Scope.isNull())
1907 O << Scope.getFilename();
1908 else
1909 O << "<unknown>";
Devang Patel6b61f582010-01-16 06:09:35 +00001910 O << ':' << DLT.getLineNumber();
1911 if (DLT.getColumnNumber() != 0)
1912 O << ':' << DLT.getColumnNumber();
David Greene1924aab2009-11-13 21:34:57 +00001913 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001914 }
David Greene1924aab2009-11-13 21:34:57 +00001915
David Greeneddff9412009-11-16 15:12:23 +00001916 // Check for spills and reloads
1917 int FI;
1918
1919 const MachineFrameInfo *FrameInfo =
1920 MI.getParent()->getParent()->getFrameInfo();
1921
1922 // We assume a single instruction only has a spill or reload, not
1923 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001924 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001925 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1926 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001927 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001928 if (Newline) O << '\n';
1929 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001930 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001931 Newline = true;
1932 }
1933 }
David Greenef7ea2a52009-12-04 22:46:04 +00001934 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001935 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1936 if (Newline) O << '\n';
1937 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001938 O << MAI->getCommentString() << ' '
1939 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001940 Newline = true;
1941 }
1942 }
1943 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1944 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001945 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001946 if (Newline) O << '\n';
1947 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001948 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001949 Newline = true;
1950 }
1951 }
David Greenef7ea2a52009-12-04 22:46:04 +00001952 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001953 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1954 if (Newline) O << '\n';
1955 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001956 O << MAI->getCommentString() << ' '
1957 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001958 Newline = true;
1959 }
1960 }
1961
1962 // Check for spill-induced copies
1963 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1964 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1965 SrcSubIdx, DstSubIdx)) {
1966 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1967 if (Newline) O << '\n';
1968 O.PadToColumn(MAI->getCommentColumn());
1969 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001970 }
1971 }
David Greene014700c2009-07-13 20:25:48 +00001972}
1973
David Greenefe37ab32009-08-18 19:22:55 +00001974/// PrintChildLoopComment - Print comments about child loops within
1975/// the loop for this basic block, with nesting.
1976///
1977static void PrintChildLoopComment(formatted_raw_ostream &O,
1978 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001979 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001980 int FunctionNumber) {
1981 // Add child loop information
1982 for(MachineLoop::iterator cl = loop->begin(),
1983 clend = loop->end();
1984 cl != clend;
1985 ++cl) {
1986 MachineBasicBlock *Header = (*cl)->getHeader();
1987 assert(Header && "No header for loop");
1988
1989 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001990 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001991
Chris Lattner33adcfb2009-08-22 21:43:10 +00001992 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001993 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001994 << " Child Loop BB" << FunctionNumber << "_"
1995 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1996
Chris Lattner33adcfb2009-08-22 21:43:10 +00001997 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001998 }
1999}
2000
David Greeneb71d1b22009-08-10 16:38:07 +00002001/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00002002void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00002003 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00002004 // Add loop depth information
2005 const MachineLoop *loop = LI->getLoopFor(&MBB);
2006
2007 if (loop) {
2008 // Print a newline after bb# annotation.
2009 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00002010 O.PadToColumn(MAI->getCommentColumn());
2011 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00002012 << '\n';
2013
Chris Lattner33adcfb2009-08-22 21:43:10 +00002014 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00002015
2016 MachineBasicBlock *Header = loop->getHeader();
2017 assert(Header && "No header for loop");
2018
2019 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002020 O << MAI->getCommentString() << " Loop Header";
2021 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00002022 }
2023 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002024 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00002025 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
2026 }
2027
2028 if (loop->empty()) {
2029 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002030 O.PadToColumn(MAI->getCommentColumn());
2031 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00002032 }
2033
2034 // Add parent loop information
2035 for (const MachineLoop *CurLoop = loop->getParentLoop();
2036 CurLoop;
2037 CurLoop = CurLoop->getParentLoop()) {
2038 MachineBasicBlock *Header = CurLoop->getHeader();
2039 assert(Header && "No header for loop");
2040
2041 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002042 O.PadToColumn(MAI->getCommentColumn());
2043 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002044 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002045 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00002046 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
2047 }
2048 }
2049 }
2050}