blob: f041f3be56fcf32684c4290f339dcfc907aa1c0b [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}
50
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051std::string AsmPrinter::getSectionForFunction(const Function &F) const {
52 return TAI->getTextSection();
53}
54
55
56/// SwitchToTextSection - Switch to the specified text section of the executable
57/// if we are not already in it!
58///
59void AsmPrinter::SwitchToTextSection(const char *NewSection,
60 const GlobalValue *GV) {
61 std::string NS;
62 if (GV && GV->hasSection())
63 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
64 else
65 NS = NewSection;
66
67 // If we're already in this section, we're done.
68 if (CurrentSection == NS) return;
69
70 // Close the current section, if applicable.
71 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000072 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073
74 CurrentSection = NS;
75
76 if (!CurrentSection.empty())
77 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Cheng45c1edb2008-02-28 00:43:03 +000078
79 IsInTextSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080}
81
82/// SwitchToDataSection - Switch to the specified data section of the executable
83/// if we are not already in it!
84///
85void AsmPrinter::SwitchToDataSection(const char *NewSection,
86 const GlobalValue *GV) {
87 std::string NS;
88 if (GV && GV->hasSection())
89 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
90 else
91 NS = NewSection;
92
93 // If we're already in this section, we're done.
94 if (CurrentSection == NS) return;
95
96 // Close the current section, if applicable.
97 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000098 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
100 CurrentSection = NS;
101
102 if (!CurrentSection.empty())
103 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000104
105 IsInTextSection = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106}
107
108
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000109void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
110 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000111 AU.addRequired<GCModuleInfo>();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000112}
113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114bool AsmPrinter::doInitialization(Module &M) {
115 Mang = new Mangler(M, TAI->getGlobalPrefix());
116
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000117 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
118 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
119 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
120 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
121 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000122
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 if (!M.getModuleInlineAsm().empty())
124 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
125 << M.getModuleInlineAsm()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000126 << '\n' << TAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 << " End of file scope inline assembly\n";
128
129 SwitchToDataSection(""); // Reset back to no section.
130
Evan Chengc439a852008-02-04 23:06:48 +0000131 MMI = getAnalysisToUpdate<MachineModuleInfo>();
132 if (MMI) MMI->AnalyzeModule(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133
134 return false;
135}
136
137bool AsmPrinter::doFinalization(Module &M) {
138 if (TAI->getWeakRefDirective()) {
139 if (!ExtWeakSymbols.empty())
140 SwitchToDataSection("");
141
142 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
143 e = ExtWeakSymbols.end(); i != e; ++i) {
144 const GlobalValue *GV = *i;
145 std::string Name = Mang->getValueName(GV);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000146 O << TAI->getWeakRefDirective() << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 }
148 }
149
150 if (TAI->getSetDirective()) {
151 if (!M.alias_empty())
152 SwitchToTextSection(TAI->getTextSection());
153
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000154 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
156 I!=E; ++I) {
157 std::string Name = Mang->getValueName(I);
158 std::string Target;
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000159
160 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
161 Target = Mang->getValueName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000163 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000164 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 else if (I->hasWeakLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000166 O << TAI->getWeakRefDirective() << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 else if (!I->hasInternalLinkage())
168 assert(0 && "Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000169
170 if (I->hasHiddenVisibility()) {
171 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000172 O << Directive << Name << '\n';
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000173 } else if (I->hasProtectedVisibility()) {
174 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000175 O << Directive << Name << '\n';
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000176 }
177
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000178 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000179
180 // If the aliasee has external weak linkage it can be referenced only by
181 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
182 // weak reference in such case.
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000183 if (GV->hasExternalWeakLinkage()) {
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000184 if (TAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000185 O << TAI->getWeakRefDirective() << Target << '\n';
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000186 else
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000187 O << "\t.globl\t" << Target << '\n';
Anton Korobeynikov53422f62008-02-20 11:10:28 +0000188 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
190 }
191
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000192 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>();
193 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
194 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
195 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
196 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000197
Dan Gohmana65530a2008-05-05 00:28:39 +0000198 // If we don't have any trampolines, then we don't require stack memory
199 // to be executable. Some targets have a directive to declare this.
200 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
201 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
202 if (TAI->getNonexecutableStackDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000203 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000204
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 delete Mang; Mang = 0;
206 return false;
207}
208
Bill Wendlinge9ecdcf2007-09-18 09:10:16 +0000209std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendlingef9211a2007-09-18 01:47:22 +0000210 assert(MF && "No machine function?");
Dale Johannesene73bcbb2008-04-02 20:10:52 +0000211 std::string Name = MF->getFunction()->getName();
212 if (Name.empty())
213 Name = Mang->getValueName(MF->getFunction());
214 return Mang->makeNameProper(Name + ".eh", TAI->getGlobalPrefix());
Bill Wendlingef9211a2007-09-18 01:47:22 +0000215}
216
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
218 // What's my mangled name?
219 CurrentFnName = Mang->getValueName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000220 IncrementFunctionNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221}
222
223/// EmitConstantPool - Print to the current output stream assembly
224/// representations of the constants in the constant pool MCP. This is
225/// used to print out constants which have been "spilled to memory" by
226/// the code generator.
227///
228void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
229 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
230 if (CP.empty()) return;
231
232 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
233 // in special sections.
234 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
235 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
236 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
237 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
238 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
239 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
240 MachineConstantPoolEntry CPE = CP[i];
241 const Type *Ty = CPE.getType();
242 if (TAI->getFourByteConstantSection() &&
Duncan Sands8157ef42007-11-05 00:04:43 +0000243 TM.getTargetData()->getABITypeSize(Ty) == 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 FourByteCPs.push_back(std::make_pair(CPE, i));
245 else if (TAI->getEightByteConstantSection() &&
Duncan Sands8157ef42007-11-05 00:04:43 +0000246 TM.getTargetData()->getABITypeSize(Ty) == 8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 EightByteCPs.push_back(std::make_pair(CPE, i));
248 else if (TAI->getSixteenByteConstantSection() &&
Duncan Sands8157ef42007-11-05 00:04:43 +0000249 TM.getTargetData()->getABITypeSize(Ty) == 16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 SixteenByteCPs.push_back(std::make_pair(CPE, i));
251 else
252 OtherCPs.push_back(std::make_pair(CPE, i));
253 }
254
255 unsigned Alignment = MCP->getConstantPoolAlignment();
256 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
257 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
258 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
259 SixteenByteCPs);
260 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
261}
262
263void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
264 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
265 if (CP.empty()) return;
266
267 SwitchToDataSection(Section);
268 EmitAlignment(Alignment);
269 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Cheng477013c2007-10-14 05:57:21 +0000270 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Owen Anderson847b99b2008-08-21 00:14:44 +0000271 << CP[i].second << ":\t\t\t\t\t";
272 // O << TAI->getCommentString() << ' ' <<
273 // WriteTypeSymbolic(O, CP[i].first.getType(), 0);
Chris Lattner2e5c7ae2008-08-19 04:44:30 +0000274 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 if (CP[i].first.isMachineConstantPoolEntry())
276 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
277 else
278 EmitGlobalConstant(CP[i].first.Val.ConstVal);
279 if (i != e-1) {
280 const Type *Ty = CP[i].first.getType();
281 unsigned EntSize =
Duncan Sands8157ef42007-11-05 00:04:43 +0000282 TM.getTargetData()->getABITypeSize(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
284 // Emit inter-object padding for alignment.
285 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
286 }
287 }
288}
289
290/// EmitJumpTableInfo - Print assembly representations of the jump tables used
291/// by the current function to the current output stream.
292///
293void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
294 MachineFunction &MF) {
295 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
296 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000297
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
299
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 // Pick the directive to use to print the jump table entries, and switch to
301 // the appropriate section.
302 TargetLowering *LoweringInfo = TM.getTargetLowering();
303
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000304 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
305 const Function *F = MF.getFunction();
306 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000308 !JumpTableDataSection ||
309 SectionFlags & SectionFlags::Linkonce) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 // In PIC mode, we need to emit the jump table to the same section as the
311 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000312 // We should also do if the section name is NULL or function is declared in
313 // discardable section.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
315 } else {
316 SwitchToDataSection(JumpTableDataSection);
317 }
318
319 EmitAlignment(Log2_32(MJTI->getAlignment()));
320
321 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
322 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
323
324 // If this jump table was deleted, ignore it.
325 if (JTBBs.empty()) continue;
326
327 // For PIC codegen, if possible we want to use the SetDirective to reduce
328 // the number of relocations the assembler will generate for the jump table.
329 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000330 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 if (TAI->getSetDirective() && IsPic)
332 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000333 if (EmittedSets.insert(JTBBs[ii]))
334 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335
336 // On some targets (e.g. darwin) we want to emit two consequtive labels
337 // before each jump table. The first label is never referenced, but tells
338 // the assembler and linker the extents of the jump table object. The
339 // second label is actually referenced by the code.
340 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
Evan Cheng477013c2007-10-14 05:57:21 +0000341 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342
Evan Cheng477013c2007-10-14 05:57:21 +0000343 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
344 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345
346 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000347 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 O << '\n';
349 }
350 }
351}
352
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000353void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
354 const MachineBasicBlock *MBB,
355 unsigned uid) const {
356 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
357
358 // Use JumpTableDirective otherwise honor the entry size from the jump table
359 // info.
360 const char *JTEntryDirective = TAI->getJumpTableDirective();
361 bool HadJTEntryDirective = JTEntryDirective != NULL;
362 if (!HadJTEntryDirective) {
363 JTEntryDirective = MJTI->getEntrySize() == 4 ?
364 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
365 }
366
367 O << JTEntryDirective << ' ';
368
369 // If we have emitted set directives for the jump table entries, print
370 // them rather than the entries themselves. If we're emitting PIC, then
371 // emit the table entries as differences between two text section labels.
372 // If we're emitting non-PIC code, then emit the entries as direct
373 // references to the target basic blocks.
374 if (IsPic) {
375 if (TAI->getSetDirective()) {
376 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
377 << '_' << uid << "_set_" << MBB->getNumber();
378 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000379 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000380 // If the arch uses custom Jump Table directives, don't calc relative to
381 // JT
382 if (!HadJTEntryDirective)
383 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
384 << getFunctionNumber() << '_' << uid;
385 }
386 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000387 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000388 }
389}
390
391
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
393/// special global used by LLVM. If so, emit it and return true, otherwise
394/// do nothing and return false.
395bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000396 if (GV->getName() == "llvm.used") {
397 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
398 EmitLLVMUsedList(GV->getInitializer());
399 return true;
400 }
401
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 // Ignore debug and non-emitted data.
403 if (GV->getSection() == "llvm.metadata") return true;
404
405 if (!GV->hasAppendingLinkage()) return false;
406
407 assert(GV->hasInitializer() && "Not a special LLVM global!");
408
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 const TargetData *TD = TM.getTargetData();
410 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
411 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
412 SwitchToDataSection(TAI->getStaticCtorsSection());
413 EmitAlignment(Align, 0);
414 EmitXXStructorList(GV->getInitializer());
415 return true;
416 }
417
418 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
419 SwitchToDataSection(TAI->getStaticDtorsSection());
420 EmitAlignment(Align, 0);
421 EmitXXStructorList(GV->getInitializer());
422 return true;
423 }
424
425 return false;
426}
427
Dale Johannesen4911fb82008-09-03 20:34:58 +0000428/// findGlobalValue - if CV is an expression equivalent to a single
429/// global value, return that value.
430const GlobalValue * AsmPrinter::findGlobalValue(const Constant *CV) {
431 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
432 return GV;
433 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
434 const TargetData *TD = TM.getTargetData();
435 unsigned Opcode = CE->getOpcode();
436 switch (Opcode) {
437 case Instruction::GetElementPtr: {
438 const Constant *ptrVal = CE->getOperand(0);
439 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
440 if (TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], idxVec.size()))
441 return 0;
442 return findGlobalValue(ptrVal);
443 }
444 case Instruction::BitCast:
445 return findGlobalValue(CE->getOperand(0));
446 default:
447 return 0;
448 }
449 }
450 return 0;
451}
452
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
454/// global in the specified llvm.used list as being used with this directive.
Dale Johannesen2d34f9f2008-09-09 01:21:22 +0000455/// Internally linked data beginning with the PrivateGlobalPrefix or the
456/// LessPrivateGlobalPrefix does not have the directive emitted (this
457/// occurs in ObjC metadata).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458void AsmPrinter::EmitLLVMUsedList(Constant *List) {
459 const char *Directive = TAI->getUsedDirective();
460
461 // Should be an array of 'sbyte*'.
462 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
463 if (InitList == 0) return;
464
465 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000466 const GlobalValue *GV = findGlobalValue(InitList->getOperand(i));
Dale Johannesen2d34f9f2008-09-09 01:21:22 +0000467 if (GV) {
468 if (GV->hasInternalLinkage() && !isa<Function>(GV) &&
469 ((strlen(TAI->getPrivateGlobalPrefix()) != 0 &&
470 Mang->getValueName(GV)
471 .substr(0,strlen(TAI->getPrivateGlobalPrefix())) ==
472 TAI->getPrivateGlobalPrefix()) ||
473 (strlen(TAI->getLessPrivateGlobalPrefix()) != 0 &&
474 Mang->getValueName(GV)
475 .substr(0,strlen(TAI->getLessPrivateGlobalPrefix())) ==
476 TAI->getLessPrivateGlobalPrefix())))
477 continue;
Dale Johannesen4911fb82008-09-03 20:34:58 +0000478 O << Directive;
479 EmitConstantValueOnly(InitList->getOperand(i));
480 O << '\n';
481 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 }
483}
484
485/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
486/// function pointers, ignoring the init priority.
487void AsmPrinter::EmitXXStructorList(Constant *List) {
488 // Should be an array of '{ int, void ()* }' structs. The first value is the
489 // init priority, which we ignore.
490 if (!isa<ConstantArray>(List)) return;
491 ConstantArray *InitList = cast<ConstantArray>(List);
492 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
493 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
494 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
495
496 if (CS->getOperand(1)->isNullValue())
497 return; // Found a null terminator, exit printing.
498 // Emit the function pointer.
499 EmitGlobalConstant(CS->getOperand(1));
500 }
501}
502
503/// getGlobalLinkName - Returns the asm/link name of of the specified
504/// global variable. Should be overridden by each target asm printer to
505/// generate the appropriate value.
506const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
507 std::string LinkName;
508
509 if (isa<Function>(GV)) {
510 LinkName += TAI->getFunctionAddrPrefix();
511 LinkName += Mang->getValueName(GV);
512 LinkName += TAI->getFunctionAddrSuffix();
513 } else {
514 LinkName += TAI->getGlobalVarAddrPrefix();
515 LinkName += Mang->getValueName(GV);
516 LinkName += TAI->getGlobalVarAddrSuffix();
517 }
518
519 return LinkName;
520}
521
522/// EmitExternalGlobal - Emit the external reference to a global variable.
523/// Should be overridden if an indirect reference should be used.
524void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
525 O << getGlobalLinkName(GV);
526}
527
528
529
530//===----------------------------------------------------------------------===//
531/// LEB 128 number encoding.
532
533/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
534/// representing an unsigned leb128 value.
535void AsmPrinter::PrintULEB128(unsigned Value) const {
536 do {
537 unsigned Byte = Value & 0x7f;
538 Value >>= 7;
539 if (Value) Byte |= 0x80;
Owen Anderson847b99b2008-08-21 00:14:44 +0000540 O << "0x" << utohexstr(Byte);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 if (Value) O << ", ";
542 } while (Value);
543}
544
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
546/// representing a signed leb128 value.
547void AsmPrinter::PrintSLEB128(int Value) const {
548 int Sign = Value >> (8 * sizeof(Value) - 1);
549 bool IsMore;
aslc200b112008-08-16 12:57:46 +0000550
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 do {
552 unsigned Byte = Value & 0x7f;
553 Value >>= 7;
554 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
555 if (IsMore) Byte |= 0x80;
Owen Anderson847b99b2008-08-21 00:14:44 +0000556 O << "0x" << utohexstr(Byte);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 if (IsMore) O << ", ";
558 } while (IsMore);
559}
560
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561//===--------------------------------------------------------------------===//
562// Emission and print routines
563//
564
565/// PrintHex - Print a value as a hexidecimal value.
566///
567void AsmPrinter::PrintHex(int Value) const {
Owen Anderson847b99b2008-08-21 00:14:44 +0000568 O << "0x" << utohexstr(static_cast<unsigned>(Value));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569}
570
571/// EOL - Print a newline character to asm stream. If a comment is present
572/// then it will be printed first. Comments should not contain '\n'.
573void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000574 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000576
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000578 if (VerboseAsm && !Comment.empty()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000579 O << '\t'
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 << TAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000581 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 << Comment;
583 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000584 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585}
586
Owen Anderson367bfbb2008-07-01 21:16:27 +0000587void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000588 if (VerboseAsm && *Comment) {
Owen Anderson367bfbb2008-07-01 21:16:27 +0000589 O << '\t'
590 << TAI->getCommentString()
591 << ' '
592 << Comment;
593 }
594 O << '\n';
595}
596
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
598/// unsigned leb128 value.
599void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
600 if (TAI->hasLEB128()) {
601 O << "\t.uleb128\t"
602 << Value;
603 } else {
604 O << TAI->getData8bitsDirective();
605 PrintULEB128(Value);
606 }
607}
608
609/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
610/// signed leb128 value.
611void AsmPrinter::EmitSLEB128Bytes(int Value) const {
612 if (TAI->hasLEB128()) {
613 O << "\t.sleb128\t"
614 << Value;
615 } else {
616 O << TAI->getData8bitsDirective();
617 PrintSLEB128(Value);
618 }
619}
620
621/// EmitInt8 - Emit a byte directive and value.
622///
623void AsmPrinter::EmitInt8(int Value) const {
624 O << TAI->getData8bitsDirective();
625 PrintHex(Value & 0xFF);
626}
627
628/// EmitInt16 - Emit a short directive and value.
629///
630void AsmPrinter::EmitInt16(int Value) const {
631 O << TAI->getData16bitsDirective();
632 PrintHex(Value & 0xFFFF);
633}
634
635/// EmitInt32 - Emit a long directive and value.
636///
637void AsmPrinter::EmitInt32(int Value) const {
638 O << TAI->getData32bitsDirective();
639 PrintHex(Value);
640}
641
642/// EmitInt64 - Emit a long long directive and value.
643///
644void AsmPrinter::EmitInt64(uint64_t Value) const {
645 if (TAI->getData64bitsDirective()) {
646 O << TAI->getData64bitsDirective();
647 PrintHex(Value);
648 } else {
649 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000650 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 EmitInt32(unsigned(Value));
652 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000653 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 EmitInt32(unsigned(Value >> 32));
655 }
656 }
657}
658
659/// toOctal - Convert the low order bits of X into an octal digit.
660///
661static inline char toOctal(int X) {
662 return (X&7)+'0';
663}
664
665/// printStringChar - Print a char, escaped if necessary.
666///
Owen Anderson847b99b2008-08-21 00:14:44 +0000667static void printStringChar(raw_ostream &O, char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 if (C == '"') {
669 O << "\\\"";
670 } else if (C == '\\') {
671 O << "\\\\";
672 } else if (isprint(C)) {
673 O << C;
674 } else {
675 switch(C) {
676 case '\b': O << "\\b"; break;
677 case '\f': O << "\\f"; break;
678 case '\n': O << "\\n"; break;
679 case '\r': O << "\\r"; break;
680 case '\t': O << "\\t"; break;
681 default:
682 O << '\\';
683 O << toOctal(C >> 6);
684 O << toOctal(C >> 3);
685 O << toOctal(C >> 0);
686 break;
687 }
688 }
689}
690
691/// EmitString - Emit a string with quotes and a null terminator.
692/// Special characters are emitted properly.
693/// \literal (Eg. '\t') \endliteral
694void AsmPrinter::EmitString(const std::string &String) const {
695 const char* AscizDirective = TAI->getAscizDirective();
696 if (AscizDirective)
697 O << AscizDirective;
698 else
699 O << TAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000700 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 for (unsigned i = 0, N = String.size(); i < N; ++i) {
702 unsigned char C = String[i];
703 printStringChar(O, C);
704 }
705 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000706 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 else
708 O << "\\0\"";
709}
710
711
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000712/// EmitFile - Emit a .file directive.
713void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
714 O << "\t.file\t" << Number << " \"";
715 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
716 unsigned char C = Name[i];
717 printStringChar(O, C);
718 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000719 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000720}
721
722
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723//===----------------------------------------------------------------------===//
724
725// EmitAlignment - Emit an alignment directive to the specified power of
726// two boundary. For example, if you pass in 3 here, you will get an 8
727// byte alignment. If a global value is specified, and if that global has
728// an explicit alignment requested, it will unconditionally override the
729// alignment request. However, if ForcedAlignBits is specified, this value
730// has final say: the ultimate alignment will be the max of ForcedAlignBits
731// and the alignment computed with NumBits and the global.
732//
733// The algorithm is:
734// Align = NumBits;
735// if (GV && GV->hasalignment) Align = GV->getalignment();
736// Align = std::max(Align, ForcedAlignBits);
737//
738void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000739 unsigned ForcedAlignBits,
740 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741 if (GV && GV->getAlignment())
742 NumBits = Log2_32(GV->getAlignment());
743 NumBits = std::max(NumBits, ForcedAlignBits);
744
745 if (NumBits == 0) return; // No need to emit alignment.
746 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Chengc1f41aa2007-07-25 23:35:07 +0000747 O << TAI->getAlignDirective() << NumBits;
Evan Cheng45c1edb2008-02-28 00:43:03 +0000748
749 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng7e7d1942008-02-29 19:36:59 +0000750 UseFillExpr &= IsInTextSection && FillValue;
Owen Anderson847b99b2008-08-21 00:14:44 +0000751 if (UseFillExpr) O << ",0x" << utohexstr(FillValue);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000752 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753}
754
755
756/// EmitZeros - Emit a block of zeros.
757///
758void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
759 if (NumZeros) {
760 if (TAI->getZeroDirective()) {
761 O << TAI->getZeroDirective() << NumZeros;
762 if (TAI->getZeroDirectiveSuffix())
763 O << TAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000764 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 } else {
766 for (; NumZeros; --NumZeros)
767 O << TAI->getData8bitsDirective() << "0\n";
768 }
769 }
770}
771
772// Print out the specified constant, without a storage class. Only the
773// constants valid in constant expressions can occur here.
774void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
775 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000776 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000777 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000778 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
780 // This is a constant address for a global variable or function. Use the
781 // name of the variable or function as the address value, possibly
782 // decorating it with GlobalVarAddrPrefix/Suffix or
783 // FunctionAddrPrefix/Suffix (these all default to "" )
784 if (isa<Function>(GV)) {
785 O << TAI->getFunctionAddrPrefix()
786 << Mang->getValueName(GV)
787 << TAI->getFunctionAddrSuffix();
788 } else {
789 O << TAI->getGlobalVarAddrPrefix()
790 << Mang->getValueName(GV)
791 << TAI->getGlobalVarAddrSuffix();
792 }
793 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
794 const TargetData *TD = TM.getTargetData();
795 unsigned Opcode = CE->getOpcode();
796 switch (Opcode) {
797 case Instruction::GetElementPtr: {
798 // generate a symbolic expression for the byte address
799 const Constant *ptrVal = CE->getOperand(0);
800 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
801 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
802 idxVec.size())) {
803 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000804 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 EmitConstantValueOnly(ptrVal);
806 if (Offset > 0)
807 O << ") + " << Offset;
808 else if (Offset < 0)
809 O << ") - " << -Offset;
810 } else {
811 EmitConstantValueOnly(ptrVal);
812 }
813 break;
814 }
815 case Instruction::Trunc:
816 case Instruction::ZExt:
817 case Instruction::SExt:
818 case Instruction::FPTrunc:
819 case Instruction::FPExt:
820 case Instruction::UIToFP:
821 case Instruction::SIToFP:
822 case Instruction::FPToUI:
823 case Instruction::FPToSI:
824 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
825 break;
826 case Instruction::BitCast:
827 return EmitConstantValueOnly(CE->getOperand(0));
828
829 case Instruction::IntToPtr: {
830 // Handle casts to pointers by changing them into casts to the appropriate
831 // integer type. This promotes constant folding and simplifies this code.
832 Constant *Op = CE->getOperand(0);
833 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
834 return EmitConstantValueOnly(Op);
835 }
836
837
838 case Instruction::PtrToInt: {
839 // Support only foldable casts to/from pointers that can be eliminated by
840 // changing the pointer to the appropriately sized integer type.
841 Constant *Op = CE->getOperand(0);
842 const Type *Ty = CE->getType();
843
844 // We can emit the pointer value into this slot if the slot is an
845 // integer slot greater or equal to the size of the pointer.
Nick Lewycky95353ad2008-08-08 06:34:07 +0000846 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000848
849 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000851 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
Chris Lattner89b36582008-08-17 07:19:36 +0000852
853 SmallString<40> S;
854 ptrMask.toStringUnsigned(S);
855 O << ") & " << S.c_str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 break;
857 }
858 case Instruction::Add:
859 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000860 case Instruction::And:
861 case Instruction::Or:
862 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000863 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000865 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000866 switch (Opcode) {
867 case Instruction::Add:
868 O << " + ";
869 break;
870 case Instruction::Sub:
871 O << " - ";
872 break;
873 case Instruction::And:
874 O << " & ";
875 break;
876 case Instruction::Or:
877 O << " | ";
878 break;
879 case Instruction::Xor:
880 O << " ^ ";
881 break;
882 default:
883 break;
884 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000885 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000887 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 break;
889 default:
890 assert(0 && "Unsupported operator!");
891 }
892 } else {
893 assert(0 && "Unknown constant value!");
894 }
895}
896
897/// printAsCString - Print the specified array as a C compatible string, only if
898/// the predicate isString is true.
899///
Owen Anderson847b99b2008-08-21 00:14:44 +0000900static void printAsCString(raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 unsigned LastElt) {
902 assert(CVA->isString() && "Array is not string compatible!");
903
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000904 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905 for (unsigned i = 0; i != LastElt; ++i) {
906 unsigned char C =
907 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
908 printStringChar(O, C);
909 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000910 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911}
912
913/// EmitString - Emit a zero-byte-terminated string constant.
914///
915void AsmPrinter::EmitString(const ConstantArray *CVA) const {
916 unsigned NumElts = CVA->getNumOperands();
917 if (TAI->getAscizDirective() && NumElts &&
918 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
919 O << TAI->getAscizDirective();
920 printAsCString(O, CVA, NumElts-1);
921 } else {
922 O << TAI->getAsciiDirective();
923 printAsCString(O, CVA, NumElts);
924 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000925 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926}
927
928/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sands4afc5752008-06-04 08:21:45 +0000929void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 const TargetData *TD = TM.getTargetData();
Duncan Sands4afc5752008-06-04 08:21:45 +0000931 unsigned Size = TD->getABITypeSize(CV->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932
933 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands8157ef42007-11-05 00:04:43 +0000934 EmitZeros(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 return;
936 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
937 if (CVA->isString()) {
938 EmitString(CVA);
939 } else { // Not a string. Print the values in successive locations
Duncan Sands8157ef42007-11-05 00:04:43 +0000940 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Duncan Sands4afc5752008-06-04 08:21:45 +0000941 EmitGlobalConstant(CVA->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 }
943 return;
944 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
945 // Print the fields in successive locations. Pad to align if needed!
946 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
947 uint64_t sizeSoFar = 0;
948 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
949 const Constant* field = CVS->getOperand(i);
950
951 // Check if padding is needed and insert one or more 0s.
Duncan Sands4afc5752008-06-04 08:21:45 +0000952 uint64_t fieldSize = TD->getABITypeSize(field->getType());
Duncan Sandsc15d58c2007-11-05 18:03:02 +0000953 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 - cvsLayout->getElementOffset(i)) - fieldSize;
955 sizeSoFar += fieldSize + padSize;
956
Duncan Sands4afc5752008-06-04 08:21:45 +0000957 // Now print the actual field value.
958 EmitGlobalConstant(field);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959
Duncan Sandsc15d58c2007-11-05 18:03:02 +0000960 // Insert padding - this may include padding to increase the size of the
961 // current field up to the ABI size (if the struct is not packed) as well
962 // as padding to ensure that the next field starts at the right offset.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963 EmitZeros(padSize);
964 }
965 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
966 "Layout of constant struct may be incorrect!");
967 return;
968 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
969 // FP Constants are printed as integer constants to avoid losing
970 // precision...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000972 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000973 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 if (TAI->getData64bitsDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000975 O << TAI->getData64bitsDirective() << i << '\t'
976 << TAI->getCommentString() << " double value: " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 else if (TD->isBigEndian()) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000978 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000979 << '\t' << TAI->getCommentString()
980 << " double most significant word " << Val << '\n';
Dale Johannesen1616e902007-09-11 18:32:33 +0000981 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000982 << '\t' << TAI->getCommentString()
983 << " double least significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +0000985 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000986 << '\t' << TAI->getCommentString()
987 << " double least significant word " << Val << '\n';
Dale Johannesen1616e902007-09-11 18:32:33 +0000988 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000989 << '\t' << TAI->getCommentString()
990 << " double most significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 }
992 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000993 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000994 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
995 O << TAI->getData32bitsDirective()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000996 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000997 << '\t' << TAI->getCommentString() << " float " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000999 } else if (CFP->getType() == Type::X86_FP80Ty) {
1000 // all long double variants are printed as hex
Dale Johannesen693aa822007-09-26 23:20:33 +00001001 // api needed to prevent premature destruction
1002 APInt api = CFP->getValueAPF().convertToAPInt();
1003 const uint64_t *p = api.getRawData();
Chris Lattner00d63062008-01-27 06:09:28 +00001004 APFloat DoubleVal = CFP->getValueAPF();
1005 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001006 if (TD->isBigEndian()) {
1007 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001008 << '\t' << TAI->getCommentString()
Chris Lattner00d63062008-01-27 06:09:28 +00001009 << " long double most significant halfword of ~"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001010 << DoubleVal.convertToDouble() << '\n';
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001011 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001012 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001013 << " long double next halfword\n";
1014 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001015 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001016 << " long double next halfword\n";
1017 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001018 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001019 << " long double next halfword\n";
1020 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001021 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001022 << " long double least significant halfword\n";
1023 } else {
1024 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001025 << '\t' << TAI->getCommentString()
Chris Lattner00d63062008-01-27 06:09:28 +00001026 << " long double least 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])
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] >> 32)
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[0] >> 48)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001038 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001039 << " long double most significant halfword\n";
1040 }
Duncan Sands8157ef42007-11-05 00:04:43 +00001041 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001042 return;
Dale Johannesend3b6af32007-10-11 23:32:15 +00001043 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1044 // all long double variants are printed as hex
1045 // api needed to prevent premature destruction
1046 APInt api = CFP->getValueAPF().convertToAPInt();
1047 const uint64_t *p = api.getRawData();
1048 if (TD->isBigEndian()) {
1049 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001050 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001051 << " long double most significant word\n";
1052 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001053 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001054 << " long double next word\n";
1055 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001056 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001057 << " long double next word\n";
1058 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001059 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001060 << " long double least significant word\n";
1061 } else {
1062 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001063 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001064 << " long double least significant word\n";
1065 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001066 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001067 << " long double next word\n";
1068 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001069 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001070 << " long double next word\n";
1071 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001072 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001073 << " long double most significant word\n";
1074 }
1075 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001076 } else assert(0 && "Floating point constant type not handled");
Dan Gohman07a91ea2008-09-08 16:40:13 +00001077 } else if (CV->getType()->isInteger() &&
1078 cast<IntegerType>(CV->getType())->getBitWidth() >= 64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Dan Gohman07a91ea2008-09-08 16:40:13 +00001080 unsigned BitWidth = CI->getBitWidth();
1081 assert(isPowerOf2_32(BitWidth) &&
1082 "Non-power-of-2-sized integers not handled!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083
Dan Gohman07a91ea2008-09-08 16:40:13 +00001084 // We don't expect assemblers to support integer data directives
1085 // for more than 64 bits, so we emit the data in at most 64-bit
1086 // quantities at a time.
1087 const uint64_t *RawData = CI->getValue().getRawData();
1088 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1089 uint64_t Val;
1090 if (TD->isBigEndian())
1091 Val = RawData[e - i - 1];
1092 else
1093 Val = RawData[i];
1094
1095 if (TAI->getData64bitsDirective())
1096 O << TAI->getData64bitsDirective() << Val << '\n';
1097 else if (TD->isBigEndian()) {
1098 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1099 << '\t' << TAI->getCommentString()
1100 << " Double-word most significant word " << Val << '\n';
1101 O << TAI->getData32bitsDirective() << unsigned(Val)
1102 << '\t' << TAI->getCommentString()
1103 << " Double-word least significant word " << Val << '\n';
1104 } else {
1105 O << TAI->getData32bitsDirective() << unsigned(Val)
1106 << '\t' << TAI->getCommentString()
1107 << " Double-word least significant word " << Val << '\n';
1108 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
1109 << '\t' << TAI->getCommentString()
1110 << " Double-word most significant word " << Val << '\n';
1111 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001112 }
1113 return;
1114 }
1115 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1116 const VectorType *PTy = CP->getType();
1117
1118 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sands4afc5752008-06-04 08:21:45 +00001119 EmitGlobalConstant(CP->getOperand(I));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001120
1121 return;
1122 }
1123
1124 const Type *type = CV->getType();
1125 printDataDirective(type);
1126 EmitConstantValueOnly(CV);
Scott Michele067c3c2008-06-03 15:39:51 +00001127 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner89b36582008-08-17 07:19:36 +00001128 SmallString<40> S;
1129 CI->getValue().toStringUnsigned(S, 16);
1130 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
Scott Michele067c3c2008-06-03 15:39:51 +00001131 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001132 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001133}
1134
Chris Lattner89b36582008-08-17 07:19:36 +00001135void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136 // Target doesn't support this yet!
1137 abort();
1138}
1139
1140/// PrintSpecial - Print information related to the specified machine instr
1141/// that is independent of the operand, and may be independent of the instr
1142/// itself. This can be useful for portably encoding the comment character
1143/// or other bits of target-specific knowledge into the asmstrings. The
1144/// syntax used is ${:comment}. Targets can override this to add support
1145/// for their own strange codes.
1146void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
1147 if (!strcmp(Code, "private")) {
1148 O << TAI->getPrivateGlobalPrefix();
1149 } else if (!strcmp(Code, "comment")) {
1150 O << TAI->getCommentString();
1151 } else if (!strcmp(Code, "uid")) {
1152 // Assign a unique ID to this machine instruction.
1153 static const MachineInstr *LastMI = 0;
1154 static const Function *F = 0;
1155 static unsigned Counter = 0U-1;
1156
1157 // Comparing the address of MI isn't sufficient, because machineinstrs may
1158 // be allocated to the same address across functions.
1159 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1160
1161 // If this is a new machine instruction, bump the counter.
1162 if (LastMI != MI || F != ThisF) {
1163 ++Counter;
1164 LastMI = MI;
1165 F = ThisF;
1166 }
1167 O << Counter;
1168 } else {
1169 cerr << "Unknown special formatter '" << Code
1170 << "' for machine instr: " << *MI;
1171 exit(1);
1172 }
1173}
1174
1175
1176/// printInlineAsm - This method formats and prints the specified machine
1177/// instruction that is an inline asm.
1178void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1179 unsigned NumOperands = MI->getNumOperands();
1180
1181 // Count the number of register definitions.
1182 unsigned NumDefs = 0;
Dan Gohman38a9a9f2007-09-14 20:33:02 +00001183 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001184 ++NumDefs)
1185 assert(NumDefs != NumOperands-1 && "No asm string?");
1186
1187 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
1188
1189 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1190 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1191
Dale Johannesene99fc902008-01-29 02:21:21 +00001192 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1193 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001194 if (AsmStr[0] == 0) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001195 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 return;
1197 }
1198
1199 O << TAI->getInlineAsmStart() << "\n\t";
1200
1201 // The variant of the current asmprinter.
1202 int AsmPrinterVariant = TAI->getAssemblerDialect();
1203
1204 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1205 const char *LastEmitted = AsmStr; // One past the last character emitted.
1206
1207 while (*LastEmitted) {
1208 switch (*LastEmitted) {
1209 default: {
1210 // Not a special case, emit the string section literally.
1211 const char *LiteralEnd = LastEmitted+1;
1212 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1213 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1214 ++LiteralEnd;
1215 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1216 O.write(LastEmitted, LiteralEnd-LastEmitted);
1217 LastEmitted = LiteralEnd;
1218 break;
1219 }
1220 case '\n':
1221 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001222 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001223 break;
1224 case '$': {
1225 ++LastEmitted; // Consume '$' character.
1226 bool Done = true;
1227
1228 // Handle escapes.
1229 switch (*LastEmitted) {
1230 default: Done = false; break;
1231 case '$': // $$ -> $
1232 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1233 O << '$';
1234 ++LastEmitted; // Consume second '$' character.
1235 break;
1236 case '(': // $( -> same as GCC's { character.
1237 ++LastEmitted; // Consume '(' character.
1238 if (CurVariant != -1) {
1239 cerr << "Nested variants found in inline asm string: '"
1240 << AsmStr << "'\n";
1241 exit(1);
1242 }
1243 CurVariant = 0; // We're in the first variant now.
1244 break;
1245 case '|':
1246 ++LastEmitted; // consume '|' character.
1247 if (CurVariant == -1) {
1248 cerr << "Found '|' character outside of variant in inline asm "
1249 << "string: '" << AsmStr << "'\n";
1250 exit(1);
1251 }
1252 ++CurVariant; // We're in the next variant.
1253 break;
1254 case ')': // $) -> same as GCC's } char.
1255 ++LastEmitted; // consume ')' character.
1256 if (CurVariant == -1) {
1257 cerr << "Found '}' character outside of variant in inline asm "
1258 << "string: '" << AsmStr << "'\n";
1259 exit(1);
1260 }
1261 CurVariant = -1;
1262 break;
1263 }
1264 if (Done) break;
1265
1266 bool HasCurlyBraces = false;
1267 if (*LastEmitted == '{') { // ${variable}
1268 ++LastEmitted; // Consume '{' character.
1269 HasCurlyBraces = true;
1270 }
1271
1272 const char *IDStart = LastEmitted;
1273 char *IDEnd;
1274 errno = 0;
1275 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1276 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
1277 cerr << "Bad $ operand number in inline asm string: '"
1278 << AsmStr << "'\n";
1279 exit(1);
1280 }
1281 LastEmitted = IDEnd;
1282
1283 char Modifier[2] = { 0, 0 };
1284
1285 if (HasCurlyBraces) {
1286 // If we have curly braces, check for a modifier character. This
1287 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1288 if (*LastEmitted == ':') {
1289 ++LastEmitted; // Consume ':' character.
1290 if (*LastEmitted == 0) {
1291 cerr << "Bad ${:} expression in inline asm string: '"
1292 << AsmStr << "'\n";
1293 exit(1);
1294 }
1295
1296 Modifier[0] = *LastEmitted;
1297 ++LastEmitted; // Consume modifier character.
1298 }
1299
1300 if (*LastEmitted != '}') {
1301 cerr << "Bad ${} expression in inline asm string: '"
1302 << AsmStr << "'\n";
1303 exit(1);
1304 }
1305 ++LastEmitted; // Consume '}' character.
1306 }
1307
1308 if ((unsigned)Val >= NumOperands-1) {
1309 cerr << "Invalid $ operand number in inline asm string: '"
1310 << AsmStr << "'\n";
1311 exit(1);
1312 }
1313
1314 // Okay, we finally have a value number. Ask the target to print this
1315 // operand!
1316 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1317 unsigned OpNo = 1;
1318
1319 bool Error = false;
1320
1321 // Scan to find the machine operand number for the operand.
1322 for (; Val; --Val) {
1323 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001324 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 OpNo += (OpFlags >> 3) + 1;
1326 }
1327
1328 if (OpNo >= MI->getNumOperands()) {
1329 Error = true;
1330 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001331 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332 ++OpNo; // Skip over the ID number.
1333
Dale Johannesencfb19e62007-11-05 21:20:28 +00001334 if (Modifier[0]=='l') // labels are target independent
Chris Lattner6017d482007-12-30 23:10:15 +00001335 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Cheng45c1edb2008-02-28 00:43:03 +00001336 false, false, false);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001337 else {
1338 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1339 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1340 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1341 Modifier[0] ? Modifier : 0);
1342 } else {
1343 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1344 Modifier[0] ? Modifier : 0);
1345 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346 }
1347 }
1348 if (Error) {
1349 cerr << "Invalid operand found in inline asm: '"
1350 << AsmStr << "'\n";
1351 MI->dump();
1352 exit(1);
1353 }
1354 }
1355 break;
1356 }
1357 }
1358 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001359 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360}
1361
Evan Cheng3c0eda52008-03-15 00:03:38 +00001362/// printImplicitDef - This method prints the specified machine instruction
1363/// that is an implicit def.
1364void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001365 O << '\t' << TAI->getCommentString() << " implicit-def: "
1366 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng3c0eda52008-03-15 00:03:38 +00001367}
1368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369/// printLabel - This method prints a local label used by debug and
1370/// exception handling tables.
1371void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001372 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373}
1374
Evan Chenga53c40a2008-02-01 09:10:45 +00001375void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng8b988692008-02-02 08:39:46 +00001376 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chenga53c40a2008-02-01 09:10:45 +00001377}
1378
Evan Cheng2e28d622008-02-02 04:07:54 +00001379/// printDeclare - This method prints a local variable declaration used by
1380/// debug tables.
Evan Chengc439a852008-02-04 23:06:48 +00001381/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1382/// entry into dwarf table.
Evan Cheng2e28d622008-02-02 04:07:54 +00001383void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Evan Chengc439a852008-02-04 23:06:48 +00001384 int FI = MI->getOperand(0).getIndex();
1385 GlobalValue *GV = MI->getOperand(1).getGlobal();
1386 MMI->RecordVariable(GV, FI);
Evan Cheng2e28d622008-02-02 04:07:54 +00001387}
1388
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1390/// instruction, using the specified assembler variant. Targets should
1391/// overried this to format as appropriate.
1392bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1393 unsigned AsmVariant, const char *ExtraCode) {
1394 // Target doesn't support this yet!
1395 return true;
1396}
1397
1398bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1399 unsigned AsmVariant,
1400 const char *ExtraCode) {
1401 // Target doesn't support this yet!
1402 return true;
1403}
1404
1405/// printBasicBlockLabel - This method prints the label for the specified
1406/// MachineBasicBlock
1407void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Cheng45c1edb2008-02-28 00:43:03 +00001408 bool printAlign,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 bool printColon,
1410 bool printComment) const {
Evan Cheng45c1edb2008-02-28 00:43:03 +00001411 if (printAlign) {
1412 unsigned Align = MBB->getAlignment();
1413 if (Align)
1414 EmitAlignment(Log2_32(Align));
1415 }
1416
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001417 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng477013c2007-10-14 05:57:21 +00001418 << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001419 if (printColon)
1420 O << ':';
1421 if (printComment && MBB->getBasicBlock())
Dan Gohman0912cda2007-07-30 15:06:25 +00001422 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng76443dc2008-07-08 00:55:58 +00001423 << MBB->getBasicBlock()->getNameStart();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424}
1425
Evan Cheng6fb06762007-11-09 01:32:10 +00001426/// printPICJumpTableSetLabel - This method prints a set label for the
1427/// specified MachineBasicBlock for a jumptable entry.
1428void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1429 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430 if (!TAI->getSetDirective())
1431 return;
1432
1433 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001434 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001435 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001436 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1437 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438}
1439
Evan Cheng6fb06762007-11-09 01:32:10 +00001440void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1441 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 if (!TAI->getSetDirective())
1443 return;
1444
1445 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001446 << getFunctionNumber() << '_' << uid << '_' << uid2
1447 << "_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 << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451}
1452
1453/// printDataDirective - This method prints the asm directive for the
1454/// specified type.
1455void AsmPrinter::printDataDirective(const Type *type) {
1456 const TargetData *TD = TM.getTargetData();
1457 switch (type->getTypeID()) {
1458 case Type::IntegerTyID: {
1459 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1460 if (BitWidth <= 8)
1461 O << TAI->getData8bitsDirective();
1462 else if (BitWidth <= 16)
1463 O << TAI->getData16bitsDirective();
1464 else if (BitWidth <= 32)
1465 O << TAI->getData32bitsDirective();
1466 else if (BitWidth <= 64) {
1467 assert(TAI->getData64bitsDirective() &&
1468 "Target cannot handle 64-bit constant exprs!");
1469 O << TAI->getData64bitsDirective();
Dan Gohman07a91ea2008-09-08 16:40:13 +00001470 } else {
1471 assert(0 && "Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 }
1473 break;
1474 }
1475 case Type::PointerTyID:
1476 if (TD->getPointerSize() == 8) {
1477 assert(TAI->getData64bitsDirective() &&
1478 "Target cannot handle 64-bit pointer exprs!");
1479 O << TAI->getData64bitsDirective();
1480 } else {
1481 O << TAI->getData32bitsDirective();
1482 }
1483 break;
1484 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen3b5303b2007-09-28 18:06:58 +00001485 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001486 assert (0 && "Should have already output floating point constant.");
1487 default:
1488 assert (0 && "Can't handle printing this type of thing");
1489 break;
1490 }
1491}
1492
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001493void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
1494 const char *Prefix) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001495 if (Name[0]=='\"')
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001496 O << '\"';
1497 O << TAI->getPrivateGlobalPrefix();
1498 if (Prefix) O << Prefix;
1499 if (Name[0]=='\"')
1500 O << '\"';
1501 if (Name[0]=='\"')
1502 O << Name[1];
Dale Johannesena21b5202008-05-19 21:38:18 +00001503 else
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001504 O << Name;
1505 O << Suffix;
1506 if (Name[0]=='\"')
1507 O << '\"';
Dale Johannesena21b5202008-05-19 21:38:18 +00001508}
Evan Cheng76443dc2008-07-08 00:55:58 +00001509
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001510void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
Evan Cheng76443dc2008-07-08 00:55:58 +00001511 printSuffixedName(Name.c_str(), Suffix);
1512}
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001513
1514void AsmPrinter::printVisibility(const std::string& Name,
1515 unsigned Visibility) const {
1516 if (Visibility == GlobalValue::HiddenVisibility) {
1517 if (const char *Directive = TAI->getHiddenDirective())
1518 O << Directive << Name << '\n';
1519 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1520 if (const char *Directive = TAI->getProtectedDirective())
1521 O << Directive << Name << '\n';
1522 }
1523}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001524
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001525GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1526 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001527 return 0;
1528
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001529 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001530 if (GCPI != GCMetadataPrinters.end())
1531 return GCPI->second;
1532
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001533 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001534
1535 for (GCMetadataPrinterRegistry::iterator
1536 I = GCMetadataPrinterRegistry::begin(),
1537 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1538 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001539 GCMetadataPrinter *GMP = I->instantiate();
1540 GMP->S = S;
1541 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1542 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001543 }
1544
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001545 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001546 abort();
1547}