blob: 6de6f2b47f4ad17ad79f82a4b1e4b251fbc6284b [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000020#include "llvm/CodeGen/MachineConstantPool.h"
David Greenefe37ab32009-08-18 19:22:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000022#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelc48c5502009-01-13 21:44:10 +000025#include "llvm/CodeGen/DwarfWriter.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000026#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000027#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000028#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCStreamer.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000031#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000032#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000033#include "llvm/Support/FormattedStream.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000034#include "llvm/Support/Mangler.h"
Jim Laskey563321a2006-09-06 18:34:40 +000035#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000036#include "llvm/Target/TargetData.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000037#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000038#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000039#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000040#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000041#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000042#include "llvm/ADT/SmallString.h"
Owen Andersoncb371882008-08-21 00:14:44 +000043#include "llvm/ADT/StringExtras.h"
Chris Lattner66099132006-02-01 22:41:11 +000044#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000045using namespace llvm;
46
Evan Cheng42bf74b2009-03-25 01:47:28 +000047static cl::opt<cl::boolOrDefault>
48AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
49 cl::init(cl::BOU_UNSET));
50
Devang Patel19974732007-05-03 01:11:54 +000051char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000052AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000053 const TargetAsmInfo *T, bool VDef)
54 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Chengda47e6e2008-03-15 00:03:38 +000055 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000056
57 OutContext(*new MCContext()),
Chris Lattnerf3ce0092009-08-17 04:23:44 +000058 OutStreamer(*createAsmStreamer(OutContext, O, *T, this)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000059
Chris Lattner290c2f52009-08-03 23:20:21 +000060 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersona8dbf362009-06-25 16:55:32 +000061 PrevDLT(0, ~0U, ~0U) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000062 DW = 0; MMI = 0;
Evan Cheng42bf74b2009-03-25 01:47:28 +000063 switch (AsmVerbose) {
64 case cl::BOU_UNSET: VerboseAsm = VDef; break;
65 case cl::BOU_TRUE: VerboseAsm = true; break;
66 case cl::BOU_FALSE: VerboseAsm = false; break;
67 }
68}
Chris Lattnered138932005-12-13 06:32:10 +000069
Gordon Henriksenc317a602008-08-17 12:08:44 +000070AsmPrinter::~AsmPrinter() {
71 for (gcp_iterator I = GCMetadataPrinters.begin(),
72 E = GCMetadataPrinters.end(); I != E; ++I)
73 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000074
75 delete &OutStreamer;
76 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000077}
Chris Lattnered138932005-12-13 06:32:10 +000078
Chris Lattner38c39882009-08-03 19:12:26 +000079TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000080 return TM.getTargetLowering()->getObjFileLowering();
81}
82
Anton Korobeynikovb5a32e22008-09-24 22:12:10 +000083/// SwitchToSection - Switch to the specified section of the executable if we
Chris Lattnerdabf07c2009-08-18 06:15:16 +000084/// are not already in it!
Chris Lattnera87dea42009-07-31 18:48:30 +000085void AsmPrinter::SwitchToSection(const MCSection *NS) {
Chris Lattnerdabf07c2009-08-18 06:15:16 +000086 assert(NS != 0 && "Must specify a section to switch to");
87 OutStreamer.SwitchSection(NS);
Anton Korobeynikovb5a32e22008-09-24 22:12:10 +000088}
Chris Lattner4632d7a2006-05-09 04:59:56 +000089
Chris Lattnerdabf07c2009-08-18 06:15:16 +000090/// getCurrentSection() - Return the current section we are emitting to.
91const MCSection *AsmPrinter::getCurrentSection() const {
92 return OutStreamer.getCurrentSection();
93}
94
95
Gordon Henriksence224772008-01-07 01:30:38 +000096void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000097 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +000098 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000099 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +0000100 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000101 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +0000102}
103
Chris Lattnera80ba712004-08-16 23:15:22 +0000104bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000105 // Initialize TargetLoweringObjectFile.
106 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
107 .Initialize(OutContext, TM);
108
Bill Wendling4cef5842009-07-20 21:30:28 +0000109 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
Chris Lattner90f8b702009-07-21 17:30:51 +0000110 TAI->getLinkerPrivateGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +0000111
Chris Lattnera93ca922009-06-18 23:41:35 +0000112 if (TAI->doesAllowQuotesInName())
113 Mang->setUseQuotes(true);
114
Duncan Sands1465d612009-01-28 13:14:17 +0000115 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000116 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola952b8392008-12-03 11:01:37 +0000117
118 if (TAI->hasSingleParameterDotFile()) {
119 /* Very minimal debug info. It is ignored if we emit actual
120 debug info. If we don't, this at helps the user find where
121 a function came from. */
122 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
123 }
124
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000125 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
126 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
127 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000128
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000129 if (!M.getModuleInlineAsm().empty())
Jim Laskey563321a2006-09-06 18:34:40 +0000130 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000131 << M.getModuleInlineAsm()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000132 << '\n' << TAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000133 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000134
Chris Lattner0de1fc42009-06-24 19:09:55 +0000135 if (TAI->doesSupportDebugInformation() ||
136 TAI->doesSupportExceptionHandling()) {
137 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
138 if (MMI)
Devang Patel14a55d92009-06-19 21:54:26 +0000139 MMI->AnalyzeModule(M);
Chris Lattner0de1fc42009-06-24 19:09:55 +0000140 DW = getAnalysisIfAvailable<DwarfWriter>();
141 if (DW)
142 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000143 }
144
Chris Lattnera80ba712004-08-16 23:15:22 +0000145 return false;
146}
147
148bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000149 // Emit global variables.
150 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
151 I != E; ++I)
152 PrintGlobalVariable(I);
153
Chris Lattner1f522fe2009-06-24 18:54:37 +0000154 // Emit final debug information.
155 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
156 DW->EndModule();
157
Chris Lattner0a7befa2009-06-24 18:52:01 +0000158 // If the target wants to know about weak references, print them all.
Rafael Espindola15404d02006-12-18 03:37:18 +0000159 if (TAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000160 // FIXME: This is not lazy, it would be nice to only print weak references
161 // to stuff that is actually used. Note that doing so would require targets
162 // to notice uses in operands (due to constant exprs etc). This should
163 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000164
Chris Lattner0a7befa2009-06-24 18:52:01 +0000165 // Print out module-level global variables here.
166 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
167 I != E; ++I) {
168 if (I->hasExternalWeakLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000169 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000170 }
171
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000172 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000173 if (I->hasExternalWeakLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000174 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000175 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000176 }
177
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000178 if (TAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000179 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000180 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000181 I != E; ++I) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000182 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000183
184 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000185 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000186
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000187 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000189 else if (I->hasWeakLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000190 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000191 else if (!I->hasLocalLinkage())
Torok Edwinc23197a2009-07-14 16:55:14 +0000192 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000193
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000194 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000195
Dan Gohmand19a53b2008-06-30 22:03:41 +0000196 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000197 }
198 }
199
Duncan Sands1465d612009-01-28 13:14:17 +0000200 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000201 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
202 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
203 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
204 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000205
Dan Gohmana779a982008-05-05 00:28:39 +0000206 // If we don't have any trampolines, then we don't require stack memory
207 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000208 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000209 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
210 if (TAI->getNonexecutableStackDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000211 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000212
Chris Lattnera80ba712004-08-16 23:15:22 +0000213 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000214 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000215
216 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000217 return false;
218}
219
Chris Lattnere2cf37b2009-07-17 20:46:40 +0000220std::string
221AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendling6e198962007-09-18 01:47:22 +0000222 assert(MF && "No machine function?");
Chris Lattnere2cf37b2009-07-17 20:46:40 +0000223 return Mang->getMangledName(MF->getFunction(), ".eh",
224 TAI->is_EHSymbolPrivate());
Bill Wendling6e198962007-09-18 01:47:22 +0000225}
226
Chris Lattner25045bd2005-11-21 07:51:36 +0000227void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000228 // What's my mangled name?
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000229 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000230 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000231
David Greenefe37ab32009-08-18 19:22:55 +0000232 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +0000233 LI = &getAnalysis<MachineLoopInfo>();
234 }
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;
Chris Lattnera87dea42009-07-31 18:48:30 +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) {
303 SwitchToSection(CPSections[i].S);
304 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
319 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000320 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000321 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +0000322 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmancf20ac42009-08-13 01:36:44 +0000323 O << TAI->getCommentString() << " constant ";
324 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 Lattner83d77fa2009-08-01 23:46:12 +0000357 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000358 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000359 // Otherwise, drop it in the readonly section.
360 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000361 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner83d77fa2009-08-01 23:46:12 +0000362 SwitchToSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000363 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000364 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000365
366 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000367
Nate Begeman37efe672006-04-22 18:53:45 +0000368 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000369 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000370
371 // If this jump table was deleted, ignore it.
372 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000373
374 // For PIC codegen, if possible we want to use the SetDirective to reduce
375 // the number of relocations the assembler will generate for the jump table.
376 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000377 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000378 if (TAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000379 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000380 if (EmittedSets.insert(JTBBs[ii]))
381 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000382
Chris Lattner393a8ee2007-01-18 01:12:56 +0000383 // On some targets (e.g. darwin) we want to emit two consequtive labels
384 // before each jump table. The first label is never referenced, but tells
385 // the assembler and linker the extents of the jump table object. The
386 // second label is actually referenced by the code.
Evan Chengb13bafe2009-06-18 20:37:15 +0000387 if (JTInDiffSection) {
388 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
389 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
390 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000391
Evan Cheng347d39f2007-10-14 05:57:21 +0000392 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
393 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000394
Nate Begeman37efe672006-04-22 18:53:45 +0000395 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000396 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000397 O << '\n';
398 }
399 }
400}
401
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000402void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
403 const MachineBasicBlock *MBB,
404 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000405 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000406
407 // Use JumpTableDirective otherwise honor the entry size from the jump table
408 // info.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000409 const char *JTEntryDirective = TAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000410 bool HadJTEntryDirective = JTEntryDirective != NULL;
411 if (!HadJTEntryDirective) {
412 JTEntryDirective = MJTI->getEntrySize() == 4 ?
413 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
414 }
415
416 O << JTEntryDirective << ' ';
417
418 // If we have emitted set directives for the jump table entries, print
419 // them rather than the entries themselves. If we're emitting PIC, then
420 // emit the table entries as differences between two text section labels.
421 // If we're emitting non-PIC code, then emit the entries as direct
422 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000423 if (!isPIC) {
424 printBasicBlockLabel(MBB, false, false, false);
425 } else if (TAI->getSetDirective()) {
426 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
427 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000428 } else {
Evan Chengfb8075d2008-02-28 00:43:03 +0000429 printBasicBlockLabel(MBB, false, false, false);
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000430 // If the arch uses custom Jump Table directives, don't calc relative to
431 // JT
432 if (!HadJTEntryDirective)
433 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
434 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000435 }
436}
437
438
Chris Lattnered138932005-12-13 06:32:10 +0000439/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
440/// special global used by LLVM. If so, emit it and return true, otherwise
441/// do nothing and return false.
442bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000443 if (GV->getName() == "llvm.used") {
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000444 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
445 EmitLLVMUsedList(GV->getInitializer());
446 return true;
447 }
448
Chris Lattner401e10c2009-07-20 06:14:25 +0000449 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000450 if (GV->getSection() == "llvm.metadata" ||
451 GV->hasAvailableExternallyLinkage())
452 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000453
454 if (!GV->hasAppendingLinkage()) return false;
455
456 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000457
Evan Cheng916d07c2007-06-04 20:39:18 +0000458 const TargetData *TD = TM.getTargetData();
459 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000460 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner80ec2792009-08-02 00:34:36 +0000461 SwitchToSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000462 EmitAlignment(Align, 0);
463 EmitXXStructorList(GV->getInitializer());
464 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000465 }
466
Chris Lattnerf231c072009-03-09 05:52:15 +0000467 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner80ec2792009-08-02 00:34:36 +0000468 SwitchToSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000469 EmitAlignment(Align, 0);
470 EmitXXStructorList(GV->getInitializer());
471 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000472 }
473
474 return false;
475}
476
Chris Lattnercb05af82006-09-26 03:38:18 +0000477/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000478/// global in the specified llvm.used list for which emitUsedDirectiveFor
479/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000480void AsmPrinter::EmitLLVMUsedList(Constant *List) {
481 const char *Directive = TAI->getUsedDirective();
482
Dan Gohmana119de82009-06-14 23:30:43 +0000483 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000484 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
485 if (InitList == 0) return;
486
487 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000488 const GlobalValue *GV =
489 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000490 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000491 O << Directive;
492 EmitConstantValueOnly(InitList->getOperand(i));
493 O << '\n';
494 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000495 }
496}
497
Chris Lattnered138932005-12-13 06:32:10 +0000498/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
499/// function pointers, ignoring the init priority.
500void AsmPrinter::EmitXXStructorList(Constant *List) {
501 // Should be an array of '{ int, void ()* }' structs. The first value is the
502 // init priority, which we ignore.
503 if (!isa<ConstantArray>(List)) return;
504 ConstantArray *InitList = cast<ConstantArray>(List);
505 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
506 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
507 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000508
509 if (CS->getOperand(1)->isNullValue())
510 return; // Found a null terminator, exit printing.
511 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000512 EmitGlobalConstant(CS->getOperand(1));
513 }
514}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000515
Jim Laskeya1a19f82006-10-17 13:41:07 +0000516/// getGlobalLinkName - Returns the asm/link name of of the specified
517/// global variable. Should be overridden by each target asm printer to
518/// generate the appropriate value.
Bill Wendling7d16e852009-04-10 00:12:49 +0000519const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
520 std::string &LinkName) const {
Jim Laskey03742482006-11-20 20:29:06 +0000521 if (isa<Function>(GV)) {
522 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000523 LinkName += Mang->getMangledName(GV);
Jim Laskey03742482006-11-20 20:29:06 +0000524 LinkName += TAI->getFunctionAddrSuffix();
525 } else {
526 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000527 LinkName += Mang->getMangledName(GV);
Jim Laskey03742482006-11-20 20:29:06 +0000528 LinkName += TAI->getGlobalVarAddrSuffix();
529 }
530
Jim Laskey99e41ee2006-10-17 17:17:24 +0000531 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000532}
533
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000534/// EmitExternalGlobal - Emit the external reference to a global variable.
535/// Should be overridden if an indirect reference should be used.
536void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling7d16e852009-04-10 00:12:49 +0000537 std::string GLN;
538 O << getGlobalLinkName(GV, GLN);
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000539}
540
541
542
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000543//===----------------------------------------------------------------------===//
544/// LEB 128 number encoding.
545
546/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
547/// representing an unsigned leb128 value.
548void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattnera64f4632008-11-10 04:35:24 +0000549 char Buffer[20];
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000550 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000551 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000552 Value >>= 7;
553 if (Value) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000554 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000555 if (Value) O << ", ";
556 } while (Value);
557}
558
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000559/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
560/// representing a signed leb128 value.
561void AsmPrinter::PrintSLEB128(int Value) const {
562 int Sign = Value >> (8 * sizeof(Value) - 1);
563 bool IsMore;
Chris Lattnera64f4632008-11-10 04:35:24 +0000564 char Buffer[20];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000565
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000566 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000567 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000568 Value >>= 7;
569 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
570 if (IsMore) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000571 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000572 if (IsMore) O << ", ";
573 } while (IsMore);
574}
575
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000576//===--------------------------------------------------------------------===//
577// Emission and print routines
578//
579
580/// PrintHex - Print a value as a hexidecimal value.
581///
582void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerc6a13462008-11-10 04:30:26 +0000583 char Buffer[20];
Chris Lattnerc6a13462008-11-10 04:30:26 +0000584 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000585}
586
587/// EOL - Print a newline character to asm stream. If a comment is present
588/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000589void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000590 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000591}
Owen Anderson25995092008-07-01 21:16:27 +0000592
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000593void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000594 if (VerboseAsm && !Comment.empty()) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +0000595 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +0000596 O << TAI->getCommentString()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000597 << ' '
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000598 << Comment;
599 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000600 O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000601}
602
Owen Anderson25995092008-07-01 21:16:27 +0000603void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000604 if (VerboseAsm && *Comment) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +0000605 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +0000606 O << TAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000607 << ' '
608 << Comment;
609 }
610 O << '\n';
611}
612
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000613/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
614/// unsigned leb128 value.
615void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
616 if (TAI->hasLEB128()) {
617 O << "\t.uleb128\t"
618 << Value;
619 } else {
620 O << TAI->getData8bitsDirective();
621 PrintULEB128(Value);
622 }
623}
624
625/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
626/// signed leb128 value.
627void AsmPrinter::EmitSLEB128Bytes(int Value) const {
628 if (TAI->hasLEB128()) {
629 O << "\t.sleb128\t"
630 << Value;
631 } else {
632 O << TAI->getData8bitsDirective();
633 PrintSLEB128(Value);
634 }
635}
636
637/// EmitInt8 - Emit a byte directive and value.
638///
639void AsmPrinter::EmitInt8(int Value) const {
640 O << TAI->getData8bitsDirective();
641 PrintHex(Value & 0xFF);
642}
643
644/// EmitInt16 - Emit a short directive and value.
645///
646void AsmPrinter::EmitInt16(int Value) const {
647 O << TAI->getData16bitsDirective();
648 PrintHex(Value & 0xFFFF);
649}
650
651/// EmitInt32 - Emit a long directive and value.
652///
653void AsmPrinter::EmitInt32(int Value) const {
654 O << TAI->getData32bitsDirective();
655 PrintHex(Value);
656}
657
658/// EmitInt64 - Emit a long long directive and value.
659///
660void AsmPrinter::EmitInt64(uint64_t Value) const {
661 if (TAI->getData64bitsDirective()) {
662 O << TAI->getData64bitsDirective();
663 PrintHex(Value);
664 } else {
665 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000666 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000667 EmitInt32(unsigned(Value));
668 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000669 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000670 EmitInt32(unsigned(Value >> 32));
671 }
672 }
673}
674
675/// toOctal - Convert the low order bits of X into an octal digit.
676///
677static inline char toOctal(int X) {
678 return (X&7)+'0';
679}
680
681/// printStringChar - Print a char, escaped if necessary.
682///
David Greene71847812009-07-14 20:18:05 +0000683static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000684 if (C == '"') {
685 O << "\\\"";
686 } else if (C == '\\') {
687 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000688 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000689 O << C;
690 } else {
691 switch(C) {
692 case '\b': O << "\\b"; break;
693 case '\f': O << "\\f"; break;
694 case '\n': O << "\\n"; break;
695 case '\r': O << "\\r"; break;
696 case '\t': O << "\\t"; break;
697 default:
698 O << '\\';
699 O << toOctal(C >> 6);
700 O << toOctal(C >> 3);
701 O << toOctal(C >> 0);
702 break;
703 }
704 }
705}
706
707/// EmitString - Emit a string with quotes and a null terminator.
708/// Special characters are emitted properly.
709/// \literal (Eg. '\t') \endliteral
710void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendlingf34be822009-04-09 23:51:31 +0000711 EmitString(String.c_str(), String.size());
712}
713
714void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000715 const char* AscizDirective = TAI->getAscizDirective();
716 if (AscizDirective)
717 O << AscizDirective;
718 else
719 O << TAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000720 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000721 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000722 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000723 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000724 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000725 else
726 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000727}
728
729
Dan Gohman189f80d2007-09-24 20:58:13 +0000730/// EmitFile - Emit a .file directive.
731void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
732 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000733 for (unsigned i = 0, N = Name.size(); i < N; ++i)
734 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000735 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000736}
737
738
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000739//===----------------------------------------------------------------------===//
740
Chris Lattner3a420532007-05-31 18:57:45 +0000741// EmitAlignment - Emit an alignment directive to the specified power of
742// two boundary. For example, if you pass in 3 here, you will get an 8
743// byte alignment. If a global value is specified, and if that global has
744// an explicit alignment requested, it will unconditionally override the
745// alignment request. However, if ForcedAlignBits is specified, this value
746// has final say: the ultimate alignment will be the max of ForcedAlignBits
747// and the alignment computed with NumBits and the global.
748//
749// The algorithm is:
750// Align = NumBits;
751// if (GV && GV->hasalignment) Align = GV->getalignment();
752// Align = std::max(Align, ForcedAlignBits);
753//
754void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000755 unsigned ForcedAlignBits,
756 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000757 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000758 NumBits = Log2_32(GV->getAlignment());
759 NumBits = std::max(NumBits, ForcedAlignBits);
760
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000761 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskey563321a2006-09-06 18:34:40 +0000762 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng73a259a2007-07-25 23:35:07 +0000763 O << TAI->getAlignDirective() << NumBits;
Evan Chengfb8075d2008-02-28 00:43:03 +0000764
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000765 if (getCurrentSection()->getKind().isText())
Chris Lattner290c2f52009-08-03 23:20:21 +0000766 if (unsigned FillValue = TAI->getTextAlignFillValue()) {
767 O << ',';
768 PrintHex(FillValue);
769 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000770 O << '\n';
Chris Lattnerbfddc202004-08-17 19:14:29 +0000771}
David Greenea5bb59f2009-08-05 21:00:52 +0000772
Chris Lattner25045bd2005-11-21 07:51:36 +0000773/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000774///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000775void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000776 if (NumZeros) {
Jim Laskey563321a2006-09-06 18:34:40 +0000777 if (TAI->getZeroDirective()) {
778 O << TAI->getZeroDirective() << NumZeros;
779 if (TAI->getZeroDirectiveSuffix())
780 O << TAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000781 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000782 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000783 for (; NumZeros; --NumZeros)
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000784 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000785 }
786 }
787}
788
Chris Lattnera80ba712004-08-16 23:15:22 +0000789// Print out the specified constant, without a storage class. Only the
790// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000791void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000792 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohmand19a53b2008-06-30 22:03:41 +0000793 O << '0';
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000794 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000795 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000796 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000797 // This is a constant address for a global variable or function. Use the
798 // name of the variable or function as the address value, possibly
799 // decorating it with GlobalVarAddrPrefix/Suffix or
800 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000801 if (isa<Function>(GV)) {
802 O << TAI->getFunctionAddrPrefix()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000803 << Mang->getMangledName(GV)
Jim Laskey563321a2006-09-06 18:34:40 +0000804 << TAI->getFunctionAddrSuffix();
805 } else {
806 O << TAI->getGlobalVarAddrPrefix()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000807 << Mang->getMangledName(GV)
Jim Laskey563321a2006-09-06 18:34:40 +0000808 << TAI->getGlobalVarAddrSuffix();
809 }
Duraid Madina855a5192005-04-02 12:21:51 +0000810 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000811 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000812 unsigned Opcode = CE->getOpcode();
813 switch (Opcode) {
Chris Lattnerc19ee612009-08-01 22:25:12 +0000814 case Instruction::Trunc:
815 case Instruction::ZExt:
816 case Instruction::SExt:
817 case Instruction::FPTrunc:
818 case Instruction::FPExt:
819 case Instruction::UIToFP:
820 case Instruction::SIToFP:
821 case Instruction::FPToUI:
822 case Instruction::FPToSI:
823 llvm_unreachable("FIXME: Don't support this constant cast expr");
Chris Lattnera80ba712004-08-16 23:15:22 +0000824 case Instruction::GetElementPtr: {
825 // generate a symbolic expression for the byte address
826 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000827 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
828 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
829 idxVec.size())) {
Chris Lattner4798bbe2009-02-05 06:55:21 +0000830 // Truncate/sext the offset to the pointer size.
831 if (TD->getPointerSizeInBits() != 64) {
832 int SExtAmount = 64-TD->getPointerSizeInBits();
833 Offset = (Offset << SExtAmount) >> SExtAmount;
834 }
835
Chris Lattner27e19212005-02-14 21:40:26 +0000836 if (Offset)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000837 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000838 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000839 if (Offset > 0)
840 O << ") + " << Offset;
841 else if (Offset < 0)
842 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000843 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000844 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000845 }
846 break;
847 }
Chris Lattnercb0a6812006-12-12 05:14:13 +0000848 case Instruction::BitCast:
849 return EmitConstantValueOnly(CE->getOperand(0));
850
Chris Lattnerddc94012006-12-12 05:18:19 +0000851 case Instruction::IntToPtr: {
852 // Handle casts to pointers by changing them into casts to the appropriate
853 // integer type. This promotes constant folding and simplifies this code.
854 Constant *Op = CE->getOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +0000855 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
856 false/*ZExt*/);
Chris Lattnerddc94012006-12-12 05:18:19 +0000857 return EmitConstantValueOnly(Op);
858 }
859
860
861 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000862 // Support only foldable casts to/from pointers that can be eliminated by
863 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000864 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000865 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000866
Chris Lattnerddc94012006-12-12 05:18:19 +0000867 // We can emit the pointer value into this slot if the slot is an
868 // integer slot greater or equal to the size of the pointer.
Chris Lattnerc19ee612009-08-01 22:25:12 +0000869 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000870 return EmitConstantValueOnly(Op);
Nick Lewyckyd6227382008-08-08 06:34:07 +0000871
872 O << "((";
Chris Lattner25045bd2005-11-21 07:51:36 +0000873 EmitConstantValueOnly(Op);
Chris Lattnerc19ee612009-08-01 22:25:12 +0000874 APInt ptrMask =
875 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattnerfad86b02008-08-17 07:19:36 +0000876
877 SmallString<40> S;
878 ptrMask.toStringUnsigned(S);
879 O << ") & " << S.c_str() << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000880 break;
881 }
882 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000883 case Instruction::Sub:
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000884 case Instruction::And:
885 case Instruction::Or:
886 case Instruction::Xor:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000887 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000888 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000889 O << ')';
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000890 switch (Opcode) {
891 case Instruction::Add:
892 O << " + ";
893 break;
894 case Instruction::Sub:
895 O << " - ";
896 break;
897 case Instruction::And:
898 O << " & ";
899 break;
900 case Instruction::Or:
901 O << " | ";
902 break;
903 case Instruction::Xor:
904 O << " ^ ";
905 break;
906 default:
907 break;
908 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000909 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000910 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000911 O << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000912 break;
913 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000914 llvm_unreachable("Unsupported operator!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000915 }
916 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000917 llvm_unreachable("Unknown constant value!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000918 }
919}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000920
Chris Lattner2980cef2005-11-10 18:06:33 +0000921/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000922/// the predicate isString is true.
923///
David Greene71847812009-07-14 20:18:05 +0000924static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000925 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000926 assert(CVA->isString() && "Array is not string compatible!");
927
Dan Gohmand19a53b2008-06-30 22:03:41 +0000928 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000929 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000930 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000931 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000932 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000933 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000934 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000935}
936
Jeff Cohenc884db42006-05-02 01:16:28 +0000937/// EmitString - Emit a zero-byte-terminated string constant.
938///
939void AsmPrinter::EmitString(const ConstantArray *CVA) const {
940 unsigned NumElts = CVA->getNumOperands();
Jim Laskey563321a2006-09-06 18:34:40 +0000941 if (TAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000942 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskey563321a2006-09-06 18:34:40 +0000943 O << TAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000944 printAsCString(O, CVA, NumElts-1);
945 } else {
Jim Laskey563321a2006-09-06 18:34:40 +0000946 O << TAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000947 printAsCString(O, CVA, NumElts);
948 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000949 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000950}
951
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000952void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
953 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000954 if (CVA->isString()) {
955 EmitString(CVA);
956 } else { // Not a string. Print the values in successive locations
957 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000958 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000959 }
960}
961
962void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
963 const VectorType *PTy = CP->getType();
964
965 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
966 EmitGlobalConstant(CP->getOperand(I));
967}
968
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000969void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
970 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000971 // Print the fields in successive locations. Pad to align if needed!
972 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +0000973 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000974 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
975 uint64_t sizeSoFar = 0;
976 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
977 const Constant* field = CVS->getOperand(i);
978
979 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +0000980 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000981 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
982 - cvsLayout->getElementOffset(i)) - fieldSize;
983 sizeSoFar += fieldSize + padSize;
984
985 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000986 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000987
988 // Insert padding - this may include padding to increase the size of the
989 // current field up to the ABI size (if the struct is not packed) as well
990 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000991 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000992 }
993 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
994 "Layout of constant struct may be incorrect!");
995}
996
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000997void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
998 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000999 // FP Constants are printed as integer constants to avoid losing
1000 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001001 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001002 const TargetData *TD = TM.getTargetData();
Owen Anderson1d0be152009-08-13 21:58:54 +00001003 if (CFP->getType() == Type::getDoubleTy(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001004 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1005 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001006 if (TAI->getData64bitsDirective(AddrSpace)) {
1007 O << TAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001008 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001009 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmancf20ac42009-08-13 01:36:44 +00001010 O << TAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001011 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001012 O << '\n';
1013 } else if (TD->isBigEndian()) {
1014 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001015 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001016 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001017 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001018 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001019 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001020 O << '\n';
1021 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001022 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001023 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001024 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001025 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001026 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001027 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001028 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001029 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001030 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001031 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001032 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001033 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001034 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001035 O << '\n';
1036 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001037 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001038 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001039 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001040 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001041 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001042 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001043 }
1044 return;
Owen Anderson1d0be152009-08-13 21:58:54 +00001045 } else if (CFP->getType() == Type::getFloatTy(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001046 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001047 O << TAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001048 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001049 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001050 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001051 O << TAI->getCommentString() << " float " << Val;
1052 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001053 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001054 return;
Owen Anderson1d0be152009-08-13 21:58:54 +00001055 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001056 // all long double variants are printed as hex
1057 // api needed to prevent premature destruction
1058 APInt api = CFP->getValueAPF().bitcastToAPInt();
1059 const uint64_t *p = api.getRawData();
1060 // Convert to double so we can print the approximate val as a comment.
1061 APFloat DoubleVal = CFP->getValueAPF();
1062 bool ignored;
1063 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1064 &ignored);
1065 if (TD->isBigEndian()) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001066 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001067 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001068 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001069 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001070 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001071 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001072 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001073 O << '\n';
1074 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001075 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001076 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmancf20ac42009-08-13 01:36:44 +00001077 O << TAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001078 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001079 O << '\n';
1080 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001081 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001082 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmancf20ac42009-08-13 01:36:44 +00001083 O << TAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001084 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001085 O << '\n';
1086 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001087 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001088 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmancf20ac42009-08-13 01:36:44 +00001089 O << TAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001090 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001091 O << '\n';
1092 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001093 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001094 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001095 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001096 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001097 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001098 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001099 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001100 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001101 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001102 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001103 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001104 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001105 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001106 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001107 O << '\n';
1108 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001109 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001110 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001111 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001112 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001113 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001114 O << '\n';
1115 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001116 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001117 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001118 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001119 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001120 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001121 O << '\n';
1122 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001123 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001124 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001125 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001126 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001127 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001128 O << '\n';
1129 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001130 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001131 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001132 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001133 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001134 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001135 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001136 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001137 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1138 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001139 return;
Owen Anderson1d0be152009-08-13 21:58:54 +00001140 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001141 // all long double variants are printed as hex
1142 // api needed to prevent premature destruction
1143 APInt api = CFP->getValueAPF().bitcastToAPInt();
1144 const uint64_t *p = api.getRawData();
1145 if (TD->isBigEndian()) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001146 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001147 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001148 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001149 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001150 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001151 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001152 O << '\n';
1153 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001154 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001155 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001156 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001157 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001158 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001159 O << '\n';
1160 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001161 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001162 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001163 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001164 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001165 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001166 O << '\n';
1167 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001168 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001169 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001170 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001171 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001172 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001173 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001174 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001175 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001176 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001177 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001178 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001179 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001180 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001181 O << '\n';
1182 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001183 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001184 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001185 O << TAI->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';
1189 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001190 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001191 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001192 O << TAI->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';
1196 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001197 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001198 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001199 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001200 << " most 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 }
1204 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001205 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001206}
1207
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001208void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1209 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001210 const TargetData *TD = TM.getTargetData();
1211 unsigned BitWidth = CI->getBitWidth();
1212 assert(isPowerOf2_32(BitWidth) &&
1213 "Non-power-of-2-sized integers not handled!");
1214
1215 // We don't expect assemblers to support integer data directives
1216 // for more than 64 bits, so we emit the data in at most 64-bit
1217 // quantities at a time.
1218 const uint64_t *RawData = CI->getValue().getRawData();
1219 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1220 uint64_t Val;
1221 if (TD->isBigEndian())
1222 Val = RawData[e - i - 1];
1223 else
1224 Val = RawData[i];
1225
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001226 if (TAI->getData64bitsDirective(AddrSpace))
1227 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001228 else if (TD->isBigEndian()) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001229 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001230 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001231 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001232 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001233 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001234 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001235 O << '\n';
1236 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001237 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001238 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001239 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001240 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001241 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001242 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001243 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001244 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001245 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001246 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001247 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001248 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001249 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001250 O << '\n';
1251 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001252 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001253 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001254 O << TAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001255 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001256 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001257 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001258 }
1259 }
1260}
1261
Chris Lattner25045bd2005-11-21 07:51:36 +00001262/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001263void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001264 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001265 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001266 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001267
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001268 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001269 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001270 return;
1271 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001272 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001273 return;
1274 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001275 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001276 return;
1277 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001278 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001279 return;
1280 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1281 // Small integers are handled below; large integers are handled here.
1282 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001283 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001284 return;
1285 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001286 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001287 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001288 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001289 }
1290
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001291 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001292 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001293 if (VerboseAsm) {
1294 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1295 SmallString<40> S;
1296 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001297 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmane45da0d2009-08-12 18:47:05 +00001298 O << TAI->getCommentString() << " 0x" << S.c_str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001299 }
Scott Micheleefc8452008-06-03 15:39:51 +00001300 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001301 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001302}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001303
Chris Lattnerfad86b02008-08-17 07:19:36 +00001304void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001305 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001306 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001307}
1308
Chris Lattner3ce9b672006-09-26 23:59:50 +00001309/// PrintSpecial - Print information related to the specified machine instr
1310/// that is independent of the operand, and may be independent of the instr
1311/// itself. This can be useful for portably encoding the comment character
1312/// or other bits of target-specific knowledge into the asmstrings. The
1313/// syntax used is ${:comment}. Targets can override this to add support
1314/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001315void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001316 if (!strcmp(Code, "private")) {
1317 O << TAI->getPrivateGlobalPrefix();
1318 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001319 if (VerboseAsm)
1320 O << TAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001321 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001322 // Comparing the address of MI isn't sufficient, because machineinstrs may
1323 // be allocated to the same address across functions.
1324 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1325
Owen Andersonbd58edf2009-06-24 22:28:12 +00001326 // If this is a new LastFn instruction, bump the counter.
1327 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001328 ++Counter;
1329 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001330 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001331 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001332 O << Counter;
1333 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001334 std::string msg;
1335 raw_string_ostream Msg(msg);
1336 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001337 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001338 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001339 }
1340}
1341
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001342/// processDebugLoc - Processes the debug information of each machine
1343/// instruction's DebugLoc.
1344void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattnerd2503292009-08-05 04:09:18 +00001345 if (!TAI || !DW)
1346 return;
1347
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001348 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1349 if (!DL.isUnknown()) {
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001350 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1351
1352 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1353 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1354 DICompileUnit(CurDLT.CompileUnit)));
1355
1356 PrevDLT = CurDLT;
1357 }
1358 }
1359}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001360
Chris Lattner0264d1a2006-01-27 02:10:10 +00001361/// printInlineAsm - This method formats and prints the specified machine
1362/// instruction that is an inline asm.
1363void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001364 unsigned NumOperands = MI->getNumOperands();
1365
1366 // Count the number of register definitions.
1367 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001368 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001369 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001370 assert(NumDefs != NumOperands-1 && "No asm string?");
1371
Dan Gohmand735b802008-10-03 15:45:36 +00001372 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001373
1374 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001375 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001376
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001377 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1378 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001379 if (AsmStr[0] == 0) {
Chris Lattnere2b06012009-08-11 22:39:40 +00001380 O << TAI->getCommentString() << TAI->getInlineAsmStart() << "\n\t";
1381 O << TAI->getCommentString() << TAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001382 return;
1383 }
1384
Chris Lattnere2b06012009-08-11 22:39:40 +00001385 O << TAI->getCommentString() << TAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001386
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001387 // The variant of the current asmprinter.
1388 int AsmPrinterVariant = TAI->getAssemblerDialect();
1389
Chris Lattner66099132006-02-01 22:41:11 +00001390 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1391 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001392
Chris Lattner66099132006-02-01 22:41:11 +00001393 while (*LastEmitted) {
1394 switch (*LastEmitted) {
1395 default: {
1396 // Not a special case, emit the string section literally.
1397 const char *LiteralEnd = LastEmitted+1;
1398 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001399 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001400 ++LiteralEnd;
1401 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1402 O.write(LastEmitted, LiteralEnd-LastEmitted);
1403 LastEmitted = LiteralEnd;
1404 break;
1405 }
Chris Lattner1c059972006-05-05 21:47:05 +00001406 case '\n':
1407 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001408 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001409 break;
Chris Lattner66099132006-02-01 22:41:11 +00001410 case '$': {
1411 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001412 bool Done = true;
1413
1414 // Handle escapes.
1415 switch (*LastEmitted) {
1416 default: Done = false; break;
1417 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001418 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1419 O << '$';
1420 ++LastEmitted; // Consume second '$' character.
1421 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001422 case '(': // $( -> same as GCC's { character.
1423 ++LastEmitted; // Consume '(' character.
1424 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001425 llvm_report_error("Nested variants found in inline asm string: '"
1426 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001427 }
1428 CurVariant = 0; // We're in the first variant now.
1429 break;
1430 case '|':
1431 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001432 if (CurVariant == -1)
1433 O << '|'; // this is gcc's behavior for | outside a variant
1434 else
1435 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001436 break;
1437 case ')': // $) -> same as GCC's } char.
1438 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001439 if (CurVariant == -1)
1440 O << '}'; // this is gcc's behavior for } outside a variant
1441 else
1442 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001443 break;
Chris Lattner66099132006-02-01 22:41:11 +00001444 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001445 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001446
1447 bool HasCurlyBraces = false;
1448 if (*LastEmitted == '{') { // ${variable}
1449 ++LastEmitted; // Consume '{' character.
1450 HasCurlyBraces = true;
1451 }
1452
Chris Lattner3e0cc262009-03-10 05:37:13 +00001453 // If we have ${:foo}, then this is not a real operand reference, it is a
1454 // "magic" string reference, just like in .td files. Arrange to call
1455 // PrintSpecial.
1456 if (HasCurlyBraces && *LastEmitted == ':') {
1457 ++LastEmitted;
1458 const char *StrStart = LastEmitted;
1459 const char *StrEnd = strchr(StrStart, '}');
1460 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001461 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1462 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001463 }
1464
1465 std::string Val(StrStart, StrEnd);
1466 PrintSpecial(MI, Val.c_str());
1467 LastEmitted = StrEnd+1;
1468 break;
1469 }
1470
Chris Lattner66099132006-02-01 22:41:11 +00001471 const char *IDStart = LastEmitted;
1472 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001473 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001474 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1475 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001476 llvm_report_error("Bad $ operand number in inline asm string: '"
1477 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001478 }
1479 LastEmitted = IDEnd;
1480
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001481 char Modifier[2] = { 0, 0 };
1482
Chris Lattner66099132006-02-01 22:41:11 +00001483 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001484 // If we have curly braces, check for a modifier character. This
1485 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1486 if (*LastEmitted == ':') {
1487 ++LastEmitted; // Consume ':' character.
1488 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001489 llvm_report_error("Bad ${:} expression in inline asm string: '"
1490 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001491 }
1492
1493 Modifier[0] = *LastEmitted;
1494 ++LastEmitted; // Consume modifier character.
1495 }
1496
Chris Lattner66099132006-02-01 22:41:11 +00001497 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001498 llvm_report_error("Bad ${} expression in inline asm string: '"
1499 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001500 }
1501 ++LastEmitted; // Consume '}' character.
1502 }
1503
1504 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001505 llvm_report_error("Invalid $ operand number in inline asm string: '"
1506 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001507 }
1508
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001509 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001510 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001511 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1512 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001513
1514 bool Error = false;
1515
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001516 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001517 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001518 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001519 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001520 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001521 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001522
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001523 if (OpNo >= MI->getNumOperands()) {
1524 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001525 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001526 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001527 ++OpNo; // Skip over the ID number.
1528
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001529 if (Modifier[0]=='l') // labels are target independent
Chris Lattner8aa797a2007-12-30 23:10:15 +00001530 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengfb8075d2008-02-28 00:43:03 +00001531 false, false, false);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001532 else {
1533 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001534 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001535 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1536 Modifier[0] ? Modifier : 0);
1537 } else {
1538 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1539 Modifier[0] ? Modifier : 0);
1540 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001541 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001542 }
1543 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001544 std::string msg;
1545 raw_string_ostream Msg(msg);
1546 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001547 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001548 MI->print(Msg);
1549 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001550 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001551 }
Chris Lattner66099132006-02-01 22:41:11 +00001552 break;
1553 }
Chris Lattner66099132006-02-01 22:41:11 +00001554 }
1555 }
Chris Lattnere2b06012009-08-11 22:39:40 +00001556 O << "\n\t" << TAI->getCommentString() << TAI->getInlineAsmEnd() << '\n';
Chris Lattner66099132006-02-01 22:41:11 +00001557}
1558
Evan Chengda47e6e2008-03-15 00:03:38 +00001559/// printImplicitDef - This method prints the specified machine instruction
1560/// that is an implicit def.
1561void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohmana12e9d72009-08-12 18:32:22 +00001562 if (VerboseAsm) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001563 O.PadToColumn(TAI->getCommentColumn());
Dan Gohmana12e9d72009-08-12 18:32:22 +00001564 O << TAI->getCommentString() << " implicit-def: "
Evan Chengf1c0ae92009-03-24 00:17:40 +00001565 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Dan Gohmana12e9d72009-08-12 18:32:22 +00001566 }
Evan Chengda47e6e2008-03-15 00:03:38 +00001567}
1568
Jim Laskey1ee29252007-01-26 14:34:52 +00001569/// printLabel - This method prints a local label used by debug and
1570/// exception handling tables.
1571void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001572 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001573}
1574
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001575void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng4eecdeb2008-02-02 08:39:46 +00001576 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001577}
1578
Evan Chenga844bde2008-02-02 04:07:54 +00001579/// printDeclare - This method prints a local variable declaration used by
1580/// debug tables.
Evan Cheng4e3f5a42008-02-04 23:06:48 +00001581/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1582/// entry into dwarf table.
Evan Chenga844bde2008-02-02 04:07:54 +00001583void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Patelc48c5502009-01-13 21:44:10 +00001584 unsigned FI = MI->getOperand(0).getIndex();
Evan Cheng4e3f5a42008-02-04 23:06:48 +00001585 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel1be3ecc2009-04-15 00:10:26 +00001586 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Chenga844bde2008-02-02 04:07:54 +00001587}
1588
Chris Lattner66099132006-02-01 22:41:11 +00001589/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1590/// instruction, using the specified assembler variant. Targets should
1591/// overried this to format as appropriate.
1592bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001593 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001594 // Target doesn't support this yet!
1595 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001596}
Chris Lattnerdd260332006-02-24 20:21:58 +00001597
1598bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1599 unsigned AsmVariant,
1600 const char *ExtraCode) {
1601 // Target doesn't support this yet!
1602 return true;
1603}
Nate Begeman37efe672006-04-22 18:53:45 +00001604
1605/// printBasicBlockLabel - This method prints the label for the specified
1606/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +00001607void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengfb8075d2008-02-28 00:43:03 +00001608 bool printAlign,
Nate Begemancdf38c42006-05-02 05:37:32 +00001609 bool printColon,
1610 bool printComment) const {
Evan Chengfb8075d2008-02-28 00:43:03 +00001611 if (printAlign) {
1612 unsigned Align = MBB->getAlignment();
1613 if (Align)
1614 EmitAlignment(Log2_32(Align));
1615 }
1616
Dan Gohmand19a53b2008-06-30 22:03:41 +00001617 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng347d39f2007-10-14 05:57:21 +00001618 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +00001619 if (printColon)
1620 O << ':';
David Greeneb71d1b22009-08-10 16:38:07 +00001621 if (printComment) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001622 if (const BasicBlock *BB = MBB->getBasicBlock())
1623 if (BB->hasName()) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001624 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman46d50562009-08-12 20:56:56 +00001625 O << TAI->getCommentString() << ' ';
1626 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001627 }
David Greeneb71d1b22009-08-10 16:38:07 +00001628
1629 if (printColon)
1630 EmitComments(*MBB);
1631 }
Nate Begeman37efe672006-04-22 18:53:45 +00001632}
Nate Begeman52a51e382006-08-12 21:29:52 +00001633
Evan Chengcc415862007-11-09 01:32:10 +00001634/// printPICJumpTableSetLabel - This method prints a set label for the
1635/// specified MachineBasicBlock for a jumptable entry.
1636void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1637 const MachineBasicBlock *MBB) const {
Jim Laskey563321a2006-09-06 18:34:40 +00001638 if (!TAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001639 return;
1640
Jim Laskey563321a2006-09-06 18:34:40 +00001641 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001642 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +00001643 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001644 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1645 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001646}
Evan Chengd6594ae2006-09-12 21:00:35 +00001647
Evan Chengcc415862007-11-09 01:32:10 +00001648void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1649 const MachineBasicBlock *MBB) const {
Evan Cheng41349c12006-11-01 09:23:08 +00001650 if (!TAI->getSetDirective())
1651 return;
1652
1653 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001654 << getFunctionNumber() << '_' << uid << '_' << uid2
1655 << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +00001656 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng347d39f2007-10-14 05:57:21 +00001657 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1658 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001659}
1660
Evan Chengd6594ae2006-09-12 21:00:35 +00001661/// printDataDirective - This method prints the asm directive for the
1662/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001663void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001664 const TargetData *TD = TM.getTargetData();
1665 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001666 case Type::FloatTyID: case Type::DoubleTyID:
1667 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1668 assert(0 && "Should have already output floating point constant.");
1669 default:
1670 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001671 case Type::IntegerTyID: {
1672 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1673 if (BitWidth <= 8)
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001674 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001675 else if (BitWidth <= 16)
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001676 O << TAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001677 else if (BitWidth <= 32)
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001678 O << TAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001679 else if (BitWidth <= 64) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001680 assert(TAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001681 "Target cannot handle 64-bit constant exprs!");
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001682 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001683 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001684 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001685 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001686 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001687 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001688 case Type::PointerTyID:
1689 if (TD->getPointerSize() == 8) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001690 assert(TAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001691 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001692 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001693 } else if (TD->getPointerSize() == 2) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001694 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001695 } else if (TD->getPointerSize() == 1) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001696 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001697 } else {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001698 O << TAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001699 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001700 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001701 }
1702}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001703
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001704void AsmPrinter::printVisibility(const std::string& Name,
1705 unsigned Visibility) const {
1706 if (Visibility == GlobalValue::HiddenVisibility) {
1707 if (const char *Directive = TAI->getHiddenDirective())
1708 O << Directive << Name << '\n';
1709 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1710 if (const char *Directive = TAI->getProtectedDirective())
1711 O << Directive << Name << '\n';
1712 }
1713}
Gordon Henriksenc317a602008-08-17 12:08:44 +00001714
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001715void AsmPrinter::printOffset(int64_t Offset) const {
1716 if (Offset > 0)
1717 O << '+' << Offset;
1718 else if (Offset < 0)
1719 O << Offset;
1720}
1721
Daniel Dunbar575327b2009-08-14 03:43:57 +00001722void AsmPrinter::printMCInst(const MCInst *MI) {
1723 llvm_unreachable("MCInst printing unavailable on this target!");
1724}
1725
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001726GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1727 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001728 return 0;
1729
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001730 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001731 if (GCPI != GCMetadataPrinters.end())
1732 return GCPI->second;
1733
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001734 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001735
1736 for (GCMetadataPrinterRegistry::iterator
1737 I = GCMetadataPrinterRegistry::begin(),
1738 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1739 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001740 GCMetadataPrinter *GMP = I->instantiate();
1741 GMP->S = S;
1742 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1743 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001744 }
1745
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001746 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001747 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001748}
David Greene014700c2009-07-13 20:25:48 +00001749
1750/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001751void AsmPrinter::EmitComments(const MachineInstr &MI) const {
1752 if (!VerboseAsm ||
1753 MI.getDebugLoc().isUnknown())
1754 return;
1755
1756 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene3ac1ab82009-07-16 22:24:20 +00001757
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001758 // Print source line info.
1759 O.PadToColumn(TAI->getCommentColumn());
Chris Lattner5f51cd02009-08-07 23:42:01 +00001760 O << TAI->getCommentString() << " SrcLine ";
1761 if (DLT.CompileUnit->hasInitializer()) {
1762 Constant *Name = DLT.CompileUnit->getInitializer();
1763 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1764 if (NameString->isString())
1765 O << NameString->getAsString() << " ";
David Greene3ac1ab82009-07-16 22:24:20 +00001766 }
Chris Lattner5f51cd02009-08-07 23:42:01 +00001767 O << DLT.Line;
1768 if (DLT.Col != 0)
1769 O << ":" << DLT.Col;
David Greene014700c2009-07-13 20:25:48 +00001770}
1771
1772/// EmitComments - Pretty-print comments for instructions
1773void AsmPrinter::EmitComments(const MCInst &MI) const
1774{
David Greene67e59832009-07-22 20:33:26 +00001775 if (VerboseAsm) {
1776 if (!MI.getDebugLoc().isUnknown()) {
1777 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene3ac1ab82009-07-16 22:24:20 +00001778
David Greene67e59832009-07-22 20:33:26 +00001779 // Print source line info
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001780 O.PadToColumn(TAI->getCommentColumn());
David Greene67e59832009-07-22 20:33:26 +00001781 O << TAI->getCommentString() << " SrcLine ";
1782 if (DLT.CompileUnit->hasInitializer()) {
1783 Constant *Name = DLT.CompileUnit->getInitializer();
1784 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1785 if (NameString->isString()) {
1786 O << NameString->getAsString() << " ";
1787 }
1788 }
1789 O << DLT.Line;
1790 if (DLT.Col != 0)
1791 O << ":" << DLT.Col;
1792 }
David Greene3ac1ab82009-07-16 22:24:20 +00001793 }
David Greene014700c2009-07-13 20:25:48 +00001794}
David Greeneb71d1b22009-08-10 16:38:07 +00001795
David Greenefe37ab32009-08-18 19:22:55 +00001796/// Indent - Insert spaces into the character output stream. The
1797/// "level" is multiplied by the "scale" to calculate the number of
1798/// spaces to insert. "level" can represent something like loop
1799/// nesting level, for example.
1800///
1801static formatted_raw_ostream &
1802Indent(formatted_raw_ostream &out, int level, int scale = 2) {
1803 for(int i = 0; i < level*scale; ++i) {
1804 out << " ";
1805 }
1806 return out;
1807}
1808
1809/// PrintChildLoopComment - Print comments about child loops within
1810/// the loop for this basic block, with nesting.
1811///
1812static void PrintChildLoopComment(formatted_raw_ostream &O,
1813 const MachineLoop *loop,
1814 const TargetAsmInfo *TAI,
1815 int FunctionNumber) {
1816 // Add child loop information
1817 for(MachineLoop::iterator cl = loop->begin(),
1818 clend = loop->end();
1819 cl != clend;
1820 ++cl) {
1821 MachineBasicBlock *Header = (*cl)->getHeader();
1822 assert(Header && "No header for loop");
1823
1824 O << '\n';
1825 O.PadToColumn(TAI->getCommentColumn());
1826
1827 O << TAI->getCommentString();
1828 Indent(O, (*cl)->getLoopDepth()-1)
1829 << " Child Loop BB" << FunctionNumber << "_"
1830 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1831
1832 PrintChildLoopComment(O, *cl, TAI, FunctionNumber);
1833 }
1834}
1835
David Greeneb71d1b22009-08-10 16:38:07 +00001836/// EmitComments - Pretty-print comments for basic blocks
1837void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1838{
David Greenefe37ab32009-08-18 19:22:55 +00001839 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00001840 // Add loop depth information
1841 const MachineLoop *loop = LI->getLoopFor(&MBB);
1842
1843 if (loop) {
1844 // Print a newline after bb# annotation.
1845 O << "\n";
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001846 O.PadToColumn(TAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001847 O << TAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
1848 << '\n';
1849
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001850 O.PadToColumn(TAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001851
1852 MachineBasicBlock *Header = loop->getHeader();
1853 assert(Header && "No header for loop");
1854
1855 if (Header == &MBB) {
1856 O << TAI->getCommentString() << " Loop Header";
David Greenefe37ab32009-08-18 19:22:55 +00001857 PrintChildLoopComment(O, loop, TAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00001858 }
1859 else {
1860 O << TAI->getCommentString() << " Loop Header is BB"
1861 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1862 }
1863
1864 if (loop->empty()) {
1865 O << '\n';
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001866 O.PadToColumn(TAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001867 O << TAI->getCommentString() << " Inner Loop";
1868 }
1869
1870 // Add parent loop information
1871 for (const MachineLoop *CurLoop = loop->getParentLoop();
1872 CurLoop;
1873 CurLoop = CurLoop->getParentLoop()) {
1874 MachineBasicBlock *Header = CurLoop->getHeader();
1875 assert(Header && "No header for loop");
1876
1877 O << '\n';
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001878 O.PadToColumn(TAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001879 O << TAI->getCommentString();
1880 Indent(O, CurLoop->getLoopDepth()-1)
1881 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00001882 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1883 }
1884 }
1885 }
1886}