blob: 9a19f81e38b92acebbefc9eba69be2f5ef828b0e [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner45111d12010-01-16 21:57:06 +000019#include "llvm/CodeGen/DwarfWriter.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000020#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000021#include "llvm/CodeGen/MachineConstantPool.h"
David Greene1924aab2009-11-13 21:34:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefe37ab32009-08-18 19:22:55 +000023#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000027#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000028#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000029#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000030#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCStreamer.h"
Chris Lattner7cb384d2009-09-12 23:02:08 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000033#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000035#include "llvm/Support/FormattedStream.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattner45111d12010-01-16 21:57:06 +000037#include "llvm/Target/Mangler.h"
Owen Anderson07000c62006-05-12 06:33:49 +000038#include "llvm/Target/TargetData.h"
David Greene1924aab2009-11-13 21:34:57 +000039#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000040#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000042#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000043#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000044#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000045#include "llvm/ADT/SmallString.h"
Chris Lattner66099132006-02-01 22:41:11 +000046#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000047using namespace llvm;
48
Evan Cheng42bf74b2009-03-25 01:47:28 +000049static cl::opt<cl::boolOrDefault>
50AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
51 cl::init(cl::BOU_UNSET));
52
Devang Patel19974732007-05-03 01:11:54 +000053char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000054AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000055 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000056 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000057 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000058
59 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000060 // FIXME: Pass instprinter to streamer.
61 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000062
Devang Patel6b61f582010-01-16 06:09:35 +000063 LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000064 DW = 0; MMI = 0;
Evan Cheng42bf74b2009-03-25 01:47:28 +000065 switch (AsmVerbose) {
66 case cl::BOU_UNSET: VerboseAsm = VDef; break;
67 case cl::BOU_TRUE: VerboseAsm = true; break;
68 case cl::BOU_FALSE: VerboseAsm = false; break;
69 }
70}
Chris Lattnered138932005-12-13 06:32:10 +000071
Gordon Henriksenc317a602008-08-17 12:08:44 +000072AsmPrinter::~AsmPrinter() {
73 for (gcp_iterator I = GCMetadataPrinters.begin(),
74 E = GCMetadataPrinters.end(); I != E; ++I)
75 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000076
77 delete &OutStreamer;
78 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000079}
Chris Lattnered138932005-12-13 06:32:10 +000080
Chris Lattner38c39882009-08-03 19:12:26 +000081TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000082 return TM.getTargetLowering()->getObjFileLowering();
83}
84
Chris Lattnerdabf07c2009-08-18 06:15:16 +000085/// getCurrentSection() - Return the current section we are emitting to.
86const MCSection *AsmPrinter::getCurrentSection() const {
87 return OutStreamer.getCurrentSection();
88}
89
90
Gordon Henriksence224772008-01-07 01:30:38 +000091void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000092 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +000093 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000094 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +000095 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +000096 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +000097}
98
Chris Lattnera80ba712004-08-16 23:15:22 +000099bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000100 // Initialize TargetLoweringObjectFile.
101 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
102 .Initialize(OutContext, TM);
103
Chris Lattnerc0dba722010-01-17 18:22:35 +0000104 Mang = new Mangler(*MAI);
Chris Lattner2c1b1592006-01-23 23:47:53 +0000105
Bob Wilson812209a2009-09-30 22:06:26 +0000106 // Allow the target to emit any magic that it wants at the start of the file.
107 EmitStartOfAsmFile(M);
Rafael Espindola952b8392008-12-03 11:01:37 +0000108
Chris Lattner33adcfb2009-08-22 21:43:10 +0000109 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000110 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilsonbc9506f2009-09-30 21:26:13 +0000111 debug info. If we don't, this at least helps the user find where
Rafael Espindola952b8392008-12-03 11:01:37 +0000112 a function came from. */
113 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
114 }
115
Bob Wilson812209a2009-09-30 22:06:26 +0000116 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
117 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000118 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
119 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000120 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000121
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000122 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000123 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000124 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000125 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000126 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000127
Chris Lattnerb55e0682009-09-24 05:44:53 +0000128 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
129 if (MMI)
130 MMI->AnalyzeModule(M);
131 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000132 if (DW)
Chris Lattnerb55e0682009-09-24 05:44:53 +0000133 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000134
Chris Lattnera80ba712004-08-16 23:15:22 +0000135 return false;
136}
137
138bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000139 // Emit global variables.
140 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
141 I != E; ++I)
142 PrintGlobalVariable(I);
143
Chris Lattner1f522fe2009-06-24 18:54:37 +0000144 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000145 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000146 DW->EndModule();
147
Chris Lattner0a7befa2009-06-24 18:52:01 +0000148 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000149 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000150 // FIXME: This is not lazy, it would be nice to only print weak references
151 // to stuff that is actually used. Note that doing so would require targets
152 // to notice uses in operands (due to constant exprs etc). This should
153 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000154
Chris Lattner0a7befa2009-06-24 18:52:01 +0000155 // Print out module-level global variables here.
156 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
157 I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000158 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner10b318b2010-01-17 21:43:43 +0000159 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000160 }
161
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000162 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000163 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner10b318b2010-01-17 21:43:43 +0000164 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000165 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000166 }
167
Chris Lattner33adcfb2009-08-22 21:43:10 +0000168 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000169 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000170 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000171 I != E; ++I) {
Chris Lattner5c40e692010-01-16 01:17:26 +0000172 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000173
174 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner5c40e692010-01-16 01:17:26 +0000175 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000176
Chris Lattner10b318b2010-01-17 21:43:43 +0000177 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
178 O << "\t.globl\t" << *Name << '\n';
179 else if (I->hasWeakLinkage())
180 O << MAI->getWeakRefDirective() << *Name << '\n';
181 else
Chris Lattner10595492010-01-16 01:37:14 +0000182 assert(I->hasLocalLinkage() && "Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000183
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000184 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000185
Chris Lattner10b318b2010-01-17 21:43:43 +0000186 O << MAI->getSetDirective() << ' ' << *Name << ", " << *Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000187 }
188 }
189
Duncan Sands1465d612009-01-28 13:14:17 +0000190 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000191 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
192 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
193 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000194 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000195
Dan Gohmana779a982008-05-05 00:28:39 +0000196 // If we don't have any trampolines, then we don't require stack memory
197 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000198 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000199 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000200 if (MAI->getNonexecutableStackDirective())
201 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000202
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000203
204 // Allow the target to emit any magic that it wants at the end of the file,
205 // after everything else has gone out.
206 EmitEndOfAsmFile(M);
207
Chris Lattnera80ba712004-08-16 23:15:22 +0000208 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000209 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000210
211 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000212 return false;
213}
214
Chris Lattner25045bd2005-11-21 07:51:36 +0000215void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner412c3a52010-01-16 01:24:10 +0000216 // Get the function symbol.
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000217 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000218 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000219
Chris Lattner25d812b2009-09-16 00:35:39 +0000220 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000221 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000222}
223
Evan Cheng1606e8e2009-03-13 07:51:59 +0000224namespace {
225 // SectionCPs - Keep track the alignment, constpool entries per Section.
226 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000227 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000228 unsigned Alignment;
229 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000230 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000231 };
232}
233
Chris Lattner3b4fd322005-11-21 08:25:09 +0000234/// EmitConstantPool - Print to the current output stream assembly
235/// representations of the constants in the constant pool MCP. This is
236/// used to print out constants which have been "spilled to memory" by
237/// the code generator.
238///
239void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000240 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000241 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000242
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000243 // Calculate sections for constant pool entries. We collect entries to go into
244 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000245 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000246 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000247 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000248 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000249
250 SectionKind Kind;
251 switch (CPE.getRelocationInfo()) {
252 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000253 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000254 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000255 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000256 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000257 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000258 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000259 case 4: Kind = SectionKind::getMergeableConst4(); break;
260 case 8: Kind = SectionKind::getMergeableConst8(); break;
261 case 16: Kind = SectionKind::getMergeableConst16();break;
262 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000263 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000264 }
265
Chris Lattner83d77fa2009-08-01 23:46:12 +0000266 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000267
Evan Cheng1606e8e2009-03-13 07:51:59 +0000268 // The number of sections are small, just do a linear search from the
269 // last section to the first.
270 bool Found = false;
271 unsigned SecIdx = CPSections.size();
272 while (SecIdx != 0) {
273 if (CPSections[--SecIdx].S == S) {
274 Found = true;
275 break;
276 }
277 }
278 if (!Found) {
279 SecIdx = CPSections.size();
280 CPSections.push_back(SectionCPs(S, Align));
281 }
282
283 if (Align > CPSections[SecIdx].Alignment)
284 CPSections[SecIdx].Alignment = Align;
285 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000286 }
287
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000288 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000289 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000290 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000291 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000292
Evan Cheng1606e8e2009-03-13 07:51:59 +0000293 unsigned Offset = 0;
294 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
295 unsigned CPI = CPSections[i].CPEs[j];
296 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000297
Chris Lattner3029f922006-02-09 04:46:04 +0000298 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000299 unsigned AlignMask = CPE.getAlignment() - 1;
300 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
301 EmitZeros(NewOffset - Offset);
302
303 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000304 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000305
Chris Lattner33adcfb2009-08-22 21:43:10 +0000306 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000307 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000308 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000309 O.PadToColumn(MAI->getCommentColumn());
310 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000311 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000312 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000313 O << '\n';
314 if (CPE.isMachineConstantPoolEntry())
315 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
316 else
317 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000318 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000319 }
320}
321
Nate Begeman37efe672006-04-22 18:53:45 +0000322/// EmitJumpTableInfo - Print assembly representations of the jump tables used
323/// by the current function to the current output stream.
324///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000325void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
326 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000327 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
328 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000329
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000330 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000331
Nate Begeman2f1ae882006-07-27 01:13:04 +0000332 // Pick the directive to use to print the jump table entries, and switch to
333 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000334 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000335
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000336 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000337 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000338 if (F->isWeakForLinker() ||
339 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000340 // In PIC mode, we need to emit the jump table to the same section as the
341 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000342 // We should also do if the section name is NULL or function is declared in
343 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000344 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
345 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000346 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000347 // Otherwise, drop it in the readonly section.
348 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000349 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000350 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000351 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000352 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000353
354 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000355
Nate Begeman37efe672006-04-22 18:53:45 +0000356 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000357 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000358
359 // If this jump table was deleted, ignore it.
360 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000361
362 // For PIC codegen, if possible we want to use the SetDirective to reduce
363 // the number of relocations the assembler will generate for the jump table.
364 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000365 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000366 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000367 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000368 if (EmittedSets.insert(JTBBs[ii]))
369 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000370
Chris Lattner7c301912009-09-13 19:02:16 +0000371 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000372 // before each jump table. The first label is never referenced, but tells
373 // the assembler and linker the extents of the jump table object. The
374 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000375 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
376 O << MAI->getLinkerPrivateGlobalPrefix()
377 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000378 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000379
Chris Lattner33adcfb2009-08-22 21:43:10 +0000380 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000381 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000382
Nate Begeman37efe672006-04-22 18:53:45 +0000383 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000384 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000385 O << '\n';
386 }
387 }
388}
389
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000390void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
391 const MachineBasicBlock *MBB,
392 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000393 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000394
395 // Use JumpTableDirective otherwise honor the entry size from the jump table
396 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000397 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000398 bool HadJTEntryDirective = JTEntryDirective != NULL;
399 if (!HadJTEntryDirective) {
400 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000401 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000402 }
403
404 O << JTEntryDirective << ' ';
405
406 // If we have emitted set directives for the jump table entries, print
407 // them rather than the entries themselves. If we're emitting PIC, then
408 // emit the table entries as differences between two text section labels.
409 // If we're emitting non-PIC code, then emit the entries as direct
410 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000411 if (!isPIC) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000412 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattner33adcfb2009-08-22 21:43:10 +0000413 } else if (MAI->getSetDirective()) {
414 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000415 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000416 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +0000417 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000418 // If the arch uses custom Jump Table directives, don't calc relative to
419 // JT
420 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000421 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000422 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000423 }
424}
425
426
Chris Lattnered138932005-12-13 06:32:10 +0000427/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
428/// special global used by LLVM. If so, emit it and return true, otherwise
429/// do nothing and return false.
430bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000431 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000432 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000433 EmitLLVMUsedList(GV->getInitializer());
434 return true;
435 }
436
Chris Lattner401e10c2009-07-20 06:14:25 +0000437 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000438 if (GV->getSection() == "llvm.metadata" ||
439 GV->hasAvailableExternallyLinkage())
440 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000441
442 if (!GV->hasAppendingLinkage()) return false;
443
444 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000445
Evan Cheng916d07c2007-06-04 20:39:18 +0000446 const TargetData *TD = TM.getTargetData();
447 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000448 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000449 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000450 EmitAlignment(Align, 0);
451 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000452
453 if (TM.getRelocationModel() == Reloc::Static &&
454 MAI->hasStaticCtorDtorReferenceInStaticMode())
455 O << ".reference .constructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000456 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000457 }
458
Chris Lattnerf231c072009-03-09 05:52:15 +0000459 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000460 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000461 EmitAlignment(Align, 0);
462 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000463
464 if (TM.getRelocationModel() == Reloc::Static &&
465 MAI->hasStaticCtorDtorReferenceInStaticMode())
466 O << ".reference .destructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000467 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000468 }
469
470 return false;
471}
472
Chris Lattner33adcfb2009-08-22 21:43:10 +0000473/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000474/// global in the specified llvm.used list for which emitUsedDirectiveFor
475/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000476void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000477 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000478
Dan Gohmana119de82009-06-14 23:30:43 +0000479 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000480 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
481 if (InitList == 0) return;
482
483 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000484 const GlobalValue *GV =
485 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000486 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000487 O << Directive;
488 EmitConstantValueOnly(InitList->getOperand(i));
489 O << '\n';
490 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000491 }
492}
493
Chris Lattnered138932005-12-13 06:32:10 +0000494/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
495/// function pointers, ignoring the init priority.
496void AsmPrinter::EmitXXStructorList(Constant *List) {
497 // Should be an array of '{ int, void ()* }' structs. The first value is the
498 // init priority, which we ignore.
499 if (!isa<ConstantArray>(List)) return;
500 ConstantArray *InitList = cast<ConstantArray>(List);
501 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
502 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
503 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000504
505 if (CS->getOperand(1)->isNullValue())
506 return; // Found a null terminator, exit printing.
507 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000508 EmitGlobalConstant(CS->getOperand(1));
509 }
510}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000511
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000512
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000513//===----------------------------------------------------------------------===//
514/// LEB 128 number encoding.
515
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000516/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000517/// representing an unsigned leb128 value.
518void AsmPrinter::PrintULEB128(unsigned Value) const {
519 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000520 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000521 Value >>= 7;
522 if (Value) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000523 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000524 if (Value) O << ", ";
525 } while (Value);
526}
527
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000528/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000529/// representing a signed leb128 value.
530void AsmPrinter::PrintSLEB128(int Value) const {
531 int Sign = Value >> (8 * sizeof(Value) - 1);
532 bool IsMore;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000533
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000534 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000535 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000536 Value >>= 7;
537 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
538 if (IsMore) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000539 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000540 if (IsMore) O << ", ";
541 } while (IsMore);
542}
543
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000544//===--------------------------------------------------------------------===//
545// Emission and print routines
546//
547
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000548/// PrintHex - Print a value as a hexadecimal value.
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000549///
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000550void AsmPrinter::PrintHex(uint64_t Value) const {
551 O << "0x";
552 O.write_hex(Value);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000553}
554
555/// EOL - Print a newline character to asm stream. If a comment is present
556/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000557void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000558 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000559}
Owen Anderson25995092008-07-01 21:16:27 +0000560
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000561void AsmPrinter::EOL(const Twine &Comment) const {
562 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000563 O.PadToColumn(MAI->getCommentColumn());
564 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000565 << ' '
566 << Comment;
567 }
568 O << '\n';
569}
570
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000571static const char *DecodeDWARFEncoding(unsigned Encoding) {
572 switch (Encoding) {
573 case dwarf::DW_EH_PE_absptr:
574 return "absptr";
575 case dwarf::DW_EH_PE_omit:
576 return "omit";
577 case dwarf::DW_EH_PE_pcrel:
578 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000579 case dwarf::DW_EH_PE_udata4:
580 return "udata4";
581 case dwarf::DW_EH_PE_udata8:
582 return "udata8";
583 case dwarf::DW_EH_PE_sdata4:
584 return "sdata4";
585 case dwarf::DW_EH_PE_sdata8:
586 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000587 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
588 return "pcrel udata4";
589 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
590 return "pcrel sdata4";
591 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
592 return "pcrel udata8";
593 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
594 return "pcrel sdata8";
595 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
596 return "indirect pcrel udata4";
597 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
598 return "indirect pcrel sdata4";
599 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
600 return "indirect pcrel udata8";
601 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
602 return "indirect pcrel sdata8";
603 }
604
605 return 0;
606}
607
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000608void AsmPrinter::EOL(const Twine &Comment, unsigned Encoding) const {
609 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000610 O.PadToColumn(MAI->getCommentColumn());
611 O << MAI->getCommentString()
612 << ' '
613 << Comment;
614
615 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
616 O << " (" << EncStr << ')';
617 }
618 O << '\n';
619}
620
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000621/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
622/// unsigned leb128 value.
623void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000624 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000625 O << "\t.uleb128\t"
626 << Value;
627 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000628 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000629 PrintULEB128(Value);
630 }
631}
632
633/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
634/// signed leb128 value.
635void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000636 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000637 O << "\t.sleb128\t"
638 << Value;
639 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000640 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000641 PrintSLEB128(Value);
642 }
643}
644
645/// EmitInt8 - Emit a byte directive and value.
646///
647void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000648 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000649 PrintHex(Value & 0xFF);
650}
651
652/// EmitInt16 - Emit a short directive and value.
653///
654void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000655 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000656 PrintHex(Value & 0xFFFF);
657}
658
659/// EmitInt32 - Emit a long directive and value.
660///
661void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000662 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000663 PrintHex(Value);
664}
665
666/// EmitInt64 - Emit a long long directive and value.
667///
668void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000669 if (MAI->getData64bitsDirective()) {
670 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000671 PrintHex(Value);
672 } else {
673 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000674 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000675 EmitInt32(unsigned(Value));
676 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000677 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000678 EmitInt32(unsigned(Value >> 32));
679 }
680 }
681}
682
683/// toOctal - Convert the low order bits of X into an octal digit.
684///
685static inline char toOctal(int X) {
686 return (X&7)+'0';
687}
688
689/// printStringChar - Print a char, escaped if necessary.
690///
David Greene71847812009-07-14 20:18:05 +0000691static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000692 if (C == '"') {
693 O << "\\\"";
694 } else if (C == '\\') {
695 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000696 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000697 O << C;
698 } else {
699 switch(C) {
700 case '\b': O << "\\b"; break;
701 case '\f': O << "\\f"; break;
702 case '\n': O << "\\n"; break;
703 case '\r': O << "\\r"; break;
704 case '\t': O << "\\t"; break;
705 default:
706 O << '\\';
707 O << toOctal(C >> 6);
708 O << toOctal(C >> 3);
709 O << toOctal(C >> 0);
710 break;
711 }
712 }
713}
714
715/// EmitString - Emit a string with quotes and a null terminator.
716/// Special characters are emitted properly.
717/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000718void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000719 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000720}
721
722void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000723 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000724 if (AscizDirective)
725 O << AscizDirective;
726 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000727 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000728 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000729 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000730 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000731 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000732 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000733 else
734 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000735}
736
737
Dan Gohman189f80d2007-09-24 20:58:13 +0000738/// EmitFile - Emit a .file directive.
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000739void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
Dan Gohman189f80d2007-09-24 20:58:13 +0000740 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000741 for (unsigned i = 0, N = Name.size(); i < N; ++i)
742 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000743 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000744}
745
746
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000747//===----------------------------------------------------------------------===//
748
Chris Lattner3a420532007-05-31 18:57:45 +0000749// EmitAlignment - Emit an alignment directive to the specified power of
750// two boundary. For example, if you pass in 3 here, you will get an 8
751// byte alignment. If a global value is specified, and if that global has
752// an explicit alignment requested, it will unconditionally override the
753// alignment request. However, if ForcedAlignBits is specified, this value
754// has final say: the ultimate alignment will be the max of ForcedAlignBits
755// and the alignment computed with NumBits and the global.
756//
757// The algorithm is:
758// Align = NumBits;
759// if (GV && GV->hasalignment) Align = GV->getalignment();
760// Align = std::max(Align, ForcedAlignBits);
761//
762void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000763 unsigned ForcedAlignBits,
764 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000765 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000766 NumBits = Log2_32(GV->getAlignment());
767 NumBits = std::max(NumBits, ForcedAlignBits);
768
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000769 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000770
771 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000772 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000773 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000774
775 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000776}
David Greenea5bb59f2009-08-05 21:00:52 +0000777
Chris Lattner25045bd2005-11-21 07:51:36 +0000778/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000779///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000780void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000781 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000782 if (MAI->getZeroDirective()) {
783 O << MAI->getZeroDirective() << NumZeros;
784 if (MAI->getZeroDirectiveSuffix())
785 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000786 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000787 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000788 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000789 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000790 }
791 }
792}
793
Chris Lattnera80ba712004-08-16 23:15:22 +0000794// Print out the specified constant, without a storage class. Only the
795// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000796void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000797 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000798 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000799 return;
800 }
801
802 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000803 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000804 return;
805 }
806
807 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000808 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000809 // name of the variable or function as the address value.
Chris Lattner10b318b2010-01-17 21:43:43 +0000810 O << *GetGlobalValueSymbol(GV);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000811 return;
812 }
813
814 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000815 O << *GetBlockAddressSymbol(BA);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000816 return;
817 }
818
819 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
820 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000821 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000822 O << '0';
823 return;
824 }
825
826 switch (CE->getOpcode()) {
827 case Instruction::ZExt:
828 case Instruction::SExt:
829 case Instruction::FPTrunc:
830 case Instruction::FPExt:
831 case Instruction::UIToFP:
832 case Instruction::SIToFP:
833 case Instruction::FPToUI:
834 case Instruction::FPToSI:
835 default:
836 llvm_unreachable("FIXME: Don't support this constant cast expr");
837 case Instruction::GetElementPtr: {
838 // generate a symbolic expression for the byte address
839 const TargetData *TD = TM.getTargetData();
840 const Constant *ptrVal = CE->getOperand(0);
841 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
842 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
843 idxVec.size());
844 if (Offset == 0)
845 return EmitConstantValueOnly(ptrVal);
846
847 // Truncate/sext the offset to the pointer size.
848 if (TD->getPointerSizeInBits() != 64) {
849 int SExtAmount = 64-TD->getPointerSizeInBits();
850 Offset = (Offset << SExtAmount) >> SExtAmount;
851 }
852
853 if (Offset)
854 O << '(';
855 EmitConstantValueOnly(ptrVal);
856 if (Offset > 0)
857 O << ") + " << Offset;
858 else
859 O << ") - " << -Offset;
860 return;
861 }
862 case Instruction::BitCast:
863 return EmitConstantValueOnly(CE->getOperand(0));
864
865 case Instruction::IntToPtr: {
866 // Handle casts to pointers by changing them into casts to the appropriate
867 // integer type. This promotes constant folding and simplifies this code.
868 const TargetData *TD = TM.getTargetData();
869 Constant *Op = CE->getOperand(0);
870 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
871 false/*ZExt*/);
872 return EmitConstantValueOnly(Op);
873 }
874
875 case Instruction::PtrToInt: {
876 // Support only foldable casts to/from pointers that can be eliminated by
877 // changing the pointer to the appropriately sized integer type.
878 Constant *Op = CE->getOperand(0);
879 const Type *Ty = CE->getType();
880 const TargetData *TD = TM.getTargetData();
881
882 // We can emit the pointer value into this slot if the slot is an
883 // integer slot greater or equal to the size of the pointer.
884 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
885 return EmitConstantValueOnly(Op);
886
887 O << "((";
888 EmitConstantValueOnly(Op);
889 APInt ptrMask =
890 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
891
892 SmallString<40> S;
893 ptrMask.toStringUnsigned(S);
894 O << ") & " << S.str() << ')';
895 return;
896 }
897
898 case Instruction::Trunc:
899 // We emit the value and depend on the assembler to truncate the generated
900 // expression properly. This is important for differences between
901 // blockaddress labels. Since the two labels are in the same function, it
902 // is reasonable to treat their delta as a 32-bit value.
903 return EmitConstantValueOnly(CE->getOperand(0));
904
905 case Instruction::Add:
906 case Instruction::Sub:
907 case Instruction::And:
908 case Instruction::Or:
909 case Instruction::Xor:
910 O << '(';
911 EmitConstantValueOnly(CE->getOperand(0));
912 O << ')';
913 switch (CE->getOpcode()) {
914 case Instruction::Add:
915 O << " + ";
916 break;
917 case Instruction::Sub:
918 O << " - ";
919 break;
920 case Instruction::And:
921 O << " & ";
922 break;
923 case Instruction::Or:
924 O << " | ";
925 break;
926 case Instruction::Xor:
927 O << " ^ ";
928 break;
929 default:
930 break;
931 }
932 O << '(';
933 EmitConstantValueOnly(CE->getOperand(1));
934 O << ')';
935 break;
Chris Lattnera80ba712004-08-16 23:15:22 +0000936 }
937}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000938
Chris Lattner2980cef2005-11-10 18:06:33 +0000939/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000940/// the predicate isString is true.
941///
David Greene71847812009-07-14 20:18:05 +0000942static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000943 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000944 assert(CVA->isString() && "Array is not string compatible!");
945
Dan Gohmand19a53b2008-06-30 22:03:41 +0000946 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000947 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000948 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000949 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000950 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000951 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000952 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000953}
954
Jeff Cohenc884db42006-05-02 01:16:28 +0000955/// EmitString - Emit a zero-byte-terminated string constant.
956///
957void AsmPrinter::EmitString(const ConstantArray *CVA) const {
958 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000959 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000960 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000961 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000962 printAsCString(O, CVA, NumElts-1);
963 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000964 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000965 printAsCString(O, CVA, NumElts);
966 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000967 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000968}
969
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000970void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
971 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000972 if (CVA->isString()) {
973 EmitString(CVA);
974 } else { // Not a string. Print the values in successive locations
975 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000976 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000977 }
978}
979
980void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
981 const VectorType *PTy = CP->getType();
982
983 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
984 EmitGlobalConstant(CP->getOperand(I));
985}
986
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000987void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
988 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000989 // Print the fields in successive locations. Pad to align if needed!
990 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +0000991 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000992 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
993 uint64_t sizeSoFar = 0;
994 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
995 const Constant* field = CVS->getOperand(i);
996
997 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +0000998 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000999 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1000 - cvsLayout->getElementOffset(i)) - fieldSize;
1001 sizeSoFar += fieldSize + padSize;
1002
1003 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001004 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001005
1006 // Insert padding - this may include padding to increase the size of the
1007 // current field up to the ABI size (if the struct is not packed) as well
1008 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001009 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001010 }
1011 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1012 "Layout of constant struct may be incorrect!");
1013}
1014
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001015void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1016 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001017 // FP Constants are printed as integer constants to avoid losing
1018 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001019 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001020 const TargetData *TD = TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001021 if (CFP->getType()->isDoubleTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001022 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1023 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001024 if (MAI->getData64bitsDirective(AddrSpace)) {
1025 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001026 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001027 O.PadToColumn(MAI->getCommentColumn());
1028 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001029 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001030 O << '\n';
1031 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001032 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001033 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001034 O.PadToColumn(MAI->getCommentColumn());
1035 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001036 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001037 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001038 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001039 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001040 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001041 O.PadToColumn(MAI->getCommentColumn());
1042 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001043 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001044 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001045 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001046 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001047 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001048 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001049 O.PadToColumn(MAI->getCommentColumn());
1050 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001051 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001052 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001053 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001054 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001055 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001056 O.PadToColumn(MAI->getCommentColumn());
1057 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001058 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001059 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001060 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001061 }
1062 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001063 }
1064
1065 if (CFP->getType()->isFloatTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001066 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001067 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001068 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001069 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001070 O.PadToColumn(MAI->getCommentColumn());
1071 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001072 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001073 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001074 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001075 }
1076
1077 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001078 // all long double variants are printed as hex
1079 // api needed to prevent premature destruction
1080 APInt api = CFP->getValueAPF().bitcastToAPInt();
1081 const uint64_t *p = api.getRawData();
1082 // Convert to double so we can print the approximate val as a comment.
1083 APFloat DoubleVal = CFP->getValueAPF();
1084 bool ignored;
1085 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1086 &ignored);
1087 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001088 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001089 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001090 O.PadToColumn(MAI->getCommentColumn());
1091 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001092 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001093 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001094 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001095 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001096 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001097 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001098 O.PadToColumn(MAI->getCommentColumn());
1099 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001100 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001101 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001102 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001103 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001104 O.PadToColumn(MAI->getCommentColumn());
1105 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001106 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001107 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001108 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001109 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001110 O.PadToColumn(MAI->getCommentColumn());
1111 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001112 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001113 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001114 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001115 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001116 O.PadToColumn(MAI->getCommentColumn());
1117 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001118 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001119 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001120 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001121 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001122 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001123 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001124 O.PadToColumn(MAI->getCommentColumn());
1125 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001126 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001127 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001128 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001129 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001130 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001131 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001132 O.PadToColumn(MAI->getCommentColumn());
1133 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001134 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001135 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001136 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001137 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001138 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001139 O.PadToColumn(MAI->getCommentColumn());
1140 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001141 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001142 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001143 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001144 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001145 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001146 O.PadToColumn(MAI->getCommentColumn());
1147 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001148 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001149 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001150 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001151 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001152 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001153 O.PadToColumn(MAI->getCommentColumn());
1154 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001155 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001156 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001157 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001158 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001159 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1160 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001161 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001162 }
1163
1164 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001165 // all long double variants are printed as hex
1166 // api needed to prevent premature destruction
1167 APInt api = CFP->getValueAPF().bitcastToAPInt();
1168 const uint64_t *p = api.getRawData();
1169 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001170 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001171 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001172 O.PadToColumn(MAI->getCommentColumn());
1173 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001174 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001175 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001176 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001177 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001178 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001179 O.PadToColumn(MAI->getCommentColumn());
1180 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001181 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001182 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001183 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001184 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001185 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001186 O.PadToColumn(MAI->getCommentColumn());
1187 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001188 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001189 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001190 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001191 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001192 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001193 O.PadToColumn(MAI->getCommentColumn());
1194 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001195 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001196 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001197 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001198 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001199 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001200 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001201 O.PadToColumn(MAI->getCommentColumn());
1202 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001203 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001204 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001205 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001206 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001207 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001208 O.PadToColumn(MAI->getCommentColumn());
1209 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001210 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001211 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001212 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001213 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001214 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001215 O.PadToColumn(MAI->getCommentColumn());
1216 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001217 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001218 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001219 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001220 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001221 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001222 O.PadToColumn(MAI->getCommentColumn());
1223 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001224 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001225 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001226 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001227 }
1228 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001229 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001230}
1231
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001232void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1233 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001234 const TargetData *TD = TM.getTargetData();
1235 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001236 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001237
1238 // We don't expect assemblers to support integer data directives
1239 // for more than 64 bits, so we emit the data in at most 64-bit
1240 // quantities at a time.
1241 const uint64_t *RawData = CI->getValue().getRawData();
1242 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1243 uint64_t Val;
1244 if (TD->isBigEndian())
1245 Val = RawData[e - i - 1];
1246 else
1247 Val = RawData[i];
1248
Chris Lattner5979dff2010-01-13 04:38:16 +00001249 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001250 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Chris Lattner5979dff2010-01-13 04:38:16 +00001251 continue;
Dan Gohman00d448a2008-12-22 21:14:27 +00001252 }
Chris Lattner5979dff2010-01-13 04:38:16 +00001253
1254 // Emit two 32-bit chunks, order depends on endianness.
1255 unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
1256 const char *FirstName = " least", *SecondName = " most";
1257 if (TD->isBigEndian()) {
1258 std::swap(FirstChunk, SecondChunk);
1259 std::swap(FirstName, SecondName);
1260 }
1261
1262 O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
1263 if (VerboseAsm) {
1264 O.PadToColumn(MAI->getCommentColumn());
1265 O << MAI->getCommentString()
1266 << FirstName << " significant half of i64 " << Val;
1267 }
1268 O << '\n';
1269
1270 O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
1271 if (VerboseAsm) {
1272 O.PadToColumn(MAI->getCommentColumn());
1273 O << MAI->getCommentString()
1274 << SecondName << " significant half of i64 " << Val;
1275 }
1276 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001277 }
1278}
1279
Chris Lattner25045bd2005-11-21 07:51:36 +00001280/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001281void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001282 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001283 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001284 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001285
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001286 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001287 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001288 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001289 }
1290
1291 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001292 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001293 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001294 }
1295
1296 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001297 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001298 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001299 }
1300
1301 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001302 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001303 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001304 }
1305
1306 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1307 // If we can directly emit an 8-byte constant, do it.
1308 if (Size == 8)
1309 if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
1310 O << Data64Dir << CI->getZExtValue() << '\n';
1311 return;
1312 }
1313
Dan Gohman00d448a2008-12-22 21:14:27 +00001314 // Small integers are handled below; large integers are handled here.
1315 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001316 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001317 return;
1318 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001319 }
1320
1321 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001322 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001323 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001324 }
1325
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001326 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001327 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001328 if (VerboseAsm) {
1329 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1330 SmallString<40> S;
1331 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001332 O.PadToColumn(MAI->getCommentColumn());
1333 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001334 }
Scott Micheleefc8452008-06-03 15:39:51 +00001335 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001336 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001337}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001338
Chris Lattnerfad86b02008-08-17 07:19:36 +00001339void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001340 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001341 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001342}
1343
Chris Lattner3ce9b672006-09-26 23:59:50 +00001344/// PrintSpecial - Print information related to the specified machine instr
1345/// that is independent of the operand, and may be independent of the instr
1346/// itself. This can be useful for portably encoding the comment character
1347/// or other bits of target-specific knowledge into the asmstrings. The
1348/// syntax used is ${:comment}. Targets can override this to add support
1349/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001350void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001351 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001352 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001353 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001354 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001355 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001356 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001357 // Comparing the address of MI isn't sufficient, because machineinstrs may
1358 // be allocated to the same address across functions.
1359 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1360
Owen Andersonbd58edf2009-06-24 22:28:12 +00001361 // If this is a new LastFn instruction, bump the counter.
1362 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001363 ++Counter;
1364 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001365 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001366 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001367 O << Counter;
1368 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001369 std::string msg;
1370 raw_string_ostream Msg(msg);
1371 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001372 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001373 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001374 }
1375}
1376
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001377/// processDebugLoc - Processes the debug information of each machine
1378/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001379void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1380 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001381 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1382 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001383 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001384 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001385 if (DL.isUnknown())
1386 return;
Devang Patel6b61f582010-01-16 06:09:35 +00001387 DILocation CurDLT = MF->getDILocation(DL);
1388 if (CurDLT.getScope().isNull())
Devang Patel53bb5c92009-11-10 23:06:00 +00001389 return;
1390
1391 if (BeforePrintingInsn) {
Devang Patel6b61f582010-01-16 06:09:35 +00001392 if (CurDLT.getNode() != PrevDLT.getNode()) {
1393 unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(),
1394 CurDLT.getColumnNumber(),
1395 CurDLT.getScope().getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001396 printLabel(L);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001397 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001398 DW->BeginScope(MI, L);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001399 PrevDLT = CurDLT;
1400 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001401 } else {
1402 // After printing instruction
1403 DW->EndScope(MI);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001404 }
1405}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001406
Devang Patel53bb5c92009-11-10 23:06:00 +00001407
Chris Lattner0264d1a2006-01-27 02:10:10 +00001408/// printInlineAsm - This method formats and prints the specified machine
1409/// instruction that is an inline asm.
1410void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001411 unsigned NumOperands = MI->getNumOperands();
1412
1413 // Count the number of register definitions.
1414 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001415 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001416 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001417 assert(NumDefs != NumOperands-1 && "No asm string?");
1418
Dan Gohmand735b802008-10-03 15:45:36 +00001419 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001420
1421 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001422 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001423
Dan Gohman40c57862009-11-06 00:04:54 +00001424 O << '\t';
1425
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001426 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1427 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001428 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001429 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1430 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001431 return;
1432 }
1433
Chris Lattner33adcfb2009-08-22 21:43:10 +00001434 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001435
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001436 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001437 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001438
Chris Lattner66099132006-02-01 22:41:11 +00001439 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1440 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001441
Chris Lattner66099132006-02-01 22:41:11 +00001442 while (*LastEmitted) {
1443 switch (*LastEmitted) {
1444 default: {
1445 // Not a special case, emit the string section literally.
1446 const char *LiteralEnd = LastEmitted+1;
1447 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001448 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001449 ++LiteralEnd;
1450 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1451 O.write(LastEmitted, LiteralEnd-LastEmitted);
1452 LastEmitted = LiteralEnd;
1453 break;
1454 }
Chris Lattner1c059972006-05-05 21:47:05 +00001455 case '\n':
1456 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001457 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001458 break;
Chris Lattner66099132006-02-01 22:41:11 +00001459 case '$': {
1460 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001461 bool Done = true;
1462
1463 // Handle escapes.
1464 switch (*LastEmitted) {
1465 default: Done = false; break;
1466 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001467 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1468 O << '$';
1469 ++LastEmitted; // Consume second '$' character.
1470 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001471 case '(': // $( -> same as GCC's { character.
1472 ++LastEmitted; // Consume '(' character.
1473 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001474 llvm_report_error("Nested variants found in inline asm string: '"
1475 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001476 }
1477 CurVariant = 0; // We're in the first variant now.
1478 break;
1479 case '|':
1480 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001481 if (CurVariant == -1)
1482 O << '|'; // this is gcc's behavior for | outside a variant
1483 else
1484 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001485 break;
1486 case ')': // $) -> same as GCC's } char.
1487 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001488 if (CurVariant == -1)
1489 O << '}'; // this is gcc's behavior for } outside a variant
1490 else
1491 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001492 break;
Chris Lattner66099132006-02-01 22:41:11 +00001493 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001494 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001495
1496 bool HasCurlyBraces = false;
1497 if (*LastEmitted == '{') { // ${variable}
1498 ++LastEmitted; // Consume '{' character.
1499 HasCurlyBraces = true;
1500 }
1501
Chris Lattner3e0cc262009-03-10 05:37:13 +00001502 // If we have ${:foo}, then this is not a real operand reference, it is a
1503 // "magic" string reference, just like in .td files. Arrange to call
1504 // PrintSpecial.
1505 if (HasCurlyBraces && *LastEmitted == ':') {
1506 ++LastEmitted;
1507 const char *StrStart = LastEmitted;
1508 const char *StrEnd = strchr(StrStart, '}');
1509 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001510 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1511 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001512 }
1513
1514 std::string Val(StrStart, StrEnd);
1515 PrintSpecial(MI, Val.c_str());
1516 LastEmitted = StrEnd+1;
1517 break;
1518 }
1519
Chris Lattner66099132006-02-01 22:41:11 +00001520 const char *IDStart = LastEmitted;
1521 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001522 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001523 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1524 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001525 llvm_report_error("Bad $ operand number in inline asm string: '"
1526 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001527 }
1528 LastEmitted = IDEnd;
1529
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001530 char Modifier[2] = { 0, 0 };
1531
Chris Lattner66099132006-02-01 22:41:11 +00001532 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001533 // If we have curly braces, check for a modifier character. This
1534 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1535 if (*LastEmitted == ':') {
1536 ++LastEmitted; // Consume ':' character.
1537 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001538 llvm_report_error("Bad ${:} expression in inline asm string: '"
1539 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001540 }
1541
1542 Modifier[0] = *LastEmitted;
1543 ++LastEmitted; // Consume modifier character.
1544 }
1545
Chris Lattner66099132006-02-01 22:41:11 +00001546 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001547 llvm_report_error("Bad ${} expression in inline asm string: '"
1548 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001549 }
1550 ++LastEmitted; // Consume '}' character.
1551 }
1552
1553 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001554 llvm_report_error("Invalid $ operand number in inline asm string: '"
1555 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001556 }
1557
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001558 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001559 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001560 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1561 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001562
1563 bool Error = false;
1564
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001565 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001566 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001567 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001568 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001569 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001570 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001571
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001572 if (OpNo >= MI->getNumOperands()) {
1573 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001574 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001575 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001576 ++OpNo; // Skip over the ID number.
1577
Chris Lattner10b318b2010-01-17 21:43:43 +00001578 if (Modifier[0] == 'l') // labels are target independent
1579 O << *GetMBBSymbol(MI->getOperand(OpNo).getMBB()->getNumber());
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001580 else {
1581 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001582 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001583 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1584 Modifier[0] ? Modifier : 0);
1585 } else {
1586 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1587 Modifier[0] ? Modifier : 0);
1588 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001589 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001590 }
1591 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001592 std::string msg;
1593 raw_string_ostream Msg(msg);
Chris Lattner10b318b2010-01-17 21:43:43 +00001594 Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001595 MI->print(Msg);
1596 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001597 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001598 }
Chris Lattner66099132006-02-01 22:41:11 +00001599 break;
1600 }
Chris Lattner66099132006-02-01 22:41:11 +00001601 }
1602 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001603 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001604}
1605
Evan Chengda47e6e2008-03-15 00:03:38 +00001606/// printImplicitDef - This method prints the specified machine instruction
1607/// that is an implicit def.
1608void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001609 if (!VerboseAsm) return;
1610 O.PadToColumn(MAI->getCommentColumn());
1611 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001612 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001613}
1614
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001615void AsmPrinter::printKill(const MachineInstr *MI) const {
1616 if (!VerboseAsm) return;
1617 O.PadToColumn(MAI->getCommentColumn());
1618 O << MAI->getCommentString() << " kill:";
1619 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1620 const MachineOperand &op = MI->getOperand(n);
1621 assert(op.isReg() && "KILL instruction must have only register operands");
1622 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1623 }
1624}
1625
Jim Laskey1ee29252007-01-26 14:34:52 +00001626/// printLabel - This method prints a local label used by debug and
1627/// exception handling tables.
1628void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001629 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001630}
1631
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001632void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001633 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001634}
1635
Chris Lattner66099132006-02-01 22:41:11 +00001636/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1637/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001638/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001639bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001640 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001641 // Target doesn't support this yet!
1642 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001643}
Chris Lattnerdd260332006-02-24 20:21:58 +00001644
1645bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1646 unsigned AsmVariant,
1647 const char *ExtraCode) {
1648 // Target doesn't support this yet!
1649 return true;
1650}
Nate Begeman37efe672006-04-22 18:53:45 +00001651
Dan Gohman29cbade2009-11-20 23:18:13 +00001652MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1653 const char *Suffix) const {
1654 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001655}
1656
1657MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001658 const BasicBlock *BB,
1659 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001660 assert(BB->hasName() &&
1661 "Address of anonymous basic block not supported yet!");
1662
Dan Gohman568a3be2009-11-05 23:14:35 +00001663 // This code must use the function name itself, and not the function number,
1664 // since it must be possible to generate the label name from within other
1665 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001666 SmallString<60> FnName;
1667 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001668
Chris Lattner2f8cc262010-01-13 07:30:49 +00001669 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001670 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001671 Mang->getNameWithPrefix(NameResult,
1672 StringRef("BA") + Twine((unsigned)FnName.size()) +
1673 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1674 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001675
Chris Lattner48130352010-01-13 06:38:18 +00001676 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001677}
1678
Chris Lattner7cb384d2009-09-12 23:02:08 +00001679MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1680 SmallString<60> Name;
1681 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1682 << getFunctionNumber() << '_' << MBBID;
1683
1684 return OutContext.GetOrCreateSymbol(Name.str());
1685}
1686
Chris Lattner6b04ede2010-01-15 23:18:17 +00001687/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1688/// value.
1689MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1690 SmallString<60> NameStr;
1691 Mang->getNameWithPrefix(NameStr, GV, false);
1692 return OutContext.GetOrCreateSymbol(NameStr.str());
1693}
1694
Chris Lattner7a2ba942010-01-16 18:37:32 +00001695/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
Chris Lattnerd588b972010-01-15 23:25:11 +00001696/// global value name as its base, with the specified suffix, and where the
Chris Lattner7a2ba942010-01-16 18:37:32 +00001697/// symbol is forced to have private linkage if ForcePrivate is true.
1698MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1699 StringRef Suffix,
1700 bool ForcePrivate) const {
Chris Lattnerd588b972010-01-15 23:25:11 +00001701 SmallString<60> NameStr;
Chris Lattner7a2ba942010-01-16 18:37:32 +00001702 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
Chris Lattnerd588b972010-01-15 23:25:11 +00001703 NameStr.append(Suffix.begin(), Suffix.end());
1704 return OutContext.GetOrCreateSymbol(NameStr.str());
1705}
1706
Chris Lattner6b04ede2010-01-15 23:18:17 +00001707/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1708/// ExternalSymbol.
1709MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1710 SmallString<60> NameStr;
1711 Mang->getNameWithPrefix(NameStr, Sym);
1712 return OutContext.GetOrCreateSymbol(NameStr.str());
1713}
1714
Chris Lattner7cb384d2009-09-12 23:02:08 +00001715
Chris Lattner70a54c02009-09-13 18:25:37 +00001716/// EmitBasicBlockStart - This method prints the label for the specified
1717/// MachineBasicBlock, an alignment (if present) and a comment describing
1718/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001719void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001720 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001721 if (unsigned Align = MBB->getAlignment())
1722 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001723
Dan Gohmanb1cac332009-10-30 01:34:35 +00001724 // If the block has its address taken, emit a special label to satisfy
1725 // references to the block. This is done so that we don't need to
1726 // remember the number of this label, and so that we can make
1727 // forward references to labels without knowing what their numbers
1728 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001729 if (MBB->hasAddressTaken()) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001730 O << *GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1731 MBB->getBasicBlock());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001732 O << ':';
1733 if (VerboseAsm) {
1734 O.PadToColumn(MAI->getCommentColumn());
1735 O << MAI->getCommentString() << " Address Taken";
1736 }
1737 O << '\n';
1738 }
1739
Dan Gohmanb1cac332009-10-30 01:34:35 +00001740 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001741 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1742 if (VerboseAsm)
1743 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1744 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +00001745 O << *GetMBBSymbol(MBB->getNumber()) << ':';
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001746 if (!VerboseAsm)
1747 O << '\n';
1748 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001749
Dan Gohmanb1cac332009-10-30 01:34:35 +00001750 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001751 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001752 if (const BasicBlock *BB = MBB->getBasicBlock())
1753 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001754 O.PadToColumn(MAI->getCommentColumn());
1755 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001756 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001757 }
David Greeneb71d1b22009-08-10 16:38:07 +00001758
Chris Lattner70a54c02009-09-13 18:25:37 +00001759 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001760 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001761 }
Nate Begeman37efe672006-04-22 18:53:45 +00001762}
Nate Begeman52a51e382006-08-12 21:29:52 +00001763
Evan Chengcc415862007-11-09 01:32:10 +00001764/// printPICJumpTableSetLabel - This method prints a set label for the
1765/// specified MachineBasicBlock for a jumptable entry.
1766void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1767 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001768 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001769 return;
1770
Chris Lattner33adcfb2009-08-22 21:43:10 +00001771 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Chris Lattner10b318b2010-01-17 21:43:43 +00001772 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','
1773 << *GetMBBSymbol(MBB->getNumber())
1774 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001775 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001776}
Evan Chengd6594ae2006-09-12 21:00:35 +00001777
Evan Chengcc415862007-11-09 01:32:10 +00001778void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1779 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001780 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001781 return;
1782
Chris Lattner33adcfb2009-08-22 21:43:10 +00001783 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001784 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner10b318b2010-01-17 21:43:43 +00001785 << "_set_" << MBB->getNumber() << ','
1786 << *GetMBBSymbol(MBB->getNumber())
1787 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001788 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001789}
1790
Evan Chengd6594ae2006-09-12 21:00:35 +00001791/// printDataDirective - This method prints the asm directive for the
1792/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001793void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001794 const TargetData *TD = TM.getTargetData();
1795 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001796 case Type::FloatTyID: case Type::DoubleTyID:
1797 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1798 assert(0 && "Should have already output floating point constant.");
1799 default:
1800 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001801 case Type::IntegerTyID: {
1802 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1803 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001804 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001805 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001806 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001807 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001808 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001809 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001810 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001811 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001812 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001813 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001814 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001815 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001816 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001817 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001818 case Type::PointerTyID:
1819 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001820 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001821 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001822 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001823 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001824 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001825 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001826 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001827 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001828 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001829 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001830 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001831 }
1832}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001833
Chris Lattner53d4d782010-01-15 23:38:51 +00001834void AsmPrinter::printVisibility(const MCSymbol *Sym,
1835 unsigned Visibility) const {
1836 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001837 if (const char *Directive = MAI->getHiddenDirective())
1838 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001839 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001840 if (const char *Directive = MAI->getProtectedDirective())
1841 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001842 }
1843}
1844
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001845void AsmPrinter::printOffset(int64_t Offset) const {
1846 if (Offset > 0)
1847 O << '+' << Offset;
1848 else if (Offset < 0)
1849 O << Offset;
1850}
1851
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001852GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1853 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001854 return 0;
1855
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001856 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001857 if (GCPI != GCMetadataPrinters.end())
1858 return GCPI->second;
1859
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001860 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001861
1862 for (GCMetadataPrinterRegistry::iterator
1863 I = GCMetadataPrinterRegistry::begin(),
1864 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1865 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001866 GCMetadataPrinter *GMP = I->instantiate();
1867 GMP->S = S;
1868 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1869 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001870 }
1871
Chris Lattner103289e2009-08-23 07:19:13 +00001872 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001873 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001874}
David Greene014700c2009-07-13 20:25:48 +00001875
1876/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001877void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001878 if (!VerboseAsm)
1879 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001880
David Greene1924aab2009-11-13 21:34:57 +00001881 bool Newline = false;
1882
1883 if (!MI.getDebugLoc().isUnknown()) {
Devang Patel6b61f582010-01-16 06:09:35 +00001884 DILocation DLT = MF->getDILocation(MI.getDebugLoc());
David Greene1924aab2009-11-13 21:34:57 +00001885
1886 // Print source line info.
1887 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001888 O << MAI->getCommentString() << ' ';
Devang Patel6b61f582010-01-16 06:09:35 +00001889 DIScope Scope = DLT.getScope();
Dan Gohman3b9bc042009-12-05 00:23:29 +00001890 // Omit the directory, because it's likely to be long and uninteresting.
1891 if (!Scope.isNull())
1892 O << Scope.getFilename();
1893 else
1894 O << "<unknown>";
Devang Patel6b61f582010-01-16 06:09:35 +00001895 O << ':' << DLT.getLineNumber();
1896 if (DLT.getColumnNumber() != 0)
1897 O << ':' << DLT.getColumnNumber();
David Greene1924aab2009-11-13 21:34:57 +00001898 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001899 }
David Greene1924aab2009-11-13 21:34:57 +00001900
David Greeneddff9412009-11-16 15:12:23 +00001901 // Check for spills and reloads
1902 int FI;
1903
1904 const MachineFrameInfo *FrameInfo =
1905 MI.getParent()->getParent()->getFrameInfo();
1906
1907 // We assume a single instruction only has a spill or reload, not
1908 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001909 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001910 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1911 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001912 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001913 if (Newline) O << '\n';
1914 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001915 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001916 Newline = true;
1917 }
1918 }
David Greenef7ea2a52009-12-04 22:46:04 +00001919 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001920 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1921 if (Newline) O << '\n';
1922 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001923 O << MAI->getCommentString() << ' '
1924 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001925 Newline = true;
1926 }
1927 }
1928 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1929 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001930 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001931 if (Newline) O << '\n';
1932 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001933 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001934 Newline = true;
1935 }
1936 }
David Greenef7ea2a52009-12-04 22:46:04 +00001937 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001938 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1939 if (Newline) O << '\n';
1940 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001941 O << MAI->getCommentString() << ' '
1942 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001943 Newline = true;
1944 }
1945 }
1946
1947 // Check for spill-induced copies
1948 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1949 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1950 SrcSubIdx, DstSubIdx)) {
1951 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1952 if (Newline) O << '\n';
1953 O.PadToColumn(MAI->getCommentColumn());
1954 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001955 }
1956 }
David Greene014700c2009-07-13 20:25:48 +00001957}
1958
David Greenefe37ab32009-08-18 19:22:55 +00001959/// PrintChildLoopComment - Print comments about child loops within
1960/// the loop for this basic block, with nesting.
1961///
1962static void PrintChildLoopComment(formatted_raw_ostream &O,
1963 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001964 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001965 int FunctionNumber) {
1966 // Add child loop information
1967 for(MachineLoop::iterator cl = loop->begin(),
1968 clend = loop->end();
1969 cl != clend;
1970 ++cl) {
1971 MachineBasicBlock *Header = (*cl)->getHeader();
1972 assert(Header && "No header for loop");
1973
1974 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001975 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001976
Chris Lattner33adcfb2009-08-22 21:43:10 +00001977 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001978 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001979 << " Child Loop BB" << FunctionNumber << "_"
1980 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1981
Chris Lattner33adcfb2009-08-22 21:43:10 +00001982 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001983 }
1984}
1985
David Greeneb71d1b22009-08-10 16:38:07 +00001986/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00001987void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00001988 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00001989 // Add loop depth information
1990 const MachineLoop *loop = LI->getLoopFor(&MBB);
1991
1992 if (loop) {
1993 // Print a newline after bb# annotation.
1994 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00001995 O.PadToColumn(MAI->getCommentColumn());
1996 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00001997 << '\n';
1998
Chris Lattner33adcfb2009-08-22 21:43:10 +00001999 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00002000
2001 MachineBasicBlock *Header = loop->getHeader();
2002 assert(Header && "No header for loop");
2003
2004 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002005 O << MAI->getCommentString() << " Loop Header";
2006 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00002007 }
2008 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002009 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00002010 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
2011 }
2012
2013 if (loop->empty()) {
2014 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002015 O.PadToColumn(MAI->getCommentColumn());
2016 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00002017 }
2018
2019 // Add parent loop information
2020 for (const MachineLoop *CurLoop = loop->getParentLoop();
2021 CurLoop;
2022 CurLoop = CurLoop->getParentLoop()) {
2023 MachineBasicBlock *Header = CurLoop->getHeader();
2024 assert(Header && "No header for loop");
2025
2026 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002027 O.PadToColumn(MAI->getCommentColumn());
2028 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002029 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002030 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00002031 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
2032 }
2033 }
2034 }
2035}