blob: dec78a398fa64aa3ce34d0251a3748dc93c56b7c [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"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000020#include "llvm/CodeGen/MachineConstantPool.h"
David Greenefe37ab32009-08-18 19:22:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000022#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelc48c5502009-01-13 21:44:10 +000025#include "llvm/CodeGen/DwarfWriter.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000026#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000027#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000028#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCStreamer.h"
Chris Lattner7cb384d2009-09-12 23:02:08 +000031#include "llvm/MC/MCSymbol.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000032#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000033#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000034#include "llvm/Support/FormattedStream.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000035#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000037#include "llvm/Target/TargetData.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000038#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000040#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000041#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000042#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000043#include "llvm/ADT/SmallString.h"
Owen Andersoncb371882008-08-21 00:14:44 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattner66099132006-02-01 22:41:11 +000045#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000046using namespace llvm;
47
Evan Cheng42bf74b2009-03-25 01:47:28 +000048static cl::opt<cl::boolOrDefault>
49AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
50 cl::init(cl::BOU_UNSET));
51
Devang Patel19974732007-05-03 01:11:54 +000052char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000053AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000054 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000055 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000056 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000057
58 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000059 // FIXME: Pass instprinter to streamer.
60 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000061
Chris Lattner290c2f52009-08-03 23:20:21 +000062 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersona8dbf362009-06-25 16:55:32 +000063 PrevDLT(0, ~0U, ~0U) {
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
Chris Lattner33adcfb2009-08-22 21:43:10 +0000107 if (MAI->doesAllowQuotesInName())
Chris Lattnera93ca922009-06-18 23:41:35 +0000108 Mang->setUseQuotes(true);
Anton Korobeynikovc6f729e2009-09-18 16:57:42 +0000109
110 if (MAI->doesAllowNameToStartWithDigit())
111 Mang->setSymbolsCanStartWithDigit(true);
Chris Lattnera93ca922009-06-18 23:41:35 +0000112
Duncan Sands1465d612009-01-28 13:14:17 +0000113 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000114 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola952b8392008-12-03 11:01:37 +0000115
Chris Lattner33adcfb2009-08-22 21:43:10 +0000116 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000117 /* Very minimal debug info. It is ignored if we emit actual
118 debug info. If we don't, this at helps the user find where
119 a function came from. */
120 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
121 }
122
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000123 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
124 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000125 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000126
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000127 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000128 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000129 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000130 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000131 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000132
Chris Lattner33adcfb2009-08-22 21:43:10 +0000133 if (MAI->doesSupportDebugInformation() ||
134 MAI->doesSupportExceptionHandling()) {
Chris Lattner0de1fc42009-06-24 19:09:55 +0000135 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
136 if (MMI)
Devang Patel14a55d92009-06-19 21:54:26 +0000137 MMI->AnalyzeModule(M);
Chris Lattner0de1fc42009-06-24 19:09:55 +0000138 DW = getAnalysisIfAvailable<DwarfWriter>();
139 if (DW)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000140 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000141 }
142
Chris Lattnera80ba712004-08-16 23:15:22 +0000143 return false;
144}
145
146bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000147 // Emit global variables.
148 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
149 I != E; ++I)
150 PrintGlobalVariable(I);
151
Chris Lattner1f522fe2009-06-24 18:54:37 +0000152 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000153 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000154 DW->EndModule();
155
Chris Lattner0a7befa2009-06-24 18:52:01 +0000156 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000157 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000158 // FIXME: This is not lazy, it would be nice to only print weak references
159 // to stuff that is actually used. Note that doing so would require targets
160 // to notice uses in operands (due to constant exprs etc). This should
161 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000162
Chris Lattner0a7befa2009-06-24 18:52:01 +0000163 // Print out module-level global variables here.
164 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
165 I != E; ++I) {
166 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000167 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000168 }
169
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000170 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000171 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000172 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000173 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000174 }
175
Chris Lattner33adcfb2009-08-22 21:43:10 +0000176 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000177 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000178 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000179 I != E; ++I) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000180 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000181
182 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000183 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000184
Chris Lattner33adcfb2009-08-22 21:43:10 +0000185 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000186 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000187 else if (I->hasWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000188 O << MAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000189 else if (!I->hasLocalLinkage())
Torok Edwinc23197a2009-07-14 16:55:14 +0000190 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000191
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000192 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000193
Chris Lattner33adcfb2009-08-22 21:43:10 +0000194 O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000195 }
196 }
197
Duncan Sands1465d612009-01-28 13:14:17 +0000198 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000199 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
200 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
201 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000202 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000203
Dan Gohmana779a982008-05-05 00:28:39 +0000204 // If we don't have any trampolines, then we don't require stack memory
205 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000206 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000207 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000208 if (MAI->getNonexecutableStackDirective())
209 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000210
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000211
212 // Allow the target to emit any magic that it wants at the end of the file,
213 // after everything else has gone out.
214 EmitEndOfAsmFile(M);
215
Chris Lattnera80ba712004-08-16 23:15:22 +0000216 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000217 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000218
219 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000220 return false;
221}
222
Chris Lattner25045bd2005-11-21 07:51:36 +0000223void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000224 // What's my mangled name?
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000225 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000226 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000227
Chris Lattner25d812b2009-09-16 00:35:39 +0000228 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000229 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000230}
231
Evan Cheng1606e8e2009-03-13 07:51:59 +0000232namespace {
233 // SectionCPs - Keep track the alignment, constpool entries per Section.
234 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000235 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000236 unsigned Alignment;
237 SmallVector<unsigned, 4> CPEs;
Chris Lattnera87dea42009-07-31 18:48:30 +0000238 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng1606e8e2009-03-13 07:51:59 +0000239 };
240}
241
Chris Lattner3b4fd322005-11-21 08:25:09 +0000242/// EmitConstantPool - Print to the current output stream assembly
243/// representations of the constants in the constant pool MCP. This is
244/// used to print out constants which have been "spilled to memory" by
245/// the code generator.
246///
247void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000248 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000249 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000250
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000251 // Calculate sections for constant pool entries. We collect entries to go into
252 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000253 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000254 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000255 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000256 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000257
258 SectionKind Kind;
259 switch (CPE.getRelocationInfo()) {
260 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000261 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000262 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000263 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000264 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000265 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000266 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000267 case 4: Kind = SectionKind::getMergeableConst4(); break;
268 case 8: Kind = SectionKind::getMergeableConst8(); break;
269 case 16: Kind = SectionKind::getMergeableConst16();break;
270 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000271 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000272 }
273
Chris Lattner83d77fa2009-08-01 23:46:12 +0000274 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000275
Evan Cheng1606e8e2009-03-13 07:51:59 +0000276 // The number of sections are small, just do a linear search from the
277 // last section to the first.
278 bool Found = false;
279 unsigned SecIdx = CPSections.size();
280 while (SecIdx != 0) {
281 if (CPSections[--SecIdx].S == S) {
282 Found = true;
283 break;
284 }
285 }
286 if (!Found) {
287 SecIdx = CPSections.size();
288 CPSections.push_back(SectionCPs(S, Align));
289 }
290
291 if (Align > CPSections[SecIdx].Alignment)
292 CPSections[SecIdx].Alignment = Align;
293 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000294 }
295
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000296 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000297 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000298 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000299 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000300
Evan Cheng1606e8e2009-03-13 07:51:59 +0000301 unsigned Offset = 0;
302 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
303 unsigned CPI = CPSections[i].CPEs[j];
304 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000305
Chris Lattner3029f922006-02-09 04:46:04 +0000306 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000307 unsigned AlignMask = CPE.getAlignment() - 1;
308 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
309 EmitZeros(NewOffset - Offset);
310
311 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000312 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000313
Chris Lattner33adcfb2009-08-22 21:43:10 +0000314 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000315 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000316 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000317 O.PadToColumn(MAI->getCommentColumn());
318 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000319 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000320 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000321 O << '\n';
322 if (CPE.isMachineConstantPoolEntry())
323 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
324 else
325 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000326 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000327 }
328}
329
Nate Begeman37efe672006-04-22 18:53:45 +0000330/// EmitJumpTableInfo - Print assembly representations of the jump tables used
331/// by the current function to the current output stream.
332///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000333void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
334 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000335 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
336 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000337
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000338 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000339
Nate Begeman2f1ae882006-07-27 01:13:04 +0000340 // Pick the directive to use to print the jump table entries, and switch to
341 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000342 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000343
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000344 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000345 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000346 if (F->isWeakForLinker() ||
347 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000348 // In PIC mode, we need to emit the jump table to the same section as the
349 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000350 // We should also do if the section name is NULL or function is declared in
351 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000352 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
353 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000354 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000355 // Otherwise, drop it in the readonly section.
356 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000357 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000358 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000359 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000360 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000361
362 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000363
Nate Begeman37efe672006-04-22 18:53:45 +0000364 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000365 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000366
367 // If this jump table was deleted, ignore it.
368 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000369
370 // For PIC codegen, if possible we want to use the SetDirective to reduce
371 // the number of relocations the assembler will generate for the jump table.
372 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000373 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000374 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000375 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000376 if (EmittedSets.insert(JTBBs[ii]))
377 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000378
Chris Lattner7c301912009-09-13 19:02:16 +0000379 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000380 // before each jump table. The first label is never referenced, but tells
381 // the assembler and linker the extents of the jump table object. The
382 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000383 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
384 O << MAI->getLinkerPrivateGlobalPrefix()
385 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000386 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000387
Chris Lattner33adcfb2009-08-22 21:43:10 +0000388 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000389 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000390
Nate Begeman37efe672006-04-22 18:53:45 +0000391 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000392 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000393 O << '\n';
394 }
395 }
396}
397
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000398void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
399 const MachineBasicBlock *MBB,
400 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000401 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000402
403 // Use JumpTableDirective otherwise honor the entry size from the jump table
404 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000405 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000406 bool HadJTEntryDirective = JTEntryDirective != NULL;
407 if (!HadJTEntryDirective) {
408 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000409 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000410 }
411
412 O << JTEntryDirective << ' ';
413
414 // If we have emitted set directives for the jump table entries, print
415 // them rather than the entries themselves. If we're emitting PIC, then
416 // emit the table entries as differences between two text section labels.
417 // If we're emitting non-PIC code, then emit the entries as direct
418 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000419 if (!isPIC) {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000420 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000421 } else if (MAI->getSetDirective()) {
422 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000423 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000424 } else {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000425 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000426 // If the arch uses custom Jump Table directives, don't calc relative to
427 // JT
428 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000429 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000430 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000431 }
432}
433
434
Chris Lattnered138932005-12-13 06:32:10 +0000435/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
436/// special global used by LLVM. If so, emit it and return true, otherwise
437/// do nothing and return false.
438bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000439 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000440 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000441 EmitLLVMUsedList(GV->getInitializer());
442 return true;
443 }
444
Chris Lattner401e10c2009-07-20 06:14:25 +0000445 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000446 if (GV->getSection() == "llvm.metadata" ||
447 GV->hasAvailableExternallyLinkage())
448 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000449
450 if (!GV->hasAppendingLinkage()) return false;
451
452 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000453
Evan Cheng916d07c2007-06-04 20:39:18 +0000454 const TargetData *TD = TM.getTargetData();
455 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000456 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000457 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000458 EmitAlignment(Align, 0);
459 EmitXXStructorList(GV->getInitializer());
460 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000461 }
462
Chris Lattnerf231c072009-03-09 05:52:15 +0000463 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000464 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000465 EmitAlignment(Align, 0);
466 EmitXXStructorList(GV->getInitializer());
467 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000468 }
469
470 return false;
471}
472
Chris Lattner33adcfb2009-08-22 21:43:10 +0000473/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000474/// global in the specified llvm.used list for which emitUsedDirectiveFor
475/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000476void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000477 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000478
Dan Gohmana119de82009-06-14 23:30:43 +0000479 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000480 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
481 if (InitList == 0) return;
482
483 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000484 const GlobalValue *GV =
485 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000486 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000487 O << Directive;
488 EmitConstantValueOnly(InitList->getOperand(i));
489 O << '\n';
490 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000491 }
492}
493
Chris Lattnered138932005-12-13 06:32:10 +0000494/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
495/// function pointers, ignoring the init priority.
496void AsmPrinter::EmitXXStructorList(Constant *List) {
497 // Should be an array of '{ int, void ()* }' structs. The first value is the
498 // init priority, which we ignore.
499 if (!isa<ConstantArray>(List)) return;
500 ConstantArray *InitList = cast<ConstantArray>(List);
501 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
502 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
503 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000504
505 if (CS->getOperand(1)->isNullValue())
506 return; // Found a null terminator, exit printing.
507 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000508 EmitGlobalConstant(CS->getOperand(1));
509 }
510}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000511
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000512
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000513//===----------------------------------------------------------------------===//
514/// LEB 128 number encoding.
515
516/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
517/// representing an unsigned leb128 value.
518void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattnera64f4632008-11-10 04:35:24 +0000519 char Buffer[20];
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000520 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000521 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000522 Value >>= 7;
523 if (Value) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000524 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000525 if (Value) O << ", ";
526 } while (Value);
527}
528
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000529/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
530/// representing a signed leb128 value.
531void AsmPrinter::PrintSLEB128(int Value) const {
532 int Sign = Value >> (8 * sizeof(Value) - 1);
533 bool IsMore;
Chris Lattnera64f4632008-11-10 04:35:24 +0000534 char Buffer[20];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000535
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000536 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000537 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000538 Value >>= 7;
539 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
540 if (IsMore) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000541 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000542 if (IsMore) O << ", ";
543 } while (IsMore);
544}
545
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000546//===--------------------------------------------------------------------===//
547// Emission and print routines
548//
549
550/// PrintHex - Print a value as a hexidecimal value.
551///
552void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerc6a13462008-11-10 04:30:26 +0000553 char Buffer[20];
Chris Lattnerc6a13462008-11-10 04:30:26 +0000554 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000555}
556
557/// EOL - Print a newline character to asm stream. If a comment is present
558/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000559void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000560 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000561}
Owen Anderson25995092008-07-01 21:16:27 +0000562
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000563void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000564 if (VerboseAsm && !Comment.empty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000565 O.PadToColumn(MAI->getCommentColumn());
566 O << MAI->getCommentString()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000567 << ' '
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000568 << Comment;
569 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000570 O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000571}
572
Owen Anderson25995092008-07-01 21:16:27 +0000573void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000574 if (VerboseAsm && *Comment) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000575 O.PadToColumn(MAI->getCommentColumn());
576 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000577 << ' '
578 << Comment;
579 }
580 O << '\n';
581}
582
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000583static const char *DecodeDWARFEncoding(unsigned Encoding) {
584 switch (Encoding) {
585 case dwarf::DW_EH_PE_absptr:
586 return "absptr";
587 case dwarf::DW_EH_PE_omit:
588 return "omit";
589 case dwarf::DW_EH_PE_pcrel:
590 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000591 case dwarf::DW_EH_PE_udata4:
592 return "udata4";
593 case dwarf::DW_EH_PE_udata8:
594 return "udata8";
595 case dwarf::DW_EH_PE_sdata4:
596 return "sdata4";
597 case dwarf::DW_EH_PE_sdata8:
598 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000599 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
600 return "pcrel udata4";
601 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
602 return "pcrel sdata4";
603 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
604 return "pcrel udata8";
605 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
606 return "pcrel sdata8";
607 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
608 return "indirect pcrel udata4";
609 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
610 return "indirect pcrel sdata4";
611 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
612 return "indirect pcrel udata8";
613 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
614 return "indirect pcrel sdata8";
615 }
616
617 return 0;
618}
619
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000620void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
621 if (VerboseAsm && *Comment) {
622 O.PadToColumn(MAI->getCommentColumn());
623 O << MAI->getCommentString()
624 << ' '
625 << Comment;
626
627 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
628 O << " (" << EncStr << ')';
629 }
630 O << '\n';
631}
632
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000633/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
634/// unsigned leb128 value.
635void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000636 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000637 O << "\t.uleb128\t"
638 << Value;
639 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000640 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000641 PrintULEB128(Value);
642 }
643}
644
645/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
646/// signed leb128 value.
647void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000648 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000649 O << "\t.sleb128\t"
650 << Value;
651 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000652 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000653 PrintSLEB128(Value);
654 }
655}
656
657/// EmitInt8 - Emit a byte directive and value.
658///
659void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000660 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000661 PrintHex(Value & 0xFF);
662}
663
664/// EmitInt16 - Emit a short directive and value.
665///
666void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000667 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000668 PrintHex(Value & 0xFFFF);
669}
670
671/// EmitInt32 - Emit a long directive and value.
672///
673void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000674 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000675 PrintHex(Value);
676}
677
678/// EmitInt64 - Emit a long long directive and value.
679///
680void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000681 if (MAI->getData64bitsDirective()) {
682 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000683 PrintHex(Value);
684 } else {
685 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000686 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000687 EmitInt32(unsigned(Value));
688 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000689 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000690 EmitInt32(unsigned(Value >> 32));
691 }
692 }
693}
694
695/// toOctal - Convert the low order bits of X into an octal digit.
696///
697static inline char toOctal(int X) {
698 return (X&7)+'0';
699}
700
701/// printStringChar - Print a char, escaped if necessary.
702///
David Greene71847812009-07-14 20:18:05 +0000703static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000704 if (C == '"') {
705 O << "\\\"";
706 } else if (C == '\\') {
707 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000708 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000709 O << C;
710 } else {
711 switch(C) {
712 case '\b': O << "\\b"; break;
713 case '\f': O << "\\f"; break;
714 case '\n': O << "\\n"; break;
715 case '\r': O << "\\r"; break;
716 case '\t': O << "\\t"; break;
717 default:
718 O << '\\';
719 O << toOctal(C >> 6);
720 O << toOctal(C >> 3);
721 O << toOctal(C >> 0);
722 break;
723 }
724 }
725}
726
727/// EmitString - Emit a string with quotes and a null terminator.
728/// Special characters are emitted properly.
729/// \literal (Eg. '\t') \endliteral
730void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendlingf34be822009-04-09 23:51:31 +0000731 EmitString(String.c_str(), String.size());
732}
733
734void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000735 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000736 if (AscizDirective)
737 O << AscizDirective;
738 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000739 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000740 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000741 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000742 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000743 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000744 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000745 else
746 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000747}
748
749
Dan Gohman189f80d2007-09-24 20:58:13 +0000750/// EmitFile - Emit a .file directive.
751void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
752 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000753 for (unsigned i = 0, N = Name.size(); i < N; ++i)
754 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000755 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000756}
757
758
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000759//===----------------------------------------------------------------------===//
760
Chris Lattner3a420532007-05-31 18:57:45 +0000761// EmitAlignment - Emit an alignment directive to the specified power of
762// two boundary. For example, if you pass in 3 here, you will get an 8
763// byte alignment. If a global value is specified, and if that global has
764// an explicit alignment requested, it will unconditionally override the
765// alignment request. However, if ForcedAlignBits is specified, this value
766// has final say: the ultimate alignment will be the max of ForcedAlignBits
767// and the alignment computed with NumBits and the global.
768//
769// The algorithm is:
770// Align = NumBits;
771// if (GV && GV->hasalignment) Align = GV->getalignment();
772// Align = std::max(Align, ForcedAlignBits);
773//
774void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000775 unsigned ForcedAlignBits,
776 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000777 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000778 NumBits = Log2_32(GV->getAlignment());
779 NumBits = std::max(NumBits, ForcedAlignBits);
780
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000781 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000782
783 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000784 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000785 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000786
787 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000788}
David Greenea5bb59f2009-08-05 21:00:52 +0000789
Chris Lattner25045bd2005-11-21 07:51:36 +0000790/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000791///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000792void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000793 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000794 if (MAI->getZeroDirective()) {
795 O << MAI->getZeroDirective() << NumZeros;
796 if (MAI->getZeroDirectiveSuffix())
797 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000798 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000799 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000800 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000801 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000802 }
803 }
804}
805
Chris Lattnera80ba712004-08-16 23:15:22 +0000806// Print out the specified constant, without a storage class. Only the
807// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000808void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000809 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohmand19a53b2008-06-30 22:03:41 +0000810 O << '0';
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000811 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000812 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000813 } else 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.
816 O << Mang->getMangledName(GV);
Duraid Madina855a5192005-04-02 12:21:51 +0000817 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000818 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000819 unsigned Opcode = CE->getOpcode();
820 switch (Opcode) {
Chris Lattnerc19ee612009-08-01 22:25:12 +0000821 case Instruction::Trunc:
822 case Instruction::ZExt:
823 case Instruction::SExt:
824 case Instruction::FPTrunc:
825 case Instruction::FPExt:
826 case Instruction::UIToFP:
827 case Instruction::SIToFP:
828 case Instruction::FPToUI:
829 case Instruction::FPToSI:
830 llvm_unreachable("FIXME: Don't support this constant cast expr");
Chris Lattnera80ba712004-08-16 23:15:22 +0000831 case Instruction::GetElementPtr: {
832 // generate a symbolic expression for the byte address
833 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000834 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
835 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
836 idxVec.size())) {
Chris Lattner4798bbe2009-02-05 06:55:21 +0000837 // Truncate/sext the offset to the pointer size.
838 if (TD->getPointerSizeInBits() != 64) {
839 int SExtAmount = 64-TD->getPointerSizeInBits();
840 Offset = (Offset << SExtAmount) >> SExtAmount;
841 }
842
Chris Lattner27e19212005-02-14 21:40:26 +0000843 if (Offset)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000844 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000845 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000846 if (Offset > 0)
847 O << ") + " << Offset;
848 else if (Offset < 0)
849 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000850 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000851 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000852 }
853 break;
854 }
Chris Lattnercb0a6812006-12-12 05:14:13 +0000855 case Instruction::BitCast:
856 return EmitConstantValueOnly(CE->getOperand(0));
857
Chris Lattnerddc94012006-12-12 05:18:19 +0000858 case Instruction::IntToPtr: {
859 // Handle casts to pointers by changing them into casts to the appropriate
860 // integer type. This promotes constant folding and simplifies this code.
861 Constant *Op = CE->getOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +0000862 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
863 false/*ZExt*/);
Chris Lattnerddc94012006-12-12 05:18:19 +0000864 return EmitConstantValueOnly(Op);
865 }
866
867
868 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000869 // Support only foldable casts to/from pointers that can be eliminated by
870 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000871 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000872 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000873
Chris Lattnerddc94012006-12-12 05:18:19 +0000874 // We can emit the pointer value into this slot if the slot is an
875 // integer slot greater or equal to the size of the pointer.
Chris Lattnerc19ee612009-08-01 22:25:12 +0000876 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000877 return EmitConstantValueOnly(Op);
Nick Lewyckyd6227382008-08-08 06:34:07 +0000878
879 O << "((";
Chris Lattner25045bd2005-11-21 07:51:36 +0000880 EmitConstantValueOnly(Op);
Chris Lattnerc19ee612009-08-01 22:25:12 +0000881 APInt ptrMask =
882 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattnerfad86b02008-08-17 07:19:36 +0000883
884 SmallString<40> S;
885 ptrMask.toStringUnsigned(S);
Daniel Dunbardddfd342009-08-19 20:07:03 +0000886 O << ") & " << S.str() << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000887 break;
888 }
889 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000890 case Instruction::Sub:
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000891 case Instruction::And:
892 case Instruction::Or:
893 case Instruction::Xor:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000894 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000895 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000896 O << ')';
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000897 switch (Opcode) {
898 case Instruction::Add:
899 O << " + ";
900 break;
901 case Instruction::Sub:
902 O << " - ";
903 break;
904 case Instruction::And:
905 O << " & ";
906 break;
907 case Instruction::Or:
908 O << " | ";
909 break;
910 case Instruction::Xor:
911 O << " ^ ";
912 break;
913 default:
914 break;
915 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000916 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000917 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000918 O << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000919 break;
920 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000921 llvm_unreachable("Unsupported operator!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000922 }
923 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000924 llvm_unreachable("Unknown constant value!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000925 }
926}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000927
Chris Lattner2980cef2005-11-10 18:06:33 +0000928/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000929/// the predicate isString is true.
930///
David Greene71847812009-07-14 20:18:05 +0000931static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000932 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000933 assert(CVA->isString() && "Array is not string compatible!");
934
Dan Gohmand19a53b2008-06-30 22:03:41 +0000935 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000936 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000937 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000938 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000939 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000940 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000941 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000942}
943
Jeff Cohenc884db42006-05-02 01:16:28 +0000944/// EmitString - Emit a zero-byte-terminated string constant.
945///
946void AsmPrinter::EmitString(const ConstantArray *CVA) const {
947 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000948 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000949 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000950 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000951 printAsCString(O, CVA, NumElts-1);
952 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000953 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000954 printAsCString(O, CVA, NumElts);
955 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000956 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000957}
958
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000959void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
960 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000961 if (CVA->isString()) {
962 EmitString(CVA);
963 } else { // Not a string. Print the values in successive locations
964 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000965 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000966 }
967}
968
969void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
970 const VectorType *PTy = CP->getType();
971
972 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
973 EmitGlobalConstant(CP->getOperand(I));
974}
975
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000976void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
977 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000978 // Print the fields in successive locations. Pad to align if needed!
979 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +0000980 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000981 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
982 uint64_t sizeSoFar = 0;
983 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
984 const Constant* field = CVS->getOperand(i);
985
986 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +0000987 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000988 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
989 - cvsLayout->getElementOffset(i)) - fieldSize;
990 sizeSoFar += fieldSize + padSize;
991
992 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000993 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000994
995 // Insert padding - this may include padding to increase the size of the
996 // current field up to the ABI size (if the struct is not packed) as well
997 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000998 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000999 }
1000 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1001 "Layout of constant struct may be incorrect!");
1002}
1003
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001004void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1005 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001006 // FP Constants are printed as integer constants to avoid losing
1007 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001008 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001009 const TargetData *TD = TM.getTargetData();
Owen Anderson1d0be152009-08-13 21:58:54 +00001010 if (CFP->getType() == Type::getDoubleTy(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001011 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1012 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001013 if (MAI->getData64bitsDirective(AddrSpace)) {
1014 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001015 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001016 O.PadToColumn(MAI->getCommentColumn());
1017 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001018 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001019 O << '\n';
1020 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001021 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001022 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001023 O.PadToColumn(MAI->getCommentColumn());
1024 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001025 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001026 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001027 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001028 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001029 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001030 O.PadToColumn(MAI->getCommentColumn());
1031 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001032 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001033 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001034 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001035 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001036 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001037 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001038 O.PadToColumn(MAI->getCommentColumn());
1039 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001040 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001041 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001042 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001043 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001044 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001045 O.PadToColumn(MAI->getCommentColumn());
1046 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001047 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001048 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001049 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001050 }
1051 return;
Owen Anderson1d0be152009-08-13 21:58:54 +00001052 } else if (CFP->getType() == Type::getFloatTy(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001053 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001054 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001055 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001056 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001057 O.PadToColumn(MAI->getCommentColumn());
1058 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001059 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001060 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001061 return;
Owen Anderson1d0be152009-08-13 21:58:54 +00001062 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001063 // all long double variants are printed as hex
1064 // api needed to prevent premature destruction
1065 APInt api = CFP->getValueAPF().bitcastToAPInt();
1066 const uint64_t *p = api.getRawData();
1067 // Convert to double so we can print the approximate val as a comment.
1068 APFloat DoubleVal = CFP->getValueAPF();
1069 bool ignored;
1070 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1071 &ignored);
1072 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001073 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001074 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001075 O.PadToColumn(MAI->getCommentColumn());
1076 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001077 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001078 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001079 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001080 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001081 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001082 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001083 O.PadToColumn(MAI->getCommentColumn());
1084 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001085 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001086 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001087 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001088 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001089 O.PadToColumn(MAI->getCommentColumn());
1090 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001091 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001092 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001093 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001094 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001095 O.PadToColumn(MAI->getCommentColumn());
1096 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001097 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001098 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001099 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001100 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001101 O.PadToColumn(MAI->getCommentColumn());
1102 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001103 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001104 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001105 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001106 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001107 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001108 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001109 O.PadToColumn(MAI->getCommentColumn());
1110 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001111 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001112 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001113 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001114 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001115 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001116 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001117 O.PadToColumn(MAI->getCommentColumn());
1118 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001119 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001120 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001121 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001122 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001123 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001124 O.PadToColumn(MAI->getCommentColumn());
1125 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001126 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001127 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001128 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001129 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001130 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001131 O.PadToColumn(MAI->getCommentColumn());
1132 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001133 << " next halfword";
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[1]);
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 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001141 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001142 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001143 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001144 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1145 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001146 return;
Owen Anderson1d0be152009-08-13 21:58:54 +00001147 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001148 // all long double variants are printed as hex
1149 // api needed to prevent premature destruction
1150 APInt api = CFP->getValueAPF().bitcastToAPInt();
1151 const uint64_t *p = api.getRawData();
1152 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001153 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001154 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001155 O.PadToColumn(MAI->getCommentColumn());
1156 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001157 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001158 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001159 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001160 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001161 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001162 O.PadToColumn(MAI->getCommentColumn());
1163 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001164 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001165 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001166 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001167 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001168 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001169 O.PadToColumn(MAI->getCommentColumn());
1170 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001171 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001172 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001173 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001174 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001175 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001176 O.PadToColumn(MAI->getCommentColumn());
1177 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001178 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001179 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001180 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001181 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001182 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001183 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001184 O.PadToColumn(MAI->getCommentColumn());
1185 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001186 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001187 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001188 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001189 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001190 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001191 O.PadToColumn(MAI->getCommentColumn());
1192 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001193 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001194 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001195 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001196 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001197 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001198 O.PadToColumn(MAI->getCommentColumn());
1199 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001200 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001201 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001202 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001203 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001204 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001205 O.PadToColumn(MAI->getCommentColumn());
1206 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001207 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001208 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001209 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001210 }
1211 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001212 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001213}
1214
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001215void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1216 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001217 const TargetData *TD = TM.getTargetData();
1218 unsigned BitWidth = CI->getBitWidth();
1219 assert(isPowerOf2_32(BitWidth) &&
1220 "Non-power-of-2-sized integers not handled!");
1221
1222 // We don't expect assemblers to support integer data directives
1223 // for more than 64 bits, so we emit the data in at most 64-bit
1224 // quantities at a time.
1225 const uint64_t *RawData = CI->getValue().getRawData();
1226 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1227 uint64_t Val;
1228 if (TD->isBigEndian())
1229 Val = RawData[e - i - 1];
1230 else
1231 Val = RawData[i];
1232
Chris Lattner33adcfb2009-08-22 21:43:10 +00001233 if (MAI->getData64bitsDirective(AddrSpace))
1234 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001235 else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001236 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001237 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001238 O.PadToColumn(MAI->getCommentColumn());
1239 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001240 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001241 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001242 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001243 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001244 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001245 O.PadToColumn(MAI->getCommentColumn());
1246 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001247 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001248 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001249 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001250 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001251 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001252 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001253 O.PadToColumn(MAI->getCommentColumn());
1254 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001255 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001256 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001257 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001258 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001259 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001260 O.PadToColumn(MAI->getCommentColumn());
1261 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001262 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001263 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001264 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001265 }
1266 }
1267}
1268
Chris Lattner25045bd2005-11-21 07:51:36 +00001269/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001270void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001271 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001272 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001273 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001274
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001275 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001276 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001277 return;
1278 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001279 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001280 return;
1281 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001282 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001283 return;
1284 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001285 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001286 return;
1287 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1288 // Small integers are handled below; large integers are handled here.
1289 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001290 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001291 return;
1292 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001293 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001294 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001295 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001296 }
1297
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001298 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001299 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001300 if (VerboseAsm) {
1301 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1302 SmallString<40> S;
1303 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001304 O.PadToColumn(MAI->getCommentColumn());
1305 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001306 }
Scott Micheleefc8452008-06-03 15:39:51 +00001307 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001308 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001309}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001310
Chris Lattnerfad86b02008-08-17 07:19:36 +00001311void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001312 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001313 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001314}
1315
Chris Lattner3ce9b672006-09-26 23:59:50 +00001316/// PrintSpecial - Print information related to the specified machine instr
1317/// that is independent of the operand, and may be independent of the instr
1318/// itself. This can be useful for portably encoding the comment character
1319/// or other bits of target-specific knowledge into the asmstrings. The
1320/// syntax used is ${:comment}. Targets can override this to add support
1321/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001322void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001323 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001324 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001325 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001326 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001327 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001328 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001329 // Comparing the address of MI isn't sufficient, because machineinstrs may
1330 // be allocated to the same address across functions.
1331 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1332
Owen Andersonbd58edf2009-06-24 22:28:12 +00001333 // If this is a new LastFn instruction, bump the counter.
1334 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001335 ++Counter;
1336 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001337 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001338 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001339 O << Counter;
1340 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001341 std::string msg;
1342 raw_string_ostream Msg(msg);
1343 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001344 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001345 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001346 }
1347}
1348
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001349/// processDebugLoc - Processes the debug information of each machine
1350/// instruction's DebugLoc.
1351void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001352 if (!MAI || !DW)
Chris Lattnerd2503292009-08-05 04:09:18 +00001353 return;
1354
Chris Lattner33adcfb2009-08-22 21:43:10 +00001355 if (MAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001356 if (!DL.isUnknown()) {
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001357 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1358
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001359 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) {
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001360 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1361 DICompileUnit(CurDLT.CompileUnit)));
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001362 O << '\n';
1363 }
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001364
1365 PrevDLT = CurDLT;
1366 }
1367 }
1368}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001369
Chris Lattner0264d1a2006-01-27 02:10:10 +00001370/// printInlineAsm - This method formats and prints the specified machine
1371/// instruction that is an inline asm.
1372void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001373 unsigned NumOperands = MI->getNumOperands();
1374
1375 // Count the number of register definitions.
1376 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001377 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001378 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001379 assert(NumDefs != NumOperands-1 && "No asm string?");
1380
Dan Gohmand735b802008-10-03 15:45:36 +00001381 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001382
1383 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001384 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001385
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001386 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1387 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001388 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001389 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1390 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001391 return;
1392 }
1393
Chris Lattner33adcfb2009-08-22 21:43:10 +00001394 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001395
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001396 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001397 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001398
Chris Lattner66099132006-02-01 22:41:11 +00001399 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1400 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001401
Chris Lattner66099132006-02-01 22:41:11 +00001402 while (*LastEmitted) {
1403 switch (*LastEmitted) {
1404 default: {
1405 // Not a special case, emit the string section literally.
1406 const char *LiteralEnd = LastEmitted+1;
1407 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001408 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001409 ++LiteralEnd;
1410 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1411 O.write(LastEmitted, LiteralEnd-LastEmitted);
1412 LastEmitted = LiteralEnd;
1413 break;
1414 }
Chris Lattner1c059972006-05-05 21:47:05 +00001415 case '\n':
1416 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001417 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001418 break;
Chris Lattner66099132006-02-01 22:41:11 +00001419 case '$': {
1420 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001421 bool Done = true;
1422
1423 // Handle escapes.
1424 switch (*LastEmitted) {
1425 default: Done = false; break;
1426 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001427 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1428 O << '$';
1429 ++LastEmitted; // Consume second '$' character.
1430 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001431 case '(': // $( -> same as GCC's { character.
1432 ++LastEmitted; // Consume '(' character.
1433 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001434 llvm_report_error("Nested variants found in inline asm string: '"
1435 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001436 }
1437 CurVariant = 0; // We're in the first variant now.
1438 break;
1439 case '|':
1440 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001441 if (CurVariant == -1)
1442 O << '|'; // this is gcc's behavior for | outside a variant
1443 else
1444 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001445 break;
1446 case ')': // $) -> same as GCC's } char.
1447 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001448 if (CurVariant == -1)
1449 O << '}'; // this is gcc's behavior for } outside a variant
1450 else
1451 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001452 break;
Chris Lattner66099132006-02-01 22:41:11 +00001453 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001454 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001455
1456 bool HasCurlyBraces = false;
1457 if (*LastEmitted == '{') { // ${variable}
1458 ++LastEmitted; // Consume '{' character.
1459 HasCurlyBraces = true;
1460 }
1461
Chris Lattner3e0cc262009-03-10 05:37:13 +00001462 // If we have ${:foo}, then this is not a real operand reference, it is a
1463 // "magic" string reference, just like in .td files. Arrange to call
1464 // PrintSpecial.
1465 if (HasCurlyBraces && *LastEmitted == ':') {
1466 ++LastEmitted;
1467 const char *StrStart = LastEmitted;
1468 const char *StrEnd = strchr(StrStart, '}');
1469 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001470 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1471 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001472 }
1473
1474 std::string Val(StrStart, StrEnd);
1475 PrintSpecial(MI, Val.c_str());
1476 LastEmitted = StrEnd+1;
1477 break;
1478 }
1479
Chris Lattner66099132006-02-01 22:41:11 +00001480 const char *IDStart = LastEmitted;
1481 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001482 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001483 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1484 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001485 llvm_report_error("Bad $ operand number in inline asm string: '"
1486 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001487 }
1488 LastEmitted = IDEnd;
1489
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001490 char Modifier[2] = { 0, 0 };
1491
Chris Lattner66099132006-02-01 22:41:11 +00001492 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001493 // If we have curly braces, check for a modifier character. This
1494 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1495 if (*LastEmitted == ':') {
1496 ++LastEmitted; // Consume ':' character.
1497 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001498 llvm_report_error("Bad ${:} expression in inline asm string: '"
1499 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001500 }
1501
1502 Modifier[0] = *LastEmitted;
1503 ++LastEmitted; // Consume modifier character.
1504 }
1505
Chris Lattner66099132006-02-01 22:41:11 +00001506 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001507 llvm_report_error("Bad ${} expression in inline asm string: '"
1508 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001509 }
1510 ++LastEmitted; // Consume '}' character.
1511 }
1512
1513 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001514 llvm_report_error("Invalid $ operand number in inline asm string: '"
1515 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001516 }
1517
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001518 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001519 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001520 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1521 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001522
1523 bool Error = false;
1524
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001525 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001526 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001527 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001528 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001529 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001530 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001531
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001532 if (OpNo >= MI->getNumOperands()) {
1533 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001534 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001535 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001536 ++OpNo; // Skip over the ID number.
1537
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001538 if (Modifier[0]=='l') // labels are target independent
Chris Lattner325d3dc2009-09-13 17:14:04 +00001539 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1540 ->getNumber())->print(O, MAI);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001541 else {
1542 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001543 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001544 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1545 Modifier[0] ? Modifier : 0);
1546 } else {
1547 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1548 Modifier[0] ? Modifier : 0);
1549 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001550 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001551 }
1552 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001553 std::string msg;
1554 raw_string_ostream Msg(msg);
1555 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001556 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001557 MI->print(Msg);
1558 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001559 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001560 }
Chris Lattner66099132006-02-01 22:41:11 +00001561 break;
1562 }
Chris Lattner66099132006-02-01 22:41:11 +00001563 }
1564 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001565 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001566}
1567
Evan Chengda47e6e2008-03-15 00:03:38 +00001568/// printImplicitDef - This method prints the specified machine instruction
1569/// that is an implicit def.
1570void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001571 if (!VerboseAsm) return;
1572 O.PadToColumn(MAI->getCommentColumn());
1573 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001574 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001575}
1576
Jim Laskey1ee29252007-01-26 14:34:52 +00001577/// printLabel - This method prints a local label used by debug and
1578/// exception handling tables.
1579void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001580 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001581}
1582
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001583void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001584 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001585}
1586
Chris Lattner66099132006-02-01 22:41:11 +00001587/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1588/// instruction, using the specified assembler variant. Targets should
1589/// overried this to format as appropriate.
1590bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001591 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001592 // Target doesn't support this yet!
1593 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001594}
Chris Lattnerdd260332006-02-24 20:21:58 +00001595
1596bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1597 unsigned AsmVariant,
1598 const char *ExtraCode) {
1599 // Target doesn't support this yet!
1600 return true;
1601}
Nate Begeman37efe672006-04-22 18:53:45 +00001602
Chris Lattner7cb384d2009-09-12 23:02:08 +00001603MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1604 SmallString<60> Name;
1605 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1606 << getFunctionNumber() << '_' << MBBID;
1607
1608 return OutContext.GetOrCreateSymbol(Name.str());
1609}
1610
1611
Chris Lattner70a54c02009-09-13 18:25:37 +00001612/// EmitBasicBlockStart - This method prints the label for the specified
1613/// MachineBasicBlock, an alignment (if present) and a comment describing
1614/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001615void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Chris Lattner70a54c02009-09-13 18:25:37 +00001616 if (unsigned Align = MBB->getAlignment())
1617 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001618
Chris Lattner7cb384d2009-09-12 23:02:08 +00001619 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner662316c2009-09-14 03:15:54 +00001620 O << ':';
Chris Lattner70a54c02009-09-13 18:25:37 +00001621
1622 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001623 if (const BasicBlock *BB = MBB->getBasicBlock())
1624 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001625 O.PadToColumn(MAI->getCommentColumn());
1626 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001627 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001628 }
David Greeneb71d1b22009-08-10 16:38:07 +00001629
Chris Lattner70a54c02009-09-13 18:25:37 +00001630 EmitComments(*MBB);
David Greeneb71d1b22009-08-10 16:38:07 +00001631 }
Nate Begeman37efe672006-04-22 18:53:45 +00001632}
Nate Begeman52a51e382006-08-12 21:29:52 +00001633
Evan Chengcc415862007-11-09 01:32:10 +00001634/// printPICJumpTableSetLabel - This method prints a set label for the
1635/// specified MachineBasicBlock for a jumptable entry.
1636void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1637 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001638 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001639 return;
1640
Chris Lattner33adcfb2009-08-22 21:43:10 +00001641 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001642 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001643 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001644 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001645 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001646}
Evan Chengd6594ae2006-09-12 21:00:35 +00001647
Evan Chengcc415862007-11-09 01:32:10 +00001648void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1649 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001650 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001651 return;
1652
Chris Lattner33adcfb2009-08-22 21:43:10 +00001653 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001654 << getFunctionNumber() << '_' << uid << '_' << uid2
1655 << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001656 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001657 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001658 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001659}
1660
Evan Chengd6594ae2006-09-12 21:00:35 +00001661/// printDataDirective - This method prints the asm directive for the
1662/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001663void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001664 const TargetData *TD = TM.getTargetData();
1665 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001666 case Type::FloatTyID: case Type::DoubleTyID:
1667 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1668 assert(0 && "Should have already output floating point constant.");
1669 default:
1670 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001671 case Type::IntegerTyID: {
1672 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1673 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001674 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001675 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001676 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001677 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001678 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001679 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001680 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001681 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001682 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001683 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001684 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001685 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001686 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001687 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001688 case Type::PointerTyID:
1689 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001690 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001691 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001692 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001693 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001694 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001695 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001696 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001697 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001698 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001699 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001700 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001701 }
1702}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001703
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001704void AsmPrinter::printVisibility(const std::string& Name,
1705 unsigned Visibility) const {
1706 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001707 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001708 O << Directive << Name << '\n';
1709 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001710 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001711 O << Directive << Name << '\n';
1712 }
1713}
Gordon Henriksenc317a602008-08-17 12:08:44 +00001714
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001715void AsmPrinter::printOffset(int64_t Offset) const {
1716 if (Offset > 0)
1717 O << '+' << Offset;
1718 else if (Offset < 0)
1719 O << Offset;
1720}
1721
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001722GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1723 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001724 return 0;
1725
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001726 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001727 if (GCPI != GCMetadataPrinters.end())
1728 return GCPI->second;
1729
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001730 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001731
1732 for (GCMetadataPrinterRegistry::iterator
1733 I = GCMetadataPrinterRegistry::begin(),
1734 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1735 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001736 GCMetadataPrinter *GMP = I->instantiate();
1737 GMP->S = S;
1738 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1739 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001740 }
1741
Chris Lattner103289e2009-08-23 07:19:13 +00001742 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001743 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001744}
David Greene014700c2009-07-13 20:25:48 +00001745
1746/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001747void AsmPrinter::EmitComments(const MachineInstr &MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001748 assert(VerboseAsm && !MI.getDebugLoc().isUnknown());
Chris Lattner5f51cd02009-08-07 23:42:01 +00001749
1750 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene3ac1ab82009-07-16 22:24:20 +00001751
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001752 // Print source line info.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001753 O.PadToColumn(MAI->getCommentColumn());
1754 O << MAI->getCommentString() << " SrcLine ";
Devang Patele4b27562009-08-28 23:24:31 +00001755 if (DLT.CompileUnit) {
1756 std::string Str;
1757 DICompileUnit CU(DLT.CompileUnit);
1758 O << CU.getFilename(Str) << " ";
David Greene3ac1ab82009-07-16 22:24:20 +00001759 }
Chris Lattner5f51cd02009-08-07 23:42:01 +00001760 O << DLT.Line;
1761 if (DLT.Col != 0)
1762 O << ":" << DLT.Col;
David Greene014700c2009-07-13 20:25:48 +00001763}
1764
David Greenefe37ab32009-08-18 19:22:55 +00001765/// PrintChildLoopComment - Print comments about child loops within
1766/// the loop for this basic block, with nesting.
1767///
1768static void PrintChildLoopComment(formatted_raw_ostream &O,
1769 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001770 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001771 int FunctionNumber) {
1772 // Add child loop information
1773 for(MachineLoop::iterator cl = loop->begin(),
1774 clend = loop->end();
1775 cl != clend;
1776 ++cl) {
1777 MachineBasicBlock *Header = (*cl)->getHeader();
1778 assert(Header && "No header for loop");
1779
1780 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001781 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001782
Chris Lattner33adcfb2009-08-22 21:43:10 +00001783 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001784 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001785 << " Child Loop BB" << FunctionNumber << "_"
1786 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1787
Chris Lattner33adcfb2009-08-22 21:43:10 +00001788 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001789 }
1790}
1791
David Greeneb71d1b22009-08-10 16:38:07 +00001792/// EmitComments - Pretty-print comments for basic blocks
1793void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1794{
David Greenefe37ab32009-08-18 19:22:55 +00001795 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00001796 // Add loop depth information
1797 const MachineLoop *loop = LI->getLoopFor(&MBB);
1798
1799 if (loop) {
1800 // Print a newline after bb# annotation.
1801 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00001802 O.PadToColumn(MAI->getCommentColumn());
1803 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00001804 << '\n';
1805
Chris Lattner33adcfb2009-08-22 21:43:10 +00001806 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001807
1808 MachineBasicBlock *Header = loop->getHeader();
1809 assert(Header && "No header for loop");
1810
1811 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001812 O << MAI->getCommentString() << " Loop Header";
1813 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00001814 }
1815 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001816 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00001817 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1818 }
1819
1820 if (loop->empty()) {
1821 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001822 O.PadToColumn(MAI->getCommentColumn());
1823 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00001824 }
1825
1826 // Add parent loop information
1827 for (const MachineLoop *CurLoop = loop->getParentLoop();
1828 CurLoop;
1829 CurLoop = CurLoop->getParentLoop()) {
1830 MachineBasicBlock *Header = CurLoop->getHeader();
1831 assert(Header && "No header for loop");
1832
1833 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001834 O.PadToColumn(MAI->getCommentColumn());
1835 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001836 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001837 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00001838 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1839 }
1840 }
1841 }
1842}