1 //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AsmPrinter class.
12 //===----------------------------------------------------------------------===//
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/Mangler.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetMachine.h"
29 AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
30 : FunctionNumber(0), O(o), TM(tm),
33 PrivateGlobalPrefix("."),
34 GlobalVarAddrPrefix(""),
35 GlobalVarAddrSuffix(""),
36 FunctionAddrPrefix(""),
37 FunctionAddrSuffix(""),
38 InlineAsmStart("#APP"),
39 InlineAsmEnd("#NO_APP"),
40 ZeroDirective("\t.zero\t"),
41 ZeroDirectiveSuffix(0),
42 AsciiDirective("\t.ascii\t"),
43 AscizDirective("\t.asciz\t"),
44 Data8bitsDirective("\t.byte\t"),
45 Data16bitsDirective("\t.short\t"),
46 Data32bitsDirective("\t.long\t"),
47 Data64bitsDirective("\t.quad\t"),
48 AlignDirective("\t.align\t"),
49 AlignmentIsInBytes(true),
50 SwitchToSectionDirective("\t.section\t"),
51 TextSectionStartSuffix(""),
52 DataSectionStartSuffix(""),
53 SectionEndDirectiveSuffix(0),
54 ConstantPoolSection("\t.section .rodata\n"),
55 JumpTableSection("\t.section .rodata\n"),
56 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
57 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
58 FourByteConstantSection(0),
59 EightByteConstantSection(0),
60 SixteenByteConstantSection(0),
62 COMMDirective("\t.comm\t"),
63 COMMDirectiveTakesAlignment(true),
64 HasDotTypeDotSizeDirective(true) {
68 /// SwitchToTextSection - Switch to the specified text section of the executable
69 /// if we are not already in it!
71 void AsmPrinter::SwitchToTextSection(const char *NewSection,
72 const GlobalValue *GV) {
74 if (GV && GV->hasSection())
75 NS = SwitchToSectionDirective + GV->getSection();
79 // If we're already in this section, we're done.
80 if (CurrentSection == NS) return;
82 // Close the current section, if applicable.
83 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
84 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
88 if (!CurrentSection.empty())
89 O << CurrentSection << TextSectionStartSuffix << '\n';
92 /// SwitchToTextSection - Switch to the specified text section of the executable
93 /// if we are not already in it!
95 void AsmPrinter::SwitchToDataSection(const char *NewSection,
96 const GlobalValue *GV) {
98 if (GV && GV->hasSection())
99 NS = SwitchToSectionDirective + GV->getSection();
103 // If we're already in this section, we're done.
104 if (CurrentSection == NS) return;
106 // Close the current section, if applicable.
107 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
108 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
112 if (!CurrentSection.empty())
113 O << CurrentSection << DataSectionStartSuffix << '\n';
117 bool AsmPrinter::doInitialization(Module &M) {
118 Mang = new Mangler(M, GlobalPrefix);
120 if (!M.getModuleInlineAsm().empty())
121 O << CommentString << " Start of file scope inline assembly\n"
122 << M.getModuleInlineAsm()
123 << "\n" << CommentString << " End of file scope inline assembly\n";
125 SwitchToDataSection("", 0); // Reset back to no section.
127 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
128 DebugInfo->AnalyzeModule(M);
134 bool AsmPrinter::doFinalization(Module &M) {
135 delete Mang; Mang = 0;
139 void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
140 // What's my mangled name?
141 CurrentFnName = Mang->getValueName(MF.getFunction());
142 IncrementFunctionNumber();
145 /// EmitConstantPool - Print to the current output stream assembly
146 /// representations of the constants in the constant pool MCP. This is
147 /// used to print out constants which have been "spilled to memory" by
148 /// the code generator.
150 void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
151 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
152 if (CP.empty()) return;
154 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
155 // in special sections.
156 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
157 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
158 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
159 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
160 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
161 MachineConstantPoolEntry CPE = CP[i];
162 const Constant *CV = CPE.Val;
163 const Type *Ty = CV->getType();
164 if (FourByteConstantSection &&
165 TM.getTargetData()->getTypeSize(Ty) == 4)
166 FourByteCPs.push_back(std::make_pair(CPE, i));
167 else if (EightByteConstantSection &&
168 TM.getTargetData()->getTypeSize(Ty) == 8)
169 EightByteCPs.push_back(std::make_pair(CPE, i));
170 else if (SixteenByteConstantSection &&
171 TM.getTargetData()->getTypeSize(Ty) == 16)
172 SixteenByteCPs.push_back(std::make_pair(CPE, i));
174 OtherCPs.push_back(std::make_pair(CPE, i));
177 unsigned Alignment = MCP->getConstantPoolAlignment();
178 EmitConstantPool(Alignment, FourByteConstantSection, FourByteCPs);
179 EmitConstantPool(Alignment, EightByteConstantSection, EightByteCPs);
180 EmitConstantPool(Alignment, SixteenByteConstantSection, SixteenByteCPs);
181 EmitConstantPool(Alignment, ConstantPoolSection, OtherCPs);
184 void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
185 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
186 if (CP.empty()) return;
188 SwitchToDataSection(Section, 0);
189 EmitAlignment(Alignment);
190 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
191 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_'
192 << CP[i].second << ":\t\t\t\t\t" << CommentString << " ";
193 WriteTypeSymbolic(O, CP[i].first.Val->getType(), 0) << '\n';
194 EmitGlobalConstant(CP[i].first.Val);
197 TM.getTargetData()->getTypeSize(CP[i].first.Val->getType());
198 unsigned ValEnd = CP[i].first.Offset + EntSize;
199 // Emit inter-object padding for alignment.
200 EmitZeros(CP[i+1].first.Offset-ValEnd);
205 /// EmitJumpTableInfo - Print assembly representations of the jump tables used
206 /// by the current function to the current output stream.
208 void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
209 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
210 if (JT.empty()) return;
211 const TargetData *TD = TM.getTargetData();
213 // FIXME: someday we need to handle PIC jump tables
214 assert((TM.getRelocationModel() == Reloc::Static ||
215 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
216 "Unhandled relocation model emitting jump table information!");
218 SwitchToDataSection(JumpTableSection, 0);
219 EmitAlignment(Log2_32(TD->getPointerAlignment()));
221 // Pick the directive to use based on the pointer size. FIXME: when we support
222 // PIC jumptables, this should always use the 32-bit directive for label
224 const char *PtrDataDirective = Data32bitsDirective;
225 if (TD->getPointerSize() == 8)
226 PtrDataDirective = Data64bitsDirective;
228 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
229 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
231 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
232 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
233 O << PtrDataDirective << ' ';
234 printBasicBlockLabel(JTBBs[ii]);
240 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
241 /// special global used by LLVM. If so, emit it and return true, otherwise
242 /// do nothing and return false.
243 bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
244 // Ignore debug and non-emitted data.
245 if (GV->getSection() == "llvm.metadata") return true;
247 if (!GV->hasAppendingLinkage()) return false;
249 assert(GV->hasInitializer() && "Not a special LLVM global!");
251 if (GV->getName() == "llvm.used")
252 return true; // No need to emit this at all.
254 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
255 SwitchToDataSection(StaticCtorsSection, 0);
257 EmitXXStructorList(GV->getInitializer());
261 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
262 SwitchToDataSection(StaticDtorsSection, 0);
264 EmitXXStructorList(GV->getInitializer());
271 /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
272 /// function pointers, ignoring the init priority.
273 void AsmPrinter::EmitXXStructorList(Constant *List) {
274 // Should be an array of '{ int, void ()* }' structs. The first value is the
275 // init priority, which we ignore.
276 if (!isa<ConstantArray>(List)) return;
277 ConstantArray *InitList = cast<ConstantArray>(List);
278 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
279 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
280 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
282 if (CS->getOperand(1)->isNullValue())
283 return; // Found a null terminator, exit printing.
284 // Emit the function pointer.
285 EmitGlobalConstant(CS->getOperand(1));
289 /// getPreferredAlignmentLog - Return the preferred alignment of the
290 /// specified global, returned in log form. This includes an explicitly
291 /// requested alignment (if the global has one).
292 unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
293 const Type *ElemType = GV->getType()->getElementType();
294 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
295 if (GV->getAlignment() > (1U << Alignment))
296 Alignment = Log2_32(GV->getAlignment());
298 if (GV->hasInitializer()) {
299 // Always round up alignment of global doubles to 8 bytes.
300 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
303 // If the global is not external, see if it is large. If so, give it a
305 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
306 Alignment = 4; // 16-byte alignment.
312 // EmitAlignment - Emit an alignment directive to the specified power of two.
313 void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
314 if (GV && GV->getAlignment())
315 NumBits = Log2_32(GV->getAlignment());
316 if (NumBits == 0) return; // No need to emit alignment.
317 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
318 O << AlignDirective << NumBits << "\n";
321 /// EmitZeros - Emit a block of zeros.
323 void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
326 O << ZeroDirective << NumZeros;
327 if (ZeroDirectiveSuffix)
328 O << ZeroDirectiveSuffix;
331 for (; NumZeros; --NumZeros)
332 O << Data8bitsDirective << "0\n";
337 // Print out the specified constant, without a storage class. Only the
338 // constants valid in constant expressions can occur here.
339 void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
340 if (CV->isNullValue() || isa<UndefValue>(CV))
342 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
343 assert(CB == ConstantBool::True);
345 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
346 if (((CI->getValue() << 32) >> 32) == CI->getValue())
349 O << (uint64_t)CI->getValue();
350 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
352 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
353 // This is a constant address for a global variable or function. Use the
354 // name of the variable or function as the address value, possibly
355 // decorating it with GlobalVarAddrPrefix/Suffix or
356 // FunctionAddrPrefix/Suffix (these all default to "" )
357 if (isa<Function>(GV))
358 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
360 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
361 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
362 const TargetData *TD = TM.getTargetData();
363 switch(CE->getOpcode()) {
364 case Instruction::GetElementPtr: {
365 // generate a symbolic expression for the byte address
366 const Constant *ptrVal = CE->getOperand(0);
367 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
368 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
371 EmitConstantValueOnly(ptrVal);
373 O << ") + " << Offset;
375 O << ") - " << -Offset;
377 EmitConstantValueOnly(ptrVal);
381 case Instruction::Cast: {
382 // Support only non-converting or widening casts for now, that is, ones
383 // that do not involve a change in value. This assertion is really gross,
384 // and may not even be a complete check.
385 Constant *Op = CE->getOperand(0);
386 const Type *OpTy = Op->getType(), *Ty = CE->getType();
388 // Remember, kids, pointers can be losslessly converted back and forth
389 // into 32-bit or wider integers, regardless of signedness. :-P
390 assert(((isa<PointerType>(OpTy)
391 && (Ty == Type::LongTy || Ty == Type::ULongTy
392 || Ty == Type::IntTy || Ty == Type::UIntTy))
393 || (isa<PointerType>(Ty)
394 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
395 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
396 || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
397 && OpTy->isLosslesslyConvertibleTo(Ty))))
398 && "FIXME: Don't yet support this kind of constant cast expr");
399 EmitConstantValueOnly(Op);
402 case Instruction::Add:
404 EmitConstantValueOnly(CE->getOperand(0));
406 EmitConstantValueOnly(CE->getOperand(1));
410 assert(0 && "Unsupported operator!");
413 assert(0 && "Unknown constant value!");
417 /// toOctal - Convert the low order bits of X into an octal digit.
419 static inline char toOctal(int X) {
423 /// printAsCString - Print the specified array as a C compatible string, only if
424 /// the predicate isString is true.
426 static void printAsCString(std::ostream &O, const ConstantArray *CVA,
428 assert(CVA->isString() && "Array is not string compatible!");
431 for (unsigned i = 0; i != LastElt; ++i) {
433 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
437 } else if (C == '\\') {
439 } else if (isprint(C)) {
443 case '\b': O << "\\b"; break;
444 case '\f': O << "\\f"; break;
445 case '\n': O << "\\n"; break;
446 case '\r': O << "\\r"; break;
447 case '\t': O << "\\t"; break;
450 O << toOctal(C >> 6);
451 O << toOctal(C >> 3);
452 O << toOctal(C >> 0);
460 /// EmitString - Emit a zero-byte-terminated string constant.
462 void AsmPrinter::EmitString(const ConstantArray *CVA) const {
463 unsigned NumElts = CVA->getNumOperands();
464 if (AscizDirective && NumElts &&
465 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
467 printAsCString(O, CVA, NumElts-1);
470 printAsCString(O, CVA, NumElts);
475 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
477 void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
478 const TargetData *TD = TM.getTargetData();
480 if (CV->isNullValue() || isa<UndefValue>(CV)) {
481 EmitZeros(TD->getTypeSize(CV->getType()));
483 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
484 if (CVA->isString()) {
486 } else { // Not a string. Print the values in successive locations
487 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
488 EmitGlobalConstant(CVA->getOperand(i));
491 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
492 // Print the fields in successive locations. Pad to align if needed!
493 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
494 uint64_t sizeSoFar = 0;
495 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
496 const Constant* field = CVS->getOperand(i);
498 // Check if padding is needed and insert one or more 0s.
499 uint64_t fieldSize = TD->getTypeSize(field->getType());
500 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
501 : cvsLayout->MemberOffsets[i+1])
502 - cvsLayout->MemberOffsets[i]) - fieldSize;
503 sizeSoFar += fieldSize + padSize;
505 // Now print the actual field value
506 EmitGlobalConstant(field);
508 // Insert the field padding unless it's zero bytes...
511 assert(sizeSoFar == cvsLayout->StructSize &&
512 "Layout of constant struct may be incorrect!");
514 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
515 // FP Constants are printed as integer constants to avoid losing
517 double Val = CFP->getValue();
518 if (CFP->getType() == Type::DoubleTy) {
519 if (Data64bitsDirective)
520 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
521 << " double value: " << Val << "\n";
522 else if (TD->isBigEndian()) {
523 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
524 << "\t" << CommentString << " double most significant word "
526 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
527 << "\t" << CommentString << " double least significant word "
530 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
531 << "\t" << CommentString << " double least significant word " << Val
533 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
534 << "\t" << CommentString << " double most significant word " << Val
539 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
540 << " float " << Val << "\n";
543 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
544 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
545 uint64_t Val = CI->getRawValue();
547 if (Data64bitsDirective)
548 O << Data64bitsDirective << Val << "\n";
549 else if (TD->isBigEndian()) {
550 O << Data32bitsDirective << unsigned(Val >> 32)
551 << "\t" << CommentString << " Double-word most significant word "
553 O << Data32bitsDirective << unsigned(Val)
554 << "\t" << CommentString << " Double-word least significant word "
557 O << Data32bitsDirective << unsigned(Val)
558 << "\t" << CommentString << " Double-word least significant word "
560 O << Data32bitsDirective << unsigned(Val >> 32)
561 << "\t" << CommentString << " Double-word most significant word "
566 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
567 const PackedType *PTy = CP->getType();
569 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
570 EmitGlobalConstant(CP->getOperand(I));
575 const Type *type = CV->getType();
576 switch (type->getTypeID()) {
578 case Type::UByteTyID: case Type::SByteTyID:
579 O << Data8bitsDirective;
581 case Type::UShortTyID: case Type::ShortTyID:
582 O << Data16bitsDirective;
584 case Type::PointerTyID:
585 if (TD->getPointerSize() == 8) {
586 assert(Data64bitsDirective &&
587 "Target cannot handle 64-bit pointer exprs!");
588 O << Data64bitsDirective;
591 //Fall through for pointer size == int size
592 case Type::UIntTyID: case Type::IntTyID:
593 O << Data32bitsDirective;
595 case Type::ULongTyID: case Type::LongTyID:
596 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
597 O << Data64bitsDirective;
599 case Type::FloatTyID: case Type::DoubleTyID:
600 assert (0 && "Should have already output floating point constant.");
602 assert (0 && "Can't handle printing this type of thing");
605 EmitConstantValueOnly(CV);
609 /// printInlineAsm - This method formats and prints the specified machine
610 /// instruction that is an inline asm.
611 void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
612 O << InlineAsmStart << "\n\t";
613 unsigned NumOperands = MI->getNumOperands();
615 // Count the number of register definitions.
616 unsigned NumDefs = 0;
617 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
618 assert(NumDefs != NumOperands-1 && "No asm string?");
620 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
622 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
623 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
625 // The variant of the current asmprinter: FIXME: change.
626 int AsmPrinterVariant = 0;
628 int CurVariant = -1; // The number of the {.|.|.} region we are in.
629 const char *LastEmitted = AsmStr; // One past the last character emitted.
631 while (*LastEmitted) {
632 switch (*LastEmitted) {
634 // Not a special case, emit the string section literally.
635 const char *LiteralEnd = LastEmitted+1;
636 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
637 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
639 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
640 O.write(LastEmitted, LiteralEnd-LastEmitted);
641 LastEmitted = LiteralEnd;
645 ++LastEmitted; // Consume newline character.
646 O << "\n\t"; // Indent code with newline.
649 ++LastEmitted; // Consume '$' character.
650 if (*LastEmitted == '$') { // $$ -> $
651 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
653 ++LastEmitted; // Consume second '$' character.
657 bool HasCurlyBraces = false;
658 if (*LastEmitted == '{') { // ${variable}
659 ++LastEmitted; // Consume '{' character.
660 HasCurlyBraces = true;
663 const char *IDStart = LastEmitted;
665 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
666 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
667 std::cerr << "Bad $ operand number in inline asm string: '"
673 char Modifier[2] = { 0, 0 };
675 if (HasCurlyBraces) {
676 // If we have curly braces, check for a modifier character. This
677 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
678 if (*LastEmitted == ':') {
679 ++LastEmitted; // Consume ':' character.
680 if (*LastEmitted == 0) {
681 std::cerr << "Bad ${:} expression in inline asm string: '"
686 Modifier[0] = *LastEmitted;
687 ++LastEmitted; // Consume modifier character.
690 if (*LastEmitted != '}') {
691 std::cerr << "Bad ${} expression in inline asm string: '"
695 ++LastEmitted; // Consume '}' character.
698 if ((unsigned)Val >= NumOperands-1) {
699 std::cerr << "Invalid $ operand number in inline asm string: '"
704 // Okay, we finally have a value number. Ask the target to print this
706 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
711 // Scan to find the machine operand number for the operand.
713 if (OpNo >= MI->getNumOperands()) break;
714 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
715 OpNo += (OpFlags >> 3) + 1;
718 if (OpNo >= MI->getNumOperands()) {
721 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
722 ++OpNo; // Skip over the ID number.
724 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
725 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
726 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
727 Modifier[0] ? Modifier : 0);
729 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
730 Modifier[0] ? Modifier : 0);
734 std::cerr << "Invalid operand found in inline asm: '"
743 ++LastEmitted; // Consume '{' character.
744 if (CurVariant != -1) {
745 std::cerr << "Nested variants found in inline asm string: '"
749 CurVariant = 0; // We're in the first variant now.
752 ++LastEmitted; // consume '|' character.
753 if (CurVariant == -1) {
754 std::cerr << "Found '|' character outside of variant in inline asm "
755 << "string: '" << AsmStr << "'\n";
758 ++CurVariant; // We're in the next variant.
761 ++LastEmitted; // consume '}' character.
762 if (CurVariant == -1) {
763 std::cerr << "Found '}' character outside of variant in inline asm "
764 << "string: '" << AsmStr << "'\n";
771 O << "\n\t" << InlineAsmEnd << "\n";
774 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
775 /// instruction, using the specified assembler variant. Targets should
776 /// overried this to format as appropriate.
777 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
778 unsigned AsmVariant, const char *ExtraCode) {
779 // Target doesn't support this yet!
783 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
785 const char *ExtraCode) {
786 // Target doesn't support this yet!
790 /// printBasicBlockLabel - This method prints the label for the specified
791 /// MachineBasicBlock
792 void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
794 bool printComment) const {
795 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
800 O << '\t' << CommentString << MBB->getBasicBlock()->getName();