blob: e3cc3b0a2706e193a088bbc74aaa2c0352906783 [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 Henriksen1aed5992008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/MachineConstantPool.h"
21#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000024#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Target/TargetAsmInfo.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Target/TargetMachine.h"
Evan Cheng0eeed442008-07-01 23:18:29 +000029#include "llvm/Target/TargetOptions.h"
Evan Cheng3c0eda52008-03-15 00:03:38 +000030#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng6fb06762007-11-09 01:32:10 +000031#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner89b36582008-08-17 07:19:36 +000032#include "llvm/ADT/SmallString.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000033#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include <cerrno>
35using namespace llvm;
36
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037char AsmPrinter::ID = 0;
Owen Anderson847b99b2008-08-21 00:14:44 +000038AsmPrinter::AsmPrinter(raw_ostream &o, TargetMachine &tm,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 const TargetAsmInfo *T)
Dan Gohman26f8c272008-09-04 17:05:41 +000040 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Cheng3c0eda52008-03-15 00:03:38 +000041 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Evan Cheng45c1edb2008-02-28 00:43:03 +000042 IsInTextSection(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043{}
44
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000045AsmPrinter::~AsmPrinter() {
46 for (gcp_iterator I = GCMetadataPrinters.begin(),
47 E = GCMetadataPrinters.end(); I != E; ++I)
48 delete I->second;
49}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
51/// SwitchToTextSection - Switch to the specified text section of the executable
52/// if we are not already in it!
53///
54void AsmPrinter::SwitchToTextSection(const char *NewSection,
55 const GlobalValue *GV) {
56 std::string NS;
57 if (GV && GV->hasSection())
58 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
59 else
60 NS = NewSection;
61
62 // If we're already in this section, we're done.
63 if (CurrentSection == NS) return;
64
65 // Close the current section, if applicable.
66 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000067 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
69 CurrentSection = NS;
70
71 if (!CurrentSection.empty())
72 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Cheng45c1edb2008-02-28 00:43:03 +000073
74 IsInTextSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075}
76
77/// SwitchToDataSection - Switch to the specified data section of the executable
78/// if we are not already in it!
79///
80void AsmPrinter::SwitchToDataSection(const char *NewSection,
81 const GlobalValue *GV) {
82 std::string NS;
83 if (GV && GV->hasSection())
84 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
85 else
86 NS = NewSection;
87
88 // If we're already in this section, we're done.
89 if (CurrentSection == NS) return;
90
91 // Close the current section, if applicable.
92 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000093 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094
95 CurrentSection = NS;
96
97 if (!CurrentSection.empty())
98 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Cheng45c1edb2008-02-28 00:43:03 +000099
100 IsInTextSection = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101}
102
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +0000103/// SwitchToSection - Switch to the specified section of the executable if we
104/// are not already in it!
105void AsmPrinter::SwitchToSection(const Section* NS) {
106 const std::string& NewSection = NS->getName();
107
108 // If we're already in this section, we're done.
109 if (CurrentSection == NewSection) return;
110
111 // Close the current section, if applicable.
112 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
113 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
114
115 // FIXME: Make CurrentSection a Section* in the future
116 CurrentSection = NewSection;
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000117 CurrentSection_ = NS;
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +0000118
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000119 if (!CurrentSection.empty()) {
120 // If section is named we need to switch into it via special '.section'
121 // directive and also append funky flags. Otherwise - section name is just
122 // some magic assembler directive.
123 if (NS->isNamed())
124 O << TAI->getSwitchToSectionDirective()
125 << CurrentSection
126 << TAI->getSectionFlags(NS->getFlags());
127 else
128 O << CurrentSection;
129 O << TAI->getDataSectionStartSuffix() << '\n';
130 }
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +0000131
132 IsInTextSection = (NS->getFlags() & SectionFlags::Code);
133}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000135void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
136 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000137 AU.addRequired<GCModuleInfo>();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000138}
139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140bool AsmPrinter::doInitialization(Module &M) {
141 Mang = new Mangler(M, TAI->getGlobalPrefix());
142
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000143 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
144 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000145
146 if (TAI->hasSingleParameterDotFile()) {
147 /* Very minimal debug info. It is ignored if we emit actual
148 debug info. If we don't, this at helps the user find where
149 a function came from. */
150 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
151 }
152
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000153 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
154 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
155 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000156
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 if (!M.getModuleInlineAsm().empty())
158 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
159 << M.getModuleInlineAsm()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000160 << '\n' << TAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 << " End of file scope inline assembly\n";
162
163 SwitchToDataSection(""); // Reset back to no section.
164
Evan Chengc439a852008-02-04 23:06:48 +0000165 MMI = getAnalysisToUpdate<MachineModuleInfo>();
166 if (MMI) MMI->AnalyzeModule(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167
168 return false;
169}
170
171bool AsmPrinter::doFinalization(Module &M) {
172 if (TAI->getWeakRefDirective()) {
173 if (!ExtWeakSymbols.empty())
174 SwitchToDataSection("");
175
176 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
177 e = ExtWeakSymbols.end(); i != e; ++i) {
178 const GlobalValue *GV = *i;
179 std::string Name = Mang->getValueName(GV);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << TAI->getWeakRefDirective() << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 }
182 }
183
184 if (TAI->getSetDirective()) {
185 if (!M.alias_empty())
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000186 SwitchToSection(TAI->getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
190 I!=E; ++I) {
191 std::string Name = Mang->getValueName(I);
192 std::string Target;
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000193
194 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
195 Target = Mang->getValueName(GV);
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000196
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000197 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000198 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 else if (I->hasWeakLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000200 O << TAI->getWeakRefDirective() << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 else if (!I->hasInternalLinkage())
202 assert(0 && "Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000203
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000204 printVisibility(Name, I->getVisibility());
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000205
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000206 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000207
208 // If the aliasee has external weak linkage it can be referenced only by
209 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
210 // weak reference in such case.
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000211 if (GV->hasExternalWeakLinkage()) {
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000212 if (TAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000213 O << TAI->getWeakRefDirective() << Target << '\n';
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000214 else
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000215 O << "\t.globl\t" << Target << '\n';
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000216 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 }
218 }
219
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000220 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
221 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
222 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
223 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
224 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000225
Dan Gohmana65530a2008-05-05 00:28:39 +0000226 // If we don't have any trampolines, then we don't require stack memory
227 // to be executable. Some targets have a directive to declare this.
228 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
229 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
230 if (TAI->getNonexecutableStackDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000231 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 delete Mang; Mang = 0;
234 return false;
235}
236
Bill Wendlinge9ecdcf2007-09-18 09:10:16 +0000237std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendlingef9211a2007-09-18 01:47:22 +0000238 assert(MF && "No machine function?");
Dale Johannesene73bcbb2008-04-02 20:10:52 +0000239 std::string Name = MF->getFunction()->getName();
240 if (Name.empty())
241 Name = Mang->getValueName(MF->getFunction());
Rafael Espindola1a931842008-12-19 10:55:56 +0000242 return Mang->makeNameProper(TAI->getEHGlobalPrefix() +
243 Name + ".eh", TAI->getGlobalPrefix());
Bill Wendlingef9211a2007-09-18 01:47:22 +0000244}
245
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
247 // What's my mangled name?
248 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000249 IncrementFunctionNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250}
251
252/// EmitConstantPool - Print to the current output stream assembly
253/// representations of the constants in the constant pool MCP. This is
254/// used to print out constants which have been "spilled to memory" by
255/// the code generator.
256///
257void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
258 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
259 if (CP.empty()) return;
260
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000261 // Calculate sections for constant pool entries. We collect entries to go into
262 // the same section together to reduce amount of section switch statements.
263 typedef
264 std::multimap<const Section*,
265 std::pair<MachineConstantPoolEntry, unsigned> > CPMap;
266 CPMap CPs;
Anton Korobeynikov96df4252008-09-24 22:20:46 +0000267 SmallPtrSet<const Section*, 5> Sections;
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000268
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
270 MachineConstantPoolEntry CPE = CP[i];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000271 const Section* S = TAI->SelectSectionForMachineConst(CPE.getType());
272 CPs.insert(std::make_pair(S, std::make_pair(CPE, i)));
273 Sections.insert(S);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 }
275
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000276 // Now print stuff into the calculated sections.
Anton Korobeynikov96df4252008-09-24 22:20:46 +0000277 for (SmallPtrSet<const Section*, 5>::iterator IS = Sections.begin(),
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000278 ES = Sections.end(); IS != ES; ++IS) {
279 SwitchToSection(*IS);
280 EmitAlignment(MCP->getConstantPoolAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000282 std::pair<CPMap::iterator, CPMap::iterator> II = CPs.equal_range(*IS);
283 for (CPMap::iterator I = II.first, E = II.second; I != E; ++I) {
284 CPMap::iterator J = next(I);
285 MachineConstantPoolEntry Entry = I->second.first;
286 unsigned index = I->second.second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000288 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
289 << index << ":\t\t\t\t\t";
Owen Anderson847b99b2008-08-21 00:14:44 +0000290 // O << TAI->getCommentString() << ' ' <<
291 // WriteTypeSymbolic(O, CP[i].first.getType(), 0);
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000292 O << '\n';
293 if (Entry.isMachineConstantPoolEntry())
294 EmitMachineConstantPoolValue(Entry.Val.MachineCPVal);
295 else
296 EmitGlobalConstant(Entry.Val.ConstVal);
297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 // Emit inter-object padding for alignment.
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000299 if (J != E) {
300 const Type *Ty = Entry.getType();
301 unsigned EntSize = TM.getTargetData()->getABITypeSize(Ty);
302 unsigned ValEnd = Entry.getOffset() + EntSize;
303 EmitZeros(J->second.first.getOffset()-ValEnd);
304 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 }
306 }
307}
308
309/// EmitJumpTableInfo - Print assembly representations of the jump tables used
310/// by the current function to the current output stream.
311///
312void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
313 MachineFunction &MF) {
314 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
315 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000316
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
318
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 // Pick the directive to use to print the jump table entries, and switch to
320 // the appropriate section.
321 TargetLowering *LoweringInfo = TM.getTargetLowering();
322
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000323 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
324 const Function *F = MF.getFunction();
325 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000327 !JumpTableDataSection ||
328 SectionFlags & SectionFlags::Linkonce) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 // In PIC mode, we need to emit the jump table to the same section as the
330 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000331 // We should also do if the section name is NULL or function is declared in
332 // discardable section.
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000333 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 } else {
335 SwitchToDataSection(JumpTableDataSection);
336 }
337
338 EmitAlignment(Log2_32(MJTI->getAlignment()));
339
340 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
341 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
342
343 // If this jump table was deleted, ignore it.
344 if (JTBBs.empty()) continue;
345
346 // For PIC codegen, if possible we want to use the SetDirective to reduce
347 // the number of relocations the assembler will generate for the jump table.
348 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000349 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 if (TAI->getSetDirective() && IsPic)
351 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000352 if (EmittedSets.insert(JTBBs[ii]))
353 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354
355 // On some targets (e.g. darwin) we want to emit two consequtive labels
356 // before each jump table. The first label is never referenced, but tells
357 // the assembler and linker the extents of the jump table object. The
358 // second label is actually referenced by the code.
359 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Cheng477013c2007-10-14 05:57:21 +0000360 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361
Evan Cheng477013c2007-10-14 05:57:21 +0000362 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
363 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364
365 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000366 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 O << '\n';
368 }
369 }
370}
371
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000372void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
373 const MachineBasicBlock *MBB,
374 unsigned uid) const {
375 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
376
377 // Use JumpTableDirective otherwise honor the entry size from the jump table
378 // info.
379 const char *JTEntryDirective = TAI->getJumpTableDirective();
380 bool HadJTEntryDirective = JTEntryDirective != NULL;
381 if (!HadJTEntryDirective) {
382 JTEntryDirective = MJTI->getEntrySize() == 4 ?
383 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
384 }
385
386 O << JTEntryDirective << ' ';
387
388 // If we have emitted set directives for the jump table entries, print
389 // them rather than the entries themselves. If we're emitting PIC, then
390 // emit the table entries as differences between two text section labels.
391 // If we're emitting non-PIC code, then emit the entries as direct
392 // references to the target basic blocks.
393 if (IsPic) {
394 if (TAI->getSetDirective()) {
395 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
396 << '_' << uid << "_set_" << MBB->getNumber();
397 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000398 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000399 // If the arch uses custom Jump Table directives, don't calc relative to
400 // JT
401 if (!HadJTEntryDirective)
402 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
403 << getFunctionNumber() << '_' << uid;
404 }
405 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000406 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000407 }
408}
409
410
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
412/// special global used by LLVM. If so, emit it and return true, otherwise
413/// do nothing and return false.
414bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000415 if (GV->getName() == "llvm.used") {
416 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
417 EmitLLVMUsedList(GV->getInitializer());
418 return true;
419 }
420
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 // Ignore debug and non-emitted data.
422 if (GV->getSection() == "llvm.metadata") return true;
423
424 if (!GV->hasAppendingLinkage()) return false;
425
426 assert(GV->hasInitializer() && "Not a special LLVM global!");
427
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 const TargetData *TD = TM.getTargetData();
429 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
430 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
431 SwitchToDataSection(TAI->getStaticCtorsSection());
432 EmitAlignment(Align, 0);
433 EmitXXStructorList(GV->getInitializer());
434 return true;
435 }
436
437 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
438 SwitchToDataSection(TAI->getStaticDtorsSection());
439 EmitAlignment(Align, 0);
440 EmitXXStructorList(GV->getInitializer());
441 return true;
442 }
443
444 return false;
445}
446
Dale Johannesen4911fb82008-09-03 20:34:58 +0000447/// findGlobalValue - if CV is an expression equivalent to a single
448/// global value, return that value.
449const GlobalValue * AsmPrinter::findGlobalValue(const Constant *CV) {
450 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
451 return GV;
452 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
453 const TargetData *TD = TM.getTargetData();
454 unsigned Opcode = CE->getOpcode();
455 switch (Opcode) {
456 case Instruction::GetElementPtr: {
457 const Constant *ptrVal = CE->getOperand(0);
458 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
459 if (TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], idxVec.size()))
460 return 0;
461 return findGlobalValue(ptrVal);
462 }
463 case Instruction::BitCast:
464 return findGlobalValue(CE->getOperand(0));
465 default:
466 return 0;
467 }
468 }
469 return 0;
470}
471
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000473/// global in the specified llvm.used list for which emitUsedDirectiveFor
474/// is true, as being used with this directive.
475
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476void AsmPrinter::EmitLLVMUsedList(Constant *List) {
477 const char *Directive = TAI->getUsedDirective();
478
479 // Should be an array of 'sbyte*'.
480 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
481 if (InitList == 0) return;
482
483 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000484 const GlobalValue *GV = findGlobalValue(InitList->getOperand(i));
Dale Johannesen60567622008-09-09 22:29:13 +0000485 if (TAI->emitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000486 O << Directive;
487 EmitConstantValueOnly(InitList->getOperand(i));
488 O << '\n';
489 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 }
491}
492
493/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
494/// function pointers, ignoring the init priority.
495void AsmPrinter::EmitXXStructorList(Constant *List) {
496 // Should be an array of '{ int, void ()* }' structs. The first value is the
497 // init priority, which we ignore.
498 if (!isa<ConstantArray>(List)) return;
499 ConstantArray *InitList = cast<ConstantArray>(List);
500 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
501 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
502 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
503
504 if (CS->getOperand(1)->isNullValue())
505 return; // Found a null terminator, exit printing.
506 // Emit the function pointer.
507 EmitGlobalConstant(CS->getOperand(1));
508 }
509}
510
511/// getGlobalLinkName - Returns the asm/link name of of the specified
512/// global variable. Should be overridden by each target asm printer to
513/// generate the appropriate value.
514const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
515 std::string LinkName;
516
517 if (isa<Function>(GV)) {
518 LinkName += TAI->getFunctionAddrPrefix();
519 LinkName += Mang->getValueName(GV);
520 LinkName += TAI->getFunctionAddrSuffix();
521 } else {
522 LinkName += TAI->getGlobalVarAddrPrefix();
523 LinkName += Mang->getValueName(GV);
524 LinkName += TAI->getGlobalVarAddrSuffix();
525 }
526
527 return LinkName;
528}
529
530/// EmitExternalGlobal - Emit the external reference to a global variable.
531/// Should be overridden if an indirect reference should be used.
532void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
533 O << getGlobalLinkName(GV);
534}
535
536
537
538//===----------------------------------------------------------------------===//
539/// LEB 128 number encoding.
540
541/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
542/// representing an unsigned leb128 value.
543void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000544 char Buffer[20];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000546 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 Value >>= 7;
548 if (Value) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000549 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 if (Value) O << ", ";
551 } while (Value);
552}
553
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
555/// representing a signed leb128 value.
556void AsmPrinter::PrintSLEB128(int Value) const {
557 int Sign = Value >> (8 * sizeof(Value) - 1);
558 bool IsMore;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000559 char Buffer[20];
aslc200b112008-08-16 12:57:46 +0000560
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000562 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 Value >>= 7;
564 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
565 if (IsMore) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000566 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 if (IsMore) O << ", ";
568 } while (IsMore);
569}
570
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571//===--------------------------------------------------------------------===//
572// Emission and print routines
573//
574
575/// PrintHex - Print a value as a hexidecimal value.
576///
577void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerda2047e2008-11-10 04:30:26 +0000578 char Buffer[20];
Chris Lattnerda2047e2008-11-10 04:30:26 +0000579 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580}
581
582/// EOL - Print a newline character to asm stream. If a comment is present
583/// then it will be printed first. Comments should not contain '\n'.
584void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000585 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000587
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000589 if (VerboseAsm && !Comment.empty()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000590 O << '\t'
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 << TAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000592 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 << Comment;
594 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000595 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596}
597
Owen Anderson367bfbb2008-07-01 21:16:27 +0000598void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000599 if (VerboseAsm && *Comment) {
Owen Anderson367bfbb2008-07-01 21:16:27 +0000600 O << '\t'
601 << TAI->getCommentString()
602 << ' '
603 << Comment;
604 }
605 O << '\n';
606}
607
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
609/// unsigned leb128 value.
610void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
611 if (TAI->hasLEB128()) {
612 O << "\t.uleb128\t"
613 << Value;
614 } else {
615 O << TAI->getData8bitsDirective();
616 PrintULEB128(Value);
617 }
618}
619
620/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
621/// signed leb128 value.
622void AsmPrinter::EmitSLEB128Bytes(int Value) const {
623 if (TAI->hasLEB128()) {
624 O << "\t.sleb128\t"
625 << Value;
626 } else {
627 O << TAI->getData8bitsDirective();
628 PrintSLEB128(Value);
629 }
630}
631
632/// EmitInt8 - Emit a byte directive and value.
633///
634void AsmPrinter::EmitInt8(int Value) const {
635 O << TAI->getData8bitsDirective();
636 PrintHex(Value & 0xFF);
637}
638
639/// EmitInt16 - Emit a short directive and value.
640///
641void AsmPrinter::EmitInt16(int Value) const {
642 O << TAI->getData16bitsDirective();
643 PrintHex(Value & 0xFFFF);
644}
645
646/// EmitInt32 - Emit a long directive and value.
647///
648void AsmPrinter::EmitInt32(int Value) const {
649 O << TAI->getData32bitsDirective();
650 PrintHex(Value);
651}
652
653/// EmitInt64 - Emit a long long directive and value.
654///
655void AsmPrinter::EmitInt64(uint64_t Value) const {
656 if (TAI->getData64bitsDirective()) {
657 O << TAI->getData64bitsDirective();
658 PrintHex(Value);
659 } else {
660 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000661 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 EmitInt32(unsigned(Value));
663 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000664 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 EmitInt32(unsigned(Value >> 32));
666 }
667 }
668}
669
670/// toOctal - Convert the low order bits of X into an octal digit.
671///
672static inline char toOctal(int X) {
673 return (X&7)+'0';
674}
675
676/// printStringChar - Print a char, escaped if necessary.
677///
Owen Anderson847b99b2008-08-21 00:14:44 +0000678static void printStringChar(raw_ostream &O, char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 if (C == '"') {
680 O << "\\\"";
681 } else if (C == '\\') {
682 O << "\\\\";
683 } else if (isprint(C)) {
684 O << C;
685 } else {
686 switch(C) {
687 case '\b': O << "\\b"; break;
688 case '\f': O << "\\f"; break;
689 case '\n': O << "\\n"; break;
690 case '\r': O << "\\r"; break;
691 case '\t': O << "\\t"; break;
692 default:
693 O << '\\';
694 O << toOctal(C >> 6);
695 O << toOctal(C >> 3);
696 O << toOctal(C >> 0);
697 break;
698 }
699 }
700}
701
702/// EmitString - Emit a string with quotes and a null terminator.
703/// Special characters are emitted properly.
704/// \literal (Eg. '\t') \endliteral
705void AsmPrinter::EmitString(const std::string &String) const {
706 const char* AscizDirective = TAI->getAscizDirective();
707 if (AscizDirective)
708 O << AscizDirective;
709 else
710 O << TAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000711 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 for (unsigned i = 0, N = String.size(); i < N; ++i) {
713 unsigned char C = String[i];
714 printStringChar(O, C);
715 }
716 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000717 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 else
719 O << "\\0\"";
720}
721
722
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000723/// EmitFile - Emit a .file directive.
724void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
725 O << "\t.file\t" << Number << " \"";
726 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
727 unsigned char C = Name[i];
728 printStringChar(O, C);
729 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000730 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000731}
732
733
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734//===----------------------------------------------------------------------===//
735
736// EmitAlignment - Emit an alignment directive to the specified power of
737// two boundary. For example, if you pass in 3 here, you will get an 8
738// byte alignment. If a global value is specified, and if that global has
739// an explicit alignment requested, it will unconditionally override the
740// alignment request. However, if ForcedAlignBits is specified, this value
741// has final say: the ultimate alignment will be the max of ForcedAlignBits
742// and the alignment computed with NumBits and the global.
743//
744// The algorithm is:
745// Align = NumBits;
746// if (GV && GV->hasalignment) Align = GV->getalignment();
747// Align = std::max(Align, ForcedAlignBits);
748//
749void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000750 unsigned ForcedAlignBits,
751 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 if (GV && GV->getAlignment())
753 NumBits = Log2_32(GV->getAlignment());
754 NumBits = std::max(NumBits, ForcedAlignBits);
755
756 if (NumBits == 0) return; // No need to emit alignment.
757 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Chengc1f41aa2007-07-25 23:35:07 +0000758 O << TAI->getAlignDirective() << NumBits;
Evan Cheng45c1edb2008-02-28 00:43:03 +0000759
760 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng7e7d1942008-02-29 19:36:59 +0000761 UseFillExpr &= IsInTextSection && FillValue;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000762 if (UseFillExpr) {
763 O << ',';
764 PrintHex(FillValue);
765 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000766 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767}
768
769
770/// EmitZeros - Emit a block of zeros.
771///
772void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
773 if (NumZeros) {
774 if (TAI->getZeroDirective()) {
775 O << TAI->getZeroDirective() << NumZeros;
776 if (TAI->getZeroDirectiveSuffix())
777 O << TAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000778 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 } else {
780 for (; NumZeros; --NumZeros)
781 O << TAI->getData8bitsDirective() << "0\n";
782 }
783 }
784}
785
786// Print out the specified constant, without a storage class. Only the
787// constants valid in constant expressions can occur here.
788void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
789 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000790 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000792 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000793 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
794 // This is a constant address for a global variable or function. Use the
795 // name of the variable or function as the address value, possibly
796 // decorating it with GlobalVarAddrPrefix/Suffix or
797 // FunctionAddrPrefix/Suffix (these all default to "" )
798 if (isa<Function>(GV)) {
799 O << TAI->getFunctionAddrPrefix()
800 << Mang->getValueName(GV)
801 << TAI->getFunctionAddrSuffix();
802 } else {
803 O << TAI->getGlobalVarAddrPrefix()
804 << Mang->getValueName(GV)
805 << TAI->getGlobalVarAddrSuffix();
806 }
807 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
808 const TargetData *TD = TM.getTargetData();
809 unsigned Opcode = CE->getOpcode();
810 switch (Opcode) {
811 case Instruction::GetElementPtr: {
812 // generate a symbolic expression for the byte address
813 const Constant *ptrVal = CE->getOperand(0);
814 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
815 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
816 idxVec.size())) {
817 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000818 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 EmitConstantValueOnly(ptrVal);
820 if (Offset > 0)
821 O << ") + " << Offset;
822 else if (Offset < 0)
823 O << ") - " << -Offset;
824 } else {
825 EmitConstantValueOnly(ptrVal);
826 }
827 break;
828 }
829 case Instruction::Trunc:
830 case Instruction::ZExt:
831 case Instruction::SExt:
832 case Instruction::FPTrunc:
833 case Instruction::FPExt:
834 case Instruction::UIToFP:
835 case Instruction::SIToFP:
836 case Instruction::FPToUI:
837 case Instruction::FPToSI:
838 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
839 break;
840 case Instruction::BitCast:
841 return EmitConstantValueOnly(CE->getOperand(0));
842
843 case Instruction::IntToPtr: {
844 // Handle casts to pointers by changing them into casts to the appropriate
845 // integer type. This promotes constant folding and simplifies this code.
846 Constant *Op = CE->getOperand(0);
847 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
848 return EmitConstantValueOnly(Op);
849 }
850
851
852 case Instruction::PtrToInt: {
853 // Support only foldable casts to/from pointers that can be eliminated by
854 // changing the pointer to the appropriately sized integer type.
855 Constant *Op = CE->getOperand(0);
856 const Type *Ty = CE->getType();
857
858 // We can emit the pointer value into this slot if the slot is an
859 // integer slot greater or equal to the size of the pointer.
Nick Lewycky95353ad2008-08-08 06:34:07 +0000860 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000862
863 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000865 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
Chris Lattner89b36582008-08-17 07:19:36 +0000866
867 SmallString<40> S;
868 ptrMask.toStringUnsigned(S);
869 O << ") & " << S.c_str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 break;
871 }
872 case Instruction::Add:
873 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000874 case Instruction::And:
875 case Instruction::Or:
876 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000877 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000879 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000880 switch (Opcode) {
881 case Instruction::Add:
882 O << " + ";
883 break;
884 case Instruction::Sub:
885 O << " - ";
886 break;
887 case Instruction::And:
888 O << " & ";
889 break;
890 case Instruction::Or:
891 O << " | ";
892 break;
893 case Instruction::Xor:
894 O << " ^ ";
895 break;
896 default:
897 break;
898 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000899 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000901 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 break;
903 default:
904 assert(0 && "Unsupported operator!");
905 }
906 } else {
907 assert(0 && "Unknown constant value!");
908 }
909}
910
911/// printAsCString - Print the specified array as a C compatible string, only if
912/// the predicate isString is true.
913///
Owen Anderson847b99b2008-08-21 00:14:44 +0000914static void printAsCString(raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 unsigned LastElt) {
916 assert(CVA->isString() && "Array is not string compatible!");
917
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000918 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 for (unsigned i = 0; i != LastElt; ++i) {
920 unsigned char C =
921 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
922 printStringChar(O, C);
923 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000924 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925}
926
927/// EmitString - Emit a zero-byte-terminated string constant.
928///
929void AsmPrinter::EmitString(const ConstantArray *CVA) const {
930 unsigned NumElts = CVA->getNumOperands();
931 if (TAI->getAscizDirective() && NumElts &&
932 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
933 O << TAI->getAscizDirective();
934 printAsCString(O, CVA, NumElts-1);
935 } else {
936 O << TAI->getAsciiDirective();
937 printAsCString(O, CVA, NumElts);
938 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000939 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940}
941
942/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sands4afc5752008-06-04 08:21:45 +0000943void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 const TargetData *TD = TM.getTargetData();
Duncan Sands4afc5752008-06-04 08:21:45 +0000945 unsigned Size = TD->getABITypeSize(CV->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946
947 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands8157ef42007-11-05 00:04:43 +0000948 EmitZeros(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 return;
950 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
951 if (CVA->isString()) {
952 EmitString(CVA);
953 } else { // Not a string. Print the values in successive locations
Duncan Sands8157ef42007-11-05 00:04:43 +0000954 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Duncan Sands4afc5752008-06-04 08:21:45 +0000955 EmitGlobalConstant(CVA->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956 }
957 return;
958 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
959 // Print the fields in successive locations. Pad to align if needed!
960 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
961 uint64_t sizeSoFar = 0;
962 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
963 const Constant* field = CVS->getOperand(i);
964
965 // Check if padding is needed and insert one or more 0s.
Duncan Sands4afc5752008-06-04 08:21:45 +0000966 uint64_t fieldSize = TD->getABITypeSize(field->getType());
Duncan Sandsc15d58c2007-11-05 18:03:02 +0000967 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968 - cvsLayout->getElementOffset(i)) - fieldSize;
969 sizeSoFar += fieldSize + padSize;
970
Duncan Sands4afc5752008-06-04 08:21:45 +0000971 // Now print the actual field value.
972 EmitGlobalConstant(field);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973
Duncan Sandsc15d58c2007-11-05 18:03:02 +0000974 // Insert padding - this may include padding to increase the size of the
975 // current field up to the ABI size (if the struct is not packed) as well
976 // as padding to ensure that the next field starts at the right offset.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 EmitZeros(padSize);
978 }
979 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
980 "Layout of constant struct may be incorrect!");
981 return;
982 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
983 // FP Constants are printed as integer constants to avoid losing
984 // precision...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000986 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesen49cc7ce2008-10-09 18:53:47 +0000987 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 if (TAI->getData64bitsDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000989 O << TAI->getData64bitsDirective() << i << '\t'
990 << TAI->getCommentString() << " double value: " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 else if (TD->isBigEndian()) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000992 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000993 << '\t' << TAI->getCommentString()
994 << " double most significant word " << Val << '\n';
Dale Johannesen1616e902007-09-11 18:32:33 +0000995 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000996 << '\t' << TAI->getCommentString()
997 << " double least significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +0000999 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001000 << '\t' << TAI->getCommentString()
1001 << " double least significant word " << Val << '\n';
Dale Johannesen1616e902007-09-11 18:32:33 +00001002 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001003 << '\t' << TAI->getCommentString()
1004 << " double most significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 }
1006 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001007 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +00001008 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
1009 O << TAI->getData32bitsDirective()
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00001010 << CFP->getValueAPF().bitcastToAPInt().getZExtValue()
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001011 << '\t' << TAI->getCommentString() << " float " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001013 } else if (CFP->getType() == Type::X86_FP80Ty) {
1014 // all long double variants are printed as hex
Dale Johannesen693aa822007-09-26 23:20:33 +00001015 // api needed to prevent premature destruction
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00001016 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen693aa822007-09-26 23:20:33 +00001017 const uint64_t *p = api.getRawData();
Dale Johannesen6e547b42008-10-09 23:00:39 +00001018 // Convert to double so we can print the approximate val as a comment.
Chris Lattner00d63062008-01-27 06:09:28 +00001019 APFloat DoubleVal = CFP->getValueAPF();
Dale Johannesen6e547b42008-10-09 23:00:39 +00001020 bool ignored;
1021 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1022 &ignored);
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001023 if (TD->isBigEndian()) {
1024 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001025 << '\t' << TAI->getCommentString()
Chris Lattner00d63062008-01-27 06:09:28 +00001026 << " long double most significant halfword of ~"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001027 << DoubleVal.convertToDouble() << '\n';
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001028 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001029 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001030 << " long double next halfword\n";
1031 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001032 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001033 << " long double next halfword\n";
1034 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001035 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001036 << " long double next halfword\n";
1037 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001038 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001039 << " long double least significant halfword\n";
1040 } else {
1041 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001042 << '\t' << TAI->getCommentString()
Chris Lattner00d63062008-01-27 06:09:28 +00001043 << " long double least significant halfword of ~"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001044 << DoubleVal.convertToDouble() << '\n';
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001045 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001046 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001047 << " long double next halfword\n";
1048 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001049 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001050 << " long double next halfword\n";
1051 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001052 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001053 << " long double next halfword\n";
1054 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001055 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001056 << " long double most significant halfword\n";
1057 }
Duncan Sands8157ef42007-11-05 00:04:43 +00001058 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001059 return;
Dale Johannesend3b6af32007-10-11 23:32:15 +00001060 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1061 // all long double variants are printed as hex
1062 // api needed to prevent premature destruction
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00001063 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesend3b6af32007-10-11 23:32:15 +00001064 const uint64_t *p = api.getRawData();
1065 if (TD->isBigEndian()) {
1066 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001067 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001068 << " long double most significant word\n";
1069 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001070 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001071 << " long double next word\n";
1072 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001073 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001074 << " long double next word\n";
1075 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001076 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001077 << " long double least significant word\n";
1078 } else {
1079 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001080 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001081 << " long double least significant word\n";
1082 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001083 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001084 << " long double next word\n";
1085 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001086 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001087 << " long double next word\n";
1088 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001089 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001090 << " long double most significant word\n";
1091 }
1092 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001093 } else assert(0 && "Floating point constant type not handled");
Dan Gohman07a91ea2008-09-08 16:40:13 +00001094 } else if (CV->getType()->isInteger() &&
1095 cast<IntegerType>(CV->getType())->getBitWidth() >= 64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Dan Gohman07a91ea2008-09-08 16:40:13 +00001097 unsigned BitWidth = CI->getBitWidth();
1098 assert(isPowerOf2_32(BitWidth) &&
1099 "Non-power-of-2-sized integers not handled!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100
Dan Gohman07a91ea2008-09-08 16:40:13 +00001101 // We don't expect assemblers to support integer data directives
1102 // for more than 64 bits, so we emit the data in at most 64-bit
1103 // quantities at a time.
1104 const uint64_t *RawData = CI->getValue().getRawData();
1105 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1106 uint64_t Val;
1107 if (TD->isBigEndian())
1108 Val = RawData[e - i - 1];
1109 else
1110 Val = RawData[i];
1111
1112 if (TAI->getData64bitsDirective())
1113 O << TAI->getData64bitsDirective() << Val << '\n';
1114 else if (TD->isBigEndian()) {
1115 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1116 << '\t' << TAI->getCommentString()
1117 << " Double-word most significant word " << Val << '\n';
1118 O << TAI->getData32bitsDirective() << unsigned(Val)
1119 << '\t' << TAI->getCommentString()
1120 << " Double-word least significant word " << Val << '\n';
1121 } else {
1122 O << TAI->getData32bitsDirective() << unsigned(Val)
1123 << '\t' << TAI->getCommentString()
1124 << " Double-word least significant word " << Val << '\n';
1125 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1126 << '\t' << TAI->getCommentString()
1127 << " Double-word most significant word " << Val << '\n';
1128 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001129 }
1130 return;
1131 }
1132 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1133 const VectorType *PTy = CP->getType();
1134
1135 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sands4afc5752008-06-04 08:21:45 +00001136 EmitGlobalConstant(CP->getOperand(I));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137
1138 return;
1139 }
1140
1141 const Type *type = CV->getType();
1142 printDataDirective(type);
1143 EmitConstantValueOnly(CV);
Scott Michele067c3c2008-06-03 15:39:51 +00001144 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner89b36582008-08-17 07:19:36 +00001145 SmallString<40> S;
1146 CI->getValue().toStringUnsigned(S, 16);
1147 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
Scott Michele067c3c2008-06-03 15:39:51 +00001148 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001149 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150}
1151
Chris Lattner89b36582008-08-17 07:19:36 +00001152void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001153 // Target doesn't support this yet!
1154 abort();
1155}
1156
1157/// PrintSpecial - Print information related to the specified machine instr
1158/// that is independent of the operand, and may be independent of the instr
1159/// itself. This can be useful for portably encoding the comment character
1160/// or other bits of target-specific knowledge into the asmstrings. The
1161/// syntax used is ${:comment}. Targets can override this to add support
1162/// for their own strange codes.
1163void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
1164 if (!strcmp(Code, "private")) {
1165 O << TAI->getPrivateGlobalPrefix();
1166 } else if (!strcmp(Code, "comment")) {
1167 O << TAI->getCommentString();
1168 } else if (!strcmp(Code, "uid")) {
1169 // Assign a unique ID to this machine instruction.
1170 static const MachineInstr *LastMI = 0;
1171 static const Function *F = 0;
1172 static unsigned Counter = 0U-1;
1173
1174 // Comparing the address of MI isn't sufficient, because machineinstrs may
1175 // be allocated to the same address across functions.
1176 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1177
1178 // If this is a new machine instruction, bump the counter.
1179 if (LastMI != MI || F != ThisF) {
1180 ++Counter;
1181 LastMI = MI;
1182 F = ThisF;
1183 }
1184 O << Counter;
1185 } else {
1186 cerr << "Unknown special formatter '" << Code
1187 << "' for machine instr: " << *MI;
1188 exit(1);
1189 }
1190}
1191
1192
1193/// printInlineAsm - This method formats and prints the specified machine
1194/// instruction that is an inline asm.
1195void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1196 unsigned NumOperands = MI->getNumOperands();
1197
1198 // Count the number of register definitions.
1199 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001200 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001201 ++NumDefs)
1202 assert(NumDefs != NumOperands-1 && "No asm string?");
1203
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001204 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001205
1206 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1207 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1208
Dale Johannesene99fc902008-01-29 02:21:21 +00001209 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1210 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001211 if (AsmStr[0] == 0) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001212 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 return;
1214 }
1215
1216 O << TAI->getInlineAsmStart() << "\n\t";
1217
1218 // The variant of the current asmprinter.
1219 int AsmPrinterVariant = TAI->getAssemblerDialect();
1220
1221 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1222 const char *LastEmitted = AsmStr; // One past the last character emitted.
1223
1224 while (*LastEmitted) {
1225 switch (*LastEmitted) {
1226 default: {
1227 // Not a special case, emit the string section literally.
1228 const char *LiteralEnd = LastEmitted+1;
1229 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1230 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1231 ++LiteralEnd;
1232 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1233 O.write(LastEmitted, LiteralEnd-LastEmitted);
1234 LastEmitted = LiteralEnd;
1235 break;
1236 }
1237 case '\n':
1238 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001239 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 break;
1241 case '$': {
1242 ++LastEmitted; // Consume '$' character.
1243 bool Done = true;
1244
1245 // Handle escapes.
1246 switch (*LastEmitted) {
1247 default: Done = false; break;
1248 case '$': // $$ -> $
1249 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1250 O << '$';
1251 ++LastEmitted; // Consume second '$' character.
1252 break;
1253 case '(': // $( -> same as GCC's { character.
1254 ++LastEmitted; // Consume '(' character.
1255 if (CurVariant != -1) {
1256 cerr << "Nested variants found in inline asm string: '"
1257 << AsmStr << "'\n";
1258 exit(1);
1259 }
1260 CurVariant = 0; // We're in the first variant now.
1261 break;
1262 case '|':
1263 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001264 if (CurVariant == -1)
1265 O << '|'; // this is gcc's behavior for | outside a variant
1266 else
1267 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 break;
1269 case ')': // $) -> same as GCC's } char.
1270 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001271 if (CurVariant == -1)
1272 O << '}'; // this is gcc's behavior for } outside a variant
1273 else
1274 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 break;
1276 }
1277 if (Done) break;
1278
1279 bool HasCurlyBraces = false;
1280 if (*LastEmitted == '{') { // ${variable}
1281 ++LastEmitted; // Consume '{' character.
1282 HasCurlyBraces = true;
1283 }
1284
1285 const char *IDStart = LastEmitted;
1286 char *IDEnd;
1287 errno = 0;
1288 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1289 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
1290 cerr << "Bad $ operand number in inline asm string: '"
1291 << AsmStr << "'\n";
1292 exit(1);
1293 }
1294 LastEmitted = IDEnd;
1295
1296 char Modifier[2] = { 0, 0 };
1297
1298 if (HasCurlyBraces) {
1299 // If we have curly braces, check for a modifier character. This
1300 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1301 if (*LastEmitted == ':') {
1302 ++LastEmitted; // Consume ':' character.
1303 if (*LastEmitted == 0) {
1304 cerr << "Bad ${:} expression in inline asm string: '"
1305 << AsmStr << "'\n";
1306 exit(1);
1307 }
1308
1309 Modifier[0] = *LastEmitted;
1310 ++LastEmitted; // Consume modifier character.
1311 }
1312
1313 if (*LastEmitted != '}') {
1314 cerr << "Bad ${} expression in inline asm string: '"
1315 << AsmStr << "'\n";
1316 exit(1);
1317 }
1318 ++LastEmitted; // Consume '}' character.
1319 }
1320
1321 if ((unsigned)Val >= NumOperands-1) {
1322 cerr << "Invalid $ operand number in inline asm string: '"
1323 << AsmStr << "'\n";
1324 exit(1);
1325 }
1326
1327 // Okay, we finally have a value number. Ask the target to print this
1328 // operand!
1329 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1330 unsigned OpNo = 1;
1331
1332 bool Error = false;
1333
1334 // Scan to find the machine operand number for the operand.
1335 for (; Val; --Val) {
1336 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001337 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001338 OpNo += (OpFlags >> 3) + 1;
1339 }
1340
1341 if (OpNo >= MI->getNumOperands()) {
1342 Error = true;
1343 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001344 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001345 ++OpNo; // Skip over the ID number.
1346
Dale Johannesencfb19e62007-11-05 21:20:28 +00001347 if (Modifier[0]=='l') // labels are target independent
Chris Lattner6017d482007-12-30 23:10:15 +00001348 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Cheng45c1edb2008-02-28 00:43:03 +00001349 false, false, false);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001350 else {
1351 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001352 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001353 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1354 Modifier[0] ? Modifier : 0);
1355 } else {
1356 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1357 Modifier[0] ? Modifier : 0);
1358 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 }
1360 }
1361 if (Error) {
1362 cerr << "Invalid operand found in inline asm: '"
1363 << AsmStr << "'\n";
1364 MI->dump();
1365 exit(1);
1366 }
1367 }
1368 break;
1369 }
1370 }
1371 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001372 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373}
1374
Evan Cheng3c0eda52008-03-15 00:03:38 +00001375/// printImplicitDef - This method prints the specified machine instruction
1376/// that is an implicit def.
1377void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001378 O << '\t' << TAI->getCommentString() << " implicit-def: "
1379 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng3c0eda52008-03-15 00:03:38 +00001380}
1381
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382/// printLabel - This method prints a local label used by debug and
1383/// exception handling tables.
1384void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001385 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386}
1387
Evan Chenga53c40a2008-02-01 09:10:45 +00001388void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng8b988692008-02-02 08:39:46 +00001389 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chenga53c40a2008-02-01 09:10:45 +00001390}
1391
Evan Cheng2e28d622008-02-02 04:07:54 +00001392/// printDeclare - This method prints a local variable declaration used by
1393/// debug tables.
Evan Chengc439a852008-02-04 23:06:48 +00001394/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1395/// entry into dwarf table.
Evan Cheng2e28d622008-02-02 04:07:54 +00001396void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Evan Chengc439a852008-02-04 23:06:48 +00001397 int FI = MI->getOperand(0).getIndex();
1398 GlobalValue *GV = MI->getOperand(1).getGlobal();
1399 MMI->RecordVariable(GV, FI);
Evan Cheng2e28d622008-02-02 04:07:54 +00001400}
1401
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001402/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1403/// instruction, using the specified assembler variant. Targets should
1404/// overried this to format as appropriate.
1405bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1406 unsigned AsmVariant, const char *ExtraCode) {
1407 // Target doesn't support this yet!
1408 return true;
1409}
1410
1411bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1412 unsigned AsmVariant,
1413 const char *ExtraCode) {
1414 // Target doesn't support this yet!
1415 return true;
1416}
1417
1418/// printBasicBlockLabel - This method prints the label for the specified
1419/// MachineBasicBlock
1420void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Cheng45c1edb2008-02-28 00:43:03 +00001421 bool printAlign,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422 bool printColon,
1423 bool printComment) const {
Evan Cheng45c1edb2008-02-28 00:43:03 +00001424 if (printAlign) {
1425 unsigned Align = MBB->getAlignment();
1426 if (Align)
1427 EmitAlignment(Log2_32(Align));
1428 }
1429
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001430 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng477013c2007-10-14 05:57:21 +00001431 << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001432 if (printColon)
1433 O << ':';
1434 if (printComment && MBB->getBasicBlock())
Dan Gohman0912cda2007-07-30 15:06:25 +00001435 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng76443dc2008-07-08 00:55:58 +00001436 << MBB->getBasicBlock()->getNameStart();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437}
1438
Evan Cheng6fb06762007-11-09 01:32:10 +00001439/// printPICJumpTableSetLabel - This method prints a set label for the
1440/// specified MachineBasicBlock for a jumptable entry.
1441void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1442 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 if (!TAI->getSetDirective())
1444 return;
1445
1446 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001447 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001448 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001449 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1450 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451}
1452
Evan Cheng6fb06762007-11-09 01:32:10 +00001453void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1454 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 if (!TAI->getSetDirective())
1456 return;
1457
1458 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001459 << getFunctionNumber() << '_' << uid << '_' << uid2
1460 << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001461 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001462 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1463 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464}
1465
1466/// printDataDirective - This method prints the asm directive for the
1467/// specified type.
1468void AsmPrinter::printDataDirective(const Type *type) {
1469 const TargetData *TD = TM.getTargetData();
1470 switch (type->getTypeID()) {
1471 case Type::IntegerTyID: {
1472 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1473 if (BitWidth <= 8)
1474 O << TAI->getData8bitsDirective();
1475 else if (BitWidth <= 16)
1476 O << TAI->getData16bitsDirective();
1477 else if (BitWidth <= 32)
1478 O << TAI->getData32bitsDirective();
1479 else if (BitWidth <= 64) {
1480 assert(TAI->getData64bitsDirective() &&
1481 "Target cannot handle 64-bit constant exprs!");
1482 O << TAI->getData64bitsDirective();
Dan Gohman07a91ea2008-09-08 16:40:13 +00001483 } else {
1484 assert(0 && "Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485 }
1486 break;
1487 }
1488 case Type::PointerTyID:
1489 if (TD->getPointerSize() == 8) {
1490 assert(TAI->getData64bitsDirective() &&
1491 "Target cannot handle 64-bit pointer exprs!");
1492 O << TAI->getData64bitsDirective();
1493 } else {
1494 O << TAI->getData32bitsDirective();
1495 }
1496 break;
1497 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen3b5303b2007-09-28 18:06:58 +00001498 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499 assert (0 && "Should have already output floating point constant.");
1500 default:
1501 assert (0 && "Can't handle printing this type of thing");
1502 break;
1503 }
1504}
1505
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001506void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
1507 const char *Prefix) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001508 if (Name[0]=='\"')
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001509 O << '\"';
1510 O << TAI->getPrivateGlobalPrefix();
1511 if (Prefix) O << Prefix;
1512 if (Name[0]=='\"')
1513 O << '\"';
1514 if (Name[0]=='\"')
1515 O << Name[1];
Dale Johannesena21b5202008-05-19 21:38:18 +00001516 else
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001517 O << Name;
1518 O << Suffix;
1519 if (Name[0]=='\"')
1520 O << '\"';
Dale Johannesena21b5202008-05-19 21:38:18 +00001521}
Evan Cheng76443dc2008-07-08 00:55:58 +00001522
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001523void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
Evan Cheng76443dc2008-07-08 00:55:58 +00001524 printSuffixedName(Name.c_str(), Suffix);
1525}
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001526
1527void AsmPrinter::printVisibility(const std::string& Name,
1528 unsigned Visibility) const {
1529 if (Visibility == GlobalValue::HiddenVisibility) {
1530 if (const char *Directive = TAI->getHiddenDirective())
1531 O << Directive << Name << '\n';
1532 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1533 if (const char *Directive = TAI->getProtectedDirective())
1534 O << Directive << Name << '\n';
1535 }
1536}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001537
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001538void AsmPrinter::printOffset(int64_t Offset) const {
1539 if (Offset > 0)
1540 O << '+' << Offset;
1541 else if (Offset < 0)
1542 O << Offset;
1543}
1544
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001545GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1546 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001547 return 0;
1548
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001549 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001550 if (GCPI != GCMetadataPrinters.end())
1551 return GCPI->second;
1552
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001553 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001554
1555 for (GCMetadataPrinterRegistry::iterator
1556 I = GCMetadataPrinterRegistry::begin(),
1557 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1558 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001559 GCMetadataPrinter *GMP = I->instantiate();
1560 GMP->S = S;
1561 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1562 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001563 }
1564
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001565 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001566 abort();
1567}