blob: de31840d703b31cd1c7335bc7f357bbfe854aa97 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/Assembly/Writer.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Constants.h"
18#include "llvm/Module.h"
Gordon Henriksenf194af22008-08-17 12:56:54 +000019#include "llvm/CodeGen/GCStrategy.h"
20#include "llvm/CodeGen/GCMetadata.h"
21#include "llvm/CodeGen/GCs.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Support/Mangler.h"
26#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/Streams.h"
28#include "llvm/Target/TargetAsmInfo.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetLowering.h"
31#include "llvm/Target/TargetMachine.h"
Evan Cheng0eeed442008-07-01 23:18:29 +000032#include "llvm/Target/TargetOptions.h"
Evan Cheng3c0eda52008-03-15 00:03:38 +000033#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng6fb06762007-11-09 01:32:10 +000034#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner89b36582008-08-17 07:19:36 +000035#include "llvm/ADT/SmallString.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include <cerrno>
37using namespace llvm;
38
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039char AsmPrinter::ID = 0;
40AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
41 const TargetAsmInfo *T)
Evan Cheng3c0eda52008-03-15 00:03:38 +000042 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o),
43 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Evan Cheng45c1edb2008-02-28 00:43:03 +000044 IsInTextSection(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045{}
46
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000047AsmPrinter::~AsmPrinter() {
48 for (gcp_iterator I = GCMetadataPrinters.begin(),
49 E = GCMetadataPrinters.end(); I != E; ++I)
50 delete I->second;
51}
52
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053std::string AsmPrinter::getSectionForFunction(const Function &F) const {
54 return TAI->getTextSection();
55}
56
57
58/// SwitchToTextSection - Switch to the specified text section of the executable
59/// if we are not already in it!
60///
61void AsmPrinter::SwitchToTextSection(const char *NewSection,
62 const GlobalValue *GV) {
63 std::string NS;
64 if (GV && GV->hasSection())
65 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
66 else
67 NS = NewSection;
68
69 // If we're already in this section, we're done.
70 if (CurrentSection == NS) return;
71
72 // Close the current section, if applicable.
73 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000074 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075
76 CurrentSection = NS;
77
78 if (!CurrentSection.empty())
79 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Cheng45c1edb2008-02-28 00:43:03 +000080
81 IsInTextSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082}
83
84/// SwitchToDataSection - Switch to the specified data section of the executable
85/// if we are not already in it!
86///
87void AsmPrinter::SwitchToDataSection(const char *NewSection,
88 const GlobalValue *GV) {
89 std::string NS;
90 if (GV && GV->hasSection())
91 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
92 else
93 NS = NewSection;
94
95 // If we're already in this section, we're done.
96 if (CurrentSection == NS) return;
97
98 // Close the current section, if applicable.
99 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000100 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101
102 CurrentSection = NS;
103
104 if (!CurrentSection.empty())
105 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000106
107 IsInTextSection = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108}
109
110
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000111void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
112 MachineFunctionPass::getAnalysisUsage(AU);
113 AU.addRequired<CollectorModuleMetadata>();
114}
115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116bool AsmPrinter::doInitialization(Module &M) {
117 Mang = new Mangler(M, TAI->getGlobalPrefix());
118
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000119 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
120 assert(CMM && "AsmPrinter didn't require CollectorModuleMetadata?");
121 for (CollectorModuleMetadata::iterator I = CMM->begin(),
122 E = CMM->end(); I != E; ++I)
Gordon Henriksen3385c9b2008-08-17 12:08:44 +0000123 if (GCMetadataPrinter *GCP = GetOrCreateGCPrinter(*I))
124 GCP->beginAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000125
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 if (!M.getModuleInlineAsm().empty())
127 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
128 << M.getModuleInlineAsm()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000129 << '\n' << TAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 << " End of file scope inline assembly\n";
131
132 SwitchToDataSection(""); // Reset back to no section.
133
Evan Chengc439a852008-02-04 23:06:48 +0000134 MMI = getAnalysisToUpdate<MachineModuleInfo>();
135 if (MMI) MMI->AnalyzeModule(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136
137 return false;
138}
139
140bool AsmPrinter::doFinalization(Module &M) {
141 if (TAI->getWeakRefDirective()) {
142 if (!ExtWeakSymbols.empty())
143 SwitchToDataSection("");
144
145 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
146 e = ExtWeakSymbols.end(); i != e; ++i) {
147 const GlobalValue *GV = *i;
148 std::string Name = Mang->getValueName(GV);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000149 O << TAI->getWeakRefDirective() << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 }
151 }
152
153 if (TAI->getSetDirective()) {
154 if (!M.alias_empty())
155 SwitchToTextSection(TAI->getTextSection());
156
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000157 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
159 I!=E; ++I) {
160 std::string Name = Mang->getValueName(I);
161 std::string Target;
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000162
163 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
164 Target = Mang->getValueName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000166 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000167 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 else if (I->hasWeakLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000169 O << TAI->getWeakRefDirective() << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 else if (!I->hasInternalLinkage())
171 assert(0 && "Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000172
173 if (I->hasHiddenVisibility()) {
174 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000175 O << Directive << Name << '\n';
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000176 } else if (I->hasProtectedVisibility()) {
177 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000178 O << Directive << Name << '\n';
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000179 }
180
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000181 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000182
183 // If the aliasee has external weak linkage it can be referenced only by
184 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
185 // weak reference in such case.
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000186 if (GV->hasExternalWeakLinkage()) {
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000187 if (TAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << TAI->getWeakRefDirective() << Target << '\n';
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000189 else
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000190 O << "\t.globl\t" << Target << '\n';
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000191 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 }
193 }
194
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000195 CollectorModuleMetadata *CMM = getAnalysisToUpdate<CollectorModuleMetadata>();
196 assert(CMM && "AsmPrinter didn't require CollectorModuleMetadata?");
197 for (CollectorModuleMetadata::iterator I = CMM->end(),
198 E = CMM->begin(); I != E; )
Gordon Henriksen3385c9b2008-08-17 12:08:44 +0000199 if (GCMetadataPrinter *GCP = GetOrCreateGCPrinter(*--I))
200 GCP->finishAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000201
Dan Gohmana65530a2008-05-05 00:28:39 +0000202 // If we don't have any trampolines, then we don't require stack memory
203 // to be executable. Some targets have a directive to declare this.
204 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
205 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
206 if (TAI->getNonexecutableStackDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000207 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 delete Mang; Mang = 0;
210 return false;
211}
212
Bill Wendlinge9ecdcf2007-09-18 09:10:16 +0000213std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendlingef9211a2007-09-18 01:47:22 +0000214 assert(MF && "No machine function?");
Dale Johannesene73bcbb2008-04-02 20:10:52 +0000215 std::string Name = MF->getFunction()->getName();
216 if (Name.empty())
217 Name = Mang->getValueName(MF->getFunction());
218 return Mang->makeNameProper(Name + ".eh", TAI->getGlobalPrefix());
Bill Wendlingef9211a2007-09-18 01:47:22 +0000219}
220
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
222 // What's my mangled name?
223 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000224 IncrementFunctionNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225}
226
227/// EmitConstantPool - Print to the current output stream assembly
228/// representations of the constants in the constant pool MCP. This is
229/// used to print out constants which have been "spilled to memory" by
230/// the code generator.
231///
232void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
233 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
234 if (CP.empty()) return;
235
236 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
237 // in special sections.
238 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
239 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
240 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
241 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
242 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
243 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
244 MachineConstantPoolEntry CPE = CP[i];
245 const Type *Ty = CPE.getType();
246 if (TAI->getFourByteConstantSection() &&
Duncan Sands8157ef42007-11-05 00:04:43 +0000247 TM.getTargetData()->getABITypeSize(Ty) == 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 FourByteCPs.push_back(std::make_pair(CPE, i));
249 else if (TAI->getEightByteConstantSection() &&
Duncan Sands8157ef42007-11-05 00:04:43 +0000250 TM.getTargetData()->getABITypeSize(Ty) == 8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 EightByteCPs.push_back(std::make_pair(CPE, i));
252 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sands8157ef42007-11-05 00:04:43 +0000253 TM.getTargetData()->getABITypeSize(Ty) == 16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 SixteenByteCPs.push_back(std::make_pair(CPE, i));
255 else
256 OtherCPs.push_back(std::make_pair(CPE, i));
257 }
258
259 unsigned Alignment = MCP->getConstantPoolAlignment();
260 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
261 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
262 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
263 SixteenByteCPs);
264 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
265}
266
267void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
268 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
269 if (CP.empty()) return;
270
271 SwitchToDataSection(Section);
272 EmitAlignment(Alignment);
273 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng477013c2007-10-14 05:57:21 +0000274 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000275 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << ' ';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
277 if (CP[i].first.isMachineConstantPoolEntry())
278 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
279 else
280 EmitGlobalConstant(CP[i].first.Val.ConstVal);
281 if (i != e-1) {
282 const Type *Ty = CP[i].first.getType();
283 unsigned EntSize =
Duncan Sands8157ef42007-11-05 00:04:43 +0000284 TM.getTargetData()->getABITypeSize(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
286 // Emit inter-object padding for alignment.
287 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
288 }
289 }
290}
291
292/// EmitJumpTableInfo - Print assembly representations of the jump tables used
293/// by the current function to the current output stream.
294///
295void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
296 MachineFunction &MF) {
297 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
298 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000299
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
301
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 // Pick the directive to use to print the jump table entries, and switch to
303 // the appropriate section.
304 TargetLowering *LoweringInfo = TM.getTargetLowering();
305
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000306 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
307 const Function *F = MF.getFunction();
308 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000310 !JumpTableDataSection ||
311 SectionFlags & SectionFlags::Linkonce) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 // In PIC mode, we need to emit the jump table to the same section as the
313 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000314 // We should also do if the section name is NULL or function is declared in
315 // discardable section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
317 } else {
318 SwitchToDataSection(JumpTableDataSection);
319 }
320
321 EmitAlignment(Log2_32(MJTI->getAlignment()));
322
323 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
324 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
325
326 // If this jump table was deleted, ignore it.
327 if (JTBBs.empty()) continue;
328
329 // For PIC codegen, if possible we want to use the SetDirective to reduce
330 // the number of relocations the assembler will generate for the jump table.
331 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000332 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 if (TAI->getSetDirective() && IsPic)
334 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000335 if (EmittedSets.insert(JTBBs[ii]))
336 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337
338 // On some targets (e.g. darwin) we want to emit two consequtive labels
339 // before each jump table. The first label is never referenced, but tells
340 // the assembler and linker the extents of the jump table object. The
341 // second label is actually referenced by the code.
342 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Cheng477013c2007-10-14 05:57:21 +0000343 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344
Evan Cheng477013c2007-10-14 05:57:21 +0000345 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
346 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347
348 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000349 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 O << '\n';
351 }
352 }
353}
354
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000355void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
356 const MachineBasicBlock *MBB,
357 unsigned uid) const {
358 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
359
360 // Use JumpTableDirective otherwise honor the entry size from the jump table
361 // info.
362 const char *JTEntryDirective = TAI->getJumpTableDirective();
363 bool HadJTEntryDirective = JTEntryDirective != NULL;
364 if (!HadJTEntryDirective) {
365 JTEntryDirective = MJTI->getEntrySize() == 4 ?
366 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
367 }
368
369 O << JTEntryDirective << ' ';
370
371 // If we have emitted set directives for the jump table entries, print
372 // them rather than the entries themselves. If we're emitting PIC, then
373 // emit the table entries as differences between two text section labels.
374 // If we're emitting non-PIC code, then emit the entries as direct
375 // references to the target basic blocks.
376 if (IsPic) {
377 if (TAI->getSetDirective()) {
378 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
379 << '_' << uid << "_set_" << MBB->getNumber();
380 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000381 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000382 // If the arch uses custom Jump Table directives, don't calc relative to
383 // JT
384 if (!HadJTEntryDirective)
385 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
386 << getFunctionNumber() << '_' << uid;
387 }
388 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000389 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000390 }
391}
392
393
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
395/// special global used by LLVM. If so, emit it and return true, otherwise
396/// do nothing and return false.
397bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000398 if (GV->getName() == "llvm.used") {
399 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
400 EmitLLVMUsedList(GV->getInitializer());
401 return true;
402 }
403
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 // Ignore debug and non-emitted data.
405 if (GV->getSection() == "llvm.metadata") return true;
406
407 if (!GV->hasAppendingLinkage()) return false;
408
409 assert(GV->hasInitializer() && "Not a special LLVM global!");
410
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 const TargetData *TD = TM.getTargetData();
412 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
413 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
414 SwitchToDataSection(TAI->getStaticCtorsSection());
415 EmitAlignment(Align, 0);
416 EmitXXStructorList(GV->getInitializer());
417 return true;
418 }
419
420 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
421 SwitchToDataSection(TAI->getStaticDtorsSection());
422 EmitAlignment(Align, 0);
423 EmitXXStructorList(GV->getInitializer());
424 return true;
425 }
426
427 return false;
428}
429
430/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
431/// global in the specified llvm.used list as being used with this directive.
432void AsmPrinter::EmitLLVMUsedList(Constant *List) {
433 const char *Directive = TAI->getUsedDirective();
434
435 // Should be an array of 'sbyte*'.
436 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
437 if (InitList == 0) return;
438
439 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
440 O << Directive;
441 EmitConstantValueOnly(InitList->getOperand(i));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000442 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 }
444}
445
446/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
447/// function pointers, ignoring the init priority.
448void AsmPrinter::EmitXXStructorList(Constant *List) {
449 // Should be an array of '{ int, void ()* }' structs. The first value is the
450 // init priority, which we ignore.
451 if (!isa<ConstantArray>(List)) return;
452 ConstantArray *InitList = cast<ConstantArray>(List);
453 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
454 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
455 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
456
457 if (CS->getOperand(1)->isNullValue())
458 return; // Found a null terminator, exit printing.
459 // Emit the function pointer.
460 EmitGlobalConstant(CS->getOperand(1));
461 }
462}
463
464/// getGlobalLinkName - Returns the asm/link name of of the specified
465/// global variable. Should be overridden by each target asm printer to
466/// generate the appropriate value.
467const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
468 std::string LinkName;
469
470 if (isa<Function>(GV)) {
471 LinkName += TAI->getFunctionAddrPrefix();
472 LinkName += Mang->getValueName(GV);
473 LinkName += TAI->getFunctionAddrSuffix();
474 } else {
475 LinkName += TAI->getGlobalVarAddrPrefix();
476 LinkName += Mang->getValueName(GV);
477 LinkName += TAI->getGlobalVarAddrSuffix();
478 }
479
480 return LinkName;
481}
482
483/// EmitExternalGlobal - Emit the external reference to a global variable.
484/// Should be overridden if an indirect reference should be used.
485void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
486 O << getGlobalLinkName(GV);
487}
488
489
490
491//===----------------------------------------------------------------------===//
492/// LEB 128 number encoding.
493
494/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
495/// representing an unsigned leb128 value.
496void AsmPrinter::PrintULEB128(unsigned Value) const {
497 do {
498 unsigned Byte = Value & 0x7f;
499 Value >>= 7;
500 if (Value) Byte |= 0x80;
501 O << "0x" << std::hex << Byte << std::dec;
502 if (Value) O << ", ";
503 } while (Value);
504}
505
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
507/// representing a signed leb128 value.
508void AsmPrinter::PrintSLEB128(int Value) const {
509 int Sign = Value >> (8 * sizeof(Value) - 1);
510 bool IsMore;
aslc200b112008-08-16 12:57:46 +0000511
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 do {
513 unsigned Byte = Value & 0x7f;
514 Value >>= 7;
515 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
516 if (IsMore) Byte |= 0x80;
517 O << "0x" << std::hex << Byte << std::dec;
518 if (IsMore) O << ", ";
519 } while (IsMore);
520}
521
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522//===--------------------------------------------------------------------===//
523// Emission and print routines
524//
525
526/// PrintHex - Print a value as a hexidecimal value.
527///
528void AsmPrinter::PrintHex(int Value) const {
529 O << "0x" << std::hex << Value << std::dec;
530}
531
532/// EOL - Print a newline character to asm stream. If a comment is present
533/// then it will be printed first. Comments should not contain '\n'.
534void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000535 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000537
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000539 if (VerboseAsm && !Comment.empty()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000540 O << '\t'
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 << TAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000542 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 << Comment;
544 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000545 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546}
547
Owen Anderson367bfbb2008-07-01 21:16:27 +0000548void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000549 if (VerboseAsm && *Comment) {
Owen Anderson367bfbb2008-07-01 21:16:27 +0000550 O << '\t'
551 << TAI->getCommentString()
552 << ' '
553 << Comment;
554 }
555 O << '\n';
556}
557
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
559/// unsigned leb128 value.
560void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
561 if (TAI->hasLEB128()) {
562 O << "\t.uleb128\t"
563 << Value;
564 } else {
565 O << TAI->getData8bitsDirective();
566 PrintULEB128(Value);
567 }
568}
569
570/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
571/// signed leb128 value.
572void AsmPrinter::EmitSLEB128Bytes(int Value) const {
573 if (TAI->hasLEB128()) {
574 O << "\t.sleb128\t"
575 << Value;
576 } else {
577 O << TAI->getData8bitsDirective();
578 PrintSLEB128(Value);
579 }
580}
581
582/// EmitInt8 - Emit a byte directive and value.
583///
584void AsmPrinter::EmitInt8(int Value) const {
585 O << TAI->getData8bitsDirective();
586 PrintHex(Value & 0xFF);
587}
588
589/// EmitInt16 - Emit a short directive and value.
590///
591void AsmPrinter::EmitInt16(int Value) const {
592 O << TAI->getData16bitsDirective();
593 PrintHex(Value & 0xFFFF);
594}
595
596/// EmitInt32 - Emit a long directive and value.
597///
598void AsmPrinter::EmitInt32(int Value) const {
599 O << TAI->getData32bitsDirective();
600 PrintHex(Value);
601}
602
603/// EmitInt64 - Emit a long long directive and value.
604///
605void AsmPrinter::EmitInt64(uint64_t Value) const {
606 if (TAI->getData64bitsDirective()) {
607 O << TAI->getData64bitsDirective();
608 PrintHex(Value);
609 } else {
610 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000611 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 EmitInt32(unsigned(Value));
613 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000614 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 EmitInt32(unsigned(Value >> 32));
616 }
617 }
618}
619
620/// toOctal - Convert the low order bits of X into an octal digit.
621///
622static inline char toOctal(int X) {
623 return (X&7)+'0';
624}
625
626/// printStringChar - Print a char, escaped if necessary.
627///
628static void printStringChar(std::ostream &O, unsigned char C) {
629 if (C == '"') {
630 O << "\\\"";
631 } else if (C == '\\') {
632 O << "\\\\";
633 } else if (isprint(C)) {
634 O << C;
635 } else {
636 switch(C) {
637 case '\b': O << "\\b"; break;
638 case '\f': O << "\\f"; break;
639 case '\n': O << "\\n"; break;
640 case '\r': O << "\\r"; break;
641 case '\t': O << "\\t"; break;
642 default:
643 O << '\\';
644 O << toOctal(C >> 6);
645 O << toOctal(C >> 3);
646 O << toOctal(C >> 0);
647 break;
648 }
649 }
650}
651
652/// EmitString - Emit a string with quotes and a null terminator.
653/// Special characters are emitted properly.
654/// \literal (Eg. '\t') \endliteral
655void AsmPrinter::EmitString(const std::string &String) const {
656 const char* AscizDirective = TAI->getAscizDirective();
657 if (AscizDirective)
658 O << AscizDirective;
659 else
660 O << TAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000661 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 for (unsigned i = 0, N = String.size(); i < N; ++i) {
663 unsigned char C = String[i];
664 printStringChar(O, C);
665 }
666 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000667 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 else
669 O << "\\0\"";
670}
671
672
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000673/// EmitFile - Emit a .file directive.
674void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
675 O << "\t.file\t" << Number << " \"";
676 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
677 unsigned char C = Name[i];
678 printStringChar(O, C);
679 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000680 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000681}
682
683
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684//===----------------------------------------------------------------------===//
685
686// EmitAlignment - Emit an alignment directive to the specified power of
687// two boundary. For example, if you pass in 3 here, you will get an 8
688// byte alignment. If a global value is specified, and if that global has
689// an explicit alignment requested, it will unconditionally override the
690// alignment request. However, if ForcedAlignBits is specified, this value
691// has final say: the ultimate alignment will be the max of ForcedAlignBits
692// and the alignment computed with NumBits and the global.
693//
694// The algorithm is:
695// Align = NumBits;
696// if (GV && GV->hasalignment) Align = GV->getalignment();
697// Align = std::max(Align, ForcedAlignBits);
698//
699void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000700 unsigned ForcedAlignBits,
701 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 if (GV && GV->getAlignment())
703 NumBits = Log2_32(GV->getAlignment());
704 NumBits = std::max(NumBits, ForcedAlignBits);
705
706 if (NumBits == 0) return; // No need to emit alignment.
707 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Chengc1f41aa2007-07-25 23:35:07 +0000708 O << TAI->getAlignDirective() << NumBits;
Evan Cheng45c1edb2008-02-28 00:43:03 +0000709
710 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng7e7d1942008-02-29 19:36:59 +0000711 UseFillExpr &= IsInTextSection && FillValue;
Evan Chengc1f41aa2007-07-25 23:35:07 +0000712 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000713 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714}
715
716
717/// EmitZeros - Emit a block of zeros.
718///
719void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
720 if (NumZeros) {
721 if (TAI->getZeroDirective()) {
722 O << TAI->getZeroDirective() << NumZeros;
723 if (TAI->getZeroDirectiveSuffix())
724 O << TAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000725 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 } else {
727 for (; NumZeros; --NumZeros)
728 O << TAI->getData8bitsDirective() << "0\n";
729 }
730 }
731}
732
733// Print out the specified constant, without a storage class. Only the
734// constants valid in constant expressions can occur here.
735void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
736 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000737 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000739 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
741 // This is a constant address for a global variable or function. Use the
742 // name of the variable or function as the address value, possibly
743 // decorating it with GlobalVarAddrPrefix/Suffix or
744 // FunctionAddrPrefix/Suffix (these all default to "" )
745 if (isa<Function>(GV)) {
746 O << TAI->getFunctionAddrPrefix()
747 << Mang->getValueName(GV)
748 << TAI->getFunctionAddrSuffix();
749 } else {
750 O << TAI->getGlobalVarAddrPrefix()
751 << Mang->getValueName(GV)
752 << TAI->getGlobalVarAddrSuffix();
753 }
754 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
755 const TargetData *TD = TM.getTargetData();
756 unsigned Opcode = CE->getOpcode();
757 switch (Opcode) {
758 case Instruction::GetElementPtr: {
759 // generate a symbolic expression for the byte address
760 const Constant *ptrVal = CE->getOperand(0);
761 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
762 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
763 idxVec.size())) {
764 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000765 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 EmitConstantValueOnly(ptrVal);
767 if (Offset > 0)
768 O << ") + " << Offset;
769 else if (Offset < 0)
770 O << ") - " << -Offset;
771 } else {
772 EmitConstantValueOnly(ptrVal);
773 }
774 break;
775 }
776 case Instruction::Trunc:
777 case Instruction::ZExt:
778 case Instruction::SExt:
779 case Instruction::FPTrunc:
780 case Instruction::FPExt:
781 case Instruction::UIToFP:
782 case Instruction::SIToFP:
783 case Instruction::FPToUI:
784 case Instruction::FPToSI:
785 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
786 break;
787 case Instruction::BitCast:
788 return EmitConstantValueOnly(CE->getOperand(0));
789
790 case Instruction::IntToPtr: {
791 // Handle casts to pointers by changing them into casts to the appropriate
792 // integer type. This promotes constant folding and simplifies this code.
793 Constant *Op = CE->getOperand(0);
794 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
795 return EmitConstantValueOnly(Op);
796 }
797
798
799 case Instruction::PtrToInt: {
800 // Support only foldable casts to/from pointers that can be eliminated by
801 // changing the pointer to the appropriately sized integer type.
802 Constant *Op = CE->getOperand(0);
803 const Type *Ty = CE->getType();
804
805 // We can emit the pointer value into this slot if the slot is an
806 // integer slot greater or equal to the size of the pointer.
Nick Lewycky95353ad2008-08-08 06:34:07 +0000807 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000809
810 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000812 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
Chris Lattner89b36582008-08-17 07:19:36 +0000813
814 SmallString<40> S;
815 ptrMask.toStringUnsigned(S);
816 O << ") & " << S.c_str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 break;
818 }
819 case Instruction::Add:
820 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000821 case Instruction::And:
822 case Instruction::Or:
823 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000826 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000827 switch (Opcode) {
828 case Instruction::Add:
829 O << " + ";
830 break;
831 case Instruction::Sub:
832 O << " - ";
833 break;
834 case Instruction::And:
835 O << " & ";
836 break;
837 case Instruction::Or:
838 O << " | ";
839 break;
840 case Instruction::Xor:
841 O << " ^ ";
842 break;
843 default:
844 break;
845 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000846 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000848 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 break;
850 default:
851 assert(0 && "Unsupported operator!");
852 }
853 } else {
854 assert(0 && "Unknown constant value!");
855 }
856}
857
858/// printAsCString - Print the specified array as a C compatible string, only if
859/// the predicate isString is true.
860///
861static void printAsCString(std::ostream &O, const ConstantArray *CVA,
862 unsigned LastElt) {
863 assert(CVA->isString() && "Array is not string compatible!");
864
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000865 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 for (unsigned i = 0; i != LastElt; ++i) {
867 unsigned char C =
868 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
869 printStringChar(O, C);
870 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000871 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872}
873
874/// EmitString - Emit a zero-byte-terminated string constant.
875///
876void AsmPrinter::EmitString(const ConstantArray *CVA) const {
877 unsigned NumElts = CVA->getNumOperands();
878 if (TAI->getAscizDirective() && NumElts &&
879 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
880 O << TAI->getAscizDirective();
881 printAsCString(O, CVA, NumElts-1);
882 } else {
883 O << TAI->getAsciiDirective();
884 printAsCString(O, CVA, NumElts);
885 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000886 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887}
888
889/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sands4afc5752008-06-04 08:21:45 +0000890void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 const TargetData *TD = TM.getTargetData();
Duncan Sands4afc5752008-06-04 08:21:45 +0000892 unsigned Size = TD->getABITypeSize(CV->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893
894 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands8157ef42007-11-05 00:04:43 +0000895 EmitZeros(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 return;
897 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
898 if (CVA->isString()) {
899 EmitString(CVA);
900 } else { // Not a string. Print the values in successive locations
Duncan Sands8157ef42007-11-05 00:04:43 +0000901 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Duncan Sands4afc5752008-06-04 08:21:45 +0000902 EmitGlobalConstant(CVA->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 }
904 return;
905 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
906 // Print the fields in successive locations. Pad to align if needed!
907 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
908 uint64_t sizeSoFar = 0;
909 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
910 const Constant* field = CVS->getOperand(i);
911
912 // Check if padding is needed and insert one or more 0s.
Duncan Sands4afc5752008-06-04 08:21:45 +0000913 uint64_t fieldSize = TD->getABITypeSize(field->getType());
Duncan Sandsc15d58c2007-11-05 18:03:02 +0000914 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 - cvsLayout->getElementOffset(i)) - fieldSize;
916 sizeSoFar += fieldSize + padSize;
917
Duncan Sands4afc5752008-06-04 08:21:45 +0000918 // Now print the actual field value.
919 EmitGlobalConstant(field);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920
Duncan Sandsc15d58c2007-11-05 18:03:02 +0000921 // Insert padding - this may include padding to increase the size of the
922 // current field up to the ABI size (if the struct is not packed) as well
923 // as padding to ensure that the next field starts at the right offset.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 EmitZeros(padSize);
925 }
926 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
927 "Layout of constant struct may be incorrect!");
928 return;
929 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
930 // FP Constants are printed as integer constants to avoid losing
931 // precision...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000933 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000934 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 if (TAI->getData64bitsDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000936 O << TAI->getData64bitsDirective() << i << '\t'
937 << TAI->getCommentString() << " double value: " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 else if (TD->isBigEndian()) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000939 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000940 << '\t' << TAI->getCommentString()
941 << " double most significant word " << Val << '\n';
Dale Johannesen1616e902007-09-11 18:32:33 +0000942 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000943 << '\t' << TAI->getCommentString()
944 << " double least significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +0000946 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000947 << '\t' << TAI->getCommentString()
948 << " double least significant word " << Val << '\n';
Dale Johannesen1616e902007-09-11 18:32:33 +0000949 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000950 << '\t' << TAI->getCommentString()
951 << " double most significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 }
953 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000954 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000955 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
956 O << TAI->getData32bitsDirective()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000957 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000958 << '\t' << TAI->getCommentString() << " float " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000960 } else if (CFP->getType() == Type::X86_FP80Ty) {
961 // all long double variants are printed as hex
Dale Johannesen693aa822007-09-26 23:20:33 +0000962 // api needed to prevent premature destruction
963 APInt api = CFP->getValueAPF().convertToAPInt();
964 const uint64_t *p = api.getRawData();
Chris Lattner00d63062008-01-27 06:09:28 +0000965 APFloat DoubleVal = CFP->getValueAPF();
966 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000967 if (TD->isBigEndian()) {
968 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000969 << '\t' << TAI->getCommentString()
Chris Lattner00d63062008-01-27 06:09:28 +0000970 << " long double most significant halfword of ~"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000971 << DoubleVal.convertToDouble() << '\n';
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000972 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000973 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000974 << " long double next halfword\n";
975 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000976 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000977 << " long double next halfword\n";
978 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000979 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000980 << " long double next halfword\n";
981 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000982 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000983 << " long double least significant halfword\n";
984 } else {
985 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000986 << '\t' << TAI->getCommentString()
Chris Lattner00d63062008-01-27 06:09:28 +0000987 << " long double least significant halfword of ~"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000988 << DoubleVal.convertToDouble() << '\n';
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000989 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000990 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000991 << " long double next halfword\n";
992 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000993 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000994 << " long double next halfword\n";
995 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000996 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000997 << " long double next halfword\n";
998 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000999 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001000 << " long double most significant halfword\n";
1001 }
Duncan Sands8157ef42007-11-05 00:04:43 +00001002 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001003 return;
Dale Johannesend3b6af32007-10-11 23:32:15 +00001004 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1005 // all long double variants are printed as hex
1006 // api needed to prevent premature destruction
1007 APInt api = CFP->getValueAPF().convertToAPInt();
1008 const uint64_t *p = api.getRawData();
1009 if (TD->isBigEndian()) {
1010 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001011 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001012 << " long double most significant word\n";
1013 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001014 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001015 << " long double next word\n";
1016 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001017 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001018 << " long double next word\n";
1019 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001020 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001021 << " long double least significant word\n";
1022 } else {
1023 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001024 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001025 << " long double least significant word\n";
1026 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001027 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001028 << " long double next word\n";
1029 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001030 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001031 << " long double next word\n";
1032 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001033 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001034 << " long double most significant word\n";
1035 }
1036 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001037 } else assert(0 && "Floating point constant type not handled");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 } else if (CV->getType() == Type::Int64Ty) {
1039 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1040 uint64_t Val = CI->getZExtValue();
1041
1042 if (TAI->getData64bitsDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001043 O << TAI->getData64bitsDirective() << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 else if (TD->isBigEndian()) {
1045 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001046 << '\t' << TAI->getCommentString()
1047 << " Double-word most significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001049 << '\t' << TAI->getCommentString()
1050 << " Double-word least significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 } else {
1052 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001053 << '\t' << TAI->getCommentString()
1054 << " Double-word least significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001056 << '\t' << TAI->getCommentString()
1057 << " Double-word most significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 }
1059 return;
1060 }
1061 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1062 const VectorType *PTy = CP->getType();
1063
1064 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sands4afc5752008-06-04 08:21:45 +00001065 EmitGlobalConstant(CP->getOperand(I));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066
1067 return;
1068 }
1069
1070 const Type *type = CV->getType();
1071 printDataDirective(type);
1072 EmitConstantValueOnly(CV);
Scott Michele067c3c2008-06-03 15:39:51 +00001073 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner89b36582008-08-17 07:19:36 +00001074 SmallString<40> S;
1075 CI->getValue().toStringUnsigned(S, 16);
1076 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
Scott Michele067c3c2008-06-03 15:39:51 +00001077 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001078 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079}
1080
Chris Lattner89b36582008-08-17 07:19:36 +00001081void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 // Target doesn't support this yet!
1083 abort();
1084}
1085
1086/// PrintSpecial - Print information related to the specified machine instr
1087/// that is independent of the operand, and may be independent of the instr
1088/// itself. This can be useful for portably encoding the comment character
1089/// or other bits of target-specific knowledge into the asmstrings. The
1090/// syntax used is ${:comment}. Targets can override this to add support
1091/// for their own strange codes.
1092void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
1093 if (!strcmp(Code, "private")) {
1094 O << TAI->getPrivateGlobalPrefix();
1095 } else if (!strcmp(Code, "comment")) {
1096 O << TAI->getCommentString();
1097 } else if (!strcmp(Code, "uid")) {
1098 // Assign a unique ID to this machine instruction.
1099 static const MachineInstr *LastMI = 0;
1100 static const Function *F = 0;
1101 static unsigned Counter = 0U-1;
1102
1103 // Comparing the address of MI isn't sufficient, because machineinstrs may
1104 // be allocated to the same address across functions.
1105 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1106
1107 // If this is a new machine instruction, bump the counter.
1108 if (LastMI != MI || F != ThisF) {
1109 ++Counter;
1110 LastMI = MI;
1111 F = ThisF;
1112 }
1113 O << Counter;
1114 } else {
1115 cerr << "Unknown special formatter '" << Code
1116 << "' for machine instr: " << *MI;
1117 exit(1);
1118 }
1119}
1120
1121
1122/// printInlineAsm - This method formats and prints the specified machine
1123/// instruction that is an inline asm.
1124void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1125 unsigned NumOperands = MI->getNumOperands();
1126
1127 // Count the number of register definitions.
1128 unsigned NumDefs = 0;
Dan Gohman38a9a9f2007-09-14 20:33:02 +00001129 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 ++NumDefs)
1131 assert(NumDefs != NumOperands-1 && "No asm string?");
1132
1133 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
1134
1135 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1136 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1137
Dale Johannesene99fc902008-01-29 02:21:21 +00001138 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1139 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 if (AsmStr[0] == 0) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001141 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001142 return;
1143 }
1144
1145 O << TAI->getInlineAsmStart() << "\n\t";
1146
1147 // The variant of the current asmprinter.
1148 int AsmPrinterVariant = TAI->getAssemblerDialect();
1149
1150 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1151 const char *LastEmitted = AsmStr; // One past the last character emitted.
1152
1153 while (*LastEmitted) {
1154 switch (*LastEmitted) {
1155 default: {
1156 // Not a special case, emit the string section literally.
1157 const char *LiteralEnd = LastEmitted+1;
1158 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1159 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1160 ++LiteralEnd;
1161 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1162 O.write(LastEmitted, LiteralEnd-LastEmitted);
1163 LastEmitted = LiteralEnd;
1164 break;
1165 }
1166 case '\n':
1167 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001168 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169 break;
1170 case '$': {
1171 ++LastEmitted; // Consume '$' character.
1172 bool Done = true;
1173
1174 // Handle escapes.
1175 switch (*LastEmitted) {
1176 default: Done = false; break;
1177 case '$': // $$ -> $
1178 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1179 O << '$';
1180 ++LastEmitted; // Consume second '$' character.
1181 break;
1182 case '(': // $( -> same as GCC's { character.
1183 ++LastEmitted; // Consume '(' character.
1184 if (CurVariant != -1) {
1185 cerr << "Nested variants found in inline asm string: '"
1186 << AsmStr << "'\n";
1187 exit(1);
1188 }
1189 CurVariant = 0; // We're in the first variant now.
1190 break;
1191 case '|':
1192 ++LastEmitted; // consume '|' character.
1193 if (CurVariant == -1) {
1194 cerr << "Found '|' character outside of variant in inline asm "
1195 << "string: '" << AsmStr << "'\n";
1196 exit(1);
1197 }
1198 ++CurVariant; // We're in the next variant.
1199 break;
1200 case ')': // $) -> same as GCC's } char.
1201 ++LastEmitted; // consume ')' character.
1202 if (CurVariant == -1) {
1203 cerr << "Found '}' character outside of variant in inline asm "
1204 << "string: '" << AsmStr << "'\n";
1205 exit(1);
1206 }
1207 CurVariant = -1;
1208 break;
1209 }
1210 if (Done) break;
1211
1212 bool HasCurlyBraces = false;
1213 if (*LastEmitted == '{') { // ${variable}
1214 ++LastEmitted; // Consume '{' character.
1215 HasCurlyBraces = true;
1216 }
1217
1218 const char *IDStart = LastEmitted;
1219 char *IDEnd;
1220 errno = 0;
1221 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1222 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
1223 cerr << "Bad $ operand number in inline asm string: '"
1224 << AsmStr << "'\n";
1225 exit(1);
1226 }
1227 LastEmitted = IDEnd;
1228
1229 char Modifier[2] = { 0, 0 };
1230
1231 if (HasCurlyBraces) {
1232 // If we have curly braces, check for a modifier character. This
1233 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1234 if (*LastEmitted == ':') {
1235 ++LastEmitted; // Consume ':' character.
1236 if (*LastEmitted == 0) {
1237 cerr << "Bad ${:} expression in inline asm string: '"
1238 << AsmStr << "'\n";
1239 exit(1);
1240 }
1241
1242 Modifier[0] = *LastEmitted;
1243 ++LastEmitted; // Consume modifier character.
1244 }
1245
1246 if (*LastEmitted != '}') {
1247 cerr << "Bad ${} expression in inline asm string: '"
1248 << AsmStr << "'\n";
1249 exit(1);
1250 }
1251 ++LastEmitted; // Consume '}' character.
1252 }
1253
1254 if ((unsigned)Val >= NumOperands-1) {
1255 cerr << "Invalid $ operand number in inline asm string: '"
1256 << AsmStr << "'\n";
1257 exit(1);
1258 }
1259
1260 // Okay, we finally have a value number. Ask the target to print this
1261 // operand!
1262 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1263 unsigned OpNo = 1;
1264
1265 bool Error = false;
1266
1267 // Scan to find the machine operand number for the operand.
1268 for (; Val; --Val) {
1269 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001270 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 OpNo += (OpFlags >> 3) + 1;
1272 }
1273
1274 if (OpNo >= MI->getNumOperands()) {
1275 Error = true;
1276 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001277 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278 ++OpNo; // Skip over the ID number.
1279
Dale Johannesencfb19e62007-11-05 21:20:28 +00001280 if (Modifier[0]=='l') // labels are target independent
Chris Lattner6017d482007-12-30 23:10:15 +00001281 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Cheng45c1edb2008-02-28 00:43:03 +00001282 false, false, false);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001283 else {
1284 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1285 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1286 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1287 Modifier[0] ? Modifier : 0);
1288 } else {
1289 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1290 Modifier[0] ? Modifier : 0);
1291 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 }
1293 }
1294 if (Error) {
1295 cerr << "Invalid operand found in inline asm: '"
1296 << AsmStr << "'\n";
1297 MI->dump();
1298 exit(1);
1299 }
1300 }
1301 break;
1302 }
1303 }
1304 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001305 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306}
1307
Evan Cheng3c0eda52008-03-15 00:03:38 +00001308/// printImplicitDef - This method prints the specified machine instruction
1309/// that is an implicit def.
1310void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001311 O << '\t' << TAI->getCommentString() << " implicit-def: "
1312 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng3c0eda52008-03-15 00:03:38 +00001313}
1314
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315/// printLabel - This method prints a local label used by debug and
1316/// exception handling tables.
1317void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001318 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319}
1320
Evan Chenga53c40a2008-02-01 09:10:45 +00001321void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng8b988692008-02-02 08:39:46 +00001322 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chenga53c40a2008-02-01 09:10:45 +00001323}
1324
Evan Cheng2e28d622008-02-02 04:07:54 +00001325/// printDeclare - This method prints a local variable declaration used by
1326/// debug tables.
Evan Chengc439a852008-02-04 23:06:48 +00001327/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1328/// entry into dwarf table.
Evan Cheng2e28d622008-02-02 04:07:54 +00001329void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Evan Chengc439a852008-02-04 23:06:48 +00001330 int FI = MI->getOperand(0).getIndex();
1331 GlobalValue *GV = MI->getOperand(1).getGlobal();
1332 MMI->RecordVariable(GV, FI);
Evan Cheng2e28d622008-02-02 04:07:54 +00001333}
1334
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1336/// instruction, using the specified assembler variant. Targets should
1337/// overried this to format as appropriate.
1338bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1339 unsigned AsmVariant, const char *ExtraCode) {
1340 // Target doesn't support this yet!
1341 return true;
1342}
1343
1344bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1345 unsigned AsmVariant,
1346 const char *ExtraCode) {
1347 // Target doesn't support this yet!
1348 return true;
1349}
1350
1351/// printBasicBlockLabel - This method prints the label for the specified
1352/// MachineBasicBlock
1353void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Cheng45c1edb2008-02-28 00:43:03 +00001354 bool printAlign,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355 bool printColon,
1356 bool printComment) const {
Evan Cheng45c1edb2008-02-28 00:43:03 +00001357 if (printAlign) {
1358 unsigned Align = MBB->getAlignment();
1359 if (Align)
1360 EmitAlignment(Log2_32(Align));
1361 }
1362
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001363 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng477013c2007-10-14 05:57:21 +00001364 << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 if (printColon)
1366 O << ':';
1367 if (printComment && MBB->getBasicBlock())
Dan Gohman0912cda2007-07-30 15:06:25 +00001368 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng76443dc2008-07-08 00:55:58 +00001369 << MBB->getBasicBlock()->getNameStart();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370}
1371
Evan Cheng6fb06762007-11-09 01:32:10 +00001372/// printPICJumpTableSetLabel - This method prints a set label for the
1373/// specified MachineBasicBlock for a jumptable entry.
1374void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1375 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376 if (!TAI->getSetDirective())
1377 return;
1378
1379 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001380 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001381 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001382 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1383 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384}
1385
Evan Cheng6fb06762007-11-09 01:32:10 +00001386void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1387 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 if (!TAI->getSetDirective())
1389 return;
1390
1391 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001392 << getFunctionNumber() << '_' << uid << '_' << uid2
1393 << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001394 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001395 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1396 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397}
1398
1399/// printDataDirective - This method prints the asm directive for the
1400/// specified type.
1401void AsmPrinter::printDataDirective(const Type *type) {
1402 const TargetData *TD = TM.getTargetData();
1403 switch (type->getTypeID()) {
1404 case Type::IntegerTyID: {
1405 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1406 if (BitWidth <= 8)
1407 O << TAI->getData8bitsDirective();
1408 else if (BitWidth <= 16)
1409 O << TAI->getData16bitsDirective();
1410 else if (BitWidth <= 32)
1411 O << TAI->getData32bitsDirective();
1412 else if (BitWidth <= 64) {
1413 assert(TAI->getData64bitsDirective() &&
1414 "Target cannot handle 64-bit constant exprs!");
1415 O << TAI->getData64bitsDirective();
1416 }
1417 break;
1418 }
1419 case Type::PointerTyID:
1420 if (TD->getPointerSize() == 8) {
1421 assert(TAI->getData64bitsDirective() &&
1422 "Target cannot handle 64-bit pointer exprs!");
1423 O << TAI->getData64bitsDirective();
1424 } else {
1425 O << TAI->getData32bitsDirective();
1426 }
1427 break;
1428 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen3b5303b2007-09-28 18:06:58 +00001429 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430 assert (0 && "Should have already output floating point constant.");
1431 default:
1432 assert (0 && "Can't handle printing this type of thing");
1433 break;
1434 }
1435}
1436
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001437void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
1438 const char *Prefix) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001439 if (Name[0]=='\"')
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001440 O << '\"';
1441 O << TAI->getPrivateGlobalPrefix();
1442 if (Prefix) O << Prefix;
1443 if (Name[0]=='\"')
1444 O << '\"';
1445 if (Name[0]=='\"')
1446 O << Name[1];
Dale Johannesena21b5202008-05-19 21:38:18 +00001447 else
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001448 O << Name;
1449 O << Suffix;
1450 if (Name[0]=='\"')
1451 O << '\"';
Dale Johannesena21b5202008-05-19 21:38:18 +00001452}
Evan Cheng76443dc2008-07-08 00:55:58 +00001453
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001454void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
Evan Cheng76443dc2008-07-08 00:55:58 +00001455 printSuffixedName(Name.c_str(), Suffix);
1456}
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001457
1458void AsmPrinter::printVisibility(const std::string& Name,
1459 unsigned Visibility) const {
1460 if (Visibility == GlobalValue::HiddenVisibility) {
1461 if (const char *Directive = TAI->getHiddenDirective())
1462 O << Directive << Name << '\n';
1463 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1464 if (const char *Directive = TAI->getProtectedDirective())
1465 O << Directive << Name << '\n';
1466 }
1467}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001468
1469GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(Collector *C) {
1470 if (!C->usesMetadata())
1471 return 0;
1472
1473 gcp_iterator GCPI = GCMetadataPrinters.find(C);
1474 if (GCPI != GCMetadataPrinters.end())
1475 return GCPI->second;
1476
1477 const char *Name = C->getName().c_str();
1478
1479 for (GCMetadataPrinterRegistry::iterator
1480 I = GCMetadataPrinterRegistry::begin(),
1481 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1482 if (strcmp(Name, I->getName()) == 0) {
1483 GCMetadataPrinter *GCP = I->instantiate();
1484 GCP->Coll = C;
1485 GCMetadataPrinters.insert(std::make_pair(C, GCP));
1486 return GCP;
1487 }
1488
1489 cerr << "no GCMetadataPrinter registered for collector: " << Name << "\n";
1490 abort();
1491}