Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1 | //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the AsmPrinter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "asm-printer" |
| 15 | #include "llvm/CodeGen/AsmPrinter.h" |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 16 | #include "DwarfDebug.h" |
| 17 | #include "DwarfException.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 19 | #include "llvm/CodeGen/GCMetadataPrinter.h" |
| 20 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 24 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 26 | #include "llvm/Analysis/ConstantFolding.h" |
| 27 | #include "llvm/Analysis/DebugInfo.h" |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 28 | #include "llvm/MC/MCAsmInfo.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 29 | #include "llvm/MC/MCContext.h" |
| 30 | #include "llvm/MC/MCExpr.h" |
| 31 | #include "llvm/MC/MCInst.h" |
| 32 | #include "llvm/MC/MCSection.h" |
| 33 | #include "llvm/MC/MCStreamer.h" |
| 34 | #include "llvm/MC/MCSymbol.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 35 | #include "llvm/Target/Mangler.h" |
| 36 | #include "llvm/Target/TargetData.h" |
| 37 | #include "llvm/Target/TargetInstrInfo.h" |
| 38 | #include "llvm/Target/TargetLowering.h" |
| 39 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 40 | #include "llvm/Target/TargetRegisterInfo.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 41 | #include "llvm/ADT/SmallString.h" |
| 42 | #include "llvm/ADT/Statistic.h" |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 43 | #include "llvm/Support/ErrorHandling.h" |
| 44 | #include "llvm/Support/Format.h" |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 45 | #include "llvm/Support/Timer.h" |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 46 | #include <ctype.h> |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 49 | static const char *DWARFGroupName = "DWARF Emission"; |
| 50 | static const char *DbgTimerName = "DWARF Debug Writer"; |
| 51 | static const char *EHTimerName = "DWARF Exception Writer"; |
| 52 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 53 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 54 | |
| 55 | char AsmPrinter::ID = 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 56 | |
| 57 | typedef DenseMap<GCStrategy*,GCMetadataPrinter*> gcp_map_type; |
| 58 | static gcp_map_type &getGCMap(void *&P) { |
| 59 | if (P == 0) |
| 60 | P = new gcp_map_type(); |
| 61 | return *(gcp_map_type*)P; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | AsmPrinter::AsmPrinter(TargetMachine &tm, MCStreamer &Streamer) |
| 66 | : MachineFunctionPass(&ID), |
| 67 | TM(tm), MAI(tm.getMCAsmInfo()), |
| 68 | OutContext(Streamer.getContext()), |
| 69 | OutStreamer(Streamer), |
| 70 | LastMI(0), LastFn(0), Counter(~0U), SetCounter(0) { |
| 71 | DD = 0; DE = 0; MMI = 0; LI = 0; |
| 72 | GCMetadataPrinters = 0; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 73 | VerboseAsm = Streamer.isVerboseAsm(); |
| 74 | } |
| 75 | |
| 76 | AsmPrinter::~AsmPrinter() { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 77 | assert(DD == 0 && DE == 0 && "Debug/EH info didn't get finalized"); |
| 78 | |
| 79 | if (GCMetadataPrinters != 0) { |
| 80 | gcp_map_type &GCMap = getGCMap(GCMetadataPrinters); |
| 81 | |
| 82 | for (gcp_map_type::iterator I = GCMap.begin(), E = GCMap.end(); I != E; ++I) |
| 83 | delete I->second; |
| 84 | delete &GCMap; |
| 85 | GCMetadataPrinters = 0; |
| 86 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 87 | |
| 88 | delete &OutStreamer; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /// getFunctionNumber - Return a unique ID for the current function. |
| 92 | /// |
| 93 | unsigned AsmPrinter::getFunctionNumber() const { |
| 94 | return MF->getFunctionNumber(); |
| 95 | } |
| 96 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 97 | const TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 98 | return TM.getTargetLowering()->getObjFileLowering(); |
| 99 | } |
| 100 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 101 | |
| 102 | /// getTargetData - Return information about data layout. |
| 103 | const TargetData &AsmPrinter::getTargetData() const { |
| 104 | return *TM.getTargetData(); |
| 105 | } |
| 106 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 107 | /// getCurrentSection() - Return the current section we are emitting to. |
| 108 | const MCSection *AsmPrinter::getCurrentSection() const { |
| 109 | return OutStreamer.getCurrentSection(); |
| 110 | } |
| 111 | |
| 112 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 113 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 114 | void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { |
| 115 | AU.setPreservesAll(); |
| 116 | MachineFunctionPass::getAnalysisUsage(AU); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 117 | AU.addRequired<MachineModuleInfo>(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 118 | AU.addRequired<GCModuleInfo>(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 119 | if (isVerbose()) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 120 | AU.addRequired<MachineLoopInfo>(); |
| 121 | } |
| 122 | |
| 123 | bool AsmPrinter::doInitialization(Module &M) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 124 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 125 | MMI->AnalyzeModule(M); |
| 126 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 127 | // Initialize TargetLoweringObjectFile. |
| 128 | const_cast<TargetLoweringObjectFile&>(getObjFileLowering()) |
| 129 | .Initialize(OutContext, TM); |
| 130 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 131 | Mang = new Mangler(OutContext, *TM.getTargetData()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 132 | |
| 133 | // Allow the target to emit any magic that it wants at the start of the file. |
| 134 | EmitStartOfAsmFile(M); |
| 135 | |
| 136 | // Very minimal debug info. It is ignored if we emit actual debug info. If we |
| 137 | // don't, this at least helps the user find where a global came from. |
| 138 | if (MAI->hasSingleParameterDotFile()) { |
| 139 | // .file "foo.c" |
| 140 | OutStreamer.EmitFileDirective(M.getModuleIdentifier()); |
| 141 | } |
| 142 | |
| 143 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); |
| 144 | assert(MI && "AsmPrinter didn't require GCModuleInfo?"); |
| 145 | for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) |
| 146 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I)) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 147 | MP->beginAssembly(*this); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 148 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 149 | // Emit module-level inline asm if it exists. |
| 150 | if (!M.getModuleInlineAsm().empty()) { |
| 151 | OutStreamer.AddComment("Start of file scope inline assembly"); |
| 152 | OutStreamer.AddBlankLine(); |
| 153 | EmitInlineAsm(M.getModuleInlineAsm(), 0/*no loc cookie*/); |
| 154 | OutStreamer.AddComment("End of file scope inline assembly"); |
| 155 | OutStreamer.AddBlankLine(); |
| 156 | } |
| 157 | |
| 158 | if (MAI->doesSupportDebugInformation()) |
| 159 | DD = new DwarfDebug(this, &M); |
| 160 | |
| 161 | if (MAI->doesSupportExceptionHandling()) |
| 162 | DE = new DwarfException(this); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | void AsmPrinter::EmitLinkage(unsigned Linkage, MCSymbol *GVSym) const { |
| 168 | switch ((GlobalValue::LinkageTypes)Linkage) { |
| 169 | case GlobalValue::CommonLinkage: |
| 170 | case GlobalValue::LinkOnceAnyLinkage: |
| 171 | case GlobalValue::LinkOnceODRLinkage: |
| 172 | case GlobalValue::WeakAnyLinkage: |
| 173 | case GlobalValue::WeakODRLinkage: |
| 174 | case GlobalValue::LinkerPrivateLinkage: |
| 175 | if (MAI->getWeakDefDirective() != 0) { |
| 176 | // .globl _foo |
| 177 | OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global); |
| 178 | // .weak_definition _foo |
| 179 | OutStreamer.EmitSymbolAttribute(GVSym, MCSA_WeakDefinition); |
| 180 | } else if (const char *LinkOnce = MAI->getLinkOnceDirective()) { |
| 181 | // .globl _foo |
| 182 | OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global); |
| 183 | // FIXME: linkonce should be a section attribute, handled by COFF Section |
| 184 | // assignment. |
| 185 | // http://sourceware.org/binutils/docs-2.20/as/Linkonce.html#Linkonce |
| 186 | // .linkonce discard |
| 187 | // FIXME: It would be nice to use .linkonce samesize for non-common |
| 188 | // globals. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 189 | OutStreamer.EmitRawText(StringRef(LinkOnce)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 190 | } else { |
| 191 | // .weak _foo |
| 192 | OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Weak); |
| 193 | } |
| 194 | break; |
| 195 | case GlobalValue::DLLExportLinkage: |
| 196 | case GlobalValue::AppendingLinkage: |
| 197 | // FIXME: appending linkage variables should go into a section of |
| 198 | // their name or something. For now, just emit them as external. |
| 199 | case GlobalValue::ExternalLinkage: |
| 200 | // If external or appending, declare as a global symbol. |
| 201 | // .globl _foo |
| 202 | OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global); |
| 203 | break; |
| 204 | case GlobalValue::PrivateLinkage: |
| 205 | case GlobalValue::InternalLinkage: |
| 206 | break; |
| 207 | default: |
| 208 | llvm_unreachable("Unknown linkage type!"); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /// EmitGlobalVariable - Emit the specified global variable to the .s file. |
| 214 | void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) { |
| 215 | if (!GV->hasInitializer()) // External globals require no code. |
| 216 | return; |
| 217 | |
| 218 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 219 | if (EmitSpecialLLVMGlobal(GV)) |
| 220 | return; |
| 221 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 222 | MCSymbol *GVSym = Mang->getSymbol(GV); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 223 | EmitVisibility(GVSym, GV->getVisibility()); |
| 224 | |
| 225 | if (MAI->hasDotTypeDotSizeDirective()) |
| 226 | OutStreamer.EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject); |
| 227 | |
| 228 | SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM); |
| 229 | |
| 230 | const TargetData *TD = TM.getTargetData(); |
| 231 | unsigned Size = TD->getTypeAllocSize(GV->getType()->getElementType()); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 232 | |
| 233 | // If the alignment is specified, we *must* obey it. Overaligning a global |
| 234 | // with a specified alignment is a prompt way to break globals emitted to |
| 235 | // sections and expected to be contiguous (e.g. ObjC metadata). |
| 236 | unsigned AlignLog; |
| 237 | if (unsigned GVAlign = GV->getAlignment()) |
| 238 | AlignLog = Log2_32(GVAlign); |
| 239 | else |
| 240 | AlignLog = TD->getPreferredAlignmentLog(GV); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 241 | |
| 242 | // Handle common and BSS local symbols (.lcomm). |
| 243 | if (GVKind.isCommon() || GVKind.isBSSLocal()) { |
| 244 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 245 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 246 | if (isVerbose()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 247 | WriteAsOperand(OutStreamer.GetCommentOS(), GV, |
| 248 | /*PrintType=*/false, GV->getParent()); |
| 249 | OutStreamer.GetCommentOS() << '\n'; |
| 250 | } |
| 251 | |
| 252 | // Handle common symbols. |
| 253 | if (GVKind.isCommon()) { |
| 254 | // .comm _foo, 42, 4 |
| 255 | OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | // Handle local BSS symbols. |
| 260 | if (MAI->hasMachoZeroFillDirective()) { |
| 261 | const MCSection *TheSection = |
| 262 | getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM); |
| 263 | // .zerofill __DATA, __bss, _foo, 400, 5 |
| 264 | OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | if (MAI->hasLCOMMDirective()) { |
| 269 | // .lcomm _foo, 42 |
| 270 | OutStreamer.EmitLocalCommonSymbol(GVSym, Size); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | // .local _foo |
| 275 | OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Local); |
| 276 | // .comm _foo, 42, 4 |
| 277 | OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | const MCSection *TheSection = |
| 282 | getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM); |
| 283 | |
| 284 | // Handle the zerofill directive on darwin, which is a special form of BSS |
| 285 | // emission. |
| 286 | if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) { |
| 287 | // .globl _foo |
| 288 | OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global); |
| 289 | // .zerofill __DATA, __common, _foo, 400, 5 |
| 290 | OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | OutStreamer.SwitchSection(TheSection); |
| 295 | |
| 296 | EmitLinkage(GV->getLinkage(), GVSym); |
| 297 | EmitAlignment(AlignLog, GV); |
| 298 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 299 | if (isVerbose()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 300 | WriteAsOperand(OutStreamer.GetCommentOS(), GV, |
| 301 | /*PrintType=*/false, GV->getParent()); |
| 302 | OutStreamer.GetCommentOS() << '\n'; |
| 303 | } |
| 304 | OutStreamer.EmitLabel(GVSym); |
| 305 | |
| 306 | EmitGlobalConstant(GV->getInitializer()); |
| 307 | |
| 308 | if (MAI->hasDotTypeDotSizeDirective()) |
| 309 | // .size foo, 42 |
| 310 | OutStreamer.EmitELFSize(GVSym, MCConstantExpr::Create(Size, OutContext)); |
| 311 | |
| 312 | OutStreamer.AddBlankLine(); |
| 313 | } |
| 314 | |
| 315 | /// EmitFunctionHeader - This method emits the header for the current |
| 316 | /// function. |
| 317 | void AsmPrinter::EmitFunctionHeader() { |
| 318 | // Print out constants referenced by the function |
| 319 | EmitConstantPool(); |
| 320 | |
| 321 | // Print the 'header' of function. |
| 322 | const Function *F = MF->getFunction(); |
| 323 | |
| 324 | OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); |
| 325 | EmitVisibility(CurrentFnSym, F->getVisibility()); |
| 326 | |
| 327 | EmitLinkage(F->getLinkage(), CurrentFnSym); |
| 328 | EmitAlignment(MF->getAlignment(), F); |
| 329 | |
| 330 | if (MAI->hasDotTypeDotSizeDirective()) |
| 331 | OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction); |
| 332 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 333 | if (isVerbose()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 334 | WriteAsOperand(OutStreamer.GetCommentOS(), F, |
| 335 | /*PrintType=*/false, F->getParent()); |
| 336 | OutStreamer.GetCommentOS() << '\n'; |
| 337 | } |
| 338 | |
| 339 | // Emit the CurrentFnSym. This is a virtual function to allow targets to |
| 340 | // do their wild and crazy things as required. |
| 341 | EmitFunctionEntryLabel(); |
| 342 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 343 | // If the function had address-taken blocks that got deleted, then we have |
| 344 | // references to the dangling symbols. Emit them at the start of the function |
| 345 | // so that we don't get references to undefined symbols. |
| 346 | std::vector<MCSymbol*> DeadBlockSyms; |
| 347 | MMI->takeDeletedSymbolsForFunction(F, DeadBlockSyms); |
| 348 | for (unsigned i = 0, e = DeadBlockSyms.size(); i != e; ++i) { |
| 349 | OutStreamer.AddComment("Address taken block that was later removed"); |
| 350 | OutStreamer.EmitLabel(DeadBlockSyms[i]); |
| 351 | } |
| 352 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 353 | // Add some workaround for linkonce linkage on Cygwin\MinGW. |
| 354 | if (MAI->getLinkOnceDirective() != 0 && |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 355 | (F->hasLinkOnceLinkage() || F->hasWeakLinkage())) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 356 | // FIXME: What is this? |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 357 | MCSymbol *FakeStub = |
| 358 | OutContext.GetOrCreateSymbol(Twine("Lllvm$workaround$fake$stub$")+ |
| 359 | CurrentFnSym->getName()); |
| 360 | OutStreamer.EmitLabel(FakeStub); |
| 361 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 362 | |
| 363 | // Emit pre-function debug and/or EH information. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 364 | if (DE) { |
| 365 | if (TimePassesIsEnabled) { |
| 366 | NamedRegionTimer T(EHTimerName, DWARFGroupName); |
| 367 | DE->BeginFunction(MF); |
| 368 | } else { |
| 369 | DE->BeginFunction(MF); |
| 370 | } |
| 371 | } |
| 372 | if (DD) { |
| 373 | if (TimePassesIsEnabled) { |
| 374 | NamedRegionTimer T(DbgTimerName, DWARFGroupName); |
| 375 | DD->beginFunction(MF); |
| 376 | } else { |
| 377 | DD->beginFunction(MF); |
| 378 | } |
| 379 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the |
| 383 | /// function. This can be overridden by targets as required to do custom stuff. |
| 384 | void AsmPrinter::EmitFunctionEntryLabel() { |
| 385 | OutStreamer.EmitLabel(CurrentFnSym); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | /// EmitComments - Pretty-print comments for instructions. |
| 390 | static void EmitComments(const MachineInstr &MI, raw_ostream &CommentOS) { |
| 391 | const MachineFunction *MF = MI.getParent()->getParent(); |
| 392 | const TargetMachine &TM = MF->getTarget(); |
| 393 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 394 | DebugLoc DL = MI.getDebugLoc(); |
| 395 | if (!DL.isUnknown()) { // Print source line info. |
| 396 | DIScope Scope(DL.getScope(MF->getFunction()->getContext())); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 397 | // Omit the directory, because it's likely to be long and uninteresting. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 398 | if (Scope.Verify()) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 399 | CommentOS << Scope.getFilename(); |
| 400 | else |
| 401 | CommentOS << "<unknown>"; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 402 | CommentOS << ':' << DL.getLine(); |
| 403 | if (DL.getCol() != 0) |
| 404 | CommentOS << ':' << DL.getCol(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 405 | CommentOS << '\n'; |
| 406 | } |
| 407 | |
| 408 | // Check for spills and reloads |
| 409 | int FI; |
| 410 | |
| 411 | const MachineFrameInfo *FrameInfo = MF->getFrameInfo(); |
| 412 | |
| 413 | // We assume a single instruction only has a spill or reload, not |
| 414 | // both. |
| 415 | const MachineMemOperand *MMO; |
| 416 | if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) { |
| 417 | if (FrameInfo->isSpillSlotObjectIndex(FI)) { |
| 418 | MMO = *MI.memoperands_begin(); |
| 419 | CommentOS << MMO->getSize() << "-byte Reload\n"; |
| 420 | } |
| 421 | } else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) { |
| 422 | if (FrameInfo->isSpillSlotObjectIndex(FI)) |
| 423 | CommentOS << MMO->getSize() << "-byte Folded Reload\n"; |
| 424 | } else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) { |
| 425 | if (FrameInfo->isSpillSlotObjectIndex(FI)) { |
| 426 | MMO = *MI.memoperands_begin(); |
| 427 | CommentOS << MMO->getSize() << "-byte Spill\n"; |
| 428 | } |
| 429 | } else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) { |
| 430 | if (FrameInfo->isSpillSlotObjectIndex(FI)) |
| 431 | CommentOS << MMO->getSize() << "-byte Folded Spill\n"; |
| 432 | } |
| 433 | |
| 434 | // Check for spill-induced copies |
| 435 | unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx; |
| 436 | if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg, |
| 437 | SrcSubIdx, DstSubIdx)) { |
| 438 | if (MI.getAsmPrinterFlag(MachineInstr::ReloadReuse)) |
| 439 | CommentOS << " Reload Reuse\n"; |
| 440 | } |
| 441 | } |
| 442 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 443 | /// EmitImplicitDef - This method emits the specified machine instruction |
| 444 | /// that is an implicit def. |
| 445 | static void EmitImplicitDef(const MachineInstr *MI, AsmPrinter &AP) { |
| 446 | unsigned RegNo = MI->getOperand(0).getReg(); |
| 447 | AP.OutStreamer.AddComment(Twine("implicit-def: ") + |
| 448 | AP.TM.getRegisterInfo()->getName(RegNo)); |
| 449 | AP.OutStreamer.AddBlankLine(); |
| 450 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 451 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 452 | static void EmitKill(const MachineInstr *MI, AsmPrinter &AP) { |
| 453 | std::string Str = "kill:"; |
| 454 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 455 | const MachineOperand &Op = MI->getOperand(i); |
| 456 | assert(Op.isReg() && "KILL instruction must have only register operands"); |
| 457 | Str += ' '; |
| 458 | Str += AP.TM.getRegisterInfo()->getName(Op.getReg()); |
| 459 | Str += (Op.isDef() ? "<def>" : "<kill>"); |
| 460 | } |
| 461 | AP.OutStreamer.AddComment(Str); |
| 462 | AP.OutStreamer.AddBlankLine(); |
| 463 | } |
| 464 | |
| 465 | /// EmitDebugValueComment - This method handles the target-independent form |
| 466 | /// of DBG_VALUE, returning true if it was able to do so. A false return |
| 467 | /// means the target will need to handle MI in EmitInstruction. |
| 468 | static bool EmitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) { |
| 469 | // This code handles only the 3-operand target-independent form. |
| 470 | if (MI->getNumOperands() != 3) |
| 471 | return false; |
| 472 | |
| 473 | SmallString<128> Str; |
| 474 | raw_svector_ostream OS(Str); |
| 475 | OS << '\t' << AP.MAI->getCommentString() << "DEBUG_VALUE: "; |
| 476 | |
| 477 | // cast away const; DIetc do not take const operands for some reason. |
| 478 | DIVariable V(const_cast<MDNode*>(MI->getOperand(2).getMetadata())); |
| 479 | OS << V.getName() << " <- "; |
| 480 | |
| 481 | // Register or immediate value. Register 0 means undef. |
| 482 | if (MI->getOperand(0).isFPImm()) { |
| 483 | APFloat APF = APFloat(MI->getOperand(0).getFPImm()->getValueAPF()); |
| 484 | if (MI->getOperand(0).getFPImm()->getType()->isFloatTy()) { |
| 485 | OS << (double)APF.convertToFloat(); |
| 486 | } else if (MI->getOperand(0).getFPImm()->getType()->isDoubleTy()) { |
| 487 | OS << APF.convertToDouble(); |
| 488 | } else { |
| 489 | // There is no good way to print long double. Convert a copy to |
| 490 | // double. Ah well, it's only a comment. |
| 491 | bool ignored; |
| 492 | APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, |
| 493 | &ignored); |
| 494 | OS << "(long double) " << APF.convertToDouble(); |
| 495 | } |
| 496 | } else if (MI->getOperand(0).isImm()) { |
| 497 | OS << MI->getOperand(0).getImm(); |
| 498 | } else { |
| 499 | assert(MI->getOperand(0).isReg() && "Unknown operand type"); |
| 500 | if (MI->getOperand(0).getReg() == 0) { |
| 501 | // Suppress offset, it is not meaningful here. |
| 502 | OS << "undef"; |
| 503 | // NOTE: Want this comment at start of line, don't emit with AddComment. |
| 504 | AP.OutStreamer.EmitRawText(OS.str()); |
| 505 | return true; |
| 506 | } |
| 507 | OS << AP.TM.getRegisterInfo()->getName(MI->getOperand(0).getReg()); |
| 508 | } |
| 509 | |
| 510 | OS << '+' << MI->getOperand(1).getImm(); |
| 511 | // NOTE: Want this comment at start of line, don't emit with AddComment. |
| 512 | AP.OutStreamer.EmitRawText(OS.str()); |
| 513 | return true; |
| 514 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 515 | |
| 516 | /// EmitFunctionBody - This method emits the body and trailer for a |
| 517 | /// function. |
| 518 | void AsmPrinter::EmitFunctionBody() { |
| 519 | // Emit target-specific gunk before the function body. |
| 520 | EmitFunctionBodyStart(); |
| 521 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 522 | bool ShouldPrintDebugScopes = DD && MMI->hasDebugInfo(); |
| 523 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 524 | // Print out code for the function. |
| 525 | bool HasAnyRealCode = false; |
| 526 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); |
| 527 | I != E; ++I) { |
| 528 | // Print a label for the basic block. |
| 529 | EmitBasicBlockStart(I); |
| 530 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 531 | II != IE; ++II) { |
| 532 | // Print the assembly for the instruction. |
| 533 | if (!II->isLabel()) |
| 534 | HasAnyRealCode = true; |
| 535 | |
| 536 | ++EmittedInsts; |
| 537 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 538 | if (ShouldPrintDebugScopes) { |
| 539 | if (TimePassesIsEnabled) { |
| 540 | NamedRegionTimer T(DbgTimerName, DWARFGroupName); |
| 541 | DD->beginScope(II); |
| 542 | } else { |
| 543 | DD->beginScope(II); |
| 544 | } |
| 545 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 546 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 547 | if (isVerbose()) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 548 | EmitComments(*II, OutStreamer.GetCommentOS()); |
| 549 | |
| 550 | switch (II->getOpcode()) { |
| 551 | case TargetOpcode::DBG_LABEL: |
| 552 | case TargetOpcode::EH_LABEL: |
| 553 | case TargetOpcode::GC_LABEL: |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 554 | OutStreamer.EmitLabel(II->getOperand(0).getMCSymbol()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 555 | break; |
| 556 | case TargetOpcode::INLINEASM: |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 557 | EmitInlineAsm(II); |
| 558 | break; |
| 559 | case TargetOpcode::DBG_VALUE: |
| 560 | if (isVerbose()) { |
| 561 | if (!EmitDebugValueComment(II, *this)) |
| 562 | EmitInstruction(II); |
| 563 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 564 | break; |
| 565 | case TargetOpcode::IMPLICIT_DEF: |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 566 | if (isVerbose()) EmitImplicitDef(II, *this); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 567 | break; |
| 568 | case TargetOpcode::KILL: |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 569 | if (isVerbose()) EmitKill(II, *this); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 570 | break; |
| 571 | default: |
| 572 | EmitInstruction(II); |
| 573 | break; |
| 574 | } |
| 575 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 576 | if (ShouldPrintDebugScopes) { |
| 577 | if (TimePassesIsEnabled) { |
| 578 | NamedRegionTimer T(DbgTimerName, DWARFGroupName); |
| 579 | DD->endScope(II); |
| 580 | } else { |
| 581 | DD->endScope(II); |
| 582 | } |
| 583 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | // If the function is empty and the object file uses .subsections_via_symbols, |
| 588 | // then we need to emit *something* to the function body to prevent the |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 589 | // labels from collapsing together. Just emit a noop. |
| 590 | if (MAI->hasSubsectionsViaSymbols() && !HasAnyRealCode) { |
| 591 | MCInst Noop; |
| 592 | TM.getInstrInfo()->getNoopForMachoTarget(Noop); |
| 593 | if (Noop.getOpcode()) { |
| 594 | OutStreamer.AddComment("avoids zero-length function"); |
| 595 | OutStreamer.EmitInstruction(Noop); |
| 596 | } else // Target not mc-ized yet. |
| 597 | OutStreamer.EmitRawText(StringRef("\tnop\n")); |
| 598 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 599 | |
| 600 | // Emit target-specific gunk after the function body. |
| 601 | EmitFunctionBodyEnd(); |
| 602 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 603 | // If the target wants a .size directive for the size of the function, emit |
| 604 | // it. |
| 605 | if (MAI->hasDotTypeDotSizeDirective()) { |
| 606 | // Create a symbol for the end of function, so we can get the size as |
| 607 | // difference between the function label and the temp label. |
| 608 | MCSymbol *FnEndLabel = OutContext.CreateTempSymbol(); |
| 609 | OutStreamer.EmitLabel(FnEndLabel); |
| 610 | |
| 611 | const MCExpr *SizeExp = |
| 612 | MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(FnEndLabel, OutContext), |
| 613 | MCSymbolRefExpr::Create(CurrentFnSym, OutContext), |
| 614 | OutContext); |
| 615 | OutStreamer.EmitELFSize(CurrentFnSym, SizeExp); |
| 616 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 617 | |
| 618 | // Emit post-function debug information. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 619 | if (DD) { |
| 620 | if (TimePassesIsEnabled) { |
| 621 | NamedRegionTimer T(DbgTimerName, DWARFGroupName); |
| 622 | DD->endFunction(MF); |
| 623 | } else { |
| 624 | DD->endFunction(MF); |
| 625 | } |
| 626 | } |
| 627 | if (DE) { |
| 628 | if (TimePassesIsEnabled) { |
| 629 | NamedRegionTimer T(EHTimerName, DWARFGroupName); |
| 630 | DE->EndFunction(); |
| 631 | } else { |
| 632 | DE->EndFunction(); |
| 633 | } |
| 634 | } |
| 635 | MMI->EndFunction(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 636 | |
| 637 | // Print out jump tables referenced by the function. |
| 638 | EmitJumpTableInfo(); |
| 639 | |
| 640 | OutStreamer.AddBlankLine(); |
| 641 | } |
| 642 | |
| 643 | |
| 644 | bool AsmPrinter::doFinalization(Module &M) { |
| 645 | // Emit global variables. |
| 646 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 647 | I != E; ++I) |
| 648 | EmitGlobalVariable(I); |
| 649 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 650 | // Finalize debug and EH information. |
| 651 | if (DE) { |
| 652 | if (TimePassesIsEnabled) { |
| 653 | NamedRegionTimer T(EHTimerName, DWARFGroupName); |
| 654 | DE->EndModule(); |
| 655 | } else { |
| 656 | DE->EndModule(); |
| 657 | } |
| 658 | delete DE; DE = 0; |
| 659 | } |
| 660 | if (DD) { |
| 661 | if (TimePassesIsEnabled) { |
| 662 | NamedRegionTimer T(DbgTimerName, DWARFGroupName); |
| 663 | DD->endModule(); |
| 664 | } else { |
| 665 | DD->endModule(); |
| 666 | } |
| 667 | delete DD; DD = 0; |
| 668 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 669 | |
| 670 | // If the target wants to know about weak references, print them all. |
| 671 | if (MAI->getWeakRefDirective()) { |
| 672 | // FIXME: This is not lazy, it would be nice to only print weak references |
| 673 | // to stuff that is actually used. Note that doing so would require targets |
| 674 | // to notice uses in operands (due to constant exprs etc). This should |
| 675 | // happen with the MC stuff eventually. |
| 676 | |
| 677 | // Print out module-level global variables here. |
| 678 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 679 | I != E; ++I) { |
| 680 | if (!I->hasExternalWeakLinkage()) continue; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 681 | OutStreamer.EmitSymbolAttribute(Mang->getSymbol(I), MCSA_WeakReference); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 685 | if (!I->hasExternalWeakLinkage()) continue; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 686 | OutStreamer.EmitSymbolAttribute(Mang->getSymbol(I), MCSA_WeakReference); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
| 690 | if (MAI->hasSetDirective()) { |
| 691 | OutStreamer.AddBlankLine(); |
| 692 | for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 693 | I != E; ++I) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 694 | MCSymbol *Name = Mang->getSymbol(I); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 695 | |
| 696 | const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal()); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 697 | MCSymbol *Target = Mang->getSymbol(GV); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 698 | |
| 699 | if (I->hasExternalLinkage() || !MAI->getWeakRefDirective()) |
| 700 | OutStreamer.EmitSymbolAttribute(Name, MCSA_Global); |
| 701 | else if (I->hasWeakLinkage()) |
| 702 | OutStreamer.EmitSymbolAttribute(Name, MCSA_WeakReference); |
| 703 | else |
| 704 | assert(I->hasLocalLinkage() && "Invalid alias linkage"); |
| 705 | |
| 706 | EmitVisibility(Name, I->getVisibility()); |
| 707 | |
| 708 | // Emit the directives as assignments aka .set: |
| 709 | OutStreamer.EmitAssignment(Name, |
| 710 | MCSymbolRefExpr::Create(Target, OutContext)); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); |
| 715 | assert(MI && "AsmPrinter didn't require GCModuleInfo?"); |
| 716 | for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; ) |
| 717 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I)) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 718 | MP->finishAssembly(*this); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 719 | |
| 720 | // If we don't have any trampolines, then we don't require stack memory |
| 721 | // to be executable. Some targets have a directive to declare this. |
| 722 | Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline"); |
| 723 | if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty()) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 724 | if (const MCSection *S = MAI->getNonexecutableStackSection(OutContext)) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 725 | OutStreamer.SwitchSection(S); |
| 726 | |
| 727 | // Allow the target to emit any magic that it wants at the end of the file, |
| 728 | // after everything else has gone out. |
| 729 | EmitEndOfAsmFile(M); |
| 730 | |
| 731 | delete Mang; Mang = 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 732 | MMI = 0; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 733 | |
| 734 | OutStreamer.Finish(); |
| 735 | return false; |
| 736 | } |
| 737 | |
| 738 | void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { |
| 739 | this->MF = &MF; |
| 740 | // Get the function symbol. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 741 | CurrentFnSym = Mang->getSymbol(MF.getFunction()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 742 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 743 | if (isVerbose()) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 744 | LI = &getAnalysis<MachineLoopInfo>(); |
| 745 | } |
| 746 | |
| 747 | namespace { |
| 748 | // SectionCPs - Keep track the alignment, constpool entries per Section. |
| 749 | struct SectionCPs { |
| 750 | const MCSection *S; |
| 751 | unsigned Alignment; |
| 752 | SmallVector<unsigned, 4> CPEs; |
| 753 | SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {} |
| 754 | }; |
| 755 | } |
| 756 | |
| 757 | /// EmitConstantPool - Print to the current output stream assembly |
| 758 | /// representations of the constants in the constant pool MCP. This is |
| 759 | /// used to print out constants which have been "spilled to memory" by |
| 760 | /// the code generator. |
| 761 | /// |
| 762 | void AsmPrinter::EmitConstantPool() { |
| 763 | const MachineConstantPool *MCP = MF->getConstantPool(); |
| 764 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
| 765 | if (CP.empty()) return; |
| 766 | |
| 767 | // Calculate sections for constant pool entries. We collect entries to go into |
| 768 | // the same section together to reduce amount of section switch statements. |
| 769 | SmallVector<SectionCPs, 4> CPSections; |
| 770 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 771 | const MachineConstantPoolEntry &CPE = CP[i]; |
| 772 | unsigned Align = CPE.getAlignment(); |
| 773 | |
| 774 | SectionKind Kind; |
| 775 | switch (CPE.getRelocationInfo()) { |
| 776 | default: llvm_unreachable("Unknown section kind"); |
| 777 | case 2: Kind = SectionKind::getReadOnlyWithRel(); break; |
| 778 | case 1: |
| 779 | Kind = SectionKind::getReadOnlyWithRelLocal(); |
| 780 | break; |
| 781 | case 0: |
| 782 | switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) { |
| 783 | case 4: Kind = SectionKind::getMergeableConst4(); break; |
| 784 | case 8: Kind = SectionKind::getMergeableConst8(); break; |
| 785 | case 16: Kind = SectionKind::getMergeableConst16();break; |
| 786 | default: Kind = SectionKind::getMergeableConst(); break; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | const MCSection *S = getObjFileLowering().getSectionForConstant(Kind); |
| 791 | |
| 792 | // The number of sections are small, just do a linear search from the |
| 793 | // last section to the first. |
| 794 | bool Found = false; |
| 795 | unsigned SecIdx = CPSections.size(); |
| 796 | while (SecIdx != 0) { |
| 797 | if (CPSections[--SecIdx].S == S) { |
| 798 | Found = true; |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | if (!Found) { |
| 803 | SecIdx = CPSections.size(); |
| 804 | CPSections.push_back(SectionCPs(S, Align)); |
| 805 | } |
| 806 | |
| 807 | if (Align > CPSections[SecIdx].Alignment) |
| 808 | CPSections[SecIdx].Alignment = Align; |
| 809 | CPSections[SecIdx].CPEs.push_back(i); |
| 810 | } |
| 811 | |
| 812 | // Now print stuff into the calculated sections. |
| 813 | for (unsigned i = 0, e = CPSections.size(); i != e; ++i) { |
| 814 | OutStreamer.SwitchSection(CPSections[i].S); |
| 815 | EmitAlignment(Log2_32(CPSections[i].Alignment)); |
| 816 | |
| 817 | unsigned Offset = 0; |
| 818 | for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) { |
| 819 | unsigned CPI = CPSections[i].CPEs[j]; |
| 820 | MachineConstantPoolEntry CPE = CP[CPI]; |
| 821 | |
| 822 | // Emit inter-object padding for alignment. |
| 823 | unsigned AlignMask = CPE.getAlignment() - 1; |
| 824 | unsigned NewOffset = (Offset + AlignMask) & ~AlignMask; |
| 825 | OutStreamer.EmitFill(NewOffset - Offset, 0/*fillval*/, 0/*addrspace*/); |
| 826 | |
| 827 | const Type *Ty = CPE.getType(); |
| 828 | Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty); |
| 829 | |
| 830 | // Emit the label with a comment on it. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 831 | if (isVerbose()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 832 | OutStreamer.GetCommentOS() << "constant pool "; |
| 833 | WriteTypeSymbolic(OutStreamer.GetCommentOS(), CPE.getType(), |
| 834 | MF->getFunction()->getParent()); |
| 835 | OutStreamer.GetCommentOS() << '\n'; |
| 836 | } |
| 837 | OutStreamer.EmitLabel(GetCPISymbol(CPI)); |
| 838 | |
| 839 | if (CPE.isMachineConstantPoolEntry()) |
| 840 | EmitMachineConstantPoolValue(CPE.Val.MachineCPVal); |
| 841 | else |
| 842 | EmitGlobalConstant(CPE.Val.ConstVal); |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /// EmitJumpTableInfo - Print assembly representations of the jump tables used |
| 848 | /// by the current function to the current output stream. |
| 849 | /// |
| 850 | void AsmPrinter::EmitJumpTableInfo() { |
| 851 | const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); |
| 852 | if (MJTI == 0) return; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 853 | if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) return; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 854 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 855 | if (JT.empty()) return; |
| 856 | |
| 857 | // Pick the directive to use to print the jump table entries, and switch to |
| 858 | // the appropriate section. |
| 859 | const Function *F = MF->getFunction(); |
| 860 | bool JTInDiffSection = false; |
| 861 | if (// In PIC mode, we need to emit the jump table to the same section as the |
| 862 | // function body itself, otherwise the label differences won't make sense. |
| 863 | // FIXME: Need a better predicate for this: what about custom entries? |
| 864 | MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 || |
| 865 | // We should also do if the section name is NULL or function is declared |
| 866 | // in discardable section |
| 867 | // FIXME: this isn't the right predicate, should be based on the MCSection |
| 868 | // for the function. |
| 869 | F->isWeakForLinker()) { |
| 870 | OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F,Mang,TM)); |
| 871 | } else { |
| 872 | // Otherwise, drop it in the readonly section. |
| 873 | const MCSection *ReadOnlySection = |
| 874 | getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly()); |
| 875 | OutStreamer.SwitchSection(ReadOnlySection); |
| 876 | JTInDiffSection = true; |
| 877 | } |
| 878 | |
| 879 | EmitAlignment(Log2_32(MJTI->getEntryAlignment(*TM.getTargetData()))); |
| 880 | |
| 881 | for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) { |
| 882 | const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; |
| 883 | |
| 884 | // If this jump table was deleted, ignore it. |
| 885 | if (JTBBs.empty()) continue; |
| 886 | |
| 887 | // For the EK_LabelDifference32 entry, if the target supports .set, emit a |
| 888 | // .set directive for each unique entry. This reduces the number of |
| 889 | // relocations the assembler will generate for the jump table. |
| 890 | if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 && |
| 891 | MAI->hasSetDirective()) { |
| 892 | SmallPtrSet<const MachineBasicBlock*, 16> EmittedSets; |
| 893 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 894 | const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext); |
| 895 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { |
| 896 | const MachineBasicBlock *MBB = JTBBs[ii]; |
| 897 | if (!EmittedSets.insert(MBB)) continue; |
| 898 | |
| 899 | // .set LJTSet, LBB32-base |
| 900 | const MCExpr *LHS = |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 901 | MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 902 | OutStreamer.EmitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()), |
| 903 | MCBinaryExpr::CreateSub(LHS, Base, OutContext)); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | // On some targets (e.g. Darwin) we want to emit two consequtive labels |
| 908 | // before each jump table. The first label is never referenced, but tells |
| 909 | // the assembler and linker the extents of the jump table object. The |
| 910 | // second label is actually referenced by the code. |
| 911 | if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) |
| 912 | // FIXME: This doesn't have to have any specific name, just any randomly |
| 913 | // named and numbered 'l' label would work. Simplify GetJTISymbol. |
| 914 | OutStreamer.EmitLabel(GetJTISymbol(JTI, true)); |
| 915 | |
| 916 | OutStreamer.EmitLabel(GetJTISymbol(JTI)); |
| 917 | |
| 918 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) |
| 919 | EmitJumpTableEntry(MJTI, JTBBs[ii], JTI); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | /// EmitJumpTableEntry - Emit a jump table entry for the specified MBB to the |
| 924 | /// current stream. |
| 925 | void AsmPrinter::EmitJumpTableEntry(const MachineJumpTableInfo *MJTI, |
| 926 | const MachineBasicBlock *MBB, |
| 927 | unsigned UID) const { |
| 928 | const MCExpr *Value = 0; |
| 929 | switch (MJTI->getEntryKind()) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 930 | case MachineJumpTableInfo::EK_Inline: |
| 931 | llvm_unreachable("Cannot emit EK_Inline jump table entry"); break; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 932 | case MachineJumpTableInfo::EK_Custom32: |
| 933 | Value = TM.getTargetLowering()->LowerCustomJumpTableEntry(MJTI, MBB, UID, |
| 934 | OutContext); |
| 935 | break; |
| 936 | case MachineJumpTableInfo::EK_BlockAddress: |
| 937 | // EK_BlockAddress - Each entry is a plain address of block, e.g.: |
| 938 | // .word LBB123 |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 939 | Value = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 940 | break; |
| 941 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: { |
| 942 | // EK_GPRel32BlockAddress - Each entry is an address of block, encoded |
| 943 | // with a relocation as gp-relative, e.g.: |
| 944 | // .gprel32 LBB123 |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 945 | MCSymbol *MBBSym = MBB->getSymbol(); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 946 | OutStreamer.EmitGPRel32Value(MCSymbolRefExpr::Create(MBBSym, OutContext)); |
| 947 | return; |
| 948 | } |
| 949 | |
| 950 | case MachineJumpTableInfo::EK_LabelDifference32: { |
| 951 | // EK_LabelDifference32 - Each entry is the address of the block minus |
| 952 | // the address of the jump table. This is used for PIC jump tables where |
| 953 | // gprel32 is not supported. e.g.: |
| 954 | // .word LBB123 - LJTI1_2 |
| 955 | // If the .set directive is supported, this is emitted as: |
| 956 | // .set L4_5_set_123, LBB123 - LJTI1_2 |
| 957 | // .word L4_5_set_123 |
| 958 | |
| 959 | // If we have emitted set directives for the jump table entries, print |
| 960 | // them rather than the entries themselves. If we're emitting PIC, then |
| 961 | // emit the table entries as differences between two text section labels. |
| 962 | if (MAI->hasSetDirective()) { |
| 963 | // If we used .set, reference the .set's symbol. |
| 964 | Value = MCSymbolRefExpr::Create(GetJTSetSymbol(UID, MBB->getNumber()), |
| 965 | OutContext); |
| 966 | break; |
| 967 | } |
| 968 | // Otherwise, use the difference as the jump table entry. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 969 | Value = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 970 | const MCExpr *JTI = MCSymbolRefExpr::Create(GetJTISymbol(UID), OutContext); |
| 971 | Value = MCBinaryExpr::CreateSub(Value, JTI, OutContext); |
| 972 | break; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | assert(Value && "Unknown entry kind!"); |
| 977 | |
| 978 | unsigned EntrySize = MJTI->getEntrySize(*TM.getTargetData()); |
| 979 | OutStreamer.EmitValue(Value, EntrySize, /*addrspace*/0); |
| 980 | } |
| 981 | |
| 982 | |
| 983 | /// EmitSpecialLLVMGlobal - Check to see if the specified global is a |
| 984 | /// special global used by LLVM. If so, emit it and return true, otherwise |
| 985 | /// do nothing and return false. |
| 986 | bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { |
| 987 | if (GV->getName() == "llvm.used") { |
| 988 | if (MAI->hasNoDeadStrip()) // No need to emit this at all. |
| 989 | EmitLLVMUsedList(GV->getInitializer()); |
| 990 | return true; |
| 991 | } |
| 992 | |
| 993 | // Ignore debug and non-emitted data. This handles llvm.compiler.used. |
| 994 | if (GV->getSection() == "llvm.metadata" || |
| 995 | GV->hasAvailableExternallyLinkage()) |
| 996 | return true; |
| 997 | |
| 998 | if (!GV->hasAppendingLinkage()) return false; |
| 999 | |
| 1000 | assert(GV->hasInitializer() && "Not a special LLVM global!"); |
| 1001 | |
| 1002 | const TargetData *TD = TM.getTargetData(); |
| 1003 | unsigned Align = Log2_32(TD->getPointerPrefAlignment()); |
| 1004 | if (GV->getName() == "llvm.global_ctors") { |
| 1005 | OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection()); |
| 1006 | EmitAlignment(Align, 0); |
| 1007 | EmitXXStructorList(GV->getInitializer()); |
| 1008 | |
| 1009 | if (TM.getRelocationModel() == Reloc::Static && |
| 1010 | MAI->hasStaticCtorDtorReferenceInStaticMode()) { |
| 1011 | StringRef Sym(".constructors_used"); |
| 1012 | OutStreamer.EmitSymbolAttribute(OutContext.GetOrCreateSymbol(Sym), |
| 1013 | MCSA_Reference); |
| 1014 | } |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
| 1018 | if (GV->getName() == "llvm.global_dtors") { |
| 1019 | OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection()); |
| 1020 | EmitAlignment(Align, 0); |
| 1021 | EmitXXStructorList(GV->getInitializer()); |
| 1022 | |
| 1023 | if (TM.getRelocationModel() == Reloc::Static && |
| 1024 | MAI->hasStaticCtorDtorReferenceInStaticMode()) { |
| 1025 | StringRef Sym(".destructors_used"); |
| 1026 | OutStreamer.EmitSymbolAttribute(OutContext.GetOrCreateSymbol(Sym), |
| 1027 | MCSA_Reference); |
| 1028 | } |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | return false; |
| 1033 | } |
| 1034 | |
| 1035 | /// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each |
| 1036 | /// global in the specified llvm.used list for which emitUsedDirectiveFor |
| 1037 | /// is true, as being used with this directive. |
| 1038 | void AsmPrinter::EmitLLVMUsedList(Constant *List) { |
| 1039 | // Should be an array of 'i8*'. |
| 1040 | ConstantArray *InitList = dyn_cast<ConstantArray>(List); |
| 1041 | if (InitList == 0) return; |
| 1042 | |
| 1043 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
| 1044 | const GlobalValue *GV = |
| 1045 | dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts()); |
| 1046 | if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1047 | OutStreamer.EmitSymbolAttribute(Mang->getSymbol(GV), MCSA_NoDeadStrip); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the |
| 1052 | /// function pointers, ignoring the init priority. |
| 1053 | void AsmPrinter::EmitXXStructorList(Constant *List) { |
| 1054 | // Should be an array of '{ int, void ()* }' structs. The first value is the |
| 1055 | // init priority, which we ignore. |
| 1056 | if (!isa<ConstantArray>(List)) return; |
| 1057 | ConstantArray *InitList = cast<ConstantArray>(List); |
| 1058 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 1059 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 1060 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
| 1061 | |
| 1062 | if (CS->getOperand(1)->isNullValue()) |
| 1063 | return; // Found a null terminator, exit printing. |
| 1064 | // Emit the function pointer. |
| 1065 | EmitGlobalConstant(CS->getOperand(1)); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | //===--------------------------------------------------------------------===// |
| 1070 | // Emission and print routines |
| 1071 | // |
| 1072 | |
| 1073 | /// EmitInt8 - Emit a byte directive and value. |
| 1074 | /// |
| 1075 | void AsmPrinter::EmitInt8(int Value) const { |
| 1076 | OutStreamer.EmitIntValue(Value, 1, 0/*addrspace*/); |
| 1077 | } |
| 1078 | |
| 1079 | /// EmitInt16 - Emit a short directive and value. |
| 1080 | /// |
| 1081 | void AsmPrinter::EmitInt16(int Value) const { |
| 1082 | OutStreamer.EmitIntValue(Value, 2, 0/*addrspace*/); |
| 1083 | } |
| 1084 | |
| 1085 | /// EmitInt32 - Emit a long directive and value. |
| 1086 | /// |
| 1087 | void AsmPrinter::EmitInt32(int Value) const { |
| 1088 | OutStreamer.EmitIntValue(Value, 4, 0/*addrspace*/); |
| 1089 | } |
| 1090 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1091 | /// EmitLabelDifference - Emit something like ".long Hi-Lo" where the size |
| 1092 | /// in bytes of the directive is specified by Size and Hi/Lo specify the |
| 1093 | /// labels. This implicitly uses .set if it is available. |
| 1094 | void AsmPrinter::EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, |
| 1095 | unsigned Size) const { |
| 1096 | // Get the Hi-Lo expression. |
| 1097 | const MCExpr *Diff = |
| 1098 | MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(Hi, OutContext), |
| 1099 | MCSymbolRefExpr::Create(Lo, OutContext), |
| 1100 | OutContext); |
| 1101 | |
| 1102 | if (!MAI->hasSetDirective()) { |
| 1103 | OutStreamer.EmitValue(Diff, Size, 0/*AddrSpace*/); |
| 1104 | return; |
| 1105 | } |
| 1106 | |
| 1107 | // Otherwise, emit with .set (aka assignment). |
| 1108 | MCSymbol *SetLabel = GetTempSymbol("set", SetCounter++); |
| 1109 | OutStreamer.EmitAssignment(SetLabel, Diff); |
| 1110 | OutStreamer.EmitSymbolValue(SetLabel, Size, 0/*AddrSpace*/); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1113 | /// EmitLabelOffsetDifference - Emit something like ".long Hi+Offset-Lo" |
| 1114 | /// where the size in bytes of the directive is specified by Size and Hi/Lo |
| 1115 | /// specify the labels. This implicitly uses .set if it is available. |
| 1116 | void AsmPrinter::EmitLabelOffsetDifference(const MCSymbol *Hi, uint64_t Offset, |
| 1117 | const MCSymbol *Lo, unsigned Size) |
| 1118 | const { |
| 1119 | |
| 1120 | // Emit Hi+Offset - Lo |
| 1121 | // Get the Hi+Offset expression. |
| 1122 | const MCExpr *Plus = |
| 1123 | MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Hi, OutContext), |
| 1124 | MCConstantExpr::Create(Offset, OutContext), |
| 1125 | OutContext); |
| 1126 | |
| 1127 | // Get the Hi+Offset-Lo expression. |
| 1128 | const MCExpr *Diff = |
| 1129 | MCBinaryExpr::CreateSub(Plus, |
| 1130 | MCSymbolRefExpr::Create(Lo, OutContext), |
| 1131 | OutContext); |
| 1132 | |
| 1133 | if (!MAI->hasSetDirective()) |
| 1134 | OutStreamer.EmitValue(Diff, 4, 0/*AddrSpace*/); |
| 1135 | else { |
| 1136 | // Otherwise, emit with .set (aka assignment). |
| 1137 | MCSymbol *SetLabel = GetTempSymbol("set", SetCounter++); |
| 1138 | OutStreamer.EmitAssignment(SetLabel, Diff); |
| 1139 | OutStreamer.EmitSymbolValue(SetLabel, 4, 0/*AddrSpace*/); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1144 | //===----------------------------------------------------------------------===// |
| 1145 | |
| 1146 | // EmitAlignment - Emit an alignment directive to the specified power of |
| 1147 | // two boundary. For example, if you pass in 3 here, you will get an 8 |
| 1148 | // byte alignment. If a global value is specified, and if that global has |
| 1149 | // an explicit alignment requested, it will unconditionally override the |
| 1150 | // alignment request. However, if ForcedAlignBits is specified, this value |
| 1151 | // has final say: the ultimate alignment will be the max of ForcedAlignBits |
| 1152 | // and the alignment computed with NumBits and the global. |
| 1153 | // |
| 1154 | // The algorithm is: |
| 1155 | // Align = NumBits; |
| 1156 | // if (GV && GV->hasalignment) Align = GV->getalignment(); |
| 1157 | // Align = std::max(Align, ForcedAlignBits); |
| 1158 | // |
| 1159 | void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV, |
| 1160 | unsigned ForcedAlignBits, |
| 1161 | bool UseFillExpr) const { |
| 1162 | if (GV && GV->getAlignment()) |
| 1163 | NumBits = Log2_32(GV->getAlignment()); |
| 1164 | NumBits = std::max(NumBits, ForcedAlignBits); |
| 1165 | |
| 1166 | if (NumBits == 0) return; // No need to emit alignment. |
| 1167 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1168 | if (getCurrentSection()->getKind().isText()) |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1169 | OutStreamer.EmitCodeAlignment(1 << NumBits); |
| 1170 | else |
| 1171 | OutStreamer.EmitValueToAlignment(1 << NumBits, 0, 1, 0); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1174 | //===----------------------------------------------------------------------===// |
| 1175 | // Constant emission. |
| 1176 | //===----------------------------------------------------------------------===// |
| 1177 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1178 | /// LowerConstant - Lower the specified LLVM Constant to an MCExpr. |
| 1179 | /// |
| 1180 | static const MCExpr *LowerConstant(const Constant *CV, AsmPrinter &AP) { |
| 1181 | MCContext &Ctx = AP.OutContext; |
| 1182 | |
| 1183 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
| 1184 | return MCConstantExpr::Create(0, Ctx); |
| 1185 | |
| 1186 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) |
| 1187 | return MCConstantExpr::Create(CI->getZExtValue(), Ctx); |
| 1188 | |
| 1189 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1190 | return MCSymbolRefExpr::Create(AP.Mang->getSymbol(GV), Ctx); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1191 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) |
| 1192 | return MCSymbolRefExpr::Create(AP.GetBlockAddressSymbol(BA), Ctx); |
| 1193 | |
| 1194 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV); |
| 1195 | if (CE == 0) { |
| 1196 | llvm_unreachable("Unknown constant value to lower!"); |
| 1197 | return MCConstantExpr::Create(0, Ctx); |
| 1198 | } |
| 1199 | |
| 1200 | switch (CE->getOpcode()) { |
| 1201 | default: |
| 1202 | // If the code isn't optimized, there may be outstanding folding |
| 1203 | // opportunities. Attempt to fold the expression using TargetData as a |
| 1204 | // last resort before giving up. |
| 1205 | if (Constant *C = |
| 1206 | ConstantFoldConstantExpression(CE, AP.TM.getTargetData())) |
| 1207 | if (C != CE) |
| 1208 | return LowerConstant(C, AP); |
| 1209 | #ifndef NDEBUG |
| 1210 | CE->dump(); |
| 1211 | #endif |
| 1212 | llvm_unreachable("FIXME: Don't support this constant expr"); |
| 1213 | case Instruction::GetElementPtr: { |
| 1214 | const TargetData &TD = *AP.TM.getTargetData(); |
| 1215 | // Generate a symbolic expression for the byte address |
| 1216 | const Constant *PtrVal = CE->getOperand(0); |
| 1217 | SmallVector<Value*, 8> IdxVec(CE->op_begin()+1, CE->op_end()); |
| 1218 | int64_t Offset = TD.getIndexedOffset(PtrVal->getType(), &IdxVec[0], |
| 1219 | IdxVec.size()); |
| 1220 | |
| 1221 | const MCExpr *Base = LowerConstant(CE->getOperand(0), AP); |
| 1222 | if (Offset == 0) |
| 1223 | return Base; |
| 1224 | |
| 1225 | // Truncate/sext the offset to the pointer size. |
| 1226 | if (TD.getPointerSizeInBits() != 64) { |
| 1227 | int SExtAmount = 64-TD.getPointerSizeInBits(); |
| 1228 | Offset = (Offset << SExtAmount) >> SExtAmount; |
| 1229 | } |
| 1230 | |
| 1231 | return MCBinaryExpr::CreateAdd(Base, MCConstantExpr::Create(Offset, Ctx), |
| 1232 | Ctx); |
| 1233 | } |
| 1234 | |
| 1235 | case Instruction::Trunc: |
| 1236 | // We emit the value and depend on the assembler to truncate the generated |
| 1237 | // expression properly. This is important for differences between |
| 1238 | // blockaddress labels. Since the two labels are in the same function, it |
| 1239 | // is reasonable to treat their delta as a 32-bit value. |
| 1240 | // FALL THROUGH. |
| 1241 | case Instruction::BitCast: |
| 1242 | return LowerConstant(CE->getOperand(0), AP); |
| 1243 | |
| 1244 | case Instruction::IntToPtr: { |
| 1245 | const TargetData &TD = *AP.TM.getTargetData(); |
| 1246 | // Handle casts to pointers by changing them into casts to the appropriate |
| 1247 | // integer type. This promotes constant folding and simplifies this code. |
| 1248 | Constant *Op = CE->getOperand(0); |
| 1249 | Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CV->getContext()), |
| 1250 | false/*ZExt*/); |
| 1251 | return LowerConstant(Op, AP); |
| 1252 | } |
| 1253 | |
| 1254 | case Instruction::PtrToInt: { |
| 1255 | const TargetData &TD = *AP.TM.getTargetData(); |
| 1256 | // Support only foldable casts to/from pointers that can be eliminated by |
| 1257 | // changing the pointer to the appropriately sized integer type. |
| 1258 | Constant *Op = CE->getOperand(0); |
| 1259 | const Type *Ty = CE->getType(); |
| 1260 | |
| 1261 | const MCExpr *OpExpr = LowerConstant(Op, AP); |
| 1262 | |
| 1263 | // We can emit the pointer value into this slot if the slot is an |
| 1264 | // integer slot equal to the size of the pointer. |
| 1265 | if (TD.getTypeAllocSize(Ty) == TD.getTypeAllocSize(Op->getType())) |
| 1266 | return OpExpr; |
| 1267 | |
| 1268 | // Otherwise the pointer is smaller than the resultant integer, mask off |
| 1269 | // the high bits so we are sure to get a proper truncation if the input is |
| 1270 | // a constant expr. |
| 1271 | unsigned InBits = TD.getTypeAllocSizeInBits(Op->getType()); |
| 1272 | const MCExpr *MaskExpr = MCConstantExpr::Create(~0ULL >> (64-InBits), Ctx); |
| 1273 | return MCBinaryExpr::CreateAnd(OpExpr, MaskExpr, Ctx); |
| 1274 | } |
| 1275 | |
| 1276 | // The MC library also has a right-shift operator, but it isn't consistently |
| 1277 | // signed or unsigned between different targets. |
| 1278 | case Instruction::Add: |
| 1279 | case Instruction::Sub: |
| 1280 | case Instruction::Mul: |
| 1281 | case Instruction::SDiv: |
| 1282 | case Instruction::SRem: |
| 1283 | case Instruction::Shl: |
| 1284 | case Instruction::And: |
| 1285 | case Instruction::Or: |
| 1286 | case Instruction::Xor: { |
| 1287 | const MCExpr *LHS = LowerConstant(CE->getOperand(0), AP); |
| 1288 | const MCExpr *RHS = LowerConstant(CE->getOperand(1), AP); |
| 1289 | switch (CE->getOpcode()) { |
| 1290 | default: llvm_unreachable("Unknown binary operator constant cast expr"); |
| 1291 | case Instruction::Add: return MCBinaryExpr::CreateAdd(LHS, RHS, Ctx); |
| 1292 | case Instruction::Sub: return MCBinaryExpr::CreateSub(LHS, RHS, Ctx); |
| 1293 | case Instruction::Mul: return MCBinaryExpr::CreateMul(LHS, RHS, Ctx); |
| 1294 | case Instruction::SDiv: return MCBinaryExpr::CreateDiv(LHS, RHS, Ctx); |
| 1295 | case Instruction::SRem: return MCBinaryExpr::CreateMod(LHS, RHS, Ctx); |
| 1296 | case Instruction::Shl: return MCBinaryExpr::CreateShl(LHS, RHS, Ctx); |
| 1297 | case Instruction::And: return MCBinaryExpr::CreateAnd(LHS, RHS, Ctx); |
| 1298 | case Instruction::Or: return MCBinaryExpr::CreateOr (LHS, RHS, Ctx); |
| 1299 | case Instruction::Xor: return MCBinaryExpr::CreateXor(LHS, RHS, Ctx); |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1305 | static void EmitGlobalConstantImpl(const Constant *C, unsigned AddrSpace, |
| 1306 | AsmPrinter &AP); |
| 1307 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1308 | static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace, |
| 1309 | AsmPrinter &AP) { |
| 1310 | if (AddrSpace != 0 || !CA->isString()) { |
| 1311 | // Not a string. Print the values in successive locations |
| 1312 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1313 | EmitGlobalConstantImpl(CA->getOperand(i), AddrSpace, AP); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | |
| 1317 | // Otherwise, it can be emitted as .ascii. |
| 1318 | SmallVector<char, 128> TmpVec; |
| 1319 | TmpVec.reserve(CA->getNumOperands()); |
| 1320 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) |
| 1321 | TmpVec.push_back(cast<ConstantInt>(CA->getOperand(i))->getZExtValue()); |
| 1322 | |
| 1323 | AP.OutStreamer.EmitBytes(StringRef(TmpVec.data(), TmpVec.size()), AddrSpace); |
| 1324 | } |
| 1325 | |
| 1326 | static void EmitGlobalConstantVector(const ConstantVector *CV, |
| 1327 | unsigned AddrSpace, AsmPrinter &AP) { |
| 1328 | for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1329 | EmitGlobalConstantImpl(CV->getOperand(i), AddrSpace, AP); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | static void EmitGlobalConstantStruct(const ConstantStruct *CS, |
| 1333 | unsigned AddrSpace, AsmPrinter &AP) { |
| 1334 | // Print the fields in successive locations. Pad to align if needed! |
| 1335 | const TargetData *TD = AP.TM.getTargetData(); |
| 1336 | unsigned Size = TD->getTypeAllocSize(CS->getType()); |
| 1337 | const StructLayout *Layout = TD->getStructLayout(CS->getType()); |
| 1338 | uint64_t SizeSoFar = 0; |
| 1339 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) { |
| 1340 | const Constant *Field = CS->getOperand(i); |
| 1341 | |
| 1342 | // Check if padding is needed and insert one or more 0s. |
| 1343 | uint64_t FieldSize = TD->getTypeAllocSize(Field->getType()); |
| 1344 | uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1)) |
| 1345 | - Layout->getElementOffset(i)) - FieldSize; |
| 1346 | SizeSoFar += FieldSize + PadSize; |
| 1347 | |
| 1348 | // Now print the actual field value. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1349 | EmitGlobalConstantImpl(Field, AddrSpace, AP); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1350 | |
| 1351 | // Insert padding - this may include padding to increase the size of the |
| 1352 | // current field up to the ABI size (if the struct is not packed) as well |
| 1353 | // as padding to ensure that the next field starts at the right offset. |
| 1354 | AP.OutStreamer.EmitZeros(PadSize, AddrSpace); |
| 1355 | } |
| 1356 | assert(SizeSoFar == Layout->getSizeInBytes() && |
| 1357 | "Layout of constant struct may be incorrect!"); |
| 1358 | } |
| 1359 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1360 | static void EmitGlobalConstantUnion(const ConstantUnion *CU, |
| 1361 | unsigned AddrSpace, AsmPrinter &AP) { |
| 1362 | const TargetData *TD = AP.TM.getTargetData(); |
| 1363 | unsigned Size = TD->getTypeAllocSize(CU->getType()); |
| 1364 | |
| 1365 | const Constant *Contents = CU->getOperand(0); |
| 1366 | unsigned FilledSize = TD->getTypeAllocSize(Contents->getType()); |
| 1367 | |
| 1368 | // Print the actually filled part |
| 1369 | EmitGlobalConstantImpl(Contents, AddrSpace, AP); |
| 1370 | |
| 1371 | // And pad with enough zeroes |
| 1372 | AP.OutStreamer.EmitZeros(Size-FilledSize, AddrSpace); |
| 1373 | } |
| 1374 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1375 | static void EmitGlobalConstantFP(const ConstantFP *CFP, unsigned AddrSpace, |
| 1376 | AsmPrinter &AP) { |
| 1377 | // FP Constants are printed as integer constants to avoid losing |
| 1378 | // precision. |
| 1379 | if (CFP->getType()->isDoubleTy()) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1380 | if (AP.isVerbose()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1381 | double Val = CFP->getValueAPF().convertToDouble(); |
| 1382 | AP.OutStreamer.GetCommentOS() << "double " << Val << '\n'; |
| 1383 | } |
| 1384 | |
| 1385 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 1386 | AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace); |
| 1387 | return; |
| 1388 | } |
| 1389 | |
| 1390 | if (CFP->getType()->isFloatTy()) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1391 | if (AP.isVerbose()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1392 | float Val = CFP->getValueAPF().convertToFloat(); |
| 1393 | AP.OutStreamer.GetCommentOS() << "float " << Val << '\n'; |
| 1394 | } |
| 1395 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 1396 | AP.OutStreamer.EmitIntValue(Val, 4, AddrSpace); |
| 1397 | return; |
| 1398 | } |
| 1399 | |
| 1400 | if (CFP->getType()->isX86_FP80Ty()) { |
| 1401 | // all long double variants are printed as hex |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1402 | // API needed to prevent premature destruction |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1403 | APInt API = CFP->getValueAPF().bitcastToAPInt(); |
| 1404 | const uint64_t *p = API.getRawData(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1405 | if (AP.isVerbose()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1406 | // Convert to double so we can print the approximate val as a comment. |
| 1407 | APFloat DoubleVal = CFP->getValueAPF(); |
| 1408 | bool ignored; |
| 1409 | DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, |
| 1410 | &ignored); |
| 1411 | AP.OutStreamer.GetCommentOS() << "x86_fp80 ~= " |
| 1412 | << DoubleVal.convertToDouble() << '\n'; |
| 1413 | } |
| 1414 | |
| 1415 | if (AP.TM.getTargetData()->isBigEndian()) { |
| 1416 | AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace); |
| 1417 | AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace); |
| 1418 | } else { |
| 1419 | AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace); |
| 1420 | AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace); |
| 1421 | } |
| 1422 | |
| 1423 | // Emit the tail padding for the long double. |
| 1424 | const TargetData &TD = *AP.TM.getTargetData(); |
| 1425 | AP.OutStreamer.EmitZeros(TD.getTypeAllocSize(CFP->getType()) - |
| 1426 | TD.getTypeStoreSize(CFP->getType()), AddrSpace); |
| 1427 | return; |
| 1428 | } |
| 1429 | |
| 1430 | assert(CFP->getType()->isPPC_FP128Ty() && |
| 1431 | "Floating point constant type not handled"); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1432 | // All long double variants are printed as hex |
| 1433 | // API needed to prevent premature destruction. |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1434 | APInt API = CFP->getValueAPF().bitcastToAPInt(); |
| 1435 | const uint64_t *p = API.getRawData(); |
| 1436 | if (AP.TM.getTargetData()->isBigEndian()) { |
| 1437 | AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace); |
| 1438 | AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace); |
| 1439 | } else { |
| 1440 | AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace); |
| 1441 | AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | static void EmitGlobalConstantLargeInt(const ConstantInt *CI, |
| 1446 | unsigned AddrSpace, AsmPrinter &AP) { |
| 1447 | const TargetData *TD = AP.TM.getTargetData(); |
| 1448 | unsigned BitWidth = CI->getBitWidth(); |
| 1449 | assert((BitWidth & 63) == 0 && "only support multiples of 64-bits"); |
| 1450 | |
| 1451 | // We don't expect assemblers to support integer data directives |
| 1452 | // for more than 64 bits, so we emit the data in at most 64-bit |
| 1453 | // quantities at a time. |
| 1454 | const uint64_t *RawData = CI->getValue().getRawData(); |
| 1455 | for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) { |
| 1456 | uint64_t Val = TD->isBigEndian() ? RawData[e - i - 1] : RawData[i]; |
| 1457 | AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace); |
| 1458 | } |
| 1459 | } |
| 1460 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1461 | static void EmitGlobalConstantImpl(const Constant *CV, unsigned AddrSpace, |
| 1462 | AsmPrinter &AP) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1463 | if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV)) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1464 | uint64_t Size = AP.TM.getTargetData()->getTypeAllocSize(CV->getType()); |
| 1465 | return AP.OutStreamer.EmitZeros(Size, AddrSpace); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1469 | unsigned Size = AP.TM.getTargetData()->getTypeAllocSize(CV->getType()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1470 | switch (Size) { |
| 1471 | case 1: |
| 1472 | case 2: |
| 1473 | case 4: |
| 1474 | case 8: |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1475 | if (AP.isVerbose()) |
| 1476 | AP.OutStreamer.GetCommentOS() << format("0x%llx\n", CI->getZExtValue()); |
| 1477 | AP.OutStreamer.EmitIntValue(CI->getZExtValue(), Size, AddrSpace); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1478 | return; |
| 1479 | default: |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1480 | EmitGlobalConstantLargeInt(CI, AddrSpace, AP); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1481 | return; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1486 | return EmitGlobalConstantArray(CVA, AddrSpace, AP); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1487 | |
| 1488 | if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1489 | return EmitGlobalConstantStruct(CVS, AddrSpace, AP); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1490 | |
| 1491 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1492 | return EmitGlobalConstantFP(CFP, AddrSpace, AP); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1493 | |
| 1494 | if (isa<ConstantPointerNull>(CV)) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1495 | unsigned Size = AP.TM.getTargetData()->getTypeAllocSize(CV->getType()); |
| 1496 | AP.OutStreamer.EmitIntValue(0, Size, AddrSpace); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1497 | return; |
| 1498 | } |
| 1499 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1500 | if (const ConstantUnion *CVU = dyn_cast<ConstantUnion>(CV)) |
| 1501 | return EmitGlobalConstantUnion(CVU, AddrSpace, AP); |
| 1502 | |
| 1503 | if (const ConstantVector *V = dyn_cast<ConstantVector>(CV)) |
| 1504 | return EmitGlobalConstantVector(V, AddrSpace, AP); |
| 1505 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1506 | // Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it |
| 1507 | // thread the streamer with EmitValue. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1508 | AP.OutStreamer.EmitValue(LowerConstant(CV, AP), |
| 1509 | AP.TM.getTargetData()->getTypeAllocSize(CV->getType()), |
| 1510 | AddrSpace); |
| 1511 | } |
| 1512 | |
| 1513 | /// EmitGlobalConstant - Print a general LLVM constant to the .s file. |
| 1514 | void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) { |
| 1515 | uint64_t Size = TM.getTargetData()->getTypeAllocSize(CV->getType()); |
| 1516 | if (Size) |
| 1517 | EmitGlobalConstantImpl(CV, AddrSpace, *this); |
| 1518 | else if (MAI->hasSubsectionsViaSymbols()) { |
| 1519 | // If the global has zero size, emit a single byte so that two labels don't |
| 1520 | // look like they are at the same location. |
| 1521 | OutStreamer.EmitIntValue(0, 1, AddrSpace); |
| 1522 | } |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { |
| 1526 | // Target doesn't support this yet! |
| 1527 | llvm_unreachable("Target does not support EmitMachineConstantPoolValue"); |
| 1528 | } |
| 1529 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1530 | void AsmPrinter::printOffset(int64_t Offset, raw_ostream &OS) const { |
| 1531 | if (Offset > 0) |
| 1532 | OS << '+' << Offset; |
| 1533 | else if (Offset < 0) |
| 1534 | OS << Offset; |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1537 | //===----------------------------------------------------------------------===// |
| 1538 | // Symbol Lowering Routines. |
| 1539 | //===----------------------------------------------------------------------===// |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1540 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1541 | /// GetTempSymbol - Return the MCSymbol corresponding to the assembler |
| 1542 | /// temporary label with the specified stem and unique ID. |
| 1543 | MCSymbol *AsmPrinter::GetTempSymbol(StringRef Name, unsigned ID) const { |
| 1544 | return OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + |
| 1545 | Name + Twine(ID)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1546 | } |
| 1547 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1548 | /// GetTempSymbol - Return an assembler temporary label with the specified |
| 1549 | /// stem. |
| 1550 | MCSymbol *AsmPrinter::GetTempSymbol(StringRef Name) const { |
| 1551 | return OutContext.GetOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix())+ |
| 1552 | Name); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1553 | } |
| 1554 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1555 | |
| 1556 | MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1557 | return MMI->getAddrLabelSymbol(BA->getBasicBlock()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1560 | MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BasicBlock *BB) const { |
| 1561 | return MMI->getAddrLabelSymbol(BB); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /// GetCPISymbol - Return the symbol for the specified constant pool entry. |
| 1565 | MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1566 | return OutContext.GetOrCreateSymbol |
| 1567 | (Twine(MAI->getPrivateGlobalPrefix()) + "CPI" + Twine(getFunctionNumber()) |
| 1568 | + "_" + Twine(CPID)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | /// GetJTISymbol - Return the symbol for the specified jump table entry. |
| 1572 | MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const { |
| 1573 | return MF->getJTISymbol(JTID, OutContext, isLinkerPrivate); |
| 1574 | } |
| 1575 | |
| 1576 | /// GetJTSetSymbol - Return the symbol for the specified jump table .set |
| 1577 | /// FIXME: privatize to AsmPrinter. |
| 1578 | MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1579 | return OutContext.GetOrCreateSymbol |
| 1580 | (Twine(MAI->getPrivateGlobalPrefix()) + Twine(getFunctionNumber()) + "_" + |
| 1581 | Twine(UID) + "_set_" + Twine(MBBID)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with |
| 1585 | /// global value name as its base, with the specified suffix, and where the |
| 1586 | /// symbol is forced to have private linkage if ForcePrivate is true. |
| 1587 | MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV, |
| 1588 | StringRef Suffix, |
| 1589 | bool ForcePrivate) const { |
| 1590 | SmallString<60> NameStr; |
| 1591 | Mang->getNameWithPrefix(NameStr, GV, ForcePrivate); |
| 1592 | NameStr.append(Suffix.begin(), Suffix.end()); |
| 1593 | return OutContext.GetOrCreateSymbol(NameStr.str()); |
| 1594 | } |
| 1595 | |
| 1596 | /// GetExternalSymbolSymbol - Return the MCSymbol for the specified |
| 1597 | /// ExternalSymbol. |
| 1598 | MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const { |
| 1599 | SmallString<60> NameStr; |
| 1600 | Mang->getNameWithPrefix(NameStr, Sym); |
| 1601 | return OutContext.GetOrCreateSymbol(NameStr.str()); |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | |
| 1606 | /// PrintParentLoopComment - Print comments about parent loops of this one. |
| 1607 | static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop, |
| 1608 | unsigned FunctionNumber) { |
| 1609 | if (Loop == 0) return; |
| 1610 | PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber); |
| 1611 | OS.indent(Loop->getLoopDepth()*2) |
| 1612 | << "Parent Loop BB" << FunctionNumber << "_" |
| 1613 | << Loop->getHeader()->getNumber() |
| 1614 | << " Depth=" << Loop->getLoopDepth() << '\n'; |
| 1615 | } |
| 1616 | |
| 1617 | |
| 1618 | /// PrintChildLoopComment - Print comments about child loops within |
| 1619 | /// the loop for this basic block, with nesting. |
| 1620 | static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop, |
| 1621 | unsigned FunctionNumber) { |
| 1622 | // Add child loop information |
| 1623 | for (MachineLoop::iterator CL = Loop->begin(), E = Loop->end();CL != E; ++CL){ |
| 1624 | OS.indent((*CL)->getLoopDepth()*2) |
| 1625 | << "Child Loop BB" << FunctionNumber << "_" |
| 1626 | << (*CL)->getHeader()->getNumber() << " Depth " << (*CL)->getLoopDepth() |
| 1627 | << '\n'; |
| 1628 | PrintChildLoopComment(OS, *CL, FunctionNumber); |
| 1629 | } |
| 1630 | } |
| 1631 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1632 | /// EmitBasicBlockLoopComments - Pretty-print comments for basic blocks. |
| 1633 | static void EmitBasicBlockLoopComments(const MachineBasicBlock &MBB, |
| 1634 | const MachineLoopInfo *LI, |
| 1635 | const AsmPrinter &AP) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1636 | // Add loop depth information |
| 1637 | const MachineLoop *Loop = LI->getLoopFor(&MBB); |
| 1638 | if (Loop == 0) return; |
| 1639 | |
| 1640 | MachineBasicBlock *Header = Loop->getHeader(); |
| 1641 | assert(Header && "No header for loop"); |
| 1642 | |
| 1643 | // If this block is not a loop header, just print out what is the loop header |
| 1644 | // and return. |
| 1645 | if (Header != &MBB) { |
| 1646 | AP.OutStreamer.AddComment(" in Loop: Header=BB" + |
| 1647 | Twine(AP.getFunctionNumber())+"_" + |
| 1648 | Twine(Loop->getHeader()->getNumber())+ |
| 1649 | " Depth="+Twine(Loop->getLoopDepth())); |
| 1650 | return; |
| 1651 | } |
| 1652 | |
| 1653 | // Otherwise, it is a loop header. Print out information about child and |
| 1654 | // parent loops. |
| 1655 | raw_ostream &OS = AP.OutStreamer.GetCommentOS(); |
| 1656 | |
| 1657 | PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber()); |
| 1658 | |
| 1659 | OS << "=>"; |
| 1660 | OS.indent(Loop->getLoopDepth()*2-2); |
| 1661 | |
| 1662 | OS << "This "; |
| 1663 | if (Loop->empty()) |
| 1664 | OS << "Inner "; |
| 1665 | OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n'; |
| 1666 | |
| 1667 | PrintChildLoopComment(OS, Loop, AP.getFunctionNumber()); |
| 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | /// EmitBasicBlockStart - This method prints the label for the specified |
| 1672 | /// MachineBasicBlock, an alignment (if present) and a comment describing |
| 1673 | /// it if appropriate. |
| 1674 | void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const { |
| 1675 | // Emit an alignment directive for this block, if needed. |
| 1676 | if (unsigned Align = MBB->getAlignment()) |
| 1677 | EmitAlignment(Log2_32(Align)); |
| 1678 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1679 | // If the block has its address taken, emit any labels that were used to |
| 1680 | // reference the block. It is possible that there is more than one label |
| 1681 | // here, because multiple LLVM BB's may have been RAUW'd to this block after |
| 1682 | // the references were generated. |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1683 | if (MBB->hasAddressTaken()) { |
| 1684 | const BasicBlock *BB = MBB->getBasicBlock(); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1685 | if (isVerbose()) |
| 1686 | OutStreamer.AddComment("Block address taken"); |
| 1687 | |
| 1688 | std::vector<MCSymbol*> Syms = MMI->getAddrLabelSymbolToEmit(BB); |
| 1689 | |
| 1690 | for (unsigned i = 0, e = Syms.size(); i != e; ++i) |
| 1691 | OutStreamer.EmitLabel(Syms[i]); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | // Print the main label for the block. |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1695 | if (MBB->pred_empty() || isBlockOnlyReachableByFallthrough(MBB)) { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1696 | if (isVerbose() && OutStreamer.hasRawTextSupport()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1697 | if (const BasicBlock *BB = MBB->getBasicBlock()) |
| 1698 | if (BB->hasName()) |
| 1699 | OutStreamer.AddComment("%" + BB->getName()); |
| 1700 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1701 | EmitBasicBlockLoopComments(*MBB, LI, *this); |
| 1702 | |
| 1703 | // NOTE: Want this comment at start of line, don't emit with AddComment. |
| 1704 | OutStreamer.EmitRawText(Twine(MAI->getCommentString()) + " BB#" + |
| 1705 | Twine(MBB->getNumber()) + ":"); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1706 | } |
| 1707 | } else { |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1708 | if (isVerbose()) { |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1709 | if (const BasicBlock *BB = MBB->getBasicBlock()) |
| 1710 | if (BB->hasName()) |
| 1711 | OutStreamer.AddComment("%" + BB->getName()); |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1712 | EmitBasicBlockLoopComments(*MBB, LI, *this); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1713 | } |
| 1714 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1715 | OutStreamer.EmitLabel(MBB->getSymbol()); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | void AsmPrinter::EmitVisibility(MCSymbol *Sym, unsigned Visibility) const { |
| 1720 | MCSymbolAttr Attr = MCSA_Invalid; |
| 1721 | |
| 1722 | switch (Visibility) { |
| 1723 | default: break; |
| 1724 | case GlobalValue::HiddenVisibility: |
| 1725 | Attr = MAI->getHiddenVisibilityAttr(); |
| 1726 | break; |
| 1727 | case GlobalValue::ProtectedVisibility: |
| 1728 | Attr = MAI->getProtectedVisibilityAttr(); |
| 1729 | break; |
| 1730 | } |
| 1731 | |
| 1732 | if (Attr != MCSA_Invalid) |
| 1733 | OutStreamer.EmitSymbolAttribute(Sym, Attr); |
| 1734 | } |
| 1735 | |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1736 | /// isBlockOnlyReachableByFallthough - Return true if the basic block has |
| 1737 | /// exactly one predecessor and the control transfer mechanism between |
| 1738 | /// the predecessor and this block is a fall-through. |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1739 | bool AsmPrinter:: |
| 1740 | isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const { |
Shih-wei Liao | e445432 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1741 | // If this is a landing pad, it isn't a fall through. If it has no preds, |
| 1742 | // then nothing falls through to it. |
| 1743 | if (MBB->isLandingPad() || MBB->pred_empty()) |
| 1744 | return false; |
| 1745 | |
| 1746 | // If there isn't exactly one predecessor, it can't be a fall through. |
| 1747 | MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI; |
| 1748 | ++PI2; |
| 1749 | if (PI2 != MBB->pred_end()) |
| 1750 | return false; |
| 1751 | |
| 1752 | // The predecessor has to be immediately before this block. |
| 1753 | const MachineBasicBlock *Pred = *PI; |
| 1754 | |
| 1755 | if (!Pred->isLayoutSuccessor(MBB)) |
| 1756 | return false; |
| 1757 | |
| 1758 | // If the block is completely empty, then it definitely does fall through. |
| 1759 | if (Pred->empty()) |
| 1760 | return true; |
| 1761 | |
| 1762 | // Otherwise, check the last instruction. |
| 1763 | const MachineInstr &LastInst = Pred->back(); |
| 1764 | return !LastInst.getDesc().isBarrier(); |
| 1765 | } |
| 1766 | |
| 1767 | |
| 1768 | |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1769 | GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) { |
| 1770 | if (!S->usesMetadata()) |
| 1771 | return 0; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1772 | |
| 1773 | gcp_map_type &GCMap = getGCMap(GCMetadataPrinters); |
| 1774 | gcp_map_type::iterator GCPI = GCMap.find(S); |
| 1775 | if (GCPI != GCMap.end()) |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1776 | return GCPI->second; |
| 1777 | |
| 1778 | const char *Name = S->getName().c_str(); |
| 1779 | |
| 1780 | for (GCMetadataPrinterRegistry::iterator |
| 1781 | I = GCMetadataPrinterRegistry::begin(), |
| 1782 | E = GCMetadataPrinterRegistry::end(); I != E; ++I) |
| 1783 | if (strcmp(Name, I->getName()) == 0) { |
| 1784 | GCMetadataPrinter *GMP = I->instantiate(); |
| 1785 | GMP->S = S; |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1786 | GCMap.insert(std::make_pair(S, GMP)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1787 | return GMP; |
| 1788 | } |
| 1789 | |
Shih-wei Liao | 7abe37e | 2010-04-28 01:47:00 -0700 | [diff] [blame^] | 1790 | report_fatal_error("no GCMetadataPrinter registered for GC: " + Twine(Name)); |
Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [diff] [blame] | 1791 | return 0; |
| 1792 | } |
| 1793 | |