blob: e4d8ef06f1ba5cd19d52ef224278b9b79b6a5b95 [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"
Chris Lattneraf76e592009-08-22 20:48:53 +000035#include "llvm/MC/MCAsmInfo.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,
Chris Lattneraf76e592009-08-22 20:48:53 +000053 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000054 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000055 TM(tm), MAI(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
Chris Lattnerdabf07c2009-08-18 06:15:16 +000083/// getCurrentSection() - Return the current section we are emitting to.
84const MCSection *AsmPrinter::getCurrentSection() const {
85 return OutStreamer.getCurrentSection();
86}
87
88
Gordon Henriksence224772008-01-07 01:30:38 +000089void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000090 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +000091 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000092 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +000093 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +000094 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +000095}
96
Chris Lattnera80ba712004-08-16 23:15:22 +000097bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +000098 // Initialize TargetLoweringObjectFile.
99 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
100 .Initialize(OutContext, TM);
101
Chris Lattner33adcfb2009-08-22 21:43:10 +0000102 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
103 MAI->getLinkerPrivateGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +0000104
Chris Lattner33adcfb2009-08-22 21:43:10 +0000105 if (MAI->doesAllowQuotesInName())
Chris Lattnera93ca922009-06-18 23:41:35 +0000106 Mang->setUseQuotes(true);
107
Duncan Sands1465d612009-01-28 13:14:17 +0000108 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000109 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola952b8392008-12-03 11:01:37 +0000110
Chris Lattner33adcfb2009-08-22 21:43:10 +0000111 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000112 /* Very minimal debug info. It is ignored if we emit actual
113 debug info. If we don't, this at helps the user find where
114 a function came from. */
115 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
116 }
117
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000118 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
119 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000120 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000121
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000122 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000123 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000124 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000125 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000126 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000127
Chris Lattner33adcfb2009-08-22 21:43:10 +0000128 if (MAI->doesSupportDebugInformation() ||
129 MAI->doesSupportExceptionHandling()) {
Chris Lattner0de1fc42009-06-24 19:09:55 +0000130 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
131 if (MMI)
Devang Patel14a55d92009-06-19 21:54:26 +0000132 MMI->AnalyzeModule(M);
Chris Lattner0de1fc42009-06-24 19:09:55 +0000133 DW = getAnalysisIfAvailable<DwarfWriter>();
134 if (DW)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000135 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000136 }
137
Chris Lattnera80ba712004-08-16 23:15:22 +0000138 return false;
139}
140
141bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000142 // Emit global variables.
143 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
144 I != E; ++I)
145 PrintGlobalVariable(I);
146
Chris Lattner1f522fe2009-06-24 18:54:37 +0000147 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000148 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000149 DW->EndModule();
150
Chris Lattner0a7befa2009-06-24 18:52:01 +0000151 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000152 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000153 // FIXME: This is not lazy, it would be nice to only print weak references
154 // to stuff that is actually used. Note that doing so would require targets
155 // to notice uses in operands (due to constant exprs etc). This should
156 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000157
Chris Lattner0a7befa2009-06-24 18:52:01 +0000158 // Print out module-level global variables here.
159 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
160 I != E; ++I) {
161 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000162 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000163 }
164
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000165 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000166 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000167 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000168 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000169 }
170
Chris Lattner33adcfb2009-08-22 21:43:10 +0000171 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000172 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000173 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000174 I != E; ++I) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000175 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000176
177 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000178 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000179
Chris Lattner33adcfb2009-08-22 21:43:10 +0000180 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000181 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000182 else if (I->hasWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000183 O << MAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000184 else if (!I->hasLocalLinkage())
Torok Edwinc23197a2009-07-14 16:55:14 +0000185 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000186
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000187 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000188
Chris Lattner33adcfb2009-08-22 21:43:10 +0000189 O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000190 }
191 }
192
Duncan Sands1465d612009-01-28 13:14:17 +0000193 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000194 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
195 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
196 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000197 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000198
Dan Gohmana779a982008-05-05 00:28:39 +0000199 // If we don't have any trampolines, then we don't require stack memory
200 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000201 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000202 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000203 if (MAI->getNonexecutableStackDirective())
204 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000205
Chris Lattnera80ba712004-08-16 23:15:22 +0000206 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000207 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000208
209 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000210 return false;
211}
212
Chris Lattnere2cf37b2009-07-17 20:46:40 +0000213std::string
214AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendling6e198962007-09-18 01:47:22 +0000215 assert(MF && "No machine function?");
Chris Lattnere2cf37b2009-07-17 20:46:40 +0000216 return Mang->getMangledName(MF->getFunction(), ".eh",
Chris Lattner33adcfb2009-08-22 21:43:10 +0000217 MAI->is_EHSymbolPrivate());
Bill Wendling6e198962007-09-18 01:47:22 +0000218}
219
Chris Lattner25045bd2005-11-21 07:51:36 +0000220void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000221 // What's my mangled name?
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000222 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000223 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000224
David Greenefe37ab32009-08-18 19:22:55 +0000225 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +0000226 LI = &getAnalysis<MachineLoopInfo>();
227 }
Chris Lattnera80ba712004-08-16 23:15:22 +0000228}
229
Evan Cheng1606e8e2009-03-13 07:51:59 +0000230namespace {
231 // SectionCPs - Keep track the alignment, constpool entries per Section.
232 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000233 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000234 unsigned Alignment;
235 SmallVector<unsigned, 4> CPEs;
Chris Lattnera87dea42009-07-31 18:48:30 +0000236 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng1606e8e2009-03-13 07:51:59 +0000237 };
238}
239
Chris Lattner3b4fd322005-11-21 08:25:09 +0000240/// EmitConstantPool - Print to the current output stream assembly
241/// representations of the constants in the constant pool MCP. This is
242/// used to print out constants which have been "spilled to memory" by
243/// the code generator.
244///
245void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000246 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000247 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000248
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000249 // Calculate sections for constant pool entries. We collect entries to go into
250 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000251 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000252 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000253 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000254 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000255
256 SectionKind Kind;
257 switch (CPE.getRelocationInfo()) {
258 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000259 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000260 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000261 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000262 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000263 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000264 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000265 case 4: Kind = SectionKind::getMergeableConst4(); break;
266 case 8: Kind = SectionKind::getMergeableConst8(); break;
267 case 16: Kind = SectionKind::getMergeableConst16();break;
268 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000269 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000270 }
271
Chris Lattner83d77fa2009-08-01 23:46:12 +0000272 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000273
Evan Cheng1606e8e2009-03-13 07:51:59 +0000274 // The number of sections are small, just do a linear search from the
275 // last section to the first.
276 bool Found = false;
277 unsigned SecIdx = CPSections.size();
278 while (SecIdx != 0) {
279 if (CPSections[--SecIdx].S == S) {
280 Found = true;
281 break;
282 }
283 }
284 if (!Found) {
285 SecIdx = CPSections.size();
286 CPSections.push_back(SectionCPs(S, Align));
287 }
288
289 if (Align > CPSections[SecIdx].Alignment)
290 CPSections[SecIdx].Alignment = Align;
291 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000292 }
293
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000294 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000295 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000296 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000297 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000298
Evan Cheng1606e8e2009-03-13 07:51:59 +0000299 unsigned Offset = 0;
300 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
301 unsigned CPI = CPSections[i].CPEs[j];
302 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000303
Chris Lattner3029f922006-02-09 04:46:04 +0000304 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000305 unsigned AlignMask = CPE.getAlignment() - 1;
306 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
307 EmitZeros(NewOffset - Offset);
308
309 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000310 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000311
Chris Lattner33adcfb2009-08-22 21:43:10 +0000312 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000313 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000314 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000315 O.PadToColumn(MAI->getCommentColumn());
316 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000317 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000318 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000319 O << '\n';
320 if (CPE.isMachineConstantPoolEntry())
321 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
322 else
323 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000324 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000325 }
326}
327
Nate Begeman37efe672006-04-22 18:53:45 +0000328/// EmitJumpTableInfo - Print assembly representations of the jump tables used
329/// by the current function to the current output stream.
330///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000331void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
332 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000333 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
334 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000335
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000336 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000337
Nate Begeman2f1ae882006-07-27 01:13:04 +0000338 // Pick the directive to use to print the jump table entries, and switch to
339 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000340 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000341
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000342 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000343 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000344 if (F->isWeakForLinker() ||
345 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000346 // In PIC mode, we need to emit the jump table to the same section as the
347 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000348 // We should also do if the section name is NULL or function is declared in
349 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000350 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
351 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000352 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000353 // Otherwise, drop it in the readonly section.
354 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000355 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000356 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000357 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000358 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000359
360 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000361
Nate Begeman37efe672006-04-22 18:53:45 +0000362 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000363 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000364
365 // If this jump table was deleted, ignore it.
366 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000367
368 // For PIC codegen, if possible we want to use the SetDirective to reduce
369 // the number of relocations the assembler will generate for the jump table.
370 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000371 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000372 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000373 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000374 if (EmittedSets.insert(JTBBs[ii]))
375 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000376
Chris Lattner393a8ee2007-01-18 01:12:56 +0000377 // On some targets (e.g. darwin) we want to emit two consequtive labels
378 // before each jump table. The first label is never referenced, but tells
379 // the assembler and linker the extents of the jump table object. The
380 // second label is actually referenced by the code.
Evan Chengb13bafe2009-06-18 20:37:15 +0000381 if (JTInDiffSection) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000382 if (const char *JTLabelPrefix = MAI->getJumpTableSpecialLabelPrefix())
Evan Chengb13bafe2009-06-18 20:37:15 +0000383 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
384 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000385
Chris Lattner33adcfb2009-08-22 21:43:10 +0000386 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000387 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000388
Nate Begeman37efe672006-04-22 18:53:45 +0000389 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000390 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000391 O << '\n';
392 }
393 }
394}
395
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000396void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
397 const MachineBasicBlock *MBB,
398 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000399 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000400
401 // Use JumpTableDirective otherwise honor the entry size from the jump table
402 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000403 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000404 bool HadJTEntryDirective = JTEntryDirective != NULL;
405 if (!HadJTEntryDirective) {
406 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000407 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000408 }
409
410 O << JTEntryDirective << ' ';
411
412 // If we have emitted set directives for the jump table entries, print
413 // them rather than the entries themselves. If we're emitting PIC, then
414 // emit the table entries as differences between two text section labels.
415 // If we're emitting non-PIC code, then emit the entries as direct
416 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000417 if (!isPIC) {
418 printBasicBlockLabel(MBB, false, false, false);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000419 } else if (MAI->getSetDirective()) {
420 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000421 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000422 } else {
Evan Chengfb8075d2008-02-28 00:43:03 +0000423 printBasicBlockLabel(MBB, false, false, false);
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000424 // If the arch uses custom Jump Table directives, don't calc relative to
425 // JT
426 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000427 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000428 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000429 }
430}
431
432
Chris Lattnered138932005-12-13 06:32:10 +0000433/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
434/// special global used by LLVM. If so, emit it and return true, otherwise
435/// do nothing and return false.
436bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000437 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000438 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000439 EmitLLVMUsedList(GV->getInitializer());
440 return true;
441 }
442
Chris Lattner401e10c2009-07-20 06:14:25 +0000443 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000444 if (GV->getSection() == "llvm.metadata" ||
445 GV->hasAvailableExternallyLinkage())
446 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000447
448 if (!GV->hasAppendingLinkage()) return false;
449
450 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000451
Evan Cheng916d07c2007-06-04 20:39:18 +0000452 const TargetData *TD = TM.getTargetData();
453 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000454 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000455 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000456 EmitAlignment(Align, 0);
457 EmitXXStructorList(GV->getInitializer());
458 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000459 }
460
Chris Lattnerf231c072009-03-09 05:52:15 +0000461 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000462 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000463 EmitAlignment(Align, 0);
464 EmitXXStructorList(GV->getInitializer());
465 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000466 }
467
468 return false;
469}
470
Chris Lattner33adcfb2009-08-22 21:43:10 +0000471/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000472/// global in the specified llvm.used list for which emitUsedDirectiveFor
473/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000474void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000475 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000476
Dan Gohmana119de82009-06-14 23:30:43 +0000477 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000478 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
479 if (InitList == 0) return;
480
481 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000482 const GlobalValue *GV =
483 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000484 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000485 O << Directive;
486 EmitConstantValueOnly(InitList->getOperand(i));
487 O << '\n';
488 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000489 }
490}
491
Chris Lattnered138932005-12-13 06:32:10 +0000492/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
493/// function pointers, ignoring the init priority.
494void AsmPrinter::EmitXXStructorList(Constant *List) {
495 // Should be an array of '{ int, void ()* }' structs. The first value is the
496 // init priority, which we ignore.
497 if (!isa<ConstantArray>(List)) return;
498 ConstantArray *InitList = cast<ConstantArray>(List);
499 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
500 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
501 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000502
503 if (CS->getOperand(1)->isNullValue())
504 return; // Found a null terminator, exit printing.
505 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000506 EmitGlobalConstant(CS->getOperand(1));
507 }
508}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000509
Jim Laskeya1a19f82006-10-17 13:41:07 +0000510/// getGlobalLinkName - Returns the asm/link name of of the specified
511/// global variable. Should be overridden by each target asm printer to
512/// generate the appropriate value.
Bill Wendling7d16e852009-04-10 00:12:49 +0000513const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
514 std::string &LinkName) const {
Jim Laskey03742482006-11-20 20:29:06 +0000515 if (isa<Function>(GV)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000516 LinkName += MAI->getFunctionAddrPrefix();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000517 LinkName += Mang->getMangledName(GV);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000518 LinkName += MAI->getFunctionAddrSuffix();
Jim Laskey03742482006-11-20 20:29:06 +0000519 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000520 LinkName += MAI->getGlobalVarAddrPrefix();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000521 LinkName += Mang->getMangledName(GV);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000522 LinkName += MAI->getGlobalVarAddrSuffix();
Jim Laskey03742482006-11-20 20:29:06 +0000523 }
524
Jim Laskey99e41ee2006-10-17 17:17:24 +0000525 return LinkName;
Jim Laskeya1a19f82006-10-17 13:41:07 +0000526}
527
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000528/// EmitExternalGlobal - Emit the external reference to a global variable.
529/// Should be overridden if an indirect reference should be used.
530void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling7d16e852009-04-10 00:12:49 +0000531 std::string GLN;
532 O << getGlobalLinkName(GV, GLN);
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000533}
534
535
536
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000537//===----------------------------------------------------------------------===//
538/// LEB 128 number encoding.
539
540/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
541/// representing an unsigned leb128 value.
542void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattnera64f4632008-11-10 04:35:24 +0000543 char Buffer[20];
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000544 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000545 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000546 Value >>= 7;
547 if (Value) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000548 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000549 if (Value) O << ", ";
550 } while (Value);
551}
552
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000553/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
554/// representing a signed leb128 value.
555void AsmPrinter::PrintSLEB128(int Value) const {
556 int Sign = Value >> (8 * sizeof(Value) - 1);
557 bool IsMore;
Chris Lattnera64f4632008-11-10 04:35:24 +0000558 char Buffer[20];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000559
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000560 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000561 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000562 Value >>= 7;
563 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
564 if (IsMore) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000565 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000566 if (IsMore) O << ", ";
567 } while (IsMore);
568}
569
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000570//===--------------------------------------------------------------------===//
571// Emission and print routines
572//
573
574/// PrintHex - Print a value as a hexidecimal value.
575///
576void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerc6a13462008-11-10 04:30:26 +0000577 char Buffer[20];
Chris Lattnerc6a13462008-11-10 04:30:26 +0000578 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000579}
580
581/// EOL - Print a newline character to asm stream. If a comment is present
582/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000583void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000584 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000585}
Owen Anderson25995092008-07-01 21:16:27 +0000586
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000587void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000588 if (VerboseAsm && !Comment.empty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000589 O.PadToColumn(MAI->getCommentColumn());
590 O << MAI->getCommentString()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000591 << ' '
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000592 << Comment;
593 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000594 O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000595}
596
Owen Anderson25995092008-07-01 21:16:27 +0000597void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000598 if (VerboseAsm && *Comment) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000599 O.PadToColumn(MAI->getCommentColumn());
600 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000601 << ' '
602 << Comment;
603 }
604 O << '\n';
605}
606
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000607static const char *DecodeDWARFEncoding(unsigned Encoding) {
608 switch (Encoding) {
609 case dwarf::DW_EH_PE_absptr:
610 return "absptr";
611 case dwarf::DW_EH_PE_omit:
612 return "omit";
613 case dwarf::DW_EH_PE_pcrel:
614 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000615 case dwarf::DW_EH_PE_udata4:
616 return "udata4";
617 case dwarf::DW_EH_PE_udata8:
618 return "udata8";
619 case dwarf::DW_EH_PE_sdata4:
620 return "sdata4";
621 case dwarf::DW_EH_PE_sdata8:
622 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000623 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
624 return "pcrel udata4";
625 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
626 return "pcrel sdata4";
627 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
628 return "pcrel udata8";
629 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
630 return "pcrel sdata8";
631 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
632 return "indirect pcrel udata4";
633 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
634 return "indirect pcrel sdata4";
635 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
636 return "indirect pcrel udata8";
637 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
638 return "indirect pcrel sdata8";
639 }
640
641 return 0;
642}
643
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000644void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
645 if (VerboseAsm && *Comment) {
646 O.PadToColumn(MAI->getCommentColumn());
647 O << MAI->getCommentString()
648 << ' '
649 << Comment;
650
651 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
652 O << " (" << EncStr << ')';
653 }
654 O << '\n';
655}
656
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000657/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
658/// unsigned leb128 value.
659void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000660 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000661 O << "\t.uleb128\t"
662 << Value;
663 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000664 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000665 PrintULEB128(Value);
666 }
667}
668
669/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
670/// signed leb128 value.
671void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000672 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000673 O << "\t.sleb128\t"
674 << Value;
675 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000676 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000677 PrintSLEB128(Value);
678 }
679}
680
681/// EmitInt8 - Emit a byte directive and value.
682///
683void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000684 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000685 PrintHex(Value & 0xFF);
686}
687
688/// EmitInt16 - Emit a short directive and value.
689///
690void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000691 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000692 PrintHex(Value & 0xFFFF);
693}
694
695/// EmitInt32 - Emit a long directive and value.
696///
697void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000698 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000699 PrintHex(Value);
700}
701
702/// EmitInt64 - Emit a long long directive and value.
703///
704void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000705 if (MAI->getData64bitsDirective()) {
706 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000707 PrintHex(Value);
708 } else {
709 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000710 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000711 EmitInt32(unsigned(Value));
712 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000713 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000714 EmitInt32(unsigned(Value >> 32));
715 }
716 }
717}
718
719/// toOctal - Convert the low order bits of X into an octal digit.
720///
721static inline char toOctal(int X) {
722 return (X&7)+'0';
723}
724
725/// printStringChar - Print a char, escaped if necessary.
726///
David Greene71847812009-07-14 20:18:05 +0000727static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000728 if (C == '"') {
729 O << "\\\"";
730 } else if (C == '\\') {
731 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000732 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000733 O << C;
734 } else {
735 switch(C) {
736 case '\b': O << "\\b"; break;
737 case '\f': O << "\\f"; break;
738 case '\n': O << "\\n"; break;
739 case '\r': O << "\\r"; break;
740 case '\t': O << "\\t"; break;
741 default:
742 O << '\\';
743 O << toOctal(C >> 6);
744 O << toOctal(C >> 3);
745 O << toOctal(C >> 0);
746 break;
747 }
748 }
749}
750
751/// EmitString - Emit a string with quotes and a null terminator.
752/// Special characters are emitted properly.
753/// \literal (Eg. '\t') \endliteral
754void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendlingf34be822009-04-09 23:51:31 +0000755 EmitString(String.c_str(), String.size());
756}
757
758void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000759 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000760 if (AscizDirective)
761 O << AscizDirective;
762 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000763 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000764 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000765 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000766 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000767 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000768 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000769 else
770 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000771}
772
773
Dan Gohman189f80d2007-09-24 20:58:13 +0000774/// EmitFile - Emit a .file directive.
775void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
776 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000777 for (unsigned i = 0, N = Name.size(); i < N; ++i)
778 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000779 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000780}
781
782
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000783//===----------------------------------------------------------------------===//
784
Chris Lattner3a420532007-05-31 18:57:45 +0000785// EmitAlignment - Emit an alignment directive to the specified power of
786// two boundary. For example, if you pass in 3 here, you will get an 8
787// byte alignment. If a global value is specified, and if that global has
788// an explicit alignment requested, it will unconditionally override the
789// alignment request. However, if ForcedAlignBits is specified, this value
790// has final say: the ultimate alignment will be the max of ForcedAlignBits
791// and the alignment computed with NumBits and the global.
792//
793// The algorithm is:
794// Align = NumBits;
795// if (GV && GV->hasalignment) Align = GV->getalignment();
796// Align = std::max(Align, ForcedAlignBits);
797//
798void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000799 unsigned ForcedAlignBits,
800 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000801 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000802 NumBits = Log2_32(GV->getAlignment());
803 NumBits = std::max(NumBits, ForcedAlignBits);
804
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000805 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000806
807 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000808 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000809 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000810
811 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000812}
David Greenea5bb59f2009-08-05 21:00:52 +0000813
Chris Lattner25045bd2005-11-21 07:51:36 +0000814/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000815///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000816void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000817 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000818 if (MAI->getZeroDirective()) {
819 O << MAI->getZeroDirective() << NumZeros;
820 if (MAI->getZeroDirectiveSuffix())
821 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000822 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000823 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000824 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000825 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000826 }
827 }
828}
829
Chris Lattnera80ba712004-08-16 23:15:22 +0000830// Print out the specified constant, without a storage class. Only the
831// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000832void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000833 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohmand19a53b2008-06-30 22:03:41 +0000834 O << '0';
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000835 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000836 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000837 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000838 // This is a constant address for a global variable or function. Use the
839 // name of the variable or function as the address value, possibly
840 // decorating it with GlobalVarAddrPrefix/Suffix or
841 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskey563321a2006-09-06 18:34:40 +0000842 if (isa<Function>(GV)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000843 O << MAI->getFunctionAddrPrefix()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000844 << Mang->getMangledName(GV)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000845 << MAI->getFunctionAddrSuffix();
Jim Laskey563321a2006-09-06 18:34:40 +0000846 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000847 O << MAI->getGlobalVarAddrPrefix()
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000848 << Mang->getMangledName(GV)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000849 << MAI->getGlobalVarAddrSuffix();
Jim Laskey563321a2006-09-06 18:34:40 +0000850 }
Duraid Madina855a5192005-04-02 12:21:51 +0000851 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000852 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000853 unsigned Opcode = CE->getOpcode();
854 switch (Opcode) {
Chris Lattnerc19ee612009-08-01 22:25:12 +0000855 case Instruction::Trunc:
856 case Instruction::ZExt:
857 case Instruction::SExt:
858 case Instruction::FPTrunc:
859 case Instruction::FPExt:
860 case Instruction::UIToFP:
861 case Instruction::SIToFP:
862 case Instruction::FPToUI:
863 case Instruction::FPToSI:
864 llvm_unreachable("FIXME: Don't support this constant cast expr");
Chris Lattnera80ba712004-08-16 23:15:22 +0000865 case Instruction::GetElementPtr: {
866 // generate a symbolic expression for the byte address
867 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000868 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
869 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
870 idxVec.size())) {
Chris Lattner4798bbe2009-02-05 06:55:21 +0000871 // Truncate/sext the offset to the pointer size.
872 if (TD->getPointerSizeInBits() != 64) {
873 int SExtAmount = 64-TD->getPointerSizeInBits();
874 Offset = (Offset << SExtAmount) >> SExtAmount;
875 }
876
Chris Lattner27e19212005-02-14 21:40:26 +0000877 if (Offset)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000878 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000879 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000880 if (Offset > 0)
881 O << ") + " << Offset;
882 else if (Offset < 0)
883 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000884 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000885 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000886 }
887 break;
888 }
Chris Lattnercb0a6812006-12-12 05:14:13 +0000889 case Instruction::BitCast:
890 return EmitConstantValueOnly(CE->getOperand(0));
891
Chris Lattnerddc94012006-12-12 05:18:19 +0000892 case Instruction::IntToPtr: {
893 // Handle casts to pointers by changing them into casts to the appropriate
894 // integer type. This promotes constant folding and simplifies this code.
895 Constant *Op = CE->getOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +0000896 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
897 false/*ZExt*/);
Chris Lattnerddc94012006-12-12 05:18:19 +0000898 return EmitConstantValueOnly(Op);
899 }
900
901
902 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000903 // Support only foldable casts to/from pointers that can be eliminated by
904 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000905 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000906 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000907
Chris Lattnerddc94012006-12-12 05:18:19 +0000908 // We can emit the pointer value into this slot if the slot is an
909 // integer slot greater or equal to the size of the pointer.
Chris Lattnerc19ee612009-08-01 22:25:12 +0000910 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000911 return EmitConstantValueOnly(Op);
Nick Lewyckyd6227382008-08-08 06:34:07 +0000912
913 O << "((";
Chris Lattner25045bd2005-11-21 07:51:36 +0000914 EmitConstantValueOnly(Op);
Chris Lattnerc19ee612009-08-01 22:25:12 +0000915 APInt ptrMask =
916 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattnerfad86b02008-08-17 07:19:36 +0000917
918 SmallString<40> S;
919 ptrMask.toStringUnsigned(S);
Daniel Dunbardddfd342009-08-19 20:07:03 +0000920 O << ") & " << S.str() << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000921 break;
922 }
923 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000924 case Instruction::Sub:
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000925 case Instruction::And:
926 case Instruction::Or:
927 case Instruction::Xor:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000928 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000929 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000930 O << ')';
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000931 switch (Opcode) {
932 case Instruction::Add:
933 O << " + ";
934 break;
935 case Instruction::Sub:
936 O << " - ";
937 break;
938 case Instruction::And:
939 O << " & ";
940 break;
941 case Instruction::Or:
942 O << " | ";
943 break;
944 case Instruction::Xor:
945 O << " ^ ";
946 break;
947 default:
948 break;
949 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000950 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000951 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000952 O << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000953 break;
954 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000955 llvm_unreachable("Unsupported operator!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000956 }
957 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000958 llvm_unreachable("Unknown constant value!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000959 }
960}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000961
Chris Lattner2980cef2005-11-10 18:06:33 +0000962/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000963/// the predicate isString is true.
964///
David Greene71847812009-07-14 20:18:05 +0000965static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000966 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000967 assert(CVA->isString() && "Array is not string compatible!");
968
Dan Gohmand19a53b2008-06-30 22:03:41 +0000969 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000970 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000971 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000972 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000973 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000974 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000975 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000976}
977
Jeff Cohenc884db42006-05-02 01:16:28 +0000978/// EmitString - Emit a zero-byte-terminated string constant.
979///
980void AsmPrinter::EmitString(const ConstantArray *CVA) const {
981 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000982 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000983 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000984 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000985 printAsCString(O, CVA, NumElts-1);
986 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000987 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000988 printAsCString(O, CVA, NumElts);
989 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000990 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000991}
992
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000993void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
994 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000995 if (CVA->isString()) {
996 EmitString(CVA);
997 } else { // Not a string. Print the values in successive locations
998 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000999 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001000 }
1001}
1002
1003void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
1004 const VectorType *PTy = CP->getType();
1005
1006 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
1007 EmitGlobalConstant(CP->getOperand(I));
1008}
1009
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001010void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1011 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001012 // Print the fields in successive locations. Pad to align if needed!
1013 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +00001014 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001015 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1016 uint64_t sizeSoFar = 0;
1017 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1018 const Constant* field = CVS->getOperand(i);
1019
1020 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +00001021 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001022 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1023 - cvsLayout->getElementOffset(i)) - fieldSize;
1024 sizeSoFar += fieldSize + padSize;
1025
1026 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001027 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001028
1029 // Insert padding - this may include padding to increase the size of the
1030 // current field up to the ABI size (if the struct is not packed) as well
1031 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001032 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001033 }
1034 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1035 "Layout of constant struct may be incorrect!");
1036}
1037
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001038void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1039 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001040 // FP Constants are printed as integer constants to avoid losing
1041 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001042 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001043 const TargetData *TD = TM.getTargetData();
Owen Anderson1d0be152009-08-13 21:58:54 +00001044 if (CFP->getType() == Type::getDoubleTy(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001045 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1046 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001047 if (MAI->getData64bitsDirective(AddrSpace)) {
1048 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001049 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001050 O.PadToColumn(MAI->getCommentColumn());
1051 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001052 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001053 O << '\n';
1054 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001055 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001056 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001057 O.PadToColumn(MAI->getCommentColumn());
1058 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001059 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001060 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001061 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001062 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001063 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001064 O.PadToColumn(MAI->getCommentColumn());
1065 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001066 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001067 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001068 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001069 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001070 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001071 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001072 O.PadToColumn(MAI->getCommentColumn());
1073 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001074 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001075 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001076 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001077 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001078 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001079 O.PadToColumn(MAI->getCommentColumn());
1080 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001081 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001082 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001083 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001084 }
1085 return;
Owen Anderson1d0be152009-08-13 21:58:54 +00001086 } else if (CFP->getType() == Type::getFloatTy(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001087 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001088 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001089 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001090 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001091 O.PadToColumn(MAI->getCommentColumn());
1092 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001093 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001094 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001095 return;
Owen Anderson1d0be152009-08-13 21:58:54 +00001096 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001097 // all long double variants are printed as hex
1098 // api needed to prevent premature destruction
1099 APInt api = CFP->getValueAPF().bitcastToAPInt();
1100 const uint64_t *p = api.getRawData();
1101 // Convert to double so we can print the approximate val as a comment.
1102 APFloat DoubleVal = CFP->getValueAPF();
1103 bool ignored;
1104 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1105 &ignored);
1106 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001107 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001108 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001109 O.PadToColumn(MAI->getCommentColumn());
1110 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001111 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001112 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001113 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001114 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001115 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001116 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001117 O.PadToColumn(MAI->getCommentColumn());
1118 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001119 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001120 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001121 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001122 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001123 O.PadToColumn(MAI->getCommentColumn());
1124 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001125 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001126 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001127 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001128 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001129 O.PadToColumn(MAI->getCommentColumn());
1130 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001131 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001132 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001133 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001134 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001135 O.PadToColumn(MAI->getCommentColumn());
1136 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001137 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001138 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001139 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001140 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001141 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001142 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001143 O.PadToColumn(MAI->getCommentColumn());
1144 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001145 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001146 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001147 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001148 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001149 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001150 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001151 O.PadToColumn(MAI->getCommentColumn());
1152 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001153 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001154 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001155 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001156 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001157 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001158 O.PadToColumn(MAI->getCommentColumn());
1159 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001160 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001161 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001162 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001163 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001164 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001165 O.PadToColumn(MAI->getCommentColumn());
1166 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001167 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001168 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001169 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001170 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001171 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001172 O.PadToColumn(MAI->getCommentColumn());
1173 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001174 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001175 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001176 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001177 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001178 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1179 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001180 return;
Owen Anderson1d0be152009-08-13 21:58:54 +00001181 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001182 // all long double variants are printed as hex
1183 // api needed to prevent premature destruction
1184 APInt api = CFP->getValueAPF().bitcastToAPInt();
1185 const uint64_t *p = api.getRawData();
1186 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001187 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001188 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001189 O.PadToColumn(MAI->getCommentColumn());
1190 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001191 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001192 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001193 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001194 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001195 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001196 O.PadToColumn(MAI->getCommentColumn());
1197 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001198 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001199 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001200 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001201 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001202 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001203 O.PadToColumn(MAI->getCommentColumn());
1204 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001205 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001206 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001207 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001208 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001209 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001210 O.PadToColumn(MAI->getCommentColumn());
1211 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001212 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001213 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001214 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001215 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001216 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001217 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001218 O.PadToColumn(MAI->getCommentColumn());
1219 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001220 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001221 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001222 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001223 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001224 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001225 O.PadToColumn(MAI->getCommentColumn());
1226 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001227 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001228 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001229 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001230 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001231 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001232 O.PadToColumn(MAI->getCommentColumn());
1233 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001234 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001235 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001236 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001237 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001238 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001239 O.PadToColumn(MAI->getCommentColumn());
1240 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001241 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001242 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001243 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001244 }
1245 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001246 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001247}
1248
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001249void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1250 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001251 const TargetData *TD = TM.getTargetData();
1252 unsigned BitWidth = CI->getBitWidth();
1253 assert(isPowerOf2_32(BitWidth) &&
1254 "Non-power-of-2-sized integers not handled!");
1255
1256 // We don't expect assemblers to support integer data directives
1257 // for more than 64 bits, so we emit the data in at most 64-bit
1258 // quantities at a time.
1259 const uint64_t *RawData = CI->getValue().getRawData();
1260 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1261 uint64_t Val;
1262 if (TD->isBigEndian())
1263 Val = RawData[e - i - 1];
1264 else
1265 Val = RawData[i];
1266
Chris Lattner33adcfb2009-08-22 21:43:10 +00001267 if (MAI->getData64bitsDirective(AddrSpace))
1268 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001269 else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001270 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001271 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001272 O.PadToColumn(MAI->getCommentColumn());
1273 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001274 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001275 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001276 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001277 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001278 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001279 O.PadToColumn(MAI->getCommentColumn());
1280 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001281 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001282 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001283 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001284 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001285 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001286 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001287 O.PadToColumn(MAI->getCommentColumn());
1288 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001289 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001290 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001291 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001292 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001293 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001294 O.PadToColumn(MAI->getCommentColumn());
1295 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001296 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001297 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001298 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001299 }
1300 }
1301}
1302
Chris Lattner25045bd2005-11-21 07:51:36 +00001303/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001304void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001305 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001306 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001307 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001308
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001309 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001310 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001311 return;
1312 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001313 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001314 return;
1315 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001316 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001317 return;
1318 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001319 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001320 return;
1321 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1322 // Small integers are handled below; large integers are handled here.
1323 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001324 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001325 return;
1326 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001327 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001328 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001329 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001330 }
1331
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001332 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001333 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001334 if (VerboseAsm) {
1335 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1336 SmallString<40> S;
1337 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001338 O.PadToColumn(MAI->getCommentColumn());
1339 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001340 }
Scott Micheleefc8452008-06-03 15:39:51 +00001341 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001342 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001343}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001344
Chris Lattnerfad86b02008-08-17 07:19:36 +00001345void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001346 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001347 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001348}
1349
Chris Lattner3ce9b672006-09-26 23:59:50 +00001350/// PrintSpecial - Print information related to the specified machine instr
1351/// that is independent of the operand, and may be independent of the instr
1352/// itself. This can be useful for portably encoding the comment character
1353/// or other bits of target-specific knowledge into the asmstrings. The
1354/// syntax used is ${:comment}. Targets can override this to add support
1355/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001356void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001357 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001358 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001359 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001360 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001361 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001362 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001363 // Comparing the address of MI isn't sufficient, because machineinstrs may
1364 // be allocated to the same address across functions.
1365 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1366
Owen Andersonbd58edf2009-06-24 22:28:12 +00001367 // If this is a new LastFn instruction, bump the counter.
1368 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001369 ++Counter;
1370 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001371 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001372 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001373 O << Counter;
1374 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001375 std::string msg;
1376 raw_string_ostream Msg(msg);
1377 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001378 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001379 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001380 }
1381}
1382
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001383/// processDebugLoc - Processes the debug information of each machine
1384/// instruction's DebugLoc.
1385void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001386 if (!MAI || !DW)
Chris Lattnerd2503292009-08-05 04:09:18 +00001387 return;
1388
Chris Lattner33adcfb2009-08-22 21:43:10 +00001389 if (MAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001390 if (!DL.isUnknown()) {
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001391 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1392
1393 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1394 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1395 DICompileUnit(CurDLT.CompileUnit)));
1396
1397 PrevDLT = CurDLT;
1398 }
1399 }
1400}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001401
Chris Lattner0264d1a2006-01-27 02:10:10 +00001402/// printInlineAsm - This method formats and prints the specified machine
1403/// instruction that is an inline asm.
1404void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001405 unsigned NumOperands = MI->getNumOperands();
1406
1407 // Count the number of register definitions.
1408 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001409 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001410 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001411 assert(NumDefs != NumOperands-1 && "No asm string?");
1412
Dan Gohmand735b802008-10-03 15:45:36 +00001413 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001414
1415 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001416 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001417
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001418 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1419 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001420 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001421 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1422 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001423 return;
1424 }
1425
Chris Lattner33adcfb2009-08-22 21:43:10 +00001426 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001427
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001428 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001429 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001430
Chris Lattner66099132006-02-01 22:41:11 +00001431 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1432 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001433
Chris Lattner66099132006-02-01 22:41:11 +00001434 while (*LastEmitted) {
1435 switch (*LastEmitted) {
1436 default: {
1437 // Not a special case, emit the string section literally.
1438 const char *LiteralEnd = LastEmitted+1;
1439 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001440 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001441 ++LiteralEnd;
1442 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1443 O.write(LastEmitted, LiteralEnd-LastEmitted);
1444 LastEmitted = LiteralEnd;
1445 break;
1446 }
Chris Lattner1c059972006-05-05 21:47:05 +00001447 case '\n':
1448 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001449 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001450 break;
Chris Lattner66099132006-02-01 22:41:11 +00001451 case '$': {
1452 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001453 bool Done = true;
1454
1455 // Handle escapes.
1456 switch (*LastEmitted) {
1457 default: Done = false; break;
1458 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001459 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1460 O << '$';
1461 ++LastEmitted; // Consume second '$' character.
1462 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001463 case '(': // $( -> same as GCC's { character.
1464 ++LastEmitted; // Consume '(' character.
1465 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001466 llvm_report_error("Nested variants found in inline asm string: '"
1467 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001468 }
1469 CurVariant = 0; // We're in the first variant now.
1470 break;
1471 case '|':
1472 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001473 if (CurVariant == -1)
1474 O << '|'; // this is gcc's behavior for | outside a variant
1475 else
1476 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001477 break;
1478 case ')': // $) -> same as GCC's } char.
1479 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001480 if (CurVariant == -1)
1481 O << '}'; // this is gcc's behavior for } outside a variant
1482 else
1483 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001484 break;
Chris Lattner66099132006-02-01 22:41:11 +00001485 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001486 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001487
1488 bool HasCurlyBraces = false;
1489 if (*LastEmitted == '{') { // ${variable}
1490 ++LastEmitted; // Consume '{' character.
1491 HasCurlyBraces = true;
1492 }
1493
Chris Lattner3e0cc262009-03-10 05:37:13 +00001494 // If we have ${:foo}, then this is not a real operand reference, it is a
1495 // "magic" string reference, just like in .td files. Arrange to call
1496 // PrintSpecial.
1497 if (HasCurlyBraces && *LastEmitted == ':') {
1498 ++LastEmitted;
1499 const char *StrStart = LastEmitted;
1500 const char *StrEnd = strchr(StrStart, '}');
1501 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001502 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1503 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001504 }
1505
1506 std::string Val(StrStart, StrEnd);
1507 PrintSpecial(MI, Val.c_str());
1508 LastEmitted = StrEnd+1;
1509 break;
1510 }
1511
Chris Lattner66099132006-02-01 22:41:11 +00001512 const char *IDStart = LastEmitted;
1513 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001514 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001515 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1516 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001517 llvm_report_error("Bad $ operand number in inline asm string: '"
1518 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001519 }
1520 LastEmitted = IDEnd;
1521
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001522 char Modifier[2] = { 0, 0 };
1523
Chris Lattner66099132006-02-01 22:41:11 +00001524 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001525 // If we have curly braces, check for a modifier character. This
1526 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1527 if (*LastEmitted == ':') {
1528 ++LastEmitted; // Consume ':' character.
1529 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001530 llvm_report_error("Bad ${:} expression in inline asm string: '"
1531 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001532 }
1533
1534 Modifier[0] = *LastEmitted;
1535 ++LastEmitted; // Consume modifier character.
1536 }
1537
Chris Lattner66099132006-02-01 22:41:11 +00001538 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001539 llvm_report_error("Bad ${} expression in inline asm string: '"
1540 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001541 }
1542 ++LastEmitted; // Consume '}' character.
1543 }
1544
1545 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001546 llvm_report_error("Invalid $ operand number in inline asm string: '"
1547 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001548 }
1549
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001550 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001551 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001552 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1553 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001554
1555 bool Error = false;
1556
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001557 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001558 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001559 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001560 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001561 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001562 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001563
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001564 if (OpNo >= MI->getNumOperands()) {
1565 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001566 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001567 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001568 ++OpNo; // Skip over the ID number.
1569
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001570 if (Modifier[0]=='l') // labels are target independent
Chris Lattner8aa797a2007-12-30 23:10:15 +00001571 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengfb8075d2008-02-28 00:43:03 +00001572 false, false, false);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001573 else {
1574 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001575 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001576 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1577 Modifier[0] ? Modifier : 0);
1578 } else {
1579 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1580 Modifier[0] ? Modifier : 0);
1581 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001582 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001583 }
1584 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001585 std::string msg;
1586 raw_string_ostream Msg(msg);
1587 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001588 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001589 MI->print(Msg);
1590 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001591 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001592 }
Chris Lattner66099132006-02-01 22:41:11 +00001593 break;
1594 }
Chris Lattner66099132006-02-01 22:41:11 +00001595 }
1596 }
Chris Lattner33adcfb2009-08-22 21:43:10 +00001597 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattner66099132006-02-01 22:41:11 +00001598}
1599
Evan Chengda47e6e2008-03-15 00:03:38 +00001600/// printImplicitDef - This method prints the specified machine instruction
1601/// that is an implicit def.
1602void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohmana12e9d72009-08-12 18:32:22 +00001603 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001604 O.PadToColumn(MAI->getCommentColumn());
1605 O << MAI->getCommentString() << " implicit-def: "
Evan Chengf1c0ae92009-03-24 00:17:40 +00001606 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Dan Gohmana12e9d72009-08-12 18:32:22 +00001607 }
Evan Chengda47e6e2008-03-15 00:03:38 +00001608}
1609
Jim Laskey1ee29252007-01-26 14:34:52 +00001610/// printLabel - This method prints a local label used by debug and
1611/// exception handling tables.
1612void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001613 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001614}
1615
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001616void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001617 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001618}
1619
Chris Lattner66099132006-02-01 22:41:11 +00001620/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1621/// instruction, using the specified assembler variant. Targets should
1622/// overried this to format as appropriate.
1623bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001624 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001625 // Target doesn't support this yet!
1626 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001627}
Chris Lattnerdd260332006-02-24 20:21:58 +00001628
1629bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1630 unsigned AsmVariant,
1631 const char *ExtraCode) {
1632 // Target doesn't support this yet!
1633 return true;
1634}
Nate Begeman37efe672006-04-22 18:53:45 +00001635
1636/// printBasicBlockLabel - This method prints the label for the specified
1637/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +00001638void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengfb8075d2008-02-28 00:43:03 +00001639 bool printAlign,
Nate Begemancdf38c42006-05-02 05:37:32 +00001640 bool printColon,
1641 bool printComment) const {
Evan Chengfb8075d2008-02-28 00:43:03 +00001642 if (printAlign) {
1643 unsigned Align = MBB->getAlignment();
1644 if (Align)
1645 EmitAlignment(Log2_32(Align));
1646 }
1647
Chris Lattner33adcfb2009-08-22 21:43:10 +00001648 O << MAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng347d39f2007-10-14 05:57:21 +00001649 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +00001650 if (printColon)
1651 O << ':';
David Greeneb71d1b22009-08-10 16:38:07 +00001652 if (printComment) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001653 if (const BasicBlock *BB = MBB->getBasicBlock())
1654 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001655 O.PadToColumn(MAI->getCommentColumn());
1656 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001657 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001658 }
David Greeneb71d1b22009-08-10 16:38:07 +00001659
1660 if (printColon)
1661 EmitComments(*MBB);
1662 }
Nate Begeman37efe672006-04-22 18:53:45 +00001663}
Nate Begeman52a51e382006-08-12 21:29:52 +00001664
Evan Chengcc415862007-11-09 01:32:10 +00001665/// printPICJumpTableSetLabel - This method prints a set label for the
1666/// specified MachineBasicBlock for a jumptable entry.
1667void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1668 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001669 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001670 return;
1671
Chris Lattner33adcfb2009-08-22 21:43:10 +00001672 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001673 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +00001674 printBasicBlockLabel(MBB, false, false, false);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001675 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001676 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001677}
Evan Chengd6594ae2006-09-12 21:00:35 +00001678
Evan Chengcc415862007-11-09 01:32:10 +00001679void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1680 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001681 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001682 return;
1683
Chris Lattner33adcfb2009-08-22 21:43:10 +00001684 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001685 << getFunctionNumber() << '_' << uid << '_' << uid2
1686 << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +00001687 printBasicBlockLabel(MBB, false, false, false);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001688 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001689 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001690}
1691
Evan Chengd6594ae2006-09-12 21:00:35 +00001692/// printDataDirective - This method prints the asm directive for the
1693/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001694void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001695 const TargetData *TD = TM.getTargetData();
1696 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001697 case Type::FloatTyID: case Type::DoubleTyID:
1698 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1699 assert(0 && "Should have already output floating point constant.");
1700 default:
1701 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001702 case Type::IntegerTyID: {
1703 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1704 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001705 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001706 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001707 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001708 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001709 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001710 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001711 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001712 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001713 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001714 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001715 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001716 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001717 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001718 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001719 case Type::PointerTyID:
1720 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001721 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001722 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001723 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001724 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001725 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001726 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001727 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001728 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001729 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001730 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001731 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001732 }
1733}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001734
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001735void AsmPrinter::printVisibility(const std::string& Name,
1736 unsigned Visibility) const {
1737 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001738 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001739 O << Directive << Name << '\n';
1740 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001741 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001742 O << Directive << Name << '\n';
1743 }
1744}
Gordon Henriksenc317a602008-08-17 12:08:44 +00001745
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001746void AsmPrinter::printOffset(int64_t Offset) const {
1747 if (Offset > 0)
1748 O << '+' << Offset;
1749 else if (Offset < 0)
1750 O << Offset;
1751}
1752
Daniel Dunbar575327b2009-08-14 03:43:57 +00001753void AsmPrinter::printMCInst(const MCInst *MI) {
1754 llvm_unreachable("MCInst printing unavailable on this target!");
1755}
1756
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001757GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1758 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001759 return 0;
1760
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001761 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001762 if (GCPI != GCMetadataPrinters.end())
1763 return GCPI->second;
1764
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001765 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001766
1767 for (GCMetadataPrinterRegistry::iterator
1768 I = GCMetadataPrinterRegistry::begin(),
1769 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1770 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001771 GCMetadataPrinter *GMP = I->instantiate();
1772 GMP->S = S;
1773 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1774 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001775 }
1776
Chris Lattner103289e2009-08-23 07:19:13 +00001777 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001778 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001779}
David Greene014700c2009-07-13 20:25:48 +00001780
1781/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001782void AsmPrinter::EmitComments(const MachineInstr &MI) const {
1783 if (!VerboseAsm ||
1784 MI.getDebugLoc().isUnknown())
1785 return;
1786
1787 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene3ac1ab82009-07-16 22:24:20 +00001788
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001789 // Print source line info.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001790 O.PadToColumn(MAI->getCommentColumn());
1791 O << MAI->getCommentString() << " SrcLine ";
Devang Patele4b27562009-08-28 23:24:31 +00001792 if (DLT.CompileUnit) {
1793 std::string Str;
1794 DICompileUnit CU(DLT.CompileUnit);
1795 O << CU.getFilename(Str) << " ";
David Greene3ac1ab82009-07-16 22:24:20 +00001796 }
Chris Lattner5f51cd02009-08-07 23:42:01 +00001797 O << DLT.Line;
1798 if (DLT.Col != 0)
1799 O << ":" << DLT.Col;
David Greene014700c2009-07-13 20:25:48 +00001800}
1801
1802/// EmitComments - Pretty-print comments for instructions
Chris Lattner623dd142009-08-23 00:51:47 +00001803void AsmPrinter::EmitComments(const MCInst &MI) const {
1804 if (!VerboseAsm ||
1805 MI.getDebugLoc().isUnknown())
1806 return;
1807
1808 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene3ac1ab82009-07-16 22:24:20 +00001809
Chris Lattner623dd142009-08-23 00:51:47 +00001810 // Print source line info
1811 O.PadToColumn(MAI->getCommentColumn());
1812 O << MAI->getCommentString() << " SrcLine ";
Devang Patele4b27562009-08-28 23:24:31 +00001813 if (DLT.CompileUnit) {
1814 std::string Str;
1815 DICompileUnit CU(DLT.CompileUnit);
1816 O << CU.getFilename(Str) << " ";
David Greene3ac1ab82009-07-16 22:24:20 +00001817 }
Chris Lattner623dd142009-08-23 00:51:47 +00001818 O << DLT.Line;
1819 if (DLT.Col != 0)
1820 O << ":" << DLT.Col;
David Greene014700c2009-07-13 20:25:48 +00001821}
David Greeneb71d1b22009-08-10 16:38:07 +00001822
David Greenefe37ab32009-08-18 19:22:55 +00001823/// PrintChildLoopComment - Print comments about child loops within
1824/// the loop for this basic block, with nesting.
1825///
1826static void PrintChildLoopComment(formatted_raw_ostream &O,
1827 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001828 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001829 int FunctionNumber) {
1830 // Add child loop information
1831 for(MachineLoop::iterator cl = loop->begin(),
1832 clend = loop->end();
1833 cl != clend;
1834 ++cl) {
1835 MachineBasicBlock *Header = (*cl)->getHeader();
1836 assert(Header && "No header for loop");
1837
1838 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001839 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001840
Chris Lattner33adcfb2009-08-22 21:43:10 +00001841 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001842 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001843 << " Child Loop BB" << FunctionNumber << "_"
1844 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1845
Chris Lattner33adcfb2009-08-22 21:43:10 +00001846 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001847 }
1848}
1849
David Greeneb71d1b22009-08-10 16:38:07 +00001850/// EmitComments - Pretty-print comments for basic blocks
1851void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1852{
David Greenefe37ab32009-08-18 19:22:55 +00001853 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00001854 // Add loop depth information
1855 const MachineLoop *loop = LI->getLoopFor(&MBB);
1856
1857 if (loop) {
1858 // Print a newline after bb# annotation.
1859 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00001860 O.PadToColumn(MAI->getCommentColumn());
1861 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00001862 << '\n';
1863
Chris Lattner33adcfb2009-08-22 21:43:10 +00001864 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001865
1866 MachineBasicBlock *Header = loop->getHeader();
1867 assert(Header && "No header for loop");
1868
1869 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001870 O << MAI->getCommentString() << " Loop Header";
1871 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00001872 }
1873 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001874 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00001875 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1876 }
1877
1878 if (loop->empty()) {
1879 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001880 O.PadToColumn(MAI->getCommentColumn());
1881 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00001882 }
1883
1884 // Add parent loop information
1885 for (const MachineLoop *CurLoop = loop->getParentLoop();
1886 CurLoop;
1887 CurLoop = CurLoop->getParentLoop()) {
1888 MachineBasicBlock *Header = CurLoop->getHeader();
1889 assert(Header && "No header for loop");
1890
1891 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001892 O.PadToColumn(MAI->getCommentColumn());
1893 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001894 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001895 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00001896 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1897 }
1898 }
1899 }
1900}