blob: e352922fee7b15e6cf3801f0406779249a17ffe9 [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) {
Rafael Espindola6de96a12009-01-15 20:18:42 +0000157 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +0000158
Chris Lattnerb8476452009-06-18 23:41:35 +0000159 if (TAI->doesAllowQuotesInName())
160 Mang->setUseQuotes(true);
161
Duncan Sands5a913d62009-01-28 13:14:17 +0000162 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000163 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindolacda011b2008-12-03 11:01:37 +0000164
165 if (TAI->hasSingleParameterDotFile()) {
166 /* Very minimal debug info. It is ignored if we emit actual
167 debug info. If we don't, this at helps the user find where
168 a function came from. */
169 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
170 }
171
Gordon Henriksend930f912008-08-17 18:44:35 +0000172 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
173 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
174 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000175
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000176 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000177 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000178 << M.getModuleInlineAsm()
Dan Gohman6896901e2008-06-30 22:03:41 +0000179 << '\n' << TAI->getCommentString()
Jim Laskeya6211dc2006-09-06 18:34:40 +0000180 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000181
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000182 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000183
Chris Lattner1fd58882009-06-24 19:09:55 +0000184 if (TAI->doesSupportDebugInformation() ||
185 TAI->doesSupportExceptionHandling()) {
186 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
187 if (MMI)
Devang Patel33f4eb42009-06-19 21:54:26 +0000188 MMI->AnalyzeModule(M);
Chris Lattner1fd58882009-06-24 19:09:55 +0000189 DW = getAnalysisIfAvailable<DwarfWriter>();
190 if (DW)
191 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel33f4eb42009-06-19 21:54:26 +0000192 }
193
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000194 return false;
195}
196
197bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner70413122009-06-24 18:54:37 +0000198 // Emit final debug information.
199 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
200 DW->EndModule();
201
Chris Lattner2981dc12009-06-24 18:52:01 +0000202 // If the target wants to know about weak references, print them all.
Rafael Espindolad7998d02006-12-18 03:37:18 +0000203 if (TAI->getWeakRefDirective()) {
Chris Lattner2981dc12009-06-24 18:52:01 +0000204 // FIXME: This is not lazy, it would be nice to only print weak references
205 // to stuff that is actually used. Note that doing so would require targets
206 // to notice uses in operands (due to constant exprs etc). This should
207 // happen with the MC stuff eventually.
208 SwitchToDataSection("");
Rafael Espindolad7998d02006-12-18 03:37:18 +0000209
Chris Lattner2981dc12009-06-24 18:52:01 +0000210 // Print out module-level global variables here.
211 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
212 I != E; ++I) {
213 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000214 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000215 }
216
217 for (Module::const_iterator I = M.begin(), E = M.end();
218 I != E; ++I) {
219 if (I->hasExternalWeakLinkage())
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000220 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner2981dc12009-06-24 18:52:01 +0000221 }
Rafael Espindolad7998d02006-12-18 03:37:18 +0000222 }
223
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000224 if (TAI->getSetDirective()) {
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000225 if (!M.alias_empty())
Anton Korobeynikovf8dc8aa2008-09-24 22:15:21 +0000226 SwitchToSection(TAI->getTextSection());
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000227
Dan Gohman6896901e2008-06-30 22:03:41 +0000228 O << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000229 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner2981dc12009-06-24 18:52:01 +0000230 I != E; ++I) {
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000231 std::string Name = Mang->getMangledName(I);
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000232
233 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000234 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000235
Anton Korobeynikova07765b2007-09-06 17:21:48 +0000236 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000237 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000238 else if (I->hasWeakLinkage())
Dan Gohman6896901e2008-06-30 22:03:41 +0000239 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindola6de96a12009-01-15 20:18:42 +0000240 else if (!I->hasLocalLinkage())
Torok Edwinfbcc6632009-07-14 16:55:14 +0000241 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000242
Anton Korobeynikovfd7faec2008-09-24 22:21:04 +0000243 printVisibility(Name, I->getVisibility());
Anton Korobeynikov2601d7e2008-03-11 21:41:14 +0000244
Dan Gohman6896901e2008-06-30 22:03:41 +0000245 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000246 }
247 }
248
Duncan Sands5a913d62009-01-28 13:14:17 +0000249 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000250 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
251 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
252 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
253 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksen5180e852008-01-07 01:30:38 +0000254
Dan Gohmanbcde1722008-05-05 00:28:39 +0000255 // If we don't have any trampolines, then we don't require stack memory
256 // to be executable. Some targets have a directive to declare this.
Chris Lattner2981dc12009-06-24 18:52:01 +0000257 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmanbcde1722008-05-05 00:28:39 +0000258 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
259 if (TAI->getNonexecutableStackDirective())
Dan Gohman6896901e2008-06-30 22:03:41 +0000260 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmanbcde1722008-05-05 00:28:39 +0000261
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000262 delete Mang; Mang = 0;
Chris Lattner1fd58882009-06-24 19:09:55 +0000263 DW = 0; MMI = 0;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000264 return false;
265}
266
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000267std::string
268AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendling067f1d82007-09-18 01:47:22 +0000269 assert(MF && "No machine function?");
Chris Lattnere79b2bc2009-07-17 20:46:40 +0000270 return Mang->getMangledName(MF->getFunction(), ".eh",
271 TAI->is_EHSymbolPrivate());
Bill Wendling067f1d82007-09-18 01:47:22 +0000272}
273
Chris Lattnerbb644e32005-11-21 07:51:36 +0000274void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000275 // What's my mangled name?
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000276 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Chengcdf36092007-10-14 05:57:21 +0000277 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000278}
279
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000280namespace {
281 // SectionCPs - Keep track the alignment, constpool entries per Section.
282 struct SectionCPs {
283 const Section *S;
284 unsigned Alignment;
285 SmallVector<unsigned, 4> CPEs;
286 SectionCPs(const Section *s, unsigned a) : S(s), Alignment(a) {};
287 };
288}
289
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000290/// EmitConstantPool - Print to the current output stream assembly
291/// representations of the constants in the constant pool MCP. This is
292/// used to print out constants which have been "spilled to memory" by
293/// the code generator.
294///
295void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000296 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000297 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000298
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000299 // Calculate sections for constant pool entries. We collect entries to go into
300 // the same section together to reduce amount of section switch statements.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000301 SmallVector<SectionCPs, 4> CPSections;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000302 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000303 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000304 unsigned Align = CPE.getAlignment();
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000305 const Section* S = TAI->SelectSectionForMachineConst(CPE.getType());
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000306 // The number of sections are small, just do a linear search from the
307 // last section to the first.
308 bool Found = false;
309 unsigned SecIdx = CPSections.size();
310 while (SecIdx != 0) {
311 if (CPSections[--SecIdx].S == S) {
312 Found = true;
313 break;
314 }
315 }
316 if (!Found) {
317 SecIdx = CPSections.size();
318 CPSections.push_back(SectionCPs(S, Align));
319 }
320
321 if (Align > CPSections[SecIdx].Alignment)
322 CPSections[SecIdx].Alignment = Align;
323 CPSections[SecIdx].CPEs.push_back(i);
Evan Chengec1d60b2006-06-29 00:26:09 +0000324 }
325
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000326 // Now print stuff into the calculated sections.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000327 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
328 SwitchToSection(CPSections[i].S);
329 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Chengec1d60b2006-06-29 00:26:09 +0000330
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000331 unsigned Offset = 0;
332 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
333 unsigned CPI = CPSections[i].CPEs[j];
334 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000335
Chris Lattnerf6190822006-02-09 04:46:04 +0000336 // Emit inter-object padding for alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000337 unsigned AlignMask = CPE.getAlignment() - 1;
338 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
339 EmitZeros(NewOffset - Offset);
340
341 const Type *Ty = CPE.getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000342 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000343
344 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
345 << CPI << ":\t\t\t\t\t";
346 if (VerboseAsm) {
347 O << TAI->getCommentString() << ' ';
348 WriteTypeSymbolic(O, CPE.getType(), 0);
Anton Korobeynikov123afb82008-09-24 22:17:59 +0000349 }
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000350 O << '\n';
351 if (CPE.isMachineConstantPoolEntry())
352 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
353 else
354 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000355 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000356 }
357}
358
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000359/// EmitJumpTableInfo - Print assembly representations of the jump tables used
360/// by the current function to the current output stream.
361///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000362void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
363 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000364 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
365 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000366
Jim Laskey70323a82006-12-14 19:17:33 +0000367 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000368
Nate Begeman78756502006-07-27 01:13:04 +0000369 // Pick the directive to use to print the jump table entries, and switch to
370 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000371 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000372
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000373 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
374 const Function *F = MF.getFunction();
375 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
Evan Chengde9e36a2009-06-18 20:37:15 +0000376 bool JTInDiffSection = false;
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000377 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Evan Chengde9e36a2009-06-18 20:37:15 +0000378 !JumpTableDataSection ||
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000379 SectionFlags & SectionFlags::Linkonce) {
Jim Laskey70323a82006-12-14 19:17:33 +0000380 // In PIC mode, we need to emit the jump table to the same section as the
381 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikovfe047d22008-07-09 13:27:16 +0000382 // We should also do if the section name is NULL or function is declared in
383 // discardable section.
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000384 SwitchToSection(TAI->SectionForGlobal(F));
Nate Begeman78756502006-07-27 01:13:04 +0000385 } else {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000386 SwitchToDataSection(JumpTableDataSection);
Evan Chengde9e36a2009-06-18 20:37:15 +0000387 JTInDiffSection = true;
Nate Begeman78756502006-07-27 01:13:04 +0000388 }
Jim Laskey70323a82006-12-14 19:17:33 +0000389
390 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000391
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000392 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000393 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000394
395 // If this jump table was deleted, ignore it.
396 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000397
398 // For PIC codegen, if possible we want to use the SetDirective to reduce
399 // the number of relocations the assembler will generate for the jump table.
400 // Set directives are all printed before the jump table itself.
Evan Cheng797d56f2007-11-09 01:32:10 +0000401 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000402 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000403 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng797d56f2007-11-09 01:32:10 +0000404 if (EmittedSets.insert(JTBBs[ii]))
405 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman984c1a42006-08-12 21:29:52 +0000406
Chris Lattner0ee2d462007-01-18 01:12:56 +0000407 // On some targets (e.g. darwin) we want to emit two consequtive labels
408 // before each jump table. The first label is never referenced, but tells
409 // the assembler and linker the extents of the jump table object. The
410 // second label is actually referenced by the code.
Evan Chengde9e36a2009-06-18 20:37:15 +0000411 if (JTInDiffSection) {
412 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
413 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
414 }
Chris Lattner0ee2d462007-01-18 01:12:56 +0000415
Evan Chengcdf36092007-10-14 05:57:21 +0000416 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
417 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000418
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000419 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000420 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000421 O << '\n';
422 }
423 }
424}
425
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000426void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
427 const MachineBasicBlock *MBB,
428 unsigned uid) const {
429 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
430
431 // Use JumpTableDirective otherwise honor the entry size from the jump table
432 // info.
433 const char *JTEntryDirective = TAI->getJumpTableDirective();
434 bool HadJTEntryDirective = JTEntryDirective != NULL;
435 if (!HadJTEntryDirective) {
436 JTEntryDirective = MJTI->getEntrySize() == 4 ?
437 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
438 }
439
440 O << JTEntryDirective << ' ';
441
442 // If we have emitted set directives for the jump table entries, print
443 // them rather than the entries themselves. If we're emitting PIC, then
444 // emit the table entries as differences between two text section labels.
445 // If we're emitting non-PIC code, then emit the entries as direct
446 // references to the target basic blocks.
447 if (IsPic) {
448 if (TAI->getSetDirective()) {
449 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
450 << '_' << uid << "_set_" << MBB->getNumber();
451 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000452 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000453 // If the arch uses custom Jump Table directives, don't calc relative to
454 // JT
455 if (!HadJTEntryDirective)
456 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
457 << getFunctionNumber() << '_' << uid;
458 }
459 } else {
Evan Chengc7990652008-02-28 00:43:03 +0000460 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov2c638782007-11-14 09:18:41 +0000461 }
462}
463
464
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000465/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
466/// special global used by LLVM. If so, emit it and return true, otherwise
467/// do nothing and return false.
468bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharthbeb80a92007-08-22 19:33:11 +0000469 if (GV->getName() == "llvm.used") {
470 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
471 EmitLLVMUsedList(GV->getInitializer());
472 return true;
473 }
474
Jim Laskey313570f2006-03-07 22:00:35 +0000475 // Ignore debug and non-emitted data.
Chris Lattner184f1be2009-04-13 05:44:34 +0000476 if (GV->getSection() == "llvm.metadata" ||
477 GV->hasAvailableExternallyLinkage())
478 return true;
Jim Laskey313570f2006-03-07 22:00:35 +0000479
480 if (!GV->hasAppendingLinkage()) return false;
481
482 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000483
Evan Cheng53ce7de2007-06-04 20:39:18 +0000484 const TargetData *TD = TM.getTargetData();
485 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner317293b2009-03-09 05:52:15 +0000486 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner126dab22009-03-09 08:18:48 +0000487 SwitchToDataSection(TAI->getStaticCtorsSection());
488 EmitAlignment(Align, 0);
489 EmitXXStructorList(GV->getInitializer());
490 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000491 }
492
Chris Lattner317293b2009-03-09 05:52:15 +0000493 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner126dab22009-03-09 08:18:48 +0000494 SwitchToDataSection(TAI->getStaticDtorsSection());
495 EmitAlignment(Align, 0);
496 EmitXXStructorList(GV->getInitializer());
497 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000498 }
499
500 return false;
501}
502
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000503/// findGlobalValue - if CV is an expression equivalent to a single
504/// global value, return that value.
505const GlobalValue * AsmPrinter::findGlobalValue(const Constant *CV) {
506 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
507 return GV;
508 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
509 const TargetData *TD = TM.getTargetData();
510 unsigned Opcode = CE->getOpcode();
511 switch (Opcode) {
512 case Instruction::GetElementPtr: {
513 const Constant *ptrVal = CE->getOperand(0);
514 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
515 if (TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], idxVec.size()))
516 return 0;
517 return findGlobalValue(ptrVal);
518 }
519 case Instruction::BitCast:
520 return findGlobalValue(CE->getOperand(0));
521 default:
522 return 0;
523 }
524 }
525 return 0;
526}
527
Chris Lattner66af3902006-09-26 03:38:18 +0000528/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesenabb1e772008-09-09 22:29:13 +0000529/// global in the specified llvm.used list for which emitUsedDirectiveFor
530/// is true, as being used with this directive.
531
Chris Lattner66af3902006-09-26 03:38:18 +0000532void AsmPrinter::EmitLLVMUsedList(Constant *List) {
533 const char *Directive = TAI->getUsedDirective();
534
Dan Gohman4fe64de2009-06-14 23:30:43 +0000535 // Should be an array of 'i8*'.
Chris Lattner66af3902006-09-26 03:38:18 +0000536 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
537 if (InitList == 0) return;
538
539 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000540 const GlobalValue *GV = findGlobalValue(InitList->getOperand(i));
Dale Johannesenabb1e772008-09-09 22:29:13 +0000541 if (TAI->emitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen5c1ff112008-09-03 20:34:58 +0000542 O << Directive;
543 EmitConstantValueOnly(InitList->getOperand(i));
544 O << '\n';
545 }
Chris Lattner66af3902006-09-26 03:38:18 +0000546 }
547}
548
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000549/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
550/// function pointers, ignoring the init priority.
551void AsmPrinter::EmitXXStructorList(Constant *List) {
552 // Should be an array of '{ int, void ()* }' structs. The first value is the
553 // init priority, which we ignore.
554 if (!isa<ConstantArray>(List)) return;
555 ConstantArray *InitList = cast<ConstantArray>(List);
556 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
557 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
558 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000559
560 if (CS->getOperand(1)->isNullValue())
561 return; // Found a null terminator, exit printing.
562 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000563 EmitGlobalConstant(CS->getOperand(1));
564 }
565}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000566
Jim Laskey71262542006-10-17 13:41:07 +0000567/// getGlobalLinkName - Returns the asm/link name of of the specified
568/// global variable. Should be overridden by each target asm printer to
569/// generate the appropriate value.
Bill Wendling992f8462009-04-10 00:12:49 +0000570const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
571 std::string &LinkName) const {
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000572 if (isa<Function>(GV)) {
573 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000574 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000575 LinkName += TAI->getFunctionAddrSuffix();
576 } else {
577 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000578 LinkName += Mang->getMangledName(GV);
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000579 LinkName += TAI->getGlobalVarAddrSuffix();
580 }
581
Jim Laskeyd24b9132006-10-17 17:17:24 +0000582 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000583}
584
Jim Laskey18fc0972007-02-21 22:47:38 +0000585/// EmitExternalGlobal - Emit the external reference to a global variable.
586/// Should be overridden if an indirect reference should be used.
587void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling992f8462009-04-10 00:12:49 +0000588 std::string GLN;
589 O << getGlobalLinkName(GV, GLN);
Jim Laskey18fc0972007-02-21 22:47:38 +0000590}
591
592
593
Jim Laskey1c055e82007-01-25 15:12:02 +0000594//===----------------------------------------------------------------------===//
595/// LEB 128 number encoding.
596
597/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
598/// representing an unsigned leb128 value.
599void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner09487cd2008-11-10 04:35:24 +0000600 char Buffer[20];
Jim Laskey1c055e82007-01-25 15:12:02 +0000601 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000602 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000603 Value >>= 7;
604 if (Value) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000605 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000606 if (Value) O << ", ";
607 } while (Value);
608}
609
Jim Laskey1c055e82007-01-25 15:12:02 +0000610/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
611/// representing a signed leb128 value.
612void AsmPrinter::PrintSLEB128(int Value) const {
613 int Sign = Value >> (8 * sizeof(Value) - 1);
614 bool IsMore;
Chris Lattner09487cd2008-11-10 04:35:24 +0000615 char Buffer[20];
Anton Korobeynikovbd890b12008-08-16 12:57:46 +0000616
Jim Laskey1c055e82007-01-25 15:12:02 +0000617 do {
Chris Lattner09487cd2008-11-10 04:35:24 +0000618 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskey1c055e82007-01-25 15:12:02 +0000619 Value >>= 7;
620 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
621 if (IsMore) Byte |= 0x80;
Chris Lattner09487cd2008-11-10 04:35:24 +0000622 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000623 if (IsMore) O << ", ";
624 } while (IsMore);
625}
626
Jim Laskey1c055e82007-01-25 15:12:02 +0000627//===--------------------------------------------------------------------===//
628// Emission and print routines
629//
630
631/// PrintHex - Print a value as a hexidecimal value.
632///
633void AsmPrinter::PrintHex(int Value) const {
Chris Lattner5505eed2008-11-10 04:30:26 +0000634 char Buffer[20];
Chris Lattner5505eed2008-11-10 04:30:26 +0000635 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskey1c055e82007-01-25 15:12:02 +0000636}
637
638/// EOL - Print a newline character to asm stream. If a comment is present
639/// then it will be printed first. Comments should not contain '\n'.
Jim Laskey18fc0972007-02-21 22:47:38 +0000640void AsmPrinter::EOL() const {
Dan Gohman6896901e2008-06-30 22:03:41 +0000641 O << '\n';
Jim Laskey18fc0972007-02-21 22:47:38 +0000642}
Owen Anderson1d952532008-07-01 21:16:27 +0000643
Jim Laskey1c055e82007-01-25 15:12:02 +0000644void AsmPrinter::EOL(const std::string &Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000645 if (VerboseAsm && !Comment.empty()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000646 O << '\t'
Jim Laskey1c055e82007-01-25 15:12:02 +0000647 << TAI->getCommentString()
Dan Gohman6896901e2008-06-30 22:03:41 +0000648 << ' '
Jim Laskey1c055e82007-01-25 15:12:02 +0000649 << Comment;
650 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000651 O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000652}
653
Owen Anderson1d952532008-07-01 21:16:27 +0000654void AsmPrinter::EOL(const char* Comment) const {
Evan Chengc963f6c2008-07-01 23:18:29 +0000655 if (VerboseAsm && *Comment) {
Owen Anderson1d952532008-07-01 21:16:27 +0000656 O << '\t'
657 << TAI->getCommentString()
658 << ' '
659 << Comment;
660 }
661 O << '\n';
662}
663
Jim Laskey1c055e82007-01-25 15:12:02 +0000664/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
665/// unsigned leb128 value.
666void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
667 if (TAI->hasLEB128()) {
668 O << "\t.uleb128\t"
669 << Value;
670 } else {
671 O << TAI->getData8bitsDirective();
672 PrintULEB128(Value);
673 }
674}
675
676/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
677/// signed leb128 value.
678void AsmPrinter::EmitSLEB128Bytes(int Value) const {
679 if (TAI->hasLEB128()) {
680 O << "\t.sleb128\t"
681 << Value;
682 } else {
683 O << TAI->getData8bitsDirective();
684 PrintSLEB128(Value);
685 }
686}
687
688/// EmitInt8 - Emit a byte directive and value.
689///
690void AsmPrinter::EmitInt8(int Value) const {
691 O << TAI->getData8bitsDirective();
692 PrintHex(Value & 0xFF);
693}
694
695/// EmitInt16 - Emit a short directive and value.
696///
697void AsmPrinter::EmitInt16(int Value) const {
698 O << TAI->getData16bitsDirective();
699 PrintHex(Value & 0xFFFF);
700}
701
702/// EmitInt32 - Emit a long directive and value.
703///
704void AsmPrinter::EmitInt32(int Value) const {
705 O << TAI->getData32bitsDirective();
706 PrintHex(Value);
707}
708
709/// EmitInt64 - Emit a long long directive and value.
710///
711void AsmPrinter::EmitInt64(uint64_t Value) const {
712 if (TAI->getData64bitsDirective()) {
713 O << TAI->getData64bitsDirective();
714 PrintHex(Value);
715 } else {
716 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman6896901e2008-06-30 22:03:41 +0000717 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000718 EmitInt32(unsigned(Value));
719 } else {
Dan Gohman6896901e2008-06-30 22:03:41 +0000720 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskey1c055e82007-01-25 15:12:02 +0000721 EmitInt32(unsigned(Value >> 32));
722 }
723 }
724}
725
726/// toOctal - Convert the low order bits of X into an octal digit.
727///
728static inline char toOctal(int X) {
729 return (X&7)+'0';
730}
731
732/// printStringChar - Print a char, escaped if necessary.
733///
David Greenea31f96c2009-07-14 20:18:05 +0000734static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000735 if (C == '"') {
736 O << "\\\"";
737 } else if (C == '\\') {
738 O << "\\\\";
Chris Lattner1f9053a2009-01-22 23:38:45 +0000739 } else if (isprint((unsigned char)C)) {
Jim Laskey1c055e82007-01-25 15:12:02 +0000740 O << C;
741 } else {
742 switch(C) {
743 case '\b': O << "\\b"; break;
744 case '\f': O << "\\f"; break;
745 case '\n': O << "\\n"; break;
746 case '\r': O << "\\r"; break;
747 case '\t': O << "\\t"; break;
748 default:
749 O << '\\';
750 O << toOctal(C >> 6);
751 O << toOctal(C >> 3);
752 O << toOctal(C >> 0);
753 break;
754 }
755 }
756}
757
758/// EmitString - Emit a string with quotes and a null terminator.
759/// Special characters are emitted properly.
760/// \literal (Eg. '\t') \endliteral
761void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling16abfc92009-04-09 23:51:31 +0000762 EmitString(String.c_str(), String.size());
763}
764
765void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000766 const char* AscizDirective = TAI->getAscizDirective();
767 if (AscizDirective)
768 O << AscizDirective;
769 else
770 O << TAI->getAsciiDirective();
Dan Gohman6896901e2008-06-30 22:03:41 +0000771 O << '\"';
Bill Wendling16abfc92009-04-09 23:51:31 +0000772 for (unsigned i = 0; i < Size; ++i)
Chris Lattner69b586e2009-04-08 00:28:38 +0000773 printStringChar(O, String[i]);
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000774 if (AscizDirective)
Dan Gohman6896901e2008-06-30 22:03:41 +0000775 O << '\"';
Anton Korobeynikov6c5e0ad2007-03-06 19:25:02 +0000776 else
777 O << "\\0\"";
Jim Laskey1c055e82007-01-25 15:12:02 +0000778}
779
780
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000781/// EmitFile - Emit a .file directive.
782void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
783 O << "\t.file\t" << Number << " \"";
Chris Lattner69b586e2009-04-08 00:28:38 +0000784 for (unsigned i = 0, N = Name.size(); i < N; ++i)
785 printStringChar(O, Name[i]);
Dan Gohman6896901e2008-06-30 22:03:41 +0000786 O << '\"';
Dan Gohmanbd8331d2007-09-24 20:58:13 +0000787}
788
789
Jim Laskey1c055e82007-01-25 15:12:02 +0000790//===----------------------------------------------------------------------===//
791
Chris Lattner3e3ff302007-05-31 18:57:45 +0000792// EmitAlignment - Emit an alignment directive to the specified power of
793// two boundary. For example, if you pass in 3 here, you will get an 8
794// byte alignment. If a global value is specified, and if that global has
795// an explicit alignment requested, it will unconditionally override the
796// alignment request. However, if ForcedAlignBits is specified, this value
797// has final say: the ultimate alignment will be the max of ForcedAlignBits
798// and the alignment computed with NumBits and the global.
799//
800// The algorithm is:
801// Align = NumBits;
802// if (GV && GV->hasalignment) Align = GV->getalignment();
803// Align = std::max(Align, ForcedAlignBits);
804//
805void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng26edb592008-02-29 19:36:59 +0000806 unsigned ForcedAlignBits,
807 bool UseFillExpr) const {
Dale Johannesen8653d292007-04-23 23:33:31 +0000808 if (GV && GV->getAlignment())
Chris Lattner3e3ff302007-05-31 18:57:45 +0000809 NumBits = Log2_32(GV->getAlignment());
810 NumBits = std::max(NumBits, ForcedAlignBits);
811
Chris Lattner747960d2005-11-10 18:09:27 +0000812 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000813 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Cheng86eb3fd2007-07-25 23:35:07 +0000814 O << TAI->getAlignDirective() << NumBits;
Evan Chengc7990652008-02-28 00:43:03 +0000815
816 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng26edb592008-02-29 19:36:59 +0000817 UseFillExpr &= IsInTextSection && FillValue;
Chris Lattner09487cd2008-11-10 04:35:24 +0000818 if (UseFillExpr) {
819 O << ',';
820 PrintHex(FillValue);
821 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000822 O << '\n';
Chris Lattner1d35c162004-08-17 19:14:29 +0000823}
824
Jim Laskey18fc0972007-02-21 22:47:38 +0000825
Chris Lattnerbb644e32005-11-21 07:51:36 +0000826/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000827///
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000828void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000829 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000830 if (TAI->getZeroDirective()) {
831 O << TAI->getZeroDirective() << NumZeros;
832 if (TAI->getZeroDirectiveSuffix())
833 O << TAI->getZeroDirectiveSuffix();
Dan Gohman6896901e2008-06-30 22:03:41 +0000834 O << '\n';
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000835 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000836 for (; NumZeros; --NumZeros)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +0000837 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000838 }
839 }
840}
841
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000842// Print out the specified constant, without a storage class. Only the
843// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000844void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000845 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman6896901e2008-06-30 22:03:41 +0000846 O << '0';
Zhou Sheng75b871f2007-01-11 12:24:14 +0000847 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel499c1192008-06-03 06:18:19 +0000848 O << CI->getZExtValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000849 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000850 // This is a constant address for a global variable or function. Use the
851 // name of the variable or function as the address value, possibly
852 // decorating it with GlobalVarAddrPrefix/Suffix or
853 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000854 if (isa<Function>(GV)) {
855 O << TAI->getFunctionAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000856 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000857 << TAI->getFunctionAddrSuffix();
858 } else {
859 O << TAI->getGlobalVarAddrPrefix()
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000860 << Mang->getMangledName(GV)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000861 << TAI->getGlobalVarAddrSuffix();
862 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000863 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000864 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000865 unsigned Opcode = CE->getOpcode();
866 switch (Opcode) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000867 case Instruction::GetElementPtr: {
868 // generate a symbolic expression for the byte address
869 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner83dfca82007-02-10 20:31:59 +0000870 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
871 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
872 idxVec.size())) {
Chris Lattner7e1d2862009-02-05 06:55:21 +0000873 // Truncate/sext the offset to the pointer size.
874 if (TD->getPointerSizeInBits() != 64) {
875 int SExtAmount = 64-TD->getPointerSizeInBits();
876 Offset = (Offset << SExtAmount) >> SExtAmount;
877 }
878
Chris Lattner145569b2005-02-14 21:40:26 +0000879 if (Offset)
Dan Gohman6896901e2008-06-30 22:03:41 +0000880 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000881 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000882 if (Offset > 0)
883 O << ") + " << Offset;
884 else if (Offset < 0)
885 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000886 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000887 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000888 }
889 break;
890 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000891 case Instruction::Trunc:
892 case Instruction::ZExt:
893 case Instruction::SExt:
894 case Instruction::FPTrunc:
895 case Instruction::FPExt:
896 case Instruction::UIToFP:
897 case Instruction::SIToFP:
898 case Instruction::FPToUI:
899 case Instruction::FPToSI:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000900 llvm_unreachable("FIXME: Don't yet support this kind of constant cast expr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000901 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000902 case Instruction::BitCast:
903 return EmitConstantValueOnly(CE->getOperand(0));
904
Chris Lattner0c537da2006-12-12 05:18:19 +0000905 case Instruction::IntToPtr: {
906 // Handle casts to pointers by changing them into casts to the appropriate
907 // integer type. This promotes constant folding and simplifies this code.
908 Constant *Op = CE->getOperand(0);
909 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
910 return EmitConstantValueOnly(Op);
911 }
912
913
914 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000915 // Support only foldable casts to/from pointers that can be eliminated by
916 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000917 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000918 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000919
Chris Lattner0c537da2006-12-12 05:18:19 +0000920 // We can emit the pointer value into this slot if the slot is an
921 // integer slot greater or equal to the size of the pointer.
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000922 if (TD->getTypeAllocSize(Ty) >= TD->getTypeAllocSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000923 return EmitConstantValueOnly(Op);
Nick Lewycky42a19b62008-08-08 06:34:07 +0000924
925 O << "((";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000926 EmitConstantValueOnly(Op);
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000927 APInt ptrMask = APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Ty));
Chris Lattner17f71652008-08-17 07:19:36 +0000928
929 SmallString<40> S;
930 ptrMask.toStringUnsigned(S);
931 O << ") & " << S.c_str() << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000932 break;
933 }
934 case Instruction::Add:
Anton Korobeynikov465c0252007-02-04 23:27:42 +0000935 case Instruction::Sub:
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000936 case Instruction::And:
937 case Instruction::Or:
938 case Instruction::Xor:
Dan Gohman6896901e2008-06-30 22:03:41 +0000939 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000940 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman6896901e2008-06-30 22:03:41 +0000941 O << ')';
Anton Korobeynikov95cc3e02007-12-18 20:53:41 +0000942 switch (Opcode) {
943 case Instruction::Add:
944 O << " + ";
945 break;
946 case Instruction::Sub:
947 O << " - ";
948 break;
949 case Instruction::And:
950 O << " & ";
951 break;
952 case Instruction::Or:
953 O << " | ";
954 break;
955 case Instruction::Xor:
956 O << " ^ ";
957 break;
958 default:
959 break;
960 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000961 O << '(';
Chris Lattnerbb644e32005-11-21 07:51:36 +0000962 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman6896901e2008-06-30 22:03:41 +0000963 O << ')';
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000964 break;
965 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000966 llvm_unreachable("Unsupported operator!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000967 }
968 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000969 llvm_unreachable("Unknown constant value!");
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000970 }
971}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000972
Chris Lattner55a6d902005-11-10 18:06:33 +0000973/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000974/// the predicate isString is true.
975///
David Greenea31f96c2009-07-14 20:18:05 +0000976static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner55a6d902005-11-10 18:06:33 +0000977 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000978 assert(CVA->isString() && "Array is not string compatible!");
979
Dan Gohman6896901e2008-06-30 22:03:41 +0000980 O << '\"';
Chris Lattner55a6d902005-11-10 18:06:33 +0000981 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000982 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000983 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskey1c055e82007-01-25 15:12:02 +0000984 printStringChar(O, C);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000985 }
Dan Gohman6896901e2008-06-30 22:03:41 +0000986 O << '\"';
Chris Lattner8452a1f2004-08-17 06:36:49 +0000987}
988
Jeff Cohen24a62a92006-05-02 01:16:28 +0000989/// EmitString - Emit a zero-byte-terminated string constant.
990///
991void AsmPrinter::EmitString(const ConstantArray *CVA) const {
992 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000993 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000994 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000995 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000996 printAsCString(O, CVA, NumElts-1);
997 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000998 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000999 printAsCString(O, CVA, NumElts);
1000 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001001 O << '\n';
Jeff Cohen24a62a92006-05-02 01:16:28 +00001002}
1003
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001004void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
1005 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001006 if (CVA->isString()) {
1007 EmitString(CVA);
1008 } else { // Not a string. Print the values in successive locations
1009 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001010 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001011 }
1012}
1013
1014void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
1015 const VectorType *PTy = CP->getType();
1016
1017 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
1018 EmitGlobalConstant(CP->getOperand(I));
1019}
1020
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001021void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1022 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001023 // Print the fields in successive locations. Pad to align if needed!
1024 const TargetData *TD = TM.getTargetData();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001025 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001026 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1027 uint64_t sizeSoFar = 0;
1028 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1029 const Constant* field = CVS->getOperand(i);
1030
1031 // Check if padding is needed and insert one or more 0s.
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001032 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman014caa42008-12-22 21:14:27 +00001033 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1034 - cvsLayout->getElementOffset(i)) - fieldSize;
1035 sizeSoFar += fieldSize + padSize;
1036
1037 // Now print the actual field value.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001038 EmitGlobalConstant(field, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001039
1040 // Insert padding - this may include padding to increase the size of the
1041 // current field up to the ABI size (if the struct is not packed) as well
1042 // as padding to ensure that the next field starts at the right offset.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001043 EmitZeros(padSize, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001044 }
1045 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1046 "Layout of constant struct may be incorrect!");
1047}
1048
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001049void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1050 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001051 // FP Constants are printed as integer constants to avoid losing
1052 // precision...
1053 const TargetData *TD = TM.getTargetData();
1054 if (CFP->getType() == Type::DoubleTy) {
1055 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1056 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Chenga774a992009-03-24 00:17:40 +00001057 if (TAI->getData64bitsDirective(AddrSpace)) {
1058 O << TAI->getData64bitsDirective(AddrSpace) << i;
1059 if (VerboseAsm)
1060 O << '\t' << TAI->getCommentString() << " double value: " << Val;
1061 O << '\n';
1062 } else if (TD->isBigEndian()) {
1063 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1064 if (VerboseAsm)
1065 O << '\t' << TAI->getCommentString()
1066 << " double most significant word " << Val;
1067 O << '\n';
1068 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1069 if (VerboseAsm)
1070 O << '\t' << TAI->getCommentString()
1071 << " double least significant word " << Val;
1072 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001073 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001074 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1075 if (VerboseAsm)
1076 O << '\t' << TAI->getCommentString()
1077 << " double least significant word " << Val;
1078 O << '\n';
1079 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1080 if (VerboseAsm)
1081 O << '\t' << TAI->getCommentString()
1082 << " double most significant word " << Val;
1083 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001084 }
1085 return;
1086 } else if (CFP->getType() == Type::FloatTy) {
1087 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001088 O << TAI->getData32bitsDirective(AddrSpace)
Evan Chenga774a992009-03-24 00:17:40 +00001089 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1090 if (VerboseAsm)
1091 O << '\t' << TAI->getCommentString() << " float " << Val;
1092 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001093 return;
1094 } else if (CFP->getType() == Type::X86_FP80Ty) {
1095 // all long double variants are printed as hex
1096 // api needed to prevent premature destruction
1097 APInt api = CFP->getValueAPF().bitcastToAPInt();
1098 const uint64_t *p = api.getRawData();
1099 // Convert to double so we can print the approximate val as a comment.
1100 APFloat DoubleVal = CFP->getValueAPF();
1101 bool ignored;
1102 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1103 &ignored);
1104 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001105 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1106 if (VerboseAsm)
1107 O << '\t' << TAI->getCommentString()
1108 << " long double most significant halfword of ~"
1109 << DoubleVal.convertToDouble();
1110 O << '\n';
1111 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1112 if (VerboseAsm)
1113 O << '\t' << TAI->getCommentString() << " long double next halfword";
1114 O << '\n';
1115 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1116 if (VerboseAsm)
1117 O << '\t' << TAI->getCommentString() << " long double next halfword";
1118 O << '\n';
1119 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1120 if (VerboseAsm)
1121 O << '\t' << TAI->getCommentString() << " long double next halfword";
1122 O << '\n';
1123 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1124 if (VerboseAsm)
1125 O << '\t' << TAI->getCommentString()
1126 << " long double least significant halfword";
1127 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001128 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001129 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1130 if (VerboseAsm)
1131 O << '\t' << TAI->getCommentString()
1132 << " long double least significant halfword of ~"
1133 << DoubleVal.convertToDouble();
1134 O << '\n';
1135 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1136 if (VerboseAsm)
1137 O << '\t' << TAI->getCommentString()
1138 << " long double next halfword";
1139 O << '\n';
1140 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1141 if (VerboseAsm)
1142 O << '\t' << TAI->getCommentString()
1143 << " long double next halfword";
1144 O << '\n';
1145 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1146 if (VerboseAsm)
1147 O << '\t' << TAI->getCommentString()
1148 << " long double next halfword";
1149 O << '\n';
1150 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1151 if (VerboseAsm)
1152 O << '\t' << TAI->getCommentString()
1153 << " long double most significant halfword";
1154 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001155 }
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001156 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) -
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001157 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001158 return;
1159 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1160 // all long double variants are printed as hex
1161 // api needed to prevent premature destruction
1162 APInt api = CFP->getValueAPF().bitcastToAPInt();
1163 const uint64_t *p = api.getRawData();
1164 if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001165 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1166 if (VerboseAsm)
1167 O << '\t' << TAI->getCommentString()
1168 << " long double most significant word";
1169 O << '\n';
1170 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1171 if (VerboseAsm)
1172 O << '\t' << TAI->getCommentString()
1173 << " long double next word";
1174 O << '\n';
1175 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1176 if (VerboseAsm)
1177 O << '\t' << TAI->getCommentString()
1178 << " long double next word";
1179 O << '\n';
1180 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1181 if (VerboseAsm)
1182 O << '\t' << TAI->getCommentString()
1183 << " long double least significant word";
1184 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001185 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001186 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1187 if (VerboseAsm)
1188 O << '\t' << TAI->getCommentString()
1189 << " long double least significant word";
1190 O << '\n';
1191 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1192 if (VerboseAsm)
1193 O << '\t' << TAI->getCommentString()
1194 << " long double next word";
1195 O << '\n';
1196 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1197 if (VerboseAsm)
1198 O << '\t' << TAI->getCommentString()
1199 << " long double next word";
1200 O << '\n';
1201 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1202 if (VerboseAsm)
1203 O << '\t' << TAI->getCommentString()
1204 << " long double most significant word";
1205 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001206 }
1207 return;
Torok Edwinfbcc6632009-07-14 16:55:14 +00001208 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman014caa42008-12-22 21:14:27 +00001209}
1210
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001211void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1212 unsigned AddrSpace) {
Dan Gohman014caa42008-12-22 21:14:27 +00001213 const TargetData *TD = TM.getTargetData();
1214 unsigned BitWidth = CI->getBitWidth();
1215 assert(isPowerOf2_32(BitWidth) &&
1216 "Non-power-of-2-sized integers not handled!");
1217
1218 // We don't expect assemblers to support integer data directives
1219 // for more than 64 bits, so we emit the data in at most 64-bit
1220 // quantities at a time.
1221 const uint64_t *RawData = CI->getValue().getRawData();
1222 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1223 uint64_t Val;
1224 if (TD->isBigEndian())
1225 Val = RawData[e - i - 1];
1226 else
1227 Val = RawData[i];
1228
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001229 if (TAI->getData64bitsDirective(AddrSpace))
1230 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001231 else if (TD->isBigEndian()) {
Evan Chenga774a992009-03-24 00:17:40 +00001232 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1233 if (VerboseAsm)
1234 O << '\t' << TAI->getCommentString()
1235 << " Double-word most significant word " << Val;
1236 O << '\n';
1237 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1238 if (VerboseAsm)
1239 O << '\t' << TAI->getCommentString()
1240 << " Double-word least significant word " << Val;
1241 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001242 } else {
Evan Chenga774a992009-03-24 00:17:40 +00001243 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1244 if (VerboseAsm)
1245 O << '\t' << TAI->getCommentString()
1246 << " Double-word least significant word " << Val;
1247 O << '\n';
1248 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1249 if (VerboseAsm)
1250 O << '\t' << TAI->getCommentString()
1251 << " Double-word most significant word " << Val;
1252 O << '\n';
Dan Gohman014caa42008-12-22 21:14:27 +00001253 }
1254 }
1255}
1256
Chris Lattnerbb644e32005-11-21 07:51:36 +00001257/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001258void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Anderson20a631f2006-05-03 01:29:57 +00001259 const TargetData *TD = TM.getTargetData();
Dan Gohman014caa42008-12-22 21:14:27 +00001260 const Type *type = CV->getType();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00001261 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001262
Chris Lattner61753bf2004-10-16 18:19:26 +00001263 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001264 EmitZeros(Size, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001265 return;
1266 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Gupta99b1b272009-04-28 16:34:20 +00001267 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001268 return;
1269 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001270 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001271 return;
1272 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001273 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman014caa42008-12-22 21:14:27 +00001274 return;
1275 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1276 // Small integers are handled below; large integers are handled here.
1277 if (Size > 4) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001278 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner8452a1f2004-08-17 06:36:49 +00001279 return;
1280 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001281 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman014caa42008-12-22 21:14:27 +00001282 EmitGlobalConstantVector(CP);
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001283 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +00001284 }
1285
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001286 printDataDirective(type, AddrSpace);
Chris Lattnerbb644e32005-11-21 07:51:36 +00001287 EmitConstantValueOnly(CV);
Evan Chenga774a992009-03-24 00:17:40 +00001288 if (VerboseAsm) {
1289 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1290 SmallString<40> S;
1291 CI->getValue().toStringUnsigned(S, 16);
1292 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1293 }
Scott Michelc0e9ff62008-06-03 15:39:51 +00001294 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001295 O << '\n';
Chris Lattner8452a1f2004-08-17 06:36:49 +00001296}
Chris Lattner061d9e22006-01-27 02:10:10 +00001297
Chris Lattner17f71652008-08-17 07:19:36 +00001298void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001299 // Target doesn't support this yet!
Torok Edwinfbcc6632009-07-14 16:55:14 +00001300 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001301}
1302
Chris Lattnera32814b2006-09-26 23:59:50 +00001303/// PrintSpecial - Print information related to the specified machine instr
1304/// that is independent of the operand, and may be independent of the instr
1305/// itself. This can be useful for portably encoding the comment character
1306/// or other bits of target-specific knowledge into the asmstrings. The
1307/// syntax used is ${:comment}. Targets can override this to add support
1308/// for their own strange codes.
Chris Lattner1522e242009-03-10 05:37:13 +00001309void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattner30b47082006-09-27 00:06:07 +00001310 if (!strcmp(Code, "private")) {
1311 O << TAI->getPrivateGlobalPrefix();
1312 } else if (!strcmp(Code, "comment")) {
Evan Chenga774a992009-03-24 00:17:40 +00001313 if (VerboseAsm)
1314 O << TAI->getCommentString();
Chris Lattnera32814b2006-09-26 23:59:50 +00001315 } else if (!strcmp(Code, "uid")) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001316 // Comparing the address of MI isn't sufficient, because machineinstrs may
1317 // be allocated to the same address across functions.
1318 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1319
Owen Andersonae471cf2009-06-24 22:28:12 +00001320 // If this is a new LastFn instruction, bump the counter.
1321 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnera9428c42007-02-05 21:23:52 +00001322 ++Counter;
1323 LastMI = MI;
Owen Andersonae471cf2009-06-24 22:28:12 +00001324 LastFn = ThisF;
Chris Lattnera9428c42007-02-05 21:23:52 +00001325 }
Chris Lattnera32814b2006-09-26 23:59:50 +00001326 O << Counter;
1327 } else {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001328 std::string msg;
1329 raw_string_ostream Msg(msg);
1330 Msg << "Unknown special formatter '" << Code
Bill Wendlingf3baad32006-12-07 01:30:32 +00001331 << "' for machine instr: " << *MI;
Torok Edwinccb29cd2009-07-11 13:10:19 +00001332 llvm_report_error(Msg.str());
Chris Lattnera32814b2006-09-26 23:59:50 +00001333 }
1334}
1335
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001336/// processDebugLoc - Processes the debug information of each machine
1337/// instruction's DebugLoc.
1338void AsmPrinter::processDebugLoc(DebugLoc DL) {
1339 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1340 if (!DL.isUnknown()) {
Argyrios Kyrtzidis58f38112009-05-07 13:55:51 +00001341 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1342
1343 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1344 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1345 DICompileUnit(CurDLT.CompileUnit)));
1346
1347 PrevDLT = CurDLT;
1348 }
1349 }
1350}
Chris Lattnera32814b2006-09-26 23:59:50 +00001351
Chris Lattner061d9e22006-01-27 02:10:10 +00001352/// printInlineAsm - This method formats and prints the specified machine
1353/// instruction that is an inline asm.
1354void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +00001355 unsigned NumOperands = MI->getNumOperands();
1356
1357 // Count the number of register definitions.
1358 unsigned NumDefs = 0;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001359 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner45456d42006-09-05 20:02:51 +00001360 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +00001361 assert(NumDefs != NumOperands-1 && "No asm string?");
1362
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001363 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001364
1365 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +00001366 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +00001367
Dale Johannesen2b3bc302008-01-29 02:21:21 +00001368 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1369 // These are useful to see where empty asm's wound up.
Chris Lattner3390cbe2006-07-28 00:17:20 +00001370 if (AsmStr[0] == 0) {
Dan Gohman6896901e2008-06-30 22:03:41 +00001371 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattner3390cbe2006-07-28 00:17:20 +00001372 return;
1373 }
1374
Jim Laskeya6211dc2006-09-06 18:34:40 +00001375 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +00001376
Bill Wendlinge21237e2007-01-16 03:42:04 +00001377 // The variant of the current asmprinter.
1378 int AsmPrinterVariant = TAI->getAssemblerDialect();
1379
Chris Lattneraa23fa92006-02-01 22:41:11 +00001380 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1381 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +00001382
Chris Lattneraa23fa92006-02-01 22:41:11 +00001383 while (*LastEmitted) {
1384 switch (*LastEmitted) {
1385 default: {
1386 // Not a special case, emit the string section literally.
1387 const char *LiteralEnd = LastEmitted+1;
1388 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +00001389 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +00001390 ++LiteralEnd;
1391 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1392 O.write(LastEmitted, LiteralEnd-LastEmitted);
1393 LastEmitted = LiteralEnd;
1394 break;
1395 }
Chris Lattnera633c3132006-05-05 21:47:05 +00001396 case '\n':
1397 ++LastEmitted; // Consume newline character.
Dan Gohman6896901e2008-06-30 22:03:41 +00001398 O << '\n'; // Indent code with newline.
Chris Lattnera633c3132006-05-05 21:47:05 +00001399 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001400 case '$': {
1401 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001402 bool Done = true;
1403
1404 // Handle escapes.
1405 switch (*LastEmitted) {
1406 default: Done = false; break;
1407 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +00001408 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1409 O << '$';
1410 ++LastEmitted; // Consume second '$' character.
1411 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001412 case '(': // $( -> same as GCC's { character.
1413 ++LastEmitted; // Consume '(' character.
1414 if (CurVariant != -1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001415 llvm_report_error("Nested variants found in inline asm string: '"
1416 + std::string(AsmStr) + "'");
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001417 }
1418 CurVariant = 0; // We're in the first variant now.
1419 break;
1420 case '|':
1421 ++LastEmitted; // consume '|' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001422 if (CurVariant == -1)
1423 O << '|'; // this is gcc's behavior for | outside a variant
1424 else
1425 ++CurVariant; // We're in the next variant.
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001426 break;
1427 case ')': // $) -> same as GCC's } char.
1428 ++LastEmitted; // consume ')' character.
Dale Johannesen4f279162008-10-10 21:04:42 +00001429 if (CurVariant == -1)
1430 O << '}'; // this is gcc's behavior for } outside a variant
1431 else
1432 CurVariant = -1;
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001433 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001434 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +00001435 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001436
1437 bool HasCurlyBraces = false;
1438 if (*LastEmitted == '{') { // ${variable}
1439 ++LastEmitted; // Consume '{' character.
1440 HasCurlyBraces = true;
1441 }
1442
Chris Lattner1522e242009-03-10 05:37:13 +00001443 // If we have ${:foo}, then this is not a real operand reference, it is a
1444 // "magic" string reference, just like in .td files. Arrange to call
1445 // PrintSpecial.
1446 if (HasCurlyBraces && *LastEmitted == ':') {
1447 ++LastEmitted;
1448 const char *StrStart = LastEmitted;
1449 const char *StrEnd = strchr(StrStart, '}');
1450 if (StrEnd == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001451 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1452 + std::string(AsmStr) + "'");
Chris Lattner1522e242009-03-10 05:37:13 +00001453 }
1454
1455 std::string Val(StrStart, StrEnd);
1456 PrintSpecial(MI, Val.c_str());
1457 LastEmitted = StrEnd+1;
1458 break;
1459 }
1460
Chris Lattneraa23fa92006-02-01 22:41:11 +00001461 const char *IDStart = LastEmitted;
1462 char *IDEnd;
Chris Lattnerd39e3882007-01-23 00:36:17 +00001463 errno = 0;
Chris Lattneraa23fa92006-02-01 22:41:11 +00001464 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1465 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001466 llvm_report_error("Bad $ operand number in inline asm string: '"
1467 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001468 }
1469 LastEmitted = IDEnd;
1470
Chris Lattner34f74c12006-02-06 22:17:23 +00001471 char Modifier[2] = { 0, 0 };
1472
Chris Lattneraa23fa92006-02-01 22:41:11 +00001473 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +00001474 // If we have curly braces, check for a modifier character. This
1475 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1476 if (*LastEmitted == ':') {
1477 ++LastEmitted; // Consume ':' character.
1478 if (*LastEmitted == 0) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001479 llvm_report_error("Bad ${:} expression in inline asm string: '"
1480 + std::string(AsmStr) + "'");
Chris Lattner34f74c12006-02-06 22:17:23 +00001481 }
1482
1483 Modifier[0] = *LastEmitted;
1484 ++LastEmitted; // Consume modifier character.
1485 }
1486
Chris Lattneraa23fa92006-02-01 22:41:11 +00001487 if (*LastEmitted != '}') {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001488 llvm_report_error("Bad ${} expression in inline asm string: '"
1489 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001490 }
1491 ++LastEmitted; // Consume '}' character.
1492 }
1493
1494 if ((unsigned)Val >= NumOperands-1) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001495 llvm_report_error("Invalid $ operand number in inline asm string: '"
1496 + std::string(AsmStr) + "'");
Chris Lattneraa23fa92006-02-01 22:41:11 +00001497 }
1498
Chris Lattner571d9642006-02-23 19:21:04 +00001499 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +00001500 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +00001501 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1502 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001503
1504 bool Error = false;
1505
Chris Lattner571d9642006-02-23 19:21:04 +00001506 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +00001507 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001508 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner81798412007-12-30 20:50:28 +00001509 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng2e559232009-03-20 18:03:34 +00001510 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattner5af3fde2006-02-24 19:50:58 +00001511 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001512
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001513 if (OpNo >= MI->getNumOperands()) {
1514 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +00001515 } else {
Chris Lattner81798412007-12-30 20:50:28 +00001516 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001517 ++OpNo; // Skip over the ID number.
1518
Dale Johannesen4646aa32007-11-05 21:20:28 +00001519 if (Modifier[0]=='l') // labels are target independent
Chris Lattnera5bb3702007-12-30 23:10:15 +00001520 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Chengc7990652008-02-28 00:43:03 +00001521 false, false, false);
Dale Johannesen4646aa32007-11-05 21:20:28 +00001522 else {
1523 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesenc36660d2008-09-24 01:07:17 +00001524 if ((OpFlags & 7) == 4) {
Dale Johannesen4646aa32007-11-05 21:20:28 +00001525 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1526 Modifier[0] ? Modifier : 0);
1527 } else {
1528 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1529 Modifier[0] ? Modifier : 0);
1530 }
Chris Lattner8f8b5e42006-06-08 18:00:47 +00001531 }
Chris Lattner1d08c652006-02-24 20:21:58 +00001532 }
1533 if (Error) {
Torok Edwinccb29cd2009-07-11 13:10:19 +00001534 std::string msg;
1535 raw_string_ostream Msg(msg);
1536 Msg << "Invalid operand found in inline asm: '"
Bill Wendlingf3baad32006-12-07 01:30:32 +00001537 << AsmStr << "'\n";
Torok Edwinccb29cd2009-07-11 13:10:19 +00001538 MI->print(Msg);
1539 llvm_report_error(Msg.str());
Chris Lattneraa23fa92006-02-01 22:41:11 +00001540 }
Chris Lattner571d9642006-02-23 19:21:04 +00001541 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001542 break;
1543 }
Chris Lattneraa23fa92006-02-01 22:41:11 +00001544 }
1545 }
Dan Gohman6896901e2008-06-30 22:03:41 +00001546 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Chris Lattneraa23fa92006-02-01 22:41:11 +00001547}
1548
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001549/// printImplicitDef - This method prints the specified machine instruction
1550/// that is an implicit def.
1551void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Evan Chenga774a992009-03-24 00:17:40 +00001552 if (VerboseAsm)
1553 O << '\t' << TAI->getCommentString() << " implicit-def: "
1554 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng0e7b00d2008-03-15 00:03:38 +00001555}
1556
Jim Laskeyf9e54452007-01-26 14:34:52 +00001557/// printLabel - This method prints a local label used by debug and
1558/// exception handling tables.
1559void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohmanb58aff42008-07-01 00:16:26 +00001560 printLabel(MI->getOperand(0).getImm());
Jim Laskeyf9e54452007-01-26 14:34:52 +00001561}
1562
Evan Chengd6e44ab2008-02-01 09:10:45 +00001563void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng32e53472008-02-02 08:39:46 +00001564 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chengd6e44ab2008-02-01 09:10:45 +00001565}
1566
Evan Chengefd142a2008-02-02 04:07:54 +00001567/// printDeclare - This method prints a local variable declaration used by
1568/// debug tables.
Evan Cheng2cb90682008-02-04 23:06:48 +00001569/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1570/// entry into dwarf table.
Evan Chengefd142a2008-02-02 04:07:54 +00001571void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Pateldd25a9d2009-01-13 21:44:10 +00001572 unsigned FI = MI->getOperand(0).getIndex();
Evan Cheng2cb90682008-02-04 23:06:48 +00001573 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel32d17a12009-04-15 00:10:26 +00001574 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Chengefd142a2008-02-02 04:07:54 +00001575}
1576
Chris Lattneraa23fa92006-02-01 22:41:11 +00001577/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1578/// instruction, using the specified assembler variant. Targets should
1579/// overried this to format as appropriate.
1580bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +00001581 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +00001582 // Target doesn't support this yet!
1583 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +00001584}
Chris Lattner1d08c652006-02-24 20:21:58 +00001585
1586bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1587 unsigned AsmVariant,
1588 const char *ExtraCode) {
1589 // Target doesn't support this yet!
1590 return true;
1591}
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001592
1593/// printBasicBlockLabel - This method prints the label for the specified
1594/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +00001595void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Chengc7990652008-02-28 00:43:03 +00001596 bool printAlign,
Nate Begemanb9d4f832006-05-02 05:37:32 +00001597 bool printColon,
1598 bool printComment) const {
Evan Chengc7990652008-02-28 00:43:03 +00001599 if (printAlign) {
1600 unsigned Align = MBB->getAlignment();
1601 if (Align)
1602 EmitAlignment(Log2_32(Align));
1603 }
1604
Dan Gohman6896901e2008-06-30 22:03:41 +00001605 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Chengcdf36092007-10-14 05:57:21 +00001606 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +00001607 if (printColon)
1608 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +00001609 if (printComment && MBB->getBasicBlock())
Dan Gohman33d0ea22007-07-30 15:06:25 +00001610 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng53495222008-07-08 00:55:58 +00001611 << MBB->getBasicBlock()->getNameStart();
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001612}
Nate Begeman984c1a42006-08-12 21:29:52 +00001613
Evan Cheng797d56f2007-11-09 01:32:10 +00001614/// printPICJumpTableSetLabel - This method prints a set label for the
1615/// specified MachineBasicBlock for a jumptable entry.
1616void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1617 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +00001618 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +00001619 return;
1620
Jim Laskeya6211dc2006-09-06 18:34:40 +00001621 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001622 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001623 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001624 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1625 << '_' << uid << '\n';
Nate Begeman984c1a42006-08-12 21:29:52 +00001626}
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001627
Evan Cheng797d56f2007-11-09 01:32:10 +00001628void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1629 const MachineBasicBlock *MBB) const {
Evan Cheng91f120f2006-11-01 09:23:08 +00001630 if (!TAI->getSetDirective())
1631 return;
1632
1633 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Chengcdf36092007-10-14 05:57:21 +00001634 << getFunctionNumber() << '_' << uid << '_' << uid2
1635 << "_set_" << MBB->getNumber() << ',';
Evan Chengc7990652008-02-28 00:43:03 +00001636 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcdf36092007-10-14 05:57:21 +00001637 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1638 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng91f120f2006-11-01 09:23:08 +00001639}
1640
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001641/// printDataDirective - This method prints the asm directive for the
1642/// specified type.
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001643void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001644 const TargetData *TD = TM.getTargetData();
1645 switch (type->getTypeID()) {
Chris Lattnerfe785582009-07-15 04:42:49 +00001646 case Type::FloatTyID: case Type::DoubleTyID:
1647 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1648 assert(0 && "Should have already output floating point constant.");
1649 default:
1650 assert(0 && "Can't handle printing this type of thing");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001651 case Type::IntegerTyID: {
1652 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1653 if (BitWidth <= 8)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001654 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001655 else if (BitWidth <= 16)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001656 O << TAI->getData16bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001657 else if (BitWidth <= 32)
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001658 O << TAI->getData32bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001659 else if (BitWidth <= 64) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001660 assert(TAI->getData64bitsDirective(AddrSpace) &&
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001661 "Target cannot handle 64-bit constant exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001662 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohmanf9b20542008-09-08 16:40:13 +00001663 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001664 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001665 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001666 break;
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001667 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001668 case Type::PointerTyID:
1669 if (TD->getPointerSize() == 8) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001670 assert(TAI->getData64bitsDirective(AddrSpace) &&
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001671 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001672 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001673 } else if (TD->getPointerSize() == 2) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001674 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta4186ddf2009-01-22 10:14:21 +00001675 } else if (TD->getPointerSize() == 1) {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001676 O << TAI->getData8bitsDirective(AddrSpace);
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001677 } else {
Sanjiv Gupta964a29f2009-01-30 04:25:10 +00001678 O << TAI->getData32bitsDirective(AddrSpace);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001679 }
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001680 break;
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001681 }
1682}
Dale Johannesen915e1542007-02-16 01:54:53 +00001683
Anton Korobeynikoved473292008-08-08 18:25:07 +00001684void AsmPrinter::printVisibility(const std::string& Name,
1685 unsigned Visibility) const {
1686 if (Visibility == GlobalValue::HiddenVisibility) {
1687 if (const char *Directive = TAI->getHiddenDirective())
1688 O << Directive << Name << '\n';
1689 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1690 if (const char *Directive = TAI->getProtectedDirective())
1691 O << Directive << Name << '\n';
1692 }
1693}
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001694
Anton Korobeynikovbff4b372008-11-22 16:15:34 +00001695void AsmPrinter::printOffset(int64_t Offset) const {
1696 if (Offset > 0)
1697 O << '+' << Offset;
1698 else if (Offset < 0)
1699 O << Offset;
1700}
1701
Gordon Henriksend930f912008-08-17 18:44:35 +00001702GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1703 if (!S->usesMetadata())
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001704 return 0;
1705
Gordon Henriksend930f912008-08-17 18:44:35 +00001706 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001707 if (GCPI != GCMetadataPrinters.end())
1708 return GCPI->second;
1709
Gordon Henriksend930f912008-08-17 18:44:35 +00001710 const char *Name = S->getName().c_str();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001711
1712 for (GCMetadataPrinterRegistry::iterator
1713 I = GCMetadataPrinterRegistry::begin(),
1714 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1715 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksend930f912008-08-17 18:44:35 +00001716 GCMetadataPrinter *GMP = I->instantiate();
1717 GMP->S = S;
1718 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1719 return GMP;
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001720 }
1721
Gordon Henriksend930f912008-08-17 18:44:35 +00001722 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +00001723 llvm_unreachable(0);
Gordon Henriksendbe06d32008-08-17 12:08:44 +00001724}
David Greenede544782009-07-13 20:25:48 +00001725
1726/// EmitComments - Pretty-print comments for instructions
1727void AsmPrinter::EmitComments(const MachineInstr &MI) const
1728{
David Greenede9cd442009-07-16 22:24:20 +00001729 if (!MI.getDebugLoc().isUnknown()) {
1730 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
1731
1732 // Print source line info
1733 O.PadToColumn(TAI->getCommentColumn(), 1);
1734 O << TAI->getCommentString() << " SrcLine " << DLT.Line << ":" << DLT.Col;
1735 }
David Greenede544782009-07-13 20:25:48 +00001736}
1737
1738/// EmitComments - Pretty-print comments for instructions
1739void AsmPrinter::EmitComments(const MCInst &MI) const
1740{
David Greenede9cd442009-07-16 22:24:20 +00001741 if (!MI.getDebugLoc().isUnknown()) {
1742 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
1743
1744 // Print source line info
1745 O.PadToColumn(TAI->getCommentColumn(), 1);
1746 O << TAI->getCommentString() << " SrcLine " << DLT.Line << ":" << DLT.Col;
1747 }
David Greenede544782009-07-13 20:25:48 +00001748}