blob: bb1f1d31d143bb72b9641643f516bd860f10be54 [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;
159 O << MAI->getWeakRefDirective();
160 GetGlobalValueSymbol(I)->print(O, MAI);
161 O << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000162 }
163
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000164 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000165 if (!I->hasExternalWeakLinkage()) continue;
166 O << MAI->getWeakRefDirective();
167 GetGlobalValueSymbol(I)->print(O, MAI);
168 O << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000169 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000170 }
171
Chris Lattner33adcfb2009-08-22 21:43:10 +0000172 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000173 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000174 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000175 I != E; ++I) {
Chris Lattner5c40e692010-01-16 01:17:26 +0000176 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000177
178 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner5c40e692010-01-16 01:17:26 +0000179 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000180
Chris Lattner5c40e692010-01-16 01:17:26 +0000181 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective()) {
182 O << "\t.globl\t";
183 Name->print(O, MAI);
184 O << '\n';
185 } else if (I->hasWeakLinkage()) {
186 O << MAI->getWeakRefDirective();
187 Name->print(O, MAI);
188 O << '\n';
189 } else {
Chris Lattner10595492010-01-16 01:37:14 +0000190 assert(I->hasLocalLinkage() && "Invalid alias linkage");
Chris Lattner5c40e692010-01-16 01:17:26 +0000191 }
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000192
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000193 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000194
Chris Lattner5c40e692010-01-16 01:17:26 +0000195 O << MAI->getSetDirective() << ' ';
196 Name->print(O, MAI);
197 O << ", ";
198 Target->print(O, MAI);
199 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000200 }
201 }
202
Duncan Sands1465d612009-01-28 13:14:17 +0000203 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000204 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
205 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
206 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000207 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000208
Dan Gohmana779a982008-05-05 00:28:39 +0000209 // If we don't have any trampolines, then we don't require stack memory
210 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000211 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000212 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000213 if (MAI->getNonexecutableStackDirective())
214 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000215
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000216
217 // Allow the target to emit any magic that it wants at the end of the file,
218 // after everything else has gone out.
219 EmitEndOfAsmFile(M);
220
Chris Lattnera80ba712004-08-16 23:15:22 +0000221 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000222 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000223
224 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000225 return false;
226}
227
Chris Lattner25045bd2005-11-21 07:51:36 +0000228void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner412c3a52010-01-16 01:24:10 +0000229 // Get the function symbol.
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000230 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000231 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000232
Chris Lattner25d812b2009-09-16 00:35:39 +0000233 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000234 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000235}
236
Evan Cheng1606e8e2009-03-13 07:51:59 +0000237namespace {
238 // SectionCPs - Keep track the alignment, constpool entries per Section.
239 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000240 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000241 unsigned Alignment;
242 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000243 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000244 };
245}
246
Chris Lattner3b4fd322005-11-21 08:25:09 +0000247/// EmitConstantPool - Print to the current output stream assembly
248/// representations of the constants in the constant pool MCP. This is
249/// used to print out constants which have been "spilled to memory" by
250/// the code generator.
251///
252void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000253 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000254 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000255
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000256 // Calculate sections for constant pool entries. We collect entries to go into
257 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000258 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000259 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000260 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000261 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000262
263 SectionKind Kind;
264 switch (CPE.getRelocationInfo()) {
265 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000266 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000267 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000268 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000269 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000270 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000271 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000272 case 4: Kind = SectionKind::getMergeableConst4(); break;
273 case 8: Kind = SectionKind::getMergeableConst8(); break;
274 case 16: Kind = SectionKind::getMergeableConst16();break;
275 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000276 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000277 }
278
Chris Lattner83d77fa2009-08-01 23:46:12 +0000279 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000280
Evan Cheng1606e8e2009-03-13 07:51:59 +0000281 // The number of sections are small, just do a linear search from the
282 // last section to the first.
283 bool Found = false;
284 unsigned SecIdx = CPSections.size();
285 while (SecIdx != 0) {
286 if (CPSections[--SecIdx].S == S) {
287 Found = true;
288 break;
289 }
290 }
291 if (!Found) {
292 SecIdx = CPSections.size();
293 CPSections.push_back(SectionCPs(S, Align));
294 }
295
296 if (Align > CPSections[SecIdx].Alignment)
297 CPSections[SecIdx].Alignment = Align;
298 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000299 }
300
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000301 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000302 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000303 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000304 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000305
Evan Cheng1606e8e2009-03-13 07:51:59 +0000306 unsigned Offset = 0;
307 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
308 unsigned CPI = CPSections[i].CPEs[j];
309 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000310
Chris Lattner3029f922006-02-09 04:46:04 +0000311 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000312 unsigned AlignMask = CPE.getAlignment() - 1;
313 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
314 EmitZeros(NewOffset - Offset);
315
316 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000317 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000318
Chris Lattner33adcfb2009-08-22 21:43:10 +0000319 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000320 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000321 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000322 O.PadToColumn(MAI->getCommentColumn());
323 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000324 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000325 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000326 O << '\n';
327 if (CPE.isMachineConstantPoolEntry())
328 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
329 else
330 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000331 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000332 }
333}
334
Nate Begeman37efe672006-04-22 18:53:45 +0000335/// EmitJumpTableInfo - Print assembly representations of the jump tables used
336/// by the current function to the current output stream.
337///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000338void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
339 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000340 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
341 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000342
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000343 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000344
Nate Begeman2f1ae882006-07-27 01:13:04 +0000345 // Pick the directive to use to print the jump table entries, and switch to
346 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000347 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000348
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000349 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000350 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000351 if (F->isWeakForLinker() ||
352 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000353 // In PIC mode, we need to emit the jump table to the same section as the
354 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000355 // We should also do if the section name is NULL or function is declared in
356 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000357 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
358 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000359 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000360 // Otherwise, drop it in the readonly section.
361 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000362 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000363 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000364 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000365 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000366
367 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000368
Nate Begeman37efe672006-04-22 18:53:45 +0000369 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000370 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000371
372 // If this jump table was deleted, ignore it.
373 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000374
375 // For PIC codegen, if possible we want to use the SetDirective to reduce
376 // the number of relocations the assembler will generate for the jump table.
377 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000378 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000379 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000380 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000381 if (EmittedSets.insert(JTBBs[ii]))
382 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000383
Chris Lattner7c301912009-09-13 19:02:16 +0000384 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000385 // before each jump table. The first label is never referenced, but tells
386 // the assembler and linker the extents of the jump table object. The
387 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000388 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
389 O << MAI->getLinkerPrivateGlobalPrefix()
390 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000391 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000392
Chris Lattner33adcfb2009-08-22 21:43:10 +0000393 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000394 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000395
Nate Begeman37efe672006-04-22 18:53:45 +0000396 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000397 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000398 O << '\n';
399 }
400 }
401}
402
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000403void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
404 const MachineBasicBlock *MBB,
405 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000406 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000407
408 // Use JumpTableDirective otherwise honor the entry size from the jump table
409 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000410 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000411 bool HadJTEntryDirective = JTEntryDirective != NULL;
412 if (!HadJTEntryDirective) {
413 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000414 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000415 }
416
417 O << JTEntryDirective << ' ';
418
419 // If we have emitted set directives for the jump table entries, print
420 // them rather than the entries themselves. If we're emitting PIC, then
421 // emit the table entries as differences between two text section labels.
422 // If we're emitting non-PIC code, then emit the entries as direct
423 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000424 if (!isPIC) {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000425 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000426 } else if (MAI->getSetDirective()) {
427 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000428 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000429 } else {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000430 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000431 // If the arch uses custom Jump Table directives, don't calc relative to
432 // JT
433 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000434 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000435 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000436 }
437}
438
439
Chris Lattnered138932005-12-13 06:32:10 +0000440/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
441/// special global used by LLVM. If so, emit it and return true, otherwise
442/// do nothing and return false.
443bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000444 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000445 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000446 EmitLLVMUsedList(GV->getInitializer());
447 return true;
448 }
449
Chris Lattner401e10c2009-07-20 06:14:25 +0000450 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000451 if (GV->getSection() == "llvm.metadata" ||
452 GV->hasAvailableExternallyLinkage())
453 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000454
455 if (!GV->hasAppendingLinkage()) return false;
456
457 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000458
Evan Cheng916d07c2007-06-04 20:39:18 +0000459 const TargetData *TD = TM.getTargetData();
460 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000461 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000462 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000463 EmitAlignment(Align, 0);
464 EmitXXStructorList(GV->getInitializer());
465 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000466 }
467
Chris Lattnerf231c072009-03-09 05:52:15 +0000468 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000469 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000470 EmitAlignment(Align, 0);
471 EmitXXStructorList(GV->getInitializer());
472 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000473 }
474
475 return false;
476}
477
Chris Lattner33adcfb2009-08-22 21:43:10 +0000478/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000479/// global in the specified llvm.used list for which emitUsedDirectiveFor
480/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000481void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000482 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000483
Dan Gohmana119de82009-06-14 23:30:43 +0000484 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000485 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
486 if (InitList == 0) return;
487
488 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000489 const GlobalValue *GV =
490 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000491 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000492 O << Directive;
493 EmitConstantValueOnly(InitList->getOperand(i));
494 O << '\n';
495 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000496 }
497}
498
Chris Lattnered138932005-12-13 06:32:10 +0000499/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
500/// function pointers, ignoring the init priority.
501void AsmPrinter::EmitXXStructorList(Constant *List) {
502 // Should be an array of '{ int, void ()* }' structs. The first value is the
503 // init priority, which we ignore.
504 if (!isa<ConstantArray>(List)) return;
505 ConstantArray *InitList = cast<ConstantArray>(List);
506 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
507 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
508 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000509
510 if (CS->getOperand(1)->isNullValue())
511 return; // Found a null terminator, exit printing.
512 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000513 EmitGlobalConstant(CS->getOperand(1));
514 }
515}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000516
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000517
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000518//===----------------------------------------------------------------------===//
519/// LEB 128 number encoding.
520
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000521/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000522/// representing an unsigned leb128 value.
523void AsmPrinter::PrintULEB128(unsigned Value) const {
524 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000525 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000526 Value >>= 7;
527 if (Value) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000528 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000529 if (Value) O << ", ";
530 } while (Value);
531}
532
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000533/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000534/// representing a signed leb128 value.
535void AsmPrinter::PrintSLEB128(int Value) const {
536 int Sign = Value >> (8 * sizeof(Value) - 1);
537 bool IsMore;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000538
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000539 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000540 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000541 Value >>= 7;
542 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
543 if (IsMore) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000544 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000545 if (IsMore) O << ", ";
546 } while (IsMore);
547}
548
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000549//===--------------------------------------------------------------------===//
550// Emission and print routines
551//
552
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000553/// PrintHex - Print a value as a hexadecimal value.
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000554///
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000555void AsmPrinter::PrintHex(uint64_t Value) const {
556 O << "0x";
557 O.write_hex(Value);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000558}
559
560/// EOL - Print a newline character to asm stream. If a comment is present
561/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000562void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000563 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000564}
Owen Anderson25995092008-07-01 21:16:27 +0000565
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000566void AsmPrinter::EOL(const Twine &Comment) const {
567 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000568 O.PadToColumn(MAI->getCommentColumn());
569 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000570 << ' '
571 << Comment;
572 }
573 O << '\n';
574}
575
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000576static const char *DecodeDWARFEncoding(unsigned Encoding) {
577 switch (Encoding) {
578 case dwarf::DW_EH_PE_absptr:
579 return "absptr";
580 case dwarf::DW_EH_PE_omit:
581 return "omit";
582 case dwarf::DW_EH_PE_pcrel:
583 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000584 case dwarf::DW_EH_PE_udata4:
585 return "udata4";
586 case dwarf::DW_EH_PE_udata8:
587 return "udata8";
588 case dwarf::DW_EH_PE_sdata4:
589 return "sdata4";
590 case dwarf::DW_EH_PE_sdata8:
591 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000592 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
593 return "pcrel udata4";
594 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
595 return "pcrel sdata4";
596 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
597 return "pcrel udata8";
598 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
599 return "pcrel sdata8";
600 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
601 return "indirect pcrel udata4";
602 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
603 return "indirect pcrel sdata4";
604 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
605 return "indirect pcrel udata8";
606 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
607 return "indirect pcrel sdata8";
608 }
609
610 return 0;
611}
612
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000613void AsmPrinter::EOL(const Twine &Comment, unsigned Encoding) const {
614 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000615 O.PadToColumn(MAI->getCommentColumn());
616 O << MAI->getCommentString()
617 << ' '
618 << Comment;
619
620 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
621 O << " (" << EncStr << ')';
622 }
623 O << '\n';
624}
625
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000626/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
627/// unsigned leb128 value.
628void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000629 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000630 O << "\t.uleb128\t"
631 << Value;
632 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000633 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000634 PrintULEB128(Value);
635 }
636}
637
638/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
639/// signed leb128 value.
640void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000641 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000642 O << "\t.sleb128\t"
643 << Value;
644 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000645 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000646 PrintSLEB128(Value);
647 }
648}
649
650/// EmitInt8 - Emit a byte directive and value.
651///
652void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000653 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000654 PrintHex(Value & 0xFF);
655}
656
657/// EmitInt16 - Emit a short directive and value.
658///
659void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000660 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000661 PrintHex(Value & 0xFFFF);
662}
663
664/// EmitInt32 - Emit a long directive and value.
665///
666void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000667 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000668 PrintHex(Value);
669}
670
671/// EmitInt64 - Emit a long long directive and value.
672///
673void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000674 if (MAI->getData64bitsDirective()) {
675 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000676 PrintHex(Value);
677 } else {
678 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000679 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000680 EmitInt32(unsigned(Value));
681 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000682 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000683 EmitInt32(unsigned(Value >> 32));
684 }
685 }
686}
687
688/// toOctal - Convert the low order bits of X into an octal digit.
689///
690static inline char toOctal(int X) {
691 return (X&7)+'0';
692}
693
694/// printStringChar - Print a char, escaped if necessary.
695///
David Greene71847812009-07-14 20:18:05 +0000696static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000697 if (C == '"') {
698 O << "\\\"";
699 } else if (C == '\\') {
700 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000701 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000702 O << C;
703 } else {
704 switch(C) {
705 case '\b': O << "\\b"; break;
706 case '\f': O << "\\f"; break;
707 case '\n': O << "\\n"; break;
708 case '\r': O << "\\r"; break;
709 case '\t': O << "\\t"; break;
710 default:
711 O << '\\';
712 O << toOctal(C >> 6);
713 O << toOctal(C >> 3);
714 O << toOctal(C >> 0);
715 break;
716 }
717 }
718}
719
720/// EmitString - Emit a string with quotes and a null terminator.
721/// Special characters are emitted properly.
722/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000723void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000724 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000725}
726
727void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000728 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000729 if (AscizDirective)
730 O << AscizDirective;
731 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000732 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000733 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000734 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000735 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000736 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000737 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000738 else
739 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000740}
741
742
Dan Gohman189f80d2007-09-24 20:58:13 +0000743/// EmitFile - Emit a .file directive.
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000744void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
Dan Gohman189f80d2007-09-24 20:58:13 +0000745 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000746 for (unsigned i = 0, N = Name.size(); i < N; ++i)
747 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000748 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000749}
750
751
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000752//===----------------------------------------------------------------------===//
753
Chris Lattner3a420532007-05-31 18:57:45 +0000754// EmitAlignment - Emit an alignment directive to the specified power of
755// two boundary. For example, if you pass in 3 here, you will get an 8
756// byte alignment. If a global value is specified, and if that global has
757// an explicit alignment requested, it will unconditionally override the
758// alignment request. However, if ForcedAlignBits is specified, this value
759// has final say: the ultimate alignment will be the max of ForcedAlignBits
760// and the alignment computed with NumBits and the global.
761//
762// The algorithm is:
763// Align = NumBits;
764// if (GV && GV->hasalignment) Align = GV->getalignment();
765// Align = std::max(Align, ForcedAlignBits);
766//
767void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000768 unsigned ForcedAlignBits,
769 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000770 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000771 NumBits = Log2_32(GV->getAlignment());
772 NumBits = std::max(NumBits, ForcedAlignBits);
773
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000774 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000775
776 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000777 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000778 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000779
780 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000781}
David Greenea5bb59f2009-08-05 21:00:52 +0000782
Chris Lattner25045bd2005-11-21 07:51:36 +0000783/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000784///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000785void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000786 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000787 if (MAI->getZeroDirective()) {
788 O << MAI->getZeroDirective() << NumZeros;
789 if (MAI->getZeroDirectiveSuffix())
790 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000791 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000792 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000793 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000794 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000795 }
796 }
797}
798
Chris Lattnera80ba712004-08-16 23:15:22 +0000799// Print out the specified constant, without a storage class. Only the
800// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000801void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000802 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000803 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000804 return;
805 }
806
807 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000808 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000809 return;
810 }
811
812 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000813 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000814 // name of the variable or function as the address value.
Chris Lattner08ce3b42010-01-16 18:17:26 +0000815 GetGlobalValueSymbol(GV)->print(O, MAI);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000816 return;
817 }
818
819 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Dan Gohman6a55e622009-10-30 01:45:18 +0000820 GetBlockAddressSymbol(BA)->print(O, MAI);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000821 return;
822 }
823
824 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
825 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000826 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000827 O << '0';
828 return;
829 }
830
831 switch (CE->getOpcode()) {
832 case Instruction::ZExt:
833 case Instruction::SExt:
834 case Instruction::FPTrunc:
835 case Instruction::FPExt:
836 case Instruction::UIToFP:
837 case Instruction::SIToFP:
838 case Instruction::FPToUI:
839 case Instruction::FPToSI:
840 default:
841 llvm_unreachable("FIXME: Don't support this constant cast expr");
842 case Instruction::GetElementPtr: {
843 // generate a symbolic expression for the byte address
844 const TargetData *TD = TM.getTargetData();
845 const Constant *ptrVal = CE->getOperand(0);
846 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
847 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
848 idxVec.size());
849 if (Offset == 0)
850 return EmitConstantValueOnly(ptrVal);
851
852 // Truncate/sext the offset to the pointer size.
853 if (TD->getPointerSizeInBits() != 64) {
854 int SExtAmount = 64-TD->getPointerSizeInBits();
855 Offset = (Offset << SExtAmount) >> SExtAmount;
856 }
857
858 if (Offset)
859 O << '(';
860 EmitConstantValueOnly(ptrVal);
861 if (Offset > 0)
862 O << ") + " << Offset;
863 else
864 O << ") - " << -Offset;
865 return;
866 }
867 case Instruction::BitCast:
868 return EmitConstantValueOnly(CE->getOperand(0));
869
870 case Instruction::IntToPtr: {
871 // Handle casts to pointers by changing them into casts to the appropriate
872 // integer type. This promotes constant folding and simplifies this code.
873 const TargetData *TD = TM.getTargetData();
874 Constant *Op = CE->getOperand(0);
875 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
876 false/*ZExt*/);
877 return EmitConstantValueOnly(Op);
878 }
879
880 case Instruction::PtrToInt: {
881 // Support only foldable casts to/from pointers that can be eliminated by
882 // changing the pointer to the appropriately sized integer type.
883 Constant *Op = CE->getOperand(0);
884 const Type *Ty = CE->getType();
885 const TargetData *TD = TM.getTargetData();
886
887 // We can emit the pointer value into this slot if the slot is an
888 // integer slot greater or equal to the size of the pointer.
889 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
890 return EmitConstantValueOnly(Op);
891
892 O << "((";
893 EmitConstantValueOnly(Op);
894 APInt ptrMask =
895 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
896
897 SmallString<40> S;
898 ptrMask.toStringUnsigned(S);
899 O << ") & " << S.str() << ')';
900 return;
901 }
902
903 case Instruction::Trunc:
904 // We emit the value and depend on the assembler to truncate the generated
905 // expression properly. This is important for differences between
906 // blockaddress labels. Since the two labels are in the same function, it
907 // is reasonable to treat their delta as a 32-bit value.
908 return EmitConstantValueOnly(CE->getOperand(0));
909
910 case Instruction::Add:
911 case Instruction::Sub:
912 case Instruction::And:
913 case Instruction::Or:
914 case Instruction::Xor:
915 O << '(';
916 EmitConstantValueOnly(CE->getOperand(0));
917 O << ')';
918 switch (CE->getOpcode()) {
919 case Instruction::Add:
920 O << " + ";
921 break;
922 case Instruction::Sub:
923 O << " - ";
924 break;
925 case Instruction::And:
926 O << " & ";
927 break;
928 case Instruction::Or:
929 O << " | ";
930 break;
931 case Instruction::Xor:
932 O << " ^ ";
933 break;
934 default:
935 break;
936 }
937 O << '(';
938 EmitConstantValueOnly(CE->getOperand(1));
939 O << ')';
940 break;
Chris Lattnera80ba712004-08-16 23:15:22 +0000941 }
942}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000943
Chris Lattner2980cef2005-11-10 18:06:33 +0000944/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000945/// the predicate isString is true.
946///
David Greene71847812009-07-14 20:18:05 +0000947static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000948 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000949 assert(CVA->isString() && "Array is not string compatible!");
950
Dan Gohmand19a53b2008-06-30 22:03:41 +0000951 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000952 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000953 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000954 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000955 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000956 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000957 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000958}
959
Jeff Cohenc884db42006-05-02 01:16:28 +0000960/// EmitString - Emit a zero-byte-terminated string constant.
961///
962void AsmPrinter::EmitString(const ConstantArray *CVA) const {
963 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000964 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000965 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000966 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000967 printAsCString(O, CVA, NumElts-1);
968 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000969 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000970 printAsCString(O, CVA, NumElts);
971 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000972 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000973}
974
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000975void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
976 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000977 if (CVA->isString()) {
978 EmitString(CVA);
979 } else { // Not a string. Print the values in successive locations
980 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000981 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000982 }
983}
984
985void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
986 const VectorType *PTy = CP->getType();
987
988 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
989 EmitGlobalConstant(CP->getOperand(I));
990}
991
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000992void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
993 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000994 // Print the fields in successive locations. Pad to align if needed!
995 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +0000996 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000997 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
998 uint64_t sizeSoFar = 0;
999 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1000 const Constant* field = CVS->getOperand(i);
1001
1002 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +00001003 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001004 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1005 - cvsLayout->getElementOffset(i)) - fieldSize;
1006 sizeSoFar += fieldSize + padSize;
1007
1008 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001009 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001010
1011 // Insert padding - this may include padding to increase the size of the
1012 // current field up to the ABI size (if the struct is not packed) as well
1013 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001014 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001015 }
1016 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1017 "Layout of constant struct may be incorrect!");
1018}
1019
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001020void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1021 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001022 // FP Constants are printed as integer constants to avoid losing
1023 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001024 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001025 const TargetData *TD = TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001026 if (CFP->getType()->isDoubleTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001027 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1028 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001029 if (MAI->getData64bitsDirective(AddrSpace)) {
1030 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001031 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001032 O.PadToColumn(MAI->getCommentColumn());
1033 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001034 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001035 O << '\n';
1036 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001037 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001038 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001039 O.PadToColumn(MAI->getCommentColumn());
1040 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001041 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001042 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001043 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001044 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001045 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001046 O.PadToColumn(MAI->getCommentColumn());
1047 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001048 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001049 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001050 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001051 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001052 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001053 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001054 O.PadToColumn(MAI->getCommentColumn());
1055 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001056 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001057 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001058 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001059 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001060 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001061 O.PadToColumn(MAI->getCommentColumn());
1062 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001063 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001064 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001065 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001066 }
1067 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001068 }
1069
1070 if (CFP->getType()->isFloatTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001071 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001072 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001073 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001074 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001075 O.PadToColumn(MAI->getCommentColumn());
1076 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001077 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001078 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001079 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001080 }
1081
1082 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001083 // all long double variants are printed as hex
1084 // api needed to prevent premature destruction
1085 APInt api = CFP->getValueAPF().bitcastToAPInt();
1086 const uint64_t *p = api.getRawData();
1087 // Convert to double so we can print the approximate val as a comment.
1088 APFloat DoubleVal = CFP->getValueAPF();
1089 bool ignored;
1090 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1091 &ignored);
1092 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001093 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001094 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001095 O.PadToColumn(MAI->getCommentColumn());
1096 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001097 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001098 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001099 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001100 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001101 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001102 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001103 O.PadToColumn(MAI->getCommentColumn());
1104 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001105 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001106 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001107 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001108 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001109 O.PadToColumn(MAI->getCommentColumn());
1110 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001111 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001112 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001113 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001114 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001115 O.PadToColumn(MAI->getCommentColumn());
1116 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001117 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001118 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001119 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001120 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001121 O.PadToColumn(MAI->getCommentColumn());
1122 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001123 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001124 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001125 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001126 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001127 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001128 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001129 O.PadToColumn(MAI->getCommentColumn());
1130 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001131 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001132 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001133 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001134 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001135 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001136 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001137 O.PadToColumn(MAI->getCommentColumn());
1138 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001139 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001140 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001141 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001142 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001143 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001144 O.PadToColumn(MAI->getCommentColumn());
1145 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001146 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001147 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001148 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001149 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001150 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001151 O.PadToColumn(MAI->getCommentColumn());
1152 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001153 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001154 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001155 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001156 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001157 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001158 O.PadToColumn(MAI->getCommentColumn());
1159 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001160 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001161 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001162 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001163 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001164 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1165 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001166 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001167 }
1168
1169 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001170 // all long double variants are printed as hex
1171 // api needed to prevent premature destruction
1172 APInt api = CFP->getValueAPF().bitcastToAPInt();
1173 const uint64_t *p = api.getRawData();
1174 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001175 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001176 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001177 O.PadToColumn(MAI->getCommentColumn());
1178 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001179 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001180 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001181 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001182 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001183 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001184 O.PadToColumn(MAI->getCommentColumn());
1185 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001186 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001187 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001188 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001189 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001190 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001191 O.PadToColumn(MAI->getCommentColumn());
1192 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001193 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001194 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001195 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001196 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001197 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001198 O.PadToColumn(MAI->getCommentColumn());
1199 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001200 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001201 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001202 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001203 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001204 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001205 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001206 O.PadToColumn(MAI->getCommentColumn());
1207 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001208 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001209 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001210 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001211 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001212 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001213 O.PadToColumn(MAI->getCommentColumn());
1214 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001215 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001216 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001217 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001218 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001219 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001220 O.PadToColumn(MAI->getCommentColumn());
1221 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001222 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001223 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001224 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001225 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001226 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001227 O.PadToColumn(MAI->getCommentColumn());
1228 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001229 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001230 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001231 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001232 }
1233 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001234 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001235}
1236
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001237void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1238 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001239 const TargetData *TD = TM.getTargetData();
1240 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001241 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001242
1243 // We don't expect assemblers to support integer data directives
1244 // for more than 64 bits, so we emit the data in at most 64-bit
1245 // quantities at a time.
1246 const uint64_t *RawData = CI->getValue().getRawData();
1247 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1248 uint64_t Val;
1249 if (TD->isBigEndian())
1250 Val = RawData[e - i - 1];
1251 else
1252 Val = RawData[i];
1253
Chris Lattner5979dff2010-01-13 04:38:16 +00001254 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001255 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Chris Lattner5979dff2010-01-13 04:38:16 +00001256 continue;
Dan Gohman00d448a2008-12-22 21:14:27 +00001257 }
Chris Lattner5979dff2010-01-13 04:38:16 +00001258
1259 // Emit two 32-bit chunks, order depends on endianness.
1260 unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
1261 const char *FirstName = " least", *SecondName = " most";
1262 if (TD->isBigEndian()) {
1263 std::swap(FirstChunk, SecondChunk);
1264 std::swap(FirstName, SecondName);
1265 }
1266
1267 O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
1268 if (VerboseAsm) {
1269 O.PadToColumn(MAI->getCommentColumn());
1270 O << MAI->getCommentString()
1271 << FirstName << " significant half of i64 " << Val;
1272 }
1273 O << '\n';
1274
1275 O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
1276 if (VerboseAsm) {
1277 O.PadToColumn(MAI->getCommentColumn());
1278 O << MAI->getCommentString()
1279 << SecondName << " significant half of i64 " << Val;
1280 }
1281 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001282 }
1283}
1284
Chris Lattner25045bd2005-11-21 07:51:36 +00001285/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001286void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001287 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001288 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001289 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001290
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001291 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001292 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001293 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001294 }
1295
1296 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001297 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001298 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001299 }
1300
1301 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001302 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001303 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001304 }
1305
1306 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001307 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001308 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001309 }
1310
1311 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1312 // If we can directly emit an 8-byte constant, do it.
1313 if (Size == 8)
1314 if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
1315 O << Data64Dir << CI->getZExtValue() << '\n';
1316 return;
1317 }
1318
Dan Gohman00d448a2008-12-22 21:14:27 +00001319 // Small integers are handled below; large integers are handled here.
1320 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001321 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001322 return;
1323 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001324 }
1325
1326 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001327 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001328 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001329 }
1330
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001331 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001332 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001333 if (VerboseAsm) {
1334 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1335 SmallString<40> S;
1336 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001337 O.PadToColumn(MAI->getCommentColumn());
1338 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001339 }
Scott Micheleefc8452008-06-03 15:39:51 +00001340 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001341 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001342}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001343
Chris Lattnerfad86b02008-08-17 07:19:36 +00001344void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001345 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001346 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001347}
1348
Chris Lattner3ce9b672006-09-26 23:59:50 +00001349/// PrintSpecial - Print information related to the specified machine instr
1350/// that is independent of the operand, and may be independent of the instr
1351/// itself. This can be useful for portably encoding the comment character
1352/// or other bits of target-specific knowledge into the asmstrings. The
1353/// syntax used is ${:comment}. Targets can override this to add support
1354/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001355void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001356 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001357 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001358 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001359 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001360 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001361 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001362 // Comparing the address of MI isn't sufficient, because machineinstrs may
1363 // be allocated to the same address across functions.
1364 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1365
Owen Andersonbd58edf2009-06-24 22:28:12 +00001366 // If this is a new LastFn instruction, bump the counter.
1367 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001368 ++Counter;
1369 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001370 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001371 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001372 O << Counter;
1373 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001374 std::string msg;
1375 raw_string_ostream Msg(msg);
1376 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001377 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001378 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001379 }
1380}
1381
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001382/// processDebugLoc - Processes the debug information of each machine
1383/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001384void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1385 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001386 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1387 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001388 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001389 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001390 if (DL.isUnknown())
1391 return;
Devang Patel6b61f582010-01-16 06:09:35 +00001392 DILocation CurDLT = MF->getDILocation(DL);
1393 if (CurDLT.getScope().isNull())
Devang Patel53bb5c92009-11-10 23:06:00 +00001394 return;
1395
1396 if (BeforePrintingInsn) {
Devang Patel6b61f582010-01-16 06:09:35 +00001397 if (CurDLT.getNode() != PrevDLT.getNode()) {
1398 unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(),
1399 CurDLT.getColumnNumber(),
1400 CurDLT.getScope().getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001401 printLabel(L);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001402 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001403 DW->BeginScope(MI, L);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001404 PrevDLT = CurDLT;
1405 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001406 } else {
1407 // After printing instruction
1408 DW->EndScope(MI);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001409 }
1410}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001411
Devang Patel53bb5c92009-11-10 23:06:00 +00001412
Chris Lattner0264d1a2006-01-27 02:10:10 +00001413/// printInlineAsm - This method formats and prints the specified machine
1414/// instruction that is an inline asm.
1415void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001416 unsigned NumOperands = MI->getNumOperands();
1417
1418 // Count the number of register definitions.
1419 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001420 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001421 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001422 assert(NumDefs != NumOperands-1 && "No asm string?");
1423
Dan Gohmand735b802008-10-03 15:45:36 +00001424 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001425
1426 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001427 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001428
Dan Gohman40c57862009-11-06 00:04:54 +00001429 O << '\t';
1430
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001431 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1432 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001433 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001434 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1435 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001436 return;
1437 }
1438
Chris Lattner33adcfb2009-08-22 21:43:10 +00001439 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001440
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001441 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001442 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001443
Chris Lattner66099132006-02-01 22:41:11 +00001444 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1445 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001446
Chris Lattner66099132006-02-01 22:41:11 +00001447 while (*LastEmitted) {
1448 switch (*LastEmitted) {
1449 default: {
1450 // Not a special case, emit the string section literally.
1451 const char *LiteralEnd = LastEmitted+1;
1452 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001453 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001454 ++LiteralEnd;
1455 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1456 O.write(LastEmitted, LiteralEnd-LastEmitted);
1457 LastEmitted = LiteralEnd;
1458 break;
1459 }
Chris Lattner1c059972006-05-05 21:47:05 +00001460 case '\n':
1461 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001462 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001463 break;
Chris Lattner66099132006-02-01 22:41:11 +00001464 case '$': {
1465 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001466 bool Done = true;
1467
1468 // Handle escapes.
1469 switch (*LastEmitted) {
1470 default: Done = false; break;
1471 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001472 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1473 O << '$';
1474 ++LastEmitted; // Consume second '$' character.
1475 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001476 case '(': // $( -> same as GCC's { character.
1477 ++LastEmitted; // Consume '(' character.
1478 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001479 llvm_report_error("Nested variants found in inline asm string: '"
1480 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001481 }
1482 CurVariant = 0; // We're in the first variant now.
1483 break;
1484 case '|':
1485 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001486 if (CurVariant == -1)
1487 O << '|'; // this is gcc's behavior for | outside a variant
1488 else
1489 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001490 break;
1491 case ')': // $) -> same as GCC's } char.
1492 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001493 if (CurVariant == -1)
1494 O << '}'; // this is gcc's behavior for } outside a variant
1495 else
1496 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001497 break;
Chris Lattner66099132006-02-01 22:41:11 +00001498 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001499 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001500
1501 bool HasCurlyBraces = false;
1502 if (*LastEmitted == '{') { // ${variable}
1503 ++LastEmitted; // Consume '{' character.
1504 HasCurlyBraces = true;
1505 }
1506
Chris Lattner3e0cc262009-03-10 05:37:13 +00001507 // If we have ${:foo}, then this is not a real operand reference, it is a
1508 // "magic" string reference, just like in .td files. Arrange to call
1509 // PrintSpecial.
1510 if (HasCurlyBraces && *LastEmitted == ':') {
1511 ++LastEmitted;
1512 const char *StrStart = LastEmitted;
1513 const char *StrEnd = strchr(StrStart, '}');
1514 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001515 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1516 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001517 }
1518
1519 std::string Val(StrStart, StrEnd);
1520 PrintSpecial(MI, Val.c_str());
1521 LastEmitted = StrEnd+1;
1522 break;
1523 }
1524
Chris Lattner66099132006-02-01 22:41:11 +00001525 const char *IDStart = LastEmitted;
1526 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001527 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001528 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1529 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001530 llvm_report_error("Bad $ operand number in inline asm string: '"
1531 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001532 }
1533 LastEmitted = IDEnd;
1534
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001535 char Modifier[2] = { 0, 0 };
1536
Chris Lattner66099132006-02-01 22:41:11 +00001537 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001538 // If we have curly braces, check for a modifier character. This
1539 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1540 if (*LastEmitted == ':') {
1541 ++LastEmitted; // Consume ':' character.
1542 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001543 llvm_report_error("Bad ${:} expression in inline asm string: '"
1544 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001545 }
1546
1547 Modifier[0] = *LastEmitted;
1548 ++LastEmitted; // Consume modifier character.
1549 }
1550
Chris Lattner66099132006-02-01 22:41:11 +00001551 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001552 llvm_report_error("Bad ${} expression in inline asm string: '"
1553 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001554 }
1555 ++LastEmitted; // Consume '}' character.
1556 }
1557
1558 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001559 llvm_report_error("Invalid $ operand number in inline asm string: '"
1560 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001561 }
1562
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001563 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001564 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001565 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1566 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001567
1568 bool Error = false;
1569
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001570 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001571 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001572 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001573 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001574 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001575 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001576
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001577 if (OpNo >= MI->getNumOperands()) {
1578 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001579 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001580 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001581 ++OpNo; // Skip over the ID number.
1582
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001583 if (Modifier[0]=='l') // labels are target independent
Chris Lattner325d3dc2009-09-13 17:14:04 +00001584 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1585 ->getNumber())->print(O, MAI);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001586 else {
1587 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001588 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001589 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1590 Modifier[0] ? Modifier : 0);
1591 } else {
1592 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1593 Modifier[0] ? Modifier : 0);
1594 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001595 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001596 }
1597 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001598 std::string msg;
1599 raw_string_ostream Msg(msg);
1600 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001601 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001602 MI->print(Msg);
1603 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001604 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001605 }
Chris Lattner66099132006-02-01 22:41:11 +00001606 break;
1607 }
Chris Lattner66099132006-02-01 22:41:11 +00001608 }
1609 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001610 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001611}
1612
Evan Chengda47e6e2008-03-15 00:03:38 +00001613/// printImplicitDef - This method prints the specified machine instruction
1614/// that is an implicit def.
1615void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001616 if (!VerboseAsm) return;
1617 O.PadToColumn(MAI->getCommentColumn());
1618 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001619 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001620}
1621
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001622void AsmPrinter::printKill(const MachineInstr *MI) const {
1623 if (!VerboseAsm) return;
1624 O.PadToColumn(MAI->getCommentColumn());
1625 O << MAI->getCommentString() << " kill:";
1626 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1627 const MachineOperand &op = MI->getOperand(n);
1628 assert(op.isReg() && "KILL instruction must have only register operands");
1629 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1630 }
1631}
1632
Jim Laskey1ee29252007-01-26 14:34:52 +00001633/// printLabel - This method prints a local label used by debug and
1634/// exception handling tables.
1635void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001636 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001637}
1638
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001639void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001640 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001641}
1642
Chris Lattner66099132006-02-01 22:41:11 +00001643/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1644/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001645/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001646bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001647 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001648 // Target doesn't support this yet!
1649 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001650}
Chris Lattnerdd260332006-02-24 20:21:58 +00001651
1652bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1653 unsigned AsmVariant,
1654 const char *ExtraCode) {
1655 // Target doesn't support this yet!
1656 return true;
1657}
Nate Begeman37efe672006-04-22 18:53:45 +00001658
Dan Gohman29cbade2009-11-20 23:18:13 +00001659MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1660 const char *Suffix) const {
1661 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001662}
1663
1664MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001665 const BasicBlock *BB,
1666 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001667 assert(BB->hasName() &&
1668 "Address of anonymous basic block not supported yet!");
1669
Dan Gohman568a3be2009-11-05 23:14:35 +00001670 // This code must use the function name itself, and not the function number,
1671 // since it must be possible to generate the label name from within other
1672 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001673 SmallString<60> FnName;
1674 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001675
Chris Lattner2f8cc262010-01-13 07:30:49 +00001676 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001677 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001678 Mang->getNameWithPrefix(NameResult,
1679 StringRef("BA") + Twine((unsigned)FnName.size()) +
1680 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1681 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001682
Chris Lattner48130352010-01-13 06:38:18 +00001683 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001684}
1685
Chris Lattner7cb384d2009-09-12 23:02:08 +00001686MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1687 SmallString<60> Name;
1688 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1689 << getFunctionNumber() << '_' << MBBID;
1690
1691 return OutContext.GetOrCreateSymbol(Name.str());
1692}
1693
Chris Lattner6b04ede2010-01-15 23:18:17 +00001694/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1695/// value.
1696MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1697 SmallString<60> NameStr;
1698 Mang->getNameWithPrefix(NameStr, GV, false);
1699 return OutContext.GetOrCreateSymbol(NameStr.str());
1700}
1701
Chris Lattner7a2ba942010-01-16 18:37:32 +00001702/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
Chris Lattnerd588b972010-01-15 23:25:11 +00001703/// global value name as its base, with the specified suffix, and where the
Chris Lattner7a2ba942010-01-16 18:37:32 +00001704/// symbol is forced to have private linkage if ForcePrivate is true.
1705MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1706 StringRef Suffix,
1707 bool ForcePrivate) const {
Chris Lattnerd588b972010-01-15 23:25:11 +00001708 SmallString<60> NameStr;
Chris Lattner7a2ba942010-01-16 18:37:32 +00001709 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
Chris Lattnerd588b972010-01-15 23:25:11 +00001710 NameStr.append(Suffix.begin(), Suffix.end());
1711 return OutContext.GetOrCreateSymbol(NameStr.str());
1712}
1713
Chris Lattner6b04ede2010-01-15 23:18:17 +00001714/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1715/// ExternalSymbol.
1716MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1717 SmallString<60> NameStr;
1718 Mang->getNameWithPrefix(NameStr, Sym);
1719 return OutContext.GetOrCreateSymbol(NameStr.str());
1720}
1721
Chris Lattner7cb384d2009-09-12 23:02:08 +00001722
Chris Lattner70a54c02009-09-13 18:25:37 +00001723/// EmitBasicBlockStart - This method prints the label for the specified
1724/// MachineBasicBlock, an alignment (if present) and a comment describing
1725/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001726void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001727 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001728 if (unsigned Align = MBB->getAlignment())
1729 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001730
Dan Gohmanb1cac332009-10-30 01:34:35 +00001731 // If the block has its address taken, emit a special label to satisfy
1732 // references to the block. This is done so that we don't need to
1733 // remember the number of this label, and so that we can make
1734 // forward references to labels without knowing what their numbers
1735 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001736 if (MBB->hasAddressTaken()) {
1737 GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1738 MBB->getBasicBlock())->print(O, MAI);
1739 O << ':';
1740 if (VerboseAsm) {
1741 O.PadToColumn(MAI->getCommentColumn());
1742 O << MAI->getCommentString() << " Address Taken";
1743 }
1744 O << '\n';
1745 }
1746
Dan Gohmanb1cac332009-10-30 01:34:35 +00001747 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001748 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1749 if (VerboseAsm)
1750 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1751 } else {
1752 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
1753 O << ':';
1754 if (!VerboseAsm)
1755 O << '\n';
1756 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001757
Dan Gohmanb1cac332009-10-30 01:34:35 +00001758 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001759 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001760 if (const BasicBlock *BB = MBB->getBasicBlock())
1761 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001762 O.PadToColumn(MAI->getCommentColumn());
1763 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001764 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001765 }
David Greeneb71d1b22009-08-10 16:38:07 +00001766
Chris Lattner70a54c02009-09-13 18:25:37 +00001767 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001768 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001769 }
Nate Begeman37efe672006-04-22 18:53:45 +00001770}
Nate Begeman52a51e382006-08-12 21:29:52 +00001771
Evan Chengcc415862007-11-09 01:32:10 +00001772/// printPICJumpTableSetLabel - This method prints a set label for the
1773/// specified MachineBasicBlock for a jumptable entry.
1774void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1775 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001776 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001777 return;
1778
Chris Lattner33adcfb2009-08-22 21:43:10 +00001779 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001780 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001781 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001782 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001783 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001784}
Evan Chengd6594ae2006-09-12 21:00:35 +00001785
Evan Chengcc415862007-11-09 01:32:10 +00001786void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1787 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001788 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001789 return;
1790
Chris Lattner33adcfb2009-08-22 21:43:10 +00001791 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001792 << getFunctionNumber() << '_' << uid << '_' << uid2
1793 << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001794 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001795 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001796 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001797}
1798
Evan Chengd6594ae2006-09-12 21:00:35 +00001799/// printDataDirective - This method prints the asm directive for the
1800/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001801void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001802 const TargetData *TD = TM.getTargetData();
1803 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001804 case Type::FloatTyID: case Type::DoubleTyID:
1805 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1806 assert(0 && "Should have already output floating point constant.");
1807 default:
1808 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001809 case Type::IntegerTyID: {
1810 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1811 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001812 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001813 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001814 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001815 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001816 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001817 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001818 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001819 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001820 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001821 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001822 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001823 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001824 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001825 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001826 case Type::PointerTyID:
1827 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001828 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001829 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001830 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001831 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001832 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001833 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001834 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001835 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001836 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001837 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001838 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001839 }
1840}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001841
Chris Lattner53d4d782010-01-15 23:38:51 +00001842void AsmPrinter::printVisibility(const MCSymbol *Sym,
1843 unsigned Visibility) const {
1844 if (Visibility == GlobalValue::HiddenVisibility) {
1845 if (const char *Directive = MAI->getHiddenDirective()) {
1846 O << Directive;
1847 Sym->print(O, MAI);
1848 O << '\n';
1849 }
1850 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1851 if (const char *Directive = MAI->getProtectedDirective()) {
1852 O << Directive;
1853 Sym->print(O, MAI);
1854 O << '\n';
1855 }
1856 }
1857}
1858
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001859void AsmPrinter::printOffset(int64_t Offset) const {
1860 if (Offset > 0)
1861 O << '+' << Offset;
1862 else if (Offset < 0)
1863 O << Offset;
1864}
1865
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001866GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1867 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001868 return 0;
1869
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001870 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001871 if (GCPI != GCMetadataPrinters.end())
1872 return GCPI->second;
1873
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001874 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001875
1876 for (GCMetadataPrinterRegistry::iterator
1877 I = GCMetadataPrinterRegistry::begin(),
1878 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1879 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001880 GCMetadataPrinter *GMP = I->instantiate();
1881 GMP->S = S;
1882 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1883 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001884 }
1885
Chris Lattner103289e2009-08-23 07:19:13 +00001886 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001887 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001888}
David Greene014700c2009-07-13 20:25:48 +00001889
1890/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001891void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001892 if (!VerboseAsm)
1893 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001894
David Greene1924aab2009-11-13 21:34:57 +00001895 bool Newline = false;
1896
1897 if (!MI.getDebugLoc().isUnknown()) {
Devang Patel6b61f582010-01-16 06:09:35 +00001898 DILocation DLT = MF->getDILocation(MI.getDebugLoc());
David Greene1924aab2009-11-13 21:34:57 +00001899
1900 // Print source line info.
1901 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001902 O << MAI->getCommentString() << ' ';
Devang Patel6b61f582010-01-16 06:09:35 +00001903 DIScope Scope = DLT.getScope();
Dan Gohman3b9bc042009-12-05 00:23:29 +00001904 // Omit the directory, because it's likely to be long and uninteresting.
1905 if (!Scope.isNull())
1906 O << Scope.getFilename();
1907 else
1908 O << "<unknown>";
Devang Patel6b61f582010-01-16 06:09:35 +00001909 O << ':' << DLT.getLineNumber();
1910 if (DLT.getColumnNumber() != 0)
1911 O << ':' << DLT.getColumnNumber();
David Greene1924aab2009-11-13 21:34:57 +00001912 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001913 }
David Greene1924aab2009-11-13 21:34:57 +00001914
David Greeneddff9412009-11-16 15:12:23 +00001915 // Check for spills and reloads
1916 int FI;
1917
1918 const MachineFrameInfo *FrameInfo =
1919 MI.getParent()->getParent()->getFrameInfo();
1920
1921 // We assume a single instruction only has a spill or reload, not
1922 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001923 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001924 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1925 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001926 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001927 if (Newline) O << '\n';
1928 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001929 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001930 Newline = true;
1931 }
1932 }
David Greenef7ea2a52009-12-04 22:46:04 +00001933 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001934 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1935 if (Newline) O << '\n';
1936 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001937 O << MAI->getCommentString() << ' '
1938 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001939 Newline = true;
1940 }
1941 }
1942 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1943 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001944 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001945 if (Newline) O << '\n';
1946 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001947 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001948 Newline = true;
1949 }
1950 }
David Greenef7ea2a52009-12-04 22:46:04 +00001951 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001952 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1953 if (Newline) O << '\n';
1954 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001955 O << MAI->getCommentString() << ' '
1956 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001957 Newline = true;
1958 }
1959 }
1960
1961 // Check for spill-induced copies
1962 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1963 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1964 SrcSubIdx, DstSubIdx)) {
1965 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1966 if (Newline) O << '\n';
1967 O.PadToColumn(MAI->getCommentColumn());
1968 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001969 }
1970 }
David Greene014700c2009-07-13 20:25:48 +00001971}
1972
David Greenefe37ab32009-08-18 19:22:55 +00001973/// PrintChildLoopComment - Print comments about child loops within
1974/// the loop for this basic block, with nesting.
1975///
1976static void PrintChildLoopComment(formatted_raw_ostream &O,
1977 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001978 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001979 int FunctionNumber) {
1980 // Add child loop information
1981 for(MachineLoop::iterator cl = loop->begin(),
1982 clend = loop->end();
1983 cl != clend;
1984 ++cl) {
1985 MachineBasicBlock *Header = (*cl)->getHeader();
1986 assert(Header && "No header for loop");
1987
1988 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001989 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001990
Chris Lattner33adcfb2009-08-22 21:43:10 +00001991 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001992 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001993 << " Child Loop BB" << FunctionNumber << "_"
1994 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1995
Chris Lattner33adcfb2009-08-22 21:43:10 +00001996 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001997 }
1998}
1999
David Greeneb71d1b22009-08-10 16:38:07 +00002000/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00002001void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00002002 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00002003 // Add loop depth information
2004 const MachineLoop *loop = LI->getLoopFor(&MBB);
2005
2006 if (loop) {
2007 // Print a newline after bb# annotation.
2008 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00002009 O.PadToColumn(MAI->getCommentColumn());
2010 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00002011 << '\n';
2012
Chris Lattner33adcfb2009-08-22 21:43:10 +00002013 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00002014
2015 MachineBasicBlock *Header = loop->getHeader();
2016 assert(Header && "No header for loop");
2017
2018 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002019 O << MAI->getCommentString() << " Loop Header";
2020 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00002021 }
2022 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002023 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00002024 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
2025 }
2026
2027 if (loop->empty()) {
2028 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002029 O.PadToColumn(MAI->getCommentColumn());
2030 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00002031 }
2032
2033 // Add parent loop information
2034 for (const MachineLoop *CurLoop = loop->getParentLoop();
2035 CurLoop;
2036 CurLoop = CurLoop->getParentLoop()) {
2037 MachineBasicBlock *Header = CurLoop->getHeader();
2038 assert(Header && "No header for loop");
2039
2040 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002041 O.PadToColumn(MAI->getCommentColumn());
2042 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002043 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002044 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00002045 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
2046 }
2047 }
2048 }
2049}