blob: dc88da954ab60af0c0909cdbebbc9efe974a1560 [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)
Evan Cheng3c0eda52008-03-15 00:03:38 +000040 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o),
41 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
428/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
429/// global in the specified llvm.used list as being used with this directive.
430void AsmPrinter::EmitLLVMUsedList(Constant *List) {
431 const char *Directive = TAI->getUsedDirective();
432
433 // Should be an array of 'sbyte*'.
434 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
435 if (InitList == 0) return;
436
437 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
438 O << Directive;
439 EmitConstantValueOnly(InitList->getOperand(i));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000440 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 }
442}
443
444/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
445/// function pointers, ignoring the init priority.
446void AsmPrinter::EmitXXStructorList(Constant *List) {
447 // Should be an array of '{ int, void ()* }' structs. The first value is the
448 // init priority, which we ignore.
449 if (!isa<ConstantArray>(List)) return;
450 ConstantArray *InitList = cast<ConstantArray>(List);
451 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
452 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
453 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
454
455 if (CS->getOperand(1)->isNullValue())
456 return; // Found a null terminator, exit printing.
457 // Emit the function pointer.
458 EmitGlobalConstant(CS->getOperand(1));
459 }
460}
461
462/// getGlobalLinkName - Returns the asm/link name of of the specified
463/// global variable. Should be overridden by each target asm printer to
464/// generate the appropriate value.
465const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
466 std::string LinkName;
467
468 if (isa<Function>(GV)) {
469 LinkName += TAI->getFunctionAddrPrefix();
470 LinkName += Mang->getValueName(GV);
471 LinkName += TAI->getFunctionAddrSuffix();
472 } else {
473 LinkName += TAI->getGlobalVarAddrPrefix();
474 LinkName += Mang->getValueName(GV);
475 LinkName += TAI->getGlobalVarAddrSuffix();
476 }
477
478 return LinkName;
479}
480
481/// EmitExternalGlobal - Emit the external reference to a global variable.
482/// Should be overridden if an indirect reference should be used.
483void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
484 O << getGlobalLinkName(GV);
485}
486
487
488
489//===----------------------------------------------------------------------===//
490/// LEB 128 number encoding.
491
492/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
493/// representing an unsigned leb128 value.
494void AsmPrinter::PrintULEB128(unsigned Value) const {
495 do {
496 unsigned Byte = Value & 0x7f;
497 Value >>= 7;
498 if (Value) Byte |= 0x80;
Owen Anderson847b99b2008-08-21 00:14:44 +0000499 O << "0x" << utohexstr(Byte);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 if (Value) O << ", ";
501 } while (Value);
502}
503
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
505/// representing a signed leb128 value.
506void AsmPrinter::PrintSLEB128(int Value) const {
507 int Sign = Value >> (8 * sizeof(Value) - 1);
508 bool IsMore;
aslc200b112008-08-16 12:57:46 +0000509
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 do {
511 unsigned Byte = Value & 0x7f;
512 Value >>= 7;
513 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
514 if (IsMore) Byte |= 0x80;
Owen Anderson847b99b2008-08-21 00:14:44 +0000515 O << "0x" << utohexstr(Byte);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 if (IsMore) O << ", ";
517 } while (IsMore);
518}
519
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520//===--------------------------------------------------------------------===//
521// Emission and print routines
522//
523
524/// PrintHex - Print a value as a hexidecimal value.
525///
526void AsmPrinter::PrintHex(int Value) const {
Owen Anderson847b99b2008-08-21 00:14:44 +0000527 O << "0x" << utohexstr(static_cast<unsigned>(Value));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528}
529
530/// EOL - Print a newline character to asm stream. If a comment is present
531/// then it will be printed first. Comments should not contain '\n'.
532void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000533 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000535
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000537 if (VerboseAsm && !Comment.empty()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000538 O << '\t'
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 << TAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000540 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 << Comment;
542 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000543 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544}
545
Owen Anderson367bfbb2008-07-01 21:16:27 +0000546void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000547 if (VerboseAsm && *Comment) {
Owen Anderson367bfbb2008-07-01 21:16:27 +0000548 O << '\t'
549 << TAI->getCommentString()
550 << ' '
551 << Comment;
552 }
553 O << '\n';
554}
555
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
557/// unsigned leb128 value.
558void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
559 if (TAI->hasLEB128()) {
560 O << "\t.uleb128\t"
561 << Value;
562 } else {
563 O << TAI->getData8bitsDirective();
564 PrintULEB128(Value);
565 }
566}
567
568/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
569/// signed leb128 value.
570void AsmPrinter::EmitSLEB128Bytes(int Value) const {
571 if (TAI->hasLEB128()) {
572 O << "\t.sleb128\t"
573 << Value;
574 } else {
575 O << TAI->getData8bitsDirective();
576 PrintSLEB128(Value);
577 }
578}
579
580/// EmitInt8 - Emit a byte directive and value.
581///
582void AsmPrinter::EmitInt8(int Value) const {
583 O << TAI->getData8bitsDirective();
584 PrintHex(Value & 0xFF);
585}
586
587/// EmitInt16 - Emit a short directive and value.
588///
589void AsmPrinter::EmitInt16(int Value) const {
590 O << TAI->getData16bitsDirective();
591 PrintHex(Value & 0xFFFF);
592}
593
594/// EmitInt32 - Emit a long directive and value.
595///
596void AsmPrinter::EmitInt32(int Value) const {
597 O << TAI->getData32bitsDirective();
598 PrintHex(Value);
599}
600
601/// EmitInt64 - Emit a long long directive and value.
602///
603void AsmPrinter::EmitInt64(uint64_t Value) const {
604 if (TAI->getData64bitsDirective()) {
605 O << TAI->getData64bitsDirective();
606 PrintHex(Value);
607 } else {
608 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000609 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 EmitInt32(unsigned(Value));
611 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000612 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 EmitInt32(unsigned(Value >> 32));
614 }
615 }
616}
617
618/// toOctal - Convert the low order bits of X into an octal digit.
619///
620static inline char toOctal(int X) {
621 return (X&7)+'0';
622}
623
624/// printStringChar - Print a char, escaped if necessary.
625///
Owen Anderson847b99b2008-08-21 00:14:44 +0000626static void printStringChar(raw_ostream &O, char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 if (C == '"') {
628 O << "\\\"";
629 } else if (C == '\\') {
630 O << "\\\\";
631 } else if (isprint(C)) {
632 O << C;
633 } else {
634 switch(C) {
635 case '\b': O << "\\b"; break;
636 case '\f': O << "\\f"; break;
637 case '\n': O << "\\n"; break;
638 case '\r': O << "\\r"; break;
639 case '\t': O << "\\t"; break;
640 default:
641 O << '\\';
642 O << toOctal(C >> 6);
643 O << toOctal(C >> 3);
644 O << toOctal(C >> 0);
645 break;
646 }
647 }
648}
649
650/// EmitString - Emit a string with quotes and a null terminator.
651/// Special characters are emitted properly.
652/// \literal (Eg. '\t') \endliteral
653void AsmPrinter::EmitString(const std::string &String) const {
654 const char* AscizDirective = TAI->getAscizDirective();
655 if (AscizDirective)
656 O << AscizDirective;
657 else
658 O << TAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000659 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 for (unsigned i = 0, N = String.size(); i < N; ++i) {
661 unsigned char C = String[i];
662 printStringChar(O, C);
663 }
664 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000665 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 else
667 O << "\\0\"";
668}
669
670
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000671/// EmitFile - Emit a .file directive.
672void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
673 O << "\t.file\t" << Number << " \"";
674 for (unsigned i = 0, N = Name.size(); i < N; ++i) {
675 unsigned char C = Name[i];
676 printStringChar(O, C);
677 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000678 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000679}
680
681
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682//===----------------------------------------------------------------------===//
683
684// EmitAlignment - Emit an alignment directive to the specified power of
685// two boundary. For example, if you pass in 3 here, you will get an 8
686// byte alignment. If a global value is specified, and if that global has
687// an explicit alignment requested, it will unconditionally override the
688// alignment request. However, if ForcedAlignBits is specified, this value
689// has final say: the ultimate alignment will be the max of ForcedAlignBits
690// and the alignment computed with NumBits and the global.
691//
692// The algorithm is:
693// Align = NumBits;
694// if (GV && GV->hasalignment) Align = GV->getalignment();
695// Align = std::max(Align, ForcedAlignBits);
696//
697void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000698 unsigned ForcedAlignBits,
699 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 if (GV && GV->getAlignment())
701 NumBits = Log2_32(GV->getAlignment());
702 NumBits = std::max(NumBits, ForcedAlignBits);
703
704 if (NumBits == 0) return; // No need to emit alignment.
705 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Chengc1f41aa2007-07-25 23:35:07 +0000706 O << TAI->getAlignDirective() << NumBits;
Evan Cheng45c1edb2008-02-28 00:43:03 +0000707
708 unsigned FillValue = TAI->getTextAlignFillValue();
Evan Cheng7e7d1942008-02-29 19:36:59 +0000709 UseFillExpr &= IsInTextSection && FillValue;
Owen Anderson847b99b2008-08-21 00:14:44 +0000710 if (UseFillExpr) O << ",0x" << utohexstr(FillValue);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000711 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712}
713
714
715/// EmitZeros - Emit a block of zeros.
716///
717void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
718 if (NumZeros) {
719 if (TAI->getZeroDirective()) {
720 O << TAI->getZeroDirective() << NumZeros;
721 if (TAI->getZeroDirectiveSuffix())
722 O << TAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000723 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 } else {
725 for (; NumZeros; --NumZeros)
726 O << TAI->getData8bitsDirective() << "0\n";
727 }
728 }
729}
730
731// Print out the specified constant, without a storage class. Only the
732// constants valid in constant expressions can occur here.
733void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
734 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000735 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000737 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
739 // This is a constant address for a global variable or function. Use the
740 // name of the variable or function as the address value, possibly
741 // decorating it with GlobalVarAddrPrefix/Suffix or
742 // FunctionAddrPrefix/Suffix (these all default to "" )
743 if (isa<Function>(GV)) {
744 O << TAI->getFunctionAddrPrefix()
745 << Mang->getValueName(GV)
746 << TAI->getFunctionAddrSuffix();
747 } else {
748 O << TAI->getGlobalVarAddrPrefix()
749 << Mang->getValueName(GV)
750 << TAI->getGlobalVarAddrSuffix();
751 }
752 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
753 const TargetData *TD = TM.getTargetData();
754 unsigned Opcode = CE->getOpcode();
755 switch (Opcode) {
756 case Instruction::GetElementPtr: {
757 // generate a symbolic expression for the byte address
758 const Constant *ptrVal = CE->getOperand(0);
759 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
760 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
761 idxVec.size())) {
762 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000763 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 EmitConstantValueOnly(ptrVal);
765 if (Offset > 0)
766 O << ") + " << Offset;
767 else if (Offset < 0)
768 O << ") - " << -Offset;
769 } else {
770 EmitConstantValueOnly(ptrVal);
771 }
772 break;
773 }
774 case Instruction::Trunc:
775 case Instruction::ZExt:
776 case Instruction::SExt:
777 case Instruction::FPTrunc:
778 case Instruction::FPExt:
779 case Instruction::UIToFP:
780 case Instruction::SIToFP:
781 case Instruction::FPToUI:
782 case Instruction::FPToSI:
783 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
784 break;
785 case Instruction::BitCast:
786 return EmitConstantValueOnly(CE->getOperand(0));
787
788 case Instruction::IntToPtr: {
789 // Handle casts to pointers by changing them into casts to the appropriate
790 // integer type. This promotes constant folding and simplifies this code.
791 Constant *Op = CE->getOperand(0);
792 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
793 return EmitConstantValueOnly(Op);
794 }
795
796
797 case Instruction::PtrToInt: {
798 // Support only foldable casts to/from pointers that can be eliminated by
799 // changing the pointer to the appropriately sized integer type.
800 Constant *Op = CE->getOperand(0);
801 const Type *Ty = CE->getType();
802
803 // We can emit the pointer value into this slot if the slot is an
804 // integer slot greater or equal to the size of the pointer.
Nick Lewycky95353ad2008-08-08 06:34:07 +0000805 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000807
808 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000810 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
Chris Lattner89b36582008-08-17 07:19:36 +0000811
812 SmallString<40> S;
813 ptrMask.toStringUnsigned(S);
814 O << ") & " << S.c_str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 break;
816 }
817 case Instruction::Add:
818 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000819 case Instruction::And:
820 case Instruction::Or:
821 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000822 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000825 switch (Opcode) {
826 case Instruction::Add:
827 O << " + ";
828 break;
829 case Instruction::Sub:
830 O << " - ";
831 break;
832 case Instruction::And:
833 O << " & ";
834 break;
835 case Instruction::Or:
836 O << " | ";
837 break;
838 case Instruction::Xor:
839 O << " ^ ";
840 break;
841 default:
842 break;
843 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000844 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000846 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 break;
848 default:
849 assert(0 && "Unsupported operator!");
850 }
851 } else {
852 assert(0 && "Unknown constant value!");
853 }
854}
855
856/// printAsCString - Print the specified array as a C compatible string, only if
857/// the predicate isString is true.
858///
Owen Anderson847b99b2008-08-21 00:14:44 +0000859static void printAsCString(raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 unsigned LastElt) {
861 assert(CVA->isString() && "Array is not string compatible!");
862
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000863 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 for (unsigned i = 0; i != LastElt; ++i) {
865 unsigned char C =
866 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
867 printStringChar(O, C);
868 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000869 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870}
871
872/// EmitString - Emit a zero-byte-terminated string constant.
873///
874void AsmPrinter::EmitString(const ConstantArray *CVA) const {
875 unsigned NumElts = CVA->getNumOperands();
876 if (TAI->getAscizDirective() && NumElts &&
877 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
878 O << TAI->getAscizDirective();
879 printAsCString(O, CVA, NumElts-1);
880 } else {
881 O << TAI->getAsciiDirective();
882 printAsCString(O, CVA, NumElts);
883 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000884 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885}
886
887/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Duncan Sands4afc5752008-06-04 08:21:45 +0000888void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 const TargetData *TD = TM.getTargetData();
Duncan Sands4afc5752008-06-04 08:21:45 +0000890 unsigned Size = TD->getABITypeSize(CV->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891
892 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Duncan Sands8157ef42007-11-05 00:04:43 +0000893 EmitZeros(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000894 return;
895 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
896 if (CVA->isString()) {
897 EmitString(CVA);
898 } else { // Not a string. Print the values in successive locations
Duncan Sands8157ef42007-11-05 00:04:43 +0000899 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Duncan Sands4afc5752008-06-04 08:21:45 +0000900 EmitGlobalConstant(CVA->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 }
902 return;
903 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
904 // Print the fields in successive locations. Pad to align if needed!
905 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
906 uint64_t sizeSoFar = 0;
907 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
908 const Constant* field = CVS->getOperand(i);
909
910 // Check if padding is needed and insert one or more 0s.
Duncan Sands4afc5752008-06-04 08:21:45 +0000911 uint64_t fieldSize = TD->getABITypeSize(field->getType());
Duncan Sandsc15d58c2007-11-05 18:03:02 +0000912 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 - cvsLayout->getElementOffset(i)) - fieldSize;
914 sizeSoFar += fieldSize + padSize;
915
Duncan Sands4afc5752008-06-04 08:21:45 +0000916 // Now print the actual field value.
917 EmitGlobalConstant(field);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918
Duncan Sandsc15d58c2007-11-05 18:03:02 +0000919 // Insert padding - this may include padding to increase the size of the
920 // current field up to the ABI size (if the struct is not packed) as well
921 // as padding to ensure that the next field starts at the right offset.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 EmitZeros(padSize);
923 }
924 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
925 "Layout of constant struct may be incorrect!");
926 return;
927 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
928 // FP Constants are printed as integer constants to avoid losing
929 // precision...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000931 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000932 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 if (TAI->getData64bitsDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000934 O << TAI->getData64bitsDirective() << i << '\t'
935 << TAI->getCommentString() << " double value: " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936 else if (TD->isBigEndian()) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000937 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000938 << '\t' << TAI->getCommentString()
939 << " double most significant word " << Val << '\n';
Dale Johannesen1616e902007-09-11 18:32:33 +0000940 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000941 << '\t' << TAI->getCommentString()
942 << " double least significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +0000944 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000945 << '\t' << TAI->getCommentString()
946 << " double least significant word " << Val << '\n';
Dale Johannesen1616e902007-09-11 18:32:33 +0000947 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000948 << '\t' << TAI->getCommentString()
949 << " double most significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950 }
951 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000952 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000953 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
954 O << TAI->getData32bitsDirective()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000955 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000956 << '\t' << TAI->getCommentString() << " float " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000958 } else if (CFP->getType() == Type::X86_FP80Ty) {
959 // all long double variants are printed as hex
Dale Johannesen693aa822007-09-26 23:20:33 +0000960 // api needed to prevent premature destruction
961 APInt api = CFP->getValueAPF().convertToAPInt();
962 const uint64_t *p = api.getRawData();
Chris Lattner00d63062008-01-27 06:09:28 +0000963 APFloat DoubleVal = CFP->getValueAPF();
964 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000965 if (TD->isBigEndian()) {
966 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000967 << '\t' << TAI->getCommentString()
Chris Lattner00d63062008-01-27 06:09:28 +0000968 << " long double most significant halfword of ~"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000969 << DoubleVal.convertToDouble() << '\n';
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000970 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000971 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000972 << " long double next halfword\n";
973 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000974 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000975 << " long double next halfword\n";
976 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000977 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000978 << " long double next halfword\n";
979 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000980 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000981 << " long double least significant halfword\n";
982 } else {
983 O << TAI->getData16bitsDirective() << uint16_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000984 << '\t' << TAI->getCommentString()
Chris Lattner00d63062008-01-27 06:09:28 +0000985 << " long double least significant halfword of ~"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000986 << DoubleVal.convertToDouble() << '\n';
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000987 O << TAI->getData16bitsDirective() << uint16_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000988 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000989 << " long double next halfword\n";
990 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000991 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000992 << " long double next halfword\n";
993 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000994 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000995 << " long double next halfword\n";
996 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000997 << '\t' << TAI->getCommentString()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000998 << " long double most significant halfword\n";
999 }
Duncan Sands8157ef42007-11-05 00:04:43 +00001000 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty));
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001001 return;
Dale Johannesend3b6af32007-10-11 23:32:15 +00001002 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1003 // all long double variants are printed as hex
1004 // api needed to prevent premature destruction
1005 APInt api = CFP->getValueAPF().convertToAPInt();
1006 const uint64_t *p = api.getRawData();
1007 if (TD->isBigEndian()) {
1008 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001009 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001010 << " long double most significant word\n";
1011 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001012 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001013 << " long double next word\n";
1014 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001015 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001016 << " long double next word\n";
1017 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001018 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001019 << " long double least significant word\n";
1020 } else {
1021 O << TAI->getData32bitsDirective() << uint32_t(p[1])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001022 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001023 << " long double least significant word\n";
1024 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001025 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001026 << " long double next word\n";
1027 O << TAI->getData32bitsDirective() << uint32_t(p[0])
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001028 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001029 << " long double next word\n";
1030 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001031 << '\t' << TAI->getCommentString()
Dale Johannesend3b6af32007-10-11 23:32:15 +00001032 << " long double most significant word\n";
1033 }
1034 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00001035 } else assert(0 && "Floating point constant type not handled");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 } else if (CV->getType() == Type::Int64Ty) {
1037 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1038 uint64_t Val = CI->getZExtValue();
1039
1040 if (TAI->getData64bitsDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001041 O << TAI->getData64bitsDirective() << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 else if (TD->isBigEndian()) {
1043 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001044 << '\t' << TAI->getCommentString()
1045 << " Double-word most significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001047 << '\t' << TAI->getCommentString()
1048 << " Double-word least significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 } else {
1050 O << TAI->getData32bitsDirective() << unsigned(Val)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001051 << '\t' << TAI->getCommentString()
1052 << " Double-word least significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001054 << '\t' << TAI->getCommentString()
1055 << " Double-word most significant word " << Val << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 }
1057 return;
1058 }
1059 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1060 const VectorType *PTy = CP->getType();
1061
1062 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Duncan Sands4afc5752008-06-04 08:21:45 +00001063 EmitGlobalConstant(CP->getOperand(I));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064
1065 return;
1066 }
1067
1068 const Type *type = CV->getType();
1069 printDataDirective(type);
1070 EmitConstantValueOnly(CV);
Scott Michele067c3c2008-06-03 15:39:51 +00001071 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner89b36582008-08-17 07:19:36 +00001072 SmallString<40> S;
1073 CI->getValue().toStringUnsigned(S, 16);
1074 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
Scott Michele067c3c2008-06-03 15:39:51 +00001075 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001076 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077}
1078
Chris Lattner89b36582008-08-17 07:19:36 +00001079void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080 // Target doesn't support this yet!
1081 abort();
1082}
1083
1084/// PrintSpecial - Print information related to the specified machine instr
1085/// that is independent of the operand, and may be independent of the instr
1086/// itself. This can be useful for portably encoding the comment character
1087/// or other bits of target-specific knowledge into the asmstrings. The
1088/// syntax used is ${:comment}. Targets can override this to add support
1089/// for their own strange codes.
1090void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
1091 if (!strcmp(Code, "private")) {
1092 O << TAI->getPrivateGlobalPrefix();
1093 } else if (!strcmp(Code, "comment")) {
1094 O << TAI->getCommentString();
1095 } else if (!strcmp(Code, "uid")) {
1096 // Assign a unique ID to this machine instruction.
1097 static const MachineInstr *LastMI = 0;
1098 static const Function *F = 0;
1099 static unsigned Counter = 0U-1;
1100
1101 // Comparing the address of MI isn't sufficient, because machineinstrs may
1102 // be allocated to the same address across functions.
1103 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1104
1105 // If this is a new machine instruction, bump the counter.
1106 if (LastMI != MI || F != ThisF) {
1107 ++Counter;
1108 LastMI = MI;
1109 F = ThisF;
1110 }
1111 O << Counter;
1112 } else {
1113 cerr << "Unknown special formatter '" << Code
1114 << "' for machine instr: " << *MI;
1115 exit(1);
1116 }
1117}
1118
1119
1120/// printInlineAsm - This method formats and prints the specified machine
1121/// instruction that is an inline asm.
1122void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1123 unsigned NumOperands = MI->getNumOperands();
1124
1125 // Count the number of register definitions.
1126 unsigned NumDefs = 0;
Dan Gohman38a9a9f2007-09-14 20:33:02 +00001127 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 ++NumDefs)
1129 assert(NumDefs != NumOperands-1 && "No asm string?");
1130
1131 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
1132
1133 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1134 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1135
Dale Johannesene99fc902008-01-29 02:21:21 +00001136 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1137 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138 if (AsmStr[0] == 0) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001139 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 return;
1141 }
1142
1143 O << TAI->getInlineAsmStart() << "\n\t";
1144
1145 // The variant of the current asmprinter.
1146 int AsmPrinterVariant = TAI->getAssemblerDialect();
1147
1148 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1149 const char *LastEmitted = AsmStr; // One past the last character emitted.
1150
1151 while (*LastEmitted) {
1152 switch (*LastEmitted) {
1153 default: {
1154 // Not a special case, emit the string section literally.
1155 const char *LiteralEnd = LastEmitted+1;
1156 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1157 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1158 ++LiteralEnd;
1159 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1160 O.write(LastEmitted, LiteralEnd-LastEmitted);
1161 LastEmitted = LiteralEnd;
1162 break;
1163 }
1164 case '\n':
1165 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001166 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001167 break;
1168 case '$': {
1169 ++LastEmitted; // Consume '$' character.
1170 bool Done = true;
1171
1172 // Handle escapes.
1173 switch (*LastEmitted) {
1174 default: Done = false; break;
1175 case '$': // $$ -> $
1176 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1177 O << '$';
1178 ++LastEmitted; // Consume second '$' character.
1179 break;
1180 case '(': // $( -> same as GCC's { character.
1181 ++LastEmitted; // Consume '(' character.
1182 if (CurVariant != -1) {
1183 cerr << "Nested variants found in inline asm string: '"
1184 << AsmStr << "'\n";
1185 exit(1);
1186 }
1187 CurVariant = 0; // We're in the first variant now.
1188 break;
1189 case '|':
1190 ++LastEmitted; // consume '|' character.
1191 if (CurVariant == -1) {
1192 cerr << "Found '|' character outside of variant in inline asm "
1193 << "string: '" << AsmStr << "'\n";
1194 exit(1);
1195 }
1196 ++CurVariant; // We're in the next variant.
1197 break;
1198 case ')': // $) -> same as GCC's } char.
1199 ++LastEmitted; // consume ')' character.
1200 if (CurVariant == -1) {
1201 cerr << "Found '}' character outside of variant in inline asm "
1202 << "string: '" << AsmStr << "'\n";
1203 exit(1);
1204 }
1205 CurVariant = -1;
1206 break;
1207 }
1208 if (Done) break;
1209
1210 bool HasCurlyBraces = false;
1211 if (*LastEmitted == '{') { // ${variable}
1212 ++LastEmitted; // Consume '{' character.
1213 HasCurlyBraces = true;
1214 }
1215
1216 const char *IDStart = LastEmitted;
1217 char *IDEnd;
1218 errno = 0;
1219 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1220 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
1221 cerr << "Bad $ operand number in inline asm string: '"
1222 << AsmStr << "'\n";
1223 exit(1);
1224 }
1225 LastEmitted = IDEnd;
1226
1227 char Modifier[2] = { 0, 0 };
1228
1229 if (HasCurlyBraces) {
1230 // If we have curly braces, check for a modifier character. This
1231 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1232 if (*LastEmitted == ':') {
1233 ++LastEmitted; // Consume ':' character.
1234 if (*LastEmitted == 0) {
1235 cerr << "Bad ${:} expression in inline asm string: '"
1236 << AsmStr << "'\n";
1237 exit(1);
1238 }
1239
1240 Modifier[0] = *LastEmitted;
1241 ++LastEmitted; // Consume modifier character.
1242 }
1243
1244 if (*LastEmitted != '}') {
1245 cerr << "Bad ${} expression in inline asm string: '"
1246 << AsmStr << "'\n";
1247 exit(1);
1248 }
1249 ++LastEmitted; // Consume '}' character.
1250 }
1251
1252 if ((unsigned)Val >= NumOperands-1) {
1253 cerr << "Invalid $ operand number in inline asm string: '"
1254 << AsmStr << "'\n";
1255 exit(1);
1256 }
1257
1258 // Okay, we finally have a value number. Ask the target to print this
1259 // operand!
1260 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1261 unsigned OpNo = 1;
1262
1263 bool Error = false;
1264
1265 // Scan to find the machine operand number for the operand.
1266 for (; Val; --Val) {
1267 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001268 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 OpNo += (OpFlags >> 3) + 1;
1270 }
1271
1272 if (OpNo >= MI->getNumOperands()) {
1273 Error = true;
1274 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001275 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 ++OpNo; // Skip over the ID number.
1277
Dale Johannesencfb19e62007-11-05 21:20:28 +00001278 if (Modifier[0]=='l') // labels are target independent
Chris Lattner6017d482007-12-30 23:10:15 +00001279 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Cheng45c1edb2008-02-28 00:43:03 +00001280 false, false, false);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001281 else {
1282 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1283 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1284 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1285 Modifier[0] ? Modifier : 0);
1286 } else {
1287 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1288 Modifier[0] ? Modifier : 0);
1289 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 }
1291 }
1292 if (Error) {
1293 cerr << "Invalid operand found in inline asm: '"
1294 << AsmStr << "'\n";
1295 MI->dump();
1296 exit(1);
1297 }
1298 }
1299 break;
1300 }
1301 }
1302 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001303 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304}
1305
Evan Cheng3c0eda52008-03-15 00:03:38 +00001306/// printImplicitDef - This method prints the specified machine instruction
1307/// that is an implicit def.
1308void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001309 O << '\t' << TAI->getCommentString() << " implicit-def: "
1310 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng3c0eda52008-03-15 00:03:38 +00001311}
1312
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313/// printLabel - This method prints a local label used by debug and
1314/// exception handling tables.
1315void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001316 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317}
1318
Evan Chenga53c40a2008-02-01 09:10:45 +00001319void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng8b988692008-02-02 08:39:46 +00001320 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chenga53c40a2008-02-01 09:10:45 +00001321}
1322
Evan Cheng2e28d622008-02-02 04:07:54 +00001323/// printDeclare - This method prints a local variable declaration used by
1324/// debug tables.
Evan Chengc439a852008-02-04 23:06:48 +00001325/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1326/// entry into dwarf table.
Evan Cheng2e28d622008-02-02 04:07:54 +00001327void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Evan Chengc439a852008-02-04 23:06:48 +00001328 int FI = MI->getOperand(0).getIndex();
1329 GlobalValue *GV = MI->getOperand(1).getGlobal();
1330 MMI->RecordVariable(GV, FI);
Evan Cheng2e28d622008-02-02 04:07:54 +00001331}
1332
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1334/// instruction, using the specified assembler variant. Targets should
1335/// overried this to format as appropriate.
1336bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1337 unsigned AsmVariant, const char *ExtraCode) {
1338 // Target doesn't support this yet!
1339 return true;
1340}
1341
1342bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1343 unsigned AsmVariant,
1344 const char *ExtraCode) {
1345 // Target doesn't support this yet!
1346 return true;
1347}
1348
1349/// printBasicBlockLabel - This method prints the label for the specified
1350/// MachineBasicBlock
1351void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Cheng45c1edb2008-02-28 00:43:03 +00001352 bool printAlign,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 bool printColon,
1354 bool printComment) const {
Evan Cheng45c1edb2008-02-28 00:43:03 +00001355 if (printAlign) {
1356 unsigned Align = MBB->getAlignment();
1357 if (Align)
1358 EmitAlignment(Log2_32(Align));
1359 }
1360
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001361 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng477013c2007-10-14 05:57:21 +00001362 << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 if (printColon)
1364 O << ':';
1365 if (printComment && MBB->getBasicBlock())
Dan Gohman0912cda2007-07-30 15:06:25 +00001366 O << '\t' << TAI->getCommentString() << ' '
Evan Cheng76443dc2008-07-08 00:55:58 +00001367 << MBB->getBasicBlock()->getNameStart();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368}
1369
Evan Cheng6fb06762007-11-09 01:32:10 +00001370/// printPICJumpTableSetLabel - This method prints a set label for the
1371/// specified MachineBasicBlock for a jumptable entry.
1372void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1373 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374 if (!TAI->getSetDirective())
1375 return;
1376
1377 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001378 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001379 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001380 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1381 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382}
1383
Evan Cheng6fb06762007-11-09 01:32:10 +00001384void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1385 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 if (!TAI->getSetDirective())
1387 return;
1388
1389 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001390 << getFunctionNumber() << '_' << uid << '_' << uid2
1391 << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001392 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001393 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1394 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395}
1396
1397/// printDataDirective - This method prints the asm directive for the
1398/// specified type.
1399void AsmPrinter::printDataDirective(const Type *type) {
1400 const TargetData *TD = TM.getTargetData();
1401 switch (type->getTypeID()) {
1402 case Type::IntegerTyID: {
1403 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1404 if (BitWidth <= 8)
1405 O << TAI->getData8bitsDirective();
1406 else if (BitWidth <= 16)
1407 O << TAI->getData16bitsDirective();
1408 else if (BitWidth <= 32)
1409 O << TAI->getData32bitsDirective();
1410 else if (BitWidth <= 64) {
1411 assert(TAI->getData64bitsDirective() &&
1412 "Target cannot handle 64-bit constant exprs!");
1413 O << TAI->getData64bitsDirective();
1414 }
1415 break;
1416 }
1417 case Type::PointerTyID:
1418 if (TD->getPointerSize() == 8) {
1419 assert(TAI->getData64bitsDirective() &&
1420 "Target cannot handle 64-bit pointer exprs!");
1421 O << TAI->getData64bitsDirective();
1422 } else {
1423 O << TAI->getData32bitsDirective();
1424 }
1425 break;
1426 case Type::FloatTyID: case Type::DoubleTyID:
Dale Johannesen3b5303b2007-09-28 18:06:58 +00001427 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001428 assert (0 && "Should have already output floating point constant.");
1429 default:
1430 assert (0 && "Can't handle printing this type of thing");
1431 break;
1432 }
1433}
1434
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001435void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
1436 const char *Prefix) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001437 if (Name[0]=='\"')
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001438 O << '\"';
1439 O << TAI->getPrivateGlobalPrefix();
1440 if (Prefix) O << Prefix;
1441 if (Name[0]=='\"')
1442 O << '\"';
1443 if (Name[0]=='\"')
1444 O << Name[1];
Dale Johannesena21b5202008-05-19 21:38:18 +00001445 else
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001446 O << Name;
1447 O << Suffix;
1448 if (Name[0]=='\"')
1449 O << '\"';
Dale Johannesena21b5202008-05-19 21:38:18 +00001450}
Evan Cheng76443dc2008-07-08 00:55:58 +00001451
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001452void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
Evan Cheng76443dc2008-07-08 00:55:58 +00001453 printSuffixedName(Name.c_str(), Suffix);
1454}
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001455
1456void AsmPrinter::printVisibility(const std::string& Name,
1457 unsigned Visibility) const {
1458 if (Visibility == GlobalValue::HiddenVisibility) {
1459 if (const char *Directive = TAI->getHiddenDirective())
1460 O << Directive << Name << '\n';
1461 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1462 if (const char *Directive = TAI->getProtectedDirective())
1463 O << Directive << Name << '\n';
1464 }
1465}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001466
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001467GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1468 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001469 return 0;
1470
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001471 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001472 if (GCPI != GCMetadataPrinters.end())
1473 return GCPI->second;
1474
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001475 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001476
1477 for (GCMetadataPrinterRegistry::iterator
1478 I = GCMetadataPrinterRegistry::begin(),
1479 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1480 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001481 GCMetadataPrinter *GMP = I->instantiate();
1482 GMP->S = S;
1483 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1484 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001485 }
1486
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001487 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001488 abort();
1489}