blob: 6de6f2b47f4ad17ad79f82a4b1e4b251fbc6284b [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"
Evan Cheng42ceb472009-03-25 01:47:28 +000031#include "llvm/Support/CommandLine.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000032#include "llvm/Support/ErrorHandling.h"
David Greene63486122009-07-13 20:25:48 +000033#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Support/Mangler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Target/TargetAsmInfo.h"
36#include "llvm/Target/TargetData.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000038#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng0eeed442008-07-01 23:18:29 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng3c0eda52008-03-15 00:03:38 +000040#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng6fb06762007-11-09 01:32:10 +000041#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner89b36582008-08-17 07:19:36 +000042#include "llvm/ADT/SmallString.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000043#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include <cerrno>
45using namespace llvm;
46
Evan Cheng42ceb472009-03-25 01:47:28 +000047static cl::opt<cl::boolOrDefault>
48AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
49 cl::init(cl::BOU_UNSET));
50
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051char AsmPrinter::ID = 0;
David Greene302008d2009-07-14 20:18:05 +000052AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000053 const TargetAsmInfo *T, bool VDef)
54 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Cheng3c0eda52008-03-15 00:03:38 +000055 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000056
57 OutContext(*new MCContext()),
Chris Lattner13a4af72009-08-17 04:23:44 +000058 OutStreamer(*createAsmStreamer(OutContext, O, *T, this)),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000059
Chris Lattnerebd055c2009-08-03 23:20:21 +000060 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersonc82bd822009-06-25 16:55:32 +000061 PrevDLT(0, ~0U, ~0U) {
Chris Lattner2424eac2009-06-24 19:09:55 +000062 DW = 0; MMI = 0;
Evan Cheng42ceb472009-03-25 01:47:28 +000063 switch (AsmVerbose) {
64 case cl::BOU_UNSET: VerboseAsm = VDef; break;
65 case cl::BOU_TRUE: VerboseAsm = true; break;
66 case cl::BOU_FALSE: VerboseAsm = false; break;
67 }
68}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000070AsmPrinter::~AsmPrinter() {
71 for (gcp_iterator I = GCMetadataPrinters.begin(),
72 E = GCMetadataPrinters.end(); I != E; ++I)
73 delete I->second;
Chris Lattner1eb0ad02009-07-27 21:28:04 +000074
75 delete &OutStreamer;
76 delete &OutContext;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000077}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
Chris Lattner75bdd292009-08-03 19:12:26 +000079TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerc4c40a92009-07-28 03:13:23 +000080 return TM.getTargetLowering()->getObjFileLowering();
81}
82
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +000083/// SwitchToSection - Switch to the specified section of the executable if we
Chris Lattnerd69d8ad2009-08-18 06:15:16 +000084/// are not already in it!
Chris Lattnere6ad12f2009-07-31 18:48:30 +000085void AsmPrinter::SwitchToSection(const MCSection *NS) {
Chris Lattnerd69d8ad2009-08-18 06:15:16 +000086 assert(NS != 0 && "Must specify a section to switch to");
87 OutStreamer.SwitchSection(NS);
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +000088}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089
Chris Lattnerd69d8ad2009-08-18 06:15:16 +000090/// getCurrentSection() - Return the current section we are emitting to.
91const MCSection *AsmPrinter::getCurrentSection() const {
92 return OutStreamer.getCurrentSection();
93}
94
95
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000096void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +000097 AU.setPreservesAll();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000098 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen1aed5992008-08-17 18:44:35 +000099 AU.addRequired<GCModuleInfo>();
David Greene066ed6a2009-08-18 19:22:55 +0000100 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +0000101 AU.addRequired<MachineLoopInfo>();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000102}
103
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner01316272009-07-31 17:42:42 +0000105 // Initialize TargetLoweringObjectFile.
106 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
107 .Initialize(OutContext, TM);
108
Bill Wendling4c5482b2009-07-20 21:30:28 +0000109 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
Chris Lattnerc889b3b2009-07-21 17:30:51 +0000110 TAI->getLinkerPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111
Chris Lattner690b02c2009-06-18 23:41:35 +0000112 if (TAI->doesAllowQuotesInName())
113 Mang->setUseQuotes(true);
114
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000115 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000116 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000117
118 if (TAI->hasSingleParameterDotFile()) {
119 /* Very minimal debug info. It is ignored if we emit actual
120 debug info. If we don't, this at helps the user find where
121 a function came from. */
122 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
123 }
124
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000125 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
126 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
127 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 if (!M.getModuleInlineAsm().empty())
130 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
131 << M.getModuleInlineAsm()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000132 << '\n' << TAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 << " End of file scope inline assembly\n";
134
Chris Lattner2424eac2009-06-24 19:09:55 +0000135 if (TAI->doesSupportDebugInformation() ||
136 TAI->doesSupportExceptionHandling()) {
137 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
138 if (MMI)
Devang Patel1a454842009-06-19 21:54:26 +0000139 MMI->AnalyzeModule(M);
Chris Lattner2424eac2009-06-24 19:09:55 +0000140 DW = getAnalysisIfAvailable<DwarfWriter>();
141 if (DW)
142 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel1a454842009-06-19 21:54:26 +0000143 }
144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 return false;
146}
147
148bool AsmPrinter::doFinalization(Module &M) {
Chris Lattnerae982212009-07-21 18:38:57 +0000149 // Emit global variables.
150 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
151 I != E; ++I)
152 PrintGlobalVariable(I);
153
Chris Lattnere1225cc2009-06-24 18:54:37 +0000154 // Emit final debug information.
155 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
156 DW->EndModule();
157
Chris Lattnerdb191f02009-06-24 18:52:01 +0000158 // If the target wants to know about weak references, print them all.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 if (TAI->getWeakRefDirective()) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000160 // FIXME: This is not lazy, it would be nice to only print weak references
161 // to stuff that is actually used. Note that doing so would require targets
162 // to notice uses in operands (due to constant exprs etc). This should
163 // happen with the MC stuff eventually.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164
Chris Lattnerdb191f02009-06-24 18:52:01 +0000165 // Print out module-level global variables here.
166 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
167 I != E; ++I) {
168 if (I->hasExternalWeakLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000169 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000170 }
171
Chris Lattner0f98c332009-08-03 23:10:34 +0000172 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000173 if (I->hasExternalWeakLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000174 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000175 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 }
177
178 if (TAI->getSetDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000179 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattnerdb191f02009-06-24 18:52:01 +0000181 I != E; ++I) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000182 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000183
184 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000185 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000186
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000187 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 else if (I->hasWeakLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000190 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000191 else if (!I->hasLocalLinkage())
Edwin Törökbd448e32009-07-14 16:55:14 +0000192 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000193
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000194 printVisibility(Name, I->getVisibility());
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000195
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000196 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 }
198 }
199
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000200 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000201 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
202 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
203 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
204 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000205
Dan Gohmana65530a2008-05-05 00:28:39 +0000206 // If we don't have any trampolines, then we don't require stack memory
207 // to be executable. Some targets have a directive to declare this.
Chris Lattnerdb191f02009-06-24 18:52:01 +0000208 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana65530a2008-05-05 00:28:39 +0000209 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
210 if (TAI->getNonexecutableStackDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000211 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000212
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 delete Mang; Mang = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +0000214 DW = 0; MMI = 0;
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000215
216 OutStreamer.Finish();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 return false;
218}
219
Chris Lattner5c3475e2009-07-17 20:46:40 +0000220std::string
221AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendlingef9211a2007-09-18 01:47:22 +0000222 assert(MF && "No machine function?");
Chris Lattner5c3475e2009-07-17 20:46:40 +0000223 return Mang->getMangledName(MF->getFunction(), ".eh",
224 TAI->is_EHSymbolPrivate());
Bill Wendlingef9211a2007-09-18 01:47:22 +0000225}
226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
228 // What's my mangled name?
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000229 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000230 IncrementFunctionNumber();
David Greenee52fd872009-08-10 16:38:07 +0000231
David Greene066ed6a2009-08-18 19:22:55 +0000232 if (VerboseAsm) {
David Greenee52fd872009-08-10 16:38:07 +0000233 LI = &getAnalysis<MachineLoopInfo>();
234 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235}
236
Evan Cheng68c18682009-03-13 07:51:59 +0000237namespace {
238 // SectionCPs - Keep track the alignment, constpool entries per Section.
239 struct SectionCPs {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000240 const MCSection *S;
Evan Cheng68c18682009-03-13 07:51:59 +0000241 unsigned Alignment;
242 SmallVector<unsigned, 4> CPEs;
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000243 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng68c18682009-03-13 07:51:59 +0000244 };
245}
246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247/// EmitConstantPool - Print to the current output stream assembly
248/// representations of the constants in the constant pool MCP. This is
249/// used to print out constants which have been "spilled to memory" by
250/// the code generator.
251///
252void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
253 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
254 if (CP.empty()) return;
255
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000256 // Calculate sections for constant pool entries. We collect entries to go into
257 // the same section together to reduce amount of section switch statements.
Evan Cheng68c18682009-03-13 07:51:59 +0000258 SmallVector<SectionCPs, 4> CPSections;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner680c6f62009-07-22 00:28:43 +0000260 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng68c18682009-03-13 07:51:59 +0000261 unsigned Align = CPE.getAlignment();
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000262
263 SectionKind Kind;
264 switch (CPE.getRelocationInfo()) {
265 default: llvm_unreachable("Unknown section kind");
Chris Lattnera9453412009-08-01 23:57:16 +0000266 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000267 case 1:
Chris Lattnera9453412009-08-01 23:57:16 +0000268 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnered0c6762009-07-26 07:00:12 +0000269 break;
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000270 case 0:
Chris Lattnered0c6762009-07-26 07:00:12 +0000271 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnera9453412009-08-01 23:57:16 +0000272 case 4: Kind = SectionKind::getMergeableConst4(); break;
273 case 8: Kind = SectionKind::getMergeableConst8(); break;
274 case 16: Kind = SectionKind::getMergeableConst16();break;
275 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000276 }
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000277 }
278
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000279 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner680c6f62009-07-22 00:28:43 +0000280
Evan Cheng68c18682009-03-13 07:51:59 +0000281 // The number of sections are small, just do a linear search from the
282 // last section to the first.
283 bool Found = false;
284 unsigned SecIdx = CPSections.size();
285 while (SecIdx != 0) {
286 if (CPSections[--SecIdx].S == S) {
287 Found = true;
288 break;
289 }
290 }
291 if (!Found) {
292 SecIdx = CPSections.size();
293 CPSections.push_back(SectionCPs(S, Align));
294 }
295
296 if (Align > CPSections[SecIdx].Alignment)
297 CPSections[SecIdx].Alignment = Align;
298 CPSections[SecIdx].CPEs.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 }
300
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000301 // Now print stuff into the calculated sections.
Evan Cheng68c18682009-03-13 07:51:59 +0000302 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
303 SwitchToSection(CPSections[i].S);
304 EmitAlignment(Log2_32(CPSections[i].Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305
Evan Cheng68c18682009-03-13 07:51:59 +0000306 unsigned Offset = 0;
307 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
308 unsigned CPI = CPSections[i].CPEs[j];
309 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000310
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 // Emit inter-object padding for alignment.
Evan Cheng68c18682009-03-13 07:51:59 +0000312 unsigned AlignMask = CPE.getAlignment() - 1;
313 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
314 EmitZeros(NewOffset - Offset);
315
316 const Type *Ty = CPE.getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000317 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng68c18682009-03-13 07:51:59 +0000318
319 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohman87581eb2009-08-12 18:32:22 +0000320 << CPI << ':';
Evan Cheng68c18682009-03-13 07:51:59 +0000321 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +0000322 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman2aa282f2009-08-13 01:36:44 +0000323 O << TAI->getCommentString() << " constant ";
324 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000325 }
Evan Cheng68c18682009-03-13 07:51:59 +0000326 O << '\n';
327 if (CPE.isMachineConstantPoolEntry())
328 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
329 else
330 EmitGlobalConstant(CPE.Val.ConstVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 }
332 }
333}
334
335/// EmitJumpTableInfo - Print assembly representations of the jump tables used
336/// by the current function to the current output stream.
337///
338void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
339 MachineFunction &MF) {
340 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
341 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
344
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 // Pick the directive to use to print the jump table entries, and switch to
346 // the appropriate section.
347 TargetLowering *LoweringInfo = TM.getTargetLowering();
348
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000349 const Function *F = MF.getFunction();
Evan Chengccca6f72009-06-18 20:37:15 +0000350 bool JTInDiffSection = false;
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000351 if (F->isWeakForLinker() ||
352 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 // In PIC mode, we need to emit the jump table to the same section as the
354 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000355 // We should also do if the section name is NULL or function is declared in
356 // discardable section.
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000357 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 } else {
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000359 // Otherwise, drop it in the readonly section.
360 const MCSection *ReadOnlySection =
Chris Lattnera9453412009-08-01 23:57:16 +0000361 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000362 SwitchToSection(ReadOnlySection);
Evan Chengccca6f72009-06-18 20:37:15 +0000363 JTInDiffSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 }
365
366 EmitAlignment(Log2_32(MJTI->getAlignment()));
367
368 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
369 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
370
371 // If this jump table was deleted, ignore it.
372 if (JTBBs.empty()) continue;
373
374 // For PIC codegen, if possible we want to use the SetDirective to reduce
375 // the number of relocations the assembler will generate for the jump table.
376 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000377 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 if (TAI->getSetDirective() && IsPic)
379 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000380 if (EmittedSets.insert(JTBBs[ii]))
381 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382
383 // On some targets (e.g. darwin) we want to emit two consequtive labels
384 // before each jump table. The first label is never referenced, but tells
385 // the assembler and linker the extents of the jump table object. The
386 // second label is actually referenced by the code.
Evan Chengccca6f72009-06-18 20:37:15 +0000387 if (JTInDiffSection) {
388 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
389 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
390 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391
Evan Cheng477013c2007-10-14 05:57:21 +0000392 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
393 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394
395 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000396 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 O << '\n';
398 }
399 }
400}
401
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000402void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
403 const MachineBasicBlock *MBB,
404 unsigned uid) const {
Chris Lattner3fb451e2009-08-11 20:29:57 +0000405 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000406
407 // Use JumpTableDirective otherwise honor the entry size from the jump table
408 // info.
Chris Lattner3fb451e2009-08-11 20:29:57 +0000409 const char *JTEntryDirective = TAI->getJumpTableDirective(isPIC);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000410 bool HadJTEntryDirective = JTEntryDirective != NULL;
411 if (!HadJTEntryDirective) {
412 JTEntryDirective = MJTI->getEntrySize() == 4 ?
413 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
414 }
415
416 O << JTEntryDirective << ' ';
417
418 // If we have emitted set directives for the jump table entries, print
419 // them rather than the entries themselves. If we're emitting PIC, then
420 // emit the table entries as differences between two text section labels.
421 // If we're emitting non-PIC code, then emit the entries as direct
422 // references to the target basic blocks.
Chris Lattner3fb451e2009-08-11 20:29:57 +0000423 if (!isPIC) {
424 printBasicBlockLabel(MBB, false, false, false);
425 } else if (TAI->getSetDirective()) {
426 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
427 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000428 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000429 printBasicBlockLabel(MBB, false, false, false);
Chris Lattner3fb451e2009-08-11 20:29:57 +0000430 // If the arch uses custom Jump Table directives, don't calc relative to
431 // JT
432 if (!HadJTEntryDirective)
433 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
434 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000435 }
436}
437
438
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
440/// special global used by LLVM. If so, emit it and return true, otherwise
441/// do nothing and return false.
442bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000443 if (GV->getName() == "llvm.used") {
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000444 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
445 EmitLLVMUsedList(GV->getInitializer());
446 return true;
447 }
448
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000449 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner68433442009-04-13 05:44:34 +0000450 if (GV->getSection() == "llvm.metadata" ||
451 GV->hasAvailableExternallyLinkage())
452 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453
454 if (!GV->hasAppendingLinkage()) return false;
455
456 assert(GV->hasInitializer() && "Not a special LLVM global!");
457
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 const TargetData *TD = TM.getTargetData();
459 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner0c433e92009-03-09 05:52:15 +0000460 if (GV->getName() == "llvm.global_ctors") {
Chris Lattnerb5c952a2009-08-02 00:34:36 +0000461 SwitchToSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000462 EmitAlignment(Align, 0);
463 EmitXXStructorList(GV->getInitializer());
464 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 }
466
Chris Lattner0c433e92009-03-09 05:52:15 +0000467 if (GV->getName() == "llvm.global_dtors") {
Chris Lattnerb5c952a2009-08-02 00:34:36 +0000468 SwitchToSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000469 EmitAlignment(Align, 0);
470 EmitXXStructorList(GV->getInitializer());
471 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 }
473
474 return false;
475}
476
477/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000478/// global in the specified llvm.used list for which emitUsedDirectiveFor
479/// is true, as being used with this directive.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480void AsmPrinter::EmitLLVMUsedList(Constant *List) {
481 const char *Directive = TAI->getUsedDirective();
482
Dan Gohman9e1657f2009-06-14 23:30:43 +0000483 // Should be an array of 'i8*'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
485 if (InitList == 0) return;
486
487 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner3f621b32009-07-17 22:00:23 +0000488 const GlobalValue *GV =
489 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner84362ac2009-07-31 20:52:39 +0000490 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000491 O << Directive;
492 EmitConstantValueOnly(InitList->getOperand(i));
493 O << '\n';
494 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 }
496}
497
498/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
499/// function pointers, ignoring the init priority.
500void AsmPrinter::EmitXXStructorList(Constant *List) {
501 // Should be an array of '{ int, void ()* }' structs. The first value is the
502 // init priority, which we ignore.
503 if (!isa<ConstantArray>(List)) return;
504 ConstantArray *InitList = cast<ConstantArray>(List);
505 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
506 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
507 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
508
509 if (CS->getOperand(1)->isNullValue())
510 return; // Found a null terminator, exit printing.
511 // Emit the function pointer.
512 EmitGlobalConstant(CS->getOperand(1));
513 }
514}
515
516/// getGlobalLinkName - Returns the asm/link name of of the specified
517/// global variable. Should be overridden by each target asm printer to
518/// generate the appropriate value.
Bill Wendling26a8ab92009-04-10 00:12:49 +0000519const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
520 std::string &LinkName) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 if (isa<Function>(GV)) {
522 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000523 LinkName += Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 LinkName += TAI->getFunctionAddrSuffix();
525 } else {
526 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000527 LinkName += Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 LinkName += TAI->getGlobalVarAddrSuffix();
529 }
530
531 return LinkName;
532}
533
534/// EmitExternalGlobal - Emit the external reference to a global variable.
535/// Should be overridden if an indirect reference should be used.
536void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000537 std::string GLN;
538 O << getGlobalLinkName(GV, GLN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539}
540
541
542
543//===----------------------------------------------------------------------===//
544/// LEB 128 number encoding.
545
546/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
547/// representing an unsigned leb128 value.
548void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000549 char Buffer[20];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000551 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 Value >>= 7;
553 if (Value) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000554 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 if (Value) O << ", ";
556 } while (Value);
557}
558
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
560/// representing a signed leb128 value.
561void AsmPrinter::PrintSLEB128(int Value) const {
562 int Sign = Value >> (8 * sizeof(Value) - 1);
563 bool IsMore;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000564 char Buffer[20];
aslc200b112008-08-16 12:57:46 +0000565
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000567 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 Value >>= 7;
569 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
570 if (IsMore) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000571 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 if (IsMore) O << ", ";
573 } while (IsMore);
574}
575
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576//===--------------------------------------------------------------------===//
577// Emission and print routines
578//
579
580/// PrintHex - Print a value as a hexidecimal value.
581///
582void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerda2047e2008-11-10 04:30:26 +0000583 char Buffer[20];
Chris Lattnerda2047e2008-11-10 04:30:26 +0000584 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585}
586
587/// EOL - Print a newline character to asm stream. If a comment is present
588/// then it will be printed first. Comments should not contain '\n'.
589void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000590 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000592
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000594 if (VerboseAsm && !Comment.empty()) {
Chris Lattner46148952009-08-17 15:48:08 +0000595 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +0000596 O << TAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000597 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 << Comment;
599 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000600 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601}
602
Owen Anderson367bfbb2008-07-01 21:16:27 +0000603void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000604 if (VerboseAsm && *Comment) {
Chris Lattner46148952009-08-17 15:48:08 +0000605 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +0000606 O << TAI->getCommentString()
Owen Anderson367bfbb2008-07-01 21:16:27 +0000607 << ' '
608 << Comment;
609 }
610 O << '\n';
611}
612
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
614/// unsigned leb128 value.
615void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
616 if (TAI->hasLEB128()) {
617 O << "\t.uleb128\t"
618 << Value;
619 } else {
620 O << TAI->getData8bitsDirective();
621 PrintULEB128(Value);
622 }
623}
624
625/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
626/// signed leb128 value.
627void AsmPrinter::EmitSLEB128Bytes(int Value) const {
628 if (TAI->hasLEB128()) {
629 O << "\t.sleb128\t"
630 << Value;
631 } else {
632 O << TAI->getData8bitsDirective();
633 PrintSLEB128(Value);
634 }
635}
636
637/// EmitInt8 - Emit a byte directive and value.
638///
639void AsmPrinter::EmitInt8(int Value) const {
640 O << TAI->getData8bitsDirective();
641 PrintHex(Value & 0xFF);
642}
643
644/// EmitInt16 - Emit a short directive and value.
645///
646void AsmPrinter::EmitInt16(int Value) const {
647 O << TAI->getData16bitsDirective();
648 PrintHex(Value & 0xFFFF);
649}
650
651/// EmitInt32 - Emit a long directive and value.
652///
653void AsmPrinter::EmitInt32(int Value) const {
654 O << TAI->getData32bitsDirective();
655 PrintHex(Value);
656}
657
658/// EmitInt64 - Emit a long long directive and value.
659///
660void AsmPrinter::EmitInt64(uint64_t Value) const {
661 if (TAI->getData64bitsDirective()) {
662 O << TAI->getData64bitsDirective();
663 PrintHex(Value);
664 } else {
665 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000666 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 EmitInt32(unsigned(Value));
668 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000669 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 EmitInt32(unsigned(Value >> 32));
671 }
672 }
673}
674
675/// toOctal - Convert the low order bits of X into an octal digit.
676///
677static inline char toOctal(int X) {
678 return (X&7)+'0';
679}
680
681/// printStringChar - Print a char, escaped if necessary.
682///
David Greene302008d2009-07-14 20:18:05 +0000683static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684 if (C == '"') {
685 O << "\\\"";
686 } else if (C == '\\') {
687 O << "\\\\";
Chris Lattner07ec1462009-01-22 23:38:45 +0000688 } else if (isprint((unsigned char)C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 O << C;
690 } else {
691 switch(C) {
692 case '\b': O << "\\b"; break;
693 case '\f': O << "\\f"; break;
694 case '\n': O << "\\n"; break;
695 case '\r': O << "\\r"; break;
696 case '\t': O << "\\t"; break;
697 default:
698 O << '\\';
699 O << toOctal(C >> 6);
700 O << toOctal(C >> 3);
701 O << toOctal(C >> 0);
702 break;
703 }
704 }
705}
706
707/// EmitString - Emit a string with quotes and a null terminator.
708/// Special characters are emitted properly.
709/// \literal (Eg. '\t') \endliteral
710void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling3f94b412009-04-09 23:51:31 +0000711 EmitString(String.c_str(), String.size());
712}
713
714void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 const char* AscizDirective = TAI->getAscizDirective();
716 if (AscizDirective)
717 O << AscizDirective;
718 else
719 O << TAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000720 O << '\"';
Bill Wendling3f94b412009-04-09 23:51:31 +0000721 for (unsigned i = 0; i < Size; ++i)
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000722 printStringChar(O, String[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000724 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 else
726 O << "\\0\"";
727}
728
729
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000730/// EmitFile - Emit a .file directive.
731void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
732 O << "\t.file\t" << Number << " \"";
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000733 for (unsigned i = 0, N = Name.size(); i < N; ++i)
734 printStringChar(O, Name[i]);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000735 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000736}
737
738
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739//===----------------------------------------------------------------------===//
740
741// EmitAlignment - Emit an alignment directive to the specified power of
742// two boundary. For example, if you pass in 3 here, you will get an 8
743// byte alignment. If a global value is specified, and if that global has
744// an explicit alignment requested, it will unconditionally override the
745// alignment request. However, if ForcedAlignBits is specified, this value
746// has final say: the ultimate alignment will be the max of ForcedAlignBits
747// and the alignment computed with NumBits and the global.
748//
749// The algorithm is:
750// Align = NumBits;
751// if (GV && GV->hasalignment) Align = GV->getalignment();
752// Align = std::max(Align, ForcedAlignBits);
753//
754void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000755 unsigned ForcedAlignBits,
756 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 if (GV && GV->getAlignment())
758 NumBits = Log2_32(GV->getAlignment());
759 NumBits = std::max(NumBits, ForcedAlignBits);
760
761 if (NumBits == 0) return; // No need to emit alignment.
762 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Chengc1f41aa2007-07-25 23:35:07 +0000763 O << TAI->getAlignDirective() << NumBits;
Evan Cheng45c1edb2008-02-28 00:43:03 +0000764
Chris Lattnerd69d8ad2009-08-18 06:15:16 +0000765 if (getCurrentSection()->getKind().isText())
Chris Lattnerebd055c2009-08-03 23:20:21 +0000766 if (unsigned FillValue = TAI->getTextAlignFillValue()) {
767 O << ',';
768 PrintHex(FillValue);
769 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000770 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771}
David Greenecdc2de32009-08-05 21:00:52 +0000772
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773/// EmitZeros - Emit a block of zeros.
774///
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000775void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 if (NumZeros) {
777 if (TAI->getZeroDirective()) {
778 O << TAI->getZeroDirective() << NumZeros;
779 if (TAI->getZeroDirectiveSuffix())
780 O << TAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000781 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 } else {
783 for (; NumZeros; --NumZeros)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000784 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 }
786 }
787}
788
789// Print out the specified constant, without a storage class. Only the
790// constants valid in constant expressions can occur here.
791void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
792 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000793 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000795 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
797 // This is a constant address for a global variable or function. Use the
798 // name of the variable or function as the address value, possibly
799 // decorating it with GlobalVarAddrPrefix/Suffix or
800 // FunctionAddrPrefix/Suffix (these all default to "" )
801 if (isa<Function>(GV)) {
802 O << TAI->getFunctionAddrPrefix()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000803 << Mang->getMangledName(GV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 << TAI->getFunctionAddrSuffix();
805 } else {
806 O << TAI->getGlobalVarAddrPrefix()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000807 << Mang->getMangledName(GV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 << TAI->getGlobalVarAddrSuffix();
809 }
810 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
811 const TargetData *TD = TM.getTargetData();
812 unsigned Opcode = CE->getOpcode();
813 switch (Opcode) {
Chris Lattner34e4cde2009-08-01 22:25:12 +0000814 case Instruction::Trunc:
815 case Instruction::ZExt:
816 case Instruction::SExt:
817 case Instruction::FPTrunc:
818 case Instruction::FPExt:
819 case Instruction::UIToFP:
820 case Instruction::SIToFP:
821 case Instruction::FPToUI:
822 case Instruction::FPToSI:
823 llvm_unreachable("FIXME: Don't support this constant cast expr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 case Instruction::GetElementPtr: {
825 // generate a symbolic expression for the byte address
826 const Constant *ptrVal = CE->getOperand(0);
827 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
828 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
829 idxVec.size())) {
Chris Lattner911129a2009-02-05 06:55:21 +0000830 // Truncate/sext the offset to the pointer size.
831 if (TD->getPointerSizeInBits() != 64) {
832 int SExtAmount = 64-TD->getPointerSizeInBits();
833 Offset = (Offset << SExtAmount) >> SExtAmount;
834 }
835
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000837 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 EmitConstantValueOnly(ptrVal);
839 if (Offset > 0)
840 O << ") + " << Offset;
841 else if (Offset < 0)
842 O << ") - " << -Offset;
843 } else {
844 EmitConstantValueOnly(ptrVal);
845 }
846 break;
847 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 case Instruction::BitCast:
849 return EmitConstantValueOnly(CE->getOperand(0));
850
851 case Instruction::IntToPtr: {
852 // Handle casts to pointers by changing them into casts to the appropriate
853 // integer type. This promotes constant folding and simplifies this code.
854 Constant *Op = CE->getOperand(0);
Owen Anderson35b47072009-08-13 21:58:54 +0000855 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
856 false/*ZExt*/);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 return EmitConstantValueOnly(Op);
858 }
859
860
861 case Instruction::PtrToInt: {
862 // Support only foldable casts to/from pointers that can be eliminated by
863 // changing the pointer to the appropriately sized integer type.
864 Constant *Op = CE->getOperand(0);
865 const Type *Ty = CE->getType();
866
867 // We can emit the pointer value into this slot if the slot is an
868 // integer slot greater or equal to the size of the pointer.
Chris Lattner34e4cde2009-08-01 22:25:12 +0000869 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000871
872 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 EmitConstantValueOnly(Op);
Chris Lattner34e4cde2009-08-01 22:25:12 +0000874 APInt ptrMask =
875 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner89b36582008-08-17 07:19:36 +0000876
877 SmallString<40> S;
878 ptrMask.toStringUnsigned(S);
879 O << ") & " << S.c_str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 break;
881 }
882 case Instruction::Add:
883 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000884 case Instruction::And:
885 case Instruction::Or:
886 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000887 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000889 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000890 switch (Opcode) {
891 case Instruction::Add:
892 O << " + ";
893 break;
894 case Instruction::Sub:
895 O << " - ";
896 break;
897 case Instruction::And:
898 O << " & ";
899 break;
900 case Instruction::Or:
901 O << " | ";
902 break;
903 case Instruction::Xor:
904 O << " ^ ";
905 break;
906 default:
907 break;
908 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000909 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000911 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 break;
913 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000914 llvm_unreachable("Unsupported operator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 }
916 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000917 llvm_unreachable("Unknown constant value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 }
919}
920
921/// printAsCString - Print the specified array as a C compatible string, only if
922/// the predicate isString is true.
923///
David Greene302008d2009-07-14 20:18:05 +0000924static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 unsigned LastElt) {
926 assert(CVA->isString() && "Array is not string compatible!");
927
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000928 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 for (unsigned i = 0; i != LastElt; ++i) {
930 unsigned char C =
931 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
932 printStringChar(O, C);
933 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000934 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935}
936
937/// EmitString - Emit a zero-byte-terminated string constant.
938///
939void AsmPrinter::EmitString(const ConstantArray *CVA) const {
940 unsigned NumElts = CVA->getNumOperands();
941 if (TAI->getAscizDirective() && NumElts &&
942 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
943 O << TAI->getAscizDirective();
944 printAsCString(O, CVA, NumElts-1);
945 } else {
946 O << TAI->getAsciiDirective();
947 printAsCString(O, CVA, NumElts);
948 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000949 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950}
951
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000952void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
953 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000954 if (CVA->isString()) {
955 EmitString(CVA);
956 } else { // Not a string. Print the values in successive locations
957 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000958 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000959 }
960}
961
962void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
963 const VectorType *PTy = CP->getType();
964
965 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
966 EmitGlobalConstant(CP->getOperand(I));
967}
968
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000969void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
970 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000971 // Print the fields in successive locations. Pad to align if needed!
972 const TargetData *TD = TM.getTargetData();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000973 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000974 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
975 uint64_t sizeSoFar = 0;
976 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
977 const Constant* field = CVS->getOperand(i);
978
979 // Check if padding is needed and insert one or more 0s.
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000980 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000981 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
982 - cvsLayout->getElementOffset(i)) - fieldSize;
983 sizeSoFar += fieldSize + padSize;
984
985 // Now print the actual field value.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000986 EmitGlobalConstant(field, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000987
988 // Insert padding - this may include padding to increase the size of the
989 // current field up to the ABI size (if the struct is not packed) as well
990 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000991 EmitZeros(padSize, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000992 }
993 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
994 "Layout of constant struct may be incorrect!");
995}
996
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000997void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
998 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000999 // FP Constants are printed as integer constants to avoid losing
1000 // precision...
Owen Anderson35b47072009-08-13 21:58:54 +00001001 LLVMContext &Context = CFP->getContext();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001002 const TargetData *TD = TM.getTargetData();
Owen Anderson35b47072009-08-13 21:58:54 +00001003 if (CFP->getType() == Type::getDoubleTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001004 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1005 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Cheng11db8142009-03-24 00:17:40 +00001006 if (TAI->getData64bitsDirective(AddrSpace)) {
1007 O << TAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohman87581eb2009-08-12 18:32:22 +00001008 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001009 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman2aa282f2009-08-13 01:36:44 +00001010 O << TAI->getCommentString() << " double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001011 }
Evan Cheng11db8142009-03-24 00:17:40 +00001012 O << '\n';
1013 } else if (TD->isBigEndian()) {
1014 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001015 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001016 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001017 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001018 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001019 }
Evan Cheng11db8142009-03-24 00:17:40 +00001020 O << '\n';
1021 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001022 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001023 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001024 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001025 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001026 }
Evan Cheng11db8142009-03-24 00:17:40 +00001027 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001028 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001029 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001030 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001031 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001032 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001033 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001034 }
Evan Cheng11db8142009-03-24 00:17:40 +00001035 O << '\n';
1036 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001037 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001038 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001039 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001040 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001041 }
Evan Cheng11db8142009-03-24 00:17:40 +00001042 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001043 }
1044 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001045 } else if (CFP->getType() == Type::getFloatTy(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001046 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001047 O << TAI->getData32bitsDirective(AddrSpace)
Evan Cheng11db8142009-03-24 00:17:40 +00001048 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman87581eb2009-08-12 18:32:22 +00001049 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001050 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001051 O << TAI->getCommentString() << " float " << Val;
1052 }
Evan Cheng11db8142009-03-24 00:17:40 +00001053 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001054 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001055 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001056 // all long double variants are printed as hex
1057 // api needed to prevent premature destruction
1058 APInt api = CFP->getValueAPF().bitcastToAPInt();
1059 const uint64_t *p = api.getRawData();
1060 // Convert to double so we can print the approximate val as a comment.
1061 APFloat DoubleVal = CFP->getValueAPF();
1062 bool ignored;
1063 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1064 &ignored);
1065 if (TD->isBigEndian()) {
Evan Cheng11db8142009-03-24 00:17:40 +00001066 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001067 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001068 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001069 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001070 << " most significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001071 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001072 }
Evan Cheng11db8142009-03-24 00:17:40 +00001073 O << '\n';
1074 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001075 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001076 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman2aa282f2009-08-13 01:36:44 +00001077 O << TAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001078 }
Evan Cheng11db8142009-03-24 00:17:40 +00001079 O << '\n';
1080 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001081 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001082 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman2aa282f2009-08-13 01:36:44 +00001083 O << TAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001084 }
Evan Cheng11db8142009-03-24 00:17:40 +00001085 O << '\n';
1086 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001087 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001088 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman2aa282f2009-08-13 01:36:44 +00001089 O << TAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001090 }
Evan Cheng11db8142009-03-24 00:17:40 +00001091 O << '\n';
1092 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001093 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001094 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001095 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001096 << " least significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001097 }
Evan Cheng11db8142009-03-24 00:17:40 +00001098 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001099 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001100 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001101 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001102 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001103 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001104 << " least significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001105 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001106 }
Evan Cheng11db8142009-03-24 00:17:40 +00001107 O << '\n';
1108 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001109 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001110 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001111 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001112 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001113 }
Evan Cheng11db8142009-03-24 00:17:40 +00001114 O << '\n';
1115 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001116 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001117 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001118 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001119 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001120 }
Evan Cheng11db8142009-03-24 00:17:40 +00001121 O << '\n';
1122 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001123 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001124 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001125 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001126 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001127 }
Evan Cheng11db8142009-03-24 00:17:40 +00001128 O << '\n';
1129 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001130 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001131 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001132 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001133 << " most significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001134 }
Evan Cheng11db8142009-03-24 00:17:40 +00001135 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001136 }
Owen Anderson35b47072009-08-13 21:58:54 +00001137 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1138 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001139 return;
Owen Anderson35b47072009-08-13 21:58:54 +00001140 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001141 // all long double variants are printed as hex
1142 // api needed to prevent premature destruction
1143 APInt api = CFP->getValueAPF().bitcastToAPInt();
1144 const uint64_t *p = api.getRawData();
1145 if (TD->isBigEndian()) {
Evan Cheng11db8142009-03-24 00:17:40 +00001146 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001147 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001148 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001149 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001150 << " most significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001151 }
Evan Cheng11db8142009-03-24 00:17:40 +00001152 O << '\n';
1153 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001154 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001155 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001156 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001157 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001158 }
Evan Cheng11db8142009-03-24 00:17:40 +00001159 O << '\n';
1160 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001161 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001162 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001163 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001164 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001165 }
Evan Cheng11db8142009-03-24 00:17:40 +00001166 O << '\n';
1167 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001168 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001169 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001170 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001171 << " least significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001172 }
Evan Cheng11db8142009-03-24 00:17:40 +00001173 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001174 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001175 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001176 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001177 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001178 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001179 << " least significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001180 }
Evan Cheng11db8142009-03-24 00:17:40 +00001181 O << '\n';
1182 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001183 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001184 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001185 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001186 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001187 }
Evan Cheng11db8142009-03-24 00:17:40 +00001188 O << '\n';
1189 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001190 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001191 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001192 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001193 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001194 }
Evan Cheng11db8142009-03-24 00:17:40 +00001195 O << '\n';
1196 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001197 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001198 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001199 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001200 << " most significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001201 }
Evan Cheng11db8142009-03-24 00:17:40 +00001202 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001203 }
1204 return;
Edwin Törökbd448e32009-07-14 16:55:14 +00001205 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001206}
1207
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001208void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1209 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001210 const TargetData *TD = TM.getTargetData();
1211 unsigned BitWidth = CI->getBitWidth();
1212 assert(isPowerOf2_32(BitWidth) &&
1213 "Non-power-of-2-sized integers not handled!");
1214
1215 // We don't expect assemblers to support integer data directives
1216 // for more than 64 bits, so we emit the data in at most 64-bit
1217 // quantities at a time.
1218 const uint64_t *RawData = CI->getValue().getRawData();
1219 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1220 uint64_t Val;
1221 if (TD->isBigEndian())
1222 Val = RawData[e - i - 1];
1223 else
1224 Val = RawData[i];
1225
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001226 if (TAI->getData64bitsDirective(AddrSpace))
1227 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001228 else if (TD->isBigEndian()) {
Evan Cheng11db8142009-03-24 00:17:40 +00001229 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001230 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001231 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001232 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001233 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001234 }
Evan Cheng11db8142009-03-24 00:17:40 +00001235 O << '\n';
1236 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001237 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001238 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001239 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001240 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001241 }
Evan Cheng11db8142009-03-24 00:17:40 +00001242 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001243 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001244 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohman87581eb2009-08-12 18:32:22 +00001245 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001246 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001247 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001248 << " least significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001249 }
Evan Cheng11db8142009-03-24 00:17:40 +00001250 O << '\n';
1251 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001252 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001253 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001254 O << TAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001255 << " most significant half of i64 " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001256 }
Evan Cheng11db8142009-03-24 00:17:40 +00001257 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001258 }
1259 }
1260}
1261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001263void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 const TargetData *TD = TM.getTargetData();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001265 const Type *type = CV->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001266 unsigned Size = TD->getTypeAllocSize(type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001267
1268 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001269 EmitZeros(Size, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 return;
1271 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +00001272 EmitGlobalConstantArray(CVA , AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273 return;
1274 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001275 EmitGlobalConstantStruct(CVS, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 return;
1277 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001278 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001279 return;
1280 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1281 // Small integers are handled below; large integers are handled here.
1282 if (Size > 4) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001283 EmitGlobalConstantLargeInt(CI, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001284 return;
1285 }
1286 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001287 EmitGlobalConstantVector(CP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 return;
1289 }
1290
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001291 printDataDirective(type, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 EmitConstantValueOnly(CV);
Evan Cheng11db8142009-03-24 00:17:40 +00001293 if (VerboseAsm) {
1294 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1295 SmallString<40> S;
1296 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner46148952009-08-17 15:48:08 +00001297 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman495af972009-08-12 18:47:05 +00001298 O << TAI->getCommentString() << " 0x" << S.c_str();
Evan Cheng11db8142009-03-24 00:17:40 +00001299 }
Scott Michele067c3c2008-06-03 15:39:51 +00001300 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001301 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302}
1303
Chris Lattner89b36582008-08-17 07:19:36 +00001304void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 // Target doesn't support this yet!
Edwin Törökbd448e32009-07-14 16:55:14 +00001306 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001307}
1308
1309/// PrintSpecial - Print information related to the specified machine instr
1310/// that is independent of the operand, and may be independent of the instr
1311/// itself. This can be useful for portably encoding the comment character
1312/// or other bits of target-specific knowledge into the asmstrings. The
1313/// syntax used is ${:comment}. Targets can override this to add support
1314/// for their own strange codes.
Chris Lattner6af48032009-03-10 05:37:13 +00001315void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 if (!strcmp(Code, "private")) {
1317 O << TAI->getPrivateGlobalPrefix();
1318 } else if (!strcmp(Code, "comment")) {
Evan Cheng11db8142009-03-24 00:17:40 +00001319 if (VerboseAsm)
1320 O << TAI->getCommentString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001321 } else if (!strcmp(Code, "uid")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 // Comparing the address of MI isn't sufficient, because machineinstrs may
1323 // be allocated to the same address across functions.
1324 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1325
Owen Andersonda3388b2009-06-24 22:28:12 +00001326 // If this is a new LastFn instruction, bump the counter.
1327 if (LastMI != MI || LastFn != ThisF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 ++Counter;
1329 LastMI = MI;
Owen Andersonda3388b2009-06-24 22:28:12 +00001330 LastFn = ThisF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 }
1332 O << Counter;
1333 } else {
Edwin Törökced9ff82009-07-11 13:10:19 +00001334 std::string msg;
1335 raw_string_ostream Msg(msg);
1336 Msg << "Unknown special formatter '" << Code
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 << "' for machine instr: " << *MI;
Edwin Törökced9ff82009-07-11 13:10:19 +00001338 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339 }
1340}
1341
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001342/// processDebugLoc - Processes the debug information of each machine
1343/// instruction's DebugLoc.
1344void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattner693cfcc2009-08-05 04:09:18 +00001345 if (!TAI || !DW)
1346 return;
1347
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001348 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1349 if (!DL.isUnknown()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001350 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1351
1352 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1353 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1354 DICompileUnit(CurDLT.CompileUnit)));
1355
1356 PrevDLT = CurDLT;
1357 }
1358 }
1359}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360
1361/// printInlineAsm - This method formats and prints the specified machine
1362/// instruction that is an inline asm.
1363void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1364 unsigned NumOperands = MI->getNumOperands();
1365
1366 // Count the number of register definitions.
1367 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001368 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369 ++NumDefs)
1370 assert(NumDefs != NumOperands-1 && "No asm string?");
1371
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001372 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373
1374 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1375 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1376
Dale Johannesene99fc902008-01-29 02:21:21 +00001377 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1378 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 if (AsmStr[0] == 0) {
Chris Lattnercae14fd2009-08-11 22:39:40 +00001380 O << TAI->getCommentString() << TAI->getInlineAsmStart() << "\n\t";
1381 O << TAI->getCommentString() << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382 return;
1383 }
1384
Chris Lattnercae14fd2009-08-11 22:39:40 +00001385 O << TAI->getCommentString() << TAI->getInlineAsmStart() << "\n\t";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386
1387 // The variant of the current asmprinter.
1388 int AsmPrinterVariant = TAI->getAssemblerDialect();
1389
1390 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1391 const char *LastEmitted = AsmStr; // One past the last character emitted.
1392
1393 while (*LastEmitted) {
1394 switch (*LastEmitted) {
1395 default: {
1396 // Not a special case, emit the string section literally.
1397 const char *LiteralEnd = LastEmitted+1;
1398 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1399 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1400 ++LiteralEnd;
1401 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1402 O.write(LastEmitted, LiteralEnd-LastEmitted);
1403 LastEmitted = LiteralEnd;
1404 break;
1405 }
1406 case '\n':
1407 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001408 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 break;
1410 case '$': {
1411 ++LastEmitted; // Consume '$' character.
1412 bool Done = true;
1413
1414 // Handle escapes.
1415 switch (*LastEmitted) {
1416 default: Done = false; break;
1417 case '$': // $$ -> $
1418 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1419 O << '$';
1420 ++LastEmitted; // Consume second '$' character.
1421 break;
1422 case '(': // $( -> same as GCC's { character.
1423 ++LastEmitted; // Consume '(' character.
1424 if (CurVariant != -1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001425 llvm_report_error("Nested variants found in inline asm string: '"
1426 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 }
1428 CurVariant = 0; // We're in the first variant now.
1429 break;
1430 case '|':
1431 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001432 if (CurVariant == -1)
1433 O << '|'; // this is gcc's behavior for | outside a variant
1434 else
1435 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436 break;
1437 case ')': // $) -> same as GCC's } char.
1438 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001439 if (CurVariant == -1)
1440 O << '}'; // this is gcc's behavior for } outside a variant
1441 else
1442 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 break;
1444 }
1445 if (Done) break;
1446
1447 bool HasCurlyBraces = false;
1448 if (*LastEmitted == '{') { // ${variable}
1449 ++LastEmitted; // Consume '{' character.
1450 HasCurlyBraces = true;
1451 }
1452
Chris Lattner6af48032009-03-10 05:37:13 +00001453 // If we have ${:foo}, then this is not a real operand reference, it is a
1454 // "magic" string reference, just like in .td files. Arrange to call
1455 // PrintSpecial.
1456 if (HasCurlyBraces && *LastEmitted == ':') {
1457 ++LastEmitted;
1458 const char *StrStart = LastEmitted;
1459 const char *StrEnd = strchr(StrStart, '}');
1460 if (StrEnd == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001461 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1462 + std::string(AsmStr) + "'");
Chris Lattner6af48032009-03-10 05:37:13 +00001463 }
1464
1465 std::string Val(StrStart, StrEnd);
1466 PrintSpecial(MI, Val.c_str());
1467 LastEmitted = StrEnd+1;
1468 break;
1469 }
1470
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 const char *IDStart = LastEmitted;
1472 char *IDEnd;
1473 errno = 0;
1474 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1475 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001476 llvm_report_error("Bad $ operand number in inline asm string: '"
1477 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 }
1479 LastEmitted = IDEnd;
1480
1481 char Modifier[2] = { 0, 0 };
1482
1483 if (HasCurlyBraces) {
1484 // If we have curly braces, check for a modifier character. This
1485 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1486 if (*LastEmitted == ':') {
1487 ++LastEmitted; // Consume ':' character.
1488 if (*LastEmitted == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001489 llvm_report_error("Bad ${:} expression in inline asm string: '"
1490 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 }
1492
1493 Modifier[0] = *LastEmitted;
1494 ++LastEmitted; // Consume modifier character.
1495 }
1496
1497 if (*LastEmitted != '}') {
Edwin Törökced9ff82009-07-11 13:10:19 +00001498 llvm_report_error("Bad ${} expression in inline asm string: '"
1499 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001500 }
1501 ++LastEmitted; // Consume '}' character.
1502 }
1503
1504 if ((unsigned)Val >= NumOperands-1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001505 llvm_report_error("Invalid $ operand number in inline asm string: '"
1506 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 }
1508
1509 // Okay, we finally have a value number. Ask the target to print this
1510 // operand!
1511 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1512 unsigned OpNo = 1;
1513
1514 bool Error = false;
1515
1516 // Scan to find the machine operand number for the operand.
1517 for (; Val; --Val) {
1518 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001519 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng92167402009-03-20 18:03:34 +00001520 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 }
1522
1523 if (OpNo >= MI->getNumOperands()) {
1524 Error = true;
1525 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001526 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 ++OpNo; // Skip over the ID number.
1528
Dale Johannesencfb19e62007-11-05 21:20:28 +00001529 if (Modifier[0]=='l') // labels are target independent
Chris Lattner6017d482007-12-30 23:10:15 +00001530 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Cheng45c1edb2008-02-28 00:43:03 +00001531 false, false, false);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001532 else {
1533 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001534 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001535 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1536 Modifier[0] ? Modifier : 0);
1537 } else {
1538 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1539 Modifier[0] ? Modifier : 0);
1540 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001541 }
1542 }
1543 if (Error) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001544 std::string msg;
1545 raw_string_ostream Msg(msg);
1546 Msg << "Invalid operand found in inline asm: '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 << AsmStr << "'\n";
Edwin Törökced9ff82009-07-11 13:10:19 +00001548 MI->print(Msg);
1549 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550 }
1551 }
1552 break;
1553 }
1554 }
1555 }
Chris Lattnercae14fd2009-08-11 22:39:40 +00001556 O << "\n\t" << TAI->getCommentString() << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001557}
1558
Evan Cheng3c0eda52008-03-15 00:03:38 +00001559/// printImplicitDef - This method prints the specified machine instruction
1560/// that is an implicit def.
1561void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohman87581eb2009-08-12 18:32:22 +00001562 if (VerboseAsm) {
Chris Lattner46148952009-08-17 15:48:08 +00001563 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman87581eb2009-08-12 18:32:22 +00001564 O << TAI->getCommentString() << " implicit-def: "
Evan Cheng11db8142009-03-24 00:17:40 +00001565 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Dan Gohman87581eb2009-08-12 18:32:22 +00001566 }
Evan Cheng3c0eda52008-03-15 00:03:38 +00001567}
1568
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569/// printLabel - This method prints a local label used by debug and
1570/// exception handling tables.
1571void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001572 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001573}
1574
Evan Chenga53c40a2008-02-01 09:10:45 +00001575void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng8b988692008-02-02 08:39:46 +00001576 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chenga53c40a2008-02-01 09:10:45 +00001577}
1578
Evan Cheng2e28d622008-02-02 04:07:54 +00001579/// printDeclare - This method prints a local variable declaration used by
1580/// debug tables.
Evan Chengc439a852008-02-04 23:06:48 +00001581/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1582/// entry into dwarf table.
Evan Cheng2e28d622008-02-02 04:07:54 +00001583void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Patelfe359e72009-01-13 21:44:10 +00001584 unsigned FI = MI->getOperand(0).getIndex();
Evan Chengc439a852008-02-04 23:06:48 +00001585 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001586 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Cheng2e28d622008-02-02 04:07:54 +00001587}
1588
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001589/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1590/// instruction, using the specified assembler variant. Targets should
1591/// overried this to format as appropriate.
1592bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1593 unsigned AsmVariant, const char *ExtraCode) {
1594 // Target doesn't support this yet!
1595 return true;
1596}
1597
1598bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1599 unsigned AsmVariant,
1600 const char *ExtraCode) {
1601 // Target doesn't support this yet!
1602 return true;
1603}
1604
1605/// printBasicBlockLabel - This method prints the label for the specified
1606/// MachineBasicBlock
1607void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Cheng45c1edb2008-02-28 00:43:03 +00001608 bool printAlign,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001609 bool printColon,
1610 bool printComment) const {
Evan Cheng45c1edb2008-02-28 00:43:03 +00001611 if (printAlign) {
1612 unsigned Align = MBB->getAlignment();
1613 if (Align)
1614 EmitAlignment(Log2_32(Align));
1615 }
1616
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001617 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng477013c2007-10-14 05:57:21 +00001618 << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001619 if (printColon)
1620 O << ':';
David Greenee52fd872009-08-10 16:38:07 +00001621 if (printComment) {
Dan Gohman495af972009-08-12 18:47:05 +00001622 if (const BasicBlock *BB = MBB->getBasicBlock())
1623 if (BB->hasName()) {
Chris Lattner46148952009-08-17 15:48:08 +00001624 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman5ee8d032009-08-12 20:56:56 +00001625 O << TAI->getCommentString() << ' ';
1626 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohman495af972009-08-12 18:47:05 +00001627 }
David Greenee52fd872009-08-10 16:38:07 +00001628
1629 if (printColon)
1630 EmitComments(*MBB);
1631 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001632}
1633
Evan Cheng6fb06762007-11-09 01:32:10 +00001634/// printPICJumpTableSetLabel - This method prints a set label for the
1635/// specified MachineBasicBlock for a jumptable entry.
1636void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1637 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638 if (!TAI->getSetDirective())
1639 return;
1640
1641 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001642 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001643 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001644 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1645 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646}
1647
Evan Cheng6fb06762007-11-09 01:32:10 +00001648void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1649 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001650 if (!TAI->getSetDirective())
1651 return;
1652
1653 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001654 << getFunctionNumber() << '_' << uid << '_' << uid2
1655 << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001656 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001657 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1658 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001659}
1660
1661/// printDataDirective - This method prints the asm directive for the
1662/// specified type.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001663void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001664 const TargetData *TD = TM.getTargetData();
1665 switch (type->getTypeID()) {
Chris Lattner1ad1c1d2009-07-15 04:42:49 +00001666 case Type::FloatTyID: case Type::DoubleTyID:
1667 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1668 assert(0 && "Should have already output floating point constant.");
1669 default:
1670 assert(0 && "Can't handle printing this type of thing");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001671 case Type::IntegerTyID: {
1672 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1673 if (BitWidth <= 8)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001674 O << TAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 else if (BitWidth <= 16)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001676 O << TAI->getData16bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001677 else if (BitWidth <= 32)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001678 O << TAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001679 else if (BitWidth <= 64) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001680 assert(TAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 "Target cannot handle 64-bit constant exprs!");
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001682 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohman07a91ea2008-09-08 16:40:13 +00001683 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00001684 llvm_unreachable("Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001685 }
1686 break;
1687 }
1688 case Type::PointerTyID:
1689 if (TD->getPointerSize() == 8) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001690 assert(TAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001691 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001692 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001693 } else if (TD->getPointerSize() == 2) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001694 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001695 } else if (TD->getPointerSize() == 1) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001696 O << TAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001697 } else {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001698 O << TAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001699 }
1700 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001701 }
1702}
1703
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001704void AsmPrinter::printVisibility(const std::string& Name,
1705 unsigned Visibility) const {
1706 if (Visibility == GlobalValue::HiddenVisibility) {
1707 if (const char *Directive = TAI->getHiddenDirective())
1708 O << Directive << Name << '\n';
1709 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1710 if (const char *Directive = TAI->getProtectedDirective())
1711 O << Directive << Name << '\n';
1712 }
1713}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001714
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001715void AsmPrinter::printOffset(int64_t Offset) const {
1716 if (Offset > 0)
1717 O << '+' << Offset;
1718 else if (Offset < 0)
1719 O << Offset;
1720}
1721
Daniel Dunbarc92710a2009-08-14 03:43:57 +00001722void AsmPrinter::printMCInst(const MCInst *MI) {
1723 llvm_unreachable("MCInst printing unavailable on this target!");
1724}
1725
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001726GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1727 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001728 return 0;
1729
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001730 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001731 if (GCPI != GCMetadataPrinters.end())
1732 return GCPI->second;
1733
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001734 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001735
1736 for (GCMetadataPrinterRegistry::iterator
1737 I = GCMetadataPrinterRegistry::begin(),
1738 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1739 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001740 GCMetadataPrinter *GMP = I->instantiate();
1741 GMP->S = S;
1742 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1743 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001744 }
1745
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001746 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001747 llvm_unreachable(0);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001748}
David Greene63486122009-07-13 20:25:48 +00001749
1750/// EmitComments - Pretty-print comments for instructions
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001751void AsmPrinter::EmitComments(const MachineInstr &MI) const {
1752 if (!VerboseAsm ||
1753 MI.getDebugLoc().isUnknown())
1754 return;
1755
1756 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001757
Chris Lattner46148952009-08-17 15:48:08 +00001758 // Print source line info.
1759 O.PadToColumn(TAI->getCommentColumn());
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001760 O << TAI->getCommentString() << " SrcLine ";
1761 if (DLT.CompileUnit->hasInitializer()) {
1762 Constant *Name = DLT.CompileUnit->getInitializer();
1763 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1764 if (NameString->isString())
1765 O << NameString->getAsString() << " ";
David Greene4a37a692009-07-16 22:24:20 +00001766 }
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001767 O << DLT.Line;
1768 if (DLT.Col != 0)
1769 O << ":" << DLT.Col;
David Greene63486122009-07-13 20:25:48 +00001770}
1771
1772/// EmitComments - Pretty-print comments for instructions
1773void AsmPrinter::EmitComments(const MCInst &MI) const
1774{
David Greene1fa35942009-07-22 20:33:26 +00001775 if (VerboseAsm) {
1776 if (!MI.getDebugLoc().isUnknown()) {
1777 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001778
David Greene1fa35942009-07-22 20:33:26 +00001779 // Print source line info
Chris Lattner46148952009-08-17 15:48:08 +00001780 O.PadToColumn(TAI->getCommentColumn());
David Greene1fa35942009-07-22 20:33:26 +00001781 O << TAI->getCommentString() << " SrcLine ";
1782 if (DLT.CompileUnit->hasInitializer()) {
1783 Constant *Name = DLT.CompileUnit->getInitializer();
1784 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1785 if (NameString->isString()) {
1786 O << NameString->getAsString() << " ";
1787 }
1788 }
1789 O << DLT.Line;
1790 if (DLT.Col != 0)
1791 O << ":" << DLT.Col;
1792 }
David Greene4a37a692009-07-16 22:24:20 +00001793 }
David Greene63486122009-07-13 20:25:48 +00001794}
David Greenee52fd872009-08-10 16:38:07 +00001795
David Greene066ed6a2009-08-18 19:22:55 +00001796/// Indent - Insert spaces into the character output stream. The
1797/// "level" is multiplied by the "scale" to calculate the number of
1798/// spaces to insert. "level" can represent something like loop
1799/// nesting level, for example.
1800///
1801static formatted_raw_ostream &
1802Indent(formatted_raw_ostream &out, int level, int scale = 2) {
1803 for(int i = 0; i < level*scale; ++i) {
1804 out << " ";
1805 }
1806 return out;
1807}
1808
1809/// PrintChildLoopComment - Print comments about child loops within
1810/// the loop for this basic block, with nesting.
1811///
1812static void PrintChildLoopComment(formatted_raw_ostream &O,
1813 const MachineLoop *loop,
1814 const TargetAsmInfo *TAI,
1815 int FunctionNumber) {
1816 // Add child loop information
1817 for(MachineLoop::iterator cl = loop->begin(),
1818 clend = loop->end();
1819 cl != clend;
1820 ++cl) {
1821 MachineBasicBlock *Header = (*cl)->getHeader();
1822 assert(Header && "No header for loop");
1823
1824 O << '\n';
1825 O.PadToColumn(TAI->getCommentColumn());
1826
1827 O << TAI->getCommentString();
1828 Indent(O, (*cl)->getLoopDepth()-1)
1829 << " Child Loop BB" << FunctionNumber << "_"
1830 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1831
1832 PrintChildLoopComment(O, *cl, TAI, FunctionNumber);
1833 }
1834}
1835
David Greenee52fd872009-08-10 16:38:07 +00001836/// EmitComments - Pretty-print comments for basic blocks
1837void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1838{
David Greene066ed6a2009-08-18 19:22:55 +00001839 if (VerboseAsm) {
David Greenee52fd872009-08-10 16:38:07 +00001840 // Add loop depth information
1841 const MachineLoop *loop = LI->getLoopFor(&MBB);
1842
1843 if (loop) {
1844 // Print a newline after bb# annotation.
1845 O << "\n";
Chris Lattner46148952009-08-17 15:48:08 +00001846 O.PadToColumn(TAI->getCommentColumn());
David Greenee52fd872009-08-10 16:38:07 +00001847 O << TAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
1848 << '\n';
1849
Chris Lattner46148952009-08-17 15:48:08 +00001850 O.PadToColumn(TAI->getCommentColumn());
David Greenee52fd872009-08-10 16:38:07 +00001851
1852 MachineBasicBlock *Header = loop->getHeader();
1853 assert(Header && "No header for loop");
1854
1855 if (Header == &MBB) {
1856 O << TAI->getCommentString() << " Loop Header";
David Greene066ed6a2009-08-18 19:22:55 +00001857 PrintChildLoopComment(O, loop, TAI, getFunctionNumber());
David Greenee52fd872009-08-10 16:38:07 +00001858 }
1859 else {
1860 O << TAI->getCommentString() << " Loop Header is BB"
1861 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1862 }
1863
1864 if (loop->empty()) {
1865 O << '\n';
Chris Lattner46148952009-08-17 15:48:08 +00001866 O.PadToColumn(TAI->getCommentColumn());
David Greenee52fd872009-08-10 16:38:07 +00001867 O << TAI->getCommentString() << " Inner Loop";
1868 }
1869
1870 // Add parent loop information
1871 for (const MachineLoop *CurLoop = loop->getParentLoop();
1872 CurLoop;
1873 CurLoop = CurLoop->getParentLoop()) {
1874 MachineBasicBlock *Header = CurLoop->getHeader();
1875 assert(Header && "No header for loop");
1876
1877 O << '\n';
Chris Lattner46148952009-08-17 15:48:08 +00001878 O.PadToColumn(TAI->getCommentColumn());
David Greene066ed6a2009-08-18 19:22:55 +00001879 O << TAI->getCommentString();
1880 Indent(O, CurLoop->getLoopDepth()-1)
1881 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greenee52fd872009-08-10 16:38:07 +00001882 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1883 }
1884 }
1885 }
1886}