blob: 0bbbddf54c716e89b15ffc02e4904a63f26b0e4f [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"
David Greenede9cd442009-07-16 22:24:20 +000025#include "llvm/MC/MCInst.h"
Evan Cheng5e5a63c2009-03-25 01:47:28 +000026#include "llvm/Support/CommandLine.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
David Greenede544782009-07-13 20:25:48 +000028#include "llvm/Support/FormattedStream.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000029#include "llvm/Support/Mangler.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000030#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000031#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000032#include "llvm/Target/TargetLowering.h"
Evan Chengc963f6c2008-07-01 23:18:29 +000033#include "llvm/Target/TargetOptions.h"
Evan Cheng0e7b00d2008-03-15 00:03:38 +000034#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng797d56f2007-11-09 01:32:10 +000035#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner17f71652008-08-17 07:19:36 +000036#include "llvm/ADT/SmallString.h"
Owen Anderson93719642008-08-21 00:14:44 +000037#include "llvm/ADT/StringExtras.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000038#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000039using namespace llvm;
40
Evan Cheng5e5a63c2009-03-25 01:47:28 +000041static cl::opt<cl::boolOrDefault>
42AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
43 cl::init(cl::BOU_UNSET));
44
Devang Patel8c78a0b2007-05-03 01:11:54 +000045char AsmPrinter::ID = 0;
David Greenea31f96c2009-07-14 20:18:05 +000046AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbar75c12e12009-07-01 01:48:54 +000047 const TargetAsmInfo *T, bool VDef)
48 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Cheng0e7b00d2008-03-15 00:03:38 +000049 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Owen Andersone3849522009-06-25 16:55:32 +000050 IsInTextSection(false), LastMI(0), LastFn(0), Counter(~0U),
51 PrevDLT(0, ~0U, ~0U) {
Chris Lattner1fd58882009-06-24 19:09:55 +000052 DW = 0; MMI = 0;
Evan Cheng5e5a63c2009-03-25 01:47:28 +000053 switch (AsmVerbose) {
54 case cl::BOU_UNSET: VerboseAsm = VDef; break;
55 case cl::BOU_TRUE: VerboseAsm = true; break;
56 case cl::BOU_FALSE: VerboseAsm = false; break;
57 }
58}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000059
Gordon Henriksendbe06d32008-08-17 12:08:44 +000060AsmPrinter::~AsmPrinter() {
61 for (gcp_iterator I = GCMetadataPrinters.begin(),
62 E = GCMetadataPrinters.end(); I != E; ++I)
63 delete I->second;
64}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000065
Chris Lattner8488ba22006-05-09 04:59:56 +000066/// SwitchToTextSection - Switch to the specified text section of the executable
67/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000068///
Chris Lattner8488ba22006-05-09 04:59:56 +000069void AsmPrinter::SwitchToTextSection(const char *NewSection,
70 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000071 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000072 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000073 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000074 else
75 NS = NewSection;
76
77 // If we're already in this section, we're done.
78 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000079
Chris Lattner4ebc6a22006-05-09 05:33:48 +000080 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000081 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +000082 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Jeff Cohen470f4312006-05-02 03:58:45 +000083
Chris Lattner4ebc6a22006-05-09 05:33:48 +000084 CurrentSection = NS;
85
86 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000087 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +000088
89 IsInTextSection = true;
Chris Lattner2ea5c992005-11-21 07:06:27 +000090}
91
Nate Begeman78756502006-07-27 01:13:04 +000092/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000093/// if we are not already in it!
94///
95void AsmPrinter::SwitchToDataSection(const char *NewSection,
96 const GlobalValue *GV) {
97 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000098 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000099 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +0000100 else
101 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +0000102
Chris Lattner6341df82006-05-09 05:23:12 +0000103 // If we're already in this section, we're done.
104 if (CurrentSection == NS) return;
105
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000106 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000107 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +0000108 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000109
110 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +0000111
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000112 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000113 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +0000114
115 IsInTextSection = false;
Chris Lattner8488ba22006-05-09 04:59:56 +0000116}
117
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000118/// SwitchToSection - Switch to the specified section of the executable if we
119/// are not already in it!
120void AsmPrinter::SwitchToSection(const Section* NS) {
121 const std::string& NewSection = NS->getName();
122
123 // If we're already in this section, we're done.
124 if (CurrentSection == NewSection) return;
125
126 // Close the current section, if applicable.
127 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
128 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
129
130 // FIXME: Make CurrentSection a Section* in the future
131 CurrentSection = NewSection;
Anton Korobeynikovf8dc8aa2008-09-24 22:15:21 +0000132 CurrentSection_ = NS;
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000133
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000134 if (!CurrentSection.empty()) {
135 // If section is named we need to switch into it via special '.section'
136 // directive and also append funky flags. Otherwise - section name is just
137 // some magic assembler directive.
Chris Lattner68d64a22009-07-24 04:26:19 +0000138 if (NS->hasFlag(SectionFlags::Named))
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000139 O << TAI->getSwitchToSectionDirective()
140 << CurrentSection
141 << TAI->getSectionFlags(NS->getFlags());
142 else
143 O << CurrentSection;
144 O << TAI->getDataSectionStartSuffix() << '\n';
145 }
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000146
147 IsInTextSection = (NS->getFlags() & SectionFlags::Code);
148}
Chris Lattner8488ba22006-05-09 04:59:56 +0000149
Gordon Henriksen5180e852008-01-07 01:30:38 +0000150void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
151 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksend930f912008-08-17 18:44:35 +0000152 AU.addRequired<GCModuleInfo>();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000153}
154
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000155bool AsmPrinter::doInitialization(Module &M) {
Bill Wendlingdde1a8e2009-07-20 21:30:28 +0000156 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
Chris Lattner1177cee2009-07-21 17:30:51 +0000157 TAI->getLinkerPrivateGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000158
Chris Lattnerb8476452009-06-18 23:41:35 +0000159 if (TAI->doesAllowQuotesInName())
160 Mang->setUseQuotes(true);
161
Duncan Sands5a913d62009-01-28 13:14:17 +0000162 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000163 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindolacda011b2008-12-03 11:01:37 +0000164
165 if (TAI->hasSingleParameterDotFile()) {
166 /* Very minimal debug info. It is ignored if we emit actual
167 debug info. If we don't, this at helps the user find where
168 a function came from. */
169 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
170 }
171
Gordon Henriksend930f912008-08-17 18:44:35 +0000172 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
173 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
174 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000175
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000176 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000177 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000178 << M.getModuleInlineAsm()
Dan Gohman6896901e2008-06-30 22:03:41 +0000179 << '\n' << TAI->getCommentString()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000180 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000181
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000182 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000183
Chris Lattner1fd58882009-06-24 19:09:55 +0000184 if (TAI->doesSupportDebugInformation() ||
185 TAI->doesSupportExceptionHandling()) {
186 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
187 if (MMI)
Devang Patel33f4eb42009-06-19 21:54:26 +0000188 MMI->AnalyzeModule(M);
Chris Lattner1fd58882009-06-24 19:09:55 +0000189 DW = getAnalysisIfAvailable<DwarfWriter>();
190 if (DW)
191 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel33f4eb42009-06-19 21:54:26 +0000192 }
193
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000194 return false;
195}
196
197bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner100865e2009-07-21 18:38:57 +0000198 // Emit global variables.
199 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
200 I != E; ++I)
201 PrintGlobalVariable(I);
202
Chris Lattner70413122009-06-24 18:54:37 +0000203 // Emit final debug information.
204 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
205 DW->EndModule();
206
Chris Lattner2981dc12009-06-24 18:52:01 +0000207 // If the target wants to know about weak references, print them all.
Rafael Espindolad7998d02006-12-18 03:37:18 +0000208 if (TAI->getWeakRefDirective()) {
Chris Lattner2981dc12009-06-24 18:52:01 +0000209 // FIXME: This is not lazy, it would be nice to only print weak references
210 // to stuff that is actually used. Note that doing so would require targets
211 // to notice uses in operands (due to constant exprs etc). This should
212 // happen with the MC stuff eventually.
213 SwitchToDataSection("");
Rafael Espindolad7998d02006-12-18 03:37:18 +0000214
Chris Lattner2981dc12009-06-24 18:52:01 +0000215 // Print out module-level global variables here.
216 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
217 I != E; ++I) {
218 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000219 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000220 }
221
222 for (Module::const_iterator I = M.begin(), E = M.end();
223 I != E; ++I) {
224 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000225 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000226 }
Rafael Espindolad7998d02006-12-18 03:37:18 +0000227 }
228
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000229 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000230 if (!M.alias_empty())
Anton Korobeynikovf8dc8aa2008-09-24 22:15:21 +0000231 SwitchToSection(TAI->getTextSection());
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000232
Dan Gohman6896901e2008-06-30 22:03:41 +0000233 O << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000234 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner2981dc12009-06-24 18:52:01 +0000235 I != E; ++I) {
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000236 std::string Name = Mang->getMangledName(I);
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000237
238 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000239 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000240
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000241 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000242 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000243 else if (I->hasWeakLinkage())
Dan Gohman6896901e2008-06-30 22:03:41 +0000244 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindola6de96a12009-01-15 20:18:42 +0000245 else if (!I->hasLocalLinkage())
Torok Edwinfbcc6632009-07-14 16:55:14 +0000246 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000247
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000248 printVisibility(Name, I->getVisibility());
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000249
Dan Gohman6896901e2008-06-30 22:03:41 +0000250 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000251 }
252 }
253
Duncan Sands5a913d62009-01-28 13:14:17 +0000254 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000255 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
256 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
257 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
258 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000259
Dan Gohmanbcde1722008-05-05 00:28:39 +0000260 // If we don't have any trampolines, then we don't require stack memory
261 // to be executable. Some targets have a directive to declare this.
Chris Lattner2981dc12009-06-24 18:52:01 +0000262 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmanbcde1722008-05-05 00:28:39 +0000263 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
264 if (TAI->getNonexecutableStackDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000265 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmanbcde1722008-05-05 00:28:39 +0000266
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000267 delete Mang; Mang = 0;
Chris Lattner1fd58882009-06-24 19:09:55 +0000268 DW = 0; MMI = 0;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000269 return false;
270}
271
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000272std::string
273AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendling067f1d82007-09-18 01:47:22 +0000274 assert(MF && "No machine function?");
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000275 return Mang->getMangledName(MF->getFunction(), ".eh",
276 TAI->is_EHSymbolPrivate());
Bill Wendling067f1d82007-09-18 01:47:22 +0000277}
278
Chris Lattnerbb644e32005-11-21 07:51:36 +0000279void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000280 // What's my mangled name?
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000281 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000282 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000283}
284
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000285namespace {
286 // SectionCPs - Keep track the alignment, constpool entries per Section.
287 struct SectionCPs {
288 const Section *S;
289 unsigned Alignment;
290 SmallVector<unsigned, 4> CPEs;
291 SectionCPs(const Section *s, unsigned a) : S(s), Alignment(a) {};
292 };
293}
294
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000295/// EmitConstantPool - Print to the current output stream assembly
296/// representations of the constants in the constant pool MCP. This is
297/// used to print out constants which have been "spilled to memory" by
298/// the code generator.
299///
300void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000301 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000302 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000303
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000304 // Calculate sections for constant pool entries. We collect entries to go into
305 // the same section together to reduce amount of section switch statements.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000306 SmallVector<SectionCPs, 4> CPSections;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000307 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerb300a4f2009-07-22 00:28:43 +0000308 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000309 unsigned Align = CPE.getAlignment();
Chris Lattnerfb6867c2009-07-26 06:26:55 +0000310
311 SectionKind Kind;
312 switch (CPE.getRelocationInfo()) {
313 default: llvm_unreachable("Unknown section kind");
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000314 case 2: Kind = SectionKind::get(SectionKind::ReadOnlyWithRel, false); break;
315 case 1:
316 Kind = SectionKind::get(SectionKind::ReadOnlyWithRelLocal,false);
317 break;
Chris Lattnerfb6867c2009-07-26 06:26:55 +0000318 case 0:
Chris Lattnere45ff5c2009-07-26 07:00:12 +0000319 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
320 case 4: Kind = SectionKind::get(SectionKind::MergeableConst4,false); break;
321 case 8: Kind = SectionKind::get(SectionKind::MergeableConst8,false); break;
322 case 16: Kind = SectionKind::get(SectionKind::MergeableConst16,false);break;
323 default: Kind = SectionKind::get(SectionKind::MergeableConst,false); break;
324 }
Chris Lattnerfb6867c2009-07-26 06:26:55 +0000325 }
326
Chris Lattner29151b02009-07-26 06:48:26 +0000327 const Section *S = TAI->getSectionForMergeableConstant(Kind);
Chris Lattnerb300a4f2009-07-22 00:28:43 +0000328
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000329 // The number of sections are small, just do a linear search from the
330 // last section to the first.
331 bool Found = false;
332 unsigned SecIdx = CPSections.size();
333 while (SecIdx != 0) {
334 if (CPSections[--SecIdx].S == S) {
335 Found = true;
336 break;
337 }
338 }
339 if (!Found) {
340 SecIdx = CPSections.size();
341 CPSections.push_back(SectionCPs(S, Align));
342 }
343
344 if (Align > CPSections[SecIdx].Alignment)
345 CPSections[SecIdx].Alignment = Align;
346 CPSections[SecIdx].CPEs.push_back(i);
Evan Chengec1d60b2006-06-29 00:26:09 +0000347 }
348
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000349 // Now print stuff into the calculated sections.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000350 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
351 SwitchToSection(CPSections[i].S);
352 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Chengec1d60b2006-06-29 00:26:09 +0000353
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000354 unsigned Offset = 0;
355 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
356 unsigned CPI = CPSections[i].CPEs[j];
357 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000358
Chris Lattnerf6190822006-02-09 04:46:04 +0000359 // Emit inter-object padding for alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000360 unsigned AlignMask = CPE.getAlignment() - 1;
361 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
362 EmitZeros(NewOffset - Offset);
363
364 const Type *Ty = CPE.getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000365 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000366
367 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
368 << CPI << ":\t\t\t\t\t";
369 if (VerboseAsm) {
370 O << TAI->getCommentString() << ' ';
371 WriteTypeSymbolic(O, CPE.getType(), 0);
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000372 }
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000373 O << '\n';
374 if (CPE.isMachineConstantPoolEntry())
375 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
376 else
377 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000378 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000379 }
380}
381
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000382/// EmitJumpTableInfo - Print assembly representations of the jump tables used
383/// by the current function to the current output stream.
384///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000385void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
386 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000387 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
388 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000389
Jim Laskey70323a82006-12-14 19:17:33 +0000390 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000391
Nate Begeman78756502006-07-27 01:13:04 +0000392 // Pick the directive to use to print the jump table entries, and switch to
393 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000394 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000395
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000396 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
397 const Function *F = MF.getFunction();
Chris Lattner71b6d692009-07-24 16:40:09 +0000398 const Section *FuncSection = TAI->SectionForGlobal(F);
399
Evan Chengde9e36a2009-06-18 20:37:15 +0000400 bool JTInDiffSection = false;
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000401 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Evan Chengde9e36a2009-06-18 20:37:15 +0000402 !JumpTableDataSection ||
Chris Lattner71b6d692009-07-24 16:40:09 +0000403 FuncSection->hasFlag(SectionFlags::Linkonce)) {
Jim Laskey70323a82006-12-14 19:17:33 +0000404 // In PIC mode, we need to emit the jump table to the same section as the
405 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000406 // We should also do if the section name is NULL or function is declared in
407 // discardable section.
Chris Lattner71b6d692009-07-24 16:40:09 +0000408 SwitchToSection(FuncSection);
Nate Begeman78756502006-07-27 01:13:04 +0000409 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000410 SwitchToDataSection(JumpTableDataSection);
Evan Chengde9e36a2009-06-18 20:37:15 +0000411 JTInDiffSection = true;
Nate Begeman78756502006-07-27 01:13:04 +0000412 }
Jim Laskey70323a82006-12-14 19:17:33 +0000413
414 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000415
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000416 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000417 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000418
419 // If this jump table was deleted, ignore it.
420 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000421
422 // For PIC codegen, if possible we want to use the SetDirective to reduce
423 // the number of relocations the assembler will generate for the jump table.
424 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000425 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000426 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000427 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000428 if (EmittedSets.insert(JTBBs[ii]))
429 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000430
Chris Lattner0ee2d462007-01-18 01:12:56 +0000431 // On some targets (e.g. darwin) we want to emit two consequtive labels
432 // before each jump table. The first label is never referenced, but tells
433 // the assembler and linker the extents of the jump table object. The
434 // second label is actually referenced by the code.
Evan Chengde9e36a2009-06-18 20:37:15 +0000435 if (JTInDiffSection) {
436 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
437 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
438 }
Chris Lattner0ee2d462007-01-18 01:12:56 +0000439
Evan Chengcdf36092007-10-14 05:57:21 +0000440 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
441 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000442
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000443 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000444 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000445 O << '\n';
446 }
447 }
448}
449
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000450void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
451 const MachineBasicBlock *MBB,
452 unsigned uid) const {
453 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
454
455 // Use JumpTableDirective otherwise honor the entry size from the jump table
456 // info.
457 const char *JTEntryDirective = TAI->getJumpTableDirective();
458 bool HadJTEntryDirective = JTEntryDirective != NULL;
459 if (!HadJTEntryDirective) {
460 JTEntryDirective = MJTI->getEntrySize() == 4 ?
461 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
462 }
463
464 O << JTEntryDirective << ' ';
465
466 // If we have emitted set directives for the jump table entries, print
467 // them rather than the entries themselves. If we're emitting PIC, then
468 // emit the table entries as differences between two text section labels.
469 // If we're emitting non-PIC code, then emit the entries as direct
470 // references to the target basic blocks.
471 if (IsPic) {
472 if (TAI->getSetDirective()) {
473 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
474 << '_' << uid << "_set_" << MBB->getNumber();
475 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000476 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000477 // If the arch uses custom Jump Table directives, don't calc relative to
478 // JT
479 if (!HadJTEntryDirective)
480 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
481 << getFunctionNumber() << '_' << uid;
482 }
483 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000484 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000485 }
486}
487
488
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000489/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
490/// special global used by LLVM. If so, emit it and return true, otherwise
491/// do nothing and return false.
492bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000493 if (GV->getName() == "llvm.used") {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000494 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
495 EmitLLVMUsedList(GV->getInitializer());
496 return true;
497 }
498
Chris Lattner58f9bb22009-07-20 06:14:25 +0000499 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner184f1be2009-04-13 05:44:34 +0000500 if (GV->getSection() == "llvm.metadata" ||
501 GV->hasAvailableExternallyLinkage())
502 return true;
Jim Laskey313570f2006-03-07 22:00:35 +0000503
504 if (!GV->hasAppendingLinkage()) return false;
505
506 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000507
Evan Cheng53ce7de2007-06-04 20:39:18 +0000508 const TargetData *TD = TM.getTargetData();
509 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner317293b2009-03-09 05:52:15 +0000510 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner126dab22009-03-09 08:18:48 +0000511 SwitchToDataSection(TAI->getStaticCtorsSection());
512 EmitAlignment(Align, 0);
513 EmitXXStructorList(GV->getInitializer());
514 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000515 }
516
Chris Lattner317293b2009-03-09 05:52:15 +0000517 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner126dab22009-03-09 08:18:48 +0000518 SwitchToDataSection(TAI->getStaticDtorsSection());
519 EmitAlignment(Align, 0);
520 EmitXXStructorList(GV->getInitializer());
521 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000522 }
523
524 return false;
525}
526
Chris Lattner66af3902006-09-26 03:38:18 +0000527/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesenabb1e772008-09-09 22:29:13 +0000528/// global in the specified llvm.used list for which emitUsedDirectiveFor
529/// is true, as being used with this directive.
Chris Lattner66af3902006-09-26 03:38:18 +0000530void AsmPrinter::EmitLLVMUsedList(Constant *List) {
531 const char *Directive = TAI->getUsedDirective();
532
Dan Gohman4fe64de2009-06-14 23:30:43 +0000533 // Should be an array of 'i8*'.
Chris Lattner66af3902006-09-26 03:38:18 +0000534 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
535 if (InitList == 0) return;
536
537 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner1fcac9f2009-07-17 22:00:23 +0000538 const GlobalValue *GV =
539 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
540 if (GV && TAI->emitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000541 O << Directive;
542 EmitConstantValueOnly(InitList->getOperand(i));
543 O << '\n';
544 }
Chris Lattner66af3902006-09-26 03:38:18 +0000545 }
546}
547
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000548/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
549/// function pointers, ignoring the init priority.
550void AsmPrinter::EmitXXStructorList(Constant *List) {
551 // Should be an array of '{ int, void ()* }' structs. The first value is the
552 // init priority, which we ignore.
553 if (!isa<ConstantArray>(List)) return;
554 ConstantArray *InitList = cast<ConstantArray>(List);
555 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
556 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
557 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000558
559 if (CS->getOperand(1)->isNullValue())
560 return; // Found a null terminator, exit printing.
561 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000562 EmitGlobalConstant(CS->getOperand(1));
563 }
564}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000565
Jim Laskey71262542006-10-17 13:41:07 +0000566/// getGlobalLinkName - Returns the asm/link name of of the specified
567/// global variable. Should be overridden by each target asm printer to
568/// generate the appropriate value.
Bill Wendling992f8462009-04-10 00:12:49 +0000569const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
570 std::string &LinkName) const {
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000571 if (isa<Function>(GV)) {
572 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000573 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000574 LinkName += TAI->getFunctionAddrSuffix();
575 } else {
576 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000577 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000578 LinkName += TAI->getGlobalVarAddrSuffix();
579 }
580
Jim Laskeyd24b9132006-10-17 17:17:24 +0000581 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000582}
583
Jim Laskey18fc0972007-02-21 22:47:38 +0000584/// EmitExternalGlobal - Emit the external reference to a global variable.
585/// Should be overridden if an indirect reference should be used.
586void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling992f8462009-04-10 00:12:49 +0000587 std::string GLN;
588 O << getGlobalLinkName(GV, GLN);
Jim Laskey18fc0972007-02-21 22:47:38 +0000589}
590
591
592
Jim Laskey1c055e82007-01-25 15:12:02 +0000593//===----------------------------------------------------------------------===//
594/// LEB 128 number encoding.
595
596/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
597/// representing an unsigned leb128 value.
598void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner09487cd2008-11-10 04:35:24 +0000599 char Buffer[20];
Jim Laskey1c055e82007-01-25 15:12:02 +0000600 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000601 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000602 Value >>= 7;
603 if (Value) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000604 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000605 if (Value) O << ", ";
606 } while (Value);
607}
608
Jim Laskey1c055e82007-01-25 15:12:02 +0000609/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
610/// representing a signed leb128 value.
611void AsmPrinter::PrintSLEB128(int Value) const {
612 int Sign = Value >> (8 * sizeof(Value) - 1);
613 bool IsMore;
Chris Lattner09487cd2008-11-10 04:35:24 +0000614 char Buffer[20];
Anton Korobeynikovbd890b12008-08-16 12:57:46 +0000615
Jim Laskey1c055e82007-01-25 15:12:02 +0000616 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000617 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000618 Value >>= 7;
619 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
620 if (IsMore) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000621 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000622 if (IsMore) O << ", ";
623 } while (IsMore);
624}
625
Jim Laskey1c055e82007-01-25 15:12:02 +0000626//===--------------------------------------------------------------------===//
627// Emission and print routines
628//
629
630/// PrintHex - Print a value as a hexidecimal value.
631///
632void AsmPrinter::PrintHex(int Value) const {
Chris Lattner5505eed2008-11-10 04:30:26 +0000633 char Buffer[20];
Chris Lattner5505eed2008-11-10 04:30:26 +0000634 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000635}
636
637/// EOL - Print a newline character to asm stream. If a comment is present
638/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000639void AsmPrinter::EOL() const {
Dan Gohman6896901e2008-06-30 22:03:41 +0000640 O << '\n';
Jim Laskey18fc0972007-02-21 22:47:38 +0000641}
Owen Anderson1d952532008-07-01 21:16:27 +0000642
Jim Laskey1c055e82007-01-25 15:12:02 +0000643void AsmPrinter::EOL(const std::string &Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000644 if (VerboseAsm && !Comment.empty()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000645 O << '\t'
Jim Laskey1c055e82007-01-25 15:12:02 +0000646 << TAI->getCommentString()
Dan Gohman6896901e2008-06-30 22:03:41 +0000647 << ' '
Jim Laskey1c055e82007-01-25 15:12:02 +0000648 << Comment;
649 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000650 O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000651}
652
Owen Anderson1d952532008-07-01 21:16:27 +0000653void AsmPrinter::EOL(const char* Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000654 if (VerboseAsm && *Comment) {
Owen Anderson1d952532008-07-01 21:16:27 +0000655 O << '\t'
656 << TAI->getCommentString()
657 << ' '
658 << Comment;
659 }
660 O << '\n';
661}
662
Jim Laskey1c055e82007-01-25 15:12:02 +0000663/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
664/// unsigned leb128 value.
665void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
666 if (TAI->hasLEB128()) {
667 O << "\t.uleb128\t"
668 << Value;
669 } else {
670 O << TAI->getData8bitsDirective();
671 PrintULEB128(Value);
672 }
673}
674
675/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
676/// signed leb128 value.
677void AsmPrinter::EmitSLEB128Bytes(int Value) const {
678 if (TAI->hasLEB128()) {
679 O << "\t.sleb128\t"
680 << Value;
681 } else {
682 O << TAI->getData8bitsDirective();
683 PrintSLEB128(Value);
684 }
685}
686
687/// EmitInt8 - Emit a byte directive and value.
688///
689void AsmPrinter::EmitInt8(int Value) const {
690 O << TAI->getData8bitsDirective();
691 PrintHex(Value & 0xFF);
692}
693
694/// EmitInt16 - Emit a short directive and value.
695///
696void AsmPrinter::EmitInt16(int Value) const {
697 O << TAI->getData16bitsDirective();
698 PrintHex(Value & 0xFFFF);
699}
700
701/// EmitInt32 - Emit a long directive and value.
702///
703void AsmPrinter::EmitInt32(int Value) const {
704 O << TAI->getData32bitsDirective();
705 PrintHex(Value);
706}
707
708/// EmitInt64 - Emit a long long directive and value.
709///
710void AsmPrinter::EmitInt64(uint64_t Value) const {
711 if (TAI->getData64bitsDirective()) {
712 O << TAI->getData64bitsDirective();
713 PrintHex(Value);
714 } else {
715 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000716 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000717 EmitInt32(unsigned(Value));
718 } else {
Dan Gohman6896901e2008-06-30 22:03:41 +0000719 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000720 EmitInt32(unsigned(Value >> 32));
721 }
722 }
723}
724
725/// toOctal - Convert the low order bits of X into an octal digit.
726///
727static inline char toOctal(int X) {
728 return (X&7)+'0';
729}
730
731/// printStringChar - Print a char, escaped if necessary.
732///
David Greenea31f96c2009-07-14 20:18:05 +0000733static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000734 if (C == '"') {
735 O << "\\\"";
736 } else if (C == '\\') {
737 O << "\\\\";
Chris Lattner1f9053a2009-01-22 23:38:45 +0000738 } else if (isprint((unsigned char)C)) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000739 O << C;
740 } else {
741 switch(C) {
742 case '\b': O << "\\b"; break;
743 case '\f': O << "\\f"; break;
744 case '\n': O << "\\n"; break;
745 case '\r': O << "\\r"; break;
746 case '\t': O << "\\t"; break;
747 default:
748 O << '\\';
749 O << toOctal(C >> 6);
750 O << toOctal(C >> 3);
751 O << toOctal(C >> 0);
752 break;
753 }
754 }
755}
756
757/// EmitString - Emit a string with quotes and a null terminator.
758/// Special characters are emitted properly.
759/// \literal (Eg. '\t') \endliteral
760void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling16abfc92009-04-09 23:51:31 +0000761 EmitString(String.c_str(), String.size());
762}
763
764void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000765 const char* AscizDirective = TAI->getAscizDirective();
766 if (AscizDirective)
767 O << AscizDirective;
768 else
769 O << TAI->getAsciiDirective();
Dan Gohman6896901e2008-06-30 22:03:41 +0000770 O << '\"';
Bill Wendling16abfc92009-04-09 23:51:31 +0000771 for (unsigned i = 0; i < Size; ++i)
Chris Lattner69b586e2009-04-08 00:28:38 +0000772 printStringChar(O, String[i]);
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000773 if (AscizDirective)
Dan Gohman6896901e2008-06-30 22:03:41 +0000774 O << '\"';
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000775 else
776 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000777}
778
779
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000780/// EmitFile - Emit a .file directive.
781void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
782 O << "\t.file\t" << Number << " \"";
Chris Lattner69b586e2009-04-08 00:28:38 +0000783 for (unsigned i = 0, N = Name.size(); i < N; ++i)
784 printStringChar(O, Name[i]);
Dan Gohman6896901e2008-06-30 22:03:41 +0000785 O << '\"';
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000786}
787
788
Jim Laskey1c055e82007-01-25 15:12:02 +0000789//===----------------------------------------------------------------------===//
790
Chris Lattner3e3ff302007-05-31 18:57:45 +0000791// EmitAlignment - Emit an alignment directive to the specified power of
792// two boundary. For example, if you pass in 3 here, you will get an 8
793// byte alignment. If a global value is specified, and if that global has
794// an explicit alignment requested, it will unconditionally override the
795// alignment request. However, if ForcedAlignBits is specified, this value
796// has final say: the ultimate alignment will be the max of ForcedAlignBits
797// and the alignment computed with NumBits and the global.
798//
799// The algorithm is:
800// Align = NumBits;
801// if (GV && GV->hasalignment) Align = GV->getalignment();
802// Align = std::max(Align, ForcedAlignBits);
803//
804void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000805 unsigned ForcedAlignBits,
806 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000807 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000808 NumBits = Log2_32(GV->getAlignment());
809 NumBits = std::max(NumBits, ForcedAlignBits);
810
Chris Lattner747960d2005-11-10 18:09:27 +0000811 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000812 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000813 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000814
815 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng26edb592008-02-29 19:36:59 +0000816 UseFillExpr &= IsInTextSection && FillValue;
Chris Lattner09487cd2008-11-10 04:35:24 +0000817 if (UseFillExpr) {
818 O << ',';
819 PrintHex(FillValue);
820 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000821 O << '\n';
Chris Lattner1d35c162004-08-17 19:14:29 +0000822}
823
Jim Laskey18fc0972007-02-21 22:47:38 +0000824
Chris Lattnerbb644e32005-11-21 07:51:36 +0000825/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000826///
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000827void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000828 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000829 if (TAI->getZeroDirective()) {
830 O << TAI->getZeroDirective() << NumZeros;
831 if (TAI->getZeroDirectiveSuffix())
832 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000833 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000834 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000835 for (; NumZeros; --NumZeros)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000836 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000837 }
838 }
839}
840
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000841// Print out the specified constant, without a storage class. Only the
842// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000843void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000844 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000845 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000846 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000847 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000848 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000849 // This is a constant address for a global variable or function. Use the
850 // name of the variable or function as the address value, possibly
851 // decorating it with GlobalVarAddrPrefix/Suffix or
852 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000853 if (isa<Function>(GV)) {
854 O << TAI->getFunctionAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000855 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000856 << TAI->getFunctionAddrSuffix();
857 } else {
858 O << TAI->getGlobalVarAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000859 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000860 << TAI->getGlobalVarAddrSuffix();
861 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000862 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000863 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000864 unsigned Opcode = CE->getOpcode();
865 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000866 case Instruction::GetElementPtr: {
867 // generate a symbolic expression for the byte address
868 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000869 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
870 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
871 idxVec.size())) {
Chris Lattner7e1d2862009-02-05 06:55:21 +0000872 // Truncate/sext the offset to the pointer size.
873 if (TD->getPointerSizeInBits() != 64) {
874 int SExtAmount = 64-TD->getPointerSizeInBits();
875 Offset = (Offset << SExtAmount) >> SExtAmount;
876 }
877
Chris Lattner145569b2005-02-14 21:40:26 +0000878 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000879 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000880 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000881 if (Offset > 0)
882 O << ") + " << Offset;
883 else if (Offset < 0)
884 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000885 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000886 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000887 }
888 break;
889 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000890 case Instruction::Trunc:
891 case Instruction::ZExt:
892 case Instruction::SExt:
893 case Instruction::FPTrunc:
894 case Instruction::FPExt:
895 case Instruction::UIToFP:
896 case Instruction::SIToFP:
897 case Instruction::FPToUI:
898 case Instruction::FPToSI:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000899 llvm_unreachable("FIXME: Don't yet support this kind of constant cast expr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000900 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000901 case Instruction::BitCast:
902 return EmitConstantValueOnly(CE->getOperand(0));
903
Chris Lattner0c537da2006-12-12 05:18:19 +0000904 case Instruction::IntToPtr: {
905 // Handle casts to pointers by changing them into casts to the appropriate
906 // integer type. This promotes constant folding and simplifies this code.
907 Constant *Op = CE->getOperand(0);
908 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
909 return EmitConstantValueOnly(Op);
910 }
911
912
913 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000914 // Support only foldable casts to/from pointers that can be eliminated by
915 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000916 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000917 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000918
Chris Lattner0c537da2006-12-12 05:18:19 +0000919 // We can emit the pointer value into this slot if the slot is an
920 // integer slot greater or equal to the size of the pointer.
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000921 if (TD->getTypeAllocSize(Ty) >= TD->getTypeAllocSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000922 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000923
924 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000925 EmitConstantValueOnly(Op);
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000926 APInt ptrMask = APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Ty));
Chris Lattner17f71652008-08-17 07:19:36 +0000927
928 SmallString<40> S;
929 ptrMask.toStringUnsigned(S);
930 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000931 break;
932 }
933 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000934 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000935 case Instruction::And:
936 case Instruction::Or:
937 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000938 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000939 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000940 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000941 switch (Opcode) {
942 case Instruction::Add:
943 O << " + ";
944 break;
945 case Instruction::Sub:
946 O << " - ";
947 break;
948 case Instruction::And:
949 O << " & ";
950 break;
951 case Instruction::Or:
952 O << " | ";
953 break;
954 case Instruction::Xor:
955 O << " ^ ";
956 break;
957 default:
958 break;
959 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000960 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000961 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000962 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000963 break;
964 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000965 llvm_unreachable("Unsupported operator!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000966 }
967 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000968 llvm_unreachable("Unknown constant value!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000969 }
970}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000971
Chris Lattner55a6d902005-11-10 18:06:33 +0000972/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000973/// the predicate isString is true.
974///
David Greenea31f96c2009-07-14 20:18:05 +0000975static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner55a6d902005-11-10 18:06:33 +0000976 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000977 assert(CVA->isString() && "Array is not string compatible!");
978
Dan Gohman6896901e2008-06-30 22:03:41 +0000979 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000980 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000981 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000982 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000983 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000984 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000985 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000986}
987
Jeff Cohen24a62a92006-05-02 01:16:28 +0000988/// EmitString - Emit a zero-byte-terminated string constant.
989///
990void AsmPrinter::EmitString(const ConstantArray *CVA) const {
991 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000992 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000993 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000994 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000995 printAsCString(O, CVA, NumElts-1);
996 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000997 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000998 printAsCString(O, CVA, NumElts);
999 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001000 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +00001001}
1002
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001003void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
1004 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001005 if (CVA->isString()) {
1006 EmitString(CVA);
1007 } else { // Not a string. Print the values in successive locations
1008 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001009 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001010 }
1011}
1012
1013void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
1014 const VectorType *PTy = CP->getType();
1015
1016 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
1017 EmitGlobalConstant(CP->getOperand(I));
1018}
1019
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001020void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1021 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001022 // Print the fields in successive locations. Pad to align if needed!
1023 const TargetData *TD = TM.getTargetData();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001024 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001025 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1026 uint64_t sizeSoFar = 0;
1027 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1028 const Constant* field = CVS->getOperand(i);
1029
1030 // Check if padding is needed and insert one or more 0s.
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001031 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001032 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1033 - cvsLayout->getElementOffset(i)) - fieldSize;
1034 sizeSoFar += fieldSize + padSize;
1035
1036 // Now print the actual field value.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001037 EmitGlobalConstant(field, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001038
1039 // Insert padding - this may include padding to increase the size of the
1040 // current field up to the ABI size (if the struct is not packed) as well
1041 // as padding to ensure that the next field starts at the right offset.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001042 EmitZeros(padSize, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001043 }
1044 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1045 "Layout of constant struct may be incorrect!");
1046}
1047
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001048void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1049 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001050 // FP Constants are printed as integer constants to avoid losing
1051 // precision...
1052 const TargetData *TD = TM.getTargetData();
1053 if (CFP->getType() == Type::DoubleTy) {
1054 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1055 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Chenga774a992009-03-24 00:17:40 +00001056 if (TAI->getData64bitsDirective(AddrSpace)) {
1057 O << TAI->getData64bitsDirective(AddrSpace) << i;
1058 if (VerboseAsm)
1059 O << '\t' << TAI->getCommentString() << " double value: " << Val;
1060 O << '\n';
1061 } else if (TD->isBigEndian()) {
1062 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1063 if (VerboseAsm)
1064 O << '\t' << TAI->getCommentString()
1065 << " double most significant word " << Val;
1066 O << '\n';
1067 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1068 if (VerboseAsm)
1069 O << '\t' << TAI->getCommentString()
1070 << " double least significant word " << Val;
1071 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001072 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001073 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1074 if (VerboseAsm)
1075 O << '\t' << TAI->getCommentString()
1076 << " double least significant word " << Val;
1077 O << '\n';
1078 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1079 if (VerboseAsm)
1080 O << '\t' << TAI->getCommentString()
1081 << " double most significant word " << Val;
1082 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001083 }
1084 return;
1085 } else if (CFP->getType() == Type::FloatTy) {
1086 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001087 O << TAI->getData32bitsDirective(AddrSpace)
Evan Chenga774a992009-03-24 00:17:40 +00001088 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1089 if (VerboseAsm)
1090 O << '\t' << TAI->getCommentString() << " float " << Val;
1091 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001092 return;
1093 } else if (CFP->getType() == Type::X86_FP80Ty) {
1094 // all long double variants are printed as hex
1095 // api needed to prevent premature destruction
1096 APInt api = CFP->getValueAPF().bitcastToAPInt();
1097 const uint64_t *p = api.getRawData();
1098 // Convert to double so we can print the approximate val as a comment.
1099 APFloat DoubleVal = CFP->getValueAPF();
1100 bool ignored;
1101 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1102 &ignored);
1103 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001104 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1105 if (VerboseAsm)
1106 O << '\t' << TAI->getCommentString()
1107 << " long double most significant halfword of ~"
1108 << DoubleVal.convertToDouble();
1109 O << '\n';
1110 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1111 if (VerboseAsm)
1112 O << '\t' << TAI->getCommentString() << " long double next halfword";
1113 O << '\n';
1114 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1115 if (VerboseAsm)
1116 O << '\t' << TAI->getCommentString() << " long double next halfword";
1117 O << '\n';
1118 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1119 if (VerboseAsm)
1120 O << '\t' << TAI->getCommentString() << " long double next halfword";
1121 O << '\n';
1122 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1123 if (VerboseAsm)
1124 O << '\t' << TAI->getCommentString()
1125 << " long double least significant halfword";
1126 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001127 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001128 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1129 if (VerboseAsm)
1130 O << '\t' << TAI->getCommentString()
1131 << " long double least significant halfword of ~"
1132 << DoubleVal.convertToDouble();
1133 O << '\n';
1134 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1135 if (VerboseAsm)
1136 O << '\t' << TAI->getCommentString()
1137 << " long double next halfword";
1138 O << '\n';
1139 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1140 if (VerboseAsm)
1141 O << '\t' << TAI->getCommentString()
1142 << " long double next halfword";
1143 O << '\n';
1144 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1145 if (VerboseAsm)
1146 O << '\t' << TAI->getCommentString()
1147 << " long double next halfword";
1148 O << '\n';
1149 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1150 if (VerboseAsm)
1151 O << '\t' << TAI->getCommentString()
1152 << " long double most significant halfword";
1153 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001154 }
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001155 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) -
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001156 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001157 return;
1158 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1159 // all long double variants are printed as hex
1160 // api needed to prevent premature destruction
1161 APInt api = CFP->getValueAPF().bitcastToAPInt();
1162 const uint64_t *p = api.getRawData();
1163 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001164 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1165 if (VerboseAsm)
1166 O << '\t' << TAI->getCommentString()
1167 << " long double most significant word";
1168 O << '\n';
1169 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1170 if (VerboseAsm)
1171 O << '\t' << TAI->getCommentString()
1172 << " long double next word";
1173 O << '\n';
1174 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1175 if (VerboseAsm)
1176 O << '\t' << TAI->getCommentString()
1177 << " long double next word";
1178 O << '\n';
1179 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1180 if (VerboseAsm)
1181 O << '\t' << TAI->getCommentString()
1182 << " long double least significant word";
1183 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001184 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001185 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1186 if (VerboseAsm)
1187 O << '\t' << TAI->getCommentString()
1188 << " long double least significant word";
1189 O << '\n';
1190 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1191 if (VerboseAsm)
1192 O << '\t' << TAI->getCommentString()
1193 << " long double next word";
1194 O << '\n';
1195 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1196 if (VerboseAsm)
1197 O << '\t' << TAI->getCommentString()
1198 << " long double next word";
1199 O << '\n';
1200 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1201 if (VerboseAsm)
1202 O << '\t' << TAI->getCommentString()
1203 << " long double most significant word";
1204 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001205 }
1206 return;
Torok Edwinfbcc6632009-07-14 16:55:14 +00001207 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman014caa42008-12-22 21:14:27 +00001208}
1209
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001210void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1211 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001212 const TargetData *TD = TM.getTargetData();
1213 unsigned BitWidth = CI->getBitWidth();
1214 assert(isPowerOf2_32(BitWidth) &&
1215 "Non-power-of-2-sized integers not handled!");
1216
1217 // We don't expect assemblers to support integer data directives
1218 // for more than 64 bits, so we emit the data in at most 64-bit
1219 // quantities at a time.
1220 const uint64_t *RawData = CI->getValue().getRawData();
1221 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1222 uint64_t Val;
1223 if (TD->isBigEndian())
1224 Val = RawData[e - i - 1];
1225 else
1226 Val = RawData[i];
1227
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001228 if (TAI->getData64bitsDirective(AddrSpace))
1229 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001230 else if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001231 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1232 if (VerboseAsm)
1233 O << '\t' << TAI->getCommentString()
1234 << " Double-word most significant word " << Val;
1235 O << '\n';
1236 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1237 if (VerboseAsm)
1238 O << '\t' << TAI->getCommentString()
1239 << " Double-word least significant word " << Val;
1240 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001241 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001242 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1243 if (VerboseAsm)
1244 O << '\t' << TAI->getCommentString()
1245 << " Double-word least significant word " << Val;
1246 O << '\n';
1247 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1248 if (VerboseAsm)
1249 O << '\t' << TAI->getCommentString()
1250 << " Double-word most significant word " << Val;
1251 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001252 }
1253 }
1254}
1255
Chris Lattnerbb644e32005-11-21 07:51:36 +00001256/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001257void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Anderson20a631f2006-05-03 01:29:57 +00001258 const TargetData *TD = TM.getTargetData();
Dan Gohman014caa42008-12-22 21:14:27 +00001259 const Type *type = CV->getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001260 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001261
Chris Lattner61753bf2004-10-16 18:19:26 +00001262 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001263 EmitZeros(Size, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001264 return;
1265 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001266 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001267 return;
1268 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001269 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001270 return;
1271 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001272 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001273 return;
1274 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1275 // Small integers are handled below; large integers are handled here.
1276 if (Size > 4) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001277 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001278 return;
1279 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001280 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman014caa42008-12-22 21:14:27 +00001281 EmitGlobalConstantVector(CP);
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001282 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001283 }
1284
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001285 printDataDirective(type, AddrSpace);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001286 EmitConstantValueOnly(CV);
Evan Chenga774a992009-03-24 00:17:40 +00001287 if (VerboseAsm) {
1288 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1289 SmallString<40> S;
1290 CI->getValue().toStringUnsigned(S, 16);
1291 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1292 }
Scott Michelc0e9ff62008-06-03 15:39:51 +00001293 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001294 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001295}
Chris Lattner061d9e22006-01-27 02:10:10 +00001296
Chris Lattner17f71652008-08-17 07:19:36 +00001297void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001298 // Target doesn't support this yet!
Torok Edwinfbcc6632009-07-14 16:55:14 +00001299 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001300}
1301
Chris Lattnera32814b2006-09-26 23:59:50 +00001302/// PrintSpecial - Print information related to the specified machine instr
1303/// that is independent of the operand, and may be independent of the instr
1304/// itself. This can be useful for portably encoding the comment character
1305/// or other bits of target-specific knowledge into the asmstrings. The
1306/// syntax used is ${:comment}. Targets can override this to add support
1307/// for their own strange codes.
Chris Lattner1522e242009-03-10 05:37:13 +00001308void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattner30b47082006-09-27 00:06:07 +00001309 if (!strcmp(Code, "private")) {
1310 O << TAI->getPrivateGlobalPrefix();
1311 } else if (!strcmp(Code, "comment")) {
Evan Chenga774a992009-03-24 00:17:40 +00001312 if (VerboseAsm)
1313 O << TAI->getCommentString();
Chris Lattnera32814b2006-09-26 23:59:50 +00001314 } else if (!strcmp(Code, "uid")) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001315 // Comparing the address of MI isn't sufficient, because machineinstrs may
1316 // be allocated to the same address across functions.
1317 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1318
Owen Andersonae471cf2009-06-24 22:28:12 +00001319 // If this is a new LastFn instruction, bump the counter.
1320 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001321 ++Counter;
1322 LastMI = MI;
Owen Andersonae471cf2009-06-24 22:28:12 +00001323 LastFn = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001324 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001325 O << Counter;
1326 } else {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001327 std::string msg;
1328 raw_string_ostream Msg(msg);
1329 Msg << "Unknown special formatter '" << Code
Bill Wendlingf3baad32006-12-07 01:30:32 +00001330 << "' for machine instr: " << *MI;
Torok Edwinccb29cd2009-07-11 13:10:19 +00001331 llvm_report_error(Msg.str());
Chris Lattnera32814b2006-09-26 23:59:50 +00001332 }
1333}
1334
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001335/// processDebugLoc - Processes the debug information of each machine
1336/// instruction's DebugLoc.
1337void AsmPrinter::processDebugLoc(DebugLoc DL) {
1338 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1339 if (!DL.isUnknown()) {
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001340 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1341
1342 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1343 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1344 DICompileUnit(CurDLT.CompileUnit)));
1345
1346 PrevDLT = CurDLT;
1347 }
1348 }
1349}
Chris Lattnera32814b2006-09-26 23:59:50 +00001350
Chris Lattner061d9e22006-01-27 02:10:10 +00001351/// printInlineAsm - This method formats and prints the specified machine
1352/// instruction that is an inline asm.
1353void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001354 unsigned NumOperands = MI->getNumOperands();
1355
1356 // Count the number of register definitions.
1357 unsigned NumDefs = 0;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001358 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001359 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001360 assert(NumDefs != NumOperands-1 && "No asm string?");
1361
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001362 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001363
1364 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001365 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001366
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001367 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1368 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001369 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001370 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001371 return;
1372 }
1373
Jim Laskeya6211dc2006-09-06 18:34:40 +00001374 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001375
Bill Wendlinge21237e2007-01-16 03:42:04 +00001376 // The variant of the current asmprinter.
1377 int AsmPrinterVariant = TAI->getAssemblerDialect();
1378
Chris Lattneraa23fa92006-02-01 22:41:11 +00001379 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1380 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001381
Chris Lattneraa23fa92006-02-01 22:41:11 +00001382 while (*LastEmitted) {
1383 switch (*LastEmitted) {
1384 default: {
1385 // Not a special case, emit the string section literally.
1386 const char *LiteralEnd = LastEmitted+1;
1387 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001388 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001389 ++LiteralEnd;
1390 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1391 O.write(LastEmitted, LiteralEnd-LastEmitted);
1392 LastEmitted = LiteralEnd;
1393 break;
1394 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001395 case '\n':
1396 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001397 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001398 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001399 case '$': {
1400 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001401 bool Done = true;
1402
1403 // Handle escapes.
1404 switch (*LastEmitted) {
1405 default: Done = false; break;
1406 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001407 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1408 O << '$';
1409 ++LastEmitted; // Consume second '$' character.
1410 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001411 case '(': // $( -> same as GCC's { character.
1412 ++LastEmitted; // Consume '(' character.
1413 if (CurVariant != -1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001414 llvm_report_error("Nested variants found in inline asm string: '"
1415 + std::string(AsmStr) + "'");
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001416 }
1417 CurVariant = 0; // We're in the first variant now.
1418 break;
1419 case '|':
1420 ++LastEmitted; // consume '|' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001421 if (CurVariant == -1)
1422 O << '|'; // this is gcc's behavior for | outside a variant
1423 else
1424 ++CurVariant; // We're in the next variant.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001425 break;
1426 case ')': // $) -> same as GCC's } char.
1427 ++LastEmitted; // consume ')' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001428 if (CurVariant == -1)
1429 O << '}'; // this is gcc's behavior for } outside a variant
1430 else
1431 CurVariant = -1;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001432 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001433 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001434 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001435
1436 bool HasCurlyBraces = false;
1437 if (*LastEmitted == '{') { // ${variable}
1438 ++LastEmitted; // Consume '{' character.
1439 HasCurlyBraces = true;
1440 }
1441
Chris Lattner1522e242009-03-10 05:37:13 +00001442 // If we have ${:foo}, then this is not a real operand reference, it is a
1443 // "magic" string reference, just like in .td files. Arrange to call
1444 // PrintSpecial.
1445 if (HasCurlyBraces && *LastEmitted == ':') {
1446 ++LastEmitted;
1447 const char *StrStart = LastEmitted;
1448 const char *StrEnd = strchr(StrStart, '}');
1449 if (StrEnd == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001450 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1451 + std::string(AsmStr) + "'");
Chris Lattner1522e242009-03-10 05:37:13 +00001452 }
1453
1454 std::string Val(StrStart, StrEnd);
1455 PrintSpecial(MI, Val.c_str());
1456 LastEmitted = StrEnd+1;
1457 break;
1458 }
1459
Chris Lattneraa23fa92006-02-01 22:41:11 +00001460 const char *IDStart = LastEmitted;
1461 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001462 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001463 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1464 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001465 llvm_report_error("Bad $ operand number in inline asm string: '"
1466 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001467 }
1468 LastEmitted = IDEnd;
1469
Chris Lattner34f74c12006-02-06 22:17:23 +00001470 char Modifier[2] = { 0, 0 };
1471
Chris Lattneraa23fa92006-02-01 22:41:11 +00001472 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001473 // If we have curly braces, check for a modifier character. This
1474 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1475 if (*LastEmitted == ':') {
1476 ++LastEmitted; // Consume ':' character.
1477 if (*LastEmitted == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001478 llvm_report_error("Bad ${:} expression in inline asm string: '"
1479 + std::string(AsmStr) + "'");
Chris Lattner34f74c12006-02-06 22:17:23 +00001480 }
1481
1482 Modifier[0] = *LastEmitted;
1483 ++LastEmitted; // Consume modifier character.
1484 }
1485
Chris Lattneraa23fa92006-02-01 22:41:11 +00001486 if (*LastEmitted != '}') {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001487 llvm_report_error("Bad ${} expression in inline asm string: '"
1488 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001489 }
1490 ++LastEmitted; // Consume '}' character.
1491 }
1492
1493 if ((unsigned)Val >= NumOperands-1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001494 llvm_report_error("Invalid $ operand number in inline asm string: '"
1495 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001496 }
1497
Chris Lattner571d9642006-02-23 19:21:04 +00001498 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001499 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001500 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1501 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001502
1503 bool Error = false;
1504
Chris Lattner571d9642006-02-23 19:21:04 +00001505 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001506 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001507 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001508 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng2e559232009-03-20 18:03:34 +00001509 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattner5af3fde2006-02-24 19:50:58 +00001510 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001511
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001512 if (OpNo >= MI->getNumOperands()) {
1513 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001514 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001515 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001516 ++OpNo; // Skip over the ID number.
1517
Dale Johannesen4646aa32007-11-05 21:20:28 +00001518 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001519 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001520 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001521 else {
1522 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesenc36660d2008-09-24 01:07:17 +00001523 if ((OpFlags & 7) == 4) {
Dale Johannesen4646aa32007-11-05 21:20:28 +00001524 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1525 Modifier[0] ? Modifier : 0);
1526 } else {
1527 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1528 Modifier[0] ? Modifier : 0);
1529 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001530 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001531 }
1532 if (Error) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001533 std::string msg;
1534 raw_string_ostream Msg(msg);
1535 Msg << "Invalid operand found in inline asm: '"
Bill Wendlingf3baad32006-12-07 01:30:32 +00001536 << AsmStr << "'\n";
Torok Edwinccb29cd2009-07-11 13:10:19 +00001537 MI->print(Msg);
1538 llvm_report_error(Msg.str());
Chris Lattneraa23fa92006-02-01 22:41:11 +00001539 }
Chris Lattner571d9642006-02-23 19:21:04 +00001540 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001541 break;
1542 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001543 }
1544 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001545 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001546}
1547
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001548/// printImplicitDef - This method prints the specified machine instruction
1549/// that is an implicit def.
1550void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Evan Chenga774a992009-03-24 00:17:40 +00001551 if (VerboseAsm)
1552 O << '\t' << TAI->getCommentString() << " implicit-def: "
1553 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001554}
1555
Jim Laskeyf9e54452007-01-26 14:34:52 +00001556/// printLabel - This method prints a local label used by debug and
1557/// exception handling tables.
1558void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001559 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001560}
1561
Evan Chengd6e44ab2008-02-01 09:10:45 +00001562void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001563 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001564}
1565
Evan Chengefd142a2008-02-02 04:07:54 +00001566/// printDeclare - This method prints a local variable declaration used by
1567/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001568/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1569/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001570void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Pateldd25a9d2009-01-13 21:44:10 +00001571 unsigned FI = MI->getOperand(0).getIndex();
Evan Cheng2cb90682008-02-04 23:06:48 +00001572 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel32d17a12009-04-15 00:10:26 +00001573 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Chengefd142a2008-02-02 04:07:54 +00001574}
1575
Chris Lattneraa23fa92006-02-01 22:41:11 +00001576/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1577/// instruction, using the specified assembler variant. Targets should
1578/// overried this to format as appropriate.
1579bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001580 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001581 // Target doesn't support this yet!
1582 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001583}
Chris Lattner1d08c652006-02-24 20:21:58 +00001584
1585bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1586 unsigned AsmVariant,
1587 const char *ExtraCode) {
1588 // Target doesn't support this yet!
1589 return true;
1590}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001591
1592/// printBasicBlockLabel - This method prints the label for the specified
1593/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001594void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001595 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001596 bool printColon,
1597 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001598 if (printAlign) {
1599 unsigned Align = MBB->getAlignment();
1600 if (Align)
1601 EmitAlignment(Log2_32(Align));
1602 }
1603
Dan Gohman6896901e2008-06-30 22:03:41 +00001604 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001605 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001606 if (printColon)
1607 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001608 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001609 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng53495222008-07-08 00:55:58 +00001610 << MBB->getBasicBlock()->getNameStart();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001611}
Nate Begeman984c1a42006-08-12 21:29:52 +00001612
Evan Cheng797d56f2007-11-09 01:32:10 +00001613/// printPICJumpTableSetLabel - This method prints a set label for the
1614/// specified MachineBasicBlock for a jumptable entry.
1615void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1616 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001617 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001618 return;
1619
Jim Laskeya6211dc2006-09-06 18:34:40 +00001620 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001621 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001622 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001623 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1624 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001625}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001626
Evan Cheng797d56f2007-11-09 01:32:10 +00001627void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1628 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001629 if (!TAI->getSetDirective())
1630 return;
1631
1632 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001633 << getFunctionNumber() << '_' << uid << '_' << uid2
1634 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001635 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001636 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1637 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001638}
1639
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001640/// printDataDirective - This method prints the asm directive for the
1641/// specified type.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001642void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001643 const TargetData *TD = TM.getTargetData();
1644 switch (type->getTypeID()) {
Chris Lattnerfe785582009-07-15 04:42:49 +00001645 case Type::FloatTyID: case Type::DoubleTyID:
1646 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1647 assert(0 && "Should have already output floating point constant.");
1648 default:
1649 assert(0 && "Can't handle printing this type of thing");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001650 case Type::IntegerTyID: {
1651 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1652 if (BitWidth <= 8)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001653 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001654 else if (BitWidth <= 16)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001655 O << TAI->getData16bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001656 else if (BitWidth <= 32)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001657 O << TAI->getData32bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001658 else if (BitWidth <= 64) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001659 assert(TAI->getData64bitsDirective(AddrSpace) &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001660 "Target cannot handle 64-bit constant exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001661 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohmanf9b20542008-09-08 16:40:13 +00001662 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001663 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001664 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001665 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001666 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001667 case Type::PointerTyID:
1668 if (TD->getPointerSize() == 8) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001669 assert(TAI->getData64bitsDirective(AddrSpace) &&
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001670 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001671 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001672 } else if (TD->getPointerSize() == 2) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001673 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001674 } else if (TD->getPointerSize() == 1) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001675 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001676 } else {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001677 O << TAI->getData32bitsDirective(AddrSpace);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001678 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001679 break;
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001680 }
1681}
Dale Johannesen915e1542007-02-16 01:54:53 +00001682
Anton Korobeynikoved473292008-08-08 18:25:07 +00001683void AsmPrinter::printVisibility(const std::string& Name,
1684 unsigned Visibility) const {
1685 if (Visibility == GlobalValue::HiddenVisibility) {
1686 if (const char *Directive = TAI->getHiddenDirective())
1687 O << Directive << Name << '\n';
1688 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1689 if (const char *Directive = TAI->getProtectedDirective())
1690 O << Directive << Name << '\n';
1691 }
1692}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001693
Anton Korobeynikovbff4b372008-11-22 16:15:34 +00001694void AsmPrinter::printOffset(int64_t Offset) const {
1695 if (Offset > 0)
1696 O << '+' << Offset;
1697 else if (Offset < 0)
1698 O << Offset;
1699}
1700
Gordon Henriksend930f912008-08-17 18:44:35 +00001701GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1702 if (!S->usesMetadata())
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001703 return 0;
1704
Gordon Henriksend930f912008-08-17 18:44:35 +00001705 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001706 if (GCPI != GCMetadataPrinters.end())
1707 return GCPI->second;
1708
Gordon Henriksend930f912008-08-17 18:44:35 +00001709 const char *Name = S->getName().c_str();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001710
1711 for (GCMetadataPrinterRegistry::iterator
1712 I = GCMetadataPrinterRegistry::begin(),
1713 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1714 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001715 GCMetadataPrinter *GMP = I->instantiate();
1716 GMP->S = S;
1717 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1718 return GMP;
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001719 }
1720
Gordon Henriksend930f912008-08-17 18:44:35 +00001721 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +00001722 llvm_unreachable(0);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001723}
David Greenede544782009-07-13 20:25:48 +00001724
1725/// EmitComments - Pretty-print comments for instructions
1726void AsmPrinter::EmitComments(const MachineInstr &MI) const
1727{
David Greene6e2eda12009-07-22 20:33:26 +00001728 if (VerboseAsm) {
1729 if (!MI.getDebugLoc().isUnknown()) {
1730 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greenede9cd442009-07-16 22:24:20 +00001731
David Greene6e2eda12009-07-22 20:33:26 +00001732 // Print source line info
1733 O.PadToColumn(TAI->getCommentColumn(), 1);
1734 O << TAI->getCommentString() << " SrcLine ";
1735 if (DLT.CompileUnit->hasInitializer()) {
1736 Constant *Name = DLT.CompileUnit->getInitializer();
1737 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1738 if (NameString->isString()) {
1739 O << NameString->getAsString() << " ";
1740 }
1741 }
1742 O << DLT.Line;
1743 if (DLT.Col != 0)
1744 O << ":" << DLT.Col;
1745 }
David Greenede9cd442009-07-16 22:24:20 +00001746 }
David Greenede544782009-07-13 20:25:48 +00001747}
1748
1749/// EmitComments - Pretty-print comments for instructions
1750void AsmPrinter::EmitComments(const MCInst &MI) const
1751{
David Greene6e2eda12009-07-22 20:33:26 +00001752 if (VerboseAsm) {
1753 if (!MI.getDebugLoc().isUnknown()) {
1754 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greenede9cd442009-07-16 22:24:20 +00001755
David Greene6e2eda12009-07-22 20:33:26 +00001756 // Print source line info
1757 O.PadToColumn(TAI->getCommentColumn(), 1);
1758 O << TAI->getCommentString() << " SrcLine ";
1759 if (DLT.CompileUnit->hasInitializer()) {
1760 Constant *Name = DLT.CompileUnit->getInitializer();
1761 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1762 if (NameString->isString()) {
1763 O << NameString->getAsString() << " ";
1764 }
1765 }
1766 O << DLT.Line;
1767 if (DLT.Col != 0)
1768 O << ":" << DLT.Col;
1769 }
David Greenede9cd442009-07-16 22:24:20 +00001770 }
David Greenede544782009-07-13 20:25:48 +00001771}