blob: fca4b8008a909816a5cf1c32963f14f4108d0c87 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/Assembly/Writer.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Constants.h"
18#include "llvm/Module.h"
Gordon Henriksen1aed5992008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/MachineConstantPool.h"
David Greene066ed6a2009-08-18 19:22:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greenee52fd872009-08-10 16:38:07 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelfe359e72009-01-13 21:44:10 +000025#include "llvm/CodeGen/DwarfWriter.h"
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +000026#include "llvm/Analysis/DebugInfo.h"
Chris Lattner1eb0ad02009-07-27 21:28:04 +000027#include "llvm/MC/MCContext.h"
David Greene4a37a692009-07-16 22:24:20 +000028#include "llvm/MC/MCInst.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000029#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCStreamer.h"
Chris Lattner08c97082009-09-12 23:02:08 +000031#include "llvm/MC/MCSymbol.h"
Evan Cheng42ceb472009-03-25 01:47:28 +000032#include "llvm/Support/CommandLine.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000033#include "llvm/Support/ErrorHandling.h"
David Greene63486122009-07-13 20:25:48 +000034#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/Mangler.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Target/TargetData.h"
38#include "llvm/Target/TargetLowering.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng0eeed442008-07-01 23:18:29 +000040#include "llvm/Target/TargetOptions.h"
Evan Cheng3c0eda52008-03-15 00:03:38 +000041#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng6fb06762007-11-09 01:32:10 +000042#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner89b36582008-08-17 07:19:36 +000043#include "llvm/ADT/SmallString.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000044#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045#include <cerrno>
46using namespace llvm;
47
Evan Cheng42ceb472009-03-25 01:47:28 +000048static cl::opt<cl::boolOrDefault>
49AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
50 cl::init(cl::BOU_UNSET));
51
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052char AsmPrinter::ID = 0;
David Greene302008d2009-07-14 20:18:05 +000053AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +000054 const MCAsmInfo *T, bool VDef)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000055 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattnera5ef4d32009-08-22 21:43:10 +000056 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000057
58 OutContext(*new MCContext()),
Chris Lattner13a4af72009-08-17 04:23:44 +000059 OutStreamer(*createAsmStreamer(OutContext, O, *T, this)),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000060
Chris Lattnerebd055c2009-08-03 23:20:21 +000061 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersonc82bd822009-06-25 16:55:32 +000062 PrevDLT(0, ~0U, ~0U) {
Chris Lattner2424eac2009-06-24 19:09:55 +000063 DW = 0; MMI = 0;
Evan Cheng42ceb472009-03-25 01:47:28 +000064 switch (AsmVerbose) {
65 case cl::BOU_UNSET: VerboseAsm = VDef; break;
66 case cl::BOU_TRUE: VerboseAsm = true; break;
67 case cl::BOU_FALSE: VerboseAsm = false; break;
68 }
69}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000071AsmPrinter::~AsmPrinter() {
72 for (gcp_iterator I = GCMetadataPrinters.begin(),
73 E = GCMetadataPrinters.end(); I != E; ++I)
74 delete I->second;
Chris Lattner1eb0ad02009-07-27 21:28:04 +000075
76 delete &OutStreamer;
77 delete &OutContext;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000078}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079
Chris Lattner75bdd292009-08-03 19:12:26 +000080TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerc4c40a92009-07-28 03:13:23 +000081 return TM.getTargetLowering()->getObjFileLowering();
82}
83
Chris Lattnerd69d8ad2009-08-18 06:15:16 +000084/// getCurrentSection() - Return the current section we are emitting to.
85const MCSection *AsmPrinter::getCurrentSection() const {
86 return OutStreamer.getCurrentSection();
87}
88
89
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000090void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +000091 AU.setPreservesAll();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000092 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen1aed5992008-08-17 18:44:35 +000093 AU.addRequired<GCModuleInfo>();
David Greene066ed6a2009-08-18 19:22:55 +000094 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +000095 AU.addRequired<MachineLoopInfo>();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000096}
97
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner01316272009-07-31 17:42:42 +000099 // Initialize TargetLoweringObjectFile.
100 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
101 .Initialize(OutContext, TM);
102
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000103 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
104 MAI->getLinkerPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000106 if (MAI->doesAllowQuotesInName())
Chris Lattner690b02c2009-06-18 23:41:35 +0000107 Mang->setUseQuotes(true);
108
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000109 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000110 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000111
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000112 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000113 /* Very minimal debug info. It is ignored if we emit actual
114 debug info. If we don't, this at helps the user find where
115 a function came from. */
116 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
117 }
118
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000119 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
120 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000121 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000122
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 if (!M.getModuleInlineAsm().empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000124 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 << M.getModuleInlineAsm()
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000126 << '\n' << MAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 << " End of file scope inline assembly\n";
128
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000129 if (MAI->doesSupportDebugInformation() ||
130 MAI->doesSupportExceptionHandling()) {
Chris Lattner2424eac2009-06-24 19:09:55 +0000131 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
132 if (MMI)
Devang Patel1a454842009-06-19 21:54:26 +0000133 MMI->AnalyzeModule(M);
Chris Lattner2424eac2009-06-24 19:09:55 +0000134 DW = getAnalysisIfAvailable<DwarfWriter>();
135 if (DW)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000136 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel1a454842009-06-19 21:54:26 +0000137 }
138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 return false;
140}
141
142bool AsmPrinter::doFinalization(Module &M) {
Chris Lattnerae982212009-07-21 18:38:57 +0000143 // Emit global variables.
144 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
145 I != E; ++I)
146 PrintGlobalVariable(I);
147
Chris Lattnere1225cc2009-06-24 18:54:37 +0000148 // Emit final debug information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000149 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattnere1225cc2009-06-24 18:54:37 +0000150 DW->EndModule();
151
Chris Lattnerdb191f02009-06-24 18:52:01 +0000152 // If the target wants to know about weak references, print them all.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000153 if (MAI->getWeakRefDirective()) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000154 // FIXME: This is not lazy, it would be nice to only print weak references
155 // to stuff that is actually used. Note that doing so would require targets
156 // to notice uses in operands (due to constant exprs etc). This should
157 // happen with the MC stuff eventually.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Chris Lattnerdb191f02009-06-24 18:52:01 +0000159 // Print out module-level global variables here.
160 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
161 I != E; ++I) {
162 if (I->hasExternalWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000163 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000164 }
165
Chris Lattner0f98c332009-08-03 23:10:34 +0000166 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000167 if (I->hasExternalWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000168 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000169 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 }
171
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000172 if (MAI->getSetDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000173 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattnerdb191f02009-06-24 18:52:01 +0000175 I != E; ++I) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000176 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000177
178 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000179 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000180
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000181 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000182 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 else if (I->hasWeakLinkage())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000184 O << MAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000185 else if (!I->hasLocalLinkage())
Edwin Törökbd448e32009-07-14 16:55:14 +0000186 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000187
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000188 printVisibility(Name, I->getVisibility());
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000189
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000190 O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 }
192 }
193
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000194 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000195 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
196 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
197 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000198 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000199
Dan Gohmana65530a2008-05-05 00:28:39 +0000200 // If we don't have any trampolines, then we don't require stack memory
201 // to be executable. Some targets have a directive to declare this.
Chris Lattnerdb191f02009-06-24 18:52:01 +0000202 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana65530a2008-05-05 00:28:39 +0000203 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000204 if (MAI->getNonexecutableStackDirective())
205 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 delete Mang; Mang = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +0000208 DW = 0; MMI = 0;
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000209
210 OutStreamer.Finish();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 return false;
212}
213
Chris Lattner5c3475e2009-07-17 20:46:40 +0000214std::string
215AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendlingef9211a2007-09-18 01:47:22 +0000216 assert(MF && "No machine function?");
Chris Lattner5c3475e2009-07-17 20:46:40 +0000217 return Mang->getMangledName(MF->getFunction(), ".eh",
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000218 MAI->is_EHSymbolPrivate());
Bill Wendlingef9211a2007-09-18 01:47:22 +0000219}
220
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
222 // What's my mangled name?
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000223 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000224 IncrementFunctionNumber();
David Greenee52fd872009-08-10 16:38:07 +0000225
David Greene066ed6a2009-08-18 19:22:55 +0000226 if (VerboseAsm) {
David Greenee52fd872009-08-10 16:38:07 +0000227 LI = &getAnalysis<MachineLoopInfo>();
228 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229}
230
Evan Cheng68c18682009-03-13 07:51:59 +0000231namespace {
232 // SectionCPs - Keep track the alignment, constpool entries per Section.
233 struct SectionCPs {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000234 const MCSection *S;
Evan Cheng68c18682009-03-13 07:51:59 +0000235 unsigned Alignment;
236 SmallVector<unsigned, 4> CPEs;
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000237 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng68c18682009-03-13 07:51:59 +0000238 };
239}
240
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241/// EmitConstantPool - Print to the current output stream assembly
242/// representations of the constants in the constant pool MCP. This is
243/// used to print out constants which have been "spilled to memory" by
244/// the code generator.
245///
246void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
247 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
248 if (CP.empty()) return;
249
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000250 // Calculate sections for constant pool entries. We collect entries to go into
251 // the same section together to reduce amount of section switch statements.
Evan Cheng68c18682009-03-13 07:51:59 +0000252 SmallVector<SectionCPs, 4> CPSections;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner680c6f62009-07-22 00:28:43 +0000254 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng68c18682009-03-13 07:51:59 +0000255 unsigned Align = CPE.getAlignment();
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000256
257 SectionKind Kind;
258 switch (CPE.getRelocationInfo()) {
259 default: llvm_unreachable("Unknown section kind");
Chris Lattnera9453412009-08-01 23:57:16 +0000260 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000261 case 1:
Chris Lattnera9453412009-08-01 23:57:16 +0000262 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnered0c6762009-07-26 07:00:12 +0000263 break;
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000264 case 0:
Chris Lattnered0c6762009-07-26 07:00:12 +0000265 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnera9453412009-08-01 23:57:16 +0000266 case 4: Kind = SectionKind::getMergeableConst4(); break;
267 case 8: Kind = SectionKind::getMergeableConst8(); break;
268 case 16: Kind = SectionKind::getMergeableConst16();break;
269 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000270 }
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000271 }
272
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000273 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner680c6f62009-07-22 00:28:43 +0000274
Evan Cheng68c18682009-03-13 07:51:59 +0000275 // The number of sections are small, just do a linear search from the
276 // last section to the first.
277 bool Found = false;
278 unsigned SecIdx = CPSections.size();
279 while (SecIdx != 0) {
280 if (CPSections[--SecIdx].S == S) {
281 Found = true;
282 break;
283 }
284 }
285 if (!Found) {
286 SecIdx = CPSections.size();
287 CPSections.push_back(SectionCPs(S, Align));
288 }
289
290 if (Align > CPSections[SecIdx].Alignment)
291 CPSections[SecIdx].Alignment = Align;
292 CPSections[SecIdx].CPEs.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 }
294
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000295 // Now print stuff into the calculated sections.
Evan Cheng68c18682009-03-13 07:51:59 +0000296 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000297 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng68c18682009-03-13 07:51:59 +0000298 EmitAlignment(Log2_32(CPSections[i].Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299
Evan Cheng68c18682009-03-13 07:51:59 +0000300 unsigned Offset = 0;
301 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
302 unsigned CPI = CPSections[i].CPEs[j];
303 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000304
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 // Emit inter-object padding for alignment.
Evan Cheng68c18682009-03-13 07:51:59 +0000306 unsigned AlignMask = CPE.getAlignment() - 1;
307 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
308 EmitZeros(NewOffset - Offset);
309
310 const Type *Ty = CPE.getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000311 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng68c18682009-03-13 07:51:59 +0000312
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000313 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohman87581eb2009-08-12 18:32:22 +0000314 << CPI << ':';
Evan Cheng68c18682009-03-13 07:51:59 +0000315 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000316 O.PadToColumn(MAI->getCommentColumn());
317 O << MAI->getCommentString() << " constant ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000318 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000319 }
Evan Cheng68c18682009-03-13 07:51:59 +0000320 O << '\n';
321 if (CPE.isMachineConstantPoolEntry())
322 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
323 else
324 EmitGlobalConstant(CPE.Val.ConstVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 }
326 }
327}
328
329/// EmitJumpTableInfo - Print assembly representations of the jump tables used
330/// by the current function to the current output stream.
331///
332void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
333 MachineFunction &MF) {
334 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
335 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000336
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 // Pick the directive to use to print the jump table entries, and switch to
340 // the appropriate section.
341 TargetLowering *LoweringInfo = TM.getTargetLowering();
342
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000343 const Function *F = MF.getFunction();
Evan Chengccca6f72009-06-18 20:37:15 +0000344 bool JTInDiffSection = false;
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000345 if (F->isWeakForLinker() ||
346 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 // In PIC mode, we need to emit the jump table to the same section as the
348 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000349 // We should also do if the section name is NULL or function is declared in
350 // discardable section.
Chris Lattner73266f92009-08-19 05:49:37 +0000351 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
352 TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 } else {
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000354 // Otherwise, drop it in the readonly section.
355 const MCSection *ReadOnlySection =
Chris Lattnera9453412009-08-01 23:57:16 +0000356 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner73266f92009-08-19 05:49:37 +0000357 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengccca6f72009-06-18 20:37:15 +0000358 JTInDiffSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 }
360
361 EmitAlignment(Log2_32(MJTI->getAlignment()));
362
363 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
364 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
365
366 // If this jump table was deleted, ignore it.
367 if (JTBBs.empty()) continue;
368
369 // For PIC codegen, if possible we want to use the SetDirective to reduce
370 // the number of relocations the assembler will generate for the jump table.
371 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000372 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000373 if (MAI->getSetDirective() && IsPic)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000375 if (EmittedSets.insert(JTBBs[ii]))
376 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377
Chris Lattner149495f2009-09-13 19:02:16 +0000378 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 // before each jump table. The first label is never referenced, but tells
380 // the assembler and linker the extents of the jump table object. The
381 // second label is actually referenced by the code.
Chris Lattner149495f2009-09-13 19:02:16 +0000382 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
383 O << MAI->getLinkerPrivateGlobalPrefix()
384 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengccca6f72009-06-18 20:37:15 +0000385 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000387 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +0000388 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389
390 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000391 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 O << '\n';
393 }
394 }
395}
396
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000397void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
398 const MachineBasicBlock *MBB,
399 unsigned uid) const {
Chris Lattner3fb451e2009-08-11 20:29:57 +0000400 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000401
402 // Use JumpTableDirective otherwise honor the entry size from the jump table
403 // info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000404 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000405 bool HadJTEntryDirective = JTEntryDirective != NULL;
406 if (!HadJTEntryDirective) {
407 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000408 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000409 }
410
411 O << JTEntryDirective << ' ';
412
413 // If we have emitted set directives for the jump table entries, print
414 // them rather than the entries themselves. If we're emitting PIC, then
415 // emit the table entries as differences between two text section labels.
416 // If we're emitting non-PIC code, then emit the entries as direct
417 // references to the target basic blocks.
Chris Lattner3fb451e2009-08-11 20:29:57 +0000418 if (!isPIC) {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000419 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000420 } else if (MAI->getSetDirective()) {
421 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3fb451e2009-08-11 20:29:57 +0000422 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000423 } else {
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000424 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner3fb451e2009-08-11 20:29:57 +0000425 // If the arch uses custom Jump Table directives, don't calc relative to
426 // JT
427 if (!HadJTEntryDirective)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000428 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner3fb451e2009-08-11 20:29:57 +0000429 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000430 }
431}
432
433
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
435/// special global used by LLVM. If so, emit it and return true, otherwise
436/// do nothing and return false.
437bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000438 if (GV->getName() == "llvm.used") {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000439 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000440 EmitLLVMUsedList(GV->getInitializer());
441 return true;
442 }
443
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000444 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner68433442009-04-13 05:44:34 +0000445 if (GV->getSection() == "llvm.metadata" ||
446 GV->hasAvailableExternallyLinkage())
447 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448
449 if (!GV->hasAppendingLinkage()) return false;
450
451 assert(GV->hasInitializer() && "Not a special LLVM global!");
452
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 const TargetData *TD = TM.getTargetData();
454 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner0c433e92009-03-09 05:52:15 +0000455 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000456 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000457 EmitAlignment(Align, 0);
458 EmitXXStructorList(GV->getInitializer());
459 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 }
461
Chris Lattner0c433e92009-03-09 05:52:15 +0000462 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000463 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000464 EmitAlignment(Align, 0);
465 EmitXXStructorList(GV->getInitializer());
466 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 }
468
469 return false;
470}
471
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000472/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000473/// global in the specified llvm.used list for which emitUsedDirectiveFor
474/// is true, as being used with this directive.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000476 const char *Directive = MAI->getUsedDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477
Dan Gohman9e1657f2009-06-14 23:30:43 +0000478 // Should be an array of 'i8*'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
480 if (InitList == 0) return;
481
482 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner3f621b32009-07-17 22:00:23 +0000483 const GlobalValue *GV =
484 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner84362ac2009-07-31 20:52:39 +0000485 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000486 O << Directive;
487 EmitConstantValueOnly(InitList->getOperand(i));
488 O << '\n';
489 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 }
491}
492
493/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
494/// function pointers, ignoring the init priority.
495void AsmPrinter::EmitXXStructorList(Constant *List) {
496 // Should be an array of '{ int, void ()* }' structs. The first value is the
497 // init priority, which we ignore.
498 if (!isa<ConstantArray>(List)) return;
499 ConstantArray *InitList = cast<ConstantArray>(List);
500 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
501 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
502 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
503
504 if (CS->getOperand(1)->isNullValue())
505 return; // Found a null terminator, exit printing.
506 // Emit the function pointer.
507 EmitGlobalConstant(CS->getOperand(1));
508 }
509}
510
511/// getGlobalLinkName - Returns the asm/link name of of the specified
512/// global variable. Should be overridden by each target asm printer to
513/// generate the appropriate value.
Bill Wendling26a8ab92009-04-10 00:12:49 +0000514const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
515 std::string &LinkName) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 if (isa<Function>(GV)) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000517 LinkName += MAI->getFunctionAddrPrefix();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000518 LinkName += Mang->getMangledName(GV);
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000519 LinkName += MAI->getFunctionAddrSuffix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000521 LinkName += MAI->getGlobalVarAddrPrefix();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000522 LinkName += Mang->getMangledName(GV);
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000523 LinkName += MAI->getGlobalVarAddrSuffix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 }
525
526 return LinkName;
527}
528
529/// EmitExternalGlobal - Emit the external reference to a global variable.
530/// Should be overridden if an indirect reference should be used.
531void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000532 std::string GLN;
533 O << getGlobalLinkName(GV, GLN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534}
535
536
537
538//===----------------------------------------------------------------------===//
539/// LEB 128 number encoding.
540
541/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
542/// representing an unsigned leb128 value.
543void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000544 char Buffer[20];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000546 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 Value >>= 7;
548 if (Value) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000549 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 if (Value) O << ", ";
551 } while (Value);
552}
553
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
555/// representing a signed leb128 value.
556void AsmPrinter::PrintSLEB128(int Value) const {
557 int Sign = Value >> (8 * sizeof(Value) - 1);
558 bool IsMore;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000559 char Buffer[20];
aslc200b112008-08-16 12:57:46 +0000560
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000562 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 Value >>= 7;
564 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
565 if (IsMore) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000566 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 if (IsMore) O << ", ";
568 } while (IsMore);
569}
570
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571//===--------------------------------------------------------------------===//
572// Emission and print routines
573//
574
575/// PrintHex - Print a value as a hexidecimal value.
576///
577void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerda2047e2008-11-10 04:30:26 +0000578 char Buffer[20];
Chris Lattnerda2047e2008-11-10 04:30:26 +0000579 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580}
581
582/// EOL - Print a newline character to asm stream. If a comment is present
583/// then it will be printed first. Comments should not contain '\n'.
584void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000585 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000587
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000589 if (VerboseAsm && !Comment.empty()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000590 O.PadToColumn(MAI->getCommentColumn());
591 O << MAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000592 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 << Comment;
594 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000595 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596}
597
Owen Anderson367bfbb2008-07-01 21:16:27 +0000598void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000599 if (VerboseAsm && *Comment) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000600 O.PadToColumn(MAI->getCommentColumn());
601 O << MAI->getCommentString()
Owen Anderson367bfbb2008-07-01 21:16:27 +0000602 << ' '
603 << Comment;
604 }
605 O << '\n';
606}
607
Bill Wendling946b5212009-08-29 12:17:53 +0000608static const char *DecodeDWARFEncoding(unsigned Encoding) {
609 switch (Encoding) {
610 case dwarf::DW_EH_PE_absptr:
611 return "absptr";
612 case dwarf::DW_EH_PE_omit:
613 return "omit";
614 case dwarf::DW_EH_PE_pcrel:
615 return "pcrel";
Bill Wendlingbe23fd42009-09-09 21:26:19 +0000616 case dwarf::DW_EH_PE_udata4:
617 return "udata4";
618 case dwarf::DW_EH_PE_udata8:
619 return "udata8";
620 case dwarf::DW_EH_PE_sdata4:
621 return "sdata4";
622 case dwarf::DW_EH_PE_sdata8:
623 return "sdata8";
Bill Wendling946b5212009-08-29 12:17:53 +0000624 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
625 return "pcrel udata4";
626 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
627 return "pcrel sdata4";
628 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
629 return "pcrel udata8";
630 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
631 return "pcrel sdata8";
632 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
633 return "indirect pcrel udata4";
634 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
635 return "indirect pcrel sdata4";
636 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
637 return "indirect pcrel udata8";
638 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
639 return "indirect pcrel sdata8";
640 }
641
642 return 0;
643}
644
Bill Wendling946b5212009-08-29 12:17:53 +0000645void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
646 if (VerboseAsm && *Comment) {
647 O.PadToColumn(MAI->getCommentColumn());
648 O << MAI->getCommentString()
649 << ' '
650 << Comment;
651
652 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
653 O << " (" << EncStr << ')';
654 }
655 O << '\n';
656}
657
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
659/// unsigned leb128 value.
660void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000661 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 O << "\t.uleb128\t"
663 << Value;
664 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000665 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 PrintULEB128(Value);
667 }
668}
669
670/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
671/// signed leb128 value.
672void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000673 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 O << "\t.sleb128\t"
675 << Value;
676 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000677 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 PrintSLEB128(Value);
679 }
680}
681
682/// EmitInt8 - Emit a byte directive and value.
683///
684void AsmPrinter::EmitInt8(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000685 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 PrintHex(Value & 0xFF);
687}
688
689/// EmitInt16 - Emit a short directive and value.
690///
691void AsmPrinter::EmitInt16(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000692 O << MAI->getData16bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 PrintHex(Value & 0xFFFF);
694}
695
696/// EmitInt32 - Emit a long directive and value.
697///
698void AsmPrinter::EmitInt32(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000699 O << MAI->getData32bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 PrintHex(Value);
701}
702
703/// EmitInt64 - Emit a long long directive and value.
704///
705void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000706 if (MAI->getData64bitsDirective()) {
707 O << MAI->getData64bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 PrintHex(Value);
709 } else {
710 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000711 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 EmitInt32(unsigned(Value));
713 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000714 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 EmitInt32(unsigned(Value >> 32));
716 }
717 }
718}
719
720/// toOctal - Convert the low order bits of X into an octal digit.
721///
722static inline char toOctal(int X) {
723 return (X&7)+'0';
724}
725
726/// printStringChar - Print a char, escaped if necessary.
727///
David Greene302008d2009-07-14 20:18:05 +0000728static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 if (C == '"') {
730 O << "\\\"";
731 } else if (C == '\\') {
732 O << "\\\\";
Chris Lattner07ec1462009-01-22 23:38:45 +0000733 } else if (isprint((unsigned char)C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 O << C;
735 } else {
736 switch(C) {
737 case '\b': O << "\\b"; break;
738 case '\f': O << "\\f"; break;
739 case '\n': O << "\\n"; break;
740 case '\r': O << "\\r"; break;
741 case '\t': O << "\\t"; break;
742 default:
743 O << '\\';
744 O << toOctal(C >> 6);
745 O << toOctal(C >> 3);
746 O << toOctal(C >> 0);
747 break;
748 }
749 }
750}
751
752/// EmitString - Emit a string with quotes and a null terminator.
753/// Special characters are emitted properly.
754/// \literal (Eg. '\t') \endliteral
755void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling3f94b412009-04-09 23:51:31 +0000756 EmitString(String.c_str(), String.size());
757}
758
759void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000760 const char* AscizDirective = MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 if (AscizDirective)
762 O << AscizDirective;
763 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000764 O << MAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000765 O << '\"';
Bill Wendling3f94b412009-04-09 23:51:31 +0000766 for (unsigned i = 0; i < Size; ++i)
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000767 printStringChar(O, String[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000769 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770 else
771 O << "\\0\"";
772}
773
774
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000775/// EmitFile - Emit a .file directive.
776void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
777 O << "\t.file\t" << Number << " \"";
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000778 for (unsigned i = 0, N = Name.size(); i < N; ++i)
779 printStringChar(O, Name[i]);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000780 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000781}
782
783
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784//===----------------------------------------------------------------------===//
785
786// EmitAlignment - Emit an alignment directive to the specified power of
787// two boundary. For example, if you pass in 3 here, you will get an 8
788// byte alignment. If a global value is specified, and if that global has
789// an explicit alignment requested, it will unconditionally override the
790// alignment request. However, if ForcedAlignBits is specified, this value
791// has final say: the ultimate alignment will be the max of ForcedAlignBits
792// and the alignment computed with NumBits and the global.
793//
794// The algorithm is:
795// Align = NumBits;
796// if (GV && GV->hasalignment) Align = GV->getalignment();
797// Align = std::max(Align, ForcedAlignBits);
798//
799void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000800 unsigned ForcedAlignBits,
801 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 if (GV && GV->getAlignment())
803 NumBits = Log2_32(GV->getAlignment());
804 NumBits = std::max(NumBits, ForcedAlignBits);
805
806 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner4891d142009-08-19 06:12:02 +0000807
808 unsigned FillValue = 0;
Chris Lattnerd69d8ad2009-08-18 06:15:16 +0000809 if (getCurrentSection()->getKind().isText())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000810 FillValue = MAI->getTextAlignFillValue();
Chris Lattner4891d142009-08-19 06:12:02 +0000811
812 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813}
David Greenecdc2de32009-08-05 21:00:52 +0000814
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815/// EmitZeros - Emit a block of zeros.
816///
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000817void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 if (NumZeros) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000819 if (MAI->getZeroDirective()) {
820 O << MAI->getZeroDirective() << NumZeros;
821 if (MAI->getZeroDirectiveSuffix())
822 O << MAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000823 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 } else {
825 for (; NumZeros; --NumZeros)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000826 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 }
828 }
829}
830
831// Print out the specified constant, without a storage class. Only the
832// constants valid in constant expressions can occur here.
833void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
834 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000835 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000837 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
839 // This is a constant address for a global variable or function. Use the
840 // name of the variable or function as the address value, possibly
841 // decorating it with GlobalVarAddrPrefix/Suffix or
842 // FunctionAddrPrefix/Suffix (these all default to "" )
843 if (isa<Function>(GV)) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000844 O << MAI->getFunctionAddrPrefix()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000845 << Mang->getMangledName(GV)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000846 << MAI->getFunctionAddrSuffix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000848 O << MAI->getGlobalVarAddrPrefix()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000849 << Mang->getMangledName(GV)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000850 << MAI->getGlobalVarAddrSuffix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 }
852 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
853 const TargetData *TD = TM.getTargetData();
854 unsigned Opcode = CE->getOpcode();
855 switch (Opcode) {
Chris Lattner34e4cde2009-08-01 22:25:12 +0000856 case Instruction::Trunc:
857 case Instruction::ZExt:
858 case Instruction::SExt:
859 case Instruction::FPTrunc:
860 case Instruction::FPExt:
861 case Instruction::UIToFP:
862 case Instruction::SIToFP:
863 case Instruction::FPToUI:
864 case Instruction::FPToSI:
865 llvm_unreachable("FIXME: Don't support this constant cast expr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 case Instruction::GetElementPtr: {
867 // generate a symbolic expression for the byte address
868 const Constant *ptrVal = CE->getOperand(0);
869 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
870 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
871 idxVec.size())) {
Chris Lattner911129a2009-02-05 06:55:21 +0000872 // Truncate/sext the offset to the pointer size.
873 if (TD->getPointerSizeInBits() != 64) {
874 int SExtAmount = 64-TD->getPointerSizeInBits();
875 Offset = (Offset << SExtAmount) >> SExtAmount;
876 }
877
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000879 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 EmitConstantValueOnly(ptrVal);
881 if (Offset > 0)
882 O << ") + " << Offset;
883 else if (Offset < 0)
884 O << ") - " << -Offset;
885 } else {
886 EmitConstantValueOnly(ptrVal);
887 }
888 break;
889 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 case Instruction::BitCast:
891 return EmitConstantValueOnly(CE->getOperand(0));
892
893 case Instruction::IntToPtr: {
894 // Handle casts to pointers by changing them into casts to the appropriate
895 // integer type. This promotes constant folding and simplifies this code.
896 Constant *Op = CE->getOperand(0);
Owen Anderson35b47072009-08-13 21:58:54 +0000897 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
898 false/*ZExt*/);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 return EmitConstantValueOnly(Op);
900 }
901
902
903 case Instruction::PtrToInt: {
904 // Support only foldable casts to/from pointers that can be eliminated by
905 // changing the pointer to the appropriately sized integer type.
906 Constant *Op = CE->getOperand(0);
907 const Type *Ty = CE->getType();
908
909 // We can emit the pointer value into this slot if the slot is an
910 // integer slot greater or equal to the size of the pointer.
Chris Lattner34e4cde2009-08-01 22:25:12 +0000911 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000913
914 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 EmitConstantValueOnly(Op);
Chris Lattner34e4cde2009-08-01 22:25:12 +0000916 APInt ptrMask =
917 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner89b36582008-08-17 07:19:36 +0000918
919 SmallString<40> S;
920 ptrMask.toStringUnsigned(S);
Daniel Dunbar768e97d2009-08-19 20:07:03 +0000921 O << ") & " << S.str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 break;
923 }
924 case Instruction::Add:
925 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000926 case Instruction::And:
927 case Instruction::Or:
928 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000929 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000931 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000932 switch (Opcode) {
933 case Instruction::Add:
934 O << " + ";
935 break;
936 case Instruction::Sub:
937 O << " - ";
938 break;
939 case Instruction::And:
940 O << " & ";
941 break;
942 case Instruction::Or:
943 O << " | ";
944 break;
945 case Instruction::Xor:
946 O << " ^ ";
947 break;
948 default:
949 break;
950 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000951 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000953 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 break;
955 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000956 llvm_unreachable("Unsupported operator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 }
958 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000959 llvm_unreachable("Unknown constant value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000960 }
961}
962
963/// printAsCString - Print the specified array as a C compatible string, only if
964/// the predicate isString is true.
965///
David Greene302008d2009-07-14 20:18:05 +0000966static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 unsigned LastElt) {
968 assert(CVA->isString() && "Array is not string compatible!");
969
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000970 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 for (unsigned i = 0; i != LastElt; ++i) {
972 unsigned char C =
973 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
974 printStringChar(O, C);
975 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000976 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977}
978
979/// EmitString - Emit a zero-byte-terminated string constant.
980///
981void AsmPrinter::EmitString(const ConstantArray *CVA) const {
982 unsigned NumElts = CVA->getNumOperands();
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000983 if (MAI->getAscizDirective() && NumElts &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000985 O << MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 printAsCString(O, CVA, NumElts-1);
987 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000988 O << MAI->getAsciiDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 printAsCString(O, CVA, NumElts);
990 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000991 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992}
993
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000994void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
995 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000996 if (CVA->isString()) {
997 EmitString(CVA);
998 } else { // Not a string. Print the values in successive locations
999 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +00001000 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001001 }
1002}
1003
1004void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
1005 const VectorType *PTy = CP->getType();
1006
1007 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
1008 EmitGlobalConstant(CP->getOperand(I));
1009}
1010
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001011void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1012 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001013 // Print the fields in successive locations. Pad to align if needed!
1014 const TargetData *TD = TM.getTargetData();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001015 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +00001016 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1017 uint64_t sizeSoFar = 0;
1018 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1019 const Constant* field = CVS->getOperand(i);
1020
1021 // Check if padding is needed and insert one or more 0s.
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001022 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +00001023 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1024 - cvsLayout->getElementOffset(i)) - fieldSize;
1025 sizeSoFar += fieldSize + padSize;
1026
1027 // Now print the actual field value.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001028 EmitGlobalConstant(field, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001029
1030 // Insert padding - this may include padding to increase the size of the
1031 // current field up to the ABI size (if the struct is not packed) as well
1032 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001033 EmitZeros(padSize, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001034 }
1035 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1036 "Layout of constant struct may be incorrect!");
1037}
1038
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001039void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1040 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001041 // FP Constants are printed as integer constants to avoid losing
1042 // precision...
Owen Anderson35b47072009-08-13 21:58:54 +00001043 LLVMContext &Context = CFP->getContext();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001044 const TargetData *TD = TM.getTargetData();
Owen Anderson35b47072009-08-13 21:58:54 +00001045 if (CFP->getType() == Type::getDoubleTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001046 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1047 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001048 if (MAI->getData64bitsDirective(AddrSpace)) {
1049 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohman87581eb2009-08-12 18:32:22 +00001050 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001051 O.PadToColumn(MAI->getCommentColumn());
1052 O << MAI->getCommentString() << " double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001053 }
Evan Cheng11db8142009-03-24 00:17:40 +00001054 O << '\n';
1055 } else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001056 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001057 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001058 O.PadToColumn(MAI->getCommentColumn());
1059 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001060 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001061 }
Evan Cheng11db8142009-03-24 00:17:40 +00001062 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001063 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001064 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001065 O.PadToColumn(MAI->getCommentColumn());
1066 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001067 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001068 }
Evan Cheng11db8142009-03-24 00:17:40 +00001069 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001070 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001071 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001072 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001073 O.PadToColumn(MAI->getCommentColumn());
1074 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001075 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001076 }
Evan Cheng11db8142009-03-24 00:17:40 +00001077 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001078 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001079 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001080 O.PadToColumn(MAI->getCommentColumn());
1081 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001082 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001083 }
Evan Cheng11db8142009-03-24 00:17:40 +00001084 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001085 }
1086 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001087 } else if (CFP->getType() == Type::getFloatTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001088 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001089 O << MAI->getData32bitsDirective(AddrSpace)
Evan Cheng11db8142009-03-24 00:17:40 +00001090 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman87581eb2009-08-12 18:32:22 +00001091 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001092 O.PadToColumn(MAI->getCommentColumn());
1093 O << MAI->getCommentString() << " float " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001094 }
Evan Cheng11db8142009-03-24 00:17:40 +00001095 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001096 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001097 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001098 // all long double variants are printed as hex
1099 // api needed to prevent premature destruction
1100 APInt api = CFP->getValueAPF().bitcastToAPInt();
1101 const uint64_t *p = api.getRawData();
1102 // Convert to double so we can print the approximate val as a comment.
1103 APFloat DoubleVal = CFP->getValueAPF();
1104 bool ignored;
1105 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1106 &ignored);
1107 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001108 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001109 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001110 O.PadToColumn(MAI->getCommentColumn());
1111 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001112 << " most significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001113 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001114 }
Evan Cheng11db8142009-03-24 00:17:40 +00001115 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001116 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001117 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001118 O.PadToColumn(MAI->getCommentColumn());
1119 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001120 }
Evan Cheng11db8142009-03-24 00:17:40 +00001121 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001122 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001123 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001124 O.PadToColumn(MAI->getCommentColumn());
1125 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001126 }
Evan Cheng11db8142009-03-24 00:17:40 +00001127 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001128 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001129 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001130 O.PadToColumn(MAI->getCommentColumn());
1131 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001132 }
Evan Cheng11db8142009-03-24 00:17:40 +00001133 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001134 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001135 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001136 O.PadToColumn(MAI->getCommentColumn());
1137 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001138 << " least significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001139 }
Evan Cheng11db8142009-03-24 00:17:40 +00001140 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001141 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001142 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001143 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001144 O.PadToColumn(MAI->getCommentColumn());
1145 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001146 << " least significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001147 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001148 }
Evan Cheng11db8142009-03-24 00:17:40 +00001149 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001150 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001151 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001152 O.PadToColumn(MAI->getCommentColumn());
1153 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001154 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001155 }
Evan Cheng11db8142009-03-24 00:17:40 +00001156 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001157 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001158 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001159 O.PadToColumn(MAI->getCommentColumn());
1160 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001161 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001162 }
Evan Cheng11db8142009-03-24 00:17:40 +00001163 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001164 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001165 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001166 O.PadToColumn(MAI->getCommentColumn());
1167 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001168 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001169 }
Evan Cheng11db8142009-03-24 00:17:40 +00001170 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001171 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001172 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001173 O.PadToColumn(MAI->getCommentColumn());
1174 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001175 << " most significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001176 }
Evan Cheng11db8142009-03-24 00:17:40 +00001177 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001178 }
Owen Anderson35b47072009-08-13 21:58:54 +00001179 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1180 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001181 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001182 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001183 // all long double variants are printed as hex
1184 // api needed to prevent premature destruction
1185 APInt api = CFP->getValueAPF().bitcastToAPInt();
1186 const uint64_t *p = api.getRawData();
1187 if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001188 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001189 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001190 O.PadToColumn(MAI->getCommentColumn());
1191 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001192 << " most significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001193 }
Evan Cheng11db8142009-03-24 00:17:40 +00001194 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001195 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001196 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001197 O.PadToColumn(MAI->getCommentColumn());
1198 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001199 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001200 }
Evan Cheng11db8142009-03-24 00:17:40 +00001201 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001202 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001203 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001204 O.PadToColumn(MAI->getCommentColumn());
1205 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001206 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001207 }
Evan Cheng11db8142009-03-24 00:17:40 +00001208 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001209 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001210 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001211 O.PadToColumn(MAI->getCommentColumn());
1212 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001213 << " least significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001214 }
Evan Cheng11db8142009-03-24 00:17:40 +00001215 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001216 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001217 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001218 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001219 O.PadToColumn(MAI->getCommentColumn());
1220 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001221 << " least significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001222 }
Evan Cheng11db8142009-03-24 00:17:40 +00001223 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001224 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001225 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001226 O.PadToColumn(MAI->getCommentColumn());
1227 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001228 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001229 }
Evan Cheng11db8142009-03-24 00:17:40 +00001230 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001231 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001232 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001233 O.PadToColumn(MAI->getCommentColumn());
1234 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001235 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001236 }
Evan Cheng11db8142009-03-24 00:17:40 +00001237 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001238 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001239 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001240 O.PadToColumn(MAI->getCommentColumn());
1241 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001242 << " most significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001243 }
Evan Cheng11db8142009-03-24 00:17:40 +00001244 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001245 }
1246 return;
Edwin Törökbd448e32009-07-14 16:55:14 +00001247 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001248}
1249
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001250void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1251 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001252 const TargetData *TD = TM.getTargetData();
1253 unsigned BitWidth = CI->getBitWidth();
1254 assert(isPowerOf2_32(BitWidth) &&
1255 "Non-power-of-2-sized integers not handled!");
1256
1257 // We don't expect assemblers to support integer data directives
1258 // for more than 64 bits, so we emit the data in at most 64-bit
1259 // quantities at a time.
1260 const uint64_t *RawData = CI->getValue().getRawData();
1261 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1262 uint64_t Val;
1263 if (TD->isBigEndian())
1264 Val = RawData[e - i - 1];
1265 else
1266 Val = RawData[i];
1267
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001268 if (MAI->getData64bitsDirective(AddrSpace))
1269 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001270 else if (TD->isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001271 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001272 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001273 O.PadToColumn(MAI->getCommentColumn());
1274 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001275 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001276 }
Evan Cheng11db8142009-03-24 00:17:40 +00001277 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001278 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001279 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001280 O.PadToColumn(MAI->getCommentColumn());
1281 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001282 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001283 }
Evan Cheng11db8142009-03-24 00:17:40 +00001284 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001285 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001286 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001287 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001288 O.PadToColumn(MAI->getCommentColumn());
1289 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001290 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001291 }
Evan Cheng11db8142009-03-24 00:17:40 +00001292 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001293 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001294 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001295 O.PadToColumn(MAI->getCommentColumn());
1296 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001297 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001298 }
Evan Cheng11db8142009-03-24 00:17:40 +00001299 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001300 }
1301 }
1302}
1303
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001305void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 const TargetData *TD = TM.getTargetData();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001307 const Type *type = CV->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001308 unsigned Size = TD->getTypeAllocSize(type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001309
1310 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001311 EmitZeros(Size, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 return;
1313 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +00001314 EmitGlobalConstantArray(CVA , AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 return;
1316 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001317 EmitGlobalConstantStruct(CVS, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318 return;
1319 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001320 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001321 return;
1322 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1323 // Small integers are handled below; large integers are handled here.
1324 if (Size > 4) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001325 EmitGlobalConstantLargeInt(CI, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326 return;
1327 }
1328 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001329 EmitGlobalConstantVector(CP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 return;
1331 }
1332
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001333 printDataDirective(type, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334 EmitConstantValueOnly(CV);
Evan Cheng11db8142009-03-24 00:17:40 +00001335 if (VerboseAsm) {
1336 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1337 SmallString<40> S;
1338 CI->getValue().toStringUnsigned(S, 16);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001339 O.PadToColumn(MAI->getCommentColumn());
1340 O << MAI->getCommentString() << " 0x" << S.str();
Evan Cheng11db8142009-03-24 00:17:40 +00001341 }
Scott Michele067c3c2008-06-03 15:39:51 +00001342 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001343 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344}
1345
Chris Lattner89b36582008-08-17 07:19:36 +00001346void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 // Target doesn't support this yet!
Edwin Törökbd448e32009-07-14 16:55:14 +00001348 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349}
1350
1351/// PrintSpecial - Print information related to the specified machine instr
1352/// that is independent of the operand, and may be independent of the instr
1353/// itself. This can be useful for portably encoding the comment character
1354/// or other bits of target-specific knowledge into the asmstrings. The
1355/// syntax used is ${:comment}. Targets can override this to add support
1356/// for their own strange codes.
Chris Lattner6af48032009-03-10 05:37:13 +00001357void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358 if (!strcmp(Code, "private")) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001359 O << MAI->getPrivateGlobalPrefix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 } else if (!strcmp(Code, "comment")) {
Evan Cheng11db8142009-03-24 00:17:40 +00001361 if (VerboseAsm)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001362 O << MAI->getCommentString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 } else if (!strcmp(Code, "uid")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001364 // Comparing the address of MI isn't sufficient, because machineinstrs may
1365 // be allocated to the same address across functions.
1366 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1367
Owen Andersonda3388b2009-06-24 22:28:12 +00001368 // If this is a new LastFn instruction, bump the counter.
1369 if (LastMI != MI || LastFn != ThisF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 ++Counter;
1371 LastMI = MI;
Owen Andersonda3388b2009-06-24 22:28:12 +00001372 LastFn = ThisF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 }
1374 O << Counter;
1375 } else {
Edwin Törökced9ff82009-07-11 13:10:19 +00001376 std::string msg;
1377 raw_string_ostream Msg(msg);
1378 Msg << "Unknown special formatter '" << Code
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 << "' for machine instr: " << *MI;
Edwin Törökced9ff82009-07-11 13:10:19 +00001380 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381 }
1382}
1383
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001384/// processDebugLoc - Processes the debug information of each machine
1385/// instruction's DebugLoc.
1386void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001387 if (!MAI || !DW)
Chris Lattner693cfcc2009-08-05 04:09:18 +00001388 return;
1389
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001390 if (MAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001391 if (!DL.isUnknown()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001392 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1393
Chris Lattner32d4cc72009-09-09 23:14:36 +00001394 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001395 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1396 DICompileUnit(CurDLT.CompileUnit)));
Chris Lattner32d4cc72009-09-09 23:14:36 +00001397 O << '\n';
1398 }
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001399
1400 PrevDLT = CurDLT;
1401 }
1402 }
1403}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001404
1405/// printInlineAsm - This method formats and prints the specified machine
1406/// instruction that is an inline asm.
1407void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1408 unsigned NumOperands = MI->getNumOperands();
1409
1410 // Count the number of register definitions.
1411 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001412 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 ++NumDefs)
1414 assert(NumDefs != NumOperands-1 && "No asm string?");
1415
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001416 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417
1418 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1419 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1420
Dale Johannesene99fc902008-01-29 02:21:21 +00001421 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1422 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001423 if (AsmStr[0] == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001424 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1425 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001426 return;
1427 }
1428
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001429 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430
1431 // The variant of the current asmprinter.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001432 int AsmPrinterVariant = MAI->getAssemblerDialect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433
1434 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1435 const char *LastEmitted = AsmStr; // One past the last character emitted.
1436
1437 while (*LastEmitted) {
1438 switch (*LastEmitted) {
1439 default: {
1440 // Not a special case, emit the string section literally.
1441 const char *LiteralEnd = LastEmitted+1;
1442 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1443 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1444 ++LiteralEnd;
1445 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1446 O.write(LastEmitted, LiteralEnd-LastEmitted);
1447 LastEmitted = LiteralEnd;
1448 break;
1449 }
1450 case '\n':
1451 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001452 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 break;
1454 case '$': {
1455 ++LastEmitted; // Consume '$' character.
1456 bool Done = true;
1457
1458 // Handle escapes.
1459 switch (*LastEmitted) {
1460 default: Done = false; break;
1461 case '$': // $$ -> $
1462 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1463 O << '$';
1464 ++LastEmitted; // Consume second '$' character.
1465 break;
1466 case '(': // $( -> same as GCC's { character.
1467 ++LastEmitted; // Consume '(' character.
1468 if (CurVariant != -1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001469 llvm_report_error("Nested variants found in inline asm string: '"
1470 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 }
1472 CurVariant = 0; // We're in the first variant now.
1473 break;
1474 case '|':
1475 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001476 if (CurVariant == -1)
1477 O << '|'; // this is gcc's behavior for | outside a variant
1478 else
1479 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001480 break;
1481 case ')': // $) -> same as GCC's } char.
1482 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001483 if (CurVariant == -1)
1484 O << '}'; // this is gcc's behavior for } outside a variant
1485 else
1486 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 break;
1488 }
1489 if (Done) break;
1490
1491 bool HasCurlyBraces = false;
1492 if (*LastEmitted == '{') { // ${variable}
1493 ++LastEmitted; // Consume '{' character.
1494 HasCurlyBraces = true;
1495 }
1496
Chris Lattner6af48032009-03-10 05:37:13 +00001497 // If we have ${:foo}, then this is not a real operand reference, it is a
1498 // "magic" string reference, just like in .td files. Arrange to call
1499 // PrintSpecial.
1500 if (HasCurlyBraces && *LastEmitted == ':') {
1501 ++LastEmitted;
1502 const char *StrStart = LastEmitted;
1503 const char *StrEnd = strchr(StrStart, '}');
1504 if (StrEnd == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001505 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1506 + std::string(AsmStr) + "'");
Chris Lattner6af48032009-03-10 05:37:13 +00001507 }
1508
1509 std::string Val(StrStart, StrEnd);
1510 PrintSpecial(MI, Val.c_str());
1511 LastEmitted = StrEnd+1;
1512 break;
1513 }
1514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 const char *IDStart = LastEmitted;
1516 char *IDEnd;
1517 errno = 0;
1518 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1519 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001520 llvm_report_error("Bad $ operand number in inline asm string: '"
1521 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001522 }
1523 LastEmitted = IDEnd;
1524
1525 char Modifier[2] = { 0, 0 };
1526
1527 if (HasCurlyBraces) {
1528 // If we have curly braces, check for a modifier character. This
1529 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1530 if (*LastEmitted == ':') {
1531 ++LastEmitted; // Consume ':' character.
1532 if (*LastEmitted == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001533 llvm_report_error("Bad ${:} expression in inline asm string: '"
1534 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001535 }
1536
1537 Modifier[0] = *LastEmitted;
1538 ++LastEmitted; // Consume modifier character.
1539 }
1540
1541 if (*LastEmitted != '}') {
Edwin Törökced9ff82009-07-11 13:10:19 +00001542 llvm_report_error("Bad ${} expression in inline asm string: '"
1543 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001544 }
1545 ++LastEmitted; // Consume '}' character.
1546 }
1547
1548 if ((unsigned)Val >= NumOperands-1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001549 llvm_report_error("Invalid $ operand number in inline asm string: '"
1550 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551 }
1552
1553 // Okay, we finally have a value number. Ask the target to print this
1554 // operand!
1555 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1556 unsigned OpNo = 1;
1557
1558 bool Error = false;
1559
1560 // Scan to find the machine operand number for the operand.
1561 for (; Val; --Val) {
1562 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001563 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng92167402009-03-20 18:03:34 +00001564 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001565 }
1566
1567 if (OpNo >= MI->getNumOperands()) {
1568 Error = true;
1569 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001570 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571 ++OpNo; // Skip over the ID number.
1572
Dale Johannesencfb19e62007-11-05 21:20:28 +00001573 if (Modifier[0]=='l') // labels are target independent
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001574 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1575 ->getNumber())->print(O, MAI);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001576 else {
1577 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001578 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001579 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1580 Modifier[0] ? Modifier : 0);
1581 } else {
1582 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1583 Modifier[0] ? Modifier : 0);
1584 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001585 }
1586 }
1587 if (Error) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001588 std::string msg;
1589 raw_string_ostream Msg(msg);
1590 Msg << "Invalid operand found in inline asm: '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591 << AsmStr << "'\n";
Edwin Törökced9ff82009-07-11 13:10:19 +00001592 MI->print(Msg);
1593 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001594 }
1595 }
1596 break;
1597 }
1598 }
1599 }
Chris Lattner32d4cc72009-09-09 23:14:36 +00001600 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001601}
1602
Evan Cheng3c0eda52008-03-15 00:03:38 +00001603/// printImplicitDef - This method prints the specified machine instruction
1604/// that is an implicit def.
1605void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001606 if (!VerboseAsm) return;
1607 O.PadToColumn(MAI->getCommentColumn());
1608 O << MAI->getCommentString() << " implicit-def: "
Chris Lattner28f7e352009-09-13 19:48:37 +00001609 << TRI->getName(MI->getOperand(0).getReg());
Evan Cheng3c0eda52008-03-15 00:03:38 +00001610}
1611
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612/// printLabel - This method prints a local label used by debug and
1613/// exception handling tables.
1614void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001615 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001616}
1617
Evan Chenga53c40a2008-02-01 09:10:45 +00001618void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001619 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Chenga53c40a2008-02-01 09:10:45 +00001620}
1621
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001622/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1623/// instruction, using the specified assembler variant. Targets should
1624/// overried this to format as appropriate.
1625bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1626 unsigned AsmVariant, const char *ExtraCode) {
1627 // Target doesn't support this yet!
1628 return true;
1629}
1630
1631bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1632 unsigned AsmVariant,
1633 const char *ExtraCode) {
1634 // Target doesn't support this yet!
1635 return true;
1636}
1637
Chris Lattner08c97082009-09-12 23:02:08 +00001638MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1639 SmallString<60> Name;
1640 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1641 << getFunctionNumber() << '_' << MBBID;
1642
1643 return OutContext.GetOrCreateSymbol(Name.str());
1644}
1645
1646
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001647/// EmitBasicBlockStart - This method prints the label for the specified
1648/// MachineBasicBlock, an alignment (if present) and a comment describing
1649/// it if appropriate.
1650void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB,
1651 bool PrintColon) const {
1652 if (unsigned Align = MBB->getAlignment())
1653 EmitAlignment(Log2_32(Align));
Evan Cheng45c1edb2008-02-28 00:43:03 +00001654
Chris Lattner08c97082009-09-12 23:02:08 +00001655 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
1656
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001657 if (PrintColon)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658 O << ':';
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001659
1660 if (VerboseAsm) {
Dan Gohman495af972009-08-12 18:47:05 +00001661 if (const BasicBlock *BB = MBB->getBasicBlock())
1662 if (BB->hasName()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001663 O.PadToColumn(MAI->getCommentColumn());
1664 O << MAI->getCommentString() << ' ';
Dan Gohman5ee8d032009-08-12 20:56:56 +00001665 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohman495af972009-08-12 18:47:05 +00001666 }
David Greenee52fd872009-08-10 16:38:07 +00001667
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001668 EmitComments(*MBB);
David Greenee52fd872009-08-10 16:38:07 +00001669 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001670}
1671
Evan Cheng6fb06762007-11-09 01:32:10 +00001672/// printPICJumpTableSetLabel - This method prints a set label for the
1673/// specified MachineBasicBlock for a jumptable entry.
1674void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1675 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001676 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001677 return;
1678
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001679 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001680 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001681 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001682 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001683 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001684}
1685
Evan Cheng6fb06762007-11-09 01:32:10 +00001686void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1687 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001688 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001689 return;
1690
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001691 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001692 << getFunctionNumber() << '_' << uid << '_' << uid2
1693 << "_set_" << MBB->getNumber() << ',';
Chris Lattnerc6f802d2009-09-13 17:14:04 +00001694 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001695 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001696 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001697}
1698
1699/// printDataDirective - This method prints the asm directive for the
1700/// specified type.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001701void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001702 const TargetData *TD = TM.getTargetData();
1703 switch (type->getTypeID()) {
Chris Lattner1ad1c1d2009-07-15 04:42:49 +00001704 case Type::FloatTyID: case Type::DoubleTyID:
1705 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1706 assert(0 && "Should have already output floating point constant.");
1707 default:
1708 assert(0 && "Can't handle printing this type of thing");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001709 case Type::IntegerTyID: {
1710 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1711 if (BitWidth <= 8)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001712 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 else if (BitWidth <= 16)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001714 O << MAI->getData16bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001715 else if (BitWidth <= 32)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001716 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001717 else if (BitWidth <= 64) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001718 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 "Target cannot handle 64-bit constant exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001720 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman07a91ea2008-09-08 16:40:13 +00001721 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00001722 llvm_unreachable("Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001723 }
1724 break;
1725 }
1726 case Type::PointerTyID:
1727 if (TD->getPointerSize() == 8) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001728 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001729 "Target cannot handle 64-bit pointer exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001730 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001731 } else if (TD->getPointerSize() == 2) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001732 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001733 } else if (TD->getPointerSize() == 1) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001734 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001736 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737 }
1738 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001739 }
1740}
1741
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001742void AsmPrinter::printVisibility(const std::string& Name,
1743 unsigned Visibility) const {
1744 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001745 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001746 O << Directive << Name << '\n';
1747 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001748 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001749 O << Directive << Name << '\n';
1750 }
1751}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001752
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001753void AsmPrinter::printOffset(int64_t Offset) const {
1754 if (Offset > 0)
1755 O << '+' << Offset;
1756 else if (Offset < 0)
1757 O << Offset;
1758}
1759
Daniel Dunbarc92710a2009-08-14 03:43:57 +00001760void AsmPrinter::printMCInst(const MCInst *MI) {
1761 llvm_unreachable("MCInst printing unavailable on this target!");
1762}
1763
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001764GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1765 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001766 return 0;
1767
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001768 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001769 if (GCPI != GCMetadataPrinters.end())
1770 return GCPI->second;
1771
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001772 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001773
1774 for (GCMetadataPrinterRegistry::iterator
1775 I = GCMetadataPrinterRegistry::begin(),
1776 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1777 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001778 GCMetadataPrinter *GMP = I->instantiate();
1779 GMP->S = S;
1780 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1781 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001782 }
1783
Chris Lattnerbf9d76d2009-08-23 07:19:13 +00001784 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001785 llvm_unreachable(0);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001786}
David Greene63486122009-07-13 20:25:48 +00001787
1788/// EmitComments - Pretty-print comments for instructions
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001789void AsmPrinter::EmitComments(const MachineInstr &MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001790 assert(VerboseAsm && !MI.getDebugLoc().isUnknown());
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001791
1792 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001793
Chris Lattner46148952009-08-17 15:48:08 +00001794 // Print source line info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001795 O.PadToColumn(MAI->getCommentColumn());
1796 O << MAI->getCommentString() << " SrcLine ";
Devang Patel15e723d2009-08-28 23:24:31 +00001797 if (DLT.CompileUnit) {
1798 std::string Str;
1799 DICompileUnit CU(DLT.CompileUnit);
1800 O << CU.getFilename(Str) << " ";
David Greene4a37a692009-07-16 22:24:20 +00001801 }
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001802 O << DLT.Line;
1803 if (DLT.Col != 0)
1804 O << ":" << DLT.Col;
David Greene63486122009-07-13 20:25:48 +00001805}
1806
David Greene066ed6a2009-08-18 19:22:55 +00001807/// PrintChildLoopComment - Print comments about child loops within
1808/// the loop for this basic block, with nesting.
1809///
1810static void PrintChildLoopComment(formatted_raw_ostream &O,
1811 const MachineLoop *loop,
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001812 const MCAsmInfo *MAI,
David Greene066ed6a2009-08-18 19:22:55 +00001813 int FunctionNumber) {
1814 // Add child loop information
1815 for(MachineLoop::iterator cl = loop->begin(),
1816 clend = loop->end();
1817 cl != clend;
1818 ++cl) {
1819 MachineBasicBlock *Header = (*cl)->getHeader();
1820 assert(Header && "No header for loop");
1821
1822 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001823 O.PadToColumn(MAI->getCommentColumn());
David Greene066ed6a2009-08-18 19:22:55 +00001824
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001825 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001826 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001827 << " Child Loop BB" << FunctionNumber << "_"
1828 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1829
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001830 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greene066ed6a2009-08-18 19:22:55 +00001831 }
1832}
1833
David Greenee52fd872009-08-10 16:38:07 +00001834/// EmitComments - Pretty-print comments for basic blocks
1835void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1836{
David Greene066ed6a2009-08-18 19:22:55 +00001837 if (VerboseAsm) {
David Greenee52fd872009-08-10 16:38:07 +00001838 // Add loop depth information
1839 const MachineLoop *loop = LI->getLoopFor(&MBB);
1840
1841 if (loop) {
1842 // Print a newline after bb# annotation.
1843 O << "\n";
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001844 O.PadToColumn(MAI->getCommentColumn());
1845 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greenee52fd872009-08-10 16:38:07 +00001846 << '\n';
1847
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001848 O.PadToColumn(MAI->getCommentColumn());
David Greenee52fd872009-08-10 16:38:07 +00001849
1850 MachineBasicBlock *Header = loop->getHeader();
1851 assert(Header && "No header for loop");
1852
1853 if (Header == &MBB) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001854 O << MAI->getCommentString() << " Loop Header";
1855 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greenee52fd872009-08-10 16:38:07 +00001856 }
1857 else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001858 O << MAI->getCommentString() << " Loop Header is BB"
David Greenee52fd872009-08-10 16:38:07 +00001859 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1860 }
1861
1862 if (loop->empty()) {
1863 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001864 O.PadToColumn(MAI->getCommentColumn());
1865 O << MAI->getCommentString() << " Inner Loop";
David Greenee52fd872009-08-10 16:38:07 +00001866 }
1867
1868 // Add parent loop information
1869 for (const MachineLoop *CurLoop = loop->getParentLoop();
1870 CurLoop;
1871 CurLoop = CurLoop->getParentLoop()) {
1872 MachineBasicBlock *Header = CurLoop->getHeader();
1873 assert(Header && "No header for loop");
1874
1875 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001876 O.PadToColumn(MAI->getCommentColumn());
1877 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00001878 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00001879 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greenee52fd872009-08-10 16:38:07 +00001880 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1881 }
1882 }
1883 }
1884}