blob: 3102bdae017c1df0a64e7504b085e9156d5e7477 [file] [log] [blame]
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattner6a8e0f52004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Gordon Henriksend930f912008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Pateldd25a9d2009-01-13 21:44:10 +000023#include "llvm/CodeGen/DwarfWriter.h"
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +000024#include "llvm/Analysis/DebugInfo.h"
Chris Lattner9d0e7622009-07-27 21:28:04 +000025#include "llvm/MC/MCContext.h"
David Greenede9cd442009-07-16 22:24:20 +000026#include "llvm/MC/MCInst.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000027#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCStreamer.h"
Evan Cheng5e5a63c2009-03-25 01:47:28 +000029#include "llvm/Support/CommandLine.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000030#include "llvm/Support/ErrorHandling.h"
David Greenede544782009-07-13 20:25:48 +000031#include "llvm/Support/FormattedStream.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000032#include "llvm/Support/Mangler.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000033#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000034#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000035#include "llvm/Target/TargetLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000036#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Chengc963f6c2008-07-01 23:18:29 +000037#include "llvm/Target/TargetOptions.h"
Evan Cheng0e7b00d2008-03-15 00:03:38 +000038#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng797d56f2007-11-09 01:32:10 +000039#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner17f71652008-08-17 07:19:36 +000040#include "llvm/ADT/SmallString.h"
Owen Anderson93719642008-08-21 00:14:44 +000041#include "llvm/ADT/StringExtras.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000042#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000043using namespace llvm;
44
Evan Cheng5e5a63c2009-03-25 01:47:28 +000045static cl::opt<cl::boolOrDefault>
46AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
47 cl::init(cl::BOU_UNSET));
48
Devang Patel8c78a0b2007-05-03 01:11:54 +000049char AsmPrinter::ID = 0;
David Greenea31f96c2009-07-14 20:18:05 +000050AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbar75c12e12009-07-01 01:48:54 +000051 const TargetAsmInfo *T, bool VDef)
52 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Cheng0e7b00d2008-03-15 00:03:38 +000053 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner9d0e7622009-07-27 21:28:04 +000054
55 OutContext(*new MCContext()),
56 OutStreamer(*createAsmStreamer(OutContext, O)),
57
Chris Lattner8ce12532009-08-03 23:20:21 +000058 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersone3849522009-06-25 16:55:32 +000059 PrevDLT(0, ~0U, ~0U) {
Chris Lattner8ce12532009-08-03 23:20:21 +000060 CurrentSection = 0;
Chris Lattner1fd58882009-06-24 19:09:55 +000061 DW = 0; MMI = 0;
Evan Cheng5e5a63c2009-03-25 01:47:28 +000062 switch (AsmVerbose) {
63 case cl::BOU_UNSET: VerboseAsm = VDef; break;
64 case cl::BOU_TRUE: VerboseAsm = true; break;
65 case cl::BOU_FALSE: VerboseAsm = false; break;
66 }
67}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000068
Gordon Henriksendbe06d32008-08-17 12:08:44 +000069AsmPrinter::~AsmPrinter() {
70 for (gcp_iterator I = GCMetadataPrinters.begin(),
71 E = GCMetadataPrinters.end(); I != E; ++I)
72 delete I->second;
Chris Lattner9d0e7622009-07-27 21:28:04 +000073
74 delete &OutStreamer;
75 delete &OutContext;
Gordon Henriksendbe06d32008-08-17 12:08:44 +000076}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000077
Chris Lattner9170f362009-08-03 19:12:26 +000078TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattner5e693ed2009-07-28 03:13:23 +000079 return TM.getTargetLowering()->getObjFileLowering();
80}
81
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +000082/// SwitchToSection - Switch to the specified section of the executable if we
Chris Lattnerc8565212009-08-03 18:04:42 +000083/// are not already in it! If "NS" is null, then this causes us to exit the
84/// current section and not reenter another one. This is generally used for
85/// asmprinter hacks.
86///
87/// FIXME: Remove support for null sections.
88///
Chris Lattner4d2c0f92009-07-31 18:48:30 +000089void AsmPrinter::SwitchToSection(const MCSection *NS) {
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +000090 // If we're already in this section, we're done.
Chris Lattner8ce12532009-08-03 23:20:21 +000091 if (CurrentSection == NS) return;
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +000092
Chris Lattner8ce12532009-08-03 23:20:21 +000093 CurrentSection = NS;
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +000094
Chris Lattner39fb5462009-08-05 20:49:52 +000095 if (NS == 0) return;
96
97 // If section is named we need to switch into it via special '.section'
98 // directive and also append funky flags. Otherwise - section name is just
99 // some magic assembler directive.
100 if (!NS->isDirective()) {
101 SmallString<32> FlagsStr;
102 getObjFileLowering().getSectionFlagsAsString(NS->getKind(), FlagsStr);
Chris Lattner149465e2009-07-27 05:32:16 +0000103
Chris Lattner39fb5462009-08-05 20:49:52 +0000104 O << TAI->getSwitchToSectionDirective()
105 << CurrentSection->getName() << FlagsStr.c_str() << '\n';
106 } else {
107 O << CurrentSection->getName() << '\n';
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000108 }
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000109}
Chris Lattner8488ba22006-05-09 04:59:56 +0000110
Gordon Henriksen5180e852008-01-07 01:30:38 +0000111void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman04023152009-07-31 23:37:33 +0000112 AU.setPreservesAll();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000113 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksend930f912008-08-17 18:44:35 +0000114 AU.addRequired<GCModuleInfo>();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000115}
116
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000117bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner51d5b432009-07-31 17:42:42 +0000118 // Initialize TargetLoweringObjectFile.
119 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
120 .Initialize(OutContext, TM);
121
Bill Wendlingdde1a8e2009-07-20 21:30:28 +0000122 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
Chris Lattner1177cee2009-07-21 17:30:51 +0000123 TAI->getLinkerPrivateGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000124
Chris Lattnerb8476452009-06-18 23:41:35 +0000125 if (TAI->doesAllowQuotesInName())
126 Mang->setUseQuotes(true);
127
Duncan Sands5a913d62009-01-28 13:14:17 +0000128 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000129 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindolacda011b2008-12-03 11:01:37 +0000130
131 if (TAI->hasSingleParameterDotFile()) {
132 /* Very minimal debug info. It is ignored if we emit actual
133 debug info. If we don't, this at helps the user find where
134 a function came from. */
135 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
136 }
137
Gordon Henriksend930f912008-08-17 18:44:35 +0000138 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
139 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
140 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000141
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000142 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000143 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000144 << M.getModuleInlineAsm()
Dan Gohman6896901e2008-06-30 22:03:41 +0000145 << '\n' << TAI->getCommentString()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000146 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000147
Chris Lattner00753fd2009-08-03 23:10:34 +0000148 SwitchToSection(0); // Reset back to no section to close off sections.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000149
Chris Lattner1fd58882009-06-24 19:09:55 +0000150 if (TAI->doesSupportDebugInformation() ||
151 TAI->doesSupportExceptionHandling()) {
152 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
153 if (MMI)
Devang Patel33f4eb42009-06-19 21:54:26 +0000154 MMI->AnalyzeModule(M);
Chris Lattner1fd58882009-06-24 19:09:55 +0000155 DW = getAnalysisIfAvailable<DwarfWriter>();
156 if (DW)
157 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel33f4eb42009-06-19 21:54:26 +0000158 }
159
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000160 return false;
161}
162
163bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner100865e2009-07-21 18:38:57 +0000164 // Emit global variables.
165 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
166 I != E; ++I)
167 PrintGlobalVariable(I);
168
Chris Lattner70413122009-06-24 18:54:37 +0000169 // Emit final debug information.
170 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
171 DW->EndModule();
172
Chris Lattner2981dc12009-06-24 18:52:01 +0000173 // If the target wants to know about weak references, print them all.
Rafael Espindolad7998d02006-12-18 03:37:18 +0000174 if (TAI->getWeakRefDirective()) {
Chris Lattner2981dc12009-06-24 18:52:01 +0000175 // FIXME: This is not lazy, it would be nice to only print weak references
176 // to stuff that is actually used. Note that doing so would require targets
177 // to notice uses in operands (due to constant exprs etc). This should
178 // happen with the MC stuff eventually.
Chris Lattner00753fd2009-08-03 23:10:34 +0000179 SwitchToSection(0);
Rafael Espindolad7998d02006-12-18 03:37:18 +0000180
Chris Lattner2981dc12009-06-24 18:52:01 +0000181 // Print out module-level global variables here.
182 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
183 I != E; ++I) {
184 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000185 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000186 }
187
Chris Lattner00753fd2009-08-03 23:10:34 +0000188 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner2981dc12009-06-24 18:52:01 +0000189 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000190 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000191 }
Rafael Espindolad7998d02006-12-18 03:37:18 +0000192 }
193
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000194 if (TAI->getSetDirective()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000195 O << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000196 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner2981dc12009-06-24 18:52:01 +0000197 I != E; ++I) {
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000198 std::string Name = Mang->getMangledName(I);
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000199
200 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000201 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000202
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000203 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000204 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000205 else if (I->hasWeakLinkage())
Dan Gohman6896901e2008-06-30 22:03:41 +0000206 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindola6de96a12009-01-15 20:18:42 +0000207 else if (!I->hasLocalLinkage())
Torok Edwinfbcc6632009-07-14 16:55:14 +0000208 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000209
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000210 printVisibility(Name, I->getVisibility());
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000211
Dan Gohman6896901e2008-06-30 22:03:41 +0000212 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000213 }
214 }
215
Duncan Sands5a913d62009-01-28 13:14:17 +0000216 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000217 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
218 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
219 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
220 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000221
Dan Gohmanbcde1722008-05-05 00:28:39 +0000222 // If we don't have any trampolines, then we don't require stack memory
223 // to be executable. Some targets have a directive to declare this.
Chris Lattner2981dc12009-06-24 18:52:01 +0000224 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmanbcde1722008-05-05 00:28:39 +0000225 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
226 if (TAI->getNonexecutableStackDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000227 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmanbcde1722008-05-05 00:28:39 +0000228
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000229 delete Mang; Mang = 0;
Chris Lattner1fd58882009-06-24 19:09:55 +0000230 DW = 0; MMI = 0;
Chris Lattner9d0e7622009-07-27 21:28:04 +0000231
232 OutStreamer.Finish();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000233 return false;
234}
235
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000236std::string
237AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendling067f1d82007-09-18 01:47:22 +0000238 assert(MF && "No machine function?");
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000239 return Mang->getMangledName(MF->getFunction(), ".eh",
240 TAI->is_EHSymbolPrivate());
Bill Wendling067f1d82007-09-18 01:47:22 +0000241}
242
Chris Lattnerbb644e32005-11-21 07:51:36 +0000243void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000244 // What's my mangled name?
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000245 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000246 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000247}
248
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000249namespace {
250 // SectionCPs - Keep track the alignment, constpool entries per Section.
251 struct SectionCPs {
Chris Lattner4d2c0f92009-07-31 18:48:30 +0000252 const MCSection *S;
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000253 unsigned Alignment;
254 SmallVector<unsigned, 4> CPEs;
Chris Lattner4d2c0f92009-07-31 18:48:30 +0000255 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000256 };
257}
258
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000259/// EmitConstantPool - Print to the current output stream assembly
260/// representations of the constants in the constant pool MCP. This is
261/// used to print out constants which have been "spilled to memory" by
262/// the code generator.
263///
264void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000265 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000266 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000267
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000268 // Calculate sections for constant pool entries. We collect entries to go into
269 // the same section together to reduce amount of section switch statements.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000270 SmallVector<SectionCPs, 4> CPSections;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000271 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerb300a4f2009-07-22 00:28:43 +0000272 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000273 unsigned Align = CPE.getAlignment();
Chris Lattnerfb6867c2009-07-26 06:26:55 +0000274
275 SectionKind Kind;
276 switch (CPE.getRelocationInfo()) {
277 default: llvm_unreachable("Unknown section kind");
Chris Lattnerf8d97102009-08-01 23:57:16 +0000278 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000279 case 1:
Chris Lattnerf8d97102009-08-01 23:57:16 +0000280 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000281 break;
Chris Lattnerfb6867c2009-07-26 06:26:55 +0000282 case 0:
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000283 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnerf8d97102009-08-01 23:57:16 +0000284 case 4: Kind = SectionKind::getMergeableConst4(); break;
285 case 8: Kind = SectionKind::getMergeableConst8(); break;
286 case 16: Kind = SectionKind::getMergeableConst16();break;
287 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000288 }
Chris Lattnerfb6867c2009-07-26 06:26:55 +0000289 }
290
Chris Lattner0c402662009-08-01 23:46:12 +0000291 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattnerb300a4f2009-07-22 00:28:43 +0000292
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000293 // The number of sections are small, just do a linear search from the
294 // last section to the first.
295 bool Found = false;
296 unsigned SecIdx = CPSections.size();
297 while (SecIdx != 0) {
298 if (CPSections[--SecIdx].S == S) {
299 Found = true;
300 break;
301 }
302 }
303 if (!Found) {
304 SecIdx = CPSections.size();
305 CPSections.push_back(SectionCPs(S, Align));
306 }
307
308 if (Align > CPSections[SecIdx].Alignment)
309 CPSections[SecIdx].Alignment = Align;
310 CPSections[SecIdx].CPEs.push_back(i);
Evan Chengec1d60b2006-06-29 00:26:09 +0000311 }
312
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000313 // Now print stuff into the calculated sections.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000314 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
315 SwitchToSection(CPSections[i].S);
316 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Chengec1d60b2006-06-29 00:26:09 +0000317
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000318 unsigned Offset = 0;
319 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
320 unsigned CPI = CPSections[i].CPEs[j];
321 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000322
Chris Lattnerf6190822006-02-09 04:46:04 +0000323 // Emit inter-object padding for alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000324 unsigned AlignMask = CPE.getAlignment() - 1;
325 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
326 EmitZeros(NewOffset - Offset);
327
328 const Type *Ty = CPE.getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000329 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000330
331 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
332 << CPI << ":\t\t\t\t\t";
333 if (VerboseAsm) {
334 O << TAI->getCommentString() << ' ';
335 WriteTypeSymbolic(O, CPE.getType(), 0);
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000336 }
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000337 O << '\n';
338 if (CPE.isMachineConstantPoolEntry())
339 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
340 else
341 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000342 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000343 }
344}
345
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000346/// EmitJumpTableInfo - Print assembly representations of the jump tables used
347/// by the current function to the current output stream.
348///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000349void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
350 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000351 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
352 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000353
Jim Laskey70323a82006-12-14 19:17:33 +0000354 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000355
Nate Begeman78756502006-07-27 01:13:04 +0000356 // Pick the directive to use to print the jump table entries, and switch to
357 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000358 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000359
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000360 const Function *F = MF.getFunction();
Evan Chengde9e36a2009-06-18 20:37:15 +0000361 bool JTInDiffSection = false;
Chris Lattner0c402662009-08-01 23:46:12 +0000362 if (F->isWeakForLinker() ||
363 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskey70323a82006-12-14 19:17:33 +0000364 // In PIC mode, we need to emit the jump table to the same section as the
365 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000366 // We should also do if the section name is NULL or function is declared in
367 // discardable section.
Chris Lattner0c402662009-08-01 23:46:12 +0000368 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Nate Begeman78756502006-07-27 01:13:04 +0000369 } else {
Chris Lattner0c402662009-08-01 23:46:12 +0000370 // Otherwise, drop it in the readonly section.
371 const MCSection *ReadOnlySection =
Chris Lattnerf8d97102009-08-01 23:57:16 +0000372 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner0c402662009-08-01 23:46:12 +0000373 SwitchToSection(ReadOnlySection);
Evan Chengde9e36a2009-06-18 20:37:15 +0000374 JTInDiffSection = true;
Nate Begeman78756502006-07-27 01:13:04 +0000375 }
Jim Laskey70323a82006-12-14 19:17:33 +0000376
377 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000378
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000379 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000380 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000381
382 // If this jump table was deleted, ignore it.
383 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000384
385 // For PIC codegen, if possible we want to use the SetDirective to reduce
386 // the number of relocations the assembler will generate for the jump table.
387 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000388 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000389 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000390 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000391 if (EmittedSets.insert(JTBBs[ii]))
392 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000393
Chris Lattner0ee2d462007-01-18 01:12:56 +0000394 // On some targets (e.g. darwin) we want to emit two consequtive labels
395 // before each jump table. The first label is never referenced, but tells
396 // the assembler and linker the extents of the jump table object. The
397 // second label is actually referenced by the code.
Evan Chengde9e36a2009-06-18 20:37:15 +0000398 if (JTInDiffSection) {
399 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
400 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
401 }
Chris Lattner0ee2d462007-01-18 01:12:56 +0000402
Evan Chengcdf36092007-10-14 05:57:21 +0000403 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
404 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000405
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000406 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000407 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000408 O << '\n';
409 }
410 }
411}
412
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000413void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
414 const MachineBasicBlock *MBB,
415 unsigned uid) const {
416 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
417
418 // Use JumpTableDirective otherwise honor the entry size from the jump table
419 // info.
420 const char *JTEntryDirective = TAI->getJumpTableDirective();
421 bool HadJTEntryDirective = JTEntryDirective != NULL;
422 if (!HadJTEntryDirective) {
423 JTEntryDirective = MJTI->getEntrySize() == 4 ?
424 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
425 }
426
427 O << JTEntryDirective << ' ';
428
429 // If we have emitted set directives for the jump table entries, print
430 // them rather than the entries themselves. If we're emitting PIC, then
431 // emit the table entries as differences between two text section labels.
432 // If we're emitting non-PIC code, then emit the entries as direct
433 // references to the target basic blocks.
434 if (IsPic) {
435 if (TAI->getSetDirective()) {
436 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
437 << '_' << uid << "_set_" << MBB->getNumber();
438 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000439 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000440 // If the arch uses custom Jump Table directives, don't calc relative to
441 // JT
442 if (!HadJTEntryDirective)
443 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
444 << getFunctionNumber() << '_' << uid;
445 }
446 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000447 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000448 }
449}
450
451
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000452/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
453/// special global used by LLVM. If so, emit it and return true, otherwise
454/// do nothing and return false.
455bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000456 if (GV->getName() == "llvm.used") {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000457 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
458 EmitLLVMUsedList(GV->getInitializer());
459 return true;
460 }
461
Chris Lattner58f9bb22009-07-20 06:14:25 +0000462 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner184f1be2009-04-13 05:44:34 +0000463 if (GV->getSection() == "llvm.metadata" ||
464 GV->hasAvailableExternallyLinkage())
465 return true;
Jim Laskey313570f2006-03-07 22:00:35 +0000466
467 if (!GV->hasAppendingLinkage()) return false;
468
469 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000470
Evan Cheng53ce7de2007-06-04 20:39:18 +0000471 const TargetData *TD = TM.getTargetData();
472 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner317293b2009-03-09 05:52:15 +0000473 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000474 SwitchToSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner126dab22009-03-09 08:18:48 +0000475 EmitAlignment(Align, 0);
476 EmitXXStructorList(GV->getInitializer());
477 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000478 }
479
Chris Lattner317293b2009-03-09 05:52:15 +0000480 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000481 SwitchToSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner126dab22009-03-09 08:18:48 +0000482 EmitAlignment(Align, 0);
483 EmitXXStructorList(GV->getInitializer());
484 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000485 }
486
487 return false;
488}
489
Chris Lattner66af3902006-09-26 03:38:18 +0000490/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesenabb1e772008-09-09 22:29:13 +0000491/// global in the specified llvm.used list for which emitUsedDirectiveFor
492/// is true, as being used with this directive.
Chris Lattner66af3902006-09-26 03:38:18 +0000493void AsmPrinter::EmitLLVMUsedList(Constant *List) {
494 const char *Directive = TAI->getUsedDirective();
495
Dan Gohman4fe64de2009-06-14 23:30:43 +0000496 // Should be an array of 'i8*'.
Chris Lattner66af3902006-09-26 03:38:18 +0000497 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
498 if (InitList == 0) return;
499
500 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner1fcac9f2009-07-17 22:00:23 +0000501 const GlobalValue *GV =
502 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattnerd25701c2009-07-31 20:52:39 +0000503 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000504 O << Directive;
505 EmitConstantValueOnly(InitList->getOperand(i));
506 O << '\n';
507 }
Chris Lattner66af3902006-09-26 03:38:18 +0000508 }
509}
510
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000511/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
512/// function pointers, ignoring the init priority.
513void AsmPrinter::EmitXXStructorList(Constant *List) {
514 // Should be an array of '{ int, void ()* }' structs. The first value is the
515 // init priority, which we ignore.
516 if (!isa<ConstantArray>(List)) return;
517 ConstantArray *InitList = cast<ConstantArray>(List);
518 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
519 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
520 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000521
522 if (CS->getOperand(1)->isNullValue())
523 return; // Found a null terminator, exit printing.
524 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000525 EmitGlobalConstant(CS->getOperand(1));
526 }
527}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000528
Jim Laskey71262542006-10-17 13:41:07 +0000529/// getGlobalLinkName - Returns the asm/link name of of the specified
530/// global variable. Should be overridden by each target asm printer to
531/// generate the appropriate value.
Bill Wendling992f8462009-04-10 00:12:49 +0000532const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
533 std::string &LinkName) const {
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000534 if (isa<Function>(GV)) {
535 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000536 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000537 LinkName += TAI->getFunctionAddrSuffix();
538 } else {
539 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000540 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000541 LinkName += TAI->getGlobalVarAddrSuffix();
542 }
543
Jim Laskeyd24b9132006-10-17 17:17:24 +0000544 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000545}
546
Jim Laskey18fc0972007-02-21 22:47:38 +0000547/// EmitExternalGlobal - Emit the external reference to a global variable.
548/// Should be overridden if an indirect reference should be used.
549void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling992f8462009-04-10 00:12:49 +0000550 std::string GLN;
551 O << getGlobalLinkName(GV, GLN);
Jim Laskey18fc0972007-02-21 22:47:38 +0000552}
553
554
555
Jim Laskey1c055e82007-01-25 15:12:02 +0000556//===----------------------------------------------------------------------===//
557/// LEB 128 number encoding.
558
559/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
560/// representing an unsigned leb128 value.
561void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner09487cd2008-11-10 04:35:24 +0000562 char Buffer[20];
Jim Laskey1c055e82007-01-25 15:12:02 +0000563 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000564 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000565 Value >>= 7;
566 if (Value) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000567 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000568 if (Value) O << ", ";
569 } while (Value);
570}
571
Jim Laskey1c055e82007-01-25 15:12:02 +0000572/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
573/// representing a signed leb128 value.
574void AsmPrinter::PrintSLEB128(int Value) const {
575 int Sign = Value >> (8 * sizeof(Value) - 1);
576 bool IsMore;
Chris Lattner09487cd2008-11-10 04:35:24 +0000577 char Buffer[20];
Anton Korobeynikovbd890b12008-08-16 12:57:46 +0000578
Jim Laskey1c055e82007-01-25 15:12:02 +0000579 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000580 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000581 Value >>= 7;
582 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
583 if (IsMore) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000584 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000585 if (IsMore) O << ", ";
586 } while (IsMore);
587}
588
Jim Laskey1c055e82007-01-25 15:12:02 +0000589//===--------------------------------------------------------------------===//
590// Emission and print routines
591//
592
593/// PrintHex - Print a value as a hexidecimal value.
594///
595void AsmPrinter::PrintHex(int Value) const {
Chris Lattner5505eed2008-11-10 04:30:26 +0000596 char Buffer[20];
Chris Lattner5505eed2008-11-10 04:30:26 +0000597 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000598}
599
600/// EOL - Print a newline character to asm stream. If a comment is present
601/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000602void AsmPrinter::EOL() const {
Dan Gohman6896901e2008-06-30 22:03:41 +0000603 O << '\n';
Jim Laskey18fc0972007-02-21 22:47:38 +0000604}
Owen Anderson1d952532008-07-01 21:16:27 +0000605
Jim Laskey1c055e82007-01-25 15:12:02 +0000606void AsmPrinter::EOL(const std::string &Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000607 if (VerboseAsm && !Comment.empty()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000608 O << '\t'
Jim Laskey1c055e82007-01-25 15:12:02 +0000609 << TAI->getCommentString()
Dan Gohman6896901e2008-06-30 22:03:41 +0000610 << ' '
Jim Laskey1c055e82007-01-25 15:12:02 +0000611 << Comment;
612 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000613 O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000614}
615
Owen Anderson1d952532008-07-01 21:16:27 +0000616void AsmPrinter::EOL(const char* Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000617 if (VerboseAsm && *Comment) {
Owen Anderson1d952532008-07-01 21:16:27 +0000618 O << '\t'
619 << TAI->getCommentString()
620 << ' '
621 << Comment;
622 }
623 O << '\n';
624}
625
Jim Laskey1c055e82007-01-25 15:12:02 +0000626/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
627/// unsigned leb128 value.
628void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
629 if (TAI->hasLEB128()) {
630 O << "\t.uleb128\t"
631 << Value;
632 } else {
633 O << TAI->getData8bitsDirective();
634 PrintULEB128(Value);
635 }
636}
637
638/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
639/// signed leb128 value.
640void AsmPrinter::EmitSLEB128Bytes(int Value) const {
641 if (TAI->hasLEB128()) {
642 O << "\t.sleb128\t"
643 << Value;
644 } else {
645 O << TAI->getData8bitsDirective();
646 PrintSLEB128(Value);
647 }
648}
649
650/// EmitInt8 - Emit a byte directive and value.
651///
652void AsmPrinter::EmitInt8(int Value) const {
653 O << TAI->getData8bitsDirective();
654 PrintHex(Value & 0xFF);
655}
656
657/// EmitInt16 - Emit a short directive and value.
658///
659void AsmPrinter::EmitInt16(int Value) const {
660 O << TAI->getData16bitsDirective();
661 PrintHex(Value & 0xFFFF);
662}
663
664/// EmitInt32 - Emit a long directive and value.
665///
666void AsmPrinter::EmitInt32(int Value) const {
667 O << TAI->getData32bitsDirective();
668 PrintHex(Value);
669}
670
671/// EmitInt64 - Emit a long long directive and value.
672///
673void AsmPrinter::EmitInt64(uint64_t Value) const {
674 if (TAI->getData64bitsDirective()) {
675 O << TAI->getData64bitsDirective();
676 PrintHex(Value);
677 } else {
678 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000679 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000680 EmitInt32(unsigned(Value));
681 } else {
Dan Gohman6896901e2008-06-30 22:03:41 +0000682 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000683 EmitInt32(unsigned(Value >> 32));
684 }
685 }
686}
687
688/// toOctal - Convert the low order bits of X into an octal digit.
689///
690static inline char toOctal(int X) {
691 return (X&7)+'0';
692}
693
694/// printStringChar - Print a char, escaped if necessary.
695///
David Greenea31f96c2009-07-14 20:18:05 +0000696static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000697 if (C == '"') {
698 O << "\\\"";
699 } else if (C == '\\') {
700 O << "\\\\";
Chris Lattner1f9053a2009-01-22 23:38:45 +0000701 } else if (isprint((unsigned char)C)) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000702 O << C;
703 } else {
704 switch(C) {
705 case '\b': O << "\\b"; break;
706 case '\f': O << "\\f"; break;
707 case '\n': O << "\\n"; break;
708 case '\r': O << "\\r"; break;
709 case '\t': O << "\\t"; break;
710 default:
711 O << '\\';
712 O << toOctal(C >> 6);
713 O << toOctal(C >> 3);
714 O << toOctal(C >> 0);
715 break;
716 }
717 }
718}
719
720/// EmitString - Emit a string with quotes and a null terminator.
721/// Special characters are emitted properly.
722/// \literal (Eg. '\t') \endliteral
723void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling16abfc92009-04-09 23:51:31 +0000724 EmitString(String.c_str(), String.size());
725}
726
727void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000728 const char* AscizDirective = TAI->getAscizDirective();
729 if (AscizDirective)
730 O << AscizDirective;
731 else
732 O << TAI->getAsciiDirective();
Dan Gohman6896901e2008-06-30 22:03:41 +0000733 O << '\"';
Bill Wendling16abfc92009-04-09 23:51:31 +0000734 for (unsigned i = 0; i < Size; ++i)
Chris Lattner69b586e2009-04-08 00:28:38 +0000735 printStringChar(O, String[i]);
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000736 if (AscizDirective)
Dan Gohman6896901e2008-06-30 22:03:41 +0000737 O << '\"';
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000738 else
739 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000740}
741
742
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000743/// EmitFile - Emit a .file directive.
744void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
745 O << "\t.file\t" << Number << " \"";
Chris Lattner69b586e2009-04-08 00:28:38 +0000746 for (unsigned i = 0, N = Name.size(); i < N; ++i)
747 printStringChar(O, Name[i]);
Dan Gohman6896901e2008-06-30 22:03:41 +0000748 O << '\"';
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000749}
750
751
Jim Laskey1c055e82007-01-25 15:12:02 +0000752//===----------------------------------------------------------------------===//
753
Chris Lattner3e3ff302007-05-31 18:57:45 +0000754// EmitAlignment - Emit an alignment directive to the specified power of
755// two boundary. For example, if you pass in 3 here, you will get an 8
756// byte alignment. If a global value is specified, and if that global has
757// an explicit alignment requested, it will unconditionally override the
758// alignment request. However, if ForcedAlignBits is specified, this value
759// has final say: the ultimate alignment will be the max of ForcedAlignBits
760// and the alignment computed with NumBits and the global.
761//
762// The algorithm is:
763// Align = NumBits;
764// if (GV && GV->hasalignment) Align = GV->getalignment();
765// Align = std::max(Align, ForcedAlignBits);
766//
767void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000768 unsigned ForcedAlignBits,
769 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000770 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000771 NumBits = Log2_32(GV->getAlignment());
772 NumBits = std::max(NumBits, ForcedAlignBits);
773
Chris Lattner747960d2005-11-10 18:09:27 +0000774 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000775 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000776 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000777
Chris Lattner8ce12532009-08-03 23:20:21 +0000778 if (CurrentSection && CurrentSection->getKind().isText())
779 if (unsigned FillValue = TAI->getTextAlignFillValue()) {
780 O << ',';
781 PrintHex(FillValue);
782 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000783 O << '\n';
Chris Lattner1d35c162004-08-17 19:14:29 +0000784}
Jim Laskey18fc0972007-02-21 22:47:38 +0000785
David Greene81bcae52009-07-31 21:57:10 +0000786/// PadToColumn - This gets called every time a tab is emitted. If
787/// column padding is turned on, we replace the tab with the
788/// appropriate amount of padding. If not, we replace the tab with a
789/// space, except for the first operand so that initial operands are
790/// always lined up by tabs.
791void AsmPrinter::PadToColumn(unsigned Operand) const {
792 if (TAI->getOperandColumn(Operand) > 0) {
793 O.PadToColumn(TAI->getOperandColumn(Operand), 1);
794 }
795 else {
796 if (Operand == 1) {
797 // Emit the tab after the mnemonic.
798 O << '\t';
799 }
800 else {
801 // Replace the tab with a space.
802 O << ' ';
803 }
804 }
805}
806
Chris Lattnerbb644e32005-11-21 07:51:36 +0000807/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000808///
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000809void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000810 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000811 if (TAI->getZeroDirective()) {
812 O << TAI->getZeroDirective() << NumZeros;
813 if (TAI->getZeroDirectiveSuffix())
814 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000815 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000816 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000817 for (; NumZeros; --NumZeros)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000818 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000819 }
820 }
821}
822
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000823// Print out the specified constant, without a storage class. Only the
824// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000825void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000826 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000827 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000828 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000829 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000830 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000831 // This is a constant address for a global variable or function. Use the
832 // name of the variable or function as the address value, possibly
833 // decorating it with GlobalVarAddrPrefix/Suffix or
834 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000835 if (isa<Function>(GV)) {
836 O << TAI->getFunctionAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000837 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000838 << TAI->getFunctionAddrSuffix();
839 } else {
840 O << TAI->getGlobalVarAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000841 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000842 << TAI->getGlobalVarAddrSuffix();
843 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000844 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000845 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000846 unsigned Opcode = CE->getOpcode();
847 switch (Opcode) {
Chris Lattnerb4b10122009-08-01 22:25:12 +0000848 case Instruction::Trunc:
849 case Instruction::ZExt:
850 case Instruction::SExt:
851 case Instruction::FPTrunc:
852 case Instruction::FPExt:
853 case Instruction::UIToFP:
854 case Instruction::SIToFP:
855 case Instruction::FPToUI:
856 case Instruction::FPToSI:
857 llvm_unreachable("FIXME: Don't support this constant cast expr");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000858 case Instruction::GetElementPtr: {
859 // generate a symbolic expression for the byte address
860 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000861 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
862 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
863 idxVec.size())) {
Chris Lattner7e1d2862009-02-05 06:55:21 +0000864 // Truncate/sext the offset to the pointer size.
865 if (TD->getPointerSizeInBits() != 64) {
866 int SExtAmount = 64-TD->getPointerSizeInBits();
867 Offset = (Offset << SExtAmount) >> SExtAmount;
868 }
869
Chris Lattner145569b2005-02-14 21:40:26 +0000870 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000871 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000872 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000873 if (Offset > 0)
874 O << ") + " << Offset;
875 else if (Offset < 0)
876 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000877 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000878 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000879 }
880 break;
881 }
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000882 case Instruction::BitCast:
883 return EmitConstantValueOnly(CE->getOperand(0));
884
Chris Lattner0c537da2006-12-12 05:18:19 +0000885 case Instruction::IntToPtr: {
886 // Handle casts to pointers by changing them into casts to the appropriate
887 // integer type. This promotes constant folding and simplifies this code.
888 Constant *Op = CE->getOperand(0);
889 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
890 return EmitConstantValueOnly(Op);
891 }
892
893
894 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000895 // Support only foldable casts to/from pointers that can be eliminated by
896 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000897 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000898 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000899
Chris Lattner0c537da2006-12-12 05:18:19 +0000900 // We can emit the pointer value into this slot if the slot is an
901 // integer slot greater or equal to the size of the pointer.
Chris Lattnerb4b10122009-08-01 22:25:12 +0000902 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000903 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000904
905 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000906 EmitConstantValueOnly(Op);
Chris Lattnerb4b10122009-08-01 22:25:12 +0000907 APInt ptrMask =
908 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner17f71652008-08-17 07:19:36 +0000909
910 SmallString<40> S;
911 ptrMask.toStringUnsigned(S);
912 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000913 break;
914 }
915 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000916 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000917 case Instruction::And:
918 case Instruction::Or:
919 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000920 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000921 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000922 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000923 switch (Opcode) {
924 case Instruction::Add:
925 O << " + ";
926 break;
927 case Instruction::Sub:
928 O << " - ";
929 break;
930 case Instruction::And:
931 O << " & ";
932 break;
933 case Instruction::Or:
934 O << " | ";
935 break;
936 case Instruction::Xor:
937 O << " ^ ";
938 break;
939 default:
940 break;
941 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000942 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000943 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000944 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000945 break;
946 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000947 llvm_unreachable("Unsupported operator!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000948 }
949 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000950 llvm_unreachable("Unknown constant value!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000951 }
952}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000953
Chris Lattner55a6d902005-11-10 18:06:33 +0000954/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000955/// the predicate isString is true.
956///
David Greenea31f96c2009-07-14 20:18:05 +0000957static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner55a6d902005-11-10 18:06:33 +0000958 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000959 assert(CVA->isString() && "Array is not string compatible!");
960
Dan Gohman6896901e2008-06-30 22:03:41 +0000961 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000962 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000963 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000964 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000965 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000966 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000967 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000968}
969
Jeff Cohen24a62a92006-05-02 01:16:28 +0000970/// EmitString - Emit a zero-byte-terminated string constant.
971///
972void AsmPrinter::EmitString(const ConstantArray *CVA) const {
973 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000974 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000975 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000976 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000977 printAsCString(O, CVA, NumElts-1);
978 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000979 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000980 printAsCString(O, CVA, NumElts);
981 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000982 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +0000983}
984
Sanjiv Gupta99b1b272009-04-28 16:34:20 +0000985void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
986 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +0000987 if (CVA->isString()) {
988 EmitString(CVA);
989 } else { // Not a string. Print the values in successive locations
990 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Gupta99b1b272009-04-28 16:34:20 +0000991 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +0000992 }
993}
994
995void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
996 const VectorType *PTy = CP->getType();
997
998 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
999 EmitGlobalConstant(CP->getOperand(I));
1000}
1001
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001002void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1003 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001004 // Print the fields in successive locations. Pad to align if needed!
1005 const TargetData *TD = TM.getTargetData();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001006 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001007 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1008 uint64_t sizeSoFar = 0;
1009 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1010 const Constant* field = CVS->getOperand(i);
1011
1012 // Check if padding is needed and insert one or more 0s.
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001013 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001014 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1015 - cvsLayout->getElementOffset(i)) - fieldSize;
1016 sizeSoFar += fieldSize + padSize;
1017
1018 // Now print the actual field value.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001019 EmitGlobalConstant(field, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001020
1021 // Insert padding - this may include padding to increase the size of the
1022 // current field up to the ABI size (if the struct is not packed) as well
1023 // as padding to ensure that the next field starts at the right offset.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001024 EmitZeros(padSize, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001025 }
1026 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1027 "Layout of constant struct may be incorrect!");
1028}
1029
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001030void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1031 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001032 // FP Constants are printed as integer constants to avoid losing
1033 // precision...
1034 const TargetData *TD = TM.getTargetData();
1035 if (CFP->getType() == Type::DoubleTy) {
1036 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1037 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Chenga774a992009-03-24 00:17:40 +00001038 if (TAI->getData64bitsDirective(AddrSpace)) {
1039 O << TAI->getData64bitsDirective(AddrSpace) << i;
1040 if (VerboseAsm)
1041 O << '\t' << TAI->getCommentString() << " double value: " << Val;
1042 O << '\n';
1043 } else if (TD->isBigEndian()) {
1044 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1045 if (VerboseAsm)
1046 O << '\t' << TAI->getCommentString()
1047 << " double most significant word " << Val;
1048 O << '\n';
1049 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1050 if (VerboseAsm)
1051 O << '\t' << TAI->getCommentString()
1052 << " double least significant word " << Val;
1053 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001054 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001055 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1056 if (VerboseAsm)
1057 O << '\t' << TAI->getCommentString()
1058 << " double least significant word " << Val;
1059 O << '\n';
1060 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1061 if (VerboseAsm)
1062 O << '\t' << TAI->getCommentString()
1063 << " double most significant word " << Val;
1064 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001065 }
1066 return;
1067 } else if (CFP->getType() == Type::FloatTy) {
1068 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001069 O << TAI->getData32bitsDirective(AddrSpace)
Evan Chenga774a992009-03-24 00:17:40 +00001070 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1071 if (VerboseAsm)
1072 O << '\t' << TAI->getCommentString() << " float " << Val;
1073 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001074 return;
1075 } else if (CFP->getType() == Type::X86_FP80Ty) {
1076 // all long double variants are printed as hex
1077 // api needed to prevent premature destruction
1078 APInt api = CFP->getValueAPF().bitcastToAPInt();
1079 const uint64_t *p = api.getRawData();
1080 // Convert to double so we can print the approximate val as a comment.
1081 APFloat DoubleVal = CFP->getValueAPF();
1082 bool ignored;
1083 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1084 &ignored);
1085 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001086 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1087 if (VerboseAsm)
1088 O << '\t' << TAI->getCommentString()
1089 << " long double most significant halfword of ~"
1090 << DoubleVal.convertToDouble();
1091 O << '\n';
1092 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1093 if (VerboseAsm)
1094 O << '\t' << TAI->getCommentString() << " long double next halfword";
1095 O << '\n';
1096 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1097 if (VerboseAsm)
1098 O << '\t' << TAI->getCommentString() << " long double next halfword";
1099 O << '\n';
1100 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1101 if (VerboseAsm)
1102 O << '\t' << TAI->getCommentString() << " long double next halfword";
1103 O << '\n';
1104 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1105 if (VerboseAsm)
1106 O << '\t' << TAI->getCommentString()
1107 << " long double least significant halfword";
1108 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001109 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001110 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1111 if (VerboseAsm)
1112 O << '\t' << TAI->getCommentString()
1113 << " long double least significant halfword of ~"
1114 << DoubleVal.convertToDouble();
1115 O << '\n';
1116 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1117 if (VerboseAsm)
1118 O << '\t' << TAI->getCommentString()
1119 << " long double next halfword";
1120 O << '\n';
1121 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1122 if (VerboseAsm)
1123 O << '\t' << TAI->getCommentString()
1124 << " long double next halfword";
1125 O << '\n';
1126 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1127 if (VerboseAsm)
1128 O << '\t' << TAI->getCommentString()
1129 << " long double next halfword";
1130 O << '\n';
1131 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1132 if (VerboseAsm)
1133 O << '\t' << TAI->getCommentString()
1134 << " long double most significant halfword";
1135 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001136 }
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001137 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) -
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001138 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001139 return;
1140 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1141 // all long double variants are printed as hex
1142 // api needed to prevent premature destruction
1143 APInt api = CFP->getValueAPF().bitcastToAPInt();
1144 const uint64_t *p = api.getRawData();
1145 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001146 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1147 if (VerboseAsm)
1148 O << '\t' << TAI->getCommentString()
1149 << " long double most significant word";
1150 O << '\n';
1151 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1152 if (VerboseAsm)
1153 O << '\t' << TAI->getCommentString()
1154 << " long double next word";
1155 O << '\n';
1156 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1157 if (VerboseAsm)
1158 O << '\t' << TAI->getCommentString()
1159 << " long double next word";
1160 O << '\n';
1161 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1162 if (VerboseAsm)
1163 O << '\t' << TAI->getCommentString()
1164 << " long double least significant word";
1165 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001166 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001167 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1168 if (VerboseAsm)
1169 O << '\t' << TAI->getCommentString()
1170 << " long double least significant word";
1171 O << '\n';
1172 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1173 if (VerboseAsm)
1174 O << '\t' << TAI->getCommentString()
1175 << " long double next word";
1176 O << '\n';
1177 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1178 if (VerboseAsm)
1179 O << '\t' << TAI->getCommentString()
1180 << " long double next word";
1181 O << '\n';
1182 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1183 if (VerboseAsm)
1184 O << '\t' << TAI->getCommentString()
1185 << " long double most significant word";
1186 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001187 }
1188 return;
Torok Edwinfbcc6632009-07-14 16:55:14 +00001189 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman014caa42008-12-22 21:14:27 +00001190}
1191
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001192void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1193 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001194 const TargetData *TD = TM.getTargetData();
1195 unsigned BitWidth = CI->getBitWidth();
1196 assert(isPowerOf2_32(BitWidth) &&
1197 "Non-power-of-2-sized integers not handled!");
1198
1199 // We don't expect assemblers to support integer data directives
1200 // for more than 64 bits, so we emit the data in at most 64-bit
1201 // quantities at a time.
1202 const uint64_t *RawData = CI->getValue().getRawData();
1203 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1204 uint64_t Val;
1205 if (TD->isBigEndian())
1206 Val = RawData[e - i - 1];
1207 else
1208 Val = RawData[i];
1209
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001210 if (TAI->getData64bitsDirective(AddrSpace))
1211 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001212 else if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001213 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1214 if (VerboseAsm)
1215 O << '\t' << TAI->getCommentString()
1216 << " Double-word most significant word " << Val;
1217 O << '\n';
1218 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1219 if (VerboseAsm)
1220 O << '\t' << TAI->getCommentString()
1221 << " Double-word least significant word " << Val;
1222 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001223 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001224 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1225 if (VerboseAsm)
1226 O << '\t' << TAI->getCommentString()
1227 << " Double-word least significant word " << Val;
1228 O << '\n';
1229 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1230 if (VerboseAsm)
1231 O << '\t' << TAI->getCommentString()
1232 << " Double-word most significant word " << Val;
1233 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001234 }
1235 }
1236}
1237
Chris Lattnerbb644e32005-11-21 07:51:36 +00001238/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001239void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Anderson20a631f2006-05-03 01:29:57 +00001240 const TargetData *TD = TM.getTargetData();
Dan Gohman014caa42008-12-22 21:14:27 +00001241 const Type *type = CV->getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001242 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001243
Chris Lattner61753bf2004-10-16 18:19:26 +00001244 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001245 EmitZeros(Size, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001246 return;
1247 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001248 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001249 return;
1250 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001251 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001252 return;
1253 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001254 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001255 return;
1256 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1257 // Small integers are handled below; large integers are handled here.
1258 if (Size > 4) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001259 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001260 return;
1261 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001262 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman014caa42008-12-22 21:14:27 +00001263 EmitGlobalConstantVector(CP);
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001264 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001265 }
1266
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001267 printDataDirective(type, AddrSpace);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001268 EmitConstantValueOnly(CV);
Evan Chenga774a992009-03-24 00:17:40 +00001269 if (VerboseAsm) {
1270 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1271 SmallString<40> S;
1272 CI->getValue().toStringUnsigned(S, 16);
1273 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1274 }
Scott Michelc0e9ff62008-06-03 15:39:51 +00001275 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001276 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001277}
Chris Lattner061d9e22006-01-27 02:10:10 +00001278
Chris Lattner17f71652008-08-17 07:19:36 +00001279void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001280 // Target doesn't support this yet!
Torok Edwinfbcc6632009-07-14 16:55:14 +00001281 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001282}
1283
Chris Lattnera32814b2006-09-26 23:59:50 +00001284/// PrintSpecial - Print information related to the specified machine instr
1285/// that is independent of the operand, and may be independent of the instr
1286/// itself. This can be useful for portably encoding the comment character
1287/// or other bits of target-specific knowledge into the asmstrings. The
1288/// syntax used is ${:comment}. Targets can override this to add support
1289/// for their own strange codes.
Chris Lattner1522e242009-03-10 05:37:13 +00001290void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattner30b47082006-09-27 00:06:07 +00001291 if (!strcmp(Code, "private")) {
1292 O << TAI->getPrivateGlobalPrefix();
1293 } else if (!strcmp(Code, "comment")) {
Evan Chenga774a992009-03-24 00:17:40 +00001294 if (VerboseAsm)
1295 O << TAI->getCommentString();
Chris Lattnera32814b2006-09-26 23:59:50 +00001296 } else if (!strcmp(Code, "uid")) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001297 // Comparing the address of MI isn't sufficient, because machineinstrs may
1298 // be allocated to the same address across functions.
1299 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1300
Owen Andersonae471cf2009-06-24 22:28:12 +00001301 // If this is a new LastFn instruction, bump the counter.
1302 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001303 ++Counter;
1304 LastMI = MI;
Owen Andersonae471cf2009-06-24 22:28:12 +00001305 LastFn = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001306 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001307 O << Counter;
1308 } else {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001309 std::string msg;
1310 raw_string_ostream Msg(msg);
1311 Msg << "Unknown special formatter '" << Code
Bill Wendlingf3baad32006-12-07 01:30:32 +00001312 << "' for machine instr: " << *MI;
Torok Edwinccb29cd2009-07-11 13:10:19 +00001313 llvm_report_error(Msg.str());
Chris Lattnera32814b2006-09-26 23:59:50 +00001314 }
1315}
1316
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001317/// processDebugLoc - Processes the debug information of each machine
1318/// instruction's DebugLoc.
1319void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattner3e5b2722009-08-05 04:09:18 +00001320 if (!TAI || !DW)
1321 return;
1322
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001323 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1324 if (!DL.isUnknown()) {
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001325 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1326
1327 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1328 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1329 DICompileUnit(CurDLT.CompileUnit)));
1330
1331 PrevDLT = CurDLT;
1332 }
1333 }
1334}
Chris Lattnera32814b2006-09-26 23:59:50 +00001335
Chris Lattner061d9e22006-01-27 02:10:10 +00001336/// printInlineAsm - This method formats and prints the specified machine
1337/// instruction that is an inline asm.
1338void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001339 unsigned NumOperands = MI->getNumOperands();
1340
1341 // Count the number of register definitions.
1342 unsigned NumDefs = 0;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001343 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001344 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001345 assert(NumDefs != NumOperands-1 && "No asm string?");
1346
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001347 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001348
1349 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001350 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001351
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001352 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1353 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001354 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001355 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001356 return;
1357 }
1358
Jim Laskeya6211dc2006-09-06 18:34:40 +00001359 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001360
Bill Wendlinge21237e2007-01-16 03:42:04 +00001361 // The variant of the current asmprinter.
1362 int AsmPrinterVariant = TAI->getAssemblerDialect();
1363
Chris Lattneraa23fa92006-02-01 22:41:11 +00001364 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1365 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001366
Chris Lattneraa23fa92006-02-01 22:41:11 +00001367 while (*LastEmitted) {
1368 switch (*LastEmitted) {
1369 default: {
1370 // Not a special case, emit the string section literally.
1371 const char *LiteralEnd = LastEmitted+1;
1372 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001373 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001374 ++LiteralEnd;
1375 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1376 O.write(LastEmitted, LiteralEnd-LastEmitted);
1377 LastEmitted = LiteralEnd;
1378 break;
1379 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001380 case '\n':
1381 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001382 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001383 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001384 case '$': {
1385 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001386 bool Done = true;
1387
1388 // Handle escapes.
1389 switch (*LastEmitted) {
1390 default: Done = false; break;
1391 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001392 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1393 O << '$';
1394 ++LastEmitted; // Consume second '$' character.
1395 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001396 case '(': // $( -> same as GCC's { character.
1397 ++LastEmitted; // Consume '(' character.
1398 if (CurVariant != -1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001399 llvm_report_error("Nested variants found in inline asm string: '"
1400 + std::string(AsmStr) + "'");
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001401 }
1402 CurVariant = 0; // We're in the first variant now.
1403 break;
1404 case '|':
1405 ++LastEmitted; // consume '|' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001406 if (CurVariant == -1)
1407 O << '|'; // this is gcc's behavior for | outside a variant
1408 else
1409 ++CurVariant; // We're in the next variant.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001410 break;
1411 case ')': // $) -> same as GCC's } char.
1412 ++LastEmitted; // consume ')' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001413 if (CurVariant == -1)
1414 O << '}'; // this is gcc's behavior for } outside a variant
1415 else
1416 CurVariant = -1;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001417 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001418 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001419 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001420
1421 bool HasCurlyBraces = false;
1422 if (*LastEmitted == '{') { // ${variable}
1423 ++LastEmitted; // Consume '{' character.
1424 HasCurlyBraces = true;
1425 }
1426
Chris Lattner1522e242009-03-10 05:37:13 +00001427 // If we have ${:foo}, then this is not a real operand reference, it is a
1428 // "magic" string reference, just like in .td files. Arrange to call
1429 // PrintSpecial.
1430 if (HasCurlyBraces && *LastEmitted == ':') {
1431 ++LastEmitted;
1432 const char *StrStart = LastEmitted;
1433 const char *StrEnd = strchr(StrStart, '}');
1434 if (StrEnd == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001435 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1436 + std::string(AsmStr) + "'");
Chris Lattner1522e242009-03-10 05:37:13 +00001437 }
1438
1439 std::string Val(StrStart, StrEnd);
1440 PrintSpecial(MI, Val.c_str());
1441 LastEmitted = StrEnd+1;
1442 break;
1443 }
1444
Chris Lattneraa23fa92006-02-01 22:41:11 +00001445 const char *IDStart = LastEmitted;
1446 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001447 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001448 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1449 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001450 llvm_report_error("Bad $ operand number in inline asm string: '"
1451 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001452 }
1453 LastEmitted = IDEnd;
1454
Chris Lattner34f74c12006-02-06 22:17:23 +00001455 char Modifier[2] = { 0, 0 };
1456
Chris Lattneraa23fa92006-02-01 22:41:11 +00001457 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001458 // If we have curly braces, check for a modifier character. This
1459 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1460 if (*LastEmitted == ':') {
1461 ++LastEmitted; // Consume ':' character.
1462 if (*LastEmitted == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001463 llvm_report_error("Bad ${:} expression in inline asm string: '"
1464 + std::string(AsmStr) + "'");
Chris Lattner34f74c12006-02-06 22:17:23 +00001465 }
1466
1467 Modifier[0] = *LastEmitted;
1468 ++LastEmitted; // Consume modifier character.
1469 }
1470
Chris Lattneraa23fa92006-02-01 22:41:11 +00001471 if (*LastEmitted != '}') {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001472 llvm_report_error("Bad ${} expression in inline asm string: '"
1473 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001474 }
1475 ++LastEmitted; // Consume '}' character.
1476 }
1477
1478 if ((unsigned)Val >= NumOperands-1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001479 llvm_report_error("Invalid $ operand number in inline asm string: '"
1480 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001481 }
1482
Chris Lattner571d9642006-02-23 19:21:04 +00001483 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001484 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001485 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1486 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001487
1488 bool Error = false;
1489
Chris Lattner571d9642006-02-23 19:21:04 +00001490 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001491 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001492 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001493 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng2e559232009-03-20 18:03:34 +00001494 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattner5af3fde2006-02-24 19:50:58 +00001495 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001496
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001497 if (OpNo >= MI->getNumOperands()) {
1498 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001499 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001500 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001501 ++OpNo; // Skip over the ID number.
1502
Dale Johannesen4646aa32007-11-05 21:20:28 +00001503 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001504 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001505 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001506 else {
1507 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesenc36660d2008-09-24 01:07:17 +00001508 if ((OpFlags & 7) == 4) {
Dale Johannesen4646aa32007-11-05 21:20:28 +00001509 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1510 Modifier[0] ? Modifier : 0);
1511 } else {
1512 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1513 Modifier[0] ? Modifier : 0);
1514 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001515 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001516 }
1517 if (Error) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001518 std::string msg;
1519 raw_string_ostream Msg(msg);
1520 Msg << "Invalid operand found in inline asm: '"
Bill Wendlingf3baad32006-12-07 01:30:32 +00001521 << AsmStr << "'\n";
Torok Edwinccb29cd2009-07-11 13:10:19 +00001522 MI->print(Msg);
1523 llvm_report_error(Msg.str());
Chris Lattneraa23fa92006-02-01 22:41:11 +00001524 }
Chris Lattner571d9642006-02-23 19:21:04 +00001525 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001526 break;
1527 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001528 }
1529 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001530 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001531}
1532
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001533/// printImplicitDef - This method prints the specified machine instruction
1534/// that is an implicit def.
1535void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Evan Chenga774a992009-03-24 00:17:40 +00001536 if (VerboseAsm)
1537 O << '\t' << TAI->getCommentString() << " implicit-def: "
1538 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001539}
1540
Jim Laskeyf9e54452007-01-26 14:34:52 +00001541/// printLabel - This method prints a local label used by debug and
1542/// exception handling tables.
1543void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001544 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001545}
1546
Evan Chengd6e44ab2008-02-01 09:10:45 +00001547void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001548 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001549}
1550
Evan Chengefd142a2008-02-02 04:07:54 +00001551/// printDeclare - This method prints a local variable declaration used by
1552/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001553/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1554/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001555void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Pateldd25a9d2009-01-13 21:44:10 +00001556 unsigned FI = MI->getOperand(0).getIndex();
Evan Cheng2cb90682008-02-04 23:06:48 +00001557 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel32d17a12009-04-15 00:10:26 +00001558 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Chengefd142a2008-02-02 04:07:54 +00001559}
1560
Chris Lattneraa23fa92006-02-01 22:41:11 +00001561/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1562/// instruction, using the specified assembler variant. Targets should
1563/// overried this to format as appropriate.
1564bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001565 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001566 // Target doesn't support this yet!
1567 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001568}
Chris Lattner1d08c652006-02-24 20:21:58 +00001569
1570bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1571 unsigned AsmVariant,
1572 const char *ExtraCode) {
1573 // Target doesn't support this yet!
1574 return true;
1575}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001576
1577/// printBasicBlockLabel - This method prints the label for the specified
1578/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001579void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001580 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001581 bool printColon,
1582 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001583 if (printAlign) {
1584 unsigned Align = MBB->getAlignment();
1585 if (Align)
1586 EmitAlignment(Log2_32(Align));
1587 }
1588
Dan Gohman6896901e2008-06-30 22:03:41 +00001589 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001590 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001591 if (printColon)
1592 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001593 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001594 O << '\t' << TAI->getCommentString() << ' '
Daniel Dunbar6115b392009-07-26 09:48:23 +00001595 << MBB->getBasicBlock()->getNameStr();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001596}
Nate Begeman984c1a42006-08-12 21:29:52 +00001597
Evan Cheng797d56f2007-11-09 01:32:10 +00001598/// printPICJumpTableSetLabel - This method prints a set label for the
1599/// specified MachineBasicBlock for a jumptable entry.
1600void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1601 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001602 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001603 return;
1604
Jim Laskeya6211dc2006-09-06 18:34:40 +00001605 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001606 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001607 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001608 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1609 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001610}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001611
Evan Cheng797d56f2007-11-09 01:32:10 +00001612void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1613 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001614 if (!TAI->getSetDirective())
1615 return;
1616
1617 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001618 << getFunctionNumber() << '_' << uid << '_' << uid2
1619 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001620 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001621 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1622 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001623}
1624
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001625/// printDataDirective - This method prints the asm directive for the
1626/// specified type.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001627void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001628 const TargetData *TD = TM.getTargetData();
1629 switch (type->getTypeID()) {
Chris Lattnerfe785582009-07-15 04:42:49 +00001630 case Type::FloatTyID: case Type::DoubleTyID:
1631 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1632 assert(0 && "Should have already output floating point constant.");
1633 default:
1634 assert(0 && "Can't handle printing this type of thing");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001635 case Type::IntegerTyID: {
1636 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1637 if (BitWidth <= 8)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001638 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001639 else if (BitWidth <= 16)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001640 O << TAI->getData16bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001641 else if (BitWidth <= 32)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001642 O << TAI->getData32bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001643 else if (BitWidth <= 64) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001644 assert(TAI->getData64bitsDirective(AddrSpace) &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001645 "Target cannot handle 64-bit constant exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001646 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohmanf9b20542008-09-08 16:40:13 +00001647 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001648 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001649 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001650 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001651 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001652 case Type::PointerTyID:
1653 if (TD->getPointerSize() == 8) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001654 assert(TAI->getData64bitsDirective(AddrSpace) &&
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001655 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001656 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001657 } else if (TD->getPointerSize() == 2) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001658 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001659 } else if (TD->getPointerSize() == 1) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001660 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001661 } else {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001662 O << TAI->getData32bitsDirective(AddrSpace);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001663 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001664 break;
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001665 }
1666}
Dale Johannesen915e1542007-02-16 01:54:53 +00001667
Anton Korobeynikoved473292008-08-08 18:25:07 +00001668void AsmPrinter::printVisibility(const std::string& Name,
1669 unsigned Visibility) const {
1670 if (Visibility == GlobalValue::HiddenVisibility) {
1671 if (const char *Directive = TAI->getHiddenDirective())
1672 O << Directive << Name << '\n';
1673 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1674 if (const char *Directive = TAI->getProtectedDirective())
1675 O << Directive << Name << '\n';
1676 }
1677}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001678
Anton Korobeynikovbff4b372008-11-22 16:15:34 +00001679void AsmPrinter::printOffset(int64_t Offset) const {
1680 if (Offset > 0)
1681 O << '+' << Offset;
1682 else if (Offset < 0)
1683 O << Offset;
1684}
1685
Gordon Henriksend930f912008-08-17 18:44:35 +00001686GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1687 if (!S->usesMetadata())
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001688 return 0;
1689
Gordon Henriksend930f912008-08-17 18:44:35 +00001690 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001691 if (GCPI != GCMetadataPrinters.end())
1692 return GCPI->second;
1693
Gordon Henriksend930f912008-08-17 18:44:35 +00001694 const char *Name = S->getName().c_str();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001695
1696 for (GCMetadataPrinterRegistry::iterator
1697 I = GCMetadataPrinterRegistry::begin(),
1698 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1699 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001700 GCMetadataPrinter *GMP = I->instantiate();
1701 GMP->S = S;
1702 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1703 return GMP;
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001704 }
1705
Gordon Henriksend930f912008-08-17 18:44:35 +00001706 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +00001707 llvm_unreachable(0);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001708}
David Greenede544782009-07-13 20:25:48 +00001709
1710/// EmitComments - Pretty-print comments for instructions
1711void AsmPrinter::EmitComments(const MachineInstr &MI) const
1712{
David Greene6e2eda12009-07-22 20:33:26 +00001713 if (VerboseAsm) {
1714 if (!MI.getDebugLoc().isUnknown()) {
1715 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greenede9cd442009-07-16 22:24:20 +00001716
David Greene6e2eda12009-07-22 20:33:26 +00001717 // Print source line info
1718 O.PadToColumn(TAI->getCommentColumn(), 1);
1719 O << TAI->getCommentString() << " SrcLine ";
1720 if (DLT.CompileUnit->hasInitializer()) {
1721 Constant *Name = DLT.CompileUnit->getInitializer();
1722 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1723 if (NameString->isString()) {
1724 O << NameString->getAsString() << " ";
1725 }
1726 }
1727 O << DLT.Line;
1728 if (DLT.Col != 0)
1729 O << ":" << DLT.Col;
1730 }
David Greenede9cd442009-07-16 22:24:20 +00001731 }
David Greenede544782009-07-13 20:25:48 +00001732}
1733
1734/// EmitComments - Pretty-print comments for instructions
1735void AsmPrinter::EmitComments(const MCInst &MI) const
1736{
David Greene6e2eda12009-07-22 20:33:26 +00001737 if (VerboseAsm) {
1738 if (!MI.getDebugLoc().isUnknown()) {
1739 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greenede9cd442009-07-16 22:24:20 +00001740
David Greene6e2eda12009-07-22 20:33:26 +00001741 // Print source line info
1742 O.PadToColumn(TAI->getCommentColumn(), 1);
1743 O << TAI->getCommentString() << " SrcLine ";
1744 if (DLT.CompileUnit->hasInitializer()) {
1745 Constant *Name = DLT.CompileUnit->getInitializer();
1746 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1747 if (NameString->isString()) {
1748 O << NameString->getAsString() << " ";
1749 }
1750 }
1751 O << DLT.Line;
1752 if (DLT.Col != 0)
1753 O << ":" << DLT.Col;
1754 }
David Greenede9cd442009-07-16 22:24:20 +00001755 }
David Greenede544782009-07-13 20:25:48 +00001756}