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