blob: ec62822e627d0dfde9da81cd00f657b33310ad92 [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}
David Greenefdd25192009-08-05 21:00:52 +0000785
786/// getOperandColumn - Return the output column number (zero-based)
787/// for operand % "operand." If TargetAsmInfo has FirstOperandColumn
788/// == 0 or MaxOperandLength == 0, return 0, meaning column alignment
789/// is disabled.
790unsigned AsmPrinter::getOperandColumn(int operand) const {
791 if (TAI->getFirstOperandColumn() > 0 && TAI->getMaxOperandLength() > 0) {
792 return TAI->getFirstOperandColumn()
793 + (TAI->getMaxOperandLength()+1)*(operand-1);
794 }
795 else {
796 return 0;
797 }
798}
Jim Laskey18fc0972007-02-21 22:47:38 +0000799
David Greene81bcae52009-07-31 21:57:10 +0000800/// PadToColumn - This gets called every time a tab is emitted. If
801/// column padding is turned on, we replace the tab with the
802/// appropriate amount of padding. If not, we replace the tab with a
803/// space, except for the first operand so that initial operands are
804/// always lined up by tabs.
805void AsmPrinter::PadToColumn(unsigned Operand) const {
David Greenefdd25192009-08-05 21:00:52 +0000806 if (getOperandColumn(Operand) > 0) {
807 O.PadToColumn(getOperandColumn(Operand), 1);
David Greene81bcae52009-07-31 21:57:10 +0000808 }
809 else {
810 if (Operand == 1) {
811 // Emit the tab after the mnemonic.
812 O << '\t';
813 }
814 else {
815 // Replace the tab with a space.
816 O << ' ';
817 }
818 }
819}
820
Chris Lattnerbb644e32005-11-21 07:51:36 +0000821/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000822///
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000823void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000824 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000825 if (TAI->getZeroDirective()) {
826 O << TAI->getZeroDirective() << NumZeros;
827 if (TAI->getZeroDirectiveSuffix())
828 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000829 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000830 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000831 for (; NumZeros; --NumZeros)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000832 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000833 }
834 }
835}
836
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000837// Print out the specified constant, without a storage class. Only the
838// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000839void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000840 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000841 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000842 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000843 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000844 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000845 // This is a constant address for a global variable or function. Use the
846 // name of the variable or function as the address value, possibly
847 // decorating it with GlobalVarAddrPrefix/Suffix or
848 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000849 if (isa<Function>(GV)) {
850 O << TAI->getFunctionAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000851 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000852 << TAI->getFunctionAddrSuffix();
853 } else {
854 O << TAI->getGlobalVarAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000855 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000856 << TAI->getGlobalVarAddrSuffix();
857 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000858 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000859 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000860 unsigned Opcode = CE->getOpcode();
861 switch (Opcode) {
Chris Lattnerb4b10122009-08-01 22:25:12 +0000862 case Instruction::Trunc:
863 case Instruction::ZExt:
864 case Instruction::SExt:
865 case Instruction::FPTrunc:
866 case Instruction::FPExt:
867 case Instruction::UIToFP:
868 case Instruction::SIToFP:
869 case Instruction::FPToUI:
870 case Instruction::FPToSI:
871 llvm_unreachable("FIXME: Don't support this constant cast expr");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000872 case Instruction::GetElementPtr: {
873 // generate a symbolic expression for the byte address
874 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000875 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
876 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
877 idxVec.size())) {
Chris Lattner7e1d2862009-02-05 06:55:21 +0000878 // Truncate/sext the offset to the pointer size.
879 if (TD->getPointerSizeInBits() != 64) {
880 int SExtAmount = 64-TD->getPointerSizeInBits();
881 Offset = (Offset << SExtAmount) >> SExtAmount;
882 }
883
Chris Lattner145569b2005-02-14 21:40:26 +0000884 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000885 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000886 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000887 if (Offset > 0)
888 O << ") + " << Offset;
889 else if (Offset < 0)
890 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000891 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000892 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000893 }
894 break;
895 }
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000896 case Instruction::BitCast:
897 return EmitConstantValueOnly(CE->getOperand(0));
898
Chris Lattner0c537da2006-12-12 05:18:19 +0000899 case Instruction::IntToPtr: {
900 // Handle casts to pointers by changing them into casts to the appropriate
901 // integer type. This promotes constant folding and simplifies this code.
902 Constant *Op = CE->getOperand(0);
903 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
904 return EmitConstantValueOnly(Op);
905 }
906
907
908 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000909 // Support only foldable casts to/from pointers that can be eliminated by
910 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000911 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000912 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000913
Chris Lattner0c537da2006-12-12 05:18:19 +0000914 // We can emit the pointer value into this slot if the slot is an
915 // integer slot greater or equal to the size of the pointer.
Chris Lattnerb4b10122009-08-01 22:25:12 +0000916 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000917 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000918
919 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000920 EmitConstantValueOnly(Op);
Chris Lattnerb4b10122009-08-01 22:25:12 +0000921 APInt ptrMask =
922 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner17f71652008-08-17 07:19:36 +0000923
924 SmallString<40> S;
925 ptrMask.toStringUnsigned(S);
926 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000927 break;
928 }
929 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000930 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000931 case Instruction::And:
932 case Instruction::Or:
933 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000934 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000935 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000936 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000937 switch (Opcode) {
938 case Instruction::Add:
939 O << " + ";
940 break;
941 case Instruction::Sub:
942 O << " - ";
943 break;
944 case Instruction::And:
945 O << " & ";
946 break;
947 case Instruction::Or:
948 O << " | ";
949 break;
950 case Instruction::Xor:
951 O << " ^ ";
952 break;
953 default:
954 break;
955 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000956 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000957 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000958 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000959 break;
960 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000961 llvm_unreachable("Unsupported operator!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000962 }
963 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000964 llvm_unreachable("Unknown constant value!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000965 }
966}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000967
Chris Lattner55a6d902005-11-10 18:06:33 +0000968/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000969/// the predicate isString is true.
970///
David Greenea31f96c2009-07-14 20:18:05 +0000971static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner55a6d902005-11-10 18:06:33 +0000972 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000973 assert(CVA->isString() && "Array is not string compatible!");
974
Dan Gohman6896901e2008-06-30 22:03:41 +0000975 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000976 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000977 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000978 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000979 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000980 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000981 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000982}
983
Jeff Cohen24a62a92006-05-02 01:16:28 +0000984/// EmitString - Emit a zero-byte-terminated string constant.
985///
986void AsmPrinter::EmitString(const ConstantArray *CVA) const {
987 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000988 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000989 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000990 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000991 printAsCString(O, CVA, NumElts-1);
992 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000993 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000994 printAsCString(O, CVA, NumElts);
995 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000996 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +0000997}
998
Sanjiv Gupta99b1b272009-04-28 16:34:20 +0000999void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
1000 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001001 if (CVA->isString()) {
1002 EmitString(CVA);
1003 } else { // Not a string. Print the values in successive locations
1004 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001005 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001006 }
1007}
1008
1009void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
1010 const VectorType *PTy = CP->getType();
1011
1012 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
1013 EmitGlobalConstant(CP->getOperand(I));
1014}
1015
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001016void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1017 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001018 // Print the fields in successive locations. Pad to align if needed!
1019 const TargetData *TD = TM.getTargetData();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001020 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001021 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1022 uint64_t sizeSoFar = 0;
1023 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1024 const Constant* field = CVS->getOperand(i);
1025
1026 // Check if padding is needed and insert one or more 0s.
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001027 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001028 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1029 - cvsLayout->getElementOffset(i)) - fieldSize;
1030 sizeSoFar += fieldSize + padSize;
1031
1032 // Now print the actual field value.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001033 EmitGlobalConstant(field, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001034
1035 // Insert padding - this may include padding to increase the size of the
1036 // current field up to the ABI size (if the struct is not packed) as well
1037 // as padding to ensure that the next field starts at the right offset.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001038 EmitZeros(padSize, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001039 }
1040 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1041 "Layout of constant struct may be incorrect!");
1042}
1043
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001044void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1045 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001046 // FP Constants are printed as integer constants to avoid losing
1047 // precision...
1048 const TargetData *TD = TM.getTargetData();
1049 if (CFP->getType() == Type::DoubleTy) {
1050 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1051 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Chenga774a992009-03-24 00:17:40 +00001052 if (TAI->getData64bitsDirective(AddrSpace)) {
1053 O << TAI->getData64bitsDirective(AddrSpace) << i;
1054 if (VerboseAsm)
1055 O << '\t' << TAI->getCommentString() << " double value: " << Val;
1056 O << '\n';
1057 } else if (TD->isBigEndian()) {
1058 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1059 if (VerboseAsm)
1060 O << '\t' << TAI->getCommentString()
1061 << " double most significant word " << Val;
1062 O << '\n';
1063 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1064 if (VerboseAsm)
1065 O << '\t' << TAI->getCommentString()
1066 << " double least significant word " << Val;
1067 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001068 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001069 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1070 if (VerboseAsm)
1071 O << '\t' << TAI->getCommentString()
1072 << " double least significant word " << Val;
1073 O << '\n';
1074 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1075 if (VerboseAsm)
1076 O << '\t' << TAI->getCommentString()
1077 << " double most significant word " << Val;
1078 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001079 }
1080 return;
1081 } else if (CFP->getType() == Type::FloatTy) {
1082 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001083 O << TAI->getData32bitsDirective(AddrSpace)
Evan Chenga774a992009-03-24 00:17:40 +00001084 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1085 if (VerboseAsm)
1086 O << '\t' << TAI->getCommentString() << " float " << Val;
1087 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001088 return;
1089 } else if (CFP->getType() == Type::X86_FP80Ty) {
1090 // all long double variants are printed as hex
1091 // api needed to prevent premature destruction
1092 APInt api = CFP->getValueAPF().bitcastToAPInt();
1093 const uint64_t *p = api.getRawData();
1094 // Convert to double so we can print the approximate val as a comment.
1095 APFloat DoubleVal = CFP->getValueAPF();
1096 bool ignored;
1097 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1098 &ignored);
1099 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001100 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1101 if (VerboseAsm)
1102 O << '\t' << TAI->getCommentString()
1103 << " long double most significant halfword of ~"
1104 << DoubleVal.convertToDouble();
1105 O << '\n';
1106 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1107 if (VerboseAsm)
1108 O << '\t' << TAI->getCommentString() << " long double next halfword";
1109 O << '\n';
1110 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1111 if (VerboseAsm)
1112 O << '\t' << TAI->getCommentString() << " long double next halfword";
1113 O << '\n';
1114 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1115 if (VerboseAsm)
1116 O << '\t' << TAI->getCommentString() << " long double next halfword";
1117 O << '\n';
1118 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1119 if (VerboseAsm)
1120 O << '\t' << TAI->getCommentString()
1121 << " long double least significant halfword";
1122 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001123 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001124 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1125 if (VerboseAsm)
1126 O << '\t' << TAI->getCommentString()
1127 << " long double least significant halfword of ~"
1128 << DoubleVal.convertToDouble();
1129 O << '\n';
1130 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1131 if (VerboseAsm)
1132 O << '\t' << TAI->getCommentString()
1133 << " long double next halfword";
1134 O << '\n';
1135 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1136 if (VerboseAsm)
1137 O << '\t' << TAI->getCommentString()
1138 << " long double next halfword";
1139 O << '\n';
1140 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1141 if (VerboseAsm)
1142 O << '\t' << TAI->getCommentString()
1143 << " long double next halfword";
1144 O << '\n';
1145 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1146 if (VerboseAsm)
1147 O << '\t' << TAI->getCommentString()
1148 << " long double most significant halfword";
1149 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001150 }
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001151 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) -
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001152 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001153 return;
1154 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1155 // all long double variants are printed as hex
1156 // api needed to prevent premature destruction
1157 APInt api = CFP->getValueAPF().bitcastToAPInt();
1158 const uint64_t *p = api.getRawData();
1159 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001160 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1161 if (VerboseAsm)
1162 O << '\t' << TAI->getCommentString()
1163 << " long double most significant word";
1164 O << '\n';
1165 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1166 if (VerboseAsm)
1167 O << '\t' << TAI->getCommentString()
1168 << " long double next word";
1169 O << '\n';
1170 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1171 if (VerboseAsm)
1172 O << '\t' << TAI->getCommentString()
1173 << " long double next word";
1174 O << '\n';
1175 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1176 if (VerboseAsm)
1177 O << '\t' << TAI->getCommentString()
1178 << " long double least significant word";
1179 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001180 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001181 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1182 if (VerboseAsm)
1183 O << '\t' << TAI->getCommentString()
1184 << " long double least significant word";
1185 O << '\n';
1186 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1187 if (VerboseAsm)
1188 O << '\t' << TAI->getCommentString()
1189 << " long double next word";
1190 O << '\n';
1191 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1192 if (VerboseAsm)
1193 O << '\t' << TAI->getCommentString()
1194 << " long double next word";
1195 O << '\n';
1196 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1197 if (VerboseAsm)
1198 O << '\t' << TAI->getCommentString()
1199 << " long double most significant word";
1200 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001201 }
1202 return;
Torok Edwinfbcc6632009-07-14 16:55:14 +00001203 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman014caa42008-12-22 21:14:27 +00001204}
1205
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001206void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1207 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001208 const TargetData *TD = TM.getTargetData();
1209 unsigned BitWidth = CI->getBitWidth();
1210 assert(isPowerOf2_32(BitWidth) &&
1211 "Non-power-of-2-sized integers not handled!");
1212
1213 // We don't expect assemblers to support integer data directives
1214 // for more than 64 bits, so we emit the data in at most 64-bit
1215 // quantities at a time.
1216 const uint64_t *RawData = CI->getValue().getRawData();
1217 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1218 uint64_t Val;
1219 if (TD->isBigEndian())
1220 Val = RawData[e - i - 1];
1221 else
1222 Val = RawData[i];
1223
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001224 if (TAI->getData64bitsDirective(AddrSpace))
1225 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001226 else if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001227 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1228 if (VerboseAsm)
1229 O << '\t' << TAI->getCommentString()
1230 << " Double-word most significant word " << Val;
1231 O << '\n';
1232 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1233 if (VerboseAsm)
1234 O << '\t' << TAI->getCommentString()
1235 << " Double-word least significant word " << Val;
1236 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001237 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001238 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1239 if (VerboseAsm)
1240 O << '\t' << TAI->getCommentString()
1241 << " Double-word least significant word " << Val;
1242 O << '\n';
1243 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1244 if (VerboseAsm)
1245 O << '\t' << TAI->getCommentString()
1246 << " Double-word most significant word " << Val;
1247 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001248 }
1249 }
1250}
1251
Chris Lattnerbb644e32005-11-21 07:51:36 +00001252/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001253void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Anderson20a631f2006-05-03 01:29:57 +00001254 const TargetData *TD = TM.getTargetData();
Dan Gohman014caa42008-12-22 21:14:27 +00001255 const Type *type = CV->getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001256 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001257
Chris Lattner61753bf2004-10-16 18:19:26 +00001258 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001259 EmitZeros(Size, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001260 return;
1261 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001262 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001263 return;
1264 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001265 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001266 return;
1267 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001268 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001269 return;
1270 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1271 // Small integers are handled below; large integers are handled here.
1272 if (Size > 4) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001273 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001274 return;
1275 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001276 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman014caa42008-12-22 21:14:27 +00001277 EmitGlobalConstantVector(CP);
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001278 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001279 }
1280
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001281 printDataDirective(type, AddrSpace);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001282 EmitConstantValueOnly(CV);
Evan Chenga774a992009-03-24 00:17:40 +00001283 if (VerboseAsm) {
1284 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1285 SmallString<40> S;
1286 CI->getValue().toStringUnsigned(S, 16);
1287 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1288 }
Scott Michelc0e9ff62008-06-03 15:39:51 +00001289 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001290 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001291}
Chris Lattner061d9e22006-01-27 02:10:10 +00001292
Chris Lattner17f71652008-08-17 07:19:36 +00001293void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001294 // Target doesn't support this yet!
Torok Edwinfbcc6632009-07-14 16:55:14 +00001295 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001296}
1297
Chris Lattnera32814b2006-09-26 23:59:50 +00001298/// PrintSpecial - Print information related to the specified machine instr
1299/// that is independent of the operand, and may be independent of the instr
1300/// itself. This can be useful for portably encoding the comment character
1301/// or other bits of target-specific knowledge into the asmstrings. The
1302/// syntax used is ${:comment}. Targets can override this to add support
1303/// for their own strange codes.
Chris Lattner1522e242009-03-10 05:37:13 +00001304void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattner30b47082006-09-27 00:06:07 +00001305 if (!strcmp(Code, "private")) {
1306 O << TAI->getPrivateGlobalPrefix();
1307 } else if (!strcmp(Code, "comment")) {
Evan Chenga774a992009-03-24 00:17:40 +00001308 if (VerboseAsm)
1309 O << TAI->getCommentString();
Chris Lattnera32814b2006-09-26 23:59:50 +00001310 } else if (!strcmp(Code, "uid")) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001311 // Comparing the address of MI isn't sufficient, because machineinstrs may
1312 // be allocated to the same address across functions.
1313 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1314
Owen Andersonae471cf2009-06-24 22:28:12 +00001315 // If this is a new LastFn instruction, bump the counter.
1316 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001317 ++Counter;
1318 LastMI = MI;
Owen Andersonae471cf2009-06-24 22:28:12 +00001319 LastFn = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001320 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001321 O << Counter;
1322 } else {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001323 std::string msg;
1324 raw_string_ostream Msg(msg);
1325 Msg << "Unknown special formatter '" << Code
Bill Wendlingf3baad32006-12-07 01:30:32 +00001326 << "' for machine instr: " << *MI;
Torok Edwinccb29cd2009-07-11 13:10:19 +00001327 llvm_report_error(Msg.str());
Chris Lattnera32814b2006-09-26 23:59:50 +00001328 }
1329}
1330
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001331/// processDebugLoc - Processes the debug information of each machine
1332/// instruction's DebugLoc.
1333void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattner3e5b2722009-08-05 04:09:18 +00001334 if (!TAI || !DW)
1335 return;
1336
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001337 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1338 if (!DL.isUnknown()) {
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001339 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1340
1341 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1342 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1343 DICompileUnit(CurDLT.CompileUnit)));
1344
1345 PrevDLT = CurDLT;
1346 }
1347 }
1348}
Chris Lattnera32814b2006-09-26 23:59:50 +00001349
Chris Lattner061d9e22006-01-27 02:10:10 +00001350/// printInlineAsm - This method formats and prints the specified machine
1351/// instruction that is an inline asm.
1352void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001353 unsigned NumOperands = MI->getNumOperands();
1354
1355 // Count the number of register definitions.
1356 unsigned NumDefs = 0;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001357 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001358 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001359 assert(NumDefs != NumOperands-1 && "No asm string?");
1360
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001361 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001362
1363 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001364 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001365
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001366 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1367 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001368 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001369 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001370 return;
1371 }
1372
Jim Laskeya6211dc2006-09-06 18:34:40 +00001373 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001374
Bill Wendlinge21237e2007-01-16 03:42:04 +00001375 // The variant of the current asmprinter.
1376 int AsmPrinterVariant = TAI->getAssemblerDialect();
1377
Chris Lattneraa23fa92006-02-01 22:41:11 +00001378 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1379 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001380
Chris Lattneraa23fa92006-02-01 22:41:11 +00001381 while (*LastEmitted) {
1382 switch (*LastEmitted) {
1383 default: {
1384 // Not a special case, emit the string section literally.
1385 const char *LiteralEnd = LastEmitted+1;
1386 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001387 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001388 ++LiteralEnd;
1389 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1390 O.write(LastEmitted, LiteralEnd-LastEmitted);
1391 LastEmitted = LiteralEnd;
1392 break;
1393 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001394 case '\n':
1395 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001396 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001397 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001398 case '$': {
1399 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001400 bool Done = true;
1401
1402 // Handle escapes.
1403 switch (*LastEmitted) {
1404 default: Done = false; break;
1405 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001406 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1407 O << '$';
1408 ++LastEmitted; // Consume second '$' character.
1409 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001410 case '(': // $( -> same as GCC's { character.
1411 ++LastEmitted; // Consume '(' character.
1412 if (CurVariant != -1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001413 llvm_report_error("Nested variants found in inline asm string: '"
1414 + std::string(AsmStr) + "'");
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001415 }
1416 CurVariant = 0; // We're in the first variant now.
1417 break;
1418 case '|':
1419 ++LastEmitted; // consume '|' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001420 if (CurVariant == -1)
1421 O << '|'; // this is gcc's behavior for | outside a variant
1422 else
1423 ++CurVariant; // We're in the next variant.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001424 break;
1425 case ')': // $) -> same as GCC's } char.
1426 ++LastEmitted; // consume ')' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001427 if (CurVariant == -1)
1428 O << '}'; // this is gcc's behavior for } outside a variant
1429 else
1430 CurVariant = -1;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001431 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001432 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001433 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001434
1435 bool HasCurlyBraces = false;
1436 if (*LastEmitted == '{') { // ${variable}
1437 ++LastEmitted; // Consume '{' character.
1438 HasCurlyBraces = true;
1439 }
1440
Chris Lattner1522e242009-03-10 05:37:13 +00001441 // If we have ${:foo}, then this is not a real operand reference, it is a
1442 // "magic" string reference, just like in .td files. Arrange to call
1443 // PrintSpecial.
1444 if (HasCurlyBraces && *LastEmitted == ':') {
1445 ++LastEmitted;
1446 const char *StrStart = LastEmitted;
1447 const char *StrEnd = strchr(StrStart, '}');
1448 if (StrEnd == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001449 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1450 + std::string(AsmStr) + "'");
Chris Lattner1522e242009-03-10 05:37:13 +00001451 }
1452
1453 std::string Val(StrStart, StrEnd);
1454 PrintSpecial(MI, Val.c_str());
1455 LastEmitted = StrEnd+1;
1456 break;
1457 }
1458
Chris Lattneraa23fa92006-02-01 22:41:11 +00001459 const char *IDStart = LastEmitted;
1460 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001461 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001462 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1463 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001464 llvm_report_error("Bad $ operand number in inline asm string: '"
1465 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001466 }
1467 LastEmitted = IDEnd;
1468
Chris Lattner34f74c12006-02-06 22:17:23 +00001469 char Modifier[2] = { 0, 0 };
1470
Chris Lattneraa23fa92006-02-01 22:41:11 +00001471 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001472 // If we have curly braces, check for a modifier character. This
1473 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1474 if (*LastEmitted == ':') {
1475 ++LastEmitted; // Consume ':' character.
1476 if (*LastEmitted == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001477 llvm_report_error("Bad ${:} expression in inline asm string: '"
1478 + std::string(AsmStr) + "'");
Chris Lattner34f74c12006-02-06 22:17:23 +00001479 }
1480
1481 Modifier[0] = *LastEmitted;
1482 ++LastEmitted; // Consume modifier character.
1483 }
1484
Chris Lattneraa23fa92006-02-01 22:41:11 +00001485 if (*LastEmitted != '}') {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001486 llvm_report_error("Bad ${} expression in inline asm string: '"
1487 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001488 }
1489 ++LastEmitted; // Consume '}' character.
1490 }
1491
1492 if ((unsigned)Val >= NumOperands-1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001493 llvm_report_error("Invalid $ operand number in inline asm string: '"
1494 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001495 }
1496
Chris Lattner571d9642006-02-23 19:21:04 +00001497 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001498 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001499 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1500 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001501
1502 bool Error = false;
1503
Chris Lattner571d9642006-02-23 19:21:04 +00001504 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001505 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001506 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001507 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng2e559232009-03-20 18:03:34 +00001508 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattner5af3fde2006-02-24 19:50:58 +00001509 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001510
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001511 if (OpNo >= MI->getNumOperands()) {
1512 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001513 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001514 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001515 ++OpNo; // Skip over the ID number.
1516
Dale Johannesen4646aa32007-11-05 21:20:28 +00001517 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001518 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001519 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001520 else {
1521 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesenc36660d2008-09-24 01:07:17 +00001522 if ((OpFlags & 7) == 4) {
Dale Johannesen4646aa32007-11-05 21:20:28 +00001523 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1524 Modifier[0] ? Modifier : 0);
1525 } else {
1526 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1527 Modifier[0] ? Modifier : 0);
1528 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001529 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001530 }
1531 if (Error) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001532 std::string msg;
1533 raw_string_ostream Msg(msg);
1534 Msg << "Invalid operand found in inline asm: '"
Bill Wendlingf3baad32006-12-07 01:30:32 +00001535 << AsmStr << "'\n";
Torok Edwinccb29cd2009-07-11 13:10:19 +00001536 MI->print(Msg);
1537 llvm_report_error(Msg.str());
Chris Lattneraa23fa92006-02-01 22:41:11 +00001538 }
Chris Lattner571d9642006-02-23 19:21:04 +00001539 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001540 break;
1541 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001542 }
1543 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001544 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001545}
1546
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001547/// printImplicitDef - This method prints the specified machine instruction
1548/// that is an implicit def.
1549void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Evan Chenga774a992009-03-24 00:17:40 +00001550 if (VerboseAsm)
1551 O << '\t' << TAI->getCommentString() << " implicit-def: "
1552 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001553}
1554
Jim Laskeyf9e54452007-01-26 14:34:52 +00001555/// printLabel - This method prints a local label used by debug and
1556/// exception handling tables.
1557void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001558 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001559}
1560
Evan Chengd6e44ab2008-02-01 09:10:45 +00001561void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001562 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001563}
1564
Evan Chengefd142a2008-02-02 04:07:54 +00001565/// printDeclare - This method prints a local variable declaration used by
1566/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001567/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1568/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001569void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Pateldd25a9d2009-01-13 21:44:10 +00001570 unsigned FI = MI->getOperand(0).getIndex();
Evan Cheng2cb90682008-02-04 23:06:48 +00001571 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel32d17a12009-04-15 00:10:26 +00001572 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Chengefd142a2008-02-02 04:07:54 +00001573}
1574
Chris Lattneraa23fa92006-02-01 22:41:11 +00001575/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1576/// instruction, using the specified assembler variant. Targets should
1577/// overried this to format as appropriate.
1578bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001579 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001580 // Target doesn't support this yet!
1581 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001582}
Chris Lattner1d08c652006-02-24 20:21:58 +00001583
1584bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1585 unsigned AsmVariant,
1586 const char *ExtraCode) {
1587 // Target doesn't support this yet!
1588 return true;
1589}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001590
1591/// printBasicBlockLabel - This method prints the label for the specified
1592/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001593void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001594 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001595 bool printColon,
1596 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001597 if (printAlign) {
1598 unsigned Align = MBB->getAlignment();
1599 if (Align)
1600 EmitAlignment(Log2_32(Align));
1601 }
1602
Dan Gohman6896901e2008-06-30 22:03:41 +00001603 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001604 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001605 if (printColon)
1606 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001607 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001608 O << '\t' << TAI->getCommentString() << ' '
Daniel Dunbar6115b392009-07-26 09:48:23 +00001609 << MBB->getBasicBlock()->getNameStr();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001610}
Nate Begeman984c1a42006-08-12 21:29:52 +00001611
Evan Cheng797d56f2007-11-09 01:32:10 +00001612/// printPICJumpTableSetLabel - This method prints a set label for the
1613/// specified MachineBasicBlock for a jumptable entry.
1614void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1615 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001616 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001617 return;
1618
Jim Laskeya6211dc2006-09-06 18:34:40 +00001619 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001620 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001621 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001622 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1623 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001624}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001625
Evan Cheng797d56f2007-11-09 01:32:10 +00001626void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1627 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001628 if (!TAI->getSetDirective())
1629 return;
1630
1631 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001632 << getFunctionNumber() << '_' << uid << '_' << uid2
1633 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001634 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001635 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1636 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001637}
1638
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001639/// printDataDirective - This method prints the asm directive for the
1640/// specified type.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001641void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001642 const TargetData *TD = TM.getTargetData();
1643 switch (type->getTypeID()) {
Chris Lattnerfe785582009-07-15 04:42:49 +00001644 case Type::FloatTyID: case Type::DoubleTyID:
1645 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1646 assert(0 && "Should have already output floating point constant.");
1647 default:
1648 assert(0 && "Can't handle printing this type of thing");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001649 case Type::IntegerTyID: {
1650 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1651 if (BitWidth <= 8)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001652 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001653 else if (BitWidth <= 16)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001654 O << TAI->getData16bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001655 else if (BitWidth <= 32)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001656 O << TAI->getData32bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001657 else if (BitWidth <= 64) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001658 assert(TAI->getData64bitsDirective(AddrSpace) &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001659 "Target cannot handle 64-bit constant exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001660 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohmanf9b20542008-09-08 16:40:13 +00001661 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001662 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001663 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001664 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001665 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001666 case Type::PointerTyID:
1667 if (TD->getPointerSize() == 8) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001668 assert(TAI->getData64bitsDirective(AddrSpace) &&
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001669 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001670 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001671 } else if (TD->getPointerSize() == 2) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001672 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001673 } else if (TD->getPointerSize() == 1) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001674 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001675 } else {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001676 O << TAI->getData32bitsDirective(AddrSpace);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001677 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001678 break;
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001679 }
1680}
Dale Johannesen915e1542007-02-16 01:54:53 +00001681
Anton Korobeynikoved473292008-08-08 18:25:07 +00001682void AsmPrinter::printVisibility(const std::string& Name,
1683 unsigned Visibility) const {
1684 if (Visibility == GlobalValue::HiddenVisibility) {
1685 if (const char *Directive = TAI->getHiddenDirective())
1686 O << Directive << Name << '\n';
1687 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1688 if (const char *Directive = TAI->getProtectedDirective())
1689 O << Directive << Name << '\n';
1690 }
1691}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001692
Anton Korobeynikovbff4b372008-11-22 16:15:34 +00001693void AsmPrinter::printOffset(int64_t Offset) const {
1694 if (Offset > 0)
1695 O << '+' << Offset;
1696 else if (Offset < 0)
1697 O << Offset;
1698}
1699
Gordon Henriksend930f912008-08-17 18:44:35 +00001700GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1701 if (!S->usesMetadata())
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001702 return 0;
1703
Gordon Henriksend930f912008-08-17 18:44:35 +00001704 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001705 if (GCPI != GCMetadataPrinters.end())
1706 return GCPI->second;
1707
Gordon Henriksend930f912008-08-17 18:44:35 +00001708 const char *Name = S->getName().c_str();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001709
1710 for (GCMetadataPrinterRegistry::iterator
1711 I = GCMetadataPrinterRegistry::begin(),
1712 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1713 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001714 GCMetadataPrinter *GMP = I->instantiate();
1715 GMP->S = S;
1716 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1717 return GMP;
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001718 }
1719
Gordon Henriksend930f912008-08-17 18:44:35 +00001720 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +00001721 llvm_unreachable(0);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001722}
David Greenede544782009-07-13 20:25:48 +00001723
1724/// EmitComments - Pretty-print comments for instructions
1725void AsmPrinter::EmitComments(const MachineInstr &MI) const
1726{
David Greene6e2eda12009-07-22 20:33:26 +00001727 if (VerboseAsm) {
1728 if (!MI.getDebugLoc().isUnknown()) {
1729 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greenede9cd442009-07-16 22:24:20 +00001730
David Greene6e2eda12009-07-22 20:33:26 +00001731 // Print source line info
1732 O.PadToColumn(TAI->getCommentColumn(), 1);
1733 O << TAI->getCommentString() << " SrcLine ";
1734 if (DLT.CompileUnit->hasInitializer()) {
1735 Constant *Name = DLT.CompileUnit->getInitializer();
1736 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1737 if (NameString->isString()) {
1738 O << NameString->getAsString() << " ";
1739 }
1740 }
1741 O << DLT.Line;
1742 if (DLT.Col != 0)
1743 O << ":" << DLT.Col;
1744 }
David Greenede9cd442009-07-16 22:24:20 +00001745 }
David Greenede544782009-07-13 20:25:48 +00001746}
1747
1748/// EmitComments - Pretty-print comments for instructions
1749void AsmPrinter::EmitComments(const MCInst &MI) const
1750{
David Greene6e2eda12009-07-22 20:33:26 +00001751 if (VerboseAsm) {
1752 if (!MI.getDebugLoc().isUnknown()) {
1753 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greenede9cd442009-07-16 22:24:20 +00001754
David Greene6e2eda12009-07-22 20:33:26 +00001755 // Print source line info
1756 O.PadToColumn(TAI->getCommentColumn(), 1);
1757 O << TAI->getCommentString() << " SrcLine ";
1758 if (DLT.CompileUnit->hasInitializer()) {
1759 Constant *Name = DLT.CompileUnit->getInitializer();
1760 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1761 if (NameString->isString()) {
1762 O << NameString->getAsString() << " ";
1763 }
1764 }
1765 O << DLT.Line;
1766 if (DLT.Col != 0)
1767 O << ":" << DLT.Col;
1768 }
David Greenede9cd442009-07-16 22:24:20 +00001769 }
David Greenede544782009-07-13 20:25:48 +00001770}