Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 1 | //===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to AT&T format assembly |
| 12 | // language. This printer is the output mechanism used by `llc'. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "asm-printer" |
| 17 | #include "X86AsmPrinter.h" |
| 18 | #include "X86ATTInstPrinter.h" |
| 19 | #include "X86IntelInstPrinter.h" |
| 20 | #include "X86MCInstLower.h" |
| 21 | #include "X86.h" |
| 22 | #include "X86COFF.h" |
| 23 | #include "X86COFFMachineModuleInfo.h" |
| 24 | #include "X86MachineFunctionInfo.h" |
| 25 | #include "X86TargetMachine.h" |
| 26 | #include "llvm/CallingConv.h" |
| 27 | #include "llvm/DerivedTypes.h" |
| 28 | #include "llvm/Module.h" |
| 29 | #include "llvm/Type.h" |
| 30 | #include "llvm/Assembly/Writer.h" |
| 31 | #include "llvm/MC/MCContext.h" |
| 32 | #include "llvm/MC/MCSectionMachO.h" |
| 33 | #include "llvm/MC/MCStreamer.h" |
| 34 | #include "llvm/MC/MCSymbol.h" |
| 35 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 36 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
| 37 | #include "llvm/Support/ErrorHandling.h" |
| 38 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCAsmInfo.h" |
| 40 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 41 | #include "llvm/Target/TargetOptions.h" |
| 42 | #include "llvm/Target/TargetRegistry.h" |
| 43 | #include "llvm/ADT/SmallString.h" |
| 44 | #include "llvm/ADT/Statistic.h" |
| 45 | using namespace llvm; |
| 46 | |
| 47 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 48 | |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | // Primitive Helper Functions. |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
| 53 | void X86AsmPrinter::printMCInst(const MCInst *MI) { |
| 54 | if (MAI->getAssemblerDialect() == 0) |
| 55 | X86ATTInstPrinter(O, *MAI).printInstruction(MI); |
| 56 | else |
| 57 | X86IntelInstPrinter(O, *MAI).printInstruction(MI); |
| 58 | } |
| 59 | |
| 60 | void X86AsmPrinter::PrintPICBaseSymbol() const { |
| 61 | // FIXME: Gross const cast hack. |
| 62 | X86AsmPrinter *AP = const_cast<X86AsmPrinter*>(this); |
| 63 | X86MCInstLower(OutContext, 0, *AP).GetPICBaseSymbol()->print(O, MAI); |
| 64 | } |
| 65 | |
| 66 | void X86AsmPrinter::emitFunctionHeader(const MachineFunction &MF) { |
| 67 | unsigned FnAlign = MF.getAlignment(); |
| 68 | const Function *F = MF.getFunction(); |
| 69 | |
| 70 | if (Subtarget->isTargetCygMing()) { |
| 71 | X86COFFMachineModuleInfo &COFFMMI = |
| 72 | MMI->getObjFileInfo<X86COFFMachineModuleInfo>(); |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 73 | COFFMMI.DecorateCygMingName(CurrentFnSym, OutContext, F, |
| 74 | *TM.getTargetData()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); |
| 78 | EmitAlignment(FnAlign, F); |
| 79 | |
| 80 | switch (F->getLinkage()) { |
| 81 | default: llvm_unreachable("Unknown linkage type!"); |
| 82 | case Function::InternalLinkage: // Symbols default to internal. |
| 83 | case Function::PrivateLinkage: |
| 84 | break; |
| 85 | case Function::DLLExportLinkage: |
| 86 | case Function::ExternalLinkage: |
Chris Lattner | 86ec971 | 2010-01-16 00:32:38 +0000 | [diff] [blame] | 87 | O << "\t.globl\t"; |
| 88 | CurrentFnSym->print(O, MAI); |
| 89 | O << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 90 | break; |
| 91 | case Function::LinkerPrivateLinkage: |
| 92 | case Function::LinkOnceAnyLinkage: |
| 93 | case Function::LinkOnceODRLinkage: |
| 94 | case Function::WeakAnyLinkage: |
| 95 | case Function::WeakODRLinkage: |
| 96 | if (Subtarget->isTargetDarwin()) { |
Chris Lattner | 86ec971 | 2010-01-16 00:32:38 +0000 | [diff] [blame] | 97 | O << "\t.globl\t"; |
| 98 | CurrentFnSym->print(O, MAI); |
| 99 | O << '\n'; |
| 100 | O << MAI->getWeakDefDirective(); |
| 101 | CurrentFnSym->print(O, MAI); |
| 102 | O << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 103 | } else if (Subtarget->isTargetCygMing()) { |
Chris Lattner | 86ec971 | 2010-01-16 00:32:38 +0000 | [diff] [blame] | 104 | O << "\t.globl\t"; |
| 105 | CurrentFnSym->print(O, MAI); |
| 106 | O << "\n\t.linkonce discard\n"; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 107 | } else { |
Chris Lattner | 86ec971 | 2010-01-16 00:32:38 +0000 | [diff] [blame] | 108 | O << "\t.weak\t"; |
| 109 | CurrentFnSym->print(O, MAI); |
| 110 | O << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 111 | } |
| 112 | break; |
| 113 | } |
| 114 | |
Chris Lattner | 86ec971 | 2010-01-16 00:32:38 +0000 | [diff] [blame] | 115 | printVisibility(CurrentFnSym, F->getVisibility()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 86ec971 | 2010-01-16 00:32:38 +0000 | [diff] [blame] | 117 | if (Subtarget->isTargetELF()) { |
| 118 | O << "\t.type\t"; |
| 119 | CurrentFnSym->print(O, MAI); |
| 120 | O << ",@function\n"; |
| 121 | } else if (Subtarget->isTargetCygMing()) { |
| 122 | O << "\t.def\t "; |
| 123 | CurrentFnSym->print(O, MAI); |
| 124 | O << ";\t.scl\t" << |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 125 | (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT) |
| 126 | << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT) |
| 127 | << ";\t.endef\n"; |
| 128 | } |
| 129 | |
Chris Lattner | 86ec971 | 2010-01-16 00:32:38 +0000 | [diff] [blame] | 130 | CurrentFnSym->print(O, MAI); |
| 131 | O << ':'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 132 | if (VerboseAsm) { |
| 133 | O.PadToColumn(MAI->getCommentColumn()); |
| 134 | O << MAI->getCommentString() << ' '; |
| 135 | WriteAsOperand(O, F, /*PrintType=*/false, F->getParent()); |
| 136 | } |
| 137 | O << '\n'; |
| 138 | |
| 139 | // Add some workaround for linkonce linkage on Cygwin\MinGW |
| 140 | if (Subtarget->isTargetCygMing() && |
Chris Lattner | 86ec971 | 2010-01-16 00:32:38 +0000 | [diff] [blame] | 141 | (F->hasLinkOnceLinkage() || F->hasWeakLinkage())) { |
| 142 | O << "Lllvm$workaround$fake$stub$"; |
| 143 | CurrentFnSym->print(O, MAI); |
| 144 | O << ":\n"; |
| 145 | } |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 149 | /// method to print assembly for each instruction. |
| 150 | /// |
| 151 | bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 152 | const Function *F = MF.getFunction(); |
| 153 | this->MF = &MF; |
| 154 | CallingConv::ID CC = F->getCallingConv(); |
| 155 | |
| 156 | SetupMachineFunction(MF); |
| 157 | O << "\n\n"; |
| 158 | |
| 159 | if (Subtarget->isTargetCOFF()) { |
| 160 | X86COFFMachineModuleInfo &COFFMMI = |
| 161 | MMI->getObjFileInfo<X86COFFMachineModuleInfo>(); |
| 162 | |
| 163 | // Populate function information map. Don't want to populate |
| 164 | // non-stdcall or non-fastcall functions' information right now. |
| 165 | if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall) |
| 166 | COFFMMI.AddFunctionInfo(F, *MF.getInfo<X86MachineFunctionInfo>()); |
| 167 | } |
| 168 | |
| 169 | // Print out constants referenced by the function |
| 170 | EmitConstantPool(MF.getConstantPool()); |
| 171 | |
| 172 | // Print the 'header' of function |
| 173 | emitFunctionHeader(MF); |
| 174 | |
| 175 | // Emit pre-function debug and/or EH information. |
| 176 | if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling()) |
| 177 | DW->BeginFunction(&MF); |
| 178 | |
| 179 | // Print out code for the function. |
| 180 | bool hasAnyRealCode = false; |
| 181 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 182 | I != E; ++I) { |
| 183 | // Print a label for the basic block. |
Dan Gohman | 5fda434 | 2009-10-06 17:38:38 +0000 | [diff] [blame] | 184 | EmitBasicBlockStart(I); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 185 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 186 | II != IE; ++II) { |
| 187 | // Print the assembly for the instruction. |
| 188 | if (!II->isLabel()) |
| 189 | hasAnyRealCode = true; |
| 190 | printMachineInstruction(II); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (Subtarget->isTargetDarwin() && !hasAnyRealCode) { |
| 195 | // If the function is empty, then we need to emit *something*. Otherwise, |
| 196 | // the function's label might be associated with something that it wasn't |
| 197 | // meant to be associated with. We emit a noop in this situation. |
| 198 | // We are assuming inline asms are code. |
| 199 | O << "\tnop\n"; |
| 200 | } |
| 201 | |
Chris Lattner | 86ec971 | 2010-01-16 00:32:38 +0000 | [diff] [blame] | 202 | if (MAI->hasDotTypeDotSizeDirective()) { |
| 203 | O << "\t.size\t"; |
| 204 | CurrentFnSym->print(O, MAI); |
| 205 | O << ", .-"; |
| 206 | CurrentFnSym->print(O, MAI); |
| 207 | O << '\n'; |
| 208 | } |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 209 | |
| 210 | // Emit post-function debug information. |
| 211 | if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling()) |
| 212 | DW->EndFunction(&MF); |
| 213 | |
| 214 | // Print out jump tables referenced by the function. |
| 215 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
| 216 | |
| 217 | // We didn't modify anything. |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | /// printSymbolOperand - Print a raw symbol reference operand. This handles |
| 222 | /// jump tables, constant pools, global address and external symbols, all of |
| 223 | /// which print to a label with various suffixes for relocation types etc. |
| 224 | void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO) { |
| 225 | switch (MO.getType()) { |
| 226 | default: llvm_unreachable("unknown symbol type!"); |
| 227 | case MachineOperand::MO_JumpTableIndex: |
| 228 | O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_' |
| 229 | << MO.getIndex(); |
| 230 | break; |
| 231 | case MachineOperand::MO_ConstantPoolIndex: |
| 232 | O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' |
| 233 | << MO.getIndex(); |
| 234 | printOffset(MO.getOffset()); |
| 235 | break; |
| 236 | case MachineOperand::MO_GlobalAddress: { |
| 237 | const GlobalValue *GV = MO.getGlobal(); |
| 238 | |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 239 | const MCSymbol *GVSym; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 240 | if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame^] | 241 | GVSym = GetSymbolWithGlobalValueBase(GV, "$stub"); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 242 | else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY || |
| 243 | MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE || |
| 244 | MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE) |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame^] | 245 | GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 246 | else |
| 247 | GVSym = GetGlobalValueSymbol(GV); |
| 248 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 249 | if (Subtarget->isTargetCygMing()) { |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 250 | X86COFFMachineModuleInfo &COFFMMI = |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 251 | MMI->getObjFileInfo<X86COFFMachineModuleInfo>(); |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 252 | COFFMMI.DecorateCygMingName(GVSym, OutContext, GV, *TM.getTargetData()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Handle dllimport linkage. |
| 256 | if (MO.getTargetFlags() == X86II::MO_DLLIMPORT) |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 257 | GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 258 | |
| 259 | if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY || |
| 260 | MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) { |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame^] | 261 | MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 262 | |
| 263 | const MCSymbol *&StubSym = |
| 264 | MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym); |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 265 | if (StubSym == 0) |
| 266 | StubSym = GetGlobalValueSymbol(GV); |
| 267 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 268 | } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){ |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame^] | 269 | MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 270 | const MCSymbol *&StubSym = |
| 271 | MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym); |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 272 | if (StubSym == 0) |
| 273 | StubSym = GetGlobalValueSymbol(GV); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 274 | } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) { |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame^] | 275 | MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub"); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 276 | const MCSymbol *&StubSym = |
| 277 | MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym); |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 278 | if (StubSym == 0) |
| 279 | StubSym = GetGlobalValueSymbol(GV); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // If the name begins with a dollar-sign, enclose it in parens. We do this |
| 283 | // to avoid having it look like an integer immediate to the assembler. |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 284 | if (GVSym->getName()[0] != '$') |
| 285 | GVSym->print(O, MAI); |
| 286 | else { |
| 287 | O << '('; |
| 288 | GVSym->print(O, MAI); |
| 289 | O << ')'; |
| 290 | } |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 291 | printOffset(MO.getOffset()); |
| 292 | break; |
| 293 | } |
| 294 | case MachineOperand::MO_ExternalSymbol: { |
Chris Lattner | f80743e | 2010-01-13 07:56:59 +0000 | [diff] [blame] | 295 | const MCSymbol *SymToPrint; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 296 | if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 297 | SmallString<128> TempNameStr; |
| 298 | TempNameStr += StringRef(MO.getSymbolName()); |
| 299 | TempNameStr += StringRef("$stub"); |
| 300 | |
| 301 | const MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 302 | const MCSymbol *&StubSym = |
| 303 | MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym); |
| 304 | if (StubSym == 0) { |
Chris Lattner | a38b287 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 305 | TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end()); |
| 306 | StubSym = OutContext.GetOrCreateSymbol(TempNameStr.str()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | f80743e | 2010-01-13 07:56:59 +0000 | [diff] [blame] | 308 | SymToPrint = StubSym; |
| 309 | } else { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 310 | SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // If the name begins with a dollar-sign, enclose it in parens. We do this |
| 314 | // to avoid having it look like an integer immediate to the assembler. |
Chris Lattner | f80743e | 2010-01-13 07:56:59 +0000 | [diff] [blame] | 315 | if (SymToPrint->getName()[0] != '$') |
| 316 | SymToPrint->print(O, MAI); |
| 317 | else { |
| 318 | O << '('; |
| 319 | SymToPrint->print(O, MAI); |
| 320 | O << '('; |
| 321 | } |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | switch (MO.getTargetFlags()) { |
| 327 | default: |
| 328 | llvm_unreachable("Unknown target flag on GV operand"); |
| 329 | case X86II::MO_NO_FLAG: // No flag. |
| 330 | break; |
| 331 | case X86II::MO_DARWIN_NONLAZY: |
| 332 | case X86II::MO_DLLIMPORT: |
| 333 | case X86II::MO_DARWIN_STUB: |
| 334 | // These affect the name of the symbol, not any suffix. |
| 335 | break; |
| 336 | case X86II::MO_GOT_ABSOLUTE_ADDRESS: |
| 337 | O << " + [.-"; |
| 338 | PrintPICBaseSymbol(); |
| 339 | O << ']'; |
| 340 | break; |
| 341 | case X86II::MO_PIC_BASE_OFFSET: |
| 342 | case X86II::MO_DARWIN_NONLAZY_PIC_BASE: |
| 343 | case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: |
| 344 | O << '-'; |
| 345 | PrintPICBaseSymbol(); |
| 346 | break; |
| 347 | case X86II::MO_TLSGD: O << "@TLSGD"; break; |
| 348 | case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break; |
| 349 | case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break; |
| 350 | case X86II::MO_TPOFF: O << "@TPOFF"; break; |
| 351 | case X86II::MO_NTPOFF: O << "@NTPOFF"; break; |
| 352 | case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break; |
| 353 | case X86II::MO_GOT: O << "@GOT"; break; |
| 354 | case X86II::MO_GOTOFF: O << "@GOTOFF"; break; |
| 355 | case X86II::MO_PLT: O << "@PLT"; break; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /// print_pcrel_imm - This is used to print an immediate value that ends up |
| 360 | /// being encoded as a pc-relative value. These print slightly differently, for |
| 361 | /// example, a $ is not emitted. |
| 362 | void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) { |
| 363 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 364 | switch (MO.getType()) { |
| 365 | default: llvm_unreachable("Unknown pcrel immediate operand"); |
| 366 | case MachineOperand::MO_Immediate: |
| 367 | O << MO.getImm(); |
| 368 | return; |
| 369 | case MachineOperand::MO_MachineBasicBlock: |
| 370 | GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI); |
| 371 | return; |
| 372 | case MachineOperand::MO_GlobalAddress: |
| 373 | case MachineOperand::MO_ExternalSymbol: |
| 374 | printSymbolOperand(MO); |
| 375 | return; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | |
| 380 | void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, |
| 381 | const char *Modifier) { |
| 382 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 383 | switch (MO.getType()) { |
| 384 | default: llvm_unreachable("unknown operand type!"); |
| 385 | case MachineOperand::MO_Register: { |
| 386 | O << '%'; |
| 387 | unsigned Reg = MO.getReg(); |
| 388 | if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) { |
| 389 | EVT VT = (strcmp(Modifier+6,"64") == 0) ? |
| 390 | MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 : |
| 391 | ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8)); |
| 392 | Reg = getX86SubSuperRegister(Reg, VT); |
| 393 | } |
| 394 | O << X86ATTInstPrinter::getRegisterName(Reg); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | case MachineOperand::MO_Immediate: |
| 399 | O << '$' << MO.getImm(); |
| 400 | return; |
| 401 | |
| 402 | case MachineOperand::MO_JumpTableIndex: |
| 403 | case MachineOperand::MO_ConstantPoolIndex: |
| 404 | case MachineOperand::MO_GlobalAddress: |
| 405 | case MachineOperand::MO_ExternalSymbol: { |
| 406 | O << '$'; |
| 407 | printSymbolOperand(MO); |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) { |
| 414 | unsigned char value = MI->getOperand(Op).getImm(); |
| 415 | assert(value <= 7 && "Invalid ssecc argument!"); |
| 416 | switch (value) { |
| 417 | case 0: O << "eq"; break; |
| 418 | case 1: O << "lt"; break; |
| 419 | case 2: O << "le"; break; |
| 420 | case 3: O << "unord"; break; |
| 421 | case 4: O << "neq"; break; |
| 422 | case 5: O << "nlt"; break; |
| 423 | case 6: O << "nle"; break; |
| 424 | case 7: O << "ord"; break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op, |
| 429 | const char *Modifier) { |
| 430 | const MachineOperand &BaseReg = MI->getOperand(Op); |
| 431 | const MachineOperand &IndexReg = MI->getOperand(Op+2); |
| 432 | const MachineOperand &DispSpec = MI->getOperand(Op+3); |
| 433 | |
| 434 | // If we really don't want to print out (rip), don't. |
| 435 | bool HasBaseReg = BaseReg.getReg() != 0; |
| 436 | if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") && |
| 437 | BaseReg.getReg() == X86::RIP) |
| 438 | HasBaseReg = false; |
| 439 | |
| 440 | // HasParenPart - True if we will print out the () part of the mem ref. |
| 441 | bool HasParenPart = IndexReg.getReg() || HasBaseReg; |
| 442 | |
| 443 | if (DispSpec.isImm()) { |
| 444 | int DispVal = DispSpec.getImm(); |
| 445 | if (DispVal || !HasParenPart) |
| 446 | O << DispVal; |
| 447 | } else { |
| 448 | assert(DispSpec.isGlobal() || DispSpec.isCPI() || |
| 449 | DispSpec.isJTI() || DispSpec.isSymbol()); |
| 450 | printSymbolOperand(MI->getOperand(Op+3)); |
| 451 | } |
| 452 | |
| 453 | if (HasParenPart) { |
| 454 | assert(IndexReg.getReg() != X86::ESP && |
| 455 | "X86 doesn't allow scaling by ESP"); |
| 456 | |
| 457 | O << '('; |
| 458 | if (HasBaseReg) |
| 459 | printOperand(MI, Op, Modifier); |
| 460 | |
| 461 | if (IndexReg.getReg()) { |
| 462 | O << ','; |
| 463 | printOperand(MI, Op+2, Modifier); |
| 464 | unsigned ScaleVal = MI->getOperand(Op+1).getImm(); |
| 465 | if (ScaleVal != 1) |
| 466 | O << ',' << ScaleVal; |
| 467 | } |
| 468 | O << ')'; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op, |
| 473 | const char *Modifier) { |
| 474 | assert(isMem(MI, Op) && "Invalid memory reference!"); |
| 475 | const MachineOperand &Segment = MI->getOperand(Op+4); |
| 476 | if (Segment.getReg()) { |
| 477 | printOperand(MI, Op+4, Modifier); |
| 478 | O << ':'; |
| 479 | } |
| 480 | printLeaMemReference(MI, Op, Modifier); |
| 481 | } |
| 482 | |
| 483 | void X86AsmPrinter::printPICJumpTableSetLabel(unsigned uid, |
| 484 | const MachineBasicBlock *MBB) const { |
| 485 | if (!MAI->getSetDirective()) |
| 486 | return; |
| 487 | |
| 488 | // We don't need .set machinery if we have GOT-style relocations |
| 489 | if (Subtarget->isPICStyleGOT()) |
| 490 | return; |
| 491 | |
| 492 | O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix() |
| 493 | << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; |
| 494 | |
| 495 | GetMBBSymbol(MBB->getNumber())->print(O, MAI); |
| 496 | |
| 497 | if (Subtarget->isPICStyleRIPRel()) |
| 498 | O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 499 | << '_' << uid << '\n'; |
| 500 | else { |
| 501 | O << '-'; |
| 502 | PrintPICBaseSymbol(); |
| 503 | O << '\n'; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | |
| 508 | void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) { |
| 509 | PrintPICBaseSymbol(); |
| 510 | O << '\n'; |
| 511 | PrintPICBaseSymbol(); |
| 512 | O << ':'; |
| 513 | } |
| 514 | |
| 515 | void X86AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, |
| 516 | const MachineBasicBlock *MBB, |
| 517 | unsigned uid) const { |
| 518 | const char *JTEntryDirective = MJTI->getEntrySize() == 4 ? |
| 519 | MAI->getData32bitsDirective() : MAI->getData64bitsDirective(); |
| 520 | |
| 521 | O << JTEntryDirective << ' '; |
| 522 | |
| 523 | if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) { |
| 524 | O << MAI->getPrivateGlobalPrefix() << getFunctionNumber() |
| 525 | << '_' << uid << "_set_" << MBB->getNumber(); |
| 526 | } else if (Subtarget->isPICStyleGOT()) { |
| 527 | GetMBBSymbol(MBB->getNumber())->print(O, MAI); |
| 528 | O << "@GOTOFF"; |
| 529 | } else |
| 530 | GetMBBSymbol(MBB->getNumber())->print(O, MAI); |
| 531 | } |
| 532 | |
| 533 | bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) { |
| 534 | unsigned Reg = MO.getReg(); |
| 535 | switch (Mode) { |
| 536 | default: return true; // Unknown mode. |
| 537 | case 'b': // Print QImode register |
| 538 | Reg = getX86SubSuperRegister(Reg, MVT::i8); |
| 539 | break; |
| 540 | case 'h': // Print QImode high register |
| 541 | Reg = getX86SubSuperRegister(Reg, MVT::i8, true); |
| 542 | break; |
| 543 | case 'w': // Print HImode register |
| 544 | Reg = getX86SubSuperRegister(Reg, MVT::i16); |
| 545 | break; |
| 546 | case 'k': // Print SImode register |
| 547 | Reg = getX86SubSuperRegister(Reg, MVT::i32); |
| 548 | break; |
| 549 | case 'q': // Print DImode register |
| 550 | Reg = getX86SubSuperRegister(Reg, MVT::i64); |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | O << '%' << X86ATTInstPrinter::getRegisterName(Reg); |
| 555 | return false; |
| 556 | } |
| 557 | |
| 558 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 559 | /// |
| 560 | bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 561 | unsigned AsmVariant, |
| 562 | const char *ExtraCode) { |
| 563 | // Does this asm operand have a single letter operand modifier? |
| 564 | if (ExtraCode && ExtraCode[0]) { |
| 565 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
| 566 | |
| 567 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 568 | |
| 569 | switch (ExtraCode[0]) { |
| 570 | default: return true; // Unknown modifier. |
| 571 | case 'a': // This is an address. Currently only 'i' and 'r' are expected. |
| 572 | if (MO.isImm()) { |
| 573 | O << MO.getImm(); |
| 574 | return false; |
| 575 | } |
| 576 | if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) { |
| 577 | printSymbolOperand(MO); |
| 578 | return false; |
| 579 | } |
| 580 | if (MO.isReg()) { |
| 581 | O << '('; |
| 582 | printOperand(MI, OpNo); |
| 583 | O << ')'; |
| 584 | return false; |
| 585 | } |
| 586 | return true; |
| 587 | |
| 588 | case 'c': // Don't print "$" before a global var name or constant. |
| 589 | if (MO.isImm()) |
| 590 | O << MO.getImm(); |
| 591 | else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) |
| 592 | printSymbolOperand(MO); |
| 593 | else |
| 594 | printOperand(MI, OpNo); |
| 595 | return false; |
| 596 | |
| 597 | case 'A': // Print '*' before a register (it must be a register) |
| 598 | if (MO.isReg()) { |
| 599 | O << '*'; |
| 600 | printOperand(MI, OpNo); |
| 601 | return false; |
| 602 | } |
| 603 | return true; |
| 604 | |
| 605 | case 'b': // Print QImode register |
| 606 | case 'h': // Print QImode high register |
| 607 | case 'w': // Print HImode register |
| 608 | case 'k': // Print SImode register |
| 609 | case 'q': // Print DImode register |
| 610 | if (MO.isReg()) |
| 611 | return printAsmMRegister(MO, ExtraCode[0]); |
| 612 | printOperand(MI, OpNo); |
| 613 | return false; |
| 614 | |
| 615 | case 'P': // This is the operand of a call, treat specially. |
| 616 | print_pcrel_imm(MI, OpNo); |
| 617 | return false; |
| 618 | |
| 619 | case 'n': // Negate the immediate or print a '-' before the operand. |
| 620 | // Note: this is a temporary solution. It should be handled target |
| 621 | // independently as part of the 'MC' work. |
| 622 | if (MO.isImm()) { |
| 623 | O << -MO.getImm(); |
| 624 | return false; |
| 625 | } |
| 626 | O << '-'; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | printOperand(MI, OpNo); |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 635 | unsigned OpNo, unsigned AsmVariant, |
| 636 | const char *ExtraCode) { |
| 637 | if (ExtraCode && ExtraCode[0]) { |
| 638 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
| 639 | |
| 640 | switch (ExtraCode[0]) { |
| 641 | default: return true; // Unknown modifier. |
| 642 | case 'b': // Print QImode register |
| 643 | case 'h': // Print QImode high register |
| 644 | case 'w': // Print HImode register |
| 645 | case 'k': // Print SImode register |
| 646 | case 'q': // Print SImode register |
| 647 | // These only apply to registers, ignore on mem. |
| 648 | break; |
| 649 | case 'P': // Don't print @PLT, but do print as memory. |
| 650 | printMemReference(MI, OpNo, "no-rip"); |
| 651 | return false; |
| 652 | } |
| 653 | } |
| 654 | printMemReference(MI, OpNo); |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | |
| 660 | /// printMachineInstruction -- Print out a single X86 LLVM instruction MI in |
| 661 | /// AT&T syntax to the current output stream. |
| 662 | /// |
| 663 | void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
| 664 | ++EmittedInsts; |
| 665 | |
Devang Patel | 5450fc1 | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 666 | processDebugLoc(MI, true); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 667 | |
| 668 | printInstructionThroughMCStreamer(MI); |
| 669 | |
David Greene | ca9b04b | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 670 | if (VerboseAsm) |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 671 | EmitComments(*MI); |
| 672 | O << '\n'; |
Devang Patel | 5450fc1 | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 673 | |
| 674 | processDebugLoc(MI, false); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | void X86AsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) { |
| 678 | if (!GVar->hasInitializer()) |
| 679 | return; // External global require no code |
| 680 | |
| 681 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 682 | if (EmitSpecialLLVMGlobal(GVar)) { |
| 683 | if (Subtarget->isTargetDarwin() && |
| 684 | TM.getRelocationModel() == Reloc::Static) { |
| 685 | if (GVar->getName() == "llvm.global_ctors") |
| 686 | O << ".reference .constructors_used\n"; |
| 687 | else if (GVar->getName() == "llvm.global_dtors") |
| 688 | O << ".reference .destructors_used\n"; |
| 689 | } |
| 690 | return; |
| 691 | } |
| 692 | |
| 693 | const TargetData *TD = TM.getTargetData(); |
| 694 | |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 695 | MCSymbol *GVSym = GetGlobalValueSymbol(GVar); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 696 | Constant *C = GVar->getInitializer(); |
| 697 | const Type *Type = C->getType(); |
| 698 | unsigned Size = TD->getTypeAllocSize(Type); |
| 699 | unsigned Align = TD->getPreferredAlignmentLog(GVar); |
| 700 | |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 701 | printVisibility(GVSym, GVar->getVisibility()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 703 | if (Subtarget->isTargetELF()) { |
| 704 | O << "\t.type\t"; |
| 705 | GVSym->print(O, MAI); |
| 706 | O << ",@object\n"; |
| 707 | } |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 708 | |
| 709 | |
| 710 | SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM); |
| 711 | const MCSection *TheSection = |
| 712 | getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM); |
| 713 | OutStreamer.SwitchSection(TheSection); |
| 714 | |
| 715 | // FIXME: get this stuff from section kind flags. |
| 716 | if (C->isNullValue() && !GVar->hasSection() && |
| 717 | // Don't put things that should go in the cstring section into "comm". |
| 718 | !TheSection->getKind().isMergeableCString()) { |
| 719 | if (GVar->hasExternalLinkage()) { |
| 720 | if (const char *Directive = MAI->getZeroFillDirective()) { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 721 | O << "\t.globl "; |
| 722 | GVSym->print(O, MAI); |
| 723 | O << '\n'; |
| 724 | O << Directive << "__DATA, __common, "; |
| 725 | GVSym->print(O, MAI); |
| 726 | O << ", " << Size << ", " << Align << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 727 | return; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if (!GVar->isThreadLocal() && |
| 732 | (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) { |
| 733 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 734 | |
| 735 | if (MAI->getLCOMMDirective() != NULL) { |
| 736 | if (GVar->hasLocalLinkage()) { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 737 | O << MAI->getLCOMMDirective(); |
| 738 | GVSym->print(O, MAI); |
| 739 | O << ',' << Size; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 740 | if (Subtarget->isTargetDarwin()) |
| 741 | O << ',' << Align; |
| 742 | } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 743 | O << "\t.globl "; |
| 744 | GVSym->print(O, MAI); |
| 745 | O << '\n' << MAI->getWeakDefDirective(); |
| 746 | GVSym->print(O, MAI); |
| 747 | O << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 748 | EmitAlignment(Align, GVar); |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 749 | GVSym->print(O, MAI); |
| 750 | O << ":"; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 751 | if (VerboseAsm) { |
| 752 | O.PadToColumn(MAI->getCommentColumn()); |
| 753 | O << MAI->getCommentString() << ' '; |
| 754 | WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); |
| 755 | } |
| 756 | O << '\n'; |
| 757 | EmitGlobalConstant(C); |
| 758 | return; |
| 759 | } else { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 760 | O << MAI->getCOMMDirective(); |
| 761 | GVSym->print(O, MAI); |
| 762 | O << ',' << Size; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 763 | if (MAI->getCOMMDirectiveTakesAlignment()) |
| 764 | O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align); |
| 765 | } |
| 766 | } else { |
| 767 | if (!Subtarget->isTargetCygMing()) { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 768 | if (GVar->hasLocalLinkage()) { |
| 769 | O << "\t.local\t"; |
| 770 | GVSym->print(O, MAI); |
| 771 | O << '\n'; |
| 772 | } |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 773 | } |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 774 | O << MAI->getCOMMDirective(); |
| 775 | GVSym->print(O, MAI); |
| 776 | O << ',' << Size; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 777 | if (MAI->getCOMMDirectiveTakesAlignment()) |
| 778 | O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align); |
| 779 | } |
| 780 | if (VerboseAsm) { |
| 781 | O.PadToColumn(MAI->getCommentColumn()); |
| 782 | O << MAI->getCommentString() << ' '; |
| 783 | WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); |
| 784 | } |
| 785 | O << '\n'; |
| 786 | return; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | switch (GVar->getLinkage()) { |
| 791 | case GlobalValue::CommonLinkage: |
| 792 | case GlobalValue::LinkOnceAnyLinkage: |
| 793 | case GlobalValue::LinkOnceODRLinkage: |
| 794 | case GlobalValue::WeakAnyLinkage: |
| 795 | case GlobalValue::WeakODRLinkage: |
| 796 | case GlobalValue::LinkerPrivateLinkage: |
| 797 | if (Subtarget->isTargetDarwin()) { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 798 | O << "\t.globl "; |
| 799 | GVSym->print(O, MAI); |
| 800 | O << '\n' << MAI->getWeakDefDirective(); |
| 801 | GVSym->print(O, MAI); |
| 802 | O << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 803 | } else if (Subtarget->isTargetCygMing()) { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 804 | O << "\t.globl\t"; |
| 805 | GVSym->print(O, MAI); |
| 806 | O << "\n\t.linkonce same_size\n"; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 807 | } else { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 808 | O << "\t.weak\t"; |
| 809 | GVSym->print(O, MAI); |
| 810 | O << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 811 | } |
| 812 | break; |
| 813 | case GlobalValue::DLLExportLinkage: |
| 814 | case GlobalValue::AppendingLinkage: |
| 815 | // FIXME: appending linkage variables should go into a section of |
| 816 | // their name or something. For now, just emit them as external. |
| 817 | case GlobalValue::ExternalLinkage: |
| 818 | // If external or appending, declare as a global symbol |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 819 | O << "\t.globl "; |
| 820 | GVSym->print(O, MAI); |
| 821 | O << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 822 | // FALL THROUGH |
| 823 | case GlobalValue::PrivateLinkage: |
| 824 | case GlobalValue::InternalLinkage: |
| 825 | break; |
| 826 | default: |
| 827 | llvm_unreachable("Unknown linkage type!"); |
| 828 | } |
| 829 | |
| 830 | EmitAlignment(Align, GVar); |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 831 | GVSym->print(O, MAI); |
| 832 | O << ":"; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 833 | if (VerboseAsm){ |
| 834 | O.PadToColumn(MAI->getCommentColumn()); |
| 835 | O << MAI->getCommentString() << ' '; |
| 836 | WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent()); |
| 837 | } |
| 838 | O << '\n'; |
| 839 | |
| 840 | EmitGlobalConstant(C); |
| 841 | |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 842 | if (MAI->hasDotTypeDotSizeDirective()) { |
| 843 | O << "\t.size\t"; |
| 844 | GVSym->print(O, MAI); |
| 845 | O << ", " << Size << '\n'; |
| 846 | } |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | void X86AsmPrinter::EmitEndOfAsmFile(Module &M) { |
| 850 | if (Subtarget->isTargetDarwin()) { |
| 851 | // All darwin targets use mach-o. |
| 852 | TargetLoweringObjectFileMachO &TLOFMacho = |
| 853 | static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering()); |
| 854 | |
| 855 | MachineModuleInfoMachO &MMIMacho = |
| 856 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 857 | |
| 858 | // Output stubs for dynamically-linked functions. |
| 859 | MachineModuleInfoMachO::SymbolListTy Stubs; |
| 860 | |
| 861 | Stubs = MMIMacho.GetFnStubList(); |
| 862 | if (!Stubs.empty()) { |
| 863 | const MCSection *TheSection = |
| 864 | TLOFMacho.getMachOSection("__IMPORT", "__jump_table", |
| 865 | MCSectionMachO::S_SYMBOL_STUBS | |
| 866 | MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE | |
| 867 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 868 | 5, SectionKind::getMetadata()); |
| 869 | OutStreamer.SwitchSection(TheSection); |
| 870 | |
| 871 | for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { |
| 872 | Stubs[i].first->print(O, MAI); |
| 873 | O << ":\n" << "\t.indirect_symbol "; |
| 874 | // Get the MCSymbol without the $stub suffix. |
| 875 | Stubs[i].second->print(O, MAI); |
| 876 | O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n"; |
| 877 | } |
| 878 | O << '\n'; |
| 879 | |
| 880 | Stubs.clear(); |
| 881 | } |
| 882 | |
| 883 | // Output stubs for external and common global variables. |
| 884 | Stubs = MMIMacho.GetGVStubList(); |
| 885 | if (!Stubs.empty()) { |
| 886 | const MCSection *TheSection = |
| 887 | TLOFMacho.getMachOSection("__IMPORT", "__pointers", |
| 888 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, |
| 889 | SectionKind::getMetadata()); |
| 890 | OutStreamer.SwitchSection(TheSection); |
| 891 | |
| 892 | for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { |
| 893 | Stubs[i].first->print(O, MAI); |
| 894 | O << ":\n\t.indirect_symbol "; |
| 895 | Stubs[i].second->print(O, MAI); |
| 896 | O << "\n\t.long\t0\n"; |
| 897 | } |
| 898 | Stubs.clear(); |
| 899 | } |
| 900 | |
| 901 | Stubs = MMIMacho.GetHiddenGVStubList(); |
| 902 | if (!Stubs.empty()) { |
| 903 | OutStreamer.SwitchSection(getObjFileLowering().getDataSection()); |
| 904 | EmitAlignment(2); |
| 905 | |
| 906 | for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { |
| 907 | Stubs[i].first->print(O, MAI); |
| 908 | O << ":\n" << MAI->getData32bitsDirective(); |
| 909 | Stubs[i].second->print(O, MAI); |
| 910 | O << '\n'; |
| 911 | } |
| 912 | Stubs.clear(); |
| 913 | } |
| 914 | |
| 915 | // Funny Darwin hack: This flag tells the linker that no global symbols |
| 916 | // contain code that falls through to other global symbols (e.g. the obvious |
| 917 | // implementation of multiple entry points). If this doesn't occur, the |
| 918 | // linker can safely perform dead code stripping. Since LLVM never |
| 919 | // generates code that does this, it is always safe to set. |
Chris Lattner | fe284f7 | 2009-10-19 18:03:08 +0000 | [diff] [blame] | 920 | OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols); |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 923 | if (Subtarget->isTargetCOFF()) { |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 924 | X86COFFMachineModuleInfo &COFFMMI = |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 925 | MMI->getObjFileInfo<X86COFFMachineModuleInfo>(); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 926 | |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 927 | // Emit type information for external functions |
| 928 | for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(), |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 929 | E = COFFMMI.stub_end(); I != E; ++I) { |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 930 | O << "\t.def\t " << I->getKeyData() |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 931 | << ";\t.scl\t" << COFF::C_EXT |
| 932 | << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT) |
| 933 | << ";\t.endef\n"; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 934 | } |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 935 | |
| 936 | if (Subtarget->isTargetCygMing()) { |
| 937 | // Necessary for dllexport support |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 938 | std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals; |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 939 | |
| 940 | TargetLoweringObjectFileCOFF &TLOFCOFF = |
| 941 | static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering()); |
| 942 | |
| 943 | for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 944 | if (I->hasDLLExportLinkage()) { |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 945 | const MCSymbol *Sym = GetGlobalValueSymbol(I); |
| 946 | COFFMMI.DecorateCygMingName(Sym, OutContext, I, *TM.getTargetData()); |
| 947 | DLLExportedFns.push_back(Sym); |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | for (Module::const_global_iterator I = M.global_begin(), |
| 951 | E = M.global_end(); I != E; ++I) |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 952 | if (I->hasDLLExportLinkage()) |
| 953 | DLLExportedGlobals.push_back(GetGlobalValueSymbol(I)); |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 954 | |
| 955 | // Output linker support code for dllexported globals on windows. |
| 956 | if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) { |
| 957 | OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve", |
| 958 | true, |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 959 | SectionKind::getMetadata())); |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 960 | for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i) { |
| 961 | O << "\t.ascii \" -export:"; |
| 962 | DLLExportedGlobals[i]->print(O, MAI); |
| 963 | O << ",data\"\n"; |
| 964 | } |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 965 | |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 966 | for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i) { |
| 967 | O << "\t.ascii \" -export:"; |
| 968 | DLLExportedFns[i]->print(O, MAI); |
| 969 | O << "\"\n"; |
| 970 | } |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 971 | } |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | |
| 977 | //===----------------------------------------------------------------------===// |
| 978 | // Target Registry Stuff |
| 979 | //===----------------------------------------------------------------------===// |
| 980 | |
| 981 | static MCInstPrinter *createX86MCInstPrinter(const Target &T, |
| 982 | unsigned SyntaxVariant, |
| 983 | const MCAsmInfo &MAI, |
| 984 | raw_ostream &O) { |
| 985 | if (SyntaxVariant == 0) |
| 986 | return new X86ATTInstPrinter(O, MAI); |
| 987 | if (SyntaxVariant == 1) |
| 988 | return new X86IntelInstPrinter(O, MAI); |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | // Force static initialization. |
| 993 | extern "C" void LLVMInitializeX86AsmPrinter() { |
| 994 | RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target); |
| 995 | RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target); |
| 996 | |
| 997 | TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter); |
| 998 | TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter); |
| 999 | } |