blob: 6622bcdee669e8ef605777b770713ce7b32e6633 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
19#include "llvm/CodeGen/MachineConstantPool.h"
20#include "llvm/CodeGen/MachineJumpTableInfo.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Mangler.h"
23#include "llvm/Support/MathExtras.h"
24#include "llvm/Support/Streams.h"
25#include "llvm/Target/TargetAsmInfo.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Target/TargetMachine.h"
29#include <cerrno>
30using namespace llvm;
31
32static cl::opt<bool>
33AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
34
35char AsmPrinter::ID = 0;
36AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
37 const TargetAsmInfo *T)
38 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
39{}
40
41std::string AsmPrinter::getSectionForFunction(const Function &F) const {
42 return TAI->getTextSection();
43}
44
45
46/// SwitchToTextSection - Switch to the specified text section of the executable
47/// if we are not already in it!
48///
49void AsmPrinter::SwitchToTextSection(const char *NewSection,
50 const GlobalValue *GV) {
51 std::string NS;
52 if (GV && GV->hasSection())
53 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
54 else
55 NS = NewSection;
56
57 // If we're already in this section, we're done.
58 if (CurrentSection == NS) return;
59
60 // Close the current section, if applicable.
61 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
62 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
63
64 CurrentSection = NS;
65
66 if (!CurrentSection.empty())
67 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
68}
69
70/// SwitchToDataSection - Switch to the specified data section of the executable
71/// if we are not already in it!
72///
73void AsmPrinter::SwitchToDataSection(const char *NewSection,
74 const GlobalValue *GV) {
75 std::string NS;
76 if (GV && GV->hasSection())
77 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
78 else
79 NS = NewSection;
80
81 // If we're already in this section, we're done.
82 if (CurrentSection == NS) return;
83
84 // Close the current section, if applicable.
85 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
86 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
87
88 CurrentSection = NS;
89
90 if (!CurrentSection.empty())
91 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
92}
93
94
95bool AsmPrinter::doInitialization(Module &M) {
96 Mang = new Mangler(M, TAI->getGlobalPrefix());
97
98 if (!M.getModuleInlineAsm().empty())
99 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
100 << M.getModuleInlineAsm()
101 << "\n" << TAI->getCommentString()
102 << " End of file scope inline assembly\n";
103
104 SwitchToDataSection(""); // Reset back to no section.
105
106 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
107 MMI->AnalyzeModule(M);
108 }
109
110 return false;
111}
112
113bool AsmPrinter::doFinalization(Module &M) {
114 if (TAI->getWeakRefDirective()) {
115 if (!ExtWeakSymbols.empty())
116 SwitchToDataSection("");
117
118 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
119 e = ExtWeakSymbols.end(); i != e; ++i) {
120 const GlobalValue *GV = *i;
121 std::string Name = Mang->getValueName(GV);
122 O << TAI->getWeakRefDirective() << Name << "\n";
123 }
124 }
125
126 if (TAI->getSetDirective()) {
127 if (!M.alias_empty())
128 SwitchToTextSection(TAI->getTextSection());
129
130 O << "\n";
131 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
132 I!=E; ++I) {
133 std::string Name = Mang->getValueName(I);
134 std::string Target;
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000135
136 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
137 Target = Mang->getValueName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000139 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 O << "\t.globl\t" << Name << "\n";
141 else if (I->hasWeakLinkage())
142 O << TAI->getWeakRefDirective() << Name << "\n";
143 else if (!I->hasInternalLinkage())
144 assert(0 && "Invalid alias linkage");
145
146 O << TAI->getSetDirective() << Name << ", " << Target << "\n";
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000147
148 // If the aliasee has external weak linkage it can be referenced only by
149 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit
150 // weak reference in such case.
151 if (GV->hasExternalWeakLinkage())
152 if (TAI->getWeakRefDirective())
153 O << TAI->getWeakRefDirective() << Target << "\n";
154 else
155 O << "\t.globl\t" << Target << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 }
157 }
158
159 delete Mang; Mang = 0;
160 return false;
161}
162
Bill Wendlinge9ecdcf2007-09-18 09:10:16 +0000163std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
Bill Wendlingef9211a2007-09-18 01:47:22 +0000164 assert(MF && "No machine function?");
Bill Wendlingd2144aa2007-09-18 05:03:44 +0000165 return Mang->makeNameProper(MF->getFunction()->getName() + ".eh",
166 TAI->getGlobalPrefix());
Bill Wendlingef9211a2007-09-18 01:47:22 +0000167}
168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
170 // What's my mangled name?
171 CurrentFnName = Mang->getValueName(MF.getFunction());
172 IncrementFunctionNumber();
173}
174
175/// EmitConstantPool - Print to the current output stream assembly
176/// representations of the constants in the constant pool MCP. This is
177/// used to print out constants which have been "spilled to memory" by
178/// the code generator.
179///
180void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
181 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
182 if (CP.empty()) return;
183
184 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
185 // in special sections.
186 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
187 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
188 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
189 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
190 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
191 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
192 MachineConstantPoolEntry CPE = CP[i];
193 const Type *Ty = CPE.getType();
194 if (TAI->getFourByteConstantSection() &&
195 TM.getTargetData()->getTypeSize(Ty) == 4)
196 FourByteCPs.push_back(std::make_pair(CPE, i));
197 else if (TAI->getEightByteConstantSection() &&
198 TM.getTargetData()->getTypeSize(Ty) == 8)
199 EightByteCPs.push_back(std::make_pair(CPE, i));
200 else if (TAI->getSixteenByteConstantSection() &&
201 TM.getTargetData()->getTypeSize(Ty) == 16)
202 SixteenByteCPs.push_back(std::make_pair(CPE, i));
203 else
204 OtherCPs.push_back(std::make_pair(CPE, i));
205 }
206
207 unsigned Alignment = MCP->getConstantPoolAlignment();
208 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
209 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
210 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
211 SixteenByteCPs);
212 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
213}
214
215void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
216 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
217 if (CP.empty()) return;
218
219 SwitchToDataSection(Section);
220 EmitAlignment(Alignment);
221 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
222 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
223 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
224 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
225 if (CP[i].first.isMachineConstantPoolEntry())
226 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
227 else
228 EmitGlobalConstant(CP[i].first.Val.ConstVal);
229 if (i != e-1) {
230 const Type *Ty = CP[i].first.getType();
231 unsigned EntSize =
232 TM.getTargetData()->getTypeSize(Ty);
233 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
234 // Emit inter-object padding for alignment.
235 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
236 }
237 }
238}
239
240/// EmitJumpTableInfo - Print assembly representations of the jump tables used
241/// by the current function to the current output stream.
242///
243void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
244 MachineFunction &MF) {
245 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
246 if (JT.empty()) return;
247 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
248
249 // Use JumpTableDirective otherwise honor the entry size from the jump table
250 // info.
251 const char *JTEntryDirective = TAI->getJumpTableDirective();
252 bool HadJTEntryDirective = JTEntryDirective != NULL;
253 if (!HadJTEntryDirective) {
254 JTEntryDirective = MJTI->getEntrySize() == 4 ?
255 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
256 }
257
258 // Pick the directive to use to print the jump table entries, and switch to
259 // the appropriate section.
260 TargetLowering *LoweringInfo = TM.getTargetLowering();
261
262 const char* JumpTableDataSection = TAI->getJumpTableDataSection();
263 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
264 !JumpTableDataSection) {
265 // In PIC mode, we need to emit the jump table to the same section as the
266 // function body itself, otherwise the label differences won't make sense.
267 // We should also do if the section name is NULL.
268 const Function *F = MF.getFunction();
269 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
270 } else {
271 SwitchToDataSection(JumpTableDataSection);
272 }
273
274 EmitAlignment(Log2_32(MJTI->getAlignment()));
275
276 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
277 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
278
279 // If this jump table was deleted, ignore it.
280 if (JTBBs.empty()) continue;
281
282 // For PIC codegen, if possible we want to use the SetDirective to reduce
283 // the number of relocations the assembler will generate for the jump table.
284 // Set directives are all printed before the jump table itself.
285 std::set<MachineBasicBlock*> EmittedSets;
286 if (TAI->getSetDirective() && IsPic)
287 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
288 if (EmittedSets.insert(JTBBs[ii]).second)
289 printSetLabel(i, JTBBs[ii]);
290
291 // On some targets (e.g. darwin) we want to emit two consequtive labels
292 // before each jump table. The first label is never referenced, but tells
293 // the assembler and linker the extents of the jump table object. The
294 // second label is actually referenced by the code.
295 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
296 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
297
298 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
299 << '_' << i << ":\n";
300
301 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
302 O << JTEntryDirective << ' ';
303 // If we have emitted set directives for the jump table entries, print
304 // them rather than the entries themselves. If we're emitting PIC, then
305 // emit the table entries as differences between two text section labels.
306 // If we're emitting non-PIC code, then emit the entries as direct
307 // references to the target basic blocks.
308 if (!EmittedSets.empty()) {
309 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
310 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
311 } else if (IsPic) {
312 printBasicBlockLabel(JTBBs[ii], false, false);
313 // If the arch uses custom Jump Table directives, don't calc relative to
314 // JT
315 if (!HadJTEntryDirective)
316 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
317 << getFunctionNumber() << '_' << i;
318 } else {
319 printBasicBlockLabel(JTBBs[ii], false, false);
320 }
321 O << '\n';
322 }
323 }
324}
325
326/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
327/// special global used by LLVM. If so, emit it and return true, otherwise
328/// do nothing and return false.
329bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000330 if (GV->getName() == "llvm.used") {
331 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
332 EmitLLVMUsedList(GV->getInitializer());
333 return true;
334 }
335
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 // Ignore debug and non-emitted data.
337 if (GV->getSection() == "llvm.metadata") return true;
338
339 if (!GV->hasAppendingLinkage()) return false;
340
341 assert(GV->hasInitializer() && "Not a special LLVM global!");
342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 const TargetData *TD = TM.getTargetData();
344 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
345 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
346 SwitchToDataSection(TAI->getStaticCtorsSection());
347 EmitAlignment(Align, 0);
348 EmitXXStructorList(GV->getInitializer());
349 return true;
350 }
351
352 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
353 SwitchToDataSection(TAI->getStaticDtorsSection());
354 EmitAlignment(Align, 0);
355 EmitXXStructorList(GV->getInitializer());
356 return true;
357 }
358
359 return false;
360}
361
362/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
363/// global in the specified llvm.used list as being used with this directive.
364void AsmPrinter::EmitLLVMUsedList(Constant *List) {
365 const char *Directive = TAI->getUsedDirective();
366
367 // Should be an array of 'sbyte*'.
368 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
369 if (InitList == 0) return;
370
371 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
372 O << Directive;
373 EmitConstantValueOnly(InitList->getOperand(i));
374 O << "\n";
375 }
376}
377
378/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
379/// function pointers, ignoring the init priority.
380void AsmPrinter::EmitXXStructorList(Constant *List) {
381 // Should be an array of '{ int, void ()* }' structs. The first value is the
382 // init priority, which we ignore.
383 if (!isa<ConstantArray>(List)) return;
384 ConstantArray *InitList = cast<ConstantArray>(List);
385 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
386 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
387 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
388
389 if (CS->getOperand(1)->isNullValue())
390 return; // Found a null terminator, exit printing.
391 // Emit the function pointer.
392 EmitGlobalConstant(CS->getOperand(1));
393 }
394}
395
396/// getGlobalLinkName - Returns the asm/link name of of the specified
397/// global variable. Should be overridden by each target asm printer to
398/// generate the appropriate value.
399const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
400 std::string LinkName;
401
402 if (isa<Function>(GV)) {
403 LinkName += TAI->getFunctionAddrPrefix();
404 LinkName += Mang->getValueName(GV);
405 LinkName += TAI->getFunctionAddrSuffix();
406 } else {
407 LinkName += TAI->getGlobalVarAddrPrefix();
408 LinkName += Mang->getValueName(GV);
409 LinkName += TAI->getGlobalVarAddrSuffix();
410 }
411
412 return LinkName;
413}
414
415/// EmitExternalGlobal - Emit the external reference to a global variable.
416/// Should be overridden if an indirect reference should be used.
417void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
418 O << getGlobalLinkName(GV);
419}
420
421
422
423//===----------------------------------------------------------------------===//
424/// LEB 128 number encoding.
425
426/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
427/// representing an unsigned leb128 value.
428void AsmPrinter::PrintULEB128(unsigned Value) const {
429 do {
430 unsigned Byte = Value & 0x7f;
431 Value >>= 7;
432 if (Value) Byte |= 0x80;
433 O << "0x" << std::hex << Byte << std::dec;
434 if (Value) O << ", ";
435 } while (Value);
436}
437
438/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
439/// value.
440unsigned AsmPrinter::SizeULEB128(unsigned Value) {
441 unsigned Size = 0;
442 do {
443 Value >>= 7;
444 Size += sizeof(int8_t);
445 } while (Value);
446 return Size;
447}
448
449/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
450/// representing a signed leb128 value.
451void AsmPrinter::PrintSLEB128(int Value) const {
452 int Sign = Value >> (8 * sizeof(Value) - 1);
453 bool IsMore;
454
455 do {
456 unsigned Byte = Value & 0x7f;
457 Value >>= 7;
458 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
459 if (IsMore) Byte |= 0x80;
460 O << "0x" << std::hex << Byte << std::dec;
461 if (IsMore) O << ", ";
462 } while (IsMore);
463}
464
465/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
466/// value.
467unsigned AsmPrinter::SizeSLEB128(int Value) {
468 unsigned Size = 0;
469 int Sign = Value >> (8 * sizeof(Value) - 1);
470 bool IsMore;
471
472 do {
473 unsigned Byte = Value & 0x7f;
474 Value >>= 7;
475 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
476 Size += sizeof(int8_t);
477 } while (IsMore);
478 return Size;
479}
480
481//===--------------------------------------------------------------------===//
482// Emission and print routines
483//
484
485/// PrintHex - Print a value as a hexidecimal value.
486///
487void AsmPrinter::PrintHex(int Value) const {
488 O << "0x" << std::hex << Value << std::dec;
489}
490
491/// EOL - Print a newline character to asm stream. If a comment is present
492/// then it will be printed first. Comments should not contain '\n'.
493void AsmPrinter::EOL() const {
494 O << "\n";
495}
496void AsmPrinter::EOL(const std::string &Comment) const {
497 if (AsmVerbose && !Comment.empty()) {
498 O << "\t"
499 << TAI->getCommentString()
500 << " "
501 << Comment;
502 }
503 O << "\n";
504}
505
506/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
507/// unsigned leb128 value.
508void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
509 if (TAI->hasLEB128()) {
510 O << "\t.uleb128\t"
511 << Value;
512 } else {
513 O << TAI->getData8bitsDirective();
514 PrintULEB128(Value);
515 }
516}
517
518/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
519/// signed leb128 value.
520void AsmPrinter::EmitSLEB128Bytes(int Value) const {
521 if (TAI->hasLEB128()) {
522 O << "\t.sleb128\t"
523 << Value;
524 } else {
525 O << TAI->getData8bitsDirective();
526 PrintSLEB128(Value);
527 }
528}
529
530/// EmitInt8 - Emit a byte directive and value.
531///
532void AsmPrinter::EmitInt8(int Value) const {
533 O << TAI->getData8bitsDirective();
534 PrintHex(Value & 0xFF);
535}
536
537/// EmitInt16 - Emit a short directive and value.
538///
539void AsmPrinter::EmitInt16(int Value) const {
540 O << TAI->getData16bitsDirective();
541 PrintHex(Value & 0xFFFF);
542}
543
544/// EmitInt32 - Emit a long directive and value.
545///
546void AsmPrinter::EmitInt32(int Value) const {
547 O << TAI->getData32bitsDirective();
548 PrintHex(Value);
549}
550
551/// EmitInt64 - Emit a long long directive and value.
552///
553void AsmPrinter::EmitInt64(uint64_t Value) const {
554 if (TAI->getData64bitsDirective()) {
555 O << TAI->getData64bitsDirective();
556 PrintHex(Value);
557 } else {
558 if (TM.getTargetData()->isBigEndian()) {
559 EmitInt32(unsigned(Value >> 32)); O << "\n";
560 EmitInt32(unsigned(Value));
561 } else {
562 EmitInt32(unsigned(Value)); O << "\n";
563 EmitInt32(unsigned(Value >> 32));
564 }
565 }
566}
567
568/// toOctal - Convert the low order bits of X into an octal digit.
569///
570static inline char toOctal(int X) {
571 return (X&7)+'0';
572}
573
574/// printStringChar - Print a char, escaped if necessary.
575///
576static void printStringChar(std::ostream &O, unsigned char C) {
577 if (C == '"') {
578 O << "\\\"";
579 } else if (C == '\\') {
580 O << "\\\\";
581 } else if (isprint(C)) {
582 O << C;
583 } else {
584 switch(C) {
585 case '\b': O << "\\b"; break;
586 case '\f': O << "\\f"; break;
587 case '\n': O << "\\n"; break;
588 case '\r': O << "\\r"; break;
589 case '\t': O << "\\t"; break;
590 default:
591 O << '\\';
592 O << toOctal(C >> 6);
593 O << toOctal(C >> 3);
594 O << toOctal(C >> 0);
595 break;
596 }
597 }
598}
599
600/// EmitString - Emit a string with quotes and a null terminator.
601/// Special characters are emitted properly.
602/// \literal (Eg. '\t') \endliteral
603void AsmPrinter::EmitString(const std::string &String) const {
604 const char* AscizDirective = TAI->getAscizDirective();
605 if (AscizDirective)
606 O << AscizDirective;
607 else
608 O << TAI->getAsciiDirective();
609 O << "\"";
610 for (unsigned i = 0, N = String.size(); i < N; ++i) {
611 unsigned char C = String[i];
612 printStringChar(O, C);
613 }
614 if (AscizDirective)
615 O << "\"";
616 else
617 O << "\\0\"";
618}
619
620
621//===----------------------------------------------------------------------===//
622
623// EmitAlignment - Emit an alignment directive to the specified power of
624// two boundary. For example, if you pass in 3 here, you will get an 8
625// byte alignment. If a global value is specified, and if that global has
626// an explicit alignment requested, it will unconditionally override the
627// alignment request. However, if ForcedAlignBits is specified, this value
628// has final say: the ultimate alignment will be the max of ForcedAlignBits
629// and the alignment computed with NumBits and the global.
630//
631// The algorithm is:
632// Align = NumBits;
633// if (GV && GV->hasalignment) Align = GV->getalignment();
634// Align = std::max(Align, ForcedAlignBits);
635//
636void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Chengc1f41aa2007-07-25 23:35:07 +0000637 unsigned ForcedAlignBits, bool UseFillExpr,
638 unsigned FillValue) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 if (GV && GV->getAlignment())
640 NumBits = Log2_32(GV->getAlignment());
641 NumBits = std::max(NumBits, ForcedAlignBits);
642
643 if (NumBits == 0) return; // No need to emit alignment.
644 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Chengc1f41aa2007-07-25 23:35:07 +0000645 O << TAI->getAlignDirective() << NumBits;
646 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
647 O << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648}
649
650
651/// EmitZeros - Emit a block of zeros.
652///
653void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
654 if (NumZeros) {
655 if (TAI->getZeroDirective()) {
656 O << TAI->getZeroDirective() << NumZeros;
657 if (TAI->getZeroDirectiveSuffix())
658 O << TAI->getZeroDirectiveSuffix();
659 O << "\n";
660 } else {
661 for (; NumZeros; --NumZeros)
662 O << TAI->getData8bitsDirective() << "0\n";
663 }
664 }
665}
666
667// Print out the specified constant, without a storage class. Only the
668// constants valid in constant expressions can occur here.
669void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
670 if (CV->isNullValue() || isa<UndefValue>(CV))
671 O << "0";
672 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
673 O << CI->getZExtValue();
674 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
675 // This is a constant address for a global variable or function. Use the
676 // name of the variable or function as the address value, possibly
677 // decorating it with GlobalVarAddrPrefix/Suffix or
678 // FunctionAddrPrefix/Suffix (these all default to "" )
679 if (isa<Function>(GV)) {
680 O << TAI->getFunctionAddrPrefix()
681 << Mang->getValueName(GV)
682 << TAI->getFunctionAddrSuffix();
683 } else {
684 O << TAI->getGlobalVarAddrPrefix()
685 << Mang->getValueName(GV)
686 << TAI->getGlobalVarAddrSuffix();
687 }
688 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
689 const TargetData *TD = TM.getTargetData();
690 unsigned Opcode = CE->getOpcode();
691 switch (Opcode) {
692 case Instruction::GetElementPtr: {
693 // generate a symbolic expression for the byte address
694 const Constant *ptrVal = CE->getOperand(0);
695 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
696 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
697 idxVec.size())) {
698 if (Offset)
699 O << "(";
700 EmitConstantValueOnly(ptrVal);
701 if (Offset > 0)
702 O << ") + " << Offset;
703 else if (Offset < 0)
704 O << ") - " << -Offset;
705 } else {
706 EmitConstantValueOnly(ptrVal);
707 }
708 break;
709 }
710 case Instruction::Trunc:
711 case Instruction::ZExt:
712 case Instruction::SExt:
713 case Instruction::FPTrunc:
714 case Instruction::FPExt:
715 case Instruction::UIToFP:
716 case Instruction::SIToFP:
717 case Instruction::FPToUI:
718 case Instruction::FPToSI:
719 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
720 break;
721 case Instruction::BitCast:
722 return EmitConstantValueOnly(CE->getOperand(0));
723
724 case Instruction::IntToPtr: {
725 // Handle casts to pointers by changing them into casts to the appropriate
726 // integer type. This promotes constant folding and simplifies this code.
727 Constant *Op = CE->getOperand(0);
728 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
729 return EmitConstantValueOnly(Op);
730 }
731
732
733 case Instruction::PtrToInt: {
734 // Support only foldable casts to/from pointers that can be eliminated by
735 // changing the pointer to the appropriately sized integer type.
736 Constant *Op = CE->getOperand(0);
737 const Type *Ty = CE->getType();
738
739 // We can emit the pointer value into this slot if the slot is an
740 // integer slot greater or equal to the size of the pointer.
741 if (Ty->isInteger() &&
742 TD->getTypeSize(Ty) >= TD->getTypeSize(Op->getType()))
743 return EmitConstantValueOnly(Op);
744
745 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
746 EmitConstantValueOnly(Op);
747 break;
748 }
749 case Instruction::Add:
750 case Instruction::Sub:
751 O << "(";
752 EmitConstantValueOnly(CE->getOperand(0));
753 O << (Opcode==Instruction::Add ? ") + (" : ") - (");
754 EmitConstantValueOnly(CE->getOperand(1));
755 O << ")";
756 break;
757 default:
758 assert(0 && "Unsupported operator!");
759 }
760 } else {
761 assert(0 && "Unknown constant value!");
762 }
763}
764
765/// printAsCString - Print the specified array as a C compatible string, only if
766/// the predicate isString is true.
767///
768static void printAsCString(std::ostream &O, const ConstantArray *CVA,
769 unsigned LastElt) {
770 assert(CVA->isString() && "Array is not string compatible!");
771
772 O << "\"";
773 for (unsigned i = 0; i != LastElt; ++i) {
774 unsigned char C =
775 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
776 printStringChar(O, C);
777 }
778 O << "\"";
779}
780
781/// EmitString - Emit a zero-byte-terminated string constant.
782///
783void AsmPrinter::EmitString(const ConstantArray *CVA) const {
784 unsigned NumElts = CVA->getNumOperands();
785 if (TAI->getAscizDirective() && NumElts &&
786 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
787 O << TAI->getAscizDirective();
788 printAsCString(O, CVA, NumElts-1);
789 } else {
790 O << TAI->getAsciiDirective();
791 printAsCString(O, CVA, NumElts);
792 }
793 O << "\n";
794}
795
796/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
797///
798void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
799 const TargetData *TD = TM.getTargetData();
800
801 if (CV->isNullValue() || isa<UndefValue>(CV)) {
802 EmitZeros(TD->getTypeSize(CV->getType()));
803 return;
804 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
805 if (CVA->isString()) {
806 EmitString(CVA);
807 } else { // Not a string. Print the values in successive locations
808 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
809 EmitGlobalConstant(CVA->getOperand(i));
810 }
811 return;
812 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
813 // Print the fields in successive locations. Pad to align if needed!
814 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
815 uint64_t sizeSoFar = 0;
816 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
817 const Constant* field = CVS->getOperand(i);
818
819 // Check if padding is needed and insert one or more 0s.
820 uint64_t fieldSize = TD->getTypeSize(field->getType());
821 uint64_t padSize = ((i == e-1? cvsLayout->getSizeInBytes()
822 : cvsLayout->getElementOffset(i+1))
823 - cvsLayout->getElementOffset(i)) - fieldSize;
824 sizeSoFar += fieldSize + padSize;
825
826 // Now print the actual field value
827 EmitGlobalConstant(field);
828
829 // Insert the field padding unless it's zero bytes...
830 EmitZeros(padSize);
831 }
832 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
833 "Layout of constant struct may be incorrect!");
834 return;
835 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
836 // FP Constants are printed as integer constants to avoid losing
837 // precision...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 if (CFP->getType() == Type::DoubleTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000839 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000840 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 if (TAI->getData64bitsDirective())
Dale Johannesen1616e902007-09-11 18:32:33 +0000842 O << TAI->getData64bitsDirective() << i << "\t"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 << TAI->getCommentString() << " double value: " << Val << "\n";
844 else if (TD->isBigEndian()) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000845 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 << "\t" << TAI->getCommentString()
847 << " double most significant word " << Val << "\n";
Dale Johannesen1616e902007-09-11 18:32:33 +0000848 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 << "\t" << TAI->getCommentString()
850 << " double least significant word " << Val << "\n";
851 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +0000852 O << TAI->getData32bitsDirective() << unsigned(i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853 << "\t" << TAI->getCommentString()
854 << " double least significant word " << Val << "\n";
Dale Johannesen1616e902007-09-11 18:32:33 +0000855 O << TAI->getData32bitsDirective() << unsigned(i >> 32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 << "\t" << TAI->getCommentString()
857 << " double most significant word " << Val << "\n";
858 }
859 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000860 } else if (CFP->getType() == Type::FloatTy) {
Dale Johannesen1616e902007-09-11 18:32:33 +0000861 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
862 O << TAI->getData32bitsDirective()
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000863 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
865 return;
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000866 } else if (CFP->getType() == Type::X86_FP80Ty) {
867 // all long double variants are printed as hex
868 const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData();
869 if (TD->isBigEndian()) {
870 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
871 << "\t" << TAI->getCommentString()
872 << " long double most significant halfword\n";
873 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
874 << "\t" << TAI->getCommentString()
875 << " long double next halfword\n";
876 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
877 << "\t" << TAI->getCommentString()
878 << " long double next halfword\n";
879 O << TAI->getData16bitsDirective() << uint16_t(p[0])
880 << "\t" << TAI->getCommentString()
881 << " long double next halfword\n";
882 O << TAI->getData16bitsDirective() << uint16_t(p[1])
883 << "\t" << TAI->getCommentString()
884 << " long double least significant halfword\n";
885 } else {
886 O << TAI->getData16bitsDirective() << uint16_t(p[1])
887 << "\t" << TAI->getCommentString()
888 << " long double least significant halfword\n";
889 O << TAI->getData16bitsDirective() << uint16_t(p[0])
890 << "\t" << TAI->getCommentString()
891 << " long double next halfword\n";
892 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
893 << "\t" << TAI->getCommentString()
894 << " long double next halfword\n";
895 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
896 << "\t" << TAI->getCommentString()
897 << " long double next halfword\n";
898 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
899 << "\t" << TAI->getCommentString()
900 << " long double most significant halfword\n";
901 }
902 return;
903 } else assert(0 && "Floating point constant type not handled");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 } else if (CV->getType() == Type::Int64Ty) {
905 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
906 uint64_t Val = CI->getZExtValue();
907
908 if (TAI->getData64bitsDirective())
909 O << TAI->getData64bitsDirective() << Val << "\n";
910 else if (TD->isBigEndian()) {
911 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
912 << "\t" << TAI->getCommentString()
913 << " Double-word most significant word " << Val << "\n";
914 O << TAI->getData32bitsDirective() << unsigned(Val)
915 << "\t" << TAI->getCommentString()
916 << " Double-word least significant word " << Val << "\n";
917 } else {
918 O << TAI->getData32bitsDirective() << unsigned(Val)
919 << "\t" << TAI->getCommentString()
920 << " Double-word least significant word " << Val << "\n";
921 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
922 << "\t" << TAI->getCommentString()
923 << " Double-word most significant word " << Val << "\n";
924 }
925 return;
926 }
927 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
928 const VectorType *PTy = CP->getType();
929
930 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
931 EmitGlobalConstant(CP->getOperand(I));
932
933 return;
934 }
935
936 const Type *type = CV->getType();
937 printDataDirective(type);
938 EmitConstantValueOnly(CV);
939 O << "\n";
940}
941
942void
943AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
944 // Target doesn't support this yet!
945 abort();
946}
947
948/// PrintSpecial - Print information related to the specified machine instr
949/// that is independent of the operand, and may be independent of the instr
950/// itself. This can be useful for portably encoding the comment character
951/// or other bits of target-specific knowledge into the asmstrings. The
952/// syntax used is ${:comment}. Targets can override this to add support
953/// for their own strange codes.
954void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
955 if (!strcmp(Code, "private")) {
956 O << TAI->getPrivateGlobalPrefix();
957 } else if (!strcmp(Code, "comment")) {
958 O << TAI->getCommentString();
959 } else if (!strcmp(Code, "uid")) {
960 // Assign a unique ID to this machine instruction.
961 static const MachineInstr *LastMI = 0;
962 static const Function *F = 0;
963 static unsigned Counter = 0U-1;
964
965 // Comparing the address of MI isn't sufficient, because machineinstrs may
966 // be allocated to the same address across functions.
967 const Function *ThisF = MI->getParent()->getParent()->getFunction();
968
969 // If this is a new machine instruction, bump the counter.
970 if (LastMI != MI || F != ThisF) {
971 ++Counter;
972 LastMI = MI;
973 F = ThisF;
974 }
975 O << Counter;
976 } else {
977 cerr << "Unknown special formatter '" << Code
978 << "' for machine instr: " << *MI;
979 exit(1);
980 }
981}
982
983
984/// printInlineAsm - This method formats and prints the specified machine
985/// instruction that is an inline asm.
986void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
987 unsigned NumOperands = MI->getNumOperands();
988
989 // Count the number of register definitions.
990 unsigned NumDefs = 0;
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000991 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 ++NumDefs)
993 assert(NumDefs != NumOperands-1 && "No asm string?");
994
995 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
996
997 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
998 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
999
1000 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
1001 if (AsmStr[0] == 0) {
1002 O << "\n"; // Tab already printed, avoid double indenting next instr.
1003 return;
1004 }
1005
1006 O << TAI->getInlineAsmStart() << "\n\t";
1007
1008 // The variant of the current asmprinter.
1009 int AsmPrinterVariant = TAI->getAssemblerDialect();
1010
1011 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1012 const char *LastEmitted = AsmStr; // One past the last character emitted.
1013
1014 while (*LastEmitted) {
1015 switch (*LastEmitted) {
1016 default: {
1017 // Not a special case, emit the string section literally.
1018 const char *LiteralEnd = LastEmitted+1;
1019 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1020 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1021 ++LiteralEnd;
1022 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1023 O.write(LastEmitted, LiteralEnd-LastEmitted);
1024 LastEmitted = LiteralEnd;
1025 break;
1026 }
1027 case '\n':
1028 ++LastEmitted; // Consume newline character.
1029 O << "\n"; // Indent code with newline.
1030 break;
1031 case '$': {
1032 ++LastEmitted; // Consume '$' character.
1033 bool Done = true;
1034
1035 // Handle escapes.
1036 switch (*LastEmitted) {
1037 default: Done = false; break;
1038 case '$': // $$ -> $
1039 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1040 O << '$';
1041 ++LastEmitted; // Consume second '$' character.
1042 break;
1043 case '(': // $( -> same as GCC's { character.
1044 ++LastEmitted; // Consume '(' character.
1045 if (CurVariant != -1) {
1046 cerr << "Nested variants found in inline asm string: '"
1047 << AsmStr << "'\n";
1048 exit(1);
1049 }
1050 CurVariant = 0; // We're in the first variant now.
1051 break;
1052 case '|':
1053 ++LastEmitted; // consume '|' character.
1054 if (CurVariant == -1) {
1055 cerr << "Found '|' character outside of variant in inline asm "
1056 << "string: '" << AsmStr << "'\n";
1057 exit(1);
1058 }
1059 ++CurVariant; // We're in the next variant.
1060 break;
1061 case ')': // $) -> same as GCC's } char.
1062 ++LastEmitted; // consume ')' character.
1063 if (CurVariant == -1) {
1064 cerr << "Found '}' character outside of variant in inline asm "
1065 << "string: '" << AsmStr << "'\n";
1066 exit(1);
1067 }
1068 CurVariant = -1;
1069 break;
1070 }
1071 if (Done) break;
1072
1073 bool HasCurlyBraces = false;
1074 if (*LastEmitted == '{') { // ${variable}
1075 ++LastEmitted; // Consume '{' character.
1076 HasCurlyBraces = true;
1077 }
1078
1079 const char *IDStart = LastEmitted;
1080 char *IDEnd;
1081 errno = 0;
1082 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1083 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
1084 cerr << "Bad $ operand number in inline asm string: '"
1085 << AsmStr << "'\n";
1086 exit(1);
1087 }
1088 LastEmitted = IDEnd;
1089
1090 char Modifier[2] = { 0, 0 };
1091
1092 if (HasCurlyBraces) {
1093 // If we have curly braces, check for a modifier character. This
1094 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1095 if (*LastEmitted == ':') {
1096 ++LastEmitted; // Consume ':' character.
1097 if (*LastEmitted == 0) {
1098 cerr << "Bad ${:} expression in inline asm string: '"
1099 << AsmStr << "'\n";
1100 exit(1);
1101 }
1102
1103 Modifier[0] = *LastEmitted;
1104 ++LastEmitted; // Consume modifier character.
1105 }
1106
1107 if (*LastEmitted != '}') {
1108 cerr << "Bad ${} expression in inline asm string: '"
1109 << AsmStr << "'\n";
1110 exit(1);
1111 }
1112 ++LastEmitted; // Consume '}' character.
1113 }
1114
1115 if ((unsigned)Val >= NumOperands-1) {
1116 cerr << "Invalid $ operand number in inline asm string: '"
1117 << AsmStr << "'\n";
1118 exit(1);
1119 }
1120
1121 // Okay, we finally have a value number. Ask the target to print this
1122 // operand!
1123 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1124 unsigned OpNo = 1;
1125
1126 bool Error = false;
1127
1128 // Scan to find the machine operand number for the operand.
1129 for (; Val; --Val) {
1130 if (OpNo >= MI->getNumOperands()) break;
1131 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1132 OpNo += (OpFlags >> 3) + 1;
1133 }
1134
1135 if (OpNo >= MI->getNumOperands()) {
1136 Error = true;
1137 } else {
1138 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
1139 ++OpNo; // Skip over the ID number.
1140
1141 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
1142 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
1143 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1144 Modifier[0] ? Modifier : 0);
1145 } else {
1146 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1147 Modifier[0] ? Modifier : 0);
1148 }
1149 }
1150 if (Error) {
1151 cerr << "Invalid operand found in inline asm: '"
1152 << AsmStr << "'\n";
1153 MI->dump();
1154 exit(1);
1155 }
1156 }
1157 break;
1158 }
1159 }
1160 }
1161 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
1162}
1163
1164/// printLabel - This method prints a local label used by debug and
1165/// exception handling tables.
1166void AsmPrinter::printLabel(const MachineInstr *MI) const {
1167 O << "\n"
1168 << TAI->getPrivateGlobalPrefix()
1169 << "label"
1170 << MI->getOperand(0).getImmedValue()
1171 << ":\n";
1172}
1173
1174/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1175/// instruction, using the specified assembler variant. Targets should
1176/// overried this to format as appropriate.
1177bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1178 unsigned AsmVariant, const char *ExtraCode) {
1179 // Target doesn't support this yet!
1180 return true;
1181}
1182
1183bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1184 unsigned AsmVariant,
1185 const char *ExtraCode) {
1186 // Target doesn't support this yet!
1187 return true;
1188}
1189
1190/// printBasicBlockLabel - This method prints the label for the specified
1191/// MachineBasicBlock
1192void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1193 bool printColon,
1194 bool printComment) const {
1195 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
1196 << MBB->getNumber();
1197 if (printColon)
1198 O << ':';
1199 if (printComment && MBB->getBasicBlock())
Dan Gohman0912cda2007-07-30 15:06:25 +00001200 O << '\t' << TAI->getCommentString() << ' '
1201 << MBB->getBasicBlock()->getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202}
1203
1204/// printSetLabel - This method prints a set label for the specified
1205/// MachineBasicBlock
1206void AsmPrinter::printSetLabel(unsigned uid,
1207 const MachineBasicBlock *MBB) const {
1208 if (!TAI->getSetDirective())
1209 return;
1210
1211 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1212 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
1213 printBasicBlockLabel(MBB, false, false);
1214 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1215 << '_' << uid << '\n';
1216}
1217
1218void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
1219 const MachineBasicBlock *MBB) const {
1220 if (!TAI->getSetDirective())
1221 return;
1222
1223 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
1224 << getFunctionNumber() << '_' << uid << '_' << uid2
1225 << "_set_" << MBB->getNumber() << ',';
1226 printBasicBlockLabel(MBB, false, false);
1227 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1228 << '_' << uid << '_' << uid2 << '\n';
1229}
1230
1231/// printDataDirective - This method prints the asm directive for the
1232/// specified type.
1233void AsmPrinter::printDataDirective(const Type *type) {
1234 const TargetData *TD = TM.getTargetData();
1235 switch (type->getTypeID()) {
1236 case Type::IntegerTyID: {
1237 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1238 if (BitWidth <= 8)
1239 O << TAI->getData8bitsDirective();
1240 else if (BitWidth <= 16)
1241 O << TAI->getData16bitsDirective();
1242 else if (BitWidth <= 32)
1243 O << TAI->getData32bitsDirective();
1244 else if (BitWidth <= 64) {
1245 assert(TAI->getData64bitsDirective() &&
1246 "Target cannot handle 64-bit constant exprs!");
1247 O << TAI->getData64bitsDirective();
1248 }
1249 break;
1250 }
1251 case Type::PointerTyID:
1252 if (TD->getPointerSize() == 8) {
1253 assert(TAI->getData64bitsDirective() &&
1254 "Target cannot handle 64-bit pointer exprs!");
1255 O << TAI->getData64bitsDirective();
1256 } else {
1257 O << TAI->getData32bitsDirective();
1258 }
1259 break;
1260 case Type::FloatTyID: case Type::DoubleTyID:
1261 assert (0 && "Should have already output floating point constant.");
1262 default:
1263 assert (0 && "Can't handle printing this type of thing");
1264 break;
1265 }
1266}
1267