Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 1 | //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the AsmPrinter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Evan Cheng | 932f022 | 2006-03-03 02:04:29 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/AsmPrinter.h" |
Evan Cheng | 246ae0d | 2006-03-01 22:18:09 +0000 | [diff] [blame] | 15 | #include "llvm/Assembly/Writer.h" |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/DwarfWriter.h" |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/GCMetadataPrinter.h" |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineConstantPool.h" |
David Greene | 1924aab | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
David Greene | fe37ab3 | 2009-08-18 19:22:55 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Argyrios Kyrtzidis | cd76240 | 2009-05-07 13:55:51 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/DebugInfo.h" |
Chris Lattner | 2b2954f | 2009-07-27 21:28:04 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCContext.h" |
David Greene | 3ac1ab8 | 2009-07-16 22:24:20 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCInst.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCSection.h" |
| 31 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | 7cb384d | 2009-09-12 23:02:08 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCSymbol.h" |
Evan Cheng | 42bf74b | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
David Greene | 014700c | 2009-07-13 20:25:48 +0000 | [diff] [blame] | 35 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 37 | #include "llvm/Target/Mangler.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetData.h" |
David Greene | 1924aab | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | 0336fdb | 2006-10-06 22:50:56 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Evan Cheng | 6547e40 | 2008-07-01 23:18:29 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 46 | #include <cerrno> |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Evan Cheng | 42bf74b | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 49 | static cl::opt<cl::boolOrDefault> |
| 50 | AsmVerbose("asm-verbose", cl::desc("Add comments to directives."), |
| 51 | cl::init(cl::BOU_UNSET)); |
| 52 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 53 | char AsmPrinter::ID = 0; |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 54 | AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm, |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 55 | const MCAsmInfo *T, bool VDef) |
Daniel Dunbar | 5bcc8bd | 2009-07-01 01:48:54 +0000 | [diff] [blame] | 56 | : MachineFunctionPass(&ID), FunctionNumber(0), O(o), |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 57 | TM(tm), MAI(T), TRI(tm.getRegisterInfo()), |
Chris Lattner | 2b2954f | 2009-07-27 21:28:04 +0000 | [diff] [blame] | 58 | |
| 59 | OutContext(*new MCContext()), |
Chris Lattner | 90edac0 | 2009-09-14 03:02:37 +0000 | [diff] [blame] | 60 | // FIXME: Pass instprinter to streamer. |
Chris Lattner | 1658202 | 2010-01-20 06:39:07 +0000 | [diff] [blame] | 61 | OutStreamer(*createAsmStreamer(OutContext, O, *T, |
| 62 | TM.getTargetData()->isLittleEndian(), 0)), |
Chris Lattner | 2b2954f | 2009-07-27 21:28:04 +0000 | [diff] [blame] | 63 | |
Devang Patel | 6b61f58 | 2010-01-16 06:09:35 +0000 | [diff] [blame] | 64 | LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) { |
Chris Lattner | 0de1fc4 | 2009-06-24 19:09:55 +0000 | [diff] [blame] | 65 | DW = 0; MMI = 0; |
Evan Cheng | 42bf74b | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 66 | switch (AsmVerbose) { |
| 67 | case cl::BOU_UNSET: VerboseAsm = VDef; break; |
| 68 | case cl::BOU_TRUE: VerboseAsm = true; break; |
| 69 | case cl::BOU_FALSE: VerboseAsm = false; break; |
| 70 | } |
| 71 | } |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 72 | |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 73 | AsmPrinter::~AsmPrinter() { |
| 74 | for (gcp_iterator I = GCMetadataPrinters.begin(), |
| 75 | E = GCMetadataPrinters.end(); I != E; ++I) |
| 76 | delete I->second; |
Chris Lattner | 2b2954f | 2009-07-27 21:28:04 +0000 | [diff] [blame] | 77 | |
| 78 | delete &OutStreamer; |
| 79 | delete &OutContext; |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 80 | } |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 38c3988 | 2009-08-03 19:12:26 +0000 | [diff] [blame] | 82 | TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const { |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 83 | return TM.getTargetLowering()->getObjFileLowering(); |
| 84 | } |
| 85 | |
Chris Lattner | dabf07c | 2009-08-18 06:15:16 +0000 | [diff] [blame] | 86 | /// getCurrentSection() - Return the current section we are emitting to. |
| 87 | const MCSection *AsmPrinter::getCurrentSection() const { |
| 88 | return OutStreamer.getCurrentSection(); |
| 89 | } |
| 90 | |
| 91 | |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 92 | void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 93 | AU.setPreservesAll(); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 94 | MachineFunctionPass::getAnalysisUsage(AU); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 95 | AU.addRequired<GCModuleInfo>(); |
David Greene | fe37ab3 | 2009-08-18 19:22:55 +0000 | [diff] [blame] | 96 | if (VerboseAsm) |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 97 | AU.addRequired<MachineLoopInfo>(); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 100 | bool AsmPrinter::doInitialization(Module &M) { |
Chris Lattner | f26e03b | 2009-07-31 17:42:42 +0000 | [diff] [blame] | 101 | // Initialize TargetLoweringObjectFile. |
| 102 | const_cast<TargetLoweringObjectFile&>(getObjFileLowering()) |
| 103 | .Initialize(OutContext, TM); |
| 104 | |
Chris Lattner | c0dba72 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 105 | Mang = new Mangler(*MAI); |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 106 | |
Bob Wilson | 812209a | 2009-09-30 22:06:26 +0000 | [diff] [blame] | 107 | // Allow the target to emit any magic that it wants at the start of the file. |
| 108 | EmitStartOfAsmFile(M); |
Rafael Espindola | 952b839 | 2008-12-03 11:01:37 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 110 | if (MAI->hasSingleParameterDotFile()) { |
Rafael Espindola | 952b839 | 2008-12-03 11:01:37 +0000 | [diff] [blame] | 111 | /* Very minimal debug info. It is ignored if we emit actual |
Bob Wilson | bc9506f | 2009-09-30 21:26:13 +0000 | [diff] [blame] | 112 | debug info. If we don't, this at least helps the user find where |
Rafael Espindola | 952b839 | 2008-12-03 11:01:37 +0000 | [diff] [blame] | 113 | a function came from. */ |
| 114 | O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n"; |
| 115 | } |
| 116 | |
Bob Wilson | 812209a | 2009-09-30 22:06:26 +0000 | [diff] [blame] | 117 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); |
| 118 | assert(MI && "AsmPrinter didn't require GCModuleInfo?"); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 119 | for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) |
| 120 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I)) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 121 | MP->beginAssembly(O, *this, *MAI); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 3e2fa7a | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 123 | if (!M.getModuleInlineAsm().empty()) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 124 | O << MAI->getCommentString() << " Start of file scope inline assembly\n" |
Chris Lattner | 3e2fa7a | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 125 | << M.getModuleInlineAsm() |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 126 | << '\n' << MAI->getCommentString() |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 127 | << " End of file scope inline assembly\n"; |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 128 | |
Chris Lattner | b55e068 | 2009-09-24 05:44:53 +0000 | [diff] [blame] | 129 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 130 | if (MMI) |
| 131 | MMI->AnalyzeModule(M); |
| 132 | DW = getAnalysisIfAvailable<DwarfWriter>(); |
Sanjiv Gupta | 9a501cf | 2009-11-14 07:22:25 +0000 | [diff] [blame] | 133 | if (DW) |
Chris Lattner | b55e068 | 2009-09-24 05:44:53 +0000 | [diff] [blame] | 134 | DW->BeginModule(&M, MMI, O, this, MAI); |
Devang Patel | 14a55d9 | 2009-06-19 21:54:26 +0000 | [diff] [blame] | 135 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
Chris Lattner | 48d64ba | 2010-01-19 04:39:15 +0000 | [diff] [blame] | 139 | /// EmitGlobalVariable - Emit the specified global variable to the .s file. |
| 140 | void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) { |
| 141 | if (!GV->hasInitializer()) // External globals require no code. |
| 142 | return; |
| 143 | |
| 144 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 145 | if (EmitSpecialLLVMGlobal(GV)) |
| 146 | return; |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 147 | |
| 148 | MCSymbol *GVSym = GetGlobalValueSymbol(GV); |
| 149 | printVisibility(GVSym, GV->getVisibility()); |
| 150 | |
| 151 | if (MAI->hasDotTypeDotSizeDirective()) { |
| 152 | O << "\t.type\t" << *GVSym; |
| 153 | if (MAI->getCommentString()[0] != '@') |
| 154 | O << ",@object\n"; |
| 155 | else |
| 156 | O << ",%object\n"; |
| 157 | } |
Chris Lattner | 48d64ba | 2010-01-19 04:39:15 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 159 | SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM); |
| 160 | |
| 161 | const TargetData *TD = TM.getTargetData(); |
| 162 | unsigned Size = TD->getTypeAllocSize(GV->getType()->getElementType()); |
| 163 | unsigned AlignLog = TD->getPreferredAlignmentLog(GV); |
| 164 | |
Chris Lattner | 9744d61 | 2010-01-19 05:51:42 +0000 | [diff] [blame] | 165 | // Handle common and BSS local symbols (.lcomm). |
| 166 | if (GVKind.isCommon() || GVKind.isBSSLocal()) { |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 167 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 168 | |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 169 | if (VerboseAsm) { |
| 170 | O.PadToColumn(MAI->getCommentColumn()); |
| 171 | O << MAI->getCommentString() << ' '; |
| 172 | WriteAsOperand(O, GV, /*PrintType=*/false, GV->getParent()); |
Chris Lattner | 4ed5438 | 2010-01-19 06:01:04 +0000 | [diff] [blame] | 173 | O << '\n'; |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 174 | } |
Chris Lattner | 814819f | 2010-01-19 06:25:51 +0000 | [diff] [blame] | 175 | |
| 176 | // Handle common symbols. |
Chris Lattner | 9744d61 | 2010-01-19 05:51:42 +0000 | [diff] [blame] | 177 | if (GVKind.isCommon()) { |
| 178 | // .comm _foo, 42, 4 |
Chris Lattner | 4ed5438 | 2010-01-19 06:01:04 +0000 | [diff] [blame] | 179 | OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog); |
Chris Lattner | 814819f | 2010-01-19 06:25:51 +0000 | [diff] [blame] | 180 | return; |
Chris Lattner | 9744d61 | 2010-01-19 05:51:42 +0000 | [diff] [blame] | 181 | } |
Chris Lattner | 814819f | 2010-01-19 06:25:51 +0000 | [diff] [blame] | 182 | |
| 183 | // Handle local BSS symbols. |
| 184 | if (MAI->hasMachoZeroFillDirective()) { |
| 185 | const MCSection *TheSection = |
| 186 | getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM); |
| 187 | // .zerofill __DATA, __bss, _foo, 400, 5 |
| 188 | OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | if (const char *LComm = MAI->getLCOMMDirective()) { |
| 193 | // .lcomm _foo, 42 |
| 194 | O << LComm << *GVSym << ',' << Size; |
| 195 | O << '\n'; |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | // .local _foo |
| 200 | O << "\t.local\t" << *GVSym << '\n'; |
| 201 | // .comm _foo, 42, 4 |
| 202 | OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog); |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | const MCSection *TheSection = |
| 207 | getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM); |
| 208 | |
| 209 | // Handle the zerofill directive on darwin, which is a special form of BSS |
| 210 | // emission. |
| 211 | if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) { |
| 212 | // .globl _foo |
| 213 | OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global); |
| 214 | // .zerofill __DATA, __common, _foo, 400, 5 |
| 215 | OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | OutStreamer.SwitchSection(TheSection); |
| 220 | |
| 221 | // TODO: Factor into an 'emit linkage' thing that is shared with function |
| 222 | // bodies. |
| 223 | switch (GV->getLinkage()) { |
| 224 | case GlobalValue::CommonLinkage: |
| 225 | case GlobalValue::LinkOnceAnyLinkage: |
| 226 | case GlobalValue::LinkOnceODRLinkage: |
| 227 | case GlobalValue::WeakAnyLinkage: |
| 228 | case GlobalValue::WeakODRLinkage: |
| 229 | case GlobalValue::LinkerPrivateLinkage: |
Chris Lattner | 4c8c668 | 2010-01-19 06:41:24 +0000 | [diff] [blame] | 230 | if (MAI->getWeakDefDirective() != 0) { |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 231 | // .globl _foo |
| 232 | OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global); |
| 233 | // .weak_definition _foo |
Chris Lattner | 4c8c668 | 2010-01-19 06:41:24 +0000 | [diff] [blame] | 234 | OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::WeakDefinition); |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 235 | } else if (const char *LinkOnce = MAI->getLinkOnceDirective()) { |
| 236 | // .globl _foo |
| 237 | OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global); |
| 238 | // .linkonce same_size |
| 239 | O << LinkOnce; |
Chris Lattner | 4c8c668 | 2010-01-19 06:41:24 +0000 | [diff] [blame] | 240 | } else { |
| 241 | // .weak _foo |
| 242 | OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Weak); |
| 243 | } |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 244 | break; |
| 245 | case GlobalValue::DLLExportLinkage: |
| 246 | case GlobalValue::AppendingLinkage: |
| 247 | // FIXME: appending linkage variables should go into a section of |
| 248 | // their name or something. For now, just emit them as external. |
| 249 | case GlobalValue::ExternalLinkage: |
| 250 | // If external or appending, declare as a global symbol. |
| 251 | // .globl _foo |
| 252 | OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global); |
| 253 | break; |
| 254 | case GlobalValue::PrivateLinkage: |
| 255 | case GlobalValue::InternalLinkage: |
| 256 | break; |
| 257 | default: |
| 258 | llvm_unreachable("Unknown linkage type!"); |
| 259 | } |
| 260 | |
| 261 | EmitAlignment(AlignLog, GV); |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 262 | if (VerboseAsm) { |
| 263 | O.PadToColumn(MAI->getCommentColumn()); |
| 264 | O << MAI->getCommentString() << ' '; |
| 265 | WriteAsOperand(O, GV, /*PrintType=*/false, GV->getParent()); |
Chris Lattner | 4c8c668 | 2010-01-19 06:41:24 +0000 | [diff] [blame] | 266 | O << '\n'; |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 267 | } |
Chris Lattner | 4c8c668 | 2010-01-19 06:41:24 +0000 | [diff] [blame] | 268 | OutStreamer.EmitLabel(GVSym); |
Chris Lattner | 74bfe21 | 2010-01-19 05:38:33 +0000 | [diff] [blame] | 269 | |
| 270 | EmitGlobalConstant(GV->getInitializer()); |
| 271 | |
| 272 | if (MAI->hasDotTypeDotSizeDirective()) |
| 273 | O << "\t.size\t" << *GVSym << ", " << Size << '\n'; |
Chris Lattner | 48d64ba | 2010-01-19 04:39:15 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 277 | bool AsmPrinter::doFinalization(Module &M) { |
Chris Lattner | 40bbebd | 2009-07-21 18:38:57 +0000 | [diff] [blame] | 278 | // Emit global variables. |
| 279 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 280 | I != E; ++I) |
Chris Lattner | 48d64ba | 2010-01-19 04:39:15 +0000 | [diff] [blame] | 281 | EmitGlobalVariable(I); |
Chris Lattner | 40bbebd | 2009-07-21 18:38:57 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 1f522fe | 2009-06-24 18:54:37 +0000 | [diff] [blame] | 283 | // Emit final debug information. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 284 | if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling()) |
Chris Lattner | 1f522fe | 2009-06-24 18:54:37 +0000 | [diff] [blame] | 285 | DW->EndModule(); |
| 286 | |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 287 | // If the target wants to know about weak references, print them all. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 288 | if (MAI->getWeakRefDirective()) { |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 289 | // FIXME: This is not lazy, it would be nice to only print weak references |
| 290 | // to stuff that is actually used. Note that doing so would require targets |
| 291 | // to notice uses in operands (due to constant exprs etc). This should |
| 292 | // happen with the MC stuff eventually. |
Rafael Espindola | 15404d0 | 2006-12-18 03:37:18 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 294 | // Print out module-level global variables here. |
| 295 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 296 | I != E; ++I) { |
Chris Lattner | 08ce3b4 | 2010-01-16 18:17:26 +0000 | [diff] [blame] | 297 | if (!I->hasExternalWeakLinkage()) continue; |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 298 | O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n'; |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Chris Lattner | c6fdced | 2009-08-03 23:10:34 +0000 | [diff] [blame] | 301 | for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Chris Lattner | 08ce3b4 | 2010-01-16 18:17:26 +0000 | [diff] [blame] | 302 | if (!I->hasExternalWeakLinkage()) continue; |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 303 | O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n'; |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 304 | } |
Rafael Espindola | 15404d0 | 2006-12-18 03:37:18 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 307 | if (MAI->getSetDirective()) { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 308 | O << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 309 | for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 310 | I != E; ++I) { |
Chris Lattner | 5c40e69 | 2010-01-16 01:17:26 +0000 | [diff] [blame] | 311 | MCSymbol *Name = GetGlobalValueSymbol(I); |
Anton Korobeynikov | 325be7c | 2007-09-06 17:21:48 +0000 | [diff] [blame] | 312 | |
| 313 | const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal()); |
Chris Lattner | 5c40e69 | 2010-01-16 01:17:26 +0000 | [diff] [blame] | 314 | MCSymbol *Target = GetGlobalValueSymbol(GV); |
Anton Korobeynikov | 541af7f | 2008-09-24 22:21:04 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 316 | if (I->hasExternalLinkage() || !MAI->getWeakRefDirective()) |
| 317 | O << "\t.globl\t" << *Name << '\n'; |
| 318 | else if (I->hasWeakLinkage()) |
| 319 | O << MAI->getWeakRefDirective() << *Name << '\n'; |
| 320 | else |
Chris Lattner | 1059549 | 2010-01-16 01:37:14 +0000 | [diff] [blame] | 321 | assert(I->hasLocalLinkage() && "Invalid alias linkage"); |
Anton Korobeynikov | 22c9e65 | 2008-03-11 21:41:14 +0000 | [diff] [blame] | 322 | |
Anton Korobeynikov | 541af7f | 2008-09-24 22:21:04 +0000 | [diff] [blame] | 323 | printVisibility(Name, I->getVisibility()); |
Anton Korobeynikov | 22c9e65 | 2008-03-11 21:41:14 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 325 | O << MAI->getSetDirective() << ' ' << *Name << ", " << *Target << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 329 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 330 | assert(MI && "AsmPrinter didn't require GCModuleInfo?"); |
| 331 | for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; ) |
| 332 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I)) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 333 | MP->finishAssembly(O, *this, *MAI); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 334 | |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 335 | // If we don't have any trampolines, then we don't require stack memory |
| 336 | // to be executable. Some targets have a directive to declare this. |
Chris Lattner | 0a7befa | 2009-06-24 18:52:01 +0000 | [diff] [blame] | 337 | Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline"); |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 338 | if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty()) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 339 | if (MAI->getNonexecutableStackDirective()) |
| 340 | O << MAI->getNonexecutableStackDirective() << '\n'; |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 341 | |
Chris Lattner | bd23d5f | 2009-09-18 20:17:03 +0000 | [diff] [blame] | 342 | |
| 343 | // Allow the target to emit any magic that it wants at the end of the file, |
| 344 | // after everything else has gone out. |
| 345 | EmitEndOfAsmFile(M); |
| 346 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 347 | delete Mang; Mang = 0; |
Chris Lattner | 0de1fc4 | 2009-06-24 19:09:55 +0000 | [diff] [blame] | 348 | DW = 0; MMI = 0; |
Chris Lattner | 2b2954f | 2009-07-27 21:28:04 +0000 | [diff] [blame] | 349 | |
| 350 | OutStreamer.Finish(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 354 | void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { |
Chris Lattner | 412c3a5 | 2010-01-16 01:24:10 +0000 | [diff] [blame] | 355 | // Get the function symbol. |
Chris Lattner | d1947ed | 2010-01-15 23:55:16 +0000 | [diff] [blame] | 356 | CurrentFnSym = GetGlobalValueSymbol(MF.getFunction()); |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 357 | IncrementFunctionNumber(); |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 25d812b | 2009-09-16 00:35:39 +0000 | [diff] [blame] | 359 | if (VerboseAsm) |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 360 | LI = &getAnalysis<MachineLoopInfo>(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 363 | namespace { |
| 364 | // SectionCPs - Keep track the alignment, constpool entries per Section. |
| 365 | struct SectionCPs { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 366 | const MCSection *S; |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 367 | unsigned Alignment; |
| 368 | SmallVector<unsigned, 4> CPEs; |
Douglas Gregor | cabdd74 | 2009-12-19 07:05:23 +0000 | [diff] [blame] | 369 | SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {} |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 370 | }; |
| 371 | } |
| 372 | |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 373 | /// EmitConstantPool - Print to the current output stream assembly |
| 374 | /// representations of the constants in the constant pool MCP. This is |
| 375 | /// used to print out constants which have been "spilled to memory" by |
| 376 | /// the code generator. |
| 377 | /// |
| 378 | void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 379 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 380 | if (CP.empty()) return; |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 381 | |
Anton Korobeynikov | 088ae83 | 2008-09-24 22:17:59 +0000 | [diff] [blame] | 382 | // Calculate sections for constant pool entries. We collect entries to go into |
| 383 | // the same section together to reduce amount of section switch statements. |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 384 | SmallVector<SectionCPs, 4> CPSections; |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 385 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 386 | const MachineConstantPoolEntry &CPE = CP[i]; |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 387 | unsigned Align = CPE.getAlignment(); |
Chris Lattner | 5c2f789 | 2009-07-26 06:26:55 +0000 | [diff] [blame] | 388 | |
| 389 | SectionKind Kind; |
| 390 | switch (CPE.getRelocationInfo()) { |
| 391 | default: llvm_unreachable("Unknown section kind"); |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 392 | case 2: Kind = SectionKind::getReadOnlyWithRel(); break; |
Chris Lattner | 4c50922 | 2009-07-26 07:00:12 +0000 | [diff] [blame] | 393 | case 1: |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 394 | Kind = SectionKind::getReadOnlyWithRelLocal(); |
Chris Lattner | 4c50922 | 2009-07-26 07:00:12 +0000 | [diff] [blame] | 395 | break; |
Chris Lattner | 5c2f789 | 2009-07-26 06:26:55 +0000 | [diff] [blame] | 396 | case 0: |
Chris Lattner | 4c50922 | 2009-07-26 07:00:12 +0000 | [diff] [blame] | 397 | switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) { |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 398 | case 4: Kind = SectionKind::getMergeableConst4(); break; |
| 399 | case 8: Kind = SectionKind::getMergeableConst8(); break; |
| 400 | case 16: Kind = SectionKind::getMergeableConst16();break; |
| 401 | default: Kind = SectionKind::getMergeableConst(); break; |
Chris Lattner | 4c50922 | 2009-07-26 07:00:12 +0000 | [diff] [blame] | 402 | } |
Chris Lattner | 5c2f789 | 2009-07-26 06:26:55 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 405 | const MCSection *S = getObjFileLowering().getSectionForConstant(Kind); |
Chris Lattner | 298414e | 2009-07-22 00:28:43 +0000 | [diff] [blame] | 406 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 407 | // The number of sections are small, just do a linear search from the |
| 408 | // last section to the first. |
| 409 | bool Found = false; |
| 410 | unsigned SecIdx = CPSections.size(); |
| 411 | while (SecIdx != 0) { |
| 412 | if (CPSections[--SecIdx].S == S) { |
| 413 | Found = true; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | if (!Found) { |
| 418 | SecIdx = CPSections.size(); |
| 419 | CPSections.push_back(SectionCPs(S, Align)); |
| 420 | } |
| 421 | |
| 422 | if (Align > CPSections[SecIdx].Alignment) |
| 423 | CPSections[SecIdx].Alignment = Align; |
| 424 | CPSections[SecIdx].CPEs.push_back(i); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Anton Korobeynikov | 088ae83 | 2008-09-24 22:17:59 +0000 | [diff] [blame] | 427 | // Now print stuff into the calculated sections. |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 428 | for (unsigned i = 0, e = CPSections.size(); i != e; ++i) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 429 | OutStreamer.SwitchSection(CPSections[i].S); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 430 | EmitAlignment(Log2_32(CPSections[i].Alignment)); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 431 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 432 | unsigned Offset = 0; |
| 433 | for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) { |
| 434 | unsigned CPI = CPSections[i].CPEs[j]; |
| 435 | MachineConstantPoolEntry CPE = CP[CPI]; |
Anton Korobeynikov | 088ae83 | 2008-09-24 22:17:59 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 437 | // Emit inter-object padding for alignment. |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 438 | unsigned AlignMask = CPE.getAlignment() - 1; |
| 439 | unsigned NewOffset = (Offset + AlignMask) & ~AlignMask; |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 440 | OutStreamer.EmitFill(NewOffset - Offset, 0/*fillval*/, 0/*addrspace*/); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 441 | |
| 442 | const Type *Ty = CPE.getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 443 | Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 445 | O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' |
Dan Gohman | a12e9d7 | 2009-08-12 18:32:22 +0000 | [diff] [blame] | 446 | << CPI << ':'; |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 447 | if (VerboseAsm) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 448 | O.PadToColumn(MAI->getCommentColumn()); |
| 449 | O << MAI->getCommentString() << " constant "; |
Dan Gohman | cf20ac4 | 2009-08-13 01:36:44 +0000 | [diff] [blame] | 450 | WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent()); |
Anton Korobeynikov | 088ae83 | 2008-09-24 22:17:59 +0000 | [diff] [blame] | 451 | } |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 452 | O << '\n'; |
| 453 | if (CPE.isMachineConstantPoolEntry()) |
| 454 | EmitMachineConstantPoolValue(CPE.Val.MachineCPVal); |
| 455 | else |
| 456 | EmitGlobalConstant(CPE.Val.ConstVal); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 461 | /// EmitJumpTableInfo - Print assembly representations of the jump tables used |
| 462 | /// by the current function to the current output stream. |
| 463 | /// |
Chris Lattner | 1da31ee | 2006-10-05 03:01:21 +0000 | [diff] [blame] | 464 | void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI, |
| 465 | MachineFunction &MF) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 466 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 467 | if (JT.empty()) return; |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 468 | |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 469 | bool IsPic = TM.getRelocationModel() == Reloc::PIC_; |
Nate Begeman | 4d9bbdc | 2006-07-27 16:46:58 +0000 | [diff] [blame] | 470 | |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 471 | // Pick the directive to use to print the jump table entries, and switch to |
| 472 | // the appropriate section. |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 473 | TargetLowering *LoweringInfo = TM.getTargetLowering(); |
Anton Korobeynikov | 24287dd | 2006-12-19 21:04:20 +0000 | [diff] [blame] | 474 | |
Anton Korobeynikov | e87f52d | 2008-07-09 13:27:16 +0000 | [diff] [blame] | 475 | const Function *F = MF.getFunction(); |
Evan Cheng | b13bafe | 2009-06-18 20:37:15 +0000 | [diff] [blame] | 476 | bool JTInDiffSection = false; |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 477 | if (F->isWeakForLinker() || |
| 478 | (IsPic && !LoweringInfo->usesGlobalOffsetTable())) { |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 479 | // In PIC mode, we need to emit the jump table to the same section as the |
| 480 | // function body itself, otherwise the label differences won't make sense. |
Anton Korobeynikov | e87f52d | 2008-07-09 13:27:16 +0000 | [diff] [blame] | 481 | // We should also do if the section name is NULL or function is declared in |
| 482 | // discardable section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 483 | OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, |
| 484 | TM)); |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 485 | } else { |
Chris Lattner | 83d77fa | 2009-08-01 23:46:12 +0000 | [diff] [blame] | 486 | // Otherwise, drop it in the readonly section. |
| 487 | const MCSection *ReadOnlySection = |
Chris Lattner | 2798119 | 2009-08-01 23:57:16 +0000 | [diff] [blame] | 488 | getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly()); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 489 | OutStreamer.SwitchSection(ReadOnlySection); |
Evan Cheng | b13bafe | 2009-06-18 20:37:15 +0000 | [diff] [blame] | 490 | JTInDiffSection = true; |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 491 | } |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 492 | |
| 493 | EmitAlignment(Log2_32(MJTI->getAlignment())); |
Chris Lattner | 0c4e678 | 2006-07-15 01:34:12 +0000 | [diff] [blame] | 494 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 495 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 496 | const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs; |
Chris Lattner | 0737188 | 2006-10-28 18:10:06 +0000 | [diff] [blame] | 497 | |
| 498 | // If this jump table was deleted, ignore it. |
| 499 | if (JTBBs.empty()) continue; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 500 | |
| 501 | // For PIC codegen, if possible we want to use the SetDirective to reduce |
| 502 | // the number of relocations the assembler will generate for the jump table. |
| 503 | // Set directives are all printed before the jump table itself. |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 504 | SmallPtrSet<MachineBasicBlock*, 16> EmittedSets; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 505 | if (MAI->getSetDirective() && IsPic) |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 506 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 507 | if (EmittedSets.insert(JTBBs[ii])) |
| 508 | printPICJumpTableSetLabel(i, JTBBs[ii]); |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 7c30191 | 2009-09-13 19:02:16 +0000 | [diff] [blame] | 510 | // On some targets (e.g. Darwin) we want to emit two consequtive labels |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 511 | // before each jump table. The first label is never referenced, but tells |
| 512 | // the assembler and linker the extents of the jump table object. The |
| 513 | // second label is actually referenced by the code. |
Chris Lattner | 7c30191 | 2009-09-13 19:02:16 +0000 | [diff] [blame] | 514 | if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) { |
| 515 | O << MAI->getLinkerPrivateGlobalPrefix() |
| 516 | << "JTI" << getFunctionNumber() << '_' << i << ":\n"; |
Evan Cheng | b13bafe | 2009-06-18 20:37:15 +0000 | [diff] [blame] | 517 | } |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 519 | O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 520 | << '_' << i << ":\n"; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 521 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 522 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 523 | printPICJumpTableEntry(MJTI, JTBBs[ii], i); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 524 | O << '\n'; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 529 | void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, |
| 530 | const MachineBasicBlock *MBB, |
| 531 | unsigned uid) const { |
Chris Lattner | b3ac6e4 | 2009-08-11 20:29:57 +0000 | [diff] [blame] | 532 | bool isPIC = TM.getRelocationModel() == Reloc::PIC_; |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 533 | |
| 534 | // Use JumpTableDirective otherwise honor the entry size from the jump table |
| 535 | // info. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 536 | const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC); |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 537 | bool HadJTEntryDirective = JTEntryDirective != NULL; |
| 538 | if (!HadJTEntryDirective) { |
| 539 | JTEntryDirective = MJTI->getEntrySize() == 4 ? |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 540 | MAI->getData32bitsDirective() : MAI->getData64bitsDirective(); |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | O << JTEntryDirective << ' '; |
| 544 | |
| 545 | // If we have emitted set directives for the jump table entries, print |
| 546 | // them rather than the entries themselves. If we're emitting PIC, then |
| 547 | // emit the table entries as differences between two text section labels. |
| 548 | // If we're emitting non-PIC code, then emit the entries as direct |
| 549 | // references to the target basic blocks. |
Chris Lattner | b3ac6e4 | 2009-08-11 20:29:57 +0000 | [diff] [blame] | 550 | if (!isPIC) { |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 551 | O << *GetMBBSymbol(MBB->getNumber()); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 552 | } else if (MAI->getSetDirective()) { |
| 553 | O << MAI->getPrivateGlobalPrefix() << getFunctionNumber() |
Chris Lattner | b3ac6e4 | 2009-08-11 20:29:57 +0000 | [diff] [blame] | 554 | << '_' << uid << "_set_" << MBB->getNumber(); |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 555 | } else { |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 556 | O << *GetMBBSymbol(MBB->getNumber()); |
Chris Lattner | b3ac6e4 | 2009-08-11 20:29:57 +0000 | [diff] [blame] | 557 | // If the arch uses custom Jump Table directives, don't calc relative to |
| 558 | // JT |
| 559 | if (!HadJTEntryDirective) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 560 | O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" |
Chris Lattner | b3ac6e4 | 2009-08-11 20:29:57 +0000 | [diff] [blame] | 561 | << getFunctionNumber() << '_' << uid; |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 566 | /// EmitSpecialLLVMGlobal - Check to see if the specified global is a |
| 567 | /// special global used by LLVM. If so, emit it and return true, otherwise |
| 568 | /// do nothing and return false. |
| 569 | bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 570 | if (GV->getName() == "llvm.used") { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 571 | if (MAI->getUsedDirective() != 0) // No need to emit this at all. |
Andrew Lenharth | b753a9b | 2007-08-22 19:33:11 +0000 | [diff] [blame] | 572 | EmitLLVMUsedList(GV->getInitializer()); |
| 573 | return true; |
| 574 | } |
| 575 | |
Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 576 | // Ignore debug and non-emitted data. This handles llvm.compiler.used. |
Chris Lattner | 266c7bb | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 577 | if (GV->getSection() == "llvm.metadata" || |
| 578 | GV->hasAvailableExternallyLinkage()) |
| 579 | return true; |
Jim Laskey | 7809811 | 2006-03-07 22:00:35 +0000 | [diff] [blame] | 580 | |
| 581 | if (!GV->hasAppendingLinkage()) return false; |
| 582 | |
| 583 | assert(GV->hasInitializer() && "Not a special LLVM global!"); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 584 | |
Evan Cheng | 916d07c | 2007-06-04 20:39:18 +0000 | [diff] [blame] | 585 | const TargetData *TD = TM.getTargetData(); |
| 586 | unsigned Align = Log2_32(TD->getPointerPrefAlignment()); |
Chris Lattner | f231c07 | 2009-03-09 05:52:15 +0000 | [diff] [blame] | 587 | if (GV->getName() == "llvm.global_ctors") { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 588 | OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection()); |
Chris Lattner | ea3a9ff | 2009-03-09 08:18:48 +0000 | [diff] [blame] | 589 | EmitAlignment(Align, 0); |
| 590 | EmitXXStructorList(GV->getInitializer()); |
Chris Lattner | 71eae71 | 2010-01-19 04:34:02 +0000 | [diff] [blame] | 591 | |
| 592 | if (TM.getRelocationModel() == Reloc::Static && |
| 593 | MAI->hasStaticCtorDtorReferenceInStaticMode()) |
| 594 | O << ".reference .constructors_used\n"; |
Chris Lattner | ea3a9ff | 2009-03-09 08:18:48 +0000 | [diff] [blame] | 595 | return true; |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Chris Lattner | f231c07 | 2009-03-09 05:52:15 +0000 | [diff] [blame] | 598 | if (GV->getName() == "llvm.global_dtors") { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 599 | OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection()); |
Chris Lattner | ea3a9ff | 2009-03-09 08:18:48 +0000 | [diff] [blame] | 600 | EmitAlignment(Align, 0); |
| 601 | EmitXXStructorList(GV->getInitializer()); |
Chris Lattner | 71eae71 | 2010-01-19 04:34:02 +0000 | [diff] [blame] | 602 | |
| 603 | if (TM.getRelocationModel() == Reloc::Static && |
| 604 | MAI->hasStaticCtorDtorReferenceInStaticMode()) |
| 605 | O << ".reference .destructors_used\n"; |
Chris Lattner | ea3a9ff | 2009-03-09 08:18:48 +0000 | [diff] [blame] | 606 | return true; |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | return false; |
| 610 | } |
| 611 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 612 | /// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each |
Dale Johannesen | d2e51af | 2008-09-09 22:29:13 +0000 | [diff] [blame] | 613 | /// global in the specified llvm.used list for which emitUsedDirectiveFor |
| 614 | /// is true, as being used with this directive. |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 615 | void AsmPrinter::EmitLLVMUsedList(Constant *List) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 616 | const char *Directive = MAI->getUsedDirective(); |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 617 | |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 618 | // Should be an array of 'i8*'. |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 619 | ConstantArray *InitList = dyn_cast<ConstantArray>(List); |
| 620 | if (InitList == 0) return; |
| 621 | |
| 622 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
Chris Lattner | 16fe990 | 2009-07-17 22:00:23 +0000 | [diff] [blame] | 623 | const GlobalValue *GV = |
| 624 | dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts()); |
Chris Lattner | 26630c1 | 2009-07-31 20:52:39 +0000 | [diff] [blame] | 625 | if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) { |
Dale Johannesen | 61e6093 | 2008-09-03 20:34:58 +0000 | [diff] [blame] | 626 | O << Directive; |
| 627 | EmitConstantValueOnly(InitList->getOperand(i)); |
| 628 | O << '\n'; |
| 629 | } |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 633 | /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the |
| 634 | /// function pointers, ignoring the init priority. |
| 635 | void AsmPrinter::EmitXXStructorList(Constant *List) { |
| 636 | // Should be an array of '{ int, void ()* }' structs. The first value is the |
| 637 | // init priority, which we ignore. |
| 638 | if (!isa<ConstantArray>(List)) return; |
| 639 | ConstantArray *InitList = cast<ConstantArray>(List); |
| 640 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 641 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 642 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
Chris Lattner | 8de324b | 2005-12-21 01:17:37 +0000 | [diff] [blame] | 643 | |
| 644 | if (CS->getOperand(1)->isNullValue()) |
| 645 | return; // Found a null terminator, exit printing. |
| 646 | // Emit the function pointer. |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 647 | EmitGlobalConstant(CS->getOperand(1)); |
| 648 | } |
| 649 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 650 | |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 651 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 652 | //===----------------------------------------------------------------------===// |
| 653 | /// LEB 128 number encoding. |
| 654 | |
Bob Wilson | c0ef244 | 2009-11-06 22:38:38 +0000 | [diff] [blame] | 655 | /// PrintULEB128 - Print a series of hexadecimal values (separated by commas) |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 656 | /// representing an unsigned leb128 value. |
| 657 | void AsmPrinter::PrintULEB128(unsigned Value) const { |
| 658 | do { |
Chris Lattner | a64f463 | 2008-11-10 04:35:24 +0000 | [diff] [blame] | 659 | unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 660 | Value >>= 7; |
| 661 | if (Value) Byte |= 0x80; |
Benjamin Kramer | 1efd4fd5 | 2010-01-17 07:46:39 +0000 | [diff] [blame] | 662 | PrintHex(Byte); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 663 | if (Value) O << ", "; |
| 664 | } while (Value); |
| 665 | } |
| 666 | |
Bob Wilson | c0ef244 | 2009-11-06 22:38:38 +0000 | [diff] [blame] | 667 | /// PrintSLEB128 - Print a series of hexadecimal values (separated by commas) |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 668 | /// representing a signed leb128 value. |
| 669 | void AsmPrinter::PrintSLEB128(int Value) const { |
| 670 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 671 | bool IsMore; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 672 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 673 | do { |
Chris Lattner | a64f463 | 2008-11-10 04:35:24 +0000 | [diff] [blame] | 674 | unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 675 | Value >>= 7; |
| 676 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 677 | if (IsMore) Byte |= 0x80; |
Benjamin Kramer | 1efd4fd5 | 2010-01-17 07:46:39 +0000 | [diff] [blame] | 678 | PrintHex(Byte); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 679 | if (IsMore) O << ", "; |
| 680 | } while (IsMore); |
| 681 | } |
| 682 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 683 | //===--------------------------------------------------------------------===// |
| 684 | // Emission and print routines |
| 685 | // |
| 686 | |
Bob Wilson | c0ef244 | 2009-11-06 22:38:38 +0000 | [diff] [blame] | 687 | /// PrintHex - Print a value as a hexadecimal value. |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 688 | /// |
Benjamin Kramer | 1efd4fd5 | 2010-01-17 07:46:39 +0000 | [diff] [blame] | 689 | void AsmPrinter::PrintHex(uint64_t Value) const { |
| 690 | O << "0x"; |
| 691 | O.write_hex(Value); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /// EOL - Print a newline character to asm stream. If a comment is present |
| 695 | /// then it will be printed first. Comments should not contain '\n'. |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 696 | void AsmPrinter::EOL() const { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 697 | O << '\n'; |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 698 | } |
Owen Anderson | 2599509 | 2008-07-01 21:16:27 +0000 | [diff] [blame] | 699 | |
Benjamin Kramer | 1efd4fd5 | 2010-01-17 07:46:39 +0000 | [diff] [blame] | 700 | void AsmPrinter::EOL(const Twine &Comment) const { |
| 701 | if (VerboseAsm && !Comment.isTriviallyEmpty()) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 702 | O.PadToColumn(MAI->getCommentColumn()); |
Chris Lattner | 043c4e5 | 2010-01-20 07:19:19 +0000 | [diff] [blame] | 703 | O << MAI->getCommentString() << ' ' << Comment; |
Owen Anderson | 2599509 | 2008-07-01 21:16:27 +0000 | [diff] [blame] | 704 | } |
| 705 | O << '\n'; |
| 706 | } |
| 707 | |
Bill Wendling | b5abecd | 2009-08-29 12:17:53 +0000 | [diff] [blame] | 708 | static const char *DecodeDWARFEncoding(unsigned Encoding) { |
| 709 | switch (Encoding) { |
| 710 | case dwarf::DW_EH_PE_absptr: |
| 711 | return "absptr"; |
| 712 | case dwarf::DW_EH_PE_omit: |
| 713 | return "omit"; |
| 714 | case dwarf::DW_EH_PE_pcrel: |
| 715 | return "pcrel"; |
Bill Wendling | 0734d35 | 2009-09-09 21:26:19 +0000 | [diff] [blame] | 716 | case dwarf::DW_EH_PE_udata4: |
| 717 | return "udata4"; |
| 718 | case dwarf::DW_EH_PE_udata8: |
| 719 | return "udata8"; |
| 720 | case dwarf::DW_EH_PE_sdata4: |
| 721 | return "sdata4"; |
| 722 | case dwarf::DW_EH_PE_sdata8: |
| 723 | return "sdata8"; |
Bill Wendling | b5abecd | 2009-08-29 12:17:53 +0000 | [diff] [blame] | 724 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: |
| 725 | return "pcrel udata4"; |
| 726 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: |
| 727 | return "pcrel sdata4"; |
| 728 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: |
| 729 | return "pcrel udata8"; |
| 730 | case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: |
| 731 | return "pcrel sdata8"; |
| 732 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4: |
| 733 | return "indirect pcrel udata4"; |
| 734 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4: |
| 735 | return "indirect pcrel sdata4"; |
| 736 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8: |
| 737 | return "indirect pcrel udata8"; |
| 738 | case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8: |
| 739 | return "indirect pcrel sdata8"; |
| 740 | } |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
Benjamin Kramer | 1efd4fd5 | 2010-01-17 07:46:39 +0000 | [diff] [blame] | 745 | void AsmPrinter::EOL(const Twine &Comment, unsigned Encoding) const { |
| 746 | if (VerboseAsm && !Comment.isTriviallyEmpty()) { |
Bill Wendling | b5abecd | 2009-08-29 12:17:53 +0000 | [diff] [blame] | 747 | O.PadToColumn(MAI->getCommentColumn()); |
| 748 | O << MAI->getCommentString() |
| 749 | << ' ' |
| 750 | << Comment; |
| 751 | |
| 752 | if (const char *EncStr = DecodeDWARFEncoding(Encoding)) |
| 753 | O << " (" << EncStr << ')'; |
| 754 | } |
| 755 | O << '\n'; |
| 756 | } |
| 757 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 758 | /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an |
| 759 | /// unsigned leb128 value. |
| 760 | void AsmPrinter::EmitULEB128Bytes(unsigned Value) const { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 761 | if (MAI->hasLEB128()) { |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 762 | O << "\t.uleb128\t" |
| 763 | << Value; |
| 764 | } else { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 765 | O << MAI->getData8bitsDirective(); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 766 | PrintULEB128(Value); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /// EmitSLEB128Bytes - print an assembler byte data directive to compose a |
| 771 | /// signed leb128 value. |
| 772 | void AsmPrinter::EmitSLEB128Bytes(int Value) const { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 773 | if (MAI->hasLEB128()) { |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 774 | O << "\t.sleb128\t" |
| 775 | << Value; |
| 776 | } else { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 777 | O << MAI->getData8bitsDirective(); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 778 | PrintSLEB128(Value); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /// EmitInt8 - Emit a byte directive and value. |
| 783 | /// |
| 784 | void AsmPrinter::EmitInt8(int Value) const { |
Chris Lattner | 5eaa54e | 2010-01-20 06:45:39 +0000 | [diff] [blame] | 785 | OutStreamer.EmitIntValue(Value, 1, 0/*addrspace*/); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /// EmitInt16 - Emit a short directive and value. |
| 789 | /// |
| 790 | void AsmPrinter::EmitInt16(int Value) const { |
Chris Lattner | 5eaa54e | 2010-01-20 06:45:39 +0000 | [diff] [blame] | 791 | OutStreamer.EmitIntValue(Value, 2, 0/*addrspace*/); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /// EmitInt32 - Emit a long directive and value. |
| 795 | /// |
| 796 | void AsmPrinter::EmitInt32(int Value) const { |
Chris Lattner | 5eaa54e | 2010-01-20 06:45:39 +0000 | [diff] [blame] | 797 | OutStreamer.EmitIntValue(Value, 4, 0/*addrspace*/); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /// EmitInt64 - Emit a long long directive and value. |
| 801 | /// |
| 802 | void AsmPrinter::EmitInt64(uint64_t Value) const { |
Chris Lattner | 5eaa54e | 2010-01-20 06:45:39 +0000 | [diff] [blame] | 803 | OutStreamer.EmitIntValue(Value, 8, 0/*addrspace*/); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 807 | /// |
| 808 | static inline char toOctal(int X) { |
| 809 | return (X&7)+'0'; |
| 810 | } |
| 811 | |
| 812 | /// printStringChar - Print a char, escaped if necessary. |
| 813 | /// |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 814 | static void printStringChar(formatted_raw_ostream &O, unsigned char C) { |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 815 | if (C == '"') { |
| 816 | O << "\\\""; |
| 817 | } else if (C == '\\') { |
| 818 | O << "\\\\"; |
Chris Lattner | bbfa244 | 2009-01-22 23:38:45 +0000 | [diff] [blame] | 819 | } else if (isprint((unsigned char)C)) { |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 820 | O << C; |
| 821 | } else { |
| 822 | switch(C) { |
| 823 | case '\b': O << "\\b"; break; |
| 824 | case '\f': O << "\\f"; break; |
| 825 | case '\n': O << "\\n"; break; |
| 826 | case '\r': O << "\\r"; break; |
| 827 | case '\t': O << "\\t"; break; |
| 828 | default: |
| 829 | O << '\\'; |
| 830 | O << toOctal(C >> 6); |
| 831 | O << toOctal(C >> 3); |
| 832 | O << toOctal(C >> 0); |
| 833 | break; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /// EmitString - Emit a string with quotes and a null terminator. |
| 839 | /// Special characters are emitted properly. |
| 840 | /// \literal (Eg. '\t') \endliteral |
Devang Patel | e9a0597 | 2009-11-24 19:42:17 +0000 | [diff] [blame] | 841 | void AsmPrinter::EmitString(const StringRef String) const { |
Dan Gohman | 24f8e29 | 2009-11-13 21:55:31 +0000 | [diff] [blame] | 842 | EmitString(String.data(), String.size()); |
Bill Wendling | f34be82 | 2009-04-09 23:51:31 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | void AsmPrinter::EmitString(const char *String, unsigned Size) const { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 846 | const char* AscizDirective = MAI->getAscizDirective(); |
Anton Korobeynikov | fb269cf | 2007-03-06 19:25:02 +0000 | [diff] [blame] | 847 | if (AscizDirective) |
| 848 | O << AscizDirective; |
| 849 | else |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 850 | O << MAI->getAsciiDirective(); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 851 | O << '\"'; |
Bill Wendling | f34be82 | 2009-04-09 23:51:31 +0000 | [diff] [blame] | 852 | for (unsigned i = 0; i < Size; ++i) |
Chris Lattner | a118c2e | 2009-04-08 00:28:38 +0000 | [diff] [blame] | 853 | printStringChar(O, String[i]); |
Anton Korobeynikov | fb269cf | 2007-03-06 19:25:02 +0000 | [diff] [blame] | 854 | if (AscizDirective) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 855 | O << '\"'; |
Anton Korobeynikov | fb269cf | 2007-03-06 19:25:02 +0000 | [diff] [blame] | 856 | else |
| 857 | O << "\\0\""; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | |
Dan Gohman | 189f80d | 2007-09-24 20:58:13 +0000 | [diff] [blame] | 861 | /// EmitFile - Emit a .file directive. |
Benjamin Kramer | 1efd4fd5 | 2010-01-17 07:46:39 +0000 | [diff] [blame] | 862 | void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const { |
Dan Gohman | 189f80d | 2007-09-24 20:58:13 +0000 | [diff] [blame] | 863 | O << "\t.file\t" << Number << " \""; |
Chris Lattner | a118c2e | 2009-04-08 00:28:38 +0000 | [diff] [blame] | 864 | for (unsigned i = 0, N = Name.size(); i < N; ++i) |
| 865 | printStringChar(O, Name[i]); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 866 | O << '\"'; |
Dan Gohman | 189f80d | 2007-09-24 20:58:13 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 870 | //===----------------------------------------------------------------------===// |
| 871 | |
Chris Lattner | 3a42053 | 2007-05-31 18:57:45 +0000 | [diff] [blame] | 872 | // EmitAlignment - Emit an alignment directive to the specified power of |
| 873 | // two boundary. For example, if you pass in 3 here, you will get an 8 |
| 874 | // byte alignment. If a global value is specified, and if that global has |
| 875 | // an explicit alignment requested, it will unconditionally override the |
| 876 | // alignment request. However, if ForcedAlignBits is specified, this value |
| 877 | // has final say: the ultimate alignment will be the max of ForcedAlignBits |
| 878 | // and the alignment computed with NumBits and the global. |
| 879 | // |
| 880 | // The algorithm is: |
| 881 | // Align = NumBits; |
| 882 | // if (GV && GV->hasalignment) Align = GV->getalignment(); |
| 883 | // Align = std::max(Align, ForcedAlignBits); |
| 884 | // |
| 885 | void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV, |
Evan Cheng | 05548eb | 2008-02-29 19:36:59 +0000 | [diff] [blame] | 886 | unsigned ForcedAlignBits, |
| 887 | bool UseFillExpr) const { |
Dale Johannesen | 00d56b9 | 2007-04-23 23:33:31 +0000 | [diff] [blame] | 888 | if (GV && GV->getAlignment()) |
Chris Lattner | 3a42053 | 2007-05-31 18:57:45 +0000 | [diff] [blame] | 889 | NumBits = Log2_32(GV->getAlignment()); |
| 890 | NumBits = std::max(NumBits, ForcedAlignBits); |
| 891 | |
Chris Lattner | 2a21c6e | 2005-11-10 18:09:27 +0000 | [diff] [blame] | 892 | if (NumBits == 0) return; // No need to emit alignment. |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 893 | |
| 894 | unsigned FillValue = 0; |
Chris Lattner | dabf07c | 2009-08-18 06:15:16 +0000 | [diff] [blame] | 895 | if (getCurrentSection()->getKind().isText()) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 896 | FillValue = MAI->getTextAlignFillValue(); |
Chris Lattner | 663c2d2 | 2009-08-19 06:12:02 +0000 | [diff] [blame] | 897 | |
| 898 | OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0); |
Chris Lattner | bfddc20 | 2004-08-17 19:14:29 +0000 | [diff] [blame] | 899 | } |
David Greene | a5bb59f | 2009-08-05 21:00:52 +0000 | [diff] [blame] | 900 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 901 | // Print out the specified constant, without a storage class. Only the |
| 902 | // constants valid in constant expressions can occur here. |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 903 | void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { |
Chris Lattner | fe0e7ed | 2010-01-13 04:29:19 +0000 | [diff] [blame] | 904 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 905 | O << '0'; |
Chris Lattner | fe0e7ed | 2010-01-13 04:29:19 +0000 | [diff] [blame] | 906 | return; |
| 907 | } |
| 908 | |
| 909 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Scott Michel | 4315eee | 2008-06-03 06:18:19 +0000 | [diff] [blame] | 910 | O << CI->getZExtValue(); |
Chris Lattner | fe0e7ed | 2010-01-13 04:29:19 +0000 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | |
| 914 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 915 | // This is a constant address for a global variable or function. Use the |
Chris Lattner | bfd1e50 | 2009-09-15 23:11:32 +0000 | [diff] [blame] | 916 | // name of the variable or function as the address value. |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 917 | O << *GetGlobalValueSymbol(GV); |
Chris Lattner | fe0e7ed | 2010-01-13 04:29:19 +0000 | [diff] [blame] | 918 | return; |
| 919 | } |
| 920 | |
| 921 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) { |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 922 | O << *GetBlockAddressSymbol(BA); |
Chris Lattner | fe0e7ed | 2010-01-13 04:29:19 +0000 | [diff] [blame] | 923 | return; |
| 924 | } |
| 925 | |
| 926 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV); |
| 927 | if (CE == 0) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 928 | llvm_unreachable("Unknown constant value!"); |
Chris Lattner | fe0e7ed | 2010-01-13 04:29:19 +0000 | [diff] [blame] | 929 | O << '0'; |
| 930 | return; |
| 931 | } |
| 932 | |
| 933 | switch (CE->getOpcode()) { |
| 934 | case Instruction::ZExt: |
| 935 | case Instruction::SExt: |
| 936 | case Instruction::FPTrunc: |
| 937 | case Instruction::FPExt: |
| 938 | case Instruction::UIToFP: |
| 939 | case Instruction::SIToFP: |
| 940 | case Instruction::FPToUI: |
| 941 | case Instruction::FPToSI: |
| 942 | default: |
| 943 | llvm_unreachable("FIXME: Don't support this constant cast expr"); |
| 944 | case Instruction::GetElementPtr: { |
| 945 | // generate a symbolic expression for the byte address |
| 946 | const TargetData *TD = TM.getTargetData(); |
| 947 | const Constant *ptrVal = CE->getOperand(0); |
| 948 | SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end()); |
| 949 | int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], |
| 950 | idxVec.size()); |
| 951 | if (Offset == 0) |
| 952 | return EmitConstantValueOnly(ptrVal); |
| 953 | |
| 954 | // Truncate/sext the offset to the pointer size. |
| 955 | if (TD->getPointerSizeInBits() != 64) { |
| 956 | int SExtAmount = 64-TD->getPointerSizeInBits(); |
| 957 | Offset = (Offset << SExtAmount) >> SExtAmount; |
| 958 | } |
| 959 | |
| 960 | if (Offset) |
| 961 | O << '('; |
| 962 | EmitConstantValueOnly(ptrVal); |
| 963 | if (Offset > 0) |
| 964 | O << ") + " << Offset; |
| 965 | else |
| 966 | O << ") - " << -Offset; |
| 967 | return; |
| 968 | } |
| 969 | case Instruction::BitCast: |
| 970 | return EmitConstantValueOnly(CE->getOperand(0)); |
| 971 | |
| 972 | case Instruction::IntToPtr: { |
| 973 | // Handle casts to pointers by changing them into casts to the appropriate |
| 974 | // integer type. This promotes constant folding and simplifies this code. |
| 975 | const TargetData *TD = TM.getTargetData(); |
| 976 | Constant *Op = CE->getOperand(0); |
| 977 | Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()), |
| 978 | false/*ZExt*/); |
| 979 | return EmitConstantValueOnly(Op); |
| 980 | } |
| 981 | |
| 982 | case Instruction::PtrToInt: { |
| 983 | // Support only foldable casts to/from pointers that can be eliminated by |
| 984 | // changing the pointer to the appropriately sized integer type. |
| 985 | Constant *Op = CE->getOperand(0); |
| 986 | const Type *Ty = CE->getType(); |
| 987 | const TargetData *TD = TM.getTargetData(); |
| 988 | |
| 989 | // We can emit the pointer value into this slot if the slot is an |
| 990 | // integer slot greater or equal to the size of the pointer. |
| 991 | if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType())) |
| 992 | return EmitConstantValueOnly(Op); |
| 993 | |
| 994 | O << "(("; |
| 995 | EmitConstantValueOnly(Op); |
| 996 | APInt ptrMask = |
| 997 | APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType())); |
| 998 | |
| 999 | SmallString<40> S; |
| 1000 | ptrMask.toStringUnsigned(S); |
| 1001 | O << ") & " << S.str() << ')'; |
| 1002 | return; |
| 1003 | } |
| 1004 | |
| 1005 | case Instruction::Trunc: |
| 1006 | // We emit the value and depend on the assembler to truncate the generated |
| 1007 | // expression properly. This is important for differences between |
| 1008 | // blockaddress labels. Since the two labels are in the same function, it |
| 1009 | // is reasonable to treat their delta as a 32-bit value. |
| 1010 | return EmitConstantValueOnly(CE->getOperand(0)); |
| 1011 | |
| 1012 | case Instruction::Add: |
| 1013 | case Instruction::Sub: |
| 1014 | case Instruction::And: |
| 1015 | case Instruction::Or: |
| 1016 | case Instruction::Xor: |
| 1017 | O << '('; |
| 1018 | EmitConstantValueOnly(CE->getOperand(0)); |
| 1019 | O << ')'; |
| 1020 | switch (CE->getOpcode()) { |
| 1021 | case Instruction::Add: |
| 1022 | O << " + "; |
| 1023 | break; |
| 1024 | case Instruction::Sub: |
| 1025 | O << " - "; |
| 1026 | break; |
| 1027 | case Instruction::And: |
| 1028 | O << " & "; |
| 1029 | break; |
| 1030 | case Instruction::Or: |
| 1031 | O << " | "; |
| 1032 | break; |
| 1033 | case Instruction::Xor: |
| 1034 | O << " ^ "; |
| 1035 | break; |
| 1036 | default: |
| 1037 | break; |
| 1038 | } |
| 1039 | O << '('; |
| 1040 | EmitConstantValueOnly(CE->getOperand(1)); |
| 1041 | O << ')'; |
| 1042 | break; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 1043 | } |
| 1044 | } |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 1046 | /// printAsCString - Print the specified array as a C compatible string, only if |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1047 | /// the predicate isString is true. |
| 1048 | /// |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 1049 | static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA, |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 1050 | unsigned LastElt) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1051 | assert(CVA->isString() && "Array is not string compatible!"); |
| 1052 | |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1053 | O << '\"'; |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 1054 | for (unsigned i = 0; i != LastElt; ++i) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1055 | unsigned char C = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1056 | (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue(); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 1057 | printStringChar(O, C); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1058 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1059 | O << '\"'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 1062 | /// EmitString - Emit a zero-byte-terminated string constant. |
| 1063 | /// |
| 1064 | void AsmPrinter::EmitString(const ConstantArray *CVA) const { |
| 1065 | unsigned NumElts = CVA->getNumOperands(); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1066 | if (MAI->getAscizDirective() && NumElts && |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1067 | cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1068 | O << MAI->getAscizDirective(); |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 1069 | printAsCString(O, CVA, NumElts-1); |
| 1070 | } else { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1071 | O << MAI->getAsciiDirective(); |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 1072 | printAsCString(O, CVA, NumElts); |
| 1073 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1074 | O << '\n'; |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1077 | static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace, |
| 1078 | AsmPrinter &AP) { |
| 1079 | if (AddrSpace == 0 && CA->isString()) { |
| 1080 | AP.EmitString(CA); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1081 | } else { // Not a string. Print the values in successive locations |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1082 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 1083 | AP.EmitGlobalConstant(CA->getOperand(i), AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1087 | static void EmitGlobalConstantVector(const ConstantVector *CV, |
| 1088 | unsigned AddrSpace, AsmPrinter &AP) { |
| 1089 | const VectorType *VTy = CV->getType(); |
| 1090 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) |
| 1091 | AP.EmitGlobalConstant(CV->getOperand(i), AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1094 | static void EmitGlobalConstantStruct(const ConstantStruct *CS, |
| 1095 | unsigned AddrSpace, AsmPrinter &AP) { |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1096 | // Print the fields in successive locations. Pad to align if needed! |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1097 | const TargetData *TD = AP.TM.getTargetData(); |
| 1098 | unsigned Size = TD->getTypeAllocSize(CS->getType()); |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1099 | const StructLayout *Layout = TD->getStructLayout(CS->getType()); |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1100 | uint64_t SizeSoFar = 0; |
| 1101 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) { |
| 1102 | const Constant *field = CS->getOperand(i); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1103 | |
| 1104 | // Check if padding is needed and insert one or more 0s. |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1105 | uint64_t FieldSize = TD->getTypeAllocSize(field->getType()); |
| 1106 | uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1)) |
| 1107 | - Layout->getElementOffset(i)) - FieldSize; |
| 1108 | SizeSoFar += FieldSize + PadSize; |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Now print the actual field value. |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1111 | AP.EmitGlobalConstant(field, AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Insert padding - this may include padding to increase the size of the |
| 1114 | // current field up to the ABI size (if the struct is not packed) as well |
| 1115 | // as padding to ensure that the next field starts at the right offset. |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1116 | AP.OutStreamer.EmitZeros(PadSize, AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1117 | } |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1118 | assert(SizeSoFar == Layout->getSizeInBytes() && |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1119 | "Layout of constant struct may be incorrect!"); |
| 1120 | } |
| 1121 | |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1122 | static void EmitGlobalConstantFP(const ConstantFP *CFP, unsigned AddrSpace, |
| 1123 | AsmPrinter &AP) { |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1124 | // FP Constants are printed as integer constants to avoid losing |
Chris Lattner | 9ceff94 | 2010-01-20 06:53:37 +0000 | [diff] [blame] | 1125 | // precision. |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1126 | if (CFP->getType()->isDoubleTy()) { |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1127 | if (AP.VerboseAsm) { |
Chris Lattner | 09ce674 | 2010-01-19 22:16:33 +0000 | [diff] [blame] | 1128 | double Val = CFP->getValueAPF().convertToDouble(); // for comment only |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1129 | AP.O.PadToColumn(AP.MAI->getCommentColumn()); |
| 1130 | AP.O << AP.MAI->getCommentString() << " double " << Val << '\n'; |
Chris Lattner | 09ce674 | 2010-01-19 22:16:33 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1133 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 1134 | AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1135 | return; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | if (CFP->getType()->isFloatTy()) { |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1139 | if (AP.VerboseAsm) { |
Chris Lattner | 09ce674 | 2010-01-19 22:16:33 +0000 | [diff] [blame] | 1140 | float Val = CFP->getValueAPF().convertToFloat(); // for comment only |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1141 | AP.O.PadToColumn(AP.MAI->getCommentColumn()); |
| 1142 | AP.O << AP.MAI->getCommentString() << " float " << Val << '\n'; |
Dan Gohman | a12e9d7 | 2009-08-12 18:32:22 +0000 | [diff] [blame] | 1143 | } |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1144 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 1145 | AP.OutStreamer.EmitIntValue(Val, 4, AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1146 | return; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | if (CFP->getType()->isX86_FP80Ty()) { |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1150 | // all long double variants are printed as hex |
| 1151 | // api needed to prevent premature destruction |
Chris Lattner | 09ce674 | 2010-01-19 22:16:33 +0000 | [diff] [blame] | 1152 | APInt API = CFP->getValueAPF().bitcastToAPInt(); |
| 1153 | const uint64_t *p = API.getRawData(); |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1154 | if (AP.VerboseAsm) { |
Chris Lattner | 72b5ebc | 2010-01-19 22:11:05 +0000 | [diff] [blame] | 1155 | // Convert to double so we can print the approximate val as a comment. |
| 1156 | APFloat DoubleVal = CFP->getValueAPF(); |
| 1157 | bool ignored; |
| 1158 | DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, |
| 1159 | &ignored); |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1160 | AP.O.PadToColumn(AP.MAI->getCommentColumn()); |
| 1161 | AP.O << AP.MAI->getCommentString() << " x86_fp80 ~= " |
| 1162 | << DoubleVal.convertToDouble() << '\n'; |
Chris Lattner | 72b5ebc | 2010-01-19 22:11:05 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1165 | if (AP.TM.getTargetData()->isBigEndian()) { |
| 1166 | AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace); |
| 1167 | AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace); |
Chris Lattner | 72b5ebc | 2010-01-19 22:11:05 +0000 | [diff] [blame] | 1168 | } else { |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1169 | AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace); |
| 1170 | AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1171 | } |
Chris Lattner | 9ceff94 | 2010-01-20 06:53:37 +0000 | [diff] [blame] | 1172 | |
| 1173 | // Emit the tail padding for the long double. |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1174 | const TargetData &TD = *AP.TM.getTargetData(); |
| 1175 | AP.OutStreamer.EmitZeros(TD.getTypeAllocSize(CFP->getType()) - |
| 1176 | TD.getTypeStoreSize(CFP->getType()), AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1177 | return; |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Chris Lattner | 09ce674 | 2010-01-19 22:16:33 +0000 | [diff] [blame] | 1180 | assert(CFP->getType()->isPPC_FP128Ty() && |
| 1181 | "Floating point constant type not handled"); |
| 1182 | // All long double variants are printed as hex api needed to prevent |
| 1183 | // premature destruction. |
| 1184 | APInt API = CFP->getValueAPF().bitcastToAPInt(); |
| 1185 | const uint64_t *p = API.getRawData(); |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1186 | if (AP.TM.getTargetData()->isBigEndian()) { |
| 1187 | AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace); |
| 1188 | AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace); |
Chris Lattner | 9ceff94 | 2010-01-20 06:53:37 +0000 | [diff] [blame] | 1189 | } else { |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1190 | AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace); |
| 1191 | AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace); |
Chris Lattner | 09ce674 | 2010-01-19 22:16:33 +0000 | [diff] [blame] | 1192 | } |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1195 | static void EmitGlobalConstantLargeInt(const ConstantInt *CI, |
| 1196 | unsigned AddrSpace, AsmPrinter &AP) { |
| 1197 | const TargetData *TD = AP.TM.getTargetData(); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1198 | unsigned BitWidth = CI->getBitWidth(); |
Chris Lattner | 38c2b0a | 2010-01-13 04:39:46 +0000 | [diff] [blame] | 1199 | assert((BitWidth & 63) == 0 && "only support multiples of 64-bits"); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1200 | |
| 1201 | // We don't expect assemblers to support integer data directives |
| 1202 | // for more than 64 bits, so we emit the data in at most 64-bit |
| 1203 | // quantities at a time. |
| 1204 | const uint64_t *RawData = CI->getValue().getRawData(); |
| 1205 | for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) { |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1206 | uint64_t Val = TD->isBigEndian() ? RawData[e - i - 1] : RawData[i]; |
| 1207 | AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace); |
Dan Gohman | 00d448a | 2008-12-22 21:14:27 +0000 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 1211 | /// EmitGlobalConstant - Print a general LLVM constant to the .s file. |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1212 | void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) { |
Chris Lattner | 043c4e5 | 2010-01-20 07:19:19 +0000 | [diff] [blame] | 1213 | if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV)) { |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1214 | uint64_t Size = TM.getTargetData()->getTypeAllocSize(CV->getType()); |
Chris Lattner | 6449abf | 2010-01-19 21:51:22 +0000 | [diff] [blame] | 1215 | return OutStreamer.EmitZeros(Size, AddrSpace); |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 1219 | unsigned Size = TM.getTargetData()->getTypeAllocSize(CV->getType()); |
| 1220 | switch (Size) { |
| 1221 | case 1: |
| 1222 | case 2: |
| 1223 | case 4: |
| 1224 | case 8: |
| 1225 | if (VerboseAsm) { |
| 1226 | O.PadToColumn(MAI->getCommentColumn()); |
| 1227 | O << MAI->getCommentString() << " 0x"; |
| 1228 | O.write_hex(CI->getZExtValue()); |
| 1229 | O << '\n'; |
| 1230 | } |
| 1231 | OutStreamer.EmitIntValue(CI->getZExtValue(), Size, AddrSpace); |
| 1232 | return; |
| 1233 | default: |
| 1234 | EmitGlobalConstantLargeInt(CI, AddrSpace, *this); |
| 1235 | return; |
| 1236 | } |
| 1237 | } |
Chris Lattner | 3cc3a00 | 2010-01-13 04:34:19 +0000 | [diff] [blame] | 1238 | |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1239 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) |
| 1240 | return EmitGlobalConstantArray(CVA, AddrSpace, *this); |
Chris Lattner | 3cc3a00 | 2010-01-13 04:34:19 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1242 | if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) |
| 1243 | return EmitGlobalConstantStruct(CVS, AddrSpace, *this); |
Chris Lattner | 3cc3a00 | 2010-01-13 04:34:19 +0000 | [diff] [blame] | 1244 | |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1245 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1246 | return EmitGlobalConstantFP(CFP, AddrSpace, *this); |
Chris Lattner | 3cc3a00 | 2010-01-13 04:34:19 +0000 | [diff] [blame] | 1247 | |
Chris Lattner | 91093ec | 2010-01-19 19:10:44 +0000 | [diff] [blame] | 1248 | if (const ConstantVector *V = dyn_cast<ConstantVector>(CV)) |
| 1249 | return EmitGlobalConstantVector(V, AddrSpace, *this); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1250 | |
Chris Lattner | 043c4e5 | 2010-01-20 07:19:19 +0000 | [diff] [blame] | 1251 | // ConstantExpr case. |
Chris Lattner | 2dd245c | 2010-01-20 07:11:32 +0000 | [diff] [blame] | 1252 | printDataDirective(CV->getType(), AddrSpace); |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 1253 | EmitConstantValueOnly(CV); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1254 | O << '\n'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1255 | } |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 1257 | void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1258 | // Target doesn't support this yet! |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1259 | llvm_unreachable("Target does not support EmitMachineConstantPoolValue"); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1262 | /// PrintSpecial - Print information related to the specified machine instr |
| 1263 | /// that is independent of the operand, and may be independent of the instr |
| 1264 | /// itself. This can be useful for portably encoding the comment character |
| 1265 | /// or other bits of target-specific knowledge into the asmstrings. The |
| 1266 | /// syntax used is ${:comment}. Targets can override this to add support |
| 1267 | /// for their own strange codes. |
Chris Lattner | 3e0cc26 | 2009-03-10 05:37:13 +0000 | [diff] [blame] | 1268 | void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const { |
Chris Lattner | bae02cf | 2006-09-27 00:06:07 +0000 | [diff] [blame] | 1269 | if (!strcmp(Code, "private")) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1270 | O << MAI->getPrivateGlobalPrefix(); |
Chris Lattner | bae02cf | 2006-09-27 00:06:07 +0000 | [diff] [blame] | 1271 | } else if (!strcmp(Code, "comment")) { |
Evan Cheng | f1c0ae9 | 2009-03-24 00:17:40 +0000 | [diff] [blame] | 1272 | if (VerboseAsm) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1273 | O << MAI->getCommentString(); |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1274 | } else if (!strcmp(Code, "uid")) { |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1275 | // Comparing the address of MI isn't sufficient, because machineinstrs may |
| 1276 | // be allocated to the same address across functions. |
| 1277 | const Function *ThisF = MI->getParent()->getParent()->getFunction(); |
| 1278 | |
Owen Anderson | bd58edf | 2009-06-24 22:28:12 +0000 | [diff] [blame] | 1279 | // If this is a new LastFn instruction, bump the counter. |
| 1280 | if (LastMI != MI || LastFn != ThisF) { |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1281 | ++Counter; |
| 1282 | LastMI = MI; |
Owen Anderson | bd58edf | 2009-06-24 22:28:12 +0000 | [diff] [blame] | 1283 | LastFn = ThisF; |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1284 | } |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1285 | O << Counter; |
| 1286 | } else { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1287 | std::string msg; |
| 1288 | raw_string_ostream Msg(msg); |
| 1289 | Msg << "Unknown special formatter '" << Code |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1290 | << "' for machine instr: " << *MI; |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1291 | llvm_report_error(Msg.str()); |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
Argyrios Kyrtzidis | cd76240 | 2009-05-07 13:55:51 +0000 | [diff] [blame] | 1295 | /// processDebugLoc - Processes the debug information of each machine |
| 1296 | /// instruction's DebugLoc. |
Devang Patel | af0e272 | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 1297 | void AsmPrinter::processDebugLoc(const MachineInstr *MI, |
| 1298 | bool BeforePrintingInsn) { |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1299 | if (!MAI || !DW || !MAI->doesSupportDebugInformation() |
| 1300 | || !DW->ShouldEmitDwarfDebug()) |
Chris Lattner | d250329 | 2009-08-05 04:09:18 +0000 | [diff] [blame] | 1301 | return; |
Devang Patel | b0fdedb | 2009-09-30 23:12:50 +0000 | [diff] [blame] | 1302 | DebugLoc DL = MI->getDebugLoc(); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1303 | if (DL.isUnknown()) |
| 1304 | return; |
Devang Patel | 6b61f58 | 2010-01-16 06:09:35 +0000 | [diff] [blame] | 1305 | DILocation CurDLT = MF->getDILocation(DL); |
| 1306 | if (CurDLT.getScope().isNull()) |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1307 | return; |
| 1308 | |
Chris Lattner | 043c4e5 | 2010-01-20 07:19:19 +0000 | [diff] [blame] | 1309 | if (!BeforePrintingInsn) { |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1310 | // After printing instruction |
| 1311 | DW->EndScope(MI); |
Chris Lattner | 043c4e5 | 2010-01-20 07:19:19 +0000 | [diff] [blame] | 1312 | } else if (CurDLT.getNode() != PrevDLT) { |
| 1313 | unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(), |
| 1314 | CurDLT.getColumnNumber(), |
| 1315 | CurDLT.getScope().getNode()); |
| 1316 | printLabel(L); |
| 1317 | O << '\n'; |
| 1318 | DW->BeginScope(MI, L); |
| 1319 | PrevDLT = CurDLT.getNode(); |
Argyrios Kyrtzidis | cd76240 | 2009-05-07 13:55:51 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1322 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1323 | |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1324 | /// printInlineAsm - This method formats and prints the specified machine |
| 1325 | /// instruction that is an inline asm. |
| 1326 | void AsmPrinter::printInlineAsm(const MachineInstr *MI) const { |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 1327 | unsigned NumOperands = MI->getNumOperands(); |
| 1328 | |
| 1329 | // Count the number of register definitions. |
| 1330 | unsigned NumDefs = 0; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1331 | for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); |
Chris Lattner | 67942f5 | 2006-09-05 20:02:51 +0000 | [diff] [blame] | 1332 | ++NumDefs) |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 1333 | assert(NumDefs != NumOperands-1 && "No asm string?"); |
| 1334 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1335 | assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1336 | |
| 1337 | // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 1338 | const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1339 | |
Dan Gohman | 40c5786 | 2009-11-06 00:04:54 +0000 | [diff] [blame] | 1340 | O << '\t'; |
| 1341 | |
Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 1342 | // If this asmstr is empty, just print the #APP/#NOAPP markers. |
| 1343 | // These are useful to see where empty asm's wound up. |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 1344 | if (AsmStr[0] == 0) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1345 | O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t"; |
| 1346 | O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n'; |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 1347 | return; |
| 1348 | } |
| 1349 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1350 | O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t"; |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 1351 | |
Bill Wendling | eb9a42c | 2007-01-16 03:42:04 +0000 | [diff] [blame] | 1352 | // The variant of the current asmprinter. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1353 | int AsmPrinterVariant = MAI->getAssemblerDialect(); |
Bill Wendling | eb9a42c | 2007-01-16 03:42:04 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1355 | int CurVariant = -1; // The number of the {.|.|.} region we are in. |
| 1356 | const char *LastEmitted = AsmStr; // One past the last character emitted. |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1357 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1358 | while (*LastEmitted) { |
| 1359 | switch (*LastEmitted) { |
| 1360 | default: { |
| 1361 | // Not a special case, emit the string section literally. |
| 1362 | const char *LiteralEnd = LastEmitted+1; |
| 1363 | while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 1364 | *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1365 | ++LiteralEnd; |
| 1366 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 1367 | O.write(LastEmitted, LiteralEnd-LastEmitted); |
| 1368 | LastEmitted = LiteralEnd; |
| 1369 | break; |
| 1370 | } |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 1371 | case '\n': |
| 1372 | ++LastEmitted; // Consume newline character. |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1373 | O << '\n'; // Indent code with newline. |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 1374 | break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1375 | case '$': { |
| 1376 | ++LastEmitted; // Consume '$' character. |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1377 | bool Done = true; |
| 1378 | |
| 1379 | // Handle escapes. |
| 1380 | switch (*LastEmitted) { |
| 1381 | default: Done = false; break; |
| 1382 | case '$': // $$ -> $ |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1383 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 1384 | O << '$'; |
| 1385 | ++LastEmitted; // Consume second '$' character. |
| 1386 | break; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1387 | case '(': // $( -> same as GCC's { character. |
| 1388 | ++LastEmitted; // Consume '(' character. |
| 1389 | if (CurVariant != -1) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1390 | llvm_report_error("Nested variants found in inline asm string: '" |
| 1391 | + std::string(AsmStr) + "'"); |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1392 | } |
| 1393 | CurVariant = 0; // We're in the first variant now. |
| 1394 | break; |
| 1395 | case '|': |
| 1396 | ++LastEmitted; // consume '|' character. |
Dale Johannesen | 8b1e054 | 2008-10-10 21:04:42 +0000 | [diff] [blame] | 1397 | if (CurVariant == -1) |
| 1398 | O << '|'; // this is gcc's behavior for | outside a variant |
| 1399 | else |
| 1400 | ++CurVariant; // We're in the next variant. |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1401 | break; |
| 1402 | case ')': // $) -> same as GCC's } char. |
| 1403 | ++LastEmitted; // consume ')' character. |
Dale Johannesen | 8b1e054 | 2008-10-10 21:04:42 +0000 | [diff] [blame] | 1404 | if (CurVariant == -1) |
| 1405 | O << '}'; // this is gcc's behavior for } outside a variant |
| 1406 | else |
| 1407 | CurVariant = -1; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1408 | break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1409 | } |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1410 | if (Done) break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1411 | |
| 1412 | bool HasCurlyBraces = false; |
| 1413 | if (*LastEmitted == '{') { // ${variable} |
| 1414 | ++LastEmitted; // Consume '{' character. |
| 1415 | HasCurlyBraces = true; |
| 1416 | } |
| 1417 | |
Chris Lattner | 3e0cc26 | 2009-03-10 05:37:13 +0000 | [diff] [blame] | 1418 | // If we have ${:foo}, then this is not a real operand reference, it is a |
| 1419 | // "magic" string reference, just like in .td files. Arrange to call |
| 1420 | // PrintSpecial. |
| 1421 | if (HasCurlyBraces && *LastEmitted == ':') { |
| 1422 | ++LastEmitted; |
| 1423 | const char *StrStart = LastEmitted; |
| 1424 | const char *StrEnd = strchr(StrStart, '}'); |
| 1425 | if (StrEnd == 0) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1426 | llvm_report_error("Unterminated ${:foo} operand in inline asm string: '" |
| 1427 | + std::string(AsmStr) + "'"); |
Chris Lattner | 3e0cc26 | 2009-03-10 05:37:13 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | std::string Val(StrStart, StrEnd); |
| 1431 | PrintSpecial(MI, Val.c_str()); |
| 1432 | LastEmitted = StrEnd+1; |
| 1433 | break; |
| 1434 | } |
| 1435 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1436 | const char *IDStart = LastEmitted; |
| 1437 | char *IDEnd; |
Chris Lattner | fad2912 | 2007-01-23 00:36:17 +0000 | [diff] [blame] | 1438 | errno = 0; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1439 | long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs. |
| 1440 | if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1441 | llvm_report_error("Bad $ operand number in inline asm string: '" |
| 1442 | + std::string(AsmStr) + "'"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1443 | } |
| 1444 | LastEmitted = IDEnd; |
| 1445 | |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1446 | char Modifier[2] = { 0, 0 }; |
| 1447 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1448 | if (HasCurlyBraces) { |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1449 | // If we have curly braces, check for a modifier character. This |
| 1450 | // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. |
| 1451 | if (*LastEmitted == ':') { |
| 1452 | ++LastEmitted; // Consume ':' character. |
| 1453 | if (*LastEmitted == 0) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1454 | llvm_report_error("Bad ${:} expression in inline asm string: '" |
| 1455 | + std::string(AsmStr) + "'"); |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | Modifier[0] = *LastEmitted; |
| 1459 | ++LastEmitted; // Consume modifier character. |
| 1460 | } |
| 1461 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1462 | if (*LastEmitted != '}') { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1463 | llvm_report_error("Bad ${} expression in inline asm string: '" |
| 1464 | + std::string(AsmStr) + "'"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1465 | } |
| 1466 | ++LastEmitted; // Consume '}' character. |
| 1467 | } |
| 1468 | |
| 1469 | if ((unsigned)Val >= NumOperands-1) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1470 | llvm_report_error("Invalid $ operand number in inline asm string: '" |
| 1471 | + std::string(AsmStr) + "'"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1474 | // Okay, we finally have a value number. Ask the target to print this |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1475 | // operand! |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1476 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { |
| 1477 | unsigned OpNo = 1; |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1478 | |
| 1479 | bool Error = false; |
| 1480 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1481 | // Scan to find the machine operand number for the operand. |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 1482 | for (; Val; --Val) { |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1483 | if (OpNo >= MI->getNumOperands()) break; |
Chris Lattner | 9e33049 | 2007-12-30 20:50:28 +0000 | [diff] [blame] | 1484 | unsigned OpFlags = MI->getOperand(OpNo).getImm(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 1485 | OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1; |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 1486 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1488 | if (OpNo >= MI->getNumOperands()) { |
| 1489 | Error = true; |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1490 | } else { |
Chris Lattner | 9e33049 | 2007-12-30 20:50:28 +0000 | [diff] [blame] | 1491 | unsigned OpFlags = MI->getOperand(OpNo).getImm(); |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1492 | ++OpNo; // Skip over the ID number. |
| 1493 | |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 1494 | if (Modifier[0] == 'l') // labels are target independent |
| 1495 | O << *GetMBBSymbol(MI->getOperand(OpNo).getMBB()->getNumber()); |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1496 | else { |
| 1497 | AsmPrinter *AP = const_cast<AsmPrinter*>(this); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 1498 | if ((OpFlags & 7) == 4) { |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1499 | Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, |
| 1500 | Modifier[0] ? Modifier : 0); |
| 1501 | } else { |
| 1502 | Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, |
| 1503 | Modifier[0] ? Modifier : 0); |
| 1504 | } |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1505 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1506 | } |
| 1507 | if (Error) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1508 | std::string msg; |
| 1509 | raw_string_ostream Msg(msg); |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 1510 | Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n"; |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1511 | MI->print(Msg); |
| 1512 | llvm_report_error(Msg.str()); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1513 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1514 | } |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1515 | break; |
| 1516 | } |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
Chris Lattner | c5ea263 | 2009-09-09 23:14:36 +0000 | [diff] [blame] | 1519 | O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd(); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 1522 | /// printImplicitDef - This method prints the specified machine instruction |
| 1523 | /// that is an implicit def. |
| 1524 | void AsmPrinter::printImplicitDef(const MachineInstr *MI) const { |
Chris Lattner | c5ea263 | 2009-09-09 23:14:36 +0000 | [diff] [blame] | 1525 | if (!VerboseAsm) return; |
| 1526 | O.PadToColumn(MAI->getCommentColumn()); |
| 1527 | O << MAI->getCommentString() << " implicit-def: " |
Chris Lattner | f806c23 | 2009-09-13 19:48:37 +0000 | [diff] [blame] | 1528 | << TRI->getName(MI->getOperand(0).getReg()); |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Jakob Stoklund Olesen | ad68264 | 2009-11-04 19:24:37 +0000 | [diff] [blame] | 1531 | void AsmPrinter::printKill(const MachineInstr *MI) const { |
| 1532 | if (!VerboseAsm) return; |
| 1533 | O.PadToColumn(MAI->getCommentColumn()); |
| 1534 | O << MAI->getCommentString() << " kill:"; |
| 1535 | for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) { |
| 1536 | const MachineOperand &op = MI->getOperand(n); |
| 1537 | assert(op.isReg() && "KILL instruction must have only register operands"); |
| 1538 | O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>"); |
| 1539 | } |
| 1540 | } |
| 1541 | |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1542 | /// printLabel - This method prints a local label used by debug and |
| 1543 | /// exception handling tables. |
| 1544 | void AsmPrinter::printLabel(const MachineInstr *MI) const { |
Dan Gohman | 528bc02 | 2008-07-01 00:16:26 +0000 | [diff] [blame] | 1545 | printLabel(MI->getOperand(0).getImm()); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Evan Cheng | 1b08bbc | 2008-02-01 09:10:45 +0000 | [diff] [blame] | 1548 | void AsmPrinter::printLabel(unsigned Id) const { |
Chris Lattner | c5ea263 | 2009-09-09 23:14:36 +0000 | [diff] [blame] | 1549 | O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':'; |
Evan Cheng | 1b08bbc | 2008-02-01 09:10:45 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1552 | /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM |
| 1553 | /// instruction, using the specified assembler variant. Targets should |
Dale Johannesen | cf0b766 | 2010-01-14 21:50:17 +0000 | [diff] [blame] | 1554 | /// override this to format as appropriate. |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1555 | bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1556 | unsigned AsmVariant, const char *ExtraCode) { |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1557 | // Target doesn't support this yet! |
| 1558 | return true; |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1559 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1560 | |
| 1561 | bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 1562 | unsigned AsmVariant, |
| 1563 | const char *ExtraCode) { |
| 1564 | // Target doesn't support this yet! |
| 1565 | return true; |
| 1566 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1567 | |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 1568 | MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA, |
| 1569 | const char *Suffix) const { |
| 1570 | return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F, |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 1574 | const BasicBlock *BB, |
| 1575 | const char *Suffix) const { |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1576 | assert(BB->hasName() && |
| 1577 | "Address of anonymous basic block not supported yet!"); |
| 1578 | |
Dan Gohman | 568a3be | 2009-11-05 23:14:35 +0000 | [diff] [blame] | 1579 | // This code must use the function name itself, and not the function number, |
| 1580 | // since it must be possible to generate the label name from within other |
| 1581 | // functions. |
Chris Lattner | 2f8cc26 | 2010-01-13 07:30:49 +0000 | [diff] [blame] | 1582 | SmallString<60> FnName; |
| 1583 | Mang->getNameWithPrefix(FnName, F, false); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | 2f8cc26 | 2010-01-13 07:30:49 +0000 | [diff] [blame] | 1585 | // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME! |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 1586 | SmallString<60> NameResult; |
Chris Lattner | 2f8cc26 | 2010-01-13 07:30:49 +0000 | [diff] [blame] | 1587 | Mang->getNameWithPrefix(NameResult, |
| 1588 | StringRef("BA") + Twine((unsigned)FnName.size()) + |
| 1589 | "_" + FnName.str() + "_" + BB->getName() + Suffix, |
| 1590 | Mangler::Private); |
Dan Gohman | 568a3be | 2009-11-05 23:14:35 +0000 | [diff] [blame] | 1591 | |
Chris Lattner | 4813035 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 1592 | return OutContext.GetOrCreateSymbol(NameResult.str()); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Chris Lattner | 7cb384d | 2009-09-12 23:02:08 +0000 | [diff] [blame] | 1595 | MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const { |
| 1596 | SmallString<60> Name; |
| 1597 | raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB" |
| 1598 | << getFunctionNumber() << '_' << MBBID; |
| 1599 | |
| 1600 | return OutContext.GetOrCreateSymbol(Name.str()); |
| 1601 | } |
| 1602 | |
Chris Lattner | 6b04ede | 2010-01-15 23:18:17 +0000 | [diff] [blame] | 1603 | /// GetGlobalValueSymbol - Return the MCSymbol for the specified global |
| 1604 | /// value. |
| 1605 | MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const { |
| 1606 | SmallString<60> NameStr; |
| 1607 | Mang->getNameWithPrefix(NameStr, GV, false); |
| 1608 | return OutContext.GetOrCreateSymbol(NameStr.str()); |
| 1609 | } |
| 1610 | |
Chris Lattner | 7a2ba94 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 1611 | /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with |
Chris Lattner | d588b97 | 2010-01-15 23:25:11 +0000 | [diff] [blame] | 1612 | /// global value name as its base, with the specified suffix, and where the |
Chris Lattner | 7a2ba94 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 1613 | /// symbol is forced to have private linkage if ForcePrivate is true. |
| 1614 | MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV, |
| 1615 | StringRef Suffix, |
| 1616 | bool ForcePrivate) const { |
Chris Lattner | d588b97 | 2010-01-15 23:25:11 +0000 | [diff] [blame] | 1617 | SmallString<60> NameStr; |
Chris Lattner | 7a2ba94 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 1618 | Mang->getNameWithPrefix(NameStr, GV, ForcePrivate); |
Chris Lattner | d588b97 | 2010-01-15 23:25:11 +0000 | [diff] [blame] | 1619 | NameStr.append(Suffix.begin(), Suffix.end()); |
| 1620 | return OutContext.GetOrCreateSymbol(NameStr.str()); |
| 1621 | } |
| 1622 | |
Chris Lattner | 6b04ede | 2010-01-15 23:18:17 +0000 | [diff] [blame] | 1623 | /// GetExternalSymbolSymbol - Return the MCSymbol for the specified |
| 1624 | /// ExternalSymbol. |
| 1625 | MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const { |
| 1626 | SmallString<60> NameStr; |
| 1627 | Mang->getNameWithPrefix(NameStr, Sym); |
| 1628 | return OutContext.GetOrCreateSymbol(NameStr.str()); |
| 1629 | } |
| 1630 | |
Chris Lattner | 7cb384d | 2009-09-12 23:02:08 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | 70a54c0 | 2009-09-13 18:25:37 +0000 | [diff] [blame] | 1632 | /// EmitBasicBlockStart - This method prints the label for the specified |
| 1633 | /// MachineBasicBlock, an alignment (if present) and a comment describing |
| 1634 | /// it if appropriate. |
Chris Lattner | 662316c | 2009-09-14 03:15:54 +0000 | [diff] [blame] | 1635 | void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const { |
Dan Gohman | b1cac33 | 2009-10-30 01:34:35 +0000 | [diff] [blame] | 1636 | // Emit an alignment directive for this block, if needed. |
Chris Lattner | 70a54c0 | 2009-09-13 18:25:37 +0000 | [diff] [blame] | 1637 | if (unsigned Align = MBB->getAlignment()) |
| 1638 | EmitAlignment(Log2_32(Align)); |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1639 | |
Dan Gohman | b1cac33 | 2009-10-30 01:34:35 +0000 | [diff] [blame] | 1640 | // If the block has its address taken, emit a special label to satisfy |
| 1641 | // references to the block. This is done so that we don't need to |
| 1642 | // remember the number of this label, and so that we can make |
| 1643 | // forward references to labels without knowing what their numbers |
| 1644 | // will be. |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1645 | if (MBB->hasAddressTaken()) { |
Chris Lattner | 213168b | 2010-01-20 07:24:05 +0000 | [diff] [blame^] | 1646 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 1647 | OutStreamer.EmitLabel(GetBlockAddressSymbol(BB->getParent(), BB)); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1648 | if (VerboseAsm) { |
| 1649 | O.PadToColumn(MAI->getCommentColumn()); |
Chris Lattner | 213168b | 2010-01-20 07:24:05 +0000 | [diff] [blame^] | 1650 | O << MAI->getCommentString() << " Address Taken" << '\n'; |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1651 | } |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Dan Gohman | b1cac33 | 2009-10-30 01:34:35 +0000 | [diff] [blame] | 1654 | // Print the main label for the block. |
Dan Gohman | e3cc3f3 | 2009-10-06 17:38:38 +0000 | [diff] [blame] | 1655 | if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) { |
| 1656 | if (VerboseAsm) |
| 1657 | O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':'; |
| 1658 | } else { |
Chris Lattner | 213168b | 2010-01-20 07:24:05 +0000 | [diff] [blame^] | 1659 | OutStreamer.EmitLabel(GetMBBSymbol(MBB->getNumber())); |
Dan Gohman | e3cc3f3 | 2009-10-06 17:38:38 +0000 | [diff] [blame] | 1660 | } |
Chris Lattner | 70a54c0 | 2009-09-13 18:25:37 +0000 | [diff] [blame] | 1661 | |
Dan Gohman | b1cac33 | 2009-10-30 01:34:35 +0000 | [diff] [blame] | 1662 | // Print some comments to accompany the label. |
Chris Lattner | 70a54c0 | 2009-09-13 18:25:37 +0000 | [diff] [blame] | 1663 | if (VerboseAsm) { |
Dan Gohman | e45da0d | 2009-08-12 18:47:05 +0000 | [diff] [blame] | 1664 | if (const BasicBlock *BB = MBB->getBasicBlock()) |
| 1665 | if (BB->hasName()) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1666 | O.PadToColumn(MAI->getCommentColumn()); |
| 1667 | O << MAI->getCommentString() << ' '; |
Dan Gohman | 46d5056 | 2009-08-12 20:56:56 +0000 | [diff] [blame] | 1668 | WriteAsOperand(O, BB, /*PrintType=*/false); |
Dan Gohman | e45da0d | 2009-08-12 18:47:05 +0000 | [diff] [blame] | 1669 | } |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1670 | |
Chris Lattner | 70a54c0 | 2009-09-13 18:25:37 +0000 | [diff] [blame] | 1671 | EmitComments(*MBB); |
Dan Gohman | e3cc3f3 | 2009-10-06 17:38:38 +0000 | [diff] [blame] | 1672 | O << '\n'; |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1673 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1674 | } |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1675 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1676 | /// printPICJumpTableSetLabel - This method prints a set label for the |
| 1677 | /// specified MachineBasicBlock for a jumptable entry. |
| 1678 | void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, |
| 1679 | const MachineBasicBlock *MBB) const { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1680 | if (!MAI->getSetDirective()) |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1681 | return; |
| 1682 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1683 | O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix() |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 1684 | << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',' |
| 1685 | << *GetMBBSymbol(MBB->getNumber()) |
| 1686 | << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1687 | << '_' << uid << '\n'; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1688 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1689 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1690 | void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2, |
| 1691 | const MachineBasicBlock *MBB) const { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1692 | if (!MAI->getSetDirective()) |
Evan Cheng | 41349c1 | 2006-11-01 09:23:08 +0000 | [diff] [blame] | 1693 | return; |
| 1694 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1695 | O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix() |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1696 | << getFunctionNumber() << '_' << uid << '_' << uid2 |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 1697 | << "_set_" << MBB->getNumber() << ',' |
| 1698 | << *GetMBBSymbol(MBB->getNumber()) |
| 1699 | << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1700 | << '_' << uid << '_' << uid2 << '\n'; |
Evan Cheng | 41349c1 | 2006-11-01 09:23:08 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1703 | /// printDataDirective - This method prints the asm directive for the |
| 1704 | /// specified type. |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 1705 | void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) { |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1706 | const TargetData *TD = TM.getTargetData(); |
| 1707 | switch (type->getTypeID()) { |
Chris Lattner | f1cfea2 | 2009-07-15 04:42:49 +0000 | [diff] [blame] | 1708 | case Type::FloatTyID: case Type::DoubleTyID: |
| 1709 | case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID: |
| 1710 | assert(0 && "Should have already output floating point constant."); |
| 1711 | default: |
| 1712 | assert(0 && "Can't handle printing this type of thing"); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1713 | case Type::IntegerTyID: { |
| 1714 | unsigned BitWidth = cast<IntegerType>(type)->getBitWidth(); |
| 1715 | if (BitWidth <= 8) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1716 | O << MAI->getData8bitsDirective(AddrSpace); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1717 | else if (BitWidth <= 16) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1718 | O << MAI->getData16bitsDirective(AddrSpace); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1719 | else if (BitWidth <= 32) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1720 | O << MAI->getData32bitsDirective(AddrSpace); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1721 | else if (BitWidth <= 64) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1722 | assert(MAI->getData64bitsDirective(AddrSpace) && |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1723 | "Target cannot handle 64-bit constant exprs!"); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1724 | O << MAI->getData64bitsDirective(AddrSpace); |
Dan Gohman | 82f94f1 | 2008-09-08 16:40:13 +0000 | [diff] [blame] | 1725 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1726 | llvm_unreachable("Target cannot handle given data directive width!"); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1727 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1728 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1729 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1730 | case Type::PointerTyID: |
| 1731 | if (TD->getPointerSize() == 8) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1732 | assert(MAI->getData64bitsDirective(AddrSpace) && |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1733 | "Target cannot handle 64-bit pointer exprs!"); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1734 | O << MAI->getData64bitsDirective(AddrSpace); |
Sanjiv Gupta | fcc6f15 | 2009-01-22 10:14:21 +0000 | [diff] [blame] | 1735 | } else if (TD->getPointerSize() == 2) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1736 | O << MAI->getData16bitsDirective(AddrSpace); |
Sanjiv Gupta | fcc6f15 | 2009-01-22 10:14:21 +0000 | [diff] [blame] | 1737 | } else if (TD->getPointerSize() == 1) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1738 | O << MAI->getData8bitsDirective(AddrSpace); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1739 | } else { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1740 | O << MAI->getData32bitsDirective(AddrSpace); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1741 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1742 | break; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } |
Dale Johannesen | 4c3a5f8 | 2007-02-16 01:54:53 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | 53d4d78 | 2010-01-15 23:38:51 +0000 | [diff] [blame] | 1746 | void AsmPrinter::printVisibility(const MCSymbol *Sym, |
| 1747 | unsigned Visibility) const { |
| 1748 | if (Visibility == GlobalValue::HiddenVisibility) { |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 1749 | if (const char *Directive = MAI->getHiddenDirective()) |
| 1750 | O << Directive << *Sym << '\n'; |
Chris Lattner | 53d4d78 | 2010-01-15 23:38:51 +0000 | [diff] [blame] | 1751 | } else if (Visibility == GlobalValue::ProtectedVisibility) { |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 1752 | if (const char *Directive = MAI->getProtectedDirective()) |
| 1753 | O << Directive << *Sym << '\n'; |
Chris Lattner | 53d4d78 | 2010-01-15 23:38:51 +0000 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | |
Anton Korobeynikov | 7751ad9 | 2008-11-22 16:15:34 +0000 | [diff] [blame] | 1757 | void AsmPrinter::printOffset(int64_t Offset) const { |
| 1758 | if (Offset > 0) |
| 1759 | O << '+' << Offset; |
| 1760 | else if (Offset < 0) |
| 1761 | O << Offset; |
| 1762 | } |
| 1763 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1764 | GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) { |
| 1765 | if (!S->usesMetadata()) |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1766 | return 0; |
| 1767 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1768 | gcp_iterator GCPI = GCMetadataPrinters.find(S); |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1769 | if (GCPI != GCMetadataPrinters.end()) |
| 1770 | return GCPI->second; |
| 1771 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1772 | const char *Name = S->getName().c_str(); |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1773 | |
| 1774 | for (GCMetadataPrinterRegistry::iterator |
| 1775 | I = GCMetadataPrinterRegistry::begin(), |
| 1776 | E = GCMetadataPrinterRegistry::end(); I != E; ++I) |
| 1777 | if (strcmp(Name, I->getName()) == 0) { |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1778 | GCMetadataPrinter *GMP = I->instantiate(); |
| 1779 | GMP->S = S; |
| 1780 | GCMetadataPrinters.insert(std::make_pair(S, GMP)); |
| 1781 | return GMP; |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 1784 | errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1785 | llvm_unreachable(0); |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1786 | } |
David Greene | 014700c | 2009-07-13 20:25:48 +0000 | [diff] [blame] | 1787 | |
| 1788 | /// EmitComments - Pretty-print comments for instructions |
Chris Lattner | 5f51cd0 | 2009-08-07 23:42:01 +0000 | [diff] [blame] | 1789 | void AsmPrinter::EmitComments(const MachineInstr &MI) const { |
David Greene | 1924aab | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 1790 | if (!VerboseAsm) |
| 1791 | return; |
David Greene | 3ac1ab8 | 2009-07-16 22:24:20 +0000 | [diff] [blame] | 1792 | |
David Greene | 1924aab | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 1793 | bool Newline = false; |
| 1794 | |
| 1795 | if (!MI.getDebugLoc().isUnknown()) { |
Devang Patel | 6b61f58 | 2010-01-16 06:09:35 +0000 | [diff] [blame] | 1796 | DILocation DLT = MF->getDILocation(MI.getDebugLoc()); |
David Greene | 1924aab | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 1797 | |
| 1798 | // Print source line info. |
| 1799 | O.PadToColumn(MAI->getCommentColumn()); |
Dan Gohman | 3b9bc04 | 2009-12-05 00:23:29 +0000 | [diff] [blame] | 1800 | O << MAI->getCommentString() << ' '; |
Devang Patel | 6b61f58 | 2010-01-16 06:09:35 +0000 | [diff] [blame] | 1801 | DIScope Scope = DLT.getScope(); |
Dan Gohman | 3b9bc04 | 2009-12-05 00:23:29 +0000 | [diff] [blame] | 1802 | // Omit the directory, because it's likely to be long and uninteresting. |
| 1803 | if (!Scope.isNull()) |
| 1804 | O << Scope.getFilename(); |
| 1805 | else |
| 1806 | O << "<unknown>"; |
Devang Patel | 6b61f58 | 2010-01-16 06:09:35 +0000 | [diff] [blame] | 1807 | O << ':' << DLT.getLineNumber(); |
| 1808 | if (DLT.getColumnNumber() != 0) |
| 1809 | O << ':' << DLT.getColumnNumber(); |
David Greene | 1924aab | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 1810 | Newline = true; |
David Greene | 3ac1ab8 | 2009-07-16 22:24:20 +0000 | [diff] [blame] | 1811 | } |
David Greene | 1924aab | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 1812 | |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1813 | // Check for spills and reloads |
| 1814 | int FI; |
| 1815 | |
| 1816 | const MachineFrameInfo *FrameInfo = |
| 1817 | MI.getParent()->getParent()->getFrameInfo(); |
| 1818 | |
| 1819 | // We assume a single instruction only has a spill or reload, not |
| 1820 | // both. |
David Greene | f7ea2a5 | 2009-12-04 22:46:04 +0000 | [diff] [blame] | 1821 | const MachineMemOperand *MMO; |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1822 | if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) { |
| 1823 | if (FrameInfo->isSpillSlotObjectIndex(FI)) { |
David Greene | f7ea2a5 | 2009-12-04 22:46:04 +0000 | [diff] [blame] | 1824 | MMO = *MI.memoperands_begin(); |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1825 | if (Newline) O << '\n'; |
| 1826 | O.PadToColumn(MAI->getCommentColumn()); |
Dan Gohman | fcafe44 | 2009-12-04 23:19:55 +0000 | [diff] [blame] | 1827 | O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload"; |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1828 | Newline = true; |
| 1829 | } |
| 1830 | } |
David Greene | f7ea2a5 | 2009-12-04 22:46:04 +0000 | [diff] [blame] | 1831 | else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) { |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1832 | if (FrameInfo->isSpillSlotObjectIndex(FI)) { |
| 1833 | if (Newline) O << '\n'; |
| 1834 | O.PadToColumn(MAI->getCommentColumn()); |
Dan Gohman | fcafe44 | 2009-12-04 23:19:55 +0000 | [diff] [blame] | 1835 | O << MAI->getCommentString() << ' ' |
| 1836 | << MMO->getSize() << "-byte Folded Reload"; |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1837 | Newline = true; |
| 1838 | } |
| 1839 | } |
| 1840 | else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) { |
| 1841 | if (FrameInfo->isSpillSlotObjectIndex(FI)) { |
David Greene | f7ea2a5 | 2009-12-04 22:46:04 +0000 | [diff] [blame] | 1842 | MMO = *MI.memoperands_begin(); |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1843 | if (Newline) O << '\n'; |
| 1844 | O.PadToColumn(MAI->getCommentColumn()); |
Dan Gohman | fcafe44 | 2009-12-04 23:19:55 +0000 | [diff] [blame] | 1845 | O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill"; |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1846 | Newline = true; |
| 1847 | } |
| 1848 | } |
David Greene | f7ea2a5 | 2009-12-04 22:46:04 +0000 | [diff] [blame] | 1849 | else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) { |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1850 | if (FrameInfo->isSpillSlotObjectIndex(FI)) { |
| 1851 | if (Newline) O << '\n'; |
| 1852 | O.PadToColumn(MAI->getCommentColumn()); |
Dan Gohman | fcafe44 | 2009-12-04 23:19:55 +0000 | [diff] [blame] | 1853 | O << MAI->getCommentString() << ' ' |
| 1854 | << MMO->getSize() << "-byte Folded Spill"; |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1855 | Newline = true; |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | // Check for spill-induced copies |
| 1860 | unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx; |
| 1861 | if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg, |
| 1862 | SrcSubIdx, DstSubIdx)) { |
| 1863 | if (MI.getAsmPrinterFlag(ReloadReuse)) { |
| 1864 | if (Newline) O << '\n'; |
| 1865 | O.PadToColumn(MAI->getCommentColumn()); |
| 1866 | O << MAI->getCommentString() << " Reload Reuse"; |
David Greene | ddff941 | 2009-11-16 15:12:23 +0000 | [diff] [blame] | 1867 | } |
| 1868 | } |
David Greene | 014700c | 2009-07-13 20:25:48 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
David Greene | fe37ab3 | 2009-08-18 19:22:55 +0000 | [diff] [blame] | 1871 | /// PrintChildLoopComment - Print comments about child loops within |
| 1872 | /// the loop for this basic block, with nesting. |
| 1873 | /// |
| 1874 | static void PrintChildLoopComment(formatted_raw_ostream &O, |
| 1875 | const MachineLoop *loop, |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1876 | const MCAsmInfo *MAI, |
David Greene | fe37ab3 | 2009-08-18 19:22:55 +0000 | [diff] [blame] | 1877 | int FunctionNumber) { |
| 1878 | // Add child loop information |
| 1879 | for(MachineLoop::iterator cl = loop->begin(), |
| 1880 | clend = loop->end(); |
| 1881 | cl != clend; |
| 1882 | ++cl) { |
| 1883 | MachineBasicBlock *Header = (*cl)->getHeader(); |
| 1884 | assert(Header && "No header for loop"); |
| 1885 | |
| 1886 | O << '\n'; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1887 | O.PadToColumn(MAI->getCommentColumn()); |
David Greene | fe37ab3 | 2009-08-18 19:22:55 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1889 | O << MAI->getCommentString(); |
Chris Lattner | c281de1 | 2009-08-23 00:51:00 +0000 | [diff] [blame] | 1890 | O.indent(((*cl)->getLoopDepth()-1)*2) |
David Greene | fe37ab3 | 2009-08-18 19:22:55 +0000 | [diff] [blame] | 1891 | << " Child Loop BB" << FunctionNumber << "_" |
| 1892 | << Header->getNumber() << " Depth " << (*cl)->getLoopDepth(); |
| 1893 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1894 | PrintChildLoopComment(O, *cl, MAI, FunctionNumber); |
David Greene | fe37ab3 | 2009-08-18 19:22:55 +0000 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1898 | /// EmitComments - Pretty-print comments for basic blocks |
David Greene | 1924aab | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 1899 | void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const { |
David Greene | fe37ab3 | 2009-08-18 19:22:55 +0000 | [diff] [blame] | 1900 | if (VerboseAsm) { |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1901 | // Add loop depth information |
| 1902 | const MachineLoop *loop = LI->getLoopFor(&MBB); |
| 1903 | |
| 1904 | if (loop) { |
| 1905 | // Print a newline after bb# annotation. |
| 1906 | O << "\n"; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1907 | O.PadToColumn(MAI->getCommentColumn()); |
| 1908 | O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth() |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1909 | << '\n'; |
| 1910 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1911 | O.PadToColumn(MAI->getCommentColumn()); |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1912 | |
| 1913 | MachineBasicBlock *Header = loop->getHeader(); |
| 1914 | assert(Header && "No header for loop"); |
| 1915 | |
| 1916 | if (Header == &MBB) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1917 | O << MAI->getCommentString() << " Loop Header"; |
| 1918 | PrintChildLoopComment(O, loop, MAI, getFunctionNumber()); |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1919 | } |
| 1920 | else { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1921 | O << MAI->getCommentString() << " Loop Header is BB" |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1922 | << getFunctionNumber() << "_" << loop->getHeader()->getNumber(); |
| 1923 | } |
| 1924 | |
| 1925 | if (loop->empty()) { |
| 1926 | O << '\n'; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1927 | O.PadToColumn(MAI->getCommentColumn()); |
| 1928 | O << MAI->getCommentString() << " Inner Loop"; |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | // Add parent loop information |
| 1932 | for (const MachineLoop *CurLoop = loop->getParentLoop(); |
| 1933 | CurLoop; |
| 1934 | CurLoop = CurLoop->getParentLoop()) { |
| 1935 | MachineBasicBlock *Header = CurLoop->getHeader(); |
| 1936 | assert(Header && "No header for loop"); |
| 1937 | |
| 1938 | O << '\n'; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1939 | O.PadToColumn(MAI->getCommentColumn()); |
| 1940 | O << MAI->getCommentString(); |
Chris Lattner | c281de1 | 2009-08-23 00:51:00 +0000 | [diff] [blame] | 1941 | O.indent((CurLoop->getLoopDepth()-1)*2) |
David Greene | fe37ab3 | 2009-08-18 19:22:55 +0000 | [diff] [blame] | 1942 | << " Inside Loop BB" << getFunctionNumber() << "_" |
David Greene | b71d1b2 | 2009-08-10 16:38:07 +0000 | [diff] [blame] | 1943 | << Header->getNumber() << " Depth " << CurLoop->getLoopDepth(); |
| 1944 | } |
| 1945 | } |
| 1946 | } |
| 1947 | } |