blob: 08eab53cf29c8c2b3bfac8b18257eec84625b980 [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"
David Greenea31f96c2009-07-14 20:18:05 +000030#include "llvm/Support/FormattedStream.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000032#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000033#include "llvm/Target/TargetLowering.h"
Evan Chengc963f6c2008-07-01 23:18:29 +000034#include "llvm/Target/TargetOptions.h"
Evan Cheng0e7b00d2008-03-15 00:03:38 +000035#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng797d56f2007-11-09 01:32:10 +000036#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner17f71652008-08-17 07:19:36 +000037#include "llvm/ADT/SmallString.h"
Owen Anderson93719642008-08-21 00:14:44 +000038#include "llvm/ADT/StringExtras.h"
Chris Lattneraa23fa92006-02-01 22:41:11 +000039#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000040using namespace llvm;
41
Evan Cheng5e5a63c2009-03-25 01:47:28 +000042static cl::opt<cl::boolOrDefault>
43AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
44 cl::init(cl::BOU_UNSET));
45
Devang Patel8c78a0b2007-05-03 01:11:54 +000046char AsmPrinter::ID = 0;
David Greenea31f96c2009-07-14 20:18:05 +000047AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbar75c12e12009-07-01 01:48:54 +000048 const TargetAsmInfo *T, bool VDef)
49 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Cheng0e7b00d2008-03-15 00:03:38 +000050 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Owen Andersone3849522009-06-25 16:55:32 +000051 IsInTextSection(false), LastMI(0), LastFn(0), Counter(~0U),
52 PrevDLT(0, ~0U, ~0U) {
Chris Lattner1fd58882009-06-24 19:09:55 +000053 DW = 0; MMI = 0;
Evan Cheng5e5a63c2009-03-25 01:47:28 +000054 switch (AsmVerbose) {
55 case cl::BOU_UNSET: VerboseAsm = VDef; break;
56 case cl::BOU_TRUE: VerboseAsm = true; break;
57 case cl::BOU_FALSE: VerboseAsm = false; break;
58 }
59}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000060
Gordon Henriksendbe06d32008-08-17 12:08:44 +000061AsmPrinter::~AsmPrinter() {
62 for (gcp_iterator I = GCMetadataPrinters.begin(),
63 E = GCMetadataPrinters.end(); I != E; ++I)
64 delete I->second;
65}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000066
Chris Lattner8488ba22006-05-09 04:59:56 +000067/// SwitchToTextSection - Switch to the specified text section of the executable
68/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000069///
Chris Lattner8488ba22006-05-09 04:59:56 +000070void AsmPrinter::SwitchToTextSection(const char *NewSection,
71 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000072 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000073 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000074 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000075 else
76 NS = NewSection;
77
78 // If we're already in this section, we're done.
79 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000080
Chris Lattner4ebc6a22006-05-09 05:33:48 +000081 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000082 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +000083 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Jeff Cohen470f4312006-05-02 03:58:45 +000084
Chris Lattner4ebc6a22006-05-09 05:33:48 +000085 CurrentSection = NS;
86
87 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000088 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +000089
90 IsInTextSection = true;
Chris Lattner2ea5c992005-11-21 07:06:27 +000091}
92
Nate Begeman78756502006-07-27 01:13:04 +000093/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000094/// if we are not already in it!
95///
96void AsmPrinter::SwitchToDataSection(const char *NewSection,
97 const GlobalValue *GV) {
98 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000099 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000100 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +0000101 else
102 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +0000103
Chris Lattner6341df82006-05-09 05:23:12 +0000104 // If we're already in this section, we're done.
105 if (CurrentSection == NS) return;
106
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000107 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000108 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman6896901e2008-06-30 22:03:41 +0000109 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000110
111 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +0000112
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000113 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000114 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Chengc7990652008-02-28 00:43:03 +0000115
116 IsInTextSection = false;
Chris Lattner8488ba22006-05-09 04:59:56 +0000117}
118
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000119/// SwitchToSection - Switch to the specified section of the executable if we
120/// are not already in it!
121void AsmPrinter::SwitchToSection(const Section* NS) {
122 const std::string& NewSection = NS->getName();
123
124 // If we're already in this section, we're done.
125 if (CurrentSection == NewSection) return;
126
127 // Close the current section, if applicable.
128 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
129 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
130
131 // FIXME: Make CurrentSection a Section* in the future
132 CurrentSection = NewSection;
Anton Korobeynikovf8dc8aa2008-09-24 22:15:21 +0000133 CurrentSection_ = NS;
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000134
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000135 if (!CurrentSection.empty()) {
136 // If section is named we need to switch into it via special '.section'
137 // directive and also append funky flags. Otherwise - section name is just
138 // some magic assembler directive.
139 if (NS->isNamed())
140 O << TAI->getSwitchToSectionDirective()
141 << CurrentSection
142 << TAI->getSectionFlags(NS->getFlags());
143 else
144 O << CurrentSection;
145 O << TAI->getDataSectionStartSuffix() << '\n';
146 }
Anton Korobeynikov7d002fa2008-09-24 22:12:10 +0000147
148 IsInTextSection = (NS->getFlags() & SectionFlags::Code);
149}
Chris Lattner8488ba22006-05-09 04:59:56 +0000150
Gordon Henriksen5180e852008-01-07 01:30:38 +0000151void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
152 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksend930f912008-08-17 18:44:35 +0000153 AU.addRequired<GCModuleInfo>();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000154}
155
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000156bool AsmPrinter::doInitialization(Module &M) {
Bill Wendlingdde1a8e2009-07-20 21:30:28 +0000157 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
Chris Lattner1177cee2009-07-21 17:30:51 +0000158 TAI->getLinkerPrivateGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000159
Chris Lattnerb8476452009-06-18 23:41:35 +0000160 if (TAI->doesAllowQuotesInName())
161 Mang->setUseQuotes(true);
162
Duncan Sands5a913d62009-01-28 13:14:17 +0000163 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000164 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindolacda011b2008-12-03 11:01:37 +0000165
166 if (TAI->hasSingleParameterDotFile()) {
167 /* Very minimal debug info. It is ignored if we emit actual
168 debug info. If we don't, this at helps the user find where
169 a function came from. */
170 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
171 }
172
Gordon Henriksend930f912008-08-17 18:44:35 +0000173 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
174 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
175 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000176
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000177 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000178 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000179 << M.getModuleInlineAsm()
Dan Gohman6896901e2008-06-30 22:03:41 +0000180 << '\n' << TAI->getCommentString()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000181 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000182
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000183 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000184
Chris Lattner1fd58882009-06-24 19:09:55 +0000185 if (TAI->doesSupportDebugInformation() ||
186 TAI->doesSupportExceptionHandling()) {
187 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
188 if (MMI)
Devang Patel33f4eb42009-06-19 21:54:26 +0000189 MMI->AnalyzeModule(M);
Chris Lattner1fd58882009-06-24 19:09:55 +0000190 DW = getAnalysisIfAvailable<DwarfWriter>();
191 if (DW)
192 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel33f4eb42009-06-19 21:54:26 +0000193 }
194
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000195 return false;
196}
197
198bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner70413122009-06-24 18:54:37 +0000199 // Emit final debug information.
200 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
201 DW->EndModule();
202
Chris Lattner2981dc12009-06-24 18:52:01 +0000203 // If the target wants to know about weak references, print them all.
Rafael Espindolad7998d02006-12-18 03:37:18 +0000204 if (TAI->getWeakRefDirective()) {
Chris Lattner2981dc12009-06-24 18:52:01 +0000205 // FIXME: This is not lazy, it would be nice to only print weak references
206 // to stuff that is actually used. Note that doing so would require targets
207 // to notice uses in operands (due to constant exprs etc). This should
208 // happen with the MC stuff eventually.
209 SwitchToDataSection("");
Rafael Espindolad7998d02006-12-18 03:37:18 +0000210
Chris Lattner2981dc12009-06-24 18:52:01 +0000211 // Print out module-level global variables here.
212 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
213 I != E; ++I) {
214 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000215 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000216 }
217
218 for (Module::const_iterator I = M.begin(), E = M.end();
219 I != E; ++I) {
220 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000221 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000222 }
Rafael Espindolad7998d02006-12-18 03:37:18 +0000223 }
224
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000225 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000226 if (!M.alias_empty())
Anton Korobeynikovf8dc8aa2008-09-24 22:15:21 +0000227 SwitchToSection(TAI->getTextSection());
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000228
Dan Gohman6896901e2008-06-30 22:03:41 +0000229 O << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000230 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner2981dc12009-06-24 18:52:01 +0000231 I != E; ++I) {
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000232 std::string Name = Mang->getMangledName(I);
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000233
234 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000235 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000236
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000237 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000238 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000239 else if (I->hasWeakLinkage())
Dan Gohman6896901e2008-06-30 22:03:41 +0000240 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindola6de96a12009-01-15 20:18:42 +0000241 else if (!I->hasLocalLinkage())
Torok Edwinfbcc6632009-07-14 16:55:14 +0000242 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000243
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000244 printVisibility(Name, I->getVisibility());
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000245
Dan Gohman6896901e2008-06-30 22:03:41 +0000246 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000247 }
248 }
249
Duncan Sands5a913d62009-01-28 13:14:17 +0000250 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000251 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
252 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
253 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
254 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000255
Dan Gohmanbcde1722008-05-05 00:28:39 +0000256 // If we don't have any trampolines, then we don't require stack memory
257 // to be executable. Some targets have a directive to declare this.
Chris Lattner2981dc12009-06-24 18:52:01 +0000258 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmanbcde1722008-05-05 00:28:39 +0000259 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
260 if (TAI->getNonexecutableStackDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000261 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmanbcde1722008-05-05 00:28:39 +0000262
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000263 delete Mang; Mang = 0;
Chris Lattner1fd58882009-06-24 19:09:55 +0000264 DW = 0; MMI = 0;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000265 return false;
266}
267
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000268std::string
269AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendling067f1d82007-09-18 01:47:22 +0000270 assert(MF && "No machine function?");
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000271 return Mang->getMangledName(MF->getFunction(), ".eh",
272 TAI->is_EHSymbolPrivate());
Bill Wendling067f1d82007-09-18 01:47:22 +0000273}
274
Chris Lattnerbb644e32005-11-21 07:51:36 +0000275void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000276 // What's my mangled name?
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000277 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000278 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000279}
280
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000281namespace {
282 // SectionCPs - Keep track the alignment, constpool entries per Section.
283 struct SectionCPs {
284 const Section *S;
285 unsigned Alignment;
286 SmallVector<unsigned, 4> CPEs;
287 SectionCPs(const Section *s, unsigned a) : S(s), Alignment(a) {};
288 };
289}
290
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000291/// EmitConstantPool - Print to the current output stream assembly
292/// representations of the constants in the constant pool MCP. This is
293/// used to print out constants which have been "spilled to memory" by
294/// the code generator.
295///
296void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000297 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000298 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000299
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000300 // Calculate sections for constant pool entries. We collect entries to go into
301 // the same section together to reduce amount of section switch statements.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000302 SmallVector<SectionCPs, 4> CPSections;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000303 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000304 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000305 unsigned Align = CPE.getAlignment();
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000306 const Section* S = TAI->SelectSectionForMachineConst(CPE.getType());
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000307 // The number of sections are small, just do a linear search from the
308 // last section to the first.
309 bool Found = false;
310 unsigned SecIdx = CPSections.size();
311 while (SecIdx != 0) {
312 if (CPSections[--SecIdx].S == S) {
313 Found = true;
314 break;
315 }
316 }
317 if (!Found) {
318 SecIdx = CPSections.size();
319 CPSections.push_back(SectionCPs(S, Align));
320 }
321
322 if (Align > CPSections[SecIdx].Alignment)
323 CPSections[SecIdx].Alignment = Align;
324 CPSections[SecIdx].CPEs.push_back(i);
Evan Chengec1d60b2006-06-29 00:26:09 +0000325 }
326
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000327 // Now print stuff into the calculated sections.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000328 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
329 SwitchToSection(CPSections[i].S);
330 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Chengec1d60b2006-06-29 00:26:09 +0000331
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000332 unsigned Offset = 0;
333 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
334 unsigned CPI = CPSections[i].CPEs[j];
335 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000336
Chris Lattnerf6190822006-02-09 04:46:04 +0000337 // Emit inter-object padding for alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000338 unsigned AlignMask = CPE.getAlignment() - 1;
339 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
340 EmitZeros(NewOffset - Offset);
341
342 const Type *Ty = CPE.getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000343 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000344
345 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
346 << CPI << ":\t\t\t\t\t";
347 if (VerboseAsm) {
348 O << TAI->getCommentString() << ' ';
349 WriteTypeSymbolic(O, CPE.getType(), 0);
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000350 }
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000351 O << '\n';
352 if (CPE.isMachineConstantPoolEntry())
353 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
354 else
355 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000356 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000357 }
358}
359
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000360/// EmitJumpTableInfo - Print assembly representations of the jump tables used
361/// by the current function to the current output stream.
362///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000363void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
364 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000365 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
366 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000367
Jim Laskey70323a82006-12-14 19:17:33 +0000368 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000369
Nate Begeman78756502006-07-27 01:13:04 +0000370 // Pick the directive to use to print the jump table entries, and switch to
371 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000372 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000373
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000374 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
375 const Function *F = MF.getFunction();
376 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
Evan Chengde9e36a2009-06-18 20:37:15 +0000377 bool JTInDiffSection = false;
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000378 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Evan Chengde9e36a2009-06-18 20:37:15 +0000379 !JumpTableDataSection ||
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000380 SectionFlags & SectionFlags::Linkonce) {
Jim Laskey70323a82006-12-14 19:17:33 +0000381 // In PIC mode, we need to emit the jump table to the same section as the
382 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000383 // We should also do if the section name is NULL or function is declared in
384 // discardable section.
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000385 SwitchToSection(TAI->SectionForGlobal(F));
Nate Begeman78756502006-07-27 01:13:04 +0000386 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000387 SwitchToDataSection(JumpTableDataSection);
Evan Chengde9e36a2009-06-18 20:37:15 +0000388 JTInDiffSection = true;
Nate Begeman78756502006-07-27 01:13:04 +0000389 }
Jim Laskey70323a82006-12-14 19:17:33 +0000390
391 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000392
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000393 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000394 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000395
396 // If this jump table was deleted, ignore it.
397 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000398
399 // For PIC codegen, if possible we want to use the SetDirective to reduce
400 // the number of relocations the assembler will generate for the jump table.
401 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000402 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000403 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000404 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000405 if (EmittedSets.insert(JTBBs[ii]))
406 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000407
Chris Lattner0ee2d462007-01-18 01:12:56 +0000408 // On some targets (e.g. darwin) we want to emit two consequtive labels
409 // before each jump table. The first label is never referenced, but tells
410 // the assembler and linker the extents of the jump table object. The
411 // second label is actually referenced by the code.
Evan Chengde9e36a2009-06-18 20:37:15 +0000412 if (JTInDiffSection) {
413 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
414 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
415 }
Chris Lattner0ee2d462007-01-18 01:12:56 +0000416
Evan Chengcdf36092007-10-14 05:57:21 +0000417 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
418 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000419
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000420 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000421 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000422 O << '\n';
423 }
424 }
425}
426
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000427void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
428 const MachineBasicBlock *MBB,
429 unsigned uid) const {
430 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
431
432 // Use JumpTableDirective otherwise honor the entry size from the jump table
433 // info.
434 const char *JTEntryDirective = TAI->getJumpTableDirective();
435 bool HadJTEntryDirective = JTEntryDirective != NULL;
436 if (!HadJTEntryDirective) {
437 JTEntryDirective = MJTI->getEntrySize() == 4 ?
438 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
439 }
440
441 O << JTEntryDirective << ' ';
442
443 // If we have emitted set directives for the jump table entries, print
444 // them rather than the entries themselves. If we're emitting PIC, then
445 // emit the table entries as differences between two text section labels.
446 // If we're emitting non-PIC code, then emit the entries as direct
447 // references to the target basic blocks.
448 if (IsPic) {
449 if (TAI->getSetDirective()) {
450 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
451 << '_' << uid << "_set_" << MBB->getNumber();
452 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000453 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000454 // If the arch uses custom Jump Table directives, don't calc relative to
455 // JT
456 if (!HadJTEntryDirective)
457 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
458 << getFunctionNumber() << '_' << uid;
459 }
460 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000461 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000462 }
463}
464
465
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000466/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
467/// special global used by LLVM. If so, emit it and return true, otherwise
468/// do nothing and return false.
469bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Chris Lattner58f9bb22009-07-20 06:14:25 +0000470 if (GV->isName("llvm.used")) {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000471 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
472 EmitLLVMUsedList(GV->getInitializer());
473 return true;
474 }
475
Chris Lattner58f9bb22009-07-20 06:14:25 +0000476 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner184f1be2009-04-13 05:44:34 +0000477 if (GV->getSection() == "llvm.metadata" ||
478 GV->hasAvailableExternallyLinkage())
479 return true;
Jim Laskey313570f2006-03-07 22:00:35 +0000480
481 if (!GV->hasAppendingLinkage()) return false;
482
483 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000484
Evan Cheng53ce7de2007-06-04 20:39:18 +0000485 const TargetData *TD = TM.getTargetData();
486 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner317293b2009-03-09 05:52:15 +0000487 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner126dab22009-03-09 08:18:48 +0000488 SwitchToDataSection(TAI->getStaticCtorsSection());
489 EmitAlignment(Align, 0);
490 EmitXXStructorList(GV->getInitializer());
491 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000492 }
493
Chris Lattner317293b2009-03-09 05:52:15 +0000494 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner126dab22009-03-09 08:18:48 +0000495 SwitchToDataSection(TAI->getStaticDtorsSection());
496 EmitAlignment(Align, 0);
497 EmitXXStructorList(GV->getInitializer());
498 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000499 }
500
501 return false;
502}
503
Chris Lattner66af3902006-09-26 03:38:18 +0000504/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesenabb1e772008-09-09 22:29:13 +0000505/// global in the specified llvm.used list for which emitUsedDirectiveFor
506/// is true, as being used with this directive.
Chris Lattner66af3902006-09-26 03:38:18 +0000507void AsmPrinter::EmitLLVMUsedList(Constant *List) {
508 const char *Directive = TAI->getUsedDirective();
509
Dan Gohman4fe64de2009-06-14 23:30:43 +0000510 // Should be an array of 'i8*'.
Chris Lattner66af3902006-09-26 03:38:18 +0000511 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
512 if (InitList == 0) return;
513
514 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner1fcac9f2009-07-17 22:00:23 +0000515 const GlobalValue *GV =
516 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
517 if (GV && TAI->emitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000518 O << Directive;
519 EmitConstantValueOnly(InitList->getOperand(i));
520 O << '\n';
521 }
Chris Lattner66af3902006-09-26 03:38:18 +0000522 }
523}
524
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000525/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
526/// function pointers, ignoring the init priority.
527void AsmPrinter::EmitXXStructorList(Constant *List) {
528 // Should be an array of '{ int, void ()* }' structs. The first value is the
529 // init priority, which we ignore.
530 if (!isa<ConstantArray>(List)) return;
531 ConstantArray *InitList = cast<ConstantArray>(List);
532 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
533 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
534 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000535
536 if (CS->getOperand(1)->isNullValue())
537 return; // Found a null terminator, exit printing.
538 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000539 EmitGlobalConstant(CS->getOperand(1));
540 }
541}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000542
Jim Laskey71262542006-10-17 13:41:07 +0000543/// getGlobalLinkName - Returns the asm/link name of of the specified
544/// global variable. Should be overridden by each target asm printer to
545/// generate the appropriate value.
Bill Wendling992f8462009-04-10 00:12:49 +0000546const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
547 std::string &LinkName) const {
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000548 if (isa<Function>(GV)) {
549 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000550 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000551 LinkName += TAI->getFunctionAddrSuffix();
552 } else {
553 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000554 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000555 LinkName += TAI->getGlobalVarAddrSuffix();
556 }
557
Jim Laskeyd24b9132006-10-17 17:17:24 +0000558 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000559}
560
Jim Laskey18fc0972007-02-21 22:47:38 +0000561/// EmitExternalGlobal - Emit the external reference to a global variable.
562/// Should be overridden if an indirect reference should be used.
563void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling992f8462009-04-10 00:12:49 +0000564 std::string GLN;
565 O << getGlobalLinkName(GV, GLN);
Jim Laskey18fc0972007-02-21 22:47:38 +0000566}
567
568
569
Jim Laskey1c055e82007-01-25 15:12:02 +0000570//===----------------------------------------------------------------------===//
571/// LEB 128 number encoding.
572
573/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
574/// representing an unsigned leb128 value.
575void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner09487cd2008-11-10 04:35:24 +0000576 char Buffer[20];
Jim Laskey1c055e82007-01-25 15:12:02 +0000577 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000578 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000579 Value >>= 7;
580 if (Value) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000581 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000582 if (Value) O << ", ";
583 } while (Value);
584}
585
Jim Laskey1c055e82007-01-25 15:12:02 +0000586/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
587/// representing a signed leb128 value.
588void AsmPrinter::PrintSLEB128(int Value) const {
589 int Sign = Value >> (8 * sizeof(Value) - 1);
590 bool IsMore;
Chris Lattner09487cd2008-11-10 04:35:24 +0000591 char Buffer[20];
Anton Korobeynikovbd890b12008-08-16 12:57:46 +0000592
Jim Laskey1c055e82007-01-25 15:12:02 +0000593 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000594 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000595 Value >>= 7;
596 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
597 if (IsMore) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000598 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000599 if (IsMore) O << ", ";
600 } while (IsMore);
601}
602
Jim Laskey1c055e82007-01-25 15:12:02 +0000603//===--------------------------------------------------------------------===//
604// Emission and print routines
605//
606
607/// PrintHex - Print a value as a hexidecimal value.
608///
609void AsmPrinter::PrintHex(int Value) const {
Chris Lattner5505eed2008-11-10 04:30:26 +0000610 char Buffer[20];
Chris Lattner5505eed2008-11-10 04:30:26 +0000611 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000612}
613
614/// EOL - Print a newline character to asm stream. If a comment is present
615/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000616void AsmPrinter::EOL() const {
Dan Gohman6896901e2008-06-30 22:03:41 +0000617 O << '\n';
Jim Laskey18fc0972007-02-21 22:47:38 +0000618}
Owen Anderson1d952532008-07-01 21:16:27 +0000619
Jim Laskey1c055e82007-01-25 15:12:02 +0000620void AsmPrinter::EOL(const std::string &Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000621 if (VerboseAsm && !Comment.empty()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000622 O << '\t'
Jim Laskey1c055e82007-01-25 15:12:02 +0000623 << TAI->getCommentString()
Dan Gohman6896901e2008-06-30 22:03:41 +0000624 << ' '
Jim Laskey1c055e82007-01-25 15:12:02 +0000625 << Comment;
626 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000627 O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000628}
629
Owen Anderson1d952532008-07-01 21:16:27 +0000630void AsmPrinter::EOL(const char* Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000631 if (VerboseAsm && *Comment) {
Owen Anderson1d952532008-07-01 21:16:27 +0000632 O << '\t'
633 << TAI->getCommentString()
634 << ' '
635 << Comment;
636 }
637 O << '\n';
638}
639
Jim Laskey1c055e82007-01-25 15:12:02 +0000640/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
641/// unsigned leb128 value.
642void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
643 if (TAI->hasLEB128()) {
644 O << "\t.uleb128\t"
645 << Value;
646 } else {
647 O << TAI->getData8bitsDirective();
648 PrintULEB128(Value);
649 }
650}
651
652/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
653/// signed leb128 value.
654void AsmPrinter::EmitSLEB128Bytes(int Value) const {
655 if (TAI->hasLEB128()) {
656 O << "\t.sleb128\t"
657 << Value;
658 } else {
659 O << TAI->getData8bitsDirective();
660 PrintSLEB128(Value);
661 }
662}
663
664/// EmitInt8 - Emit a byte directive and value.
665///
666void AsmPrinter::EmitInt8(int Value) const {
667 O << TAI->getData8bitsDirective();
668 PrintHex(Value & 0xFF);
669}
670
671/// EmitInt16 - Emit a short directive and value.
672///
673void AsmPrinter::EmitInt16(int Value) const {
674 O << TAI->getData16bitsDirective();
675 PrintHex(Value & 0xFFFF);
676}
677
678/// EmitInt32 - Emit a long directive and value.
679///
680void AsmPrinter::EmitInt32(int Value) const {
681 O << TAI->getData32bitsDirective();
682 PrintHex(Value);
683}
684
685/// EmitInt64 - Emit a long long directive and value.
686///
687void AsmPrinter::EmitInt64(uint64_t Value) const {
688 if (TAI->getData64bitsDirective()) {
689 O << TAI->getData64bitsDirective();
690 PrintHex(Value);
691 } else {
692 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000693 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000694 EmitInt32(unsigned(Value));
695 } else {
Dan Gohman6896901e2008-06-30 22:03:41 +0000696 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000697 EmitInt32(unsigned(Value >> 32));
698 }
699 }
700}
701
702/// toOctal - Convert the low order bits of X into an octal digit.
703///
704static inline char toOctal(int X) {
705 return (X&7)+'0';
706}
707
708/// printStringChar - Print a char, escaped if necessary.
709///
David Greenea31f96c2009-07-14 20:18:05 +0000710static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000711 if (C == '"') {
712 O << "\\\"";
713 } else if (C == '\\') {
714 O << "\\\\";
Chris Lattner1f9053a2009-01-22 23:38:45 +0000715 } else if (isprint((unsigned char)C)) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000716 O << C;
717 } else {
718 switch(C) {
719 case '\b': O << "\\b"; break;
720 case '\f': O << "\\f"; break;
721 case '\n': O << "\\n"; break;
722 case '\r': O << "\\r"; break;
723 case '\t': O << "\\t"; break;
724 default:
725 O << '\\';
726 O << toOctal(C >> 6);
727 O << toOctal(C >> 3);
728 O << toOctal(C >> 0);
729 break;
730 }
731 }
732}
733
734/// EmitString - Emit a string with quotes and a null terminator.
735/// Special characters are emitted properly.
736/// \literal (Eg. '\t') \endliteral
737void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling16abfc92009-04-09 23:51:31 +0000738 EmitString(String.c_str(), String.size());
739}
740
741void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000742 const char* AscizDirective = TAI->getAscizDirective();
743 if (AscizDirective)
744 O << AscizDirective;
745 else
746 O << TAI->getAsciiDirective();
Dan Gohman6896901e2008-06-30 22:03:41 +0000747 O << '\"';
Bill Wendling16abfc92009-04-09 23:51:31 +0000748 for (unsigned i = 0; i < Size; ++i)
Chris Lattner69b586e2009-04-08 00:28:38 +0000749 printStringChar(O, String[i]);
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000750 if (AscizDirective)
Dan Gohman6896901e2008-06-30 22:03:41 +0000751 O << '\"';
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000752 else
753 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000754}
755
756
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000757/// EmitFile - Emit a .file directive.
758void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
759 O << "\t.file\t" << Number << " \"";
Chris Lattner69b586e2009-04-08 00:28:38 +0000760 for (unsigned i = 0, N = Name.size(); i < N; ++i)
761 printStringChar(O, Name[i]);
Dan Gohman6896901e2008-06-30 22:03:41 +0000762 O << '\"';
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000763}
764
765
Jim Laskey1c055e82007-01-25 15:12:02 +0000766//===----------------------------------------------------------------------===//
767
Chris Lattner3e3ff302007-05-31 18:57:45 +0000768// EmitAlignment - Emit an alignment directive to the specified power of
769// two boundary. For example, if you pass in 3 here, you will get an 8
770// byte alignment. If a global value is specified, and if that global has
771// an explicit alignment requested, it will unconditionally override the
772// alignment request. However, if ForcedAlignBits is specified, this value
773// has final say: the ultimate alignment will be the max of ForcedAlignBits
774// and the alignment computed with NumBits and the global.
775//
776// The algorithm is:
777// Align = NumBits;
778// if (GV && GV->hasalignment) Align = GV->getalignment();
779// Align = std::max(Align, ForcedAlignBits);
780//
781void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000782 unsigned ForcedAlignBits,
783 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000784 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000785 NumBits = Log2_32(GV->getAlignment());
786 NumBits = std::max(NumBits, ForcedAlignBits);
787
Chris Lattner747960d2005-11-10 18:09:27 +0000788 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000789 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000790 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000791
792 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng26edb592008-02-29 19:36:59 +0000793 UseFillExpr &= IsInTextSection && FillValue;
Chris Lattner09487cd2008-11-10 04:35:24 +0000794 if (UseFillExpr) {
795 O << ',';
796 PrintHex(FillValue);
797 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000798 O << '\n';
Chris Lattner1d35c162004-08-17 19:14:29 +0000799}
800
Jim Laskey18fc0972007-02-21 22:47:38 +0000801
Chris Lattnerbb644e32005-11-21 07:51:36 +0000802/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000803///
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000804void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000805 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000806 if (TAI->getZeroDirective()) {
807 O << TAI->getZeroDirective() << NumZeros;
808 if (TAI->getZeroDirectiveSuffix())
809 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000810 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000811 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000812 for (; NumZeros; --NumZeros)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000813 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000814 }
815 }
816}
817
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000818// Print out the specified constant, without a storage class. Only the
819// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000820void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000821 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000822 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000823 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000824 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000825 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000826 // This is a constant address for a global variable or function. Use the
827 // name of the variable or function as the address value, possibly
828 // decorating it with GlobalVarAddrPrefix/Suffix or
829 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000830 if (isa<Function>(GV)) {
831 O << TAI->getFunctionAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000832 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000833 << TAI->getFunctionAddrSuffix();
834 } else {
835 O << TAI->getGlobalVarAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000836 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000837 << TAI->getGlobalVarAddrSuffix();
838 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000839 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000840 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000841 unsigned Opcode = CE->getOpcode();
842 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000843 case Instruction::GetElementPtr: {
844 // generate a symbolic expression for the byte address
845 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000846 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
847 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
848 idxVec.size())) {
Chris Lattner7e1d2862009-02-05 06:55:21 +0000849 // Truncate/sext the offset to the pointer size.
850 if (TD->getPointerSizeInBits() != 64) {
851 int SExtAmount = 64-TD->getPointerSizeInBits();
852 Offset = (Offset << SExtAmount) >> SExtAmount;
853 }
854
Chris Lattner145569b2005-02-14 21:40:26 +0000855 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000856 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000857 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000858 if (Offset > 0)
859 O << ") + " << Offset;
860 else if (Offset < 0)
861 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000862 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000863 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000864 }
865 break;
866 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000867 case Instruction::Trunc:
868 case Instruction::ZExt:
869 case Instruction::SExt:
870 case Instruction::FPTrunc:
871 case Instruction::FPExt:
872 case Instruction::UIToFP:
873 case Instruction::SIToFP:
874 case Instruction::FPToUI:
875 case Instruction::FPToSI:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000876 llvm_unreachable("FIXME: Don't yet support this kind of constant cast expr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000877 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000878 case Instruction::BitCast:
879 return EmitConstantValueOnly(CE->getOperand(0));
880
Chris Lattner0c537da2006-12-12 05:18:19 +0000881 case Instruction::IntToPtr: {
882 // Handle casts to pointers by changing them into casts to the appropriate
883 // integer type. This promotes constant folding and simplifies this code.
884 Constant *Op = CE->getOperand(0);
885 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
886 return EmitConstantValueOnly(Op);
887 }
888
889
890 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000891 // Support only foldable casts to/from pointers that can be eliminated by
892 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000893 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000894 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000895
Chris Lattner0c537da2006-12-12 05:18:19 +0000896 // We can emit the pointer value into this slot if the slot is an
897 // integer slot greater or equal to the size of the pointer.
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000898 if (TD->getTypeAllocSize(Ty) >= TD->getTypeAllocSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000899 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000900
901 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000902 EmitConstantValueOnly(Op);
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000903 APInt ptrMask = APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Ty));
Chris Lattner17f71652008-08-17 07:19:36 +0000904
905 SmallString<40> S;
906 ptrMask.toStringUnsigned(S);
907 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000908 break;
909 }
910 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000911 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000912 case Instruction::And:
913 case Instruction::Or:
914 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000915 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000916 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000917 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000918 switch (Opcode) {
919 case Instruction::Add:
920 O << " + ";
921 break;
922 case Instruction::Sub:
923 O << " - ";
924 break;
925 case Instruction::And:
926 O << " & ";
927 break;
928 case Instruction::Or:
929 O << " | ";
930 break;
931 case Instruction::Xor:
932 O << " ^ ";
933 break;
934 default:
935 break;
936 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000937 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000938 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000939 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000940 break;
941 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000942 llvm_unreachable("Unsupported operator!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000943 }
944 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000945 llvm_unreachable("Unknown constant value!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000946 }
947}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000948
Chris Lattner55a6d902005-11-10 18:06:33 +0000949/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000950/// the predicate isString is true.
951///
David Greenea31f96c2009-07-14 20:18:05 +0000952static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner55a6d902005-11-10 18:06:33 +0000953 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000954 assert(CVA->isString() && "Array is not string compatible!");
955
Dan Gohman6896901e2008-06-30 22:03:41 +0000956 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000957 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000958 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000959 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000960 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000961 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000962 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000963}
964
Jeff Cohen24a62a92006-05-02 01:16:28 +0000965/// EmitString - Emit a zero-byte-terminated string constant.
966///
967void AsmPrinter::EmitString(const ConstantArray *CVA) const {
968 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000969 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000970 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000971 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000972 printAsCString(O, CVA, NumElts-1);
973 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000974 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000975 printAsCString(O, CVA, NumElts);
976 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000977 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +0000978}
979
Sanjiv Gupta99b1b272009-04-28 16:34:20 +0000980void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
981 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +0000982 if (CVA->isString()) {
983 EmitString(CVA);
984 } else { // Not a string. Print the values in successive locations
985 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Gupta99b1b272009-04-28 16:34:20 +0000986 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +0000987 }
988}
989
990void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
991 const VectorType *PTy = CP->getType();
992
993 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
994 EmitGlobalConstant(CP->getOperand(I));
995}
996
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000997void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
998 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +0000999 // Print the fields in successive locations. Pad to align if needed!
1000 const TargetData *TD = TM.getTargetData();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001001 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001002 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1003 uint64_t sizeSoFar = 0;
1004 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1005 const Constant* field = CVS->getOperand(i);
1006
1007 // Check if padding is needed and insert one or more 0s.
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001008 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001009 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1010 - cvsLayout->getElementOffset(i)) - fieldSize;
1011 sizeSoFar += fieldSize + padSize;
1012
1013 // Now print the actual field value.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001014 EmitGlobalConstant(field, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001015
1016 // Insert padding - this may include padding to increase the size of the
1017 // current field up to the ABI size (if the struct is not packed) as well
1018 // as padding to ensure that the next field starts at the right offset.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001019 EmitZeros(padSize, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001020 }
1021 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1022 "Layout of constant struct may be incorrect!");
1023}
1024
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001025void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1026 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001027 // FP Constants are printed as integer constants to avoid losing
1028 // precision...
1029 const TargetData *TD = TM.getTargetData();
1030 if (CFP->getType() == Type::DoubleTy) {
1031 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1032 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Chenga774a992009-03-24 00:17:40 +00001033 if (TAI->getData64bitsDirective(AddrSpace)) {
1034 O << TAI->getData64bitsDirective(AddrSpace) << i;
1035 if (VerboseAsm)
1036 O << '\t' << TAI->getCommentString() << " double value: " << Val;
1037 O << '\n';
1038 } else if (TD->isBigEndian()) {
1039 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1040 if (VerboseAsm)
1041 O << '\t' << TAI->getCommentString()
1042 << " double most significant word " << Val;
1043 O << '\n';
1044 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1045 if (VerboseAsm)
1046 O << '\t' << TAI->getCommentString()
1047 << " double least significant word " << Val;
1048 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001049 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001050 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1051 if (VerboseAsm)
1052 O << '\t' << TAI->getCommentString()
1053 << " double least significant word " << Val;
1054 O << '\n';
1055 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1056 if (VerboseAsm)
1057 O << '\t' << TAI->getCommentString()
1058 << " double most significant word " << Val;
1059 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001060 }
1061 return;
1062 } else if (CFP->getType() == Type::FloatTy) {
1063 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001064 O << TAI->getData32bitsDirective(AddrSpace)
Evan Chenga774a992009-03-24 00:17:40 +00001065 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1066 if (VerboseAsm)
1067 O << '\t' << TAI->getCommentString() << " float " << Val;
1068 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001069 return;
1070 } else if (CFP->getType() == Type::X86_FP80Ty) {
1071 // all long double variants are printed as hex
1072 // api needed to prevent premature destruction
1073 APInt api = CFP->getValueAPF().bitcastToAPInt();
1074 const uint64_t *p = api.getRawData();
1075 // Convert to double so we can print the approximate val as a comment.
1076 APFloat DoubleVal = CFP->getValueAPF();
1077 bool ignored;
1078 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1079 &ignored);
1080 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001081 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1082 if (VerboseAsm)
1083 O << '\t' << TAI->getCommentString()
1084 << " long double most significant halfword of ~"
1085 << DoubleVal.convertToDouble();
1086 O << '\n';
1087 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1088 if (VerboseAsm)
1089 O << '\t' << TAI->getCommentString() << " long double next halfword";
1090 O << '\n';
1091 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1092 if (VerboseAsm)
1093 O << '\t' << TAI->getCommentString() << " long double next halfword";
1094 O << '\n';
1095 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1096 if (VerboseAsm)
1097 O << '\t' << TAI->getCommentString() << " long double next halfword";
1098 O << '\n';
1099 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1100 if (VerboseAsm)
1101 O << '\t' << TAI->getCommentString()
1102 << " long double least significant halfword";
1103 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001104 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001105 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1106 if (VerboseAsm)
1107 O << '\t' << TAI->getCommentString()
1108 << " long double least significant halfword of ~"
1109 << DoubleVal.convertToDouble();
1110 O << '\n';
1111 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1112 if (VerboseAsm)
1113 O << '\t' << TAI->getCommentString()
1114 << " long double next halfword";
1115 O << '\n';
1116 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1117 if (VerboseAsm)
1118 O << '\t' << TAI->getCommentString()
1119 << " long double next halfword";
1120 O << '\n';
1121 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1122 if (VerboseAsm)
1123 O << '\t' << TAI->getCommentString()
1124 << " long double next halfword";
1125 O << '\n';
1126 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1127 if (VerboseAsm)
1128 O << '\t' << TAI->getCommentString()
1129 << " long double most significant halfword";
1130 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001131 }
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001132 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) -
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001133 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001134 return;
1135 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1136 // all long double variants are printed as hex
1137 // api needed to prevent premature destruction
1138 APInt api = CFP->getValueAPF().bitcastToAPInt();
1139 const uint64_t *p = api.getRawData();
1140 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001141 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1142 if (VerboseAsm)
1143 O << '\t' << TAI->getCommentString()
1144 << " long double most significant word";
1145 O << '\n';
1146 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1147 if (VerboseAsm)
1148 O << '\t' << TAI->getCommentString()
1149 << " long double next word";
1150 O << '\n';
1151 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1152 if (VerboseAsm)
1153 O << '\t' << TAI->getCommentString()
1154 << " long double next word";
1155 O << '\n';
1156 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1157 if (VerboseAsm)
1158 O << '\t' << TAI->getCommentString()
1159 << " long double least significant word";
1160 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001161 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001162 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1163 if (VerboseAsm)
1164 O << '\t' << TAI->getCommentString()
1165 << " long double least significant word";
1166 O << '\n';
1167 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1168 if (VerboseAsm)
1169 O << '\t' << TAI->getCommentString()
1170 << " long double next word";
1171 O << '\n';
1172 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1173 if (VerboseAsm)
1174 O << '\t' << TAI->getCommentString()
1175 << " long double next word";
1176 O << '\n';
1177 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1178 if (VerboseAsm)
1179 O << '\t' << TAI->getCommentString()
1180 << " long double most significant word";
1181 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001182 }
1183 return;
Torok Edwinfbcc6632009-07-14 16:55:14 +00001184 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman014caa42008-12-22 21:14:27 +00001185}
1186
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001187void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1188 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001189 const TargetData *TD = TM.getTargetData();
1190 unsigned BitWidth = CI->getBitWidth();
1191 assert(isPowerOf2_32(BitWidth) &&
1192 "Non-power-of-2-sized integers not handled!");
1193
1194 // We don't expect assemblers to support integer data directives
1195 // for more than 64 bits, so we emit the data in at most 64-bit
1196 // quantities at a time.
1197 const uint64_t *RawData = CI->getValue().getRawData();
1198 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1199 uint64_t Val;
1200 if (TD->isBigEndian())
1201 Val = RawData[e - i - 1];
1202 else
1203 Val = RawData[i];
1204
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001205 if (TAI->getData64bitsDirective(AddrSpace))
1206 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001207 else if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001208 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1209 if (VerboseAsm)
1210 O << '\t' << TAI->getCommentString()
1211 << " Double-word most significant word " << Val;
1212 O << '\n';
1213 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1214 if (VerboseAsm)
1215 O << '\t' << TAI->getCommentString()
1216 << " Double-word least significant word " << Val;
1217 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001218 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001219 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1220 if (VerboseAsm)
1221 O << '\t' << TAI->getCommentString()
1222 << " Double-word least significant word " << Val;
1223 O << '\n';
1224 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1225 if (VerboseAsm)
1226 O << '\t' << TAI->getCommentString()
1227 << " Double-word most significant word " << Val;
1228 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001229 }
1230 }
1231}
1232
Chris Lattnerbb644e32005-11-21 07:51:36 +00001233/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001234void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Anderson20a631f2006-05-03 01:29:57 +00001235 const TargetData *TD = TM.getTargetData();
Dan Gohman014caa42008-12-22 21:14:27 +00001236 const Type *type = CV->getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001237 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001238
Chris Lattner61753bf2004-10-16 18:19:26 +00001239 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001240 EmitZeros(Size, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001241 return;
1242 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001243 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001244 return;
1245 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001246 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001247 return;
1248 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001249 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001250 return;
1251 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1252 // Small integers are handled below; large integers are handled here.
1253 if (Size > 4) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001254 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001255 return;
1256 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001257 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman014caa42008-12-22 21:14:27 +00001258 EmitGlobalConstantVector(CP);
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001259 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001260 }
1261
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001262 printDataDirective(type, AddrSpace);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001263 EmitConstantValueOnly(CV);
Evan Chenga774a992009-03-24 00:17:40 +00001264 if (VerboseAsm) {
1265 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1266 SmallString<40> S;
1267 CI->getValue().toStringUnsigned(S, 16);
1268 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1269 }
Scott Michelc0e9ff62008-06-03 15:39:51 +00001270 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001271 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001272}
Chris Lattner061d9e22006-01-27 02:10:10 +00001273
Chris Lattner17f71652008-08-17 07:19:36 +00001274void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001275 // Target doesn't support this yet!
Torok Edwinfbcc6632009-07-14 16:55:14 +00001276 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001277}
1278
Chris Lattnera32814b2006-09-26 23:59:50 +00001279/// PrintSpecial - Print information related to the specified machine instr
1280/// that is independent of the operand, and may be independent of the instr
1281/// itself. This can be useful for portably encoding the comment character
1282/// or other bits of target-specific knowledge into the asmstrings. The
1283/// syntax used is ${:comment}. Targets can override this to add support
1284/// for their own strange codes.
Chris Lattner1522e242009-03-10 05:37:13 +00001285void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattner30b47082006-09-27 00:06:07 +00001286 if (!strcmp(Code, "private")) {
1287 O << TAI->getPrivateGlobalPrefix();
1288 } else if (!strcmp(Code, "comment")) {
Evan Chenga774a992009-03-24 00:17:40 +00001289 if (VerboseAsm)
1290 O << TAI->getCommentString();
Chris Lattnera32814b2006-09-26 23:59:50 +00001291 } else if (!strcmp(Code, "uid")) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001292 // Comparing the address of MI isn't sufficient, because machineinstrs may
1293 // be allocated to the same address across functions.
1294 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1295
Owen Andersonae471cf2009-06-24 22:28:12 +00001296 // If this is a new LastFn instruction, bump the counter.
1297 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001298 ++Counter;
1299 LastMI = MI;
Owen Andersonae471cf2009-06-24 22:28:12 +00001300 LastFn = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001301 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001302 O << Counter;
1303 } else {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001304 std::string msg;
1305 raw_string_ostream Msg(msg);
1306 Msg << "Unknown special formatter '" << Code
Bill Wendlingf3baad32006-12-07 01:30:32 +00001307 << "' for machine instr: " << *MI;
Torok Edwinccb29cd2009-07-11 13:10:19 +00001308 llvm_report_error(Msg.str());
Chris Lattnera32814b2006-09-26 23:59:50 +00001309 }
1310}
1311
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001312/// processDebugLoc - Processes the debug information of each machine
1313/// instruction's DebugLoc.
1314void AsmPrinter::processDebugLoc(DebugLoc DL) {
1315 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1316 if (!DL.isUnknown()) {
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001317 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1318
1319 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1320 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1321 DICompileUnit(CurDLT.CompileUnit)));
1322
1323 PrevDLT = CurDLT;
1324 }
1325 }
1326}
Chris Lattnera32814b2006-09-26 23:59:50 +00001327
Chris Lattner061d9e22006-01-27 02:10:10 +00001328/// printInlineAsm - This method formats and prints the specified machine
1329/// instruction that is an inline asm.
1330void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001331 unsigned NumOperands = MI->getNumOperands();
1332
1333 // Count the number of register definitions.
1334 unsigned NumDefs = 0;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001335 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001336 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001337 assert(NumDefs != NumOperands-1 && "No asm string?");
1338
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001339 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001340
1341 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001342 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001343
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001344 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1345 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001346 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001347 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001348 return;
1349 }
1350
Jim Laskeya6211dc2006-09-06 18:34:40 +00001351 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001352
Bill Wendlinge21237e2007-01-16 03:42:04 +00001353 // The variant of the current asmprinter.
1354 int AsmPrinterVariant = TAI->getAssemblerDialect();
1355
Chris Lattneraa23fa92006-02-01 22:41:11 +00001356 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1357 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001358
Chris Lattneraa23fa92006-02-01 22:41:11 +00001359 while (*LastEmitted) {
1360 switch (*LastEmitted) {
1361 default: {
1362 // Not a special case, emit the string section literally.
1363 const char *LiteralEnd = LastEmitted+1;
1364 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001365 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001366 ++LiteralEnd;
1367 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1368 O.write(LastEmitted, LiteralEnd-LastEmitted);
1369 LastEmitted = LiteralEnd;
1370 break;
1371 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001372 case '\n':
1373 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001374 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001375 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001376 case '$': {
1377 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001378 bool Done = true;
1379
1380 // Handle escapes.
1381 switch (*LastEmitted) {
1382 default: Done = false; break;
1383 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001384 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1385 O << '$';
1386 ++LastEmitted; // Consume second '$' character.
1387 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001388 case '(': // $( -> same as GCC's { character.
1389 ++LastEmitted; // Consume '(' character.
1390 if (CurVariant != -1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001391 llvm_report_error("Nested variants found in inline asm string: '"
1392 + std::string(AsmStr) + "'");
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001393 }
1394 CurVariant = 0; // We're in the first variant now.
1395 break;
1396 case '|':
1397 ++LastEmitted; // consume '|' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001398 if (CurVariant == -1)
1399 O << '|'; // this is gcc's behavior for | outside a variant
1400 else
1401 ++CurVariant; // We're in the next variant.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001402 break;
1403 case ')': // $) -> same as GCC's } char.
1404 ++LastEmitted; // consume ')' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001405 if (CurVariant == -1)
1406 O << '}'; // this is gcc's behavior for } outside a variant
1407 else
1408 CurVariant = -1;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001409 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001410 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001411 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001412
1413 bool HasCurlyBraces = false;
1414 if (*LastEmitted == '{') { // ${variable}
1415 ++LastEmitted; // Consume '{' character.
1416 HasCurlyBraces = true;
1417 }
1418
Chris Lattner1522e242009-03-10 05:37:13 +00001419 // If we have ${:foo}, then this is not a real operand reference, it is a
1420 // "magic" string reference, just like in .td files. Arrange to call
1421 // PrintSpecial.
1422 if (HasCurlyBraces && *LastEmitted == ':') {
1423 ++LastEmitted;
1424 const char *StrStart = LastEmitted;
1425 const char *StrEnd = strchr(StrStart, '}');
1426 if (StrEnd == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001427 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1428 + std::string(AsmStr) + "'");
Chris Lattner1522e242009-03-10 05:37:13 +00001429 }
1430
1431 std::string Val(StrStart, StrEnd);
1432 PrintSpecial(MI, Val.c_str());
1433 LastEmitted = StrEnd+1;
1434 break;
1435 }
1436
Chris Lattneraa23fa92006-02-01 22:41:11 +00001437 const char *IDStart = LastEmitted;
1438 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001439 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001440 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1441 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001442 llvm_report_error("Bad $ operand number in inline asm string: '"
1443 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001444 }
1445 LastEmitted = IDEnd;
1446
Chris Lattner34f74c12006-02-06 22:17:23 +00001447 char Modifier[2] = { 0, 0 };
1448
Chris Lattneraa23fa92006-02-01 22:41:11 +00001449 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001450 // If we have curly braces, check for a modifier character. This
1451 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1452 if (*LastEmitted == ':') {
1453 ++LastEmitted; // Consume ':' character.
1454 if (*LastEmitted == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001455 llvm_report_error("Bad ${:} expression in inline asm string: '"
1456 + std::string(AsmStr) + "'");
Chris Lattner34f74c12006-02-06 22:17:23 +00001457 }
1458
1459 Modifier[0] = *LastEmitted;
1460 ++LastEmitted; // Consume modifier character.
1461 }
1462
Chris Lattneraa23fa92006-02-01 22:41:11 +00001463 if (*LastEmitted != '}') {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001464 llvm_report_error("Bad ${} expression in inline asm string: '"
1465 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001466 }
1467 ++LastEmitted; // Consume '}' character.
1468 }
1469
1470 if ((unsigned)Val >= NumOperands-1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001471 llvm_report_error("Invalid $ operand number in inline asm string: '"
1472 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001473 }
1474
Chris Lattner571d9642006-02-23 19:21:04 +00001475 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001476 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001477 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1478 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001479
1480 bool Error = false;
1481
Chris Lattner571d9642006-02-23 19:21:04 +00001482 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001483 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001484 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001485 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng2e559232009-03-20 18:03:34 +00001486 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattner5af3fde2006-02-24 19:50:58 +00001487 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001488
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001489 if (OpNo >= MI->getNumOperands()) {
1490 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001491 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001492 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001493 ++OpNo; // Skip over the ID number.
1494
Dale Johannesen4646aa32007-11-05 21:20:28 +00001495 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001496 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001497 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001498 else {
1499 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesenc36660d2008-09-24 01:07:17 +00001500 if ((OpFlags & 7) == 4) {
Dale Johannesen4646aa32007-11-05 21:20:28 +00001501 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1502 Modifier[0] ? Modifier : 0);
1503 } else {
1504 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1505 Modifier[0] ? Modifier : 0);
1506 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001507 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001508 }
1509 if (Error) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001510 std::string msg;
1511 raw_string_ostream Msg(msg);
1512 Msg << "Invalid operand found in inline asm: '"
Bill Wendlingf3baad32006-12-07 01:30:32 +00001513 << AsmStr << "'\n";
Torok Edwinccb29cd2009-07-11 13:10:19 +00001514 MI->print(Msg);
1515 llvm_report_error(Msg.str());
Chris Lattneraa23fa92006-02-01 22:41:11 +00001516 }
Chris Lattner571d9642006-02-23 19:21:04 +00001517 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001518 break;
1519 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001520 }
1521 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001522 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001523}
1524
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001525/// printImplicitDef - This method prints the specified machine instruction
1526/// that is an implicit def.
1527void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Evan Chenga774a992009-03-24 00:17:40 +00001528 if (VerboseAsm)
1529 O << '\t' << TAI->getCommentString() << " implicit-def: "
1530 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001531}
1532
Jim Laskeyf9e54452007-01-26 14:34:52 +00001533/// printLabel - This method prints a local label used by debug and
1534/// exception handling tables.
1535void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001536 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001537}
1538
Evan Chengd6e44ab2008-02-01 09:10:45 +00001539void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001540 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001541}
1542
Evan Chengefd142a2008-02-02 04:07:54 +00001543/// printDeclare - This method prints a local variable declaration used by
1544/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001545/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1546/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001547void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Pateldd25a9d2009-01-13 21:44:10 +00001548 unsigned FI = MI->getOperand(0).getIndex();
Evan Cheng2cb90682008-02-04 23:06:48 +00001549 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel32d17a12009-04-15 00:10:26 +00001550 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Chengefd142a2008-02-02 04:07:54 +00001551}
1552
Chris Lattneraa23fa92006-02-01 22:41:11 +00001553/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1554/// instruction, using the specified assembler variant. Targets should
1555/// overried this to format as appropriate.
1556bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001557 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001558 // Target doesn't support this yet!
1559 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001560}
Chris Lattner1d08c652006-02-24 20:21:58 +00001561
1562bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1563 unsigned AsmVariant,
1564 const char *ExtraCode) {
1565 // Target doesn't support this yet!
1566 return true;
1567}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001568
1569/// printBasicBlockLabel - This method prints the label for the specified
1570/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001571void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001572 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001573 bool printColon,
1574 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001575 if (printAlign) {
1576 unsigned Align = MBB->getAlignment();
1577 if (Align)
1578 EmitAlignment(Log2_32(Align));
1579 }
1580
Dan Gohman6896901e2008-06-30 22:03:41 +00001581 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001582 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001583 if (printColon)
1584 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001585 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001586 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng53495222008-07-08 00:55:58 +00001587 << MBB->getBasicBlock()->getNameStart();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001588}
Nate Begeman984c1a42006-08-12 21:29:52 +00001589
Evan Cheng797d56f2007-11-09 01:32:10 +00001590/// printPICJumpTableSetLabel - This method prints a set label for the
1591/// specified MachineBasicBlock for a jumptable entry.
1592void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1593 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001594 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001595 return;
1596
Jim Laskeya6211dc2006-09-06 18:34:40 +00001597 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001598 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001599 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001600 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1601 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001602}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001603
Evan Cheng797d56f2007-11-09 01:32:10 +00001604void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1605 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001606 if (!TAI->getSetDirective())
1607 return;
1608
1609 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001610 << getFunctionNumber() << '_' << uid << '_' << uid2
1611 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001612 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001613 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1614 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001615}
1616
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001617/// printDataDirective - This method prints the asm directive for the
1618/// specified type.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001619void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001620 const TargetData *TD = TM.getTargetData();
1621 switch (type->getTypeID()) {
Chris Lattnerfe785582009-07-15 04:42:49 +00001622 case Type::FloatTyID: case Type::DoubleTyID:
1623 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1624 assert(0 && "Should have already output floating point constant.");
1625 default:
1626 assert(0 && "Can't handle printing this type of thing");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001627 case Type::IntegerTyID: {
1628 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1629 if (BitWidth <= 8)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001630 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001631 else if (BitWidth <= 16)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001632 O << TAI->getData16bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001633 else if (BitWidth <= 32)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001634 O << TAI->getData32bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001635 else if (BitWidth <= 64) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001636 assert(TAI->getData64bitsDirective(AddrSpace) &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001637 "Target cannot handle 64-bit constant exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001638 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohmanf9b20542008-09-08 16:40:13 +00001639 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001640 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001641 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001642 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001643 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001644 case Type::PointerTyID:
1645 if (TD->getPointerSize() == 8) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001646 assert(TAI->getData64bitsDirective(AddrSpace) &&
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001647 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001648 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001649 } else if (TD->getPointerSize() == 2) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001650 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001651 } else if (TD->getPointerSize() == 1) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001652 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001653 } else {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001654 O << TAI->getData32bitsDirective(AddrSpace);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001655 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001656 break;
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001657 }
1658}
Dale Johannesen915e1542007-02-16 01:54:53 +00001659
Anton Korobeynikoved473292008-08-08 18:25:07 +00001660void AsmPrinter::printVisibility(const std::string& Name,
1661 unsigned Visibility) const {
1662 if (Visibility == GlobalValue::HiddenVisibility) {
1663 if (const char *Directive = TAI->getHiddenDirective())
1664 O << Directive << Name << '\n';
1665 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1666 if (const char *Directive = TAI->getProtectedDirective())
1667 O << Directive << Name << '\n';
1668 }
1669}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001670
Anton Korobeynikovbff4b372008-11-22 16:15:34 +00001671void AsmPrinter::printOffset(int64_t Offset) const {
1672 if (Offset > 0)
1673 O << '+' << Offset;
1674 else if (Offset < 0)
1675 O << Offset;
1676}
1677
Gordon Henriksend930f912008-08-17 18:44:35 +00001678GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1679 if (!S->usesMetadata())
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001680 return 0;
1681
Gordon Henriksend930f912008-08-17 18:44:35 +00001682 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001683 if (GCPI != GCMetadataPrinters.end())
1684 return GCPI->second;
1685
Gordon Henriksend930f912008-08-17 18:44:35 +00001686 const char *Name = S->getName().c_str();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001687
1688 for (GCMetadataPrinterRegistry::iterator
1689 I = GCMetadataPrinterRegistry::begin(),
1690 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1691 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001692 GCMetadataPrinter *GMP = I->instantiate();
1693 GMP->S = S;
1694 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1695 return GMP;
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001696 }
1697
Gordon Henriksend930f912008-08-17 18:44:35 +00001698 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +00001699 llvm_unreachable(0);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001700}
David Greenede544782009-07-13 20:25:48 +00001701
1702/// EmitComments - Pretty-print comments for instructions
1703void AsmPrinter::EmitComments(const MachineInstr &MI) const
1704{
David Greenede9cd442009-07-16 22:24:20 +00001705 if (!MI.getDebugLoc().isUnknown()) {
1706 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
1707
1708 // Print source line info
1709 O.PadToColumn(TAI->getCommentColumn(), 1);
1710 O << TAI->getCommentString() << " SrcLine " << DLT.Line << ":" << DLT.Col;
1711 }
David Greenede544782009-07-13 20:25:48 +00001712}
1713
1714/// EmitComments - Pretty-print comments for instructions
1715void AsmPrinter::EmitComments(const MCInst &MI) const
1716{
David Greenede9cd442009-07-16 22:24:20 +00001717 if (!MI.getDebugLoc().isUnknown()) {
1718 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
1719
1720 // Print source line info
1721 O.PadToColumn(TAI->getCommentColumn(), 1);
1722 O << TAI->getCommentString() << " SrcLine " << DLT.Line << ":" << DLT.Col;
1723 }
David Greenede544782009-07-13 20:25:48 +00001724}